1 : /*****************************************************************************
2 : * $Id: SWapi.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 :
30 : June 05, 2003 Abe Taaheri / Bruce Beaumont
31 :
32 : Changed MAXNREGIONS to 1024 to support MOPITT data
33 : Supplied cast for compcode in call to
34 : SDsetcompress to avoid compiler error
35 : Removed declaration for unused variable rstatus
36 : in SWwrrdfield
37 : Removed initialization code for unused variables
38 : in SWwrrdfield
39 : Removed declaration for unused variable tmpVal
40 : in SWdefboxregion
41 : Added code in SWdefboxregion to check for index k
42 : exceeding NSWATHREGN to avoid overwriting
43 : memory
44 : Removed declaration for unused variable retchar
45 : in SWregionindex
46 : Removed initialization code for unused variables
47 : in SWregionindex
48 : Removed declarations for unused variables tstatus,
49 : nfields, nflgs, and swathname in SWextractregion
50 : Removed initialization code for unused variables
51 : in SWextractregion
52 : Removed declaration for unused variable
53 : land_status in SWscan2longlat
54 : Removed initialization code for unused variables
55 : in SWscan2longlat
56 : Added clear (0) of timeflag in SWextractperiod if
57 : return status from SWextractregion is non-zero
58 : Removed declarations for unused variables tstatus,
59 : scandim, ndfields, ndflds, and swathname in
60 : SWregioninfo
61 : Removed initialization code for unused variables
62 : in SWregioninfo
63 : Added clear (0) of timeflag in SWperiodinfo if
64 : return status from SWregioninfo is non-zero
65 : Removed declarations for unused variables size,
66 : nfields, nflds, nswath, idxsz, cornerlon, and
67 : cornerlat in SWdefscanregion
68 : Removed initialization code for unused variables
69 : in SWdefscanregion
70 : Removed declarations for unused variables dims2,
71 : rank, nt, swathname, dimlist, and buffer in
72 : SWupdateidxmap
73 : Removed declaration for unused variable statmeta
74 : in SWgeomapinfo
75 : ******************************************************************************/
76 :
77 : #include "mfhdf.h"
78 : #include "hcomp.h"
79 : #include "HdfEosDef.h"
80 : #include <math.h>
81 :
82 : #include "hdf4compat.h"
83 :
84 : #define SWIDOFFSET 1048576
85 :
86 :
87 : static int32 SWX1dcomb[512*3];
88 : static int32 SWXSDcomb[512*5];
89 : static char SWXSDname[HDFE_NAMBUFSIZE];
90 : static char SWXSDdims[HDFE_DIMBUFSIZE];
91 :
92 : /* This flag was added to allow the Time field to have different Dimensions
93 : ** than Longitude and Latitude and still be used for subsetting
94 : ** 23 June,1997 DaW
95 : */
96 : static intn timeflag = 0;
97 :
98 :
99 : /* Added for routine that converts scanline to Lat/long
100 : ** for floating scene subsetting
101 : ** Jul 1999 DaW
102 : */
103 : #define PI 3.141592653589793238
104 : #define RADOE 6371.0 /* Radius of Earth in Km */
105 :
106 : #define NSWATH 200
107 : /* Swath Structure External Arrays */
108 : struct swathStructure
109 : {
110 : int32 active;
111 : int32 IDTable;
112 : int32 VIDTable[3];
113 : int32 fid;
114 : int32 nSDS;
115 : int32 *sdsID;
116 : int32 compcode;
117 : intn compparm[5];
118 : int32 tilecode;
119 : int32 tilerank;
120 : int32 tiledims[8];
121 : };
122 : static struct swathStructure SWXSwath[NSWATH];
123 :
124 :
125 :
126 : #define NSWATHREGN 256
127 : #define MAXNREGIONS 1024
128 : struct swathRegion
129 : {
130 : int32 fid;
131 : int32 swathID;
132 : int32 nRegions;
133 : int32 StartRegion[MAXNREGIONS];
134 : int32 StopRegion[MAXNREGIONS];
135 : int32 StartVertical[8];
136 : int32 StopVertical[8];
137 : int32 StartScan[8];
138 : int32 StopScan[8];
139 : char *DimNamePtr[8];
140 : intn band8flag;
141 : intn scanflag;
142 : };
143 : static struct swathRegion *SWXRegion[NSWATHREGN];
144 :
145 : /* define a macro for the string size of the utility strings. The value
146 : of 80 in previous version of this code was resulting in core dump (Array
147 : Bounds Write and Array Bounds Read problem in SWfinfo function and the
148 : functions called from there) for 7-8 dimensional fields where the
149 : string length for "DimList" can exceed 80 characters, including " and
150 : commas in the string. The length now is 512 which seems to be more
151 : than enough to avoid the problem mentioned above. */
152 :
153 : #define UTLSTR_MAX_SIZE 512
154 :
155 : /* Swath Prototypes (internal routines) */
156 : static intn SWchkswid(int32, char *, int32 *, int32 *, int32 *);
157 : static int32 SWfinfo(int32, const char *, const char *, int32 *,
158 : int32 [], int32 *, char *);
159 : static intn SWdefinefield(int32, char *, char *, char *, int32, int32);
160 : static intn SWwrrdattr(int32, char *, int32, int32, char *, VOIDP);
161 : static intn SW1dfldsrch(int32, int32, const char *, const char *, int32 *,
162 : int32 *, int32 *);
163 : static intn SWSDfldsrch(int32, int32, const char *, int32 *, int32 *,
164 : int32 *, int32 *, int32 [], int32 *);
165 : static intn SWwrrdfield(int32, const char *, const char *,
166 : int32 [], int32 [], int32 [], VOIDP);
167 : static int32 SWinqfields(int32, char *, char *, int32 [], int32 []);
168 : static intn SWscan2longlat(int32, char *, VOIDP, int32 [], int32 [],
169 : int32 *, int32, int32);
170 :
171 :
172 : /*----------------------------------------------------------------------------|
173 : | BEGIN_PROLOG |
174 : | |
175 : | FUNCTION: SWopen |
176 : | |
177 : | DESCRIPTION: |
178 : | |
179 : | |
180 : | Return Value Type Units Description |
181 : | ============ ====== ========= ===================================== |
182 : | fid int32 HDF-EOS file ID |
183 : | |
184 : | INPUTS: |
185 : | filename char Filename |
186 : | access intn HDF access code |
187 : | |
188 : | |
189 : | OUTPUTS: |
190 : | None |
191 : | |
192 : | NOTES: |
193 : | |
194 : | |
195 : | Date Programmer Description |
196 : | ====== ============ ================================================= |
197 : | Jun 96 Joel Gales Original Programmer |
198 : | |
199 : | END_PROLOG |
200 : -----------------------------------------------------------------------------*/
201 : int32
202 0 : SWopen(char *filename, intn access)
203 :
204 : {
205 : int32 fid /* HDF-EOS file ID */ ;
206 :
207 : /* Call EHopen to perform file access */
208 : /* ---------------------------------- */
209 0 : fid = EHopen(filename, access);
210 :
211 0 : return (fid);
212 : }
213 :
214 : /*----------------------------------------------------------------------------|
215 : | BEGIN_PROLOG |
216 : | |
217 : | FUNCTION: SWcreate |
218 : | |
219 : | DESCRIPTION: Creates a new swath structure and returns swath ID |
220 : | |
221 : | |
222 : | Return Value Type Units Description |
223 : | ============ ====== ========= ===================================== |
224 : | swathID int32 Swath structure ID |
225 : | |
226 : | INPUTS: |
227 : | fid int32 File ID |
228 : | swathname char Swath structure name |
229 : | |
230 : | OUTPUTS: |
231 : | None |
232 : | |
233 : | NOTES: |
234 : | |
235 : | |
236 : | Date Programmer Description |
237 : | ====== ============ ================================================= |
238 : | Jun 96 Joel Gales Original Programmer |
239 : | Aug 96 Joel Gales Make metadata ODL compliant |
240 : | Sep 96 Joel Gales Check swath name for length |
241 : | Mar 97 Joel Gales Enlarge utlbuf to 512 |
242 : | |
243 : | END_PROLOG |
244 : -----------------------------------------------------------------------------*/
245 : int32
246 0 : SWcreate(int32 fid, char *swathname)
247 : {
248 : intn i; /* Loop index */
249 0 : intn nswathopen = 0; /* # of swath structures open */
250 0 : intn status = 0; /* routine return status variable */
251 :
252 : uint8 access; /* Read/Write file access code */
253 :
254 : int32 HDFfid; /* HDF file id */
255 : int32 vgRef; /* Vgroup reference number */
256 : int32 vgid[4]; /* Vgroup ID array */
257 0 : int32 swathID = -1; /* HDF-EOS swath ID */
258 :
259 : int32 sdInterfaceID; /* HDF SDS interface ID */
260 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
261 0 : int32 nSwath = 0; /* Swath counter */
262 :
263 : char name[80]; /* Vgroup name */
264 : char class[80]; /* Vgroup class */
265 : char errbuf[256];/* Buffer for error message */
266 : char utlbuf[512];/* Utility buffer */
267 : char utlbuf2[32];/* Utility buffer 2 */
268 :
269 : /*
270 : * Check HDF-EOS file ID, get back HDF file ID, SD interface ID and
271 : * access code
272 : */
273 0 : status = EHchkfid(fid, swathname, &HDFfid, &sdInterfaceID, &access);
274 :
275 :
276 : /* Check swathname for length */
277 : /* -------------------------- */
278 0 : if ((intn) strlen(swathname) > VGNAMELENMAX)
279 : {
280 0 : status = -1;
281 0 : HEpush(DFE_GENAPP, "SWcreate", __FILE__, __LINE__);
282 0 : HEreport("Swathname \"%s\" must be less than %d characters.\n",
283 : swathname, VGNAMELENMAX);
284 : }
285 :
286 :
287 0 : if (status == 0)
288 : {
289 :
290 : /* Determine number of swaths currently opened */
291 : /* ------------------------------------------- */
292 0 : for (i = 0; i < NSWATH; i++)
293 : {
294 0 : nswathopen += SWXSwath[i].active;
295 : }
296 :
297 :
298 : /* Setup file interface */
299 : /* -------------------- */
300 0 : if (nswathopen < NSWATH)
301 : {
302 :
303 : /* Check that swath has not been previously opened */
304 : /* ----------------------------------------------- */
305 0 : vgRef = -1;
306 :
307 : while (1)
308 : {
309 0 : vgRef = Vgetid(HDFfid, vgRef);
310 :
311 : /* If no more Vgroups then exist while loop */
312 : /* ---------------------------------------- */
313 0 : if (vgRef == -1)
314 : {
315 0 : break;
316 : }
317 :
318 : /* Get name and class of Vgroup */
319 : /* ---------------------------- */
320 0 : vgid[0] = Vattach(HDFfid, vgRef, "r");
321 0 : Vgetname(vgid[0], name);
322 0 : Vgetclass(vgid[0], class);
323 0 : Vdetach(vgid[0]);
324 :
325 : /* If SWATH then increment # swath counter */
326 : /* --------------------------------------- */
327 0 : if (strcmp(class, "SWATH") == 0)
328 : {
329 0 : nSwath++;
330 : }
331 :
332 : /* If swath already exist, return error */
333 : /* ------------------------------------ */
334 0 : if (strcmp(name, swathname) == 0 &&
335 0 : strcmp(class, "SWATH") == 0)
336 : {
337 0 : status = -1;
338 0 : HEpush(DFE_GENAPP, "SWcreate", __FILE__, __LINE__);
339 0 : HEreport("\"%s\" already exists.\n", swathname);
340 0 : break;
341 : }
342 0 : }
343 :
344 :
345 0 : if (status == 0)
346 : {
347 :
348 : /* Create Root Vgroup for Swath */
349 : /* ---------------------------- */
350 0 : vgid[0] = Vattach(HDFfid, -1, "w");
351 :
352 :
353 : /* Set Name and Class (SWATH) */
354 : /* -------------------------- */
355 0 : Vsetname(vgid[0], swathname);
356 0 : Vsetclass(vgid[0], "SWATH");
357 :
358 :
359 :
360 : /* Create Geolocation Fields Vgroup */
361 : /* -------------------------------- */
362 0 : vgid[1] = Vattach(HDFfid, -1, "w");
363 0 : Vsetname(vgid[1], "Geolocation Fields");
364 0 : Vsetclass(vgid[1], "SWATH Vgroup");
365 0 : Vinsert(vgid[0], vgid[1]);
366 :
367 :
368 :
369 : /* Create Data Fields Vgroup */
370 : /* ------------------------- */
371 0 : vgid[2] = Vattach(HDFfid, -1, "w");
372 0 : Vsetname(vgid[2], "Data Fields");
373 0 : Vsetclass(vgid[2], "SWATH Vgroup");
374 0 : Vinsert(vgid[0], vgid[2]);
375 :
376 :
377 :
378 : /* Create Attributes Vgroup */
379 : /* ------------------------ */
380 0 : vgid[3] = Vattach(HDFfid, -1, "w");
381 0 : Vsetname(vgid[3], "Swath Attributes");
382 0 : Vsetclass(vgid[3], "SWATH Vgroup");
383 0 : Vinsert(vgid[0], vgid[3]);
384 :
385 :
386 :
387 : /* Establish Swath in Structural MetaData Block */
388 : /* -------------------------------------------- */
389 0 : sprintf(utlbuf, "%s%ld%s%s%s",
390 : "\tGROUP=SWATH_", (long)nSwath + 1,
391 : "\n\t\tSwathName=\"", swathname, "\"\n");
392 :
393 0 : strcat(utlbuf, "\t\tGROUP=Dimension\n");
394 0 : strcat(utlbuf, "\t\tEND_GROUP=Dimension\n");
395 0 : strcat(utlbuf, "\t\tGROUP=DimensionMap\n");
396 0 : strcat(utlbuf, "\t\tEND_GROUP=DimensionMap\n");
397 0 : strcat(utlbuf, "\t\tGROUP=IndexDimensionMap\n");
398 0 : strcat(utlbuf, "\t\tEND_GROUP=IndexDimensionMap\n");
399 0 : strcat(utlbuf, "\t\tGROUP=GeoField\n");
400 0 : strcat(utlbuf, "\t\tEND_GROUP=GeoField\n");
401 0 : strcat(utlbuf, "\t\tGROUP=DataField\n");
402 0 : strcat(utlbuf, "\t\tEND_GROUP=DataField\n");
403 0 : strcat(utlbuf, "\t\tGROUP=MergedFields\n");
404 0 : strcat(utlbuf, "\t\tEND_GROUP=MergedFields\n");
405 0 : sprintf(utlbuf2, "%s%ld%s",
406 : "\tEND_GROUP=SWATH_", (long)nSwath + 1, "\n");
407 0 : strcat(utlbuf, utlbuf2);
408 :
409 :
410 0 : status = EHinsertmeta(sdInterfaceID, "", "s", 1001L,
411 : utlbuf, NULL);
412 : }
413 : }
414 : else
415 : {
416 : /* Too many files opened */
417 : /* --------------------- */
418 0 : status = -1;
419 0 : strcpy(errbuf,
420 : "No more than %d swaths may be open simutaneously");
421 0 : strcat(errbuf, " (%s)");
422 0 : HEpush(DFE_DENIED, "SWcreate", __FILE__, __LINE__);
423 0 : HEreport(errbuf, NSWATH, swathname);
424 : }
425 :
426 :
427 : /* Assign swathID # & Load swath and SWXSwath table entries */
428 : /* -------------------------------------------------------- */
429 0 : if (status == 0)
430 : {
431 :
432 0 : for (i = 0; i < NSWATH; i++)
433 : {
434 0 : if (SWXSwath[i].active == 0)
435 : {
436 : /*
437 : * Set swathID, Set swath entry active, Store root Vgroup
438 : * ID, Store sub Vgroup IDs, Store HDF-EOS file ID
439 : */
440 0 : swathID = i + idOffset;
441 0 : SWXSwath[i].active = 1;
442 0 : SWXSwath[i].IDTable = vgid[0];
443 0 : SWXSwath[i].VIDTable[0] = vgid[1];
444 0 : SWXSwath[i].VIDTable[1] = vgid[2];
445 0 : SWXSwath[i].VIDTable[2] = vgid[3];
446 0 : SWXSwath[i].fid = fid;
447 0 : status = 0;
448 0 : break;
449 : }
450 : }
451 :
452 : }
453 : }
454 0 : return (swathID);
455 : }
456 :
457 :
458 : /*----------------------------------------------------------------------------|
459 : | BEGIN_PROLOG |
460 : | |
461 : | FUNCTION: SWattach |
462 : | |
463 : | DESCRIPTION: Attaches to an existing swath within the file. |
464 : | |
465 : | |
466 : | Return Value Type Units Description |
467 : | ============ ====== ========= ===================================== |
468 : | swathID int32 swath structure ID |
469 : | |
470 : | INPUTS: |
471 : | fid int32 HDF-EOS file ID |
472 : | swathname char swath structure name |
473 : | |
474 : | OUTPUTS: |
475 : | None |
476 : | |
477 : | NOTES: |
478 : | |
479 : | |
480 : | Date Programmer Description |
481 : | ====== ============ ================================================= |
482 : | Jun 96 Joel Gales Original Programmer |
483 : | Apr 99 David Wynne Modified test for memory allocation check when no |
484 : | SDSs are in the Swath, NCR22513 |
485 : | |
486 : | END_PROLOG |
487 : -----------------------------------------------------------------------------*/
488 : int32
489 0 : SWattach(int32 fid, char *swathname)
490 :
491 : {
492 : intn i; /* Loop index */
493 : intn j; /* Loop index */
494 0 : intn nswathopen = 0; /* # of swath structures open */
495 : intn status; /* routine return status variable */
496 :
497 : uint8 acs; /* Read/Write file access code */
498 :
499 : int32 HDFfid; /* HDF file id */
500 : int32 vgRef; /* Vgroup reference number */
501 : int32 vgid[4]; /* Vgroup ID array */
502 0 : int32 swathID = -1; /* HDF-EOS swath ID */
503 : int32 *tags; /* Pnt to Vgroup object tags array */
504 : int32 *refs; /* Pnt to Vgroup object refs array */
505 : int32 dum; /* dummy varible */
506 : int32 sdInterfaceID; /* HDF SDS interface ID */
507 : int32 nObjects; /* # of objects in Vgroup */
508 : int32 nSDS; /* SDS counter */
509 : int32 index; /* SDS index */
510 : int32 sdid; /* SDS object ID */
511 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
512 :
513 : char name[80]; /* Vgroup name */
514 : char class[80]; /* Vgroup class */
515 : char errbuf[256];/* Buffer for error message */
516 : char acsCode[1]; /* Read/Write access char: "r/w" */
517 :
518 :
519 : /* Check HDF-EOS file ID, get back HDF file ID and access code */
520 : /* ----------------------------------------------------------- */
521 0 : status = EHchkfid(fid, swathname, &HDFfid, &dum, &acs);
522 :
523 :
524 0 : if (status == 0)
525 : {
526 : /* Convert numeric access code to character */
527 : /* ---------------------------------------- */
528 0 : acsCode[0] = (acs == 1) ? 'w' : 'r';
529 :
530 : /* Determine number of swaths currently opened */
531 : /* ------------------------------------------- */
532 0 : for (i = 0; i < NSWATH; i++)
533 : {
534 0 : nswathopen += SWXSwath[i].active;
535 : }
536 :
537 : /* If room for more ... */
538 : /* -------------------- */
539 0 : if (nswathopen < NSWATH)
540 : {
541 :
542 : /* Search Vgroups for Swath */
543 : /* ------------------------ */
544 0 : vgRef = -1;
545 :
546 : while (1)
547 : {
548 0 : vgRef = Vgetid(HDFfid, vgRef);
549 :
550 :
551 : /* If no more Vgroups then exist while loop */
552 : /* ---------------------------------------- */
553 0 : if (vgRef == -1)
554 : {
555 0 : break;
556 : }
557 :
558 : /* Get name and class of Vgroup */
559 : /* ---------------------------- */
560 0 : vgid[0] = Vattach(HDFfid, vgRef, "r");
561 0 : Vgetname(vgid[0], name);
562 0 : Vgetclass(vgid[0], class);
563 :
564 :
565 : /*
566 : * If Vgroup with swathname and class SWATH found, load
567 : * tables
568 : */
569 :
570 0 : if (strcmp(name, swathname) == 0 &&
571 0 : strcmp(class, "SWATH") == 0)
572 : {
573 : /* Attach to "Fields" and "Swath Attributes" Vgroups */
574 : /* ------------------------------------------------- */
575 0 : tags = (int32 *) malloc(sizeof(int32) * 3);
576 0 : if(tags == NULL)
577 : {
578 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
579 0 : return(-1);
580 : }
581 0 : refs = (int32 *) malloc(sizeof(int32) * 3);
582 0 : if(refs == NULL)
583 : {
584 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
585 0 : free(tags);
586 0 : return(-1);
587 : }
588 0 : Vgettagrefs(vgid[0], tags, refs, 3);
589 0 : vgid[1] = Vattach(HDFfid, refs[0], acsCode);
590 0 : vgid[2] = Vattach(HDFfid, refs[1], acsCode);
591 0 : vgid[3] = Vattach(HDFfid, refs[2], acsCode);
592 0 : free(tags);
593 0 : free(refs);
594 :
595 : /* Setup External Arrays */
596 : /* --------------------- */
597 0 : for (i = 0; i < NSWATH; i++)
598 : {
599 : /* Find empty entry in array */
600 : /* ------------------------- */
601 0 : if (SWXSwath[i].active == 0)
602 : {
603 : /*
604 : * Set swathID, Set swath entry active, Store
605 : * root Vgroup ID, Store sub Vgroup IDs, Store
606 : * HDF-EOS file ID
607 : */
608 0 : swathID = i + idOffset;
609 0 : SWXSwath[i].active = 1;
610 0 : SWXSwath[i].IDTable = vgid[0];
611 0 : SWXSwath[i].VIDTable[0] = vgid[1];
612 0 : SWXSwath[i].VIDTable[1] = vgid[2];
613 0 : SWXSwath[i].VIDTable[2] = vgid[3];
614 0 : SWXSwath[i].fid = fid;
615 0 : break;
616 : }
617 : }
618 :
619 : /* Get SDS interface ID */
620 : /* -------------------- */
621 0 : status = SWchkswid(swathID, "SWattach", &dum,
622 : &sdInterfaceID, &dum);
623 :
624 :
625 : /* Access swath "Geolocation" SDS */
626 : /* ------------------------------ */
627 :
628 : /* Get # of entries within this Vgroup & search for SDS */
629 : /* ---------------------------------------------------- */
630 0 : nObjects = Vntagrefs(vgid[1]);
631 :
632 0 : if (nObjects > 0)
633 : {
634 : /* Get tag and ref # for Geolocation Vgroup objects */
635 : /* ------------------------------------------------ */
636 0 : tags = (int32 *) malloc(sizeof(int32) * nObjects);
637 0 : if(tags == NULL)
638 : {
639 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
640 0 : return(-1);
641 : }
642 0 : refs = (int32 *) malloc(sizeof(int32) * nObjects);
643 0 : if(refs == NULL)
644 : {
645 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
646 0 : free(tags);
647 0 : return(-1);
648 : }
649 0 : Vgettagrefs(vgid[1], tags, refs, nObjects);
650 :
651 : /* Count number of SDS & allocate SDS ID array */
652 : /* ------------------------------------------- */
653 0 : nSDS = 0;
654 0 : for (j = 0; j < nObjects; j++)
655 : {
656 0 : if (tags[j] == DFTAG_NDG)
657 : {
658 0 : nSDS++;
659 : }
660 : }
661 0 : SWXSwath[i].sdsID = (int32 *) calloc(nSDS, 4);
662 0 : if(SWXSwath[i].sdsID == NULL && nSDS != 0)
663 : {
664 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
665 0 : free(tags);
666 0 : free(refs);
667 0 : return(-1);
668 : }
669 0 : nSDS = 0;
670 :
671 :
672 : /* Fill SDS ID array */
673 : /* ----------------- */
674 0 : for (j = 0; j < nObjects; j++)
675 : {
676 : /* If object is SDS then get id */
677 : /* ---------------------------- */
678 0 : if (tags[j] == DFTAG_NDG)
679 : {
680 0 : index = SDreftoindex(sdInterfaceID, refs[j]);
681 0 : sdid = SDselect(sdInterfaceID, index);
682 0 : SWXSwath[i].sdsID[nSDS] = sdid;
683 0 : nSDS++;
684 0 : SWXSwath[i].nSDS++;
685 : }
686 : }
687 0 : free(tags);
688 0 : free(refs);
689 : }
690 :
691 : /* Access swath "Data" SDS */
692 : /* ----------------------- */
693 :
694 : /* Get # of entries within this Vgroup & search for SDS */
695 : /* ---------------------------------------------------- */
696 0 : nObjects = Vntagrefs(vgid[2]);
697 :
698 0 : if (nObjects > 0)
699 : {
700 : /* Get tag and ref # for Data Vgroup objects */
701 : /* ----------------------------------------- */
702 0 : tags = (int32 *) malloc(sizeof(int32) * nObjects);
703 0 : if(tags == NULL)
704 : {
705 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
706 0 : return(-1);
707 : }
708 0 : refs = (int32 *) malloc(sizeof(int32) * nObjects);
709 0 : if(refs == NULL)
710 : {
711 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
712 0 : free(tags);
713 0 : return(-1);
714 : }
715 0 : Vgettagrefs(vgid[2], tags, refs, nObjects);
716 :
717 :
718 : /* Count number of SDS & allocate SDS ID array */
719 : /* ------------------------------------------- */
720 0 : nSDS = 0;
721 0 : for (j = 0; j < nObjects; j++)
722 : {
723 0 : if (tags[j] == DFTAG_NDG)
724 : {
725 0 : nSDS++;
726 : }
727 : }
728 0 : SWXSwath[i].sdsID = (int32 *)
729 : realloc((void *) SWXSwath[i].sdsID,
730 0 : (SWXSwath[i].nSDS + nSDS) * 4);
731 0 : if(SWXSwath[i].sdsID == NULL && nSDS != 0)
732 : {
733 0 : HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
734 0 : return(-1);
735 : }
736 :
737 : /* Fill SDS ID array */
738 : /* ----------------- */
739 0 : for (j = 0; j < nObjects; j++)
740 : {
741 : /* If object is SDS then get id */
742 : /* ---------------------------- */
743 0 : if (tags[j] == DFTAG_NDG)
744 : {
745 0 : index = SDreftoindex(sdInterfaceID, refs[j]);
746 0 : sdid = SDselect(sdInterfaceID, index);
747 0 : SWXSwath[i].sdsID[SWXSwath[i].nSDS] = sdid;
748 0 : SWXSwath[i].nSDS++;
749 : }
750 : }
751 0 : free(tags);
752 0 : free(refs);
753 : }
754 0 : break;
755 : }
756 :
757 : /* Detach Vgroup if not desired Swath */
758 : /* ---------------------------------- */
759 0 : Vdetach(vgid[0]);
760 0 : }
761 :
762 : /* If Swath not found then set up error message */
763 : /* -------------------------------------------- */
764 0 : if (swathID == -1)
765 : {
766 0 : HEpush(DFE_RANGE, "SWattach", __FILE__, __LINE__);
767 0 : HEreport("Swath: \"%s\" does not exist within HDF file.\n",
768 : swathname);
769 : }
770 : }
771 : else
772 : {
773 : /* Too many files opened */
774 : /* --------------------- */
775 0 : swathID = -1;
776 0 : strcpy(errbuf,
777 : "No more than %d swaths may be open simutaneously");
778 0 : strcat(errbuf, " (%s)");
779 0 : HEpush(DFE_DENIED, "SWattach", __FILE__, __LINE__);
780 0 : HEreport(errbuf, NSWATH, swathname);
781 : }
782 :
783 : }
784 0 : return (swathID);
785 : }
786 :
787 : /*----------------------------------------------------------------------------|
788 : | BEGIN_PROLOG |
789 : | |
790 : | FUNCTION: SWchkswid |
791 : | |
792 : | DESCRIPTION: Checks for valid swathID and returns file ID, SDS ID, and |
793 : | swath Vgroup ID |
794 : | |
795 : | |
796 : | Return Value Type Units Description |
797 : | ============ ====== ========= ===================================== |
798 : | status intn return status (0) SUCCEED, (-1) FAIL |
799 : | |
800 : | INPUTS: |
801 : | swathID int32 swath structure ID |
802 : | routname char Name of routine calling SWchkswid |
803 : | |
804 : | OUTPUTS: |
805 : | fid int32 File ID |
806 : | sdInterfaceID int32 SDS interface ID |
807 : | swVgrpID int32 swath Vgroup ID |
808 : | |
809 : | NOTES: |
810 : | |
811 : | |
812 : | Date Programmer Description |
813 : | ====== ============ ================================================= |
814 : | Jun 96 Joel Gales Original Programmer |
815 : | |
816 : | END_PROLOG |
817 : -----------------------------------------------------------------------------*/
818 : static intn
819 0 : SWchkswid(int32 swathID, char *routname,
820 : int32 * fid, int32 * sdInterfaceID, int32 * swVgrpID)
821 :
822 : {
823 0 : intn status = 0; /* routine return status variable */
824 : uint8 access; /* Read/Write access code */
825 :
826 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
827 :
828 : char message1[] =
829 0 : "Invalid swath id: %d in routine \"%s\". ID must be >= %d and < %d.\n";
830 : char message2[] =
831 0 : "Swath id %d in routine \"%s\" not active.\n";
832 :
833 :
834 : /* Check for valid swath id */
835 : /* ------------------------ */
836 0 : if (swathID < idOffset || swathID >= NSWATH + idOffset)
837 : {
838 0 : status = -1;
839 0 : HEpush(DFE_RANGE, "SWchkswid", __FILE__, __LINE__);
840 0 : HEreport(message1, swathID, routname, idOffset, NSWATH + idOffset);
841 : }
842 : else
843 : {
844 : /* Check for active swath ID */
845 : /* ------------------------- */
846 0 : if (SWXSwath[swathID % idOffset].active == 0)
847 : {
848 0 : status = -1;
849 0 : HEpush(DFE_GENAPP, "SWchkswid", __FILE__, __LINE__);
850 0 : HEreport(message2, swathID, routname);
851 : }
852 : else
853 : {
854 :
855 : /* Get file & SDS ids and Swath Vgroup */
856 : /* ----------------------------------- */
857 0 : status = EHchkfid(SWXSwath[swathID % idOffset].fid, " ", fid,
858 : sdInterfaceID, &access);
859 0 : *swVgrpID = SWXSwath[swathID % idOffset].IDTable;
860 : }
861 : }
862 0 : return (status);
863 : }
864 :
865 :
866 :
867 :
868 :
869 :
870 :
871 : /*----------------------------------------------------------------------------|
872 : | BEGIN_PROLOG |
873 : | |
874 : | FUNCTION: SWdefdim |
875 : | |
876 : | DESCRIPTION: Defines numerical value of dimension |
877 : | |
878 : | |
879 : | Return Value Type Units Description |
880 : | ============ ====== ========= ===================================== |
881 : | status intn return status (0) SUCCEED, (-1) FAIL |
882 : | |
883 : | INPUTS: |
884 : | swathID int32 swath structure ID |
885 : | dimname char Dimension name to define |
886 : | dim int32 Dimemsion value |
887 : | |
888 : | OUTPUTS: |
889 : | None |
890 : | |
891 : | NOTES: |
892 : | |
893 : | |
894 : | Date Programmer Description |
895 : | ====== ============ ================================================= |
896 : | Jun 96 Joel Gales Original Programmer |
897 : | Dec 96 Joel Gales Check that dim value >= 0 |
898 : | |
899 : | END_PROLOG |
900 : -----------------------------------------------------------------------------*/
901 : intn
902 0 : SWdefdim(int32 swathID, char *dimname, int32 dim)
903 :
904 : {
905 : intn status; /* routine return status variable */
906 :
907 : int32 fid; /* HDF-EOS file id */
908 : int32 sdInterfaceID; /* HDF SDS interface ID */
909 : int32 swVgrpID; /* Swath root Vgroup ID */
910 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
911 :
912 : char swathname[80] /* Swath name */ ;
913 :
914 :
915 : /* Check for valid swath id */
916 : /* ------------------------ */
917 0 : status = SWchkswid(swathID, "SWdefdim", &fid, &sdInterfaceID, &swVgrpID);
918 :
919 :
920 : /* Make sure dimension >= 0 */
921 : /* ------------------------ */
922 0 : if (dim < 0)
923 : {
924 0 : status = -1;
925 0 : HEpush(DFE_GENAPP, "SWdefdim", __FILE__, __LINE__);
926 0 : HEreport("Dimension value for \"%s\" less than zero: %d.\n",
927 : dimname, dim);
928 : }
929 :
930 :
931 : /* Write Dimension to Structural MetaData */
932 : /* -------------------------------------- */
933 0 : if (status == 0)
934 : {
935 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
936 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s", 0L,
937 : dimname, &dim);
938 : }
939 0 : return (status);
940 : }
941 :
942 :
943 :
944 : /*----------------------------------------------------------------------------|
945 : | BEGIN_PROLOG |
946 : | |
947 : | FUNCTION: SWdiminfo |
948 : | |
949 : | DESCRIPTION: Returns size in bytes of named dimension |
950 : | |
951 : | |
952 : | Return Value Type Units Description |
953 : | ============ ====== ========= ===================================== |
954 : | size int32 Size of dimension |
955 : | |
956 : | INPUTS: |
957 : | swathID int32 swath structure id |
958 : | dimname char Dimension name |
959 : | |
960 : | |
961 : | OUTPUTS: |
962 : | None |
963 : | |
964 : | NOTES: |
965 : | |
966 : | |
967 : | Date Programmer Description |
968 : | ====== ============ ================================================= |
969 : | Jun 96 Joel Gales Original Programmer |
970 : | Aug 96 Joel Gales Make metadata ODL compliant |
971 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
972 : | |
973 : | END_PROLOG |
974 : -----------------------------------------------------------------------------*/
975 : int32
976 0 : SWdiminfo(int32 swathID, char *dimname)
977 :
978 : {
979 : intn status; /* routine return status variable */
980 :
981 : int32 fid; /* HDF-EOS file ID */
982 : int32 sdInterfaceID; /* HDF SDS interface ID */
983 : int32 swVgrpID; /* Swath root Vgroup ID */
984 : int32 size; /* Dimension size */
985 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
986 :
987 :
988 : char *metabuf; /* Pointer to structural metadata (SM) */
989 : char *metaptrs[2];/* Pointers to begin and end of SM section */
990 : char swathname[80]; /* Swath Name */
991 : char *utlstr; /* Utility string */
992 :
993 :
994 : /* Allocate space for utility string */
995 : /* --------------------------------- */
996 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
997 0 : if(utlstr == NULL)
998 : {
999 0 : HEpush(DFE_NOSPACE,"SWdiminfo", __FILE__, __LINE__);
1000 0 : return(-1);
1001 : }
1002 : /* Initialize return value */
1003 0 : size = -1;
1004 :
1005 : /* Check Swath ID */
1006 0 : status = SWchkswid(swathID, "SWdiminfo", &fid, &sdInterfaceID, &swVgrpID);
1007 :
1008 0 : if (status == 0)
1009 : {
1010 : /* Get swath name */
1011 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
1012 :
1013 : /* Get pointers to "Dimension" section within SM */
1014 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
1015 : "Dimension", metaptrs);
1016 :
1017 0 : if(metabuf == NULL)
1018 : {
1019 0 : free(utlstr);
1020 0 : return(-1);
1021 : }
1022 :
1023 : /* Search for dimension name (surrounded by quotes) */
1024 0 : sprintf(utlstr, "%s%s%s", "\"", dimname, "\"\n");
1025 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
1026 :
1027 : /*
1028 : * If dimension found within swath structure then get dimension value
1029 : */
1030 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
1031 : {
1032 : /* Set endptr at end of dimension definition entry */
1033 0 : metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
1034 :
1035 0 : status = EHgetmetavalue(metaptrs, "Size", utlstr);
1036 :
1037 0 : if (status == 0)
1038 : {
1039 0 : size = atol(utlstr);
1040 : }
1041 : else
1042 : {
1043 0 : HEpush(DFE_GENAPP, "SWdiminfo", __FILE__, __LINE__);
1044 0 : HEreport("\"Size\" string not found in metadata.\n");
1045 : }
1046 : }
1047 : else
1048 : {
1049 0 : HEpush(DFE_GENAPP, "SWdiminfo", __FILE__, __LINE__);
1050 0 : HEreport("Dimension \"%s\" not found.\n", dimname);
1051 : }
1052 :
1053 0 : free(metabuf);
1054 : }
1055 0 : free(utlstr);
1056 :
1057 0 : return (size);
1058 : }
1059 :
1060 :
1061 :
1062 :
1063 : /*----------------------------------------------------------------------------|
1064 : | BEGIN_PROLOG |
1065 : | |
1066 : | FUNCTION: SWmapinfo |
1067 : | |
1068 : | DESCRIPTION: Returns dimension mapping information |
1069 : | |
1070 : | |
1071 : | Return Value Type Units Description |
1072 : | ============ ====== ========= ===================================== |
1073 : | status intn return status (0) SUCCEED, (-1) FAIL |
1074 : | |
1075 : | INPUTS: |
1076 : | swathID int32 swath structure id |
1077 : | geodim char geolocation dimension name |
1078 : | datadim char data dimension name |
1079 : | |
1080 : | OUTPUTS: |
1081 : | offset int32 mapping offset |
1082 : | increment int32 mapping increment |
1083 : | |
1084 : | NOTES: |
1085 : | |
1086 : | |
1087 : | Date Programmer Description |
1088 : | ====== ============ ================================================= |
1089 : | Jun 96 Joel Gales Original Programmer |
1090 : | Aug 96 Joel Gales Make metadata ODL compliant |
1091 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
1092 : | |
1093 : | END_PROLOG |
1094 : -----------------------------------------------------------------------------*/
1095 : intn
1096 0 : SWmapinfo(int32 swathID, char *geodim, char *datadim, int32 * offset,
1097 : int32 * increment)
1098 :
1099 : {
1100 : intn status; /* routine return status variable */
1101 0 : intn statmeta = 0; /* EHgetmetavalue return status */
1102 :
1103 : int32 fid; /* HDF-EOS file ID */
1104 : int32 sdInterfaceID; /* HDF SDS interface ID */
1105 : int32 swVgrpID; /* Swath root Vgroup ID */
1106 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
1107 :
1108 : char *metabuf; /* Pointer to structural metadata (SM) */
1109 : char *metaptrs[2];/* Pointers to begin and end of SM section */
1110 : char swathname[80]; /* Swath Name */
1111 : char *utlstr; /* Utility string */
1112 :
1113 :
1114 : /* Allocate space for utility string */
1115 : /* --------------------------------- */
1116 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
1117 0 : if(utlstr == NULL)
1118 : {
1119 0 : HEpush(DFE_NOSPACE,"SWmapinfo", __FILE__, __LINE__);
1120 0 : return(-1);
1121 : }
1122 : /* Initialize return values */
1123 0 : *offset = -1;
1124 0 : *increment = -1;
1125 :
1126 : /* Check Swath ID */
1127 0 : status = SWchkswid(swathID, "SWmapinfo", &fid, &sdInterfaceID, &swVgrpID);
1128 :
1129 0 : if (status == 0)
1130 : {
1131 : /* Get swath name */
1132 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
1133 :
1134 : /* Get pointers to "DimensionMap" section within SM */
1135 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
1136 : "DimensionMap", metaptrs);
1137 0 : if(metabuf == NULL)
1138 : {
1139 0 : free(utlstr);
1140 0 : return(-1);
1141 : }
1142 :
1143 : /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
1144 0 : sprintf(utlstr, "%s%s%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
1145 : "\"\n\t\t\t\tDataDimension=\"", datadim, "\"\n");
1146 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
1147 :
1148 : /*
1149 : * If mapping found within swath structure then get offset and
1150 : * increment value
1151 : */
1152 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
1153 : {
1154 : /* Get Offset */
1155 0 : statmeta = EHgetmetavalue(metaptrs, "Offset", utlstr);
1156 0 : if (statmeta == 0)
1157 : {
1158 0 : *offset = atol(utlstr);
1159 : }
1160 : else
1161 : {
1162 0 : status = -1;
1163 0 : HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
1164 0 : HEreport("\"Offset\" string not found in metadata.\n");
1165 : }
1166 :
1167 :
1168 : /* Get Increment */
1169 0 : statmeta = EHgetmetavalue(metaptrs, "Increment", utlstr);
1170 0 : if (statmeta == 0)
1171 : {
1172 0 : *increment = atol(utlstr);
1173 : }
1174 : else
1175 : {
1176 0 : status = -1;
1177 0 : HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
1178 0 : HEreport("\"Increment\" string not found in metadata.\n");
1179 : }
1180 : }
1181 : else
1182 : {
1183 0 : status = -1;
1184 0 : HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
1185 0 : HEreport("Mapping \"%s/%s\" not found.\n", geodim, datadim);
1186 : }
1187 :
1188 0 : free(metabuf);
1189 : }
1190 0 : free(utlstr);
1191 0 : return (status);
1192 : }
1193 :
1194 :
1195 :
1196 :
1197 : /*----------------------------------------------------------------------------|
1198 : | BEGIN_PROLOG |
1199 : | |
1200 : | FUNCTION: SWidxmapinfo |
1201 : | |
1202 : | DESCRIPTION: Returns indexed mapping information |
1203 : | |
1204 : | |
1205 : | Return Value Type Units Description |
1206 : | ============ ====== ========= ===================================== |
1207 : | gsize int32 Number of index values (sz of geo dim) |
1208 : | |
1209 : | INPUTS: |
1210 : | swathID int32 swath structure id |
1211 : | geodim char geolocation dimension name |
1212 : | datadim char data dimension name |
1213 : | |
1214 : | |
1215 : | OUTPUTS: |
1216 : | index int32 array of index values |
1217 : | |
1218 : | NOTES: |
1219 : | |
1220 : | |
1221 : | Date Programmer Description |
1222 : | ====== ============ ================================================= |
1223 : | Jun 96 Joel Gales Original Programmer |
1224 : | |
1225 : | END_PROLOG |
1226 : -----------------------------------------------------------------------------*/
1227 : int32
1228 0 : SWidxmapinfo(int32 swathID, char *geodim, char *datadim, int32 index[])
1229 : {
1230 : intn status; /* routine return status variable */
1231 :
1232 : int32 fid; /* HDF-EOS file ID */
1233 : int32 sdInterfaceID; /* HDF SDS interface ID */
1234 : int32 swVgrpID; /* Swath root Vgroup ID */
1235 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
1236 : int32 vgid; /* Swath Attributes Vgroup ID */
1237 : int32 vdataID; /* Index Mapping Vdata ID */
1238 0 : int32 gsize = -1; /* Size of geo dim */
1239 :
1240 : char utlbuf[256];/* Utility buffer */
1241 :
1242 :
1243 : /* Check Swath ID */
1244 0 : status = SWchkswid(swathID, "SWidxmapinfo",
1245 : &fid, &sdInterfaceID, &swVgrpID);
1246 :
1247 0 : if (status == 0)
1248 : {
1249 : /* Find Index Mapping Vdata with Swath Attributes Vgroup */
1250 0 : sprintf(utlbuf, "%s%s%s%s", "INDXMAP:", geodim, "/", datadim);
1251 0 : vgid = SWXSwath[swathID % idOffset].VIDTable[2];
1252 0 : vdataID = EHgetid(fid, vgid, utlbuf, 1, "r");
1253 :
1254 : /* If found then get geodim size & read index mapping values */
1255 0 : if (vdataID != -1)
1256 : {
1257 0 : gsize = SWdiminfo(swathID, geodim);
1258 :
1259 0 : VSsetfields(vdataID, "Index");
1260 0 : VSread(vdataID, (uint8 *) index, 1, FULL_INTERLACE);
1261 0 : VSdetach(vdataID);
1262 : }
1263 : else
1264 : {
1265 0 : status = -1;
1266 0 : HEpush(DFE_GENAPP, "SWidxmapinfo", __FILE__, __LINE__);
1267 0 : HEreport("Index Mapping \"%s\" not found.\n", utlbuf);
1268 : }
1269 : }
1270 0 : return (gsize);
1271 : }
1272 :
1273 :
1274 :
1275 :
1276 : /*----------------------------------------------------------------------------|
1277 : | BEGIN_PROLOG |
1278 : | |
1279 : | FUNCTION: SWcompinfo |
1280 : | |
1281 : | DESCRIPTION: |
1282 : | |
1283 : | |
1284 : | Return Value Type Units Description |
1285 : | ============ ====== ========= ===================================== |
1286 : | status intn |
1287 : | |
1288 : | INPUTS: |
1289 : | swathID int32 |
1290 : | compcode int32 |
1291 : | compparm intn |
1292 : | |
1293 : | |
1294 : | OUTPUTS: |
1295 : | None |
1296 : | |
1297 : | NOTES: |
1298 : | |
1299 : | |
1300 : | Date Programmer Description |
1301 : | ====== ============ ================================================= |
1302 : | Oct 96 Joel Gales Original Programmer |
1303 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
1304 : | |
1305 : | END_PROLOG |
1306 : -----------------------------------------------------------------------------*/
1307 : intn
1308 0 : SWcompinfo(int32 swathID, char *fieldname, int32 * compcode, intn compparm[])
1309 : {
1310 : intn i; /* Loop Index */
1311 : intn status; /* routine return status variable */
1312 0 : intn statmeta = 0; /* EHgetmetavalue return status */
1313 :
1314 : int32 fid; /* HDF-EOS file ID */
1315 : int32 sdInterfaceID; /* HDF SDS interface ID */
1316 : int32 swVgrpID; /* Swath root Vgroup ID */
1317 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
1318 :
1319 : char *metabuf; /* Pointer to structural metadata (SM) */
1320 : char *metaptrs[2];/* Pointers to begin and end of SM section */
1321 : char swathname[80]; /* Swath Name */
1322 : char *utlstr; /* Utility string */
1323 :
1324 : char *HDFcomp[5] = {"HDFE_COMP_NONE", "HDFE_COMP_RLE",
1325 : "HDFE_COMP_NBIT", "HDFE_COMP_SKPHUFF",
1326 0 : "HDFE_COMP_DEFLATE"}; /* Compression Codes */
1327 :
1328 : /* Allocate space for utility string */
1329 : /* --------------------------------- */
1330 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
1331 0 : if(utlstr == NULL)
1332 : {
1333 0 : HEpush(DFE_NOSPACE,"SWcompinfo", __FILE__, __LINE__);
1334 0 : return(-1);
1335 : }
1336 :
1337 : /* Check Swath ID */
1338 0 : status = SWchkswid(swathID, "SWcompinfo",
1339 : &fid, &sdInterfaceID, &swVgrpID);
1340 :
1341 0 : if (status == 0)
1342 : {
1343 : /* Get swath name */
1344 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
1345 :
1346 : /* Get pointers to "DataField" section within SM */
1347 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
1348 : "DataField", metaptrs);
1349 0 : if(metabuf == NULL)
1350 : {
1351 0 : free(utlstr);
1352 0 : return(-1);
1353 : }
1354 : /* Search for field */
1355 0 : sprintf(utlstr, "%s%s%s", "\"", fieldname, "\"\n");
1356 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
1357 :
1358 : /* If not found then search in "GeoField" section */
1359 0 : if (metaptrs[0] > metaptrs[1] || metaptrs[0] == NULL)
1360 : {
1361 0 : free(metabuf);
1362 :
1363 : /* Get pointers to "GeoField" section within SM */
1364 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
1365 : "GeoField", metaptrs);
1366 0 : if(metabuf == NULL)
1367 : {
1368 0 : free(utlstr);
1369 0 : return(-1);
1370 : }
1371 : /* Search for field */
1372 0 : sprintf(utlstr, "%s%s%s", "\"", fieldname, "\"\n");
1373 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
1374 : }
1375 :
1376 :
1377 : /* If field found and user wants compression code ... */
1378 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
1379 : {
1380 0 : if (compcode != NULL)
1381 : {
1382 : /* Set endptr at end of field's definition entry */
1383 0 : metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
1384 :
1385 : /* Get compression type */
1386 0 : statmeta = EHgetmetavalue(metaptrs, "CompressionType", utlstr);
1387 :
1388 : /*
1389 : * Default is no compression if "CompressionType" string not
1390 : * in metadata
1391 : */
1392 0 : *compcode = HDFE_COMP_NONE;
1393 :
1394 : /* If compression code is found ... */
1395 0 : if (statmeta == 0)
1396 : {
1397 : /* Loop through compression types until match */
1398 0 : for (i = 0; i < 5; i++)
1399 : {
1400 0 : if (strcmp(utlstr, HDFcomp[i]) == 0)
1401 : {
1402 0 : *compcode = i;
1403 0 : break;
1404 : }
1405 : }
1406 : }
1407 : }
1408 :
1409 : /* If user wants compression parameters ... */
1410 0 : if (compparm != NULL && compcode != NULL)
1411 : {
1412 : /* Initialize to zero */
1413 0 : for (i = 0; i < 4; i++)
1414 : {
1415 0 : compparm[i] = 0.0;
1416 : }
1417 :
1418 : /*
1419 : * Get compression parameters if NBIT or DEFLATE compression
1420 : */
1421 0 : if (*compcode == HDFE_COMP_NBIT)
1422 : {
1423 0 : statmeta =
1424 0 : EHgetmetavalue(metaptrs, "CompressionParams", utlstr);
1425 0 : if (statmeta == 0)
1426 : {
1427 0 : sscanf(utlstr, "(%d,%d,%d,%d)",
1428 : &compparm[0], &compparm[1],
1429 : &compparm[2], &compparm[3]);
1430 : }
1431 : else
1432 : {
1433 0 : status = -1;
1434 0 : HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
1435 0 : HEreport(
1436 : "\"CompressionParams\" string not found in metadata.\n");
1437 : }
1438 : }
1439 0 : else if (*compcode == HDFE_COMP_DEFLATE)
1440 : {
1441 0 : statmeta =
1442 0 : EHgetmetavalue(metaptrs, "DeflateLevel", utlstr);
1443 0 : if (statmeta == 0)
1444 : {
1445 0 : sscanf(utlstr, "%d", &compparm[0]);
1446 : }
1447 : else
1448 : {
1449 0 : status = -1;
1450 0 : HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
1451 0 : HEreport(
1452 : "\"DeflateLevel\" string not found in metadata.\n");
1453 : }
1454 : }
1455 : }
1456 : }
1457 : else
1458 : {
1459 0 : HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
1460 0 : HEreport("Fieldname \"%s\" not found.\n", fieldname);
1461 : }
1462 :
1463 0 : free(metabuf);
1464 : }
1465 0 : free(utlstr);
1466 :
1467 0 : return (status);
1468 : }
1469 :
1470 :
1471 :
1472 : /*----------------------------------------------------------------------------|
1473 : | BEGIN_PROLOG |
1474 : | |
1475 : | FUNCTION: SWfinfo |
1476 : | |
1477 : | DESCRIPTION: Returns field info |
1478 : | |
1479 : | |
1480 : | Return Value Type Units Description |
1481 : | ============ ====== ========= ===================================== |
1482 : | status intn return status (0) SUCCEED, (-1) FAIL |
1483 : | |
1484 : | INPUTS: |
1485 : | swathID int32 swath structure id |
1486 : | fieldtype const char fieldtype (geo or data) |
1487 : | fieldname const char name of field |
1488 : | |
1489 : | |
1490 : | OUTPUTS: |
1491 : | rank int32 rank of field (# of dims) |
1492 : | dims int32 field dimensions |
1493 : | numbertype int32 field number type |
1494 : | dimlist char field dimension list |
1495 : | |
1496 : | NOTES: |
1497 : | |
1498 : | |
1499 : | Date Programmer Description |
1500 : | ====== ============ ================================================= |
1501 : | Jun 96 Joel Gales Original Programmer |
1502 : | Aug 96 Joel Gales Make metadata ODL compliant |
1503 : | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue |
1504 : | |
1505 : | END_PROLOG |
1506 : -----------------------------------------------------------------------------*/
1507 : static int32
1508 0 : SWfinfo(int32 swathID, const char *fieldtype, const char *fieldname,
1509 : int32 *rank, int32 dims[], int32 *numbertype, char *dimlist)
1510 :
1511 : {
1512 : intn i; /* Loop index */
1513 : intn j; /* Loop index */
1514 : intn status; /* routine return status variable */
1515 0 : intn statmeta = 0; /* EHgetmetavalue return status */
1516 :
1517 : int32 fid; /* HDF-EOS file ID */
1518 : int32 sdInterfaceID; /* HDF SDS interface ID */
1519 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
1520 : int32 fsize; /* field size in bytes */
1521 0 : int32 ndims = 0; /* Number of dimensions */
1522 : int32 slen[8]; /* Length of each entry in parsed string */
1523 : int32 dum; /* Dummy variable */
1524 : int32 vdataID; /* 1d field vdata ID */
1525 :
1526 : uint8 *buf; /* One-Dim field buffer */
1527 :
1528 : char *metabuf; /* Pointer to structural metadata (SM) */
1529 : char *metaptrs[2];/* Pointers to begin and end of SM section */
1530 : char swathname[80]; /* Swath Name */
1531 : char *utlstr; /* Utility string */
1532 : char *ptr[8]; /* String pointers for parsed string */
1533 : char dimstr[64]; /* Individual dimension entry string */
1534 :
1535 :
1536 : /* Allocate space for utility string */
1537 : /* --------------------------------- */
1538 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
1539 0 : if(utlstr == NULL)
1540 : {
1541 0 : HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__);
1542 0 : return(-1);
1543 : }
1544 :
1545 : /* Initialize rank and numbertype to -1 (error) */
1546 : /* -------------------------------------------- */
1547 0 : *rank = -1;
1548 0 : *numbertype = -1;
1549 :
1550 : /* Get HDF-EOS file ID and SDS interface ID */
1551 0 : status = SWchkswid(swathID, "SWfinfo", &fid, &sdInterfaceID, &dum);
1552 :
1553 : /* Get swath name */
1554 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
1555 :
1556 : /* Get pointers to appropriate "Field" section within SM */
1557 0 : if (strcmp(fieldtype, "Geolocation Fields") == 0)
1558 : {
1559 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
1560 : "GeoField", metaptrs);
1561 :
1562 0 : if(metabuf == NULL)
1563 : {
1564 0 : free(utlstr);
1565 0 : return(-1);
1566 : }
1567 : }
1568 : else
1569 : {
1570 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
1571 : "DataField", metaptrs);
1572 0 : if(metabuf == NULL)
1573 : {
1574 0 : free(utlstr);
1575 0 : return(-1);
1576 : }
1577 : }
1578 :
1579 :
1580 : /* Search for field */
1581 0 : sprintf(utlstr, "%s%s%s", "\"", fieldname, "\"\n");
1582 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
1583 :
1584 : /* If field found ... */
1585 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
1586 : {
1587 : /* Get DataType string */
1588 0 : statmeta = EHgetmetavalue(metaptrs, "DataType", utlstr);
1589 :
1590 : /* Convert to numbertype code */
1591 0 : if (statmeta == 0)
1592 0 : *numbertype = EHnumstr(utlstr);
1593 : else
1594 : {
1595 0 : status = -1;
1596 0 : HEpush(DFE_GENAPP, "SWfinfo", __FILE__, __LINE__);
1597 0 : HEreport("\"DataType\" string not found in metadata.\n");
1598 : }
1599 :
1600 :
1601 : /*
1602 : * Get DimList string and trim off leading and trailing parens "()"
1603 : */
1604 0 : statmeta = EHgetmetavalue(metaptrs, "DimList", utlstr);
1605 :
1606 0 : if (statmeta == 0)
1607 : {
1608 0 : memmove(utlstr, utlstr + 1, strlen(utlstr) - 2);
1609 0 : utlstr[strlen(utlstr) - 2] = 0;
1610 :
1611 : /* Parse trimmed DimList string and get rank */
1612 0 : ndims = EHparsestr(utlstr, ',', ptr, slen);
1613 0 : *rank = ndims;
1614 : }
1615 : else
1616 : {
1617 0 : status = -1;
1618 0 : HEpush(DFE_GENAPP, "SWfinfo", __FILE__, __LINE__);
1619 0 : HEreport("\"DimList\" string not found in metadata.\n");
1620 : }
1621 :
1622 : /* If dimension list is desired by user then initialize length to 0 */
1623 0 : if (dimlist != NULL)
1624 : {
1625 0 : dimlist[0] = 0;
1626 : }
1627 :
1628 : /*
1629 : * Copy each entry in DimList and remove leading and trailing quotes,
1630 : * Get dimension sizes and concatanate dimension names to dimension
1631 : * list
1632 : */
1633 0 : for (i = 0; i < ndims; i++)
1634 : {
1635 0 : memcpy(dimstr, ptr[i] + 1, slen[i] - 2);
1636 0 : dimstr[slen[i] - 2] = 0;
1637 0 : dims[i] = SWdiminfo(swathID, dimstr);
1638 0 : if (dimlist != NULL)
1639 : {
1640 0 : if (i > 0)
1641 : {
1642 0 : strcat(dimlist, ",");
1643 : }
1644 0 : strcat(dimlist, dimstr);
1645 : }
1646 :
1647 : }
1648 :
1649 :
1650 : /* Appendable Field Section */
1651 : /* ------------------------ */
1652 0 : if (dims[0] == 0)
1653 : {
1654 : /* One-Dimensional Field */
1655 0 : if (*rank == 1)
1656 : {
1657 : /* Get vdata ID */
1658 0 : status = SW1dfldsrch(fid, swathID, fieldname, "r",
1659 : &dum, &vdataID, &dum);
1660 :
1661 : /* Get actual size of field */
1662 0 : dims[0] = VSelts(vdataID);
1663 :
1664 : /*
1665 : * If size=1 then check where actual record of
1666 : * "initialization" record
1667 : */
1668 0 : if (dims[0] == 1)
1669 : {
1670 : /* Get record size and read 1st record */
1671 0 : fsize = VSsizeof(vdataID, (char *)fieldname);
1672 0 : buf = (uint8 *) calloc(fsize, 1);
1673 0 : if(buf == NULL)
1674 : {
1675 0 : HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__);
1676 0 : free(utlstr);
1677 0 : return(-1);
1678 : }
1679 0 : VSsetfields(vdataID, fieldname);
1680 0 : VSseek(vdataID, 0);
1681 0 : VSread(vdataID, (uint8 *) buf, 1, FULL_INTERLACE);
1682 :
1683 : /* Sum up "bytes" in record */
1684 0 : for (i = 0, j = 0; i < fsize; i++)
1685 : {
1686 0 : j += buf[i];
1687 : }
1688 :
1689 : /*
1690 : * If filled with 255 then "initialization" record,
1691 : * actual number of records = 0
1692 : */
1693 0 : if (j == 255 * fsize)
1694 : {
1695 0 : dims[0] = 0;
1696 : }
1697 :
1698 0 : free(buf);
1699 : }
1700 : /* Detach from 1d field */
1701 0 : VSdetach(vdataID);
1702 : }
1703 : else
1704 : {
1705 : /* Get actual size of Multi-Dimensional Field */
1706 0 : status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
1707 : &dum, &dum, &dum, &dum, dims,
1708 : &dum);
1709 : }
1710 : }
1711 : }
1712 0 : free(metabuf);
1713 :
1714 0 : if (*rank == -1)
1715 : {
1716 0 : status = -1;
1717 : }
1718 0 : free(utlstr);
1719 :
1720 0 : return (status);
1721 : }
1722 :
1723 :
1724 :
1725 :
1726 :
1727 : /*----------------------------------------------------------------------------|
1728 : | BEGIN_PROLOG |
1729 : | |
1730 : | FUNCTION: SWfieldinfo |
1731 : | |
1732 : | DESCRIPTION: Wrapper arount SWfinfo |
1733 : | |
1734 : | |
1735 : | Return Value Type Units Description |
1736 : | ============ ====== ========= ===================================== |
1737 : | status intn return status (0) SUCCEED, (-1) FAIL |
1738 : | |
1739 : | INPUTS: |
1740 : | swathID int32 swath structure id |
1741 : | fieldname const char name of field |
1742 : | |
1743 : | |
1744 : | OUTPUTS: |
1745 : | rank int32 rank of field (# of dims) |
1746 : | dims int32 field dimensions |
1747 : | numbertype int32 field number type |
1748 : | dimlist char field dimension list |
1749 : | |
1750 : | NOTES: |
1751 : | |
1752 : | |
1753 : | Date Programmer Description |
1754 : | ====== ============ ================================================= |
1755 : | Jun 96 Joel Gales Original Programmer |
1756 : | |
1757 : | END_PROLOG |
1758 : -----------------------------------------------------------------------------*/
1759 : intn
1760 0 : SWfieldinfo(int32 swathID, const char *fieldname, int32 * rank, int32 dims[],
1761 : int32 * numbertype, char *dimlist)
1762 :
1763 : {
1764 : intn status; /* routine return status variable */
1765 :
1766 : int32 fid; /* HDF-EOS file ID */
1767 : int32 sdInterfaceID; /* HDF SDS interface ID */
1768 : int32 swVgrpID; /* Swath root Vgroup ID */
1769 :
1770 :
1771 : /* Check for valid swath id */
1772 0 : status = SWchkswid(swathID, "SWfieldinfo", &fid,
1773 : &sdInterfaceID, &swVgrpID);
1774 0 : if (status == 0)
1775 : {
1776 : /* Check for field within Geolocatation Fields */
1777 0 : status = SWfinfo(swathID, "Geolocation Fields", fieldname,
1778 : rank, dims, numbertype, dimlist);
1779 :
1780 : /* If not there then check within Data Fields */
1781 0 : if (status == -1)
1782 : {
1783 0 : status = SWfinfo(swathID, "Data Fields", fieldname,
1784 : rank, dims, numbertype, dimlist);
1785 : }
1786 :
1787 : /* If not there either then can't be found */
1788 0 : if (status == -1)
1789 : {
1790 0 : HEpush(DFE_GENAPP, "SWfieldinfo", __FILE__, __LINE__);
1791 0 : HEreport("Fieldname \"%s\" not found.\n", fieldname);
1792 : }
1793 : }
1794 0 : return (status);
1795 : }
1796 :
1797 :
1798 :
1799 :
1800 :
1801 :
1802 : /*----------------------------------------------------------------------------|
1803 : | BEGIN_PROLOG |
1804 : | |
1805 : | FUNCTION: SWdefdimmap |
1806 : | |
1807 : | DESCRIPTION: Defines mapping between geolocation and data dimensions |
1808 : | |
1809 : | |
1810 : | Return Value Type Units Description |
1811 : | ============ ====== ========= ===================================== |
1812 : | status intn return status (0) SUCCEED, (-1) FAIL |
1813 : | |
1814 : | INPUTS: |
1815 : | swathID int32 swath structure ID |
1816 : | geodim char Geolocation dimension |
1817 : | datadim char Data dimension |
1818 : | offset int32 Mapping offset |
1819 : | increment int32 Mapping increment |
1820 : | |
1821 : | OUTPUTS: |
1822 : | None |
1823 : | |
1824 : | NOTES: |
1825 : | |
1826 : | |
1827 : | Date Programmer Description |
1828 : | ====== ============ ================================================= |
1829 : | Jun 96 Joel Gales Original Programmer |
1830 : | |
1831 : | END_PROLOG |
1832 : -----------------------------------------------------------------------------*/
1833 : intn
1834 0 : SWdefdimmap(int32 swathID, char *geodim, char *datadim, int32 offset,
1835 : int32 increment)
1836 :
1837 : {
1838 : intn status; /* routine return status variable */
1839 :
1840 : int32 fid; /* HDF-EOS file ID */
1841 : int32 sdInterfaceID; /* HDF SDS interface ID */
1842 : int32 size; /* Size of geo dim */
1843 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
1844 : int32 dum; /* Dummy variable */
1845 : int32 metadata[2];/* Offset & Increment (passed to metadata) */
1846 :
1847 : char mapname[80];/* Mapping name (geodim/datadim) */
1848 : char swathname[80]; /* Swath name */
1849 :
1850 : /* Check Swath ID */
1851 0 : status = SWchkswid(swathID, "SWdefdimmap", &fid, &sdInterfaceID, &dum);
1852 :
1853 0 : if (status == 0)
1854 : {
1855 :
1856 : /* Search Dimension Vdata for dimension entries */
1857 : /* -------------------------------------------- */
1858 0 : size = SWdiminfo(swathID, geodim);
1859 0 : if (size == -1)
1860 : {
1861 0 : status = -1;
1862 0 : HEpush(DFE_GENAPP, "SWdefdimmap", __FILE__, __LINE__);
1863 0 : HEreport("Geolocation dimension name: \"%s\" not found.\n",
1864 : geodim);
1865 : }
1866 : /* Data Dimension Search */
1867 : /* --------------------- */
1868 0 : if (status == 0)
1869 : {
1870 0 : size = SWdiminfo(swathID, datadim);
1871 0 : if (size == -1)
1872 : {
1873 0 : status = -1;
1874 0 : HEpush(DFE_GENAPP, "SWdefdimmap", __FILE__, __LINE__);
1875 0 : HEreport("Data dimension name: \"%s\" not found.\n",
1876 : datadim);
1877 : }
1878 : }
1879 :
1880 : /* Write Dimension Map to Structural MetaData */
1881 : /* ------------------------------------------ */
1882 0 : if (status == 0)
1883 : {
1884 0 : sprintf(mapname, "%s%s%s", geodim, "/", datadim);
1885 0 : metadata[0] = offset;
1886 0 : metadata[1] = increment;
1887 :
1888 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
1889 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s", 1L,
1890 : mapname, metadata);
1891 :
1892 : }
1893 : }
1894 0 : return (status);
1895 : }
1896 :
1897 :
1898 :
1899 : /*----------------------------------------------------------------------------|
1900 : | BEGIN_PROLOG |
1901 : | |
1902 : | FUNCTION: SWdefidxmap |
1903 : | |
1904 : | DESCRIPTION: Defines indexed (non-linear) mapping between geolocation |
1905 : | and data dimensions |
1906 : | |
1907 : | |
1908 : | Return Value Type Units Description |
1909 : | ============ ====== ========= ===================================== |
1910 : | status intn return status (0) SUCCEED, (-1) FAIL |
1911 : | |
1912 : | INPUTS: |
1913 : | swathID int32 swath structure ID |
1914 : | geodim char Geolocation dimension |
1915 : | datadim char Data dimension |
1916 : | index int32 Index mapping array |
1917 : | |
1918 : | OUTPUTS: |
1919 : | None |
1920 : | |
1921 : | NOTES: |
1922 : | |
1923 : | |
1924 : | Date Programmer Description |
1925 : | ====== ============ ================================================= |
1926 : | Jun 96 Joel Gales Original Programmer |
1927 : | |
1928 : | END_PROLOG |
1929 : -----------------------------------------------------------------------------*/
1930 : intn
1931 0 : SWdefidxmap(int32 swathID, char *geodim, char *datadim, int32 index[])
1932 :
1933 : {
1934 : intn status; /* routine return status variable */
1935 :
1936 : int32 fid; /* HDF-EOS file ID */
1937 : int32 sdInterfaceID; /* HDF SDS interface ID */
1938 : int32 attrVgrpID; /* Swath attribute ID */
1939 : int32 vdataID; /* Mapping Index Vdata ID */
1940 : int32 gsize; /* Size of geo dim */
1941 : int32 dsize; /* Size of data dim */
1942 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
1943 : int32 dum; /* Dummy variable */
1944 :
1945 : uint8 *buf; /* Vdata field buffer */
1946 :
1947 : char mapname[80];/* Mapping name (geodim/datadim) */
1948 : char swathname[80]; /* Swath name */
1949 : char utlbuf[256];/* Utility buffer */
1950 :
1951 :
1952 : /* Check Swath ID */
1953 0 : status = SWchkswid(swathID, "SWdefidxmap", &fid, &sdInterfaceID, &dum);
1954 0 : if (status == 0)
1955 : {
1956 : /* Search Dimension Vdata for dimension entries */
1957 : /* -------------------------------------------- */
1958 :
1959 : /* Geo Dimension Search */
1960 : /* -------------------- */
1961 :
1962 0 : gsize = SWdiminfo(swathID, geodim);
1963 :
1964 0 : if (gsize == -1)
1965 : {
1966 0 : status = -1;
1967 0 : HEpush(DFE_GENAPP, "SWdefidxmap", __FILE__, __LINE__);
1968 0 : HEreport("Geolocation dimension name: \"%s\" not found.\n",
1969 : geodim);
1970 : }
1971 : /* Data Dimension Search */
1972 : /* --------------------- */
1973 0 : if (status == 0)
1974 : {
1975 0 : dsize = SWdiminfo(swathID, datadim);
1976 0 : if (dsize == -1)
1977 : {
1978 0 : status = -1;
1979 0 : HEpush(DFE_GENAPP, "SWdefidxmap", __FILE__, __LINE__);
1980 0 : HEreport("Data dimension name: \"%s\" not found.\n",
1981 : datadim);
1982 : }
1983 : }
1984 : /* Define Index Vdata and Store Index Array */
1985 : /* ---------------------------------------- */
1986 0 : if (status == 0)
1987 : {
1988 : /* Get attribute Vgroup ID and allocate data buffer */
1989 : /* ------------------------------------------------ */
1990 0 : attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
1991 0 : buf = (uint8 *) calloc(4 * gsize, 1);
1992 0 : if(buf == NULL)
1993 : {
1994 0 : HEpush(DFE_NOSPACE,"SWdefidxmap", __FILE__, __LINE__);
1995 0 : return(-1);
1996 : }
1997 :
1998 : /* Name: "INDXMAP:" + geodim + "/" + datadim */
1999 0 : sprintf(utlbuf, "%s%s%s%s", "INDXMAP:", geodim, "/", datadim);
2000 :
2001 0 : vdataID = VSattach(fid, -1, "w");
2002 0 : VSsetname(vdataID, utlbuf);
2003 :
2004 : /* Attribute Class */
2005 0 : VSsetclass(vdataID, "Attr0.0");
2006 :
2007 : /* Fieldname is "Index" */
2008 0 : VSfdefine(vdataID, "Index", DFNT_INT32, gsize);
2009 0 : VSsetfields(vdataID, "Index");
2010 0 : memcpy(buf, index, 4 * gsize);
2011 :
2012 : /* Write to vdata and free data buffer */
2013 0 : VSwrite(vdataID, buf, 1, FULL_INTERLACE);
2014 0 : free(buf);
2015 :
2016 : /* Insert in Attribute Vgroup and detach Vdata */
2017 0 : Vinsert(attrVgrpID, vdataID);
2018 0 : VSdetach(vdataID);
2019 :
2020 :
2021 : /* Write to Structural Metadata */
2022 0 : sprintf(mapname, "%s%s%s", geodim, "/", datadim);
2023 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
2024 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s", 2L,
2025 : mapname, &dum);
2026 :
2027 : }
2028 : }
2029 0 : return (status);
2030 :
2031 : }
2032 :
2033 :
2034 : /*----------------------------------------------------------------------------|
2035 : | BEGIN_PROLOG |
2036 : | |
2037 : | FUNCTION: SWdefcomp |
2038 : | |
2039 : | DESCRIPTION: Defines compression type and parameters |
2040 : | |
2041 : | |
2042 : | Return Value Type Units Description |
2043 : | ============ ====== ========= ===================================== |
2044 : | status intn return status (0) SUCCEED, (-1) FAIL |
2045 : | |
2046 : | INPUTS: |
2047 : | swathID int32 swath structure ID |
2048 : | compcode int32 compression code |
2049 : | compparm intn compression parameters |
2050 : | |
2051 : | OUTPUTS: |
2052 : | None |
2053 : | |
2054 : | NOTES: |
2055 : | |
2056 : | |
2057 : | Date Programmer Description |
2058 : | ====== ============ ================================================= |
2059 : | Sep 96 Joel Gales Original Programmer |
2060 : | |
2061 : | END_PROLOG |
2062 : -----------------------------------------------------------------------------*/
2063 : intn
2064 0 : SWdefcomp(int32 swathID, int32 compcode, intn compparm[])
2065 : {
2066 0 : intn status = 0; /* routine return status variable */
2067 :
2068 : int32 fid; /* HDF-EOS file id */
2069 : int32 sdInterfaceID; /* HDF SDS interface ID */
2070 : int32 swVgrpID; /* Swath root Vgroup ID */
2071 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
2072 : int32 sID; /* swathID - offset */
2073 :
2074 :
2075 : /* Check for valid swath id */
2076 0 : status = SWchkswid(swathID, "SWdefcomp", &fid, &sdInterfaceID, &swVgrpID);
2077 :
2078 0 : if (status == 0)
2079 : {
2080 0 : sID = swathID % idOffset;
2081 :
2082 : /* Set compression code in compression exteral array */
2083 0 : SWXSwath[sID].compcode = compcode;
2084 :
2085 0 : switch (compcode)
2086 : {
2087 : /* Set NBIT compression parameters in compression external array */
2088 : case HDFE_COMP_NBIT:
2089 :
2090 0 : SWXSwath[sID].compparm[0] = compparm[0];
2091 0 : SWXSwath[sID].compparm[1] = compparm[1];
2092 0 : SWXSwath[sID].compparm[2] = compparm[2];
2093 0 : SWXSwath[sID].compparm[3] = compparm[3];
2094 :
2095 0 : break;
2096 :
2097 : /* Set GZIP compression parameter in compression external array */
2098 : case HDFE_COMP_DEFLATE:
2099 :
2100 0 : SWXSwath[sID].compparm[0] = compparm[0];
2101 :
2102 : break;
2103 :
2104 : }
2105 : }
2106 :
2107 0 : return (status);
2108 : }
2109 :
2110 : /*----------------------------------------------------------------------------|
2111 : | BEGIN_PROLOG |
2112 : | |
2113 : | FUNCTION: SWdefinefield |
2114 : | |
2115 : | DESCRIPTION: Defines geolocation or data field within swath structure |
2116 : | |
2117 : | |
2118 : | Return Value Type Units Description |
2119 : | ============ ====== ========= ===================================== |
2120 : | status intn return status (0) SUCCEED, (-1) FAIL |
2121 : | |
2122 : | INPUTS: |
2123 : | swathID int32 swath structure ID |
2124 : | fieldtype char geo/data fieldtype |
2125 : | fieldname char fieldname |
2126 : | dimlist char Dimension list (comma-separated list) |
2127 : | numbertype int32 field type |
2128 : | merge int32 merge code |
2129 : | |
2130 : | |
2131 : | OUTPUTS: |
2132 : | None |
2133 : | |
2134 : | NOTES: |
2135 : | |
2136 : | |
2137 : | Date Programmer Description |
2138 : | ====== ============ ================================================= |
2139 : | Jun 96 Joel Gales Original Programmer |
2140 : | Aug 96 Joel Gales Check name for length |
2141 : | Sep 96 Joel Gales Make string array "dimbuf" dynamic |
2142 : | Oct 96 Joel Gales Make sure total length of "merged" Vdata < 64 |
2143 : | Jun 03 Abe Taaheri Supplied cast comp_coder_t in call to SDsetcompress |
2144 : | |
2145 : | END_PROLOG |
2146 : -----------------------------------------------------------------------------*/
2147 : static intn
2148 0 : SWdefinefield(int32 swathID, char *fieldtype, char *fieldname, char *dimlist,
2149 : int32 numbertype, int32 merge)
2150 :
2151 : {
2152 : intn i; /* Loop index */
2153 : intn status; /* routine return status variable */
2154 : intn found; /* utility found flag */
2155 0 : intn foundNT = 0;/* found number type flag */
2156 0 : intn foundAllDim = 1; /* found all dimensions flag */
2157 0 : intn first = 1; /* first entry flag */
2158 : intn fac; /* Geo (-1), Data (+1) field factor */
2159 0 : int32 cnt = 0;
2160 :
2161 : int32 fid; /* HDF-EOS file ID */
2162 : int32 vdataID; /* Vdata ID */
2163 : int32 vgid; /* Geo/Data field Vgroup ID */
2164 : int32 sdInterfaceID; /* HDF SDS interface ID */
2165 : int32 sdid; /* SDS object ID */
2166 : int32 dimid; /* SDS dimension ID */
2167 : int32 recSize; /* Vdata record size */
2168 : int32 swVgrpID; /* Swath root Vgroup ID */
2169 : int32 dims[8]; /* Dimension size array */
2170 : int32 dimsize; /* Dimension size */
2171 0 : int32 rank = 0; /* Field rank */
2172 : int32 slen[32]; /* String length array */
2173 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
2174 : int32 compcode; /* Compression code */
2175 : int32 sID; /* SwathID - offset */
2176 :
2177 : uint8 *oneDbuf; /* Vdata record buffer */
2178 : char *dimbuf; /* Dimension buffer */
2179 : char *comma; /* Pointer to comma */
2180 : char *dimcheck; /* Dimension check buffer */
2181 : char utlbuf[512];/* Utility buffer */
2182 : char utlbuf2[256]; /* Utility buffer 2 */
2183 : char *ptr[32]; /* String pointer array */
2184 : char swathname[80]; /* Swath name */
2185 : char errbuf1[128]; /* Error message buffer 1 */
2186 : char errbuf2[128]; /* Error message buffer 2 */
2187 : char compparmbuf[128]; /* Compression parmeter string buffer */
2188 :
2189 : char *HDFcomp[5] = {"HDFE_COMP_NONE", "HDFE_COMP_RLE",
2190 : "HDFE_COMP_NBIT", "HDFE_COMP_SKPHUFF",
2191 0 : "HDFE_COMP_DEFLATE"};
2192 : /* Compression code names */
2193 :
2194 0 : uint16 good_number[10] = {3, 4, 5, 6, 20, 21, 22, 23, 24, 25};
2195 : /* Valid number types */
2196 : comp_info c_info; /* Compression parameter structure */
2197 :
2198 :
2199 :
2200 : /* Setup error message strings */
2201 : /* --------------------------- */
2202 0 : strcpy(errbuf1, "SWXSDname array too small.\nPlease increase ");
2203 0 : strcat(errbuf1, "size of HDFE_NAMBUFSIZE in \"HdfEosDef.h\".\n");
2204 0 : strcpy(errbuf2, "SWXSDdims array too small.\nPlease increase ");
2205 0 : strcat(errbuf2, "size of HDFE_DIMBUFSIZE in \"HdfEosDef.h\".\n");
2206 :
2207 :
2208 :
2209 : /*
2210 : * Check for proper swath ID and return HDF-EOS file ID, SDinterface ID,
2211 : * and swath root Vgroup ID
2212 : */
2213 0 : status = SWchkswid(swathID, "SWdefinefield",
2214 : &fid, &sdInterfaceID, &swVgrpID);
2215 :
2216 :
2217 0 : if (status == 0)
2218 : {
2219 : /* Remove offset from swath ID & get swathname */
2220 0 : sID = swathID % idOffset;
2221 0 : Vgetname(swVgrpID, swathname);
2222 :
2223 : /* Allocate space for dimbuf, copy dimlist into it, & append comma */
2224 0 : dimbuf = (char *) calloc(strlen(dimlist) + 64, 1);
2225 0 : if(dimbuf == NULL)
2226 : {
2227 0 : HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
2228 0 : return(-1);
2229 : }
2230 0 : strcpy(dimbuf, dimlist);
2231 0 : strcat(dimbuf, ",");
2232 :
2233 : /* Find comma */
2234 0 : comma = strchr(dimbuf, ',');
2235 :
2236 :
2237 : /*
2238 : * Loop through entries in dimension list to make sure they are
2239 : * defined in swath
2240 : */
2241 0 : while (comma != NULL)
2242 : {
2243 : /* Copy dimension list entry to dimcheck */
2244 0 : dimcheck = (char *) calloc(comma - dimbuf + 1, 1);
2245 0 : if(dimcheck == NULL)
2246 : {
2247 0 : HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
2248 0 : free(dimbuf);
2249 0 : return(-1);
2250 : }
2251 0 : memcpy(dimcheck, dimbuf, comma - dimbuf);
2252 :
2253 : /* Get dimension size */
2254 0 : dimsize = SWdiminfo(swathID, dimcheck);
2255 :
2256 : /* if != -1 then sent found flag, store size and increment rank */
2257 0 : if (dimsize != -1)
2258 : {
2259 0 : dims[rank] = dimsize;
2260 0 : rank++;
2261 : }
2262 : else
2263 : {
2264 : /*
2265 : * If dimension list entry not found - set error return
2266 : * status, append name to utility buffer for error report
2267 : */
2268 0 : status = -1;
2269 0 : foundAllDim = 0;
2270 0 : if (first == 1)
2271 : {
2272 0 : strcpy(utlbuf, dimcheck);
2273 : }
2274 : else
2275 : {
2276 0 : strcat(utlbuf, ",");
2277 0 : strcat(utlbuf, dimcheck);
2278 : }
2279 0 : first = 0;
2280 : }
2281 :
2282 : /*
2283 : * Go to next dimension entry, find next comma, & free up
2284 : * dimcheck buffer
2285 : */
2286 0 : *comma = '\0'; /* zero out first comma */
2287 0 : comma++;
2288 0 : comma = strchr(comma, ',');
2289 0 : if (comma != NULL)
2290 : {
2291 0 : for (i=0; i<strlen(dimcheck) + 1; i++)
2292 : {
2293 0 : dimbuf++;
2294 0 : cnt++;
2295 : }
2296 : }
2297 0 : free(dimcheck);
2298 : }
2299 0 : for(i=0; i<cnt; i++)
2300 0 : dimbuf--;
2301 :
2302 0 : free(dimbuf);
2303 :
2304 :
2305 : /* Check that UNLIMITED dimension is first dimension if present */
2306 : /* ------------------------------------------------------------ */
2307 0 : if (status == 0)
2308 : {
2309 0 : for (i = 0; i < rank; i++)
2310 : {
2311 0 : if (dims[i] == 0 && i != 0)
2312 : {
2313 0 : status = -1;
2314 0 : HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__);
2315 0 : HEreport("UNLIMITED dimension must be first dimension.\n");
2316 : }
2317 : }
2318 : }
2319 :
2320 :
2321 : /* Check fieldname length */
2322 : /* ---------------------- */
2323 0 : if (status == 0)
2324 : {
2325 : /* ((intn) strlen(fieldname) > MAX_NC_NAME - 7)
2326 : ** this was changed because HDF4.1r3 made a change in the
2327 : ** hlimits.h file. We have notidfied NCSA and asked to have
2328 : ** it made the same as in previous versions of HDF
2329 : ** see ncr 26314. DaW Apr 2000
2330 : */
2331 :
2332 0 : if (((intn) strlen(fieldname) > VSNAMELENMAX && rank == 1) ||
2333 0 : ((intn) strlen(fieldname) > (256 - 7) && rank > 1))
2334 : {
2335 0 : status = -1;
2336 0 : HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__);
2337 0 : HEreport("Fieldname \"%s\" too long.\n", fieldname);
2338 : }
2339 : }
2340 :
2341 :
2342 :
2343 :
2344 : /* Check for valid numbertype */
2345 : /* -------------------------- */
2346 0 : if (status == 0)
2347 : {
2348 0 : for (i = 0; i < 10; i++)
2349 : {
2350 0 : if (numbertype == good_number[i])
2351 : {
2352 0 : foundNT = 1;
2353 : }
2354 : }
2355 :
2356 0 : if (foundNT == 0)
2357 : {
2358 0 : HEpush(DFE_BADNUMTYPE, "SWdefinefield", __FILE__, __LINE__);
2359 0 : HEreport("Invalid number type: %d (%s).\n",
2360 : numbertype, fieldname);
2361 0 : status = -1;
2362 : }
2363 : }
2364 :
2365 :
2366 : /* Define Field */
2367 : /* ------------ */
2368 0 : if (status == 0)
2369 : {
2370 : /* Set factor & get Field Vgroup id */
2371 : /* -------------------------------- */
2372 0 : if (strcmp(fieldtype, "Geolocation Fields") == 0)
2373 : {
2374 0 : fac = -1;
2375 0 : vgid = SWXSwath[sID].VIDTable[0];
2376 : }
2377 : else
2378 : {
2379 0 : fac = +1;
2380 0 : vgid = SWXSwath[sID].VIDTable[1];
2381 : }
2382 : /*
2383 : * Note: "fac" is used to destinguish geo fields from data fields
2384 : * so that they are not merged together
2385 : */
2386 :
2387 :
2388 : /* One D Fields */
2389 : /* ------------ */
2390 0 : if (rank == 1)
2391 : {
2392 : /* No Compression for 1D (Vdata) fields */
2393 0 : compcode = HDFE_COMP_NONE;
2394 :
2395 :
2396 : /* If field non-appendable and merge set to AUTOMERGE ... */
2397 0 : if (dims[0] != 0 && merge == HDFE_AUTOMERGE)
2398 : {
2399 0 : i = 0;
2400 0 : found = 0;
2401 :
2402 : /* Loop through previous entries in 1d combination array */
2403 0 : while (SWX1dcomb[3 * i] != 0)
2404 : {
2405 : /* Get name of previous 1d combined field */
2406 0 : vdataID = SWX1dcomb[3 * i + 2];
2407 0 : VSgetname(vdataID, utlbuf);
2408 :
2409 : /*
2410 : * If dimension, field type (geo/data), and swath
2411 : * structure if current entry match a previous entry
2412 : * and combined name is less than max allowed then
2413 : * set "found" flag and exit loop
2414 : */
2415 0 : if (SWX1dcomb[3 * i] == fac * dims[0] &&
2416 0 : SWX1dcomb[3 * i + 1] == swVgrpID &&
2417 : (intn) strlen(utlbuf) +
2418 0 : (intn) strlen(fieldname) + 1 <=
2419 : VSNAMELENMAX)
2420 : {
2421 0 : found = 1;
2422 0 : break;
2423 : }
2424 : /* Increment loop index */
2425 0 : i++;
2426 : }
2427 :
2428 :
2429 0 : if (found == 0)
2430 : {
2431 : /*
2432 : * If no matching entry found then start new Vdata
2433 : * and store dimension size, swath root Vgroup ID,
2434 : * field Vdata and fieldname in external array
2435 : * "SWX1dcomb"
2436 : */
2437 0 : vdataID = VSattach(fid, -1, "w");
2438 0 : SWX1dcomb[3 * i] = fac * dims[0];
2439 0 : SWX1dcomb[3 * i + 1] = swVgrpID;
2440 0 : SWX1dcomb[3 * i + 2] = vdataID;
2441 0 : VSsetname(vdataID, fieldname);
2442 : }
2443 : else
2444 : {
2445 : /*
2446 : * If match then concatanate current fieldname to
2447 : * previous matching fieldnames
2448 : */
2449 0 : strcat(utlbuf, ",");
2450 0 : strcat(utlbuf, fieldname);
2451 0 : VSsetname(vdataID, utlbuf);
2452 : }
2453 :
2454 : /* Define field as field within Vdata */
2455 0 : VSfdefine(vdataID, fieldname, numbertype, 1);
2456 0 : Vinsert(vgid, vdataID);
2457 :
2458 : }
2459 : else
2460 : {
2461 : /* 1d No Merge Section */
2462 :
2463 : /* Get new vdata ID and establish field within Vdata */
2464 0 : vdataID = VSattach(fid, -1, "w");
2465 0 : VSsetname(vdataID, fieldname);
2466 0 : VSfdefine(vdataID, fieldname, numbertype, 1);
2467 0 : VSsetfields(vdataID, fieldname);
2468 :
2469 0 : recSize = VSsizeof(vdataID, fieldname);
2470 0 : if (dims[0] == 0)
2471 : {
2472 : /*
2473 : * If appendable field then write single record
2474 : * filled with 255
2475 : */
2476 0 : oneDbuf = (uint8 *) calloc(recSize, 1);
2477 0 : if(oneDbuf == NULL)
2478 : {
2479 0 : HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
2480 0 : return(-1);
2481 : }
2482 0 : for (i = 0; i < recSize; i++)
2483 0 : oneDbuf[i] = 255;
2484 0 : VSwrite(vdataID, oneDbuf, 1, FULL_INTERLACE);
2485 : }
2486 : else
2487 : {
2488 : /*
2489 : * If non-appendable then write entire field with
2490 : * blank records
2491 : */
2492 0 : oneDbuf = (uint8 *) calloc(recSize, dims[0]);
2493 0 : if(oneDbuf == NULL)
2494 : {
2495 0 : HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
2496 0 : return(-1);
2497 : }
2498 0 : VSwrite(vdataID, oneDbuf, dims[0], FULL_INTERLACE);
2499 : }
2500 0 : free(oneDbuf);
2501 :
2502 : /* Insert Vdata into field Vgroup & detach */
2503 0 : Vinsert(vgid, vdataID);
2504 0 : VSdetach(vdataID);
2505 :
2506 : } /* End No Merge Section */
2507 :
2508 : } /* End 1d field Section */
2509 : else
2510 : {
2511 : /* SDS Interface (Multi-dim fields) */
2512 : /* -------------------------------- */
2513 :
2514 : /* Get current compression code */
2515 0 : compcode = SWXSwath[sID].compcode;
2516 :
2517 : /*
2518 : * If rank is less than or equal to 3 (and greater than 1)
2519 : * and AUTOMERGE is set and the first dimension is not
2520 : * appendable and the compression code is set to none then
2521 : * ...
2522 : */
2523 0 : if (rank <= 3 && merge == HDFE_AUTOMERGE && dims[0] != 0
2524 : && compcode == HDFE_COMP_NONE)
2525 : {
2526 : /* Find first empty slot in external combination array */
2527 : /* --------------------------------------------------- */
2528 0 : i = 0;
2529 0 : while (SWXSDcomb[5 * i] != 0)
2530 : {
2531 0 : i++;
2532 : }
2533 :
2534 : /*
2535 : * Store dimensions (with geo/data factor), swath root
2536 : * Vgroup ID, and number type in external combination
2537 : * array "SWXSDcomb"
2538 : */
2539 :
2540 0 : if (rank == 2)
2541 : {
2542 : /* If 2-dim field then set lowest dimension to +/- 1 */
2543 0 : SWXSDcomb[5 * i] = fac;
2544 0 : SWXSDcomb[5 * i + 1] = fac * dims[0];
2545 0 : SWXSDcomb[5 * i + 2] = fac * dims[1];
2546 : }
2547 : else
2548 : {
2549 0 : SWXSDcomb[5 * i] = fac * dims[0];
2550 0 : SWXSDcomb[5 * i + 1] = fac * dims[1];
2551 0 : SWXSDcomb[5 * i + 2] = fac * dims[2];
2552 : }
2553 :
2554 0 : SWXSDcomb[5 * i + 3] = swVgrpID;
2555 0 : SWXSDcomb[5 * i + 4] = numbertype;
2556 :
2557 :
2558 : /* Concatanate fieldname with combined name string */
2559 : /* ----------------------------------------------- */
2560 0 : if ((intn) strlen(SWXSDname) +
2561 0 : (intn) strlen(fieldname) + 2 < HDFE_NAMBUFSIZE)
2562 : {
2563 0 : strcat(SWXSDname, fieldname);
2564 0 : strcat(SWXSDname, ",");
2565 : }
2566 : else
2567 : {
2568 : /* SWXSDname array too small! */
2569 : /* -------------------------- */
2570 0 : HEpush(DFE_GENAPP, "SWdefinefield",
2571 : __FILE__, __LINE__);
2572 0 : HEreport(errbuf1);
2573 0 : status = -1;
2574 0 : return (status);
2575 : }
2576 :
2577 :
2578 :
2579 : /*
2580 : * If 2-dim field then set lowest dimension (in 3-dim
2581 : * array) to "ONE"
2582 : */
2583 0 : if (rank == 2)
2584 : {
2585 0 : if ((intn) strlen(SWXSDdims) + 5 < HDFE_DIMBUFSIZE)
2586 : {
2587 0 : strcat(SWXSDdims, "ONE,");
2588 : }
2589 : else
2590 : {
2591 : /* SWXSDdims array too small! */
2592 : /* -------------------------- */
2593 0 : HEpush(DFE_GENAPP, "SWdefinefield",
2594 : __FILE__, __LINE__);
2595 0 : HEreport(errbuf2);
2596 0 : status = -1;
2597 0 : return (status);
2598 : }
2599 :
2600 : }
2601 :
2602 : /*
2603 : * Concatanate field dimlist to merged dimlist and
2604 : * separate fields with semi-colon
2605 : */
2606 0 : if ((intn) strlen(SWXSDdims) +
2607 0 : (intn) strlen(dimlist) + 2 < HDFE_DIMBUFSIZE)
2608 : {
2609 0 : strcat(SWXSDdims, dimlist);
2610 0 : strcat(SWXSDdims, ";");
2611 : }
2612 : else
2613 : {
2614 : /* SWXSDdims array too small! */
2615 : /* -------------------------- */
2616 0 : HEpush(DFE_GENAPP, "SWdefinefield",
2617 : __FILE__, __LINE__);
2618 0 : HEreport(errbuf2);
2619 0 : status = -1;
2620 0 : return (status);
2621 : }
2622 :
2623 : } /* End Multi-Dim Merge Section */
2624 : else
2625 : {
2626 : /* Multi-Dim No Merge Section */
2627 : /* ========================== */
2628 :
2629 : /* Create SDS dataset */
2630 : /* ------------------ */
2631 0 : sdid = SDcreate(sdInterfaceID, fieldname,
2632 : numbertype, rank, dims);
2633 :
2634 :
2635 : /* Store Dimension Names in SDS */
2636 : /* ---------------------------- */
2637 0 : rank = EHparsestr(dimlist, ',', ptr, slen);
2638 0 : for (i = 0; i < rank; i++)
2639 : {
2640 : /* Dimension name = Swathname:Dimname */
2641 0 : memcpy(utlbuf, ptr[i], slen[i]);
2642 0 : utlbuf[slen[i]] = 0;
2643 0 : strcat(utlbuf, ":");
2644 0 : strcat(utlbuf, swathname);
2645 :
2646 0 : dimid = SDgetdimid(sdid, i);
2647 0 : SDsetdimname(dimid, utlbuf);
2648 : }
2649 :
2650 :
2651 : /* Setup compression parameters */
2652 0 : if (compcode == HDFE_COMP_NBIT)
2653 : {
2654 0 : c_info.nbit.nt = numbertype;
2655 0 : c_info.nbit.sign_ext = SWXSwath[sID].compparm[0];
2656 0 : c_info.nbit.fill_one = SWXSwath[sID].compparm[1];
2657 0 : c_info.nbit.start_bit = SWXSwath[sID].compparm[2];
2658 0 : c_info.nbit.bit_len = SWXSwath[sID].compparm[3];
2659 : }
2660 0 : else if (compcode == HDFE_COMP_SKPHUFF)
2661 : {
2662 0 : c_info.skphuff.skp_size = (intn) DFKNTsize(numbertype);
2663 : }
2664 0 : else if (compcode == HDFE_COMP_DEFLATE)
2665 : {
2666 0 : c_info.deflate.level = SWXSwath[sID].compparm[0];
2667 : }
2668 :
2669 : /* If field is compressed then call SDsetcompress */
2670 : /* ---------------------------------------------- */
2671 0 : if (compcode != HDFE_COMP_NONE)
2672 : {
2673 0 : status = SDsetcompress(sdid, (comp_coder_t) compcode, &c_info);
2674 : }
2675 :
2676 :
2677 : /* Attach to Vgroup */
2678 : /* ---------------- */
2679 0 : Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid));
2680 :
2681 :
2682 : /* Store SDS dataset IDs */
2683 : /* --------------------- */
2684 0 : if (SWXSwath[sID].nSDS > 0)
2685 : {
2686 0 : SWXSwath[sID].sdsID = (int32 *)
2687 : realloc((void *) SWXSwath[sID].sdsID,
2688 0 : (SWXSwath[sID].nSDS + 1) * 4);
2689 0 : if(SWXSwath[sID].sdsID == NULL)
2690 : {
2691 0 : HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
2692 0 : return(-1);
2693 : }
2694 :
2695 : }
2696 : else
2697 : {
2698 0 : SWXSwath[sID].sdsID = (int32 *) calloc(1, 4);
2699 0 : if(SWXSwath[sID].sdsID == NULL)
2700 : {
2701 0 : HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
2702 0 : return(-1);
2703 : }
2704 : }
2705 0 : SWXSwath[sID].sdsID[SWXSwath[sID].nSDS] = sdid;
2706 0 : SWXSwath[sID].nSDS++;
2707 :
2708 : } /* End Multi-Dim No Merge Section */
2709 :
2710 : } /* End Multi-Dim Section */
2711 :
2712 :
2713 :
2714 : /* Setup metadata string */
2715 : /* --------------------- */
2716 0 : sprintf(utlbuf, "%s%s%s", fieldname, ":", dimlist);
2717 :
2718 :
2719 : /* Setup compression metadata */
2720 : /* -------------------------- */
2721 0 : if (compcode != HDFE_COMP_NONE)
2722 : {
2723 0 : sprintf(utlbuf2,
2724 : "%s%s",
2725 : ":\n\t\t\t\tCompressionType=", HDFcomp[compcode]);
2726 :
2727 0 : switch (compcode)
2728 : {
2729 : case HDFE_COMP_NBIT:
2730 :
2731 0 : sprintf(compparmbuf,
2732 : "%s%d,%d,%d,%d%s",
2733 : "\n\t\t\t\tCompressionParams=(",
2734 : SWXSwath[sID].compparm[0],
2735 : SWXSwath[sID].compparm[1],
2736 : SWXSwath[sID].compparm[2],
2737 : SWXSwath[sID].compparm[3], ")");
2738 0 : strcat(utlbuf2, compparmbuf);
2739 0 : break;
2740 :
2741 :
2742 : case HDFE_COMP_DEFLATE:
2743 :
2744 0 : sprintf(compparmbuf,
2745 : "%s%d",
2746 : "\n\t\t\t\tDeflateLevel=",
2747 : SWXSwath[sID].compparm[0]);
2748 0 : strcat(utlbuf2, compparmbuf);
2749 : break;
2750 : }
2751 :
2752 : /* Concatanate compression parameters with compression code */
2753 0 : strcat(utlbuf, utlbuf2);
2754 : }
2755 :
2756 :
2757 : /* Insert field metadata within File Structural Metadata */
2758 : /* ----------------------------------------------------- */
2759 0 : if (strcmp(fieldtype, "Geolocation Fields") == 0)
2760 : {
2761 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s", 3L,
2762 : utlbuf, &numbertype);
2763 : }
2764 : else
2765 : {
2766 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s", 4L,
2767 : utlbuf, &numbertype);
2768 : }
2769 :
2770 : }
2771 : }
2772 :
2773 : /* If all dimensions not found then report error */
2774 : /* --------------------------------------------- */
2775 0 : if (foundAllDim == 0)
2776 : {
2777 0 : HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__);
2778 0 : HEreport("Dimension(s): \"%s\" not found (%s).\n",
2779 : utlbuf, fieldname);
2780 0 : status = -1;
2781 : }
2782 :
2783 0 : return (status);
2784 : }
2785 :
2786 :
2787 :
2788 :
2789 : /*----------------------------------------------------------------------------|
2790 : | BEGIN_PROLOG |
2791 : | |
2792 : | FUNCTION: SWdefgeofield |
2793 : | |
2794 : | DESCRIPTION: Defines geolocation field within swath structure (wrapper) |
2795 : | |
2796 : | |
2797 : | Return Value Type Units Description |
2798 : | ============ ====== ========= ===================================== |
2799 : | status intn return status (0) SUCCEED, (-1) FAIL |
2800 : | |
2801 : | INPUTS: |
2802 : | swathID int32 swath structure ID |
2803 : | fieldname char fieldname |
2804 : | dimlist char Dimension list (comma-separated list) |
2805 : | numbertype int32 field type |
2806 : | merge int32 merge code |
2807 : | |
2808 : | OUTPUTS: |
2809 : | None |
2810 : | |
2811 : | NOTES: |
2812 : | |
2813 : | |
2814 : | Date Programmer Description |
2815 : | ====== ============ ================================================= |
2816 : | Jun 96 Joel Gales Original Programmer |
2817 : | |
2818 : | END_PROLOG |
2819 : -----------------------------------------------------------------------------*/
2820 : intn
2821 0 : SWdefgeofield(int32 swathID, char *fieldname, char *dimlist,
2822 : int32 numbertype, int32 merge)
2823 : {
2824 : intn status; /* routine return status variable */
2825 :
2826 : /* Call SWdefinefield routine */
2827 : /* -------------------------- */
2828 0 : status = SWdefinefield(swathID, "Geolocation Fields", fieldname, dimlist,
2829 : numbertype, merge);
2830 :
2831 0 : return (status);
2832 : }
2833 :
2834 :
2835 :
2836 :
2837 : /*----------------------------------------------------------------------------|
2838 : | BEGIN_PROLOG |
2839 : | |
2840 : | FUNCTION: SWdefdatafield |
2841 : | |
2842 : | DESCRIPTION: Defines data field within swath structure (wrapper) |
2843 : | |
2844 : | |
2845 : | Return Value Type Units Description |
2846 : | ============ ====== ========= ===================================== |
2847 : | status intn return status (0) SUCCEED, (-1) FAIL |
2848 : | |
2849 : | INPUTS: |
2850 : | swathID int32 swath structure ID |
2851 : | fieldname char fieldname |
2852 : | dimlist char Dimension list (comma-separated list) |
2853 : | numbertype int32 field type |
2854 : | merge int32 merge code |
2855 : | |
2856 : | OUTPUTS: |
2857 : | None |
2858 : | |
2859 : | NOTES: |
2860 : | |
2861 : | |
2862 : | Date Programmer Description |
2863 : | ====== ============ ================================================= |
2864 : | Jun 96 Joel Gales Original Programmer |
2865 : | |
2866 : | END_PROLOG |
2867 : -----------------------------------------------------------------------------*/
2868 : intn
2869 0 : SWdefdatafield(int32 swathID, char *fieldname, char *dimlist,
2870 : int32 numbertype, int32 merge)
2871 : {
2872 : intn status; /* routine return status variable */
2873 :
2874 : /* Call SWdefinefield routine */
2875 : /* -------------------------- */
2876 0 : status = SWdefinefield(swathID, "Data Fields", fieldname, dimlist,
2877 : numbertype, merge);
2878 :
2879 0 : return (status);
2880 : }
2881 :
2882 :
2883 :
2884 :
2885 :
2886 : /*----------------------------------------------------------------------------|
2887 : | BEGIN_PROLOG |
2888 : | |
2889 : | FUNCTION: SWwritegeometa |
2890 : | |
2891 : | DESCRIPTION: Defines structural metadata for pre-existing geolocation |
2892 : | field within swath structure |
2893 : | |
2894 : | |
2895 : | Return Value Type Units Description |
2896 : | ============ ====== ========= ===================================== |
2897 : | status intn return status (0) SUCCEED, (-1) FAIL |
2898 : | |
2899 : | INPUTS: |
2900 : | swathID int32 swath structure ID |
2901 : | fieldname char fieldname |
2902 : | dimlist char Dimension list (comma-separated list) |
2903 : | numbertype int32 field type |
2904 : | |
2905 : | |
2906 : | OUTPUTS: |
2907 : | None |
2908 : | |
2909 : | NOTES: |
2910 : | |
2911 : | |
2912 : | Date Programmer Description |
2913 : | ====== ============ ================================================= |
2914 : | Jun 96 Joel Gales Original Programmer |
2915 : | |
2916 : | END_PROLOG |
2917 : -----------------------------------------------------------------------------*/
2918 : intn
2919 0 : SWwritegeometa(int32 swathID, char *fieldname, char *dimlist,
2920 : int32 numbertype)
2921 : {
2922 0 : intn status = 0; /* routine return status variable */
2923 :
2924 : int32 sdInterfaceID; /* HDF SDS interface ID */
2925 : int32 dum; /* dummy variable */
2926 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
2927 :
2928 : char utlbuf[256];/* Utility buffer */
2929 : char swathname[80]; /* Swath name */
2930 :
2931 0 : status = SWchkswid(swathID, "SWwritegeometa", &dum, &sdInterfaceID,
2932 : &dum);
2933 :
2934 0 : if (status == 0)
2935 : {
2936 : /* Setup and write field metadata */
2937 : /* ------------------------------ */
2938 0 : sprintf(utlbuf, "%s%s%s", fieldname, ":", dimlist);
2939 :
2940 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
2941 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s", 3L,
2942 : utlbuf, &numbertype);
2943 : }
2944 :
2945 0 : return (status);
2946 : }
2947 :
2948 :
2949 :
2950 :
2951 : /*----------------------------------------------------------------------------|
2952 : | BEGIN_PROLOG |
2953 : | |
2954 : | FUNCTION: SWwritedatameta |
2955 : | |
2956 : | DESCRIPTION: Defines structural metadata for pre-existing data |
2957 : | field within swath structure |
2958 : | |
2959 : | |
2960 : | Return Value Type Units Description |
2961 : | ============ ====== ========= ===================================== |
2962 : | status intn return status (0) SUCCEED, (-1) FAIL |
2963 : | |
2964 : | INPUTS: |
2965 : | swathID int32 swath structure ID |
2966 : | fieldname char fieldname |
2967 : | dimlist char Dimension list (comma-separated list) |
2968 : | numbertype int32 field type |
2969 : | |
2970 : | |
2971 : | OUTPUTS: |
2972 : | None |
2973 : | |
2974 : | NOTES: |
2975 : | |
2976 : | |
2977 : | Date Programmer Description |
2978 : | ====== ============ ================================================= |
2979 : | Jun 96 Joel Gales Original Programmer |
2980 : | |
2981 : | END_PROLOG |
2982 : -----------------------------------------------------------------------------*/
2983 : intn
2984 0 : SWwritedatameta(int32 swathID, char *fieldname, char *dimlist,
2985 : int32 numbertype)
2986 : {
2987 0 : intn status = 0; /* routine return status variable */
2988 :
2989 : int32 sdInterfaceID; /* HDF SDS interface ID */
2990 : int32 dum; /* dummy variable */
2991 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
2992 :
2993 : char utlbuf[256];/* Utility buffer */
2994 : char swathname[80]; /* Swath name */
2995 :
2996 0 : status = SWchkswid(swathID, "SWwritedatameta", &dum, &sdInterfaceID,
2997 : &dum);
2998 :
2999 0 : if (status == 0)
3000 : {
3001 : /* Setup and write field metadata */
3002 : /* ------------------------------ */
3003 0 : sprintf(utlbuf, "%s%s%s", fieldname, ":", dimlist);
3004 :
3005 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
3006 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s", 4L,
3007 : utlbuf, &numbertype);
3008 : }
3009 0 : return (status);
3010 : }
3011 :
3012 :
3013 :
3014 : /*----------------------------------------------------------------------------|
3015 : | BEGIN_PROLOG |
3016 : | |
3017 : | FUNCTION: SWwrrdattr |
3018 : | |
3019 : | DESCRIPTION: |
3020 : | |
3021 : | |
3022 : | Return Value Type Units Description |
3023 : | ============ ====== ========= ===================================== |
3024 : | status intn return status (0) SUCCEED, (-1) FAIL |
3025 : | |
3026 : | INPUTS: |
3027 : | swathID int32 swath structure ID |
3028 : | attrname char attribute name |
3029 : | numbertype int32 attribute HDF numbertype |
3030 : | count int32 Number of attribute elements |
3031 : | wrcode char Read/Write Code "w/r" |
3032 : | datbuf void I/O buffer |
3033 : | |
3034 : | OUTPUTS: |
3035 : | datbuf |
3036 : | |
3037 : | NOTES: |
3038 : | |
3039 : | |
3040 : | Date Programmer Description |
3041 : | ====== ============ ================================================= |
3042 : | Jun 96 Joel Gales Original Programmer |
3043 : | Oct 96 Joel Gales Get Attribute Vgroup ID from external array |
3044 : | |
3045 : | END_PROLOG |
3046 : -----------------------------------------------------------------------------*/
3047 : static intn
3048 0 : SWwrrdattr(int32 swathID, char *attrname, int32 numbertype, int32 count,
3049 : char *wrcode, VOIDP datbuf)
3050 :
3051 : {
3052 : intn status; /* routine return status variable */
3053 :
3054 : int32 fid; /* HDF-EOS file ID */
3055 : int32 attrVgrpID; /* Swath attribute ID */
3056 : int32 dum; /* dummy variable */
3057 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
3058 :
3059 : /* Check Swath id */
3060 0 : status = SWchkswid(swathID, "SWwrrdattr", &fid, &dum, &dum);
3061 :
3062 0 : if (status == 0)
3063 : {
3064 : /* Get attribute Vgroup ID and call EHattr to perform I/O */
3065 : /* ------------------------------------------------------ */
3066 0 : attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
3067 0 : status = EHattr(fid, attrVgrpID, attrname, numbertype, count,
3068 : wrcode, datbuf);
3069 : }
3070 0 : return (status);
3071 : }
3072 :
3073 :
3074 :
3075 : /*----------------------------------------------------------------------------|
3076 : | BEGIN_PROLOG |
3077 : | |
3078 : | FUNCTION: SWwriteattr |
3079 : | |
3080 : | DESCRIPTION: Writes/updates attribute in a swath. |
3081 : | |
3082 : | |
3083 : | Return Value Type Units Description |
3084 : | ============ ====== ========= ===================================== |
3085 : | status intn return status (0) SUCCEED, (-1) FAIL |
3086 : | |
3087 : | INPUTS: |
3088 : | swathID int32 swath structure ID |
3089 : | attrname char attribute name |
3090 : | numbertype int32 attribute HDF numbertype |
3091 : | count int32 Number of attribute elements |
3092 : | datbuf void I/O buffer |
3093 : | |
3094 : | OUTPUTS: |
3095 : | None |
3096 : | |
3097 : | NOTES: |
3098 : | |
3099 : | |
3100 : | Date Programmer Description |
3101 : | ====== ============ ================================================= |
3102 : | Jun 96 Joel Gales Original Programmer |
3103 : | |
3104 : | END_PROLOG |
3105 : -----------------------------------------------------------------------------*/
3106 : intn
3107 0 : SWwriteattr(int32 swathID, char *attrname, int32 numbertype, int32 count,
3108 : VOIDP datbuf)
3109 : {
3110 0 : intn status = 0; /* routine return status variable */
3111 :
3112 : /* Call SWwrrdattr routine to write attribute */
3113 : /* ------------------------------------------ */
3114 0 : status = SWwrrdattr(swathID, attrname, numbertype, count, "w", datbuf);
3115 :
3116 0 : return (status);
3117 : }
3118 :
3119 :
3120 :
3121 : /*----------------------------------------------------------------------------|
3122 : | BEGIN_PROLOG |
3123 : | |
3124 : | FUNCTION: SWreadattr |
3125 : | |
3126 : | DESCRIPTION: Reads attribute from a swath. |
3127 : | |
3128 : | |
3129 : | Return Value Type Units Description |
3130 : | ============ ====== ========= ===================================== |
3131 : | status intn return status (0) SUCCEED, (-1) FAIL |
3132 : | |
3133 : | INPUTS: |
3134 : | swathID int32 swath structure ID |
3135 : | attrname char attribute name |
3136 : | |
3137 : | OUTPUTS: |
3138 : | datbuf void I/O buffer |
3139 : | |
3140 : | NOTES: |
3141 : | |
3142 : | |
3143 : | Date Programmer Description |
3144 : | ====== ============ ================================================= |
3145 : | Jun 96 Joel Gales Original Programmer |
3146 : | |
3147 : | END_PROLOG |
3148 : -----------------------------------------------------------------------------*/
3149 : intn
3150 0 : SWreadattr(int32 swathID, char *attrname, VOIDP datbuf)
3151 : {
3152 0 : intn status = 0; /* routine return status variable */
3153 0 : int32 dum = 0; /* dummy variable */
3154 :
3155 : /* Call SWwrrdattr routine to read attribute */
3156 : /* ----------------------------------------- */
3157 0 : status = SWwrrdattr(swathID, attrname, dum, dum, "r", datbuf);
3158 :
3159 0 : return (status);
3160 : }
3161 :
3162 :
3163 :
3164 :
3165 :
3166 : /*----------------------------------------------------------------------------|
3167 : | BEGIN_PROLOG |
3168 : | |
3169 : | FUNCTION: SWattrinfo |
3170 : | |
3171 : | DESCRIPTION: |
3172 : | |
3173 : | |
3174 : | Return Value Type Units Description |
3175 : | ============ ====== ========= ===================================== |
3176 : | status intn return status (0) SUCCEED, (-1) FAIL |
3177 : | |
3178 : | INPUTS: |
3179 : | swathID int32 swath structure ID |
3180 : | attrname char attribute name |
3181 : | |
3182 : | OUTPUTS: |
3183 : | numbertype int32 attribute HDF numbertype |
3184 : | count int32 Number of attribute elements |
3185 : | |
3186 : | |
3187 : | OUTPUTS: |
3188 : | None |
3189 : | |
3190 : | NOTES: |
3191 : | |
3192 : | |
3193 : | Date Programmer Description |
3194 : | ====== ============ ================================================= |
3195 : | Jun 96 Joel Gales Original Programmer |
3196 : | Oct 96 Joel Gales Get Attribute Vgroup ID from external array |
3197 : | |
3198 : | END_PROLOG |
3199 : -----------------------------------------------------------------------------*/
3200 : intn
3201 0 : SWattrinfo(int32 swathID, char *attrname, int32 * numbertype, int32 * count)
3202 : {
3203 0 : intn status = 0; /* routine return status variable */
3204 :
3205 : int32 fid; /* HDF-EOS file ID */
3206 : int32 attrVgrpID; /* Swath attribute ID */
3207 : int32 dum; /* dummy variable */
3208 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
3209 :
3210 : /* Check for valid swath ID */
3211 : /* ------------------------ */
3212 0 : status = SWchkswid(swathID, "SWattrinfo", &fid, &dum, &dum);
3213 :
3214 0 : if (status == 0)
3215 : {
3216 : /* Get attribute Vgroup ID and call EHattrinfo */
3217 : /* ------------------------------------------- */
3218 0 : attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
3219 :
3220 0 : status = EHattrinfo(fid, attrVgrpID, attrname, numbertype,
3221 : count);
3222 : }
3223 0 : return (status);
3224 : }
3225 :
3226 :
3227 :
3228 :
3229 :
3230 :
3231 : /*----------------------------------------------------------------------------|
3232 : | BEGIN_PROLOG |
3233 : | |
3234 : | FUNCTION: SWinqattrs |
3235 : | |
3236 : | DESCRIPTION: |
3237 : | |
3238 : | |
3239 : | Return Value Type Units Description |
3240 : | ============ ====== ========= ===================================== |
3241 : | nattr int32 Number of attributes in swath struct |
3242 : | |
3243 : | INPUTS: |
3244 : | swath ID int32 swath structure ID |
3245 : | |
3246 : | OUTPUTS: |
3247 : | attrnames char Attribute names in swath struct |
3248 : | (Comma-separated list) |
3249 : | strbufsize int32 Attributes name list string length |
3250 : | |
3251 : | OUTPUTS: |
3252 : | None |
3253 : | |
3254 : | NOTES: |
3255 : | |
3256 : | |
3257 : | Date Programmer Description |
3258 : | ====== ============ ================================================= |
3259 : | Jun 96 Joel Gales Original Programmer |
3260 : | Oct 96 Joel Gales Initialize nattr |
3261 : | Oct 96 Joel Gales Get Attribute Vgroup ID from external array |
3262 : | |
3263 : | END_PROLOG |
3264 : -----------------------------------------------------------------------------*/
3265 : int32
3266 0 : SWinqattrs(int32 swathID, char *attrnames, int32 * strbufsize)
3267 : {
3268 : intn status; /* routine return status variable */
3269 :
3270 : int32 fid; /* HDF-EOS file ID */
3271 : int32 attrVgrpID; /* Swath attribute ID */
3272 : int32 dum; /* dummy variable */
3273 0 : int32 nattr = 0; /* Number of attributes */
3274 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
3275 :
3276 : /* Check Swath id */
3277 0 : status = SWchkswid(swathID, "SWinqattrs", &fid, &dum, &dum);
3278 :
3279 0 : if (status == 0)
3280 : {
3281 : /* Get attribute Vgroup ID and call EHattrcat */
3282 : /* ------------------------------------------ */
3283 0 : attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
3284 :
3285 0 : nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize);
3286 : }
3287 :
3288 0 : return (nattr);
3289 : }
3290 :
3291 : #define REMQUOTE \
3292 : \
3293 : memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); \
3294 : utlstr[strlen(utlstr) - 2] = 0;
3295 :
3296 :
3297 : /*----------------------------------------------------------------------------|
3298 : | BEGIN_PROLOG |
3299 : | |
3300 : | FUNCTION: SWinqdims |
3301 : | |
3302 : | DESCRIPTION: Returns dimension names and values defined in swath structure |
3303 : | |
3304 : | |
3305 : | Return Value Type Units Description |
3306 : | ============ ====== ========= ===================================== |
3307 : | nDim int32 Number of defined dimensions |
3308 : | |
3309 : | INPUTS: |
3310 : | swathID int32 swath structure ID |
3311 : | |
3312 : | OUTPUTS: |
3313 : | dimnames char Dimension names (comma-separated) |
3314 : | dims int32 Dimension values |
3315 : | |
3316 : | NOTES: |
3317 : | |
3318 : | |
3319 : | Date Programmer Description |
3320 : | ====== ============ ================================================= |
3321 : | Jun 96 Joel Gales Original Programmer |
3322 : | Aug 96 Joel Gales Make metadata ODL compliant |
3323 : | Feb 97 Joel Gales Set nDim to -1 if status = -1 |
3324 : | |
3325 : | END_PROLOG |
3326 : -----------------------------------------------------------------------------*/
3327 : int32
3328 0 : SWinqdims(int32 swathID, char *dimnames, int32 dims[])
3329 :
3330 : {
3331 : intn status; /* routine return status variable */
3332 :
3333 : int32 fid; /* HDF-EOS file ID */
3334 : int32 sdInterfaceID; /* HDF SDS interface ID */
3335 : int32 swVgrpID; /* Swath root Vgroup ID */
3336 : int32 size; /* Dimension size */
3337 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
3338 0 : int32 nDim = 0; /* Number of dimensions */
3339 :
3340 : char *metabuf; /* Pointer to structural metadata (SM) */
3341 : char *metaptrs[2];/* Pointers to begin and end of SM section */
3342 : char swathname[80]; /* Swath Name */
3343 : char *utlstr; /* Utility string */
3344 :
3345 :
3346 : /* Allocate space for utility string */
3347 : /* --------------------------------- */
3348 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
3349 0 : if(utlstr == NULL)
3350 : {
3351 0 : HEpush(DFE_NOSPACE,"SWinqdims", __FILE__, __LINE__);
3352 0 : return(-1);
3353 : }
3354 :
3355 : /* Check for valid swath id */
3356 0 : status = SWchkswid(swathID, "SWinqdims", &fid, &sdInterfaceID, &swVgrpID);
3357 :
3358 0 : if (status == 0)
3359 : {
3360 : /* If dimension names or sizes are desired ... */
3361 : /* ------------------------------------------- */
3362 0 : if (dimnames != NULL || dims != NULL)
3363 : {
3364 : /* Get swath name */
3365 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
3366 :
3367 : /* Get pointers to "Dimension" section within SM */
3368 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
3369 : "Dimension", metaptrs);
3370 0 : if(metabuf == NULL)
3371 : {
3372 0 : free(utlstr);
3373 0 : return(-1);
3374 : }
3375 : /* If dimension names are desired then "clear" name buffer */
3376 0 : if (dimnames != NULL)
3377 : {
3378 0 : dimnames[0] = 0;
3379 : }
3380 :
3381 :
3382 : /* Begin loop through dimension entries in metadata */
3383 : /* ------------------------------------------------ */
3384 : while (1)
3385 : {
3386 : /* Search for OBJECT string */
3387 0 : metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
3388 :
3389 : /* If found within "Dimension" metadata section ... */
3390 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
3391 : {
3392 : /* Get Dimension Name (if desired) */
3393 0 : if (dimnames != NULL)
3394 : {
3395 : /* Check 1st for old meta data then new */
3396 : /* ------------------------------------ */
3397 0 : EHgetmetavalue(metaptrs, "OBJECT", utlstr);
3398 :
3399 : /*
3400 : * If OBJECT value begins with double quote then old
3401 : * metadata, dimension name is OBJECT value.
3402 : * Otherwise search for "DimensionName" string
3403 : */
3404 0 : if (utlstr[0] != '"')
3405 : {
3406 0 : metaptrs[0] =
3407 0 : strstr(metaptrs[0], "\t\t\t\tDimensionName=");
3408 0 : EHgetmetavalue(metaptrs, "DimensionName", utlstr);
3409 : }
3410 :
3411 : /* Strip off double quotes */
3412 : /* ----------------------- */
3413 0 : REMQUOTE
3414 :
3415 : /* If not first name then add comma delimitor */
3416 0 : if (nDim > 0)
3417 : {
3418 0 : strcat(dimnames, ",");
3419 : }
3420 : /* Add dimension name to dimension list */
3421 0 : strcat(dimnames, utlstr);
3422 : }
3423 :
3424 : /* Get Dimension Size (if desired) */
3425 0 : if (dims != NULL)
3426 : {
3427 0 : EHgetmetavalue(metaptrs, "Size", utlstr);
3428 0 : size = atol(utlstr);
3429 0 : dims[nDim] = size;
3430 : }
3431 : /* Increment number of dimensions */
3432 0 : nDim++;
3433 : }
3434 : else
3435 : /* No more dimensions found */
3436 : {
3437 : break;
3438 : }
3439 0 : }
3440 0 : free(metabuf);
3441 : }
3442 : }
3443 :
3444 :
3445 : /* Set nDim to -1 if error status exists */
3446 : /* ------------------------------------- */
3447 0 : if (status == -1)
3448 : {
3449 0 : nDim = -1;
3450 : }
3451 0 : free(utlstr);
3452 :
3453 0 : return (nDim);
3454 : }
3455 :
3456 :
3457 :
3458 :
3459 :
3460 : /*----------------------------------------------------------------------------|
3461 : | BEGIN_PROLOG |
3462 : | |
3463 : | FUNCTION: SWinqmaps |
3464 : | |
3465 : | DESCRIPTION: Returns dimension mappings and offsets and increments |
3466 : | |
3467 : | |
3468 : | Return Value Type Units Description |
3469 : | ============ ====== ========= ===================================== |
3470 : | nMap int32 Number of dimension mappings |
3471 : | |
3472 : | INPUTS: |
3473 : | swathID int32 swath structure ID |
3474 : | |
3475 : | OUTPUTS: |
3476 : | dimmaps char dimension mappings (comma-separated) |
3477 : | offset int32 array of offsets |
3478 : | increment int32 array of increments |
3479 : | |
3480 : | NOTES: |
3481 : | |
3482 : | |
3483 : | Date Programmer Description |
3484 : | ====== ============ ================================================= |
3485 : | Jun 96 Joel Gales Original Programmer |
3486 : | Aug 96 Joel Gales Make metadata ODL compliant |
3487 : | Feb 97 Joel Gales Set nMap to -1 if status = -1 |
3488 : | |
3489 : | END_PROLOG |
3490 : -----------------------------------------------------------------------------*/
3491 : int32
3492 0 : SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[])
3493 :
3494 : {
3495 : intn status; /* routine return status variable */
3496 :
3497 : int32 fid; /* HDF-EOS file ID */
3498 : int32 sdInterfaceID; /* HDF SDS interface ID */
3499 : int32 swVgrpID; /* Swath root Vgroup ID */
3500 : int32 off; /* Mapping Offset */
3501 : int32 incr; /* Mapping Increment */
3502 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
3503 0 : int32 nMap = 0; /* Number of mappings */
3504 :
3505 : char *metabuf; /* Pointer to structural metadata (SM) */
3506 : char *metaptrs[2];/* Pointers to begin and end of SM section */
3507 : char swathname[80]; /* Swath Name */
3508 : char *utlstr; /* Utility string */
3509 :
3510 :
3511 : /* Allocate space for utility string */
3512 : /* --------------------------------- */
3513 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
3514 0 : if(utlstr == NULL)
3515 : {
3516 0 : HEpush(DFE_NOSPACE,"SWinqmaps", __FILE__, __LINE__);
3517 0 : return(-1);
3518 : }
3519 :
3520 : /* Check for valid swath id */
3521 0 : status = SWchkswid(swathID, "SWinqmaps", &fid, &sdInterfaceID, &swVgrpID);
3522 0 : if (status == 0)
3523 : {
3524 : /* If mapping names or offsets or increments desired ... */
3525 : /* ----------------------------------------------------- */
3526 0 : if (dimmaps != NULL || offset != NULL || increment != NULL)
3527 : {
3528 :
3529 : /* Get swath name */
3530 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
3531 :
3532 : /* Get pointers to "DimensionMap" section within SM */
3533 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
3534 : "DimensionMap", metaptrs);
3535 0 : if(metabuf == NULL)
3536 : {
3537 0 : free(utlstr);
3538 0 : return(-1);
3539 : }
3540 : /* If mapping names are desired then "clear" name buffer */
3541 0 : if (dimmaps != NULL)
3542 : {
3543 0 : dimmaps[0] = 0;
3544 : }
3545 :
3546 : /* Begin loop through mapping entries in metadata */
3547 : /* ---------------------------------------------- */
3548 : while (1)
3549 : {
3550 : /* Search for OBJECT string */
3551 0 : metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
3552 :
3553 : /* If found within "DimensionMap" metadata section ... */
3554 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
3555 : {
3556 : /* Get Geo & Data Dimensions (if desired) */
3557 0 : if (dimmaps != NULL)
3558 : {
3559 : /* Get Geo Dim, remove quotes, add "/" */
3560 0 : EHgetmetavalue(metaptrs, "GeoDimension", utlstr);
3561 0 : REMQUOTE
3562 0 : strcat(utlstr, "/");
3563 :
3564 : /* if not first map then add comma delimitor */
3565 0 : if (nMap > 0)
3566 : {
3567 0 : strcat(dimmaps, ",");
3568 : }
3569 :
3570 : /* Add to map list */
3571 0 : strcat(dimmaps, utlstr);
3572 :
3573 : /* Get Data Dim, remove quotes */
3574 0 : EHgetmetavalue(metaptrs, "DataDimension", utlstr);
3575 0 : REMQUOTE
3576 :
3577 : /* Add to map list */
3578 0 : strcat(dimmaps, utlstr);
3579 : }
3580 :
3581 : /* Get Offset (if desired) */
3582 0 : if (offset != NULL)
3583 : {
3584 0 : EHgetmetavalue(metaptrs, "Offset", utlstr);
3585 0 : off = atol(utlstr);
3586 0 : offset[nMap] = off;
3587 : }
3588 :
3589 : /* Get Increment (if desired) */
3590 0 : if (increment != NULL)
3591 : {
3592 0 : EHgetmetavalue(metaptrs, "Increment", utlstr);
3593 0 : incr = atol(utlstr);
3594 0 : increment[nMap] = incr;
3595 : }
3596 :
3597 : /* Increment number of maps */
3598 0 : nMap++;
3599 : }
3600 : else
3601 : /* No more mappings found */
3602 : {
3603 : break;
3604 : }
3605 0 : }
3606 0 : free(metabuf);
3607 : }
3608 : }
3609 :
3610 :
3611 : /* Set nMap to -1 if error status exists */
3612 : /* ------------------------------------- */
3613 0 : if (status == -1)
3614 : {
3615 0 : nMap = -1;
3616 : }
3617 0 : free(utlstr);
3618 :
3619 0 : return (nMap);
3620 : }
3621 :
3622 :
3623 :
3624 :
3625 :
3626 : /*----------------------------------------------------------------------------|
3627 : | BEGIN_PROLOG |
3628 : | |
3629 : | FUNCTION: SWinqidxmaps |
3630 : | |
3631 : | DESCRIPTION: Returns indexed mappings and index sizes |
3632 : | |
3633 : | |
3634 : | Return Value Type Units Description |
3635 : | ============ ====== ========= ===================================== |
3636 : | nMap int32 Number of indexed dimension mappings |
3637 : | |
3638 : | INPUTS: |
3639 : | swathID int32 swath structure ID |
3640 : | |
3641 : | OUTPUTS: |
3642 : | idxmaps char indexed dimension mappings |
3643 : | (comma-separated) |
3644 : | idxsizes int32 Number of elements in each mapping |
3645 : | |
3646 : | |
3647 : | NOTES: |
3648 : | |
3649 : | |
3650 : | Date Programmer Description |
3651 : | ====== ============ ================================================= |
3652 : | Jun 96 Joel Gales Original Programmer |
3653 : | Aug 96 Joel Gales Make metadata ODL compliant |
3654 : | Feb 97 Joel Gales Set nMap to -1 if status = -1 |
3655 : | |
3656 : | END_PROLOG |
3657 : -----------------------------------------------------------------------------*/
3658 : int32
3659 0 : SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[])
3660 :
3661 : {
3662 : intn status; /* routine return status variable */
3663 :
3664 : int32 fid; /* HDF-EOS file ID */
3665 : int32 sdInterfaceID; /* HDF SDS interface ID */
3666 : int32 swVgrpID; /* Swath root Vgroup ID */
3667 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
3668 0 : int32 nMap = 0; /* Number of mappings */
3669 :
3670 : char *metabuf; /* Pointer to structural metadata (SM) */
3671 : char *metaptrs[2];/* Pointers to begin and end of SM section */
3672 : char swathname[80]; /* Swath Name */
3673 : char *utlstr; /* Utility string */
3674 : char *slash; /* Pointer to slash */
3675 :
3676 :
3677 : /* Allocate space for utility string */
3678 : /* --------------------------------- */
3679 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
3680 0 : if(utlstr == NULL)
3681 : {
3682 0 : HEpush(DFE_NOSPACE,"SWinqidxmaps", __FILE__, __LINE__);
3683 0 : return(-1);
3684 : }
3685 : /* Check for valid swath id */
3686 0 : status = SWchkswid(swathID, "SWinqidxmaps", &fid,
3687 : &sdInterfaceID, &swVgrpID);
3688 :
3689 0 : if (status == 0)
3690 : {
3691 : /* If mapping names or index sizes desired ... */
3692 : /* ------------------------------------------- */
3693 0 : if (idxmaps != NULL || idxsizes != NULL)
3694 : {
3695 : /* Get swath name */
3696 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
3697 :
3698 : /* Get pointers to "IndexDimensionMap" section within SM */
3699 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
3700 : "IndexDimensionMap", metaptrs);
3701 0 : if(metabuf == NULL)
3702 : {
3703 0 : free(utlstr);
3704 0 : return(-1);
3705 : }
3706 : /* If mapping names are desired then "clear" name buffer */
3707 0 : if (idxmaps != NULL)
3708 : {
3709 0 : idxmaps[0] = 0;
3710 : }
3711 :
3712 : /* Begin loop through mapping entries in metadata */
3713 : /* ---------------------------------------------- */
3714 : while (1)
3715 : {
3716 : /* Search for OBJECT string */
3717 0 : metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
3718 :
3719 : /* If found within "IndexDimensionMap" metadata section ... */
3720 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
3721 : {
3722 : /* Get Geo & Data Dimensions and # of indices */
3723 0 : if (idxmaps != NULL)
3724 : {
3725 : /* Get Geo Dim, remove quotes, add "/" */
3726 0 : EHgetmetavalue(metaptrs, "GeoDimension", utlstr);
3727 0 : REMQUOTE
3728 0 : strcat(utlstr, "/");
3729 :
3730 : /* if not first map then add comma delimitor */
3731 0 : if (nMap > 0)
3732 : {
3733 0 : strcat(idxmaps, ",");
3734 : }
3735 :
3736 : /* Add to map list */
3737 0 : strcat(idxmaps, utlstr);
3738 :
3739 :
3740 : /* Get Index size (if desired) */
3741 0 : if (idxsizes != NULL)
3742 : {
3743 : /* Parse off geo dimension and find its size */
3744 0 : slash = strchr(utlstr, '/');
3745 0 : *slash = 0;
3746 0 : idxsizes[nMap] = SWdiminfo(swathID, utlstr);
3747 : }
3748 :
3749 :
3750 : /* Get Data Dim, remove quotes */
3751 0 : EHgetmetavalue(metaptrs, "DataDimension", utlstr);
3752 0 : REMQUOTE
3753 :
3754 : /* Add to map list */
3755 0 : strcat(idxmaps, utlstr);
3756 : }
3757 :
3758 : /* Increment number of maps */
3759 0 : nMap++;
3760 : }
3761 : else
3762 : /* No more mappings found */
3763 : {
3764 : break;
3765 : }
3766 0 : }
3767 0 : free(metabuf);
3768 : }
3769 : }
3770 :
3771 :
3772 : /* Set nMap to -1 if error status exists */
3773 : /* ------------------------------------- */
3774 0 : if (status == -1)
3775 : {
3776 0 : nMap = -1;
3777 : }
3778 0 : free(utlstr);
3779 :
3780 0 : return (nMap);
3781 : }
3782 :
3783 :
3784 :
3785 :
3786 : /*----------------------------------------------------------------------------|
3787 : | BEGIN_PROLOG |
3788 : | |
3789 : | FUNCTION: SWinqfields |
3790 : | |
3791 : | DESCRIPTION: Returns fieldnames, ranks and numbertypes defined in swath. |
3792 : | |
3793 : | |
3794 : | Return Value Type Units Description |
3795 : | ============ ====== ========= ===================================== |
3796 : | nFld int32 Number of (geo/data) fields in swath |
3797 : | |
3798 : | INPUTS: |
3799 : | swathID int32 swath structure ID |
3800 : | fieldtype char field type (geo or data) |
3801 : | |
3802 : | |
3803 : | OUTPUTS: |
3804 : | fieldlist char Field names (comma-separated) |
3805 : | rank int32 Array of ranks |
3806 : | numbertype int32 Array of HDF number types |
3807 : | |
3808 : | NOTES: |
3809 : | |
3810 : | |
3811 : | Date Programmer Description |
3812 : | ====== ============ ================================================= |
3813 : | Jun 96 Joel Gales Original Programmer |
3814 : | Aug 96 Joel Gales Make metadata ODL compliant |
3815 : | Feb 97 Joel Gales Set nFld to -1 if status = -1 |
3816 : | |
3817 : | END_PROLOG |
3818 : -----------------------------------------------------------------------------*/
3819 : static int32
3820 0 : SWinqfields(int32 swathID, char *fieldtype, char *fieldlist, int32 rank[],
3821 : int32 numbertype[])
3822 :
3823 : {
3824 : intn status; /* routine return status variable */
3825 :
3826 : int32 fid; /* HDF-EOS file ID */
3827 : int32 sdInterfaceID; /* HDF SDS interface ID */
3828 : int32 swVgrpID; /* Swath root Vgroup ID */
3829 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
3830 0 : int32 nFld = 0; /* Number of mappings */
3831 : int32 slen[8]; /* String length array */
3832 :
3833 : char *metabuf; /* Pointer to structural metadata (SM) */
3834 : char *metaptrs[2];/* Pointers to begin and end of SM section */
3835 : char swathname[80]; /* Swath Name */
3836 : char *utlstr; /* Utility string */
3837 : char *utlstr2; /* Utility string 2 */
3838 : char *ptr[8]; /* String pointer array */
3839 :
3840 :
3841 : /* Allocate space for utility string */
3842 : /* --------------------------------- */
3843 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
3844 0 : if(utlstr == NULL)
3845 : {
3846 0 : HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__);
3847 0 : return(-1);
3848 : }
3849 :
3850 0 : utlstr2 = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
3851 0 : if(utlstr2 == NULL)
3852 : {
3853 0 : HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__);
3854 0 : free(utlstr);
3855 0 : return(-1);
3856 : }
3857 :
3858 : /* Check for valid swath id */
3859 0 : status = SWchkswid(swathID, "SWinqfields",
3860 : &fid, &sdInterfaceID, &swVgrpID);
3861 :
3862 0 : if (status == 0)
3863 : {
3864 : /* If field names, ranks, or number types desired ... */
3865 : /* --------------------------------------------------- */
3866 0 : if (fieldlist != NULL || rank != NULL || numbertype != NULL)
3867 : {
3868 : /* Get swath name */
3869 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
3870 :
3871 : /* Get pointers to "GeoField" or "DataField" section within SM */
3872 0 : if (strcmp(fieldtype, "Geolocation Fields") == 0)
3873 : {
3874 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
3875 : "GeoField", metaptrs);
3876 0 : if(metabuf == NULL)
3877 : {
3878 0 : free(utlstr);
3879 0 : free(utlstr2);
3880 0 : return(-1);
3881 : }
3882 0 : strcpy(utlstr2, "GeoFieldName");
3883 : }
3884 : else
3885 : {
3886 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
3887 : "DataField", metaptrs);
3888 0 : if(metabuf == NULL)
3889 : {
3890 0 : free(utlstr);
3891 0 : free(utlstr2);
3892 0 : return(-1);
3893 : }
3894 0 : strcpy(utlstr2, "DataFieldName");
3895 : }
3896 :
3897 :
3898 : /* If field names are desired then "clear" name buffer */
3899 0 : if (fieldlist != NULL)
3900 : {
3901 0 : fieldlist[0] = 0;
3902 : }
3903 :
3904 :
3905 : /* Begin loop through mapping entries in metadata */
3906 : /* ---------------------------------------------- */
3907 : while (1)
3908 : {
3909 : /* Search for OBJECT string */
3910 0 : metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
3911 :
3912 : /* If found within "Geo" or "Data" Field metadata section .. */
3913 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
3914 : {
3915 : /* Get Fieldnames (if desired) */
3916 0 : if (fieldlist != NULL)
3917 : {
3918 : /* Check 1st for old meta data then new */
3919 : /* ------------------------------------ */
3920 0 : EHgetmetavalue(metaptrs, "OBJECT", utlstr);
3921 :
3922 : /*
3923 : * If OBJECT value begins with double quote then old
3924 : * metadata, field name is OBJECT value. Otherwise
3925 : * search for "GeoFieldName" or "DataFieldName"
3926 : * string
3927 : */
3928 :
3929 0 : if (utlstr[0] != '"')
3930 : {
3931 0 : strcpy(utlstr, "\t\t\t\t");
3932 0 : strcat(utlstr, utlstr2);
3933 0 : strcat(utlstr, "=");
3934 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
3935 0 : EHgetmetavalue(metaptrs, utlstr2, utlstr);
3936 : }
3937 :
3938 : /* Strip off double quotes */
3939 : /* ----------------------- */
3940 0 : REMQUOTE
3941 :
3942 :
3943 : /* Add to fieldlist */
3944 : /* ---------------- */
3945 0 : if (nFld > 0)
3946 : {
3947 0 : strcat(fieldlist, ",");
3948 : }
3949 0 : strcat(fieldlist, utlstr);
3950 :
3951 : }
3952 : /* Get Numbertype */
3953 0 : if (numbertype != NULL)
3954 : {
3955 0 : EHgetmetavalue(metaptrs, "DataType", utlstr);
3956 0 : numbertype[nFld] = EHnumstr(utlstr);
3957 : }
3958 : /*
3959 : * Get Rank (if desired) by counting # of dimensions in
3960 : * "DimList" string
3961 : */
3962 0 : if (rank != NULL)
3963 : {
3964 0 : EHgetmetavalue(metaptrs, "DimList", utlstr);
3965 0 : rank[nFld] = EHparsestr(utlstr, ',', ptr, slen);
3966 : }
3967 : /* Increment number of fields */
3968 0 : nFld++;
3969 : }
3970 : else
3971 : /* No more fields found */
3972 : {
3973 : break;
3974 : }
3975 0 : }
3976 0 : free(metabuf);
3977 : }
3978 : }
3979 :
3980 : /* Set nFld to -1 if error status exists */
3981 : /* ------------------------------------- */
3982 0 : if (status == -1)
3983 : {
3984 0 : nFld = -1;
3985 : }
3986 :
3987 0 : free(utlstr);
3988 0 : free(utlstr2);
3989 :
3990 0 : return (nFld);
3991 : }
3992 :
3993 :
3994 :
3995 :
3996 : /*----------------------------------------------------------------------------|
3997 : | BEGIN_PROLOG |
3998 : | |
3999 : | FUNCTION: SWinqgeofields |
4000 : | |
4001 : | DESCRIPTION: Inquires about geo fields in swath |
4002 : | |
4003 : | |
4004 : | Return Value Type Units Description |
4005 : | ============ ====== ========= ===================================== |
4006 : | nflds int32 Number of geo fields in swath |
4007 : | |
4008 : | INPUTS: |
4009 : | swathID int32 swath structure ID |
4010 : | |
4011 : | OUTPUTS: |
4012 : | fieldlist char Field names (comma-separated) |
4013 : | rank int32 Array of ranks |
4014 : | numbertype int32 Array of HDF number types |
4015 : | |
4016 : | NOTES: |
4017 : | |
4018 : | |
4019 : | Date Programmer Description |
4020 : | ====== ============ ================================================= |
4021 : | Jun 96 Joel Gales Original Programmer |
4022 : | |
4023 : | END_PROLOG |
4024 : -----------------------------------------------------------------------------*/
4025 : int32
4026 0 : SWinqgeofields(int32 swathID, char *fieldlist, int32 rank[],
4027 : int32 numbertype[])
4028 : {
4029 :
4030 : int32 nflds; /* Number of Geolocation fields */
4031 :
4032 : /* Call "SWinqfields" routine */
4033 : /* -------------------------- */
4034 0 : nflds = SWinqfields(swathID, "Geolocation Fields", fieldlist, rank,
4035 : numbertype);
4036 :
4037 0 : return (nflds);
4038 :
4039 : }
4040 :
4041 :
4042 :
4043 :
4044 : /*----------------------------------------------------------------------------|
4045 : | BEGIN_PROLOG |
4046 : | |
4047 : | FUNCTION: SWinqdatafields |
4048 : | |
4049 : | DESCRIPTION: Inquires about data fields in swath |
4050 : | |
4051 : | |
4052 : | Return Value Type Units Description |
4053 : | ============ ====== ========= ===================================== |
4054 : | nflds int32 Number of data fields in swath |
4055 : | |
4056 : | INPUTS: |
4057 : | swathID int32 swath structure ID |
4058 : | |
4059 : | OUTPUTS: |
4060 : | fieldlist char Field names (comma-separated) |
4061 : | rank int32 Array of ranks |
4062 : | numbertype int32 Array of HDF number types |
4063 : | |
4064 : | NOTES: |
4065 : | |
4066 : | |
4067 : | Date Programmer Description |
4068 : | ====== ============ ================================================= |
4069 : | Jun 96 Joel Gales Original Programmer |
4070 : | |
4071 : | END_PROLOG |
4072 : -----------------------------------------------------------------------------*/
4073 : int32
4074 0 : SWinqdatafields(int32 swathID, char *fieldlist, int32 rank[],
4075 : int32 numbertype[])
4076 : {
4077 :
4078 : int32 nflds; /* Number of Data fields */
4079 :
4080 : /* Call "SWinqfields" routine */
4081 : /* -------------------------- */
4082 0 : nflds = SWinqfields(swathID, "Data Fields", fieldlist, rank,
4083 : numbertype);
4084 :
4085 0 : return (nflds);
4086 :
4087 : }
4088 :
4089 :
4090 :
4091 :
4092 : /*----------------------------------------------------------------------------|
4093 : | BEGIN_PROLOG |
4094 : | |
4095 : | FUNCTION: SWnentries |
4096 : | |
4097 : | DESCRIPTION: Returns number of entries and string buffer size |
4098 : | |
4099 : | |
4100 : | Return Value Type Units Description |
4101 : | ============ ====== ========= ===================================== |
4102 : | nEntries int32 Number of entries |
4103 : | |
4104 : | INPUTS: |
4105 : | swathID int32 swath structure ID |
4106 : | entrycode int32 Entry code |
4107 : | HDFE_NENTDIM (0) |
4108 : | HDFE_NENTMAP (1) |
4109 : | HDFE_NENTIMAP (2) |
4110 : | HDFE_NENTGFLD (3) |
4111 : | HDFE_NENTDFLD (4) |
4112 : | |
4113 : | |
4114 : | OUTPUTS: |
4115 : | strbufsize int32 Length of comma-separated list |
4116 : | (Does not include null-terminator |
4117 : | |
4118 : | NOTES: |
4119 : | |
4120 : | |
4121 : | Date Programmer Description |
4122 : | ====== ============ ================================================= |
4123 : | Jun 96 Joel Gales Original Programmer |
4124 : | Aug 96 Joel Gales Make metadata ODL compliant |
4125 : | Feb 97 Joel Gales Set nEntries to -1 if status = -1 |
4126 : | |
4127 : | END_PROLOG |
4128 : -----------------------------------------------------------------------------*/
4129 : int32
4130 0 : SWnentries(int32 swathID, int32 entrycode, int32 * strbufsize)
4131 :
4132 : {
4133 : intn status; /* routine return status variable */
4134 : intn i; /* Loop index */
4135 :
4136 : int32 fid; /* HDF-EOS file ID */
4137 : int32 sdInterfaceID; /* HDF SDS interface ID */
4138 : int32 swVgrpID; /* Swath root Vgroup ID */
4139 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
4140 0 : int32 nEntries = 0; /* Number of entries */
4141 : int32 metaflag; /* Old (0), New (1) metadata flag) */
4142 : int32 nVal; /* Number of strings to search for */
4143 :
4144 0 : char *metabuf = NULL; /* Pointer to structural metadata (SM) */
4145 : char *metaptrs[2]; /* Pointers to begin and end of SM section */
4146 : char swathname[80]; /* Swath Name */
4147 : char *utlstr; /* Utility string */
4148 : char valName[2][32]; /* Strings to search for */
4149 :
4150 : /* Allocate space for utility string */
4151 : /* --------------------------------- */
4152 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
4153 0 : if(utlstr == NULL)
4154 : {
4155 0 : HEpush(DFE_NOSPACE,"SWnemtries", __FILE__, __LINE__);
4156 0 : return(-1);
4157 : }
4158 : /* Check for valid swath id */
4159 : /* ------------------------ */
4160 0 : status = SWchkswid(swathID, "SWnentries", &fid, &sdInterfaceID, &swVgrpID);
4161 :
4162 0 : if (status == 0)
4163 : {
4164 : /* Get swath name */
4165 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
4166 :
4167 : /* Zero out string buffer size */
4168 0 : *strbufsize = 0;
4169 :
4170 :
4171 : /*
4172 : * Get pointer to relevant section within SM and Get names of
4173 : * metadata strings to inquire about
4174 : */
4175 0 : switch (entrycode)
4176 : {
4177 : case HDFE_NENTDIM:
4178 : /* Dimensions */
4179 : {
4180 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
4181 : "Dimension", metaptrs);
4182 0 : if(metabuf == NULL)
4183 : {
4184 0 : free(utlstr);
4185 0 : return(-1);
4186 : }
4187 0 : nVal = 1;
4188 0 : strcpy(&valName[0][0], "DimensionName");
4189 : }
4190 0 : break;
4191 :
4192 : case HDFE_NENTMAP:
4193 : /* Dimension Maps */
4194 : {
4195 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
4196 : "DimensionMap", metaptrs);
4197 0 : if(metabuf == NULL)
4198 : {
4199 0 : free(utlstr);
4200 0 : return(-1);
4201 : }
4202 0 : nVal = 2;
4203 0 : strcpy(&valName[0][0], "GeoDimension");
4204 0 : strcpy(&valName[1][0], "DataDimension");
4205 : }
4206 0 : break;
4207 :
4208 : case HDFE_NENTIMAP:
4209 : /* Indexed Dimension Maps */
4210 : {
4211 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
4212 : "IndexDimensionMap", metaptrs);
4213 0 : if(metabuf == NULL)
4214 : {
4215 0 : free(utlstr);
4216 0 : return(-1);
4217 : }
4218 0 : nVal = 2;
4219 0 : strcpy(&valName[0][0], "GeoDimension");
4220 0 : strcpy(&valName[1][0], "DataDimension");
4221 : }
4222 0 : break;
4223 :
4224 : case HDFE_NENTGFLD:
4225 : /* Geolocation Fields */
4226 : {
4227 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
4228 : "GeoField", metaptrs);
4229 0 : if(metabuf == NULL)
4230 : {
4231 0 : free(utlstr);
4232 0 : return(-1);
4233 : }
4234 0 : nVal = 1;
4235 0 : strcpy(&valName[0][0], "GeoFieldName");
4236 : }
4237 0 : break;
4238 :
4239 : case HDFE_NENTDFLD:
4240 : /* Data Fields */
4241 : {
4242 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
4243 : "DataField", metaptrs);
4244 0 : if(metabuf == NULL)
4245 : {
4246 0 : free(utlstr);
4247 0 : return(-1);
4248 : }
4249 0 : nVal = 1;
4250 0 : strcpy(&valName[0][0], "DataFieldName");
4251 : }
4252 : break;
4253 : }
4254 :
4255 :
4256 : /*
4257 : * Check for presence of 'GROUP="' string If found then old metadata,
4258 : * search on OBJECT string
4259 : */
4260 0 : if (metabuf)
4261 : {
4262 0 : metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0;
4263 0 : if (metaflag == 0)
4264 : {
4265 0 : nVal = 1;
4266 0 : strcpy(&valName[0][0], "\t\tOBJECT");
4267 : }
4268 :
4269 :
4270 : /* Begin loop through entries in metadata */
4271 : /* -------------------------------------- */
4272 : while (1)
4273 : {
4274 : /* Search for first string */
4275 0 : strcpy(utlstr, &valName[0][0]);
4276 0 : strcat(utlstr, "=");
4277 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
4278 :
4279 : /* If found within relevant metadata section ... */
4280 0 : if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
4281 : {
4282 0 : for (i = 0; i < nVal; i++)
4283 : {
4284 : /*
4285 : * Get all string values Don't count quotes
4286 : */
4287 0 : EHgetmetavalue(metaptrs, &valName[i][0], utlstr);
4288 0 : *strbufsize += strlen(utlstr) - 2;
4289 : }
4290 : /* Increment number of entries */
4291 0 : nEntries++;
4292 :
4293 : /* Go to end of OBJECT */
4294 0 : metaptrs[0] = strstr(metaptrs[0], "END_OBJECT");
4295 : }
4296 : else
4297 : /* No more entries found */
4298 : {
4299 : break;
4300 : }
4301 0 : }
4302 0 : free(metabuf);
4303 : }
4304 :
4305 :
4306 : /* Count comma separators & slashes (if mappings) */
4307 : /* ---------------------------------------------- */
4308 0 : if (nEntries > 0)
4309 : {
4310 0 : *strbufsize += nEntries - 1;
4311 0 : *strbufsize += (nVal - 1) * nEntries;
4312 : }
4313 : }
4314 :
4315 :
4316 : /* Set nEntries to -1 if error status exists */
4317 : /* ----------------------------------------- */
4318 0 : if (status == -1)
4319 0 : nEntries = -1;
4320 :
4321 0 : free(utlstr);
4322 :
4323 0 : return (nEntries);
4324 : }
4325 :
4326 :
4327 :
4328 :
4329 :
4330 : /*----------------------------------------------------------------------------|
4331 : | BEGIN_PROLOG |
4332 : | |
4333 : | FUNCTION: SWinqswath |
4334 : | |
4335 : | DESCRIPTION: Returns number and names of swath structures in file |
4336 : | |
4337 : | |
4338 : | Return Value Type Units Description |
4339 : | ============ ====== ========= ===================================== |
4340 : | nSwath int32 Number of swath structures in file |
4341 : | |
4342 : | INPUTS: |
4343 : | filename char HDF-EOS filename |
4344 : | |
4345 : | OUTPUTS: |
4346 : | swathlist char List of swath names (comma-separated) |
4347 : | strbufsize int32 Length of swathlist |
4348 : | |
4349 : | NOTES: |
4350 : | |
4351 : | |
4352 : | Date Programmer Description |
4353 : | ====== ============ ================================================= |
4354 : | Jun 96 Joel Gales Original Programmer |
4355 : | |
4356 : | END_PROLOG |
4357 : -----------------------------------------------------------------------------*/
4358 : int32
4359 0 : SWinqswath(char *filename, char *swathlist, int32 * strbufsize)
4360 : {
4361 : int32 nSwath; /* Number of swath structures in file */
4362 :
4363 : /* Call "EHinquire" routine */
4364 : /* ------------------------ */
4365 0 : nSwath = EHinquire(filename, "SWATH", swathlist, strbufsize);
4366 :
4367 0 : return (nSwath);
4368 : }
4369 :
4370 :
4371 :
4372 : /*----------------------------------------------------------------------------|
4373 : | BEGIN_PROLOG |
4374 : | |
4375 : | FUNCTION: SW1dfldsrch |
4376 : | |
4377 : | DESCRIPTION: Retrieves information about a 1D field |
4378 : | |
4379 : | |
4380 : | Return Value Type Units Description |
4381 : | ============ ====== ========= ===================================== |
4382 : | status intn return status (0) SUCCEED, (-1) FAIL |
4383 : | |
4384 : | INPUTS: |
4385 : | fid int32 HDF-EOS file ID |
4386 : | swathID int32 swath structure ID |
4387 : | fieldname const char field name |
4388 : | access const char Access code (w/r) |
4389 : | |
4390 : | |
4391 : | OUTPUTS: |
4392 : | vgidout int32 Field (geo/data) vgroup ID |
4393 : | vdataIDout int32 Field Vdata ID |
4394 : | fldtype int32 Field type |
4395 : | |
4396 : | NOTES: |
4397 : | |
4398 : | |
4399 : | Date Programmer Description |
4400 : | ====== ============ ================================================= |
4401 : | Jun 96 Joel Gales Original Programmer |
4402 : | |
4403 : | END_PROLOG |
4404 : -----------------------------------------------------------------------------*/
4405 : static intn
4406 0 : SW1dfldsrch(int32 fid, int32 swathID, const char *fieldname, const char *access,
4407 : int32 * vgidout, int32 * vdataIDout, int32 * fldtype)
4408 :
4409 : {
4410 0 : intn status = 0; /* routine return status variable */
4411 :
4412 : int32 sID; /* SwathID - offset */
4413 : int32 vgid; /* Swath Geo or Data Vgroup ID */
4414 : int32 vdataID; /* 1d field vdata */
4415 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
4416 :
4417 :
4418 : /* Compute "reduced" swath ID */
4419 : /* -------------------------- */
4420 0 : sID = swathID % idOffset;
4421 :
4422 :
4423 : /* Get Geolocation Vgroup id and 1D field name Vdata id */
4424 : /* ---------------------------------------------------- */
4425 0 : vgid = SWXSwath[sID].VIDTable[0];
4426 0 : vdataID = EHgetid(fid, vgid, fieldname, 1, access);
4427 0 : *fldtype = 0;
4428 :
4429 :
4430 : /*
4431 : * If name not found in Geolocation Vgroup then detach Geolocation Vgroup
4432 : * and search in Data Vgroup
4433 : */
4434 0 : if (vdataID == -1)
4435 : {
4436 0 : vgid = SWXSwath[sID].VIDTable[1];;
4437 0 : vdataID = EHgetid(fid, vgid, fieldname, 1, access);
4438 0 : *fldtype = 1;
4439 :
4440 : /* If field also not found in Data Vgroup then set error status */
4441 : /* ------------------------------------------------------------ */
4442 0 : if (vdataID == -1)
4443 : {
4444 0 : status = -1;
4445 0 : vgid = -1;
4446 0 : vdataID = -1;
4447 : }
4448 : }
4449 0 : *vgidout = vgid;
4450 0 : *vdataIDout = vdataID;
4451 :
4452 0 : return (status);
4453 : }
4454 :
4455 :
4456 :
4457 :
4458 :
4459 : /*----------------------------------------------------------------------------|
4460 : | BEGIN_PROLOG |
4461 : | |
4462 : | FUNCTION: SWSDfldsrch |
4463 : | |
4464 : | DESCRIPTION: Retrieves information SDS field |
4465 : | |
4466 : | |
4467 : | Return Value Type Units Description |
4468 : | ============ ====== ========= ===================================== |
4469 : | status intn return status (0) SUCCEED, (-1) FAIL |
4470 : | |
4471 : | INPUTS: |
4472 : | swathID int32 swath structure ID |
4473 : | sdInterfaceID int32 SD interface ID |
4474 : | fieldname const char field name |
4475 : | |
4476 : | |
4477 : | OUTPUTS: |
4478 : | sdid int32 SD element ID |
4479 : | rankSDS int32 Rank of SDS |
4480 : | rankFld int32 True rank of field (merging) |
4481 : | offset int32 Offset of field within merged field |
4482 : | dims int32 Dimensions of field |
4483 : | solo int32 Solo field flag |
4484 : | |
4485 : | NOTES: |
4486 : | |
4487 : | |
4488 : | Date Programmer Description |
4489 : | ====== ============ ================================================= |
4490 : | Jun 96 Joel Gales Original Programmer |
4491 : | Aug 96 Joel Gales Make metadata ODL compliant |
4492 : | |
4493 : | END_PROLOG |
4494 : -----------------------------------------------------------------------------*/
4495 : static intn
4496 0 : SWSDfldsrch(int32 swathID, int32 sdInterfaceID, const char *fieldname,
4497 : int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset,
4498 : int32 dims[], int32 * solo)
4499 : {
4500 : intn i; /* Loop index */
4501 0 : intn status = -1;/* routine return status variable */
4502 :
4503 : int32 sID; /* SwathID - offset */
4504 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
4505 : int32 dum; /* Dummy variable */
4506 : int32 dums[128]; /* Dummy array */
4507 : int32 attrIndex; /* Attribute index */
4508 :
4509 : char name[2048]; /* Merged-Field Names */
4510 : char swathname[80]; /* Swath Name */
4511 : char *utlstr; /* Utility string */
4512 : char *metabuf; /* Pointer to structural metadata (SM) */
4513 : char *metaptrs[2];/* Pointers to begin and end of SM section */
4514 : char *oldmetaptr; /* Pointer within SM section */
4515 :
4516 :
4517 : /* Allocate space for utility string */
4518 : /* --------------------------------- */
4519 0 : utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
4520 0 : if(utlstr == NULL)
4521 : {
4522 0 : HEpush(DFE_NOSPACE,"SWSDfldsrch", __FILE__, __LINE__);
4523 0 : return(-1);
4524 : }
4525 : /* Set solo flag to 0 (no) */
4526 : /* ----------------------- */
4527 0 : *solo = 0;
4528 :
4529 :
4530 : /* Compute "reduced" swath ID */
4531 : /* -------------------------- */
4532 0 : sID = swathID % idOffset;
4533 :
4534 :
4535 : /* Loop through all SDSs in swath */
4536 : /* ------------------------------ */
4537 0 : for (i = 0; i < SWXSwath[sID].nSDS; i++)
4538 : {
4539 : /* If active SDS ... */
4540 : /* ----------------- */
4541 0 : if (SWXSwath[sID].sdsID[i] != 0)
4542 : {
4543 : /* Get SDS ID, name, rankSDS, and dimensions */
4544 : /* ----------------------------------------- */
4545 0 : *sdid = SWXSwath[sID].sdsID[i];
4546 0 : SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum);
4547 0 : *rankFld = *rankSDS;
4548 :
4549 : /* If merged field ... */
4550 : /* ------------------- */
4551 0 : if (strstr(name, "MRGFLD_") == &name[0])
4552 : {
4553 : /* Get swath name */
4554 : /* -------------- */
4555 0 : Vgetname(SWXSwath[sID].IDTable, swathname);
4556 :
4557 :
4558 : /* Get pointers to "MergedFields" section within SM */
4559 : /* ------------------------------------------------ */
4560 0 : metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
4561 : "MergedFields", metaptrs);
4562 0 : if(metabuf == NULL)
4563 : {
4564 0 : free(utlstr);
4565 0 : return(-1);
4566 : }
4567 :
4568 : /* Store metaptr in order to recover */
4569 : /* --------------------------------- */
4570 0 : oldmetaptr = metaptrs[0];
4571 :
4572 :
4573 : /* Search for Merged field name */
4574 : /* ---------------------------- */
4575 0 : sprintf(utlstr, "%s%s%s", "MergedFieldName=\"",
4576 : name, "\"\n");
4577 0 : metaptrs[0] = strstr(metaptrs[0], utlstr);
4578 :
4579 :
4580 : /* If not found check for old metadata */
4581 : /* ----------------------------------- */
4582 0 : if (metaptrs[0] == NULL)
4583 : {
4584 0 : sprintf(utlstr, "%s%s%s", "OBJECT=\"", name, "\"\n");
4585 0 : metaptrs[0] = strstr(oldmetaptr, utlstr);
4586 : }
4587 :
4588 :
4589 : /* Get field list and strip off leading and trailing quotes */
4590 0 : EHgetmetavalue(metaptrs, "FieldList", name); /* not return status --xhua */
4591 0 : memmove(name, name + 1, strlen(name) - 2);
4592 0 : name[strlen(name) - 2] = 0;
4593 :
4594 : /* Search for desired field within merged field list */
4595 0 : sprintf(utlstr, "%s%s%s", "\"", fieldname, "\"");
4596 0 : dum = EHstrwithin(utlstr, name, ',');
4597 :
4598 0 : free(metabuf);
4599 : }
4600 : else
4601 : {
4602 : /* If solo (unmerged) check if SDS name matches fieldname */
4603 : /* ------------------------------------------------------ */
4604 0 : dum = EHstrwithin(fieldname, name, ',');
4605 0 : if (dum != -1)
4606 : {
4607 0 : *solo = 1;
4608 0 : *offset = 0;
4609 : }
4610 : }
4611 :
4612 :
4613 : /* If field found ... */
4614 : /* ------------------ */
4615 0 : if (dum != -1)
4616 : {
4617 0 : status = 0;
4618 :
4619 : /* If merged field ... */
4620 : /* ------------------- */
4621 0 : if (*solo == 0)
4622 : {
4623 : /* Get "Field Offsets" SDS attribute index */
4624 : /* --------------------------------------- */
4625 0 : attrIndex = SDfindattr(*sdid, "Field Offsets");
4626 :
4627 : /*
4628 : * If attribute exists then get offset of desired field
4629 : * within merged field
4630 : */
4631 0 : if (attrIndex != -1)
4632 : {
4633 0 : SDreadattr(*sdid, attrIndex, (VOIDP) dums);
4634 0 : *offset = dums[dum];
4635 : }
4636 :
4637 :
4638 : /* Get "Field Dims" SDS attribute index */
4639 : /* ------------------------------------ */
4640 0 : attrIndex = SDfindattr(*sdid, "Field Dims");
4641 :
4642 : /*
4643 : * If attribute exists then get 0th dimension of desired
4644 : * field within merged field
4645 : */
4646 0 : if (attrIndex != -1)
4647 : {
4648 0 : SDreadattr(*sdid, attrIndex, (VOIDP) dums);
4649 0 : dims[0] = dums[dum];
4650 :
4651 : /* If this dimension = 1 then field is really 2 dim */
4652 : /* ------------------------------------------------ */
4653 0 : if (dums[dum] == 1)
4654 : {
4655 0 : *rankFld = 2;
4656 : }
4657 : }
4658 : }
4659 :
4660 :
4661 : /* Break out of SDS loop */
4662 : /* --------------------- */
4663 0 : break;
4664 : } /* End of found field section */
4665 : }
4666 : else
4667 : {
4668 : /* First non-active SDS signifies no more, break out of SDS loop */
4669 : /* ------------------------------------------------------------- */
4670 0 : break;
4671 : }
4672 : }
4673 :
4674 0 : free(utlstr);
4675 :
4676 0 : return (status);
4677 : }
4678 :
4679 :
4680 :
4681 :
4682 :
4683 : /*----------------------------------------------------------------------------|
4684 : | BEGIN_PROLOG |
4685 : | |
4686 : | FUNCTION: SWwrrdfield |
4687 : | |
4688 : | DESCRIPTION: Writes/Reads fields |
4689 : | |
4690 : | |
4691 : | Return Value Type Units Description |
4692 : | ============ ====== ========= ===================================== |
4693 : | status intn return status (0) SUCCEED, (-1) FAIL |
4694 : | |
4695 : | INPUTS: |
4696 : | swathID int32 swath structure ID |
4697 : | fieldname const char fieldname |
4698 : | code const char Write/Read code (w/r) |
4699 : | start int32 start array |
4700 : | stride int32 stride array |
4701 : | edge int32 edge array |
4702 : | datbuf void data buffer for read |
4703 : | |
4704 : | |
4705 : | OUTPUTS: |
4706 : | datbuf void data buffer for write |
4707 : | |
4708 : | NOTES: |
4709 : | |
4710 : | |
4711 : | Date Programmer Description |
4712 : | ====== ============ ================================================= |
4713 : | Jun 96 Joel Gales Original Programmer |
4714 : | Feb 97 Joel Gales Stride = 1 HDF compression workaround |
4715 : | |
4716 : | END_PROLOG |
4717 : -----------------------------------------------------------------------------*/
4718 : static intn
4719 0 : SWwrrdfield(int32 swathID, const char *fieldname, const char *code,
4720 : int32 start[], int32 stride[], int32 edge[], VOIDP datbuf)
4721 :
4722 : {
4723 : intn i; /* Loop index */
4724 0 : intn status = 0; /* routine return status variable */
4725 :
4726 : int32 fid; /* HDF-EOS file ID */
4727 : int32 sdInterfaceID; /* HDF SDS interface ID */
4728 : int32 vgid; /* Swath Geo or Data Vgroup ID */
4729 : int32 sdid; /* SDS ID */
4730 : int32 dum; /* Dummy variable */
4731 : int32 rankSDS; /* Rank of SDS */
4732 : int32 rankFld; /* Rank of field */
4733 :
4734 : int32 vdataID; /* 1d field vdata */
4735 : int32 recsize; /* Vdata record size */
4736 : int32 fldsize; /* Field size */
4737 : int32 nrec; /* Number of records in Vdata */
4738 :
4739 : int32 offset[8]; /* I/O offset (start) */
4740 : int32 incr[8]; /* I/O incrment (stride) */
4741 : int32 count[8]; /* I/O count (edge) */
4742 : int32 dims[8]; /* Field/SDS dimensions */
4743 : int32 mrgOffset; /* Merged field offset */
4744 : int32 nflds; /* Number of fields in Vdata */
4745 : int32 strideOne; /* Strides = 1 flag */
4746 :
4747 : uint8 *buf; /* I/O (transfer) buffer */
4748 : uint8 *fillbuf; /* Fill value buffer */
4749 :
4750 : char attrName[80]; /* Name of fill value attribute */
4751 : char *ptr[64]; /* String pointer array */
4752 : char fieldlist[256]; /* Vdata field list */
4753 :
4754 :
4755 : /* Check for valid swath ID */
4756 : /* ------------------------ */
4757 0 : status = SWchkswid(swathID, "SWwrrdfield", &fid, &sdInterfaceID, &dum);
4758 :
4759 :
4760 0 : if (status == 0)
4761 : {
4762 :
4763 : /* Check whether fieldname is in SDS (multi-dim field) */
4764 : /* --------------------------------------------------- */
4765 0 : status = SWSDfldsrch(swathID, sdInterfaceID, fieldname, &sdid,
4766 : &rankSDS, &rankFld, &mrgOffset, dims, &dum);
4767 :
4768 : /* Multi-Dimensional Field Section */
4769 : /* ------------------------------- */
4770 0 : if (status != -1)
4771 : {
4772 : /* Set I/O offset Section */
4773 : /* ---------------------- */
4774 :
4775 : /*
4776 : * If start == NULL (default) set I/O offset of 0th field to
4777 : * offset within merged field (if any) and the rest to 0
4778 : */
4779 0 : if (start == NULL)
4780 : {
4781 0 : for (i = 0; i < rankSDS; i++)
4782 : {
4783 0 : offset[i] = 0;
4784 : }
4785 0 : offset[0] = mrgOffset;
4786 : }
4787 : else
4788 : {
4789 : /*
4790 : * ... otherwise set I/O offset to user values, adjusting the
4791 : * 0th field with the merged field offset (if any)
4792 : */
4793 0 : if (rankFld == rankSDS)
4794 : {
4795 0 : for (i = 0; i < rankSDS; i++)
4796 : {
4797 0 : offset[i] = start[i];
4798 : }
4799 0 : offset[0] += mrgOffset;
4800 : }
4801 : else
4802 : {
4803 : /*
4804 : * If field really 2-dim merged in 3-dim field then set
4805 : * 0th field offset to merge offset and then next two to
4806 : * the user values
4807 : */
4808 0 : for (i = 0; i < rankFld; i++)
4809 : {
4810 0 : offset[i + 1] = start[i];
4811 : }
4812 0 : offset[0] = mrgOffset;
4813 : }
4814 : }
4815 :
4816 :
4817 :
4818 : /* Set I/O stride Section */
4819 : /* ---------------------- */
4820 :
4821 : /*
4822 : * If stride == NULL (default) set I/O stride to 1
4823 : */
4824 0 : if (stride == NULL)
4825 : {
4826 0 : for (i = 0; i < rankSDS; i++)
4827 : {
4828 0 : incr[i] = 1;
4829 : }
4830 : }
4831 : else
4832 : {
4833 : /*
4834 : * ... otherwise set I/O stride to user values
4835 : */
4836 0 : if (rankFld == rankSDS)
4837 : {
4838 0 : for (i = 0; i < rankSDS; i++)
4839 : {
4840 0 : incr[i] = stride[i];
4841 : }
4842 : }
4843 : else
4844 : {
4845 : /*
4846 : * If field really 2-dim merged in 3-dim field then set
4847 : * 0th field stride to 1 and then next two to the user
4848 : * values.
4849 : */
4850 0 : for (i = 0; i < rankFld; i++)
4851 : {
4852 0 : incr[i + 1] = stride[i];
4853 : }
4854 0 : incr[0] = 1;
4855 : }
4856 : }
4857 :
4858 :
4859 :
4860 : /* Set I/O count Section */
4861 : /* --------------------- */
4862 :
4863 : /*
4864 : * If edge == NULL (default) set I/O count to number of remaining
4865 : * entries (dims - start) / increment. Note that 0th field
4866 : * offset corrected for merged field offset (if any).
4867 : */
4868 0 : if (edge == NULL)
4869 : {
4870 0 : for (i = 1; i < rankSDS; i++)
4871 : {
4872 0 : count[i] = (dims[i] - offset[i]) / incr[i];
4873 : }
4874 0 : count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0];
4875 : }
4876 : else
4877 : {
4878 : /*
4879 : * ... otherwise set I/O count to user values
4880 : */
4881 0 : if (rankFld == rankSDS)
4882 : {
4883 0 : for (i = 0; i < rankSDS; i++)
4884 : {
4885 0 : count[i] = edge[i];
4886 : }
4887 : }
4888 : else
4889 : {
4890 : /*
4891 : * If field really 2-dim merged in 3-dim field then set
4892 : * 0th field count to 1 and then next two to the user
4893 : * values.
4894 : */
4895 0 : for (i = 0; i < rankFld; i++)
4896 : {
4897 0 : count[i + 1] = edge[i];
4898 : }
4899 0 : count[0] = 1;
4900 : }
4901 : }
4902 :
4903 : /* Perform I/O with relevant HDF I/O routine */
4904 : /* ----------------------------------------- */
4905 0 : if (strcmp(code, "w") == 0)
4906 : {
4907 : /* Set strideOne to true (1) */
4908 : /* ------------------------- */
4909 0 : strideOne = 1;
4910 :
4911 :
4912 : /* If incr[i] != 1 set strideOne to false (0) */
4913 : /* ------------------------------------------ */
4914 0 : for (i = 0; i < rankSDS; i++)
4915 : {
4916 0 : if (incr[i] != 1)
4917 : {
4918 0 : strideOne = 0;
4919 0 : break;
4920 : }
4921 : }
4922 :
4923 :
4924 : /*
4925 : * If strideOne is true use NULL paramater for stride. This
4926 : * is a work-around to HDF compression problem
4927 : */
4928 0 : if (strideOne == 1)
4929 : {
4930 0 : status = SDwritedata(sdid, offset, NULL, count,
4931 : (VOIDP) datbuf);
4932 : }
4933 : else
4934 : {
4935 0 : status = SDwritedata(sdid, offset, incr, count,
4936 : (VOIDP) datbuf);
4937 : }
4938 : }
4939 : else
4940 : {
4941 0 : status = SDreaddata(sdid, offset, incr, count,
4942 : (VOIDP) datbuf);
4943 : }
4944 : } /* End of Multi-Dimensional Field Section */
4945 : else
4946 : {
4947 :
4948 : /* One-Dimensional Field Section */
4949 : /* ----------------------------- */
4950 :
4951 : /* Check fieldname within 1d field Vgroups */
4952 : /* --------------------------------------- */
4953 0 : status = SW1dfldsrch(fid, swathID, fieldname, code,
4954 : &vgid, &vdataID, &dum);
4955 :
4956 0 : if (status != -1)
4957 : {
4958 :
4959 : /* Get number of records */
4960 : /* --------------------- */
4961 0 : nrec = VSelts(vdataID);
4962 :
4963 :
4964 : /* Set offset, increment, & count */
4965 : /* ------------------------------ */
4966 0 : offset[0] = (start == NULL) ? 0 : start[0];
4967 0 : incr[0] = (stride == NULL) ? 1 : stride[0];
4968 0 : count[0] = (edge == NULL)
4969 0 : ? (nrec - offset[0]) / incr[0]
4970 : : edge[0];
4971 :
4972 :
4973 :
4974 : /* Write Section */
4975 : /* ------------- */
4976 0 : if (strcmp(code, "w") == 0)
4977 : {
4978 : /* Get size of field and setup fill buffer */
4979 : /* --------------------------------------- */
4980 0 : fldsize = VSsizeof(vdataID, (char *)fieldname);
4981 0 : fillbuf = (uint8 *) calloc(fldsize, 1);
4982 0 : if(fillbuf == NULL)
4983 : {
4984 0 : HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
4985 0 : return(-1);
4986 : }
4987 :
4988 : /* Get size of record in Vdata and setup I/O buffer */
4989 : /* ------------------------------------------------ */
4990 0 : VSQueryvsize(vdataID, &recsize);
4991 0 : buf = (uint8 *) calloc(recsize, count[0] * incr[0]);
4992 0 : if(buf == NULL)
4993 : {
4994 0 : HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
4995 0 : return(-1);
4996 : }
4997 :
4998 :
4999 : /* Get names and number of fields in each record */
5000 : /* ---------------------------------------------- */
5001 0 : VSgetfields(vdataID, fieldlist);
5002 0 : dum = EHstrwithin(fieldname, fieldlist, ',');
5003 0 : nflds = EHparsestr(fieldlist, ',', ptr, NULL);
5004 :
5005 :
5006 : /* Get Merged Field Offset (if any) */
5007 : /* -------------------------------- */
5008 0 : if (nflds > 1)
5009 : {
5010 0 : if (dum > 0)
5011 : {
5012 0 : *(ptr[dum] - 1) = 0;
5013 0 : mrgOffset = VSsizeof(vdataID, fieldlist);
5014 0 : *(ptr[dum] - 1) = ',';
5015 : }
5016 : else
5017 : {
5018 0 : mrgOffset = 0;
5019 : }
5020 :
5021 : /* Read records to recover previously written data */
5022 0 : status = VSsetfields(vdataID, fieldlist);
5023 0 : status = VSseek(vdataID, offset[0]);
5024 0 : nrec = VSread(vdataID, buf, count[0] * incr[0],
5025 : FULL_INTERLACE);
5026 : }
5027 : else
5028 : {
5029 0 : mrgOffset = 0;
5030 : }
5031 :
5032 :
5033 :
5034 : /* Fill buffer with "Fill" value (if any) */
5035 : /* -------------------------------------- */
5036 0 : strcpy(attrName, "_FV_");
5037 0 : strcat(attrName, fieldname);
5038 :
5039 0 : status = SWreadattr(swathID, attrName, (char *) fillbuf);
5040 0 : if (status == 0)
5041 : {
5042 0 : for (i = 0; i < count[0] * incr[0]; i++)
5043 : {
5044 0 : memcpy(buf + i * recsize + mrgOffset,
5045 : fillbuf, fldsize);
5046 : }
5047 : }
5048 :
5049 :
5050 : /* Write new data into buffer */
5051 : /* -------------------------- */
5052 0 : if (incr[0] == 1 && nflds == 1)
5053 : {
5054 0 : memcpy(buf, datbuf, count[0] * recsize);
5055 : }
5056 : else
5057 : {
5058 0 : for (i = 0; i < count[0]; i++)
5059 : {
5060 0 : memcpy(buf + i * recsize * incr[0] + mrgOffset,
5061 0 : (uint8 *) datbuf + i * fldsize, fldsize);
5062 : }
5063 : }
5064 :
5065 :
5066 : /* If append read last record */
5067 : /* -------------------------- */
5068 0 : if (offset[0] == nrec)
5069 : {
5070 : /* abe added "status =" to next line 8/8/97 */
5071 0 : status = VSseek(vdataID, offset[0] - 1);
5072 0 : VSread(vdataID, fillbuf, 1, FULL_INTERLACE);
5073 : }
5074 : else
5075 : {
5076 0 : status = VSseek(vdataID, offset[0]);
5077 : }
5078 :
5079 :
5080 : /* Write data into Vdata */
5081 : /* --------------------- */
5082 0 : nrec = VSwrite(vdataID, buf, count[0] * incr[0],
5083 : FULL_INTERLACE);
5084 :
5085 0 : free(fillbuf);
5086 0 : if (status > 0)
5087 0 : status = 0;
5088 :
5089 : } /* End Write Section */
5090 : else
5091 : {
5092 : /* Read Section */
5093 : /* ------------ */
5094 0 : status = VSsetfields(vdataID, fieldname);
5095 0 : fldsize = VSsizeof(vdataID, (char *)fieldname);
5096 0 : buf = (uint8 *) calloc(fldsize, count[0] * incr[0]);
5097 0 : if(buf == NULL)
5098 : {
5099 0 : HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
5100 0 : return(-1);
5101 : }
5102 :
5103 0 : (void) VSseek(vdataID, offset[0]);
5104 0 : (void) VSread(vdataID, buf, count[0] * incr[0],
5105 : FULL_INTERLACE);
5106 :
5107 :
5108 : /* Copy from input buffer to returned data buffer */
5109 : /* ---------------------------------------------- */
5110 0 : if (incr[0] == 1)
5111 : {
5112 0 : memcpy(datbuf, buf, count[0] * fldsize);
5113 : }
5114 : else
5115 : {
5116 0 : for (i = 0; i < count[0]; i++)
5117 : {
5118 0 : memcpy((uint8 *) datbuf + i * fldsize,
5119 0 : buf + i * fldsize * incr[0], fldsize);
5120 : }
5121 : }
5122 :
5123 : } /* End Read Section */
5124 :
5125 0 : free(buf);
5126 0 : VSdetach(vdataID);
5127 : }
5128 : else
5129 : {
5130 0 : HEpush(DFE_GENAPP, "SWwrrdfield", __FILE__, __LINE__);
5131 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
5132 : }
5133 : } /* End One-D Field Section */
5134 :
5135 : }
5136 0 : return (status);
5137 : }
5138 :
5139 :
5140 :
5141 : /*----------------------------------------------------------------------------|
5142 : | BEGIN_PROLOG |
5143 : | |
5144 : | FUNCTION: SWwritefield |
5145 : | |
5146 : | DESCRIPTION: Writes data to field |
5147 : | |
5148 : | |
5149 : | Return Value Type Units Description |
5150 : | ============ ====== ========= ===================================== |
5151 : | status intn return status (0) SUCCEED, (-1) FAIL |
5152 : | |
5153 : | INPUTS: |
5154 : | swathID int32 swath structure ID |
5155 : | fieldname char fieldname |
5156 : | start int32 start array |
5157 : | stride int32 stride array |
5158 : | edge int32 edge array |
5159 : | |
5160 : | |
5161 : | OUTPUTS: |
5162 : | data void data buffer for write |
5163 : | |
5164 : | NOTES: |
5165 : | |
5166 : | |
5167 : | Date Programmer Description |
5168 : | ====== ============ ================================================= |
5169 : | Jun 96 Joel Gales Original Programmer |
5170 : | |
5171 : | END_PROLOG |
5172 : -----------------------------------------------------------------------------*/
5173 : intn
5174 0 : SWwritefield(int32 swathID, char *fieldname,
5175 : int32 start[], int32 stride[], int32 edge[], VOIDP data)
5176 :
5177 : {
5178 0 : intn status = 0; /* routine return status variable */
5179 :
5180 0 : status = SWwrrdfield(swathID, fieldname, "w", start, stride, edge,
5181 : data);
5182 0 : return (status);
5183 : }
5184 :
5185 :
5186 :
5187 :
5188 :
5189 :
5190 : /*----------------------------------------------------------------------------|
5191 : | BEGIN_PROLOG |
5192 : | |
5193 : | FUNCTION: SWreadfield |
5194 : | |
5195 : | DESCRIPTION: Reads data from field |
5196 : | |
5197 : | Return Value Type Units Description |
5198 : | ============ ====== ========= ===================================== |
5199 : | status intn return status (0) SUCCEED, (-1) FAIL |
5200 : | |
5201 : | INPUTS: |
5202 : | swathID int32 swath structure ID |
5203 : | fieldname const char fieldname |
5204 : | start int32 start array |
5205 : | stride int32 stride array |
5206 : | edge int32 edge array |
5207 : | buffer void data buffer for read |
5208 : | |
5209 : | |
5210 : | OUTPUTS: |
5211 : | None |
5212 : | |
5213 : | NOTES: |
5214 : | |
5215 : | |
5216 : | Date Programmer Description |
5217 : | ====== ============ ================================================= |
5218 : | Jun 96 Joel Gales Original Programmer |
5219 : | |
5220 : | END_PROLOG |
5221 : -----------------------------------------------------------------------------*/
5222 : intn
5223 0 : SWreadfield(int32 swathID, const char *fieldname,
5224 : int32 start[], int32 stride[], int32 edge[], VOIDP buffer)
5225 :
5226 : {
5227 0 : intn status = 0; /* routine return status variable */
5228 :
5229 0 : status = SWwrrdfield(swathID, fieldname, "r", start, stride, edge,
5230 : buffer);
5231 0 : return (status);
5232 : }
5233 :
5234 :
5235 :
5236 :
5237 : /*----------------------------------------------------------------------------|
5238 : | BEGIN_PROLOG |
5239 : | |
5240 : | FUNCTION: SWdefboxregion |
5241 : | |
5242 : | DESCRIPTION: Finds swath cross tracks within area of interest and returns |
5243 : | region ID |
5244 : | |
5245 : | |
5246 : | Return Value Type Units Description |
5247 : | ============ ====== ========= ===================================== |
5248 : | regionID int32 Region ID |
5249 : | |
5250 : | INPUTS: |
5251 : | swathID int32 Swath structure ID |
5252 : | cornerlon float64 dec deg Longitude of opposite corners of box |
5253 : | cornerlat float64 dec deg Latitude of opposite corners of box |
5254 : | mode int32 Search mode |
5255 : | HDFE_MIDPOINT - Use midpoint of Xtrack |
5256 : | HDFE_ENDPOINT - Use endpoints of Xtrack |
5257 : | HDFE_ANYPOINT - Use all points of Xtrack|
5258 : | |
5259 : | OUTPUTS: |
5260 : | None |
5261 : | |
5262 : | NOTES: |
5263 : | |
5264 : | |
5265 : | Date Programmer Description |
5266 : | ====== ============ ================================================= |
5267 : | Jun 96 Joel Gales Original Programmer |
5268 : | Oct 96 Joel Gales Add ability to handle regions crossing date line |
5269 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
5270 : | Jul 98 Abe Taaheri Fixed core dump in SWregioninfo associated with |
5271 : | SWXRegion[k]->nRegions exceeding MAXNREGIONS in |
5272 : | this function |
5273 : | Aug 99 Abe Taaheri Fixed the code so that all cross tracks or all |
5274 : | points on the along track that fall inside the box |
5275 : | are identified. At the same time added code to |
5276 : | function "updatescene" to reject cases where there |
5277 : | is single cross track in the box (for LANDSAT) |
5278 : | Jun 03 Abe Taaheri Added a few lines to report error and return -1 if |
5279 : | regionID exceeded NSWATHREGN |
5280 : | Mar 04 Abe Taaheri Added recognition for GeodeticLatitude |
5281 : | |
5282 : | END_PROLOG |
5283 : -----------------------------------------------------------------------------*/
5284 : int32
5285 0 : SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[],
5286 : int32 mode)
5287 : {
5288 : intn i; /* Loop index */
5289 : intn j; /* Loop index */
5290 : intn k; /* Loop index */
5291 :
5292 : intn status; /* routine return status variable */
5293 : intn statLon; /* Status from SWfieldinfo for longitude */
5294 : intn statLat; /* Status from SWfieldinfo for latitude */
5295 0 : intn statCoLat = -1; /* Status from SWfieldinfo for
5296 : * Colatitude */
5297 0 : intn statGeodeticLat = -1; /* Status from SWfieldinfo for
5298 : * GeodeticLatitude */
5299 :
5300 0 : uint8 found = 0; /* Found flag */
5301 : uint8 *flag; /* Pointer to track flag array */
5302 0 : intn validReg = -1; /* -1 is invalid validReg */
5303 :
5304 : int32 fid; /* HDF-EOS file ID */
5305 : int32 sdInterfaceID; /* HDF SDS interface ID */
5306 : int32 swVgrpID; /* Swath Vgroup ID */
5307 : int32 rank; /* Rank of geolocation fields */
5308 : int32 nt; /* Number type of geolocation fields */
5309 : int32 dims[8]; /* Dimensions of geolocation fields */
5310 : int32 nElem; /* Number of elements to read */
5311 : int32 bndflag; /* +/-180 longitude boundary flag */
5312 : int32 lonTest; /* Longitude test flag */
5313 : int32 latTest; /* Latitude test flag */
5314 : int32 start[2]; /* Start array (read) */
5315 0 : int32 stride[2] = {1, 1}; /* Stride array (read) */
5316 : int32 edge[2]; /* Edge array (read) */
5317 0 : int32 regionID = -1; /* Region ID (return) */
5318 : int32 anyStart[2];/* ANYPOINT start array (read) */
5319 : int32 anyEdge[2]; /* ANYPOINT edge array (read) */
5320 :
5321 : float32 temp32; /* Temporary float32 variable */
5322 :
5323 : float64 lonTestVal; /* Longitude test value */
5324 : float64 latTestVal; /* Latitude test value */
5325 : float64 temp64; /* Temporary float64 variable */
5326 :
5327 : char *lonArr; /* Longitude data array */
5328 : char *latArr; /* Latitude data array */
5329 : char dimlist[256]; /* Dimension list (geolocation
5330 : * fields) */
5331 : char latName[16];/* Latitude field name */
5332 :
5333 :
5334 : /* Check for valid swath ID */
5335 : /* ------------------------ */
5336 0 : status = SWchkswid(swathID, "SWdefboxregion", &fid, &sdInterfaceID,
5337 : &swVgrpID);
5338 :
5339 :
5340 : /* Inclusion mode must be between 0 and 2 */
5341 : /* -------------------------------------- */
5342 0 : if (mode < 0 || mode > 2)
5343 : {
5344 0 : status = -1;
5345 0 : HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
5346 0 : HEreport("Improper Inclusion Mode: %d.\n", mode);
5347 : }
5348 :
5349 :
5350 0 : if (status == 0)
5351 : {
5352 : /* Get "Longitude" field info */
5353 : /* -------------------------- */
5354 0 : statLon = SWfieldinfo(swathID, "Longitude", &rank, dims, &nt, dimlist);
5355 0 : if (statLon != 0)
5356 : {
5357 0 : status = -1;
5358 0 : HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
5359 0 : HEreport("\"Longitude\" field not found.\n");
5360 : }
5361 :
5362 : /* Get "Latitude" field info */
5363 : /* -------------------------- */
5364 0 : statLat = SWfieldinfo(swathID, "Latitude", &rank, dims, &nt, dimlist);
5365 0 : if (statLat != 0)
5366 : {
5367 : /* If not found check for "Colatitude" field info */
5368 : /* ---------------------------------------------- */
5369 0 : statCoLat = SWfieldinfo(swathID, "Colatitude", &rank, dims, &nt,
5370 : dimlist);
5371 0 : if (statCoLat != 0)
5372 : {
5373 : /* Check again for Geodeticlatitude */
5374 0 : statGeodeticLat = SWfieldinfo(swathID,
5375 : "GeodeticLatitude", &rank,
5376 : dims, &nt, dimlist);
5377 0 : if (statGeodeticLat != 0)
5378 : {
5379 : /* Neither "Latitude" nor "Colatitude" nor
5380 : "GeodeticLatitude" field found */
5381 : /* ----------------------------------------------- */
5382 0 : status = -1;
5383 0 : HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
5384 0 : HEreport(
5385 : "Neither \"Latitude\" nor \"Colatitude\" nor \"GeodeticLatitude\" fields found.\n");
5386 : }
5387 : else
5388 : {
5389 : /* Latitude field is "GeodeticLatitude" */
5390 : /* ------------------------------ */
5391 0 : strcpy(latName, "GeodeticLatitude");
5392 : }
5393 : }
5394 : else
5395 : {
5396 : /* Latitude field is "Colatitude" */
5397 : /* ------------------------------ */
5398 0 : strcpy(latName, "Colatitude");
5399 : }
5400 : }
5401 : else
5402 : {
5403 : /* Latitude field is "Latitude" */
5404 : /* ---------------------------- */
5405 0 : strcpy(latName, "Latitude");
5406 : }
5407 :
5408 :
5409 0 : if (status == 0)
5410 : {
5411 : /* Search along entire "Track" dimension from beginning to end */
5412 : /* ----------------------------------------------------------- */
5413 0 : start[0] = 0;
5414 0 : edge[0] = dims[0];
5415 :
5416 :
5417 : /* If 1D geolocation fields then set mode to MIDPOINT */
5418 : /* -------------------------------------------------- */
5419 0 : if (rank == 1)
5420 : {
5421 0 : mode = HDFE_MIDPOINT;
5422 : }
5423 :
5424 :
5425 0 : switch (mode)
5426 : {
5427 : /* If MIDPOINT search single point in middle of "CrossTrack" */
5428 : /* --------------------------------------------------------- */
5429 : case HDFE_MIDPOINT:
5430 :
5431 0 : start[1] = dims[1] / 2;
5432 0 : edge[1] = 1;
5433 :
5434 0 : break;
5435 :
5436 : /* If ENDPOINT search 2 points at either end of "CrossTrack" */
5437 : /* --------------------------------------------------------- */
5438 : case HDFE_ENDPOINT:
5439 :
5440 0 : start[1] = 0;
5441 0 : stride[1] = dims[1] - 1;
5442 0 : edge[1] = 2;
5443 :
5444 0 : break;
5445 :
5446 : /* If ANYPOINT do initial MIDPOINT search */
5447 : /* -------------------------------------- */
5448 : case HDFE_ANYPOINT:
5449 :
5450 0 : start[1] = dims[1] / 2;
5451 0 : edge[1] = 1;
5452 :
5453 : break;
5454 : }
5455 :
5456 :
5457 : /* Compute number of elements */
5458 : /* -------------------------- */
5459 0 : nElem = edge[0] * edge[1];
5460 :
5461 :
5462 : /* Allocate space for longitude and latitude (float64) */
5463 : /* --------------------------------------------------- */
5464 0 : lonArr = (char *) calloc(nElem, sizeof(float64));
5465 0 : if(lonArr == NULL)
5466 : {
5467 0 : HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
5468 0 : return(-1);
5469 : }
5470 :
5471 0 : latArr = (char *) calloc(nElem, sizeof(float64));
5472 0 : if(latArr == NULL)
5473 : {
5474 0 : HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
5475 0 : free(lonArr);
5476 0 : return(-1);
5477 : }
5478 :
5479 :
5480 : /* Allocate space for flag array (uint8) */
5481 : /* ------------------------------------- */
5482 0 : flag = (uint8 *) calloc(edge[0] + 1, 1);
5483 0 : if(flag == NULL)
5484 : {
5485 0 : HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
5486 0 : free(lonArr);
5487 0 : free(latArr);
5488 0 : return(-1);
5489 : }
5490 :
5491 :
5492 : /* Read Longitude and Latitude fields */
5493 : /* ---------------------------------- */
5494 0 : status = SWreadfield(swathID, "Longitude",
5495 : start, stride, edge, lonArr);
5496 0 : status = SWreadfield(swathID, latName,
5497 : start, stride, edge, latArr);
5498 :
5499 :
5500 :
5501 : /*
5502 : * If geolocation fields are FLOAT32 then cast each entry as
5503 : * FLOAT64
5504 : */
5505 0 : if (nt == DFNT_FLOAT32)
5506 : {
5507 0 : for (i = nElem - 1; i >= 0; i--)
5508 : {
5509 0 : memcpy(&temp32, lonArr + 4 * i, 4);
5510 0 : temp64 = (float64) temp32;
5511 0 : memcpy(lonArr + 8 * i, &temp64, 8);
5512 :
5513 0 : memcpy(&temp32, latArr + 4 * i, 4);
5514 0 : temp64 = (float64) temp32;
5515 0 : memcpy(latArr + 8 * i, &temp64, 8);
5516 : }
5517 : }
5518 :
5519 :
5520 : /* Set boundary flag */
5521 : /* ----------------- */
5522 :
5523 : /*
5524 : * This variable is set to 1 if the region of interest crosses
5525 : * the +/- 180 longitude boundary
5526 : */
5527 0 : bndflag = (cornerlon[0] < cornerlon[1]) ? 0 : 1;
5528 :
5529 :
5530 :
5531 : /* Main Search Loop */
5532 : /* ---------------- */
5533 :
5534 : /* For each track ... */
5535 : /* ------------------ */
5536 :
5537 0 : for (i = 0; i < edge[0]; i++)
5538 : {
5539 : /* For each value from Cross Track ... */
5540 : /* ----------------------------------- */
5541 0 : for (j = 0; j < edge[1]; j++)
5542 : {
5543 : /* Read in single lon & lat values from data buffers */
5544 : /* ------------------------------------------------- */
5545 0 : memcpy(&lonTestVal, &lonArr[8 * (i * edge[1] + j)], 8);
5546 0 : memcpy(&latTestVal, &latArr[8 * (i * edge[1] + j)], 8);
5547 :
5548 :
5549 : /* If longitude value > 180 convert to -180 to 180 range */
5550 : /* ----------------------------------------------------- */
5551 0 : if (lonTestVal > 180)
5552 : {
5553 0 : lonTestVal = lonTestVal - 360;
5554 : }
5555 :
5556 : /* If Colatitude value convert to latitude value */
5557 : /* --------------------------------------------- */
5558 0 : if (statCoLat == 0)
5559 : {
5560 0 : latTestVal = 90 - latTestVal;
5561 : }
5562 :
5563 :
5564 : /* Test if lat value is within range */
5565 : /* --------------------------------- */
5566 0 : latTest = (latTestVal >= cornerlat[0] &&
5567 0 : latTestVal <= cornerlat[1]);
5568 :
5569 :
5570 0 : if (bndflag == 1)
5571 : {
5572 : /*
5573 : * If boundary flag set test whether longitude value
5574 : * is outside region and then flip
5575 : */
5576 0 : lonTest = (lonTestVal >= cornerlon[1] &&
5577 0 : lonTestVal <= cornerlon[0]);
5578 0 : lonTest = 1 - lonTest;
5579 : }
5580 : else
5581 : {
5582 0 : lonTest = (lonTestVal >= cornerlon[0] &&
5583 0 : lonTestVal <= cornerlon[1]);
5584 : }
5585 :
5586 :
5587 : /*
5588 : * If both longitude and latitude are within region set
5589 : * flag on for this track
5590 : */
5591 0 : if (lonTest + latTest == 2)
5592 : {
5593 0 : flag[i] = 1;
5594 0 : found = 1;
5595 0 : break;
5596 : }
5597 : }
5598 : }
5599 :
5600 :
5601 :
5602 : /* ANYPOINT search */
5603 : /* --------------- */
5604 0 : if (mode == HDFE_ANYPOINT && rank > 1)
5605 : {
5606 0 : free(lonArr);
5607 0 : free(latArr);
5608 :
5609 : /* Allocate space for an entire single cross track */
5610 : /* ----------------------------------------------- */
5611 0 : lonArr = (char *) calloc(dims[1], sizeof(float64));
5612 0 : if(lonArr == NULL)
5613 : {
5614 0 : HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
5615 0 : return(-1);
5616 : }
5617 :
5618 0 : latArr = (char *) calloc(dims[1], sizeof(float64));
5619 0 : if(latArr == NULL)
5620 : {
5621 0 : HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
5622 0 : free(lonArr);
5623 0 : return(-1);
5624 : }
5625 :
5626 :
5627 : /* Setup start and edge */
5628 : /* -------------------- */
5629 0 : anyStart[1] = 0;
5630 0 : anyEdge[0] = 1;
5631 0 : anyEdge[1] = dims[1];
5632 :
5633 :
5634 : /* For each track starting from 0 */
5635 : /* ------------------------------ */
5636 0 : for (i = 0; i < edge[0]; i++)
5637 : {
5638 :
5639 : /* If cross track not in region (with MIDPOINT search ... */
5640 : /* ------------------------------------------------------ */
5641 0 : if (flag[i] == 0)
5642 : {
5643 : /* Setup track start */
5644 : /* ----------------- */
5645 0 : anyStart[0] = i;
5646 :
5647 :
5648 : /* Read in lon and lat values for cross track */
5649 : /* ------------------------------------------ */
5650 0 : status = SWreadfield(swathID, "Longitude",
5651 : anyStart, NULL, anyEdge, lonArr);
5652 0 : status = SWreadfield(swathID, latName,
5653 : anyStart, NULL, anyEdge, latArr);
5654 :
5655 :
5656 :
5657 : /*
5658 : * If geolocation fields are FLOAT32 then cast each
5659 : * entry as FLOAT64
5660 : */
5661 0 : if (nt == DFNT_FLOAT32)
5662 : {
5663 0 : for (j = dims[1] - 1; j >= 0; j--)
5664 : {
5665 0 : memcpy(&temp32, lonArr + 4 * j, 4);
5666 0 : temp64 = (float64) temp32;
5667 0 : memcpy(lonArr + 8 * j, &temp64, 8);
5668 :
5669 0 : memcpy(&temp32, latArr + 4 * j, 4);
5670 0 : temp64 = (float64) temp32;
5671 0 : memcpy(latArr + 8 * j, &temp64, 8);
5672 : }
5673 : }
5674 :
5675 :
5676 : /* For each value from Cross Track ... */
5677 : /* ----------------------------------- */
5678 0 : for (j = 0; j < dims[1]; j++)
5679 : {
5680 : /* Read in single lon & lat values from buffers */
5681 : /* -------------------------------------------- */
5682 0 : memcpy(&lonTestVal, &lonArr[8 * j], 8);
5683 0 : memcpy(&latTestVal, &latArr[8 * j], 8);
5684 :
5685 :
5686 : /* If lon value > 180 convert to -180 - 180 range */
5687 : /* ---------------------------------------------- */
5688 0 : if (lonTestVal > 180)
5689 : {
5690 0 : lonTestVal = lonTestVal - 360;
5691 : }
5692 :
5693 : /* If Colatitude value convert to latitude value */
5694 : /* --------------------------------------------- */
5695 0 : if (statCoLat == 0)
5696 : {
5697 0 : latTestVal = 90 - latTestVal;
5698 : }
5699 :
5700 :
5701 : /* Test if lat value is within range */
5702 : /* --------------------------------- */
5703 0 : latTest = (latTestVal >= cornerlat[0] &&
5704 0 : latTestVal <= cornerlat[1]);
5705 :
5706 :
5707 0 : if (bndflag == 1)
5708 : {
5709 : /*
5710 : * If boundary flag set test whether
5711 : * longitude value is outside region and then
5712 : * flip
5713 : */
5714 0 : lonTest = (lonTestVal >= cornerlon[1] &&
5715 0 : lonTestVal <= cornerlon[0]);
5716 0 : lonTest = 1 - lonTest;
5717 : }
5718 : else
5719 : {
5720 0 : lonTest = (lonTestVal >= cornerlon[0] &&
5721 0 : lonTestVal <= cornerlon[1]);
5722 : }
5723 :
5724 :
5725 : /*
5726 : * If both longitude and latitude are within
5727 : * region set flag on for this track
5728 : */
5729 0 : if (lonTest + latTest == 2)
5730 : {
5731 0 : flag[i] = 1;
5732 0 : found = 1;
5733 0 : break;
5734 : }
5735 : }
5736 : }
5737 : }
5738 : }
5739 :
5740 : /* If within region setup Region Structure */
5741 : /* --------------------------------------- */
5742 0 : if (found == 1)
5743 : {
5744 : /* For all entries in SWXRegion array ... */
5745 : /* -------------------------------------- */
5746 0 : for (k = 0; k < NSWATHREGN; k++)
5747 : {
5748 : /* If empty region ... */
5749 : /* ------------------- */
5750 0 : if (SWXRegion[k] == 0)
5751 : {
5752 : /* Allocate space for region entry */
5753 : /* ------------------------------- */
5754 0 : SWXRegion[k] = (struct swathRegion *)
5755 : calloc(1, sizeof(struct swathRegion));
5756 0 : if(SWXRegion[k] == NULL)
5757 : {
5758 0 : HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
5759 0 : return(-1);
5760 : }
5761 :
5762 : /* Store file and swath ID */
5763 : /* ----------------------- */
5764 0 : SWXRegion[k]->fid = fid;
5765 0 : SWXRegion[k]->swathID = swathID;
5766 :
5767 :
5768 : /* Set Start & Stop Vertical arrays to -1 */
5769 : /* -------------------------------------- */
5770 0 : for (j = 0; j < 8; j++)
5771 : {
5772 0 : SWXRegion[k]->StartVertical[j] = -1;
5773 0 : SWXRegion[k]->StopVertical[j] = -1;
5774 0 : SWXRegion[k]->StartScan[j] = -1;
5775 0 : SWXRegion[k]->StopScan[j] = -1;
5776 : }
5777 :
5778 :
5779 : /* Set region ID */
5780 : /* ------------- */
5781 0 : regionID = k;
5782 0 : break;
5783 : }
5784 : }
5785 0 : if (k >= NSWATHREGN)
5786 : {
5787 0 : HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
5788 0 : HEreport(
5789 : "regionID exceeded NSWATHREGN.\n");
5790 0 : return (-1);
5791 : }
5792 :
5793 : /* Find start and stop of regions */
5794 : /* ------------------------------ */
5795 :
5796 : /* Subtract previous flag value from current one */
5797 : /* --------------------------------------------- */
5798 :
5799 : /*
5800 : * Transisition points will have flag value (+1) start or
5801 : * (255 = (uint8) -1) stop of region
5802 : */
5803 0 : for (i = edge[0]; i > 0; i--)
5804 : {
5805 0 : flag[i] -= flag[i - 1];
5806 : }
5807 :
5808 :
5809 0 : for (i = 0; i <= edge[0]; i++)
5810 : {
5811 : /* Start of region */
5812 : /* --------------- */
5813 0 : if (flag[i] == 1)
5814 : {
5815 : /* Increment (multiple) region counter */
5816 : /* ----------------------------------- */
5817 0 : j = ++SWXRegion[k]->nRegions;
5818 :
5819 : /* if SWXRegion[k]->nRegions greater than MAXNREGIONS */
5820 : /* free allocated memory and return FAIL */
5821 :
5822 0 : if ((SWXRegion[k]->nRegions) > MAXNREGIONS)
5823 : {
5824 0 : HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
5825 0 : HEreport("SWXRegion[%d]->nRegions exceeds MAXNREGIONS= %d.\n", k, MAXNREGIONS);
5826 0 : free(lonArr);
5827 0 : free(latArr);
5828 0 : free(flag);
5829 0 : return(-1);
5830 : }
5831 :
5832 0 : SWXRegion[k]->StartRegion[j - 1] = i;
5833 : }
5834 :
5835 : /* End of region */
5836 : /* ------------- */
5837 0 : if (flag[i] == 255)
5838 : {
5839 0 : SWXRegion[k]->StopRegion[j - 1] = i - 1;
5840 0 : validReg = 0;
5841 : }
5842 : }
5843 : }
5844 0 : free(lonArr);
5845 0 : free(latArr);
5846 0 : free(flag);
5847 : }
5848 : }
5849 0 : if(validReg==0)
5850 : {
5851 0 : return (regionID);
5852 : }
5853 : else
5854 : {
5855 0 : return (-1);
5856 : }
5857 :
5858 : }
5859 :
5860 :
5861 :
5862 :
5863 : /*----------------------------------------------------------------------------|
5864 : | BEGIN_PROLOG |
5865 : | |
5866 : | FUNCTION: SWregionindex |
5867 : | |
5868 : | DESCRIPTION: Finds swath cross tracks within area of interest and returns |
5869 : | region index and region ID |
5870 : | |
5871 : | |
5872 : | Return Value Type Units Description |
5873 : | ============ ====== ========= ===================================== |
5874 : | regionID int32 Region ID |
5875 : | |
5876 : | INPUTS: |
5877 : | swathID int32 Swath structure ID |
5878 : | cornerlon float64 dec deg Longitude of opposite corners of box |
5879 : | cornerlat float64 dec deg Latitude of opposite corners of box |
5880 : | mode int32 Search mode |
5881 : | HDFE_MIDPOINT - Use midpoint of Xtrack |
5882 : | HDFE_ENDPOINT - Use endpoints of Xtrack |
5883 : | HDFE_ANYPOINT - Use all points of Xtrack|
5884 : | |
5885 : | OUTPUTS: |
5886 : | geodim char geolocation track dimension |
5887 : | idxrange int32 indices of region for along track dim. |
5888 : | |
5889 : | NOTES: |
5890 : | |
5891 : | |
5892 : | Date Programmer Description |
5893 : | ====== ============ ================================================= |
5894 : | Jun 96 Joel Gales Original Programmer |
5895 : | Oct 96 Joel Gales Add ability to handle regions crossing date line |
5896 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
5897 : | Nov 97 Daw Add multiple vertical subsetting capability |
5898 : | END_PROLOG |
5899 : -----------------------------------------------------------------------------*/
5900 : int32
5901 0 : SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[],
5902 : int32 mode, char *geodim, int32 idxrange[])
5903 : {
5904 : intn i; /* Loop index */
5905 : intn j; /* Loop index */
5906 : intn k; /* Loop index */
5907 :
5908 0 : intn l=0; /* Loop index */
5909 : intn tmpVal; /* temp value for start region Delyth Jones*/
5910 : /*intn j1; */ /* Loop index */
5911 : intn status; /* routine return status variable */
5912 : intn mapstatus; /* status for type of mapping */
5913 : intn statLon; /* Status from SWfieldinfo for longitude */
5914 : intn statLat; /* Status from SWfieldinfo for latitude */
5915 0 : intn statCoLat = -1; /* Status from SWfieldinfo for
5916 : * Colatitude */
5917 0 : intn statGeodeticLat = -1; /* Status from SWfieldinfo for
5918 : * GeodeticLatitude */
5919 :
5920 0 : uint8 found = 0; /* Found flag */
5921 : uint8 *flag; /* Pointer to track flag array */
5922 0 : intn validReg = -1; /* -1 is invalid validReg */
5923 :
5924 : int32 fid; /* HDF-EOS file ID */
5925 : int32 sdInterfaceID; /* HDF SDS interface ID */
5926 : int32 swVgrpID; /* Swath Vgroup ID */
5927 : int32 rank; /* Rank of geolocation fields */
5928 : int32 nt; /* Number type of geolocation fields */
5929 : int32 dims[8]; /* Dimensions of geolocation fields */
5930 : int32 nElem; /* Number of elements to read */
5931 : int32 bndflag; /* +/-180 longitude boundary flag */
5932 : int32 lonTest; /* Longitude test flag */
5933 : int32 latTest; /* Latitude test flag */
5934 : int32 start[2]; /* Start array (read) */
5935 0 : int32 stride[2] = {1, 1}; /* Stride array (read) */
5936 : int32 edge[2]; /* Edge array (read) */
5937 0 : int32 regionID = -1; /* Region ID (return) */
5938 : int32 anyStart[2];/* ANYPOINT start array (read) */
5939 : int32 anyEdge[2]; /* ANYPOINT edge array (read) */
5940 :
5941 : float32 temp32; /* Temporary float32 variable */
5942 :
5943 : float64 lonTestVal; /* Longitude test value */
5944 : float64 latTestVal; /* Latitude test value */
5945 : float64 temp64; /* Temporary float64 variable */
5946 :
5947 : char *lonArr; /* Longitude data array */
5948 : char *latArr; /* Latitude data array */
5949 : char dimlist[256]; /* Dimension list (geolocation
5950 : * fields) */
5951 : char latName[16];/* Latitude field name */
5952 :
5953 :
5954 : /* Check for valid swath ID */
5955 : /* ------------------------ */
5956 0 : status = SWchkswid(swathID, "SWregionindex", &fid, &sdInterfaceID,
5957 : &swVgrpID);
5958 :
5959 :
5960 : /* Inclusion mode must be between 0 and 2 */
5961 : /* -------------------------------------- */
5962 0 : if (mode < 0 || mode > 2)
5963 : {
5964 0 : status = -1;
5965 0 : HEpush(DFE_GENAPP, "SWregionindex", __FILE__, __LINE__);
5966 0 : HEreport("Improper Inclusion Mode: %d.\n", mode);
5967 : }
5968 :
5969 :
5970 0 : if (status == 0)
5971 : {
5972 : /* Get "Longitude" field info */
5973 : /* -------------------------- */
5974 0 : statLon = SWfieldinfo(swathID, "Longitude", &rank, dims, &nt, dimlist);
5975 0 : if (statLon != 0)
5976 : {
5977 0 : status = -1;
5978 0 : HEpush(DFE_GENAPP, "SWregionindex", __FILE__, __LINE__);
5979 0 : HEreport("\"Longitude\" field not found.\n");
5980 : }
5981 :
5982 : /* Get "Latitude" field info */
5983 : /* -------------------------- */
5984 0 : statLat = SWfieldinfo(swathID, "Latitude", &rank, dims, &nt, dimlist);
5985 0 : if (statLat != 0)
5986 : {
5987 : /* If not found check for "Colatitude" field info */
5988 : /* ---------------------------------------------- */
5989 0 : statCoLat = SWfieldinfo(swathID, "Colatitude", &rank, dims, &nt,
5990 : dimlist);
5991 0 : if (statCoLat != 0)
5992 : {
5993 : /* Check again for Geodeticlatitude */
5994 0 : statGeodeticLat = SWfieldinfo(swathID,
5995 : "GeodeticLatitude", &rank,
5996 : dims, &nt, dimlist);
5997 0 : if (statGeodeticLat != 0)
5998 : {
5999 : /* Neither "Latitude" nor "Colatitude" field found */
6000 : /* ----------------------------------------------- */
6001 0 : status = -1;
6002 0 : HEpush(DFE_GENAPP, "SWregionindex", __FILE__, __LINE__);
6003 0 : HEreport(
6004 : "Neither \"Latitude\" nor \"Colatitude\" fields found.\n");
6005 : }
6006 : else
6007 : {
6008 : /* Latitude field is "Colatitude" */
6009 : /* ------------------------------ */
6010 0 : strcpy(latName, "GeodeticLatitude");
6011 : }
6012 : }
6013 : else
6014 : {
6015 : /* Latitude field is "Colatitude" */
6016 : /* ------------------------------ */
6017 0 : strcpy(latName, "Colatitude");
6018 : }
6019 : }
6020 : else
6021 : {
6022 : /* Latitude field is "Latitude" */
6023 : /* ---------------------------- */
6024 0 : strcpy(latName, "Latitude");
6025 : }
6026 :
6027 : /* This line modifies the dimlist variable so only the along-track */
6028 : /* dimension remains. */
6029 : /* --------------------------------------------------------------- */
6030 0 : (void) strtok(dimlist,",");
6031 0 : mapstatus = SWgeomapinfo(swathID,dimlist);
6032 0 : (void) strcpy(geodim,dimlist);
6033 :
6034 0 : if (status == 0)
6035 : {
6036 : /* Search along entire "Track" dimension from beginning to end */
6037 : /* ----------------------------------------------------------- */
6038 0 : start[0] = 0;
6039 0 : edge[0] = dims[0];
6040 :
6041 :
6042 : /* If 1D geolocation fields then set mode to MIDPOINT */
6043 : /* -------------------------------------------------- */
6044 0 : if (rank == 1)
6045 : {
6046 0 : mode = HDFE_MIDPOINT;
6047 : }
6048 :
6049 :
6050 0 : switch (mode)
6051 : {
6052 : /* If MIDPOINT search single point in middle of "CrossTrack" */
6053 : /* --------------------------------------------------------- */
6054 : case HDFE_MIDPOINT:
6055 :
6056 0 : start[1] = dims[1] / 2;
6057 0 : edge[1] = 1;
6058 :
6059 0 : break;
6060 :
6061 : /* If ENDPOINT search 2 points at either end of "CrossTrack" */
6062 : /* --------------------------------------------------------- */
6063 : case HDFE_ENDPOINT:
6064 :
6065 0 : start[1] = 0;
6066 0 : stride[1] = dims[1] - 1;
6067 0 : edge[1] = 2;
6068 :
6069 0 : break;
6070 :
6071 : /* If ANYPOINT do initial MIDPOINT search */
6072 : /* -------------------------------------- */
6073 : case HDFE_ANYPOINT:
6074 :
6075 0 : start[1] = dims[1] / 2;
6076 0 : edge[1] = 1;
6077 :
6078 : break;
6079 : }
6080 :
6081 :
6082 : /* Compute number of elements */
6083 : /* -------------------------- */
6084 0 : nElem = edge[0] * edge[1];
6085 :
6086 :
6087 : /* Allocate space for longitude and latitude (float64) */
6088 : /* --------------------------------------------------- */
6089 0 : lonArr = (char *) calloc(nElem, sizeof(float64));
6090 0 : if(lonArr == NULL)
6091 : {
6092 0 : HEpush(DFE_NOSPACE,"SWregionindex", __FILE__, __LINE__);
6093 0 : return(-1);
6094 : }
6095 :
6096 0 : latArr = (char *) calloc(nElem, sizeof(float64));
6097 0 : if(latArr == NULL)
6098 : {
6099 0 : HEpush(DFE_NOSPACE,"SWregionindex", __FILE__, __LINE__);
6100 0 : free(lonArr);
6101 0 : return(-1);
6102 : }
6103 :
6104 :
6105 : /* Allocate space for flag array (uint8) */
6106 : /* ------------------------------------- */
6107 0 : flag = (uint8 *) calloc(edge[0] + 1, 1);
6108 0 : if(flag == NULL)
6109 : {
6110 0 : HEpush(DFE_NOSPACE,"SWregionindex", __FILE__, __LINE__);
6111 0 : free(lonArr);
6112 0 : free(latArr);
6113 0 : return(-1);
6114 : }
6115 :
6116 :
6117 : /* Read Longitude and Latitude fields */
6118 : /* ---------------------------------- */
6119 0 : status = SWreadfield(swathID, "Longitude",
6120 : start, stride, edge, lonArr);
6121 0 : status = SWreadfield(swathID, latName,
6122 : start, stride, edge, latArr);
6123 :
6124 :
6125 :
6126 : /*
6127 : * If geolocation fields are FLOAT32 then cast each entry as
6128 : * FLOAT64
6129 : */
6130 0 : if (nt == DFNT_FLOAT32)
6131 : {
6132 0 : for (i = nElem - 1; i >= 0; i--)
6133 : {
6134 0 : memcpy(&temp32, lonArr + 4 * i, 4);
6135 0 : temp64 = (float64) temp32;
6136 0 : memcpy(lonArr + 8 * i, &temp64, 8);
6137 :
6138 0 : memcpy(&temp32, latArr + 4 * i, 4);
6139 0 : temp64 = (float64) temp32;
6140 0 : memcpy(latArr + 8 * i, &temp64, 8);
6141 : }
6142 : }
6143 :
6144 :
6145 : /* Set boundary flag */
6146 : /* ----------------- */
6147 :
6148 : /*
6149 : * This variable is set to 1 if the region of interest crosses
6150 : * the +/- 180 longitude boundary
6151 : */
6152 0 : bndflag = (cornerlon[0] < cornerlon[1]) ? 0 : 1;
6153 :
6154 :
6155 :
6156 : /* Main Search Loop */
6157 : /* ---------------- */
6158 :
6159 : /* For each track ... */
6160 : /* ------------------ */
6161 0 : for (i = 0; i < edge[0]; i++)
6162 : {
6163 : /* For each value from Cross Track ... */
6164 : /* ----------------------------------- */
6165 0 : for (j = 0; j < edge[1]; j++)
6166 : {
6167 : /* Read in single lon & lat values from data buffers */
6168 : /* ------------------------------------------------- */
6169 0 : memcpy(&lonTestVal, &lonArr[8 * (i * edge[1] + j)], 8);
6170 0 : memcpy(&latTestVal, &latArr[8 * (i * edge[1] + j)], 8);
6171 :
6172 :
6173 : /* If longitude value > 180 convert to -180 to 180 range */
6174 : /* ----------------------------------------------------- */
6175 0 : if (lonTestVal > 180)
6176 : {
6177 0 : lonTestVal = lonTestVal - 360;
6178 : }
6179 :
6180 : /* If Colatitude value convert to latitude value */
6181 : /* --------------------------------------------- */
6182 0 : if (statCoLat == 0)
6183 : {
6184 0 : latTestVal = 90 - latTestVal;
6185 : }
6186 :
6187 :
6188 : /* Test if lat value is within range */
6189 : /* --------------------------------- */
6190 0 : latTest = (latTestVal >= cornerlat[0] &&
6191 0 : latTestVal <= cornerlat[1]);
6192 :
6193 :
6194 0 : if (bndflag == 1)
6195 : {
6196 : /*
6197 : * If boundary flag set test whether longitude value
6198 : * is outside region and then flip
6199 : */
6200 0 : lonTest = (lonTestVal >= cornerlon[1] &&
6201 0 : lonTestVal <= cornerlon[0]);
6202 0 : lonTest = 1 - lonTest;
6203 : }
6204 : else
6205 : {
6206 0 : lonTest = (lonTestVal >= cornerlon[0] &&
6207 0 : lonTestVal <= cornerlon[1]);
6208 : }
6209 :
6210 :
6211 : /*
6212 : * If both longitude and latitude are within region set
6213 : * flag on for this track
6214 : */
6215 0 : if (lonTest + latTest == 2)
6216 : {
6217 0 : flag[i] = 1;
6218 0 : found = 1;
6219 0 : break;
6220 : }
6221 : }
6222 : }
6223 :
6224 :
6225 :
6226 : /* ANYPOINT search */
6227 : /* --------------- */
6228 0 : if (mode == HDFE_ANYPOINT && rank > 1)
6229 : {
6230 0 : free(lonArr);
6231 0 : free(latArr);
6232 :
6233 : /* Allocate space for an entire single cross track */
6234 : /* ----------------------------------------------- */
6235 0 : lonArr = (char *) calloc(dims[1], sizeof(float64));
6236 0 : if(lonArr == NULL)
6237 : {
6238 0 : HEpush(DFE_NOSPACE,"SWregionindex", __FILE__, __LINE__);
6239 0 : return(-1);
6240 : }
6241 0 : latArr = (char *) calloc(dims[1], sizeof(float64));
6242 0 : if(latArr == NULL)
6243 : {
6244 0 : HEpush(DFE_NOSPACE,"SWregionindex", __FILE__, __LINE__);
6245 0 : free(lonArr);
6246 0 : return(-1);
6247 : }
6248 :
6249 : /* Setup start and edge */
6250 : /* -------------------- */
6251 0 : anyStart[1] = 0;
6252 0 : anyEdge[0] = 1;
6253 0 : anyEdge[1] = dims[1];
6254 :
6255 :
6256 : /* For each track ... */
6257 : /* ------------------ */
6258 0 : for (i = 0; i < edge[0]; i++)
6259 : {
6260 :
6261 : /* If cross track not in region (with MIDPOINT search ... */
6262 : /* ------------------------------------------------------ */
6263 0 : if (flag[i] == 0)
6264 : {
6265 : /* Setup track start */
6266 : /* ----------------- */
6267 0 : anyStart[0] = i;
6268 :
6269 :
6270 : /* Read in lon and lat values for cross track */
6271 : /* ------------------------------------------ */
6272 0 : status = SWreadfield(swathID, "Longitude",
6273 : anyStart, NULL, anyEdge, lonArr);
6274 0 : status = SWreadfield(swathID, latName,
6275 : anyStart, NULL, anyEdge, latArr);
6276 :
6277 :
6278 :
6279 : /*
6280 : * If geolocation fields are FLOAT32 then cast each
6281 : * entry as FLOAT64
6282 : */
6283 0 : if (nt == DFNT_FLOAT32)
6284 : {
6285 0 : for (j = dims[1] - 1; j >= 0; j--)
6286 : {
6287 0 : memcpy(&temp32, lonArr + 4 * j, 4);
6288 0 : temp64 = (float64) temp32;
6289 0 : memcpy(lonArr + 8 * j, &temp64, 8);
6290 :
6291 0 : memcpy(&temp32, latArr + 4 * j, 4);
6292 0 : temp64 = (float64) temp32;
6293 0 : memcpy(latArr + 8 * j, &temp64, 8);
6294 : }
6295 : }
6296 :
6297 :
6298 : /* For each value from Cross Track ... */
6299 : /* ----------------------------------- */
6300 0 : for (j = 0; j < dims[1]; j++)
6301 : {
6302 : /* Read in single lon & lat values from buffers */
6303 : /* -------------------------------------------- */
6304 0 : memcpy(&lonTestVal, &lonArr[8 * j], 8);
6305 0 : memcpy(&latTestVal, &latArr[8 * j], 8);
6306 :
6307 :
6308 : /* If lon value > 180 convert to -180 - 180 range */
6309 : /* ---------------------------------------------- */
6310 0 : if (lonTestVal > 180)
6311 : {
6312 0 : lonTestVal = lonTestVal - 360;
6313 : }
6314 :
6315 : /* If Colatitude value convert to latitude value */
6316 : /* --------------------------------------------- */
6317 0 : if (statCoLat == 0)
6318 : {
6319 0 : latTestVal = 90 - latTestVal;
6320 : }
6321 :
6322 :
6323 : /* Test if lat value is within range */
6324 : /* --------------------------------- */
6325 0 : latTest = (latTestVal >= cornerlat[0] &&
6326 0 : latTestVal <= cornerlat[1]);
6327 :
6328 :
6329 0 : if (bndflag == 1)
6330 : {
6331 : /*
6332 : * If boundary flag set test whether
6333 : * longitude value is outside region and then
6334 : * flip
6335 : */
6336 0 : lonTest = (lonTestVal >= cornerlon[1] &&
6337 0 : lonTestVal <= cornerlon[0]);
6338 0 : lonTest = 1 - lonTest;
6339 : }
6340 : else
6341 : {
6342 0 : lonTest = (lonTestVal >= cornerlon[0] &&
6343 0 : lonTestVal <= cornerlon[1]);
6344 : }
6345 :
6346 :
6347 : /*
6348 : * If both longitude and latitude are within
6349 : * region set flag on for this track
6350 : */
6351 0 : if (lonTest + latTest == 2)
6352 : {
6353 0 : flag[i] = 1;
6354 0 : found = 1;
6355 0 : break;
6356 : }
6357 : }
6358 : }
6359 : }
6360 : }
6361 : /*
6362 : for (j1 = 0; j1 < edge[0]; j1++)
6363 : {
6364 : idxrange[j1] = (int32) flag[j1];
6365 : }
6366 : */
6367 : /* If within region setup Region Structure */
6368 : /* --------------------------------------- */
6369 0 : if (found == 1)
6370 : {
6371 : /* For all entries in SWXRegion array ... */
6372 : /* -------------------------------------- */
6373 0 : for (k = 0; k < NSWATHREGN; k++)
6374 : {
6375 : /* If empty region ... */
6376 : /* ------------------- */
6377 0 : if (SWXRegion[k] == 0)
6378 : {
6379 : /* Allocate space for region entry */
6380 : /* ------------------------------- */
6381 0 : SWXRegion[k] = (struct swathRegion *)
6382 : calloc(1, sizeof(struct swathRegion));
6383 0 : if(SWXRegion[k] == NULL)
6384 : {
6385 0 : HEpush(DFE_NOSPACE,"SWregionindex", __FILE__, __LINE__);
6386 0 : return(-1);
6387 : }
6388 :
6389 : /* Store file and swath ID */
6390 : /* ----------------------- */
6391 0 : SWXRegion[k]->fid = fid;
6392 0 : SWXRegion[k]->swathID = swathID;
6393 :
6394 :
6395 : /* Set Start & Stop Vertical arrays to -1 */
6396 : /* -------------------------------------- */
6397 0 : for (j = 0; j < 8; j++)
6398 : {
6399 0 : SWXRegion[k]->StartVertical[j] = -1;
6400 0 : SWXRegion[k]->StopVertical[j] = -1;
6401 0 : SWXRegion[k]->StartScan[j] = -1;
6402 0 : SWXRegion[k]->StopScan[j] = -1;
6403 : }
6404 :
6405 :
6406 : /* Set region ID */
6407 : /* ------------- */
6408 0 : regionID = k;
6409 0 : break;
6410 : }
6411 : }
6412 0 : if (k >= NSWATHREGN)
6413 : {
6414 0 : HEpush(DFE_GENAPP, "SWregionindex", __FILE__, __LINE__);
6415 0 : HEreport(
6416 : "regionID exceeded NSWATHREGN.\n");
6417 0 : return (-1);
6418 : }
6419 :
6420 : /* Find start and stop of regions */
6421 : /* ------------------------------ */
6422 :
6423 : /* Subtract previous flag value from current one */
6424 : /* --------------------------------------------- */
6425 :
6426 : /*
6427 : * Transisition points will have flag value (+1) start or
6428 : * (255 = (uint8) -1) stop of region
6429 : */
6430 0 : for (i = edge[0]; i > 0; i--)
6431 : {
6432 0 : flag[i] -= flag[i - 1];
6433 : }
6434 :
6435 :
6436 0 : for (i = 0; i <= edge[0]; i++)
6437 : {
6438 : /* Start of region */
6439 : /* --------------- */
6440 0 : if (flag[i] == 1)
6441 : {
6442 : /* Delyth Jones Moved the increment of the region down
6443 : to next if statement j = ++SWXRegion[k]->nRegions; */
6444 :
6445 : /* using temp value, if not equal to stop region
6446 : invalid region otherwise ok Delyth Jones */
6447 0 : tmpVal = i+1;
6448 : }
6449 :
6450 : /* End of region */
6451 : /* ------------- */
6452 0 : if (flag[i] == 255)
6453 : {
6454 0 : if( tmpVal!=i )
6455 : {
6456 : /* Increment (multiple) region counter */
6457 : /* ----------------------------------- */
6458 0 : j = ++SWXRegion[k]->nRegions;
6459 :
6460 0 : if (mapstatus == 2)
6461 : {
6462 0 : l = i;
6463 0 : if ((tmpVal-1) % 2 == 1)
6464 : {
6465 0 : tmpVal = tmpVal + 1;
6466 : }
6467 :
6468 0 : if ((l-1) % 2 == 0)
6469 : {
6470 0 : l = l - 1;
6471 : }
6472 : }
6473 0 : SWXRegion[k]->StartRegion[j - 1] = tmpVal-1;
6474 0 : idxrange[0] = tmpVal - 1;
6475 0 : SWXRegion[k]->StopRegion[j - 1] = l - 1;
6476 0 : idxrange[1] = l - 1;
6477 0 : validReg = 0;
6478 : }
6479 : }
6480 :
6481 : }
6482 :
6483 : }
6484 0 : free(lonArr);
6485 0 : free(latArr);
6486 0 : free(flag);
6487 : }
6488 : }
6489 0 : if(validReg==0)
6490 : {
6491 0 : return (regionID);
6492 : }
6493 : else
6494 : {
6495 0 : return (-1);
6496 : }
6497 :
6498 : }
6499 :
6500 :
6501 :
6502 : /*----------------------------------------------------------------------------|
6503 : | BEGIN_PROLOG |
6504 : | |
6505 : | FUNCTION: SWdeftimeperiod |
6506 : | |
6507 : | DESCRIPTION: Finds swath cross tracks observed during time period and |
6508 : | returns period ID |
6509 : | |
6510 : | region ID |
6511 : | DESCRIPTION: |
6512 : | |
6513 : | |
6514 : | Return Value Type Units Description |
6515 : | ============ ====== ========= ===================================== |
6516 : | periodID int32 (Period ID) or (-1) if failed |
6517 : | |
6518 : | INPUTS: |
6519 : | swathID int32 Swath structure ID |
6520 : | starttime float64 TAI sec Start of time period |
6521 : | stoptime float64 TAI sec Stop of time period |
6522 : | mode int32 Search mode |
6523 : | HDFE_MIDPOINT - Use midpoint of Xtrack |
6524 : | HDFE_ENDPOINT - Use endpoints of Xtrack |
6525 : | |
6526 : | OUTPUTS: |
6527 : | None |
6528 : | |
6529 : | NOTES: |
6530 : | |
6531 : | |
6532 : | Date Programmer Description |
6533 : | ====== ============ ================================================= |
6534 : | Jun 96 Joel Gales Original Programmer |
6535 : | |
6536 : | END_PROLOG |
6537 : -----------------------------------------------------------------------------*/
6538 : int32
6539 0 : SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime,
6540 : int32 mode)
6541 : {
6542 :
6543 : intn i; /* Loop index */
6544 : intn j; /* Loop index */
6545 : intn k; /* Loop index */
6546 : intn status; /* routine return status variable */
6547 : intn statTime; /* Status from SWfieldinfo for time */
6548 :
6549 0 : uint8 found = 0; /* Found flag */
6550 :
6551 : int32 fid; /* HDF-EOS file ID */
6552 : int32 sdInterfaceID; /* HDF SDS interface ID */
6553 : int32 swVgrpID; /* Swath Vgroup ID */
6554 : int32 rank; /* Rank of geolocation fields */
6555 : int32 nt; /* Number type of geolocation fields */
6556 : int32 dims[8]; /* Dimensions of geolocation fields */
6557 : int32 start[2]; /* Start array (read) */
6558 0 : int32 stride[2] = {1, 1}; /* Stride array (read) */
6559 : int32 edge[2]; /* Edge array (read) */
6560 0 : int32 periodID = -1; /* Period ID (return) */
6561 : int32 dum; /* Dummy (loop) variable */
6562 :
6563 : float64 time64Test; /* Time test value */
6564 0 : float64 *time64 = NULL; /* Time data array */
6565 :
6566 : char dimlist[256]; /* Dimension list (geolocation fields) */
6567 :
6568 : /* Check for valid swath ID */
6569 : /* ------------------------ */
6570 0 : status = SWchkswid(swathID, "SWdeftimeperiod", &fid, &sdInterfaceID,
6571 : &swVgrpID);
6572 :
6573 0 : if (status == 0)
6574 : {
6575 : /* Get "Time" field info */
6576 : /* --------------------- */
6577 0 : statTime = SWfieldinfo(swathID, "Time", &rank, dims, &nt, dimlist);
6578 0 : if (statTime != 0)
6579 : {
6580 0 : status = -1;
6581 0 : HEpush(DFE_GENAPP, "SWdeftimeperiod", __FILE__, __LINE__);
6582 0 : HEreport("\"Time\" field not found.\n");
6583 : }
6584 :
6585 0 : if (status == 0)
6586 : {
6587 : /* Search along entire "Track" dimension from beginning to end */
6588 : /* ----------------------------------------------------------- */
6589 0 : start[0] = 0;
6590 0 : edge[0] = dims[0];
6591 :
6592 :
6593 : /* If 1D geolocation fields then set mode to MIDPOINT */
6594 : /* -------------------------------------------------- */
6595 0 : if (rank == 1)
6596 : {
6597 0 : mode = HDFE_MIDPOINT;
6598 : }
6599 :
6600 :
6601 0 : switch (mode)
6602 : {
6603 :
6604 : /* If MIDPOINT search single point in middle of "CrossTrack" */
6605 : /* --------------------------------------------------------- */
6606 : case HDFE_MIDPOINT:
6607 :
6608 0 : start[1] = dims[1] / 2;
6609 0 : edge[1] = 1;
6610 :
6611 :
6612 : /* Allocate space for time data */
6613 : /* ---------------------------- */
6614 0 : time64 = (float64 *) calloc(edge[0], 8);
6615 0 : if(time64 == NULL)
6616 : {
6617 0 : HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__);
6618 0 : return(-1);
6619 : }
6620 :
6621 :
6622 : /* Read "Time" field */
6623 : /* ----------------- */
6624 0 : status = SWreadfield(swathID, "Time",
6625 : start, NULL, edge, time64);
6626 0 : break;
6627 :
6628 :
6629 : /* If ENDPOINT search 2 points at either end of "CrossTrack" */
6630 : /* --------------------------------------------------------- */
6631 : case HDFE_ENDPOINT:
6632 0 : start[1] = 0;
6633 0 : stride[1] = dims[1] - 1;
6634 0 : edge[1] = 2;
6635 :
6636 :
6637 : /* Allocate space for time data */
6638 : /* ---------------------------- */
6639 0 : time64 = (float64 *) calloc(edge[0] * 2, 8);
6640 0 : if(time64 == NULL)
6641 : {
6642 0 : HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__);
6643 0 : return(-1);
6644 : }
6645 :
6646 : /* Read "Time" field */
6647 : /* ----------------- */
6648 0 : status = SWreadfield(swathID, "Time",
6649 : start, stride, edge, time64);
6650 : break;
6651 :
6652 : }
6653 :
6654 0 : if (time64)
6655 : {
6656 : /* For each track (from top) ... */
6657 : /* ----------------------------- */
6658 0 : for (i = 0; i < edge[0]; i++)
6659 : {
6660 : /* For each value from Cross Track ... */
6661 : /* ----------------------------------- */
6662 0 : for (j = 0; j < edge[1]; j++)
6663 : {
6664 :
6665 : /* Get time test value */
6666 : /* ------------------- */
6667 0 : time64Test = time64[i * edge[1] + j];
6668 :
6669 :
6670 : /* If within time period ... */
6671 : /* ------------------------- */
6672 0 : if (time64Test >= starttime &&
6673 0 : time64Test <= stoptime)
6674 : {
6675 : /* Set found flag */
6676 : /* -------------- */
6677 0 : found = 1;
6678 :
6679 :
6680 : /* For all entries in SWXRegion array ... */
6681 : /* -------------------------------------- */
6682 0 : for (k = 0; k < NSWATHREGN; k++)
6683 : {
6684 : /* If empty region ... */
6685 : /* ------------------- */
6686 0 : if (SWXRegion[k] == 0)
6687 : {
6688 : /* Allocate space for region entry */
6689 : /* ------------------------------- */
6690 0 : SWXRegion[k] = (struct swathRegion *)
6691 : calloc(1, sizeof(struct swathRegion));
6692 0 : if(SWXRegion[k] == NULL)
6693 : {
6694 0 : HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__);
6695 0 : return(-1);
6696 : }
6697 :
6698 : /* Store file and swath ID */
6699 : /* ----------------------- */
6700 0 : SWXRegion[k]->fid = fid;
6701 0 : SWXRegion[k]->swathID = swathID;
6702 :
6703 :
6704 : /* Set number of isolated regions to 1 */
6705 : /* ----------------------------------- */
6706 0 : SWXRegion[k]->nRegions = 1;
6707 :
6708 :
6709 : /* Set start of region to first track found */
6710 : /* ---------------------------------------- */
6711 0 : SWXRegion[k]->StartRegion[0] = i;
6712 :
6713 :
6714 : /* Set Start & Stop Vertical arrays to -1 */
6715 : /* -------------------------------------- */
6716 0 : for (dum = 0; dum < 8; dum++)
6717 : {
6718 0 : SWXRegion[k]->StartVertical[dum] = -1;
6719 0 : SWXRegion[k]->StopVertical[dum] = -1;
6720 0 : SWXRegion[k]->StartScan[dum] = -1;
6721 0 : SWXRegion[k]->StopScan[dum] = -1;
6722 : }
6723 :
6724 :
6725 : /* Set period ID */
6726 : /* ------------- */
6727 0 : periodID = k;
6728 :
6729 0 : break; /* Break from "k" loop */
6730 : }
6731 : }
6732 : }
6733 0 : if (found == 1)
6734 : {
6735 0 : break; /* Break from "j" loop */
6736 : }
6737 : }
6738 0 : if (found == 1)
6739 : {
6740 0 : break; /* Break from "i" loop */
6741 : }
6742 : }
6743 :
6744 :
6745 :
6746 : /* Clear found flag */
6747 : /* ---------------- */
6748 0 : found = 0;
6749 :
6750 :
6751 : /* For each track (from bottom) ... */
6752 : /* -------------------------------- */
6753 0 : for (i = edge[0] - 1; i >= 0; i--)
6754 : {
6755 : /* For each value from Cross Track ... */
6756 : /* ----------------------------------- */
6757 0 : for (j = 0; j < edge[1]; j++)
6758 : {
6759 :
6760 : /* Get time test value */
6761 : /* ------------------- */
6762 0 : time64Test = time64[i * edge[1] + j];
6763 :
6764 :
6765 : /* If within time period ... */
6766 : /* ------------------------- */
6767 0 : if (time64Test >= starttime &&
6768 0 : time64Test <= stoptime)
6769 : {
6770 : /* Set found flag */
6771 : /* -------------- */
6772 0 : found = 1;
6773 :
6774 : /* Set start of region to first track found */
6775 : /* ---------------------------------------- */
6776 0 : SWXRegion[k]->StopRegion[0] = i;
6777 :
6778 0 : break; /* Break from "j" loop */
6779 : }
6780 : }
6781 0 : if (found == 1)
6782 : {
6783 0 : break; /* Break from "i" loop */
6784 : }
6785 : }
6786 :
6787 0 : free(time64);
6788 : }
6789 : }
6790 : }
6791 :
6792 0 : return (periodID);
6793 : }
6794 :
6795 : /*----------------------------------------------------------------------------|
6796 : | BEGIN_PROLOG |
6797 : | |
6798 : | FUNCTION: SWextractregion |
6799 : | |
6800 : | DESCRIPTION: Retrieves data from specified region. |
6801 : | |
6802 : | |
6803 : | Return Value Type Units Description |
6804 : | ============ ====== ========= ===================================== |
6805 : | status intn return status (0) SUCCEED, (-1) FAIL |
6806 : | |
6807 : | INPUTS: |
6808 : | swathID int32 Swath structure ID |
6809 : | regionID int32 Region ID |
6810 : | fieldname char Fieldname |
6811 : | externalflag int32 External geolocation fields flag |
6812 : | HDFE_INTERNAL (0) |
6813 : | HDFE_EXTERNAL (1) |
6814 : | |
6815 : | OUTPUTS: |
6816 : | buffer void Data buffer containing subsetted region |
6817 : | |
6818 : | NOTES: |
6819 : | |
6820 : | |
6821 : | Date Programmer Description |
6822 : | ====== ============ ================================================= |
6823 : | Jun 96 Joel Gales Original Programmer |
6824 : | Aug 96 Joel Gales Add vertical subsetting |
6825 : | Oct 96 Joel Gales Mapping offset value not read from SWmapinfo |
6826 : | Dec 96 Joel Gales Vert Subset overwriting data buffer |
6827 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
6828 : | Mar 97 Joel Gales Add support for index mapping |
6829 : | Jul 99 DaW Add support for floating scene subsetting |
6830 : | Feb 03 Terry Haran/ |
6831 : | Abe Taaheri Forced map offset to 0 so that data is extracted |
6832 : | without offset consideration. This will preserve |
6833 : | original mapping between geofields and the data |
6834 : | field. |
6835 : | |
6836 : | END_PROLOG |
6837 : -----------------------------------------------------------------------------*/
6838 : intn
6839 0 : SWextractregion(int32 swathID, int32 regionID, char *fieldname,
6840 : int32 externalflag, VOIDP buffer)
6841 :
6842 : {
6843 : intn i; /* Loop index */
6844 : intn j; /* Loop index */
6845 : intn k; /* Loop index */
6846 : intn l; /* Loop index */
6847 : intn status; /* routine return status variable */
6848 0 : intn long_status = 3; /* routine return status variable */
6849 : /* for longitude */
6850 0 : intn land_status = 3; /* Used for L7 float scene sub. */
6851 0 : intn statMap = -1; /* Status from SWmapinfo */
6852 :
6853 0 : uint8 found = 0; /* Found flag */
6854 0 : uint8 vfound = 0; /* Found flag for vertical subsetting*/
6855 : /* --- xhua */
6856 0 : uint8 scene_cnt = 0; /* Used for L7 float scene sub. */
6857 0 : uint8 detect_cnt = 0; /* Used to convert scan to scanline */
6858 : /* L7 float scene sub. */
6859 :
6860 : int32 fid; /* HDF-EOS file ID */
6861 : int32 sdInterfaceID; /* HDF SDS interface ID */
6862 : int32 swVgrpID; /* Swath Vgroup ID */
6863 :
6864 0 : int32 numtype = 0; /* Used for L7 float scene sub. */
6865 0 : int32 count = 0; /* Used for L7 float scene sub. */
6866 : int32 index; /* Geo Dim Index */
6867 : int32 nDim; /* Number of dimensions */
6868 : int32 slen[64]; /* String length array */
6869 : int32 dum; /* Dummy variable */
6870 : int32 offset; /* Mapping offset */
6871 : int32 incr; /* Mapping increment */
6872 : int32 nXtrk; /* Number of cross tracks */
6873 0 : int32 scan_shift = 0; /* Used to take out partial scans */
6874 : int32 dumdims[8]; /* Dimensions from SWfieldinfo */
6875 : int32 start[8]; /* Start array for data read */
6876 : int32 edge[8]; /* Edge array for data read */
6877 : int32 dims[8]; /* Dimensions */
6878 0 : int32 rank = 0; /* Field rank */
6879 0 : int32 rk = 0; /* Field rank */
6880 0 : int32 ntype = 0; /* Field number type */
6881 : int32 bufOffset; /* Output buffer offset */
6882 : int32 size; /* Size of data buffer */
6883 0 : int32 idxMapElem = -1; /* Number of index map elements */
6884 0 : int32 *idxmap = NULL; /* Pointer to index mapping array */
6885 :
6886 0 : int32 startscanline = 0;
6887 0 : int32 stopscanline = 0;
6888 0 : char *dfieldlist = (char *)NULL;
6889 0 : int32 strbufsize = 0;
6890 : int32 dfrank[8];
6891 : int32 numtype2[8];
6892 0 : uint16 *buffer2 = (uint16 *)NULL;
6893 0 : uint16 *tbuffer = (uint16 *)NULL;
6894 : int32 dims2[8];
6895 0 : int32 nt = 0;
6896 0 : int32 startscandim = -1;
6897 0 : int32 stopscandim = -1;
6898 0 : int32 rank2 = 0;
6899 :
6900 : char dimlist[256]; /* Dimension list */
6901 : char geodim[256];/* Geolocation field dimension list */
6902 : char tgeodim[256];/* Time field dimension list */
6903 : char dgeodim[256];/* Data field dimension list for subsetting */
6904 : char utlbuf[256];/* Utility buffer */
6905 : char *ptr[64]; /* String pointer array */
6906 :
6907 :
6908 :
6909 : /* Check for valid swath ID */
6910 : /* ------------------------ */
6911 0 : status = SWchkswid(swathID, "SWextractregion", &fid, &sdInterfaceID,
6912 : &swVgrpID);
6913 :
6914 :
6915 : /* Check for valid region ID */
6916 : /* ------------------------- */
6917 0 : if (status == 0)
6918 : {
6919 0 : if (regionID < 0 || regionID >= NSWATHREGN)
6920 : {
6921 0 : status = -1;
6922 0 : HEpush(DFE_RANGE, "SWextractregion", __FILE__, __LINE__);
6923 0 : HEreport("Invalid Region id: %d.\n", regionID);
6924 : }
6925 : }
6926 :
6927 :
6928 :
6929 : /* Check for active region ID */
6930 : /* -------------------------- */
6931 0 : if (status == 0)
6932 : {
6933 0 : if (SWXRegion[regionID] == 0)
6934 : {
6935 0 : status = -1;
6936 0 : HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
6937 0 : HEreport("Inactive Region ID: %d.\n", regionID);
6938 : }
6939 : }
6940 :
6941 :
6942 : /* This code checks for the attribute detector_count */
6943 : /* which is found in Landsat 7 files. It is used */
6944 : /* for some of the loops. */
6945 : /* ================================================= */
6946 0 : if (SWXRegion[regionID]->scanflag == 1)
6947 : {
6948 0 : land_status = SWattrinfo(swathID, "detector_count", &numtype, &count);
6949 0 : if (land_status == 0)
6950 0 : land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
6951 : }
6952 :
6953 : /* Check that geo file and data file are same for INTERNAL subsetting */
6954 : /* ------------------------------------------------------------------ */
6955 0 : if (status == 0)
6956 : {
6957 0 : if (SWXRegion[regionID]->fid != fid && externalflag != HDFE_EXTERNAL)
6958 : {
6959 0 : status = -1;
6960 0 : HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
6961 0 : HEreport("Region is not defined for this file.\n");
6962 : }
6963 : }
6964 :
6965 :
6966 :
6967 : /* Check that geo swath and data swath are same for INTERNAL subsetting */
6968 : /* -------------------------------------------------------------------- */
6969 0 : if (status == 0)
6970 : {
6971 0 : if (SWXRegion[regionID]->swathID != swathID &&
6972 : externalflag != HDFE_EXTERNAL)
6973 : {
6974 0 : status = -1;
6975 0 : HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
6976 0 : HEreport("Region is not defined for this Swath.\n");
6977 : }
6978 : }
6979 :
6980 :
6981 :
6982 : /* Check for valid fieldname */
6983 : /* ------------------------- */
6984 0 : if (status == 0)
6985 : {
6986 :
6987 : /* Get data field info */
6988 : /* ------------------- */
6989 0 : status = SWfieldinfo(swathID, fieldname, &rank,
6990 : dims, &ntype, dimlist);
6991 :
6992 0 : if (status != 0)
6993 : {
6994 0 : status = -1;
6995 0 : HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
6996 0 : HEreport("Field \"%s\" Not Found.\n", fieldname);
6997 : }
6998 : }
6999 :
7000 :
7001 : /* No problems so proceed ... */
7002 : /* -------------------------- */
7003 0 : if (status == 0)
7004 : {
7005 :
7006 :
7007 : /* Initialize start and edge for all dimensions */
7008 : /* -------------------------------------------- */
7009 0 : for (j = 0; j < rank; j++)
7010 : {
7011 0 : start[j] = 0;
7012 0 : edge[j] = dims[j];
7013 : }
7014 :
7015 :
7016 : /* Vertical Subset */
7017 : /* --------------- */
7018 0 : for (j = 0; j < 8; j++)
7019 : {
7020 : /* If active vertical subset ... */
7021 : /* ----------------------------- */
7022 0 : if (SWXRegion[regionID]->StartVertical[j] != -1)
7023 : {
7024 :
7025 : /* Find vertical dimension within dimlist */
7026 : /* -------------------------------------- */
7027 0 : dum = EHstrwithin(SWXRegion[regionID]->DimNamePtr[j],
7028 : dimlist, ',');
7029 :
7030 : /* If dimension found ... */
7031 : /* ---------------------- */
7032 0 : if (dum != -1)
7033 : {
7034 : /* Compute start and edge for vertical dimension */
7035 : /* --------------------------------------------- */
7036 0 : vfound = 1; /* xhua */
7037 0 : start[dum] = SWXRegion[regionID]->StartVertical[j];
7038 0 : edge[dum] = SWXRegion[regionID]->StopVertical[j] -
7039 0 : SWXRegion[regionID]->StartVertical[j] + 1;
7040 : }
7041 : else
7042 : {
7043 : /* Vertical dimension not found */
7044 : /* ---------------------------- */
7045 0 : status = -1;
7046 0 : HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
7047 0 : HEreport("Vertical Dimension Not Found: \"%s\".\n",
7048 0 : SWXRegion[regionID]->DimNamePtr);
7049 : }
7050 : }
7051 : } /* End of Vertical Subset loop */
7052 :
7053 :
7054 :
7055 : /* No problems so proceed ... */
7056 : /* -------------------------- */
7057 0 : if (status == 0)
7058 : {
7059 : /* If non-vertical subset regions defined ... */
7060 : /* ------------------------------------------ */
7061 0 : if (SWXRegion[regionID]->nRegions > 0)
7062 : {
7063 :
7064 : /* Get geolocation dimension name */
7065 : /* ------------------------------ */
7066 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,
7067 : "Longitude", &dum,
7068 : dumdims, &dum, geodim);
7069 0 : long_status = status;
7070 :
7071 : /* If Time field being used, check for dimensions */
7072 : /* ---------------------------------------------- */
7073 0 : if (timeflag == 1)
7074 : {
7075 : /* code change to fix time subset bug for Landsat7 */
7076 :
7077 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,
7078 : "Time", &dum,
7079 : dumdims, &dum, tgeodim);
7080 :
7081 0 : if (strcmp(geodim, tgeodim) != 0)
7082 : {
7083 0 : strcpy(geodim, tgeodim);
7084 : }
7085 : }
7086 0 : timeflag = 0;
7087 :
7088 : /* If defscanregion being used, get dimensions */
7089 : /* of field being used */
7090 : /* ---------------------------------------------- */
7091 0 : if (SWXRegion[regionID]->scanflag == 1)
7092 : {
7093 0 : (void) SWnentries(SWXRegion[regionID]->swathID,4,&strbufsize);
7094 0 : dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char));
7095 0 : (void) SWinqdatafields(SWXRegion[regionID]->swathID,dfieldlist,dfrank,numtype2);
7096 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,dfieldlist,&dum,dumdims,&dum,dgeodim);
7097 :
7098 : /* The dimensions have to be switched, because */
7099 : /* the mappings force a geodim and datadim */
7100 : /* so to find the mapping, the dimensions must */
7101 : /* be switched, but the subsetting will still */
7102 : /* be based on the correct dimensions */
7103 : /* ------------------------------------------- */
7104 0 : if (strcmp(dgeodim,dimlist) != 0 || long_status == -1)
7105 : {
7106 0 : strcpy(geodim,dimlist);
7107 0 : strcpy(dimlist,dgeodim);
7108 : }
7109 : }
7110 :
7111 :
7112 : /* Get "Track" (first) Dimension from geo dimlist */
7113 : /* ---------------------------------------------- */
7114 0 : nDim = EHparsestr(geodim, ',', ptr, slen);
7115 0 : geodim[slen[0]] = 0;
7116 :
7117 :
7118 : /* Parse Data Field Dimlist & find mapping */
7119 : /* --------------------------------------- */
7120 0 : nDim = EHparsestr(dimlist, ',', ptr, slen);
7121 :
7122 :
7123 : /* Loop through all dimensions and search for mapping */
7124 : /* -------------------------------------------------- */
7125 0 : for (i = 0; i < nDim; i++)
7126 : {
7127 0 : memcpy(utlbuf, ptr[i], slen[i]);
7128 0 : utlbuf[slen[i]] = 0;
7129 0 : statMap = SWmapinfo(swathID, geodim, utlbuf,
7130 : &offset, &incr);
7131 :
7132 :
7133 : /*
7134 : * Force offset to 0.
7135 : * We're not changing the mapping, so we want
7136 : * the original offset to apply to the subsetted data.
7137 : * Otherwise, bad things happen, such as subsetting
7138 : * past the end of the original data, and being unable
7139 : * to read the first <offset> elements of the
7140 : * original data.
7141 : * The offset is only important for aligning the
7142 : * data with interpolated (incr > 0) or decimated
7143 : * (incr < 0) geolocation information for the data.
7144 : */
7145 :
7146 0 : offset = 0;
7147 :
7148 :
7149 : /* Mapping found */
7150 : /* ------------- */
7151 0 : if (statMap == 0)
7152 : {
7153 0 : found = 1;
7154 0 : index = i;
7155 0 : break;
7156 : }
7157 : }
7158 :
7159 :
7160 : /* If mapping not found check for geodim within dimlist */
7161 : /* ---------------------------------------------------- */
7162 0 : if (found == 0)
7163 : {
7164 0 : index = EHstrwithin(geodim, dimlist, ',');
7165 :
7166 : /* Geo dimension found within subset field dimlist */
7167 : /* ----------------------------------------------- */
7168 0 : if (index != -1)
7169 : {
7170 0 : found = 1;
7171 0 : offset = 0;
7172 0 : incr = 1;
7173 : }
7174 : }
7175 :
7176 :
7177 :
7178 : /* If mapping not found check for indexed mapping */
7179 : /* ---------------------------------------------- */
7180 0 : if (found == 0)
7181 : {
7182 : /* Get size of geo dim & allocate space of index mapping */
7183 : /* ----------------------------------------------------- */
7184 0 : dum = SWdiminfo(swathID, geodim);
7185 :
7186 : /* For Landsat files, the index mapping has two values */
7187 : /* for each point, a left and right point. So for a 37 */
7188 : /* scene band file there are 2x2 points for each scene */
7189 : /* meaning, 2x2x37 = 148 values. The above function */
7190 : /* only returns the number of values in the track */
7191 : /* dimension. */
7192 : /* ----------------------------------------------------- */
7193 0 : if(land_status == 0)
7194 0 : if(strcmp(fieldname, "Latitude") == 0 ||
7195 0 : strcmp(fieldname, "Longitude") == 0)
7196 : {
7197 0 : dum = dum * 2;
7198 : }
7199 0 : idxmap = (int32 *) calloc(dum, sizeof(int32));
7200 0 : if(idxmap == NULL)
7201 : {
7202 0 : HEpush(DFE_NOSPACE,"SWextractregion", __FILE__, __LINE__);
7203 0 : return(-1);
7204 : }
7205 :
7206 : /* Loop through all dimensions and search for mapping */
7207 : /* -------------------------------------------------- */
7208 0 : for (i = 0; i < nDim; i++)
7209 : {
7210 0 : memcpy(utlbuf, ptr[i], slen[i]);
7211 0 : utlbuf[slen[i]] = 0;
7212 :
7213 0 : idxMapElem =
7214 0 : SWidxmapinfo(swathID, geodim, utlbuf, idxmap);
7215 :
7216 :
7217 : /* Mapping found */
7218 : /* ------------- */
7219 0 : if (idxMapElem != -1)
7220 : {
7221 0 : found = 1;
7222 0 : index = i;
7223 0 : break;
7224 : }
7225 : }
7226 : }
7227 :
7228 :
7229 : /* If regular mapping found ... */
7230 : /* ---------------------------- */
7231 0 : if (found == 1 && idxMapElem == -1)
7232 : {
7233 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
7234 : {
7235 0 : if (k > 0)
7236 : {
7237 : /* Compute size in bytes of previous region */
7238 : /* ---------------------------------------- */
7239 0 : size = edge[0];
7240 0 : for (j = 1; j < rank; j++)
7241 : {
7242 0 : size *= edge[j];
7243 : }
7244 0 : size *= DFKNTsize(ntype);
7245 :
7246 :
7247 : /* Compute output buffer offset */
7248 : /* ---------------------------- */
7249 0 : bufOffset += size;
7250 : }
7251 : else
7252 : {
7253 : /* Initialize output buffer offset */
7254 : /* ------------------------------- */
7255 0 : bufOffset = 0;
7256 : }
7257 :
7258 :
7259 : /* Compute number of cross tracks in region */
7260 : /* ---------------------------------------- */
7261 0 : nXtrk = SWXRegion[regionID]->StopRegion[k] -
7262 0 : SWXRegion[regionID]->StartRegion[k] + 1;
7263 :
7264 :
7265 : /* Positive increment (geodim <= datadim) */
7266 : /* -------------------------------------- */
7267 0 : if (incr > 0)
7268 : {
7269 0 : if (SWXRegion[regionID]->scanflag == 1)
7270 : {
7271 0 : start[index] = SWXRegion[regionID]->StartRegion[k]/incr;
7272 0 : if(SWXRegion[regionID]->band8flag == 2 ||
7273 0 : SWXRegion[regionID]->band8flag == 3)
7274 : {
7275 0 : start[index] = (SWXRegion[regionID]->StartRegion[k]+detect_cnt)/incr;
7276 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank,dims2,&nt,dimlist);
7277 0 : buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
7278 0 : status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
7279 0 : if(incr == 1)
7280 0 : start[index] = start[index] - (buffer2[0] * detect_cnt);
7281 : else
7282 0 : start[index] = start[index] - buffer2[0];
7283 0 : free(buffer2);
7284 : }
7285 0 : scan_shift = nXtrk % incr;
7286 0 : if(scan_shift != 0)
7287 0 : nXtrk = nXtrk - scan_shift;
7288 0 : edge[index] = nXtrk / incr;
7289 0 : if (nXtrk % incr != 0)
7290 0 : edge[index]++;
7291 0 : if(long_status == -1 || incr == 1)
7292 : {
7293 0 : scan_shift = nXtrk % detect_cnt;
7294 0 : if(scan_shift != 0)
7295 0 : edge[index] = nXtrk - scan_shift;
7296 : }
7297 :
7298 : }
7299 : else
7300 : {
7301 0 : start[index] = SWXRegion[regionID]->StartRegion[k] * incr + offset;
7302 0 : edge[index] = nXtrk * incr - offset;
7303 : }
7304 : }
7305 : else
7306 : {
7307 : /* Negative increment (geodim > datadim) */
7308 : /* ------------------------------------- */
7309 0 : start[index] = SWXRegion[regionID]->StartRegion[k]
7310 0 : / (-incr) + offset;
7311 0 : edge[index] = nXtrk / (-incr);
7312 :
7313 : /*
7314 : * If Xtrk not exactly divisible by incr, round
7315 : * edge to next highest integer
7316 : */
7317 :
7318 0 : if (nXtrk % (-incr) != 0)
7319 : {
7320 0 : edge[index]++;
7321 : }
7322 : }
7323 :
7324 :
7325 : /* Read Data into output buffer */
7326 : /* ---------------------------- */
7327 0 : status = SWreadfield(swathID, fieldname,
7328 : start, NULL, edge,
7329 : (uint8 *) buffer + bufOffset);
7330 : }
7331 : }
7332 0 : else if (found == 1 && idxMapElem != -1)
7333 : {
7334 : /* Indexed Mapping */
7335 : /* --------------- */
7336 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
7337 : {
7338 0 : if (k > 0)
7339 : {
7340 : /* Compute size in bytes of previous region */
7341 : /* ---------------------------------------- */
7342 0 : size = edge[0];
7343 0 : for (j = 1; j < rank; j++)
7344 : {
7345 0 : size *= edge[j];
7346 : }
7347 0 : size *= DFKNTsize(ntype);
7348 :
7349 :
7350 : /* Compute output buffer offset */
7351 : /* ---------------------------- */
7352 0 : bufOffset += size;
7353 : }
7354 : else
7355 : {
7356 : /* Initialize output buffer offset */
7357 : /* ------------------------------- */
7358 0 : bufOffset = 0;
7359 : }
7360 :
7361 :
7362 : /* Compute start & edge from index mappings */
7363 : /* ---------------------------------------- */
7364 0 : if (SWXRegion[regionID]->scanflag == 1 &&
7365 0 : (strcmp(fieldname, "Latitude") == 0 ||
7366 0 : strcmp(fieldname, "Longitude") == 0))
7367 : {
7368 0 : if (land_status == 0)
7369 0 : status = SWreadattr(swathID, "scene_count", &scene_cnt);
7370 0 : startscanline = SWXRegion[regionID]->StartRegion[k];
7371 0 : stopscanline = SWXRegion[regionID]->StopRegion[k];
7372 0 : if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3)
7373 : {
7374 0 : status = SWfieldinfo(swathID,"scan_no",&rk,dims2,&nt,dimlist);
7375 0 : tbuffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
7376 0 : status =SWreadfield(swathID,"scan_no",NULL,NULL,NULL,tbuffer);
7377 0 : startscanline = startscanline - ((tbuffer[0] * detect_cnt) - detect_cnt);
7378 0 : stopscanline = stopscanline - ((tbuffer[0] * detect_cnt) - 1);
7379 : }
7380 0 : if(SWXRegion[regionID]->band8flag == 2 ||
7381 0 : SWXRegion[regionID]->band8flag == 3)
7382 : {
7383 0 : if(startscandim == -1)
7384 0 : if(startscanline < idxmap[0])
7385 : {
7386 0 : startscandim = 0;
7387 0 : start[index] = 0;
7388 0 : if(stopscanline > idxmap[scene_cnt * 2 - 1])
7389 : {
7390 0 : stopscandim = scene_cnt*2 - startscandim;
7391 0 : edge[index] = scene_cnt*2 - startscandim;
7392 : }
7393 : }
7394 : }
7395 0 : j = 0;
7396 0 : for (l = 0; l < scene_cnt; l++)
7397 : {
7398 0 : if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
7399 0 : if(startscandim == -1)
7400 : {
7401 0 : start[index] = j;
7402 0 : startscandim = j;
7403 : }
7404 0 : if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
7405 0 : if(startscandim != -1)
7406 : {
7407 0 : edge[index] = j - start[index] + 2;
7408 0 : stopscandim = j - start[index] + 1;
7409 : }
7410 0 : j = j + 2;
7411 : }
7412 0 : if(SWXRegion[regionID]->band8flag == 1 ||
7413 0 : SWXRegion[regionID]->band8flag == 2)
7414 : {
7415 0 : if(startscandim == -1)
7416 0 : if(startscanline < idxmap[0])
7417 : {
7418 0 : startscandim = 0;
7419 0 : start[index] = 0;
7420 : }
7421 0 : if(stopscandim == -1)
7422 0 : if(stopscanline > idxmap[scene_cnt * 2 - 1])
7423 : {
7424 0 : stopscandim = scene_cnt*2 - start[index];
7425 0 : edge[index] = scene_cnt*2 - start[index];
7426 : }
7427 : }
7428 0 : if(SWXRegion[regionID]->band8flag == 2)
7429 : {
7430 0 : if(startscandim == -1)
7431 0 : if(startscanline > idxmap[j - 1])
7432 : {
7433 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank2,dims2,&nt,dimlist);
7434 0 : buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
7435 0 : status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
7436 0 : startscanline = startscanline - (buffer2[0] * detect_cnt);
7437 0 : stopscanline = stopscanline - (buffer2[0] * detect_cnt);
7438 0 : free(buffer2);
7439 0 : j = 0;
7440 0 : for (l = 0; l < scene_cnt; l++)
7441 : {
7442 0 : if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
7443 : {
7444 0 : start[index] = j;
7445 : }
7446 0 : if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
7447 0 : edge[index] = j - start[index] + 2;
7448 0 : j = j + 2;
7449 0 : if(idxmap[j] == 0 || idxmap[j+1] == 0)
7450 0 : l = scene_cnt;
7451 : }
7452 :
7453 : }
7454 : }
7455 :
7456 : }
7457 0 : else if(SWXRegion[regionID]->scanflag == 1 &&
7458 0 : (strcmp(fieldname, "scene_center_latitude") == 0 ||
7459 0 : strcmp(fieldname, "scene_center_longitude") == 0))
7460 : {
7461 0 : if (land_status == 0)
7462 0 : status = SWreadattr(swathID, "scene_count", &scene_cnt);
7463 0 : startscanline = SWXRegion[regionID]->StartRegion[k];
7464 0 : stopscanline = SWXRegion[regionID]->StopRegion[k];
7465 0 : if(startscanline < idxmap[0])
7466 : {
7467 0 : startscandim = 0;
7468 0 : start[index] = 0;
7469 : }
7470 0 : for (l = 0; l < scene_cnt-1; l++)
7471 : {
7472 0 : if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
7473 0 : if(startscandim == -1)
7474 : {
7475 0 : start[index] = l;
7476 0 : startscandim = l;
7477 : }
7478 0 : if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
7479 0 : if(stopscandim == -1)
7480 : {
7481 0 : edge[index] = l - start[index] + 2;
7482 0 : stopscandim = l + 1;
7483 : }
7484 : }
7485 0 : if(stopscandim == -1)
7486 : {
7487 0 : if(stopscanline > idxmap[scene_cnt - 1])
7488 : {
7489 0 : edge[index] = scene_cnt - start[index];
7490 0 : stopscandim = scene_cnt - 1;
7491 : }
7492 : }
7493 :
7494 0 : if(SWXRegion[regionID]->band8flag == 1)
7495 : {
7496 0 : if(stopscandim == -1)
7497 0 : if(stopscanline > idxmap[scene_cnt - 1])
7498 : {
7499 0 : edge[index] = scene_cnt - start[index];
7500 0 : stopscandim = scene_cnt -1;
7501 : }
7502 : }
7503 0 : if(SWXRegion[regionID]->band8flag == 2 ||
7504 0 : SWXRegion[regionID]->band8flag == 3)
7505 : {
7506 0 : if(startscandim == -1)
7507 : {
7508 0 : if(startscanline < idxmap[0])
7509 : {
7510 0 : startscandim = 0;
7511 0 : start[index] = 0;
7512 0 : edge[index] = stopscandim - startscandim + 1;
7513 : }
7514 : }
7515 0 : if(startscandim == -1)
7516 : {
7517 0 : startscanline = SWXRegion[regionID]->StartScan[k] * detect_cnt - detect_cnt;
7518 0 : stopscanline = SWXRegion[regionID]->StopScan[k] * detect_cnt - 1;
7519 0 : for (l = 0; l < scene_cnt-1; l++)
7520 : {
7521 0 : if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
7522 0 : start[index] = l;
7523 0 : if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
7524 0 : edge[index] = l - start[index] + 1;
7525 : }
7526 : }
7527 : }
7528 : }
7529 : else
7530 : {
7531 0 : if (SWXRegion[regionID]->scanflag == 1 &&
7532 0 : strcmp(fieldname,dfieldlist) == 0)
7533 : {
7534 0 : start[index] = SWXRegion[regionID]->StartRegion[k];
7535 0 : edge[index] = SWXRegion[regionID]->StopRegion[k] -
7536 0 : SWXRegion[regionID]->StartRegion[k] + 1;
7537 0 : if(SWXRegion[regionID]->band8flag == 2 ||
7538 0 : SWXRegion[regionID]->band8flag == 3 )
7539 : {
7540 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank,dims2,&nt,dimlist);
7541 0 : buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
7542 0 : status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
7543 0 : start[index] = start[index] - (buffer2[0] * detect_cnt - detect_cnt);
7544 0 : free(buffer2);
7545 : }
7546 : }
7547 : else
7548 : {
7549 0 : start[index] = idxmap[SWXRegion[regionID]->StartRegion[k]];
7550 :
7551 0 : edge[index] = idxmap[SWXRegion[regionID]->StopRegion[k]] -
7552 0 : idxmap[SWXRegion[regionID]->StartRegion[k]] + 1;
7553 : }
7554 : }
7555 : /* Read Data into output buffer */
7556 : /* ---------------------------- */
7557 0 : status = SWreadfield(swathID, fieldname,
7558 : start, NULL, edge,
7559 : buffer);
7560 0 : if (SWXRegion[regionID]->scanflag == 1)
7561 : {
7562 :
7563 0 : if (strcmp(fieldname,"Longitude") == 0)
7564 : {
7565 0 : status = SWscan2longlat(swathID, fieldname, buffer, start,
7566 : edge, idxmap, startscanline, stopscanline);
7567 : }
7568 0 : if (strcmp(fieldname,"Latitude") == 0)
7569 : {
7570 0 : status = SWscan2longlat(swathID, fieldname, buffer, start,
7571 : edge, idxmap, startscanline, stopscanline);
7572 : }
7573 : }
7574 : }
7575 : }
7576 0 : else if(vfound == 1) /* Vertical subsetting */
7577 : { /* found previously, */
7578 0 : status = SWreadfield(swathID, fieldname, /* perform the vertical*/
7579 : start, NULL, edge, /* subsetting. */
7580 : (uint8 *) buffer); /* -- xhua */
7581 : }
7582 : else
7583 : {
7584 : /* Mapping not found */
7585 : /* ----------------- */
7586 0 : status = -1;
7587 0 : HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
7588 0 : HEreport("Mapping Not Defined for \"%s\" Dimension.\n",
7589 : geodim);
7590 : }
7591 : }
7592 : else
7593 : {
7594 : /* Read Data (Vert SS only) */
7595 : /* ------------------------ */
7596 0 : status = SWreadfield(swathID, fieldname,
7597 : start, NULL, edge,
7598 : (uint8 *) buffer);
7599 : }
7600 : }
7601 : }
7602 :
7603 : /* Free index mappings if applicable */
7604 : /* --------------------------------- */
7605 0 : if (idxmap != NULL)
7606 : {
7607 0 : free(idxmap);
7608 : }
7609 0 : if(dfieldlist != NULL)
7610 0 : free(dfieldlist);
7611 :
7612 0 : return (status);
7613 : }
7614 :
7615 :
7616 : /*----------------------------------------------------------------------------|
7617 : | BEGIN_PROLOG |
7618 : | |
7619 : | FUNCTION: SWscan2longlat |
7620 : | |
7621 : | DESCRIPTION: Convert scanline to Long/Lat for floating scene subsetting. |
7622 : | This will calculate/interpolate the long/lat for a given |
7623 : | scanline. |
7624 : | |
7625 : | |
7626 : | Return Value Type Units Description |
7627 : | ============ ====== ========= ===================================== |
7628 : | status intn return status (0) SUCCEED, (-1) FAIL |
7629 : | |
7630 : | INPUTS: |
7631 : | swathID int32 Swath structure ID |
7632 : | fieldname char Fieldname |
7633 : | buffer void Values to update |
7634 : | start int32 |
7635 : | edge int32 |
7636 : | idxmap int32 * Buffer of index mapping values |
7637 : | startscanline int32 Start of scan region |
7638 : | stopscanline int32 Stop of scan region |
7639 : | |
7640 : | OUTPUTS: |
7641 : | |
7642 : | NOTES: |
7643 : | |
7644 : | |
7645 : | Date Programmer Description |
7646 : | ====== ============ ================================================= |
7647 : | Jul 99 DaW Original Programmer |
7648 : | |
7649 : | END_PROLOG |
7650 : -----------------------------------------------------------------------------*/
7651 : static intn
7652 0 : SWscan2longlat(int32 swathID, char *fieldname, VOIDP buffer, int32 start[],
7653 : int32 edge[], int32 *idxmap, int32 startscanline, int32 stopscanline)
7654 : {
7655 :
7656 : enum corner {UL, UR, LL, LR};
7657 0 : enum corner pos = UL;
7658 0 : enum corner pos2 = UL;
7659 :
7660 0 : uint8 scene_cnt = 0; /* Used to convert scan to scanline */
7661 : /* L7 float scene sub. */
7662 : float32 *buffer2;
7663 : float32 *bufferc;
7664 0 : float32 deg2rad = PI/180.00;
7665 :
7666 0 : float32 p1_long = 0.0; /* point 1, longitude */
7667 0 : float32 p2_long = 0.0; /* point 2, longitude */
7668 0 : float32 pi_long = 0.0; /* interpolated point, longitude */
7669 0 : int32 scanline_p1 = 0;
7670 :
7671 0 : float32 p1_lat = 0.0; /* point 1, latitude */
7672 0 : float32 p2_lat = 0.0; /* point 2, latitude */
7673 0 : float32 pi_lat = 0.0; /* interpolated point, latitude */
7674 0 : int32 scanline_p2 = 0;
7675 :
7676 :
7677 0 : float32 x_p1 = 0.0; /* Cartesian coordinates */
7678 0 : float32 y_p1 = 0.0; /* point 1 */
7679 0 : float32 z_p1 = 0.0;
7680 :
7681 0 : float32 x_p2 = 0.0; /* Cartesian coordinates */
7682 0 : float32 y_p2 = 0.0; /* point 2 */
7683 0 : float32 z_p2 = 0.0;
7684 :
7685 0 : float32 x_pi = 0.0; /* Cartesian coordinates */
7686 0 : float32 y_pi = 0.0; /* interpolated point */
7687 0 : float32 z_pi = 0.0;
7688 0 : int32 scanline_pi = 0;
7689 :
7690 0 : intn status = -1;
7691 :
7692 0 : int i = 0;
7693 0 : int p1_long_l90_flag = 0;
7694 0 : int p1_long_g90_flag = 0;
7695 0 : int p2_long_l90_flag = 0;
7696 0 : int p2_long_g90_flag = 0;
7697 0 : int fieldflag = 0;
7698 :
7699 0 : int numofval = 0;
7700 :
7701 :
7702 :
7703 0 : numofval = edge[0] * 2;
7704 :
7705 :
7706 0 : buffer2 = (float32 *)calloc(numofval, sizeof(float32));
7707 0 : bufferc = (float32 *)calloc(numofval, sizeof(float32));
7708 0 : memmove(bufferc, buffer, numofval*sizeof(float32));
7709 :
7710 0 : (void) SWreadattr(swathID, "scene_count", &scene_cnt);
7711 :
7712 0 : if (strcmp(fieldname, "Longitude") == 0)
7713 : {
7714 0 : fieldflag = 1;
7715 0 : status = SWreadfield(swathID, "Latitude", start, NULL, edge, buffer2);
7716 : }
7717 0 : else if (strcmp(fieldname, "Latitude") == 0)
7718 : {
7719 0 : fieldflag = 2;
7720 0 : status = SWreadfield(swathID, "Longitude", start, NULL, edge, buffer2);
7721 : }
7722 :
7723 0 : for(i=0; i<4; i++)
7724 : {
7725 0 : switch(pos)
7726 : {
7727 : case UL:
7728 0 : if (fieldflag == 1)
7729 : {
7730 0 : p1_long = bufferc[0];
7731 0 : p2_long = bufferc[2];
7732 0 : p1_lat = buffer2[0];
7733 0 : p2_lat = buffer2[2];
7734 : }
7735 0 : if (fieldflag == 2)
7736 : {
7737 0 : p1_long = buffer2[0];
7738 0 : p2_long = buffer2[2];
7739 0 : p1_lat = bufferc[0];
7740 0 : p2_lat = bufferc[2];
7741 : }
7742 0 : scanline_p1 = idxmap[start[0]];
7743 0 : scanline_p2 = idxmap[start[0]+1];
7744 0 : scanline_pi = startscanline;
7745 0 : pos = UR;
7746 0 : break;
7747 : case UR:
7748 0 : if (fieldflag == 1)
7749 : {
7750 0 : p1_long = bufferc[1];
7751 0 : p2_long = bufferc[3];
7752 0 : p1_lat = buffer2[1];
7753 0 : p2_lat = buffer2[3];
7754 : }
7755 0 : if (fieldflag == 2)
7756 : {
7757 0 : p1_long = buffer2[1];
7758 0 : p2_long = buffer2[3];
7759 0 : p1_lat = bufferc[1];
7760 0 : p2_lat = bufferc[3];
7761 : }
7762 0 : scanline_p1 = idxmap[start[0]];
7763 0 : scanline_p2 = idxmap[start[0]+1];
7764 0 : scanline_pi = startscanline;
7765 0 : pos = LL;
7766 0 : break;
7767 : case LL:
7768 0 : if (fieldflag == 1)
7769 : {
7770 0 : p1_long = bufferc[numofval-4];
7771 0 : p2_long = bufferc[numofval-2];
7772 0 : p1_lat = buffer2[numofval-4];
7773 0 : p2_lat = buffer2[numofval-2];
7774 : }
7775 0 : if (fieldflag == 2)
7776 : {
7777 0 : p1_long = buffer2[numofval-4];
7778 0 : p2_long = buffer2[numofval-2];
7779 0 : p1_lat = bufferc[numofval-4];
7780 0 : p2_lat = bufferc[numofval-2];
7781 : }
7782 0 : scanline_p1 = idxmap[start[0] + edge[0] - 2];
7783 0 : scanline_p2 = idxmap[start[0] + edge[0] - 1];
7784 0 : scanline_pi = stopscanline;
7785 0 : pos = LR;
7786 0 : break;
7787 : case LR:
7788 0 : if (fieldflag == 1)
7789 : {
7790 0 : p1_long = bufferc[numofval-3];
7791 0 : p2_long = bufferc[numofval-1];
7792 0 : p1_lat = buffer2[numofval-3];
7793 0 : p2_lat = buffer2[numofval-1];
7794 : }
7795 0 : if (fieldflag == 2)
7796 : {
7797 0 : p1_long = buffer2[numofval-3];
7798 0 : p2_long = buffer2[numofval-1];
7799 0 : p1_lat = bufferc[numofval-3];
7800 0 : p2_lat = bufferc[numofval-1];
7801 : }
7802 0 : scanline_p1 = idxmap[start[0] + edge[0] - 2];
7803 0 : scanline_p2 = idxmap[start[0] + edge[0] - 1];
7804 0 : scanline_pi = stopscanline;
7805 : break;
7806 : }
7807 :
7808 :
7809 :
7810 0 : if (p1_long <= -90.0)
7811 : {
7812 0 : if (p2_long >= 90.0)
7813 : {
7814 0 : p1_long = p1_long + 180.0;
7815 0 : p2_long = p2_long - 180.0;
7816 0 : p1_long_l90_flag = 2;
7817 : }
7818 : else
7819 : {
7820 0 : p1_long = p1_long + 180.0;
7821 0 : p1_long_l90_flag = 1;
7822 : }
7823 : }
7824 0 : if (p1_long >= 90.0 && p1_long_l90_flag != 2)
7825 : {
7826 0 : if(p2_long <= -90.0)
7827 : {
7828 0 : p1_long = p1_long - 180.0;
7829 0 : p2_long = p2_long + 180.0;
7830 0 : p1_long_g90_flag = 2;
7831 : }
7832 : else
7833 : {
7834 0 : p1_long = p1_long - 90.0;
7835 0 : p1_long_g90_flag = 1;
7836 : }
7837 : }
7838 0 : if (p2_long <= -90.0)
7839 : {
7840 0 : if (p1_long < 0.0)
7841 : {
7842 0 : p2_long = p2_long + 90.0;
7843 0 : p1_long = p1_long + 90.0;
7844 0 : p2_long_l90_flag = 2;
7845 : }
7846 : else
7847 : {
7848 0 : p2_long = p2_long + 180.0;
7849 0 : p2_long_l90_flag = 1;
7850 : }
7851 : }
7852 0 : if (p2_long >= 90.0 && p1_long_l90_flag != 2)
7853 : {
7854 0 : p2_long = p2_long - 90.0;
7855 0 : p2_long_g90_flag = 1;
7856 : }
7857 :
7858 :
7859 0 : x_p1 = RADOE * cos((p1_long*deg2rad)) * sin((p1_lat*deg2rad));
7860 0 : y_p1 = RADOE * sin((p1_long*deg2rad)) * sin((p1_lat*deg2rad));
7861 0 : z_p1 = RADOE * cos((p1_lat*deg2rad));
7862 :
7863 :
7864 0 : x_p2 = RADOE * cos((p2_long*deg2rad)) * sin((p2_lat*deg2rad));
7865 0 : y_p2 = RADOE * sin((p2_long*deg2rad)) * sin((p2_lat*deg2rad));
7866 0 : z_p2 = RADOE * cos((p2_lat*deg2rad));
7867 :
7868 0 : x_pi = x_p1 + (x_p2 - x_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1);
7869 0 : y_pi = y_p1 + (y_p2 - y_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1);
7870 0 : z_pi = z_p1 + (z_p2 - z_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1);
7871 :
7872 0 : if (fieldflag == 1)
7873 : {
7874 0 : pi_long = atan(y_pi/x_pi)*180.0/PI;
7875 0 : if (p1_long_l90_flag == 1 || p2_long_l90_flag == 1)
7876 : {
7877 0 : pi_long = pi_long - 180.0;
7878 0 : p1_long_l90_flag = 0;
7879 0 : p2_long_l90_flag = 0;
7880 : }
7881 0 : if (p1_long_g90_flag == 1 || p2_long_g90_flag == 1)
7882 : {
7883 0 : pi_long = pi_long + 90.0;
7884 0 : p1_long_g90_flag = 0;
7885 0 : p2_long_g90_flag = 0;
7886 : }
7887 0 : if (p1_long_l90_flag == 2)
7888 : {
7889 0 : if (pi_long > 0.0)
7890 0 : pi_long = pi_long - 180.0;
7891 0 : else if (pi_long < 0.0)
7892 0 : pi_long = pi_long + 180.0;
7893 0 : p1_long_l90_flag = 0;
7894 : }
7895 0 : if (p1_long_g90_flag == 2)
7896 : {
7897 0 : if (pi_long > 0.0)
7898 0 : pi_long = pi_long - 180.0;
7899 0 : else if (pi_long < 0.0)
7900 0 : pi_long = pi_long + 180.0;
7901 0 : p1_long_g90_flag = 0;
7902 : }
7903 0 : if (p2_long_l90_flag == 2)
7904 : {
7905 0 : pi_long = pi_long - 90.0;
7906 0 : p2_long_l90_flag = 0;
7907 : }
7908 :
7909 :
7910 :
7911 0 : switch(pos2)
7912 : {
7913 : case UL:
7914 0 : bufferc[0] = pi_long;
7915 0 : pos2 = UR;
7916 0 : break;
7917 : case UR:
7918 0 : bufferc[1] = pi_long;
7919 0 : pos2 = LL;
7920 0 : break;
7921 : case LL:
7922 0 : if(stopscanline > idxmap[scene_cnt*2 - 1])
7923 0 : break;
7924 0 : bufferc[numofval-2] = pi_long;
7925 0 : pos2 = LR;
7926 0 : break;
7927 : case LR:
7928 0 : if(stopscanline > idxmap[scene_cnt*2 - 1])
7929 0 : break;
7930 0 : bufferc[numofval-1] = pi_long;
7931 : break;
7932 : }
7933 :
7934 : }
7935 0 : if (fieldflag == 2)
7936 : {
7937 0 : pi_lat = atan((sqrt(x_pi*x_pi + y_pi*y_pi)/z_pi))*180.0/PI;
7938 0 : switch(pos2)
7939 : {
7940 : case UL:
7941 0 : bufferc[0] = pi_lat;
7942 0 : pos2 = UR;
7943 0 : break;
7944 : case UR:
7945 0 : bufferc[1] = pi_lat;
7946 0 : pos2 = LL;
7947 0 : break;
7948 : case LL:
7949 0 : if(stopscanline > idxmap[scene_cnt*2 - 1])
7950 0 : break;
7951 0 : bufferc[numofval-2] = pi_lat;
7952 0 : pos2 = LR;
7953 0 : break;
7954 : case LR:
7955 0 : if(stopscanline > idxmap[scene_cnt*2 - 1])
7956 0 : break;
7957 0 : bufferc[numofval-1] = pi_lat;
7958 : break;
7959 : }
7960 : }
7961 : }
7962 0 : memmove(buffer, bufferc, numofval*sizeof(float32));
7963 0 : free(buffer2);
7964 0 : free(bufferc);
7965 0 : return(status);
7966 : }
7967 :
7968 :
7969 : /*----------------------------------------------------------------------------|
7970 : | BEGIN_PROLOG |
7971 : | |
7972 : | FUNCTION: SWextractperiod |
7973 : | |
7974 : | DESCRIPTION: Retrieves data from specified period. |
7975 : | |
7976 : | |
7977 : | Return Value Type Units Description |
7978 : | ============ ====== ========= ===================================== |
7979 : | status intn return status (0) SUCCEED, (-1) FAIL |
7980 : | |
7981 : | INPUTS: |
7982 : | swathID int32 Swath structure ID |
7983 : | periodID int32 Period ID |
7984 : | fieldname char Fieldname |
7985 : | externalflag int32 External geolocation fields flag |
7986 : | HDFE_INTERNAL (0) |
7987 : | HDFE_EXTERNAL (1) |
7988 : | |
7989 : | OUTPUTS: |
7990 : | buffer void Data buffer containing subsetted region |
7991 : | |
7992 : | NOTES: |
7993 : | |
7994 : | |
7995 : | Date Programmer Description |
7996 : | ====== ============ ================================================= |
7997 : | Jun 96 Joel Gales Original Programmer |
7998 : | Jun 03 Abe Taaheri added clearing timeflag if SWextractregion failes |
7999 : | |
8000 : | END_PROLOG |
8001 : -----------------------------------------------------------------------------*/
8002 : intn
8003 0 : SWextractperiod(int32 swathID, int32 periodID, char *fieldname,
8004 : int32 externalflag, VOIDP buffer)
8005 :
8006 : {
8007 : intn status; /* routine return status variable */
8008 :
8009 0 : timeflag = 1;
8010 :
8011 : /* Call SWextractregion routine */
8012 : /* ---------------------------- */
8013 0 : status = SWextractregion(swathID, periodID, fieldname, externalflag,
8014 : (char *) buffer);
8015 0 : if (status != 0) timeflag = 0; /*clear timeflag if SWextractregion failed*/
8016 0 : return (status);
8017 : }
8018 :
8019 :
8020 :
8021 :
8022 :
8023 :
8024 :
8025 : /*----------------------------------------------------------------------------|
8026 : | BEGIN_PROLOG |
8027 : | |
8028 : | FUNCTION: SWdupregion |
8029 : | |
8030 : | DESCRIPTION: Duplicates a region |
8031 : | |
8032 : | |
8033 : | Return Value Type Units Description |
8034 : | ============ ====== ========= ===================================== |
8035 : | newregionID int32 New region ID |
8036 : | |
8037 : | INPUTS: |
8038 : | oldregionID int32 Old region ID |
8039 : | |
8040 : | OUTPUTS: |
8041 : | None |
8042 : | |
8043 : | NOTES: |
8044 : | |
8045 : | |
8046 : | Date Programmer Description |
8047 : | ====== ============ ================================================= |
8048 : | Jan 97 Joel Gales Original Programmer |
8049 : | |
8050 : | END_PROLOG |
8051 : -----------------------------------------------------------------------------*/
8052 : int32
8053 0 : SWdupregion(int32 oldregionID)
8054 : {
8055 : intn i; /* Loop index */
8056 :
8057 0 : int32 newregionID = -1; /* New region ID */
8058 :
8059 :
8060 : /* Find first empty (inactive) region */
8061 : /* ---------------------------------- */
8062 0 : for (i = 0; i < NSWATHREGN; i++)
8063 : {
8064 0 : if (SWXRegion[i] == 0)
8065 : {
8066 : /* Allocate space for new swath region entry */
8067 : /* ----------------------------------------- */
8068 0 : SWXRegion[i] = (struct swathRegion *)
8069 : calloc(1, sizeof(struct swathRegion));
8070 0 : if(SWXRegion[i] == NULL)
8071 : {
8072 0 : HEpush(DFE_NOSPACE,"SWdupregion", __FILE__, __LINE__);
8073 0 : return(-1);
8074 : }
8075 :
8076 : /* Copy old region structure data to new region */
8077 : /* -------------------------------------------- */
8078 0 : *SWXRegion[i] = *SWXRegion[oldregionID];
8079 :
8080 :
8081 : /* Define new region ID */
8082 : /* -------------------- */
8083 0 : newregionID = i;
8084 :
8085 0 : break;
8086 : }
8087 : }
8088 :
8089 0 : return (newregionID);
8090 : }
8091 :
8092 :
8093 : /*----------------------------------------------------------------------------|
8094 : | BEGIN_PROLOG |
8095 : | |
8096 : | FUNCTION: SWregioninfo |
8097 : | |
8098 : | DESCRIPTION: Returns size of region in bytes |
8099 : | |
8100 : | |
8101 : | Return Value Type Units Description |
8102 : | ============ ====== ========= ===================================== |
8103 : | status intn return status (0) SUCCEED, (-1) FAIL |
8104 : | |
8105 : | INPUTS: |
8106 : | swathID int32 Swath structure ID |
8107 : | regionID int32 Region ID |
8108 : | fieldname char Fieldname |
8109 : | |
8110 : | |
8111 : | OUTPUTS: |
8112 : | ntype int32 field number type |
8113 : | rank int32 field rank |
8114 : | dims int32 dimensions of field region |
8115 : | size int32 size in bytes of field region |
8116 : | |
8117 : | NOTES: |
8118 : | |
8119 : | |
8120 : | Date Programmer Description |
8121 : | ====== ============ ================================================= |
8122 : | Jun 96 Joel Gales Original Programmer |
8123 : | Aug 96 Joel Gales Add vertical subsetting |
8124 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
8125 : | Mar 97 Joel Gales Add support for index mapping |
8126 : | Jul 99 DaW Add support for floating scene subsetting |
8127 : | |
8128 : | END_PROLOG |
8129 : -----------------------------------------------------------------------------*/
8130 : intn
8131 0 : SWregioninfo(int32 swathID, int32 regionID, char *fieldname,
8132 : int32 * ntype, int32 * rank, int32 dims[], int32 * size)
8133 :
8134 : {
8135 : intn i; /* Loop index */
8136 : intn j; /* Loop index */
8137 : intn k; /* Loop index */
8138 0 : intn l = 0; /* Loop index */
8139 : intn status; /* routine return status variable */
8140 0 : intn long_status = 3; /* routine return status variable for longitude */
8141 0 : intn land_status = 3; /* Used for L7 float scene sub. */
8142 0 : intn statMap = -1; /* Status from SWmapinfo */
8143 :
8144 0 : uint8 found = 0; /* Found flag */
8145 0 : uint8 detect_cnt = 0; /* Used for L7 float scene sub. */
8146 :
8147 0 : int32 numtype = 0; /* Used for L7 float scene sub. */
8148 0 : int32 count = 0; /* Used for L7 float scene sub. */
8149 :
8150 :
8151 : int32 fid; /* HDF-EOS file ID */
8152 : int32 sdInterfaceID; /* HDF SDS interface ID */
8153 : int32 swVgrpID; /* Swath Vgroup ID */
8154 :
8155 : int32 index; /* Geo Dim Index */
8156 : int32 nDim; /* Number of dimensions */
8157 : int32 slen[64]; /* String length array */
8158 : int32 dum; /* Dummy variable */
8159 : int32 incr; /* Mapping increment */
8160 0 : int32 nXtrk = 0; /* Number of cross tracks */
8161 0 : int32 scan_shift = 0; /* Used to take out partial scans */
8162 0 : int32 startscandim = -1; /* Used for floating scene region size */
8163 0 : int32 stopscandim = -1; /* Used for floating scene region size */
8164 : int32 dumdims[8]; /* Dimensions from SWfieldinfo */
8165 0 : int32 idxMapElem = -1; /* Number of index map elements */
8166 0 : int32 *idxmap = NULL; /* Pointer to index mapping array */
8167 0 : int32 datafld = 0;
8168 :
8169 0 : uint8 scene_cnt = 0; /* Number of scenes in swath */
8170 0 : int32 startscanline = 0;
8171 0 : int32 stopscanline = 0;
8172 0 : char *dfieldlist = (char *)NULL;
8173 0 : int32 strbufsize = 0;
8174 : int32 dfrank[8];
8175 : int32 numtype2[8];
8176 0 : int32 rank2 = 0;
8177 0 : int32 rk = 0;
8178 : int32 dims2[8];
8179 0 : int32 nt = 0;
8180 0 : uint16 *buffer2 = (uint16 *)NULL;
8181 0 : uint16 *tbuffer = (uint16 *)NULL;
8182 :
8183 : char dimlist[256]; /* Dimension list */
8184 : char geodim[256];/* Geolocation field dimension list */
8185 : char tgeodim[256];/* Time Geolocation field dimension list */
8186 : char dgeodim[256];/* Data Subsetting field dimension list */
8187 : char utlbuf[256];/* Utility buffer */
8188 : char *ptr[64]; /* String pointer array */
8189 : static const char errMesg[] = "Vertical Dimension Not Found: \"%s\".\n";
8190 :
8191 :
8192 :
8193 : /* Set region size to -1 */
8194 : /* --------------------- */
8195 0 : *size = -1;
8196 :
8197 :
8198 : /* Check for valid swath ID */
8199 : /* ------------------------ */
8200 0 : status = SWchkswid(swathID, "SWregioninfo", &fid, &sdInterfaceID,
8201 : &swVgrpID);
8202 :
8203 :
8204 : /* Check for valid region ID */
8205 : /* ------------------------- */
8206 0 : if (status == 0)
8207 : {
8208 0 : if (regionID < 0 || regionID >= NSWATHREGN)
8209 : {
8210 0 : status = -1;
8211 0 : HEpush(DFE_RANGE, "SWregioninfo", __FILE__, __LINE__);
8212 0 : HEreport("Invalid Region id: %d.\n", regionID);
8213 : }
8214 : }
8215 :
8216 :
8217 :
8218 : /* Check for active region ID */
8219 : /* -------------------------- */
8220 0 : if (status == 0)
8221 : {
8222 0 : if (SWXRegion[regionID] == 0)
8223 : {
8224 0 : status = -1;
8225 0 : HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__);
8226 0 : HEreport("Inactive Region ID: %d.\n", regionID);
8227 : }
8228 : }
8229 :
8230 : /* This code checks for the attribute detector_count */
8231 : /* which is found in Landsat 7 files. It is used */
8232 : /* for some of the loops. */
8233 : /* ================================================= */
8234 0 : if (SWXRegion[regionID]->scanflag == 1)
8235 : {
8236 0 : land_status = SWattrinfo(swathID, "detector_count", &numtype, &count);
8237 0 : if (land_status == 0)
8238 : {
8239 0 : land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
8240 0 : land_status = SWreadattr(swathID, "scene_count", &scene_cnt);
8241 :
8242 : }
8243 : }
8244 :
8245 :
8246 :
8247 :
8248 : /* Check for valid fieldname */
8249 : /* ------------------------- */
8250 0 : if (status == 0)
8251 : {
8252 : /* Get data field info */
8253 : /* ------------------- */
8254 0 : status = SWfieldinfo(swathID, fieldname, rank,
8255 : dims, ntype, dimlist);
8256 :
8257 0 : if (status != 0)
8258 : {
8259 0 : status = -1;
8260 0 : HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__);
8261 0 : HEreport("Field \"%s\" Not Found.\n", fieldname);
8262 : }
8263 : }
8264 :
8265 :
8266 :
8267 : /* No problems so proceed ... */
8268 : /* -------------------------- */
8269 0 : if (status == 0)
8270 : {
8271 : /* If non-vertical subset regions defined ... */
8272 : /* ------------------------------------------ */
8273 0 : if (SWXRegion[regionID]->nRegions > 0 || SWXRegion[regionID]->scanflag == 1)
8274 : {
8275 :
8276 : /* Get geolocation dimension name */
8277 : /* ------------------------------ */
8278 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,
8279 : "Longitude", &dum,
8280 : dumdims, &dum, geodim);
8281 0 : long_status = status;
8282 :
8283 : /* If Time field being used, check for dimensions */
8284 : /* ---------------------------------------------- */
8285 0 : if (timeflag == 1)
8286 : {
8287 : /* code change to fix time subset bug for Landsat7 */
8288 :
8289 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,
8290 : "Time", &dum,
8291 : dumdims, &dum, tgeodim);
8292 :
8293 0 : if (strcmp(geodim, tgeodim) != 0)
8294 : {
8295 0 : strcpy(geodim, tgeodim);
8296 : }
8297 0 : timeflag = 0;
8298 : }
8299 :
8300 : /* If defscanregion being used, get dimensions */
8301 : /* of field being used */
8302 : /* ---------------------------------------------- */
8303 0 : if (SWXRegion[regionID]->scanflag == 1)
8304 : {
8305 0 : (void) SWnentries(SWXRegion[regionID]->swathID,4,&strbufsize);
8306 0 : dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char));
8307 0 : (void) SWinqdatafields(SWXRegion[regionID]->swathID,dfieldlist,dfrank,numtype2);
8308 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,dfieldlist,&dum,dumdims,&dum,dgeodim);
8309 :
8310 : /* The dimensions have to be switched, because */
8311 : /* the mappings force a geodim and datadim */
8312 : /* so to find the mapping, the dimensions must */
8313 : /* be switched, but the subsetting will still */
8314 : /* be based on the correct dimensions */
8315 : /* "long_status == -1" added for CAL file which */
8316 : /* doesn't have a Traditional geolocation field */
8317 : /* ---------------------------------------------- */
8318 0 : if (strcmp(dgeodim,dimlist) != 0 || long_status == -1)
8319 : {
8320 0 : strcpy(geodim,dimlist);
8321 0 : strcpy(dimlist,dgeodim);
8322 : }
8323 : }
8324 :
8325 :
8326 : /* Get "Track" (first) Dimension from geo dimlist */
8327 : /* ---------------------------------------------- */
8328 0 : nDim = EHparsestr(geodim, ',', ptr, slen);
8329 0 : geodim[slen[0]] = 0;
8330 :
8331 :
8332 : /* Parse Data Field Dimlist & find mapping */
8333 : /* --------------------------------------- */
8334 0 : nDim = EHparsestr(dimlist, ',', ptr, slen);
8335 :
8336 :
8337 : /* Loop through all dimensions and search for mapping */
8338 : /* -------------------------------------------------- */
8339 0 : for (i = 0; i < nDim; i++)
8340 : {
8341 0 : memcpy(utlbuf, ptr[i], slen[i]);
8342 0 : utlbuf[slen[i]] = 0;
8343 0 : statMap = SWmapinfo(swathID, geodim, utlbuf,
8344 : &dum, &incr);
8345 :
8346 : /* Mapping found */
8347 : /* ------------- */
8348 0 : if (statMap == 0)
8349 : {
8350 0 : found = 1;
8351 0 : index = i;
8352 0 : break;
8353 : }
8354 : }
8355 :
8356 :
8357 : /* If mapping not found check for geodim within dimlist */
8358 : /* ---------------------------------------------------- */
8359 0 : if (found == 0)
8360 : {
8361 0 : index = EHstrwithin(geodim, dimlist, ',');
8362 :
8363 : /* Geo dimension found within subset field dimlist */
8364 : /* ----------------------------------------------- */
8365 0 : if (index != -1)
8366 : {
8367 0 : found = 1;
8368 0 : incr = 1;
8369 : }
8370 : }
8371 :
8372 :
8373 :
8374 : /* If mapping not found check for indexed mapping */
8375 : /* ---------------------------------------------- */
8376 0 : if (found == 0)
8377 : {
8378 : /* Get size of geo dim & allocate space of index mapping */
8379 : /* ----------------------------------------------------- */
8380 0 : dum = SWdiminfo(swathID, geodim);
8381 0 : idxmap = (int32 *) calloc(dum, sizeof(int32));
8382 0 : if(idxmap == NULL)
8383 : {
8384 0 : HEpush(DFE_NOSPACE,"SWregioninfo", __FILE__, __LINE__);
8385 0 : return(-1);
8386 : }
8387 :
8388 : /* Loop through all dimensions and search for mapping */
8389 : /* -------------------------------------------------- */
8390 0 : for (i = 0; i < nDim; i++)
8391 : {
8392 0 : memcpy(utlbuf, ptr[i], slen[i]);
8393 0 : utlbuf[slen[i]] = 0;
8394 :
8395 0 : idxMapElem = SWidxmapinfo(swathID, geodim, utlbuf, idxmap);
8396 :
8397 :
8398 : /* Mapping found */
8399 : /* ------------- */
8400 0 : if (idxMapElem != -1)
8401 : {
8402 0 : found = 1;
8403 0 : index = i;
8404 0 : break;
8405 : }
8406 : }
8407 : }
8408 :
8409 :
8410 : /* Regular Mapping Found */
8411 : /* --------------------- */
8412 0 : if (found == 1 && idxMapElem == -1)
8413 : {
8414 0 : dims[index] = 0;
8415 :
8416 : /* Loop through all regions */
8417 : /* ------------------------ */
8418 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
8419 : {
8420 : /* Get number of cross tracks in particular region */
8421 : /* ----------------------------------------------- */
8422 0 : nXtrk = SWXRegion[regionID]->StopRegion[k] -
8423 0 : SWXRegion[regionID]->StartRegion[k] + 1;
8424 :
8425 :
8426 : /* If increment is positive (geodim <= datadim) ... */
8427 : /* ------------------------------------------------ */
8428 0 : if (incr > 0)
8429 : {
8430 0 : if (SWXRegion[regionID]->scanflag == 1)
8431 : {
8432 0 : scan_shift = nXtrk % incr;
8433 0 : if(scan_shift != 0)
8434 0 : nXtrk = nXtrk - scan_shift;
8435 0 : dims[index] += nXtrk/incr;
8436 0 : if(long_status == -1 || incr == 1)
8437 : {
8438 0 : scan_shift = nXtrk % detect_cnt;
8439 0 : if(scan_shift != 0)
8440 0 : dims[index] = nXtrk - scan_shift;
8441 : }
8442 : }
8443 : else
8444 : {
8445 0 : dims[index] += nXtrk * incr;
8446 : }
8447 : }
8448 : else
8449 : {
8450 : /* Negative increment (geodim > datadim) */
8451 : /* ------------------------------------- */
8452 0 : dims[index] += nXtrk / (-incr);
8453 :
8454 : /*
8455 : * If Xtrk not exactly divisible by incr, round dims
8456 : * to next highest integer
8457 : */
8458 0 : if (nXtrk % (-incr) != 0)
8459 : {
8460 0 : dims[index]++;
8461 : }
8462 : }
8463 : }
8464 : }
8465 0 : else if (found == 1 && idxMapElem != -1)
8466 : {
8467 :
8468 : /* Indexed Mapping */
8469 : /* --------------- */
8470 :
8471 0 : dims[index] = 0;
8472 :
8473 : /* Loop through all regions */
8474 : /* ------------------------ */
8475 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
8476 : {
8477 0 : j = 0;
8478 0 : if(SWXRegion[regionID]->scanflag == 1)
8479 : {
8480 0 : startscanline = SWXRegion[regionID]->StartRegion[k];
8481 0 : stopscanline = SWXRegion[regionID]->StopRegion[k];
8482 0 : if (strcmp(fieldname,dfieldlist) == 0)
8483 : {
8484 0 : dims[index] = stopscanline - startscanline + 1;
8485 0 : datafld = 1;
8486 : }
8487 0 : if (strcmp(fieldname, "Latitude") == 0 ||
8488 0 : strcmp(fieldname, "Longitude") == 0)
8489 : {
8490 0 : if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3)
8491 : {
8492 0 : status = SWfieldinfo(swathID,"scan_no",&rk,dims2,&nt,dimlist);
8493 0 : tbuffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
8494 0 : status =SWreadfield(swathID,"scan_no",NULL,NULL,NULL,tbuffer);
8495 0 : startscanline = startscanline - ((tbuffer[0] * detect_cnt) - detect_cnt);
8496 0 : stopscanline = stopscanline - ((tbuffer[0] * detect_cnt) - 1);
8497 : }
8498 0 : if(SWXRegion[regionID]->band8flag == 2 ||
8499 0 : SWXRegion[regionID]->band8flag == 3)
8500 : {
8501 0 : if(startscandim == -1)
8502 0 : if(startscanline < idxmap[0])
8503 : {
8504 0 : startscandim = 0;
8505 0 : dims[index] = 0;
8506 0 : if(stopscanline > idxmap[scene_cnt *2 - 1])
8507 : {
8508 0 : stopscandim = scene_cnt*2 - startscandim;
8509 0 : dims[index] = scene_cnt*2 - startscandim;
8510 : }
8511 : }
8512 : }
8513 0 : for (l = 0; l < scene_cnt; l++)
8514 : {
8515 0 : if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
8516 0 : if(startscandim == -1)
8517 : {
8518 0 : dims[index] = j;
8519 0 : startscandim = j;
8520 : }
8521 0 : if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
8522 0 : if(startscandim != -1)
8523 : {
8524 0 : dims[index] = j - startscandim + 2;
8525 0 : stopscandim = j + 1;
8526 : }
8527 0 : j = j + 2;
8528 0 : if(idxmap[j] == 0 || idxmap[j+1] == 0)
8529 0 : l = scene_cnt;
8530 : }
8531 0 : if(SWXRegion[regionID]->band8flag == 1 ||
8532 0 : SWXRegion[regionID]->band8flag == 2)
8533 : {
8534 0 : if(stopscandim == -1)
8535 0 : if(stopscanline > idxmap[scene_cnt * 2 - 1])
8536 : {
8537 0 : stopscandim = scene_cnt*2 - dims[index];
8538 0 : dims[index] = scene_cnt*2 - dims[index];
8539 : }
8540 : }
8541 0 : if(SWXRegion[regionID]->band8flag == 3)
8542 : {
8543 0 : if(startscandim == -1)
8544 0 : if(startscanline < idxmap[0])
8545 : {
8546 0 : startscandim = 0;
8547 0 : if(stopscandim != -1)
8548 0 : dims[index] = stopscandim - startscandim + 1;
8549 : }
8550 : }
8551 0 : if(SWXRegion[regionID]->band8flag == 2)
8552 : {
8553 0 : if(startscandim == -1)
8554 0 : if(startscanline > idxmap[j - 1])
8555 : {
8556 0 : status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank2,dims2,&nt,dimlist);
8557 0 : buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
8558 0 : status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
8559 0 : startscanline = startscanline - (buffer2[0] * detect_cnt);
8560 0 : stopscanline = stopscanline - (buffer2[0] * detect_cnt);
8561 0 : free(buffer2);
8562 0 : j = 0;
8563 0 : for (l = 0; l < scene_cnt; l++)
8564 : {
8565 0 : if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
8566 : {
8567 0 : dims[index] = j;
8568 0 : startscandim = j;
8569 : }
8570 0 : if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
8571 0 : dims[index] = j - startscandim + 2;
8572 0 : j = j + 2;
8573 0 : if(idxmap[j] == 0 || idxmap[j+1] == 0)
8574 0 : l = scene_cnt;
8575 : }
8576 :
8577 : }
8578 : }
8579 : }
8580 0 : if (strcmp(fieldname, "scene_center_latitude") == 0 ||
8581 0 : strcmp(fieldname, "scene_center_longitude") == 0)
8582 : {
8583 0 : startscanline = SWXRegion[regionID]->StartRegion[k];
8584 0 : stopscanline = SWXRegion[regionID]->StopRegion[k];
8585 0 : if(startscanline < idxmap[0])
8586 : {
8587 0 : startscandim = 0;
8588 0 : dims[index] = 0;
8589 : }
8590 0 : for (l = 0; l < scene_cnt-1; l++)
8591 : {
8592 0 : if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
8593 0 : if(startscandim == -1)
8594 : {
8595 0 : dims[index] = l;
8596 0 : startscandim = l;
8597 : }
8598 0 : if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
8599 : {
8600 0 : dims[index] = l - startscandim + 2;
8601 0 : stopscandim = l + 1;
8602 : }
8603 : }
8604 0 : if(stopscandim == -1)
8605 : {
8606 0 : if(stopscanline > idxmap[scene_cnt - 1])
8607 : {
8608 0 : dims[index] = scene_cnt - startscandim;
8609 0 : stopscandim = scene_cnt - 1;
8610 : }
8611 : }
8612 0 : if(SWXRegion[regionID]->band8flag == 1)
8613 : {
8614 0 : if(stopscandim == -1)
8615 0 : if(stopscanline > idxmap[scene_cnt - 1])
8616 : {
8617 0 : dims[index] = scene_cnt - startscandim;
8618 0 : stopscandim = scene_cnt - 1;
8619 : }
8620 : }
8621 0 : if(SWXRegion[regionID]->band8flag == 2 ||
8622 0 : SWXRegion[regionID]->band8flag == 3)
8623 : {
8624 0 : if(startscandim == -1)
8625 : {
8626 0 : if(startscanline < idxmap[0])
8627 : {
8628 0 : startscandim = 0;
8629 0 : dims[index] = stopscandim - startscandim + 1;
8630 : }
8631 : }
8632 0 : if(startscandim == -1)
8633 : {
8634 0 : startscanline = SWXRegion[regionID]->StartScan[k] * detect_cnt;
8635 0 : stopscanline = SWXRegion[regionID]->StopScan[k] * detect_cnt;
8636 0 : for (l = 0; l < scene_cnt-1; l++)
8637 : {
8638 0 : if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
8639 0 : dims[index] = l;
8640 0 : if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
8641 0 : dims[index] = l - dims[index] + 1;
8642 : }
8643 : }
8644 : }
8645 : }
8646 : }
8647 : else
8648 : {
8649 0 : if (datafld != 1)
8650 : {
8651 : /* Get number of cross tracks in particular region */
8652 : /* ----------------------------------------------- */
8653 0 : nXtrk = idxmap[SWXRegion[regionID]->StopRegion[k]] -
8654 0 : idxmap[SWXRegion[regionID]->StartRegion[k]] + 1;
8655 :
8656 0 : dims[index] += nXtrk;
8657 : }
8658 : }
8659 : }
8660 : }
8661 : else
8662 : {
8663 : /* Mapping not found */
8664 : /* ----------------- */
8665 0 : status = -1;
8666 0 : HEpush(DFE_GENAPP, "SWregioninfo",
8667 : __FILE__, __LINE__);
8668 0 : HEreport(
8669 : "Mapping Not Defined for \"%s\" Dimension.\n",
8670 : geodim);
8671 : }
8672 : }
8673 :
8674 :
8675 :
8676 : /* Vertical Subset */
8677 : /* --------------- */
8678 0 : if (status == 0 || status == -1) /* check the vertical subset in any case -- xhua */
8679 : {
8680 0 : for (j = 0; j < 8; j++)
8681 : {
8682 : /* If active vertical subset ... */
8683 : /* ----------------------------- */
8684 0 : if (SWXRegion[regionID]->StartVertical[j] != -1)
8685 : {
8686 :
8687 : /* Find vertical dimension within dimlist */
8688 : /* -------------------------------------- */
8689 0 : index = EHstrwithin(SWXRegion[regionID]->DimNamePtr[j],
8690 : dimlist, ',');
8691 :
8692 : /* If dimension found ... */
8693 : /* ---------------------- */
8694 0 : if (index != -1)
8695 : {
8696 : /* Compute dimension size */
8697 : /* ---------------------- */
8698 0 : dims[index] =
8699 0 : SWXRegion[regionID]->StopVertical[j] -
8700 0 : SWXRegion[regionID]->StartVertical[j] + 1;
8701 : }
8702 : else
8703 : {
8704 : /* Vertical dimension not found */
8705 : /* ---------------------------- */
8706 0 : status = -1;
8707 0 : *size = -1;
8708 0 : HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__);
8709 0 : HEreport(errMesg, SWXRegion[regionID]->DimNamePtr[j]);
8710 : }
8711 : }
8712 : }
8713 :
8714 :
8715 :
8716 : /* Compute size of region data buffer */
8717 : /* ---------------------------------- */
8718 0 : if (status == 0)
8719 : {
8720 0 : if(idxMapElem == 1 && SWXRegion[regionID]->scanflag == 1 && land_status == 0)
8721 : {
8722 0 : if(startscandim == dims[0])
8723 0 : dims[0] = scene_cnt*2 - startscandim;
8724 : }
8725 :
8726 : /* Compute number of total elements */
8727 : /* -------------------------------- */
8728 0 : *size = dims[0];
8729 0 : for (j = 1; j < *rank; j++)
8730 : {
8731 0 : *size *= dims[j];
8732 : }
8733 :
8734 : /* Multiply by size in bytes of numbertype */
8735 : /* --------------------------------------- */
8736 0 : *size *= DFKNTsize(*ntype);
8737 : }
8738 : }
8739 : }
8740 :
8741 :
8742 :
8743 : /* Free index mappings if applicable */
8744 : /* --------------------------------- */
8745 0 : if (idxmap != NULL)
8746 : {
8747 0 : free(idxmap);
8748 : }
8749 0 : if(dfieldlist != NULL)
8750 0 : free(dfieldlist);
8751 :
8752 0 : return (status);
8753 : }
8754 :
8755 :
8756 :
8757 : /*----------------------------------------------------------------------------|
8758 : | BEGIN_PROLOG |
8759 : | |
8760 : | FUNCTION: SWperiodinfo |
8761 : | |
8762 : | DESCRIPTION: Returns size in bytes of region |
8763 : | |
8764 : | |
8765 : | Return Value Type Units Description |
8766 : | ============ ====== ========= ===================================== |
8767 : | status intn return status (0) SUCCEED, (-1) FAIL |
8768 : | |
8769 : | INPUTS: |
8770 : | swathID int32 Swath structure ID |
8771 : | periodID int32 Period ID |
8772 : | fieldname char Fieldname |
8773 : | |
8774 : | |
8775 : | OUTPUTS: |
8776 : | ntype int32 field number type |
8777 : | rank int32 field rank |
8778 : | dims int32 dimensions of field region |
8779 : | size int32 size in bytes of field region |
8780 : | |
8781 : | NOTES: |
8782 : | |
8783 : | |
8784 : | Date Programmer Description |
8785 : | ====== ============ ================================================= |
8786 : | Jun 96 Joel Gales Original Programmer |
8787 : | Jun 03 Abe Taaheri added clearing timeflag if SWregioninfo failes |
8788 : | |
8789 : | END_PROLOG |
8790 : -----------------------------------------------------------------------------*/
8791 : intn
8792 0 : SWperiodinfo(int32 swathID, int32 periodID, char *fieldname,
8793 : int32 * ntype, int32 * rank, int32 dims[], int32 * size)
8794 : {
8795 : intn status; /* routine return status variable */
8796 :
8797 :
8798 0 : timeflag = 1;
8799 : /* Call SWregioninfo */
8800 : /* ----------------- */
8801 0 : status = SWregioninfo(swathID, periodID, fieldname, ntype, rank,
8802 : dims, size);
8803 0 : if (status != 0) timeflag = 0;/* clear timeflag if SWregioninfo failed */
8804 0 : return (status);
8805 : }
8806 :
8807 :
8808 :
8809 :
8810 :
8811 : /*----------------------------------------------------------------------------|
8812 : | BEGIN_PROLOG |
8813 : | |
8814 : | FUNCTION: SWdefvrtregion |
8815 : | |
8816 : | DESCRIPTION: Finds elements of a monotonic field within a given range. |
8817 : | |
8818 : | |
8819 : | Return Value Type Units Description |
8820 : | ============ ====== ========= ===================================== |
8821 : | regionID int32 Region ID |
8822 : | |
8823 : | INPUTS: |
8824 : | swathID int32 Swath structure ID |
8825 : | regionID int32 Region ID |
8826 : | vertObj char Vertical object to subset |
8827 : | range float64 Vertical subsetting range |
8828 : | |
8829 : | OUTPUTS: |
8830 : | None |
8831 : | |
8832 : | NOTES: |
8833 : | |
8834 : | |
8835 : | Date Programmer Description |
8836 : | ====== ============ ================================================= |
8837 : | Aug 96 Joel Gales Original Programmer |
8838 : | Dec 96 Joel Gales Add multiple vertical subsetting capability |
8839 : | May 97 Joel Gales Check for supported field types |
8840 : | |
8841 : | END_PROLOG |
8842 : -----------------------------------------------------------------------------*/
8843 :
8844 :
8845 : /* Macro to initialize swath region entry */
8846 : /* -------------------------------------- */
8847 :
8848 : /*
8849 : * 1) Find empty (inactive) region. 2) Allocate space for region entry. 3)
8850 : * Store file ID and swath ID. 4) Set region ID. 5) Initialize vertical
8851 : * subset entries to -1.
8852 : */
8853 :
8854 : #define SETSWTHREG \
8855 : \
8856 : for (k = 0; k < NSWATHREGN; k++) \
8857 : { \
8858 : if (SWXRegion[k] == 0) \
8859 : { \
8860 : SWXRegion[k] = (struct swathRegion *) \
8861 : calloc(1, sizeof(struct swathRegion)); \
8862 : SWXRegion[k]->fid = fid; \
8863 : SWXRegion[k]->swathID = swathID; \
8864 : regionID = k; \
8865 : for (j=0; j<8; j++) \
8866 : { \
8867 : SWXRegion[k]->StartVertical[j] = -1; \
8868 : SWXRegion[k]->StopVertical[j] = -1; \
8869 : SWXRegion[k]->StartScan[j] = -1; \
8870 : SWXRegion[k]->StopScan[j] = -1; \
8871 : SWXRegion[k]->band8flag = -1; \
8872 : } \
8873 : break; \
8874 : } \
8875 : }
8876 :
8877 :
8878 : /* Macro to fill vertical subset entry */
8879 : /* ----------------------------------- */
8880 :
8881 : /*
8882 : * 1) Find empty (inactive) vertical region. 2) Set start of vertical region.
8883 : * 3) Allocate space for name of vertical dimension. 4) Write vertical
8884 : * dimension name.
8885 : */
8886 :
8887 : #define FILLVERTREG \
8888 : for (j=0; j<8; j++) \
8889 : { \
8890 : if (SWXRegion[regionID]->StartVertical[j] == -1) \
8891 : { \
8892 : SWXRegion[regionID]->StartVertical[j] = i; \
8893 : SWXRegion[regionID]->DimNamePtr[j] = \
8894 : (char *) malloc(slen + 1); \
8895 : memcpy(SWXRegion[regionID]->DimNamePtr[j], \
8896 : dimlist, slen + 1); \
8897 : break; \
8898 : } \
8899 : } \
8900 :
8901 :
8902 :
8903 : int32
8904 0 : SWdefvrtregion(int32 swathID, int32 regionID, char *vertObj, float64 range[])
8905 : {
8906 : intn i; /* Loop index */
8907 : intn j; /* Loop index */
8908 : intn k; /* Loop index */
8909 : intn status; /* routine return status variable */
8910 :
8911 0 : uint8 found = 0; /* Found flag */
8912 :
8913 : int16 vertINT16; /* Temporary INT16 variable */
8914 :
8915 : int32 fid; /* HDF-EOS file ID */
8916 : int32 sdInterfaceID; /* HDF SDS interface ID */
8917 : int32 swVgrpID; /* Swath Vgroup ID */
8918 : int32 slen; /* String length */
8919 : int32 rank; /* Field rank */
8920 : int32 nt; /* Field numbertype */
8921 : int32 dims[8]; /* Field dimensions */
8922 : int32 size; /* Size of numbertype in bytes */
8923 : int32 vertINT32; /* Temporary INT32 variable */
8924 :
8925 : float32 vertFLT32; /* Temporary FLT32 variable */
8926 :
8927 : float64 vertFLT64; /* Temporary FLT64 variable */
8928 :
8929 : char *vertArr; /* Pointer to vertical field data buffer */
8930 : char dimlist[256]; /* Dimension list */
8931 :
8932 :
8933 : /* Check for valid swath ID */
8934 : /* ------------------------ */
8935 0 : status = SWchkswid(swathID, "SWdefvrtregion", &fid, &sdInterfaceID,
8936 : &swVgrpID);
8937 :
8938 0 : if (status == 0)
8939 : {
8940 : /* Copy first 4 characters of vertObj into dimlist */
8941 : /* ----------------------------------------------- */
8942 0 : memcpy(dimlist, vertObj, 4);
8943 0 : dimlist[4] = 0;
8944 :
8945 :
8946 :
8947 : /* If first 4 characters of vertObj = "DIM:" ... */
8948 : /* --------------------------------------------- */
8949 :
8950 : /* Vertical Object is dimension name */
8951 : /* --------------------------------- */
8952 0 : if (strcmp(dimlist, "DIM:") == 0)
8953 : {
8954 : /* Get string length of vertObj (minus "DIM:) */
8955 : /* ------------------------------------------ */
8956 0 : slen = strlen(vertObj) - 4;
8957 :
8958 :
8959 : /* If regionID = -1 then setup swath region entry */
8960 : /* ---------------------------------------------- */
8961 0 : if (regionID == -1)
8962 : {
8963 0 : SETSWTHREG;
8964 : }
8965 :
8966 :
8967 : /* Find first empty (inactive) vertical subset entry */
8968 : /* ------------------------------------------------- */
8969 0 : for (j = 0; j < 8; j++)
8970 : {
8971 0 : if (SWXRegion[regionID]->StartVertical[j] == -1)
8972 : {
8973 : /* Store start & stop of vertical region */
8974 : /* ------------------------------------- */
8975 0 : SWXRegion[regionID]->StartVertical[j] = (int32) range[0];
8976 0 : SWXRegion[regionID]->StopVertical[j] = (int32) range[1];
8977 :
8978 : /* Store vertical dimension name */
8979 : /* ----------------------------- */
8980 0 : SWXRegion[regionID]->DimNamePtr[j] =
8981 0 : (char *) malloc(slen + 1);
8982 0 : if(SWXRegion[regionID]->DimNamePtr[j] == NULL)
8983 : {
8984 0 : HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__);
8985 0 : return(-1);
8986 : }
8987 0 : memcpy(SWXRegion[regionID]->DimNamePtr[j],
8988 0 : vertObj + 4, slen + 1);
8989 0 : break;
8990 : }
8991 : }
8992 : }
8993 : else
8994 : {
8995 :
8996 : /* Vertical Object is fieldname */
8997 : /* ---------------------------- */
8998 :
8999 :
9000 : /* Check for valid fieldname */
9001 : /* ------------------------- */
9002 0 : status = SWfieldinfo(swathID, vertObj, &rank, dims, &nt,
9003 : dimlist);
9004 :
9005 0 : if (status != 0)
9006 : {
9007 0 : status = -1;
9008 0 : HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__);
9009 0 : HEreport("Vertical Field: \"%s\" not found.\n", vertObj);
9010 : }
9011 :
9012 :
9013 :
9014 : /* Check for supported field types */
9015 : /* ------------------------------- */
9016 0 : if (nt != DFNT_INT16 &&
9017 0 : nt != DFNT_INT32 &&
9018 0 : nt != DFNT_FLOAT32 &&
9019 0 : nt != DFNT_FLOAT64)
9020 : {
9021 0 : status = -1;
9022 0 : HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__);
9023 0 : HEreport("Fieldtype: %d not supported for vertical subsetting.\n", nt);
9024 : }
9025 :
9026 :
9027 :
9028 : /* Check that vertical dimension is 1D */
9029 : /* ----------------------------------- */
9030 0 : if (status == 0)
9031 : {
9032 0 : if (rank != 1)
9033 : {
9034 0 : status = -1;
9035 0 : HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__);
9036 0 : HEreport("Vertical Field: \"%s\" must be 1-dim.\n",
9037 : vertObj);
9038 : }
9039 : }
9040 :
9041 :
9042 : /* If no problems then continue */
9043 : /* ---------------------------- */
9044 0 : if (status == 0)
9045 : {
9046 : /* Get string length of vertical dimension */
9047 : /* --------------------------------------- */
9048 0 : slen = strlen(dimlist);
9049 :
9050 :
9051 : /* Get size in bytes of vertical field numbertype */
9052 : /* ---------------------------------------------- */
9053 0 : size = DFKNTsize(nt);
9054 :
9055 :
9056 : /* Allocate space for vertical field */
9057 : /* --------------------------------- */
9058 0 : vertArr = (char *) calloc(dims[0], size);
9059 0 : if(vertArr == NULL)
9060 : {
9061 0 : HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__);
9062 0 : return(-1);
9063 : }
9064 :
9065 : /* Read vertical field */
9066 : /* ------------------- */
9067 0 : status = SWreadfield(swathID, vertObj,
9068 : NULL, NULL, NULL, vertArr);
9069 :
9070 :
9071 :
9072 0 : switch (nt)
9073 : {
9074 : case DFNT_INT16:
9075 :
9076 0 : for (i = 0; i < dims[0]; i++)
9077 : {
9078 : /* Get single element of vertical field */
9079 : /* ------------------------------------ */
9080 0 : memcpy(&vertINT16, vertArr + i * size, size);
9081 :
9082 :
9083 : /* If within range ... */
9084 : /* ------------------- */
9085 0 : if (vertINT16 >= range[0] &&
9086 0 : vertINT16 <= range[1])
9087 : {
9088 : /* Set found flag */
9089 : /* -------------- */
9090 0 : found = 1;
9091 :
9092 :
9093 : /* If regionID=-1 then setup swath region entry */
9094 : /* -------------------------------------------- */
9095 0 : if (regionID == -1)
9096 : {
9097 0 : SETSWTHREG;
9098 : }
9099 :
9100 :
9101 : /* Fill-in vertical region entries */
9102 : /* ------------------------------- */
9103 0 : FILLVERTREG;
9104 :
9105 0 : break;
9106 : }
9107 : }
9108 :
9109 :
9110 : /* If found read from "bottom" of data field */
9111 : /* ----------------------------------------- */
9112 0 : if (found == 1)
9113 : {
9114 0 : for (i = dims[0] - 1; i >= 0; i--)
9115 : {
9116 : /* Get single element of vertical field */
9117 : /* ------------------------------------ */
9118 0 : memcpy(&vertINT16, vertArr + i * size, size);
9119 :
9120 :
9121 : /* If within range ... */
9122 : /* ------------------- */
9123 0 : if (vertINT16 >= range[0] &&
9124 0 : vertINT16 <= range[1])
9125 : {
9126 : /* Set end of vertical region */
9127 : /* -------------------------- */
9128 0 : SWXRegion[regionID]->StopVertical[j] = i;
9129 0 : break;
9130 : }
9131 : }
9132 : }
9133 : else
9134 : {
9135 : /* No vertical entries within region */
9136 : /* --------------------------------- */
9137 0 : status = -1;
9138 0 : HEpush(DFE_GENAPP, "SWdefvrtregion",
9139 : __FILE__, __LINE__);
9140 0 : HEreport("No vertical field entries within region.\n");
9141 : }
9142 0 : break;
9143 :
9144 :
9145 : case DFNT_INT32:
9146 :
9147 0 : for (i = 0; i < dims[0]; i++)
9148 : {
9149 : /* Get single element of vertical field */
9150 : /* ------------------------------------ */
9151 0 : memcpy(&vertINT32, vertArr + i * size, size);
9152 :
9153 :
9154 : /* If within range ... */
9155 : /* ------------------- */
9156 0 : if (vertINT32 >= range[0] &&
9157 0 : vertINT32 <= range[1])
9158 : {
9159 : /* Set found flag */
9160 : /* -------------- */
9161 0 : found = 1;
9162 :
9163 :
9164 : /* If regionID=-1 then setup swath region entry */
9165 : /* -------------------------------------------- */
9166 0 : if (regionID == -1)
9167 : {
9168 0 : SETSWTHREG;
9169 : }
9170 :
9171 :
9172 : /* Fill-in vertical region entries */
9173 : /* ------------------------------- */
9174 0 : FILLVERTREG;
9175 :
9176 0 : break;
9177 : }
9178 : }
9179 :
9180 :
9181 : /* If found read from "bottom" of data field */
9182 : /* ----------------------------------------- */
9183 0 : if (found == 1)
9184 : {
9185 0 : for (i = dims[0] - 1; i >= 0; i--)
9186 : {
9187 : /* Get single element of vertical field */
9188 : /* ------------------------------------ */
9189 0 : memcpy(&vertINT32, vertArr + i * size, size);
9190 :
9191 :
9192 : /* If within range ... */
9193 : /* ------------------- */
9194 0 : if (vertINT32 >= range[0] &&
9195 0 : vertINT32 <= range[1])
9196 : {
9197 : /* Set end of vertical region */
9198 : /* -------------------------- */
9199 0 : SWXRegion[regionID]->StopVertical[j] = i;
9200 0 : break;
9201 : }
9202 : }
9203 : }
9204 : else
9205 : {
9206 : /* No vertical entries within region */
9207 : /* --------------------------------- */
9208 0 : status = -1;
9209 0 : HEpush(DFE_GENAPP, "SWdefvrtregion",
9210 : __FILE__, __LINE__);
9211 0 : HEreport("No vertical field entries within region.\n");
9212 : }
9213 0 : break;
9214 :
9215 :
9216 : case DFNT_FLOAT32:
9217 :
9218 0 : for (i = 0; i < dims[0]; i++)
9219 : {
9220 : /* Get single element of vertical field */
9221 : /* ------------------------------------ */
9222 0 : memcpy(&vertFLT32, vertArr + i * size, size);
9223 :
9224 :
9225 : /* If within range ... */
9226 : /* ------------------- */
9227 0 : if (vertFLT32 >= range[0] &&
9228 0 : vertFLT32 <= range[1])
9229 : {
9230 : /* Set found flag */
9231 : /* -------------- */
9232 0 : found = 1;
9233 :
9234 :
9235 : /* If regionID=-1 then setup swath region entry */
9236 : /* -------------------------------------------- */
9237 0 : if (regionID == -1)
9238 : {
9239 0 : SETSWTHREG;
9240 : }
9241 :
9242 :
9243 : /* Fill-in vertical region entries */
9244 : /* ------------------------------- */
9245 0 : FILLVERTREG;
9246 :
9247 0 : break;
9248 : }
9249 : }
9250 :
9251 :
9252 : /* If found read from "bottom" of data field */
9253 : /* ----------------------------------------- */
9254 0 : if (found == 1)
9255 : {
9256 0 : for (i = dims[0] - 1; i >= 0; i--)
9257 : {
9258 : /* Get single element of vertical field */
9259 : /* ------------------------------------ */
9260 0 : memcpy(&vertFLT32, vertArr + i * size, size);
9261 :
9262 :
9263 : /* If within range ... */
9264 : /* ------------------- */
9265 0 : if (vertFLT32 >= range[0] &&
9266 0 : vertFLT32 <= range[1])
9267 : {
9268 : /* Set end of vertical region */
9269 : /* -------------------------- */
9270 0 : SWXRegion[regionID]->StopVertical[j] = i;
9271 0 : break;
9272 : }
9273 : }
9274 : }
9275 : else
9276 : {
9277 : /* No vertical entries within region */
9278 : /* --------------------------------- */
9279 0 : status = -1;
9280 0 : HEpush(DFE_GENAPP, "SWdefvrtregion",
9281 : __FILE__, __LINE__);
9282 0 : HEreport("No vertical field entries within region.\n");
9283 : }
9284 0 : break;
9285 :
9286 :
9287 : case DFNT_FLOAT64:
9288 :
9289 0 : for (i = 0; i < dims[0]; i++)
9290 : {
9291 : /* Get single element of vertical field */
9292 : /* ------------------------------------ */
9293 0 : memcpy(&vertFLT64, vertArr + i * size, size);
9294 :
9295 :
9296 : /* If within range ... */
9297 : /* ------------------- */
9298 0 : if (vertFLT64 >= range[0] &&
9299 0 : vertFLT64 <= range[1])
9300 : {
9301 : /* Set found flag */
9302 : /* -------------- */
9303 0 : found = 1;
9304 :
9305 :
9306 : /* If regionID=-1 then setup swath region entry */
9307 : /* -------------------------------------------- */
9308 0 : if (regionID == -1)
9309 : {
9310 0 : SETSWTHREG;
9311 : }
9312 :
9313 :
9314 : /* Fill-in vertical region entries */
9315 : /* ------------------------------- */
9316 0 : FILLVERTREG;
9317 :
9318 0 : break;
9319 : }
9320 : }
9321 :
9322 :
9323 : /* If found read from "bottom" of data field */
9324 : /* ----------------------------------------- */
9325 0 : if (found == 1)
9326 : {
9327 0 : for (i = dims[0] - 1; i >= 0; i--)
9328 : {
9329 : /* Get single element of vertical field */
9330 : /* ------------------------------------ */
9331 0 : memcpy(&vertFLT64, vertArr + i * size, size);
9332 :
9333 : /* If within range ... */
9334 : /* ------------------- */
9335 0 : if (vertFLT64 >= range[0] &&
9336 0 : vertFLT64 <= range[1])
9337 : {
9338 : /* Set end of vertical region */
9339 : /* -------------------------- */
9340 0 : SWXRegion[regionID]->StopVertical[j] = i;
9341 0 : break;
9342 : }
9343 : }
9344 : }
9345 : else
9346 : {
9347 : /* No vertical entries within region */
9348 : /* --------------------------------- */
9349 0 : status = -1;
9350 0 : HEpush(DFE_GENAPP, "SWdefvrtregion",
9351 : __FILE__, __LINE__);
9352 0 : HEreport("No vertical field entries within region.\n");
9353 : }
9354 : break;
9355 :
9356 : } /* End of switch */
9357 0 : free(vertArr);
9358 : }
9359 : }
9360 : }
9361 :
9362 :
9363 : /* Set regionID to -1 if bad return status */
9364 : /* --------------------------------------- */
9365 0 : if (status == -1)
9366 : {
9367 0 : regionID = -1;
9368 : }
9369 :
9370 :
9371 0 : return (regionID);
9372 : }
9373 :
9374 :
9375 :
9376 : /*----------------------------------------------------------------------------|
9377 : | BEGIN_PROLOG |
9378 : | |
9379 : | FUNCTION: SWdefscanregion |
9380 : | |
9381 : | DESCRIPTION: Initialize the region structure for Landsat 7 float scene |
9382 : | subset |
9383 : | |
9384 : | |
9385 : | Return Value Type Units Description |
9386 : | ============ ====== ========= ===================================== |
9387 : | regionID int32 Region ID |
9388 : | |
9389 : | INPUTS: |
9390 : | swathID int32 Swath structure ID |
9391 : | fieldname char Field name to subset |
9392 : | range float64 subsetting range |
9393 : | mode int32 HDFE_ENDPOINT, HDFE_MIDPOINT or |
9394 : | HDFE_ANYPOINT |
9395 : | |
9396 : | OUTPUTS: |
9397 : | regionID int32 Region ID |
9398 : | |
9399 : | NOTES: |
9400 : | |
9401 : | |
9402 : | Date Programmer Description |
9403 : | ====== ============ ================================================= |
9404 : | Jul 99 DaW Original Programmer |
9405 : | |
9406 : | END_PROLOG |
9407 : -----------------------------------------------------------------------------*/
9408 : int32
9409 0 : SWdefscanregion(int32 swathID, char *fieldname, float64 range[], int32 mode)
9410 : {
9411 : intn j; /* Loop index */
9412 : intn k; /* Loop index */
9413 : intn status; /* routine return status variable */
9414 0 : intn land_status = 3; /* routine return status variable */
9415 0 : intn band81flag = 0;
9416 0 : intn band82flag = 0;
9417 0 : intn band83flag = 0;
9418 0 : uint8 detect_cnt = 0; /* Used to convert scan to scanline */
9419 : /* L7 float scene sub. */
9420 0 : uint8 scene_cnt = 0;
9421 :
9422 :
9423 0 : int32 nmtype = 0; /* Used for L7 float scene sub. */
9424 0 : int32 count = 0; /* Used for L7 float scene sub. */
9425 : int32 fid; /* HDF-EOS file ID */
9426 : int32 sdInterfaceID; /* HDF SDS interface ID */
9427 : int32 swVgrpID; /* Swath Vgroup ID */
9428 : int32 slen; /* String length */
9429 : int32 dfrank[8]; /* data fields rank */
9430 : int32 rank; /* Field rank */
9431 : int32 numtype[8]; /* number type of data fields */
9432 : int32 nt; /* Field numbertype */
9433 : int32 dims[8]; /* Field dimensions */
9434 : int32 dims2[8]; /* Field dimensions */
9435 0 : int32 strbufsize = 0; /* string buffer size */
9436 0 : int32 tmprange0 = 0;
9437 :
9438 :
9439 0 : uint16 *buffer = (uint16 *)NULL;
9440 0 : int32 *idxmap = (int32 *)NULL;
9441 :
9442 0 : int32 dimsize = 0;
9443 :
9444 0 : int32 regionID = -1; /* Region ID (return) */
9445 :
9446 : float64 scan[2];
9447 : float64 original_scan[2];
9448 :
9449 : char dimlist[256]; /* Dimension list */
9450 : char swathname[80];
9451 0 : char *dfieldlist = (char *)NULL; /* data field list */
9452 0 : char *tfieldname = (char *)NULL; /* temp field buffer */
9453 0 : char *band81 = (char *)NULL;
9454 0 : char *band82 = (char *)NULL;
9455 0 : char *band83 = (char *)NULL;
9456 :
9457 :
9458 : /* Check for valid swath ID */
9459 : /* ------------------------ */
9460 0 : status = SWchkswid(swathID, "SWdefscanregion", &fid, &sdInterfaceID,
9461 : &swVgrpID);
9462 :
9463 : /* This code checks for the attribute detector_count */
9464 : /* which is found in Landsat 7 files. It is used */
9465 : /* for some of the loops. The other code checks if */
9466 : /* one scan is requested. */
9467 : /* ================================================= */
9468 0 : land_status = SWattrinfo(swathID, "detector_count", &nmtype, &count);
9469 0 : if (land_status == 0)
9470 : {
9471 0 : scan[0] = range[0];
9472 0 : scan[1] = range[1];
9473 0 : original_scan[0] = range[0];
9474 0 : original_scan[1] = range[1];
9475 :
9476 0 : land_status = SWreadattr(swathID, "scene_count", &scene_cnt);
9477 0 : land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
9478 0 : if (range[0] == range[1])
9479 : {
9480 0 : range[0] = range[0] * detect_cnt - detect_cnt;
9481 0 : range[1] = range[0] + detect_cnt - 1;
9482 : }
9483 : else
9484 : {
9485 0 : range[0] = range[0] * detect_cnt - detect_cnt;
9486 0 : range[1] = range[1] * detect_cnt - 1;
9487 : }
9488 :
9489 0 : Vgetname(SWXSwath[0].IDTable, swathname);
9490 0 : band81 = strstr(swathname, "B81");
9491 0 : if (band81 != (char *)NULL)
9492 0 : band81flag = 1;
9493 0 : band82 = strstr(swathname, "B82");
9494 0 : if (band82 != (char *)NULL)
9495 0 : band82flag = 1;
9496 0 : band83 = strstr(swathname, "B83");
9497 0 : if (band83 != (char *)NULL)
9498 0 : band83flag = 1;
9499 : }
9500 :
9501 :
9502 : /* If fieldname is null then subsetting Landsat 7 */
9503 : /* floating scene. Get data field name, assume */
9504 : /* only one data field in swath */
9505 : /* ---------------------------------------------- */
9506 :
9507 0 : if (fieldname == (char *)NULL)
9508 : {
9509 0 : (void) SWnentries(swathID, 4, &strbufsize);
9510 0 : dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char));
9511 0 : (void) SWinqdatafields(swathID, dfieldlist, dfrank, numtype);
9512 0 : tfieldname = (char *)calloc(strbufsize + 1, sizeof(char));
9513 0 : strcpy(tfieldname, dfieldlist);
9514 : }
9515 : else
9516 : {
9517 0 : slen = strlen(fieldname);
9518 0 : tfieldname = (char *)calloc(slen + 1, sizeof(char));
9519 0 : strcpy(tfieldname, fieldname);
9520 : }
9521 :
9522 : /* Check for valid fieldname */
9523 : /* ------------------------- */
9524 0 : status = SWfieldinfo(swathID, tfieldname, &rank, dims, &nt,
9525 : dimlist);
9526 :
9527 0 : if (status != 0)
9528 : {
9529 0 : status = -1;
9530 0 : HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
9531 0 : HEreport("Field: \"%s\" not found.\n", tfieldname);
9532 : }
9533 :
9534 :
9535 : /* Check if input range values are within range of */
9536 : /* data field */
9537 : /* ----------------------------------------------- */
9538 0 : if(status == 0)
9539 : {
9540 0 : status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
9541 0 : buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
9542 0 : status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
9543 0 : if(scan[0] > buffer[dims2[0]-1])
9544 : {
9545 0 : HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
9546 0 : HEreport("Range values not within bounds of data field\n");
9547 0 : free(buffer);
9548 0 : buffer = (uint16 *)NULL;
9549 0 : if (dfieldlist != NULL)
9550 0 : free(dfieldlist);
9551 0 : free(tfieldname);
9552 0 : return(-1);
9553 : }
9554 0 : if(scan[0] < buffer[0])
9555 : {
9556 0 : if(scan[1] < buffer[0])
9557 : {
9558 0 : HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
9559 0 : HEreport("Range values not within bounds of data field\n");
9560 0 : free(buffer);
9561 0 : buffer = (uint16 *)NULL;
9562 0 : if (dfieldlist != NULL)
9563 0 : free(dfieldlist);
9564 0 : free(tfieldname);
9565 0 : return(-1);
9566 : }
9567 : else
9568 : {
9569 0 : scan[0] = buffer[0];
9570 0 : range[0] = scan[0] * detect_cnt - detect_cnt;
9571 : }
9572 : }
9573 0 : if(scan[1] > buffer[dims2[0] - 1])
9574 : {
9575 0 : scan[1] = buffer[dims2[0] - 1];
9576 0 : range[1] = scan[1] * detect_cnt - 1;
9577 : }
9578 : }
9579 :
9580 0 : if(status == 0)
9581 : {
9582 0 : dimsize = SWdiminfo(swathID, "GeoTrack");
9583 0 : if(dimsize > 0)
9584 : {
9585 0 : idxmap = (int32 *)calloc(dimsize, sizeof(int32));
9586 0 : (void) SWidxmapinfo(swathID, "GeoTrack", "ScanLineTrack", idxmap);
9587 0 : tmprange0 = range[0];
9588 0 : if(band82flag != 1 && band83flag != 1)
9589 : {
9590 0 : if (range[1] > idxmap[scene_cnt*2 - 1])
9591 : {
9592 0 : range[1] = idxmap[scene_cnt*2 - 1];
9593 0 : HEreport("Data length compared to geolocation length\n");
9594 : }
9595 : }
9596 0 : if(band82flag == 1 || band83flag == 1)
9597 : {
9598 0 : tmprange0 = range[0] - (buffer[0] * detect_cnt - detect_cnt);
9599 : }
9600 0 : if(tmprange0 >= idxmap[scene_cnt * 2 - 1])
9601 : {
9602 0 : HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
9603 0 : HEreport(
9604 : "Range values not within bounds of Latitude/Longitude field(s)\n");
9605 0 : if (dfieldlist != NULL)
9606 0 : free(dfieldlist);
9607 0 : free(tfieldname);
9608 0 : free(buffer);
9609 0 : free(idxmap);
9610 0 : return(-1);
9611 : }
9612 : }
9613 : }
9614 :
9615 0 : if (status == 0)
9616 : {
9617 0 : slen = strlen(tfieldname);
9618 :
9619 0 : SETSWTHREG;
9620 :
9621 : /* Find first empty (inactive) vertical subset entry */
9622 : /* ------------------------------------------------- */
9623 0 : for (j = 0; j < 8; j++)
9624 : {
9625 0 : if (SWXRegion[regionID]->StartVertical[j] == -1)
9626 : {
9627 : /* Store start & stop of region */
9628 : /* ------------------------------------- */
9629 0 : SWXRegion[regionID]->StartScan[j] = (int32) original_scan[0];
9630 0 : SWXRegion[regionID]->StopScan[j] = (int32) original_scan[1];
9631 0 : SWXRegion[regionID]->StartRegion[j] = (int32) range[0];
9632 0 : SWXRegion[regionID]->StopRegion[j] = (int32) range[1];
9633 0 : ++SWXRegion[regionID]->nRegions;
9634 0 : SWXRegion[regionID]->scanflag = 1;
9635 0 : if(band81flag == 1)
9636 0 : SWXRegion[regionID]->band8flag = 1;
9637 0 : if(band82flag == 1)
9638 0 : SWXRegion[regionID]->band8flag = 2;
9639 0 : if(band83flag == 1)
9640 0 : SWXRegion[regionID]->band8flag = 3;
9641 0 : break;
9642 : }
9643 : }
9644 : }
9645 :
9646 :
9647 : /* Set regionID to -1 if bad return status */
9648 : /* --------------------------------------- */
9649 0 : if (status == -1)
9650 : {
9651 0 : regionID = -1;
9652 : }
9653 :
9654 0 : if (dfieldlist != NULL)
9655 0 : free(dfieldlist);
9656 0 : free(tfieldname);
9657 0 : if (buffer != NULL)
9658 0 : free(buffer);
9659 0 : if (idxmap != NULL)
9660 0 : free(idxmap);
9661 :
9662 0 : return (regionID);
9663 : }
9664 :
9665 :
9666 : /*----------------------------------------------------------------------------|
9667 : | BEGIN_PROLOG |
9668 : | |
9669 : | FUNCTION: SWsetfillvalue |
9670 : | |
9671 : | DESCRIPTION: Sets fill value for the specified field. |
9672 : | |
9673 : | |
9674 : | Return Value Type Units Description |
9675 : | ============ ====== ========= ===================================== |
9676 : | status intn return status (0) SUCCEED, (-1) FAIL |
9677 : | |
9678 : | INPUTS: |
9679 : | swathID int32 swath structure ID |
9680 : | fieldname char field name |
9681 : | fillval void fill value |
9682 : | |
9683 : | OUTPUTS: |
9684 : | None |
9685 : | |
9686 : | NOTES: |
9687 : | |
9688 : | |
9689 : | Date Programmer Description |
9690 : | ====== ============ ================================================= |
9691 : | Jun 96 Joel Gales Original Programmer |
9692 : | |
9693 : | END_PROLOG |
9694 : -----------------------------------------------------------------------------*/
9695 : intn
9696 0 : SWsetfillvalue(int32 swathID, char *fieldname, VOIDP fillval)
9697 : {
9698 : intn status; /* routine return status variable */
9699 :
9700 : int32 fid; /* HDF-EOS file ID */
9701 : int32 sdInterfaceID; /* HDF SDS interface ID */
9702 : int32 swVgrpID; /* Swath root Vgroup ID */
9703 : int32 sdid; /* SDS id */
9704 : int32 nt; /* Number type */
9705 : int32 dims[8]; /* Dimensions array */
9706 : int32 dum; /* Dummy variable */
9707 : int32 solo; /* "Solo" (non-merged) field flag */
9708 :
9709 : char name[80]; /* Fill value "attribute" name */
9710 :
9711 : /* Check for valid swath ID and get SDS interface ID */
9712 0 : status = SWchkswid(swathID, "SWsetfillvalue",
9713 : &fid, &sdInterfaceID, &swVgrpID);
9714 :
9715 0 : if (status == 0)
9716 : {
9717 : /* Get field info */
9718 0 : status = SWfieldinfo(swathID, fieldname, &dum, dims, &nt, NULL);
9719 :
9720 0 : if (status == 0)
9721 : {
9722 : /* Get SDS ID and solo flag */
9723 0 : status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
9724 : &sdid, &dum, &dum, &dum,
9725 : dims, &solo);
9726 :
9727 : /* If unmerged field then call HDF set field routine */
9728 0 : if (solo == 1)
9729 : {
9730 0 : status = SDsetfillvalue(sdid, fillval);
9731 : }
9732 :
9733 : /*
9734 : * Store fill value in attribute. Name is given by fieldname
9735 : * prepended with "_FV_"
9736 : */
9737 0 : strcpy(name, "_FV_");
9738 0 : strcat(name, fieldname);
9739 0 : status = SWwriteattr(swathID, name, nt, 1, fillval);
9740 : }
9741 : else
9742 : {
9743 0 : HEpush(DFE_GENAPP, "SWsetfillvalue", __FILE__, __LINE__);
9744 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
9745 : }
9746 : }
9747 0 : return (status);
9748 : }
9749 :
9750 :
9751 :
9752 :
9753 :
9754 : /*----------------------------------------------------------------------------|
9755 : | BEGIN_PROLOG |
9756 : | |
9757 : | FUNCTION: SWgetfillvalue |
9758 : | |
9759 : | DESCRIPTION: Retrieves fill value for a specified field. |
9760 : | |
9761 : | |
9762 : | Return Value Type Units Description |
9763 : | ============ ====== ========= ===================================== |
9764 : | status intn return status (0) SUCCEED, (-1) FAIL |
9765 : | |
9766 : | INPUTS: |
9767 : | swathID int32 swath structure ID |
9768 : | fieldname char field name |
9769 : | |
9770 : | OUTPUTS: |
9771 : | fillval void fill value |
9772 : | |
9773 : | NOTES: |
9774 : | |
9775 : | |
9776 : | Date Programmer Description |
9777 : | ====== ============ ================================================= |
9778 : | Jun 96 Joel Gales Original Programmer |
9779 : | |
9780 : | END_PROLOG |
9781 : -----------------------------------------------------------------------------*/
9782 : intn
9783 0 : SWgetfillvalue(int32 swathID, char *fieldname, VOIDP fillval)
9784 : {
9785 : intn status; /* routine return status variable */
9786 :
9787 : int32 nt; /* Number type */
9788 : int32 dims[8]; /* Dimensions array */
9789 : int32 dum; /* Dummy variable */
9790 :
9791 : char name[80]; /* Fill value "attribute" name */
9792 :
9793 : /* Check for valid swath ID */
9794 0 : status = SWchkswid(swathID, "SWgetfillvalue", &dum, &dum, &dum);
9795 :
9796 0 : if (status == 0)
9797 : {
9798 : /* Get field info */
9799 0 : status = SWfieldinfo(swathID, fieldname, &dum, dims, &nt, NULL);
9800 :
9801 0 : if (status == 0)
9802 : {
9803 : /* Read fill value attribute */
9804 0 : strcpy(name, "_FV_");
9805 0 : strcat(name, fieldname);
9806 0 : status = SWreadattr(swathID, name, fillval);
9807 : }
9808 : else
9809 : {
9810 0 : HEpush(DFE_GENAPP, "SWgetfillvalue", __FILE__, __LINE__);
9811 0 : HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
9812 : }
9813 :
9814 : }
9815 0 : return (status);
9816 : }
9817 :
9818 :
9819 : /*----------------------------------------------------------------------------|
9820 : | BEGIN_PROLOG |
9821 : | |
9822 : | FUNCTION: SWdetach |
9823 : | |
9824 : | DESCRIPTION: Detachs swath structure and performs housekeeping |
9825 : | |
9826 : | |
9827 : | Return Value Type Units Description |
9828 : | ============ ====== ========= ===================================== |
9829 : | status intn return status (0) SUCCEED, (-1) FAIL |
9830 : | |
9831 : | INPUTS: |
9832 : | swathID int32 swath structure ID |
9833 : | |
9834 : | |
9835 : | OUTPUTS: |
9836 : | None |
9837 : | |
9838 : | NOTES: |
9839 : | |
9840 : | |
9841 : | Date Programmer Description |
9842 : | ====== ============ ================================================= |
9843 : | Jun 96 Joel Gales Original Programmer |
9844 : | Aug 96 Joel Gales Cleanup Region External Structure |
9845 : | Sep 96 Joel Gales Setup dim names for SDsetdimnane in dimbuf1 rather |
9846 : | than utlstr |
9847 : | Nov 96 Joel Gales Call SWchkgdid to check for proper swath ID |
9848 : | Dec 96 Joel Gales Add multiple vertical subsetting garbage collection |
9849 : | |
9850 : | END_PROLOG |
9851 : -----------------------------------------------------------------------------*/
9852 : intn
9853 0 : SWdetach(int32 swathID)
9854 :
9855 : {
9856 : intn i; /* Loop index */
9857 : intn j; /* Loop index */
9858 : intn k; /* Loop index */
9859 0 : intn status = 0; /* routine return status variable */
9860 0 : intn statusFill = 0; /* return status from SWgetfillvalue */
9861 :
9862 : uint8 *buf; /* Buffer for blank (initial) 1D records */
9863 :
9864 : int32 vdataID; /* Vdata ID */
9865 : int32 *namelen; /* Pointer to name string length array */
9866 : int32 *dimlen; /* Pointer to dim string length array */
9867 : int32 slen1[3]; /* String length array 1 */
9868 : int32 slen2[3]; /* String length array 2 */
9869 : int32 nflds; /* Number of fields */
9870 : int32 match[5]; /* Merged field match array */
9871 : int32 cmbfldcnt; /* Number of fields combined */
9872 : int32 sdid; /* SDS ID */
9873 : int32 vgid; /* Vgroup ID */
9874 : int32 dims[3]; /* Dimension array */
9875 : int32 *offset; /* Pointer to merged field offset array */
9876 : int32 *indvdims; /* Pointer to merged field size array */
9877 : int32 sdInterfaceID; /* SDS interface ID */
9878 : int32 sID; /* Swath ID - offset */
9879 : int32 nflds0; /* Number of fields */
9880 : int32 *namelen0; /* Pointer to name string length array */
9881 : int32 rank; /* Rank of merged field */
9882 : int32 truerank; /* True rank of merged field */
9883 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
9884 : int32 dum; /* Dummy variable */
9885 :
9886 : char *nambuf; /* Pointer to name buffer */
9887 : char **nameptr; /* Pointer to name string pointer array */
9888 : char **dimptr; /* Pointer to dim string pointer array */
9889 : char **nameptr0; /* Pointer to name string pointer array */
9890 : char *ptr1[3]; /* String pointer array */
9891 : char *ptr2[3]; /* String pointer array */
9892 : char dimbuf1[128]; /* Dimension buffer 1 */
9893 : char dimbuf2[128]; /* Dimension buffer 2 */
9894 : char swathname[VGNAMELENMAX + 1]; /* Swath name */
9895 : char *utlbuf; /* Utility buffer */
9896 : char fillval[32];/* Fill value buffer */
9897 :
9898 : /* Check for proper swath ID and get SD interface ID */
9899 : /* ------------------------------------------------- */
9900 0 : status = SWchkswid(swathID, "SWdetach", &dum, &sdInterfaceID, &dum);
9901 :
9902 0 : if (status == 0)
9903 : {
9904 : /* Subtract off swath ID offset and get swath name */
9905 : /* ----------------------------------------------- */
9906 0 : sID = swathID % idOffset;
9907 0 : Vgetname(SWXSwath[sID].IDTable, swathname);
9908 :
9909 :
9910 : /* Create 1D "orphened" fields */
9911 : /* --------------------------- */
9912 0 : i = 0;
9913 :
9914 : /* Find "active" entries in 1d combination array */
9915 : /* --------------------------------------------- */
9916 0 : while (SWX1dcomb[3 * i] != 0)
9917 : {
9918 : /* For fields defined within swath... */
9919 : /* ---------------------------------- */
9920 0 : if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable)
9921 : {
9922 : /* Get dimension size and vdata ID */
9923 : /* ------------------------------- */
9924 0 : dims[0] = abs(SWX1dcomb[3 * i]);
9925 0 : vdataID = SWX1dcomb[3 * i + 2];
9926 :
9927 : /* Get fieldname (= vdata name) */
9928 : /* ---------------------------- */
9929 0 : nambuf = (char *) calloc(VSNAMELENMAX + 1, 1);
9930 0 : if(nambuf == NULL)
9931 : {
9932 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
9933 0 : return(-1);
9934 : }
9935 :
9936 0 : VSgetname(vdataID, nambuf);
9937 :
9938 : /* Set field within vdata */
9939 : /* ---------------------- */
9940 0 : VSsetfields(vdataID, nambuf);
9941 :
9942 : /* Write (blank) records */
9943 : /* --------------------- */
9944 0 : buf = (uint8 *) calloc(VSsizeof(vdataID, nambuf), dims[0]);
9945 0 : if(buf == NULL)
9946 : {
9947 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
9948 0 : free(nambuf);
9949 0 : return(-1);
9950 : }
9951 0 : VSwrite(vdataID, buf, dims[0], FULL_INTERLACE);
9952 :
9953 0 : free(buf);
9954 0 : free(nambuf);
9955 :
9956 : /* Detach Vdata */
9957 : /* ------------ */
9958 0 : VSdetach(vdataID);
9959 : }
9960 0 : i++;
9961 : }
9962 :
9963 :
9964 : /* SDS combined fields */
9965 : /* ------------------- */
9966 0 : if (strlen(SWXSDname) == 0)
9967 : {
9968 0 : nflds = 0;
9969 :
9970 : /* Allocate "dummy" arrays so free() doesn't bomb later */
9971 : /* ---------------------------------------------------- */
9972 0 : nameptr = (char **) calloc(1, sizeof(char *));
9973 0 : if(nameptr == NULL)
9974 : {
9975 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
9976 0 : return(-1);
9977 : }
9978 0 : namelen = (int32 *) calloc(1, sizeof(int32));
9979 0 : if(namelen == NULL)
9980 : {
9981 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
9982 0 : free(nameptr);
9983 0 : return(-1);
9984 : }
9985 0 : nameptr0 = (char **) calloc(1, sizeof(char *));
9986 0 : if(nameptr0 == NULL)
9987 : {
9988 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
9989 0 : free(nameptr);
9990 0 : free(namelen);
9991 0 : return(-1);
9992 : }
9993 0 : namelen0 = (int32 *) calloc(1, sizeof(int32));
9994 0 : if(namelen0 == NULL)
9995 : {
9996 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
9997 0 : free(nameptr);
9998 0 : free(namelen);
9999 0 : free(nameptr0);
10000 0 : return(-1);
10001 : }
10002 0 : dimptr = (char **) calloc(1, sizeof(char *));
10003 0 : if(dimptr == NULL)
10004 : {
10005 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10006 0 : free(nameptr);
10007 0 : free(namelen);
10008 0 : free(nameptr0);
10009 0 : free(namelen0);
10010 0 : return(-1);
10011 : }
10012 0 : dimlen = (int32 *) calloc(1, sizeof(int32));
10013 0 : if(dimlen == NULL)
10014 : {
10015 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10016 0 : free(nameptr);
10017 0 : free(namelen);
10018 0 : free(nameptr0);
10019 0 : free(namelen0);
10020 0 : free(dimptr);
10021 0 : return(-1);
10022 : }
10023 0 : offset = (int32 *) calloc(1, sizeof(int32));
10024 0 : if(offset == NULL)
10025 : {
10026 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10027 0 : free(nameptr);
10028 0 : free(namelen);
10029 0 : free(nameptr0);
10030 0 : free(namelen0);
10031 0 : free(dimptr);
10032 0 : free(dimlen);
10033 0 : return(-1);
10034 : }
10035 0 : indvdims = (int32 *) calloc(1, sizeof(int32));
10036 0 : if(indvdims == NULL)
10037 : {
10038 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10039 0 : free(nameptr);
10040 0 : free(namelen);
10041 0 : free(nameptr0);
10042 0 : free(namelen0);
10043 0 : free(dimptr);
10044 0 : free(dimlen);
10045 0 : free(offset);
10046 0 : return(-1);
10047 : }
10048 : }
10049 : else
10050 : {
10051 : /*
10052 : * "Trim Off" trailing "," and ";" in SWXSDname & SWXSDdims
10053 : * respectively
10054 : */
10055 0 : SWXSDname[strlen(SWXSDname) - 1] = 0;
10056 0 : SWXSDdims[strlen(SWXSDdims) - 1] = 0;
10057 :
10058 :
10059 : /* Get number of fields from SWXSDname string */
10060 : /* ------------------------------------------ */
10061 0 : nflds = EHparsestr(SWXSDname, ',', NULL, NULL);
10062 :
10063 :
10064 : /* Allocate space for various dynamic arrays */
10065 : /* ----------------------------------------- */
10066 0 : nameptr = (char **) calloc(nflds, sizeof(char *));
10067 0 : if(nameptr == NULL)
10068 : {
10069 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10070 0 : return(-1);
10071 : }
10072 0 : namelen = (int32 *) calloc(nflds, sizeof(int32));
10073 0 : if(namelen == NULL)
10074 : {
10075 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10076 0 : free(nameptr);
10077 0 : return(-1);
10078 : }
10079 0 : nameptr0 = (char **) calloc(nflds, sizeof(char *));
10080 0 : if(nameptr0 == NULL)
10081 : {
10082 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10083 0 : free(nameptr);
10084 0 : free(namelen);
10085 0 : return(-1);
10086 : }
10087 0 : namelen0 = (int32 *) calloc(nflds, sizeof(int32));
10088 0 : if(namelen0 == NULL)
10089 : {
10090 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10091 0 : free(nameptr);
10092 0 : free(namelen);
10093 0 : free(nameptr0);
10094 0 : return(-1);
10095 : }
10096 0 : dimptr = (char **) calloc(nflds, sizeof(char *));
10097 0 : if(dimptr == NULL)
10098 : {
10099 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10100 0 : free(nameptr);
10101 0 : free(namelen);
10102 0 : free(nameptr0);
10103 0 : free(namelen0);
10104 0 : return(-1);
10105 : }
10106 0 : dimlen = (int32 *) calloc(nflds, sizeof(int32));
10107 0 : if(dimlen == NULL)
10108 : {
10109 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10110 0 : free(nameptr);
10111 0 : free(namelen);
10112 0 : free(nameptr0);
10113 0 : free(namelen0);
10114 0 : free(dimptr);
10115 0 : return(-1);
10116 : }
10117 0 : offset = (int32 *) calloc(nflds, sizeof(int32));
10118 0 : if(offset == NULL)
10119 : {
10120 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10121 0 : free(nameptr);
10122 0 : free(namelen);
10123 0 : free(nameptr0);
10124 0 : free(namelen0);
10125 0 : free(dimptr);
10126 0 : free(dimlen);
10127 0 : return(-1);
10128 : }
10129 0 : indvdims = (int32 *) calloc(nflds, sizeof(int32));
10130 0 : if(indvdims == NULL)
10131 : {
10132 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10133 0 : free(nameptr);
10134 0 : free(namelen);
10135 0 : free(nameptr0);
10136 0 : free(namelen0);
10137 0 : free(dimptr);
10138 0 : free(dimlen);
10139 0 : free(offset);
10140 0 : return(-1);
10141 : }
10142 :
10143 :
10144 : /* Parse SWXSDname and SWXSDdims strings */
10145 : /* ------------------------------------- */
10146 0 : nflds = EHparsestr(SWXSDname, ',', nameptr, namelen);
10147 0 : nflds = EHparsestr(SWXSDdims, ';', dimptr, dimlen);
10148 : }
10149 :
10150 :
10151 : /* Loop through all the fields */
10152 : /* --------------------------- */
10153 0 : for (i = 0; i < nflds; i++)
10154 : {
10155 : /* If active entry and field is within swath to be detached ... */
10156 : /* ------------------------------------------------------------ */
10157 0 : if (SWXSDcomb[5 * i] != 0 &&
10158 0 : SWXSDcomb[5 * i + 3] == SWXSwath[sID].IDTable)
10159 : {
10160 0 : nambuf = (char *) calloc(strlen(SWXSDname) + 1, 1);
10161 0 : if(nambuf == NULL)
10162 : {
10163 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10164 0 : return(-1);
10165 : }
10166 0 : utlbuf = (char *) calloc(2 * strlen(SWXSDname) + 7, 1);
10167 0 : if(utlbuf == NULL)
10168 : {
10169 0 : HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
10170 0 : free(nambuf);
10171 0 : return(-1);
10172 : }
10173 : /* Zero out dimbuf1 */
10174 : /* ---------------- */
10175 0 : for (k = 0; k < sizeof(dimbuf1); k++)
10176 : {
10177 0 : dimbuf1[k] = 0;
10178 : }
10179 :
10180 :
10181 : /* Load array to match, name & parse dims */
10182 : /* -------------------------------------- */
10183 0 : memcpy(match, &SWXSDcomb[5 * i], 20);
10184 0 : memcpy(nambuf, nameptr[i], namelen[i]);
10185 :
10186 0 : memcpy(dimbuf1, dimptr[i], dimlen[i]);
10187 0 : dum = EHparsestr(dimbuf1, ',', ptr1, slen1);
10188 :
10189 :
10190 : /* Separate combined (first) dimension from others */
10191 : /* ----------------------------------------------- */
10192 0 : dimbuf1[slen1[0]] = 0;
10193 :
10194 0 : offset[0] = 0;
10195 0 : indvdims[0] = abs(match[0]);
10196 :
10197 : /*
10198 : * Loop through remaining fields to check for matches with
10199 : * current one
10200 : */
10201 0 : for (j = i + 1, cmbfldcnt = 0; j < nflds; j++)
10202 : {
10203 0 : if (SWXSDcomb[5 * j] != 0)
10204 : {
10205 : /* Zero out dimbuf2 */
10206 : /* ---------------- */
10207 0 : for (k = 0; k < sizeof(dimbuf2); k++)
10208 : {
10209 0 : dimbuf2[k] = 0;
10210 : }
10211 :
10212 : /*
10213 : * Parse the dimensions and separate first for this
10214 : * entry
10215 : */
10216 0 : memcpy(dimbuf2, dimptr[j], dimlen[j]);
10217 0 : dum = EHparsestr(dimbuf2, ',', ptr2, slen2);
10218 0 : dimbuf2[slen2[0]] = 0;
10219 :
10220 :
10221 : /*
10222 : * If 2nd & 3rd dimension values and names (1st and
10223 : * 2nd for rank=2 array), swath ID, and numbertype
10224 : * are equal, then these fields can be combined.
10225 : */
10226 0 : if (match[1] == SWXSDcomb[5 * j + 1] &&
10227 0 : match[2] == SWXSDcomb[5 * j + 2] &&
10228 0 : match[3] == SWXSDcomb[5 * j + 3] &&
10229 0 : match[4] == SWXSDcomb[5 * j + 4] &&
10230 0 : strcmp(dimbuf1 + slen1[0] + 1,
10231 0 : dimbuf2 + slen2[0] + 1) == 0)
10232 : {
10233 : /* Add to combined dimension size */
10234 : /* ------------------------------ */
10235 0 : match[0] += SWXSDcomb[5 * j];
10236 :
10237 : /* Concatanate name */
10238 : /* ---------------- */
10239 0 : strcat(nambuf, ",");
10240 0 : memcpy(nambuf + strlen(nambuf),
10241 0 : nameptr[j], namelen[j]);
10242 :
10243 : /*
10244 : * Increment number of merged fields, store
10245 : * individual dims and dim offsets
10246 : */
10247 0 : cmbfldcnt++;
10248 0 : indvdims[cmbfldcnt] = abs(SWXSDcomb[5 * j]);
10249 0 : offset[cmbfldcnt] = offset[cmbfldcnt - 1] +
10250 0 : indvdims[cmbfldcnt - 1];
10251 :
10252 : /* Delete this field from combination list */
10253 : /* --------------------------------------- */
10254 0 : SWXSDcomb[5 * j] = 0;
10255 : }
10256 : }
10257 : }
10258 :
10259 :
10260 : /* Create SDS */
10261 : /* ---------- */
10262 :
10263 : /* Parse names string */
10264 : /* ------------------ */
10265 0 : nflds0 = EHparsestr(nambuf, ',', nameptr0, namelen0);
10266 :
10267 0 : if (abs(match[0]) == 1)
10268 : {
10269 : /* Two Dimensional Array (no merging has occured) */
10270 : /* ---------------------------------------------- */
10271 0 : dims[0] = abs(match[1]);
10272 0 : dims[1] = abs(match[2]);
10273 :
10274 : /* Create SDS */
10275 : /* ---------- */
10276 0 : rank = 2;
10277 0 : sdid = SDcreate(sdInterfaceID, nambuf,
10278 0 : SWXSDcomb[5 * i + 4], 2, dims);
10279 : }
10280 : else
10281 : {
10282 : /* Three Dimensional Array */
10283 : /* ----------------------- */
10284 0 : dims[0] = abs(match[0]);
10285 0 : dims[1] = abs(match[1]);
10286 0 : dims[2] = abs(match[2]);
10287 :
10288 0 : rank = 3;
10289 :
10290 : /*
10291 : * If merged fields then form string consisting of
10292 : * "MRGFLD_" + 1st field in merge + ":" + entire merged
10293 : * field list and store in utlbuf. Then write to
10294 : * MergedField metadata section
10295 : */
10296 0 : if (cmbfldcnt > 0)
10297 : {
10298 0 : strcpy(utlbuf, "MRGFLD_");
10299 0 : memcpy(utlbuf + 7, nameptr0[0], namelen0[0]);
10300 0 : utlbuf[7 + namelen0[0]] = 0;
10301 0 : strcat(utlbuf, ":");
10302 0 : strcat(utlbuf, nambuf);
10303 :
10304 0 : status = EHinsertmeta(sdInterfaceID, swathname, "s",
10305 : 6L, utlbuf, NULL);
10306 : }
10307 : else
10308 : {
10309 : /*
10310 : * If not merged field then store field name in
10311 : * utlbuf
10312 : */
10313 0 : strcpy(utlbuf, nambuf);
10314 : }
10315 :
10316 : /* Create SDS */
10317 : /* ---------- */
10318 0 : sdid = SDcreate(sdInterfaceID, utlbuf,
10319 0 : SWXSDcomb[5 * i + 4], 3, dims);
10320 :
10321 :
10322 : /*
10323 : * If merged field then store dimensions and offsets as
10324 : * SD attributes
10325 : */
10326 0 : if (cmbfldcnt > 0)
10327 : {
10328 0 : SDsetattr(sdid, "Field Dims", DFNT_INT32,
10329 : cmbfldcnt + 1, (VOIDP) indvdims);
10330 :
10331 0 : SDsetattr(sdid, "Field Offsets", DFNT_INT32,
10332 : cmbfldcnt + 1, (VOIDP) offset);
10333 : }
10334 : }
10335 :
10336 :
10337 :
10338 : /* Register Dimensions in SDS */
10339 : /* -------------------------- */
10340 0 : for (k = 0; k < rank; k++)
10341 : {
10342 0 : if (rank == 2)
10343 : {
10344 : /* Copy k+1th dimension into dimbuf2 if rank = 2 */
10345 : /* --------------------------------------------- */
10346 0 : memcpy(dimbuf2, ptr1[k + 1], slen1[k + 1]);
10347 0 : dimbuf2[slen1[k + 1]] = 0;
10348 : }
10349 : else
10350 : {
10351 : /* Copy kth dimension into dimbuf2 if rank > 2 */
10352 : /* ------------------------------------------- */
10353 0 : memcpy(dimbuf2, ptr1[k], slen1[k]);
10354 0 : dimbuf2[slen1[k]] = 0;
10355 : }
10356 :
10357 : /*
10358 : * If first dimension and merged field then generate
10359 : * dimension name consisting of "MRGDIM:" + swathname +
10360 : * dimension size
10361 : */
10362 0 : if (k == 0 && cmbfldcnt > 0)
10363 : {
10364 0 : sprintf(dimbuf2, "%s%s_%ld", "MRGDIM:",
10365 : swathname, (long)dims[0]);
10366 : }
10367 : else
10368 : {
10369 : /* Otherwise concatanate swathname to dim name */
10370 : /* ------------------------------------------- */
10371 0 : strcat(dimbuf2, ":");
10372 0 : strcat(dimbuf2, swathname);
10373 : }
10374 :
10375 : /* Register dimensions using "SDsetdimname" */
10376 : /* ---------------------------------------- */
10377 0 : SDsetdimname(SDgetdimid(sdid, k), (char *) dimbuf2);
10378 : }
10379 :
10380 :
10381 :
10382 : /* Write Fill Value */
10383 : /* ---------------- */
10384 0 : for (k = 0; k < nflds0; k++)
10385 : {
10386 : /* Check if fill values has been set */
10387 : /* --------------------------------- */
10388 0 : memcpy(utlbuf, nameptr0[k], namelen0[k]);
10389 0 : utlbuf[namelen[k]] = 0;
10390 0 : statusFill = SWgetfillvalue(swathID, utlbuf, fillval);
10391 :
10392 0 : if (statusFill == 0)
10393 : {
10394 : /*
10395 : * If merged field then fill value must be stored
10396 : * manually using EHfillfld
10397 : */
10398 0 : if (cmbfldcnt > 0)
10399 : {
10400 0 : dims[0] = indvdims[k];
10401 0 : truerank = (dims[0] == 1) ? 2 : 3;
10402 0 : EHfillfld(sdid, rank, truerank,
10403 0 : DFKNTsize(match[4]), offset[k],
10404 : dims, fillval);
10405 : }
10406 : /*
10407 : * If single field then just use the HDF set fill
10408 : * function
10409 : */
10410 : else
10411 : {
10412 0 : status = SDsetfillvalue(sdid, fillval);
10413 : }
10414 : }
10415 : }
10416 :
10417 :
10418 : /*
10419 : * Insert SDS within the appropriate Vgroup (geo or data) and
10420 : * "detach" newly-created SDS
10421 : */
10422 0 : vgid = (match[0] < 0)
10423 0 : ? SWXSwath[sID].VIDTable[0]
10424 0 : : SWXSwath[sID].VIDTable[1];
10425 :
10426 0 : Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid));
10427 0 : SDendaccess(sdid);
10428 :
10429 0 : free(nambuf);
10430 0 : free(utlbuf);
10431 : }
10432 : }
10433 :
10434 :
10435 :
10436 : /* "Contract" 1dcomb array */
10437 : /* ----------------------- */
10438 0 : i = 0;
10439 0 : while (SWX1dcomb[3 * i] != 0)
10440 : {
10441 0 : if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable)
10442 : {
10443 0 : memcpy(&SWX1dcomb[3 * i],
10444 : &SWX1dcomb[3 * (i + 1)],
10445 0 : (512 - i - 1) * 3 * 4);
10446 : }
10447 : else
10448 0 : i++;
10449 : }
10450 :
10451 :
10452 : /* "Contract" SDcomb array */
10453 : /* ----------------------- */
10454 0 : for (i = 0; i < nflds; i++)
10455 : {
10456 0 : if (SWXSDcomb[5 * i + 3] == SWXSwath[sID].IDTable)
10457 : {
10458 0 : if (i == (nflds - 1))
10459 : {
10460 0 : SWXSDcomb[5 * i] = 0;
10461 0 : *(nameptr[i] - (nflds != 1)) = 0;
10462 0 : *(dimptr[i] - (nflds != 1)) = 0;
10463 : }
10464 : else
10465 : {
10466 0 : memmove(&SWXSDcomb[5 * i],
10467 : &SWXSDcomb[5 * (i + 1)],
10468 0 : (512 - i - 1) * 5 * 4);
10469 :
10470 0 : memmove(nameptr[i],
10471 0 : nameptr[i + 1],
10472 0 : nameptr[0] + 2048 - nameptr[i + 1] - 1);
10473 :
10474 0 : memmove(dimptr[i],
10475 0 : dimptr[i + 1],
10476 0 : dimptr[0] + 2048 * 2 - dimptr[i + 1] - 1);
10477 : }
10478 :
10479 0 : i--;
10480 0 : nflds = EHparsestr(SWXSDname, ',', nameptr, namelen);
10481 0 : nflds = EHparsestr(SWXSDdims, ';', dimptr, dimlen);
10482 : }
10483 : }
10484 :
10485 :
10486 : /* Replace trailing delimitors on SWXSDname & SWXSDdims */
10487 : /* ---------------------------------------------------- */
10488 0 : if (nflds != 0)
10489 : {
10490 0 : strcat(SWXSDname, ",");
10491 0 : strcat(SWXSDdims, ";");
10492 : }
10493 :
10494 :
10495 :
10496 : /* Free up a bunch of dynamically allocated arrays */
10497 : /* ----------------------------------------------- */
10498 0 : free(nameptr);
10499 0 : free(namelen);
10500 0 : free(nameptr0);
10501 0 : free(namelen0);
10502 0 : free(dimptr);
10503 0 : free(dimlen);
10504 0 : free(offset);
10505 0 : free(indvdims);
10506 :
10507 :
10508 :
10509 :
10510 : /* "Detach" from previously attached SDSs */
10511 : /* -------------------------------------- */
10512 0 : for (k = 0; k < SWXSwath[sID].nSDS; k++)
10513 : {
10514 0 : SDendaccess(SWXSwath[sID].sdsID[k]);
10515 : }
10516 0 : free(SWXSwath[sID].sdsID);
10517 0 : SWXSwath[sID].sdsID = 0;
10518 0 : SWXSwath[sID].nSDS = 0;
10519 :
10520 :
10521 : /* Detach Swath Vgroups */
10522 : /* -------------------- */
10523 0 : Vdetach(SWXSwath[sID].VIDTable[0]);
10524 0 : Vdetach(SWXSwath[sID].VIDTable[1]);
10525 0 : Vdetach(SWXSwath[sID].VIDTable[2]);
10526 0 : Vdetach(SWXSwath[sID].IDTable);
10527 :
10528 :
10529 : /* Delete entries from External Arrays */
10530 : /* ----------------------------------- */
10531 0 : SWXSwath[sID].active = 0;
10532 0 : SWXSwath[sID].VIDTable[0] = 0;
10533 0 : SWXSwath[sID].VIDTable[1] = 0;
10534 0 : SWXSwath[sID].VIDTable[2] = 0;
10535 0 : SWXSwath[sID].IDTable = 0;
10536 0 : SWXSwath[sID].fid = 0;
10537 :
10538 :
10539 : /* Free Region Pointers */
10540 : /* -------------------- */
10541 0 : for (k = 0; k < NSWATHREGN; k++)
10542 : {
10543 0 : if (SWXRegion[k] != 0 &&
10544 0 : SWXRegion[k]->swathID == swathID)
10545 : {
10546 0 : for (i = 0; i < 8; i++)
10547 : {
10548 0 : if (SWXRegion[k]->DimNamePtr[i] != 0)
10549 : {
10550 0 : free(SWXRegion[k]->DimNamePtr[i]);
10551 : }
10552 : }
10553 :
10554 0 : free(SWXRegion[k]);
10555 0 : SWXRegion[k] = 0;
10556 : }
10557 : }
10558 :
10559 : }
10560 0 : return (status);
10561 : }
10562 :
10563 : /*----------------------------------------------------------------------------|
10564 : | BEGIN_PROLOG |
10565 : | |
10566 : | FUNCTION: SWclose |
10567 : | |
10568 : | DESCRIPTION: Closes HDF-EOS file |
10569 : | |
10570 : | |
10571 : | Return Value Type Units Description |
10572 : | ============ ====== ========= ===================================== |
10573 : | status intn return status (0) SUCCEED, (-1) FAIL |
10574 : | |
10575 : | INPUTS: |
10576 : | fid int32 File ID |
10577 : | |
10578 : | OUTPUTS: |
10579 : | None |
10580 : | |
10581 : | NOTES: |
10582 : | |
10583 : | |
10584 : | Date Programmer Description |
10585 : | ====== ============ ================================================= |
10586 : | Jun 96 Joel Gales Original Programmer |
10587 : | |
10588 : | END_PROLOG |
10589 : -----------------------------------------------------------------------------*/
10590 : intn
10591 0 : SWclose(int32 fid)
10592 :
10593 : {
10594 0 : intn status = 0; /* routine return status variable */
10595 :
10596 : /* Call EHclose to perform file close */
10597 : /* ---------------------------------- */
10598 0 : status = EHclose(fid);
10599 :
10600 0 : return (status);
10601 : }
10602 :
10603 : /*----------------------------------------------------------------------------|
10604 : | BEGIN_PROLOG |
10605 : | |
10606 : | FUNCTION: SWupdatescene |
10607 : | |
10608 : | DESCRIPTION: Updates the StartRegion and StopRegion values |
10609 : | for a specified region. |
10610 : | |
10611 : | |
10612 : | Return Value Type Units Description |
10613 : | ============ ====== ========= ===================================== |
10614 : | status intn return status (0) SUCCEED, (-1) FAIL |
10615 : | |
10616 : | INPUTS: |
10617 : | swathID int32 Swath structure ID |
10618 : | regionID int32 Region ID |
10619 : | |
10620 : | NOTES: |
10621 : | |
10622 : | Date Programmer Description |
10623 : | ====== ============ ================================================= |
10624 : | Nov 98 Xinmin Hua Original developing |
10625 : | Aug 99 Abe Taaheri Added code to exclude regions that have the same |
10626 : | start and stop. |
10627 : | |
10628 : | END_PROLOG |
10629 : -----------------------------------------------------------------------------*/
10630 : intn
10631 0 : SWupdatescene(int32 swathID, int32 regionID)
10632 : {
10633 : intn k; /* Loop index */
10634 : int32 status; /* routine return status variable */
10635 :
10636 : int32 fid; /* HDF-EOS file ID */
10637 : int32 sdInterfaceID; /* HDF SDS interface ID */
10638 : int32 swVgrpID; /* Swath Vgroup ID */
10639 :
10640 : int32 startReg; /* Indexed start region */
10641 : int32 stopReg; /* Indexed stop region */
10642 : int32 index[MAXNREGIONS]; /* to store indicies when stop and
10643 : start are different */
10644 :
10645 : int32 ind; /* index */
10646 : int32 tempnRegions; /* temp number of regions */
10647 :
10648 : /* Check for valid swath ID */
10649 : /* ------------------------ */
10650 0 : status = SWchkswid(swathID, "SWupdatescene", &fid, &sdInterfaceID,
10651 : &swVgrpID);
10652 :
10653 :
10654 : /* Check for valid region ID */
10655 : /* ------------------------- */
10656 0 : if (status == 0)
10657 : {
10658 0 : if (regionID < 0 || regionID >= NSWATHREGN)
10659 : {
10660 0 : status = -1;
10661 0 : HEpush(DFE_RANGE, "SWupdatescene", __FILE__, __LINE__);
10662 0 : HEreport("Invalid Region id: %d.\n", regionID);
10663 : }
10664 : }
10665 :
10666 : /* Check for active region ID */
10667 : /* -------------------------- */
10668 0 : if (status == 0)
10669 : {
10670 0 : if (SWXRegion[regionID] == 0)
10671 : {
10672 0 : status = -1;
10673 0 : HEpush(DFE_GENAPP, "SWupdatescene", __FILE__, __LINE__);
10674 0 : HEreport("Inactive Region ID: %d.\n", regionID);
10675 : }
10676 : }
10677 :
10678 0 : if (status == 0)
10679 : {
10680 0 : tempnRegions = SWXRegion[regionID]->nRegions;
10681 0 : ind =0;
10682 :
10683 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
10684 : {
10685 0 : startReg = SWXRegion[regionID]->StartRegion[k];
10686 0 : stopReg = SWXRegion[regionID]->StopRegion[k];
10687 0 : if(startReg == stopReg)
10688 : {
10689 : /* reduce number of regions by 1, if tempnRegions is 0 issue
10690 : error and break from loop*/
10691 0 : tempnRegions -= 1;
10692 :
10693 0 : if(tempnRegions == 0)
10694 : {
10695 : /* first free allocated memory for SWXRegion[regionID]
10696 : in the function SWdefboxregion and make regionID
10697 : inactive */
10698 0 : free(SWXRegion[regionID]);
10699 0 : SWXRegion[regionID] = 0;
10700 0 : status = -1;
10701 0 : HEpush(DFE_GENAPP, "SWupdatescene", __FILE__, __LINE__);
10702 0 : HEreport("Inactive Region ID: %d.\n", regionID);
10703 0 : break;
10704 : }
10705 : }
10706 : else
10707 : {
10708 : /* store index number of regions that have different start and
10709 : stop */
10710 0 : index[ind] = k;
10711 0 : ind += 1;
10712 : }
10713 : }
10714 0 : if (status != 0)
10715 : {
10716 0 : return (status);
10717 : }
10718 : else
10719 : {
10720 0 : SWXRegion[regionID]->nRegions = tempnRegions;
10721 : }
10722 : /* keep starts and stops that are different in the structure */
10723 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
10724 : {
10725 0 : SWXRegion[regionID]->StartRegion[k] =
10726 0 : SWXRegion[regionID]->StartRegion[index[k]];
10727 0 : SWXRegion[regionID]->StopRegion[k] =
10728 0 : SWXRegion[regionID]->StopRegion[index[k]];
10729 : }
10730 :
10731 : }
10732 :
10733 :
10734 0 : if (status == 0)
10735 : {
10736 :
10737 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
10738 : {
10739 :
10740 0 : startReg = SWXRegion[regionID]->StartRegion[k];
10741 0 : stopReg = SWXRegion[regionID]->StopRegion[k];
10742 :
10743 0 : if(startReg % 2 == 1) {
10744 :
10745 0 : SWXRegion[regionID]->StartRegion[k] = ++startReg;
10746 :
10747 : }
10748 0 : if(stopReg % 2 == 0) {
10749 :
10750 0 : SWXRegion[regionID]->StopRegion[k] = --stopReg;
10751 :
10752 : }
10753 :
10754 : }
10755 :
10756 : }
10757 :
10758 0 : return(status);
10759 :
10760 : }
10761 :
10762 : /*----------------------------------------------------------------------------|
10763 : | BEGIN_PROLOG |
10764 : | |
10765 : | FUNCTION: SWupdateidxmap |
10766 : | |
10767 : | DESCRIPTION: Updates the map index for a specified region. |
10768 : | |
10769 : | |
10770 : | Return Value Type Units Description |
10771 : | ============ ====== ========= ===================================== |
10772 : | nout int32 return Number of elements in output |
10773 : | index array if SUCCEED, (-1) FAIL |
10774 : | |
10775 : | INPUTS: |
10776 : | swathID int32 Swath structure ID |
10777 : | regionID int32 Region ID |
10778 : | indexin int32 array of index values |
10779 : | |
10780 : | OUTPUTS: |
10781 : | indexout int32 array of index values |
10782 : | indicies int32 array of start and stop in region |
10783 : | |
10784 : | NOTES: |
10785 : | |
10786 : | |
10787 : | Date Programmer Description |
10788 : | ====== ============ ================================================= |
10789 : | Aug 97 Abe Taaheri Original Programmer |
10790 : | AUG 97 Abe Taaheri Add support for index mapping |
10791 : | Sep 99 DaW Add support for Floating Scene Subsetting Landsat 7 |
10792 : | |
10793 : | END_PROLOG |
10794 : -----------------------------------------------------------------------------*/
10795 : int32
10796 0 : SWupdateidxmap(int32 swathID, int32 regionID, int32 indexin[], int32 indexout[], int32 indicies[])
10797 : {
10798 : intn i; /* Loop index */
10799 : intn j; /* Loop index */
10800 : intn k; /* Loop index */
10801 : int32 status; /* routine return status variable */
10802 0 : int32 land_status = 3; /* routine return status variable */
10803 :
10804 : int32 fid; /* HDF-EOS file ID */
10805 : int32 sdInterfaceID; /* HDF SDS interface ID */
10806 : int32 swVgrpID; /* Swath Vgroup ID */
10807 :
10808 0 : int32 numtype = 0; /* Used for L7 float scene sub. */
10809 0 : int32 count = 0; /* Used for L7 float scene sub. */
10810 :
10811 0 : int32 startReg = 0; /* Indexed start region */
10812 0 : int32 stopReg = 0; /* Indexed stop region */
10813 0 : int32 nout=-1; /* Number of elements in output index array */
10814 0 : int32 indexoffset = 0;
10815 0 : uint8 scene_cnt = 0; /* Used for L7 float scene sub. */
10816 0 : uint8 detect_cnt = 0; /* Used to convert scan to scanline */
10817 0 : intn gtflag = 0;
10818 0 : intn ngtflag = 0;
10819 0 : int32 *buffer1 = (int32 *)NULL;
10820 0 : int32 *buffer2 = (int32 *)NULL;
10821 :
10822 : /* Check for valid swath ID */
10823 : /* ------------------------ */
10824 0 : status = SWchkswid(swathID, "SWupdateidxmap", &fid, &sdInterfaceID,
10825 : &swVgrpID);
10826 :
10827 :
10828 : /* Check for valid region ID */
10829 : /* ------------------------- */
10830 0 : if (status == 0)
10831 : {
10832 0 : if (regionID < 0 || regionID >= NSWATHREGN)
10833 : {
10834 0 : status = -1;
10835 0 : HEpush(DFE_RANGE, "SWupdateidxmap", __FILE__, __LINE__);
10836 0 : HEreport("Invalid Region id: %d.\n", regionID);
10837 : }
10838 : }
10839 :
10840 : /* Check for active region ID */
10841 : /* -------------------------- */
10842 0 : if (status == 0)
10843 : {
10844 0 : if (SWXRegion[regionID] == 0)
10845 : {
10846 0 : status = -1;
10847 0 : HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
10848 0 : HEreport("Inactive Region ID: %d.\n", regionID);
10849 : }
10850 : }
10851 :
10852 0 : if (status == 0)
10853 : {
10854 : /* Loop through all regions */
10855 : /* ------------------------ */
10856 0 : for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
10857 : {
10858 :
10859 : /* fix overlap index mapping problem for Landsat 7 */
10860 :
10861 0 : startReg = SWXRegion[regionID]->StartRegion[k];
10862 0 : stopReg = SWXRegion[regionID]->StopRegion[k];
10863 :
10864 :
10865 0 : if(SWXRegion[regionID]->scanflag == 1)
10866 : {
10867 0 : indicies[0] = -1;
10868 0 : indicies[1] = -1;
10869 0 : j = 0;
10870 : /* This code checks for the attribute detector_count */
10871 : /* which is found in Landsat 7 files. It is used */
10872 : /* for some of the loops. */
10873 : /* ================================================= */
10874 0 : land_status = SWattrinfo(swathID, "scene_count", &numtype, &count);
10875 0 : if (land_status == 0)
10876 : {
10877 0 : land_status = SWreadattr(swathID, "scene_count", &scene_cnt);
10878 0 : land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
10879 : }
10880 :
10881 :
10882 : /* calculate the offsets first */
10883 0 : buffer1 = (int32 *)calloc(74, sizeof(int32));
10884 0 : buffer2 = (int32 *)calloc(74, sizeof(int32));
10885 :
10886 0 : status = SWidxmapinfo(swathID,"GeoTrack",
10887 : "ScanLineTrack", (int32*)buffer1);
10888 0 : status = SWidxmapinfo(swathID,"UpperTrack",
10889 : "ScanLineTrack", (int32*)buffer2);
10890 :
10891 0 : indexoffset = buffer2[0] - buffer1[0];
10892 0 : free(buffer1);
10893 0 : free(buffer2);
10894 :
10895 0 : if(SWXRegion[regionID]->band8flag == -1)
10896 : {
10897 0 : for(i=0; i<scene_cnt;i++)
10898 : {
10899 0 : if(indexin[j] <= startReg && indexin[j+1] >= startReg)
10900 0 : if(indicies[0] == -1)
10901 0 : indicies[0] = j;
10902 0 : if(indexin[j] <= stopReg && indexin[j+1] >= stopReg)
10903 0 : indicies[1] = j + 1;
10904 0 : j = j + 2;
10905 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
10906 0 : i = scene_cnt;
10907 : }
10908 0 : if(indicies[0] == -1)
10909 : {
10910 0 : if(startReg <= indexin[0])
10911 0 : indicies[0] = 0;
10912 : }
10913 0 : if(indicies[0] == -1)
10914 : {
10915 0 : j = 0;
10916 0 : for(i=0; i<scene_cnt; i++)
10917 : {
10918 0 : if(indexin[j] <= startReg && indexin[j+1] >= startReg)
10919 0 : if(indicies[0] == -1)
10920 0 : indicies[0] = j;
10921 0 : j = j + 1;
10922 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
10923 0 : i = scene_cnt;
10924 : }
10925 : }
10926 0 : if(indicies[1] == -1)
10927 : {
10928 0 : j = 0;
10929 0 : for(i=0; i<scene_cnt; i++)
10930 : {
10931 0 : if(indexin[j] <= stopReg && indexin[j+1] >= stopReg)
10932 0 : if(indicies[1] == -1)
10933 0 : indicies[1] = j + 1;
10934 0 : j = j + 1;
10935 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
10936 0 : i = scene_cnt;
10937 : }
10938 : }
10939 0 : if(indicies[1] == -1)
10940 0 : if(stopReg > indexin[scene_cnt - 1])
10941 0 : indicies[1] = scene_cnt - 1;
10942 : }
10943 :
10944 : /* This section of code handles exceptions in Landsat 7 */
10945 : /* data. The Band 8 data - multiple files, data gaps */
10946 : /* ===================================================== */
10947 0 : if(SWXRegion[regionID]->band8flag == 1 ||
10948 0 : SWXRegion[regionID]->band8flag == 2 ||
10949 0 : SWXRegion[regionID]->band8flag == 3)
10950 : {
10951 0 : j = 0;
10952 0 : for(i=0; i<scene_cnt; i++)
10953 : {
10954 0 : j = j + 2;
10955 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
10956 : {
10957 0 : if(indexin[j] == 0)
10958 0 : gtflag = 1;
10959 : else
10960 0 : ngtflag = 1;
10961 0 : i = scene_cnt;
10962 : }
10963 : }
10964 0 : j = 0;
10965 0 : if(gtflag == 1)
10966 : {
10967 0 : for(i=0; i<scene_cnt; i++)
10968 : {
10969 0 : if( startReg >= (indexin[j] + indexoffset - detect_cnt) &&
10970 0 : startReg <= (indexin[j+1] + indexoffset - detect_cnt) )
10971 0 : if(indicies[0] == -1)
10972 0 : indicies[0] = j;
10973 0 : if( stopReg >= (indexin[j] + indexoffset - detect_cnt ) &&
10974 0 : stopReg <= (indexin[j+1] + indexoffset - detect_cnt ) )
10975 0 : indicies[1] = j + 1;
10976 0 : j = j + 2;
10977 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
10978 0 : i = scene_cnt;
10979 : }
10980 0 : if(SWXRegion[regionID]->band8flag == 1)
10981 : {
10982 0 : if(indicies[1] == -1)
10983 0 : if(stopReg > (indexin[j - 1] + indexoffset - detect_cnt))
10984 0 : indicies[1] = j - 1;
10985 : }
10986 0 : if(SWXRegion[regionID]->band8flag == 2 ||
10987 0 : SWXRegion[regionID]->band8flag == 3)
10988 : {
10989 :
10990 0 : if(startReg >= (indexin[j - 1] + indexoffset - detect_cnt))
10991 : {
10992 0 : indicies[0] = -1;
10993 0 : indicies[1] = -1;
10994 : /* status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
10995 : buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
10996 : status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
10997 : indexoffset = buffer[0] * detect_cnt;
10998 : free(buffer);
10999 : startReg = startReg - (indexoffset - detect_cnt);
11000 : stopReg = stopReg - (indexoffset - 1); */
11001 0 : j = 0;
11002 0 : for(i=0; i<scene_cnt; i++)
11003 : {
11004 0 : if( startReg >= (indexin[j] + indexoffset - detect_cnt) &&
11005 0 : startReg <= (indexin[j+1] + indexoffset - detect_cnt) )
11006 0 : if(indicies[0] == -1)
11007 0 : indicies[0] = j;
11008 0 : if( stopReg >= (indexin[j] + indexoffset - detect_cnt ) &&
11009 0 : stopReg <= (indexin[j+1] + indexoffset - detect_cnt ) )
11010 0 : indicies[1] = j + 1;
11011 0 : j = j + 2;
11012 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11013 0 : i = scene_cnt;
11014 : }
11015 : }
11016 :
11017 0 : if(indicies[0] == -1)
11018 : {
11019 0 : j = 0;
11020 0 : for(i=0; i<scene_cnt; i++)
11021 : {
11022 0 : if( startReg >= (indexin[j] + indexoffset - detect_cnt) &&
11023 0 : startReg <= (indexin[j+1] + indexoffset - detect_cnt) )
11024 0 : if(indicies[0] == -1)
11025 0 : indicies[0] = j;
11026 :
11027 0 : j = j + 2;
11028 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11029 0 : i = scene_cnt;
11030 : }
11031 : }
11032 0 : if(indicies[1] == -1)
11033 0 : if(stopReg > (indexin[j - 1] + indexoffset - detect_cnt) )
11034 0 : indicies[1] = j - 1;
11035 : }
11036 0 : if(indicies[1] == -1)
11037 : {
11038 0 : j = 0;
11039 0 : for(i=0; i<scene_cnt; i++)
11040 : {
11041 0 : if( stopReg >= (indexin[j] + indexoffset - detect_cnt ) &&
11042 0 : stopReg <= (indexin[j+1] + indexoffset - detect_cnt ) )
11043 0 : indicies[1] = j;
11044 0 : j = j + 2;
11045 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11046 0 : i = scene_cnt;
11047 : }
11048 : }
11049 : }
11050 :
11051 0 : if(ngtflag == 1)
11052 : {
11053 0 : for(i=0; i<scene_cnt; i++)
11054 : {
11055 0 : if( startReg >= indexin[j] && startReg <= indexin[j+1])
11056 0 : if(indicies[0] == -1)
11057 0 : indicies[0] = j;
11058 0 : if( stopReg >= indexin[j] && stopReg <= indexin[j+1])
11059 0 : indicies[1] = j + 1;
11060 0 : j = j + 2;
11061 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11062 0 : i = scene_cnt;
11063 : }
11064 0 : if(SWXRegion[regionID]->band8flag == 2)
11065 : {
11066 0 : if(startReg >= indexin[j] )
11067 : {
11068 0 : if(indicies[0] == -1)
11069 0 : indicies[0] = j;
11070 0 : if(indicies[1] == -1)
11071 0 : indicies[1] = j;
11072 : }
11073 0 : if(indicies[0] == -1)
11074 0 : if(startReg <= indexin[0])
11075 0 : indicies[0] = 0;
11076 0 : if(indicies[1] == -1)
11077 0 : if(stopReg > indexin[j])
11078 0 : indicies[1] = j;
11079 : }
11080 0 : if(indicies[0] == -1)
11081 : {
11082 0 : j = 0;
11083 0 : for(i=0; i<scene_cnt; i++)
11084 : {
11085 0 : if( startReg >= indexin[j] && startReg <= indexin[j+1])
11086 0 : indicies[0] = j;
11087 0 : j = j + 2;
11088 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11089 0 : i = scene_cnt;
11090 : }
11091 : }
11092 0 : if(indicies[1] == -1)
11093 : {
11094 0 : j = 0;
11095 0 : for(i=0; i<scene_cnt; i++)
11096 : {
11097 0 : if( stopReg >= indexin[j] && stopReg <= indexin[j+1])
11098 0 : indicies[1] = j;
11099 0 : j = j + 2;
11100 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11101 0 : i = scene_cnt;
11102 : }
11103 : }
11104 0 : if(indicies[1] == -1)
11105 : {
11106 0 : if(stopReg > indexin[j])
11107 0 : indicies[1] = j;
11108 : }
11109 : }
11110 0 : if(indicies[0] == -1)
11111 : {
11112 0 : if(startReg <= (indexin[0]+ indexoffset - detect_cnt) )
11113 0 : indicies[0] = 0;
11114 0 : if(indicies[1] == -1)
11115 0 : if(stopReg > (indexin[j] + indexoffset - detect_cnt))
11116 0 : indicies[1] = j;
11117 : }
11118 : }
11119 0 : if (indicies[1] == -1)
11120 : {
11121 0 : if(SWXRegion[regionID]->band8flag == 2 ||
11122 0 : SWXRegion[regionID]->band8flag == 3)
11123 : {
11124 0 : if(stopReg < (indexin[0] + indexoffset - detect_cnt))
11125 : {
11126 : /*status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
11127 : buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
11128 : status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
11129 : indexoffset = buffer[0] * detect_cnt;
11130 : free(buffer);
11131 : startReg = startReg + (indexoffset - detect_cnt);
11132 : stopReg = stopReg + (indexoffset - 1); */
11133 0 : if(stopReg >= (indexin[scene_cnt - 1] + indexoffset - detect_cnt))
11134 : {
11135 0 : indicies[1] = scene_cnt - 1;
11136 : }
11137 : else
11138 : {
11139 0 : j = 0;
11140 0 : for(i=0;i<scene_cnt;i++)
11141 : {
11142 0 : if( stopReg >= (indexin[j] + indexoffset - detect_cnt ) &&
11143 0 : stopReg <= (indexin[j+1] + indexoffset - detect_cnt ) )
11144 0 : indicies[1] = j;
11145 0 : j = j + 2;
11146 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11147 0 : i = scene_cnt;
11148 : }
11149 : }
11150 : }
11151 :
11152 0 : if(startReg > (indexin[j - 1] + indexoffset - detect_cnt ))
11153 : {
11154 0 : indicies[0] = -1;
11155 0 : indicies[1] = -1;
11156 : /*status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
11157 : buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
11158 : status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
11159 : indexoffset = buffer[0] * detect_cnt;
11160 : free(buffer);
11161 : startReg = startReg - (indexoffset - detect_cnt);
11162 : stopReg = stopReg - (indexoffset - 1);*/
11163 0 : j = 0;
11164 0 : for(i=0; i<scene_cnt; i++)
11165 : {
11166 0 : if( startReg >= (indexin[j] + indexoffset - detect_cnt) &&
11167 0 : startReg <= (indexin[j+1] + indexoffset - detect_cnt) )
11168 0 : if(indicies[0] == -1)
11169 0 : indicies[0] = j;
11170 0 : if( stopReg >= (indexin[j] + indexoffset - detect_cnt ) &&
11171 0 : stopReg <= (indexin[j+1] + indexoffset - detect_cnt ) )
11172 0 : indicies[1] = j + 1;
11173 0 : j = j + 2;
11174 0 : if(indexin[j] == 0 || indexin[j+1] == 0)
11175 0 : i = scene_cnt;
11176 : }
11177 0 : if(indicies[0] == -1)
11178 0 : if(startReg < (indexin[0] + indexoffset - detect_cnt))
11179 0 : indicies[0] = 0;
11180 0 : if(indicies[1] == -1)
11181 0 : if(stopReg > (indexin[j - 1] + indexoffset - detect_cnt))
11182 0 : indicies[1] = j - 1;
11183 : }
11184 : }
11185 : }
11186 : } /* end of if for floating scene update */
11187 : else
11188 : {
11189 : /* If start of region is odd then increment */
11190 : /* ---------------------------------------- */
11191 0 : if (startReg % 2 == 1)
11192 : {
11193 0 : startReg++;
11194 : }
11195 :
11196 : /* If end of region is even then decrement */
11197 : /* --------------------------------------- */
11198 0 : if (stopReg % 2 == 0)
11199 : {
11200 0 : stopReg--;
11201 : }
11202 :
11203 0 : indicies[0]=startReg;
11204 0 : indicies[1]=stopReg;
11205 : }
11206 : }
11207 :
11208 0 : if (indexout != NULL)
11209 : {
11210 0 : if(SWXRegion[regionID]->scanflag == 1)
11211 : {
11212 0 : nout = (indicies[1] - indicies[0] + 1);
11213 0 : j = 0;
11214 0 : if (nout == 1)
11215 0 : indexout[0] = indexin[indicies[0]];
11216 0 : for(i=0; i<nout;i++)
11217 : {
11218 0 : indexout[i] = indexin[indicies[0] + i];
11219 : }
11220 : }
11221 : else
11222 : {
11223 : /* get new index values */
11224 : /* ==================== */
11225 0 : for(i = startReg; i <= stopReg ; i++)
11226 : {
11227 0 : indexout[i-startReg] = indexin[i];
11228 : }
11229 0 : nout = (stopReg - startReg) + 1;
11230 : }
11231 : }
11232 : else
11233 : {
11234 0 : nout = indicies[1] - indicies[0] + 1;
11235 : }
11236 : }
11237 :
11238 :
11239 0 : if(status == -1)
11240 : {
11241 0 : return(status);
11242 : }
11243 : else
11244 : {
11245 0 : return(nout);
11246 : }
11247 :
11248 : }
11249 :
11250 :
11251 :
11252 : /*----------------------------------------------------------------------------|
11253 : | BEGIN_PROLOG |
11254 : | |
11255 : | FUNCTION: SWgeomapinfo |
11256 : | |
11257 : | DESCRIPTION: Returns mapping information for dimension |
11258 : | |
11259 : | |
11260 : | Return Value Type Units Description |
11261 : | ============ ====== ========= ===================================== |
11262 : | status intn 2 for indexed mapping, 1 for regular |
11263 : | mapping, 0 if the dimension is not |
11264 : | and (-1) FAIL |
11265 : | |
11266 : | INPUTS: |
11267 : | swathID int32 swath structure id |
11268 : | geodim char geolocation dimension name |
11269 : | |
11270 : | OUTPUTS: |
11271 : | |
11272 : | NONE |
11273 : | |
11274 : | NOTES: |
11275 : | |
11276 : | |
11277 : | Date Programmer Description |
11278 : | ====== ============ ================================================= |
11279 : | Aug 97 Abe Taaheri Original Programmer |
11280 : | Sept 97 DaW Modified return value so errors can be trapped |
11281 : | |
11282 : | END_PROLOG |
11283 : -----------------------------------------------------------------------------*/
11284 : intn
11285 0 : SWgeomapinfo(int32 swathID, char *geodim)
11286 :
11287 : {
11288 : intn status; /* routine return status variable */
11289 :
11290 : int32 fid; /* HDF-EOS file ID */
11291 : int32 sdInterfaceID; /* HDF SDS interface ID */
11292 : int32 swVgrpID; /* Swath root Vgroup ID */
11293 0 : int32 idOffset = SWIDOFFSET; /* Swath ID offset */
11294 :
11295 : char *metabufr; /* Pointer to structural metadata (SM) */
11296 : char *metabufi; /* Pointer to structural metadata (SM) */
11297 : char *metaptrsr[2];/* Pointers to begin and end of SM section */
11298 : char *metaptrsi[2];/* Pointers to begin and end of SM section */
11299 : char swathname[80]; /* Swath Name */
11300 : char *utlstrr; /* Utility string */
11301 : char *utlstri; /* Utility string */
11302 :
11303 :
11304 : /* Allocate space for utility string */
11305 : /* --------------------------------- */
11306 0 : utlstrr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
11307 0 : if(utlstrr == NULL)
11308 : {
11309 0 : HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__);
11310 0 : return(-1);
11311 : }
11312 0 : utlstri = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
11313 0 : if(utlstri == NULL)
11314 : {
11315 0 : HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__);
11316 0 : free(utlstrr);
11317 0 : return(-1);
11318 : }
11319 0 : status = -1;
11320 :
11321 : /* Check for valid swath id */
11322 0 : status = SWchkswid(swathID, "SWgeomapinfo", &fid, &sdInterfaceID, &swVgrpID);
11323 0 : if (status == 0)
11324 : {
11325 : /* Get swath name */
11326 0 : Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
11327 :
11328 : /* Get pointers to "DimensionMap" section within SM */
11329 0 : metabufr = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
11330 : "DimensionMap", metaptrsr);
11331 :
11332 0 : if(metabufr == NULL)
11333 : {
11334 0 : free(utlstrr);
11335 0 : free(utlstri);
11336 0 : return(-1);
11337 : }
11338 : /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
11339 0 : sprintf(utlstrr, "%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
11340 : "\"\n\t\t\t\tDataDimension=");
11341 0 : metaptrsr[0] = strstr(metaptrsr[0], utlstrr);
11342 :
11343 : /* Get pointers to "IndexDimensionMap" section within SM */
11344 0 : metabufi = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
11345 : "IndexDimensionMap", metaptrsi);
11346 0 : if(metabufi == NULL)
11347 : {
11348 0 : free(utlstrr);
11349 0 : free(utlstri);
11350 0 : return(-1);
11351 : }
11352 : /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
11353 0 : sprintf(utlstri, "%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
11354 : "\"\n\t\t\t\tDataDimension=");
11355 0 : metaptrsi[0] = strstr(metaptrsi[0], utlstri);
11356 :
11357 : /*
11358 : ** If regular mapping found add 1 to status
11359 : ** If indexed mapping found add 2
11360 : */
11361 0 : if (metaptrsr[0] < metaptrsr[1] && metaptrsr[0] != NULL)
11362 : {
11363 0 : status = status + 1;
11364 : }
11365 :
11366 0 : if (metaptrsi[0] < metaptrsi[1] && metaptrsi[0] != NULL)
11367 : {
11368 0 : status = status + 2;
11369 : }
11370 :
11371 0 : free(metabufr);
11372 0 : free(metabufi);
11373 : }
11374 :
11375 0 : free(utlstrr);
11376 0 : free(utlstri);
11377 :
11378 0 : return (status);
11379 : }
11380 :
11381 : /*----------------------------------------------------------------------------|
11382 : | BEGIN_PROLOG |
11383 : | |
11384 : | FUNCTION: SWsdid |
11385 : | |
11386 : | DESCRIPTION: Returns SD element ID for swath field |
11387 : | |
11388 : | |
11389 : | Return Value Type Units Description |
11390 : | ============ ====== ========= ===================================== |
11391 : | status intn return status (0) SUCCEED, (-1) FAIL |
11392 : | |
11393 : | INPUTS: |
11394 : | swathID int32 swath structure ID |
11395 : | fieldname const char field name |
11396 : | |
11397 : | |
11398 : | OUTPUTS: |
11399 : | sdid int32 SD element ID |
11400 : | |
11401 : | NOTES: |
11402 : | |
11403 : | |
11404 : | Date Programmer Description |
11405 : | ====== ============ ================================================= |
11406 : | Oct 07 Andrey Kiselev Original Programmer |
11407 : | |
11408 : | END_PROLOG |
11409 : -----------------------------------------------------------------------------*/
11410 : intn
11411 0 : SWsdid(int32 swathID, const char *fieldname, int32 *sdid)
11412 : {
11413 : intn status; /* routine return status variable */
11414 : int32 fid; /* HDF-EOS file ID */
11415 : int32 sdInterfaceID; /* HDF SDS interface ID */
11416 : int32 dum; /* Dummy variable */
11417 : int32 dims[H4_MAX_VAR_DIMS]; /* Field/SDS dimensions */
11418 :
11419 0 : status = SWchkswid(swathID, "SWsdid", &fid, &sdInterfaceID, &dum);
11420 0 : if (status != -1)
11421 : {
11422 0 : status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
11423 : sdid, &dum, &dum, &dum, dims, &dum);
11424 : }
11425 :
11426 0 : return (status);
11427 : }
11428 :
|