1 : /*****************************************************************************
2 : * $Id: GDapi.c 15522 2008-10-13 06:17:52Z dron $
3 : *
4 : * This module has a number of additions and improvements over the original
5 : * implementation to be suitable for usage in GDAL HDF driver.
6 : *
7 : * Andrey Kiselev <dron@ak4719.spb.edu> is responsible for all the changes.
8 : ****************************************************************************/
9 :
10 : /*
11 : Copyright (C) 1996 Hughes and Applied Research Corporation
12 :
13 : Permission to use, modify, and distribute this software and its documentation
14 : for any purpose without fee is hereby granted, provided that the above
15 : copyright notice appear in all copies and that both that copyright notice and
16 : this permission notice appear in supporting documentation.
17 : */
18 : /*****************************************************************************
19 : REVISIONS:
20 :
21 : Aug 31, 1999 Abe Taaheri Changed memory allocation for utility strings to
22 : the size of UTLSTR_MAX_SIZE.
23 : Added error check for memory unavailibilty in
24 : several functions.
25 : Added check for NULL metabuf returned from
26 : EHmeta... functions. NULL pointer retruned from
27 : EHmeta... functions indicate that memory could not
28 : be allocated for metabuf.
29 : Jun 27, 2000 Abe Taaheri Added support for EASE grid that uses
30 : Behrmann Cylinderical Equal Area (BCEA) projection
31 : Oct 23, 2000 Abe Taaheri Updated for ISINUS projection, so that both codes
32 : 31 and 99 can be used for this projection.
33 : Jan 15, 2003 Abe Taaheri Modified for generalization of EASE Grid.
34 :
35 : Jun 05, 2003 Bruce Beaumont / Abe Taaheri
36 :
37 : Fixed SQUARE definition.
38 : Added static projection number/name translation
39 : Added projection table lookup in GDdefproj.
40 : Removed projection table from GDdefproj
41 : Added projection table lookup in GDprojinfo
42 : Removed projection table from GDprojinfo
43 : Added cast for compcode in call to SDsetcompress
44 : in GDdeffield to avoid compiler errors
45 : Removed declaration for unused variable endptr
46 : in GDSDfldsrch
47 : Removed initialization code for unused variables
48 : in GDSDfldsrch
49 : Removed declarations for unused variables
50 : BCEA_scale, r0, s0, xMtr0, xMtr1, yMtr0,
51 : and yMtr1 in GDll2ij
52 : Removed initialization code for unused variables
53 : in GDll2ij
54 : Added code in GEO projection handling to allow
55 : map to span dateline in GDll2ij
56 : Changed "for each point" loop in GDll2ij to
57 : return -2147483648.0 for xVal and yVal if
58 : for_trans returned an error instead of
59 : returning an error to the caller
60 : (Note: MAXLONG is defined as 2147483647.0 in
61 : function cproj.c of GCTP)
62 : Added code in GDij2ll to use for_trans to
63 : translate the BCEA corner points from packed
64 : degrees to meters
65 : Removed declarations for unused variables
66 : BCEA_scale, r0, s0, xMtr, yMtr, epsilon,
67 : beta, qp_cea, kz_cea, eccen, eccen_sq,
68 : phi1, sinphi1, cosphi1, lon, lat, xcor,
69 : ycor, and nlatlon from GDij2ll
70 : Removed initialization code for unused variables
71 : in GDij2ll
72 : Added declarations for xMtr0, yMtr0, xMtr1, and
73 : yMtr1 in GDij2ll
74 : Added special-case code for BCEA
75 : Changed "for each point" loop in GDij2ll to
76 : return PGSd_GCT_IN_ERROR (1.0e51) for
77 : longitude and latitude values if inv_trans
78 : returned an error instead of return an error
79 : to the caller
80 : Removed declaration for unused variable ii in
81 : GDgetpixvalues
82 : Removed declaration for unused variable
83 : numTileDims in GDtileinfo
84 : Added error message and error return at the
85 : end of GDll2mm_cea
86 : Added return statement to GDll2mm_cea
87 : ******************************************************************************/
88 : #include "stdio.h"
89 : #include "mfhdf.h"
90 : #include "hcomp.h"
91 : #include <math.h>
92 : #include "HdfEosDef.h"
93 :
94 : #include "hdf4compat.h"
95 :
96 : extern void for_init(int32, int32, float64 *, int32, char *, char *, int32 *,
97 : int32 (*for_trans[])());
98 : extern void inv_init(int32, int32, float64 *, int32, char *, char *, int32 *,
99 : int32 (*inv_trans[])());
100 :
101 : #define GDIDOFFSET 4194304
102 : #define SQUARE(x) ((x) * (x)) /* x**2 */
103 : #define M_PI1 3.14159265358979323846
104 :
105 : static int32 GDXSDcomb[512*5];
106 : static char GDXSDname[HDFE_NAMBUFSIZE];
107 : static char GDXSDdims[HDFE_DIMBUFSIZE];
108 :
109 :
110 : #define NGRID 200
111 : /* Grid Structure External Arrays */
112 : struct gridStructure
113 : {
114 : int32 active;
115 : int32 IDTable;
116 : int32 VIDTable[2];
117 : int32 fid;
118 : int32 nSDS;
119 : int32 *sdsID;
120 : int32 compcode;
121 : intn compparm[5];
122 : int32 tilecode;
123 : int32 tilerank;
124 : int32 tiledims[8];
125 : };
126 : static struct gridStructure GDXGrid[NGRID];
127 :
128 :
129 :
130 : #define NGRIDREGN 256
131 : struct gridRegion
132 : {
133 : int32 fid;
134 : int32 gridID;
135 : int32 xStart;
136 : int32 xCount;
137 : int32 yStart;
138 : int32 yCount;
139 : int32 somStart;
140 : int32 somCount;
141 : float64 upleftpt[2];
142 : float64 lowrightpt[2];
143 : int32 StartVertical[8];
144 : int32 StopVertical[8];
145 : char *DimNamePtr[8];
146 : };
147 : static struct gridRegion *GDXRegion[NGRIDREGN];
148 :
149 : /* define a macro for the string size of the utility strings and some dimension
150 : list strings. The value of 80 in the previous version of this code
151 : may not be enough in some cases. The length now is 512 which seems to
152 : be more than enough to hold larger strings. */
153 :
154 : #define UTLSTR_MAX_SIZE 512
155 :
156 : /* Static projection table */
157 : static const struct {
158 : int32 projcode;
159 : char *projname;
160 : } Projections[] = {
161 : {GCTP_GEO, "GCTP_GEO"},
162 : {GCTP_UTM, "GCTP_UTM"},
163 : {GCTP_SPCS, "GCTP_SPCS"},
164 : {GCTP_ALBERS, "GCTP_ALBERS"},
165 : {GCTP_LAMCC, "GCTP_LAMCC"},
166 : {GCTP_MERCAT, "GCTP_MERCAT"},
167 : {GCTP_PS, "GCTP_PS"},
168 : {GCTP_POLYC, "GCTP_POLYC"},
169 : {GCTP_EQUIDC, "GCTP_EQUIDC"},
170 : {GCTP_TM, "GCTP_TM"},
171 : {GCTP_STEREO, "GCTP_STEREO"},
172 : {GCTP_LAMAZ, "GCTP_LAMAZ"},
173 : {GCTP_AZMEQD, "GCTP_AZMEQD"},
174 : {GCTP_GNOMON, "GCTP_GNOMON"},
175 : {GCTP_ORTHO, "GCTP_ORTHO"},
176 : {GCTP_GVNSP, "GCTP_GVNSP"},
177 : {GCTP_SNSOID, "GCTP_SNSOID"},
178 : {GCTP_EQRECT, "GCTP_EQRECT"},
179 : {GCTP_MILLER, "GCTP_MILLER"},
180 : {GCTP_VGRINT, "GCTP_VGRINT"},
181 : {GCTP_HOM, "GCTP_HOM"},
182 : {GCTP_ROBIN, "GCTP_ROBIN"},
183 : {GCTP_SOM, "GCTP_SOM"},
184 : {GCTP_ALASKA, "GCTP_ALASKA"},
185 : {GCTP_GOOD, "GCTP_GOOD"},
186 : {GCTP_MOLL, "GCTP_MOLL"},
187 : {GCTP_IMOLL, "GCTP_IMOLL"},
188 : {GCTP_HAMMER, "GCTP_HAMMER"},
189 : {GCTP_WAGIV, "GCTP_WAGIV"},
190 : {GCTP_WAGVII, "GCTP_WAGVII"},
191 : {GCTP_OBLEQA, "GCTP_OBLEQA"},
192 : {GCTP_ISINUS1, "GCTP_ISINUS1"},
193 : {GCTP_CEA, "GCTP_CEA"},
194 : {GCTP_BCEA, "GCTP_BCEA"},
195 : {GCTP_ISINUS, "GCTP_ISINUS"},
196 : {-1, NULL}
197 : };
198 :
199 : /* Compression Codes */
200 : static const char *HDFcomp[] = {
201 : "HDFE_COMP_NONE",
202 : "HDFE_COMP_RLE",
203 : "HDFE_COMP_NBIT",
204 : "HDFE_COMP_SKPHUFF",
205 : "HDFE_COMP_DEFLATE"
206 : };
207 :
208 : /* Origin Codes */
209 : static const char *originNames[] = {
210 : "HDFE_GD_UL",
211 : "HDFE_GD_UR",
212 : "HDFE_GD_LL",
213 : "HDFE_GD_LR"
214 : };
215 :
216 : /* Pixel Registration Codes */
217 : static const char *pixregNames[] = {
218 : "HDFE_CENTER",
219 : "HDFE_CORNER"
220 : };
221 :
222 : /* Grid Function Prototypes (internal routines) */
223 : static intn GDchkgdid(int32, char *, int32 *, int32 *, int32 *);
224 : static intn GDSDfldsrch(int32, int32, const char *, int32 *, int32 *,
225 : int32 *, int32 *, int32 [], int32 *);
226 : static intn GDwrrdfield(int32, char *, char *,
227 : int32 [], int32 [], int32 [], VOIDP datbuf);
228 : static intn GDwrrdattr(int32, char *, int32, int32, char *, VOIDP);
229 : static intn GDll2ij(int32, int32, float64 [], int32, int32, int32, float64[],
230 : float64[], int32, float64[], float64[], int32[], int32[],
231 : float64[], float64[]);
232 : static intn GDgetdefaults(int32, int32, float64[], int32,
233 : float64[], float64[]);
234 : static intn GDtangentpnts(int32, float64[], float64[], float64[], float64[],
235 : float64 [], int32 *);
236 : static intn GDwrrdtile(int32, char *, char *, int32 [], VOIDP);
237 : static intn GDll2mm_cea(int32,int32, int32, float64[], int32, int32,
238 : float64[], float64[], int32, float64[],float64[],
239 : float64[], float64[], float64 *, float64 *);
240 :
241 : static intn GDmm2ll_cea(int32, int32, int32, float64[], int32, int32,
242 : float64[], float64[], int32, float64[], float64[],
243 : float64[], float64[]);
244 :
245 : /*----------------------------------------------------------------------------|
246 : | BEGIN_PROLOG |
247 : | |
248 : | FUNCTION: GDopen |
249 : | |
250 : | DESCRIPTION: Opens or creates HDF file in order to create, read, or write |
251 : | a grid. |
252 : | |
253 : | |
254 : | Return Value Type Units Description |
255 : | ============ ====== ========= ===================================== |
256 : | fid int32 HDF-EOS file ID |
257 : | |
258 : | INPUTS: |
259 : | filename char Filename |
260 : | access intn HDF access code |
261 : | |
262 : | |
263 : | OUTPUTS: |
264 : | None |
265 : | |
266 : | NOTES: |
267 : | |
268 : | |
269 : | Date Programmer Description |
270 : | ====== ============ ================================================= |
271 : | Jun 96 Joel Gales Original Programmer |
272 : | |
273 : | END_PROLOG |
274 : -----------------------------------------------------------------------------*/
275 : int32
276 : GDopen(char *filename, intn access)
277 :
278 10 : {
279 : int32 fid /* HDF-EOS file ID */ ;
280 :
281 : /* Call EHopen to perform file access */
282 : /* ---------------------------------- */
283 10 : fid = EHopen(filename, access);
284 :
285 10 : return (fid);
286 :
287 : }
288 :
289 :
290 :
291 : /*----------------------------------------------------------------------------|
292 : | BEGIN_PROLOG |
293 : | |
294 : | FUNCTION: GDcreate |
295 : | |
296 : | DESCRIPTION: Creates a grid within the file. |
297 : | |
298 : | |
299 : | Return Value Type Units Description |
300 : | ============ ====== ========= ===================================== |
301 : | gridID int32 Grid structure ID |
302 : | |
303 : | INPUTS: |
304 : | fid int32 File ID |
305 : | gridname char Grid structure name |
306 : | xdimsize int32 Number of columns in grid |
307 : | ydimsize int32 Number of rows in grid |
308 : | upleftpt float64 Location (m/deg) of upper left corner |
309 : | lowrightpt float64 Location (m/deg) of lower right corner |
310 : | |
311 : | |
312 : | OUTPUTS: |
313 : | None |
314 : | |
315 : | NOTES: |
316 : | |
317 : | |
318 : | Date Programmer Description |
319 : | ====== ============ ================================================= |
320 : | Jun 96 Joel Gales Original Programmer |
321 : | Aug 96 Joel Gales Make metadata ODL compliant |
322 : | Aug 96 Joel Gales Check grid name for ODL compliance |
323 : | |
324 : | END_PROLOG |
325 : -----------------------------------------------------------------------------*/
326 : int32
327 : GDcreate(int32 fid, char *gridname, int32 xdimsize, int32 ydimsize,
328 : float64 upleftpt[], float64 lowrightpt[])
329 0 : {
330 : intn i; /* Loop index */
331 0 : intn ngridopen = 0; /* # of grid structures open */
332 0 : intn status = 0; /* routine return status variable */
333 :
334 : uint8 access; /* Read/Write file access code */
335 :
336 : int32 HDFfid; /* HDF file id */
337 : int32 vgRef; /* Vgroup reference number */
338 : int32 vgid[3]; /* Vgroup ID array */
339 0 : int32 gridID = -1;/* HDF-EOS grid ID */
340 :
341 : int32 sdInterfaceID; /* HDF SDS interface ID */
342 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
343 0 : int32 nGrid = 0; /* Grid counter */
344 :
345 : char name[80]; /* Vgroup name */
346 : char class[80]; /* Vgroup class */
347 : char errbuf[256];/* Buffer for error message */
348 : char utlbuf[1024]; /* Utility buffer */
349 : char header[128];/* Structural metadata header string */
350 : char footer[256];/* Structural metadata footer string */
351 : char refstr1[128]; /* Upper left ref string (metadata) */
352 : char refstr2[128]; /* Lower right ref string (metadata) */
353 :
354 :
355 : /*
356 : * Check HDF-EOS file ID, get back HDF file ID, SD interface ID and
357 : * access code
358 : */
359 0 : status = EHchkfid(fid, gridname, &HDFfid, &sdInterfaceID, &access);
360 :
361 :
362 : /* Check gridname for length */
363 : /* ------------------------- */
364 0 : if ((intn) strlen(gridname) > VGNAMELENMAX)
365 : {
366 0 : status = -1;
367 0 : HEpush(DFE_GENAPP, "GDcreate", __FILE__, __LINE__);
368 0 : HEreport("Gridname \"%s\" must be less than %d characters.\n",
369 : gridname, VGNAMELENMAX);
370 : }
371 :
372 :
373 :
374 0 : if (status == 0)
375 : {
376 : /* Determine number of grids currently opened */
377 : /* ------------------------------------------- */
378 0 : for (i = 0; i < NGRID; i++)
379 : {
380 0 : ngridopen += GDXGrid[i].active;
381 : }
382 :
383 :
384 : /* Setup file interface */
385 : /* -------------------- */
386 0 : if (ngridopen < NGRID)
387 : {
388 :
389 : /* Check that grid has not been previously opened */
390 : /* ----------------------------------------------- */
391 0 : vgRef = -1;
392 :
393 : while (1)
394 : {
395 0 : vgRef = Vgetid(HDFfid, vgRef);
396 :
397 : /* If no more Vgroups then exist while loop */
398 : /* ---------------------------------------- */
399 0 : if (vgRef == -1)
400 : {
401 0 : break;
402 : }
403 :
404 : /* Get name and class of Vgroup */
405 : /* ---------------------------- */
406 0 : vgid[0] = Vattach(HDFfid, vgRef, "r");
407 0 : Vgetname(vgid[0], name);
408 0 : Vgetclass(vgid[0], class);
409 0 : Vdetach(vgid[0]);
410 :
411 :
412 : /* If GRID then increment # grid counter */
413 : /* ------------------------------------- */
414 0 : if (strcmp(class, "GRID") == 0)
415 : {
416 0 : nGrid++;
417 : }
418 :
419 :
420 : /* If grid already exist, return error */
421 : /* ------------------------------------ */
422 0 : if (strcmp(name, gridname) == 0 &&
423 : strcmp(class, "GRID") == 0)
424 : {
425 0 : status = -1;
426 0 : HEpush(DFE_GENAPP, "GDcreate", __FILE__, __LINE__);
427 0 : HEreport("\"%s\" already exists.\n", gridname);
428 0 : break;
429 : }
430 0 : }
431 :
432 :
433 0 : if (status == 0)
434 : {
435 : /* Create Root Vgroup for Grid */
436 : /* ---------------------------- */
437 0 : vgid[0] = Vattach(HDFfid, -1, "w");
438 :
439 :
440 : /* Set Name and Class (GRID) */
441 : /* -------------------------- */
442 0 : Vsetname(vgid[0], gridname);
443 0 : Vsetclass(vgid[0], "GRID");
444 :
445 :
446 :
447 : /* Create Data Fields Vgroup */
448 : /* ------------------------- */
449 0 : vgid[1] = Vattach(HDFfid, -1, "w");
450 0 : Vsetname(vgid[1], "Data Fields");
451 0 : Vsetclass(vgid[1], "GRID Vgroup");
452 0 : Vinsert(vgid[0], vgid[1]);
453 :
454 :
455 :
456 : /* Create Attributes Vgroup */
457 : /* ------------------------ */
458 0 : vgid[2] = Vattach(HDFfid, -1, "w");
459 0 : Vsetname(vgid[2], "Grid Attributes");
460 0 : Vsetclass(vgid[2], "GRID Vgroup");
461 0 : Vinsert(vgid[0], vgid[2]);
462 :
463 :
464 :
465 : /* Establish Grid in Structural MetaData Block */
466 : /* -------------------------------------------- */
467 0 : sprintf(header, "%s%d%s%s%s%s%d%s%s%d%s",
468 : "\tGROUP=GRID_", (int)(nGrid + 1),
469 : "\n\t\tGridName=\"", gridname, "\"\n",
470 : "\t\tXDim=", (int)xdimsize, "\n",
471 : "\t\tYDim=", (int)ydimsize, "\n");
472 :
473 :
474 0 : sprintf(footer,
475 : "%s%s%s%s%s%s%s%d%s",
476 : "\t\tGROUP=Dimension\n",
477 : "\t\tEND_GROUP=Dimension\n",
478 : "\t\tGROUP=DataField\n",
479 : "\t\tEND_GROUP=DataField\n",
480 : "\t\tGROUP=MergedFields\n",
481 : "\t\tEND_GROUP=MergedFields\n",
482 : "\tEND_GROUP=GRID_", (int)(nGrid + 1), "\n");
483 :
484 :
485 :
486 : /* Build Ref point Col-Row strings */
487 : /* ------------------------------- */
488 0 : if (upleftpt == NULL ||
489 : (upleftpt[0] == 0 && upleftpt[1] == 0 &&
490 : lowrightpt[0] == 0 && lowrightpt[1] == 0))
491 : {
492 0 : strcpy(refstr1, "DEFAULT");
493 0 : strcpy(refstr2, "DEFAULT");
494 : }
495 : else
496 : {
497 0 : sprintf(refstr1, "%s%f%s%f%s",
498 : "(", upleftpt[0], ",", upleftpt[1], ")");
499 :
500 0 : sprintf(refstr2, "%s%f%s%f%s",
501 : "(", lowrightpt[0], ",", lowrightpt[1], ")");
502 : }
503 :
504 0 : sprintf(utlbuf,
505 : "%s%s%s%s%s%s%s%s",
506 : header,
507 : "\t\tUpperLeftPointMtrs=", refstr1, "\n",
508 : "\t\tLowerRightMtrs=", refstr2, "\n",
509 : footer);
510 :
511 0 : status = EHinsertmeta(sdInterfaceID, "", "g", 1002L,
512 : utlbuf, NULL);
513 :
514 : }
515 : }
516 : else
517 : {
518 : /* Too many files opened */
519 : /* --------------------- */
520 0 : status = -1;
521 0 : strcpy(errbuf,
522 : "No more than %d grids may be open simutaneously");
523 0 : strcat(errbuf, " (%s)");
524 0 : HEpush(DFE_DENIED, "GDcreate", __FILE__, __LINE__);
525 0 : HEreport(errbuf, NGRID, gridname);
526 : }
527 :
528 :
529 : /* Assign gridID # & Load grid and GDXGrid.fid table entries */
530 : /* --------------------------------------------------------- */
531 0 : if (status == 0)
532 : {
533 :
534 0 : for (i = 0; i < NGRID; i++)
535 : {
536 0 : if (GDXGrid[i].active == 0)
537 : {
538 0 : gridID = i + idOffset;
539 0 : GDXGrid[i].active = 1;
540 0 : GDXGrid[i].IDTable = vgid[0];
541 0 : GDXGrid[i].VIDTable[0] = vgid[1];
542 0 : GDXGrid[i].VIDTable[1] = vgid[2];
543 0 : GDXGrid[i].fid = fid;
544 0 : status = 0;
545 0 : break;
546 : }
547 : }
548 :
549 : }
550 : }
551 0 : return (gridID);
552 : }
553 :
554 :
555 :
556 : /*----------------------------------------------------------------------------|
557 : | BEGIN_PROLOG |
558 : | |
559 : | FUNCTION: GDattach |
560 : | |
561 : | DESCRIPTION: Attaches to an existing grid within the file. |
562 : | |
563 : | |
564 : | Return Value Type Units Description |
565 : | ============ ====== ========= ===================================== |
566 : | gridID int32 grid structure ID |
567 : | |
568 : | INPUTS: |
569 : | fid int32 HDF-EOS file id |
570 : | gridname char grid sructure name |
571 : | |
572 : | |
573 : | OUTPUTS: |
574 : | None |
575 : | |
576 : | NOTES: |
577 : | |
578 : | |
579 : | Date Programmer Description |
580 : | ====== ============ ================================================= |
581 : | Jun 96 Joel Gales Original Programmer |
582 : | Sep 99 Abe Taaheri Modified test for memory allocation check when no |
583 : | SDSs are in the grid, NCR24147 |
584 : | |
585 : | END_PROLOG |
586 : -----------------------------------------------------------------------------*/
587 : int32
588 : GDattach(int32 fid, char *gridname)
589 :
590 111 : {
591 : intn i; /* Loop index */
592 : intn j; /* Loop index */
593 111 : intn ngridopen = 0; /* # of grid structures open */
594 : intn status; /* routine return status variable */
595 :
596 : uint8 acs; /* Read/Write file access code */
597 :
598 : int32 HDFfid; /* HDF file id */
599 : int32 vgRef; /* Vgroup reference number */
600 : int32 vgid[3]; /* Vgroup ID array */
601 111 : int32 gridID = -1;/* HDF-EOS grid ID */
602 : int32 *tags; /* Pnt to Vgroup object tags array */
603 : int32 *refs; /* Pnt to Vgroup object refs array */
604 : int32 dum; /* dummy varible */
605 : int32 sdInterfaceID; /* HDF SDS interface ID */
606 : int32 nObjects; /* # of objects in Vgroup */
607 : int32 nSDS; /* SDS counter */
608 : int32 index; /* SDS index */
609 : int32 sdid; /* SDS object ID */
610 111 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
611 :
612 : char name[80]; /* Vgroup name */
613 : char class[80]; /* Vgroup class */
614 : char errbuf[256];/* Buffer for error message */
615 : char acsCode[1]; /* Read/Write access char: "r/w" */
616 :
617 :
618 : /* Check HDF-EOS file ID, get back HDF file ID and access code */
619 : /* ----------------------------------------------------------- */
620 111 : status = EHchkfid(fid, gridname, &HDFfid, &dum, &acs);
621 :
622 :
623 111 : if (status == 0)
624 : {
625 : /* Convert numeric access code to character */
626 : /* ---------------------------------------- */
627 :
628 111 : acsCode[0] = (acs == 1) ? 'w' : 'r';
629 :
630 : /* Determine number of grids currently opened */
631 : /* ------------------------------------------- */
632 22311 : for (i = 0; i < NGRID; i++)
633 : {
634 22200 : ngridopen += GDXGrid[i].active;
635 : }
636 :
637 :
638 : /* If room for more ... */
639 : /* -------------------- */
640 111 : if (ngridopen < NGRID)
641 : {
642 :
643 : /* Search Vgroups for Grid */
644 : /* ------------------------ */
645 111 : vgRef = -1;
646 :
647 : while (1)
648 : {
649 111 : vgRef = Vgetid(HDFfid, vgRef);
650 :
651 : /* If no more Vgroups then exist while loop */
652 : /* ---------------------------------------- */
653 111 : if (vgRef == -1)
654 : {
655 0 : break;
656 : }
657 :
658 : /* Get name and class of Vgroup */
659 : /* ---------------------------- */
660 111 : vgid[0] = Vattach(HDFfid, vgRef, "r");
661 111 : Vgetname(vgid[0], name);
662 111 : Vgetclass(vgid[0], class);
663 :
664 :
665 : /*
666 : * If Vgroup with gridname and class GRID found, load tables
667 : */
668 :
669 111 : if (strcmp(name, gridname) == 0 &&
670 : strcmp(class, "GRID") == 0)
671 : {
672 : /* Attach to "Data Fields" and "Grid Attributes" Vgroups */
673 : /* ----------------------------------------------------- */
674 111 : tags = (int32 *) malloc(sizeof(int32) * 2);
675 111 : if(tags == NULL)
676 : {
677 0 : HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__);
678 0 : return(-1);
679 : }
680 111 : refs = (int32 *) malloc(sizeof(int32) * 2);
681 111 : if(refs == NULL)
682 : {
683 0 : HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__);
684 0 : free(tags);
685 0 : return(-1);
686 : }
687 111 : Vgettagrefs(vgid[0], tags, refs, 2);
688 111 : vgid[1] = Vattach(HDFfid, refs[0], acsCode);
689 111 : vgid[2] = Vattach(HDFfid, refs[1], acsCode);
690 111 : free(tags);
691 111 : free(refs);
692 :
693 :
694 : /* Setup External Arrays */
695 : /* --------------------- */
696 111 : for (i = 0; i < NGRID; i++)
697 : {
698 : /* Find empty entry in array */
699 : /* ------------------------- */
700 111 : if (GDXGrid[i].active == 0)
701 : {
702 : /*
703 : * Set gridID, Set grid entry active, Store root
704 : * Vgroup ID, Store sub Vgroup IDs, Store HDF-EOS
705 : * file ID
706 : */
707 111 : gridID = i + idOffset;
708 111 : GDXGrid[i].active = 1;
709 111 : GDXGrid[i].IDTable = vgid[0];
710 111 : GDXGrid[i].VIDTable[0] = vgid[1];
711 111 : GDXGrid[i].VIDTable[1] = vgid[2];
712 111 : GDXGrid[i].fid = fid;
713 111 : break;
714 : }
715 : }
716 :
717 : /* Get SDS interface ID */
718 : /* -------------------- */
719 111 : status = GDchkgdid(gridID, "GDattach", &dum,
720 : &sdInterfaceID, &dum);
721 :
722 :
723 : /* Get # of entries within Data Vgroup & search for SDS */
724 : /* ---------------------------------------------------- */
725 111 : nObjects = Vntagrefs(vgid[1]);
726 :
727 111 : if (nObjects > 0)
728 : {
729 : /* Get tag and ref # for Data Vgroup objects */
730 : /* ----------------------------------------- */
731 111 : tags = (int32 *) malloc(sizeof(int32) * nObjects);
732 111 : if(tags == NULL)
733 : {
734 0 : HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__);
735 0 : return(-1);
736 : }
737 111 : refs = (int32 *) malloc(sizeof(int32) * nObjects);
738 111 : if(refs == NULL)
739 : {
740 0 : HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__);
741 0 : free(tags);
742 0 : return(-1);
743 : }
744 111 : Vgettagrefs(vgid[1], tags, refs, nObjects);
745 :
746 : /* Count number of SDS & allocate SDS ID array */
747 : /* ------------------------------------------- */
748 111 : nSDS = 0;
749 1447 : for (j = 0; j < nObjects; j++)
750 : {
751 1336 : if (tags[j] == DFTAG_NDG)
752 : {
753 1336 : nSDS++;
754 : }
755 : }
756 111 : GDXGrid[i].sdsID = (int32 *) calloc(nSDS, 4);
757 111 : if(GDXGrid[i].sdsID == NULL && nSDS != 0)
758 : {
759 0 : HEpush(DFE_NOSPACE,"GDattach", __FILE__, __LINE__);
760 0 : free(tags);
761 0 : free(refs);
762 0 : return(-1);
763 : }
764 111 : nSDS = 0;
765 :
766 :
767 :
768 : /* Fill SDS ID array */
769 : /* ----------------- */
770 1447 : for (j = 0; j < nObjects; j++)
771 : {
772 : /* If object is SDS then get id */
773 : /* ---------------------------- */
774 1336 : if (tags[j] == DFTAG_NDG)
775 : {
776 1336 : index = SDreftoindex(sdInterfaceID, refs[j]);
777 1336 : sdid = SDselect(sdInterfaceID, index);
778 1336 : GDXGrid[i].sdsID[nSDS] = sdid;
779 1336 : nSDS++;
780 1336 : GDXGrid[i].nSDS++;
781 : }
782 : }
783 111 : free(tags);
784 111 : free(refs);
785 : }
786 111 : break;
787 : }
788 :
789 : /* Detach Vgroup if not desired Grid */
790 : /* --------------------------------- */
791 0 : Vdetach(vgid[0]);
792 0 : }
793 :
794 : /* If Grid not found then set up error message */
795 : /* ------------------------------------------- */
796 111 : if (gridID == -1)
797 : {
798 0 : HEpush(DFE_RANGE, "GDattach", __FILE__, __LINE__);
799 0 : HEreport("Grid: \"%s\" does not exist within HDF file.\n",
800 : gridname);
801 : }
802 : }
803 : else
804 : {
805 : /* Too many files opened */
806 : /* --------------------- */
807 0 : gridID = -1;
808 0 : strcpy(errbuf,
809 : "No more than %d grids may be open simutaneously");
810 0 : strcat(errbuf, " (%s)");
811 0 : HEpush(DFE_DENIED, "GDattach", __FILE__, __LINE__);
812 0 : HEreport(errbuf, NGRID, gridname);
813 : }
814 :
815 : }
816 111 : return (gridID);
817 : }
818 :
819 :
820 : /*----------------------------------------------------------------------------|
821 : | BEGIN_PROLOG |
822 : | |
823 : | FUNCTION: GDchkgdid |
824 : | |
825 : | DESCRIPTION: |
826 : | |
827 : | |
828 : | Return Value Type Units Description |
829 : | ============ ====== ========= ===================================== |
830 : | status intn return status (0) SUCCEED, (-1) FAIL |
831 : | |
832 : | INPUTS: |
833 : | gridID int32 grid structure ID |
834 : | routname char Name of routine calling GDchkgdid |
835 : | |
836 : | OUTPUTS: |
837 : | fid int32 File ID |
838 : | sdInterfaceID int32 SDS interface ID |
839 : | gdVgrpID int32 grid Vgroup ID |
840 : | |
841 : | |
842 : | OUTPUTS: |
843 : | None |
844 : | |
845 : | NOTES: |
846 : | |
847 : | |
848 : | Date Programmer Description |
849 : | ====== ============ ================================================= |
850 : | Jun 96 Joel Gales Original Programmer |
851 : | |
852 : | END_PROLOG |
853 : -----------------------------------------------------------------------------*/
854 : static intn
855 : GDchkgdid(int32 gridID, char *routname,
856 : int32 * fid, int32 * sdInterfaceID, int32 * gdVgrpID)
857 631 : {
858 631 : intn status = 0; /* routine return status variable */
859 : uint8 access; /* Read/Write access code */
860 : int32 gID; /* Grid ID - offset */
861 :
862 631 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
863 :
864 : static const char message1[] =
865 : "Invalid grid id: %d in routine \"%s\". ID must be >= %d and < %d.\n";
866 : static const char message2[] =
867 : "Grid id %d in routine \"%s\" not active.\n";
868 :
869 :
870 :
871 : /* Check for valid grid id */
872 :
873 631 : if (gridID < idOffset || gridID >= NGRID + idOffset)
874 : {
875 0 : status = -1;
876 0 : HEpush(DFE_RANGE, "GDchkgdid", __FILE__, __LINE__);
877 0 : HEreport(message1, gridID, routname, idOffset, NGRID + idOffset);
878 : }
879 : else
880 : {
881 :
882 : /* Compute "reduced" ID */
883 : /* -------------------- */
884 631 : gID = gridID % idOffset;
885 :
886 :
887 : /* Check for active grid ID */
888 : /* ------------------------ */
889 631 : if (GDXGrid[gID].active == 0)
890 : {
891 8 : status = -1;
892 8 : HEpush(DFE_GENAPP, "GDchkgdid", __FILE__, __LINE__);
893 8 : HEreport(message2, gridID, routname);
894 : }
895 : else
896 : {
897 :
898 : /* Get file & SDS ids and Grid key */
899 : /* -------------------------------- */
900 623 : status = EHchkfid(GDXGrid[gID].fid, " ",
901 : fid, sdInterfaceID, &access);
902 623 : *gdVgrpID = GDXGrid[gID].IDTable;
903 : }
904 : }
905 631 : return (status);
906 :
907 : }
908 :
909 :
910 : /*----------------------------------------------------------------------------|
911 : | BEGIN_PROLOG |
912 : | |
913 : | FUNCTION: GDdefdim |
914 : | |
915 : | DESCRIPTION: Defines a new dimension within the grid. |
916 : | |
917 : | |
918 : | Return Value Type Units Description |
919 : | ============ ====== ========= ===================================== |
920 : | status intn return status (0) SUCCEED, (-1) FAIL |
921 : | |
922 : | INPUTS: |
923 : | gridID int32 grid structure ID |
924 : | dimname char Dimension name to define |
925 : | dim int32 Dimemsion value |
926 : | |
927 : | |
928 : | OUTPUTS: |
929 : | None |
930 : | |
931 : | NOTES: |
932 : | |
933 : | |
934 : | Date Programmer Description |
935 : | ====== ============ ================================================= |
936 : | Jun 96 Joel Gales Original Programmer |
937 : | |
938 : | END_PROLOG |
939 : -----------------------------------------------------------------------------*/
940 : intn
941 : GDdefdim(int32 gridID, char *dimname, int32 dim)
942 :
943 0 : {
944 : intn status; /* routine return status variable */
945 :
946 : int32 fid; /* HDF-EOS file id */
947 : int32 sdInterfaceID; /* HDF SDS interface ID */
948 : int32 gdVgrpID; /* Grid root Vgroup ID */
949 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
950 :
951 : char gridname[80] /* Grid name */ ;
952 :
953 :
954 : /* Check for valid grid id */
955 0 : status = GDchkgdid(gridID, "GDdefinedim",
956 : &fid, &sdInterfaceID, &gdVgrpID);
957 :
958 :
959 : /* Make sure dimension >= 0 */
960 : /* ------------------------ */
961 0 : if (dim < 0)
962 : {
963 0 : status = -1;
964 0 : HEpush(DFE_GENAPP, "GDdefdim", __FILE__, __LINE__);
965 0 : HEreport("Dimension value for \"%s\" less than zero: %d.\n",
966 : dimname, dim);
967 : }
968 :
969 :
970 : /* Write Dimension to Structural MetaData */
971 : /* -------------------------------------- */
972 0 : if (status == 0)
973 : {
974 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
975 0 : status = EHinsertmeta(sdInterfaceID, gridname, "g", 0L,
976 : dimname, &dim);
977 : }
978 0 : return (status);
979 :
980 : }
981 :
982 : /*----------------------------------------------------------------------------|
983 : | BEGIN_PROLOG |
984 : | |
985 : | FUNCTION: GDdefproj |
986 : | |
987 : | DESCRIPTION: Defines projection of grid. |
988 : | |
989 : | |
990 : | Return Value Type Units Description |
991 : | ============ ====== ========= ===================================== |
992 : | status intn return status (0) SUCCEED, (-1) FAIL |
993 : | |
994 : | INPUTS: |
995 : | gridID int32 Grid structure ID |
996 : | projcode int32 GCTP projection code |
997 : | zonecode int32 UTM zone code |
998 : | spherecode int32 GCTP spheriod code |
999 : | projparm float64 Projection parameters |
1000 : | |
1001 : | OUTPUTS: |
1002 : | None |
1003 : | |
1004 : | NOTES: |
1005 : | |
1006 : | |
1007 : | Date Programmer Description |
1008 : | ====== ============ ================================================= |
1009 : | Jun 96 Joel Gales Original Programmer |
1010 : | Jun 00 Abe Taaheri Added support for EASE grid |
1011 : | |
1012 : | END_PROLOG |
1013 : -----------------------------------------------------------------------------*/
1014 : intn
1015 : GDdefproj(int32 gridID, int32 projcode, int32 zonecode, int32 spherecode,
1016 : float64 projparm[])
1017 0 : {
1018 : intn i; /* Loop index */
1019 : intn projx; /* Projection table index */
1020 0 : intn status = 0; /* routine return status variable */
1021 :
1022 : int32 fid; /* HDF-EOS file ID */
1023 : int32 sdInterfaceID; /* HDF SDS interface ID */
1024 : int32 gdVgrpID; /* Grid root Vgroup ID */
1025 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1026 : int32 slen; /* String length */
1027 : float64 EHconvAng();
1028 : char utlbuf[1024]; /* Utility Buffer */
1029 : char projparmbuf[512]; /* Projection parameter metadata
1030 : * string */
1031 : char gridname[80]; /* Grid Name */
1032 :
1033 :
1034 : /* Check for valid grid id */
1035 : /* ----------------------- */
1036 0 : status = GDchkgdid(gridID, "GDdefproj", &fid, &sdInterfaceID, &gdVgrpID);
1037 :
1038 0 : if (status == 0)
1039 : {
1040 : /*
1041 : * If projection not GEO, UTM, or State Code build projection
1042 : * parameter string
1043 : */
1044 0 : if (projcode != GCTP_GEO &&
1045 : projcode != GCTP_UTM &&
1046 : projcode != GCTP_SPCS)
1047 : {
1048 :
1049 : /* Begin projection parameter list with "(" */
1050 0 : strcpy(projparmbuf, "(");
1051 :
1052 0 : for (i = 0; i < 13; i++)
1053 : {
1054 : /* If projparm[i] = 0 ... */
1055 0 : if (projparm[i] == 0.0)
1056 : {
1057 0 : strcpy(utlbuf, "0,");
1058 : }
1059 : else
1060 : {
1061 : /* if projparm[i] is integer ... */
1062 0 : if ((int32) projparm[i] == projparm[i])
1063 : {
1064 0 : sprintf(utlbuf, "%d%s",
1065 : (int) projparm[i], ",");
1066 : }
1067 : /* else projparm[i] is non-zero floating point ... */
1068 : else
1069 : {
1070 0 : sprintf(utlbuf, "%f%s", projparm[i], ",");
1071 : }
1072 : }
1073 0 : strcat(projparmbuf, utlbuf);
1074 : }
1075 0 : slen = strlen(projparmbuf);
1076 :
1077 : /* Add trailing ")" */
1078 0 : projparmbuf[slen - 1] = ')';
1079 : }
1080 :
1081 0 : for (projx = 0; Projections[projx].projcode != -1; projx++)
1082 : {
1083 0 : if (projcode == Projections[projx].projcode)
1084 : {
1085 0 : break;
1086 : }
1087 : }
1088 :
1089 :
1090 : /* Build metadata string */
1091 : /* --------------------- */
1092 0 : if ((projcode == GCTP_GEO))
1093 : {
1094 0 : sprintf(utlbuf,
1095 : "%s%s%s",
1096 : "\t\tProjection=", Projections[projx].projname, "\n");
1097 : }
1098 0 : else if (projcode == GCTP_UTM || projcode == GCTP_SPCS)
1099 : {
1100 0 : sprintf(utlbuf,
1101 : "%s%s%s%s%d%s%s%d%s",
1102 : "\t\tProjection=", Projections[projx].projname, "\n",
1103 : "\t\tZoneCode=", (int)zonecode, "\n",
1104 : "\t\tSphereCode=", (int)spherecode, "\n");
1105 : }
1106 : else
1107 : {
1108 0 : sprintf(utlbuf,
1109 : "%s%s%s%s%s%s%s%d%s",
1110 : "\t\tProjection=", Projections[projx].projname, "\n",
1111 : "\t\tProjParams=", projparmbuf, "\n",
1112 : "\t\tSphereCode=", (int)spherecode, "\n");
1113 : }
1114 :
1115 :
1116 : /* Insert in structural metadata */
1117 : /* ----------------------------- */
1118 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
1119 0 : status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L,
1120 : utlbuf, NULL);
1121 : }
1122 :
1123 0 : return (status);
1124 : }
1125 :
1126 :
1127 :
1128 : /*----------------------------------------------------------------------------|
1129 : | BEGIN_PROLOG |
1130 : | |
1131 : | FUNCTION: GDblkSOMoffset |
1132 : | |
1133 : | DESCRIPTION: Writes Block SOM offset values |
1134 : | |
1135 : | |
1136 : | Return Value Type Units Description |
1137 : | ============ ====== ========= ===================================== |
1138 : | status intn return status (0) SUCCEED, (-1) FAIL |
1139 : | |
1140 : | INPUTS: |
1141 : | gridID int32 Grid structure ID |
1142 : | offset float32 Offset values |
1143 : | count int32 Number of offset values |
1144 : | code char w/r code (w/r) |
1145 : | |
1146 : | OUTPUTS: |
1147 : | None |
1148 : | |
1149 : | NOTES: |
1150 : | |
1151 : | |
1152 : | Date Programmer Description |
1153 : | ====== ============ ================================================= |
1154 : | Sep 96 Joel Gales Original Programmer |
1155 : | Mar 99 David Wynne Changed data type of offset array from int32 to |
1156 : | float32, NCR 21197 |
1157 : | |
1158 : | END_PROLOG |
1159 : -----------------------------------------------------------------------------*/
1160 : intn
1161 : GDblkSOMoffset(int32 gridID, float32 offset[], int32 count, char *code)
1162 0 : {
1163 0 : intn status = 0; /* routine return status variable */
1164 :
1165 : int32 fid; /* HDF-EOS file ID */
1166 : int32 sdInterfaceID; /* HDF SDS interface ID */
1167 : int32 gdVgrpID; /* Grid root Vgroup ID */
1168 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1169 : int32 projcode; /* GCTP projection code */
1170 :
1171 : float64 projparm[13]; /* Projection parameters */
1172 :
1173 : char utlbuf[128];/* Utility Buffer */
1174 : char gridname[80]; /* Grid Name */
1175 :
1176 : /* Check for valid grid id */
1177 0 : status = GDchkgdid(gridID, "GDblkSOMoffset",
1178 : &fid, &sdInterfaceID, &gdVgrpID);
1179 :
1180 0 : if (status == 0)
1181 : {
1182 : /* Get projection parameters */
1183 0 : status = GDprojinfo(gridID, &projcode, NULL, NULL, projparm);
1184 :
1185 : /* If SOM projection with projparm[11] non-zero ... */
1186 0 : if (projcode == GCTP_SOM && projparm[11] != 0)
1187 : {
1188 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
1189 0 : sprintf(utlbuf, "%s%s", "_BLKSOM:", gridname);
1190 :
1191 : /* Write offset values as attribute */
1192 0 : if (strcmp(code, "w") == 0)
1193 : {
1194 0 : status = GDwriteattr(gridID, utlbuf, DFNT_FLOAT32,
1195 : count, offset);
1196 : }
1197 : /* Read offset values from attribute */
1198 0 : else if (strcmp(code, "r") == 0)
1199 : {
1200 0 : status = GDreadattr(gridID, utlbuf, offset);
1201 : }
1202 : }
1203 : }
1204 0 : return (status);
1205 : }
1206 :
1207 :
1208 : /*----------------------------------------------------------------------------|
1209 : | BEGIN_PROLOG |
1210 : | |
1211 : | FUNCTION: GDdefcomp |
1212 : | |
1213 : | DESCRIPTION: Defines compression type and parameters |
1214 : | |
1215 : | |
1216 : | Return Value Type Units Description |
1217 : | ============ ====== ========= ===================================== |
1218 : | status intn return status (0) SUCCEED, (-1) FAIL |
1219 : | |
1220 : | INPUTS: |
1221 : | gridID int32 grid structure ID |
1222 : | compcode int32 compression code |
1223 : | compparm intn compression parameters |
1224 : | |
1225 : | OUTPUTS: |
1226 : | None |
1227 : | |
1228 : | NOTES: |
1229 : | |
1230 : | |
1231 : | Date Programmer Description |
1232 : | ====== ============ ================================================= |
1233 : | Sep 96 Joel Gales Original Programmer |
1234 : | |
1235 : | END_PROLOG |
1236 : -----------------------------------------------------------------------------*/
1237 : intn
1238 : GDdefcomp(int32 gridID, int32 compcode, intn compparm[])
1239 0 : {
1240 0 : intn status = 0; /* routine return status variable */
1241 :
1242 : int32 fid; /* HDF-EOS file id */
1243 : int32 sdInterfaceID; /* HDF SDS interface ID */
1244 : int32 gdVgrpID; /* Grid root Vgroup ID */
1245 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1246 : int32 gID; /* gridID - offset */
1247 :
1248 : /* Check for valid grid id */
1249 0 : status = GDchkgdid(gridID, "GDdefcomp", &fid, &sdInterfaceID, &gdVgrpID);
1250 :
1251 0 : if (status == 0)
1252 : {
1253 0 : gID = gridID % idOffset;
1254 :
1255 : /* Set compression code in compression exteral array */
1256 0 : GDXGrid[gID].compcode = compcode;
1257 :
1258 0 : switch (compcode)
1259 : {
1260 : /* Set NBIT compression parameters in compression external array */
1261 : case HDFE_COMP_NBIT:
1262 :
1263 0 : GDXGrid[gID].compparm[0] = compparm[0];
1264 0 : GDXGrid[gID].compparm[1] = compparm[1];
1265 0 : GDXGrid[gID].compparm[2] = compparm[2];
1266 0 : GDXGrid[gID].compparm[3] = compparm[3];
1267 :
1268 0 : break;
1269 :
1270 : /* Set GZIP compression parameter in compression external array */
1271 : case HDFE_COMP_DEFLATE:
1272 :
1273 0 : GDXGrid[gID].compparm[0] = compparm[0];
1274 :
1275 : break;
1276 :
1277 : }
1278 : }
1279 :
1280 0 : return (status);
1281 : }
1282 :
1283 :
1284 :
1285 :
1286 : /*----------------------------------------------------------------------------|
1287 : | BEGIN_PROLOG |
1288 : | |
1289 : | FUNCTION: GDdeftile |
1290 : | |
1291 : | DESCRIPTION: Defines tiling parameters |
1292 : | |
1293 : | |
1294 : | Return Value Type Units Description |
1295 : | ============ ====== ========= ===================================== |
1296 : | status intn return status (0) SUCCEED, (-1) FAIL |
1297 : | |
1298 : | INPUTS: |
1299 : | gridID int32 grid structure ID |
1300 : | tilecode int32 tile code |
1301 : | tilerank int32 number of tiling dimensions |
1302 : | tiledims int32 tiling dimensions |
1303 : | |
1304 : | OUTPUTS: |
1305 : | None |
1306 : | |
1307 : | NOTES: |
1308 : | |
1309 : | |
1310 : | Date Programmer Description |
1311 : | ====== ============ ================================================= |
1312 : | Jan 97 Joel Gales Original Programmer |
1313 : | |
1314 : | END_PROLOG |
1315 : -----------------------------------------------------------------------------*/
1316 : intn
1317 : GDdeftile(int32 gridID, int32 tilecode, int32 tilerank, int32 tiledims[])
1318 0 : {
1319 : intn i; /* Loop index */
1320 0 : intn status = 0; /* routine return status variable */
1321 :
1322 : int32 fid; /* HDF-EOS file id */
1323 : int32 sdInterfaceID; /* HDF SDS interface ID */
1324 : int32 gdVgrpID; /* Grid root Vgroup ID */
1325 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1326 : int32 gID; /* gridID - offset */
1327 :
1328 : /* Check for valid grid id */
1329 : /* ----------------------- */
1330 0 : status = GDchkgdid(gridID, "GDdeftile", &fid, &sdInterfaceID, &gdVgrpID);
1331 :
1332 :
1333 0 : if (status == 0)
1334 : {
1335 0 : gID = gridID % idOffset;
1336 :
1337 0 : for (i = 0; i < 8; i++)
1338 : {
1339 0 : GDXGrid[gID].tiledims[i] = 0;
1340 : }
1341 :
1342 0 : GDXGrid[gID].tilecode = tilecode;
1343 :
1344 0 : switch (tilecode)
1345 : {
1346 : case HDFE_NOTILE:
1347 :
1348 0 : GDXGrid[gID].tilerank = 0;
1349 :
1350 0 : break;
1351 :
1352 :
1353 : case HDFE_TILE:
1354 :
1355 0 : GDXGrid[gID].tilerank = tilerank;
1356 :
1357 0 : for (i = 0; i < tilerank; i++)
1358 : {
1359 0 : GDXGrid[gID].tiledims[i] = tiledims[i];
1360 :
1361 0 : if (GDXGrid[gID].tiledims[i] == 0)
1362 : {
1363 0 : GDXGrid[gID].tiledims[i] = 1;
1364 : }
1365 : }
1366 :
1367 : break;
1368 :
1369 : }
1370 : }
1371 :
1372 0 : return (status);
1373 : }
1374 :
1375 :
1376 :
1377 :
1378 : /*----------------------------------------------------------------------------|
1379 : | BEGIN_PROLOG |
1380 : | |
1381 : | FUNCTION: GDdeforigin |
1382 : | |
1383 : | DESCRIPTION: Defines the origin of the grid data. |
1384 : | |
1385 : | |
1386 : | Return Value Type Units Description |
1387 : | ============ ====== ========= ===================================== |
1388 : | status intn return status (0) SUCCEED, (-1) FAIL |
1389 : | |
1390 : | INPUTS: |
1391 : | gridID int32 grid structure ID |
1392 : | origincode int32 origin code |
1393 : | HDFE_GD_UL (0) |
1394 : | HDFE_GD_UR (1) |
1395 : | HDFE_GD_LL (2) |
1396 : | HDFE_GD_LR (3) |
1397 : | |
1398 : | |
1399 : | OUTPUTS: |
1400 : | None |
1401 : | |
1402 : | NOTES: |
1403 : | |
1404 : | |
1405 : | Date Programmer Description |
1406 : | ====== ============ ================================================= |
1407 : | Jun 96 Joel Gales Original Programmer |
1408 : | |
1409 : | END_PROLOG |
1410 : -----------------------------------------------------------------------------*/
1411 : intn
1412 : GDdeforigin(int32 gridID, int32 origincode)
1413 0 : {
1414 0 : intn status = 0; /* routine return status variable */
1415 :
1416 : int32 fid; /* HDF-EOS file ID */
1417 : int32 gdVgrpID; /* Grid root Vgroup ID */
1418 : int32 sdInterfaceID; /* HDF SDS interface ID */
1419 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1420 :
1421 : char utlbuf[64]; /* Utility buffer */
1422 : char gridname[80]; /* Grid name */
1423 :
1424 :
1425 : /* Check for valid grid id */
1426 0 : status = GDchkgdid(gridID, "GDdeforigin",
1427 : &fid, &sdInterfaceID, &gdVgrpID);
1428 :
1429 0 : if (status == 0)
1430 : {
1431 : /* If proper origin code then write to structural metadata */
1432 : /* ------------------------------------------------------- */
1433 0 : if (origincode >= 0 && origincode < sizeof(originNames))
1434 : {
1435 0 : sprintf(utlbuf, "%s%s%s",
1436 : "\t\tGridOrigin=", originNames[origincode], "\n");
1437 :
1438 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
1439 0 : status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L,
1440 : utlbuf, NULL);
1441 : }
1442 : else
1443 : {
1444 0 : status = -1;
1445 0 : HEpush(DFE_GENAPP, "GDdeforigin", __FILE__, __LINE__);
1446 0 : HEreport("Improper Grid Origin code: %d\n", origincode);
1447 : }
1448 : }
1449 :
1450 0 : return (status);
1451 : }
1452 :
1453 : /*----------------------------------------------------------------------------|
1454 : | BEGIN_PROLOG |
1455 : | |
1456 : | FUNCTION: GDdefpixreg |
1457 : | |
1458 : | DESCRIPTION: Defines pixel registration within grid cell. |
1459 : | |
1460 : | |
1461 : | Return Value Type Units Description |
1462 : | ============ ====== ========= ===================================== |
1463 : | status intn return status (0) SUCCEED, (-1) FAIL |
1464 : | |
1465 : | INPUTS: |
1466 : | gridID int32 grid structure ID |
1467 : | pixregcode int32 Pixel registration code |
1468 : | HDFE_CENTER (0) |
1469 : | HDFE_CORNER (1) |
1470 : | |
1471 : | |
1472 : | OUTPUTS: |
1473 : | None |
1474 : | |
1475 : | NOTES: |
1476 : | |
1477 : | |
1478 : | Date Programmer Description |
1479 : | ====== ============ ================================================= |
1480 : | Jun 96 Joel Gales Original Programmer |
1481 : | |
1482 : | END_PROLOG |
1483 : -----------------------------------------------------------------------------*/
1484 : intn
1485 : GDdefpixreg(int32 gridID, int32 pixregcode)
1486 0 : {
1487 0 : intn status = 0; /* routine return status variable */
1488 :
1489 : int32 fid; /* HDF-EOS file ID */
1490 : int32 gdVgrpID; /* Grid root Vgroup ID */
1491 : int32 sdInterfaceID; /* HDF SDS interface ID */
1492 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1493 :
1494 : char utlbuf[64]; /* Utility buffer */
1495 : char gridname[80]; /* Grid name */
1496 :
1497 : /* Check for valid grid id */
1498 0 : status = GDchkgdid(gridID, "GDdefpixreg",
1499 : &fid, &sdInterfaceID, &gdVgrpID);
1500 :
1501 0 : if (status == 0)
1502 : {
1503 : /* If proper pix reg code then write to structural metadata */
1504 : /* -------------------------------------------------------- */
1505 0 : if (pixregcode >= 0 && pixregcode < sizeof(pixregNames))
1506 : {
1507 0 : sprintf(utlbuf, "%s%s%s",
1508 : "\t\tPixelRegistration=", pixregNames[pixregcode], "\n");
1509 :
1510 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
1511 0 : status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L,
1512 : utlbuf, NULL);
1513 : }
1514 : else
1515 : {
1516 0 : status = -1;
1517 0 : HEpush(DFE_GENAPP, "GDdefpixreg", __FILE__, __LINE__);
1518 0 : HEreport("Improper Pixel Registration code: %d\n", pixregcode);
1519 : }
1520 : }
1521 :
1522 0 : return (status);
1523 :
1524 : }
1525 :
1526 :
1527 :
1528 :
1529 :
1530 : /*----------------------------------------------------------------------------|
1531 : | BEGIN_PROLOG |
1532 : | |
1533 : | FUNCTION: GDdiminfo |
1534 : | |
1535 : | DESCRIPTION: Retrieve size of specified dimension. |
1536 : | |
1537 : | |
1538 : | Return Value Type Units Description |
1539 : | ============ ====== ========= ===================================== |
1540 : | size int32 Size of dimension |
1541 : | |
1542 : | INPUTS: |
1543 : | gridID int32 grid structure id |
1544 : | dimname char Dimension name |
1545 : | |
1546 : | |
1547 : | OUTPUTS: |
1548 : | None |
1549 : | |
1550 : | NOTES: |
1551 : | |
1552 : | |
1553 : | Date Programmer Description |
1554 : | ====== ============ ================================================= |
1555 : | Jun 96 Joel Gales Original Programmer |
1556 : | Aug 96 Joel Gales Make metadata ODL compliant |
1557 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
1558 : | |
1559 : | END_PROLOG |
1560 : -----------------------------------------------------------------------------*/
1561 : int32
1562 : GDdiminfo(int32 gridID, char *dimname)
1563 :
1564 0 : {
1565 : intn status; /* routine return status variable */
1566 :
1567 : int32 fid; /* HDF-EOS file ID */
1568 : int32 sdInterfaceID; /* HDF SDS interface ID */
1569 : int32 gdVgrpID; /* Grid root Vgroup ID */
1570 : int32 size; /* Dimension size */
1571 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1572 :
1573 :
1574 : char *metabuf; /* Pointer to structural metadata (SM) */
1575 : char *metaptrs[2];/* Pointers to begin and end of SM section */
1576 : char gridname[80]; /* Grid Name */
1577 : char *utlstr; /* Utility string */
1578 :
1579 : /* Allocate space for utility string */
1580 : /* --------------------------------- */
1581 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
1582 0 : if(utlstr == NULL)
1583 : {
1584 0 : HEpush(DFE_NOSPACE,"GDdiminfo", __FILE__, __LINE__);
1585 0 : return(-1);
1586 : }
1587 : /* Initialize return value */
1588 : /* ----------------------- */
1589 0 : size = -1;
1590 :
1591 :
1592 : /* Check Grid ID */
1593 : /* ------------- */
1594 0 : status = GDchkgdid(gridID, "GDdiminfo", &fid, &sdInterfaceID, &gdVgrpID);
1595 :
1596 :
1597 0 : if (status == 0)
1598 : {
1599 : /* Get grid name */
1600 : /* ------------- */
1601 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
1602 :
1603 :
1604 : /* Get pointers to "Dimension" section within SM */
1605 : /* --------------------------------------------- */
1606 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
1607 : "Dimension", metaptrs);
1608 :
1609 0 : if(metabuf == NULL)
1610 : {
1611 0 : free(utlstr);
1612 0 : return(-1);
1613 : }
1614 :
1615 : /* Search for dimension name (surrounded by quotes) */
1616 : /* ------------------------------------------------ */
1617 0 : sprintf(utlstr, "%s%s%s", "\"", dimname, "\"\n");
1618 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
1619 :
1620 : /*
1621 : * If dimension found within grid structure then get dimension value
1622 : */
1623 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
1624 : {
1625 : /* Set endptr at end of dimension definition entry */
1626 : /* ----------------------------------------------- */
1627 0 : metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
1628 :
1629 0 : status = EHgetmetavalue(metaptrs, "Size", utlstr);
1630 :
1631 0 : if (status == 0)
1632 : {
1633 0 : size = atol(utlstr);
1634 : }
1635 : else
1636 : {
1637 0 : HEpush(DFE_GENAPP, "GDdiminfo", __FILE__, __LINE__);
1638 0 : HEreport("\"Size\" string not found in metadata.\n");
1639 : }
1640 : }
1641 : else
1642 : {
1643 0 : HEpush(DFE_GENAPP, "GDdiminfo", __FILE__, __LINE__);
1644 0 : HEreport("Dimension \"%s\" not found.\n", dimname);
1645 : }
1646 :
1647 0 : free(metabuf);
1648 : }
1649 0 : free(utlstr);
1650 0 : return (size);
1651 : }
1652 :
1653 :
1654 :
1655 :
1656 :
1657 : /*----------------------------------------------------------------------------|
1658 : | BEGIN_PROLOG |
1659 : | |
1660 : | FUNCTION: GDgridinfo |
1661 : | |
1662 : | DESCRIPTION: Returns xdim, ydim and location of upper left and lower |
1663 : | right corners, in meters. |
1664 : | |
1665 : | |
1666 : | Return Value Type Units Description |
1667 : | ============ ====== ========= ===================================== |
1668 : | status intn return status (0) SUCCEED, (-1) FAIL |
1669 : | |
1670 : | INPUTS: |
1671 : | fid int32 File ID |
1672 : | gridname char Grid structure name |
1673 : | |
1674 : | OUTPUTS: |
1675 : | xdimsize int32 Number of columns in grid |
1676 : | ydimsize int32 Number of rows in grid |
1677 : | upleftpt float64 Location (m/deg) of upper left corner |
1678 : | lowrightpt float64 Location (m/deg) of lower right corner |
1679 : | |
1680 : | NOTES: |
1681 : | |
1682 : | |
1683 : | Date Programmer Description |
1684 : | ====== ============ ================================================= |
1685 : | Jun 96 Joel Gales Original Programmer |
1686 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
1687 : | |
1688 : | END_PROLOG |
1689 : -----------------------------------------------------------------------------*/
1690 : intn
1691 : GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize,
1692 : float64 upleftpt[], float64 lowrightpt[])
1693 :
1694 133 : {
1695 133 : intn status = 0; /* routine return status variable */
1696 133 : intn statmeta = 0; /* EHgetmetavalue return status */
1697 :
1698 : int32 fid; /* HDF-EOS file ID */
1699 : int32 sdInterfaceID; /* HDF SDS interface ID */
1700 : int32 gdVgrpID; /* Grid root Vgroup ID */
1701 133 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1702 :
1703 :
1704 : char *metabuf; /* Pointer to structural metadata (SM) */
1705 : char *metaptrs[2];/* Pointers to begin and end of SM section */
1706 : char gridname[80]; /* Grid Name */
1707 : char *utlstr; /* Utility string */
1708 :
1709 : /* Allocate space for utility string */
1710 : /* --------------------------------- */
1711 133 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
1712 133 : if(utlstr == NULL)
1713 : {
1714 0 : HEpush(DFE_NOSPACE,"GDgridinfo", __FILE__, __LINE__);
1715 0 : return(-1);
1716 : }
1717 : /* Check Grid ID */
1718 : /* ------------- */
1719 133 : status = GDchkgdid(gridID, "GDgridinfo", &fid, &sdInterfaceID, &gdVgrpID);
1720 :
1721 133 : if (status == 0)
1722 : {
1723 : /* Get grid name */
1724 : /* ------------- */
1725 133 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
1726 :
1727 :
1728 : /* Get pointers to grid structure section within SM */
1729 : /* ------------------------------------------------ */
1730 133 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
1731 : NULL, metaptrs);
1732 :
1733 133 : if(metabuf == NULL)
1734 : {
1735 0 : free(utlstr);
1736 0 : return(-1);
1737 : }
1738 :
1739 :
1740 : /* Get xdimsize if requested */
1741 : /* ------------------------- */
1742 133 : if (xdimsize != NULL)
1743 : {
1744 133 : statmeta = EHgetmetavalue(metaptrs, "XDim", utlstr);
1745 133 : if (statmeta == 0)
1746 : {
1747 133 : *xdimsize = atol(utlstr);
1748 : }
1749 : else
1750 : {
1751 0 : status = -1;
1752 0 : HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__);
1753 0 : HEreport("\"XDim\" string not found in metadata.\n");
1754 : }
1755 : }
1756 :
1757 :
1758 : /* Get ydimsize if requested */
1759 : /* ------------------------- */
1760 133 : if (ydimsize != NULL)
1761 : {
1762 133 : statmeta = EHgetmetavalue(metaptrs, "YDim", utlstr);
1763 133 : if (statmeta == 0)
1764 : {
1765 133 : *ydimsize = atol(utlstr);
1766 : }
1767 : else
1768 : {
1769 0 : status = -1;
1770 0 : HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__);
1771 0 : HEreport("\"YDim\" string not found in metadata.\n");
1772 : }
1773 : }
1774 :
1775 :
1776 : /* Get upleftpt if requested */
1777 : /* ------------------------- */
1778 133 : if (upleftpt != NULL)
1779 : {
1780 8 : statmeta = EHgetmetavalue(metaptrs, "UpperLeftPointMtrs", utlstr);
1781 8 : if (statmeta == 0)
1782 : {
1783 : /* If value is "DEFAULT" then return zeros */
1784 : /* --------------------------------------- */
1785 8 : if (strcmp(utlstr, "DEFAULT") == 0)
1786 : {
1787 0 : upleftpt[0] = 0;
1788 0 : upleftpt[1] = 0;
1789 : }
1790 : else
1791 : {
1792 8 : sscanf(utlstr, "(%lf,%lf)",
1793 : &upleftpt[0], &upleftpt[1]);
1794 : }
1795 : }
1796 : else
1797 : {
1798 0 : status = -1;
1799 0 : HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__);
1800 0 : HEreport(
1801 : "\"UpperLeftPointMtrs\" string not found in metadata.\n");
1802 : }
1803 :
1804 : }
1805 :
1806 : /* Get lowrightpt if requested */
1807 : /* --------------------------- */
1808 133 : if (lowrightpt != NULL)
1809 : {
1810 8 : statmeta = EHgetmetavalue(metaptrs, "LowerRightMtrs", utlstr);
1811 8 : if (statmeta == 0)
1812 : {
1813 : /* If value is "DEFAULT" then return zeros */
1814 8 : if (strcmp(utlstr, "DEFAULT") == 0)
1815 : {
1816 0 : lowrightpt[0] = 0;
1817 0 : lowrightpt[1] = 0;
1818 : }
1819 : else
1820 : {
1821 8 : sscanf(utlstr, "(%lf,%lf)",
1822 : &lowrightpt[0], &lowrightpt[1]);
1823 : }
1824 : }
1825 : else
1826 : {
1827 0 : status = -1;
1828 0 : HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__);
1829 0 : HEreport(
1830 : "\"LowerRightMtrs\" string not found in metadata.\n");
1831 : }
1832 : }
1833 :
1834 133 : free(metabuf);
1835 : }
1836 133 : free(utlstr);
1837 133 : return (status);
1838 : }
1839 :
1840 :
1841 :
1842 :
1843 :
1844 :
1845 :
1846 : /*----------------------------------------------------------------------------|
1847 : | BEGIN_PROLOG |
1848 : | |
1849 : | FUNCTION: GDprojinfo |
1850 : | |
1851 : | DESCRIPTION: Returns GCTP projection code, zone code, spheroid code |
1852 : | and projection parameters. |
1853 : | |
1854 : | |
1855 : | Return Value Type Units Description |
1856 : | ============ ====== ========= ===================================== |
1857 : | status intn return status (0) SUCCEED, (-1) FAIL |
1858 : | |
1859 : | INPUTS: |
1860 : | gridID int32 Grid structure ID |
1861 : | |
1862 : | OUTPUTS: |
1863 : | projcode int32 GCTP projection code |
1864 : | zonecode int32 UTM zone code |
1865 : | spherecode int32 GCTP spheriod code |
1866 : | projparm float64 Projection parameters |
1867 : | |
1868 : | NOTES: |
1869 : | |
1870 : | |
1871 : | Date Programmer Description |
1872 : | ====== ============ ================================================= |
1873 : | Jun 96 Joel Gales Original Programmer |
1874 : | Oct 96 Joel Gales Add check for no projection code |
1875 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
1876 : | Jun 00 Abe Taaheri Added support for EASE grid |
1877 : | |
1878 : | END_PROLOG |
1879 : -----------------------------------------------------------------------------*/
1880 : intn
1881 : GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode,
1882 : int32 * spherecode, float64 projparm[])
1883 :
1884 8 : {
1885 : intn i; /* Loop index */
1886 : intn projx; /* Loop index */
1887 8 : intn status = 0; /* routine return status variable */
1888 8 : intn statmeta = 0; /* EHgetmetavalue return status */
1889 :
1890 : int32 fid; /* HDF-EOS file ID */
1891 : int32 sdInterfaceID; /* HDF SDS interface ID */
1892 : int32 gdVgrpID; /* Grid root Vgroup ID */
1893 8 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
1894 :
1895 :
1896 : char *metabuf; /* Pointer to structural metadata (SM) */
1897 : char *metaptrs[2];/* Pointers to begin and end of SM section */
1898 : char gridname[80]; /* Grid Name */
1899 : char *utlstr; /* Utility string */
1900 : char fmt[96]; /* Format String */
1901 :
1902 : /* Allocate space for utility string */
1903 : /* --------------------------------- */
1904 8 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
1905 8 : if(utlstr == NULL)
1906 : {
1907 0 : HEpush(DFE_NOSPACE,"GDprojinfo", __FILE__, __LINE__);
1908 0 : return(-1);
1909 : }
1910 :
1911 : /* Check Grid ID */
1912 : /* ------------- */
1913 8 : status = GDchkgdid(gridID, "GDprojinfo", &fid, &sdInterfaceID, &gdVgrpID);
1914 :
1915 8 : if (status == 0)
1916 : {
1917 : /* Get grid name */
1918 : /* ------------- */
1919 8 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
1920 :
1921 :
1922 : /* Get pointers to grid structure section within SM */
1923 : /* ------------------------------------------------ */
1924 8 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
1925 : NULL, metaptrs);
1926 :
1927 8 : if(metabuf == NULL)
1928 : {
1929 0 : free(utlstr);
1930 0 : return(-1);
1931 : }
1932 :
1933 :
1934 : /* Get projcode if requested */
1935 : /* ------------------------- */
1936 8 : if (projcode != NULL)
1937 : {
1938 8 : *projcode = -1;
1939 :
1940 8 : statmeta = EHgetmetavalue(metaptrs, "Projection", utlstr);
1941 8 : if (statmeta == 0)
1942 : {
1943 : /* Loop through projection codes until found */
1944 : /* ----------------------------------------- */
1945 104 : for (projx = 0; Projections[projx].projcode != -1; projx++)
1946 104 : if (strcmp(utlstr, Projections[projx].projname) == 0)
1947 8 : break;
1948 8 : if (Projections[projx].projname != NULL)
1949 8 : *projcode = Projections[projx].projcode;
1950 : }
1951 : else
1952 : {
1953 0 : status = -1;
1954 0 : HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__);
1955 0 : HEreport("Projection Code not defined for \"%s\".\n",
1956 : gridname);
1957 :
1958 0 : if (projparm != NULL)
1959 : {
1960 0 : for (i = 0; i < 13; i++)
1961 : {
1962 0 : projparm[i] = -1;
1963 : }
1964 : }
1965 : }
1966 : }
1967 :
1968 :
1969 : /* Get zonecode if requested */
1970 : /* ------------------------- */
1971 8 : if (zonecode != NULL)
1972 : {
1973 8 : *zonecode = -1;
1974 :
1975 :
1976 : /* Zone code only relevant for UTM and State Code projections */
1977 : /* ---------------------------------------------------------- */
1978 8 : if (*projcode == GCTP_UTM || *projcode == GCTP_SPCS)
1979 : {
1980 0 : statmeta = EHgetmetavalue(metaptrs, "ZoneCode", utlstr);
1981 0 : if (statmeta == 0)
1982 : {
1983 0 : *zonecode = atol(utlstr);
1984 : }
1985 : else
1986 : {
1987 0 : status = -1;
1988 0 : HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__);
1989 0 : HEreport("Zone Code not defined for \"%s\".\n",
1990 : gridname);
1991 : }
1992 : }
1993 : }
1994 :
1995 :
1996 : /* Get projection parameters if requested */
1997 : /* -------------------------------------- */
1998 8 : if (projparm != NULL)
1999 : {
2000 :
2001 : /*
2002 : * Note: No projection parameters for GEO, UTM, and State Code
2003 : * projections
2004 : */
2005 10 : if (*projcode == GCTP_GEO || *projcode == GCTP_UTM ||
2006 : *projcode == GCTP_SPCS)
2007 : {
2008 28 : for (i = 0; i < 13; i++)
2009 : {
2010 26 : projparm[i] = 0.0;
2011 : }
2012 :
2013 : }
2014 : else
2015 : {
2016 6 : statmeta = EHgetmetavalue(metaptrs, "ProjParams", utlstr);
2017 :
2018 6 : if (statmeta == 0)
2019 : {
2020 :
2021 : /* Build format string to read projection parameters */
2022 : /* ------------------------------------------------- */
2023 6 : strcpy(fmt, "%lf,");
2024 72 : for (i = 1; i <= 11; i++)
2025 66 : strcat(fmt, "%lf,");
2026 6 : strcat(fmt, "%lf");
2027 :
2028 :
2029 : /* Read parameters from numeric list */
2030 : /* --------------------------------- */
2031 6 : sscanf(&utlstr[1], fmt,
2032 : &projparm[0], &projparm[1],
2033 : &projparm[2], &projparm[3],
2034 : &projparm[4], &projparm[5],
2035 : &projparm[6], &projparm[7],
2036 : &projparm[8], &projparm[9],
2037 : &projparm[10], &projparm[11],
2038 : &projparm[12]);
2039 : }
2040 : else
2041 : {
2042 0 : status = -1;
2043 0 : HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__);
2044 0 : HEreport("Projection parameters not defined for \"%s\".\n",
2045 : gridname);
2046 :
2047 : }
2048 : }
2049 : }
2050 :
2051 :
2052 : /* Get spherecode if requested */
2053 : /* --------------------------- */
2054 8 : if (spherecode != NULL)
2055 : {
2056 8 : *spherecode = 0;
2057 :
2058 : /* Note: Spherecode not defined for GEO projection */
2059 : /* ----------------------------------------------- */
2060 8 : if ((*projcode != GCTP_GEO))
2061 : {
2062 6 : EHgetmetavalue(metaptrs, "SphereCode", utlstr);
2063 6 : if (statmeta == 0)
2064 : {
2065 6 : *spherecode = atol(utlstr);
2066 : }
2067 : }
2068 : }
2069 8 : free(metabuf);
2070 :
2071 : }
2072 8 : free(utlstr);
2073 8 : return (status);
2074 : }
2075 :
2076 :
2077 :
2078 : /*----------------------------------------------------------------------------|
2079 : | BEGIN_PROLOG |
2080 : | |
2081 : | FUNCTION: GDorigininfo |
2082 : | |
2083 : | DESCRIPTION: Returns origin code |
2084 : | |
2085 : | |
2086 : | Return Value Type Units Description |
2087 : | ============ ====== ========= ===================================== |
2088 : | status intn return status (0) SUCCEED, (-1) FAIL |
2089 : | |
2090 : | INPUTS: |
2091 : | gridID int32 Grid structure ID |
2092 : | |
2093 : | |
2094 : | OUTPUTS: |
2095 : | origincode int32 grid origin code |
2096 : | |
2097 : | NOTES: |
2098 : | |
2099 : | |
2100 : | Date Programmer Description |
2101 : | ====== ============ ================================================= |
2102 : | Jun 96 Joel Gales Original Programmer |
2103 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
2104 : | |
2105 : | END_PROLOG |
2106 : -----------------------------------------------------------------------------*/
2107 : intn
2108 : GDorigininfo(int32 gridID, int32 * origincode)
2109 0 : {
2110 : intn i; /* Loop index */
2111 0 : intn status = 0; /* routine return status variable */
2112 0 : intn statmeta = 0; /* EHgetmetavalue return status */
2113 :
2114 : int32 fid; /* HDF-EOS file ID */
2115 : int32 sdInterfaceID; /* HDF SDS interface ID */
2116 : int32 gdVgrpID; /* Grid root Vgroup ID */
2117 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
2118 :
2119 :
2120 : char *metabuf; /* Pointer to structural metadata (SM) */
2121 : char *metaptrs[2];/* Pointers to begin and end of SM section */
2122 : char gridname[80]; /* Grid Name */
2123 : char *utlstr; /* Utility string */
2124 :
2125 : /* Allocate space for utility string */
2126 : /* --------------------------------- */
2127 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
2128 0 : if(utlstr == NULL)
2129 : {
2130 0 : HEpush(DFE_NOSPACE,"GDorigininfo", __FILE__, __LINE__);
2131 0 : return(-1);
2132 : }
2133 : /* Check Grid ID */
2134 : /* ------------- */
2135 0 : status = GDchkgdid(gridID, "GDorigininfo",
2136 : &fid, &sdInterfaceID, &gdVgrpID);
2137 :
2138 :
2139 : /* Initialize pixreg code to -1 (in case of error) */
2140 : /* ----------------------------------------------- */
2141 0 : *origincode = -1;
2142 :
2143 0 : if (status == 0)
2144 : {
2145 : /* Set default origin code */
2146 : /* ----------------------- */
2147 0 : *origincode = 0;
2148 :
2149 :
2150 : /* Get grid name */
2151 : /* ------------- */
2152 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
2153 :
2154 :
2155 : /* Get pointers to grid structure section within SM */
2156 : /* ------------------------------------------------ */
2157 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
2158 : NULL, metaptrs);
2159 :
2160 0 : if(metabuf == NULL)
2161 : {
2162 0 : free(utlstr);
2163 0 : return(-1);
2164 : }
2165 :
2166 :
2167 0 : statmeta = EHgetmetavalue(metaptrs, "GridOrigin", utlstr);
2168 :
2169 0 : if (statmeta == 0)
2170 : {
2171 : /*
2172 : * If "GridOrigin" string found in metadata then convert to
2173 : * numeric origin code (fixed added: Jan 97)
2174 : */
2175 0 : for (i = 0; i < sizeof(originNames); i++)
2176 : {
2177 0 : if (strcmp(utlstr, originNames[i]) == 0)
2178 : {
2179 0 : *origincode = i;
2180 0 : break;
2181 : }
2182 : }
2183 : }
2184 :
2185 0 : free(metabuf);
2186 : }
2187 0 : free(utlstr);
2188 0 : return (status);
2189 : }
2190 :
2191 :
2192 :
2193 :
2194 :
2195 :
2196 : /*----------------------------------------------------------------------------|
2197 : | BEGIN_PROLOG |
2198 : | |
2199 : | FUNCTION: GDpixreginfo |
2200 : | |
2201 : | DESCRIPTION: |
2202 : | |
2203 : | |
2204 : | Return Value Type Units Description |
2205 : | ============ ====== ========= ===================================== |
2206 : | status intn return status (0) SUCCEED, (-1) FAIL |
2207 : | |
2208 : | INPUTS: |
2209 : | gridID int32 Grid structure ID |
2210 : | |
2211 : | |
2212 : | OUTPUTS: |
2213 : | pixregcode int32 Pixel registration code |
2214 : | |
2215 : | |
2216 : | OUTPUTS: |
2217 : | None |
2218 : | |
2219 : | NOTES: |
2220 : | |
2221 : | |
2222 : | Date Programmer Description |
2223 : | ====== ============ ================================================= |
2224 : | Jun 96 Joel Gales Original Programmer |
2225 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
2226 : | |
2227 : | END_PROLOG |
2228 : -----------------------------------------------------------------------------*/
2229 : intn
2230 : GDpixreginfo(int32 gridID, int32 * pixregcode)
2231 0 : {
2232 : intn i; /* Loop index */
2233 0 : intn status = 0; /* routine return status variable */
2234 0 : intn statmeta = 0; /* EHgetmetavalue return status */
2235 :
2236 : int32 fid; /* HDF-EOS file ID */
2237 : int32 sdInterfaceID; /* HDF SDS interface ID */
2238 : int32 gdVgrpID; /* Grid root Vgroup ID */
2239 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
2240 :
2241 :
2242 : char *metabuf; /* Pointer to structural metadata (SM) */
2243 : char *metaptrs[2];/* Pointers to begin and end of SM section */
2244 : char gridname[80]; /* Grid Name */
2245 : char *utlstr; /* Utility string */
2246 :
2247 : /* Allocate space for utility string */
2248 : /* --------------------------------- */
2249 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
2250 0 : if(utlstr == NULL)
2251 : {
2252 0 : HEpush(DFE_NOSPACE,"GDpixreginfo", __FILE__, __LINE__);
2253 0 : return(-1);
2254 : }
2255 : /* Check Grid ID */
2256 0 : status = GDchkgdid(gridID, "GDpixreginfo",
2257 : &fid, &sdInterfaceID, &gdVgrpID);
2258 :
2259 : /* Initialize pixreg code to -1 (in case of error) */
2260 0 : *pixregcode = -1;
2261 :
2262 0 : if (status == 0)
2263 : {
2264 : /* Set default pixreg code */
2265 0 : *pixregcode = 0;
2266 :
2267 : /* Get grid name */
2268 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
2269 :
2270 : /* Get pointers to grid structure section within SM */
2271 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
2272 : NULL, metaptrs);
2273 :
2274 0 : if(metabuf == NULL)
2275 : {
2276 0 : free(utlstr);
2277 0 : return(-1);
2278 : }
2279 :
2280 :
2281 0 : statmeta = EHgetmetavalue(metaptrs, "PixelRegistration", utlstr);
2282 :
2283 0 : if (statmeta == 0)
2284 : {
2285 : /*
2286 : * If "PixelRegistration" string found in metadata then convert
2287 : * to numeric origin code (fixed added: Jan 97)
2288 : */
2289 :
2290 0 : for (i = 0; i < sizeof(pixregNames); i++)
2291 : {
2292 0 : if (strcmp(utlstr, pixregNames[i]) == 0)
2293 : {
2294 0 : *pixregcode = i;
2295 0 : break;
2296 : }
2297 : }
2298 : }
2299 0 : free(metabuf);
2300 : }
2301 0 : free(utlstr);
2302 0 : return (status);
2303 : }
2304 :
2305 :
2306 :
2307 : /*----------------------------------------------------------------------------|
2308 : | BEGIN_PROLOG |
2309 : | |
2310 : | FUNCTION: GDcompinfo |
2311 : | |
2312 : | DESCRIPTION: |
2313 : | |
2314 : | |
2315 : | Return Value Type Units Description |
2316 : | ============ ====== ========= ===================================== |
2317 : | status intn |
2318 : | |
2319 : | INPUTS: |
2320 : | gridID int32 |
2321 : | compcode int32 |
2322 : | compparm intn |
2323 : | |
2324 : | |
2325 : | OUTPUTS: |
2326 : | None |
2327 : | |
2328 : | NOTES: |
2329 : | |
2330 : | |
2331 : | Date Programmer Description |
2332 : | ====== ============ ================================================= |
2333 : | Oct 96 Joel Gales Original Programmer |
2334 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
2335 : | |
2336 : | END_PROLOG |
2337 : -----------------------------------------------------------------------------*/
2338 : intn
2339 : GDcompinfo(int32 gridID, char *fieldname, int32 * compcode, intn compparm[])
2340 0 : {
2341 : intn i; /* Loop index */
2342 0 : intn status = 0; /* routine return status variable */
2343 0 : intn statmeta = 0; /* EHgetmetavalue return status */
2344 :
2345 : int32 fid; /* HDF-EOS file ID */
2346 : int32 sdInterfaceID; /* HDF SDS interface ID */
2347 : int32 gdVgrpID; /* Grid root Vgroup ID */
2348 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
2349 :
2350 :
2351 : char *metabuf; /* Pointer to structural metadata (SM) */
2352 : char *metaptrs[2];/* Pointers to begin and end of SM section */
2353 : char gridname[80]; /* Grid Name */
2354 : char *utlstr;/* Utility string */
2355 :
2356 : /* Allocate space for utility string */
2357 : /* --------------------------------- */
2358 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
2359 0 : if(utlstr == NULL)
2360 : {
2361 0 : HEpush(DFE_NOSPACE,"GDcompinfo", __FILE__, __LINE__);
2362 0 : return(-1);
2363 : }
2364 : /* Check Grid ID */
2365 0 : status = GDchkgdid(gridID, "GDcompinfo", &fid, &sdInterfaceID, &gdVgrpID);
2366 :
2367 :
2368 0 : if (status == 0)
2369 : {
2370 : /* Get grid name */
2371 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
2372 :
2373 : /* Get pointers to "DataField" section within SM */
2374 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
2375 : "DataField", metaptrs);
2376 0 : if(metabuf == NULL)
2377 : {
2378 0 : free(utlstr);
2379 0 : return(-1);
2380 : }
2381 :
2382 :
2383 : /* Search for field */
2384 0 : sprintf(utlstr, "%s%s%s", "\"", fieldname, "\"\n");
2385 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
2386 :
2387 :
2388 : /* If field found and user wants compression code ... */
2389 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
2390 : {
2391 0 : if (compcode != NULL)
2392 : {
2393 : /* Set endptr at end of field's definition entry */
2394 0 : metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
2395 :
2396 : /* Get compression type */
2397 0 : statmeta = EHgetmetavalue(metaptrs, "CompressionType", utlstr);
2398 :
2399 : /*
2400 : * Default is no compression if "CompressionType" string not
2401 : * in metadata
2402 : */
2403 0 : *compcode = HDFE_COMP_NONE;
2404 :
2405 : /* If compression code is found ... */
2406 0 : if (statmeta == 0)
2407 : {
2408 : /* Loop through compression types until match */
2409 0 : for (i = 0; i < sizeof(HDFcomp); i++)
2410 : {
2411 0 : if (strcmp(utlstr, HDFcomp[i]) == 0)
2412 : {
2413 0 : *compcode = i;
2414 0 : break;
2415 : }
2416 : }
2417 : }
2418 : }
2419 :
2420 : /* If user wants compression parameters ... */
2421 0 : if (compparm != NULL && compcode != NULL)
2422 : {
2423 : /* Initialize to zero */
2424 0 : for (i = 0; i < 4; i++)
2425 : {
2426 0 : compparm[i] = 0.0;
2427 : }
2428 :
2429 : /*
2430 : * Get compression parameters if NBIT or DEFLATE compression
2431 : */
2432 0 : if (*compcode == HDFE_COMP_NBIT)
2433 : {
2434 0 : statmeta =
2435 : EHgetmetavalue(metaptrs, "CompressionParams", utlstr);
2436 0 : if (statmeta == 0)
2437 : {
2438 0 : sscanf(utlstr, "(%d,%d,%d,%d)",
2439 : &compparm[0], &compparm[1],
2440 : &compparm[2], &compparm[3]);
2441 : }
2442 : else
2443 : {
2444 0 : status = -1;
2445 0 : HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__);
2446 0 : HEreport(
2447 : "\"CompressionParams\" string not found in metadata.\n");
2448 : }
2449 : }
2450 0 : else if (*compcode == HDFE_COMP_DEFLATE)
2451 : {
2452 0 : statmeta =
2453 : EHgetmetavalue(metaptrs, "DeflateLevel", utlstr);
2454 0 : if (statmeta == 0)
2455 : {
2456 0 : sscanf(utlstr, "%d", &compparm[0]);
2457 : }
2458 : else
2459 : {
2460 0 : status = -1;
2461 0 : HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__);
2462 0 : HEreport(
2463 : "\"DeflateLevel\" string not found in metadata.\n");
2464 : }
2465 : }
2466 : }
2467 : }
2468 : else
2469 : {
2470 0 : HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__);
2471 0 : HEreport("Fieldname \"%s\" not found.\n", fieldname);
2472 : }
2473 :
2474 0 : free(metabuf);
2475 :
2476 : }
2477 0 : free(utlstr);
2478 0 : return (status);
2479 : }
2480 :
2481 :
2482 :
2483 :
2484 :
2485 :
2486 : /*----------------------------------------------------------------------------|
2487 : | BEGIN_PROLOG |
2488 : | |
2489 : | FUNCTION: GDfieldinfo |
2490 : | |
2491 : | DESCRIPTION: Retrieve information about a specific geolocation or data |
2492 : | field in the grid. |
2493 : | |
2494 : | |
2495 : | Return Value Type Units Description |
2496 : | ============ ====== ========= ===================================== |
2497 : | status intn return status (0) SUCCEED, (-1) FAIL |
2498 : | |
2499 : | INPUTS: |
2500 : | gridID int32 grid structure id |
2501 : | fieldname char name of field |
2502 : | |
2503 : | |
2504 : | OUTPUTS: |
2505 : | rank int32 rank of field (# of dims) |
2506 : | dims int32 field dimensions |
2507 : | numbertype int32 field number type |
2508 : | dimlist char field dimension list |
2509 : | |
2510 : | |
2511 : | OUTPUTS: |
2512 : | None |
2513 : | |
2514 : | NOTES: |
2515 : | |
2516 : | |
2517 : | Date Programmer Description |
2518 : | ====== ============ ================================================= |
2519 : | Jun 96 Joel Gales Original Programmer |
2520 : | Aug 96 Joel Gales Make metadata ODL compliant |
2521 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
2522 : | Feb 99 Abe Taaheri Changed memcpy to memmove to avoid overlapping |
2523 : | problem when copying strings |
2524 : | |
2525 : | END_PROLOG |
2526 : -----------------------------------------------------------------------------*/
2527 : intn
2528 : GDfieldinfo(int32 gridID, char *fieldname, int32 * rank, int32 dims[],
2529 : int32 * numbertype, char *dimlist)
2530 :
2531 125 : {
2532 : intn i; /* Loop index */
2533 : intn status; /* routine return status variable */
2534 125 : intn statmeta = 0; /* EHgetmetavalue return status */
2535 :
2536 : int32 fid; /* HDF-EOS file ID */
2537 : int32 sdInterfaceID; /* HDF SDS interface ID */
2538 125 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
2539 : int32 ndims; /* Number of dimensions */
2540 : int32 slen[8]; /* Length of each entry in parsed string */
2541 : int32 dum; /* Dummy variable */
2542 : int32 xdim; /* X dim size */
2543 : int32 ydim; /* Y dim size */
2544 : int32 sdid; /* SDS id */
2545 :
2546 : char *metabuf; /* Pointer to structural metadata (SM) */
2547 : char *metaptrs[2]; /* Pointers to begin and end of SM section */
2548 : char gridname[80]; /* Grid Name */
2549 : char *utlstr; /* Utility string */
2550 : char *ptr[8]; /* String pointers for parsed string */
2551 : char dimstr[64]; /* Individual dimension entry string */
2552 :
2553 :
2554 : /* Allocate space for utility string */
2555 : /* --------------------------------- */
2556 125 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
2557 125 : if(utlstr == NULL)
2558 : {
2559 0 : HEpush(DFE_NOSPACE,"GDfieldinfo", __FILE__, __LINE__);
2560 0 : return(-1);
2561 : }
2562 125 : *rank = -1;
2563 125 : *numbertype = -1;
2564 :
2565 125 : status = GDchkgdid(gridID, "GDfieldinfo", &fid, &sdInterfaceID, &dum);
2566 :
2567 125 : if (status == 0)
2568 : {
2569 :
2570 125 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
2571 :
2572 125 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
2573 : "DataField", metaptrs);
2574 125 : if(metabuf == NULL)
2575 : {
2576 0 : free(utlstr);
2577 0 : return(-1);
2578 : }
2579 :
2580 :
2581 : /* Search for field */
2582 125 : sprintf(utlstr, "%s%s%s", "\"", fieldname, "\"\n");
2583 125 : metaptrs[0] = strstr(metaptrs[0], utlstr);
2584 :
2585 : /* If field found ... */
2586 125 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
2587 : {
2588 :
2589 : /* Set endptr at end of dimension definition entry */
2590 125 : metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
2591 :
2592 : /* Get DataType string */
2593 125 : statmeta = EHgetmetavalue(metaptrs, "DataType", utlstr);
2594 :
2595 : /* Convert to numbertype code */
2596 125 : if (statmeta == 0)
2597 125 : *numbertype = EHnumstr(utlstr);
2598 : else
2599 : {
2600 0 : status = -1;
2601 0 : HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__);
2602 0 : HEreport("\"DataType\" string not found in metadata.\n");
2603 : }
2604 :
2605 : /*
2606 : * Get DimList string and trim off leading and trailing parens
2607 : * "()"
2608 : */
2609 125 : statmeta = EHgetmetavalue(metaptrs, "DimList", utlstr);
2610 :
2611 125 : if (statmeta == 0)
2612 : {
2613 125 : memmove(utlstr, utlstr + 1, strlen(utlstr) - 2);
2614 125 : utlstr[strlen(utlstr) - 2] = 0;
2615 :
2616 : /* Parse trimmed DimList string and get rank */
2617 125 : ndims = EHparsestr(utlstr, ',', ptr, slen);
2618 125 : *rank = ndims;
2619 : }
2620 : else
2621 : {
2622 0 : status = -1;
2623 0 : HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__);
2624 0 : HEreport("\"DimList\" string not found in metadata.\n");
2625 : }
2626 :
2627 :
2628 125 : if (status == 0)
2629 : {
2630 125 : status = GDgridinfo(gridID, &xdim, &ydim, NULL, NULL);
2631 :
2632 375 : for (i = 0; i < ndims; i++)
2633 : {
2634 250 : memcpy(dimstr, ptr[i] + 1, slen[i] - 2);
2635 250 : dimstr[slen[i] - 2] = 0;
2636 :
2637 250 : if (strcmp(dimstr, "XDim") == 0)
2638 : {
2639 125 : dims[i] = xdim;
2640 : }
2641 125 : else if (strcmp(dimstr, "YDim") == 0)
2642 : {
2643 125 : dims[i] = ydim;
2644 : }
2645 : else
2646 : {
2647 0 : dims[i] = GDdiminfo(gridID, dimstr);
2648 : }
2649 :
2650 :
2651 250 : if (dimlist != NULL)
2652 : {
2653 16 : if (i == 0)
2654 : {
2655 8 : dimlist[0] = 0;
2656 : }
2657 :
2658 16 : if (i > 0)
2659 : {
2660 8 : strcat(dimlist, ",");
2661 : }
2662 16 : strcat(dimlist, dimstr);
2663 : }
2664 : }
2665 :
2666 :
2667 125 : if (dims[0] == 0)
2668 : {
2669 0 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname,
2670 : &sdid, &dum, &dum, &dum, dims,
2671 : &dum);
2672 : }
2673 : }
2674 : }
2675 :
2676 125 : free(metabuf);
2677 : }
2678 :
2679 125 : if (*rank == -1)
2680 : {
2681 0 : status = -1;
2682 :
2683 0 : HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__);
2684 0 : HEreport("Fieldname \"%s\" not found.\n", fieldname);
2685 : }
2686 125 : free(utlstr);
2687 125 : return (status);
2688 : }
2689 :
2690 :
2691 :
2692 :
2693 :
2694 :
2695 : /*----------------------------------------------------------------------------|
2696 : | BEGIN_PROLOG |
2697 : | |
2698 : | FUNCTION: GDdeffield |
2699 : | |
2700 : | DESCRIPTION: Defines a new data field within the grid. |
2701 : | |
2702 : | |
2703 : | Return Value Type Units Description |
2704 : | ============ ====== ========= ===================================== |
2705 : | status intn return status (0) SUCCEED, (-1) FAIL |
2706 : | |
2707 : | INPUTS: |
2708 : | gridID int32 grid structure ID |
2709 : | fieldname char fieldname |
2710 : | dimlist char Dimension list (comma-separated list) |
2711 : | numbertype int32 field type |
2712 : | merge int32 merge code |
2713 : | |
2714 : | |
2715 : | OUTPUTS: |
2716 : | None |
2717 : | |
2718 : | NOTES: |
2719 : | |
2720 : | |
2721 : | Date Programmer Description |
2722 : | ====== ============ ================================================= |
2723 : | Jun 96 Joel Gales Original Programmer |
2724 : | Aug 96 Joel Gales Check name for ODL compliance |
2725 : | Sep 96 Joel Gales Make string array "dimbuf" dynamic |
2726 : | Sep 96 Joel Gales Add support for Block SOM (MISR) |
2727 : | Jan 97 Joel Gales Add support for tiling |
2728 : | Feb 99 Abe Taaheri Changed strcpy to memmove to avoid overlapping |
2729 : | problem when copying strings |
2730 : | |
2731 : | END_PROLOG |
2732 : -----------------------------------------------------------------------------*/
2733 : intn
2734 : GDdeffield(int32 gridID, char *fieldname, char *dimlist,
2735 : int32 numbertype, int32 merge)
2736 :
2737 0 : {
2738 : intn i; /* Loop index */
2739 : intn status; /* routine return status variable */
2740 : intn found; /* utility found flag */
2741 0 : intn foundNT = 0;/* found number type flag */
2742 0 : intn foundAllDim = 1; /* found all dimensions flag */
2743 0 : intn first = 1; /* first entry flag */
2744 :
2745 : int32 fid; /* HDF-EOS file ID */
2746 : int32 vgid; /* Geo/Data field Vgroup ID */
2747 : int32 sdInterfaceID; /* HDF SDS interface ID */
2748 : int32 sdid; /* SDS object ID */
2749 : int32 dimid; /* SDS dimension ID */
2750 : int32 gdVgrpID; /* Grid root Vgroup ID */
2751 : int32 dims[8]; /* Dimension size array */
2752 : int32 dimsize; /* Dimension size */
2753 0 : int32 rank = 0; /* Field rank */
2754 : int32 slen[32]; /* String length array */
2755 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
2756 : int32 compcode; /* Compression code */
2757 : int32 tilecode; /* Tiling code */
2758 : int32 chunkFlag; /* Chunking (Tiling) flag */
2759 : int32 gID; /* GridID - offset */
2760 : int32 xdim; /* Grid X dimension */
2761 : int32 ydim; /* Grid Y dimension */
2762 : int32 projcode; /* Projection Code */
2763 :
2764 : float64 projparm[13]; /* Projection Parameters */
2765 :
2766 : char *dimbuf; /* Dimension buffer */
2767 : char *dimlist0; /* Auxilliary dimension list */
2768 : char *comma; /* Pointer to comma */
2769 : char *dimcheck; /* Dimension check buffer */
2770 : char utlbuf[512];/* Utility buffer */
2771 : char utlbuf2[256]; /* Utility buffer 1 */
2772 : char *ptr[32]; /* String pointer array */
2773 : char gridname[80]; /* Grid name */
2774 : char parmbuf[128]; /* Parameter string buffer */
2775 : char errbuf1[128]; /* Error buffer 1 */
2776 : char errbuf2[128]; /* Error buffer 2 */
2777 : static const char errmsg1[] = "Dimension: %d (size: %d) not divisible by ";
2778 : /* Tiling error message part 1 */
2779 : static const char errmsg2[] = "tile dimension (size: %d).\n";
2780 : /* Tiling error message part 2 */
2781 : char errmsg[128];/* Tiling error message */
2782 :
2783 : /* Valid number types */
2784 : static const uint16 good_number[10] = {
2785 : 3, 4, 5, 6, 20, 21, 22, 23, 24, 25
2786 : };
2787 :
2788 : comp_info c_info; /* Compression parameter structure */
2789 :
2790 : HDF_CHUNK_DEF chunkDef; /* Tiling structure */
2791 :
2792 :
2793 :
2794 : /* Setup error message strings */
2795 : /* --------------------------- */
2796 0 : strcpy(errbuf1, "GDXSDname array too small.\nPlease increase ");
2797 0 : strcat(errbuf1, "size of HDFE_NAMBUFSIZE in \"HdfEosDef.h\".\n");
2798 0 : strcpy(errbuf2, "GDXSDdims array too small.\nPlease increase ");
2799 0 : strcat(errbuf2, "size of HDFE_DIMBUFSIZE in \"HdfEosDef.h\".\n");
2800 :
2801 :
2802 : /* Build tiling dimension error message */
2803 : /* ------------------------------------ */
2804 0 : strcpy(errmsg, errmsg1);
2805 0 : strcat(errmsg, errmsg2);
2806 :
2807 : /*
2808 : * Check for proper grid ID and return HDF-EOS file ID, SDinterface ID,
2809 : * and grid root Vgroup ID
2810 : */
2811 0 : status = GDchkgdid(gridID, "GDdefinefield",
2812 : &fid, &sdInterfaceID, &gdVgrpID);
2813 :
2814 :
2815 0 : if (status == 0)
2816 : {
2817 : /* Remove offset from grid ID & get gridname */
2818 0 : gID = gridID % idOffset;
2819 0 : Vgetname(GDXGrid[gID].IDTable, gridname);
2820 :
2821 :
2822 : /* Allocate space for dimension buffer and auxilliary dimension list */
2823 : /* ----------------------------------------------------------------- */
2824 0 : dimbuf = (char *) calloc(strlen(dimlist) + 64, 1);
2825 0 : if(dimbuf == NULL)
2826 : {
2827 0 : HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__);
2828 0 : return(-1);
2829 : }
2830 0 : dimlist0 = (char *) calloc(strlen(dimlist) + 64, 1);
2831 0 : if(dimlist0 == NULL)
2832 : {
2833 0 : HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__);
2834 0 : free(dimbuf);
2835 0 : return(-1);
2836 : }
2837 :
2838 :
2839 : /* Get Grid and Projection info */
2840 : /* ---------------------------- */
2841 0 : status = GDgridinfo(gridID, &xdim, &ydim, NULL, NULL);
2842 0 : status = GDprojinfo(gridID, &projcode, NULL, NULL, projparm);
2843 :
2844 :
2845 : /* Setup Block Dimension if "Blocked" SOM projection */
2846 : /* ------------------------------------------------- */
2847 0 : if (projcode == GCTP_SOM && (int32) projparm[11] != 0)
2848 : {
2849 0 : dimsize = GDdiminfo(gridID, "SOMBlockDim");
2850 :
2851 : /* If "SOMBlockDim" not yet defined then do it */
2852 0 : if (dimsize == -1)
2853 : {
2854 0 : GDdefdim(gridID, "SOMBlockDim", (int32) projparm[11]);
2855 : }
2856 :
2857 : /* If not 1D field then prepend to dimension list */
2858 0 : if (strchr(dimlist, ',') != NULL)
2859 : {
2860 0 : strcpy(dimbuf, "SOMBlockDim,");
2861 0 : strcat(dimbuf, dimlist);
2862 : }
2863 : else
2864 : {
2865 0 : strcpy(dimbuf, dimlist);
2866 : }
2867 : }
2868 : else
2869 : {
2870 : /* If not "Blocked" SOM then just copy dim list to dim buffer */
2871 0 : strcpy(dimbuf, dimlist);
2872 : }
2873 :
2874 : /*
2875 : * Copy dimension buffer to auxilliary dimlist and Append comma to
2876 : * end of dimension list
2877 : */
2878 0 : strcpy(dimlist0, dimbuf);
2879 0 : strcat(dimbuf, ",");
2880 :
2881 :
2882 : /* Find comma */
2883 : /* ---------- */
2884 0 : comma = strchr(dimbuf, ',');
2885 :
2886 :
2887 : /*
2888 : * Loop through entries in dimension list to make sure they are
2889 : * defined in grid
2890 : */
2891 0 : while (comma != NULL)
2892 : {
2893 : /* Copy dimension list entry to dimcheck */
2894 : /* ------------------------------------- */
2895 0 : dimcheck = (char *) calloc(comma - dimbuf + 1, 1);
2896 0 : if(dimcheck == NULL)
2897 : {
2898 0 : HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__);
2899 0 : free(dimbuf);
2900 0 : free(dimlist0);
2901 0 : return(-1);
2902 : }
2903 0 : memcpy(dimcheck, dimbuf, comma - dimbuf);
2904 :
2905 :
2906 : /* Get Dimension Size */
2907 : /* ------------------ */
2908 0 : if (strcmp(dimcheck, "XDim") == 0)
2909 : {
2910 : /* If "XDim" then use xdim value for grid definition */
2911 : /* ------------------------------------------------- */
2912 0 : dimsize = xdim;
2913 0 : found = 1;
2914 0 : dims[rank] = dimsize;
2915 0 : rank++;
2916 : }
2917 0 : else if (strcmp(dimcheck, "YDim") == 0)
2918 : {
2919 : /* If "YDim" then use ydim value for grid definition */
2920 : /* ------------------------------------------------- */
2921 0 : dimsize = ydim;
2922 0 : found = 1;
2923 0 : dims[rank] = dimsize;
2924 0 : rank++;
2925 : }
2926 : else
2927 : {
2928 : /* "Regular" Dimension */
2929 : /* ------------------- */
2930 0 : dimsize = GDdiminfo(gridID, dimcheck);
2931 0 : if (dimsize != -1)
2932 : {
2933 0 : found = 1;
2934 0 : dims[rank] = dimsize;
2935 0 : rank++;
2936 : }
2937 : else
2938 : {
2939 0 : found = 0;
2940 : }
2941 : }
2942 :
2943 :
2944 : /*
2945 : * If dimension list entry not found - set error return status,
2946 : * append name to utility buffer for error report
2947 : */
2948 0 : if (found == 0)
2949 : {
2950 0 : status = -1;
2951 0 : foundAllDim = 0;
2952 0 : if (first == 1)
2953 : {
2954 0 : strcpy(utlbuf, dimcheck);
2955 : }
2956 : else
2957 : {
2958 0 : strcat(utlbuf, ",");
2959 0 : strcat(utlbuf, dimcheck);
2960 : }
2961 0 : first = 0;
2962 : }
2963 :
2964 : /*
2965 : * Go to next dimension entry, find next comma, & free up
2966 : * dimcheck buffer
2967 : */
2968 0 : memmove(dimbuf, comma + 1, strlen(comma)-1);
2969 0 : dimbuf[strlen(comma)-1]= 0;
2970 0 : comma = strchr(dimbuf, ',');
2971 0 : free(dimcheck);
2972 : }
2973 0 : free(dimbuf);
2974 :
2975 :
2976 :
2977 : /* Check fieldname length */
2978 : /* ---------------------- */
2979 0 : if (status == 0)
2980 : {
2981 : /* if ((intn) strlen(fieldname) > MAX_NC_NAME - 7)
2982 : ** this was changed because HDF4.1r3 made a change in the
2983 : ** hlimits.h file. We have notidfied NCSA and asked to have
2984 : ** it made the same as in previous versions of HDF
2985 : ** see ncr 26314. DaW Apr 2000
2986 : */
2987 0 : if((intn) strlen(fieldname) > (256 - 7))
2988 : {
2989 0 : status = -1;
2990 0 : HEpush(DFE_GENAPP, "GDdefinefield", __FILE__, __LINE__);
2991 0 : HEreport("Fieldname \"%s\" too long.\n", fieldname);
2992 : }
2993 : }
2994 :
2995 :
2996 :
2997 : /* Check for valid numbertype */
2998 : /* -------------------------- */
2999 0 : if (status == 0)
3000 : {
3001 0 : for (i = 0; i < 10; i++)
3002 : {
3003 0 : if (numbertype == good_number[i])
3004 : {
3005 0 : foundNT = 1;
3006 0 : break;
3007 : }
3008 : }
3009 :
3010 0 : if (foundNT == 0)
3011 : {
3012 0 : HEpush(DFE_BADNUMTYPE, "GDdeffield", __FILE__, __LINE__);
3013 0 : HEreport("Invalid number type: %d (%s).\n",
3014 : numbertype, fieldname);
3015 0 : status = -1;
3016 : }
3017 : }
3018 :
3019 :
3020 : /* Define Field */
3021 : /* ------------ */
3022 0 : if (status == 0)
3023 : {
3024 : /* Get Field Vgroup id, compresion code, & tiling code */
3025 : /* -------------------------------------------------- */
3026 0 : vgid = GDXGrid[gID].VIDTable[0];
3027 0 : compcode = GDXGrid[gID].compcode;
3028 0 : tilecode = GDXGrid[gID].tilecode;
3029 :
3030 :
3031 : /* SDS Interface (Multi-dim fields) */
3032 : /* -------------------------------- */
3033 :
3034 :
3035 : /*
3036 : * If rank is less than or equal to 3 (and greater than 1) and
3037 : * AUTOMERGE is set and the first dimension is not appendable and
3038 : * the compression code is set to none then ...
3039 : */
3040 0 : if (rank >= 2 && rank <= 3 && merge == HDFE_AUTOMERGE &&
3041 : dims[0] != 0 && compcode == HDFE_COMP_NONE &&
3042 : tilecode == HDFE_NOTILE)
3043 : {
3044 :
3045 : /* Find first empty slot in external combination array */
3046 : /* --------------------------------------------------- */
3047 0 : i = 0;
3048 0 : while (GDXSDcomb[5 * i] != 0)
3049 : {
3050 0 : i++;
3051 : }
3052 :
3053 : /*
3054 : * Store dimensions, grid root Vgroup ID, and number type in
3055 : * external combination array "GDXSDcomb"
3056 : */
3057 0 : if (rank == 2)
3058 : {
3059 : /* If 2-dim field then set lowest dimension to 1 */
3060 : /* --------------------------------------------- */
3061 0 : GDXSDcomb[5 * i] = 1;
3062 0 : GDXSDcomb[5 * i + 1] = dims[0];
3063 0 : GDXSDcomb[5 * i + 2] = dims[1];
3064 : }
3065 : else
3066 : {
3067 0 : GDXSDcomb[5 * i] = dims[0];
3068 0 : GDXSDcomb[5 * i + 1] = dims[1];
3069 0 : GDXSDcomb[5 * i + 2] = dims[2];
3070 : }
3071 :
3072 0 : GDXSDcomb[5 * i + 3] = gdVgrpID;
3073 0 : GDXSDcomb[5 * i + 4] = numbertype;
3074 :
3075 :
3076 : /* Concatanate fieldname with combined name string */
3077 : /* ----------------------------------------------- */
3078 0 : if ((intn) strlen(GDXSDname) +
3079 : (intn) strlen(fieldname) + 2 < HDFE_NAMBUFSIZE)
3080 : {
3081 0 : strcat(GDXSDname, fieldname);
3082 0 : strcat(GDXSDname, ",");
3083 : }
3084 : else
3085 : {
3086 : /* GDXSDname array too small! */
3087 : /* -------------------------- */
3088 0 : HEpush(DFE_GENAPP, "GDdefinefield",
3089 : __FILE__, __LINE__);
3090 0 : HEreport(errbuf1);
3091 0 : status = -1;
3092 0 : free(dimlist0);
3093 0 : return (status);
3094 : }
3095 :
3096 :
3097 : /*
3098 : * If 2-dim field then set lowest dimension (in 3-dim array)
3099 : * to "ONE"
3100 : */
3101 0 : if (rank == 2)
3102 : {
3103 0 : if ((intn) strlen(GDXSDdims) + 5 < HDFE_DIMBUFSIZE)
3104 : {
3105 0 : strcat(GDXSDdims, "ONE,");
3106 : }
3107 : else
3108 : {
3109 : /* GDXSDdims array too small! */
3110 : /* -------------------------- */
3111 0 : HEpush(DFE_GENAPP, "GDdefinefield",
3112 : __FILE__, __LINE__);
3113 0 : HEreport(errbuf2);
3114 0 : status = -1;
3115 0 : free(dimlist0);
3116 0 : return (status);
3117 : }
3118 : }
3119 :
3120 :
3121 : /*
3122 : * Concatanate field dimlist to merged dimlist and separate
3123 : * fields with semi-colon
3124 : */
3125 0 : if ((intn) strlen(GDXSDdims) +
3126 : (intn) strlen(dimlist0) + 2 < HDFE_DIMBUFSIZE)
3127 : {
3128 0 : strcat(GDXSDdims, dimlist0);
3129 0 : strcat(GDXSDdims, ";");
3130 : }
3131 : else
3132 : {
3133 : /* GDXSDdims array too small! */
3134 : /* -------------------------- */
3135 0 : HEpush(DFE_GENAPP, "GDdefinefield",
3136 : __FILE__, __LINE__);
3137 0 : HEreport(errbuf2);
3138 0 : status = -1;
3139 0 : free(dimlist0);
3140 0 : return (status);
3141 : }
3142 :
3143 : } /* End Multi-Dim Merge Section */
3144 : else
3145 : {
3146 :
3147 : /* Multi-Dim No Merge Section */
3148 : /* ========================== */
3149 :
3150 :
3151 : /* Check that field dims are divisible by tile dims */
3152 : /* ------------------------------------------------ */
3153 0 : if (tilecode == HDFE_TILE)
3154 : {
3155 0 : for (i = 0; i < GDXGrid[gID].tilerank; i++)
3156 : {
3157 0 : if ((dims[i] % GDXGrid[gID].tiledims[i]) != 0)
3158 : {
3159 0 : HEpush(DFE_GENAPP, "GDdeffield",
3160 : __FILE__, __LINE__);
3161 0 : HEreport(errmsg,
3162 : i, dims[i], GDXGrid[gID].tiledims[0]);
3163 0 : status = -1;
3164 : }
3165 : }
3166 :
3167 0 : if (status == -1)
3168 : {
3169 0 : free(dimlist0);
3170 0 : return (status);
3171 : }
3172 : }
3173 :
3174 :
3175 : /* Create SDS dataset */
3176 : /* ------------------ */
3177 0 : sdid = SDcreate(sdInterfaceID, fieldname,
3178 : numbertype, rank, dims);
3179 :
3180 :
3181 : /* Store Dimension Names in SDS */
3182 : /* ---------------------------- */
3183 0 : rank = EHparsestr(dimlist0, ',', ptr, slen);
3184 0 : for (i = 0; i < rank; i++)
3185 : {
3186 : /* Dimension name = Swathname:Dimname */
3187 : /* ---------------------------------- */
3188 0 : memcpy(utlbuf, ptr[i], slen[i]);
3189 0 : utlbuf[slen[i]] = 0;
3190 0 : strcat(utlbuf, ":");
3191 0 : strcat(utlbuf, gridname);
3192 :
3193 0 : dimid = SDgetdimid(sdid, i);
3194 0 : SDsetdimname(dimid, utlbuf);
3195 : }
3196 :
3197 :
3198 : /* Setup Compression */
3199 : /* ----------------- */
3200 0 : if (compcode == HDFE_COMP_NBIT)
3201 : {
3202 0 : c_info.nbit.nt = numbertype;
3203 0 : c_info.nbit.sign_ext = GDXGrid[gID].compparm[0];
3204 0 : c_info.nbit.fill_one = GDXGrid[gID].compparm[1];
3205 0 : c_info.nbit.start_bit = GDXGrid[gID].compparm[2];
3206 0 : c_info.nbit.bit_len = GDXGrid[gID].compparm[3];
3207 : }
3208 0 : else if (compcode == HDFE_COMP_SKPHUFF)
3209 : {
3210 0 : c_info.skphuff.skp_size = (intn) DFKNTsize(numbertype);
3211 : }
3212 0 : else if (compcode == HDFE_COMP_DEFLATE)
3213 : {
3214 0 : c_info.deflate.level = GDXGrid[gID].compparm[0];
3215 : }
3216 :
3217 :
3218 : /* If field is compressed w/o tiling then call SDsetcompress */
3219 : /* --------------------------------------------------------- */
3220 0 : if (compcode != HDFE_COMP_NONE && tilecode == HDFE_NOTILE)
3221 : {
3222 0 : status = SDsetcompress(sdid, (comp_coder_t) compcode, &c_info);
3223 : }
3224 :
3225 :
3226 : /* Setup Tiling */
3227 : /* ------------ */
3228 0 : if (tilecode == HDFE_TILE)
3229 : {
3230 : /* Tiling without Compression */
3231 : /* -------------------------- */
3232 0 : if (compcode == HDFE_COMP_NONE)
3233 : {
3234 :
3235 : /* Setup chunk lengths */
3236 : /* ------------------- */
3237 0 : for (i = 0; i < GDXGrid[gID].tilerank; i++)
3238 : {
3239 0 : chunkDef.chunk_lengths[i] =
3240 : GDXGrid[gID].tiledims[i];
3241 : }
3242 :
3243 0 : chunkFlag = HDF_CHUNK;
3244 : }
3245 :
3246 : /* Tiling with Compression */
3247 : /* ----------------------- */
3248 : else
3249 : {
3250 : /* Setup chunk lengths */
3251 : /* ------------------- */
3252 0 : for (i = 0; i < GDXGrid[gID].tilerank; i++)
3253 : {
3254 0 : chunkDef.comp.chunk_lengths[i] =
3255 : GDXGrid[gID].tiledims[i];
3256 : }
3257 :
3258 :
3259 : /* Setup chunk flag & chunk compression type */
3260 : /* ----------------------------------------- */
3261 0 : chunkFlag = HDF_CHUNK | HDF_COMP;
3262 0 : chunkDef.comp.comp_type = compcode;
3263 :
3264 :
3265 : /* Setup chunk compression parameters */
3266 : /* ---------------------------------- */
3267 0 : if (compcode == HDFE_COMP_SKPHUFF)
3268 : {
3269 0 : chunkDef.comp.cinfo.skphuff.skp_size =
3270 : c_info.skphuff.skp_size;
3271 : }
3272 0 : else if (compcode == HDFE_COMP_DEFLATE)
3273 : {
3274 0 : chunkDef.comp.cinfo.deflate.level =
3275 : c_info.deflate.level;
3276 : }
3277 : }
3278 :
3279 : /* Call SDsetchunk routine */
3280 : /* ----------------------- */
3281 0 : status = SDsetchunk(sdid, chunkDef, chunkFlag);
3282 : }
3283 :
3284 :
3285 : /* Attach to Vgroup */
3286 : /* ---------------- */
3287 0 : Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid));
3288 :
3289 :
3290 : /* Store SDS dataset IDs */
3291 : /* --------------------- */
3292 :
3293 : /* Allocate space for the SDS ID array */
3294 : /* ----------------------------------- */
3295 0 : if (GDXGrid[gID].nSDS > 0)
3296 : {
3297 : /* Array already exists therefore reallocate */
3298 : /* ----------------------------------------- */
3299 0 : GDXGrid[gID].sdsID = (int32 *)
3300 : realloc((void *) GDXGrid[gID].sdsID,
3301 : (GDXGrid[gID].nSDS + 1) * 4);
3302 0 : if(GDXGrid[gID].sdsID == NULL)
3303 : {
3304 0 : HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__);
3305 0 : free(dimlist0);
3306 0 : return(-1);
3307 : }
3308 : }
3309 : else
3310 : {
3311 : /* Array does not exist */
3312 : /* -------------------- */
3313 0 : GDXGrid[gID].sdsID = (int32 *) calloc(1, 4);
3314 0 : if(GDXGrid[gID].sdsID == NULL)
3315 : {
3316 0 : HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__);
3317 0 : free(dimlist0);
3318 0 : return(-1);
3319 : }
3320 : }
3321 :
3322 : /* Store SDS ID in array & increment count */
3323 : /* --------------------------------------- */
3324 0 : GDXGrid[gID].sdsID[GDXGrid[gID].nSDS] = sdid;
3325 0 : GDXGrid[gID].nSDS++;
3326 :
3327 : }
3328 :
3329 :
3330 : /* Setup metadata string */
3331 : /* --------------------- */
3332 0 : sprintf(utlbuf, "%s%s%s", fieldname, ":", dimlist0);
3333 :
3334 :
3335 : /* Setup compression metadata */
3336 : /* -------------------------- */
3337 0 : if (compcode != HDFE_COMP_NONE)
3338 : {
3339 0 : sprintf(utlbuf2,
3340 : "%s%s",
3341 : ":\n\t\t\t\tCompressionType=", HDFcomp[compcode]);
3342 :
3343 0 : switch (compcode)
3344 : {
3345 : case HDFE_COMP_NBIT:
3346 :
3347 0 : sprintf(parmbuf,
3348 : "%s%d,%d,%d,%d%s",
3349 : "\n\t\t\t\tCompressionParams=(",
3350 : GDXGrid[gID].compparm[0],
3351 : GDXGrid[gID].compparm[1],
3352 : GDXGrid[gID].compparm[2],
3353 : GDXGrid[gID].compparm[3], ")");
3354 0 : strcat(utlbuf2, parmbuf);
3355 0 : break;
3356 :
3357 :
3358 : case HDFE_COMP_DEFLATE:
3359 :
3360 0 : sprintf(parmbuf,
3361 : "%s%d",
3362 : "\n\t\t\t\tDeflateLevel=",
3363 : GDXGrid[gID].compparm[0]);
3364 0 : strcat(utlbuf2, parmbuf);
3365 : break;
3366 : }
3367 0 : strcat(utlbuf, utlbuf2);
3368 : }
3369 :
3370 :
3371 :
3372 :
3373 : /* Setup tiling metadata */
3374 : /* --------------------- */
3375 0 : if (tilecode == HDFE_TILE)
3376 : {
3377 0 : if (compcode == HDFE_COMP_NONE)
3378 : {
3379 0 : sprintf(utlbuf2, "%s%d",
3380 : ":\n\t\t\t\tTilingDimensions=(",
3381 : (int)GDXGrid[gID].tiledims[0]);
3382 : }
3383 : else
3384 : {
3385 0 : sprintf(utlbuf2, "%s%d",
3386 : "\n\t\t\t\tTilingDimensions=(",
3387 : (int)GDXGrid[gID].tiledims[0]);
3388 : }
3389 :
3390 0 : for (i = 1; i < GDXGrid[gID].tilerank; i++)
3391 : {
3392 0 : sprintf(parmbuf, ",%d", (int)GDXGrid[gID].tiledims[i]);
3393 0 : strcat(utlbuf2, parmbuf);
3394 : }
3395 0 : strcat(utlbuf2, ")");
3396 0 : strcat(utlbuf, utlbuf2);
3397 : }
3398 :
3399 :
3400 : /* Insert field metadata within File Structural Metadata */
3401 : /* ----------------------------------------------------- */
3402 0 : status = EHinsertmeta(sdInterfaceID, gridname, "g", 4L,
3403 : utlbuf, &numbertype);
3404 :
3405 : }
3406 0 : free(dimlist0);
3407 :
3408 : }
3409 :
3410 : /* If all dimensions not found then report error */
3411 : /* --------------------------------------------- */
3412 0 : if (foundAllDim == 0)
3413 : {
3414 0 : HEpush(DFE_GENAPP, "GDdeffield", __FILE__, __LINE__);
3415 0 : HEreport("Dimension(s): \"%s\" not found (%s).\n",
3416 : utlbuf, fieldname);
3417 0 : status = -1;
3418 : }
3419 :
3420 0 : return (status);
3421 : }
3422 :
3423 :
3424 :
3425 :
3426 : /*----------------------------------------------------------------------------|
3427 : | BEGIN_PROLOG |
3428 : | |
3429 : | FUNCTION: GDwritefieldmeta |
3430 : | |
3431 : | DESCRIPTION: Writes field meta data for an existing grid field not |
3432 : | defined within the grid API routine "GDdeffield". |
3433 : | |
3434 : | Return Value Type Units Description |
3435 : | ============ ====== ========= ===================================== |
3436 : | status intn return status (0) SUCCEED, (-1) FAIL |
3437 : | |
3438 : | INPUTS: |
3439 : | gridID int32 grid structure ID |
3440 : | fieldname char fieldname |
3441 : | dimlist char Dimension list (comma-separated list) |
3442 : | numbertype int32 field type |
3443 : | |
3444 : | |
3445 : | OUTPUTS: |
3446 : | None |
3447 : | |
3448 : | NOTES: |
3449 : | |
3450 : | |
3451 : | Date Programmer Description |
3452 : | ====== ============ ================================================= |
3453 : | Jun 96 Joel Gales Original Programmer |
3454 : | |
3455 : | END_PROLOG |
3456 : -----------------------------------------------------------------------------*/
3457 : intn
3458 : GDwritefieldmeta(int32 gridID, char *fieldname, char *dimlist,
3459 : int32 numbertype)
3460 0 : {
3461 0 : intn status = 0; /* routine return status variable */
3462 :
3463 : int32 sdInterfaceID; /* HDF SDS interface ID */
3464 : int32 dum; /* dummy variable */
3465 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
3466 :
3467 : char utlbuf[256];/* Utility buffer */
3468 : char gridname[80]; /* Grid name */
3469 :
3470 :
3471 0 : status = GDchkgdid(gridID, "GDwritefieldmeta", &dum, &sdInterfaceID,
3472 : &dum);
3473 :
3474 0 : if (status == 0)
3475 : {
3476 0 : sprintf(utlbuf, "%s%s%s", fieldname, ":", dimlist);
3477 :
3478 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
3479 0 : status = EHinsertmeta(sdInterfaceID, gridname, "g", 4L,
3480 : utlbuf, &numbertype);
3481 : }
3482 0 : return (status);
3483 : }
3484 :
3485 :
3486 :
3487 :
3488 : /*----------------------------------------------------------------------------|
3489 : | BEGIN_PROLOG |
3490 : | |
3491 : | FUNCTION: GDSDfldsrch |
3492 : | |
3493 : | DESCRIPTION: Retrieves information from SDS fields |
3494 : | |
3495 : | |
3496 : | Return Value Type Units Description |
3497 : | ============ ====== ========= ===================================== |
3498 : | status intn return status (0) SUCCEED, (-1) FAIL |
3499 : | |
3500 : | INPUTS: |
3501 : | gridID int32 grid structure ID |
3502 : | sdInterfaceID int32 SD interface ID |
3503 : | fieldname char field name |
3504 : | |
3505 : | |
3506 : | OUTPUTS: |
3507 : | sdid int32 SD element ID |
3508 : | rankSDS int32 Rank of SDS |
3509 : | rankFld int32 True rank of field (merging) |
3510 : | offset int32 Offset of field within merged field |
3511 : | dims int32 Dimensions of field |
3512 : | solo int32 Solo field flag |
3513 : | |
3514 : | NOTES: |
3515 : | |
3516 : | |
3517 : | Date Programmer Description |
3518 : | ====== ============ ================================================= |
3519 : | Jun 96 Joel Gales Original Programmer |
3520 : | Aug 96 Joel Gales Make metadata ODL compliant |
3521 : | |
3522 : | END_PROLOG |
3523 : -----------------------------------------------------------------------------*/
3524 : static intn
3525 : GDSDfldsrch(int32 gridID, int32 sdInterfaceID, const char *fieldname,
3526 : int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset,
3527 : int32 dims[], int32 * solo)
3528 123 : {
3529 : intn i; /* Loop index */
3530 123 : intn status = -1;/* routine return status variable */
3531 :
3532 : int32 gID; /* GridID - offset */
3533 123 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
3534 : int32 dum; /* Dummy variable */
3535 : int32 dums[128]; /* Dummy array */
3536 : int32 attrIndex; /* Attribute index */
3537 :
3538 : char name[2048]; /* Merged-Field Names */
3539 : char gridname[80]; /* Grid Name */
3540 : char *utlstr;/* Utility string */
3541 : char *metabuf; /* Pointer to structural metadata (SM) */
3542 : char *metaptrs[2];/* Pointers to begin and end of SM section */
3543 : char *oldmetaptr; /* Pointer within SM section */
3544 : char *metaptr; /* Pointer within SM section */
3545 :
3546 :
3547 : /* Allocate space for utility string */
3548 : /* --------------------------------- */
3549 123 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
3550 123 : if(utlstr == NULL)
3551 : {
3552 0 : HEpush(DFE_NOSPACE,"GDSDfldsrch", __FILE__, __LINE__);
3553 0 : return(-1);
3554 : }
3555 : /* Set solo flag to 0 (no) */
3556 : /* ----------------------- */
3557 123 : *solo = 0;
3558 :
3559 :
3560 : /* Compute "reduced" grid ID */
3561 : /* ------------------------- */
3562 123 : gID = gridID % idOffset;
3563 :
3564 :
3565 : /* Loop through all SDSs in grid */
3566 : /* ----------------------------- */
3567 123 : for (i = 0; i < GDXGrid[gID].nSDS; i++)
3568 : {
3569 : /* If active SDS ... */
3570 : /* ----------------- */
3571 123 : if (GDXGrid[gID].sdsID[i] != 0)
3572 : {
3573 : /* Get SDS ID, name, rankSDS, and dimensions */
3574 : /* ----------------------------------------- */
3575 123 : *sdid = GDXGrid[gID].sdsID[i];
3576 123 : SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum);
3577 123 : *rankFld = *rankSDS;
3578 :
3579 :
3580 : /* If merged field ... */
3581 : /* ------------------- */
3582 123 : if (strstr(name, "MRGFLD_") == &name[0])
3583 : {
3584 : /* Get grid name */
3585 : /* ------------- */
3586 0 : Vgetname(GDXGrid[gID].IDTable, gridname);
3587 :
3588 :
3589 : /* Get pointers to "MergedFields" section within SM */
3590 : /* ------------------------------------------------ */
3591 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
3592 : "MergedFields", metaptrs);
3593 0 : if(metabuf == NULL)
3594 : {
3595 0 : free(utlstr);
3596 0 : return(-1);
3597 : }
3598 :
3599 :
3600 : /* Initialize metaptr to beg. of section */
3601 : /* ------------------------------------- */
3602 0 : metaptr = metaptrs[0];
3603 :
3604 :
3605 : /* Store metaptr in order to recover */
3606 : /* --------------------------------- */
3607 0 : oldmetaptr = metaptr;
3608 :
3609 :
3610 : /* Search for Merged field name */
3611 : /* ---------------------------- */
3612 0 : sprintf(utlstr, "%s%s%s", "MergedFieldName=\"",
3613 : name, "\"\n");
3614 0 : metaptr = strstr(metaptr, utlstr);
3615 :
3616 :
3617 : /* If not found check for old metadata */
3618 : /* ----------------------------------- */
3619 0 : if (metaptr == NULL)
3620 : {
3621 0 : sprintf(utlstr, "%s%s%s", "OBJECT=\"", name, "\"\n");
3622 0 : metaptr = strstr(oldmetaptr, utlstr);
3623 : }
3624 :
3625 :
3626 : /* Get field list and strip off leading and trailing quotes */
3627 : /* -------------------------------------------------------- */
3628 0 : EHgetmetavalue(metaptrs, "FieldList", name);
3629 0 : memmove(name, name + 1, strlen(name) - 2);
3630 0 : name[strlen(name) - 2] = 0;
3631 :
3632 :
3633 : /* Search for desired field within merged field list */
3634 : /* ------------------------------------------------- */
3635 0 : sprintf(utlstr, "%s%s%s", "\"", fieldname, "\"");
3636 0 : dum = EHstrwithin(utlstr, name, ',');
3637 :
3638 0 : free(metabuf);
3639 : }
3640 : else
3641 : {
3642 : /* If solo (unmerged) check if SDS name matches fieldname */
3643 : /* ------------------------------------------------------ */
3644 123 : dum = EHstrwithin(fieldname, name, ',');
3645 123 : if (dum != -1)
3646 : {
3647 123 : *solo = 1;
3648 123 : *offset = 0;
3649 : }
3650 : }
3651 :
3652 :
3653 :
3654 : /* If field found ... */
3655 : /* ------------------ */
3656 123 : if (dum != -1)
3657 : {
3658 123 : status = 0;
3659 :
3660 : /* If merged field ... */
3661 : /* ------------------- */
3662 123 : if (*solo == 0)
3663 : {
3664 : /* Get "Field Offsets" SDS attribute index */
3665 : /* --------------------------------------- */
3666 98 : attrIndex = SDfindattr(*sdid, "Field Offsets");
3667 :
3668 : /*
3669 : * If attribute exists then get offset of desired field
3670 : * within merged field
3671 : */
3672 98 : if (attrIndex != -1)
3673 : {
3674 0 : SDreadattr(*sdid, attrIndex, (VOIDP) dums);
3675 0 : *offset = dums[dum];
3676 : }
3677 :
3678 :
3679 : /* Get "Field Dims" SDS attribute index */
3680 : /* ------------------------------------ */
3681 98 : attrIndex = SDfindattr(*sdid, "Field Dims");
3682 :
3683 : /*
3684 : * If attribute exists then get 0th dimension of desired
3685 : * field within merged field
3686 : */
3687 98 : if (attrIndex != -1)
3688 : {
3689 0 : SDreadattr(*sdid, attrIndex, (VOIDP) dums);
3690 0 : dims[0] = dums[dum];
3691 :
3692 : /* If this dimension = 1 then field is really 2 dim */
3693 : /* ------------------------------------------------ */
3694 0 : if (dums[dum] == 1)
3695 : {
3696 0 : *rankFld = 2;
3697 : }
3698 : }
3699 : }
3700 :
3701 :
3702 : /* Break out of SDS loop */
3703 : /* --------------------- */
3704 123 : break;
3705 : } /* End of found field section */
3706 : }
3707 : else
3708 : {
3709 : /* First non-active SDS signifies no more, break out of SDS loop */
3710 : /* ------------------------------------------------------------- */
3711 0 : break;
3712 : }
3713 : }
3714 123 : free(utlstr);
3715 123 : return (status);
3716 : }
3717 :
3718 :
3719 :
3720 :
3721 : /*----------------------------------------------------------------------------|
3722 : | BEGIN_PROLOG |
3723 : | |
3724 : | FUNCTION: GDwrrdfield |
3725 : | |
3726 : | DESCRIPTION: Writes/Reads fields |
3727 : | |
3728 : | |
3729 : | Return Value Type Units Description |
3730 : | ============ ====== ========= ===================================== |
3731 : | status intn return status (0) SUCCEED, (-1) FAIL |
3732 : | |
3733 : | INPUTS: |
3734 : | gridID int32 grid structure ID |
3735 : | fieldname char fieldname |
3736 : | code char Write/Read code (w/r) |
3737 : | start int32 start array |
3738 : | stride int32 stride array |
3739 : | edge int32 edge array |
3740 : | datbuf void data buffer for read |
3741 : | |
3742 : | |
3743 : | OUTPUTS: |
3744 : | datbuf void data buffer for write |
3745 : | |
3746 : | |
3747 : | NOTES: |
3748 : | |
3749 : | |
3750 : | Date Programmer Description |
3751 : | ====== ============ ================================================= |
3752 : | Jun 96 Joel Gales Original Programmer |
3753 : | Feb 97 Joel Gales Stride = 1 HDF compression workaround |
3754 : | |
3755 : | END_PROLOG |
3756 : -----------------------------------------------------------------------------*/
3757 : static intn
3758 : GDwrrdfield(int32 gridID, char *fieldname, char *code,
3759 : int32 start[], int32 stride[], int32 edge[], VOIDP datbuf)
3760 :
3761 25 : {
3762 : intn i; /* Loop index */
3763 25 : intn status = 0; /* routine return status variable */
3764 :
3765 : int32 fid; /* HDF-EOS file ID */
3766 : int32 sdInterfaceID; /* HDF SDS interface ID */
3767 : int32 sdid; /* SDS ID */
3768 : int32 dum; /* Dummy variable */
3769 : int32 rankSDS; /* Rank of SDS */
3770 : int32 rankFld; /* Rank of field */
3771 :
3772 : int32 offset[8]; /* I/O offset (start) */
3773 : int32 incr[8]; /* I/O increment (stride) */
3774 : int32 count[8]; /* I/O count (edge) */
3775 : int32 dims[8]; /* Field/SDS dimensions */
3776 : int32 mrgOffset; /* Merged field offset */
3777 : int32 strideOne; /* Strides = 1 flag */
3778 :
3779 :
3780 : /* Check for valid grid ID */
3781 : /* ----------------------- */
3782 25 : status = GDchkgdid(gridID, "GDwrrdfield", &fid, &sdInterfaceID, &dum);
3783 :
3784 :
3785 25 : if (status == 0)
3786 : {
3787 : /* Check that field exists */
3788 : /* ----------------------- */
3789 25 : status = GDfieldinfo(gridID, fieldname, &rankSDS, dims, &dum, NULL);
3790 :
3791 :
3792 25 : if (status != 0)
3793 : {
3794 0 : HEpush(DFE_GENAPP, "GDwrrdfield", __FILE__, __LINE__);
3795 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
3796 0 : status = -1;
3797 :
3798 : }
3799 :
3800 :
3801 25 : if (status == 0)
3802 : {
3803 25 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid,
3804 : &rankSDS, &rankFld, &mrgOffset, dims, &dum);
3805 :
3806 :
3807 : /* Set I/O offset Section */
3808 : /* ---------------------- */
3809 :
3810 : /*
3811 : * If start == NULL (default) set I/O offset of 0th field to
3812 : * offset within merged field (if any) and the rest to 0
3813 : */
3814 25 : if (start == NULL)
3815 : {
3816 0 : for (i = 0; i < rankSDS; i++)
3817 : {
3818 0 : offset[i] = 0;
3819 : }
3820 0 : offset[0] = mrgOffset;
3821 : }
3822 : else
3823 : {
3824 : /*
3825 : * ... otherwise set I/O offset to user values, adjusting the
3826 : * 0th field with the merged field offset (if any)
3827 : */
3828 25 : if (rankFld == rankSDS)
3829 : {
3830 75 : for (i = 0; i < rankSDS; i++)
3831 : {
3832 50 : offset[i] = start[i];
3833 : }
3834 25 : offset[0] += mrgOffset;
3835 : }
3836 : else
3837 : {
3838 : /*
3839 : * If field really 2-dim merged in 3-dim field then set
3840 : * 0th field offset to merge offset and then next two to
3841 : * the user values
3842 : */
3843 0 : for (i = 0; i < rankFld; i++)
3844 : {
3845 0 : offset[i + 1] = start[i];
3846 : }
3847 0 : offset[0] = mrgOffset;
3848 : }
3849 : }
3850 :
3851 :
3852 :
3853 : /* Set I/O stride Section */
3854 : /* ---------------------- */
3855 :
3856 : /*
3857 : * If stride == NULL (default) set I/O stride to 1
3858 : */
3859 25 : if (stride == NULL)
3860 : {
3861 75 : for (i = 0; i < rankSDS; i++)
3862 : {
3863 50 : incr[i] = 1;
3864 : }
3865 : }
3866 : else
3867 : {
3868 : /*
3869 : * ... otherwise set I/O stride to user values
3870 : */
3871 0 : if (rankFld == rankSDS)
3872 : {
3873 0 : for (i = 0; i < rankSDS; i++)
3874 : {
3875 0 : incr[i] = stride[i];
3876 : }
3877 : }
3878 : else
3879 : {
3880 : /*
3881 : * If field really 2-dim merged in 3-dim field then set
3882 : * 0th field stride to 1 and then next two to the user
3883 : * values.
3884 : */
3885 0 : for (i = 0; i < rankFld; i++)
3886 : {
3887 0 : incr[i + 1] = stride[i];
3888 : }
3889 0 : incr[0] = 1;
3890 : }
3891 : }
3892 :
3893 :
3894 :
3895 : /* Set I/O count Section */
3896 : /* --------------------- */
3897 :
3898 : /*
3899 : * If edge == NULL (default) set I/O count to number of remaining
3900 : * entries (dims - start) / increment. Note that 0th field
3901 : * offset corrected for merged field offset (if any).
3902 : */
3903 25 : if (edge == NULL)
3904 : {
3905 0 : for (i = 1; i < rankSDS; i++)
3906 : {
3907 0 : count[i] = (dims[i] - offset[i]) / incr[i];
3908 : }
3909 0 : count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0];
3910 : }
3911 : else
3912 : {
3913 : /*
3914 : * ... otherwise set I/O count to user values
3915 : */
3916 25 : if (rankFld == rankSDS)
3917 : {
3918 75 : for (i = 0; i < rankSDS; i++)
3919 : {
3920 50 : count[i] = edge[i];
3921 : }
3922 : }
3923 : else
3924 : {
3925 : /*
3926 : * If field really 2-dim merged in 3-dim field then set
3927 : * 0th field count to 1 and then next two to the user
3928 : * values.
3929 : */
3930 0 : for (i = 0; i < rankFld; i++)
3931 : {
3932 0 : count[i + 1] = edge[i];
3933 : }
3934 0 : count[0] = 1;
3935 : }
3936 : }
3937 :
3938 :
3939 : /* Perform I/O with relevant HDF I/O routine */
3940 : /* ----------------------------------------- */
3941 25 : if (strcmp(code, "w") == 0)
3942 : {
3943 : /* Set strideOne to true (1) */
3944 : /* ------------------------- */
3945 0 : strideOne = 1;
3946 :
3947 :
3948 : /* If incr[i] != 1 set strideOne to false (0) */
3949 : /* ------------------------------------------ */
3950 0 : for (i = 0; i < rankSDS; i++)
3951 : {
3952 0 : if (incr[i] != 1)
3953 : {
3954 0 : strideOne = 0;
3955 0 : break;
3956 : }
3957 : }
3958 :
3959 :
3960 : /*
3961 : * If strideOne is true use NULL paramater for stride. This
3962 : * is a work-around to HDF compression problem
3963 : */
3964 0 : if (strideOne == 1)
3965 : {
3966 0 : status = SDwritedata(sdid, offset, NULL, count,
3967 : (VOIDP) datbuf);
3968 : }
3969 : else
3970 : {
3971 0 : status = SDwritedata(sdid, offset, incr, count,
3972 : (VOIDP) datbuf);
3973 : }
3974 : }
3975 : else
3976 : {
3977 25 : status = SDreaddata(sdid, offset, incr, count,
3978 : (VOIDP) datbuf);
3979 : }
3980 : }
3981 : }
3982 :
3983 25 : return (status);
3984 : }
3985 :
3986 :
3987 : /*----------------------------------------------------------------------------|
3988 : | BEGIN_PROLOG |
3989 : | |
3990 : | FUNCTION: GDwritefield |
3991 : | |
3992 : | DESCRIPTION: Writes data to a grid field. |
3993 : | |
3994 : | |
3995 : | Return Value Type Units Description |
3996 : | ============ ====== ========= ===================================== |
3997 : | status intn return status (0) SUCCEED, (-1) FAIL |
3998 : | |
3999 : | INPUTS: |
4000 : | gridID int32 grid structure ID |
4001 : | fieldname char fieldname |
4002 : | start int32 start array |
4003 : | stride int32 stride array |
4004 : | edge int32 edge array |
4005 : | |
4006 : | |
4007 : | OUTPUTS: |
4008 : | data void data buffer for write |
4009 : | |
4010 : | NOTES: |
4011 : | |
4012 : | |
4013 : | Date Programmer Description |
4014 : | ====== ============ ================================================= |
4015 : | Jun 96 Joel Gales Original Programmer |
4016 : | |
4017 : | END_PROLOG |
4018 : -----------------------------------------------------------------------------*/
4019 : intn
4020 : GDwritefield(int32 gridID, char *fieldname,
4021 : int32 start[], int32 stride[], int32 edge[], VOIDP data)
4022 :
4023 0 : {
4024 0 : intn status = 0; /* routine return status variable */
4025 :
4026 0 : status = GDwrrdfield(gridID, fieldname, "w", start, stride, edge,
4027 : data);
4028 0 : return (status);
4029 : }
4030 :
4031 :
4032 :
4033 :
4034 :
4035 : /*----------------------------------------------------------------------------|
4036 : | BEGIN_PROLOG |
4037 : | |
4038 : | FUNCTION: GDreadfield |
4039 : | |
4040 : | DESCRIPTION: Reads data from a grid field. |
4041 : | |
4042 : | |
4043 : | Return Value Type Units Description |
4044 : | ============ ====== ========= ===================================== |
4045 : | status intn return status (0) SUCCEED, (-1) FAIL |
4046 : | |
4047 : | INPUTS: |
4048 : | gridID int32 grid structure ID |
4049 : | fieldname char fieldname |
4050 : | start int32 start array |
4051 : | stride int32 stride array |
4052 : | edge int32 edge array |
4053 : | buffer void data buffer for read |
4054 : | |
4055 : | |
4056 : | OUTPUTS: |
4057 : | None |
4058 : | |
4059 : | NOTES: |
4060 : | |
4061 : | |
4062 : | Date Programmer Description |
4063 : | ====== ============ ================================================= |
4064 : | Jun 96 Joel Gales Original Programmer |
4065 : | |
4066 : | END_PROLOG |
4067 : -----------------------------------------------------------------------------*/
4068 : intn
4069 : GDreadfield(int32 gridID, char *fieldname,
4070 : int32 start[], int32 stride[], int32 edge[], VOIDP buffer)
4071 :
4072 25 : {
4073 25 : intn status = 0; /* routine return status variable */
4074 :
4075 25 : status = GDwrrdfield(gridID, fieldname, "r", start, stride, edge,
4076 : buffer);
4077 25 : return (status);
4078 : }
4079 :
4080 :
4081 :
4082 :
4083 : /*----------------------------------------------------------------------------|
4084 : | BEGIN_PROLOG |
4085 : | |
4086 : | FUNCTION: GDwrrdattr |
4087 : | |
4088 : | DESCRIPTION: |
4089 : | |
4090 : | |
4091 : | Return Value Type Units Description |
4092 : | ============ ====== ========= ===================================== |
4093 : | status intn return status (0) SUCCEED, (-1) FAIL |
4094 : | |
4095 : | INPUTS: |
4096 : | gridID int32 grid structure ID |
4097 : | attrname char attribute name |
4098 : | numbertype int32 attribute HDF numbertype |
4099 : | count int32 Number of attribute elements |
4100 : | wrcode char Read/Write Code "w/r" |
4101 : | datbuf void I/O buffer |
4102 : | |
4103 : | OUTPUTS: |
4104 : | datbuf |
4105 : | |
4106 : | NOTES: |
4107 : | |
4108 : | |
4109 : | Date Programmer Description |
4110 : | ====== ============ ================================================= |
4111 : | Jun 96 Joel Gales Original Programmer |
4112 : | Oct 96 Joel Gales Get Attribute Vgroup ID from external array |
4113 : | |
4114 : | END_PROLOG |
4115 : -----------------------------------------------------------------------------*/
4116 : static intn
4117 : GDwrrdattr(int32 gridID, char *attrname, int32 numbertype, int32 count,
4118 : char *wrcode, VOIDP datbuf)
4119 :
4120 0 : {
4121 : intn status; /* routine return status variable */
4122 :
4123 : int32 fid; /* HDF-EOS file ID */
4124 : int32 attrVgrpID; /* Grid attribute ID */
4125 : int32 dum; /* dummy variable */
4126 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
4127 :
4128 :
4129 : /* Check Grid id */
4130 0 : status = GDchkgdid(gridID, "GDwrrdattr", &fid, &dum, &dum);
4131 :
4132 0 : if (status == 0)
4133 : {
4134 : /* Perform Attribute I/O */
4135 : /* --------------------- */
4136 0 : attrVgrpID = GDXGrid[gridID % idOffset].VIDTable[1];
4137 0 : status = EHattr(fid, attrVgrpID, attrname, numbertype, count,
4138 : wrcode, datbuf);
4139 : }
4140 0 : return (status);
4141 : }
4142 :
4143 :
4144 :
4145 : /*----------------------------------------------------------------------------|
4146 : | BEGIN_PROLOG |
4147 : | |
4148 : | FUNCTION: GDwriteattr |
4149 : | |
4150 : | DESCRIPTION: Writes/updates attribute in a grid. |
4151 : | |
4152 : | |
4153 : | Return Value Type Units Description |
4154 : | ============ ====== ========= ===================================== |
4155 : | status intn return status (0) SUCCEED, (-1) FAIL |
4156 : | |
4157 : | INPUTS: |
4158 : | gridID int32 grid structure ID |
4159 : | attrname char attribute name |
4160 : | numbertype int32 attribute HDF numbertype |
4161 : | count int32 Number of attribute elements |
4162 : | datbuf void I/O buffer |
4163 : | |
4164 : | OUTPUTS: |
4165 : | None |
4166 : | |
4167 : | NOTES: |
4168 : | |
4169 : | |
4170 : | Date Programmer Description |
4171 : | ====== ============ ================================================= |
4172 : | Jun 96 Joel Gales Original Programmer |
4173 : | |
4174 : | END_PROLOG |
4175 : -----------------------------------------------------------------------------*/
4176 : intn
4177 : GDwriteattr(int32 gridID, char *attrname, int32 numbertype, int32 count,
4178 : VOIDP datbuf)
4179 0 : {
4180 0 : intn status = 0; /* routine return status variable */
4181 :
4182 : /* Call GDwrrdattr routine to write attribute */
4183 : /* ------------------------------------------ */
4184 0 : status = GDwrrdattr(gridID, attrname, numbertype, count, "w", datbuf);
4185 :
4186 0 : return (status);
4187 : }
4188 :
4189 :
4190 :
4191 : /*----------------------------------------------------------------------------|
4192 : | BEGIN_PROLOG |
4193 : | |
4194 : | FUNCTION: GDreadattr |
4195 : | |
4196 : | DESCRIPTION: Reads attribute from a grid. |
4197 : | |
4198 : | |
4199 : | Return Value Type Units Description |
4200 : | ============ ====== ========= ===================================== |
4201 : | status intn return status (0) SUCCEED, (-1) FAIL |
4202 : | |
4203 : | INPUTS: |
4204 : | gridID int32 grid structure ID |
4205 : | attrname char attribute name |
4206 : | |
4207 : | OUTPUTS: |
4208 : | datbuf void I/O buffer |
4209 : | |
4210 : | NOTES: |
4211 : | |
4212 : | |
4213 : | Date Programmer Description |
4214 : | ====== ============ ================================================= |
4215 : | Jun 96 Joel Gales Original Programmer |
4216 : | |
4217 : | END_PROLOG |
4218 : -----------------------------------------------------------------------------*/
4219 : intn
4220 : GDreadattr(int32 gridID, char *attrname, VOIDP datbuf)
4221 0 : {
4222 0 : intn status = 0; /* routine return status variable */
4223 0 : int32 dum = 0; /* dummy variable */
4224 :
4225 : /* Call GDwrrdattr routine to read attribute */
4226 : /* ----------------------------------------- */
4227 0 : status = GDwrrdattr(gridID, attrname, dum, dum, "r", datbuf);
4228 :
4229 0 : return (status);
4230 : }
4231 :
4232 :
4233 :
4234 :
4235 :
4236 : /*----------------------------------------------------------------------------|
4237 : | BEGIN_PROLOG |
4238 : | |
4239 : | FUNCTION: GDattrinfo |
4240 : | |
4241 : | DESCRIPTION: |
4242 : | |
4243 : | |
4244 : | Return Value Type Units Description |
4245 : | ============ ====== ========= ===================================== |
4246 : | status intn return status (0) SUCCEED, (-1) FAIL |
4247 : | |
4248 : | INPUTS: |
4249 : | gridID int32 grid structure ID |
4250 : | attrname char attribute name |
4251 : | |
4252 : | OUTPUTS: |
4253 : | numbertype int32 attribute HDF numbertype |
4254 : | count int32 Number of attribute elements |
4255 : | |
4256 : | |
4257 : | OUTPUTS: |
4258 : | None |
4259 : | |
4260 : | NOTES: |
4261 : | |
4262 : | |
4263 : | Date Programmer Description |
4264 : | ====== ============ ================================================= |
4265 : | Jun 96 Joel Gales Original Programmer |
4266 : | Oct 96 Joel Gales Get Attribute Vgroup ID from external array |
4267 : | |
4268 : | END_PROLOG |
4269 : -----------------------------------------------------------------------------*/
4270 : intn
4271 : GDattrinfo(int32 gridID, char *attrname, int32 * numbertype, int32 * count)
4272 0 : {
4273 0 : intn status = 0; /* routine return status variable */
4274 :
4275 : int32 fid; /* HDF-EOS file ID */
4276 : int32 attrVgrpID; /* Grid attribute ID */
4277 : int32 dum; /* dummy variable */
4278 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
4279 :
4280 0 : status = GDchkgdid(gridID, "GDattrinfo", &fid, &dum, &dum);
4281 :
4282 0 : attrVgrpID = GDXGrid[gridID % idOffset].VIDTable[1];
4283 :
4284 0 : status = EHattrinfo(fid, attrVgrpID, attrname, numbertype,
4285 : count);
4286 :
4287 0 : return (status);
4288 : }
4289 :
4290 :
4291 :
4292 :
4293 :
4294 :
4295 : /*----------------------------------------------------------------------------|
4296 : | BEGIN_PROLOG |
4297 : | |
4298 : | FUNCTION: GDinqattrs |
4299 : | |
4300 : | DESCRIPTION: |
4301 : | |
4302 : | |
4303 : | Return Value Type Units Description |
4304 : | ============ ====== ========= ===================================== |
4305 : | nattr int32 Number of attributes in swath struct |
4306 : | |
4307 : | INPUTS: |
4308 : | grid ID int32 grid structure ID |
4309 : | |
4310 : | OUTPUTS: |
4311 : | attrnames char Attribute names in swath struct |
4312 : | (Comma-separated list) |
4313 : | strbufsize int32 Attributes name list string length |
4314 : | |
4315 : | OUTPUTS: |
4316 : | None |
4317 : | |
4318 : | NOTES: |
4319 : | |
4320 : | |
4321 : | Date Programmer Description |
4322 : | ====== ============ ================================================= |
4323 : | Jun 96 Joel Gales Original Programmer |
4324 : | Oct 96 Joel Gales Initialize nattr |
4325 : | Oct 96 Joel Gales Get Attribute Vgroup ID from external array |
4326 : | |
4327 : | END_PROLOG |
4328 : -----------------------------------------------------------------------------*/
4329 : int32
4330 : GDinqattrs(int32 gridID, char *attrnames, int32 * strbufsize)
4331 8 : {
4332 : intn status; /* routine return status variable */
4333 :
4334 : int32 fid; /* HDF-EOS file ID */
4335 : int32 attrVgrpID; /* Grid attribute ID */
4336 : int32 dum; /* dummy variable */
4337 8 : int32 nattr = 0; /* Number of attributes */
4338 8 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
4339 :
4340 :
4341 : /* Check Grid id */
4342 8 : status = GDchkgdid(gridID, "GDinqattrs", &fid, &dum, &dum);
4343 :
4344 8 : if (status == 0)
4345 : {
4346 8 : attrVgrpID = GDXGrid[gridID % idOffset].VIDTable[1];
4347 8 : nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize);
4348 : }
4349 :
4350 8 : return (nattr);
4351 : }
4352 :
4353 :
4354 :
4355 :
4356 :
4357 :
4358 : #define REMQUOTE \
4359 : \
4360 : memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); \
4361 : utlstr[strlen(utlstr) - 2] = 0;
4362 :
4363 :
4364 : /*----------------------------------------------------------------------------|
4365 : | BEGIN_PROLOG |
4366 : | |
4367 : | FUNCTION: GDinqdims |
4368 : | |
4369 : | DESCRIPTION: Retrieve information about all dimensions defined in a grid. |
4370 : | |
4371 : | |
4372 : | Return Value Type Units Description |
4373 : | ============ ====== ========= ===================================== |
4374 : | nDim int32 Number of defined dimensions |
4375 : | |
4376 : | INPUTS: |
4377 : | gridID int32 grid structure ID |
4378 : | |
4379 : | OUTPUTS: |
4380 : | dimnames char Dimension names (comma-separated) |
4381 : | dims int32 Dimension values |
4382 : | |
4383 : | |
4384 : | OUTPUTS: |
4385 : | None |
4386 : | |
4387 : | NOTES: |
4388 : | |
4389 : | |
4390 : | Date Programmer Description |
4391 : | ====== ============ ================================================= |
4392 : | Jun 96 Joel Gales Original Programmer |
4393 : | Aug 96 Joel Gales Make metadata ODL compliant |
4394 : | Feb 97 Joel Gales Set nDim to -1 if status = -1 |
4395 : | |
4396 : | END_PROLOG |
4397 : -----------------------------------------------------------------------------*/
4398 : int32
4399 : GDinqdims(int32 gridID, char *dimnames, int32 dims[])
4400 0 : {
4401 : intn status; /* routine return status variable */
4402 :
4403 : int32 fid; /* HDF-EOS file ID */
4404 : int32 sdInterfaceID; /* HDF SDS interface ID */
4405 : int32 gdVgrpID; /* Grid root Vgroup ID */
4406 : int32 size; /* Dimension size */
4407 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
4408 0 : int32 nDim = 0; /* Number of dimensions */
4409 :
4410 : char *metabuf; /* Pointer to structural metadata (SM) */
4411 : char *metaptrs[2];/* Pointers to begin and end of SM section */
4412 : char gridname[80]; /* Grid Name */
4413 : char *utlstr;/* Utility string */
4414 :
4415 : /* Allocate space for utility string */
4416 : /* --------------------------------- */
4417 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
4418 0 : if(utlstr == NULL)
4419 : {
4420 0 : HEpush(DFE_NOSPACE,"GDinqdims", __FILE__, __LINE__);
4421 0 : return(-1);
4422 : }
4423 : /* Check for valid grid id */
4424 : /* ----------------------- */
4425 0 : status = GDchkgdid(gridID, "GDinqdims", &fid, &sdInterfaceID, &gdVgrpID);
4426 :
4427 0 : if (status == 0)
4428 : {
4429 : /* If dimension names or sizes are requested */
4430 : /* ----------------------------------------- */
4431 0 : if (dimnames != NULL || dims != NULL)
4432 : {
4433 : /* Get grid name */
4434 : /* ------------- */
4435 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
4436 :
4437 :
4438 : /* Get pointers to "Dimension" section within SM */
4439 : /* --------------------------------------------- */
4440 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
4441 : "Dimension", metaptrs);
4442 0 : if(metabuf == NULL)
4443 : {
4444 0 : free(utlstr);
4445 0 : return(-1);
4446 : }
4447 :
4448 :
4449 : /* If dimension names are requested then "clear" name buffer */
4450 : /* --------------------------------------------------------- */
4451 0 : if (dimnames != NULL)
4452 : {
4453 0 : dimnames[0] = 0;
4454 : }
4455 :
4456 0 : while (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
4457 : {
4458 0 : strcpy(utlstr, "\t\tOBJECT=");
4459 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
4460 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
4461 : {
4462 : /* Get Dimension Name */
4463 : /* ------------------ */
4464 0 : if (dimnames != NULL)
4465 : {
4466 : /* Check 1st for old meta data then new */
4467 : /* ------------------------------------ */
4468 0 : EHgetmetavalue(metaptrs, "OBJECT", utlstr);
4469 0 : if (utlstr[0] != '"')
4470 : {
4471 0 : metaptrs[0] =
4472 : strstr(metaptrs[0], "\t\t\t\tDimensionName=");
4473 0 : EHgetmetavalue(metaptrs, "DimensionName", utlstr);
4474 : }
4475 :
4476 : /* Strip off double quotes */
4477 : /* ----------------------- */
4478 0 : memmove(utlstr, utlstr + 1, strlen(utlstr) - 2);
4479 0 : utlstr[strlen(utlstr) - 2] = 0;
4480 :
4481 0 : if (nDim > 0)
4482 : {
4483 0 : strcat(dimnames, ",");
4484 : }
4485 0 : strcat(dimnames, utlstr);
4486 : }
4487 :
4488 : /* Get Dimension Size */
4489 : /* ------------------ */
4490 0 : if (dims != NULL)
4491 : {
4492 0 : EHgetmetavalue(metaptrs, "Size", utlstr);
4493 0 : size = atol(utlstr);
4494 0 : dims[nDim] = size;
4495 : }
4496 0 : nDim++;
4497 : }
4498 : }
4499 0 : free(metabuf);
4500 :
4501 : }
4502 : }
4503 :
4504 :
4505 : /* Set nDim to -1 if error status exists */
4506 : /* ------------------------------------- */
4507 0 : if (status == -1)
4508 : {
4509 0 : nDim = -1;
4510 : }
4511 0 : free(utlstr);
4512 0 : return (nDim);
4513 : }
4514 :
4515 :
4516 :
4517 :
4518 :
4519 :
4520 : /*----------------------------------------------------------------------------|
4521 : | BEGIN_PROLOG |
4522 : | |
4523 : | FUNCTION: GDinqfields |
4524 : | |
4525 : | DESCRIPTION: Retrieve information about all data fields defined in a grid. |
4526 : | |
4527 : | |
4528 : | Return Value Type Units Description |
4529 : | ============ ====== ========= ===================================== |
4530 : | nFld int32 Number of fields in swath |
4531 : | |
4532 : | INPUTS: |
4533 : | gridID int32 grid structure ID |
4534 : | |
4535 : | |
4536 : | OUTPUTS: |
4537 : | fieldlist char Field names (comma-separated) |
4538 : | rank int32 Array of ranks |
4539 : | numbertype int32 Array of HDF number types |
4540 : | |
4541 : | NOTES: |
4542 : | |
4543 : | |
4544 : | Date Programmer Description |
4545 : | ====== ============ ================================================= |
4546 : | Jun 96 Joel Gales Original Programmer |
4547 : | Aug 96 Joel Gales Make metadata ODL compliant |
4548 : | Feb 97 Joel Gales Set nFld to -1 if status = -1 |
4549 : | |
4550 : | END_PROLOG |
4551 : -----------------------------------------------------------------------------*/
4552 : int32
4553 : GDinqfields(int32 gridID, char *fieldlist, int32 rank[],
4554 : int32 numbertype[])
4555 2 : {
4556 : intn status; /* routine return status variable */
4557 :
4558 : int32 fid; /* HDF-EOS file ID */
4559 : int32 sdInterfaceID; /* HDF SDS interface ID */
4560 : int32 gdVgrpID; /* Grid root Vgroup ID */
4561 2 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
4562 2 : int32 nFld = 0; /* Number of mappings */
4563 : int32 slen[8]; /* String length array */
4564 :
4565 : char *metabuf; /* Pointer to structural metadata (SM) */
4566 : char *metaptrs[2];/* Pointers to begin and end of SM section */
4567 : char gridname[80]; /* Grid Name */
4568 : char *utlstr;/* Utility string */
4569 : char *ptr[8]; /* String pointer array */
4570 :
4571 : /* Allocate space for utility string */
4572 : /* --------------------------------- */
4573 2 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
4574 2 : if(utlstr == NULL)
4575 : {
4576 0 : HEpush(DFE_NOSPACE,"GDinqfields", __FILE__, __LINE__);
4577 0 : return(-1);
4578 : }
4579 : /* Check for valid grid id */
4580 : /* ----------------------- */
4581 2 : status = GDchkgdid(gridID, "GDinqfields", &fid, &sdInterfaceID, &gdVgrpID);
4582 2 : if (status == 0)
4583 : {
4584 :
4585 : /* If field names, ranks, or number types desired ... */
4586 : /* --------------------------------------------------- */
4587 2 : if (fieldlist != NULL || rank != NULL || numbertype != NULL)
4588 : {
4589 : /* Get grid name */
4590 : /* ------------- */
4591 2 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
4592 :
4593 :
4594 : /* Get pointers to "DataField" section within SM */
4595 : /* --------------------------------------------- */
4596 2 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
4597 : "DataField", metaptrs);
4598 2 : if(metabuf == NULL)
4599 : {
4600 0 : free(utlstr);
4601 0 : return(-1);
4602 : }
4603 :
4604 :
4605 : /* If field names are desired then "clear" name buffer */
4606 : /* --------------------------------------------------- */
4607 2 : if (fieldlist != NULL)
4608 : {
4609 2 : fieldlist[0] = 0;
4610 : }
4611 :
4612 :
4613 : /* Begin loop through mapping entries in metadata */
4614 : /* ---------------------------------------------- */
4615 : while (1)
4616 : {
4617 : /* Search for OBJECT string */
4618 : /* ------------------------ */
4619 4 : metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
4620 :
4621 :
4622 : /* If found within "Data" Field metadata section .. */
4623 : /* ------------------------------------------------ */
4624 4 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
4625 : {
4626 : /* Get Fieldnames (if desired) */
4627 : /* --------------------------- */
4628 2 : if (fieldlist != NULL)
4629 : {
4630 : /* Check 1st for old meta data then new */
4631 : /* ------------------------------------ */
4632 2 : EHgetmetavalue(metaptrs, "OBJECT", utlstr);
4633 :
4634 : /*
4635 : * If OBJECT value begins with double quote then old
4636 : * metadata, field name is OBJECT value. Otherwise
4637 : * search for "DataFieldName" string
4638 : */
4639 :
4640 2 : if (utlstr[0] != '"')
4641 : {
4642 2 : strcpy(utlstr, "\t\t\t\t");
4643 2 : strcat(utlstr, "DataFieldName");
4644 2 : strcat(utlstr, "=");
4645 2 : metaptrs[0] = strstr(metaptrs[0], utlstr);
4646 2 : EHgetmetavalue(metaptrs, "DataFieldName", utlstr);
4647 : }
4648 :
4649 : /* Strip off double quotes */
4650 : /* ----------------------- */
4651 2 : REMQUOTE
4652 :
4653 :
4654 : /* Add to fieldlist */
4655 : /* ---------------- */
4656 2 : if (nFld > 0)
4657 : {
4658 0 : strcat(fieldlist, ",");
4659 : }
4660 2 : strcat(fieldlist, utlstr);
4661 :
4662 : }
4663 : /* Get Numbertype */
4664 2 : if (numbertype != NULL)
4665 : {
4666 2 : EHgetmetavalue(metaptrs, "DataType", utlstr);
4667 2 : numbertype[nFld] = EHnumstr(utlstr);
4668 : }
4669 : /*
4670 : * Get Rank (if desired) by counting # of dimensions in
4671 : * "DimList" string
4672 : */
4673 2 : if (rank != NULL)
4674 : {
4675 2 : EHgetmetavalue(metaptrs, "DimList", utlstr);
4676 2 : rank[nFld] = EHparsestr(utlstr, ',', ptr, slen);
4677 : }
4678 : /* Increment number of fields */
4679 2 : nFld++;
4680 : }
4681 : else
4682 : /* No more fields found */
4683 : {
4684 : break;
4685 : }
4686 2 : }
4687 2 : free(metabuf);
4688 : }
4689 : }
4690 :
4691 : /* Set nFld to -1 if error status exists */
4692 : /* ------------------------------------- */
4693 2 : if (status == -1)
4694 : {
4695 0 : nFld = -1;
4696 : }
4697 2 : free(utlstr);
4698 2 : return (nFld);
4699 : }
4700 :
4701 :
4702 :
4703 :
4704 :
4705 : /*----------------------------------------------------------------------------|
4706 : | BEGIN_PROLOG |
4707 : | |
4708 : | FUNCTION: GDnentries |
4709 : | |
4710 : | DESCRIPTION: Returns number of entries and descriptive string buffer |
4711 : | size for a specified entity. |
4712 : | |
4713 : | |
4714 : | Return Value Type Units Description |
4715 : | ============ ====== ========= ===================================== |
4716 : | nEntries int32 Number of entries |
4717 : | |
4718 : | INPUTS: |
4719 : | gridID int32 grid structure ID |
4720 : | entrycode int32 Entry code |
4721 : | HDFE_NENTDIM (0) |
4722 : | HDFE_NENTDFLD (4) |
4723 : | |
4724 : | |
4725 : | OUTPUTS: |
4726 : | strbufsize int32 Length of comma-separated list |
4727 : | (Does not include null-terminator |
4728 : | |
4729 : | NOTES: |
4730 : | |
4731 : | |
4732 : | Date Programmer Description |
4733 : | ====== ============ ================================================= |
4734 : | Jun 96 Joel Gales Original Programmer |
4735 : | Aug 96 Joel Gales Make metadata ODL compliant |
4736 : | Feb 97 Joel Gales Set nEntries to -1 if status = -1 |
4737 : | |
4738 : | END_PROLOG |
4739 : -----------------------------------------------------------------------------*/
4740 : int32
4741 : GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize)
4742 :
4743 2 : {
4744 : intn status; /* routine return status variable */
4745 : intn i; /* Loop index */
4746 :
4747 : int32 fid; /* HDF-EOS file ID */
4748 : int32 sdInterfaceID; /* HDF SDS interface ID */
4749 : int32 gdVgrpID; /* Grid root Vgroup ID */
4750 2 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
4751 2 : int32 nEntries = 0; /* Number of entries */
4752 : int32 metaflag; /* Old (0), New (1) metadata flag) */
4753 2 : int32 nVal = 0; /* Number of strings to search for */
4754 :
4755 2 : char *metabuf = NULL; /* Pointer to structural metadata (SM) */
4756 : char *metaptrs[2];/* Pointers to begin and end of SM section */
4757 : char gridname[80]; /* Grid Name */
4758 : char *utlstr;/* Utility string */
4759 : char valName[2][32]; /* Strings to search for */
4760 :
4761 : /* Allocate space for utility string */
4762 : /* --------------------------------- */
4763 2 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
4764 2 : if(utlstr == NULL)
4765 : {
4766 0 : HEpush(DFE_NOSPACE,"GDnentries", __FILE__, __LINE__);
4767 0 : return(-1);
4768 : }
4769 2 : status = GDchkgdid(gridID, "GDnentries", &fid, &sdInterfaceID, &gdVgrpID);
4770 :
4771 2 : if (status == 0)
4772 : {
4773 : /* Get grid name */
4774 2 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
4775 :
4776 : /* Zero out string buffer size */
4777 2 : *strbufsize = 0;
4778 :
4779 :
4780 : /*
4781 : * Get pointer to relevant section within SM and Get names of
4782 : * metadata strings to inquire about
4783 : */
4784 2 : switch (entrycode)
4785 : {
4786 : case HDFE_NENTDIM:
4787 : {
4788 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
4789 : "Dimension", metaptrs);
4790 0 : if(metabuf == NULL)
4791 : {
4792 0 : free(utlstr);
4793 0 : return(-1);
4794 : }
4795 :
4796 0 : nVal = 1;
4797 0 : strcpy(&valName[0][0], "DimensionName");
4798 : }
4799 0 : break;
4800 :
4801 : case HDFE_NENTDFLD:
4802 : {
4803 2 : metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g",
4804 : "DataField", metaptrs);
4805 2 : if(metabuf == NULL)
4806 : {
4807 0 : free(utlstr);
4808 0 : return(-1);
4809 : }
4810 :
4811 2 : nVal = 1;
4812 2 : strcpy(&valName[0][0], "DataFieldName");
4813 : }
4814 : break;
4815 : }
4816 :
4817 :
4818 : /*
4819 : * Check for presence of 'GROUP="' string If found then old metadata,
4820 : * search on OBJECT string
4821 : */
4822 2 : metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0;
4823 2 : if (metaflag == 0)
4824 : {
4825 0 : nVal = 1;
4826 0 : strcpy(&valName[0][0], "\t\tOBJECT");
4827 : }
4828 :
4829 :
4830 : /* Begin loop through entries in metadata */
4831 : /* -------------------------------------- */
4832 : while (1)
4833 : {
4834 : /* Search for first string */
4835 4 : strcpy(utlstr, &valName[0][0]);
4836 4 : strcat(utlstr, "=");
4837 4 : metaptrs[0] = strstr(metaptrs[0], utlstr);
4838 :
4839 : /* If found within relevant metadata section ... */
4840 4 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
4841 : {
4842 4 : for (i = 0; i < nVal; i++)
4843 : {
4844 : /*
4845 : * Get all string values Don't count quotes
4846 : */
4847 2 : EHgetmetavalue(metaptrs, &valName[i][0], utlstr);
4848 2 : *strbufsize += strlen(utlstr) - 2;
4849 : }
4850 : /* Increment number of entries */
4851 2 : nEntries++;
4852 :
4853 : /* Go to end of OBJECT */
4854 2 : metaptrs[0] = strstr(metaptrs[0], "END_OBJECT");
4855 : }
4856 : else
4857 : /* No more entries found */
4858 : {
4859 : break;
4860 : }
4861 2 : }
4862 2 : free(metabuf);
4863 :
4864 :
4865 : /* Count comma separators & slashes (if mappings) */
4866 : /* ---------------------------------------------- */
4867 2 : if (nEntries > 0)
4868 : {
4869 2 : *strbufsize += nEntries - 1;
4870 2 : *strbufsize += (nVal - 1) * nEntries;
4871 : }
4872 : }
4873 :
4874 :
4875 : /* Set nEntries to -1 if error status exists */
4876 : /* ----------------------------------------- */
4877 2 : if (status == -1)
4878 : {
4879 0 : nEntries = -1;
4880 : }
4881 :
4882 2 : free(utlstr);
4883 2 : return (nEntries);
4884 : }
4885 :
4886 :
4887 :
4888 :
4889 :
4890 : /*----------------------------------------------------------------------------|
4891 : | BEGIN_PROLOG |
4892 : | |
4893 : | FUNCTION: GDinqgrid |
4894 : | |
4895 : | DESCRIPTION: Returns number and names of grid structures in file |
4896 : | |
4897 : | |
4898 : | Return Value Type Units Description |
4899 : | ============ ====== ========= ===================================== |
4900 : | nGrid int32 Number of grid structures in file |
4901 : | |
4902 : | INPUTS: |
4903 : | filename char HDF-EOS filename |
4904 : | |
4905 : | OUTPUTS: |
4906 : | gridlist char List of grid names (comma-separated) |
4907 : | strbufsize int32 Length of gridlist |
4908 : | |
4909 : | NOTES: |
4910 : | |
4911 : | |
4912 : | Date Programmer Description |
4913 : | ====== ============ ================================================= |
4914 : | Jun 96 Joel Gales Original Programmer |
4915 : | |
4916 : | END_PROLOG |
4917 : -----------------------------------------------------------------------------*/
4918 : int32
4919 : GDinqgrid(char *filename, char *gridlist, int32 * strbufsize)
4920 4 : {
4921 : int32 nGrid; /* Number of grid structures in file */
4922 :
4923 : /* Call "EHinquire" routine */
4924 : /* ------------------------ */
4925 4 : nGrid = EHinquire(filename, "GRID", gridlist, strbufsize);
4926 :
4927 4 : return (nGrid);
4928 : }
4929 :
4930 :
4931 : /*----------------------------------------------------------------------------|
4932 : | BEGIN_PROLOG |
4933 : | |
4934 : | FUNCTION: GDsetfillvalue |
4935 : | |
4936 : | DESCRIPTION: Sets fill value for the specified field. |
4937 : | |
4938 : | |
4939 : | Return Value Type Units Description |
4940 : | ============ ====== ========= ===================================== |
4941 : | status intn return status (0) SUCCEED, (-1) FAIL |
4942 : | |
4943 : | INPUTS: |
4944 : | gridID int32 grid structure ID |
4945 : | fieldname char field name |
4946 : | fillval void fill value |
4947 : | |
4948 : | OUTPUTS: |
4949 : | None |
4950 : | |
4951 : | NOTES: |
4952 : | |
4953 : | |
4954 : | Date Programmer Description |
4955 : | ====== ============ ================================================= |
4956 : | Jun 96 Joel Gales Original Programmer |
4957 : | |
4958 : | END_PROLOG |
4959 : -----------------------------------------------------------------------------*/
4960 : intn
4961 : GDsetfillvalue(int32 gridID, char *fieldname, VOIDP fillval)
4962 0 : {
4963 : intn status; /* routine return status variable */
4964 :
4965 : int32 fid; /* HDF-EOS file ID */
4966 : int32 sdInterfaceID; /* HDF SDS interface ID */
4967 : int32 gdVgrpID; /* Grid root Vgroup ID */
4968 : int32 sdid; /* SDS id */
4969 : int32 nt; /* Number type */
4970 : int32 dims[8]; /* Dimensions array */
4971 : int32 dum; /* Dummy variable */
4972 : int32 solo; /* "Solo" (non-merged) field flag */
4973 :
4974 : char name[80]; /* Fill value "attribute" name */
4975 :
4976 : /* Check for valid grid ID and get SDS interface ID */
4977 0 : status = GDchkgdid(gridID, "GDsetfillvalue",
4978 : &fid, &sdInterfaceID, &gdVgrpID);
4979 :
4980 0 : if (status == 0)
4981 : {
4982 : /* Get field info */
4983 0 : status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL);
4984 :
4985 0 : if (status == 0)
4986 : {
4987 : /* Get SDS ID and solo flag */
4988 0 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname,
4989 : &sdid, &dum, &dum, &dum,
4990 : dims, &solo);
4991 :
4992 : /* If unmerged field then call HDF set field routine */
4993 0 : if (solo == 1)
4994 : {
4995 0 : status = SDsetfillvalue(sdid, fillval);
4996 : }
4997 :
4998 : /*
4999 : * Store fill value in attribute. Name is given by fieldname
5000 : * prepended with "_FV_"
5001 : */
5002 0 : strcpy(name, "_FV_");
5003 0 : strcat(name, fieldname);
5004 0 : status = GDwriteattr(gridID, name, nt, 1, fillval);
5005 :
5006 :
5007 : }
5008 : else
5009 : {
5010 0 : HEpush(DFE_GENAPP, "GDsetfillvalue", __FILE__, __LINE__);
5011 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
5012 : }
5013 : }
5014 0 : return (status);
5015 : }
5016 :
5017 :
5018 :
5019 :
5020 :
5021 :
5022 :
5023 : /*----------------------------------------------------------------------------|
5024 : | BEGIN_PROLOG |
5025 : | |
5026 : | FUNCTION: GDgetfillvalue |
5027 : | |
5028 : | DESCRIPTION: Retrieves fill value for a specified field. |
5029 : | |
5030 : | |
5031 : | Return Value Type Units Description |
5032 : | ============ ====== ========= ===================================== |
5033 : | status intn return status (0) SUCCEED, (-1) FAIL |
5034 : | |
5035 : | INPUTS: |
5036 : | gridID int32 grid structure ID |
5037 : | fieldname char field name |
5038 : | |
5039 : | OUTPUTS: |
5040 : | fillval void fill value |
5041 : | |
5042 : | NOTES: |
5043 : | |
5044 : | |
5045 : | Date Programmer Description |
5046 : | ====== ============ ================================================= |
5047 : | Jun 96 Joel Gales Original Programmer |
5048 : | |
5049 : | END_PROLOG |
5050 : -----------------------------------------------------------------------------*/
5051 : intn
5052 : GDgetfillvalue(int32 gridID, char *fieldname, VOIDP fillval)
5053 8 : {
5054 : intn status; /* routine return status variable */
5055 :
5056 : int32 nt; /* Number type */
5057 : int32 dims[8]; /* Dimensions array */
5058 : int32 dum; /* Dummy variable */
5059 :
5060 : char name[80]; /* Fill value "attribute" name */
5061 :
5062 8 : status = GDchkgdid(gridID, "GDgetfillvalue", &dum, &dum, &dum);
5063 :
5064 : /* Check for valid grid ID */
5065 8 : if (status == 0)
5066 : {
5067 : /* Get field info */
5068 0 : status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL);
5069 :
5070 0 : if (status == 0)
5071 : {
5072 : /* Read fill value attribute */
5073 0 : strcpy(name, "_FV_");
5074 0 : strcat(name, fieldname);
5075 0 : status = GDreadattr(gridID, name, fillval);
5076 : }
5077 : else
5078 : {
5079 0 : HEpush(DFE_GENAPP, "GDgetfillvalue", __FILE__, __LINE__);
5080 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
5081 : }
5082 :
5083 : }
5084 8 : return (status);
5085 : }
5086 :
5087 :
5088 :
5089 :
5090 :
5091 : /*----------------------------------------------------------------------------|
5092 : | BEGIN_PROLOG |
5093 : | |
5094 : | FUNCTION: GDdetach |
5095 : | |
5096 : | DESCRIPTION: Detaches from grid interface and performs file housekeeping. |
5097 : | |
5098 : | |
5099 : | Return Value Type Units Description |
5100 : | ============ ====== ========= ===================================== |
5101 : | status intn return status (0) SUCCEED, (-1) FAIL |
5102 : | |
5103 : | INPUTS: |
5104 : | gridID int32 grid structure ID |
5105 : | |
5106 : | |
5107 : | OUTPUTS: |
5108 : | None |
5109 : | |
5110 : | NOTES: |
5111 : | |
5112 : | |
5113 : | Date Programmer Description |
5114 : | ====== ============ ================================================= |
5115 : | Jun 96 Joel Gales Original Programmer |
5116 : | Sep 96 Joel Gales Setup dim names for SDsetdimname in dimbuf1 rather |
5117 : | that utlstr |
5118 : | Oct 96 Joel Gales Detach Grid Vgroups |
5119 : | Oct 96 Joel Gales "Detach" from SDS |
5120 : | Nov 96 Joel Gales Call GDchkgdid to check for proper grid ID |
5121 : | Dec 96 Joel Gales Add multiple vertical subsetting garbage collection |
5122 : | Oct 98 Abe Taaheri Added GDXRegion[k]->DimNamePtr[i] =0; after freeing |
5123 : | memory |
5124 : | Sep 99 Abe Taaheri Changed memcpy to memmove because of overlapping |
5125 : | source and destination for GDXSDcomb, nameptr, and |
5126 : | dimptr. memcpy may cause unexpected results. |
5127 : | |
5128 : | END_PROLOG |
5129 : -----------------------------------------------------------------------------*/
5130 : intn
5131 : GDdetach(int32 gridID)
5132 :
5133 111 : {
5134 : intn i; /* Loop index */
5135 : intn j; /* Loop index */
5136 : intn k; /* Loop index */
5137 111 : intn status = 0; /* routine return status variable */
5138 111 : intn statusFill = 0; /* return status from GDgetfillvalue */
5139 :
5140 : int32 *namelen; /* Pointer to name string length array */
5141 : int32 *dimlen; /* Pointer to dim string length array */
5142 : int32 slen1[3]; /* String length array 1 */
5143 : int32 slen2[3]; /* String length array 2 */
5144 : int32 nflds; /* Number of fields */
5145 : int32 match[5]; /* Merged field match array */
5146 : int32 cmbfldcnt; /* Number of fields combined */
5147 : int32 sdid; /* SDS ID */
5148 : int32 vgid; /* Vgroup ID */
5149 : int32 dims[3]; /* Dimension array */
5150 : int32 *offset; /* Pointer to merged field offset array */
5151 : int32 *indvdims; /* Pointer to merged field size array */
5152 : int32 sdInterfaceID; /* SDS interface ID */
5153 : int32 gID; /* Grid ID - offset */
5154 : int32 nflds0; /* Number of fields */
5155 : int32 *namelen0; /* Pointer to name string length array */
5156 : int32 rank; /* Rank of merged field */
5157 : int32 truerank; /* True rank of merged field */
5158 111 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
5159 : int32 dum; /* Dummy variable */
5160 :
5161 : char *nambuf; /* Pointer to name buffer */
5162 : char **nameptr; /* Pointer to name string pointer array */
5163 : char **dimptr; /* Pointer to dim string pointer array */
5164 : char **nameptr0; /* Pointer to name string pointer array */
5165 : char *ptr1[3]; /* String pointer array */
5166 : char *ptr2[3]; /* String pointer array */
5167 : char dimbuf1[128]; /* Dimension buffer 1 */
5168 : char dimbuf2[128]; /* Dimension buffer 2 */
5169 : char gridname[VGNAMELENMAX + 1]; /* Grid name */
5170 : char *utlbuf; /* Utility buffer */
5171 : char fillval[32];/* Fill value buffer */
5172 :
5173 :
5174 :
5175 111 : status = GDchkgdid(gridID, "GDdetach", &dum, &sdInterfaceID, &dum);
5176 :
5177 111 : if (status == 0)
5178 : {
5179 111 : gID = gridID % idOffset;
5180 111 : Vgetname(GDXGrid[gID].IDTable, gridname);
5181 :
5182 : /* SDS combined fields */
5183 : /* ------------------- */
5184 111 : if (strlen(GDXSDname) == 0)
5185 : {
5186 111 : nflds = 0;
5187 :
5188 : /* Allocate "dummy" arrays so free() doesn't bomb later */
5189 : /* ---------------------------------------------------- */
5190 111 : nameptr = (char **) calloc(1, sizeof(char *));
5191 111 : if(nameptr == NULL)
5192 : {
5193 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5194 0 : return(-1);
5195 : }
5196 111 : namelen = (int32 *) calloc(1, sizeof(int32));
5197 111 : if(namelen == NULL)
5198 : {
5199 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5200 0 : free(nameptr);
5201 0 : return(-1);
5202 : }
5203 111 : nameptr0 = (char **) calloc(1, sizeof(char *));
5204 111 : if(nameptr0 == NULL)
5205 : {
5206 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5207 0 : free(nameptr);
5208 0 : free(namelen);
5209 0 : return(-1);
5210 : }
5211 111 : namelen0 = (int32 *) calloc(1, sizeof(int32));
5212 111 : if(namelen0 == NULL)
5213 : {
5214 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5215 0 : free(nameptr);
5216 0 : free(namelen);
5217 0 : free(nameptr0);
5218 0 : return(-1);
5219 : }
5220 111 : dimptr = (char **) calloc(1, sizeof(char *));
5221 111 : if(dimptr == NULL)
5222 : {
5223 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5224 0 : free(nameptr);
5225 0 : free(namelen);
5226 0 : free(nameptr0);
5227 0 : free(namelen0);
5228 0 : return(-1);
5229 : }
5230 111 : dimlen = (int32 *) calloc(1, sizeof(int32));
5231 111 : if(dimlen == NULL)
5232 : {
5233 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5234 0 : free(nameptr);
5235 0 : free(namelen);
5236 0 : free(nameptr0);
5237 0 : free(namelen0);
5238 0 : free(dimptr);
5239 0 : return(-1);
5240 : }
5241 111 : offset = (int32 *) calloc(1, sizeof(int32));
5242 111 : if(offset == NULL)
5243 : {
5244 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5245 0 : free(nameptr);
5246 0 : free(namelen);
5247 0 : free(nameptr0);
5248 0 : free(namelen0);
5249 0 : free(dimptr);
5250 0 : free(dimlen);
5251 0 : return(-1);
5252 : }
5253 111 : indvdims = (int32 *) calloc(1, sizeof(int32));
5254 111 : if(indvdims == NULL)
5255 : {
5256 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5257 0 : free(nameptr);
5258 0 : free(namelen);
5259 0 : free(nameptr0);
5260 0 : free(namelen0);
5261 0 : free(dimptr);
5262 0 : free(dimlen);
5263 0 : free(offset);
5264 0 : return(-1);
5265 : }
5266 : }
5267 : else
5268 : {
5269 : /*
5270 : * "Trim Off" trailing "," and ";" in GDXSDname & GDXSDdims
5271 : * respectively
5272 : */
5273 0 : GDXSDname[strlen(GDXSDname) - 1] = 0;
5274 0 : GDXSDdims[strlen(GDXSDdims) - 1] = 0;
5275 :
5276 :
5277 : /* Get number of fields from GDXSDname string */
5278 : /* ------------------------------------------ */
5279 0 : nflds = EHparsestr(GDXSDname, ',', NULL, NULL);
5280 :
5281 : /* Allocate space for various dynamic arrays */
5282 : /* ----------------------------------------- */
5283 0 : nameptr = (char **) calloc(nflds, sizeof(char *));
5284 0 : if(nameptr == NULL)
5285 : {
5286 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5287 0 : return(-1);
5288 : }
5289 0 : namelen = (int32 *) calloc(nflds, sizeof(int32));
5290 0 : if(namelen == NULL)
5291 : {
5292 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5293 0 : free(nameptr);
5294 0 : return(-1);
5295 : }
5296 0 : nameptr0 = (char **) calloc(nflds, sizeof(char *));
5297 0 : if(nameptr0 == NULL)
5298 : {
5299 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5300 0 : free(nameptr);
5301 0 : free(namelen);
5302 0 : return(-1);
5303 : }
5304 0 : namelen0 = (int32 *) calloc(nflds, sizeof(int32));
5305 0 : if(namelen0 == NULL)
5306 : {
5307 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5308 0 : free(nameptr);
5309 0 : free(namelen);
5310 0 : free(nameptr0);
5311 0 : return(-1);
5312 : }
5313 0 : dimptr = (char **) calloc(nflds, sizeof(char *));
5314 0 : if(dimptr == NULL)
5315 : {
5316 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5317 0 : free(nameptr);
5318 0 : free(namelen);
5319 0 : free(nameptr0);
5320 0 : free(namelen0);
5321 0 : return(-1);
5322 : }
5323 0 : dimlen = (int32 *) calloc(nflds, sizeof(int32));
5324 0 : if(dimlen == NULL)
5325 : {
5326 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5327 0 : free(nameptr);
5328 0 : free(namelen);
5329 0 : free(nameptr0);
5330 0 : free(namelen0);
5331 0 : free(dimptr);
5332 0 : return(-1);
5333 : }
5334 0 : offset = (int32 *) calloc(nflds, sizeof(int32));
5335 0 : if(offset == NULL)
5336 : {
5337 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5338 0 : free(nameptr);
5339 0 : free(namelen);
5340 0 : free(nameptr0);
5341 0 : free(namelen0);
5342 0 : free(dimptr);
5343 0 : free(dimlen);
5344 0 : return(-1);
5345 : }
5346 0 : indvdims = (int32 *) calloc(nflds, sizeof(int32));
5347 0 : if(indvdims == NULL)
5348 : {
5349 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5350 0 : free(nameptr);
5351 0 : free(namelen);
5352 0 : free(nameptr0);
5353 0 : free(namelen0);
5354 0 : free(dimptr);
5355 0 : free(dimlen);
5356 0 : free(offset);
5357 0 : return(-1);
5358 : }
5359 :
5360 : /* Parse GDXSDname and GDXSDdims strings */
5361 : /* ------------------------------------- */
5362 0 : nflds = EHparsestr(GDXSDname, ',', nameptr, namelen);
5363 0 : nflds = EHparsestr(GDXSDdims, ';', dimptr, dimlen);
5364 : }
5365 :
5366 :
5367 111 : for (i = 0; i < nflds; i++)
5368 : {
5369 0 : if (GDXSDcomb[5 * i] != 0 &&
5370 : GDXSDcomb[5 * i + 3] == GDXGrid[gID].IDTable)
5371 : {
5372 0 : nambuf = (char *) calloc(strlen(GDXSDname) + 1, 1);
5373 0 : if(nambuf == NULL)
5374 : {
5375 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5376 0 : free(nameptr);
5377 0 : free(namelen);
5378 0 : free(nameptr0);
5379 0 : free(namelen0);
5380 0 : free(dimptr);
5381 0 : free(dimlen);
5382 0 : free(offset);
5383 0 : return(-1);
5384 : }
5385 0 : utlbuf = (char *) calloc(strlen(GDXSDname) * 2 + 7, 1);
5386 0 : if(utlbuf == NULL)
5387 : {
5388 0 : HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__);
5389 0 : free(nambuf);
5390 0 : free(nameptr);
5391 0 : free(namelen);
5392 0 : free(nameptr0);
5393 0 : free(namelen0);
5394 0 : free(dimptr);
5395 0 : free(dimlen);
5396 0 : free(offset);
5397 0 : return(-1);
5398 : }
5399 :
5400 0 : for (k = 0; k < sizeof(dimbuf1); k++)
5401 0 : dimbuf1[k] = 0;
5402 :
5403 :
5404 : /* Load array to match, name & parse dims */
5405 : /* -------------------------------------- */
5406 0 : memcpy(match, &GDXSDcomb[5 * i], 20);
5407 0 : memcpy(nambuf, nameptr[i], namelen[i]);
5408 :
5409 0 : memcpy(dimbuf1, dimptr[i], dimlen[i]);
5410 0 : dum = EHparsestr(dimbuf1, ',', ptr1, slen1);
5411 :
5412 :
5413 : /* Separate combined dimension from others */
5414 : /* --------------------------------------- */
5415 0 : dimbuf1[slen1[0]] = 0;
5416 :
5417 0 : offset[0] = 0;
5418 0 : indvdims[0] = abs(match[0]);
5419 :
5420 0 : for (j = i + 1, cmbfldcnt = 0; j < nflds; j++)
5421 : {
5422 0 : for (k = 0; k < sizeof(dimbuf2); k++)
5423 0 : dimbuf2[k] = 0;
5424 0 : memcpy(dimbuf2, dimptr[j], dimlen[j]);
5425 0 : dum = EHparsestr(dimbuf2, ',', ptr2, slen2);
5426 0 : dimbuf2[slen2[0]] = 0;
5427 :
5428 :
5429 0 : if (GDXSDcomb[5 * j] != 0 &&
5430 : strcmp(dimbuf1 + slen1[0],
5431 : dimbuf2 + slen2[0]) == 0 &&
5432 : match[1] == GDXSDcomb[5 * j + 1] &&
5433 : match[2] == GDXSDcomb[5 * j + 2] &&
5434 : match[3] == GDXSDcomb[5 * j + 3] &&
5435 : match[4] == GDXSDcomb[5 * j + 4])
5436 : {
5437 : /* Add to combined dimension size */
5438 0 : match[0] += GDXSDcomb[5 * j];
5439 :
5440 : /* Concatanate name */
5441 0 : strcat(nambuf, ",");
5442 0 : memcpy(nambuf + strlen(nambuf),
5443 : nameptr[j], namelen[j]);
5444 :
5445 : /* Store individual dims and dim offsets */
5446 0 : cmbfldcnt++;
5447 0 : indvdims[cmbfldcnt] = abs(GDXSDcomb[5 * j]);
5448 0 : offset[cmbfldcnt] =
5449 : offset[cmbfldcnt - 1] + indvdims[cmbfldcnt - 1];
5450 :
5451 0 : GDXSDcomb[5 * j] = 0;
5452 : }
5453 : }
5454 :
5455 :
5456 : /* Create SDS */
5457 : /* ---------- */
5458 0 : nflds0 = EHparsestr(nambuf, ',', nameptr0, namelen0);
5459 :
5460 0 : if (abs(match[0]) == 1)
5461 : {
5462 0 : for (k = 0; k < 2; k++)
5463 0 : dims[k] = abs(match[k + 1]);
5464 :
5465 0 : rank = 2;
5466 :
5467 0 : sdid = SDcreate(sdInterfaceID, nambuf,
5468 : GDXSDcomb[5 * i + 4], 2, dims);
5469 : }
5470 : else
5471 : {
5472 0 : for (k = 0; k < 3; k++)
5473 0 : dims[k] = abs(match[k]);
5474 :
5475 0 : rank = 3;
5476 :
5477 0 : if (cmbfldcnt > 0)
5478 : {
5479 0 : strcpy(utlbuf, "MRGFLD_");
5480 0 : memcpy(utlbuf + 7, nameptr0[0], namelen0[0]);
5481 0 : utlbuf[7 + namelen0[0]] = 0;
5482 0 : strcat(utlbuf, ":");
5483 0 : strcat(utlbuf, nambuf);
5484 :
5485 0 : status = EHinsertmeta(sdInterfaceID, gridname, "g",
5486 : 6L, utlbuf, NULL);
5487 : }
5488 : else
5489 : {
5490 0 : strcpy(utlbuf, nambuf);
5491 : }
5492 :
5493 0 : sdid = SDcreate(sdInterfaceID, utlbuf,
5494 : GDXSDcomb[5 * i + 4], 3, dims);
5495 :
5496 :
5497 0 : if (cmbfldcnt > 0)
5498 : {
5499 0 : SDsetattr(sdid, "Field Dims", DFNT_INT32,
5500 : cmbfldcnt + 1, (VOIDP) indvdims);
5501 :
5502 0 : SDsetattr(sdid, "Field Offsets", DFNT_INT32,
5503 : cmbfldcnt + 1, (VOIDP) offset);
5504 : }
5505 :
5506 : }
5507 :
5508 :
5509 :
5510 : /* Register Dimensions in SDS */
5511 : /* -------------------------- */
5512 0 : for (k = 0; k < rank; k++)
5513 : {
5514 0 : if (rank == 2)
5515 : {
5516 0 : memcpy(dimbuf2, ptr1[k + 1], slen1[k + 1]);
5517 0 : dimbuf2[slen1[k + 1]] = 0;
5518 : }
5519 : else
5520 : {
5521 0 : memcpy(dimbuf2, ptr1[k], slen1[k]);
5522 0 : dimbuf2[slen1[k]] = 0;
5523 : }
5524 :
5525 :
5526 0 : if (k == 0 && rank > 2 && cmbfldcnt > 0)
5527 : {
5528 0 : sprintf(dimbuf2, "%s%s_%d", "MRGDIM:",
5529 : gridname, (int)dims[0]);
5530 : }
5531 : else
5532 : {
5533 0 : strcat(dimbuf2, ":");
5534 0 : strcat(dimbuf2, gridname);
5535 : }
5536 0 : SDsetdimname(SDgetdimid(sdid, k), (char *) dimbuf2);
5537 : }
5538 :
5539 :
5540 :
5541 : /* Write Fill Value */
5542 : /* ---------------- */
5543 0 : for (k = 0; k < nflds0; k++)
5544 : {
5545 0 : memcpy(utlbuf, nameptr0[k], namelen0[k]);
5546 0 : utlbuf[namelen[k]] = 0;
5547 0 : statusFill = GDgetfillvalue(gridID, utlbuf, fillval);
5548 :
5549 0 : if (statusFill == 0)
5550 : {
5551 0 : if (cmbfldcnt > 0)
5552 : {
5553 0 : dims[0] = indvdims[k];
5554 0 : truerank = (dims[0] == 1) ? 2 : 3;
5555 0 : EHfillfld(sdid, rank, truerank,
5556 : DFKNTsize(match[4]), offset[k],
5557 : dims, fillval);
5558 : }
5559 : else
5560 : {
5561 0 : status = SDsetfillvalue(sdid, fillval);
5562 : }
5563 : }
5564 : }
5565 :
5566 :
5567 0 : vgid = GDXGrid[gID].VIDTable[0];
5568 0 : Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid));
5569 0 : SDendaccess(sdid);
5570 :
5571 0 : free(nambuf);
5572 0 : free(utlbuf);
5573 :
5574 : }
5575 : }
5576 :
5577 :
5578 111 : for (i = 0; i < nflds; i++)
5579 : {
5580 0 : if (GDXSDcomb[5 * i + 3] == GDXGrid[gID].IDTable)
5581 : {
5582 0 : if (i == (nflds - 1))
5583 : {
5584 0 : GDXSDcomb[5 * i] = 0;
5585 0 : *(nameptr[i] - (nflds != 1)) = 0;
5586 0 : *(dimptr[i] - (nflds != 1)) = 0;
5587 : }
5588 : else
5589 : {
5590 : /* memcpy(&GDXSDcomb[5 * i],
5591 : &GDXSDcomb[5 * (i + 1)],
5592 : (512 - i - 1) * 5 * 4);*/
5593 0 : memmove(&GDXSDcomb[5 * i],
5594 : &GDXSDcomb[5 * (i + 1)],
5595 : (512 - i - 1) * 5 * 4);
5596 : /* memcpy(nameptr[i],
5597 : nameptr[i + 1],
5598 : nameptr[0] + 2048 - nameptr[i + 1] - 1);*/
5599 0 : memmove(nameptr[i],
5600 : nameptr[i + 1],
5601 : nameptr[0] + 2048 - nameptr[i + 1] - 1);
5602 : /* memcpy(dimptr[i],
5603 : dimptr[i + 1],
5604 : dimptr[0] + 2048 * 2 - dimptr[i + 1] - 1);*/
5605 0 : memmove(dimptr[i],
5606 : dimptr[i + 1],
5607 : dimptr[0] + 2048 * 2 - dimptr[i + 1] - 1);
5608 : }
5609 :
5610 0 : i--;
5611 0 : nflds = EHparsestr(GDXSDname, ',', nameptr, namelen);
5612 0 : nflds = EHparsestr(GDXSDdims, ';', dimptr, dimlen);
5613 : }
5614 : }
5615 :
5616 111 : if (nflds != 0)
5617 : {
5618 0 : strcat(GDXSDname, ",");
5619 0 : strcat(GDXSDdims, ";");
5620 : }
5621 :
5622 :
5623 :
5624 : /* Free up a bunch of dynamically allocated arrays */
5625 : /* ----------------------------------------------- */
5626 111 : free(nameptr);
5627 111 : free(namelen);
5628 111 : free(nameptr0);
5629 111 : free(namelen0);
5630 111 : free(dimptr);
5631 111 : free(dimlen);
5632 111 : free(offset);
5633 111 : free(indvdims);
5634 :
5635 :
5636 :
5637 : /* "Detach" from previously attached SDSs */
5638 : /* -------------------------------------- */
5639 1447 : for (k = 0; k < GDXGrid[gID].nSDS; k++)
5640 : {
5641 1336 : SDendaccess(GDXGrid[gID].sdsID[k]);
5642 : }
5643 111 : free(GDXGrid[gID].sdsID);
5644 111 : GDXGrid[gID].sdsID = 0;
5645 111 : GDXGrid[gID].nSDS = 0;
5646 :
5647 :
5648 :
5649 : /* Detach Grid Vgroups */
5650 : /* ------------------- */
5651 111 : Vdetach(GDXGrid[gID].VIDTable[0]);
5652 111 : Vdetach(GDXGrid[gID].VIDTable[1]);
5653 111 : Vdetach(GDXGrid[gID].IDTable);
5654 :
5655 111 : GDXGrid[gID].active = 0;
5656 111 : GDXGrid[gID].VIDTable[0] = 0;
5657 111 : GDXGrid[gID].VIDTable[1] = 0;
5658 111 : GDXGrid[gID].IDTable = 0;
5659 111 : GDXGrid[gID].fid = 0;
5660 :
5661 :
5662 :
5663 :
5664 : /* Free Region Pointers */
5665 : /* -------------------- */
5666 28527 : for (k = 0; k < NGRIDREGN; k++)
5667 : {
5668 28416 : if (GDXRegion[k] != 0 &&
5669 : GDXRegion[k]->gridID == gridID)
5670 : {
5671 0 : for (i = 0; i < 8; i++)
5672 : {
5673 0 : if (GDXRegion[k]->DimNamePtr[i] != 0)
5674 : {
5675 0 : free(GDXRegion[k]->DimNamePtr[i]);
5676 0 : GDXRegion[k]->DimNamePtr[i] = 0;
5677 : }
5678 : }
5679 :
5680 0 : free(GDXRegion[k]);
5681 0 : GDXRegion[k] = 0;
5682 : }
5683 : }
5684 : }
5685 111 : return (status);
5686 : }
5687 :
5688 :
5689 : /*----------------------------------------------------------------------------|
5690 : | BEGIN_PROLOG |
5691 : | |
5692 : | FUNCTION: GDclose |
5693 : | |
5694 : | DESCRIPTION: Closes file. |
5695 : | |
5696 : | |
5697 : | Return Value Type Units Description |
5698 : | ============ ====== ========= ===================================== |
5699 : | status intn return status (0) SUCCEED, (-1) FAIL |
5700 : | |
5701 : | INPUTS: |
5702 : | fid int32 File ID |
5703 : | |
5704 : | |
5705 : | OUTPUTS: |
5706 : | None |
5707 : | |
5708 : | NOTES: |
5709 : | |
5710 : | |
5711 : | Date Programmer Description |
5712 : | ====== ============ ================================================= |
5713 : | Jun 96 Joel Gales Original Programmer |
5714 : | |
5715 : | END_PROLOG |
5716 : -----------------------------------------------------------------------------*/
5717 : intn
5718 : GDclose(int32 fid)
5719 :
5720 12 : {
5721 12 : intn status = 0; /* routine return status variable */
5722 :
5723 : /* Call EHclose to perform file close */
5724 : /* ---------------------------------- */
5725 12 : status = EHclose(fid);
5726 :
5727 12 : return (status);
5728 : }
5729 :
5730 :
5731 : /*----------------------------------------------------------------------------|
5732 : | BEGIN_PROLOG |
5733 : | |
5734 : | FUNCTION: GDgetdefaults |
5735 : | |
5736 : | DESCRIPTION: |
5737 : | |
5738 : | |
5739 : | Return Value Type Units Description |
5740 : | ============ ====== ========= ===================================== |
5741 : | status intn return status (0) SUCCEED, (-1) FAIL |
5742 : | |
5743 : | INPUTS: |
5744 : | projcode int32 GCTP projection code |
5745 : | zonecode int32 UTM zone code |
5746 : | projparm float64 Projection parameters |
5747 : | spherecode int32 GCTP spheriod code |
5748 : | upleftpt float64 upper left corner coordinates |
5749 : | lowrightpt float64 lower right corner coordinates |
5750 : | |
5751 : | |
5752 : | OUTPUTS: |
5753 : | upleftpt float64 upper left corner coordinates |
5754 : | lowrightpt float64 lower right corner coordinates |
5755 : | |
5756 : | NOTES: |
5757 : | |
5758 : | |
5759 : | Date Programmer Description |
5760 : | ====== ============ ================================================= |
5761 : | Aug 96 Joel Gales Original Programmer |
5762 : | Sep 96 Raj Gejjaga Fixed bugs in Polar Stereographic and Goode | | Homolosine default calculations. |
5763 : | Sep 96 Raj Gejjaga Added code to compute default boundary points |
5764 : | for Lambert Azimuthal Polar and Equitorial |
5765 : | projections. |
5766 : | Feb 97 Raj Gejjaga Added code to compute default boundary points |
5767 : | for Integerized Sinusoidal Grid. Added error |
5768 : | handling code. |
5769 : | Jun 00 Abe Taaheri Added support for EASE grid |
5770 : | |
5771 : | END_PROLOG |
5772 : -----------------------------------------------------------------------------*/
5773 : static intn
5774 : GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[],
5775 : int32 spherecode, float64 upleftpt[], float64 lowrightpt[])
5776 0 : {
5777 0 : int32 errorcode = 0, status = 0;
5778 : int32(*for_trans[100]) ();
5779 :
5780 : float64 lon, lat, plat, x, y;
5781 : float64 plon, tlon, llon, rlon, pplon, LLon, LLat, RLon, RLat;
5782 :
5783 :
5784 : /* invoke GCTP initialization routine */
5785 : /* ---------------------------------- */
5786 0 : for_init(projcode, zonecode, projparm, spherecode, NULL, NULL,
5787 : &errorcode, for_trans);
5788 :
5789 : /* Report error if any */
5790 : /* ------------------- */
5791 0 : if (errorcode != 0)
5792 : {
5793 0 : status = -1;
5794 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5795 0 : HEreport("GCTP Error: %d\n", errorcode);
5796 0 : return (status);
5797 : }
5798 :
5799 : /* Compute Default Boundary Points for EASE Grid */
5800 : /* Use Global coverage */
5801 : /* ------------------------------------------------------ */
5802 0 : if (projcode == GCTP_BCEA &&
5803 : upleftpt[0] == 0 && upleftpt[1] == 0 &&
5804 : lowrightpt[0] == 0 && lowrightpt[1] == 0)
5805 : {
5806 0 : upleftpt[0] = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LON, HDFE_DEG_DMS);
5807 0 : upleftpt[1] = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LAT, HDFE_DEG_DMS);
5808 0 : lowrightpt[0] = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LON, HDFE_DEG_DMS);
5809 0 : lowrightpt[1] = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LAT, HDFE_DEG_DMS);
5810 : }
5811 :
5812 : /* Compute Default Boundary Points for CEA */
5813 : /* --------------------------------------------*/
5814 0 : if (projcode == GCTP_CEA &&
5815 : upleftpt[0] == 0 && upleftpt[1] == 0 &&
5816 : lowrightpt[0] == 0 && lowrightpt[1] == 0)
5817 : {
5818 0 : LLon = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LON, HDFE_DEG_RAD);
5819 0 : LLat = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LAT, HDFE_DEG_RAD);
5820 0 : RLon = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LON, HDFE_DEG_RAD);
5821 0 : RLat = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LAT, HDFE_DEG_RAD);
5822 :
5823 0 : errorcode = for_trans[projcode] (LLon, LLat, &x, &y);
5824 0 : if (errorcode != 0)
5825 : {
5826 0 : status = -1;
5827 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5828 0 : HEreport("GCTP Error: %d\n", errorcode);
5829 0 : return (status);
5830 : }
5831 0 : upleftpt[0] = x;
5832 0 : upleftpt[1] = y;
5833 :
5834 0 : errorcode = for_trans[projcode] (RLon, RLat, &x, &y);
5835 0 : if (errorcode != 0)
5836 : {
5837 0 : status = -1;
5838 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5839 0 : HEreport("GCTP Error: %d\n", errorcode);
5840 0 : return (status);
5841 : }
5842 0 : lowrightpt[0] = x;
5843 0 : lowrightpt[1] = y;
5844 :
5845 : }
5846 :
5847 :
5848 : /* Compute Default Boundary Points for Polar Sterographic */
5849 : /* ------------------------------------------------------ */
5850 0 : if (projcode == GCTP_PS &&
5851 : upleftpt[0] == 0 && upleftpt[1] == 0 &&
5852 : lowrightpt[0] == 0 && lowrightpt[1] == 0)
5853 : {
5854 : /*
5855 : * Convert the longitude and latitude from the DMS to decimal degree
5856 : * format.
5857 : */
5858 0 : plon = EHconvAng(projparm[4], HDFE_DMS_DEG);
5859 0 : plat = EHconvAng(projparm[5], HDFE_DMS_DEG);
5860 :
5861 : /*
5862 : * Compute the longitudes at 90, 180 and 270 degrees from the central
5863 : * longitude.
5864 : */
5865 :
5866 0 : if (plon <= 0.0)
5867 : {
5868 0 : tlon = 180.0 + plon;
5869 0 : pplon = plon + 360.0;
5870 : }
5871 : else
5872 : {
5873 0 : tlon = plon - 180.0;
5874 0 : pplon = plon;
5875 : }
5876 :
5877 0 : rlon = pplon + 90.0;
5878 0 : if (rlon > 360.0)
5879 0 : rlon = rlon - 360;
5880 :
5881 0 : if (rlon > 180.0)
5882 0 : rlon = rlon - 360.0;
5883 :
5884 0 : if (rlon <= 0.0)
5885 0 : llon = 180.0 + rlon;
5886 : else
5887 0 : llon = rlon - 180.0;
5888 :
5889 :
5890 : /* Convert all four longitudes from decimal degrees to radians */
5891 0 : plon = EHconvAng(plon, HDFE_DEG_RAD);
5892 0 : tlon = EHconvAng(tlon, HDFE_DEG_RAD);
5893 0 : llon = EHconvAng(llon, HDFE_DEG_RAD);
5894 0 : rlon = EHconvAng(rlon, HDFE_DEG_RAD);
5895 :
5896 0 : errorcode = for_trans[projcode] (llon, 0.0, &x, &y);
5897 0 : if (errorcode != 0)
5898 : {
5899 0 : status = -1;
5900 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5901 0 : HEreport("GCTP Error: %d\n", errorcode);
5902 0 : return (status);
5903 : }
5904 :
5905 0 : upleftpt[0] = x;
5906 :
5907 0 : errorcode = for_trans[projcode] (rlon, 0.0, &x, &y);
5908 0 : if (errorcode != 0)
5909 : {
5910 0 : status = -1;
5911 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5912 0 : HEreport("GCTP Error: %d\n", errorcode);
5913 0 : return (status);
5914 : }
5915 :
5916 0 : lowrightpt[0] = x;
5917 :
5918 : /*
5919 : * Compute the upperleft and lowright y values based on the south or
5920 : * north polar projection
5921 : */
5922 :
5923 0 : if (plat < 0.0)
5924 : {
5925 0 : errorcode = for_trans[projcode] (plon, 0.0, &x, &y);
5926 0 : if (errorcode != 0)
5927 : {
5928 0 : status = -1;
5929 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5930 0 : HEreport("GCTP Error: %d\n", errorcode);
5931 0 : return (status);
5932 : }
5933 :
5934 0 : upleftpt[1] = y;
5935 :
5936 0 : errorcode = for_trans[projcode] (tlon, 0.0, &x, &y);
5937 0 : if (errorcode != 0)
5938 : {
5939 0 : status = -1;
5940 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5941 0 : HEreport("GCTP Error: %d\n", errorcode);
5942 0 : return (status);
5943 : }
5944 :
5945 0 : lowrightpt[1] = y;
5946 :
5947 : }
5948 : else
5949 : {
5950 0 : errorcode = for_trans[projcode] (tlon, 0.0, &x, &y);
5951 0 : if (errorcode != 0)
5952 : {
5953 0 : status = -1;
5954 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5955 0 : HEreport("GCTP Error: %d\n", errorcode);
5956 0 : return (status);
5957 : }
5958 :
5959 0 : upleftpt[1] = y;
5960 :
5961 0 : errorcode = for_trans[projcode] (plon, 0.0, &x, &y);
5962 0 : if (errorcode != 0)
5963 : {
5964 0 : status = -1;
5965 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5966 0 : HEreport("GCTP Error: %d\n", errorcode);
5967 0 : return (status);
5968 : }
5969 :
5970 0 : lowrightpt[1] = y;
5971 :
5972 : }
5973 : }
5974 :
5975 :
5976 : /* Compute Default Boundary Points for Goode Homolosine */
5977 : /* ---------------------------------------------------- */
5978 0 : if (projcode == GCTP_GOOD &&
5979 : upleftpt[0] == 0 && upleftpt[1] == 0 &&
5980 : lowrightpt[0] == 0 && lowrightpt[1] == 0)
5981 : {
5982 0 : lon = EHconvAng(-180, HDFE_DEG_RAD);
5983 0 : lat = 0.0;
5984 :
5985 0 : errorcode = for_trans[projcode] (lon, lat, &x, &y);
5986 0 : if (errorcode != 0)
5987 : {
5988 0 : status = -1;
5989 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
5990 0 : HEreport("GCTP Error: %d\n", errorcode);
5991 0 : return (status);
5992 : }
5993 :
5994 0 : upleftpt[0] = -fabs(x);
5995 0 : lowrightpt[0] = +fabs(x);
5996 :
5997 0 : lat = EHconvAng(90, HDFE_DEG_RAD);
5998 :
5999 0 : errorcode = for_trans[projcode] (lon, lat, &x, &y);
6000 0 : if (errorcode != 0)
6001 : {
6002 0 : status = -1;
6003 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6004 0 : HEreport("GCTP Error: %d\n", errorcode);
6005 0 : return (status);
6006 : }
6007 :
6008 0 : upleftpt[1] = +fabs(y);
6009 0 : lowrightpt[1] = -fabs(y);
6010 : }
6011 :
6012 : /* Compute Default Boundary Points for Lambert Azimuthal */
6013 : /* ----------------------------------------------------- */
6014 0 : if (projcode == GCTP_LAMAZ &&
6015 : upleftpt[0] == 0 && upleftpt[1] == 0 &&
6016 : lowrightpt[0] == 0 && lowrightpt[1] == 0)
6017 : {
6018 : /*
6019 : * Convert the longitude and latitude from the DMS to decimal degree
6020 : * format.
6021 : */
6022 0 : plon = EHconvAng(projparm[4], HDFE_DMS_DEG);
6023 0 : plat = EHconvAng(projparm[5], HDFE_DMS_DEG);
6024 :
6025 : /*
6026 : * Compute the longitudes at 90, 180 and 270 degrees from the central
6027 : * longitude.
6028 : */
6029 :
6030 0 : if (plon <= 0.0)
6031 : {
6032 0 : tlon = 180.0 + plon;
6033 0 : pplon = plon + 360.0;
6034 : }
6035 : else
6036 : {
6037 0 : tlon = plon - 180.0;
6038 0 : pplon = plon;
6039 : }
6040 :
6041 0 : rlon = pplon + 90.0;
6042 0 : if (rlon > 360.0)
6043 0 : rlon = rlon - 360;
6044 :
6045 0 : if (rlon > 180.0)
6046 0 : rlon = rlon - 360.0;
6047 :
6048 0 : if (rlon <= 0.0)
6049 0 : llon = 180.0 + rlon;
6050 : else
6051 0 : llon = rlon - 180.0;
6052 :
6053 : /* Convert all four longitudes from decimal degrees to radians */
6054 0 : plon = EHconvAng(plon, HDFE_DEG_RAD);
6055 0 : tlon = EHconvAng(tlon, HDFE_DEG_RAD);
6056 0 : llon = EHconvAng(llon, HDFE_DEG_RAD);
6057 0 : rlon = EHconvAng(rlon, HDFE_DEG_RAD);
6058 :
6059 0 : errorcode = for_trans[projcode] (llon, 0.0, &x, &y);
6060 0 : if (errorcode != 0)
6061 : {
6062 0 : status = -1;
6063 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6064 0 : HEreport("GCTP Error: %d\n", errorcode);
6065 0 : return (status);
6066 : }
6067 :
6068 0 : upleftpt[0] = x;
6069 :
6070 0 : errorcode = for_trans[projcode] (rlon, 0.0, &x, &y);
6071 0 : if (errorcode != 0)
6072 : {
6073 0 : status = -1;
6074 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6075 0 : HEreport("GCTP Error: %d\n", errorcode);
6076 0 : return (status);
6077 : }
6078 :
6079 0 : lowrightpt[0] = x;
6080 :
6081 : /*
6082 : * Compute upperleft and lowerright values based on whether the
6083 : * projection is south polar, north polar or equitorial
6084 : */
6085 :
6086 0 : if (plat == -90.0)
6087 : {
6088 0 : errorcode = for_trans[projcode] (plon, 0.0, &x, &y);
6089 0 : if (errorcode != 0)
6090 : {
6091 0 : status = -1;
6092 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6093 0 : HEreport("GCTP Error: %d\n", errorcode);
6094 0 : return (status);
6095 : }
6096 :
6097 0 : upleftpt[1] = y;
6098 :
6099 0 : errorcode = for_trans[projcode] (tlon, 0.0, &x, &y);
6100 0 : if (errorcode != 0)
6101 : {
6102 0 : status = -1;
6103 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6104 0 : HEreport("GCTP Error: %d\n", errorcode);
6105 0 : return (status);
6106 : }
6107 :
6108 0 : lowrightpt[1] = y;
6109 : }
6110 0 : else if (plat == 90.0)
6111 : {
6112 0 : errorcode = for_trans[projcode] (tlon, 0.0, &x, &y);
6113 0 : if (errorcode != 0)
6114 : {
6115 0 : status = -1;
6116 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6117 0 : HEreport("GCTP Error: %d\n", errorcode);
6118 0 : return (status);
6119 : }
6120 :
6121 0 : upleftpt[1] = y;
6122 :
6123 0 : errorcode = for_trans[projcode] (plon, 0.0, &x, &y);
6124 0 : if (errorcode != 0)
6125 : {
6126 0 : status = -1;
6127 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6128 0 : HEreport("GCTP Error: %d\n", errorcode);
6129 0 : return (status);
6130 : }
6131 :
6132 0 : lowrightpt[1] = y;
6133 : }
6134 : else
6135 : {
6136 0 : lat = EHconvAng(90, HDFE_DEG_RAD);
6137 0 : errorcode = for_trans[projcode] (plon, lat, &x, &y);
6138 0 : if (errorcode != 0)
6139 : {
6140 0 : status = -1;
6141 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6142 0 : HEreport("GCTP Error: %d\n", errorcode);
6143 0 : return (status);
6144 : }
6145 :
6146 0 : upleftpt[1] = y;
6147 :
6148 0 : lat = EHconvAng(-90, HDFE_DEG_RAD);
6149 0 : errorcode = for_trans[projcode] (plon, lat, &x, &y);
6150 0 : if (errorcode != 0)
6151 : {
6152 0 : status = -1;
6153 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6154 0 : HEreport("GCTP Error: %d\n", errorcode);
6155 0 : return (status);
6156 : }
6157 :
6158 0 : lowrightpt[1] = y;
6159 : }
6160 : }
6161 :
6162 : /* Compute Default Boundary Points for Integerized Sinusoidal Grid */
6163 : /* --------------------------------------------------------------- */
6164 0 : if (((projcode == GCTP_ISINUS) || (projcode == GCTP_ISINUS1)) &&
6165 : upleftpt[0] == 0 && upleftpt[1] == 0 &&
6166 : lowrightpt[0] == 0 && lowrightpt[1] == 0)
6167 : {
6168 : /*
6169 : * Convert the longitude and latitude from the DMS to decimal degree
6170 : * format.
6171 : */
6172 0 : plon = EHconvAng(projparm[4], HDFE_DMS_DEG);
6173 0 : plat = EHconvAng(projparm[5], HDFE_DMS_DEG);
6174 :
6175 : /*
6176 : * Compute the longitudes at 90, 180 and 270 degrees from the central
6177 : * longitude.
6178 : */
6179 :
6180 0 : if (plon <= 0.0)
6181 : {
6182 0 : tlon = 180.0 + plon;
6183 0 : pplon = plon + 360.0;
6184 : }
6185 : else
6186 : {
6187 0 : tlon = plon - 180.0;
6188 0 : pplon = plon;
6189 : }
6190 :
6191 0 : rlon = pplon + 90.0;
6192 0 : if (rlon > 360.0)
6193 0 : rlon = rlon - 360;
6194 :
6195 0 : if (rlon > 180.0)
6196 0 : rlon = rlon - 360.0;
6197 :
6198 0 : if (rlon <= 0.0)
6199 0 : llon = 180.0 + rlon;
6200 : else
6201 0 : llon = rlon - 180.0;
6202 :
6203 : /* Convert all four longitudes from decimal degrees to radians */
6204 0 : plon = EHconvAng(plon, HDFE_DEG_RAD);
6205 0 : tlon = EHconvAng(tlon, HDFE_DEG_RAD);
6206 0 : llon = EHconvAng(llon, HDFE_DEG_RAD);
6207 0 : rlon = EHconvAng(rlon, HDFE_DEG_RAD);
6208 :
6209 0 : errorcode = for_trans[projcode] (llon, 0.0, &x, &y);
6210 0 : if (errorcode != 0)
6211 : {
6212 0 : status = -1;
6213 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6214 0 : HEreport("GCTP Error: %d\n", errorcode);
6215 0 : return (status);
6216 : }
6217 :
6218 0 : upleftpt[0] = x;
6219 :
6220 0 : errorcode = for_trans[projcode] (rlon, 0.0, &x, &y);
6221 0 : if (errorcode != 0)
6222 : {
6223 0 : status = -1;
6224 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6225 0 : HEreport("GCTP Error: %d\n", errorcode);
6226 0 : return (status);
6227 : }
6228 :
6229 0 : lowrightpt[0] = x;
6230 :
6231 0 : lat = EHconvAng(90, HDFE_DEG_RAD);
6232 0 : errorcode = for_trans[projcode] (plon, lat, &x, &y);
6233 0 : if (errorcode != 0)
6234 : {
6235 0 : status = -1;
6236 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6237 0 : HEreport("GCTP Error: %d\n", errorcode);
6238 0 : return (status);
6239 : }
6240 :
6241 0 : upleftpt[1] = y;
6242 :
6243 0 : lat = EHconvAng(-90, HDFE_DEG_RAD);
6244 0 : errorcode = for_trans[projcode] (plon, lat, &x, &y);
6245 0 : if (errorcode != 0)
6246 : {
6247 0 : status = -1;
6248 0 : HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__);
6249 0 : HEreport("GCTP Error: %d\n", errorcode);
6250 0 : return (status);
6251 : }
6252 :
6253 0 : lowrightpt[1] = y;
6254 : }
6255 0 : return (errorcode);
6256 : }
6257 :
6258 : /*----------------------------------------------------------------------------|
6259 : | BEGIN_PROLOG |
6260 : | |
6261 : | FUNCTION: GDll2ij |
6262 : | |
6263 : | DESCRIPTION: |
6264 : | |
6265 : | |
6266 : | Return Value Type Units Description |
6267 : | ============ ====== ========= ===================================== |
6268 : | status intn return status (0) SUCCEED, (-1) FAIL |
6269 : | |
6270 : | INPUTS: |
6271 : | projcode int32 GCTP projection code |
6272 : | zonecode int32 UTM zone code |
6273 : | projparm float64 Projection parameters |
6274 : | spherecode int32 GCTP spheriod code |
6275 : | xdimsize int32 xdimsize from GDcreate |
6276 : | ydimsize int32 ydimsize from GDcreate |
6277 : | upleftpt float64 upper left corner coordinates |
6278 : | lowrightpt float64 lower right corner coordinates |
6279 : | npnts int32 number of lon-lat points |
6280 : | longitude float64 longitude array (radians) |
6281 : | latitude float64 latitude array (radians) |
6282 : | |
6283 : | OUTPUTS: |
6284 : | row int32 Row array |
6285 : | col int32 Column array |
6286 : | xval float64 X value array |
6287 : | yval float64 Y value array |
6288 : | |
6289 : | |
6290 : | NOTES: |
6291 : | |
6292 : | |
6293 : | Date Programmer Description |
6294 : | ====== ============ ================================================= |
6295 : | Jun 96 Joel Gales Original Programmer |
6296 : | Aug 96 Joel Gales Return x and y values if requested |
6297 : | Jun 00 Abe Taaheri Added support for EASE grid |
6298 : | |
6299 : | END_PROLOG |
6300 : -----------------------------------------------------------------------------*/
6301 : static intn
6302 : GDll2ij(int32 projcode, int32 zonecode, float64 projparm[],
6303 : int32 spherecode, int32 xdimsize, int32 ydimsize,
6304 : float64 upleftpt[], float64 lowrightpt[],
6305 : int32 npnts, float64 longitude[], float64 latitude[],
6306 : int32 row[], int32 col[], float64 xval[], float64 yval[])
6307 :
6308 :
6309 0 : {
6310 : intn i; /* Loop index */
6311 0 : intn status = 0; /* routine return status variable */
6312 :
6313 0 : int32 errorcode = 0; /* GCTP error code */
6314 : int32(*for_trans[100]) (); /* GCTP function pointer */
6315 :
6316 : float64 xVal; /* Scaled x distance */
6317 : float64 yVal; /* Scaled y distance */
6318 : float64 xMtr; /* X value in meters from GCTP */
6319 : float64 yMtr; /* Y value in meters from GCTP */
6320 : float64 lonrad0; /* Longitude in radians of upleft point */
6321 : float64 latrad0; /* Latitude in radians of upleft point */
6322 : float64 lonrad; /* Longitude in radians of point */
6323 : float64 latrad; /* Latitude in radians of point */
6324 : float64 scaleX; /* X scale factor */
6325 : float64 scaleY; /* Y scale factor */
6326 : float64 EHconvAng();/* Angle conversion routine */
6327 : float64 xMtr0, xMtr1, yMtr0, yMtr1;
6328 : float64 lonrad1; /* Longitude in radians of lowright point */
6329 :
6330 : /* If projection not GEO call GCTP initialization routine */
6331 : /* ------------------------------------------------------ */
6332 0 : if (projcode != GCTP_GEO)
6333 : {
6334 0 : for_init(projcode, zonecode, projparm, spherecode, NULL, NULL,
6335 : &errorcode, for_trans);
6336 :
6337 : /* Report error if any */
6338 : /* ------------------- */
6339 0 : if (errorcode != 0)
6340 : {
6341 0 : status = -1;
6342 0 : HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__);
6343 0 : HEreport("GCTP Error: %d\n", errorcode);
6344 : }
6345 : }
6346 :
6347 :
6348 0 : if (status == 0)
6349 : {
6350 : /* GEO projection */
6351 : /* -------------- */
6352 0 : if (projcode == GCTP_GEO)
6353 : {
6354 : /* Convert upleft and lowright X coords from DMS to radians */
6355 : /* -------------------------------------------------------- */
6356 0 : lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD);
6357 0 : lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD);
6358 :
6359 : /* Compute x scale factor */
6360 : /* ---------------------- */
6361 0 : scaleX = (lonrad - lonrad0) / xdimsize;
6362 :
6363 :
6364 : /* Convert upleft and lowright Y coords from DMS to radians */
6365 : /* -------------------------------------------------------- */
6366 0 : latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD);
6367 0 : latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD);
6368 :
6369 :
6370 : /* Compute y scale factor */
6371 : /* ---------------------- */
6372 0 : scaleY = (latrad - latrad0) / ydimsize;
6373 : }
6374 :
6375 : /* BCEA projection */
6376 : /* -------------- */
6377 0 : else if ( projcode == GCTP_BCEA)
6378 : {
6379 : /* Convert upleft and lowright X coords from DMS to radians */
6380 : /* -------------------------------------------------------- */
6381 :
6382 0 : lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD);
6383 0 : lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD);
6384 :
6385 : /* Convert upleft and lowright Y coords from DMS to radians */
6386 : /* -------------------------------------------------------- */
6387 0 : latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD);
6388 0 : latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD);
6389 :
6390 : /* Convert from lon/lat to meters(or whatever unit is, i.e unit
6391 : of r_major and r_minor) using GCTP */
6392 : /* ----------------------------------------- */
6393 0 : errorcode = for_trans[projcode] (lonrad0, latrad0, &xMtr0, &yMtr0);
6394 :
6395 :
6396 : /* Report error if any */
6397 : /* ------------------- */
6398 0 : if (errorcode != 0)
6399 : {
6400 0 : status = -1;
6401 0 : HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__);
6402 0 : HEreport("GCTP Error: %d\n", errorcode);
6403 0 : return (status);
6404 : }
6405 :
6406 : /* Convert from lon/lat to meters(or whatever unit is, i.e unit
6407 : of r_major and r_minor) using GCTP */
6408 : /* ----------------------------------------- */
6409 0 : errorcode = for_trans[projcode] (lonrad, latrad, &xMtr1, &yMtr1);
6410 :
6411 :
6412 : /* Report error if any */
6413 : /* ------------------- */
6414 0 : if (errorcode != 0)
6415 : {
6416 0 : status = -1;
6417 0 : HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__);
6418 0 : HEreport("GCTP Error: %d\n", errorcode);
6419 0 : return (status);
6420 : }
6421 :
6422 : /* Compute x scale factor */
6423 : /* ---------------------- */
6424 0 : scaleX = (xMtr1 - xMtr0) / xdimsize;
6425 :
6426 : /* Compute y scale factor */
6427 : /* ---------------------- */
6428 0 : scaleY = (yMtr1 - yMtr0) / ydimsize;
6429 : }
6430 : else
6431 : {
6432 : /* Non-GEO, Non_BCEA projections */
6433 : /* ---------------------------- */
6434 :
6435 : /* Compute x & y scale factors */
6436 : /* --------------------------- */
6437 0 : scaleX = (lowrightpt[0] - upleftpt[0]) / xdimsize;
6438 0 : scaleY = (lowrightpt[1] - upleftpt[1]) / ydimsize;
6439 : }
6440 :
6441 :
6442 :
6443 : /* Loop through all points */
6444 : /* ----------------------- */
6445 0 : for (i = 0; i < npnts; i++)
6446 : {
6447 : /* Convert lon & lat from decimal degrees to radians */
6448 : /* ------------------------------------------------- */
6449 0 : lonrad = EHconvAng(longitude[i], HDFE_DEG_RAD);
6450 0 : latrad = EHconvAng(latitude[i], HDFE_DEG_RAD);
6451 :
6452 :
6453 : /* GEO projection */
6454 : /* -------------- */
6455 0 : if (projcode == GCTP_GEO)
6456 : {
6457 : /*allow map to span dateline */
6458 0 : lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD);
6459 0 : lonrad1 = EHconvAng(lowrightpt[0], HDFE_DMS_RAD);
6460 : /* if time-line is paased */
6461 0 : if(lonrad < lonrad1)
6462 : {
6463 0 : if (lonrad < lonrad0) lonrad += 2.0 * M_PI1;
6464 0 : if (lonrad > lonrad1) lonrad -= 2.0 * M_PI1;
6465 : }
6466 :
6467 : /* Compute scaled distance to point from origin */
6468 : /* -------------------------------------------- */
6469 0 : xVal = (lonrad - lonrad0) / scaleX;
6470 0 : yVal = (latrad - latrad0) / scaleY;
6471 : }
6472 : else
6473 : {
6474 : /* Convert from lon/lat to meters using GCTP */
6475 : /* ----------------------------------------- */
6476 0 : errorcode = for_trans[projcode] (lonrad, latrad, &xMtr, &yMtr);
6477 :
6478 :
6479 : /* Report error if any */
6480 : /* ------------------- */
6481 0 : if (errorcode != 0)
6482 : {
6483 : /*status = -1;
6484 : HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__);
6485 : HEreport("GCTP Error: %d\n", errorcode);
6486 : return (status); */ /* Bruce Beaumont */
6487 0 : xVal = -2147483648.0; /* Bruce Beaumont */
6488 0 : yVal = -2147483648.0; /* Bruce Beaumont */
6489 : }/* (Note: MAXLONG is defined as 2147483647.0 in
6490 : function cproj.c of GCTP) */
6491 : else {
6492 : /* if projection is BCEA normalize x and y by cell size and
6493 : measure it from the uperleft corner of the grid */
6494 :
6495 : /* Compute scaled distance to point from origin */
6496 : /* -------------------------------------------- */
6497 0 : if( projcode == GCTP_BCEA)
6498 : {
6499 0 : xVal = (xMtr - xMtr0) / scaleX;
6500 0 : yVal = (yMtr - yMtr0) / scaleY;
6501 : }
6502 : else
6503 : {
6504 0 : xVal = (xMtr - upleftpt[0]) / scaleX;
6505 0 : yVal = (yMtr - upleftpt[1]) / scaleY;
6506 : }
6507 : }
6508 : }
6509 :
6510 :
6511 : /* Compute row and col from scaled distance */
6512 : /* ---------------------------------------- */
6513 0 : col[i] = (int32) xVal;
6514 0 : row[i] = (int32) yVal;
6515 :
6516 : /* Store scaled distances if requested */
6517 : /* ----------------------------------- */
6518 0 : if (xval != NULL)
6519 : {
6520 0 : xval[i] = xVal;
6521 : }
6522 :
6523 0 : if (yval != NULL)
6524 : {
6525 0 : yval[i] = yVal;
6526 : }
6527 : }
6528 : }
6529 0 : return (status);
6530 : }
6531 :
6532 :
6533 :
6534 :
6535 : /*----------------------------------------------------------------------------|
6536 : | BEGIN_PROLOG |
6537 : | |
6538 : | FUNCTION: GDrs2ll |
6539 : | |
6540 : | DESCRIPTION: Converts EASE grid's (r,s) coordinates to longitude and |
6541 : | latritude (in decimal degrees). |
6542 : | |
6543 : | |
6544 : | Return Value Type Units Description |
6545 : | ============ ====== ========= ===================================== |
6546 : | status intn return status (0) SUCCEED, (-1) FAIL |
6547 : | |
6548 : | INPUTS: |
6549 : | projcode int32 GCTP projection code |
6550 : | projparm float64 Projection parameters |
6551 : | xdimsize int32 xdimsize from GDcreate |
6552 : | ydimsize int32 ydimsize from GDcreate |
6553 : | pixcen int32 pixel center code |
6554 : | npnts int32 number of lon-lat points |
6555 : | s int32 s coordinate |
6556 : | r int32 r coordinate |
6557 : | pixcen int32 Code from GDpixreginfo |
6558 : | pixcnr int32 Code from GDorigininfo |
6559 : | upleft float64 upper left corner coordinates (DMS) |
6560 : | lowright float64 lower right corner coordinates (DMS) |
6561 : | |
6562 : | |
6563 : | OUTPUTS: |
6564 : | longitude float64 longitude array (decimal degrees) |
6565 : | latitude float64 latitude array (decimal degrees) |
6566 : | |
6567 : | NOTES: |
6568 : | |
6569 : | |
6570 : | Date Programmer Description |
6571 : | ====== ============ ================================================= |
6572 : | Jul 00 Abe Taaheri Original Programmer |
6573 : | |
6574 : | END_PROLOG |
6575 : -----------------------------------------------------------------------------*/
6576 : intn
6577 : GDrs2ll(int32 projcode, float64 projparm[],
6578 : int32 xdimsize, int32 ydimsize,
6579 : float64 upleft[], float64 lowright[],
6580 : int32 npnts, float64 r[], float64 s[],
6581 : float64 longitude[], float64 latitude[], int32 pixcen, int32 pixcnr)
6582 0 : {
6583 : intn i; /* Loop index */
6584 0 : intn status = 0; /* routine return status variable */
6585 :
6586 0 : int32 errorcode = 0; /* GCTP error code */
6587 : int32(*inv_trans[100]) (); /* GCTP function pointer */
6588 :
6589 0 : float64 pixadjX = 0.0; /* Pixel adjustment (x) */
6590 0 : float64 pixadjY = 0.0; /* Pixel adjustment (y) */
6591 : float64 lonrad; /* Longitude in radians of point */
6592 : float64 latrad; /* Latitude in radians of point */
6593 : float64 EHconvAng(); /* Angle conversion routine */
6594 : float64 xMtr; /* X value in meters from GCTP */
6595 : float64 yMtr; /* Y value in meters from GCTP */
6596 : float64 epsilon;
6597 : float64 beta;
6598 : float64 qp_cea;
6599 : float64 kz_cea;
6600 : float64 eccen, eccen_sq;
6601 : float64 phi1, sinphi1, cosphi1;
6602 : float64 scaleX, scaleY;
6603 :
6604 0 : int32 zonecode=0;
6605 :
6606 0 : int32 spherecode=0;
6607 : float64 lon[2],lat[2];
6608 : float64 xcor[2], ycor[2];
6609 : int32 nlatlon;
6610 :
6611 : /* If projection is BCEA define scale, r0 and s0 */
6612 0 : if (projcode == GCTP_BCEA)
6613 : {
6614 0 : eccen_sq = 1.0 - SQUARE(projparm[1]/projparm[0]);
6615 0 : eccen = sqrt(eccen_sq);
6616 0 : if(eccen < 0.00001)
6617 : {
6618 0 : qp_cea = 2.0;
6619 : }
6620 : else
6621 : {
6622 0 : qp_cea =
6623 : (1.0 - eccen_sq)*((1.0/(1.0 - eccen_sq))-(1.0/(2.0*eccen))*
6624 : log((1.0 - eccen)/(1.0 + eccen)));
6625 : }
6626 0 : phi1 = EHconvAng(projparm[5],HDFE_DMS_RAD);
6627 0 : cosphi1 = cos(phi1);
6628 0 : sinphi1 = sin(phi1);
6629 0 : kz_cea = cosphi1/(sqrt(1.0 - (eccen_sq*sinphi1*sinphi1)));
6630 : }
6631 :
6632 :
6633 :
6634 :
6635 : /* Compute adjustment of position within pixel */
6636 : /* ------------------------------------------- */
6637 0 : if (pixcen == HDFE_CENTER)
6638 : {
6639 : /* Pixel defined at center */
6640 : /* ----------------------- */
6641 0 : pixadjX = 0.5;
6642 0 : pixadjY = 0.5;
6643 : }
6644 : else
6645 : {
6646 0 : switch (pixcnr)
6647 : {
6648 : case HDFE_GD_UL:
6649 : {
6650 : /* Pixel defined at upper left corner */
6651 : /* ---------------------------------- */
6652 0 : pixadjX = 0.0;
6653 0 : pixadjY = 0.0;
6654 0 : break;
6655 : }
6656 :
6657 : case HDFE_GD_UR:
6658 : {
6659 : /* Pixel defined at upper right corner */
6660 : /* ----------------------------------- */
6661 0 : pixadjX = 1.0;
6662 0 : pixadjY = 0.0;
6663 0 : break;
6664 : }
6665 :
6666 : case HDFE_GD_LL:
6667 : {
6668 : /* Pixel defined at lower left corner */
6669 : /* ---------------------------------- */
6670 0 : pixadjX = 0.0;
6671 0 : pixadjY = 1.0;
6672 0 : break;
6673 : }
6674 :
6675 : case HDFE_GD_LR:
6676 : {
6677 : /* Pixel defined at lower right corner */
6678 : /* ----------------------------------- */
6679 0 : pixadjX = 1.0;
6680 0 : pixadjY = 1.0;
6681 : break;
6682 : }
6683 :
6684 : }
6685 : }
6686 :
6687 : /* If projection is BCEA call GCTP initialization routine */
6688 : /* ------------------------------------------------------ */
6689 0 : if (projcode == GCTP_BCEA)
6690 : {
6691 :
6692 0 : inv_init(projcode, 0, projparm, 0, NULL, NULL,
6693 : &errorcode, inv_trans);
6694 :
6695 : /* Report error if any */
6696 : /* ------------------- */
6697 0 : if (errorcode != 0)
6698 : {
6699 0 : status = -1;
6700 0 : HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__);
6701 0 : HEreport("GCTP Error: %d\n", errorcode);
6702 : }
6703 : else
6704 : {
6705 : /* For each point ... */
6706 : /* ------------------ */
6707 0 : for (i = 0; i < npnts; i++)
6708 : {
6709 : /* Convert from EASE grid's (r,s) to lon/lat (radians)
6710 : using GCTP */
6711 : /* --------------------------------------------------- */
6712 0 : nlatlon = 2;
6713 0 : lon[0] = upleft[0];
6714 0 : lon[1] = lowright[0];
6715 0 : lat[0] = upleft[1];
6716 0 : lat[1] = lowright[1];
6717 0 : status =
6718 : GDll2mm_cea(projcode,zonecode,spherecode,projparm,
6719 : xdimsize, ydimsize,
6720 : upleft, lowright, nlatlon,
6721 : lon, lat,
6722 : xcor, ycor, &scaleX, &scaleY);
6723 :
6724 0 : if (status == -1)
6725 : {
6726 0 : HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__);
6727 0 : return (status);
6728 : }
6729 :
6730 0 : xMtr = (r[i]/ scaleX + pixadjX - 0.5)* scaleX;
6731 0 : yMtr = - (s[i]/fabs(scaleY) + pixadjY - 0.5)* fabs(scaleY);
6732 :
6733 :
6734 : /* allow .5 cell tolerance in arcsin function
6735 : (used in bceainv function) so that grid
6736 : coordinates which are less than .5 cells
6737 : above 90.00N or below 90.00S are given lat of 90.00
6738 : */
6739 :
6740 0 : epsilon = 1 + 0.5 * (fabs(scaleY)/projparm[0]);
6741 0 : beta = 2.0 * (yMtr - projparm[7]) * kz_cea/(projparm[0] * qp_cea);
6742 :
6743 0 : if( fabs (beta) > epsilon)
6744 : {
6745 0 : status = -1;
6746 0 : HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__);
6747 0 : HEreport("GCTP Error: %s %s %s\n", "grid coordinates",
6748 : "are more than .5 cells",
6749 : "above 90.00N or below 90.00S. ");
6750 0 : return (status);
6751 : }
6752 0 : else if( beta <= -1)
6753 : {
6754 0 : errorcode = inv_trans[projcode] (xMtr, 0.0,
6755 : &lonrad, &latrad);
6756 0 : latrad = - M_PI1/2;
6757 : }
6758 0 : else if( beta >= 1)
6759 : {
6760 0 : errorcode = inv_trans[projcode] (xMtr, 0.0,
6761 : &lonrad, &latrad);
6762 0 : latrad = M_PI1/2;
6763 : }
6764 : else
6765 : {
6766 0 : errorcode = inv_trans[projcode] (xMtr, yMtr,
6767 : &lonrad, &latrad);
6768 : }
6769 :
6770 : /* Report error if any */
6771 : /* ------------------- */
6772 0 : if (errorcode != 0)
6773 : {
6774 0 : status = -1;
6775 0 : HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__);
6776 0 : HEreport("GCTP Error: %d\n", errorcode);
6777 0 : return (status);
6778 : }
6779 :
6780 : /* Convert from radians to decimal degrees */
6781 : /* --------------------------------------- */
6782 0 : longitude[i] = EHconvAng(lonrad, HDFE_RAD_DEG);
6783 0 : latitude[i] = EHconvAng(latrad, HDFE_RAD_DEG);
6784 : }
6785 : }
6786 : }
6787 :
6788 :
6789 :
6790 :
6791 0 : return (status);
6792 : }
6793 :
6794 :
6795 :
6796 : /*----------------------------------------------------------------------------|
6797 : | BEGIN_PROLOG |
6798 : | |
6799 : | FUNCTION: lamaxDxDtheta |
6800 : | |
6801 : | DESCRIPTION: Partial derivative along longitude line for Lambert Azimuthal |
6802 : | |
6803 : | |
6804 : | Return Value Type Units Description |
6805 : | ============ ====== ========= ===================================== |
6806 : | float64 Dx/D(theta) for LAMAZ projection |
6807 : | |
6808 : | INPUTS: |
6809 : | parms float64 Parameters defining partial derivative |
6810 : | |
6811 : | OUTPUTS: |
6812 : | None |
6813 : | |
6814 : | NOTES: |
6815 : | |
6816 : | |
6817 : | Date Programmer Description |
6818 : | ====== ============ ================================================= |
6819 : | Nov 96 Joel Gales Original Programmer |
6820 : | |
6821 : | END_PROLOG |
6822 : -----------------------------------------------------------------------------*/
6823 : float64
6824 : lamazDxDtheta(float64 parms[])
6825 0 : {
6826 : float64 snTheta, sn2Theta, snTheta1, csTheta1, csLamda;
6827 :
6828 0 : snTheta = sin(EHconvAng(parms[0], HDFE_DEG_RAD));
6829 0 : sn2Theta = sin(2 * EHconvAng(parms[0], HDFE_DEG_RAD));
6830 0 : snTheta1 = sin(EHconvAng(parms[1], HDFE_DEG_RAD));
6831 0 : csTheta1 = cos(EHconvAng(parms[1], HDFE_DEG_RAD));
6832 0 : csLamda = cos(EHconvAng(parms[2], HDFE_DEG_RAD) -
6833 : EHconvAng(parms[3], HDFE_DEG_RAD));
6834 :
6835 0 : return (4 * snTheta +
6836 : (csTheta1 * csLamda * sn2Theta) +
6837 : (2 * snTheta1 * (1 + (snTheta * snTheta))));
6838 : }
6839 :
6840 :
6841 :
6842 :
6843 : /*----------------------------------------------------------------------------|
6844 : | BEGIN_PROLOG |
6845 : | |
6846 : | FUNCTION: lamaxDxDlamda |
6847 : | |
6848 : | DESCRIPTION: Partial derivative along latitude line for Lambert Azimuthal |
6849 : | |
6850 : | |
6851 : | Return Value Type Units Description |
6852 : | ============ ====== ========= ===================================== |
6853 : | float64 Dx/D(lamda) for LAMAZ projection |
6854 : | |
6855 : | INPUTS: |
6856 : | parms float64 Parameters defining partial derivative |
6857 : | |
6858 : | OUTPUTS: |
6859 : | None |
6860 : | |
6861 : | NOTES: |
6862 : | |
6863 : | |
6864 : | Date Programmer Description |
6865 : | ====== ============ ================================================= |
6866 : | Nov 96 Joel Gales Original Programmer |
6867 : | |
6868 : | END_PROLOG |
6869 : -----------------------------------------------------------------------------*/
6870 : float64
6871 : lamazDxDlamda(float64 parms[])
6872 0 : {
6873 : float64 snTheta, csTheta, snTheta1, csTheta1, csLamda;
6874 : float64 cs, sn;
6875 :
6876 0 : snTheta = sin(EHconvAng(parms[2], HDFE_DEG_RAD));
6877 0 : csTheta = cos(EHconvAng(parms[2], HDFE_DEG_RAD));
6878 0 : snTheta1 = sin(EHconvAng(parms[1], HDFE_DEG_RAD));
6879 0 : csTheta1 = cos(EHconvAng(parms[1], HDFE_DEG_RAD));
6880 0 : csLamda = cos(EHconvAng(parms[0], HDFE_DEG_RAD) -
6881 : EHconvAng(parms[3], HDFE_DEG_RAD));
6882 :
6883 0 : cs = csTheta * csTheta1;
6884 0 : sn = snTheta * snTheta1;
6885 :
6886 0 : return (cs + (2 * (1 + sn) + (cs * csLamda)) * csLamda);
6887 : }
6888 :
6889 :
6890 :
6891 : /*----------------------------------------------------------------------------|
6892 : | BEGIN_PROLOG |
6893 : | |
6894 : | FUNCTION: lamaxDyDtheta |
6895 : | |
6896 : | DESCRIPTION: Partial derivative along longitude line for Lambert Azimuthal |
6897 : | |
6898 : | |
6899 : | Return Value Type Units Description |
6900 : | ============ ====== ========= ===================================== |
6901 : | float64 Dy/D(theta) for LAMAZ projection |
6902 : | |
6903 : | INPUTS: |
6904 : | parms float64 Parameters defining partial derivative |
6905 : | |
6906 : | OUTPUTS: |
6907 : | None |
6908 : | |
6909 : | NOTES: |
6910 : | |
6911 : | |
6912 : | Date Programmer Description |
6913 : | ====== ============ ================================================= |
6914 : | Nov 96 Joel Gales Original Programmer |
6915 : | |
6916 : | END_PROLOG |
6917 : -----------------------------------------------------------------------------*/
6918 : float64
6919 : lamazDyDtheta(float64 parms[])
6920 0 : {
6921 : float64 snTheta, csTheta, snTheta1, csTheta1, csLamda;
6922 : float64 sn2, cs2, sndiff;
6923 :
6924 0 : snTheta = sin(EHconvAng(parms[0], HDFE_DEG_RAD));
6925 0 : csTheta = cos(EHconvAng(parms[0], HDFE_DEG_RAD));
6926 0 : snTheta1 = sin(EHconvAng(parms[1], HDFE_DEG_RAD));
6927 0 : csTheta1 = cos(EHconvAng(parms[1], HDFE_DEG_RAD));
6928 0 : csLamda = cos(EHconvAng(parms[2], HDFE_DEG_RAD) -
6929 : EHconvAng(parms[3], HDFE_DEG_RAD));
6930 :
6931 0 : sn2 = snTheta1 * snTheta;
6932 0 : cs2 = csTheta1 * csTheta;
6933 0 : sndiff = snTheta1 - snTheta;
6934 :
6935 0 : return (cs2 * (sn2 * (1 + (csLamda * csLamda)) + 2) +
6936 : csLamda * (2 * (1 + sn2 * sn2) - (sndiff * sndiff)));
6937 : }
6938 :
6939 :
6940 :
6941 : /*----------------------------------------------------------------------------|
6942 : | BEGIN_PROLOG |
6943 : | |
6944 : | FUNCTION: homDyDtheta |
6945 : | |
6946 : | DESCRIPTION: Partial derivative along longitude line for Oblique Mercator |
6947 : | |
6948 : | |
6949 : | Return Value Type Units Description |
6950 : | ============ ====== ========= ===================================== |
6951 : | float64 Dx/D(theta) for HOM projection |
6952 : | |
6953 : | INPUTS: |
6954 : | parms float64 Parameters defining partial derivative |
6955 : | |
6956 : | OUTPUTS: |
6957 : | None |
6958 : | |
6959 : | NOTES: |
6960 : | |
6961 : | |
6962 : | Date Programmer Description |
6963 : | ====== ============ ================================================= |
6964 : | Mar 97 Joel Gales Original Programmer |
6965 : | |
6966 : | END_PROLOG |
6967 : -----------------------------------------------------------------------------*/
6968 : float64
6969 : homDyDtheta(float64 parms[])
6970 0 : {
6971 : float64 tnTheta, tnTheta1, snLamda;
6972 :
6973 0 : tnTheta = tan(EHconvAng(parms[0], HDFE_DEG_RAD));
6974 0 : tnTheta1 = tan(EHconvAng(parms[1], HDFE_DEG_RAD));
6975 0 : snLamda = cos(EHconvAng(parms[2], HDFE_DEG_RAD) -
6976 : EHconvAng(parms[3], HDFE_DEG_RAD));
6977 :
6978 0 : return (tnTheta * snLamda + tnTheta1);
6979 : }
6980 :
6981 :
6982 :
6983 :
6984 : /*----------------------------------------------------------------------------|
6985 : | BEGIN_PROLOG |
6986 : | |
6987 : | FUNCTION: GDtangentpnts |
6988 : | |
6989 : | DESCRIPTION: Finds tangent points along lon/lat lines |
6990 : | |
6991 : | |
6992 : | Return Value Type Units Description |
6993 : | ============ ====== ========= ===================================== |
6994 : | status intn return status (0) SUCCEED, (-1) FAIL |
6995 : | |
6996 : | INPUTS: |
6997 : | projcode int32 Projection code |
6998 : | projparm float64 Projection parameters |
6999 : | cornerlon float64 dec deg Longitude of opposite corners of box |
7000 : | cornerlat float64 dec deg Latitude of opposite corners of box |
7001 : | longitude float64 dec deg Longitude of points to check |
7002 : | latitude float64 dec deg Latitude of points to check |
7003 : | |
7004 : | |
7005 : | OUTPUTS: |
7006 : | npnts int32 Number of points to check in subset |
7007 : | |
7008 : | NOTES: |
7009 : | |
7010 : | |
7011 : | Date Programmer Description |
7012 : | ====== ============ ================================================= |
7013 : | Nov 96 Joel Gales Original Programmer |
7014 : | Mar 97 Joel Gales Add support for LAMCC, POLYC, TM |
7015 : | Aug 99 Abe Taaheri Add support for ALBERS, and MERCAT projections. |
7016 : | Also changed misstyped bisectParm[2] to |
7017 : | bisectParm[3] for HOM projection. |
7018 : | Jun 00 Abe Taaheri Added support for EASE grid |
7019 : | |
7020 : | END_PROLOG |
7021 : -----------------------------------------------------------------------------*/
7022 : static intn
7023 : GDtangentpnts(int32 projcode, float64 projparm[], float64 cornerlon[],
7024 : float64 cornerlat[], float64 longitude[], float64 latitude[],
7025 : int32 * npnts)
7026 0 : {
7027 : intn i; /* Loop index */
7028 0 : intn status = 0; /* routine return status variable */
7029 :
7030 : float64 lonrad; /* Longitude (radians) */
7031 : float64 latrad; /* Latitude (radians) */
7032 : float64 cs[4]; /* Cosine array */
7033 : float64 sn[4]; /* Sine array */
7034 : float64 csTest; /* Cosine test value */
7035 : float64 snTest; /* Sine test value */
7036 : float64 crs01; /* Cross product */
7037 : float64 crsTest[2]; /* Cross product array */
7038 : float64 longPol; /* Longitude beneath pole */
7039 : float64 minLat; /* Minimum latitude */
7040 : float64 bisectParm[4]; /* Bisection parameters */
7041 : float64 tanLat; /* Tangent latitude */
7042 : float64 tanLon; /* Tangent lontitude */
7043 : float64 dotPrd; /* Dot product */
7044 : float64 centMerd; /* Central Meridian */
7045 : float64 orgLat; /* Latitude of origin */
7046 : float64 dpi; /* Double precision pi */
7047 :
7048 : float64 lamazDxDtheta(); /* Lambert Azimuthal Dx/Dtheta */
7049 : float64 lamazDxDlamda(); /* Lambert Azimuthal Dx/Dlamda */
7050 : float64 lamazDyDtheta(); /* Lambert Azimuthal Dy/Dtheta */
7051 : float64 homDyDtheta(); /* Oblique Mercator Dy/Dtheta */
7052 :
7053 :
7054 : /* Conpute pi (double precsion) */
7055 : /* ---------------------------- */
7056 0 : dpi = atan(1.0) * 4;
7057 :
7058 :
7059 0 : switch (projcode)
7060 : {
7061 : case GCTP_MERCAT:
7062 : {
7063 : /* No need for tangent points, since MERCAT projection
7064 : is rectangular */
7065 : }
7066 0 : break;
7067 : case GCTP_BCEA:
7068 : {
7069 : /* No need for tangent points, since BCEA projection
7070 : is rectangular */
7071 : }
7072 0 : break;
7073 : case GCTP_CEA:
7074 : {
7075 : /* No need for tangent points, since CEA projection
7076 : is rectangular */
7077 : }
7078 0 : break;
7079 :
7080 : case GCTP_PS:
7081 : {
7082 : /* Add "xy axis" points for Polar Stereographic if necessary */
7083 : /* --------------------------------------------------------- */
7084 :
7085 :
7086 : /* Get minimum of corner latitudes */
7087 : /* ------------------------------- */
7088 0 : minLat = (fabs(cornerlat[0]) <= fabs(cornerlat[1]))
7089 : ? cornerlat[0] : cornerlat[1];
7090 :
7091 :
7092 : /* Compute sine and cosine of corner longitudes */
7093 : /* -------------------------------------------- */
7094 0 : for (i = 0; i < 2; i++)
7095 : {
7096 0 : lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD);
7097 0 : cs[i] = cos(lonrad);
7098 0 : sn[i] = sin(lonrad);
7099 : }
7100 :
7101 :
7102 : /* Compute cross product */
7103 : /* --------------------- */
7104 0 : crs01 = cs[0] * sn[1] - cs[1] * sn[0];
7105 :
7106 :
7107 : /* Convert longitude beneath pole from DMS to DEG */
7108 : /* ---------------------------------------------- */
7109 0 : longPol = EHconvAng(projparm[4], HDFE_DMS_RAD);
7110 :
7111 :
7112 :
7113 0 : for (i = 0; i < 4; i++)
7114 : {
7115 0 : csTest = cos(longPol);
7116 0 : snTest = sin(longPol);
7117 :
7118 0 : crsTest[0] = cs[0] * snTest - csTest * sn[0];
7119 0 : crsTest[1] = cs[1] * snTest - csTest * sn[1];
7120 :
7121 0 : if ((crs01 > 0 && crsTest[0] > 0 && crsTest[1] < 0) ||
7122 : (crs01 < 0 && crsTest[0] < 0 && crsTest[1] < 0) ||
7123 : (crs01 < 0 && crsTest[0] > 0 && crsTest[1] < 0) ||
7124 : (crs01 < 0 && crsTest[0] > 0 && crsTest[1] > 0))
7125 : {
7126 0 : longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG);
7127 0 : latitude[*npnts] = minLat;
7128 0 : (*npnts)++;
7129 : }
7130 0 : longPol += 0.5 * dpi;
7131 : }
7132 : }
7133 0 : break;
7134 :
7135 :
7136 : case GCTP_LAMAZ:
7137 : {
7138 0 : if ((int32) projparm[5] == +90000000 ||
7139 : (int32) projparm[5] == -90000000)
7140 : {
7141 : /* Add "xy axis" points for Polar Lambert Azimuthal */
7142 : /* ------------------------------------------------ */
7143 0 : minLat = (fabs(cornerlat[0]) <= fabs(cornerlat[1]))
7144 : ? cornerlat[0] : cornerlat[1];
7145 :
7146 0 : for (i = 0; i < 2; i++)
7147 : {
7148 0 : lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD);
7149 0 : cs[i] = cos(lonrad);
7150 0 : sn[i] = sin(lonrad);
7151 : }
7152 0 : crs01 = cs[0] * sn[1] - cs[1] * sn[0];
7153 :
7154 0 : longPol = EHconvAng(projparm[4], HDFE_DMS_RAD);
7155 0 : for (i = 0; i < 4; i++)
7156 : {
7157 0 : csTest = cos(longPol);
7158 0 : snTest = sin(longPol);
7159 :
7160 0 : crsTest[0] = cs[0] * snTest - csTest * sn[0];
7161 0 : crsTest[1] = cs[1] * snTest - csTest * sn[1];
7162 :
7163 0 : if ((crs01 > 0 && crsTest[0] > 0 && crsTest[1] < 0) ||
7164 : (crs01 < 0 && crsTest[0] < 0 && crsTest[1] < 0) ||
7165 : (crs01 < 0 && crsTest[0] > 0 && crsTest[1] < 0) ||
7166 : (crs01 < 0 && crsTest[0] > 0 && crsTest[1] > 0))
7167 : {
7168 0 : longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG);
7169 0 : latitude[*npnts] = minLat;
7170 0 : (*npnts)++;
7171 : }
7172 0 : longPol += 0.5 * dpi;
7173 : }
7174 : }
7175 0 : else if ((int32) projparm[5] == 0)
7176 : {
7177 : /* Add "Equator" points for Equatorial Lambert Azimuthal */
7178 : /* ----------------------------------------------------- */
7179 0 : if (cornerlat[0] * cornerlat[1] < 0)
7180 : {
7181 0 : longitude[4] = cornerlon[0];
7182 0 : latitude[4] = 0;
7183 :
7184 0 : longitude[5] = cornerlon[1];
7185 0 : latitude[5] = 0;
7186 :
7187 0 : *npnts = 6;
7188 : }
7189 : }
7190 : else
7191 : {
7192 : /* Add tangent points for Oblique Lambert Azimuthal */
7193 : /* ------------------------------------------------ */
7194 0 : bisectParm[0] = EHconvAng(projparm[5], HDFE_DMS_DEG);
7195 0 : bisectParm[2] = EHconvAng(projparm[4], HDFE_DMS_DEG);
7196 :
7197 :
7198 : /* Tangent to y-axis along longitude */
7199 : /* --------------------------------- */
7200 0 : for (i = 0; i < 2; i++)
7201 : {
7202 0 : bisectParm[1] = cornerlon[i];
7203 :
7204 0 : if (EHbisect(lamazDxDtheta, bisectParm, 3,
7205 : cornerlat[0], cornerlat[1],
7206 : 0.0001, &tanLat) == 0)
7207 : {
7208 0 : longitude[*npnts] = cornerlon[i];
7209 0 : latitude[*npnts] = tanLat;
7210 0 : (*npnts)++;
7211 : }
7212 : }
7213 :
7214 : /* Tangent to y-axis along latitude */
7215 : /* -------------------------------- */
7216 0 : for (i = 0; i < 2; i++)
7217 : {
7218 0 : bisectParm[1] = cornerlat[i];
7219 :
7220 0 : if (EHbisect(lamazDxDlamda, bisectParm, 3,
7221 : cornerlon[0], cornerlon[1],
7222 : 0.0001, &tanLon) == 0)
7223 : {
7224 0 : longitude[*npnts] = tanLon;
7225 0 : latitude[*npnts] = cornerlat[i];
7226 0 : (*npnts)++;
7227 : }
7228 : }
7229 :
7230 :
7231 : /* Tangent to x-axis along longitude */
7232 : /* --------------------------------- */
7233 0 : for (i = 0; i < 2; i++)
7234 : {
7235 0 : bisectParm[1] = cornerlon[i];
7236 :
7237 0 : if (EHbisect(lamazDyDtheta, bisectParm, 3,
7238 : cornerlat[0], cornerlat[1],
7239 : 0.0001, &tanLat) == 0)
7240 : {
7241 0 : longitude[*npnts] = cornerlon[i];
7242 0 : latitude[*npnts] = tanLat;
7243 0 : (*npnts)++;
7244 : }
7245 : }
7246 :
7247 : /* Tangent to x-axis along latitude */
7248 : /* -------------------------------- */
7249 0 : for (i = 0; i < 2; i++)
7250 : {
7251 0 : lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD);
7252 0 : cs[i] = cos(lonrad);
7253 0 : sn[i] = sin(lonrad);
7254 : }
7255 0 : crs01 = cs[0] * sn[1] - cs[1] * sn[0];
7256 :
7257 0 : longPol = EHconvAng(projparm[4], HDFE_DMS_RAD);
7258 0 : for (i = 0; i < 2; i++)
7259 : {
7260 0 : csTest = cos(longPol);
7261 0 : snTest = sin(longPol);
7262 :
7263 0 : crsTest[0] = cs[0] * snTest - csTest * sn[0];
7264 0 : crsTest[1] = cs[1] * snTest - csTest * sn[1];
7265 :
7266 0 : if ((crs01 > 0 && crsTest[0] > 0 && crsTest[1] < 0) ||
7267 : (crs01 < 0 && crsTest[0] < 0 && crsTest[1] < 0) ||
7268 : (crs01 < 0 && crsTest[0] > 0 && crsTest[1] < 0) ||
7269 : (crs01 < 0 && crsTest[0] > 0 && crsTest[1] > 0))
7270 : {
7271 0 : longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG);
7272 0 : latitude[*npnts] = cornerlat[0];
7273 0 : (*npnts)++;
7274 0 : longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG);
7275 0 : latitude[*npnts] = cornerlat[1];
7276 0 : (*npnts)++;
7277 : }
7278 0 : longPol += dpi;
7279 : }
7280 : }
7281 : }
7282 0 : break;
7283 :
7284 :
7285 : case GCTP_GOOD:
7286 : {
7287 : /* Add "Equator" points for Goode Homolosine if necessary */
7288 : /* ------------------------------------------------------ */
7289 0 : if (cornerlat[0] * cornerlat[1] < 0)
7290 : {
7291 0 : longitude[4] = cornerlon[0];
7292 0 : latitude[4] = 0;
7293 :
7294 0 : longitude[5] = cornerlon[1];
7295 0 : latitude[5] = 0;
7296 :
7297 0 : *npnts = 6;
7298 : }
7299 : }
7300 0 : break;
7301 :
7302 :
7303 : case GCTP_LAMCC:
7304 : {
7305 : /* Compute sine and cosine of corner longitudes */
7306 : /* -------------------------------------------- */
7307 0 : for (i = 0; i < 2; i++)
7308 : {
7309 0 : lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD);
7310 0 : cs[i] = cos(lonrad);
7311 0 : sn[i] = sin(lonrad);
7312 : }
7313 :
7314 :
7315 : /* Compute dot product */
7316 : /* ------------------- */
7317 0 : dotPrd = cs[0] * cs[1] + sn[0] * sn[1];
7318 :
7319 :
7320 : /* Convert central meridian (DMS to DEG) & compute sin & cos */
7321 : /* --------------------------------------------------------- */
7322 0 : centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG);
7323 0 : lonrad = EHconvAng(centMerd, HDFE_DEG_RAD);
7324 0 : cs[1] = cos(lonrad);
7325 0 : sn[1] = sin(lonrad);
7326 :
7327 :
7328 : /* If box brackets central meridian ... */
7329 : /* ------------------------------------ */
7330 0 : if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd)
7331 : {
7332 0 : latitude[4] = cornerlat[0];
7333 0 : longitude[4] = centMerd;
7334 :
7335 0 : latitude[5] = cornerlat[1];
7336 0 : longitude[5] = centMerd;
7337 :
7338 0 : *npnts = 6;
7339 : }
7340 : }
7341 0 : break;
7342 :
7343 :
7344 : case GCTP_ALBERS:
7345 : {
7346 : /* Compute sine and cosine of corner longitudes */
7347 : /* -------------------------------------------- */
7348 0 : for (i = 0; i < 2; i++)
7349 : {
7350 0 : lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD);
7351 0 : cs[i] = cos(lonrad);
7352 0 : sn[i] = sin(lonrad);
7353 : }
7354 :
7355 :
7356 : /* Compute dot product */
7357 : /* ------------------- */
7358 0 : dotPrd = cs[0] * cs[1] + sn[0] * sn[1];
7359 :
7360 :
7361 : /* Convert central meridian (DMS to DEG) & compute sin & cos */
7362 : /* --------------------------------------------------------- */
7363 0 : centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG);
7364 0 : lonrad = EHconvAng(centMerd, HDFE_DEG_RAD);
7365 0 : cs[1] = cos(lonrad);
7366 0 : sn[1] = sin(lonrad);
7367 :
7368 :
7369 : /* If box brackets central meridian ... */
7370 : /* ------------------------------------ */
7371 0 : if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd)
7372 : {
7373 0 : latitude[4] = cornerlat[0];
7374 0 : longitude[4] = centMerd;
7375 :
7376 0 : latitude[5] = cornerlat[1];
7377 0 : longitude[5] = centMerd;
7378 :
7379 0 : *npnts = 6;
7380 : }
7381 : }
7382 0 : break;
7383 :
7384 :
7385 : case GCTP_POLYC:
7386 : {
7387 : /* Compute sine and cosine of corner longitudes */
7388 : /* -------------------------------------------- */
7389 0 : for (i = 0; i < 2; i++)
7390 : {
7391 0 : lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD);
7392 0 : cs[i] = cos(lonrad);
7393 0 : sn[i] = sin(lonrad);
7394 : }
7395 :
7396 :
7397 : /* Compute dot product */
7398 : /* ------------------- */
7399 0 : dotPrd = cs[0] * cs[1] + sn[0] * sn[1];
7400 :
7401 :
7402 : /* Convert central meridian (DMS to DEG) & compute sin & cos */
7403 : /* --------------------------------------------------------- */
7404 0 : centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG);
7405 0 : lonrad = EHconvAng(centMerd, HDFE_DEG_RAD);
7406 0 : cs[1] = cos(lonrad);
7407 0 : sn[1] = sin(lonrad);
7408 :
7409 :
7410 : /* If box brackets central meridian ... */
7411 : /* ------------------------------------ */
7412 0 : if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd)
7413 : {
7414 0 : latitude[4] = cornerlat[0];
7415 0 : longitude[4] = centMerd;
7416 :
7417 0 : latitude[5] = cornerlat[1];
7418 0 : longitude[5] = centMerd;
7419 :
7420 0 : *npnts = 6;
7421 : }
7422 : }
7423 0 : break;
7424 :
7425 :
7426 : case GCTP_TM:
7427 : {
7428 : /* Compute sine and cosine of corner longitudes */
7429 : /* -------------------------------------------- */
7430 0 : for (i = 0; i < 2; i++)
7431 : {
7432 0 : lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD);
7433 0 : cs[i] = cos(lonrad);
7434 0 : sn[i] = sin(lonrad);
7435 : }
7436 :
7437 :
7438 : /* Compute dot product */
7439 : /* ------------------- */
7440 0 : dotPrd = cs[0] * cs[1] + sn[0] * sn[1];
7441 :
7442 :
7443 0 : for (i = -1; i <= 1; i++)
7444 : {
7445 0 : centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG);
7446 0 : lonrad = EHconvAng(centMerd + 90 * i, HDFE_DEG_RAD);
7447 0 : csTest = cos(lonrad);
7448 0 : snTest = sin(lonrad);
7449 :
7450 :
7451 : /* If box brackets meridian ... */
7452 : /* ---------------------------- */
7453 0 : if (csTest * cs[1] + snTest * sn[1] > dotPrd)
7454 : {
7455 0 : latitude[*npnts] = cornerlat[0];
7456 0 : longitude[*npnts] = centMerd;
7457 0 : (*npnts)++;
7458 :
7459 0 : latitude[*npnts] = cornerlat[1];
7460 0 : longitude[*npnts] = centMerd;
7461 0 : (*npnts)++;
7462 : }
7463 : }
7464 :
7465 :
7466 :
7467 : /* Compute sine and cosine of corner latitudes */
7468 : /* ------------------------------------------- */
7469 0 : for (i = 0; i < 2; i++)
7470 : {
7471 0 : latrad = EHconvAng(cornerlat[i], HDFE_DEG_RAD);
7472 0 : cs[i] = cos(latrad);
7473 0 : sn[i] = sin(latrad);
7474 : }
7475 :
7476 :
7477 : /* Compute dot product */
7478 : /* ------------------- */
7479 0 : dotPrd = cs[0] * cs[1] + sn[0] * sn[1];
7480 :
7481 :
7482 : /* Convert origin latitude (DMS to DEG) & compute sin & cos */
7483 : /* -------------------------------------------------------- */
7484 0 : orgLat = EHconvAng(projparm[5], HDFE_DMS_DEG);
7485 0 : latrad = EHconvAng(orgLat, HDFE_DEG_RAD);
7486 0 : cs[1] = cos(latrad);
7487 0 : sn[1] = sin(latrad);
7488 :
7489 :
7490 : /* If box brackets origin latitude ... */
7491 : /* ----------------------------------- */
7492 0 : if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd)
7493 : {
7494 0 : latitude[*npnts] = orgLat;
7495 0 : longitude[*npnts] = cornerlon[0];
7496 0 : (*npnts)++;
7497 :
7498 0 : latitude[*npnts] = orgLat;
7499 0 : longitude[*npnts] = cornerlon[1];
7500 0 : (*npnts)++;
7501 : }
7502 : }
7503 0 : break;
7504 :
7505 :
7506 : case GCTP_HOM:
7507 : {
7508 : /* Tangent to y-axis along longitude */
7509 : /* --------------------------------- */
7510 0 : if (projparm[12] == 0)
7511 : {
7512 0 : cs[0] = cos(EHconvAng(projparm[8], HDFE_DMS_RAD));
7513 0 : sn[0] = sin(EHconvAng(projparm[8], HDFE_DMS_RAD));
7514 0 : cs[1] = cos(EHconvAng(projparm[9], HDFE_DMS_RAD));
7515 0 : sn[1] = sin(EHconvAng(projparm[9], HDFE_DMS_RAD));
7516 0 : cs[2] = cos(EHconvAng(projparm[10], HDFE_DMS_RAD));
7517 0 : sn[2] = sin(EHconvAng(projparm[10], HDFE_DMS_RAD));
7518 0 : cs[3] = cos(EHconvAng(projparm[11], HDFE_DMS_RAD));
7519 0 : sn[3] = sin(EHconvAng(projparm[11], HDFE_DMS_RAD));
7520 :
7521 0 : bisectParm[3] = atan2(
7522 : (cs[1] * sn[3] * cs[0] - sn[1] * cs[3] * cs[2]),
7523 : (sn[1] * cs[3] * sn[2] - cs[1] * sn[3] * sn[0]));
7524 0 : bisectParm[0] = atan(
7525 : (sin(bisectParm[3]) * sn[0] - cos(bisectParm[3]) * cs[0]) /
7526 : (sn[1] / cs[1]));
7527 0 : bisectParm[2] = bisectParm[3] + 0.5 * dpi;
7528 : }
7529 : else
7530 : {
7531 0 : cs[0] = cos(EHconvAng(projparm[3], HDFE_DMS_RAD));
7532 0 : sn[0] = sin(EHconvAng(projparm[3], HDFE_DMS_RAD));
7533 0 : cs[1] = cos(EHconvAng(projparm[4], HDFE_DMS_RAD));
7534 0 : sn[1] = sin(EHconvAng(projparm[4], HDFE_DMS_RAD));
7535 :
7536 0 : bisectParm[0] = asin(cs[1] * sn[0]);
7537 0 : bisectParm[2] = atan2(-cs[0], (-sn[1] * sn[0])) + 0.5 * dpi;
7538 : }
7539 :
7540 0 : for (i = 0; i < 2; i++)
7541 : {
7542 0 : bisectParm[1] = cornerlon[i];
7543 :
7544 0 : if (EHbisect(homDyDtheta, bisectParm, 3,
7545 : cornerlat[0], cornerlat[1],
7546 : 0.0001, &tanLat) == 0)
7547 : {
7548 0 : longitude[*npnts] = cornerlon[i];
7549 0 : latitude[*npnts] = tanLat;
7550 0 : (*npnts)++;
7551 : }
7552 : }
7553 :
7554 : }
7555 : break;
7556 : }
7557 :
7558 :
7559 0 : return (status);
7560 : }
7561 :
7562 :
7563 :
7564 : /*----------------------------------------------------------------------------|
7565 : | BEGIN_PROLOG |
7566 : | |
7567 : | FUNCTION: GDdefboxregion |
7568 : | |
7569 : | DESCRIPTION: Defines region for subsetting in a grid. |
7570 : | |
7571 : | |
7572 : | Return Value Type Units Description |
7573 : | ============ ====== ========= ===================================== |
7574 : | regionID int32 Region ID |
7575 : | |
7576 : | INPUTS: |
7577 : | gridID int32 Grid structure ID |
7578 : | cornerlon float64 dec deg Longitude of opposite corners of box |
7579 : | cornerlat float64 dec deg Latitude of opposite corners of box |
7580 : | |
7581 : | |
7582 : | OUTPUTS: |
7583 : | None |
7584 : | |
7585 : | NOTES: |
7586 : | |
7587 : | |
7588 : | Date Programmer Description |
7589 : | ====== ============ ================================================= |
7590 : | Jun 96 Joel Gales Original Programmer |
7591 : | Oct 96 Joel Gales "Clamp" subset region around grid |
7592 : | Oct 96 Joel Gales Fix "outside region" check |
7593 : | Nov 96 Joel Gales Add check for "tangent" points (GDtangentpnts) |
7594 : | Dec 96 Joel Gales Trap if no projection code defined |
7595 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
7596 : | Mar 99 David Wynne Fix for NCR 21195, allow subsetting of MISR SOM |
7597 : | data sets |
7598 : | Jun 00 Abe Taaheri Added support for EASE grid |
7599 : | |
7600 : | END_PROLOG |
7601 : -----------------------------------------------------------------------------*/
7602 : int32
7603 : GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[])
7604 0 : {
7605 : intn i; /* Loop index */
7606 : intn j; /* Loop index */
7607 : intn k; /* Loop index */
7608 : intn n; /* Loop index */
7609 0 : intn status = 0; /* routine return status variable */
7610 :
7611 : int32 fid; /* HDF-EOS file ID */
7612 : int32 sdInterfaceID; /* HDF SDS interface ID */
7613 : int32 gdVgrpID; /* Grid root Vgroup ID */
7614 0 : int32 regionID = -1; /* Region ID */
7615 : int32 xdimsize; /* XDim size */
7616 : int32 ydimsize; /* YDim size */
7617 : int32 projcode; /* Projection code */
7618 : int32 zonecode; /* Zone code */
7619 : int32 spherecode; /* Sphere code */
7620 : int32 row[32]; /* Row array */
7621 : int32 col[32]; /* Column array */
7622 : int32 minCol; /* Minimun column value */
7623 : int32 minRow; /* Minimun row value */
7624 : int32 maxCol; /* Maximun column value */
7625 : int32 maxRow; /* Maximun row value */
7626 : int32 npnts; /* Number of boundary
7627 : (edge & tangent) pnts */
7628 :
7629 : float64 longitude[32]; /* Longitude array */
7630 : float64 latitude[32]; /* Latitude array */
7631 : float64 upleftpt[2]; /* Upper left pt coordinates */
7632 : float64 lowrightpt[2]; /* Lower right pt coordinates */
7633 : float64 somupleftpt[2]; /* temporary Upper left pt coordinates
7634 : for SOM projection */
7635 : float64 somlowrightpt[2]; /* temporary Lower right pt
7636 : coordinates for SOM projection */
7637 : float64 projparm[16]; /* Projection parameters */
7638 : float64 xscale; /* X scale */
7639 : float64 yscale; /* Y scale */
7640 : float64 lonrad0; /* Longitude of upper left point
7641 : (radians) */
7642 : float64 latrad0; /* Latitude of upper left point (radians) */
7643 : float64 lonrad2; /* Longitude of point (radians) */
7644 : float64 latrad2; /* Latitude of point (radians) */
7645 :
7646 : /* Used for SOM projection */
7647 : char *utlbuf;
7648 : char *gridname;
7649 0 : int32 blockindexstart = -1;
7650 0 : int32 blockindexstop = -1;
7651 : float32 offset[180];
7652 : float64 templeftpt[2];
7653 : float64 temprightpt[2];
7654 0 : int32 idOffset = GDIDOFFSET; /* Grid ID offset */
7655 : float64 xmtr[2], ymtr[2];
7656 : float64 lon[2],lat[2];
7657 : float64 xcor[2], ycor[2];
7658 : int32 nlatlon;
7659 : float64 upleftpt_m[2];
7660 :
7661 :
7662 0 : utlbuf = (char *)calloc(128, sizeof(char));
7663 0 : if(utlbuf == NULL)
7664 : {
7665 0 : HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__);
7666 0 : return(-1);
7667 : }
7668 0 : gridname = (char *)calloc(128, sizeof(char));
7669 0 : if(gridname == NULL)
7670 : {
7671 0 : HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__);
7672 0 : free(utlbuf);
7673 0 : return(-1);
7674 : }
7675 :
7676 : /* Check for valid grid ID */
7677 : /* ----------------------- */
7678 0 : status = GDchkgdid(gridID, "GDdefboxregion",
7679 : &fid, &sdInterfaceID, &gdVgrpID);
7680 :
7681 :
7682 0 : if (status == 0)
7683 : {
7684 : /* Get grid info */
7685 : /* ------------- */
7686 0 : status = GDgridinfo(gridID, &xdimsize, &ydimsize,
7687 : upleftpt, lowrightpt);
7688 :
7689 :
7690 : /* If error then bail */
7691 : /* ------------------ */
7692 0 : if (status != 0)
7693 : {
7694 0 : regionID = -1;
7695 0 : free(utlbuf);
7696 0 : free(gridname);
7697 0 : return (regionID);
7698 : }
7699 :
7700 :
7701 : /* Get proj info */
7702 : /* ------------- */
7703 0 : status = GDprojinfo(gridID, &projcode, &zonecode,
7704 : &spherecode, projparm);
7705 :
7706 :
7707 : /* If no projection code defined then bail */
7708 : /* --------------------------------------- */
7709 0 : if (projcode == -1)
7710 : {
7711 0 : regionID = -1;
7712 0 : free(utlbuf);
7713 0 : free(gridname);
7714 0 : return (regionID);
7715 : }
7716 :
7717 :
7718 : /* Get default values for upleft and lowright if necessary */
7719 : /* ------------------------------------------------------- */
7720 0 : if (upleftpt[0] == 0 && upleftpt[1] == 0 &&
7721 : lowrightpt[0] == 0 && lowrightpt[1] == 0)
7722 : {
7723 0 : status = GDgetdefaults(projcode, zonecode, projparm, spherecode,
7724 : upleftpt, lowrightpt);
7725 :
7726 : /* If error then bail */
7727 : /* ------------------ */
7728 0 : if (status != 0)
7729 : {
7730 0 : regionID = -1;
7731 0 : free(utlbuf);
7732 0 : free(gridname);
7733 0 : return (regionID);
7734 : }
7735 : }
7736 :
7737 :
7738 :
7739 : /* Fill-up longitude and latitude arrays */
7740 : /* ------------------------------------- */
7741 0 : longitude[0] = cornerlon[0];
7742 0 : latitude[0] = cornerlat[0];
7743 :
7744 0 : longitude[1] = cornerlon[0];
7745 0 : latitude[1] = cornerlat[1];
7746 :
7747 0 : longitude[2] = cornerlon[1];
7748 0 : latitude[2] = cornerlat[0];
7749 :
7750 0 : longitude[3] = cornerlon[1];
7751 0 : latitude[3] = cornerlat[1];
7752 :
7753 0 : npnts = 4;
7754 :
7755 :
7756 : /* Find additional tangent points from GDtangentpnts */
7757 : /* ------------------------------------------------- */
7758 0 : status = GDtangentpnts(projcode, projparm, cornerlon, cornerlat,
7759 : longitude, latitude, &npnts);
7760 :
7761 : /* If SOM projection with projparm[11] non-zero ... */
7762 0 : if (projcode == GCTP_SOM && projparm[11] != 0)
7763 : {
7764 0 : Vgetname(GDXGrid[gridID % idOffset].IDTable, gridname);
7765 0 : sprintf(utlbuf, "%s%s", "_BLKSOM:", gridname);
7766 0 : status = GDreadattr(gridID, utlbuf, offset);
7767 :
7768 0 : somupleftpt[0] = upleftpt[0];
7769 0 : somupleftpt[1] = upleftpt[1];
7770 0 : somlowrightpt[0]= lowrightpt[0];
7771 0 : somlowrightpt[1] = lowrightpt[1];
7772 :
7773 0 : k = 0;
7774 0 : n = 2;
7775 :
7776 0 : for (j = 0; j <= projparm[11] - 1; j++)
7777 : {
7778 :
7779 : /* Convert from lon/lat to row/col */
7780 : /* ------------------------------- */
7781 0 : status = GDll2ij(projcode, zonecode, projparm, spherecode,
7782 : xdimsize, ydimsize, somupleftpt, somlowrightpt,
7783 : npnts, longitude, latitude, row, col, NULL, NULL);
7784 :
7785 :
7786 : /* Find min/max values for row & col */
7787 : /* --------------------------------- */
7788 0 : minCol = col[0];
7789 0 : minRow = row[0];
7790 0 : maxCol = col[0];
7791 0 : maxRow = row[0];
7792 0 : for (i = 1; i < npnts; i++)
7793 : {
7794 0 : if (col[i] < minCol)
7795 : {
7796 0 : minCol = col[i];
7797 : }
7798 :
7799 0 : if (col[i] > maxCol)
7800 : {
7801 0 : maxCol = col[i];
7802 : }
7803 :
7804 0 : if (row[i] < minRow)
7805 : {
7806 0 : minRow = row[i];
7807 : }
7808 :
7809 0 : if (row[i] > maxRow)
7810 : {
7811 0 : maxRow = row[i];
7812 : }
7813 : }
7814 :
7815 :
7816 :
7817 : /* "Clamp" if outside Grid */
7818 : /* ----------------------- */
7819 0 : minCol = (minCol < 0) ? 0 : minCol;
7820 0 : minRow = (minRow < 0) ? 0 : minRow;
7821 :
7822 0 : maxCol = (maxCol >= xdimsize) ? xdimsize - 1 : maxCol;
7823 0 : maxRow = (maxRow >= ydimsize) ? ydimsize - 1 : maxRow;
7824 :
7825 :
7826 : /* Check whether subset region is outside grid region */
7827 : /* -------------------------------------------------- */
7828 0 : if (minCol >= xdimsize || minRow >= ydimsize ||
7829 : maxCol < 0 || maxRow < 0)
7830 : {
7831 0 : if ( blockindexstart == -1 && (projparm[11]) == j)
7832 : {
7833 0 : status = -1;
7834 0 : HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__);
7835 0 : HEreport("Subset Region outside of Grid Region\n");
7836 0 : regionID = -1;
7837 : }
7838 : }
7839 : else
7840 : {
7841 0 : if (k == 0)
7842 : {
7843 0 : blockindexstart = j;
7844 0 : blockindexstop = j;
7845 0 : k = 1;
7846 : }
7847 : else
7848 : {
7849 0 : blockindexstop = j;
7850 : }
7851 : }
7852 0 : templeftpt[0] = upleftpt[0] + ((offset[j]/xdimsize)*abs(upleftpt[0] - lowrightpt[0])) + abs((upleftpt[0] - lowrightpt[0]))*(n-1);
7853 0 : templeftpt[1] = upleftpt[1] + ((lowrightpt[1] - upleftpt[1]))*(n-1);
7854 :
7855 0 : temprightpt[0] = lowrightpt[0] + ((offset[j]/xdimsize)*abs(lowrightpt[0] - upleftpt[0])) + abs((lowrightpt[0] - upleftpt[0]))*(n-1);
7856 0 : temprightpt[1] = lowrightpt[1] + ((upleftpt[1] - lowrightpt[1]))*(n-1);
7857 :
7858 0 : somupleftpt[0] = templeftpt[0];
7859 0 : somupleftpt[1] = templeftpt[1];
7860 :
7861 0 : somlowrightpt[0] = temprightpt[0];
7862 0 : somlowrightpt[1] = temprightpt[1];
7863 0 : n++;
7864 : }
7865 : }
7866 : else
7867 : {
7868 :
7869 : /* Convert from lon/lat to row/col */
7870 : /* ------------------------------- */
7871 :
7872 0 : status = GDll2ij(projcode, zonecode, projparm, spherecode,
7873 : xdimsize, ydimsize, upleftpt, lowrightpt,
7874 : npnts, longitude, latitude, row, col, NULL, NULL);
7875 :
7876 : /* Find min/max values for row & col */
7877 : /* --------------------------------- */
7878 0 : minCol = col[0];
7879 0 : minRow = row[0];
7880 0 : maxCol = col[0];
7881 0 : maxRow = row[0];
7882 0 : for (i = 1; i < npnts; i++)
7883 : {
7884 0 : if (col[i] < minCol)
7885 : {
7886 0 : minCol = col[i];
7887 : }
7888 :
7889 0 : if (col[i] > maxCol)
7890 : {
7891 0 : maxCol = col[i];
7892 : }
7893 :
7894 0 : if (row[i] < minRow)
7895 : {
7896 0 : minRow = row[i];
7897 : }
7898 :
7899 0 : if (row[i] > maxRow)
7900 : {
7901 0 : maxRow = row[i];
7902 : }
7903 : }
7904 :
7905 :
7906 :
7907 : /* "Clamp" if outside Grid */
7908 : /* ----------------------- */
7909 0 : minCol = (minCol < 0) ? 0 : minCol;
7910 0 : minRow = (minRow < 0) ? 0 : minRow;
7911 :
7912 0 : maxCol = (maxCol >= xdimsize) ? xdimsize - 1 : maxCol;
7913 0 : maxRow = (maxRow >= ydimsize) ? ydimsize - 1 : maxRow;
7914 :
7915 :
7916 : /* Check whether subset region is outside grid region */
7917 : /* -------------------------------------------------- */
7918 0 : if (minCol >= xdimsize || minRow >= ydimsize || maxCol < 0 || maxRow < 0)
7919 : {
7920 0 : status = -1;
7921 0 : HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__);
7922 0 : HEreport("Subset Region outside of Grid Region\n");
7923 0 : regionID = -1;
7924 :
7925 : }
7926 : }
7927 0 : if (status == 0)
7928 : {
7929 : /* Store grid region info */
7930 : /* ---------------------- */
7931 0 : for (i = 0; i < NGRIDREGN; i++)
7932 : {
7933 : /* Find first empty grid region */
7934 : /* ---------------------------- */
7935 0 : if (GDXRegion[i] == 0)
7936 : {
7937 : /* Allocate space for grid region entry */
7938 : /* ------------------------------------ */
7939 0 : GDXRegion[i] = (struct gridRegion *)
7940 : calloc(1, sizeof(struct gridRegion));
7941 0 : if(GDXRegion[i] == NULL)
7942 : {
7943 0 : HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__);
7944 0 : free(utlbuf);
7945 0 : free(gridname);
7946 0 : return(-1);
7947 : }
7948 :
7949 :
7950 : /* Store file and grid ID */
7951 : /* ---------------------- */
7952 0 : GDXRegion[i]->fid = fid;
7953 0 : GDXRegion[i]->gridID = gridID;
7954 :
7955 :
7956 : /* Initialize vertical subset entries to -1 */
7957 : /* ---------------------------------------- */
7958 0 : for (j = 0; j < 8; j++)
7959 : {
7960 0 : GDXRegion[i]->StartVertical[j] = -1;
7961 0 : GDXRegion[i]->StopVertical[j] = -1;
7962 : }
7963 :
7964 :
7965 : /* Store start & count along x & y */
7966 : /* ------------------------------- */
7967 0 : GDXRegion[i]->xStart = minCol;
7968 0 : GDXRegion[i]->xCount = maxCol - minCol + 1;
7969 0 : GDXRegion[i]->yStart = minRow;
7970 0 : GDXRegion[i]->yCount = maxRow - minRow + 1;
7971 :
7972 :
7973 : /* Store upleft and lowright points of subset region */
7974 : /* ------------------------------------------------- */
7975 0 : if (projcode == GCTP_GEO )
7976 : {
7977 : /* GEO projection */
7978 : /* ------------------------ */
7979 :
7980 : /* Convert upleft & lowright lon from DMS to radians */
7981 : /* ------------------------------------------------- */
7982 0 : lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD);
7983 0 : lonrad2 = EHconvAng(lowrightpt[0], HDFE_DMS_RAD);
7984 :
7985 : /* Compute X scale */
7986 : /* --------------- */
7987 0 : xscale = (lonrad2 - lonrad0) / xdimsize;
7988 :
7989 : /* Convert upleft & lowright lat from DMS to radians */
7990 : /* ------------------------------------------------- */
7991 0 : latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD);
7992 0 : latrad2 = EHconvAng(lowrightpt[1], HDFE_DMS_RAD);
7993 :
7994 : /* Compute Y scale */
7995 : /* --------------- */
7996 0 : yscale = (latrad2 - latrad0) / ydimsize;
7997 :
7998 :
7999 : /* MinCol -> radians -> DMS -> upleftpt[0] */
8000 : /* --------------------------------------- */
8001 0 : GDXRegion[i]->upleftpt[0] =
8002 : EHconvAng(lonrad0 + xscale * minCol,
8003 : HDFE_RAD_DMS);
8004 :
8005 :
8006 : /* MinRow -> radians -> DMS -> upleftpt[1] */
8007 : /* --------------------------------------- */
8008 0 : GDXRegion[i]->upleftpt[1] =
8009 : EHconvAng(latrad0 + yscale * minRow,
8010 : HDFE_RAD_DMS);
8011 :
8012 :
8013 : /* MinCol + 1 -> radians -> DMS -> lowrightpt[0] */
8014 : /* --------------------------------------------- */
8015 0 : GDXRegion[i]->lowrightpt[0] =
8016 : EHconvAng(lonrad0 + xscale * (maxCol + 1),
8017 : HDFE_RAD_DMS);
8018 :
8019 :
8020 : /* MinRow + 1 -> radians -> DMS -> lowrightpt[1] */
8021 : /* --------------------------------------------- */
8022 0 : GDXRegion[i]->lowrightpt[1] =
8023 : EHconvAng(latrad0 + yscale * (maxRow + 1),
8024 : HDFE_RAD_DMS);
8025 : }
8026 0 : else if (projcode == GCTP_BCEA)
8027 : {
8028 : /* BCEA projection */
8029 : /* -------------- */
8030 0 : nlatlon = 2;
8031 0 : lon[0] = upleftpt[0];
8032 0 : lon[1] = lowrightpt[0];
8033 0 : lat[0] = upleftpt[1];
8034 0 : lat[1] = lowrightpt[1];
8035 0 : status =
8036 : GDll2mm_cea(projcode,zonecode,spherecode,projparm,
8037 : xdimsize, ydimsize,
8038 : upleftpt, lowrightpt,nlatlon,
8039 : lon, lat,
8040 : xcor, ycor, &xscale, &yscale);
8041 0 : upleftpt_m[0] = xcor[0];
8042 0 : upleftpt_m[1] = ycor[0];
8043 :
8044 :
8045 0 : if (status == -1)
8046 : {
8047 0 : HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__);
8048 0 : free(utlbuf);
8049 0 : free(gridname);
8050 0 : return (status);
8051 : }
8052 :
8053 : /* MinCol -> meters -> upleftpt[0] */
8054 : /* ------------------------------- */
8055 0 : xmtr[0] = upleftpt_m[0] + xscale * minCol;
8056 :
8057 : /* MinRow -> meters -> upleftpt[1] */
8058 : /* ------------------------------- */
8059 0 : ymtr[0] = upleftpt_m[1] + yscale * minRow;
8060 :
8061 : /* MinCol + 1 -> meters -> lowrightpt[0] */
8062 : /* ------------------------------------- */
8063 0 : xmtr[1] = upleftpt_m[0] + xscale * (maxCol + 1);
8064 :
8065 : /* MinRow + 1 -> meters -> lowrightpt[1] */
8066 : /* ------------------------------------- */
8067 0 : ymtr[1] = upleftpt_m[1] + yscale * (maxRow + 1);
8068 :
8069 : /* Convert upleft & lowright lon from DMS to radians */
8070 : /* ------------------------------------------------- */
8071 0 : npnts = 2;
8072 0 : status = GDmm2ll_cea(projcode, zonecode, spherecode,
8073 : projparm, xdimsize, ydimsize,
8074 : upleftpt, lowrightpt, npnts,
8075 : xmtr, ymtr,
8076 : longitude, latitude);
8077 0 : if (status == -1)
8078 : {
8079 0 : HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__);
8080 0 : free(utlbuf);
8081 0 : free(gridname);
8082 0 : return (status);
8083 : }
8084 0 : GDXRegion[i]->upleftpt[0] = longitude[0];
8085 :
8086 0 : GDXRegion[i]->upleftpt[1] = latitude[0];
8087 :
8088 0 : GDXRegion[i]->lowrightpt[0] = longitude[1];
8089 :
8090 0 : GDXRegion[i]->lowrightpt[1] = latitude[1];
8091 : }
8092 0 : else if (projcode == GCTP_SOM)
8093 : {
8094 : /* Store start & count along x & y */
8095 : /* ------------------------------- */
8096 0 : GDXRegion[i]->xStart = 0;
8097 0 : GDXRegion[i]->xCount = xdimsize;
8098 0 : GDXRegion[i]->yStart = 0;
8099 0 : GDXRegion[i]->yCount = ydimsize;
8100 :
8101 0 : GDXRegion[i]->somStart = blockindexstart;
8102 0 : GDXRegion[i]->somCount = blockindexstop - blockindexstart + 1;
8103 :
8104 : /* Store upleft and lowright points of subset region */
8105 : /* ------------------------------------------------- */
8106 0 : if (blockindexstart == 0)
8107 : {
8108 0 : GDXRegion[i]->upleftpt[0] = upleftpt[0];
8109 0 : GDXRegion[i]->upleftpt[1] = upleftpt[1];
8110 0 : GDXRegion[i]->lowrightpt[0] = lowrightpt[0];
8111 0 : GDXRegion[i]->lowrightpt[1] = lowrightpt[1];
8112 : }
8113 : else
8114 : {
8115 0 : GDXRegion[i]->upleftpt[0] =
8116 : (lowrightpt[0] - upleftpt[0])*
8117 : (offset[blockindexstart-1]/xdimsize) + upleftpt[0];
8118 0 : GDXRegion[i]->upleftpt[1] =
8119 : (lowrightpt[1] - upleftpt[1])*
8120 : (blockindexstart+1-1) + upleftpt[1];
8121 :
8122 0 : GDXRegion[i]->lowrightpt[0] =
8123 : (lowrightpt[0] - upleftpt[0])*
8124 : (offset[blockindexstart-1]/xdimsize) + lowrightpt[0];
8125 0 : GDXRegion[i]->lowrightpt[1] =
8126 : (lowrightpt[1] - upleftpt[1])*
8127 : (blockindexstart+1-1) + lowrightpt[1];
8128 :
8129 : }
8130 : }
8131 : else
8132 : {
8133 : /* Non-GEO, Non-BCEA projections */
8134 : /* ---------------------------- */
8135 :
8136 : /* Compute X & Y scale */
8137 : /* ------------------- */
8138 0 : xscale = (lowrightpt[0] - upleftpt[0]) / xdimsize;
8139 0 : yscale = (lowrightpt[1] - upleftpt[1]) / ydimsize;
8140 :
8141 :
8142 : /* MinCol -> meters -> upleftpt[0] */
8143 : /* ------------------------------- */
8144 0 : GDXRegion[i]->upleftpt[0] = upleftpt[0] +
8145 : xscale * minCol;
8146 :
8147 :
8148 : /* MinRow -> meters -> upleftpt[1] */
8149 : /* ------------------------------- */
8150 0 : GDXRegion[i]->upleftpt[1] = upleftpt[1] +
8151 : yscale * minRow;
8152 :
8153 :
8154 : /* MinCol + 1 -> meters -> lowrightpt[0] */
8155 : /* ------------------------------------- */
8156 0 : GDXRegion[i]->lowrightpt[0] = upleftpt[0] +
8157 : xscale * (maxCol + 1);
8158 :
8159 :
8160 : /* MinRow + 1 -> meters -> lowrightpt[1] */
8161 : /* ------------------------------------- */
8162 0 : GDXRegion[i]->lowrightpt[1] = upleftpt[1] +
8163 : yscale * (maxRow + 1);
8164 : }
8165 :
8166 : /* Store region ID */
8167 : /* --------------- */
8168 0 : regionID = i;
8169 0 : break;
8170 : }
8171 :
8172 : }
8173 : }
8174 :
8175 : }
8176 0 : free(utlbuf);
8177 0 : free(gridname);
8178 0 : return (regionID);
8179 : }
8180 :
8181 :
8182 :
8183 :
8184 :
8185 : /*----------------------------------------------------------------------------|
8186 : | BEGIN_PROLOG |
8187 : | |
8188 : | FUNCTION: GDregioninfo |
8189 : | |
8190 : | DESCRIPTION: Retrieves size of region in bytes. |
8191 : | |
8192 : | |
8193 : | Return Value Type Units Description |
8194 : | ============ ====== ========= ===================================== |
8195 : | status intn return status (0) SUCCEED, (-1) FAIL |
8196 : | |
8197 : | INPUTS: |
8198 : | gridID int32 Grid structure ID |
8199 : | regionID int32 Region ID |
8200 : | fieldname char Fieldname |
8201 : | |
8202 : | |
8203 : | OUTPUTS: |
8204 : | ntype int32 field number type |
8205 : | rank int32 field rank |
8206 : | dims int32 dimensions of field region |
8207 : | size int32 size in bytes of field region |
8208 : | upleftpt float64 Upper left corner coord for region |
8209 : | lowrightpt float64 Lower right corner coord for region |
8210 : | |
8211 : | NOTES: |
8212 : | |
8213 : | |
8214 : | Date Programmer Description |
8215 : | ====== ============ ================================================= |
8216 : | Jun 96 Joel Gales Original Programmer |
8217 : | Aug 96 Joel Gales Add vertical subsetting |
8218 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
8219 : | Apr 99 David Wynne Added support for MISR SOM projection, NCR 21195 |
8220 : | |
8221 : | END_PROLOG |
8222 : -----------------------------------------------------------------------------*/
8223 : intn
8224 : GDregioninfo(int32 gridID, int32 regionID, char *fieldname,
8225 : int32 * ntype, int32 * rank, int32 dims[], int32 * size,
8226 : float64 upleftpt[], float64 lowrightpt[])
8227 0 : {
8228 : intn j; /* Loop index */
8229 0 : intn status = 0; /* routine return status variable */
8230 :
8231 : int32 fid; /* HDF-EOS file ID */
8232 : int32 sdInterfaceID; /* HDF SDS interface ID */
8233 : int32 gdVgrpID; /* Grid root Vgroup ID */
8234 : int32 index; /* Dimension index */
8235 :
8236 : char dimlist[256]; /* Dimension list */
8237 0 : char *errMesg = "Vertical Dimension Not Found: \"%s\".\n";
8238 0 : char *errM1 = "Both \"XDim\" and \"YDim\" must be present ";
8239 0 : char *errM2 = "in the dimension list for \"%s\".\n";
8240 : char errbuf[256];/* Error buffer */
8241 :
8242 :
8243 : /* Check for valid grid ID */
8244 : /* ----------------------- */
8245 0 : status = GDchkgdid(gridID, "GDregioninfo", &fid, &sdInterfaceID,
8246 : &gdVgrpID);
8247 :
8248 :
8249 : /* Check for valid region ID */
8250 : /* ------------------------- */
8251 0 : if (status == 0)
8252 : {
8253 0 : if (regionID < 0 || regionID >= NGRIDREGN)
8254 : {
8255 0 : status = -1;
8256 0 : HEpush(DFE_RANGE, "GDregioninfo", __FILE__, __LINE__);
8257 0 : HEreport("Invalid Region id: %d.\n", regionID);
8258 : }
8259 : }
8260 :
8261 :
8262 : /* Check for active region ID */
8263 : /* -------------------------- */
8264 0 : if (status == 0)
8265 : {
8266 0 : if (GDXRegion[regionID] == 0)
8267 : {
8268 0 : status = -1;
8269 0 : HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__);
8270 0 : HEreport("Inactive Region ID: %d.\n", regionID);
8271 : }
8272 : }
8273 :
8274 :
8275 :
8276 : /* Check that region defined for this file */
8277 : /* --------------------------------------- */
8278 0 : if (status == 0)
8279 : {
8280 0 : if (GDXRegion[regionID]->fid != fid)
8281 : {
8282 0 : status = -1;
8283 0 : HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__);
8284 0 : HEreport("Region is not defined for this file.\n");
8285 : }
8286 : }
8287 :
8288 :
8289 : /* Check that region defined for this grid */
8290 : /* --------------------------------------- */
8291 0 : if (status == 0)
8292 : {
8293 0 : if (GDXRegion[regionID]->gridID != gridID)
8294 : {
8295 0 : status = -1;
8296 0 : HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__);
8297 0 : HEreport("Region is not defined for this Grid.\n");
8298 : }
8299 : }
8300 :
8301 :
8302 :
8303 : /* Check for valid fieldname */
8304 : /* ------------------------- */
8305 0 : if (status == 0)
8306 : {
8307 0 : status = GDfieldinfo(gridID, fieldname, rank, dims, ntype, dimlist);
8308 :
8309 0 : if (status != 0)
8310 : {
8311 : /* Fieldname not found in grid */
8312 : /* --------------------------- */
8313 0 : status = -1;
8314 0 : HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__);
8315 0 : HEreport("Fieldname \"%s\" not found.\n",
8316 : fieldname);
8317 : }
8318 0 : else if (*rank == 1)
8319 : {
8320 : /* Field is 1 dimensional */
8321 : /* ---------------------- */
8322 0 : status = -1;
8323 0 : HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__);
8324 0 : HEreport(
8325 : "One-Dimesional fields \"%s\" may not be subsetted.\n",
8326 : fieldname);
8327 : }
8328 : else
8329 : {
8330 : /* "XDim" and/or "YDim" not found */
8331 : /* ------------------------------ */
8332 0 : if (EHstrwithin("XDim", dimlist, ',') == -1 ||
8333 : EHstrwithin("YDim", dimlist, ',') == -1)
8334 : {
8335 0 : status = -1;
8336 0 : HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__);
8337 0 : sprintf(errbuf, "%s%s", errM1, errM2);
8338 0 : HEreport(errbuf, fieldname);
8339 : }
8340 : }
8341 : }
8342 :
8343 :
8344 :
8345 : /* If no problems ... */
8346 : /* ------------------ */
8347 0 : if (status == 0)
8348 : {
8349 : /* Check if SOM projection */
8350 : /* ----------------------- */
8351 0 : if (EHstrwithin("SOMBlockDim", dimlist, ',') == 0)
8352 : {
8353 0 : dims[EHstrwithin("SOMBlockDim", dimlist, ',')] =
8354 : GDXRegion[regionID]->somCount;
8355 : }
8356 :
8357 : /* Load XDim dimension from region entry */
8358 : /* ------------------------------------- */
8359 0 : if (GDXRegion[regionID]->xCount != 0)
8360 : {
8361 0 : dims[EHstrwithin("XDim", dimlist, ',')] =
8362 : GDXRegion[regionID]->xCount;
8363 : }
8364 :
8365 : /* Load YDim dimension from region entry */
8366 : /* ------------------------------------- */
8367 0 : if (GDXRegion[regionID]->yCount != 0)
8368 : {
8369 0 : dims[EHstrwithin("YDim", dimlist, ',')] =
8370 : GDXRegion[regionID]->yCount;
8371 : }
8372 :
8373 :
8374 : /* Vertical Subset */
8375 : /* --------------- */
8376 0 : for (j = 0; j < 8; j++)
8377 : {
8378 :
8379 : /* If active vertical subset ... */
8380 : /* ----------------------------- */
8381 0 : if (GDXRegion[regionID]->StartVertical[j] != -1)
8382 : {
8383 : /* Find vertical dimension within dimlist */
8384 : /* -------------------------------------- */
8385 0 : index = EHstrwithin(GDXRegion[regionID]->DimNamePtr[j],
8386 : dimlist, ',');
8387 :
8388 : /* If dimension found ... */
8389 : /* ---------------------- */
8390 0 : if (index != -1)
8391 : {
8392 : /* Compute dimension size */
8393 : /* ---------------------- */
8394 0 : dims[index] =
8395 : GDXRegion[regionID]->StopVertical[j] -
8396 : GDXRegion[regionID]->StartVertical[j] + 1;
8397 : }
8398 : else
8399 : {
8400 : /* Vertical dimension not found */
8401 : /* ---------------------------- */
8402 0 : status = -1;
8403 0 : *size = -1;
8404 0 : HEpush(DFE_GENAPP, "GDregioninfo",
8405 : __FILE__, __LINE__);
8406 0 : HEreport(errMesg,
8407 : GDXRegion[regionID]->DimNamePtr[j]);
8408 : }
8409 : }
8410 : }
8411 :
8412 :
8413 0 : if (status == 0)
8414 : {
8415 : /* Compute number of total elements */
8416 : /* -------------------------------- */
8417 0 : *size = dims[0];
8418 0 : for (j = 1; j < *rank; j++)
8419 : {
8420 0 : *size *= dims[j];
8421 : }
8422 :
8423 : /* Multiply by size in bytes of numbertype */
8424 : /* --------------------------------------- */
8425 0 : *size *= DFKNTsize(*ntype);
8426 :
8427 :
8428 : /* Return upper left and lower right subset values */
8429 : /* ----------------------------------------------- */
8430 0 : upleftpt[0] = GDXRegion[regionID]->upleftpt[0];
8431 0 : upleftpt[1] = GDXRegion[regionID]->upleftpt[1];
8432 0 : lowrightpt[0] = GDXRegion[regionID]->lowrightpt[0];
8433 0 : lowrightpt[1] = GDXRegion[regionID]->lowrightpt[1];
8434 : }
8435 : }
8436 0 : return (status);
8437 : }
8438 :
8439 :
8440 :
8441 :
8442 :
8443 : /*----------------------------------------------------------------------------|
8444 : | BEGIN_PROLOG |
8445 : | |
8446 : | FUNCTION: GDextractregion |
8447 : | |
8448 : | DESCRIPTION: Retrieves data from specified region. |
8449 : | |
8450 : | |
8451 : | Return Value Type Units Description |
8452 : | ============ ====== ========= ===================================== |
8453 : | status intn return status (0) SUCCEED, (-1) FAIL |
8454 : | |
8455 : | INPUTS: |
8456 : | gridID int32 Grid structure ID |
8457 : | regionID int32 Region ID |
8458 : | fieldname char Fieldname |
8459 : | |
8460 : | OUTPUTS: |
8461 : | buffer void Data buffer containing subsetted region |
8462 : | |
8463 : | |
8464 : | NOTES: |
8465 : | |
8466 : | |
8467 : | Date Programmer Description |
8468 : | ====== ============ ================================================= |
8469 : | Jun 96 Joel Gales Original Programmer |
8470 : | Aug 96 Joel Gales Add vertical subsetting |
8471 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
8472 : | Apr 99 David Wynne Add support for MISR SOM projection, NCR 21195 |
8473 : | |
8474 : | END_PROLOG |
8475 : -----------------------------------------------------------------------------*/
8476 : intn
8477 : GDextractregion(int32 gridID, int32 regionID, char *fieldname,
8478 : VOIDP buffer)
8479 0 : {
8480 : intn i; /* Loop index */
8481 : intn j; /* Loop index */
8482 0 : intn status = 0; /* routine return status variable */
8483 :
8484 : int32 fid; /* HDF-EOS file ID */
8485 : int32 sdInterfaceID; /* HDF SDS interface ID */
8486 : int32 gdVgrpID; /* Grid root Vgroup ID */
8487 : int32 index; /* Dimension index */
8488 : int32 start[8]; /* Start array for data read */
8489 : int32 edge[8]; /* Edge array for data read */
8490 : int32 dims[8]; /* Dimensions */
8491 : int32 rank; /* Field rank */
8492 : int32 ntype; /* Field number type */
8493 : int32 origincode; /* Pixel origin code */
8494 :
8495 : char dimlist[256]; /* Dimension list */
8496 0 : char *errMesg = "Vertical Dimension Not Found: \"%s\".\n";
8497 0 : char *errM1 = "Both \"XDim\" and \"YDim\" must be present ";
8498 0 : char *errM2 = "in the dimension list for \"%s\".\n";
8499 : char errbuf[256];/* Error buffer */
8500 :
8501 :
8502 : /* Check for valid grid ID */
8503 : /* ----------------------- */
8504 0 : status = GDchkgdid(gridID, "GDextractregion", &fid, &sdInterfaceID,
8505 : &gdVgrpID);
8506 :
8507 :
8508 : /* Check for valid region ID */
8509 : /* ------------------------- */
8510 0 : if (status == 0)
8511 : {
8512 0 : if (regionID < 0 || regionID >= NGRIDREGN)
8513 : {
8514 0 : status = -1;
8515 0 : HEpush(DFE_RANGE, "GDextractregion", __FILE__, __LINE__);
8516 0 : HEreport("Invalid Region id: %d.\n", regionID);
8517 : }
8518 : }
8519 :
8520 :
8521 : /* Check for active region ID */
8522 : /* -------------------------- */
8523 0 : if (status == 0)
8524 : {
8525 0 : if (GDXRegion[regionID] == 0)
8526 : {
8527 0 : status = -1;
8528 0 : HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__);
8529 0 : HEreport("Inactive Region ID: %d.\n", regionID);
8530 : }
8531 : }
8532 :
8533 :
8534 :
8535 : /* Check that region defined for this file */
8536 : /* --------------------------------------- */
8537 0 : if (status == 0)
8538 : {
8539 0 : if (GDXRegion[regionID]->fid != fid)
8540 : {
8541 0 : status = -1;
8542 0 : HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__);
8543 0 : HEreport("Region is not defined for this file.\n");
8544 : }
8545 : }
8546 :
8547 :
8548 : /* Check that region defined for this grid */
8549 : /* --------------------------------------- */
8550 0 : if (status == 0)
8551 : {
8552 0 : if (GDXRegion[regionID]->gridID != gridID)
8553 : {
8554 0 : status = -1;
8555 0 : HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__);
8556 0 : HEreport("Region is not defined for this Grid.\n");
8557 : }
8558 : }
8559 :
8560 :
8561 :
8562 : /* Check for valid fieldname */
8563 : /* ------------------------- */
8564 0 : if (status == 0)
8565 : {
8566 0 : status = GDfieldinfo(gridID, fieldname, &rank, dims, &ntype, dimlist);
8567 :
8568 0 : if (status != 0)
8569 : {
8570 : /* Fieldname not found in grid */
8571 : /* --------------------------- */
8572 0 : status = -1;
8573 0 : HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__);
8574 0 : HEreport("Fieldname \"%s\" not found.\n",
8575 : fieldname);
8576 : }
8577 0 : else if (rank == 1)
8578 : {
8579 : /* Field is 1 dimensional */
8580 : /* ---------------------- */
8581 0 : status = -1;
8582 0 : HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__);
8583 0 : HEreport(
8584 : "One-Dimesional fields \"%s\" may not be subsetted.\n",
8585 : fieldname);
8586 : }
8587 : else
8588 : {
8589 : /* "XDim" and/or "YDim" not found */
8590 : /* ------------------------------ */
8591 0 : if (EHstrwithin("XDim", dimlist, ',') == -1 ||
8592 : EHstrwithin("YDim", dimlist, ',') == -1)
8593 : {
8594 0 : status = -1;
8595 0 : HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__);
8596 0 : sprintf(errbuf, "%s%s", errM1, errM2);
8597 0 : HEreport(errbuf, fieldname);
8598 : }
8599 : }
8600 : }
8601 :
8602 :
8603 :
8604 0 : if (status == 0)
8605 : {
8606 :
8607 : /* Get origin order info */
8608 : /* --------------------- */
8609 0 : status = GDorigininfo(gridID, &origincode);
8610 :
8611 :
8612 : /* Initialize start & edge arrays */
8613 : /* ------------------------------ */
8614 0 : for (i = 0; i < rank; i++)
8615 : {
8616 0 : start[i] = 0;
8617 0 : edge[i] = dims[i];
8618 : }
8619 :
8620 :
8621 : /* if MISR SOM projection, set start */
8622 : /* & edge arrays for SOMBlockDim */
8623 : /* --------------------------------- */
8624 0 : if (EHstrwithin("SOMBlockDim", dimlist, ',') == 0)
8625 : {
8626 0 : index = EHstrwithin("SOMBlockDim", dimlist, ',');
8627 0 : edge[index] = GDXRegion[regionID]->somCount;
8628 0 : start[index] = GDXRegion[regionID]->somStart;
8629 : }
8630 :
8631 :
8632 : /* Set start & edge arrays for XDim */
8633 : /* -------------------------------- */
8634 0 : index = EHstrwithin("XDim", dimlist, ',');
8635 0 : if (GDXRegion[regionID]->xCount != 0)
8636 : {
8637 0 : edge[index] = GDXRegion[regionID]->xCount;
8638 0 : start[index] = GDXRegion[regionID]->xStart;
8639 : }
8640 :
8641 : /* Adjust X-dim start if origin on right edge */
8642 : /* ------------------------------------------ */
8643 0 : if ((origincode & 1) == 1)
8644 : {
8645 0 : start[index] = dims[index] - (start[index] + edge[index]);
8646 : }
8647 :
8648 :
8649 : /* Set start & edge arrays for YDim */
8650 : /* -------------------------------- */
8651 0 : index = EHstrwithin("YDim", dimlist, ',');
8652 0 : if (GDXRegion[regionID]->yCount != 0)
8653 : {
8654 0 : start[index] = GDXRegion[regionID]->yStart;
8655 0 : edge[index] = GDXRegion[regionID]->yCount;
8656 : }
8657 :
8658 : /* Adjust Y-dim start if origin on lower edge */
8659 : /* ------------------------------------------ */
8660 0 : if ((origincode & 2) == 2)
8661 : {
8662 0 : start[index] = dims[index] - (start[index] + edge[index]);
8663 : }
8664 :
8665 :
8666 :
8667 : /* Vertical Subset */
8668 : /* --------------- */
8669 0 : for (j = 0; j < 8; j++)
8670 : {
8671 : /* If active vertical subset ... */
8672 : /* ----------------------------- */
8673 0 : if (GDXRegion[regionID]->StartVertical[j] != -1)
8674 : {
8675 :
8676 : /* Find vertical dimension within dimlist */
8677 : /* -------------------------------------- */
8678 0 : index = EHstrwithin(GDXRegion[regionID]->DimNamePtr[j],
8679 : dimlist, ',');
8680 :
8681 : /* If dimension found ... */
8682 : /* ---------------------- */
8683 0 : if (index != -1)
8684 : {
8685 : /* Compute start and edge for vertical dimension */
8686 : /* --------------------------------------------- */
8687 0 : start[index] = GDXRegion[regionID]->StartVertical[j];
8688 0 : edge[index] = GDXRegion[regionID]->StopVertical[j] -
8689 : GDXRegion[regionID]->StartVertical[j] + 1;
8690 : }
8691 : else
8692 : {
8693 : /* Vertical dimension not found */
8694 : /* ---------------------------- */
8695 0 : status = -1;
8696 0 : HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__);
8697 0 : HEreport(errMesg,
8698 : GDXRegion[regionID]->DimNamePtr[j]);
8699 : }
8700 : }
8701 : }
8702 :
8703 :
8704 : /* Read into data buffer */
8705 : /* --------------------- */
8706 0 : if (status == 0)
8707 : {
8708 0 : status = GDreadfield(gridID, fieldname, start, NULL, edge, buffer);
8709 : }
8710 : }
8711 0 : return (status);
8712 : }
8713 :
8714 :
8715 :
8716 :
8717 : /*----------------------------------------------------------------------------|
8718 : | BEGIN_PROLOG |
8719 : | |
8720 : | FUNCTION: GDdupregion |
8721 : | |
8722 : | DESCRIPTION: Duplicates a region |
8723 : | |
8724 : | |
8725 : | Return Value Type Units Description |
8726 : | ============ ====== ========= ===================================== |
8727 : | newregionID int32 New region ID |
8728 : | |
8729 : | INPUTS: |
8730 : | oldregionID int32 Old region ID |
8731 : | |
8732 : | OUTPUTS: |
8733 : | None |
8734 : | |
8735 : | NOTES: |
8736 : | |
8737 : | |
8738 : | Date Programmer Description |
8739 : | ====== ============ ================================================= |
8740 : | Jan 97 Joel Gales Original Programmer |
8741 : | Oct 98 Abe Taaheri changed *GDXRegion[i] = *GDXRegion[oldregionID]; |
8742 : | to copy elements of structure one by one to avoid |
8743 : | copying pointer for DimNamePtr to another place that|
8744 : | causes "Freeing Unallocated Memory" in purify when |
8745 : | using GDdetach |
8746 : | |
8747 : | END_PROLOG |
8748 : -----------------------------------------------------------------------------*/
8749 : int32
8750 : GDdupregion(int32 oldregionID)
8751 0 : {
8752 : intn i; /* Loop index */
8753 : intn j; /* Loop index */
8754 : int32 slendupregion;
8755 0 : int32 newregionID = -1; /* New region ID */
8756 :
8757 :
8758 : /* Find first empty (inactive) region */
8759 : /* ---------------------------------- */
8760 0 : for (i = 0; i < NGRIDREGN; i++)
8761 : {
8762 0 : if (GDXRegion[i] == 0)
8763 : {
8764 : /* Allocate space for new grid region entry */
8765 : /* ---------------------------------------- */
8766 0 : GDXRegion[i] = (struct gridRegion *)
8767 : calloc(1, sizeof(struct gridRegion));
8768 0 : if(GDXRegion[i] == NULL)
8769 : {
8770 0 : HEpush(DFE_NOSPACE,"GDdupregion", __FILE__, __LINE__);
8771 0 : return(-1);
8772 : }
8773 :
8774 :
8775 : /* Copy old region structure data to new region */
8776 : /* -------------------------------------------- */
8777 :
8778 0 : GDXRegion[i]->fid = GDXRegion[oldregionID]->fid;
8779 0 : GDXRegion[i]->gridID = GDXRegion[oldregionID]->gridID;
8780 0 : GDXRegion[i]->xStart = GDXRegion[oldregionID]->xStart;
8781 0 : GDXRegion[i]->xCount = GDXRegion[oldregionID]->xCount;
8782 0 : GDXRegion[i]->yStart = GDXRegion[oldregionID]->yStart;
8783 0 : GDXRegion[i]->yCount = GDXRegion[oldregionID]->yCount;
8784 0 : GDXRegion[i]->upleftpt[0] = GDXRegion[oldregionID]->upleftpt[0];
8785 0 : GDXRegion[i]->upleftpt[1] = GDXRegion[oldregionID]->upleftpt[1];
8786 0 : GDXRegion[i]->lowrightpt[0] = GDXRegion[oldregionID]->lowrightpt[0];
8787 0 : GDXRegion[i]->lowrightpt[1] = GDXRegion[oldregionID]->lowrightpt[1];
8788 0 : for (j = 0; j < 8; j++)
8789 : {
8790 0 : GDXRegion[i]->StartVertical[j] = GDXRegion[oldregionID]->StartVertical[j];
8791 0 : GDXRegion[i]->StopVertical[j] = GDXRegion[oldregionID]->StopVertical[j];
8792 : }
8793 :
8794 0 : for (j=0; j<8; j++)
8795 : {
8796 0 : if(GDXRegion[oldregionID]->DimNamePtr[j] != NULL)
8797 : {
8798 0 : slendupregion = strlen(GDXRegion[oldregionID]->DimNamePtr[j]);
8799 0 : GDXRegion[i]->DimNamePtr[j] = (char *) malloc(slendupregion + 1);
8800 0 : strcpy(GDXRegion[i]->DimNamePtr[j],GDXRegion[oldregionID]->DimNamePtr[j]);
8801 : }
8802 : }
8803 :
8804 :
8805 : /* Define new region ID */
8806 : /* -------------------- */
8807 0 : newregionID = i;
8808 :
8809 0 : break;
8810 : }
8811 : }
8812 0 : return (newregionID);
8813 : }
8814 :
8815 :
8816 : /*----------------------------------------------------------------------------|
8817 : | BEGIN_PROLOG |
8818 : | |
8819 : | FUNCTION: GDdefvrtregion |
8820 : | |
8821 : | DESCRIPTION: Finds elements of a monotonic field within a vertical subset |
8822 : | region. |
8823 : | |
8824 : | |
8825 : | Return Value Type Units Description |
8826 : | ============ ====== ========= ===================================== |
8827 : | regionID int32 Region ID |
8828 : | |
8829 : | INPUTS: |
8830 : | gridID int32 Grid structure ID |
8831 : | regionID int32 Region ID |
8832 : | vertObj char Vertical object to subset |
8833 : | range float64 Vertical subsetting range |
8834 : | |
8835 : | OUTPUTS: |
8836 : | None |
8837 : | |
8838 : | NOTES: |
8839 : | |
8840 : | |
8841 : | Date Programmer Description |
8842 : | ====== ============ ================================================= |
8843 : | Aug 96 Joel Gales Original Programmer |
8844 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
8845 : | Feb 97 Joel Gales Store XDim, YDim, upleftpt, lowrightpt in GDXRegion |
8846 : | |
8847 : | END_PROLOG |
8848 : -----------------------------------------------------------------------------*/
8849 : #define SETGRIDREG \
8850 : \
8851 : status = GDgridinfo(gridID, &xdimsize, &ydimsize, upleftpt, lowrightpt); \
8852 : for (k = 0; k < NGRIDREGN; k++) \
8853 : { \
8854 : if (GDXRegion[k] == 0) \
8855 : { \
8856 : GDXRegion[k] = (struct gridRegion *) \
8857 : calloc(1, sizeof(struct gridRegion)); \
8858 : GDXRegion[k]->fid = fid; \
8859 : GDXRegion[k]->gridID = gridID; \
8860 : GDXRegion[k]->xStart = 0; \
8861 : GDXRegion[k]->xCount = xdimsize; \
8862 : GDXRegion[k]->yStart = 0; \
8863 : GDXRegion[k]->yCount = ydimsize; \
8864 : GDXRegion[k]->upleftpt[0] = upleftpt[0]; \
8865 : GDXRegion[k]->upleftpt[1] = upleftpt[1]; \
8866 : GDXRegion[k]->lowrightpt[0] = lowrightpt[0]; \
8867 : GDXRegion[k]->lowrightpt[1] = lowrightpt[1]; \
8868 : regionID = k; \
8869 : for (j=0; j<8; j++) \
8870 : { \
8871 : GDXRegion[k]->StartVertical[j] = -1; \
8872 : GDXRegion[k]->StopVertical[j] = -1; \
8873 : } \
8874 : break; \
8875 : } \
8876 : }
8877 :
8878 : #define FILLVERTREG \
8879 : for (j=0; j<8; j++) \
8880 : { \
8881 : if (GDXRegion[regionID]->StartVertical[j] == -1) \
8882 : { \
8883 : GDXRegion[regionID]->StartVertical[j] = i; \
8884 : GDXRegion[regionID]->DimNamePtr[j] = \
8885 : (char *) malloc(slen + 1); \
8886 : memcpy(GDXRegion[regionID]->DimNamePtr[j], \
8887 : dimlist, slen + 1); \
8888 : break; \
8889 : } \
8890 : } \
8891 :
8892 :
8893 :
8894 : int32
8895 : GDdefvrtregion(int32 gridID, int32 regionID, char *vertObj, float64 range[])
8896 0 : {
8897 : intn i, j, k, status;
8898 0 : uint8 found = 0;
8899 :
8900 : int16 vertINT16;
8901 :
8902 : int32 fid, sdInterfaceID, slen;
8903 : int32 gdVgrpID, rank, nt, dims[8], size;
8904 : int32 vertINT32;
8905 : int32 xdimsize;
8906 : int32 ydimsize;
8907 :
8908 : float32 vertFLT32;
8909 : float64 vertFLT64;
8910 : float64 upleftpt[2];
8911 : float64 lowrightpt[2];
8912 :
8913 : char *vertArr;
8914 : char *dimlist;
8915 :
8916 : /* Allocate space for dimlist */
8917 : /* --------------------------------- */
8918 0 : dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
8919 0 : if(dimlist == NULL)
8920 : {
8921 0 : HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__);
8922 0 : return(-1);
8923 : }
8924 : /* Check for valid grid ID */
8925 : /* ----------------------- */
8926 0 : status = GDchkgdid(gridID, "GDdefvrtregion",
8927 : &fid, &sdInterfaceID, &gdVgrpID);
8928 :
8929 0 : if (status == 0)
8930 : {
8931 0 : memcpy(dimlist, vertObj, 4);
8932 0 : dimlist[4] = 0;
8933 :
8934 0 : if (strcmp(dimlist, "DIM:") == 0)
8935 : {
8936 0 : slen = strlen(vertObj) - 4;
8937 0 : if (regionID == -1)
8938 : {
8939 0 : SETGRIDREG;
8940 : }
8941 0 : for (j = 0; j < 8; j++)
8942 : {
8943 0 : if (GDXRegion[regionID]->StartVertical[j] == -1)
8944 : {
8945 0 : GDXRegion[regionID]->StartVertical[j] = (int32) range[0];
8946 0 : GDXRegion[regionID]->StopVertical[j] = (int32) range[1];
8947 0 : GDXRegion[regionID]->DimNamePtr[j] =
8948 : (char *) malloc(slen + 1);
8949 0 : if(GDXRegion[regionID]->DimNamePtr[j] == NULL)
8950 : {
8951 0 : HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__);
8952 0 : free(dimlist);
8953 0 : return(-1);
8954 : }
8955 0 : memcpy(GDXRegion[regionID]->DimNamePtr[j],
8956 : vertObj + 4, slen + 1);
8957 0 : break;
8958 : }
8959 : }
8960 : }
8961 : else
8962 : {
8963 0 : status = GDfieldinfo(gridID, vertObj, &rank, dims, &nt, dimlist);
8964 0 : if (status != 0)
8965 : {
8966 0 : status = -1;
8967 0 : HEpush(DFE_GENAPP, "GDdefvrtregion", __FILE__, __LINE__);
8968 0 : HEreport("Vertical Field: \"%s\" not found.\n", vertObj);
8969 : }
8970 : else
8971 : {
8972 0 : if (rank != 1)
8973 : {
8974 0 : status = -1;
8975 0 : HEpush(DFE_GENAPP, "GDdefvrtregion", __FILE__, __LINE__);
8976 0 : HEreport("Vertical Field: \"%s\" must be 1-dim.\n",
8977 : vertObj);
8978 : }
8979 : else
8980 : {
8981 0 : slen = strlen(dimlist);
8982 0 : size = DFKNTsize(nt);
8983 0 : vertArr = (char *) calloc(dims[0], size);
8984 0 : if(vertArr == NULL)
8985 : {
8986 0 : HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__);
8987 0 : free(dimlist);
8988 0 : return(-1);
8989 : }
8990 :
8991 0 : status = GDreadfield(gridID, vertObj,
8992 : NULL, NULL, NULL, vertArr);
8993 :
8994 0 : switch (nt)
8995 : {
8996 : case DFNT_INT16:
8997 :
8998 0 : for (i = 0; i < dims[0]; i++)
8999 : {
9000 0 : memcpy(&vertINT16, vertArr + i * size, size);
9001 :
9002 0 : if (vertINT16 >= range[0] &&
9003 : vertINT16 <= range[1])
9004 : {
9005 0 : found = 1;
9006 0 : if (regionID == -1)
9007 : {
9008 0 : SETGRIDREG;
9009 : }
9010 0 : FILLVERTREG;
9011 :
9012 0 : break;
9013 : }
9014 : }
9015 :
9016 0 : if (found == 1)
9017 : {
9018 0 : for (i = dims[0] - 1; i >= 0; i--)
9019 : {
9020 0 : memcpy(&vertINT16, vertArr + i * size, size);
9021 :
9022 0 : if (vertINT16 >= range[0] &&
9023 : vertINT16 <= range[1])
9024 : {
9025 0 : GDXRegion[regionID]->StopVertical[j] = i;
9026 0 : break;
9027 : }
9028 : }
9029 : }
9030 : else
9031 : {
9032 0 : status = -1;
9033 : }
9034 0 : break;
9035 :
9036 :
9037 : case DFNT_INT32:
9038 :
9039 0 : for (i = 0; i < dims[0]; i++)
9040 : {
9041 0 : memcpy(&vertINT32, vertArr + i * size, size);
9042 :
9043 0 : if (vertINT32 >= range[0] &&
9044 : vertINT32 <= range[1])
9045 : {
9046 0 : found = 1;
9047 0 : if (regionID == -1)
9048 : {
9049 0 : SETGRIDREG;
9050 : }
9051 0 : FILLVERTREG;
9052 :
9053 0 : break;
9054 : }
9055 : }
9056 :
9057 0 : if (found == 1)
9058 : {
9059 0 : for (i = dims[0] - 1; i >= 0; i--)
9060 : {
9061 0 : memcpy(&vertINT32, vertArr + i * size, size);
9062 :
9063 0 : if (vertINT32 >= range[0] &&
9064 : vertINT32 <= range[1])
9065 : {
9066 0 : GDXRegion[regionID]->StopVertical[j] = i;
9067 0 : break;
9068 : }
9069 : }
9070 : }
9071 : else
9072 : {
9073 0 : status = -1;
9074 : }
9075 0 : break;
9076 :
9077 :
9078 : case DFNT_FLOAT32:
9079 :
9080 0 : for (i = 0; i < dims[0]; i++)
9081 : {
9082 0 : memcpy(&vertFLT32, vertArr + i * size, size);
9083 :
9084 0 : if (vertFLT32 >= range[0] &&
9085 : vertFLT32 <= range[1])
9086 : {
9087 0 : found = 1;
9088 0 : if (regionID == -1)
9089 : {
9090 0 : SETGRIDREG;
9091 : }
9092 0 : FILLVERTREG;
9093 :
9094 0 : break;
9095 : }
9096 : }
9097 :
9098 0 : if (found == 1)
9099 : {
9100 0 : for (i = dims[0] - 1; i >= 0; i--)
9101 : {
9102 0 : memcpy(&vertFLT32, vertArr + i * size, size);
9103 :
9104 0 : if (vertFLT32 >= range[0] &&
9105 : vertFLT32 <= range[1])
9106 : {
9107 0 : GDXRegion[regionID]->StopVertical[j] = i;
9108 0 : break;
9109 : }
9110 : }
9111 : }
9112 : else
9113 : {
9114 0 : status = -1;
9115 : }
9116 0 : break;
9117 :
9118 :
9119 : case DFNT_FLOAT64:
9120 :
9121 0 : for (i = 0; i < dims[0]; i++)
9122 : {
9123 0 : memcpy(&vertFLT64, vertArr + i * size, size);
9124 :
9125 0 : if (vertFLT64 >= range[0] &&
9126 : vertFLT64 <= range[1])
9127 : {
9128 0 : found = 1;
9129 0 : if (regionID == -1)
9130 : {
9131 0 : SETGRIDREG;
9132 : }
9133 0 : FILLVERTREG;
9134 :
9135 0 : break;
9136 : }
9137 : }
9138 :
9139 0 : if (found == 1)
9140 : {
9141 0 : for (i = dims[0] - 1; i >= 0; i--)
9142 : {
9143 0 : memcpy(&vertFLT64, vertArr + i * size, size);
9144 :
9145 0 : if (vertFLT64 >= range[0] &&
9146 : vertFLT64 <= range[1])
9147 : {
9148 0 : GDXRegion[regionID]->StopVertical[j] = i;
9149 0 : break;
9150 : }
9151 : }
9152 : }
9153 : else
9154 : {
9155 0 : status = -1;
9156 : }
9157 : break;
9158 :
9159 : }
9160 0 : free(vertArr);
9161 : }
9162 : }
9163 : }
9164 : }
9165 0 : if (status == -1)
9166 : {
9167 0 : regionID = -1;
9168 : }
9169 0 : free(dimlist);
9170 0 : return (regionID);
9171 : }
9172 :
9173 :
9174 :
9175 :
9176 :
9177 : /*----------------------------------------------------------------------------|
9178 : | BEGIN_PROLOG |
9179 : | |
9180 : | FUNCTION: GDdeftimeperiod |
9181 : | |
9182 : | DESCRIPTION: Finds elements of the "Time" field within a given time |
9183 : | period. |
9184 : | |
9185 : | |
9186 : | Return Value Type Units Description |
9187 : | ============ ====== ========= ===================================== |
9188 : | periodID int32 Period ID |
9189 : | |
9190 : | INPUTS: |
9191 : | gridID int32 Grid structure ID |
9192 : | periodID int32 Period ID |
9193 : | starttime float64 TAI sec Start of time period |
9194 : | stoptime float64 TAI sec Stop of time period |
9195 : | |
9196 : | OUTPUTS: |
9197 : | None |
9198 : | |
9199 : | NOTES: |
9200 : | |
9201 : | |
9202 : | Date Programmer Description |
9203 : | ====== ============ ================================================= |
9204 : | Aug 96 Joel Gales Original Programmer |
9205 : | |
9206 : | END_PROLOG |
9207 : -----------------------------------------------------------------------------*/
9208 : int32
9209 : GDdeftimeperiod(int32 gridID, int32 periodID, float64 starttime,
9210 : float64 stoptime)
9211 0 : {
9212 : float64 timerange[2];
9213 :
9214 0 : timerange[0] = starttime;
9215 0 : timerange[1] = stoptime;
9216 :
9217 0 : periodID = GDdefvrtregion(gridID, periodID, "Time", timerange);
9218 :
9219 0 : return (periodID);
9220 : }
9221 :
9222 :
9223 :
9224 : /*----------------------------------------------------------------------------|
9225 : | BEGIN_PROLOG |
9226 : | |
9227 : | FUNCTION: GDgetpixels |
9228 : | |
9229 : | DESCRIPTION: Finds row and columns for specified lon/lat values |
9230 : | |
9231 : | |
9232 : | Return Value Type Units Description |
9233 : | ============ ====== ========= ===================================== |
9234 : | status intn return status (0) SUCCEED, (-1) FAIL |
9235 : | |
9236 : | INPUTS: |
9237 : | gridID int32 Grid structure ID |
9238 : | nLonLat int32 Number of lonlat values |
9239 : | lonVal float64 dec deg Longitude values |
9240 : | latVal float64 dec deg Latitude values |
9241 : | |
9242 : | |
9243 : | OUTPUTS: |
9244 : | pixRow int32 Pixel rows |
9245 : | pixCol int32 Pixel columns |
9246 : | |
9247 : | NOTES: |
9248 : | |
9249 : | |
9250 : | Date Programmer Description |
9251 : | ====== ============ ================================================= |
9252 : | Aug 96 Joel Gales Original Programmer |
9253 : | Oct 96 Joel Gales Set row/col to -1 if outside boundary |
9254 : | Mar 97 Joel Gales Adjust row/col for CORNER pixel registration |
9255 : | |
9256 : | END_PROLOG |
9257 : -----------------------------------------------------------------------------*/
9258 : intn
9259 : GDgetpixels(int32 gridID, int32 nLonLat, float64 lonVal[], float64 latVal[],
9260 : int32 pixRow[], int32 pixCol[])
9261 0 : {
9262 : intn i; /* Loop index */
9263 0 : intn status = 0; /* routine return status variable */
9264 :
9265 : int32 fid; /* HDF-EOS file ID */
9266 : int32 sdInterfaceID; /* HDF SDS interface ID */
9267 : int32 gdVgrpID; /* Grid root Vgroup ID */
9268 :
9269 : int32 xdimsize; /* Size of "XDim" */
9270 : int32 ydimsize; /* Size of "YDim" */
9271 : int32 projcode; /* GCTP projection code */
9272 : int32 zonecode; /* Zone code */
9273 : int32 spherecode; /* Sphere code */
9274 : int32 origincode; /* Origin code */
9275 : int32 pixregcode; /* Pixel registration code */
9276 :
9277 : float64 upleftpt[2];/* Upper left point */
9278 : float64 lowrightpt[2]; /* Lower right point */
9279 : float64 projparm[16]; /* Projection parameters */
9280 : float64 *xVal; /* Pointer to point x location values */
9281 : float64 *yVal; /* Pointer to point y location values */
9282 :
9283 :
9284 : /* Check for valid grid ID */
9285 : /* ----------------------- */
9286 0 : status = GDchkgdid(gridID, "GDgetpixels", &fid, &sdInterfaceID, &gdVgrpID);
9287 :
9288 0 : if (status == 0)
9289 : {
9290 : /* Get grid info */
9291 : /* ------------- */
9292 0 : status = GDgridinfo(gridID, &xdimsize, &ydimsize,
9293 : upleftpt, lowrightpt);
9294 :
9295 :
9296 : /* Get projection info */
9297 : /* ------------------- */
9298 0 : status = GDprojinfo(gridID, &projcode, &zonecode,
9299 : &spherecode, projparm);
9300 :
9301 :
9302 : /* Get explicit upleftpt & lowrightpt if defaults are used */
9303 : /* ------------------------------------------------------- */
9304 0 : status = GDgetdefaults(projcode, zonecode, projparm, spherecode,
9305 : upleftpt, lowrightpt);
9306 :
9307 :
9308 : /* Get pixel registration and origin info */
9309 : /* -------------------------------------- */
9310 0 : status = GDorigininfo(gridID, &origincode);
9311 0 : status = GDpixreginfo(gridID, &pixregcode);
9312 :
9313 :
9314 : /* Allocate space for x & y locations */
9315 : /* ---------------------------------- */
9316 0 : xVal = (float64 *) calloc(nLonLat, sizeof(float64));
9317 0 : if(xVal == NULL)
9318 : {
9319 0 : HEpush(DFE_NOSPACE,"GDgetpixels", __FILE__, __LINE__);
9320 0 : return(-1);
9321 : }
9322 0 : yVal = (float64 *) calloc(nLonLat, sizeof(float64));
9323 0 : if(yVal == NULL)
9324 : {
9325 0 : HEpush(DFE_NOSPACE,"GDgetpixels", __FILE__, __LINE__);
9326 0 : free(xVal);
9327 0 : return(-1);
9328 : }
9329 :
9330 :
9331 : /* Get pixRow, pixCol, xVal, & yVal */
9332 : /* -------------------------------- */
9333 0 : status = GDll2ij(projcode, zonecode, projparm, spherecode,
9334 : xdimsize, ydimsize, upleftpt, lowrightpt,
9335 : nLonLat, lonVal, latVal, pixRow, pixCol,
9336 : xVal, yVal);
9337 :
9338 :
9339 :
9340 : /* Loop through all lon/lat values */
9341 : /* ------------------------------- */
9342 0 : for (i = 0; i < nLonLat; i++)
9343 : {
9344 : /* Adjust columns & rows for "corner" registered grids */
9345 : /* --------------------------------------------------- */
9346 0 : if (pixregcode == HDFE_CORNER)
9347 : {
9348 0 : if (origincode == HDFE_GD_UL)
9349 : {
9350 0 : if (xVal[i] - pixCol[i] > 0.5)
9351 : {
9352 0 : ++pixCol[i];
9353 : }
9354 :
9355 0 : if (yVal[i] - pixRow[i] > 0.5)
9356 : {
9357 0 : ++pixRow[i];
9358 : }
9359 : }
9360 0 : else if (origincode == HDFE_GD_UR)
9361 : {
9362 0 : if (xVal[i] - pixCol[i] <= 0.5)
9363 : {
9364 0 : --pixCol[i];
9365 : }
9366 :
9367 0 : if (yVal[i] - pixRow[i] > 0.5)
9368 : {
9369 0 : ++pixRow[i];
9370 : }
9371 : }
9372 0 : else if (origincode == HDFE_GD_LL)
9373 : {
9374 0 : if (xVal[i] - pixCol[i] > 0.5)
9375 : {
9376 0 : ++pixCol[i];
9377 : }
9378 :
9379 0 : if (yVal[i] - pixRow[i] <= 0.5)
9380 : {
9381 0 : --pixRow[i];
9382 : }
9383 : }
9384 0 : else if (origincode == HDFE_GD_LR)
9385 : {
9386 0 : if (xVal[i] - pixCol[i] <= 0.5)
9387 : {
9388 0 : --pixCol[i];
9389 : }
9390 :
9391 0 : if (yVal[i] - pixRow[i] <= 0.5)
9392 : {
9393 0 : --pixRow[i];
9394 : }
9395 : }
9396 : }
9397 :
9398 :
9399 : /* If outside grid boundaries then set to -1 */
9400 : /* ----------------------------------------- */
9401 0 : if (pixCol[i] < 0 || pixCol[i] >= xdimsize ||
9402 : pixRow[i] < 0 || pixRow[i] >= ydimsize)
9403 : {
9404 0 : pixCol[i] = -1;
9405 0 : pixRow[i] = -1;
9406 : }
9407 : }
9408 0 : free(xVal);
9409 0 : free(yVal);
9410 : }
9411 0 : return (status);
9412 : }
9413 :
9414 :
9415 :
9416 :
9417 :
9418 : /*----------------------------------------------------------------------------|
9419 : | BEGIN_PROLOG |
9420 : | |
9421 : | FUNCTION: GDgetpixvalues |
9422 : | |
9423 : | DESCRIPTION: Retrieves data from specified pixels. |
9424 : | |
9425 : | |
9426 : | Return Value Type Units Description |
9427 : | ============ ====== ========= ===================================== |
9428 : | size*nPixels int32 Size of data buffer |
9429 : | |
9430 : | INPUTS: |
9431 : | gridID int32 Grid structure ID |
9432 : | nPixels int32 Number of pixels |
9433 : | pixRow int32 Pixel row numbers |
9434 : | pixCol int32 Pixel column numbers |
9435 : | fieldname char Fieldname |
9436 : | |
9437 : | OUTPUTS: |
9438 : | buffer void Data buffer |
9439 : | |
9440 : | |
9441 : | NOTES: |
9442 : | |
9443 : | |
9444 : | Date Programmer Description |
9445 : | ====== ============ ================================================= |
9446 : | Aug 96 Joel Gales Original Programmer |
9447 : | Oct 96 Joel Gales Check for pixels outside boundaries (-1) |
9448 : | Mar 98 Abe Taaheri revised to reduce overhead for rechecking |
9449 : | for gridid, fieldname, etc in GDreadfield. |
9450 : | June 98 AT fixed bug with 2-dim field merged in 3-dim field |
9451 : | (for offset and count) |
9452 : | END_PROLOG |
9453 : -----------------------------------------------------------------------------*/
9454 : int32
9455 : GDgetpixvalues(int32 gridID, int32 nPixels, int32 pixRow[], int32 pixCol[],
9456 : char *fieldname, VOIDP buffer)
9457 0 : {
9458 : intn i; /* Loop index */
9459 : intn j; /* Loop index */
9460 0 : intn status = 0; /* routine return status variable */
9461 :
9462 : int32 fid; /* HDF-EOS file ID */
9463 : int32 sdInterfaceID; /* HDF SDS interface ID */
9464 : int32 gdVgrpID; /* Grid root Vgroup ID */
9465 :
9466 : int32 start[8]; /* GDreadfield start array */
9467 : int32 edge[8]; /* GDreadfield edge array */
9468 : int32 dims[8]; /* Field dimensions */
9469 : int32 rank; /* Field rank */
9470 : int32 xdum; /* Location of "XDim" within field list */
9471 : int32 ydum; /* Location of "YDim" within field list */
9472 : int32 ntype; /* Field number type */
9473 : int32 origincode; /* Origin code */
9474 : int32 bufOffset; /* Data buffer offset */
9475 : int32 size; /* Size of returned data buffer for each
9476 : * value in bytes */
9477 : int32 offset[8]; /* I/O offset (start) */
9478 : int32 incr[8]; /* I/O increment (stride) */
9479 : int32 count[8]; /* I/O count (edge) */
9480 : int32 sdid; /* SDS ID */
9481 : int32 rankSDS; /* Rank of SDS */
9482 : int32 rankFld; /* Rank of field */
9483 : int32 dum; /* Dummy variable */
9484 : int32 mrgOffset; /* Merged field offset */
9485 :
9486 : char *dimlist; /* Dimension list */
9487 :
9488 :
9489 :
9490 : /* Allocate space for dimlist */
9491 : /* --------------------------------- */
9492 0 : dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
9493 0 : if(dimlist == NULL)
9494 : {
9495 0 : HEpush(DFE_NOSPACE,"GDgetpixvalues", __FILE__, __LINE__);
9496 0 : return(-1);
9497 : }
9498 : /* Check for valid grid ID */
9499 : /* ----------------------- */
9500 0 : status = GDchkgdid(gridID, "GDgetpixvalues",
9501 : &fid, &sdInterfaceID, &gdVgrpID);
9502 :
9503 :
9504 0 : if (status == 0)
9505 : {
9506 : /* Get field list */
9507 : /* -------------- */
9508 0 : status = GDfieldinfo(gridID, fieldname, &rank, dims, &ntype, dimlist);
9509 :
9510 :
9511 : /* Check for "XDim" & "YDim" in dimension list */
9512 : /* ------------------------------------------- */
9513 0 : if (status == 0)
9514 : {
9515 0 : xdum = EHstrwithin("XDim", dimlist, ',');
9516 0 : ydum = EHstrwithin("YDim", dimlist, ',');
9517 :
9518 0 : if (xdum == -1)
9519 : {
9520 0 : status = -1;
9521 0 : HEpush(DFE_GENAPP, "GDgetpixvalues", __FILE__, __LINE__);
9522 0 : HEreport(
9523 : "\"XDim\" not present in dimlist for field: \"%s\".\n",
9524 : fieldname);
9525 : }
9526 :
9527 :
9528 0 : if (ydum == -1)
9529 : {
9530 0 : status = -1;
9531 0 : HEpush(DFE_GENAPP, "GDgetpixvalues", __FILE__, __LINE__);
9532 0 : HEreport(
9533 : "\"YDim\" not present in dimlist for field: \"%s\".\n",
9534 : fieldname);
9535 : }
9536 : }
9537 : else
9538 : {
9539 0 : status = -1;
9540 0 : HEpush(DFE_GENAPP, "GDgetpixvalues", __FILE__, __LINE__);
9541 0 : HEreport("Fieldname \"%s\" not found.\n", fieldname);
9542 : }
9543 :
9544 :
9545 0 : if (status == 0)
9546 : {
9547 :
9548 : /* Get origin order info */
9549 : /* --------------------- */
9550 0 : status = GDorigininfo(gridID, &origincode);
9551 :
9552 :
9553 : /* Initialize start & edge arrays */
9554 : /* ------------------------------ */
9555 0 : for (i = 0; i < rank; i++)
9556 : {
9557 0 : start[i] = 0;
9558 0 : edge[i] = dims[i];
9559 : }
9560 :
9561 :
9562 : /* Compute size of data buffer for each pixel */
9563 : /* ------------------------------------------ */
9564 0 : edge[xdum] = 1;
9565 0 : edge[ydum] = 1;
9566 0 : size = edge[0];
9567 0 : for (j = 1; j < rank; j++)
9568 : {
9569 0 : size *= edge[j];
9570 : }
9571 0 : size *= DFKNTsize(ntype);
9572 :
9573 :
9574 :
9575 : /* If data values are requested ... */
9576 : /* -------------------------------- */
9577 0 : if (buffer != NULL)
9578 : {
9579 : /* get sdid */
9580 0 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid,
9581 : &rankSDS, &rankFld, &mrgOffset, dims, &dum);
9582 :
9583 : /* Loop through all pixels */
9584 : /* ----------------------- */
9585 0 : for (i = 0; i < nPixels; i++)
9586 : {
9587 : /* Conmpute offset within returned data buffer */
9588 : /* ------------------------------------------- */
9589 0 : bufOffset = size * i;
9590 :
9591 :
9592 : /* If pixel row & column OK ... */
9593 : /* ---------------------------- */
9594 0 : if (pixCol[i] != -1 && pixRow[i] != -1)
9595 : {
9596 0 : start[xdum] = pixCol[i];
9597 0 : start[ydum] = pixRow[i];
9598 :
9599 :
9600 : /* Adjust X-dim start if origin on right edge */
9601 : /* ------------------------------------------ */
9602 0 : if ((origincode & 1) == 1)
9603 : {
9604 0 : start[xdum] = dims[xdum] - (start[xdum] + 1);
9605 : }
9606 :
9607 :
9608 : /* Adjust Y-dim start if origin on lower edge */
9609 : /* ------------------------------------------ */
9610 0 : if ((origincode & 2) == 2)
9611 : {
9612 0 : start[ydum] = dims[ydum] - (start[ydum] + 1);
9613 : }
9614 :
9615 : /* Set I/O offset and count Section */
9616 : /* ---------------------- */
9617 :
9618 : /*
9619 : * start and edge != NULL, set I/O offset and count to
9620 : * user values, adjusting the
9621 : * 0th field with the merged field offset (if any)
9622 : */
9623 0 : if (rankFld == rankSDS)
9624 : {
9625 0 : for (j = 0; j < rankSDS; j++)
9626 : {
9627 0 : offset[j] = start[j];
9628 0 : count[j] = edge[j];
9629 : }
9630 0 : offset[0] += mrgOffset;
9631 : }
9632 : else
9633 : {
9634 : /*
9635 : * If field really 2-dim merged in 3-dim field then set
9636 : * 0th field offset to merge offset and then next two to
9637 : * the user values
9638 : */
9639 0 : for (j = 0; j < rankFld; j++)
9640 : {
9641 0 : offset[j + 1] = start[j];
9642 0 : count[j + 1] = edge[j];
9643 : }
9644 0 : offset[0] = mrgOffset;
9645 0 : count[0] = 1;
9646 : }
9647 :
9648 :
9649 :
9650 : /* Set I/O stride Section */
9651 : /* ---------------------- */
9652 :
9653 : /* In original code stride enetred as NULL.
9654 : Abe Taaheri June 12, 1998 */
9655 : /*
9656 : * If stride == NULL (default) set I/O stride to 1
9657 : */
9658 0 : for (j = 0; j < rankSDS; j++)
9659 : {
9660 0 : incr[j] = 1;
9661 : }
9662 :
9663 :
9664 : /* Read into data buffer */
9665 : /* --------------------- */
9666 0 : status = SDreaddata(sdid,
9667 : offset, incr, count,
9668 : (VOIDP) ((uint8 *) buffer + bufOffset));
9669 : }
9670 : }
9671 : }
9672 : }
9673 : }
9674 :
9675 :
9676 : /* If successful return size of returned data in bytes */
9677 : /* --------------------------------------------------- */
9678 0 : if (status == 0)
9679 : {
9680 0 : free(dimlist);
9681 0 : return (size * nPixels);
9682 : }
9683 : else
9684 : {
9685 0 : free(dimlist);
9686 0 : return ((int32) status);
9687 : }
9688 : }
9689 :
9690 :
9691 : /*----------------------------------------------------------------------------|
9692 : | BEGIN_PROLOG |
9693 : | |
9694 : | FUNCTION: GDinterpolate |
9695 : | |
9696 : | DESCRIPTION: Performs bilinear interpolate on a set of xy values |
9697 : | |
9698 : | |
9699 : | Return Value Type Units Description |
9700 : | ============ ====== ========= ===================================== |
9701 : | nRetn*nValues* int32 Size of data buffer |
9702 : | sizeof(float64) |
9703 : | |
9704 : | INPUTS: |
9705 : | gridID int32 Grid structure ID |
9706 : | nValues int32 Number of lon/lat points to interpolate |
9707 : | xyValues float64 XY values of points to interpolate |
9708 : | fieldname char Fieldname |
9709 : | |
9710 : | OUTPUTS: |
9711 : | interpVal float64 Interpolated Data Values |
9712 : | |
9713 : | |
9714 : | NOTES: |
9715 : | |
9716 : | |
9717 : | Date Programmer Description |
9718 : | ====== ============ ================================================= |
9719 : | Aug 96 Joel Gales Original Programmer |
9720 : | Oct 96 Joel Gales Fix array index problem with interpVal write |
9721 : | Apr 97 Joel Gales Trap interpolation boundary out of bounds error |
9722 : | Jun 98 Abe Taaheri changed the return value so that the Return Value |
9723 : | is size in bytes for the data buffer which is |
9724 : | float64.
9725 : | |
9726 : | END_PROLOG |
9727 : -----------------------------------------------------------------------------*/
9728 : int32
9729 : GDinterpolate(int32 gridID, int32 nValues, float64 lonVal[], float64 latVal[],
9730 : char *fieldname, float64 interpVal[])
9731 0 : {
9732 : intn i; /* Loop index */
9733 : intn j; /* Loop index */
9734 : intn k; /* Loop index */
9735 0 : intn status = 0; /* routine return status variable */
9736 :
9737 : int32 fid; /* HDF-EOS file ID */
9738 : int32 sdInterfaceID; /* HDF SDS interface ID */
9739 : int32 gdVgrpID; /* Grid root Vgroup ID */
9740 : int32 xdimsize; /* XDim size */
9741 : int32 ydimsize; /* YDim size */
9742 : int32 projcode; /* Projection code */
9743 : int32 zonecode; /* Zone code */
9744 : int32 spherecode; /* Sphere code */
9745 : int32 pixregcode; /* Pixel registration code */
9746 : int32 origincode; /* Origin code */
9747 : int32 dims[8]; /* Field dimensions */
9748 : int32 numsize; /* Size in bytes of number type */
9749 : int32 rank; /* Field rank */
9750 : int32 xdum; /* Location of "XDim" within field list */
9751 : int32 ydum; /* Location of "YDim" within field list */
9752 : int32 ntype; /* Number type */
9753 : int32 dum; /* Dummy variable */
9754 : int32 size; /* Size of returned data buffer for each
9755 : * value in bytes */
9756 : int32 pixCol[4]; /* Pixel columns for 4 nearest neighbors */
9757 : int32 pixRow[4]; /* Pixel rows for 4 nearest neighbors */
9758 : int32 tDen; /* Interpolation denominator value 1 */
9759 : int32 uDen; /* Interpolation denominator value 2 */
9760 : int32 nRetn; /* Number of data values returned */
9761 :
9762 : float64 upleftpt[2];/* Upper left pt coordinates */
9763 : float64 lowrightpt[2]; /* Lower right pt coordinates */
9764 : float64 projparm[16]; /* Projection parameters */
9765 : float64 xVal; /* "Exact" x location of interpolated point */
9766 : float64 yVal; /* "Exact" y location of interpolated point */
9767 0 : float64 tNum = 0.0; /* Interpolation numerator value 1 */
9768 0 : float64 uNum = 0.0; /* Interpolation numerator value 2 */
9769 :
9770 : int16 i16[4]; /* Working buffer (int16) */
9771 : int32 i32[4]; /* Working buffer (int132) */
9772 : float32 f32[4]; /* Working buffer (float32) */
9773 : float64 f64[4]; /* Working buffer (float64) */
9774 :
9775 : char *pixVal; /* Nearest neighbor values */
9776 : char *dimlist; /* Dimension list */
9777 :
9778 : /* Allocate space for dimlist */
9779 : /* --------------------------------- */
9780 0 : dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
9781 0 : if(dimlist == NULL)
9782 : {
9783 0 : HEpush(DFE_NOSPACE,"GDinterpolate", __FILE__, __LINE__);
9784 0 : return(-1);
9785 : }
9786 : /* Check for valid grid ID */
9787 : /* ----------------------- */
9788 0 : status = GDchkgdid(gridID, "GDinterpolate",
9789 : &fid, &sdInterfaceID, &gdVgrpID);
9790 :
9791 :
9792 : /* If no problems ... */
9793 : /* ------------------ */
9794 0 : if (status == 0)
9795 : {
9796 : /* Get field information */
9797 : /* --------------------- */
9798 0 : status = GDfieldinfo(gridID, fieldname, &rank, dims, &ntype, dimlist);
9799 :
9800 :
9801 : /* Check for "XDim" & "YDim" in dimension list */
9802 : /* ------------------------------------------- */
9803 0 : if (status == 0)
9804 : {
9805 0 : xdum = EHstrwithin("XDim", dimlist, ',');
9806 0 : ydum = EHstrwithin("YDim", dimlist, ',');
9807 :
9808 0 : if (xdum == -1)
9809 : {
9810 0 : status = -1;
9811 0 : HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__);
9812 0 : HEreport(
9813 : "\"XDim\" not present in dimlist for field: \"%s\".\n",
9814 : fieldname);
9815 : }
9816 :
9817 :
9818 0 : if (ydum == -1)
9819 : {
9820 0 : status = -1;
9821 0 : HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__);
9822 0 : HEreport(
9823 : "\"YDim\" not present in dimlist for field: \"%s\".\n",
9824 : fieldname);
9825 : }
9826 : }
9827 : else
9828 : {
9829 : /* Fieldname not found in grid */
9830 : /* --------------------------- */
9831 0 : status = -1;
9832 0 : HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__);
9833 0 : HEreport("Fieldname \"%s\" not found.\n", fieldname);
9834 : }
9835 :
9836 :
9837 : /* If no problems ... */
9838 : /* ------------------ */
9839 0 : if (status == 0)
9840 : {
9841 : /* Compute size of data buffer for each interpolated value */
9842 : /* ------------------------------------------------------- */
9843 0 : dims[xdum] = 1;
9844 0 : dims[ydum] = 1;
9845 0 : size = dims[0];
9846 0 : for (i = 1; i < rank; i++)
9847 : {
9848 0 : size *= dims[i];
9849 : }
9850 0 : numsize = DFKNTsize(ntype);
9851 0 : size *= numsize;
9852 :
9853 0 : nRetn = size / numsize;
9854 :
9855 :
9856 :
9857 : /* If interpolated values are requested ... */
9858 : /* ---------------------------------------- */
9859 0 : if (interpVal != NULL)
9860 : {
9861 : /* Get grid info */
9862 : /* ------------- */
9863 0 : status = GDgridinfo(gridID, &xdimsize, &ydimsize,
9864 : upleftpt, lowrightpt);
9865 :
9866 :
9867 : /* Get projection info */
9868 : /* ------------------- */
9869 0 : status = GDprojinfo(gridID, &projcode, &zonecode,
9870 : &spherecode, projparm);
9871 :
9872 :
9873 : /* Get explicit upleftpt & lowrightpt if defaults are used */
9874 : /* ------------------------------------------------------- */
9875 0 : status = GDgetdefaults(projcode, zonecode, projparm,
9876 : spherecode, upleftpt, lowrightpt);
9877 :
9878 :
9879 : /* Get pixel registration and origin info */
9880 : /* -------------------------------------- */
9881 0 : status = GDpixreginfo(gridID, &pixregcode);
9882 0 : status = GDorigininfo(gridID, &origincode);
9883 :
9884 :
9885 :
9886 : /* Loop through all interpolated points */
9887 : /* ------------------------------------ */
9888 0 : for (i = 0; i < nValues; i++)
9889 : {
9890 : /* Get row & column of point pixel */
9891 : /* ------------------------------- */
9892 0 : status = GDll2ij(projcode, zonecode, projparm, spherecode,
9893 : xdimsize, ydimsize, upleftpt, lowrightpt,
9894 : 1, &lonVal[i], &latVal[i],
9895 : pixRow, pixCol, &xVal, &yVal);
9896 :
9897 :
9898 : /* Get diff of interp. point from pixel location */
9899 : /* --------------------------------------------- */
9900 0 : if (pixregcode == HDFE_CENTER)
9901 : {
9902 0 : tNum = xVal - (pixCol[0] + 0.5);
9903 0 : uNum = yVal - (pixRow[0] + 0.5);
9904 : }
9905 0 : else if (origincode == HDFE_GD_UL)
9906 : {
9907 0 : tNum = xVal - pixCol[0];
9908 0 : uNum = yVal - pixRow[0];
9909 : }
9910 0 : else if (origincode == HDFE_GD_UR)
9911 : {
9912 0 : tNum = xVal - (pixCol[0] + 1);
9913 0 : uNum = yVal - pixRow[0];
9914 : }
9915 0 : else if (origincode == HDFE_GD_LL)
9916 : {
9917 0 : tNum = xVal - pixCol[0];
9918 0 : uNum = yVal - (pixRow[0] + 1);
9919 : }
9920 0 : else if (origincode == HDFE_GD_LR)
9921 : {
9922 0 : tNum = xVal - (pixCol[0] + 1);
9923 0 : uNum = yVal - (pixRow[0] + 1);
9924 : }
9925 :
9926 :
9927 : /* Get rows and columns of other nearest neighbor pixels */
9928 : /* ----------------------------------------------------- */
9929 0 : pixCol[1] = pixCol[0];
9930 0 : pixRow[3] = pixRow[0];
9931 :
9932 0 : if (tNum >= 0)
9933 : {
9934 0 : pixCol[2] = pixCol[0] + 1;
9935 0 : pixCol[3] = pixCol[0] + 1;
9936 : }
9937 :
9938 0 : if (tNum < 0)
9939 : {
9940 0 : pixCol[2] = pixCol[0] - 1;
9941 0 : pixCol[3] = pixCol[0] - 1;
9942 : }
9943 :
9944 0 : if (uNum >= 0)
9945 : {
9946 0 : pixRow[2] = pixRow[0] + 1;
9947 0 : pixRow[1] = pixRow[0] + 1;
9948 : }
9949 :
9950 0 : if (uNum < 0)
9951 : {
9952 0 : pixRow[2] = pixRow[0] - 1;
9953 0 : pixRow[1] = pixRow[0] - 1;
9954 : }
9955 :
9956 :
9957 : /* Get values of nearest neighbors */
9958 : /* -------------------------------- */
9959 0 : pixVal = (char *) malloc(4 * size);
9960 0 : if(pixVal == NULL)
9961 : {
9962 0 : HEpush(DFE_NOSPACE,"GDinterpolate", __FILE__, __LINE__);
9963 0 : free(dimlist);
9964 0 : return(-1);
9965 : }
9966 0 : dum = GDgetpixvalues(gridID, 4, pixRow, pixCol,
9967 : fieldname, pixVal);
9968 :
9969 :
9970 : /* Trap interpolation boundary out of range error */
9971 : /* ---------------------------------------------- */
9972 0 : if (dum == -1)
9973 : {
9974 0 : status = -1;
9975 0 : HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__);
9976 0 : HEreport("Interpolation boundary outside of grid.\n");
9977 : }
9978 : else
9979 : {
9980 :
9981 : /*
9982 : * Algorithm taken for Numerical Recipies in C, 2nd
9983 : * edition, Section 3.6
9984 : */
9985 :
9986 : /* Perform bilinear interpolation */
9987 : /* ------------------------------ */
9988 0 : tDen = pixCol[3] - pixCol[0];
9989 0 : uDen = pixRow[1] - pixRow[0];
9990 :
9991 0 : switch (ntype)
9992 : {
9993 : case DFNT_INT16:
9994 :
9995 :
9996 : /* Loop through all returned data values */
9997 : /* ------------------------------------- */
9998 0 : for (j = 0; j < nRetn; j++)
9999 : {
10000 : /* Copy 4 NN values into working array */
10001 : /* ----------------------------------- */
10002 0 : for (k = 0; k < 4; k++)
10003 : {
10004 0 : memcpy(&i16[k],
10005 : pixVal + j * numsize + k * size,
10006 : sizeof(int16));
10007 : }
10008 :
10009 : /* Compute interpolated value */
10010 : /* -------------------------- */
10011 0 : interpVal[i * nRetn + j] =
10012 : (1 - tNum / tDen) * (1 - uNum / uDen) *
10013 : i16[0] +
10014 : (tNum / tDen) * (1 - uNum / uDen) *
10015 : i16[3] +
10016 : (tNum / tDen) * (uNum / uDen) *
10017 : i16[2] +
10018 : (1 - tNum / tDen) * (uNum / uDen) *
10019 : i16[1];
10020 : }
10021 0 : break;
10022 :
10023 :
10024 : case DFNT_INT32:
10025 :
10026 0 : for (j = 0; j < nRetn; j++)
10027 : {
10028 0 : for (k = 0; k < 4; k++)
10029 : {
10030 0 : memcpy(&i32[k],
10031 : pixVal + j * numsize + k * size,
10032 : sizeof(int32));
10033 : }
10034 :
10035 0 : interpVal[i * nRetn + j] =
10036 : (1 - tNum / tDen) * (1 - uNum / uDen) *
10037 : i32[0] +
10038 : (tNum / tDen) * (1 - uNum / uDen) *
10039 : i32[3] +
10040 : (tNum / tDen) * (uNum / uDen) *
10041 : i32[2] +
10042 : (1 - tNum / tDen) * (uNum / uDen) *
10043 : i32[1];
10044 : }
10045 0 : break;
10046 :
10047 :
10048 : case DFNT_FLOAT32:
10049 :
10050 0 : for (j = 0; j < nRetn; j++)
10051 : {
10052 0 : for (k = 0; k < 4; k++)
10053 : {
10054 0 : memcpy(&f32[k],
10055 : pixVal + j * numsize + k * size,
10056 : sizeof(float32));
10057 : }
10058 :
10059 0 : interpVal[i * nRetn + j] =
10060 : (1 - tNum / tDen) * (1 - uNum / uDen) *
10061 : f32[0] +
10062 : (tNum / tDen) * (1 - uNum / uDen) *
10063 : f32[3] +
10064 : (tNum / tDen) * (uNum / uDen) *
10065 : f32[2] +
10066 : (1 - tNum / tDen) * (uNum / uDen) *
10067 : f32[1];
10068 : }
10069 0 : break;
10070 :
10071 :
10072 : case DFNT_FLOAT64:
10073 :
10074 0 : for (j = 0; j < nRetn; j++)
10075 : {
10076 0 : for (k = 0; k < 4; k++)
10077 : {
10078 0 : memcpy(&f64[k],
10079 : pixVal + j * numsize + k * size,
10080 : sizeof(float64));
10081 : }
10082 :
10083 0 : interpVal[i * nRetn + j] =
10084 : (1 - tNum / tDen) * (1 - uNum / uDen) *
10085 : f64[0] +
10086 : (tNum / tDen) * (1 - uNum / uDen) *
10087 : f64[3] +
10088 : (tNum / tDen) * (uNum / uDen) *
10089 : f64[2] +
10090 : (1 - tNum / tDen) * (uNum / uDen) *
10091 : f64[1];
10092 : }
10093 : break;
10094 : }
10095 : }
10096 0 : free(pixVal);
10097 : }
10098 : }
10099 : }
10100 : }
10101 :
10102 :
10103 : /* If successful return size of returned data in bytes */
10104 : /* --------------------------------------------------- */
10105 0 : if (status == 0)
10106 : {
10107 : /*always return size of float64 buffer */
10108 0 : free(dimlist);
10109 0 : return (nRetn * nValues * sizeof(float64));
10110 : }
10111 : else
10112 : {
10113 0 : free(dimlist);
10114 0 : return ((int32) status);
10115 : }
10116 :
10117 : }
10118 : /***********************************************
10119 : GDwrrdtile --
10120 : This function is the underlying function below GDwritetile and
10121 : GDreadtile.
10122 :
10123 :
10124 : Author--
10125 : Alexis Zubrow
10126 :
10127 : ********************************************************/
10128 :
10129 : static intn
10130 : GDwrrdtile(int32 gridID, char *fieldname, char *code, int32 start[],
10131 : VOIDP datbuf)
10132 76 : {
10133 : intn i; /* Loop index */
10134 76 : intn status = 0; /* routine return status variable */
10135 :
10136 : int32 fid; /* HDF-EOS file ID */
10137 : int32 sdInterfaceID; /* HDF SDS interface ID */
10138 : int32 sdid; /* SDS ID */
10139 :
10140 : int32 dum; /* Dummy variable */
10141 : int32 rankSDS; /* Rank of SDS/Field */
10142 :
10143 : int32 dims[8]; /* Field/SDS dimensions */
10144 : int32 tileFlags; /* flag to determine if field is tiled */
10145 : int32 numTileDims;/* number of tiles spanning a dimension */
10146 : HDF_CHUNK_DEF tileDef; /* union holding tiling info. */
10147 :
10148 :
10149 : /* Get gridID */
10150 76 : status = GDchkgdid(gridID, "GDwrrdtile", &fid, &sdInterfaceID, &dum);
10151 76 : if (status == 0)
10152 : {
10153 :
10154 : /* Get field info */
10155 76 : status = GDfieldinfo(gridID, fieldname, &rankSDS, dims, &dum, NULL);
10156 :
10157 76 : if (status == 0)
10158 : {
10159 :
10160 : /* Check whether fieldname is in SDS (multi-dim field) */
10161 : /* --------------------------------------------------- */
10162 76 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid,
10163 : &rankSDS, &dum, &dum, dims, &dum);
10164 :
10165 :
10166 :
10167 : /*
10168 : * Check for errors in parameters passed to GDwritetile or
10169 : * GDreadtile
10170 : */
10171 :
10172 : /* Check if untiled field */
10173 76 : status = SDgetchunkinfo(sdid, &tileDef, &tileFlags);
10174 76 : if (tileFlags == HDF_NONE)
10175 : {
10176 0 : HEpush(DFE_GENAPP, "GDwrrdtile", __FILE__, __LINE__);
10177 0 : HEreport("Field \"%s\" is not tiled.\n", fieldname);
10178 0 : status = -1;
10179 0 : return (status);
10180 :
10181 : }
10182 :
10183 : /*
10184 : * Check if rd/wr tilecoords are within the extent of the field
10185 : */
10186 228 : for (i = 0; i < rankSDS; i++)
10187 : {
10188 : /*
10189 : * Calculate the number of tiles which span a dimension of
10190 : * the field
10191 : */
10192 152 : numTileDims = dims[i] / tileDef.chunk_lengths[i];
10193 152 : if ((start[i] >= numTileDims) || (start[i] < 0))
10194 : {
10195 : /*
10196 : * ERROR INDICATING BEYOND EXTENT OF THAT DIMENSION OR
10197 : * NEGATIVE TILECOORDS
10198 : */
10199 0 : HEpush(DFE_GENAPP, "GDwrrdtile", __FILE__, __LINE__);
10200 0 : HEreport("Tilecoords for dimension \"%d\" ...\n", i);
10201 0 : HEreport("is beyond the extent of dimension length\n");
10202 0 : status = -1;
10203 :
10204 : }
10205 : }
10206 :
10207 76 : if (status == -1)
10208 : {
10209 0 : return (status);
10210 : }
10211 :
10212 :
10213 : /* Actually write/read to the field */
10214 :
10215 76 : if (strcmp(code, "w") == 0) /* write tile */
10216 : {
10217 0 : status = SDwritechunk(sdid, start, (VOIDP) datbuf);
10218 : }
10219 76 : else if (strcmp(code, "r") == 0) /* read tile */
10220 : {
10221 76 : status = SDreadchunk(sdid, start, (VOIDP) datbuf);
10222 : }
10223 :
10224 :
10225 : }
10226 :
10227 : /* Non-existent fieldname */
10228 : else
10229 : {
10230 0 : HEpush(DFE_GENAPP, "GDwrrdtile", __FILE__, __LINE__);
10231 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
10232 0 : status = -1;
10233 : }
10234 :
10235 : }
10236 :
10237 76 : return (status);
10238 : }
10239 :
10240 :
10241 : /***********************************************
10242 : GDtileinfo --
10243 : This function queries the field to determine if it is tiled. If it is
10244 : tile, one can retrieve some of the characteristics of the tiles.
10245 :
10246 : Author-- Alexis Zubrow
10247 :
10248 : ********************************************************/
10249 :
10250 :
10251 : intn
10252 : GDtileinfo(int32 gridID, char *fieldname, int32 * tilecode, int32 * tilerank,
10253 : int32 tiledims[])
10254 :
10255 14 : {
10256 : intn i; /* Loop index */
10257 14 : intn status = 0; /* routine return status variable */
10258 :
10259 : int32 fid; /* HDF-EOS file ID */
10260 : int32 sdInterfaceID; /* HDF SDS interface ID */
10261 : int32 sdid; /* SDS ID */
10262 :
10263 : int32 dum; /* Dummy variable */
10264 : int32 rankSDS; /* Rank of SDS/Field/tile */
10265 :
10266 : int32 dims[8]; /* Field/SDS dimensions */
10267 : int32 tileFlags; /* flag to determine if field is tiled */
10268 : HDF_CHUNK_DEF tileDef; /* union holding tiling info. */
10269 :
10270 :
10271 : /* Check if improper gridID */
10272 14 : status = GDchkgdid(gridID, "GDtileinfo", &fid, &sdInterfaceID, &dum);
10273 14 : if (status == 0)
10274 : {
10275 :
10276 : /* Get field info */
10277 14 : status = GDfieldinfo(gridID, fieldname, &rankSDS, dims, &dum, NULL);
10278 :
10279 14 : if (status == 0)
10280 : {
10281 :
10282 : /* Check whether fieldname is in SDS (multi-dim field) */
10283 : /* --------------------------------------------------- */
10284 14 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid,
10285 : &rankSDS, &dum, &dum, dims, &dum);
10286 :
10287 :
10288 :
10289 : /* Query field for tiling information */
10290 14 : status = SDgetchunkinfo(sdid, &tileDef, &tileFlags);
10291 :
10292 : /* If field is untiled, return untiled flag */
10293 14 : if (tileFlags == HDF_NONE)
10294 : {
10295 2 : *tilecode = HDFE_NOTILE;
10296 2 : return (status);
10297 : }
10298 :
10299 : /* IF field is tiled or tiled with compression */
10300 12 : else if ((tileFlags == HDF_CHUNK) ||
10301 : (tileFlags == (HDF_CHUNK | HDF_COMP)))
10302 : {
10303 12 : if (tilecode != NULL)
10304 : {
10305 12 : *tilecode = HDFE_TILE;
10306 : }
10307 12 : if (tilerank != NULL)
10308 : {
10309 12 : *tilerank = rankSDS;
10310 : }
10311 12 : if (tiledims != NULL)
10312 : {
10313 : /* Assign size of tile dimensions */
10314 18 : for (i = 0; i < rankSDS; i++)
10315 : {
10316 12 : tiledims[i] = tileDef.chunk_lengths[i];
10317 : }
10318 : }
10319 : }
10320 : }
10321 :
10322 : /* Non-existent fieldname */
10323 : else
10324 : {
10325 0 : HEpush(DFE_GENAPP, "GDtileinfo", __FILE__, __LINE__);
10326 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
10327 0 : status = -1;
10328 : }
10329 :
10330 : }
10331 12 : return (status);
10332 : }
10333 : /***********************************************
10334 : GDwritetile --
10335 : This function writes one tile to a particular field.
10336 :
10337 :
10338 : Author--
10339 : Alexis Zubrow
10340 :
10341 : ********************************************************/
10342 :
10343 : intn
10344 : GDwritetile(int32 gridID, char *fieldname, int32 tilecoords[],
10345 : VOIDP tileData)
10346 0 : {
10347 0 : char code[] = "w"; /* write tile code */
10348 0 : intn status = 0; /* routine return status variable */
10349 :
10350 0 : status = GDwrrdtile(gridID, fieldname, code, tilecoords, tileData);
10351 :
10352 0 : return (status);
10353 : }
10354 : /***********************************************
10355 : GDreadtile --
10356 : This function reads one tile from a particular field.
10357 :
10358 :
10359 : Author--
10360 : Alexis Zubrow
10361 :
10362 : ********************************************************/
10363 :
10364 : intn
10365 : GDreadtile(int32 gridID, char *fieldname, int32 tilecoords[],
10366 : VOIDP tileData)
10367 76 : {
10368 76 : char code[] = "r"; /* read tile code */
10369 76 : intn status = 0; /* routine return status variable */
10370 :
10371 76 : status = GDwrrdtile(gridID, fieldname, code, tilecoords, tileData);
10372 :
10373 76 : return (status);
10374 : }
10375 : /***********************************************
10376 : GDsettilecache --
10377 : This function sets the cache size for a tiled field.
10378 :
10379 :
10380 : Author--
10381 : Alexis Zubrow
10382 :
10383 : ********************************************************/
10384 :
10385 : intn
10386 : GDsettilecache(int32 gridID, char *fieldname, int32 maxcache, int32 cachecode)
10387 0 : {
10388 :
10389 0 : intn status = 0; /* routine return status variable */
10390 :
10391 : int32 fid; /* HDF-EOS file ID */
10392 : int32 sdInterfaceID; /* HDF SDS interface ID */
10393 : int32 sdid; /* SDS ID */
10394 :
10395 : int32 dum; /* Dummy variable */
10396 :
10397 : int32 dims[8]; /* Field/SDS dimensions */
10398 :
10399 :
10400 : /* Check gridID */
10401 0 : status = GDchkgdid(gridID, "GDwrrdtile", &fid, &sdInterfaceID, &dum);
10402 0 : if (status == 0)
10403 : {
10404 :
10405 : /* Get field info */
10406 0 : status = GDfieldinfo(gridID, fieldname, &dum, dims, &dum, NULL);
10407 :
10408 0 : if (status == 0)
10409 : {
10410 :
10411 : /* Check whether fieldname is in SDS (multi-dim field) */
10412 : /* --------------------------------------------------- */
10413 0 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid,
10414 : &dum, &dum, &dum, dims, &dum);
10415 :
10416 :
10417 : /* Check if maxcache is less than or equal to zero */
10418 0 : if (maxcache <= 0)
10419 : {
10420 0 : HEpush(DFE_GENAPP, "GDsettilecache", __FILE__, __LINE__);
10421 0 : HEreport("Improper maxcache \"%d\"... \n", maxcache);
10422 0 : HEreport("maxcache must be greater than zero.\n");
10423 0 : status = -1;
10424 0 : return (status);
10425 : }
10426 :
10427 :
10428 : /* Set the number of tiles to cache */
10429 : /* Presently, the only cache flag allowed is 0 */
10430 0 : status = SDsetchunkcache(sdid, maxcache, 0);
10431 :
10432 :
10433 : }
10434 :
10435 : /* Non-existent fieldname */
10436 : else
10437 : {
10438 0 : HEpush(DFE_GENAPP, "GDwrrdtile", __FILE__, __LINE__);
10439 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
10440 0 : status = -1;
10441 : }
10442 :
10443 : }
10444 :
10445 0 : return (status);
10446 : }
10447 :
10448 : /*----------------------------------------------------------------------------|
10449 : | BEGIN_PROLOG |
10450 : | |
10451 : | FUNCTION: GDsettilecomp |
10452 : | |
10453 : | DESCRIPTION: Sets the tiling/compression parameters for the specified |
10454 : | field. This can be called after GDsetfillvalue and assumes |
10455 : | that the field was defined with no compression/tiling set |
10456 : | by GDdeftile or GDdefcomp. |
10457 : | |
10458 : | This function replaces the following sequence: |
10459 : | GDdefcomp |
10460 : | GDdeftile |
10461 : | GDdeffield |
10462 : | GDsetfillvalue |
10463 : | with: |
10464 : | GDdeffield |
10465 : | GDsetfillvalue |
10466 : | GDsettilecomp |
10467 : | so that fill values will work correctly. |
10468 : | |
10469 : | Return Value Type Units Description |
10470 : | ============ ====== ========= ===================================== |
10471 : | status intn return status (0) SUCCEED, (-1) FAIL |
10472 : | |
10473 : | INPUTS: |
10474 : | gridID int32 grid structure ID |
10475 : | fieldname char field name |
10476 : | tilerank int32 number of tiling dimensions |
10477 : | tiledims int32 tiling dimensions |
10478 : | compcode int32 compression code |
10479 : | compparm intn compression parameters |
10480 : | |
10481 : | OUTPUTS: |
10482 : | None |
10483 : | |
10484 : | NOTES: |
10485 : | |
10486 : | |
10487 : | Date Programmer Description |
10488 : | ====== ============ ================================================= |
10489 : | Jun 98 MISR Used GDsetfillvalue as a template and copied |
10490 : | tiling/comp portions of GDdeffield.(NCR15866). |
10491 : | |
10492 : | END_PROLOG |
10493 : -----------------------------------------------------------------------------*/
10494 : intn
10495 : GDsettilecomp(int32 gridID, char *fieldname, int32 tilerank, int32*
10496 : tiledims, int32 compcode, intn* compparm)
10497 0 : {
10498 : intn status; /* routine return status variable */
10499 :
10500 : int32 fid; /* HDF-EOS file ID */
10501 : int32 sdInterfaceID; /* HDF SDS interface ID */
10502 : int32 gdVgrpID; /* Grid root Vgroup ID */
10503 : int i; /* Looping variable. */
10504 : int32 sdid; /* SDS id */
10505 : int32 nt; /* Number type */
10506 : int32 dims[8]; /* Dimensions array */
10507 : int32 dum; /* Dummy variable */
10508 : int32 solo; /* "Solo" (non-merged) field flag */
10509 : comp_info c_info; /* Compression parameter structure */
10510 : HDF_CHUNK_DEF chunkDef; /* Tiling structure */
10511 : int32 chunkFlag; /* Chunking (Tiling) flag */
10512 :
10513 : /* Check for valid grid ID and get SDS interface ID */
10514 0 : status = GDchkgdid(gridID, "GDsetfillvalue",
10515 : &fid, &sdInterfaceID, &gdVgrpID);
10516 :
10517 0 : if (status == 0)
10518 : {
10519 : /* Get field info */
10520 0 : status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL);
10521 :
10522 0 : if (status == 0)
10523 : {
10524 : /* Get SDS ID and solo flag */
10525 0 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname,
10526 : &sdid, &dum, &dum, &dum,
10527 : dims, &solo);
10528 0 : if (status !=0) {
10529 0 : HEpush(DFE_GENAPP, "GDsettilecomp", __FILE__, __LINE__);
10530 0 : HEreport("GDSDfldsrch failed\n", fieldname);
10531 0 : return FAIL;
10532 : }
10533 : /* Tiling with Compression */
10534 : /* ----------------------- */
10535 :
10536 :
10537 : /* Setup Compression */
10538 : /* ----------------- */
10539 0 : if (compcode == HDFE_COMP_NBIT)
10540 : {
10541 0 : c_info.nbit.nt = nt;
10542 0 : c_info.nbit.sign_ext = compparm[0];
10543 0 : c_info.nbit.fill_one = compparm[1];
10544 0 : c_info.nbit.start_bit = compparm[2];
10545 0 : c_info.nbit.bit_len = compparm[3];
10546 : }
10547 0 : else if (compcode == HDFE_COMP_SKPHUFF)
10548 : {
10549 0 : c_info.skphuff.skp_size = (intn) DFKNTsize(nt);
10550 : }
10551 0 : else if (compcode == HDFE_COMP_DEFLATE)
10552 : {
10553 0 : c_info.deflate.level = compparm[0];
10554 : }
10555 :
10556 : /* Setup chunk lengths */
10557 : /* ------------------- */
10558 0 : for (i = 0; i < tilerank; i++)
10559 : {
10560 0 : chunkDef.comp.chunk_lengths[i] = tiledims[i];
10561 : }
10562 :
10563 : /* Setup chunk flag & chunk compression type */
10564 : /* ----------------------------------------- */
10565 0 : chunkFlag = HDF_CHUNK | HDF_COMP;
10566 0 : chunkDef.comp.comp_type = compcode;
10567 :
10568 : /* Setup chunk compression parameters */
10569 : /* ---------------------------------- */
10570 0 : if (compcode == HDFE_COMP_SKPHUFF)
10571 : {
10572 0 : chunkDef.comp.cinfo.skphuff.skp_size =
10573 : c_info.skphuff.skp_size;
10574 : }
10575 0 : else if (compcode == HDFE_COMP_DEFLATE)
10576 : {
10577 0 : chunkDef.comp.cinfo.deflate.level =
10578 : c_info.deflate.level;
10579 : }
10580 : /* Call SDsetchunk routine */
10581 : /* ----------------------- */
10582 0 : status = SDsetchunk(sdid, chunkDef, chunkFlag);
10583 0 : if (status ==FAIL) {
10584 0 : HEpush(DFE_GENAPP, "GDsettilecomp", __FILE__, __LINE__);
10585 0 : HEreport("Fieldname \"%s\" does not exist.\n",
10586 : fieldname);
10587 0 : return status;
10588 : }
10589 : }
10590 : else
10591 : {
10592 0 : HEpush(DFE_GENAPP, "GDsettilecomp", __FILE__, __LINE__);
10593 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
10594 : }
10595 : }
10596 0 : return (status);
10597 : }
10598 :
10599 : /*----------------------------------------------------------------------------|
10600 : | BEGIN_PROLOG |
10601 : | |
10602 : | FUNCTION: GDll2mm_cea |
10603 : | |
10604 : | DESCRIPTION: |
10605 : | |
10606 : | |
10607 : | Return Value Type Units Description |
10608 : | ============ ====== ========= ===================================== |
10609 : | status intn return status (0) SUCCEED, (-1) FAIL |
10610 : | |
10611 : | INPUTS: |
10612 : | projcode int32 GCTP projection code |
10613 : | zonecode int32 UTM zone code |
10614 : | projparm float64 Projection parameters |
10615 : | spherecode int32 GCTP spheriod code |
10616 : | xdimsize int32 xdimsize from GDcreate |
10617 : | ydimsize int32 ydimsize from GDcreate |
10618 : | upleftpt float64 upper left corner coordinates (DMS) |
10619 : | lowrightpt float64 lower right corner coordinates (DMS) |
10620 : | longitude float64 longitude array (DMS) |
10621 : | latitude float64 latitude array (DMS) |
10622 : | npnts int32 number of lon-lat points |
10623 : | |
10624 : | OUTPUTS: |
10625 : | x float64 X value array |
10626 : | y float64 Y value array |
10627 : | scaleX float64 X grid size |
10628 : | scaley float64 Y grid size |
10629 : | |
10630 : | NOTES: |
10631 : | |
10632 : | |
10633 : | Date Programmer Description |
10634 : | ====== ============ ================================================= |
10635 : | Oct 02 Abe Taaheri Added support for EASE grid |
10636 : | |
10637 : | END_PROLOG |
10638 : -----------------------------------------------------------------------------*/
10639 : static intn GDll2mm_cea(int32 projcode,int32 zonecode, int32 spherecode,
10640 : float64 projparm[],
10641 : int32 xdimsize, int32 ydimsize,
10642 : float64 upleftpt[], float64 lowrightpt[], int32 npnts,
10643 : float64 lon[],float64 lat[],
10644 : float64 x[],float64 y[], float64 *scaleX,float64 *scaleY)
10645 0 : {
10646 0 : intn status = 0; /* routine return status variable */
10647 0 : int32 errorcode = 0; /* GCTP error code */
10648 : float64 xMtr0, xMtr1, yMtr0, yMtr1;
10649 : float64 lonrad0; /* Longitude in radians of upleft point */
10650 : float64 latrad0; /* Latitude in radians of upleft point */
10651 : float64 lonrad; /* Longitude in radians of point */
10652 : float64 latrad; /* Latitude in radians of point */
10653 : int32(*for_trans[100]) (); /* GCTP function pointer */
10654 :
10655 0 : if(npnts <= 0)
10656 : {
10657 0 : HEpush(DFE_GENAPP, " GDll2mm_cea", __FILE__, __LINE__);
10658 0 : HEreport("Improper npnts value\"%d\"... \n", npnts);
10659 0 : HEreport("npnts must be greater than zero.\n");
10660 0 : status = -1;
10661 0 : return (status);
10662 : }
10663 0 : if ( projcode == GCTP_BCEA)
10664 : {
10665 0 : for_init(projcode, zonecode, projparm, spherecode, NULL, NULL,
10666 : &errorcode, for_trans);
10667 : /* Convert upleft and lowright X coords from DMS to radians */
10668 : /* -------------------------------------------------------- */
10669 :
10670 0 : lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD);
10671 0 : lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD);
10672 :
10673 : /* Convert upleft and lowright Y coords from DMS to radians */
10674 : /* -------------------------------------------------------- */
10675 0 : latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD);
10676 0 : latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD);
10677 :
10678 : /* Convert from lon/lat to meters(or whatever unit is, i.e unit
10679 : of r_major and r_minor) using GCTP */
10680 : /* ----------------------------------------- */
10681 0 : errorcode = for_trans[projcode] (lonrad0, latrad0, &xMtr0, &yMtr0);
10682 0 : x[0] = xMtr0;
10683 0 : y[0] = yMtr0;
10684 :
10685 : /* Report error if any */
10686 : /* ------------------- */
10687 0 : if (errorcode != 0)
10688 : {
10689 0 : status = -1;
10690 0 : HEpush(DFE_GENAPP, "GDll2mm_cea", __FILE__, __LINE__);
10691 0 : HEreport("GCTP Error: %d\n", errorcode);
10692 0 : return (status);
10693 : }
10694 :
10695 : /* Convert from lon/lat to meters(or whatever unit is, i.e unit
10696 : of r_major and r_minor) using GCTP */
10697 : /* ----------------------------------------- */
10698 0 : errorcode = for_trans[projcode] (lonrad, latrad, &xMtr1, &yMtr1);
10699 0 : x[1] = xMtr1;
10700 0 : y[1] = yMtr1;
10701 :
10702 : /* Report error if any */
10703 : /* ------------------- */
10704 0 : if (errorcode != 0)
10705 : {
10706 0 : status = -1;
10707 0 : HEpush(DFE_GENAPP, "GDll2mm_cea", __FILE__, __LINE__);
10708 0 : HEreport("GCTP Error: %d\n", errorcode);
10709 0 : return (status);
10710 : }
10711 :
10712 : /* Compute x scale factor */
10713 : /* ---------------------- */
10714 0 : *scaleX = (xMtr1 - xMtr0) / xdimsize;
10715 :
10716 : /* Compute y scale factor */
10717 : /* ---------------------- */
10718 0 : *scaleY = (yMtr1 - yMtr0) / ydimsize;
10719 : }
10720 : else
10721 : {
10722 0 : status = -1;
10723 0 : HEpush(DFE_GENAPP, "GDll2mm_cea", __FILE__, __LINE__);
10724 0 : HEreport("Wrong projection code; this function is only for EASE grid");
10725 0 : return (status);
10726 : }
10727 0 : return (0);
10728 : }
10729 :
10730 :
10731 : /*----------------------------------------------------------------------------|
10732 : | BEGIN_PROLOG |
10733 : | |
10734 : | FUNCTION: GDmm2ll_cea |
10735 : | |
10736 : | DESCRIPTION: |
10737 : | |
10738 : | |
10739 : | Return Value Type Units Description |
10740 : | ============ ====== ========= ===================================== |
10741 : | status intn return status (0) SUCCEED, (-1) FAIL |
10742 : | |
10743 : | INPUTS: |
10744 : | projcode int32 GCTP projection code |
10745 : | zonecode int32 UTM zone code |
10746 : | projparm float64 Projection parameters |
10747 : | spherecode int32 GCTP spheriod code |
10748 : | xdimsize int32 xdimsize from GDcreate |
10749 : | ydimsize int32 ydimsize from GDcreate |
10750 : | upleftpt float64 upper left corner coordinates (DMS) |
10751 : | lowrightpt float64 lower right corner coordinates (DMS) |
10752 : | x float64 X value array |
10753 : | y float64 Y value array |
10754 : | npnts int32 number of x-y points |
10755 : | |
10756 : | OUTPUTS: |
10757 : | longitude float64 longitude array (DMS) |
10758 : | latitude float64 latitude array (DMS) |
10759 : | |
10760 : | NOTES: |
10761 : | |
10762 : | |
10763 : | Date Programmer Description |
10764 : | ====== ============ ================================================= |
10765 : | Oct 02 Abe Taaheri Added support for EASE grid |
10766 : | |
10767 : | END_PROLOG |
10768 : -----------------------------------------------------------------------------*/
10769 : static intn GDmm2ll_cea(int32 projcode,int32 zonecode, int32 spherecode,
10770 : float64 projparm[],
10771 : int32 xdimsize, int32 ydimsize,
10772 : float64 upleftpt[], float64 lowrightpt[], int32 npnts,
10773 : float64 x[], float64 y[],
10774 : float64 longitude[], float64 latitude[])
10775 0 : {
10776 0 : intn status = 0; /* routine return status variable */
10777 0 : int32 errorcode = 0; /* GCTP error code */
10778 : int32(*inv_trans[100]) (); /* GCTP function pointer */
10779 : int32 i;
10780 :
10781 0 : if(npnts <= 0)
10782 : {
10783 0 : HEpush(DFE_GENAPP, " GDmm2ll_cea", __FILE__, __LINE__);
10784 0 : HEreport("Improper npnts value\"%d\"... \n", npnts);
10785 0 : HEreport("npnts must be greater than zero.\n");
10786 0 : status = -1;
10787 0 : return (status);
10788 : }
10789 0 : if ( projcode == GCTP_BCEA)
10790 : {
10791 0 : inv_init(projcode, zonecode, projparm, spherecode, NULL, NULL,
10792 : &errorcode, inv_trans);
10793 :
10794 : /* Convert from meters(or whatever unit is, i.e unit
10795 : of r_major and r_minor) to lat/lon using GCTP */
10796 : /* ----------------------------------------- */
10797 0 : for(i=0; i<npnts; i++)
10798 : {
10799 0 : errorcode =
10800 : inv_trans[projcode] (x[i], y[i],&longitude[i], &latitude[i]);
10801 : /* Report error if any */
10802 : /* ------------------- */
10803 0 : if (errorcode != 0)
10804 : {
10805 0 : status = -1;
10806 0 : HEpush(DFE_GENAPP, "GDmm2ll_cea", __FILE__, __LINE__);
10807 0 : HEreport("GCTP Error: %d\n", errorcode);
10808 0 : return (status);
10809 : }
10810 0 : longitude[i] = EHconvAng(longitude[i], HDFE_RAD_DMS);
10811 0 : latitude[i] = EHconvAng(latitude[i], HDFE_RAD_DMS);
10812 : }
10813 : }
10814 : else
10815 : {
10816 : /* Wrong projection code; this function is only for EASE grid */
10817 : }
10818 0 : return(status);
10819 : }
10820 :
10821 : /*----------------------------------------------------------------------------|
10822 : | BEGIN_PROLOG |
10823 : | |
10824 : | FUNCTION: GDsdid |
10825 : | |
10826 : | DESCRIPTION: Returns SD element ID for grid field |
10827 : | |
10828 : | |
10829 : | Return Value Type Units Description |
10830 : | ============ ====== ========= ===================================== |
10831 : | status intn return status (0) SUCCEED, (-1) FAIL |
10832 : | |
10833 : | INPUTS: |
10834 : | gridID int32 grid structure ID |
10835 : | fieldname const char field name |
10836 : | |
10837 : | |
10838 : | OUTPUTS: |
10839 : | sdid int32 SD element ID |
10840 : | |
10841 : | NOTES: |
10842 : | |
10843 : | |
10844 : | Date Programmer Description |
10845 : | ====== ============ ================================================= |
10846 : | Oct 07 Andrey Kiselev Original Programmer |
10847 : | |
10848 : | END_PROLOG |
10849 : -----------------------------------------------------------------------------*/
10850 : intn
10851 : GDsdid(int32 gridID, const char *fieldname, int32 *sdid)
10852 8 : {
10853 : intn status; /* routine return status variable */
10854 : int32 fid; /* HDF-EOS file ID */
10855 : int32 sdInterfaceID; /* HDF SDS interface ID */
10856 : int32 dum; /* Dummy variable */
10857 : int32 dims[H4_MAX_VAR_DIMS]; /* Field/SDS dimensions */
10858 :
10859 8 : status = GDchkgdid(gridID, "GDsdid", &fid, &sdInterfaceID, &dum);
10860 8 : if (status != -1)
10861 : {
10862 8 : status = GDSDfldsrch(gridID, sdInterfaceID, fieldname,
10863 : sdid, &dum, &dum, &dum, dims, &dum);
10864 : }
10865 :
10866 8 : return (status);
10867 : }
10868 :
|