From 63821c3ceafd34656b695b19a6ff243aa79056d4 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 25 Nov 2024 02:54:56 +0100 Subject: [PATCH] frmts/hdf4/hdf-eos: remove code unused by GDAL --- frmts/hdf4/hdf-eos/EHapi.c | 1582 +---- frmts/hdf4/hdf-eos/GDapi.c | 10900 +++++++------------------------ frmts/hdf4/hdf-eos/HdfEosDef.h | 150 - frmts/hdf4/hdf-eos/SWapi.c | 10428 +++++------------------------ 4 files changed, 3969 insertions(+), 19091 deletions(-) diff --git a/frmts/hdf4/hdf-eos/EHapi.c b/frmts/hdf4/hdf-eos/EHapi.c index 8a6b6af3877d..565cd1a364ec 100644 --- a/frmts/hdf4/hdf-eos/EHapi.c +++ b/frmts/hdf4/hdf-eos/EHapi.c @@ -46,9 +46,6 @@ static int32 *EHXsdTable = NULL; #define MAX_RETRIES 10 /* Function Prototypes */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) -static intn EHmetalist(const char *, char *); -#endif static intn EHreset_maxopenfiles(intn); static intn EHget_maxopenfiles(intn *, intn *); static intn EHget_numfiles(void); @@ -582,53 +579,6 @@ EHidinfo(int32 fid, int32 * HDFfid, int32 * sdInterfaceID) } -#ifdef UNUSED_BY_GDAL -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: EHfilename | -| | -| DESCRIPTION: Returns HDF filename | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| fid int32 HDF-EOS file id | -| | -| OUTPUTS: | -| filename char HDF-EOS file name | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Sep 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -EHfilename(int32 fid, char *filename) -{ - intn status = 0; /* routine return status variable */ - intn dum; /* Dummy variable */ - - char *fname; /* Pointer to filename */ - - /* Get point to filename from Hfidinquire */ - /* -------------------------------------- */ - Hfidinquire(EHXfidTable[fid % EHIDOFFSET], &fname, &dum, &dum); - strcpy(filename, fname); - - return (status); -} -#endif - - - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -1397,134 +1347,12 @@ EHrevflds(const char *dimlist, char *revdimlist) return (status); } - -#ifdef UNUSED_BY_GDAL -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: EHcntOBJECT | -| | -| DESCRIPTION: Determines number of OBJECTs in metadata GROUP | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| count int32 Number of OBJECTs in GROUP | -| | -| INPUTS: | -| metabuf char Begin & end metadata pointer array | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Sep 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -EHcntOBJECT(char *metabuf[]) -{ - int32 count = 0; /* Counter */ - - char *metaptr; /* Beginning of metadata section */ - char *endptr; /* End of metadata section */ - char *tempptr; /* Pointer within metadata section */ - - - /* Get Pointers to beginning and ending of metadata section */ - /* -------------------------------------------------------- */ - metaptr = metabuf[0]; - endptr = metabuf[1]; - - - /* Find number of "END_OBJECT" strings within section */ - /* -------------------------------------------------- */ - tempptr = metaptr; - while (tempptr < endptr && tempptr != NULL) - { - tempptr = strstr(tempptr + 1, "END_OBJECT"); - count++; - } - count--; - - return (count); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: EHcntGROUP | -| | -| DESCRIPTION: Determines number of GROUPs in metadata GROUP | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| count int32 Number of GROUPs in GROUP | -| | -| INPUTS: | -| metabuf char Begin & end metadata pointer array | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Sep 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -EHcntGROUP(char *metabuf[]) -{ - int32 count = 0; /* Counter */ - - char *metaptr; /* Beginning of metadata section */ - char *endptr; /* End of metadata section */ - char *tempptr; /* Pointer within metadata section */ - - - /* Get Pointers to beginning and ending of metadata section */ - /* -------------------------------------------------------- */ - metaptr = metabuf[0]; - endptr = metabuf[1]; - - - /* Find number of "END_GROUP" strings within section */ - /* ------------------------------------------------- */ - tempptr = metaptr; - while (tempptr < endptr && tempptr != NULL) - { - tempptr = strstr(tempptr + 1, "END_GROUP"); - count++; - } - count--; - - return (count); -} -#endif - - - -#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: EHmetalist | +| FUNCTION: EHgetmetavalue | | | -| DESCRIPTION: Converts string list to metadata list | +| DESCRIPTION: Returns metadata value | | | | | | Return Value Type Units Description | @@ -1532,10 +1360,12 @@ EHcntGROUP(char *metabuf[]) | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| instring char Input string list | +| metaptrs char Begin and end of metadata section | +| parameter char parameter to access | | | | OUTPUTS: | -| outstring char Output metadata string | +| metaptr char Ptr to (updated) beginning of metadata | +| retstr char return string containing value | | | | NOTES: | | | @@ -1543,126 +1373,96 @@ EHcntGROUP(char *metabuf[]) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | +| Jan 97 Joel Gales Check string pointer against end of meta section | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -EHmetalist(const char *instring, char *outstring) +EHgetmetavalue(char *metaptrs[], const char *parameter, char *retstr) { - intn i; /* Loop index */ intn status = 0; /* routine return status variable */ - int32 nentries; /* Number of entries in search string */ - int32 listlen = 1;/* String list length */ - int32 *slen; /* Pointer to string length array */ - - char **ptr; /* Pointer to string pointer array */ - - - /* Count number of entries in search string list */ - /* --------------------------------------------- */ - nentries = EHparsestr(instring, ',', NULL, NULL); + int32 slen; /* String length */ + char *newline; /* Position of new line character */ + char *sptr; /* string pointer within metadata */ - /* Allocate string pointer and length arrays */ + /* Get string length of parameter string + 1 */ /* ----------------------------------------- */ - ptr = (char **) calloc(nentries, sizeof(char *)); - if(ptr == NULL) - { - HEpush(DFE_NOSPACE,"EHmetalist", __FILE__, __LINE__); - return(-1); - } - slen = (int32 *) calloc(nentries, sizeof(int32)); - if(slen == NULL) - { - HEpush(DFE_NOSPACE,"EHmetalist", __FILE__, __LINE__); - free(ptr); - return(-1); - } + slen = (int)strlen(parameter) + 1; - /* Parse input string */ - /* ------------------ */ - nentries = EHparsestr(instring, ',', ptr, slen); + /* Build search string (parameter string + "=") */ + /* -------------------------------------------- */ + strcpy(retstr, parameter); + strcat(retstr, "="); - /* Start output string with leading "(" */ - /* ------------------------------------ */ - strcpy(outstring, "("); + /* Search for string within metadata (beginning at metaptrs[0]) */ + /* ------------------------------------------------------------ */ + sptr = strstr(metaptrs[0], retstr); - /* Loop through all entries */ - /* ------------------------ */ - for (i = 0; i < nentries; i++) + /* If string found within desired section ... */ + /* ------------------------------------------ */ + if (sptr != NULL && sptr < metaptrs[1]) { - /* Add double quote (") to output string */ - /* ------------------------------------- */ - strcat(outstring, "\""); - listlen++; - - /* Add input string entry to output string */ - /* --------------------------------------- */ - memcpy(outstring + listlen, ptr[i], slen[i]); - listlen += slen[i]; - outstring[listlen] = 0; - - - /* Add closing double quote (") to output string */ - /* --------------------------------------------- */ - strcat(outstring, "\""); - listlen++; - outstring[listlen] = 0; - + /* Store position of string within metadata */ + /* ---------------------------------------- */ + metaptrs[0] = sptr; - /* Add comma delimiter to output string */ - /* ------------------------------------ */ - if (i != (nentries - 1)) + /* Find newline "\n" character */ + /* --------------------------- */ + newline = strchr(metaptrs[0], '\n'); + if (newline == NULL) { - strcat(outstring, ","); - listlen++; + retstr[0] = 0; + return -1; } - /* Place null terminator in output string */ - /* -------------------------------------- */ - outstring[listlen] = 0; - } - - /* End output string with trailing ")" */ - /* ----------------------------------- */ - strcat(outstring, ")"); + /* Copy from "=" to "\n" (exclusive) into return string */ + /* ---------------------------------------------------- */ + memcpy(retstr, metaptrs[0] + slen, newline - metaptrs[0] - slen); - free(ptr); - free(slen); + /* Terminate return string with null */ + /* --------------------------------- */ + retstr[newline - metaptrs[0] - slen] = 0; + } else + { + /* + * if parameter string not found within section, null return string + * and set status to -1. + */ + retstr[0] = 0; + status = -1; + } return (status); } -#endif -#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: EHinsertmeta | +| FUNCTION: EHmetagroup | | | -| DESCRIPTION: Writes metadata | +| DESCRIPTION: Returns pointers to beginning and end of metadata group | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| metabuf char Pointer to HDF-EOS object in metadata | | | | INPUTS: | | sdInterfaceID int32 SDS interface ID | | structname char HDF-EOS structure name | | structcode char Structure code ("s/g/p") | -| metacode int32 Metadata code type | -| metastr char Metadata input string | -| metadata int32 Metadata utility array | +| groupname char Metadata group name | | | | OUTPUTS: | -| None | +| metaptrs char pointers to begin and end of metadata | | | | NOTES: | | | @@ -1671,61 +1471,36 @@ EHmetalist(const char *instring, char *outstring) | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | | Aug 96 Joel Gales Make metadata ODL compliant | -| Sep 96 Joel Gales Allow new metadata object to be written in | -| old metadata. | -| Dec 96 Joel Gales Fix Point metadata problem | -| Oct 98 David Wynne Change utlstr/utlstr2 to dynamic allocation from | -| static | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode, - int32 metacode, char *metastr, int32 metadata[]) +char * +EHmetagroup(int32 sdInterfaceID, const char *structname, const char *structcode, + const char *groupname, char *metaptrs[]) { intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ int32 attrIndex; /* Structural metadata attribute index */ - int32 slen[8]; /* String length array (for dim map parsing) */ int32 nmeta; /* Number of 32000 byte metadata sections */ int32 metalen; /* Length of structural metadata */ - int32 seglen; /* Length of metadata string to insert */ - int32 count; /* Objects/Groups counter */ - int32 offset; /* Offset insertion position of new metadata - * section within existing metadata */ char *metabuf; /* Pointer (handle) to structural metadata */ - char *begptr; /* Pointer to beginning of metadata section */ + char *endptr; /* Pointer to end of metadata section */ char *metaptr; /* Metadata pointer */ char *prevmetaptr;/* Previous position of metadata pointer */ - char *ptr[8]; /* String pointer array (for dim map parsing) */ - char type[32] = {0}; /* Number type descriptor string */ - char *metaArr[2]; /* Array of metadata positions */ - char *colon; /* Colon position */ - char *colon2; /* 2nd colon position */ - char *slash; /* Slash position */ - char *utlstr; /* Utility string */ - char *utlstr2; /* Utility string 2 */ - - - /* Allocate space for utility strings */ - /* ---------------------------------- */ - utlstr = (char *) calloc(UTLSTRSIZE, sizeof(char)); + char *utlstr; /* Utility string */ + + + + /* Allocate memory for utility string */ + /* ---------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE,sizeof(char)); if(utlstr == NULL) { - HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); - return(-1); - } + HEpush(DFE_NOSPACE,"EHEHmetagroup", __FILE__, __LINE__); - utlstr2 = (char *) calloc(UTLSTRSIZE, sizeof(char)); - if(utlstr2 == NULL) - { - HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); - free(utlstr); - return(-1); + return( NULL); } - /* Determine number of structural metadata "sections" */ /* -------------------------------------------------- */ nmeta = 0; @@ -1733,7 +1508,7 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode { /* Search for "StructMetadata.x" attribute */ /* --------------------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d", "StructMetadata.", (int)nmeta); + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%d", "StructMetadata.", (int)nmeta); attrIndex = SDfindattr(sdInterfaceID, utlstr); @@ -1752,12 +1527,12 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* Allocate space for metadata (in units of 32000 bytes) */ /* ----------------------------------------------------- */ metabuf = (char *) calloc(32000 * nmeta, 1); + if(metabuf == NULL) { - HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); + HEpush(DFE_NOSPACE,"EHmetagroup", __FILE__, __LINE__); free(utlstr); - free(utlstr2); - return(-1); + return(metabuf); } @@ -1765,7 +1540,7 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode /* ------------------------ */ for (i = 0; i < nmeta; i++) { - snprintf(utlstr, UTLSTRSIZE, "%s%d", "StructMetadata.", i); + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%d", "StructMetadata.", i); attrIndex = SDfindattr(sdInterfaceID, utlstr); metalen = (int)strlen(metabuf); SDreadattr(sdInterfaceID, attrIndex, metabuf + metalen); @@ -1798,835 +1573,9 @@ EHinsertmeta(int32 sdInterfaceID, const char *structname, const char *structcode - /* Find specific (named) structure */ - /* ------------------------------- */ - if (metacode < 1000) - { - /* Save current metadata pointer */ - /* ----------------------------- */ - prevmetaptr = metaptr; - - - /* First loop for "old-style" (non-ODL) metadata string */ - /* ---------------------------------------------------- */ - if (strcmp(structcode, "s") == 0) - { - snprintf(utlstr, UTLSTRSIZE, "%s%s", "SwathName=\"", structname); - } else if (strcmp(structcode, "g") == 0) - { - snprintf(utlstr, UTLSTRSIZE, "%s%s", "GridName=\"", structname); - } else if (strcmp(structcode, "p") == 0) - { - snprintf(utlstr, UTLSTRSIZE, "%s%s", "PointName=\"", structname); - } - /* Do string search */ - /* ---------------- */ - metaptr = strstr(metaptr, utlstr); - - - /* - * If not found then return to previous position in metadata and look - * for "new-style" (ODL) metadata string - */ - if (metaptr == NULL) - { - snprintf(utlstr, UTLSTRSIZE, "%s%s", "GROUP=\"", structname); - metaptr = strstr(prevmetaptr, utlstr); - } - } - /* - * If searching for geo fields (3), data fields (4), or point fields (11) - * convert type code to string designator. - */ - if (metacode == 3 || metacode == 4 || metacode == 11) - { - switch (metadata[0]) - { - case 3: - strcpy(type, "DFNT_UCHAR8"); - break; - case 4: - strcpy(type, "DFNT_CHAR8"); - break; - case 5: - strcpy(type, "DFNT_FLOAT32"); - break; - case 6: - strcpy(type, "DFNT_FLOAT64"); - break; - case 20: - strcpy(type, "DFNT_INT8"); - break; - case 21: - strcpy(type, "DFNT_UINT8"); - break; - case 22: - strcpy(type, "DFNT_INT16"); - break; - case 23: - strcpy(type, "DFNT_UINT16"); - break; - case 24: - strcpy(type, "DFNT_INT32"); - break; - case 25: - strcpy(type, "DFNT_UINT32"); - break; - } - } - /* Metadata Section Switch */ - /* ----------------------- */ - switch (abs(metacode)) - { - - case 0: - /* Dimension Section */ - /* ----------------- */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=Dimension"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\tEND_GROUP=Dimension"); - metaptr = strstr(metaptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%d%s%d%s", - "\t\t\tOBJECT=Dimension_", (int)count, - "\n\t\t\t\tDimensionName=\"", &metastr[0], - "\"\n\t\t\t\tSize=", (int)metadata[0], - "\n\t\t\tEND_OBJECT=Dimension_", (int)count, "\n"); - break; - - - case 1: - /* Dimension Map Section */ - /* --------------------- */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=DimensionMap"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\tEND_GROUP=DimensionMap"); - metaptr = strstr(metaptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Find slash within input mapping string and replace with NULL */ - /* ------------------------------------------------------------ */ - EHparsestr(metastr, '/', ptr, slen); - metastr[slen[0]] = 0; - - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%d%s%d%s%d%s", - "\t\t\tOBJECT=DimensionMap_", (int)count, - "\n\t\t\t\tGeoDimension=\"", &metastr[0], - "\"\n\t\t\t\tDataDimension=\"", &metastr[slen[0] + 1], - "\"\n\t\t\t\tOffset=", (int)metadata[0], - "\n\t\t\t\tIncrement=", (int)metadata[1], - "\n\t\t\tEND_OBJECT=DimensionMap_", (int)count, "\n"); - break; - - - case 2: - /* Index Dimension Map Section */ - /* --------------------------- */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=IndexDimensionMap"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\tEND_GROUP=IndexDimensionMap"); - metaptr = strstr(metaptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Find slash within input mapping string and replace with NULL */ - /* ------------------------------------------------------------ */ - EHparsestr(metastr, '/', ptr, slen); - metastr[slen[0]] = 0; - - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%d%s", - "\t\t\tOBJECT=IndexDimensionMap_", (int)count, - "\n\t\t\t\tGeoDimension=\"", &metastr[0], - "\"\n\t\t\t\tDataDimension=\"", &metastr[slen[0] + 1], - "\"\n\t\t\tEND_OBJECT=IndexDimensionMap_", (int)count, "\n"); - break; - - - case 3: - /* Geolocation Field Section */ - /* ------------------------- */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=GeoField"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\tEND_GROUP=GeoField"); - metaptr = strstr(metaptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Find colon (parse off field name) */ - /* --------------------------------- */ - colon = strchr(metastr, ':'); - if (colon == NULL) - { - free(utlstr); - free(utlstr2); - return -1; - } - *colon = 0; - - /* Search for next colon (compression and/or tiling parameters) */ - /* ------------------------------------------------------------ */ - colon2 = strchr(colon + 1, ':'); - if (colon2 != NULL) - { - *colon2 = 0; - } - /* Make metadata string list for dimension list */ - /* -------------------------------------------- */ - EHmetalist(colon + 1, utlstr2); - - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%s", - "\t\t\tOBJECT=GeoField_", (int)count, - "\n\t\t\t\tGeoFieldName=\"", metastr, - "\"\n\t\t\t\tDataType=", type, - "\n\t\t\t\tDimList=", utlstr2); - - - /* If compression and/or tiling parameters add to string */ - /* ----------------------------------------------------- */ - if (colon2 != NULL) - { - strcat(utlstr, colon2 + 1); - } - /* Add END_OBJECT terminator to metadata string */ - /* -------------------------------------------- */ - snprintf(utlstr2, UTLSTRSIZE, "%s%d%s", - "\n\t\t\tEND_OBJECT=GeoField_", (int)count, "\n"); - strcat(utlstr, utlstr2); - - break; - - - case 4: - /* Data Field Section */ - /* ------------------ */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=DataField"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\tEND_GROUP=DataField"); - metaptr = strstr(metaptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Find colon (parse off field name) */ - /* --------------------------------- */ - colon = strchr(metastr, ':'); - if (colon == NULL) - { - free(utlstr); - free(utlstr2); - return -1; - } - *colon = 0; - - /* Search for next colon (compression and/or tiling parameters) */ - /* ------------------------------------------------------------ */ - colon2 = strchr(colon + 1, ':'); - if (colon2 != NULL) - { - *colon2 = 0; - } - /* Make metadata string list from dimension list */ - /* --------------------------------------------- */ - EHmetalist(colon + 1, utlstr2); - - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%s", - "\t\t\tOBJECT=DataField_", (int)count, - "\n\t\t\t\tDataFieldName=\"", metastr, - "\"\n\t\t\t\tDataType=", type, - "\n\t\t\t\tDimList=", utlstr2); - - - /* If compression and/or tiling parameters add to string */ - /* ----------------------------------------------------- */ - if (colon2 != NULL) - { - strcat(utlstr, colon2 + 1); - } - /* Add END_OBJECT terminator to metadata string */ - /* -------------------------------------------- */ - snprintf(utlstr2, UTLSTRSIZE, "%s%d%s", - "\n\t\t\tEND_OBJECT=DataField_", (int)count, "\n"); - strcat(utlstr, utlstr2); - - break; - - - case 6: - /* Merged Field Section */ - /* -------------------- */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=MergedFields"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\tEND_GROUP=MergedFields"); - metaptr = strstr(metaptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Find colon (parse off merged fieldname) */ - /* --------------------------------------- */ - colon = strchr(metastr, ':'); - if (colon == NULL) - { - free(utlstr); - free(utlstr2); - return -1; - } - *colon = 0; - - /* Make metadata string list from field list */ - /* ----------------------------------------- */ - EHmetalist(colon + 1, utlstr2); - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%s%d%s", - "\t\t\tOBJECT=MergedFields_", (int)count, - "\n\t\t\t\tMergedFieldName=\"", metastr, "\"", - "\n\t\t\t\tFieldList=", utlstr2, - "\n\t\t\tEND_OBJECT=MergedFields_", (int)count, "\n"); - break; - - - case 10: - /* Point Level Section */ - /* ------------------- */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=Level"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\n\t\tEND_GROUP=Level"); - metaptr = strstr(metaptr, utlstr) + 1; - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntGROUP(metaArr); - - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%d%s", - "\t\t\tGROUP=Level_", (int)count, - "\n\t\t\t\tLevelName=\"", metastr, - "\"\n\t\t\tEND_GROUP=Level_", (int)count, "\n"); - break; - - - case 11: - /* Point Field Section */ - /* ------------------- */ - - /* Find colon (parse off point field name) */ - /* --------------------------------------- */ - colon = strchr(metastr, ':'); - if (colon == NULL) - { - free(utlstr); - free(utlstr2); - return -1; - } - *colon = 0; - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\t\t\tLevelName=\""); - strcat(utlstr, colon + 1); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\t\tEND_GROUP=Level_"); - metaptr = strstr(begptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%d%s%d%s", - "\t\t\t\tOBJECT=PointField_", (int)count, - "\n\t\t\t\t\tPointFieldName=\"", metastr, - "\"\n\t\t\t\t\tDataType=", type, - "\n\t\t\t\t\tOrder=", (int)metadata[1], - "\n\t\t\t\tEND_OBJECT=PointField_", (int)count, "\n"); - break; - - - - case 12: - /* Level Link Section */ - /* ------------------ */ - - /* Find beginning and ending of metadata section */ - /* --------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=LevelLink"); - begptr = strstr(metaptr, utlstr); - - strcpy(utlstr, "\t\tEND_GROUP=LevelLink"); - metaptr = strstr(metaptr, utlstr); - - - /* Count number of existing entries and increment */ - /* ---------------------------------------------- */ - metaArr[0] = begptr; - metaArr[1] = metaptr; - count = EHcntOBJECT(metaArr) + 1; - - - /* Find colon (parse off parent/child level names from link field) */ - /* --------------------------------------------------------------- */ - colon = strchr(metastr, ':'); - if (colon == NULL) - { - free(utlstr); - free(utlstr2); - return -1; - } - *colon = 0; - - /* Find slash (divide parent and child levels) */ - /* ------------------------------------------- */ - slash = strchr(metastr, '/'); - if (slash == NULL) - { - free(utlstr); - free(utlstr2); - return -1; - } - *slash = 0; - - /* Build metadata entry string */ - /* --------------------------- */ - snprintf(utlstr, UTLSTRSIZE, "%s%d%s%s%s%s%s%s%s%d%s", - "\t\t\tOBJECT=LevelLink_", (int)count, - "\n\t\t\t\tParent=\"", metastr, - "\"\n\t\t\t\tChild=\"", slash + 1, - "\"\n\t\t\t\tLinkField=\"", colon + 1, - "\"\n\t\t\tEND_OBJECT=LevelLink_", (int)count, "\n"); - - break; - - - case 101: - /* Position metadata pointer for Grid proj parms, pix reg, origin */ - /* -------------------------------------------------------------- */ - strcpy(utlstr, "\t\tGROUP=Dimension"); - metaptr = strstr(metaptr, utlstr); - strcpy(utlstr, metastr); - - break; - - - case 1001: - /* Position metadata pointer for new swath structure (SWcreate) */ - /* ------------------------------------------------------------ */ - strcpy(utlstr, "END_GROUP=SwathStructure"); - metaptr = strstr(metaptr, utlstr); - strcpy(utlstr, metastr); - break; - - - case 1002: - /* Position metadata pointer for new grid structure (GDcreate) */ - /* ----------------------------------------------------------- */ - strcpy(utlstr, "END_GROUP=GridStructure"); - metaptr = strstr(metaptr, utlstr); - strcpy(utlstr, metastr); - break; - - - case 1003: - /* Position metadata pointer for new point structure (PTcreate) */ - /* ------------------------------------------------------------ */ - strcpy(utlstr, "END_GROUP=PointStructure"); - metaptr = strstr(metaptr, utlstr); - strcpy(utlstr, metastr); - break; - } - - - - /* Get length of metadata string to insert */ - /* --------------------------------------- */ - seglen = (int)strlen(utlstr); - - /* Get offset of entry position within existing metadata */ - /* ----------------------------------------------------- */ - offset = (int)(metaptr - metabuf); - - - /* If end of new metadata string outside of current metadata buffer ... */ - /* -------------------------------------------------------------------- */ - if (metalen + seglen > 32000 * nmeta - 1) - { - /* Reallocate metadata buffer with additional 32000 bytes */ - /* ------------------------------------------------------ */ - metabuf = (char *) realloc((void *) metabuf, 32000 * (nmeta + 1)); - if(metabuf == NULL) - { - HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__); - free(utlstr); - free(utlstr2); - return(-1); - } - - /* Increment metadata section counter */ - /* ---------------------------------- */ - nmeta++; - - /* Reposition metadata pointer (entry position) */ - /* -------------------------------------------- */ - metaptr = metabuf + offset; - } - /* Move metadata following entry point to its new position */ - /* ------------------------------------------------------- */ - for (i = metalen - 1; i > offset - 1; i--) - { - *(metabuf + seglen + i) = *(metabuf + i); - } - - /* Copy new metadata string (utlstr) into metadata */ - /* ---------------------------------------------- */ - memcpy(metaptr, utlstr, seglen); - - /* set to null character remaining of the metabuf */ - - memset((metabuf + metalen + seglen), '\0', (nmeta*32000 -1 - (metalen + - seglen))); - /* Add new null string terminator */ - /* ------------------------------ */ - metabuf[metalen + seglen] = 0; - - - /* Write Back to Global Attribute(s) */ - /* --------------------------------- */ - for (i = 0; i < nmeta; i++) - { - snprintf(utlstr, UTLSTRSIZE, "%s%d", "StructMetadata.", i); - SDsetattr(sdInterfaceID, utlstr, DFNT_CHAR8, - 32000, metabuf + i * 32000); - } - - free(metabuf); - free(utlstr); - free(utlstr2); - - return (status); -} -#endif - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: EHgetmetavalue | -| | -| DESCRIPTION: Returns metadata value | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| metaptrs char Begin and end of metadata section | -| parameter char parameter to access | -| | -| OUTPUTS: | -| metaptr char Ptr to (updated) beginning of metadata | -| retstr char return string containing value | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Jan 97 Joel Gales Check string pointer against end of meta section | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -EHgetmetavalue(char *metaptrs[], const char *parameter, char *retstr) -{ - intn status = 0; /* routine return status variable */ - - int32 slen; /* String length */ - char *newline; /* Position of new line character */ - char *sptr; /* string pointer within metadata */ - - - /* Get string length of parameter string + 1 */ - /* ----------------------------------------- */ - slen = (int)strlen(parameter) + 1; - - - /* Build search string (parameter string + "=") */ - /* -------------------------------------------- */ - strcpy(retstr, parameter); - strcat(retstr, "="); - - - /* Search for string within metadata (beginning at metaptrs[0]) */ - /* ------------------------------------------------------------ */ - sptr = strstr(metaptrs[0], retstr); - - - /* If string found within desired section ... */ - /* ------------------------------------------ */ - if (sptr != NULL && sptr < metaptrs[1]) - { - /* Store position of string within metadata */ - /* ---------------------------------------- */ - metaptrs[0] = sptr; - - /* Find newline "\n" character */ - /* --------------------------- */ - newline = strchr(metaptrs[0], '\n'); - if (newline == NULL) - { - retstr[0] = 0; - return -1; - } - - /* Copy from "=" to "\n" (exclusive) into return string */ - /* ---------------------------------------------------- */ - memcpy(retstr, metaptrs[0] + slen, newline - metaptrs[0] - slen); - - /* Terminate return string with null */ - /* --------------------------------- */ - retstr[newline - metaptrs[0] - slen] = 0; - } else - { - /* - * if parameter string not found within section, null return string - * and set status to -1. - */ - retstr[0] = 0; - status = -1; - } - - return (status); -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: EHmetagroup | -| | -| DESCRIPTION: Returns pointers to beginning and end of metadata group | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| metabuf char Pointer to HDF-EOS object in metadata | -| | -| INPUTS: | -| sdInterfaceID int32 SDS interface ID | -| structname char HDF-EOS structure name | -| structcode char Structure code ("s/g/p") | -| groupname char Metadata group name | -| | -| OUTPUTS: | -| metaptrs char pointers to begin and end of metadata | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -char * -EHmetagroup(int32 sdInterfaceID, const char *structname, const char *structcode, - const char *groupname, char *metaptrs[]) -{ - intn i; /* Loop index */ - - int32 attrIndex; /* Structural metadata attribute index */ - int32 nmeta; /* Number of 32000 byte metadata sections */ - int32 metalen; /* Length of structural metadata */ - - char *metabuf; /* Pointer (handle) to structural metadata */ - char *endptr; /* Pointer to end of metadata section */ - char *metaptr; /* Metadata pointer */ - char *prevmetaptr;/* Previous position of metadata pointer */ - char *utlstr; /* Utility string */ - - - - /* Allocate memory for utility string */ - /* ---------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE,sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"EHEHmetagroup", __FILE__, __LINE__); - - return( NULL); - } - /* Determine number of structural metadata "sections" */ - /* -------------------------------------------------- */ - nmeta = 0; - while (1) - { - /* Search for "StructMetadata.x" attribute */ - /* --------------------------------------- */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%d", "StructMetadata.", (int)nmeta); - attrIndex = SDfindattr(sdInterfaceID, utlstr); - - - /* If found then increment metadata section counter else exit loop */ - /* --------------------------------------------------------------- */ - if (attrIndex != -1) - { - nmeta++; - } else - { - break; - } - } - - - /* Allocate space for metadata (in units of 32000 bytes) */ - /* ----------------------------------------------------- */ - metabuf = (char *) calloc(32000 * nmeta, 1); - - if(metabuf == NULL) - { - HEpush(DFE_NOSPACE,"EHmetagroup", __FILE__, __LINE__); - free(utlstr); - return(metabuf); - } - - - /* Read structural metadata */ - /* ------------------------ */ - for (i = 0; i < nmeta; i++) - { - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%d", "StructMetadata.", i); - attrIndex = SDfindattr(sdInterfaceID, utlstr); - metalen = (int)strlen(metabuf); - SDreadattr(sdInterfaceID, attrIndex, metabuf + metalen); - } - - /* Determine length (# of characters) of metadata */ - /* ---------------------------------------------- */ - metalen = (int)strlen(metabuf); - - - - /* Find HDF-EOS structure "root" group in metadata */ - /* ----------------------------------------------- */ - - /* Setup proper search string */ - /* -------------------------- */ - if (strcmp(structcode, "s") == 0) - { - strcpy(utlstr, "GROUP=SwathStructure"); - } else if (strcmp(structcode, "g") == 0) - { - strcpy(utlstr, "GROUP=GridStructure"); - } else if (strcmp(structcode, "p") == 0) - { - strcpy(utlstr, "GROUP=PointStructure"); - } - /* Use string search routine (strstr) to move through metadata */ - /* ----------------------------------------------------------- */ - metaptr = strstr(metabuf, utlstr); - - - - /* Save current metadata pointer */ - /* ----------------------------- */ - prevmetaptr = metaptr; + /* Save current metadata pointer */ + /* ----------------------------- */ + prevmetaptr = metaptr; /* First loop for "old-style" (non-ODL) metadata string */ @@ -2684,389 +1633,6 @@ EHmetagroup(int32 sdInterfaceID, const char *structname, const char *structcode, } - - -#if defined(HDFEOS_GD_WRITE_SUPPORT) || defined(HDFEOS_SW_WRITE_SUPPORT) -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: EHfillfld | -| | -| DESCRIPTION: Fills field with fill value | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| sdid int32 SD element ID | -| rank int32 Rank of field | -| truerank int32 True rank of field (merging) | -| size int32 size of fill element | -| off int32 Offset of field within merged field | -| dims int32 Dimensions of field | -| fillval void fill value | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -EHfillfld(int32 sdid, int32 rank, CPL_UNUSED int32 truerank, int32 size, int32 off, - int32 dims[], VOIDP fillval) -{ - intn i; /* Loop index */ - intn j; /* Loop index */ - intn status = 0; /* routine return status variable */ - - int32 n; /* Max number of planes or rows in fill - * buffer */ - int32 start[3] = {0, 0, 0}; /* Start array (SDwritedata) */ - int32 edge[3]; /* Edge (count) array (SDwritedata) */ - int32 totN; /* Total number of elements in field */ - int32 planeN; /* Number of elements in plane */ - - char *fillbuf; /* Fill buffer */ - - - /* Get total number of elements in field */ - /* ------------------------------------- */ - totN = dims[0]; - for (i = 1; i < rank; i++) - { - totN *= dims[i]; - } - - - /* Get number of elements in a plane of the field */ - /* ---------------------------------------------- */ - planeN = dims[1] * dims[2]; - - - - /* Allocate & Write Fill buffer */ - /* ---------------------------- */ - if (totN * size < HDFE_MAXMEMBUF) - { - /* Entire field size (in bytes) smaller than max fill buffer */ - /* --------------------------------------------------------- */ - - - /* Allocate fill buffer */ - /* -------------------- */ - fillbuf = (char *) malloc(totN * size); - if(fillbuf == NULL) - { - HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__); - return(-1); - } - - - /* Fill buffer with fill value */ - /* --------------------------- */ - for (i = 0; i < totN; i++) - { - memcpy(fillbuf + i * size, fillval, size); - } - - - /* Write fill buffer to field */ - /* -------------------------- */ - start[0] = off; - edge[0] = dims[0]; - edge[1] = dims[1]; - edge[2] = dims[2]; - status = SDwritedata(sdid, start, NULL, edge, - (VOIDP) fillbuf); - - free(fillbuf); - - } else if (planeN * size < HDFE_MAXMEMBUF) - { - /* Single plane size (in bytes) smaller than max fill buffer */ - /* --------------------------------------------------------- */ - - - /* Compute number of planes that can be written at one time */ - /* -------------------------------------------------------- */ - n = HDFE_MAXMEMBUF / (planeN * size); - - - /* Allocate fill buffer */ - /* -------------------- */ - fillbuf = (char *) malloc(planeN * size * n); - if(fillbuf == NULL) - { - HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__); - return(-1); - } - - - /* Fill buffer with fill value */ - /* --------------------------- */ - for (i = 0; i < planeN * n; i++) - { - memcpy(fillbuf + i * size, fillval, size); - } - - - /* Write (full) fill buffer to field */ - /* --------------------------------- */ - for (i = 0; i < (dims[0] / n); i++) - { - start[0] = off + i * n; - edge[0] = n; - edge[1] = dims[1]; - edge[2] = dims[2]; - status = SDwritedata(sdid, start, NULL, edge, - (VOIDP) fillbuf); - } - - - /* Write (partial) last fill buffer to field (if necessary) */ - /* -------------------------------------------------------- */ - if (i * n != dims[0]) - { - start[0] = off + i * n; - edge[0] = dims[0] - i * n; - edge[1] = dims[1]; - edge[2] = dims[2]; - status = SDwritedata(sdid, start, NULL, edge, - (VOIDP) fillbuf); - } - free(fillbuf); - - } else - { - /* Single plane size (in bytes) greater than max fill buffer */ - /* --------------------------------------------------------- */ - - - /* Compute number of "rows" than can be written at one time */ - /* -------------------------------------------------------- */ - n = HDFE_MAXMEMBUF / (dims[rank - 1] * size); - - - /* Allocate fill buffer */ - /* -------------------- */ - fillbuf = (char *) malloc(dims[rank - 1] * size * n); - if(fillbuf == NULL) - { - HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__); - return(-1); - } - - - /* Fill buffer with fill value */ - /* --------------------------- */ - for (i = 0; i < dims[rank - 1] * n; i++) - { - memcpy(fillbuf + i * size, fillval, size); - } - - - /* For every plane in field ... */ - /* ---------------------------- */ - for (j = 0; j < dims[0]; j++) - { - - /* Write (full) fill buffer to field */ - /* --------------------------------- */ - for (i = 0; i < (dims[1] / n); i++) - { - start[0] = off + j; - start[1] = i * n; - edge[0] = 1; - edge[1] = n; - edge[2] = dims[2]; - status = SDwritedata(sdid, start, NULL, edge, - (VOIDP) fillbuf); - } - - - /* Write (partial) last fill buffer to field (if necessary) */ - /* -------------------------------------------------------- */ - if (i * n != dims[1]) - { - start[0] = off + j; - start[1] = i * n; - edge[0] = 1; - edge[1] = dims[1] - i * n; - edge[2] = dims[2]; - status = SDwritedata(sdid, start, NULL, edge, - (VOIDP) fillbuf); - } - } - - free(fillbuf); - - } - - return (status); -} -#endif - - - - -#ifdef HDFEOS_GD_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: EHbisect | -| | -| DESCRIPTION: Finds root of function using bisection | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| func() float64 Function to bisect | -| funcParms float64 Function parameters (fixed) | -| nParms int32 Number of function parameters | -| limLft float64 Lower limit of function argument | -| limRgt float64 Upper limit of function argument | -| convCrit float64 Convergence criterion | -| | -| OUTPUTS: | -| root float64 Function root | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Nov 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -EHbisect(float64(*func) (float64[]), float64 funcParms[], int32 nParms, - float64 limLft, float64 limRgt, float64 convCrit, float64 * root) -{ - intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - - float64 midPnt; /* Mid-point value */ - float64 newmidPnt; /* New mid-point value */ - float64 funcLft; /* Function value at left-hand limit */ - float64 funcMid; /* Function value at mid-point */ - float64 funcRgt; /* Function value at right-hand limit */ - float64 *parms; /* Function parameters */ - - - /* Allocate space for function parameters */ - /* -------------------------------------- */ - parms = (float64 *) calloc(nParms + 1, sizeof(float64)); - if(parms == NULL) - { - HEpush(DFE_NOSPACE, "EHbisect", __FILE__, __LINE__); - return(-1); - } - - - /* Copy (fixed) function parameters */ - /* -------------------------------- */ - for (i = 0; i < nParms; i++) - { - parms[i + 1] = funcParms[i]; - } - - - /* Copy left-hand limit to "floating" parameter */ - /* -------------------------------------------- */ - parms[0] = limLft; - - - /* Determine function value */ - /* ------------------------ */ - funcLft = (*func) (parms); - - - /* Copy right-hand limit to "floating" parameter */ - /* --------------------------------------------- */ - parms[0] = limRgt; - - - /* Determine function value */ - /* ------------------------ */ - funcRgt = (*func) (parms); - - - /* If left and right limits function values of same sign then no root */ - /* ------------------------------------------------------------------ */ - if (funcLft * funcRgt > 0) - { - free(parms); - return (-1); - } - /* Compute (initial) mid-point */ - /* --------------------------- */ - newmidPnt = 0.5 * (limLft + limRgt); - - - /* Bisection Loop */ - /* -------------- */ - while (1) - { - /* Compute function at new mid-point */ - /* --------------------------------- */ - midPnt = newmidPnt; - parms[0] = midPnt; - funcMid = (*func) (parms); - - - /* If left limit same sign as mid-point move it to mid-point */ - /* --------------------------------------------------------- */ - if (funcLft * funcMid > 0.0) - { - limLft = midPnt; - } else - { - /* Otherwise move over right-hand limit */ - /* ------------------------------------ */ - limRgt = midPnt; - } - - - /* Compute new mid-point */ - /* --------------------- */ - newmidPnt = 0.5 * (limLft + limRgt); - - - /* If relative change in midpoint < convergence crit then exit loop */ - /* ---------------------------------------------------------------- */ - if (fabs((newmidPnt - midPnt) / midPnt) < convCrit) - { - break; - } - } - - /* Save root */ - /* --------- */ - *root = newmidPnt; - - - free(parms); - - return (status); -} -#endif - - - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | diff --git a/frmts/hdf4/hdf-eos/GDapi.c b/frmts/hdf4/hdf-eos/GDapi.c index cc09d9379983..712ad047c7a1 100644 --- a/frmts/hdf4/hdf-eos/GDapi.c +++ b/frmts/hdf4/hdf-eos/GDapi.c @@ -102,12 +102,6 @@ extern void inv_init(int32, int32, float64 *, int32, const char *, const char * #define GDIDOFFSET 4194304 #define SQUARE(x) ((x) * (x)) /* x**2 */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) -static int32 GDXSDcomb[512*5]; -static char GDXSDdims[HDFE_DIMBUFSIZE]; -static char GDXSDname[HDFE_NAMBUFSIZE]; -#endif - #define NGRID 200 /* Grid Structure External Arrays */ @@ -233,21 +227,7 @@ static intn GDll2ij(int32, int32, float64 [], int32, int32, int32, float64[], float64[], float64[]); static intn GDgetdefaults(int32, int32, float64[], int32, float64[], float64[]); -#ifdef HDFEOS_GD_WRITE_SUPPORT -static intn GDtangentpnts(int32, float64[], float64[], float64[], float64[], - float64 [], int32 *); -#endif static intn GDwrrdtile(int32, const char *, const char *, int32 [], VOIDP); -#ifdef UNUSED_BY_GDAL -static intn GDll2mm_cea(int32,int32, int32, float64[], int32, int32, - float64[], float64[], int32, float64[],float64[], - float64[], float64[], float64 *, float64 *); -#endif -#ifdef HDFEOS_GD_WRITE_SUPPORT -static intn GDmm2ll_cea(int32, int32, int32, float64[], int32, int32, - float64[], float64[], int32, float64[], float64[], - float64[], float64[]); -#endif /*----------------------------------------------------------------------------| | BEGIN_PROLOG | @@ -294,272 +274,6 @@ GDopen(const char *filename, intn l_access) } -#ifdef HDFEOS_GD_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDcreate | -| | -| DESCRIPTION: Creates a grid within the file. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| gridID int32 Grid structure ID | -| | -| INPUTS: | -| fid int32 File ID | -| gridname char Grid structure name | -| xdimsize int32 Number of columns in grid | -| ydimsize int32 Number of rows in grid | -| upleftpt float64 Location (m/deg) of upper left corner | -| lowrightpt float64 Location (m/deg) of lower right corner | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Aug 96 Joel Gales Check grid name for ODL compliance | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDcreate(int32 fid, const char *gridname, int32 xdimsize, int32 ydimsize, - float64 upleftpt[], float64 lowrightpt[]) -{ - intn i; /* Loop index */ - intn ngridopen = 0; /* # of grid structures open */ - intn status = 0; /* routine return status variable */ - - uint8 l_access; /* Read/Write file l_access code */ - - int32 HDFfid; /* HDF file id */ - int32 vgRef; /* Vgroup reference number */ - int32 vgid[3]; /* Vgroup ID array */ - int32 gridID = -1;/* HDF-EOS grid ID */ - - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 nGrid = 0; /* Grid counter */ - - char name[80]; /* Vgroup name */ - char class[80]; /* Vgroup class */ - char errbuf[256];/* Buffer for error message */ - char utlbuf[1024]; /* Utility buffer */ - char header[128];/* Structural metadata header string */ - char footer[256];/* Structural metadata footer string */ - char refstr1[128]; /* Upper left ref string (metadata) */ - char refstr2[128]; /* Lower right ref string (metadata) */ - - - /* - * Check HDF-EOS file ID, get back HDF file ID, SD interface ID and - * l_access code - */ - status = EHchkfid(fid, gridname, &HDFfid, &sdInterfaceID, &l_access); - - - /* Check gridname for length */ - /* ------------------------- */ - if ((intn) strlen(gridname) > VGNAMELENMAX) - { - status = -1; - HEpush(DFE_GENAPP, "GDcreate", __FILE__, __LINE__); - HEreport("Gridname \"%s\" must be less than %d characters.\n", - gridname, VGNAMELENMAX); - } - - - - if (status == 0) - { - /* Determine number of grids currently opened */ - /* ------------------------------------------- */ - for (i = 0; i < NGRID; i++) - { - ngridopen += GDXGrid[i].active; - } - - - /* Setup file interface */ - /* -------------------- */ - if (ngridopen < NGRID) - { - - /* Check that grid has not been previously opened */ - /* ----------------------------------------------- */ - vgRef = -1; - - while (1) - { - vgRef = Vgetid(HDFfid, vgRef); - - /* If no more Vgroups then exist while loop */ - /* ---------------------------------------- */ - if (vgRef == -1) - { - break; - } - - /* Get name and class of Vgroup */ - /* ---------------------------- */ - vgid[0] = Vattach(HDFfid, vgRef, "r"); - Vgetname(vgid[0], name); - Vgetclass(vgid[0], class); - Vdetach(vgid[0]); - - - /* If GRID then increment # grid counter */ - /* ------------------------------------- */ - if (strcmp(class, "GRID") == 0) - { - nGrid++; - } - - - /* If grid already exist, return error */ - /* ------------------------------------ */ - if (strcmp(name, gridname) == 0 && - strcmp(class, "GRID") == 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDcreate", __FILE__, __LINE__); - HEreport("\"%s\" already exists.\n", gridname); - break; - } - } - - - if (status == 0) - { - /* Create Root Vgroup for Grid */ - /* ---------------------------- */ - vgid[0] = Vattach(HDFfid, -1, "w"); - - - /* Set Name and Class (GRID) */ - /* -------------------------- */ - Vsetname(vgid[0], gridname); - Vsetclass(vgid[0], "GRID"); - - - - /* Create Data Fields Vgroup */ - /* ------------------------- */ - vgid[1] = Vattach(HDFfid, -1, "w"); - Vsetname(vgid[1], "Data Fields"); - Vsetclass(vgid[1], "GRID Vgroup"); - Vinsert(vgid[0], vgid[1]); - - - - /* Create Attributes Vgroup */ - /* ------------------------ */ - vgid[2] = Vattach(HDFfid, -1, "w"); - Vsetname(vgid[2], "Grid Attributes"); - Vsetclass(vgid[2], "GRID Vgroup"); - Vinsert(vgid[0], vgid[2]); - - - - /* Establish Grid in Structural MetaData Block */ - /* -------------------------------------------- */ - snprintf(header, sizeof(header), "%s%d%s%s%s%s%d%s%s%d%s", - "\tGROUP=GRID_", (int)(nGrid + 1), - "\n\t\tGridName=\"", gridname, "\"\n", - "\t\tXDim=", (int)xdimsize, "\n", - "\t\tYDim=", (int)ydimsize, "\n"); - - - snprintf(footer, sizeof(footer), - "%s%s%s%s%s%s%s%d%s", - "\t\tGROUP=Dimension\n", - "\t\tEND_GROUP=Dimension\n", - "\t\tGROUP=DataField\n", - "\t\tEND_GROUP=DataField\n", - "\t\tGROUP=MergedFields\n", - "\t\tEND_GROUP=MergedFields\n", - "\tEND_GROUP=GRID_", (int)(nGrid + 1), "\n"); - - - - /* Build Ref point Col-Row strings */ - /* ------------------------------- */ - if (upleftpt == NULL || - (upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0)) - { - strcpy(refstr1, "DEFAULT"); - strcpy(refstr2, "DEFAULT"); - } - else - { - CPLsnprintf(refstr1, sizeof(refstr1), "%s%f%s%f%s", - "(", upleftpt[0], ",", upleftpt[1], ")"); - - CPLsnprintf(refstr2, sizeof(refstr2), "%s%f%s%f%s", - "(", lowrightpt[0], ",", lowrightpt[1], ")"); - } - - snprintf(utlbuf, sizeof(utlbuf), - "%s%s%s%s%s%s%s%s", - header, - "\t\tUpperLeftPointMtrs=", refstr1, "\n", - "\t\tLowerRightMtrs=", refstr2, "\n", - footer); - - status = EHinsertmeta(sdInterfaceID, "", "g", 1002L, - utlbuf, NULL); - - } - } - else - { - /* Too many files opened */ - /* --------------------- */ - status = -1; - strcpy(errbuf, - "No more than %d grids may be open simultaneously"); - strcat(errbuf, " (%s)"); - HEpush(DFE_DENIED, "GDcreate", __FILE__, __LINE__); - HEreport(errbuf, NGRID, gridname); - } - - - /* Assign gridID # & Load grid and GDXGrid.fid table entries */ - /* --------------------------------------------------------- */ - if (status == 0) - { - - for (i = 0; i < NGRID; i++) - { - if (GDXGrid[i].active == 0) - { - gridID = i + idOffset; - GDXGrid[i].active = 1; - GDXGrid[i].IDTable = vgid[0]; - GDXGrid[i].VIDTable[0] = vgid[1]; - GDXGrid[i].VIDTable[1] = vgid[2]; - GDXGrid[i].fid = fid; - status = 0; - break; - } - } - - } - } - return (gridID); -} -#endif - - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -916,23 +630,22 @@ GDchkgdid(int32 gridID, const char *routname, } -#ifdef HDFEOS_GD_WRITE_SUPPORT + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDdefdim | +| FUNCTION: GDdiminfo | | | -| DESCRIPTION: Defines a new dimension within the grid. | +| DESCRIPTION: Retrieve size of specified dimension. | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| size int32 Size of dimension | | | | INPUTS: | -| gridID int32 grid structure ID | -| dimname char Dimension name to define | -| dim int32 Dimension value | +| gridID int32 grid structure id | +| dimname char Dimension name | | | | | | OUTPUTS: | @@ -944,216 +657,120 @@ GDchkgdid(int32 gridID, const char *routname, | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -GDdefdim(int32 gridID, const char *dimname, int32 dim) +int32 +GDdiminfo(int32 gridID, const char *dimname) { intn status; /* routine return status variable */ - int32 fid; /* HDF-EOS file id */ + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 gdVgrpID; /* Grid root Vgroup ID */ + int32 size; /* Dimension size */ int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - char gridname[80] /* Grid name */ ; - - - /* Check for valid grid id */ - status = GDchkgdid(gridID, "GDdefinedim", - &fid, &sdInterfaceID, &gdVgrpID); + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char gridname[80]; /* Grid Name */ + char *utlstr; /* Utility string */ - /* Make sure dimension >= 0 */ - /* ------------------------ */ - if (dim < 0) + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) { - status = -1; - HEpush(DFE_GENAPP, "GDdefdim", __FILE__, __LINE__); - HEreport("Dimension value for \"%s\" less than zero: %d.\n", - dimname, dim); + HEpush(DFE_NOSPACE,"GDdiminfo", __FILE__, __LINE__); + return(-1); } + /* Initialize return value */ + /* ----------------------- */ + size = -1; + + + /* Check Grid ID */ + /* ------------- */ + status = GDchkgdid(gridID, "GDdiminfo", &fid, &sdInterfaceID, &gdVgrpID); - /* Write Dimension to Structural MetaData */ - /* -------------------------------------- */ if (status == 0) { + /* Get grid name */ + /* ------------- */ int gID = gridID % idOffset; if (gID >= NGRID) { - return -1; + free(utlstr); + return -1; } Vgetname(GDXGrid[gID].IDTable, gridname); - /* The cast to char* is somehow nasty since EHinsertmeta can modify */ - /* dimname, but not in this case since we call with metacode = 0 */ - int32 dimArray[1] = { dim }; - status = EHinsertmeta(sdInterfaceID, gridname, "g", 0L, - (char*) dimname, dimArray); - } - return (status); - -} -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDdefproj | -| | -| DESCRIPTION: Defines projection of grid. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 Grid structure ID | -| projcode int32 GCTP projection code | -| zonecode int32 UTM zone code | -| spherecode int32 GCTP spheriod code | -| projparm float64 Projection parameters | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Jun 00 Abe Taaheri Added support for EASE grid | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDdefproj(int32 gridID, int32 projcode, int32 zonecode, int32 spherecode, - float64 projparm[]) -{ - intn i; /* Loop index */ - intn projx; /* Projection table l_index */ - intn status = 0; /* routine return status variable */ - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 slen; /* String length */ - char utlbuf[1024]; /* Utility Buffer */ - char projparmbuf[512]; /* Projection parameter metadata - * string */ - char gridname[80]; /* Grid Name */ + /* Get pointers to "Dimension" section within SM */ + /* --------------------------------------------- */ + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + "Dimension", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } - /* Check for valid grid id */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDdefproj", &fid, &sdInterfaceID, &gdVgrpID); + /* Search for dimension name (surrounded by quotes) */ + /* ------------------------------------------------ */ + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", dimname, "\"\n"); + metaptrs[0] = strstr(metaptrs[0], utlstr); - if (status == 0) - { /* - * If projection not GEO, UTM, or State Code build projection - * parameter string + * If dimension found within grid structure then get dimension value */ - if (projcode != GCTP_GEO && - projcode != GCTP_UTM && - projcode != GCTP_SPCS) + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) { + /* Set endptr at end of dimension definition entry */ + /* ----------------------------------------------- */ + metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT"); - /* Begin projection parameter list with "(" */ - strcpy(projparmbuf, "("); + status = EHgetmetavalue(metaptrs, "Size", utlstr); - for (i = 0; i < 13; i++) + if (status == 0) { - char utlbufSmall[32]; - /* If projparm[i] = 0 ... */ - if (projparm[i] == 0.0) - { - strcpy(utlbufSmall, "0,"); - } - else - { - /* if projparm[i] is integer ... */ - if ((int32) projparm[i] == projparm[i]) - { - snprintf(utlbufSmall, sizeof(utlbufSmall), "%d%s", - (int) projparm[i], ","); - } - /* else projparm[i] is non-zero floating point ... */ - else - { - CPLsnprintf(utlbufSmall, sizeof(utlbufSmall), "%f%s", projparm[i], ","); - } - } - strcat(projparmbuf, utlbufSmall); + size = atoi(utlstr); + } + else + { + HEpush(DFE_GENAPP, "GDdiminfo", __FILE__, __LINE__); + HEreport("\"Size\" string not found in metadata.\n"); } - slen = (int)strlen(projparmbuf); - - /* Add trailing ")" */ - projparmbuf[slen - 1] = ')'; - } - - for (projx = 0; Projections[projx].projcode != -1; projx++) - { - if (projcode == Projections[projx].projcode) - { - break; - } - } - - - /* Build metadata string */ - /* --------------------- */ - if (projcode == GCTP_GEO) - { - snprintf(utlbuf, sizeof(utlbuf), - "%s%s%s", - "\t\tProjection=", Projections[projx].projname, "\n"); - } - else if (projcode == GCTP_UTM || projcode == GCTP_SPCS) - { - snprintf(utlbuf, sizeof(utlbuf), - "%s%s%s%s%d%s%s%d%s", - "\t\tProjection=", Projections[projx].projname, "\n", - "\t\tZoneCode=", (int)zonecode, "\n", - "\t\tSphereCode=", (int)spherecode, "\n"); } else { - snprintf(utlbuf, sizeof(utlbuf), - "%s%s%s%s%s%s%s%d%s", - "\t\tProjection=", Projections[projx].projname, "\n", - "\t\tProjParams=", projparmbuf, "\n", - "\t\tSphereCode=", (int)spherecode, "\n"); + HEpush(DFE_GENAPP, "GDdiminfo", __FILE__, __LINE__); + HEreport("Dimension \"%s\" not found.\n", dimname); } - - /* Insert in structural metadata */ - /* ----------------------------- */ - int gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - Vgetname(GDXGrid[gID].IDTable, gridname); - status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L, - utlbuf, NULL); + free(metabuf); } - - return (status); + free(utlstr); + return (size); } + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDblkSOMoffset | +| FUNCTION: GDgridinfo | | | -| DESCRIPTION: Writes Block SOM offset values | +| DESCRIPTION: Returns xdim, ydim and location of upper left and lower | +| right corners, in meters. | | | | | | Return Value Type Units Description | @@ -1161,84 +778,194 @@ GDdefproj(int32 gridID, int32 projcode, int32 zonecode, int32 spherecode, | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 Grid structure ID | -| offset float32 Offset values | -| count int32 Number of offset values | -| code char w/r code (w/r) | +| fid int32 File ID | +| gridname char Grid structure name | | | | OUTPUTS: | -| None | +| xdimsize int32 Number of columns in grid | +| ydimsize int32 Number of rows in grid | +| upleftpt float64 Location (m/deg) of upper left corner | +| lowrightpt float64 Location (m/deg) of lower right corner | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Sep 96 Joel Gales Original Programmer | -| Mar 99 David Wynne Changed data type of offset array from int32 to | -| float32, NCR 21197 | +| Jun 96 Joel Gales Original Programmer | +| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -GDblkSOMoffset(int32 gridID, float32 offset[], int32 count, const char *code) +GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, + float64 upleftpt[], float64 lowrightpt[]) + { intn status = 0; /* routine return status variable */ + intn statmeta = 0; /* EHgetmetavalue return status */ int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 gdVgrpID; /* Grid root Vgroup ID */ int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 projcode; /* GCTP projection code */ - float64 projparm[13]; /* Projection parameters */ - char utlbuf[128];/* Utility Buffer */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ char gridname[80]; /* Grid Name */ + char *utlstr; /* Utility string */ - /* Check for valid grid id */ - status = GDchkgdid(gridID, "GDblkSOMoffset", - &fid, &sdInterfaceID, &gdVgrpID); + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) + { + HEpush(DFE_NOSPACE,"GDgridinfo", __FILE__, __LINE__); + return(-1); + } + /* Check Grid ID */ + /* ------------- */ + status = GDchkgdid(gridID, "GDgridinfo", &fid, &sdInterfaceID, &gdVgrpID); if (status == 0) { - /* Get projection parameters */ - status = GDprojinfo(gridID, &projcode, NULL, NULL, projparm); + /* Get grid name */ + /* ------------- */ + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); + + + /* Get pointers to grid structure section within SM */ + /* ------------------------------------------------ */ + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + NULL, metaptrs); + + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } + + + /* Get xdimsize if requested */ + /* ------------------------- */ + if (xdimsize != NULL) + { + statmeta = EHgetmetavalue(metaptrs, "XDim", utlstr); + if (statmeta == 0) + { + *xdimsize = atoi(utlstr); + } + else + { + status = -1; + HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); + HEreport("\"XDim\" string not found in metadata.\n"); + } + } + + + /* Get ydimsize if requested */ + /* ------------------------- */ + if (ydimsize != NULL) + { + statmeta = EHgetmetavalue(metaptrs, "YDim", utlstr); + if (statmeta == 0) + { + *ydimsize = atoi(utlstr); + } + else + { + status = -1; + HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); + HEreport("\"YDim\" string not found in metadata.\n"); + } + } + - /* If SOM projection with projparm[11] non-zero ... */ - if (projcode == GCTP_SOM && projparm[11] != 0) + /* Get upleftpt if requested */ + /* ------------------------- */ + if (upleftpt != NULL) { - int gID = gridID % idOffset; - if (gID >= NGRID) + statmeta = EHgetmetavalue(metaptrs, "UpperLeftPointMtrs", utlstr); + if (statmeta == 0) + { + /* If value is "DEFAULT" then return zeros */ + /* --------------------------------------- */ + if (strcmp(utlstr, "DEFAULT") == 0) { - return -1; + upleftpt[0] = 0; + upleftpt[1] = 0; } - Vgetname(GDXGrid[gID].IDTable, gridname); - snprintf(utlbuf, sizeof(utlbuf),"%s%s", "_BLKSOM:", gridname); + else + { + sscanf(utlstr, "(%lf,%lf)", + &upleftpt[0], &upleftpt[1]); + } + } + else + { + status = -1; + HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); + HEreport( + "\"UpperLeftPointMtrs\" string not found in metadata.\n"); + } - /* Write offset values as attribute */ - if (strcmp(code, "w") == 0) + } + + /* Get lowrightpt if requested */ + /* --------------------------- */ + if (lowrightpt != NULL) + { + statmeta = EHgetmetavalue(metaptrs, "LowerRightMtrs", utlstr); + if (statmeta == 0) { - status = GDwriteattr(gridID, utlbuf, DFNT_FLOAT32, - count, offset); + /* If value is "DEFAULT" then return zeros */ + if (strcmp(utlstr, "DEFAULT") == 0) + { + lowrightpt[0] = 0; + lowrightpt[1] = 0; + } + else + { + sscanf(utlstr, "(%lf,%lf)", + &lowrightpt[0], &lowrightpt[1]); + } } - /* Read offset values from attribute */ - else if (strcmp(code, "r") == 0) + else { - status = GDreadattr(gridID, utlbuf, offset); + status = -1; + HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); + HEreport( + "\"LowerRightMtrs\" string not found in metadata.\n"); } } + + free(metabuf); } + free(utlstr); return (status); } + + + + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDdefcomp | +| FUNCTION: GDprojinfo | | | -| DESCRIPTION: Defines compression type and parameters | +| DESCRIPTION: Returns GCTP projection code, zone code, spheroid code | +| and projection parameters. | | | | | | Return Value Type Units Description | @@ -1246,174 +973,237 @@ GDblkSOMoffset(int32 gridID, float32 offset[], int32 count, const char *code) | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 grid structure ID | -| compcode int32 compression code | -| compparm intn compression parameters | +| gridID int32 Grid structure ID | | | | OUTPUTS: | -| None | +| projcode int32 GCTP projection code | +| zonecode int32 UTM zone code | +| spherecode int32 GCTP spheriod code | +| projparm float64 Projection parameters | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Sep 96 Joel Gales Original Programmer | +| Jun 96 Joel Gales Original Programmer | +| Oct 96 Joel Gales Add check for no projection code | +| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | +| Jun 00 Abe Taaheri Added support for EASE grid | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -GDdefcomp(int32 gridID, int32 compcode, intn compparm[]) +GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, + int32 * spherecode, float64 projparm[]) + { + intn i; /* Loop index */ + intn projx; /* Loop index */ intn status = 0; /* routine return status variable */ + intn statmeta = 0; /* EHgetmetavalue return status */ - int32 fid; /* HDF-EOS file id */ + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 gdVgrpID; /* Grid root Vgroup ID */ int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 gID; /* gridID - offset */ - - /* Check for valid grid id */ - status = GDchkgdid(gridID, "GDdefcomp", &fid, &sdInterfaceID, &gdVgrpID); - if (status == 0) - { - gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - /* Set compression code in compression external array */ - GDXGrid[gID].compcode = compcode; - switch (compcode) - { - /* Set NBIT compression parameters in compression external array */ - case HDFE_COMP_NBIT: + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char gridname[80]; /* Grid Name */ + char *utlstr; /* Utility string */ + char fmt[96]; /* Format String */ - GDXGrid[gID].compparm[0] = compparm[0]; - GDXGrid[gID].compparm[1] = compparm[1]; - GDXGrid[gID].compparm[2] = compparm[2]; - GDXGrid[gID].compparm[3] = compparm[3]; - - break; - - /* Set GZIP compression parameter in compression external array */ - case HDFE_COMP_DEFLATE: - - GDXGrid[gID].compparm[0] = compparm[0]; + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) + { + HEpush(DFE_NOSPACE,"GDprojinfo", __FILE__, __LINE__); + return(-1); + } - break; + /* Check Grid ID */ + /* ------------- */ + status = GDchkgdid(gridID, "GDprojinfo", &fid, &sdInterfaceID, &gdVgrpID); + if (status == 0) + { + /* Get grid name */ + /* ------------- */ + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; } - } - return (status); -} + Vgetname(GDXGrid[gID].IDTable, gridname); + /* Get pointers to grid structure section within SM */ + /* ------------------------------------------------ */ + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + NULL, metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDdeftile | -| | -| DESCRIPTION: Defines tiling parameters | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| tilecode int32 tile code | -| tilerank int32 number of tiling dimensions | -| tiledims int32 tiling dimensions | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jan 97 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDdeftile(int32 gridID, int32 tilecode, int32 tilerank, int32 tiledims[]) -{ - intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - int32 fid; /* HDF-EOS file id */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 gID; /* gridID - offset */ + /* Get projcode if requested */ + /* ------------------------- */ + if (projcode != NULL) + { + *projcode = -1; - /* Check for valid grid id */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDdeftile", &fid, &sdInterfaceID, &gdVgrpID); + statmeta = EHgetmetavalue(metaptrs, "Projection", utlstr); + if (statmeta == 0) + { + /* Loop through projection codes until found */ + /* ----------------------------------------- */ + for (projx = 0; Projections[projx].projcode != -1; projx++) + if (strcmp(utlstr, Projections[projx].projname) == 0) + break; + if (Projections[projx].projname != NULL) + *projcode = Projections[projx].projcode; + } + else + { + status = -1; + HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__); + HEreport("Projection Code not defined for \"%s\".\n", + gridname); + if (projparm != NULL) + { + for (i = 0; i < 13; i++) + { + projparm[i] = -1; + } + } + } + } - if (status == 0) - { - gID = gridID % idOffset; - if (gID >= NGRID) - return -1; - for (i = 0; i < 8; i++) + /* Get zonecode if requested */ + /* ------------------------- */ + if (projcode && zonecode != NULL) { - GDXGrid[gID].tiledims[i] = 0; + *zonecode = -1; + + + /* Zone code only relevant for UTM and State Code projections */ + /* ---------------------------------------------------------- */ + if (*projcode == GCTP_UTM || *projcode == GCTP_SPCS) + { + statmeta = EHgetmetavalue(metaptrs, "ZoneCode", utlstr); + if (statmeta == 0) + { + *zonecode = atoi(utlstr); + } + else + { + status = -1; + HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__); + HEreport("Zone Code not defined for \"%s\".\n", + gridname); + } + } } - GDXGrid[gID].tilecode = tilecode; - switch (tilecode) + /* Get projection parameters if requested */ + /* -------------------------------------- */ + if (projcode && projparm != NULL) { - case HDFE_NOTILE: - - GDXGrid[gID].tilerank = 0; - break; + /* + * Note: No projection parameters for GEO, UTM, and State Code + * projections + */ + if (*projcode == GCTP_GEO || *projcode == GCTP_UTM || + *projcode == GCTP_SPCS) + { + for (i = 0; i < 13; i++) + { + projparm[i] = 0.0; + } + } + else + { + statmeta = EHgetmetavalue(metaptrs, "ProjParams", utlstr); - case HDFE_TILE: + if (statmeta == 0) + { - GDXGrid[gID].tilerank = tilerank; + /* Build format string to read projection parameters */ + /* ------------------------------------------------- */ + strcpy(fmt, "%lf,"); + for (i = 1; i <= 11; i++) + strcat(fmt, "%lf,"); + strcat(fmt, "%lf"); - for (i = 0; i < tilerank; i++) - { - GDXGrid[gID].tiledims[i] = tiledims[i]; - if (GDXGrid[gID].tiledims[i] == 0) + /* Read parameters from numeric list */ + /* --------------------------------- */ + sscanf(&utlstr[1], fmt, + &projparm[0], &projparm[1], + &projparm[2], &projparm[3], + &projparm[4], &projparm[5], + &projparm[6], &projparm[7], + &projparm[8], &projparm[9], + &projparm[10], &projparm[11], + &projparm[12]); + } + else { - GDXGrid[gID].tiledims[i] = 1; + status = -1; + HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__); + HEreport("Projection parameters not defined for \"%s\".\n", + gridname); + } } + } - break; + /* Get spherecode if requested */ + /* --------------------------- */ + if (projcode && spherecode != NULL) + { + *spherecode = 0; + + /* Note: Spherecode not defined for GEO projection */ + /* ----------------------------------------------- */ + if ((*projcode != GCTP_GEO)) + { + EHgetmetavalue(metaptrs, "SphereCode", utlstr); + if (statmeta == 0) + { + *spherecode = atoi(utlstr); + } + } } - } + free(metabuf); + } + free(utlstr); return (status); } - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDdeforigin | +| FUNCTION: GDorigininfo | | | -| DESCRIPTION: Defines the origin of the grid data. | +| DESCRIPTION: Returns origin code | | | | | | Return Value Type Units Description | @@ -1421,16 +1211,11 @@ GDdeftile(int32 gridID, int32 tilecode, int32 tilerank, int32 tiledims[]) | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 grid structure ID | -| origincode int32 origin code | -| HDFE_GD_UL (0) | -| HDFE_GD_UR (1) | -| HDFE_GD_LL (2) | -| HDFE_GD_LR (3) | +| gridID int32 Grid structure ID | | | | | | OUTPUTS: | -| None | +| origincode int32 grid origin code | | | | NOTES: | | | @@ -1438,134 +1223,102 @@ GDdeftile(int32 gridID, int32 tilecode, int32 tilerank, int32 tiledims[]) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | +| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -GDdeforigin(int32 gridID, int32 origincode) +GDorigininfo(int32 gridID, int32 * origincode) { + intn i; /* Loop index */ intn status = 0; /* routine return status variable */ + intn statmeta = 0; /* EHgetmetavalue return status */ int32 fid; /* HDF-EOS file ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ + int32 gdVgrpID; /* Grid root Vgroup ID */ int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - char utlbuf[64]; /* Utility buffer */ - char gridname[80]; /* Grid name */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char gridname[80]; /* Grid Name */ + char *utlstr; /* Utility string */ - /* Check for valid grid id */ - status = GDchkgdid(gridID, "GDdeforigin", + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) + { + HEpush(DFE_NOSPACE,"GDorigininfo", __FILE__, __LINE__); + return(-1); + } + /* Check Grid ID */ + /* ------------- */ + status = GDchkgdid(gridID, "GDorigininfo", &fid, &sdInterfaceID, &gdVgrpID); + + /* Initialize pixreg code to -1 (in case of error) */ + /* ----------------------------------------------- */ + *origincode = -1; + if (status == 0) { - /* If proper origin code then write to structural metadata */ - /* ------------------------------------------------------- */ - if (origincode >= 0 && origincode < (int32)(sizeof(originNames) / sizeof(originNames[0]))) - { - int gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - snprintf(utlbuf, sizeof(utlbuf),"%s%s%s", - "\t\tGridOrigin=", originNames[origincode], "\n"); + /* Set default origin code */ + /* ----------------------- */ + *origincode = 0; - Vgetname(GDXGrid[gID].IDTable, gridname); - status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L, - utlbuf, NULL); - } - else + + /* Get grid name */ + /* ------------- */ + int gID = gridID % idOffset; + if (gID >= NGRID) { - status = -1; - HEpush(DFE_GENAPP, "GDdeforigin", __FILE__, __LINE__); - HEreport("Improper Grid Origin code: %d\n", origincode); + free(utlstr); + return -1; } - } - return (status); -} + Vgetname(GDXGrid[gID].IDTable, gridname); -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDdefpixreg | -| | -| DESCRIPTION: Defines pixel registration within grid cell. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| pixregcode int32 Pixel registration code | -| HDFE_CENTER (0) | -| HDFE_CORNER (1) | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDdefpixreg(int32 gridID, int32 pixregcode) -{ - intn status = 0; /* routine return status variable */ - int32 fid; /* HDF-EOS file ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + /* Get pointers to grid structure section within SM */ + /* ------------------------------------------------ */ + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + NULL, metaptrs); - char utlbuf[64]; /* Utility buffer */ - char gridname[80]; /* Grid name */ + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } - /* Check for valid grid id */ - status = GDchkgdid(gridID, "GDdefpixreg", - &fid, &sdInterfaceID, &gdVgrpID); - if (status == 0) - { - /* If proper pix reg code then write to structural metadata */ - /* -------------------------------------------------------- */ - if (pixregcode >= 0 && pixregcode < (int32)(sizeof(pixregNames) / sizeof(pixregNames[0]))) + statmeta = EHgetmetavalue(metaptrs, "GridOrigin", utlstr); + + if (statmeta == 0) { - int gID = gridID % idOffset; - if (gID >= NGRID) + /* + * If "GridOrigin" string found in metadata then convert to + * numeric origin code (fixed added: Jan 97) + */ + for (i = 0; i < (intn)(sizeof(originNames) / sizeof(originNames[0])); i++) + { + if (strcmp(utlstr, originNames[i]) == 0) { - return -1; + *origincode = i; + break; } - snprintf(utlbuf, sizeof(utlbuf),"%s%s%s", - "\t\tPixelRegistration=", pixregNames[pixregcode], "\n"); - - Vgetname(GDXGrid[gID].IDTable, gridname); - status = EHinsertmeta(sdInterfaceID, gridname, "g", 101L, - utlbuf, NULL); - } - else - { - status = -1; - HEpush(DFE_GENAPP, "GDdefpixreg", __FILE__, __LINE__); - HEreport("Improper Pixel Registration code: %d\n", pixregcode); + } } - } + free(metabuf); + } + free(utlstr); return (status); - } -#endif + + @@ -1573,18 +1326,21 @@ GDdefpixreg(int32 gridID, int32 pixregcode) /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDdiminfo | +| FUNCTION: GDpixreginfo | | | -| DESCRIPTION: Retrieve size of specified dimension. | +| DESCRIPTION: | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| size int32 Size of dimension | +| status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 grid structure id | -| dimname char Dimension name | +| gridID int32 Grid structure ID | +| | +| | +| OUTPUTS: | +| pixregcode int32 Pixel registration code | | | | | | OUTPUTS: | @@ -1596,21 +1352,20 @@ GDdefpixreg(int32 gridID, int32 pixregcode) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -int32 -GDdiminfo(int32 gridID, const char *dimname) - +intn +GDpixreginfo(int32 gridID, int32 * pixregcode) { - intn status; /* routine return status variable */ + intn i; /* Loop index */ + intn status = 0; /* routine return status variable */ + intn statmeta = 0; /* EHgetmetavalue return status */ int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 size; /* Dimension size */ int32 idOffset = GDIDOFFSET; /* Grid ID offset */ @@ -1624,36 +1379,34 @@ GDdiminfo(int32 gridID, const char *dimname) utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) { - HEpush(DFE_NOSPACE,"GDdiminfo", __FILE__, __LINE__); + HEpush(DFE_NOSPACE,"GDpixreginfo", __FILE__, __LINE__); return(-1); } - /* Initialize return value */ - /* ----------------------- */ - size = -1; - - /* Check Grid ID */ - /* ------------- */ - status = GDchkgdid(gridID, "GDdiminfo", &fid, &sdInterfaceID, &gdVgrpID); + status = GDchkgdid(gridID, "GDpixreginfo", + &fid, &sdInterfaceID, &gdVgrpID); + /* Initialize pixreg code to -1 (in case of error) */ + *pixregcode = -1; if (status == 0) { + /* Set default pixreg code */ + *pixregcode = 0; + /* Get grid name */ - /* ------------- */ int gID = gridID % idOffset; if (gID >= NGRID) { free(utlstr); return -1; } - Vgetname(GDXGrid[gID].IDTable, gridname); + Vgetname(GDXGrid[gID].IDTable, gridname); - /* Get pointers to "Dimension" section within SM */ - /* --------------------------------------------- */ + /* Get pointers to grid structure section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "Dimension", metaptrs); + NULL, metaptrs); if(metabuf == NULL) { @@ -1661,86 +1414,68 @@ GDdiminfo(int32 gridID, const char *dimname) return(-1); } - /* Search for dimension name (surrounded by quotes) */ - /* ------------------------------------------------ */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", dimname, "\"\n"); - metaptrs[0] = strstr(metaptrs[0], utlstr); - /* - * If dimension found within grid structure then get dimension value - */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - /* Set endptr at end of dimension definition entry */ - /* ----------------------------------------------- */ - metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT"); + statmeta = EHgetmetavalue(metaptrs, "PixelRegistration", utlstr); - status = EHgetmetavalue(metaptrs, "Size", utlstr); + if (statmeta == 0) + { + /* + * If "PixelRegistration" string found in metadata then convert + * to numeric origin code (fixed added: Jan 97) + */ - if (status == 0) - { - size = atoi(utlstr); - } - else + for (i = 0; i < (intn)(sizeof(pixregNames) / sizeof(pixregNames[0])); i++) { - HEpush(DFE_GENAPP, "GDdiminfo", __FILE__, __LINE__); - HEreport("\"Size\" string not found in metadata.\n"); + if (strcmp(utlstr, pixregNames[i]) == 0) + { + *pixregcode = i; + break; + } } } - else - { - HEpush(DFE_GENAPP, "GDdiminfo", __FILE__, __LINE__); - HEreport("Dimension \"%s\" not found.\n", dimname); - } - free(metabuf); } free(utlstr); - return (size); + return (status); } - - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDgridinfo | +| FUNCTION: GDcompinfo | | | -| DESCRIPTION: Returns xdim, ydim and location of upper left and lower | -| right corners, in meters. | +| DESCRIPTION: | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| status intn | | | | INPUTS: | -| fid int32 File ID | -| gridname char Grid structure name | +| gridID int32 | +| compcode int32 | +| compparm intn | +| | | | | OUTPUTS: | -| xdimsize int32 Number of columns in grid | -| ydimsize int32 Number of rows in grid | -| upleftpt float64 Location (m/deg) of upper left corner | -| lowrightpt float64 Location (m/deg) of lower right corner | +| None | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | +| Oct 96 Joel Gales Original Programmer | | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, - float64 upleftpt[], float64 lowrightpt[]) - +GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[]) { + intn i; /* Loop index */ intn status = 0; /* routine return status variable */ intn statmeta = 0; /* EHgetmetavalue return status */ @@ -1753,38 +1488,34 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, char *metabuf; /* Pointer to structural metadata (SM) */ char *metaptrs[2];/* Pointers to begin and end of SM section */ char gridname[80]; /* Grid Name */ - char *utlstr; /* Utility string */ + char *utlstr;/* Utility string */ /* Allocate space for utility string */ /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) { - HEpush(DFE_NOSPACE,"GDgridinfo", __FILE__, __LINE__); + HEpush(DFE_NOSPACE,"GDcompinfo", __FILE__, __LINE__); return(-1); } /* Check Grid ID */ - /* ------------- */ - status = GDchkgdid(gridID, "GDgridinfo", &fid, &sdInterfaceID, &gdVgrpID); + status = GDchkgdid(gridID, "GDcompinfo", &fid, &sdInterfaceID, &gdVgrpID); + if (status == 0) { /* Get grid name */ - /* ------------- */ int gID = gridID % idOffset; if (gID >= NGRID) { - free(utlstr); - return -1; + free(utlstr); + return -1; } Vgetname(GDXGrid[gID].IDTable, gridname); - - /* Get pointers to grid structure section within SM */ - /* ------------------------------------------------ */ + /* Get pointers to "DataField" section within SM */ metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - NULL, metaptrs); - + "DataField", metaptrs); if(metabuf == NULL) { free(utlstr); @@ -1792,101 +1523,99 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, } - /* Get xdimsize if requested */ - /* ------------------------- */ - if (xdimsize != NULL) - { - statmeta = EHgetmetavalue(metaptrs, "XDim", utlstr); - if (statmeta == 0) - { - *xdimsize = atoi(utlstr); - } - else - { - status = -1; - HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); - HEreport("\"XDim\" string not found in metadata.\n"); - } - } + /* Search for field */ + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n"); + metaptrs[0] = strstr(metaptrs[0], utlstr); - /* Get ydimsize if requested */ - /* ------------------------- */ - if (ydimsize != NULL) + /* If field found and user wants compression code ... */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) { - statmeta = EHgetmetavalue(metaptrs, "YDim", utlstr); - if (statmeta == 0) - { - *ydimsize = atoi(utlstr); - } - else + if (compcode != NULL) { - status = -1; - HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); - HEreport("\"YDim\" string not found in metadata.\n"); - } - } + /* Set endptr at end of field's definition entry */ + metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT"); + /* Get compression type */ + statmeta = EHgetmetavalue(metaptrs, "CompressionType", utlstr); - /* Get upleftpt if requested */ - /* ------------------------- */ - if (upleftpt != NULL) - { - statmeta = EHgetmetavalue(metaptrs, "UpperLeftPointMtrs", utlstr); - if (statmeta == 0) - { - /* If value is "DEFAULT" then return zeros */ - /* --------------------------------------- */ - if (strcmp(utlstr, "DEFAULT") == 0) - { - upleftpt[0] = 0; - upleftpt[1] = 0; - } - else + /* + * Default is no compression if "CompressionType" string not + * in metadata + */ + *compcode = HDFE_COMP_NONE; + + /* If compression code is found ... */ + if (statmeta == 0) { - sscanf(utlstr, "(%lf,%lf)", - &upleftpt[0], &upleftpt[1]); + /* Loop through compression types until match */ + for (i = 0; i < (intn)(sizeof(HDFcomp) / sizeof(HDFcomp[0])); i++) + { + if (strcmp(utlstr, HDFcomp[i]) == 0) + { + *compcode = i; + break; + } + } } } - else - { - status = -1; - HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); - HEreport( - "\"UpperLeftPointMtrs\" string not found in metadata.\n"); - } - - } - /* Get lowrightpt if requested */ - /* --------------------------- */ - if (lowrightpt != NULL) - { - statmeta = EHgetmetavalue(metaptrs, "LowerRightMtrs", utlstr); - if (statmeta == 0) + /* If user wants compression parameters ... */ + if (compparm != NULL && compcode != NULL) { - /* If value is "DEFAULT" then return zeros */ - if (strcmp(utlstr, "DEFAULT") == 0) + /* Initialize to zero */ + for (i = 0; i < 4; i++) { - lowrightpt[0] = 0; - lowrightpt[1] = 0; + compparm[i] = 0; } - else + + /* + * Get compression parameters if NBIT or DEFLATE compression + */ + if (*compcode == HDFE_COMP_NBIT) { - sscanf(utlstr, "(%lf,%lf)", - &lowrightpt[0], &lowrightpt[1]); + statmeta = + EHgetmetavalue(metaptrs, "CompressionParams", utlstr); + if (statmeta == 0) + { + sscanf(utlstr, "(%d,%d,%d,%d)", + &compparm[0], &compparm[1], + &compparm[2], &compparm[3]); + } + else + { + status = -1; + HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__); + HEreport( + "\"CompressionParams\" string not found in metadata.\n"); + } + } + else if (*compcode == HDFE_COMP_DEFLATE) + { + statmeta = + EHgetmetavalue(metaptrs, "DeflateLevel", utlstr); + if (statmeta == 0) + { + sscanf(utlstr, "%d", &compparm[0]); + } + else + { + status = -1; + HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__); + HEreport( + "\"DeflateLevel\" string not found in metadata.\n"); + } } } - else - { - status = -1; - HEpush(DFE_GENAPP, "GDgridinfo", __FILE__, __LINE__); - HEreport( - "\"LowerRightMtrs\" string not found in metadata.\n"); - } + } + else + { + HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__); + HEreport("Fieldname \"%s\" not found.\n", fieldname); } free(metabuf); + } free(utlstr); return (status); @@ -1897,14 +1626,13 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDprojinfo | +| FUNCTION: GDfieldinfo | | | -| DESCRIPTION: Returns GCTP projection code, zone code, spheroid code | -| and projection parameters. | +| DESCRIPTION: Retrieve information about a specific geolocation or data | +| field in the grid. | | | | | | Return Value Type Units Description | @@ -1912,13 +1640,19 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 Grid structure ID | +| gridID int32 grid structure id | +| fieldname char name of field | +| | | | | OUTPUTS: | -| projcode int32 GCTP projection code | -| zonecode int32 UTM zone code | -| spherecode int32 GCTP spheriod code | -| projparm float64 Projection parameters | +| rank int32 rank of field (# of dims) | +| dims int32 field dimensions | +| numbertype int32 field number type | +| dimlist char field dimension list | +| | +| | +| OUTPUTS: | +| None | | | | NOTES: | | | @@ -1926,66 +1660,65 @@ GDgridinfo(int32 gridID, int32 * xdimsize, int32 * ydimsize, | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Add check for no projection code | +| Aug 96 Joel Gales Make metadata ODL compliant | | Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | -| Jun 00 Abe Taaheri Added support for EASE grid | +| Feb 99 Abe Taaheri Changed memcpy to memmove to avoid overlapping | +| problem when copying strings | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, - int32 * spherecode, float64 projparm[]) +GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], + int32 * numbertype, char *dimlist) { - intn i; /* Loop index */ - intn projx; /* Loop index */ - intn status = 0; /* routine return status variable */ - intn statmeta = 0; /* EHgetmetavalue return status */ + intn i; /* Loop index */ + intn status; /* routine return status variable */ + intn statmeta = 0; /* EHgetmetavalue return status */ - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 fid; /* HDF-EOS file ID */ + int32 sdInterfaceID; /* HDF SDS interface ID */ + int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 ndims = 0; /* Number of dimensions */ + int32 slen[8]; /* Length of each entry in parsed string */ + int32 dum; /* Dummy variable */ + int32 xdim; /* X dim size */ + int32 ydim; /* Y dim size */ + int32 sdid; /* SDS id */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2]; /* Pointers to begin and end of SM section */ + char gridname[80]; /* Grid Name */ + char *utlstr; /* Utility string */ + char *ptr[8]; /* String pointers for parsed string */ + char dimstr[64]; /* Individual dimension entry string */ - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr; /* Utility string */ - char fmt[96]; /* Format String */ /* Allocate space for utility string */ /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) { - HEpush(DFE_NOSPACE,"GDprojinfo", __FILE__, __LINE__); + HEpush(DFE_NOSPACE,"GDfieldinfo", __FILE__, __LINE__); return(-1); } + *rank = -1; + *numbertype = -1; - /* Check Grid ID */ - /* ------------- */ - status = GDchkgdid(gridID, "GDprojinfo", &fid, &sdInterfaceID, &gdVgrpID); + status = GDchkgdid(gridID, "GDfieldinfo", &fid, &sdInterfaceID, &dum); if (status == 0) { - /* Get grid name */ - /* ------------- */ int gID = gridID % idOffset; if (gID >= NGRID) { - free(utlstr); - return -1; - } - + free(utlstr); + return -1; + } Vgetname(GDXGrid[gID].IDTable, gridname); - - /* Get pointers to grid structure section within SM */ - /* ------------------------------------------------ */ metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - NULL, metaptrs); - + "DataField", metaptrs); if(metabuf == NULL) { free(utlstr); @@ -1993,143 +1726,110 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, } - /* Get projcode if requested */ - /* ------------------------- */ - if (projcode != NULL) + /* Search for field */ + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n"); + metaptrs[0] = strstr(metaptrs[0], utlstr); + + /* If field found ... */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) { - *projcode = -1; - statmeta = EHgetmetavalue(metaptrs, "Projection", utlstr); + /* Set endptr at end of dimension definition entry */ + metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT"); + + /* Get DataType string */ + statmeta = EHgetmetavalue(metaptrs, "DataType", utlstr); + + /* Convert to numbertype code */ if (statmeta == 0) + *numbertype = EHnumstr(utlstr); + else { - /* Loop through projection codes until found */ - /* ----------------------------------------- */ - for (projx = 0; Projections[projx].projcode != -1; projx++) - if (strcmp(utlstr, Projections[projx].projname) == 0) - break; - if (Projections[projx].projname != NULL) - *projcode = Projections[projx].projcode; + status = -1; + HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__); + HEreport("\"DataType\" string not found in metadata.\n"); + } + + /* + * Get DimList string and trim off leading and trailing parens + * "()" + */ + statmeta = EHgetmetavalue(metaptrs, "DimList", utlstr); + + if (statmeta == 0) + { + memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); + utlstr[strlen(utlstr) - 2] = 0; + + /* Parse trimmed DimList string and get rank */ + ndims = EHparsestr(utlstr, ',', ptr, slen); + *rank = ndims; } else { status = -1; - HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__); - HEreport("Projection Code not defined for \"%s\".\n", - gridname); + HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__); + HEreport("\"DimList\" string not found in metadata.\n"); + } - if (projparm != NULL) + + if (status == 0) + { + status = GDgridinfo(gridID, &xdim, &ydim, NULL, NULL); + + for (i = 0; i < ndims; i++) { - for (i = 0; i < 13; i++) + memcpy(dimstr, ptr[i] + 1, slen[i] - 2); + dimstr[slen[i] - 2] = 0; + + if (strcmp(dimstr, "XDim") == 0) { - projparm[i] = -1; + dims[i] = xdim; + } + else if (strcmp(dimstr, "YDim") == 0) + { + dims[i] = ydim; + } + else + { + dims[i] = GDdiminfo(gridID, dimstr); } - } - } - } - - - /* Get zonecode if requested */ - /* ------------------------- */ - if (projcode && zonecode != NULL) - { - *zonecode = -1; - - - /* Zone code only relevant for UTM and State Code projections */ - /* ---------------------------------------------------------- */ - if (*projcode == GCTP_UTM || *projcode == GCTP_SPCS) - { - statmeta = EHgetmetavalue(metaptrs, "ZoneCode", utlstr); - if (statmeta == 0) - { - *zonecode = atoi(utlstr); - } - else - { - status = -1; - HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__); - HEreport("Zone Code not defined for \"%s\".\n", - gridname); - } - } - } - /* Get projection parameters if requested */ - /* -------------------------------------- */ - if (projcode && projparm != NULL) - { + if (dimlist != NULL) + { + if (i == 0) + { + dimlist[0] = 0; + } - /* - * Note: No projection parameters for GEO, UTM, and State Code - * projections - */ - if (*projcode == GCTP_GEO || *projcode == GCTP_UTM || - *projcode == GCTP_SPCS) - { - for (i = 0; i < 13; i++) - { - projparm[i] = 0.0; + if (i > 0) + { + strcat(dimlist, ","); + } + strcat(dimlist, dimstr); + } } - } - else - { - statmeta = EHgetmetavalue(metaptrs, "ProjParams", utlstr); - - if (statmeta == 0) - { - - /* Build format string to read projection parameters */ - /* ------------------------------------------------- */ - strcpy(fmt, "%lf,"); - for (i = 1; i <= 11; i++) - strcat(fmt, "%lf,"); - strcat(fmt, "%lf"); - - /* Read parameters from numeric list */ - /* --------------------------------- */ - sscanf(&utlstr[1], fmt, - &projparm[0], &projparm[1], - &projparm[2], &projparm[3], - &projparm[4], &projparm[5], - &projparm[6], &projparm[7], - &projparm[8], &projparm[9], - &projparm[10], &projparm[11], - &projparm[12]); - } - else + if (dims[0] == 0) { - status = -1; - HEpush(DFE_GENAPP, "GDprojinfo", __FILE__, __LINE__); - HEreport("Projection parameters not defined for \"%s\".\n", - gridname); - + status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, + &sdid, &dum, &dum, &dum, dims, + &dum); } } } - - /* Get spherecode if requested */ - /* --------------------------- */ - if (projcode && spherecode != NULL) - { - *spherecode = 0; - - /* Note: Spherecode not defined for GEO projection */ - /* ----------------------------------------------- */ - if ((*projcode != GCTP_GEO)) - { - EHgetmetavalue(metaptrs, "SphereCode", utlstr); - if (statmeta == 0) - { - *spherecode = atoi(utlstr); - } - } - } free(metabuf); + } + + if (*rank == -1) + { + status = -1; + HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__); + HEreport("Fieldname \"%s\" not found.\n", fieldname); } free(utlstr); return (status); @@ -2140,9 +1840,9 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDorigininfo | +| FUNCTION: GDSDfldsrch | | | -| DESCRIPTION: Returns origin code | +| DESCRIPTION: Retrieves information from SDS fields | | | | | | Return Value Type Units Description | @@ -2150,11 +1850,18 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 Grid structure ID | +| gridID int32 grid structure ID | +| sdInterfaceID int32 SD interface ID | +| fieldname char field name | | | | | | OUTPUTS: | -| origincode int32 grid origin code | +| sdid int32 SD element ID | +| rankSDS int32 Rank of SDS | +| rankFld int32 True rank of field (merging) | +| offset int32 Offset of field within merged field | +| dims int32 Dimensions of field | +| solo int32 Solo field flag | | | | NOTES: | | | @@ -2162,96 +1869,204 @@ GDprojinfo(int32 gridID, int32 * projcode, int32 * zonecode, | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | +| Aug 96 Joel Gales Make metadata ODL compliant | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -GDorigininfo(int32 gridID, int32 * origincode) +static intn +GDSDfldsrch(int32 gridID, int32 sdInterfaceID, const char *fieldname, + int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset, + int32 dims[], int32 * solo) { intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - intn statmeta = 0; /* EHgetmetavalue return status */ + intn status = -1;/* routine return status variable */ - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ + int32 gID; /* GridID - offset */ int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 dum; /* Dummy variable */ + int32 dums[128]; /* Dummy array */ + int32 attrIndex; /* Attribute l_index */ - + char name[2048]; /* Merged-Field Names */ + char gridname[80]; /* Grid Name */ + char *utlstr;/* Utility string */ char *metabuf; /* Pointer to structural metadata (SM) */ char *metaptrs[2];/* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr; /* Utility string */ +#ifdef broken_logic + char *oldmetaptr; /* Pointer within SM section */ + char *metaptr; /* Pointer within SM section */ +#endif /* Allocate space for utility string */ /* --------------------------------- */ utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); if(utlstr == NULL) { - HEpush(DFE_NOSPACE,"GDorigininfo", __FILE__, __LINE__); + HEpush(DFE_NOSPACE,"GDSDfldsrch", __FILE__, __LINE__); return(-1); } - /* Check Grid ID */ - /* ------------- */ - status = GDchkgdid(gridID, "GDorigininfo", - &fid, &sdInterfaceID, &gdVgrpID); + /* Set solo flag to 0 (no) */ + /* ----------------------- */ + *solo = 0; - /* Initialize pixreg code to -1 (in case of error) */ - /* ----------------------------------------------- */ - *origincode = -1; + /* Compute "reduced" grid ID */ + /* ------------------------- */ + gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } - if (status == 0) + /* Loop through all SDSs in grid */ + /* ----------------------------- */ + for (i = 0; i < GDXGrid[gID].nSDS; i++) { - /* Set default origin code */ - /* ----------------------- */ - *origincode = 0; + /* If active SDS ... */ + /* ----------------- */ + if (GDXGrid[gID].sdsID[i] != 0) + { + /* Get SDS ID, name, rankSDS, and dimensions */ + /* ----------------------------------------- */ + *sdid = GDXGrid[gID].sdsID[i]; + SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum); + *rankFld = *rankSDS; - /* Get grid name */ - /* ------------- */ - int gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlstr); - return -1; - } + /* If merged field ... */ + /* ------------------- */ + if (strstr(name, "MRGFLD_") == &name[0]) + { + /* Get grid name */ + /* ------------- */ + Vgetname(GDXGrid[gID].IDTable, gridname); - Vgetname(GDXGrid[gID].IDTable, gridname); + /* Get pointers to "MergedFields" section within SM */ + /* ------------------------------------------------ */ + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + "MergedFields", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } - /* Get pointers to grid structure section within SM */ - /* ------------------------------------------------ */ - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - NULL, metaptrs); +#ifdef broken_logic + /* Initialize metaptr to beg. of section */ + /* ------------------------------------- */ + metaptr = metaptrs[0]; - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } + /* Store metaptr in order to recover */ + /* --------------------------------- */ + oldmetaptr = metaptr; - statmeta = EHgetmetavalue(metaptrs, "GridOrigin", utlstr); - if (statmeta == 0) - { - /* - * If "GridOrigin" string found in metadata then convert to - * numeric origin code (fixed added: Jan 97) - */ - for (i = 0; i < (intn)(sizeof(originNames) / sizeof(originNames[0])); i++) - { - if (strcmp(utlstr, originNames[i]) == 0) - { - *origincode = i; - break; + /* Search for Merged field name */ + /* ---------------------------- */ + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "MergedFieldName=\"", + name, "\"\n"); + metaptr = strstr(metaptr, utlstr); + + + /* If not found check for old metadata */ + /* ----------------------------------- */ + if (metaptr == NULL) + { + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "OBJECT=\"", name, "\"\n"); + metaptr = strstr(oldmetaptr, utlstr); + } +#endif + + /* Get field list and strip off leading and trailing quotes */ + /* -------------------------------------------------------- */ + EHgetmetavalue(metaptrs, "FieldList", name); + memmove(name, name + 1, strlen(name) - 2); + name[strlen(name) - 2] = 0; + + + /* Search for desired field within merged field list */ + /* ------------------------------------------------- */ + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\""); + dum = EHstrwithin(utlstr, name, ','); + + free(metabuf); + } + else + { + /* If solo (unmerged) check if SDS name matches fieldname */ + /* ------------------------------------------------------ */ + dum = EHstrwithin(fieldname, name, ','); + if (dum != -1) + { + *solo = 1; + *offset = 0; } } - } - free(metabuf); + + + /* If field found ... */ + /* ------------------ */ + if (dum != -1) + { + status = 0; + + /* If merged field ... */ + /* ------------------- */ + if (*solo == 0) + { + /* Get "Field Offsets" SDS attribute l_index */ + /* --------------------------------------- */ + attrIndex = SDfindattr(*sdid, "Field Offsets"); + + /* + * If attribute exists then get offset of desired field + * within merged field + */ + if (attrIndex != -1) + { + SDreadattr(*sdid, attrIndex, (VOIDP) dums); + *offset = dums[dum]; + } + + + /* Get "Field Dims" SDS attribute l_index */ + /* ------------------------------------ */ + attrIndex = SDfindattr(*sdid, "Field Dims"); + + /* + * If attribute exists then get 0th dimension of desired + * field within merged field + */ + if (attrIndex != -1) + { + SDreadattr(*sdid, attrIndex, (VOIDP) dums); + dims[0] = dums[dum]; + + /* If this dimension = 1 then field is really 2 dim */ + /* ------------------------------------------------ */ + if (dums[dum] == 1) + { + *rankFld = 2; + } + } + } + + + /* Break out of SDS loop */ + /* --------------------- */ + break; + } /* End of found field section */ + } + else + { + /* First non-active SDS signifies no more, break out of SDS loop */ + /* ------------------------------------------------------------- */ + break; + } } free(utlstr); return (status); @@ -2260,14 +2075,12 @@ GDorigininfo(int32 gridID, int32 * origincode) - - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDpixreginfo | +| FUNCTION: GDwrrdfield | | | -| DESCRIPTION: | +| DESCRIPTION: Writes/Reads fields | | | | | | Return Value Type Units Description | @@ -2275,15 +2088,18 @@ GDorigininfo(int32 gridID, int32 * origincode) | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 Grid structure ID | +| gridID int32 grid structure ID | +| fieldname char fieldname | +| code char Write/Read code (w/r) | +| start int32 start array | +| stride int32 stride array | +| edge int32 edge array | +| datbuf void data buffer for read | | | | | | OUTPUTS: | -| pixregcode int32 Pixel registration code | -| | +| datbuf void data buffer for write | | | -| OUTPUTS: | -| None | | | | NOTES: | | | @@ -2291,287 +2107,246 @@ GDorigininfo(int32 gridID, int32 * origincode) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | +| Feb 97 Joel Gales Stride = 1 HDF compression workaround | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -GDpixreginfo(int32 gridID, int32 * pixregcode) +static intn +GDwrrdfield(int32 gridID, const char *fieldname, const char *code, + int32 start[], int32 stride[], int32 edge[], VOIDP datbuf) + { intn i; /* Loop index */ intn status = 0; /* routine return status variable */ - intn statmeta = 0; /* EHgetmetavalue return status */ int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 sdid; /* SDS ID */ + int32 dum; /* Dummy variable */ + int32 rankSDS; /* Rank of SDS */ + int32 rankFld; /* Rank of field */ + int32 offset[8]; /* I/O offset (start) */ + int32 incr[8]; /* I/O increment (stride) */ + int32 count[8]; /* I/O count (edge) */ + int32 dims[8]; /* Field/SDS dimensions */ + int32 mrgOffset; /* Merged field offset */ + int32 strideOne; /* Strides = 1 flag */ - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr; /* Utility string */ - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"GDpixreginfo", __FILE__, __LINE__); - return(-1); - } - /* Check Grid ID */ - status = GDchkgdid(gridID, "GDpixreginfo", - &fid, &sdInterfaceID, &gdVgrpID); + /* Check for valid grid ID */ + /* ----------------------- */ + status = GDchkgdid(gridID, "GDwrrdfield", &fid, &sdInterfaceID, &dum); - /* Initialize pixreg code to -1 (in case of error) */ - *pixregcode = -1; if (status == 0) { - /* Set default pixreg code */ - *pixregcode = 0; + /* Check that field exists */ + /* ----------------------- */ + status = GDfieldinfo(gridID, fieldname, &rankSDS, dims, &dum, NULL); - /* Get grid name */ - int gID = gridID % idOffset; - if (gID >= NGRID) + + if (status != 0) { - free(utlstr); - return -1; - } + HEpush(DFE_GENAPP, "GDwrrdfield", __FILE__, __LINE__); + HEreport("Fieldname \"%s\" does not exist.\n", fieldname); + status = -1; - Vgetname(GDXGrid[gID].IDTable, gridname); + } - /* Get pointers to grid structure section within SM */ - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - NULL, metaptrs); - if(metabuf == NULL) + if (status == 0) { - free(utlstr); - return(-1); - } + status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid, + &rankSDS, &rankFld, &mrgOffset, dims, &dum); - statmeta = EHgetmetavalue(metaptrs, "PixelRegistration", utlstr); + /* Set I/O offset Section */ + /* ---------------------- */ - if (statmeta == 0) - { /* - * If "PixelRegistration" string found in metadata then convert - * to numeric origin code (fixed added: Jan 97) + * If start == NULL (default) set I/O offset of 0th field to + * offset within merged field (if any) and the rest to 0 */ - - for (i = 0; i < (intn)(sizeof(pixregNames) / sizeof(pixregNames[0])); i++) + if (start == NULL) { - if (strcmp(utlstr, pixregNames[i]) == 0) + for (i = 0; i < rankSDS; i++) { - *pixregcode = i; - break; + offset[i] = 0; + } + offset[0] = mrgOffset; + } + else + { + /* + * ... otherwise set I/O offset to user values, adjusting the + * 0th field with the merged field offset (if any) + */ + if (rankFld == rankSDS) + { + for (i = 0; i < rankSDS; i++) + { + offset[i] = start[i]; + } + offset[0] += mrgOffset; + } + else + { + /* + * If field really 2-dim merged in 3-dim field then set + * 0th field offset to merge offset and then next two to + * the user values + */ + for (i = 0; i < rankFld; i++) + { + offset[i + 1] = start[i]; + } + offset[0] = mrgOffset; } } - } - free(metabuf); - } - free(utlstr); - return (status); -} -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDcompinfo | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn | -| | -| INPUTS: | -| gridID int32 | -| compcode int32 | -| compparm intn | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Oct 96 Joel Gales Original Programmer | -| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[]) -{ - intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - intn statmeta = 0; /* EHgetmetavalue return status */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr;/* Utility string */ - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"GDcompinfo", __FILE__, __LINE__); - return(-1); - } - /* Check Grid ID */ - status = GDchkgdid(gridID, "GDcompinfo", &fid, &sdInterfaceID, &gdVgrpID); - - - if (status == 0) - { - /* Get grid name */ - int gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlstr); - return -1; - } - Vgetname(GDXGrid[gID].IDTable, gridname); - - /* Get pointers to "DataField" section within SM */ - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "DataField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - - - /* Search for field */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n"); - metaptrs[0] = strstr(metaptrs[0], utlstr); - + /* Set I/O stride Section */ + /* ---------------------- */ - /* If field found and user wants compression code ... */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - if (compcode != NULL) + /* + * If stride == NULL (default) set I/O stride to 1 + */ + if (stride == NULL) + { + for (i = 0; i < rankSDS; i++) + { + incr[i] = 1; + } + } + else { - /* Set endptr at end of field's definition entry */ - metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT"); - - /* Get compression type */ - statmeta = EHgetmetavalue(metaptrs, "CompressionType", utlstr); - /* - * Default is no compression if "CompressionType" string not - * in metadata + * ... otherwise set I/O stride to user values */ - *compcode = HDFE_COMP_NONE; - - /* If compression code is found ... */ - if (statmeta == 0) + if (rankFld == rankSDS) { - /* Loop through compression types until match */ - for (i = 0; i < (intn)(sizeof(HDFcomp) / sizeof(HDFcomp[0])); i++) + for (i = 0; i < rankSDS; i++) { - if (strcmp(utlstr, HDFcomp[i]) == 0) - { - *compcode = i; - break; - } + incr[i] = stride[i]; + } + } + else + { + /* + * If field really 2-dim merged in 3-dim field then set + * 0th field stride to 1 and then next two to the user + * values. + */ + for (i = 0; i < rankFld; i++) + { + incr[i + 1] = stride[i]; } + incr[0] = 1; } } - /* If user wants compression parameters ... */ - if (compparm != NULL && compcode != NULL) + + + /* Set I/O count Section */ + /* --------------------- */ + + /* + * If edge == NULL (default) set I/O count to number of remaining + * entries (dims - start) / increment. Note that 0th field + * offset corrected for merged field offset (if any). + */ + if (edge == NULL) { - /* Initialize to zero */ - for (i = 0; i < 4; i++) + for (i = 1; i < rankSDS; i++) { - compparm[i] = 0; + count[i] = (dims[i] - offset[i]) / incr[i]; } - + count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0]; + } + else + { /* - * Get compression parameters if NBIT or DEFLATE compression + * ... otherwise set I/O count to user values */ - if (*compcode == HDFE_COMP_NBIT) + if (rankFld == rankSDS) { - statmeta = - EHgetmetavalue(metaptrs, "CompressionParams", utlstr); - if (statmeta == 0) - { - sscanf(utlstr, "(%d,%d,%d,%d)", - &compparm[0], &compparm[1], - &compparm[2], &compparm[3]); - } - else + for (i = 0; i < rankSDS; i++) { - status = -1; - HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__); - HEreport( - "\"CompressionParams\" string not found in metadata.\n"); + count[i] = edge[i]; } } - else if (*compcode == HDFE_COMP_DEFLATE) + else { - statmeta = - EHgetmetavalue(metaptrs, "DeflateLevel", utlstr); - if (statmeta == 0) - { - sscanf(utlstr, "%d", &compparm[0]); - } - else + /* + * If field really 2-dim merged in 3-dim field then set + * 0th field count to 1 and then next two to the user + * values. + */ + for (i = 0; i < rankFld; i++) { - status = -1; - HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__); - HEreport( - "\"DeflateLevel\" string not found in metadata.\n"); + count[i + 1] = edge[i]; } + count[0] = 1; } } - } - else - { - HEpush(DFE_GENAPP, "GDcompinfo", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" not found.\n", fieldname); - } - free(metabuf); - } - free(utlstr); - return (status); -} + /* Perform I/O with relevant HDF I/O routine */ + /* ----------------------------------------- */ + if (strcmp(code, "w") == 0) + { + /* Set strideOne to true (1) */ + /* ------------------------- */ + strideOne = 1; + /* If incr[i] != 1 set strideOne to false (0) */ + /* ------------------------------------------ */ + for (i = 0; i < rankSDS; i++) + { + if (incr[i] != 1) + { + strideOne = 0; + break; + } + } + /* + * If strideOne is true use NULL parameter for stride. This + * is a work-around to HDF compression problem + */ + if (strideOne == 1) + { + status = SDwritedata(sdid, offset, NULL, count, + (VOIDP) datbuf); + } + else + { + status = SDwritedata(sdid, offset, incr, count, + (VOIDP) datbuf); + } + } + else + { + status = SDreaddata(sdid, offset, incr, count, + (VOIDP) datbuf); + } + } + } + + return (status); +} /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDfieldinfo | +| FUNCTION: GDreadfield | | | -| DESCRIPTION: Retrieve information about a specific geolocation or data | -| field in the grid. | +| DESCRIPTION: Reads data from a grid field. | | | | | | Return Value Type Units Description | @@ -2579,19 +2354,16 @@ GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[ | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 grid structure id | -| fieldname char name of field | +| gridID int32 grid structure ID | +| fieldname char fieldname | +| start int32 start array | +| stride int32 stride array | +| edge int32 edge array | +| buffer void data buffer for read | | | | | | OUTPUTS: | -| rank int32 rank of field (# of dims) | -| dims int32 field dimensions | -| numbertype int32 field number type | -| dimlist char field dimension list | -| | -| | -| OUTPUTS: | -| None | +| None | | | | NOTES: | | | @@ -2599,192 +2371,30 @@ GDcompinfo(int32 gridID, const char *fieldname, int32 * compcode, intn compparm[ | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Jan 97 Joel Gales Check for metadata error status from EHgetmetavalue | -| Feb 99 Abe Taaheri Changed memcpy to memmove to avoid overlapping | -| problem when copying strings | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], - int32 * numbertype, char *dimlist) +GDreadfield(int32 gridID, const char *fieldname, + int32 start[], int32 stride[], int32 edge[], VOIDP buffer) { - intn i; /* Loop index */ - intn status; /* routine return status variable */ - intn statmeta = 0; /* EHgetmetavalue return status */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 ndims = 0; /* Number of dimensions */ - int32 slen[8]; /* Length of each entry in parsed string */ - int32 dum; /* Dummy variable */ - int32 xdim; /* X dim size */ - int32 ydim; /* Y dim size */ - int32 sdid; /* SDS id */ - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2]; /* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr; /* Utility string */ - char *ptr[8]; /* String pointers for parsed string */ - char dimstr[64]; /* Individual dimension entry string */ - - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"GDfieldinfo", __FILE__, __LINE__); - return(-1); - } - *rank = -1; - *numbertype = -1; - - status = GDchkgdid(gridID, "GDfieldinfo", &fid, &sdInterfaceID, &dum); - - if (status == 0) - { - int gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlstr); - return -1; - } - Vgetname(GDXGrid[gID].IDTable, gridname); - - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "DataField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - - - /* Search for field */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n"); - metaptrs[0] = strstr(metaptrs[0], utlstr); - - /* If field found ... */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - - /* Set endptr at end of dimension definition entry */ - metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT"); - - /* Get DataType string */ - statmeta = EHgetmetavalue(metaptrs, "DataType", utlstr); - - /* Convert to numbertype code */ - if (statmeta == 0) - *numbertype = EHnumstr(utlstr); - else - { - status = -1; - HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__); - HEreport("\"DataType\" string not found in metadata.\n"); - } - - /* - * Get DimList string and trim off leading and trailing parens - * "()" - */ - statmeta = EHgetmetavalue(metaptrs, "DimList", utlstr); - - if (statmeta == 0) - { - memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); - utlstr[strlen(utlstr) - 2] = 0; - - /* Parse trimmed DimList string and get rank */ - ndims = EHparsestr(utlstr, ',', ptr, slen); - *rank = ndims; - } - else - { - status = -1; - HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__); - HEreport("\"DimList\" string not found in metadata.\n"); - } - - - if (status == 0) - { - status = GDgridinfo(gridID, &xdim, &ydim, NULL, NULL); - - for (i = 0; i < ndims; i++) - { - memcpy(dimstr, ptr[i] + 1, slen[i] - 2); - dimstr[slen[i] - 2] = 0; - - if (strcmp(dimstr, "XDim") == 0) - { - dims[i] = xdim; - } - else if (strcmp(dimstr, "YDim") == 0) - { - dims[i] = ydim; - } - else - { - dims[i] = GDdiminfo(gridID, dimstr); - } - - - if (dimlist != NULL) - { - if (i == 0) - { - dimlist[0] = 0; - } - - if (i > 0) - { - strcat(dimlist, ","); - } - strcat(dimlist, dimstr); - } - } - - - if (dims[0] == 0) - { - status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, - &sdid, &dum, &dum, &dum, dims, - &dum); - } - } - } - - free(metabuf); - } - - if (*rank == -1) - { - status = -1; + intn status = 0; /* routine return status variable */ - HEpush(DFE_GENAPP, "GDfieldinfo", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" not found.\n", fieldname); - } - free(utlstr); + status = GDwrrdfield(gridID, fieldname, "r", start, stride, edge, + buffer); return (status); } - -#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDdeffield | +| FUNCTION: GDwrrdattr | | | -| DESCRIPTION: Defines a new data field within the grid. | +| DESCRIPTION: | | | | | | Return Value Type Units Description | @@ -2793,14 +2403,14 @@ GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], | | | INPUTS: | | gridID int32 grid structure ID | -| fieldname char fieldname | -| dimlist char Dimension list (comma-separated list) | -| numbertype int32 field type | -| merge int32 merge code | -| | +| attrname char attribute name | +| numbertype int32 attribute HDF numbertype | +| count int32 Number of attribute elements | +| wrcode char Read/Write Code "w/r" | +| datbuf void I/O buffer | | | | OUTPUTS: | -| None | +| datbuf | | | | NOTES: | | | @@ -2808,3630 +2418,91 @@ GDfieldinfo(int32 gridID, const char *fieldname, int32 * rank, int32 dims[], | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Check name for ODL compliance | -| Sep 96 Joel Gales Make string array "dimbuf" dynamic | -| Sep 96 Joel Gales Add support for Block SOM (MISR) | -| Jan 97 Joel Gales Add support for tiling | -| Feb 99 Abe Taaheri Changed strcpy to memmove to avoid overlapping | -| problem when copying strings | +| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -GDdeffield(int32 gridID, const char *fieldname, const char *dimlist, - int32 numbertype, int32 merge) +static intn +GDwrrdattr(int32 gridID, const char *attrname, int32 numbertype, int32 count, + const char *wrcode, VOIDP datbuf) { - intn i; /* Loop index */ intn status; /* routine return status variable */ - intn found; /* utility found flag */ - intn foundNT = 0;/* found number type flag */ - intn foundAllDim = 1; /* found all dimensions flag */ - intn first = 1; /* first entry flag */ int32 fid; /* HDF-EOS file ID */ - int32 vgid; /* Geo/Data field Vgroup ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 sdid; /* SDS object ID */ - int32 dimid; /* SDS dimension ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 dims[8]; /* Dimension size array */ - int32 dimsize; /* Dimension size */ - int32 rank = 0; /* Field rank */ - int32 slen[32]; /* String length array */ + int32 attrVgrpID; /* Grid attribute ID */ + int32 dum; /* dummy variable */ int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 compcode; /* Compression code */ - int32 tilecode; /* Tiling code */ - int32 chunkFlag; /* Chunking (Tiling) flag */ - int32 gID; /* GridID - offset */ - int32 xdim; /* Grid X dimension */ - int32 ydim; /* Grid Y dimension */ - int32 projcode; /* Projection Code */ - - float64 projparm[13]; /* Projection Parameters */ - - char *dimbuf; /* Dimension buffer */ - char *dimlist0; /* Auxiliary dimension list */ - char *comma; /* Pointer to comma */ - char *dimcheck; /* Dimension check buffer */ - char utlbuf[512];/* Utility buffer */ - char utlbuf2[256]; /* Utility buffer 1 */ - char *ptr[32]; /* String pointer array */ - char gridname[80]; /* Grid name */ - char parmbuf[128]; /* Parameter string buffer */ - char errbuf1[128]; /* Error buffer 1 */ - char errbuf2[128]; /* Error buffer 2 */ - static const char errmsg1[] = "Dimension: %d (size: %d) not divisible by "; - /* Tiling error message part 1 */ - static const char errmsg2[] = "tile dimension (size: %d).\n"; - /* Tiling error message part 2 */ - char errmsg[128];/* Tiling error message */ - - /* Valid number types */ - static const uint16 good_number[10] = { - 3, 4, 5, 6, 20, 21, 22, 23, 24, 25 - }; - - comp_info c_info; /* Compression parameter structure */ - - HDF_CHUNK_DEF chunkDef; /* Tiling structure */ - - memset(&c_info, 0, sizeof(c_info)); - memset(&chunkDef, 0, sizeof(chunkDef)); - - - /* Setup error message strings */ - /* --------------------------- */ - strcpy(errbuf1, "GDXSDname array too small.\nPlease increase "); - strcat(errbuf1, "size of HDFE_NAMBUFSIZE in \"HdfEosDef.h\".\n"); - strcpy(errbuf2, "GDXSDdims array too small.\nPlease increase "); - strcat(errbuf2, "size of HDFE_DIMBUFSIZE in \"HdfEosDef.h\".\n"); - - - /* Build tiling dimension error message */ - /* ------------------------------------ */ - strcpy(errmsg, errmsg1); - strcat(errmsg, errmsg2); - - /* - * Check for proper grid ID and return HDF-EOS file ID, SDinterface ID, - * and grid root Vgroup ID - */ - status = GDchkgdid(gridID, "GDdefinefield", - &fid, &sdInterfaceID, &gdVgrpID); + /* Check Grid id */ + status = GDchkgdid(gridID, "GDwrrdattr", &fid, &dum, &dum); + if (status == 0) { - /* Remove offset from grid ID & get gridname */ - gID = gridID % idOffset; + /* Perform Attribute I/O */ + /* --------------------- */ + int gID = gridID % idOffset; if (gID >= NGRID) { - return -1; + return -1; } - Vgetname(GDXGrid[gID].IDTable, gridname); + attrVgrpID = GDXGrid[gID].VIDTable[1]; + status = EHattr(fid, attrVgrpID, attrname, numbertype, count, + wrcode, datbuf); + } + return (status); +} +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: GDreadattr | +| | +| DESCRIPTION: Reads attribute from a grid. | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| status intn return status (0) SUCCEED, (-1) FAIL | +| | +| INPUTS: | +| gridID int32 grid structure ID | +| attrname char attribute name | +| | +| OUTPUTS: | +| datbuf void I/O buffer | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Jun 96 Joel Gales Original Programmer | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +intn +GDreadattr(int32 gridID, const char *attrname, VOIDP datbuf) +{ + intn status = 0; /* routine return status variable */ + int32 dum = 0; /* dummy variable */ - /* Allocate space for dimension buffer and auxiliary dimension list */ - /* ---------------------------------------------------------------- */ - dimbuf = (char *) calloc(strlen(dimlist) + 64, 1); - if(dimbuf == NULL) - { - HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); - return(-1); - } - dimlist0 = (char *) calloc(strlen(dimlist) + 64, 1); - if(dimlist0 == NULL) - { - HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); - free(dimbuf); - return(-1); - } - - - /* Get Grid and Projection info */ - /* ---------------------------- */ - status = GDgridinfo(gridID, &xdim, &ydim, NULL, NULL); - status = GDprojinfo(gridID, &projcode, NULL, NULL, projparm); - - - /* Setup Block Dimension if "Blocked" SOM projection */ - /* ------------------------------------------------- */ - if (projcode == GCTP_SOM && (int32) projparm[11] != 0) - { - dimsize = GDdiminfo(gridID, "SOMBlockDim"); - - /* If "SOMBlockDim" not yet defined then do it */ - if (dimsize == -1) - { - GDdefdim(gridID, "SOMBlockDim", (int32) projparm[11]); - } - - /* If not 1D field then prepend to dimension list */ - if (strchr(dimlist, ',') != NULL) - { - strcpy(dimbuf, "SOMBlockDim,"); - strcat(dimbuf, dimlist); - } - else - { - strcpy(dimbuf, dimlist); - } - } - else - { - /* If not "Blocked" SOM then just copy dim list to dim buffer */ - strcpy(dimbuf, dimlist); - } - - /* - * Copy dimension buffer to auxiliary dimlist and append a comma to - * end of dimension list. - */ - strcpy(dimlist0, dimbuf); - strcat(dimbuf, ","); - - - /* Find comma */ - /* ---------- */ - comma = strchr(dimbuf, ','); - - - /* - * Loop through entries in dimension list to make sure they are - * defined in grid - */ - while (comma != NULL) - { - /* Copy dimension list entry to dimcheck */ - /* ------------------------------------- */ - dimcheck = (char *) calloc(comma - dimbuf + 1, 1); - if(dimcheck == NULL) - { - HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); - free(dimbuf); - free(dimlist0); - return(-1); - } - memcpy(dimcheck, dimbuf, comma - dimbuf); - - - /* Get Dimension Size */ - /* ------------------ */ - if (strcmp(dimcheck, "XDim") == 0) - { - /* If "XDim" then use xdim value for grid definition */ - /* ------------------------------------------------- */ - dimsize = xdim; - found = 1; - dims[rank] = dimsize; - rank++; - } - else if (strcmp(dimcheck, "YDim") == 0) - { - /* If "YDim" then use ydim value for grid definition */ - /* ------------------------------------------------- */ - dimsize = ydim; - found = 1; - dims[rank] = dimsize; - rank++; - } - else - { - /* "Regular" Dimension */ - /* ------------------- */ - dimsize = GDdiminfo(gridID, dimcheck); - if (dimsize != -1) - { - found = 1; - dims[rank] = dimsize; - rank++; - } - else - { - found = 0; - } - } - - - /* - * If dimension list entry not found - set error return status, - * append name to utility buffer for error report - */ - if (found == 0) - { - status = -1; - foundAllDim = 0; - if (first == 1) - { - snprintf(utlbuf, sizeof(utlbuf), "%s", dimcheck); - } - else - { - char utlbufTmp[sizeof(utlbuf)]; - strcpy(utlbufTmp, utlbuf); - CPLsnprintf(utlbuf, sizeof(utlbuf), "%s,%s", utlbufTmp, dimcheck); - } - first = 0; - } - - /* - * Go to next dimension entry, find next comma, & free up - * dimcheck buffer - */ - memmove(dimbuf, comma + 1, strlen(comma)-1); - dimbuf[strlen(comma)-1]= 0; - comma = strchr(dimbuf, ','); - free(dimcheck); - } - free(dimbuf); - - /* Check fieldname length */ - /* ---------------------- */ - if (status == 0) - { -/* if ((intn) strlen(fieldname) > MAX_NC_NAME - 7) -** this was changed because HDF4.1r3 made a change in the -** hlimits.h file. We have notified NCSA and asked to have -** it made the same as in previous versions of HDF -** see ncr 26314. DaW Apr 2000 -*/ - if((intn) strlen(fieldname) > (256 - 7)) - { - status = -1; - HEpush(DFE_GENAPP, "GDdefinefield", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" too long.\n", fieldname); - } - } - - /* Check for valid numbertype */ - /* -------------------------- */ - if (status == 0) - { - for (i = 0; i < 10; i++) - { - if (numbertype == good_number[i]) - { - foundNT = 1; - break; - } - } - - if (foundNT == 0) - { - HEpush(DFE_BADNUMTYPE, "GDdeffield", __FILE__, __LINE__); - HEreport("Invalid number type: %d (%s).\n", - numbertype, fieldname); - status = -1; - } - } - - - /* Define Field */ - /* ------------ */ - if (status == 0) - { - /* Get Field Vgroup id, compression code, & tiling code */ - /* -------------------------------------------------- */ - vgid = GDXGrid[gID].VIDTable[0]; - compcode = GDXGrid[gID].compcode; - tilecode = GDXGrid[gID].tilecode; - - - /* SDS Interface (Multi-dim fields) */ - /* -------------------------------- */ - - - /* - * If rank is less than or equal to 3 (and greater than 1) and - * AUTOMERGE is set and the first dimension is not appendable and - * the compression code is set to none then ... - */ - if (rank >= 2 && rank <= 3 && merge == HDFE_AUTOMERGE && - dims[0] != 0 && compcode == HDFE_COMP_NONE && - tilecode == HDFE_NOTILE) - { - - /* Find first empty slot in external combination array */ - /* --------------------------------------------------- */ - i = 0; - while (GDXSDcomb[5 * i] != 0) - { - i++; - } - - /* - * Store dimensions, grid root Vgroup ID, and number type in - * external combination array "GDXSDcomb" - */ - if (rank == 2) - { - /* If 2-dim field then set lowest dimension to 1 */ - /* --------------------------------------------- */ - GDXSDcomb[5 * i] = 1; - GDXSDcomb[5 * i + 1] = dims[0]; - GDXSDcomb[5 * i + 2] = dims[1]; - } - else - { - GDXSDcomb[5 * i] = dims[0]; - GDXSDcomb[5 * i + 1] = dims[1]; - GDXSDcomb[5 * i + 2] = dims[2]; - } - - GDXSDcomb[5 * i + 3] = gdVgrpID; - GDXSDcomb[5 * i + 4] = numbertype; - - - /* Concatenate fieldname with combined name string */ - /* ----------------------------------------------- */ - if ((intn) strlen(GDXSDname) + - (intn) strlen(fieldname) + 2 < HDFE_NAMBUFSIZE) - { - strcat(GDXSDname, fieldname); - strcat(GDXSDname, ","); - } - else - { - /* GDXSDname array too small! */ - /* -------------------------- */ - HEpush(DFE_GENAPP, "GDdefinefield", - __FILE__, __LINE__); - HEreport(errbuf1); - status = -1; - free(dimlist0); - return (status); - } - - - /* - * If 2-dim field then set lowest dimension (in 3-dim array) - * to "ONE" - */ - if (rank == 2) - { - if ((intn) strlen(GDXSDdims) + 5 < HDFE_DIMBUFSIZE) - { - strcat(GDXSDdims, "ONE,"); - } - else - { - /* GDXSDdims array too small! */ - /* -------------------------- */ - HEpush(DFE_GENAPP, "GDdefinefield", - __FILE__, __LINE__); - HEreport(errbuf2); - status = -1; - free(dimlist0); - return (status); - } - } - - - /* - * Concatanate field dimlist to merged dimlist and separate - * fields with semi-colon. - */ - if ((intn) strlen(GDXSDdims) + - (intn) strlen(dimlist0) + 2 < HDFE_DIMBUFSIZE) - { - strcat(GDXSDdims, dimlist0); - strcat(GDXSDdims, ";"); - } - else - { - /* GDXSDdims array too small! */ - /* -------------------------- */ - HEpush(DFE_GENAPP, "GDdefinefield", - __FILE__, __LINE__); - HEreport(errbuf2); - status = -1; - free(dimlist0); - return (status); - } - - } /* End Multi-Dim Merge Section */ - else - { - - /* Multi-Dim No Merge Section */ - /* ========================== */ - - - /* Check that field dims are divisible by tile dims */ - /* ------------------------------------------------ */ - if (tilecode == HDFE_TILE) - { - for (i = 0; i < GDXGrid[gID].tilerank; i++) - { - if ((dims[i] % GDXGrid[gID].tiledims[i]) != 0) - { - HEpush(DFE_GENAPP, "GDdeffield", - __FILE__, __LINE__); - HEreport(errmsg, - i, dims[i], GDXGrid[gID].tiledims[0]); - status = -1; - } - } - - if (status == -1) - { - free(dimlist0); - return (status); - } - } - - - /* Create SDS dataset */ - /* ------------------ */ - sdid = SDcreate(sdInterfaceID, fieldname, - numbertype, rank, dims); - - - /* Store Dimension Names in SDS */ - /* ---------------------------- */ - rank = EHparsestr(dimlist0, ',', ptr, slen); - for (i = 0; i < rank; i++) - { - /* Dimension name = Swathname:Dimname */ - /* ---------------------------------- */ - memcpy(utlbuf, ptr[i], slen[i]); - utlbuf[slen[i]] = 0; - strcat(utlbuf, ":"); - strcat(utlbuf, gridname); - - dimid = SDgetdimid(sdid, i); - SDsetdimname(dimid, utlbuf); - } - - - /* Setup Compression */ - /* ----------------- */ - if (compcode == HDFE_COMP_NBIT) - { - c_info.nbit.nt = numbertype; - c_info.nbit.sign_ext = GDXGrid[gID].compparm[0]; - c_info.nbit.fill_one = GDXGrid[gID].compparm[1]; - c_info.nbit.start_bit = GDXGrid[gID].compparm[2]; - c_info.nbit.bit_len = GDXGrid[gID].compparm[3]; - } - else if (compcode == HDFE_COMP_SKPHUFF) - { - c_info.skphuff.skp_size = (intn) DFKNTsize(numbertype); - } - else if (compcode == HDFE_COMP_DEFLATE) - { - c_info.deflate.level = GDXGrid[gID].compparm[0]; - } - - - /* If field is compressed w/o tiling then call SDsetcompress */ - /* --------------------------------------------------------- */ - if (compcode != HDFE_COMP_NONE && tilecode == HDFE_NOTILE) - { - /* status = */ SDsetcompress(sdid, (comp_coder_t) compcode, &c_info); - } - - - /* Setup Tiling */ - /* ------------ */ - if (tilecode == HDFE_TILE) - { - /* Tiling without Compression */ - /* -------------------------- */ - if (compcode == HDFE_COMP_NONE) - { - - /* Setup chunk lengths */ - /* ------------------- */ - for (i = 0; i < GDXGrid[gID].tilerank; i++) - { - chunkDef.chunk_lengths[i] = - GDXGrid[gID].tiledims[i]; - } - - chunkFlag = HDF_CHUNK; - } - - /* Tiling with Compression */ - /* ----------------------- */ - else - { - /* Setup chunk lengths */ - /* ------------------- */ - for (i = 0; i < GDXGrid[gID].tilerank; i++) - { - chunkDef.comp.chunk_lengths[i] = - GDXGrid[gID].tiledims[i]; - } - - - /* Setup chunk flag & chunk compression type */ - /* ----------------------------------------- */ - chunkFlag = HDF_CHUNK | HDF_COMP; - chunkDef.comp.comp_type = compcode; - - - /* Setup chunk compression parameters */ - /* ---------------------------------- */ - if (compcode == HDFE_COMP_SKPHUFF) - { - chunkDef.comp.cinfo.skphuff.skp_size = - c_info.skphuff.skp_size; - } - else if (compcode == HDFE_COMP_DEFLATE) - { - chunkDef.comp.cinfo.deflate.level = - c_info.deflate.level; - } - } - - /* Call SDsetchunk routine */ - /* ----------------------- */ - /* status = */ SDsetchunk(sdid, chunkDef, chunkFlag); - } - - - /* Attach to Vgroup */ - /* ---------------- */ - Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid)); - - - /* Store SDS dataset IDs */ - /* --------------------- */ - - /* Allocate space for the SDS ID array */ - /* ----------------------------------- */ - if (GDXGrid[gID].nSDS > 0) - { - /* Array already exists therefore reallocate */ - /* ----------------------------------------- */ - GDXGrid[gID].sdsID = (int32 *) - realloc((void *) GDXGrid[gID].sdsID, - (GDXGrid[gID].nSDS + 1) * 4); - if(GDXGrid[gID].sdsID == NULL) - { - HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); - free(dimlist0); - return(-1); - } - } - else - { - /* Array does not exist */ - /* -------------------- */ - GDXGrid[gID].sdsID = (int32 *) calloc(1, 4); - if(GDXGrid[gID].sdsID == NULL) - { - HEpush(DFE_NOSPACE,"GDdeffield", __FILE__, __LINE__); - free(dimlist0); - return(-1); - } - } - - /* Store SDS ID in array & increment count */ - /* --------------------------------------- */ - GDXGrid[gID].sdsID[GDXGrid[gID].nSDS] = sdid; - GDXGrid[gID].nSDS++; - - } - - - /* Setup metadata string */ - /* --------------------- */ - snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist0); - - - /* Setup compression metadata */ - /* -------------------------- */ - if (compcode != HDFE_COMP_NONE) - { - snprintf(utlbuf2, sizeof(utlbuf2), - "%s%s", - ":\n\t\t\t\tCompressionType=", HDFcomp[compcode]); - - switch (compcode) - { - case HDFE_COMP_NBIT: - - snprintf(parmbuf, sizeof(parmbuf), - "%s%d,%d,%d,%d%s", - "\n\t\t\t\tCompressionParams=(", - GDXGrid[gID].compparm[0], - GDXGrid[gID].compparm[1], - GDXGrid[gID].compparm[2], - GDXGrid[gID].compparm[3], ")"); - strcat(utlbuf2, parmbuf); - break; - - - case HDFE_COMP_DEFLATE: - - snprintf(parmbuf, sizeof(parmbuf), - "%s%d", - "\n\t\t\t\tDeflateLevel=", - GDXGrid[gID].compparm[0]); - strcat(utlbuf2, parmbuf); - break; - } - strcat(utlbuf, utlbuf2); - } - - - - - /* Setup tiling metadata */ - /* --------------------- */ - if (tilecode == HDFE_TILE) - { - if (compcode == HDFE_COMP_NONE) - { - snprintf(utlbuf2, sizeof(utlbuf2), "%s%d", - ":\n\t\t\t\tTilingDimensions=(", - (int)GDXGrid[gID].tiledims[0]); - } - else - { - snprintf(utlbuf2, sizeof(utlbuf2), "%s%d", - "\n\t\t\t\tTilingDimensions=(", - (int)GDXGrid[gID].tiledims[0]); - } - - for (i = 1; i < GDXGrid[gID].tilerank; i++) - { - snprintf(parmbuf, sizeof(parmbuf), ",%d", (int)GDXGrid[gID].tiledims[i]); - strcat(utlbuf2, parmbuf); - } - strcat(utlbuf2, ")"); - strcat(utlbuf, utlbuf2); - } - - - /* Insert field metadata within File Structural Metadata */ - /* ----------------------------------------------------- */ - status = EHinsertmeta(sdInterfaceID, gridname, "g", 4L, - utlbuf, &numbertype); - - } - free(dimlist0); - - } - - /* If all dimensions not found then report error */ - /* --------------------------------------------- */ - if (foundAllDim == 0) - { - HEpush(DFE_GENAPP, "GDdeffield", __FILE__, __LINE__); - HEreport("Dimension(s): \"%s\" not found (%s).\n", - utlbuf, fieldname); - status = -1; - } - - return (status); -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDwritefieldmeta | -| | -| DESCRIPTION: Writes field meta data for an existing grid field not | -| defined within the grid API routine "GDdeffield". | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| fieldname char fieldname | -| dimlist char Dimension list (comma-separated list) | -| numbertype int32 field type | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDwritefieldmeta(int32 gridID, const char *fieldname, const char *dimlist, - int32 numbertype) -{ - intn status = 0; /* routine return status variable */ - - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 dum; /* dummy variable */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - - char utlbuf[256];/* Utility buffer */ - char gridname[80]; /* Grid name */ - - - status = GDchkgdid(gridID, "GDwritefieldmeta", &dum, &sdInterfaceID, - &dum); - - if (status == 0) - { - snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist); - - int gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - - Vgetname(GDXGrid[gID].IDTable, gridname); - int32 numbertypeArray[1] = { numbertype }; - status = EHinsertmeta(sdInterfaceID, gridname, "g", 4L, - utlbuf, numbertypeArray); - } - return (status); -} -#endif - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDSDfldsrch | -| | -| DESCRIPTION: Retrieves information from SDS fields | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| sdInterfaceID int32 SD interface ID | -| fieldname char field name | -| | -| | -| OUTPUTS: | -| sdid int32 SD element ID | -| rankSDS int32 Rank of SDS | -| rankFld int32 True rank of field (merging) | -| offset int32 Offset of field within merged field | -| dims int32 Dimensions of field | -| solo int32 Solo field flag | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -GDSDfldsrch(int32 gridID, int32 sdInterfaceID, const char *fieldname, - int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset, - int32 dims[], int32 * solo) -{ - intn i; /* Loop index */ - intn status = -1;/* routine return status variable */ - - int32 gID; /* GridID - offset */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 dum; /* Dummy variable */ - int32 dums[128]; /* Dummy array */ - int32 attrIndex; /* Attribute l_index */ - - char name[2048]; /* Merged-Field Names */ - char gridname[80]; /* Grid Name */ - char *utlstr;/* Utility string */ - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ -#ifdef broken_logic - char *oldmetaptr; /* Pointer within SM section */ - char *metaptr; /* Pointer within SM section */ -#endif - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"GDSDfldsrch", __FILE__, __LINE__); - return(-1); - } - /* Set solo flag to 0 (no) */ - /* ----------------------- */ - *solo = 0; - - - /* Compute "reduced" grid ID */ - /* ------------------------- */ - gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlstr); - return -1; - } - - /* Loop through all SDSs in grid */ - /* ----------------------------- */ - for (i = 0; i < GDXGrid[gID].nSDS; i++) - { - /* If active SDS ... */ - /* ----------------- */ - if (GDXGrid[gID].sdsID[i] != 0) - { - /* Get SDS ID, name, rankSDS, and dimensions */ - /* ----------------------------------------- */ - *sdid = GDXGrid[gID].sdsID[i]; - SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum); - *rankFld = *rankSDS; - - - /* If merged field ... */ - /* ------------------- */ - if (strstr(name, "MRGFLD_") == &name[0]) - { - /* Get grid name */ - /* ------------- */ - Vgetname(GDXGrid[gID].IDTable, gridname); - - - /* Get pointers to "MergedFields" section within SM */ - /* ------------------------------------------------ */ - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "MergedFields", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - -#ifdef broken_logic - /* Initialize metaptr to beg. of section */ - /* ------------------------------------- */ - metaptr = metaptrs[0]; - - - /* Store metaptr in order to recover */ - /* --------------------------------- */ - oldmetaptr = metaptr; - - - /* Search for Merged field name */ - /* ---------------------------- */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "MergedFieldName=\"", - name, "\"\n"); - metaptr = strstr(metaptr, utlstr); - - - /* If not found check for old metadata */ - /* ----------------------------------- */ - if (metaptr == NULL) - { - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "OBJECT=\"", name, "\"\n"); - metaptr = strstr(oldmetaptr, utlstr); - } -#endif - - /* Get field list and strip off leading and trailing quotes */ - /* -------------------------------------------------------- */ - EHgetmetavalue(metaptrs, "FieldList", name); - memmove(name, name + 1, strlen(name) - 2); - name[strlen(name) - 2] = 0; - - - /* Search for desired field within merged field list */ - /* ------------------------------------------------- */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\""); - dum = EHstrwithin(utlstr, name, ','); - - free(metabuf); - } - else - { - /* If solo (unmerged) check if SDS name matches fieldname */ - /* ------------------------------------------------------ */ - dum = EHstrwithin(fieldname, name, ','); - if (dum != -1) - { - *solo = 1; - *offset = 0; - } - } - - - - /* If field found ... */ - /* ------------------ */ - if (dum != -1) - { - status = 0; - - /* If merged field ... */ - /* ------------------- */ - if (*solo == 0) - { - /* Get "Field Offsets" SDS attribute l_index */ - /* --------------------------------------- */ - attrIndex = SDfindattr(*sdid, "Field Offsets"); - - /* - * If attribute exists then get offset of desired field - * within merged field - */ - if (attrIndex != -1) - { - SDreadattr(*sdid, attrIndex, (VOIDP) dums); - *offset = dums[dum]; - } - - - /* Get "Field Dims" SDS attribute l_index */ - /* ------------------------------------ */ - attrIndex = SDfindattr(*sdid, "Field Dims"); - - /* - * If attribute exists then get 0th dimension of desired - * field within merged field - */ - if (attrIndex != -1) - { - SDreadattr(*sdid, attrIndex, (VOIDP) dums); - dims[0] = dums[dum]; - - /* If this dimension = 1 then field is really 2 dim */ - /* ------------------------------------------------ */ - if (dums[dum] == 1) - { - *rankFld = 2; - } - } - } - - - /* Break out of SDS loop */ - /* --------------------- */ - break; - } /* End of found field section */ - } - else - { - /* First non-active SDS signifies no more, break out of SDS loop */ - /* ------------------------------------------------------------- */ - break; - } - } - free(utlstr); - return (status); -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDwrrdfield | -| | -| DESCRIPTION: Writes/Reads fields | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| fieldname char fieldname | -| code char Write/Read code (w/r) | -| start int32 start array | -| stride int32 stride array | -| edge int32 edge array | -| datbuf void data buffer for read | -| | -| | -| OUTPUTS: | -| datbuf void data buffer for write | -| | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Feb 97 Joel Gales Stride = 1 HDF compression workaround | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -GDwrrdfield(int32 gridID, const char *fieldname, const char *code, - int32 start[], int32 stride[], int32 edge[], VOIDP datbuf) - -{ - intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 sdid; /* SDS ID */ - int32 dum; /* Dummy variable */ - int32 rankSDS; /* Rank of SDS */ - int32 rankFld; /* Rank of field */ - - int32 offset[8]; /* I/O offset (start) */ - int32 incr[8]; /* I/O increment (stride) */ - int32 count[8]; /* I/O count (edge) */ - int32 dims[8]; /* Field/SDS dimensions */ - int32 mrgOffset; /* Merged field offset */ - int32 strideOne; /* Strides = 1 flag */ - - - /* Check for valid grid ID */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDwrrdfield", &fid, &sdInterfaceID, &dum); - - - if (status == 0) - { - /* Check that field exists */ - /* ----------------------- */ - status = GDfieldinfo(gridID, fieldname, &rankSDS, dims, &dum, NULL); - - - if (status != 0) - { - HEpush(DFE_GENAPP, "GDwrrdfield", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", fieldname); - status = -1; - - } - - - if (status == 0) - { - status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid, - &rankSDS, &rankFld, &mrgOffset, dims, &dum); - - - /* Set I/O offset Section */ - /* ---------------------- */ - - /* - * If start == NULL (default) set I/O offset of 0th field to - * offset within merged field (if any) and the rest to 0 - */ - if (start == NULL) - { - for (i = 0; i < rankSDS; i++) - { - offset[i] = 0; - } - offset[0] = mrgOffset; - } - else - { - /* - * ... otherwise set I/O offset to user values, adjusting the - * 0th field with the merged field offset (if any) - */ - if (rankFld == rankSDS) - { - for (i = 0; i < rankSDS; i++) - { - offset[i] = start[i]; - } - offset[0] += mrgOffset; - } - else - { - /* - * If field really 2-dim merged in 3-dim field then set - * 0th field offset to merge offset and then next two to - * the user values - */ - for (i = 0; i < rankFld; i++) - { - offset[i + 1] = start[i]; - } - offset[0] = mrgOffset; - } - } - - - - /* Set I/O stride Section */ - /* ---------------------- */ - - /* - * If stride == NULL (default) set I/O stride to 1 - */ - if (stride == NULL) - { - for (i = 0; i < rankSDS; i++) - { - incr[i] = 1; - } - } - else - { - /* - * ... otherwise set I/O stride to user values - */ - if (rankFld == rankSDS) - { - for (i = 0; i < rankSDS; i++) - { - incr[i] = stride[i]; - } - } - else - { - /* - * If field really 2-dim merged in 3-dim field then set - * 0th field stride to 1 and then next two to the user - * values. - */ - for (i = 0; i < rankFld; i++) - { - incr[i + 1] = stride[i]; - } - incr[0] = 1; - } - } - - - - /* Set I/O count Section */ - /* --------------------- */ - - /* - * If edge == NULL (default) set I/O count to number of remaining - * entries (dims - start) / increment. Note that 0th field - * offset corrected for merged field offset (if any). - */ - if (edge == NULL) - { - for (i = 1; i < rankSDS; i++) - { - count[i] = (dims[i] - offset[i]) / incr[i]; - } - count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0]; - } - else - { - /* - * ... otherwise set I/O count to user values - */ - if (rankFld == rankSDS) - { - for (i = 0; i < rankSDS; i++) - { - count[i] = edge[i]; - } - } - else - { - /* - * If field really 2-dim merged in 3-dim field then set - * 0th field count to 1 and then next two to the user - * values. - */ - for (i = 0; i < rankFld; i++) - { - count[i + 1] = edge[i]; - } - count[0] = 1; - } - } - - - /* Perform I/O with relevant HDF I/O routine */ - /* ----------------------------------------- */ - if (strcmp(code, "w") == 0) - { - /* Set strideOne to true (1) */ - /* ------------------------- */ - strideOne = 1; - - - /* If incr[i] != 1 set strideOne to false (0) */ - /* ------------------------------------------ */ - for (i = 0; i < rankSDS; i++) - { - if (incr[i] != 1) - { - strideOne = 0; - break; - } - } - - - /* - * If strideOne is true use NULL parameter for stride. This - * is a work-around to HDF compression problem - */ - if (strideOne == 1) - { - status = SDwritedata(sdid, offset, NULL, count, - (VOIDP) datbuf); - } - else - { - status = SDwritedata(sdid, offset, incr, count, - (VOIDP) datbuf); - } - } - else - { - status = SDreaddata(sdid, offset, incr, count, - (VOIDP) datbuf); - } - } - } - - return (status); -} - -#ifdef HDFEOS_GD_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDwritefield | -| | -| DESCRIPTION: Writes data to a grid field. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| fieldname char fieldname | -| start int32 start array | -| stride int32 stride array | -| edge int32 edge array | -| | -| | -| OUTPUTS: | -| data void data buffer for write | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDwritefield(int32 gridID, const char *fieldname, - int32 start[], int32 stride[], int32 edge[], VOIDP data) - -{ - intn status = 0; /* routine return status variable */ - - status = GDwrrdfield(gridID, fieldname, "w", start, stride, edge, - data); - return (status); -} -#endif - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDreadfield | -| | -| DESCRIPTION: Reads data from a grid field. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| fieldname char fieldname | -| start int32 start array | -| stride int32 stride array | -| edge int32 edge array | -| buffer void data buffer for read | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDreadfield(int32 gridID, const char *fieldname, - int32 start[], int32 stride[], int32 edge[], VOIDP buffer) - -{ - intn status = 0; /* routine return status variable */ - - status = GDwrrdfield(gridID, fieldname, "r", start, stride, edge, - buffer); - return (status); -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDwrrdattr | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| attrname char attribute name | -| numbertype int32 attribute HDF numbertype | -| count int32 Number of attribute elements | -| wrcode char Read/Write Code "w/r" | -| datbuf void I/O buffer | -| | -| OUTPUTS: | -| datbuf | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -GDwrrdattr(int32 gridID, const char *attrname, int32 numbertype, int32 count, - const char *wrcode, VOIDP datbuf) - -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 attrVgrpID; /* Grid attribute ID */ - int32 dum; /* dummy variable */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - - - /* Check Grid id */ - status = GDchkgdid(gridID, "GDwrrdattr", &fid, &dum, &dum); - - if (status == 0) - { - /* Perform Attribute I/O */ - /* --------------------- */ - int gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - attrVgrpID = GDXGrid[gID].VIDTable[1]; - status = EHattr(fid, attrVgrpID, attrname, numbertype, count, - wrcode, datbuf); - } - return (status); -} - - -#ifdef HDFEOS_GD_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDwriteattr | -| | -| DESCRIPTION: Writes/updates attribute in a grid. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| attrname char attribute name | -| numbertype int32 attribute HDF numbertype | -| count int32 Number of attribute elements | -| datbuf void I/O buffer | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDwriteattr(int32 gridID, const char *attrname, int32 numbertype, int32 count, - VOIDP datbuf) -{ - intn status = 0; /* routine return status variable */ - - /* Call GDwrrdattr routine to write attribute */ - /* ------------------------------------------ */ - status = GDwrrdattr(gridID, attrname, numbertype, count, "w", datbuf); - - return (status); -} -#endif - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDreadattr | -| | -| DESCRIPTION: Reads attribute from a grid. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| attrname char attribute name | -| | -| OUTPUTS: | -| datbuf void I/O buffer | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDreadattr(int32 gridID, const char *attrname, VOIDP datbuf) -{ - intn status = 0; /* routine return status variable */ - int32 dum = 0; /* dummy variable */ - - /* Call GDwrrdattr routine to read attribute */ - /* ----------------------------------------- */ - status = GDwrrdattr(gridID, attrname, dum, dum, "r", datbuf); - - return (status); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDattrinfo | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| attrname char attribute name | -| | -| OUTPUTS: | -| numbertype int32 attribute HDF numbertype | -| count int32 Number of attribute elements | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDattrinfo(int32 gridID, const char *attrname, int32 * numbertype, int32 * count) -{ - intn status = 0; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 attrVgrpID; /* Grid attribute ID */ - int32 dum; /* dummy variable */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - - status = GDchkgdid(gridID, "GDattrinfo", &fid, &dum, &dum); - - int gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - attrVgrpID = GDXGrid[gID].VIDTable[1]; - - status = EHattrinfo(fid, attrVgrpID, attrname, numbertype, - count); - - return (status); -} - - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDinqattrs | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nattr int32 Number of attributes in swath struct | -| | -| INPUTS: | -| grid ID int32 grid structure ID | -| | -| OUTPUTS: | -| attrnames char Attribute names in swath struct | -| (Comma-separated list) | -| strbufsize int32 Attributes name list string length | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Initialize nattr | -| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDinqattrs(int32 gridID, char *attrnames, int32 * strbufsize) -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 attrVgrpID; /* Grid attribute ID */ - int32 dum; /* dummy variable */ - int32 nattr = 0; /* Number of attributes */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - - - /* Check Grid id */ - status = GDchkgdid(gridID, "GDinqattrs", &fid, &dum, &dum); - - if (status == 0) - { - int gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - attrVgrpID = GDXGrid[gID].VIDTable[1]; - nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize); - } - - return (nattr); -} - - - - - - -#define REMQUOTE(x) do { \ - char* l_x = x; \ - const size_t l_x_len = strlen(l_x); \ - if (l_x_len >= 2 && l_x[0] == '"' && l_x[l_x_len - 1] == '"') {\ - memmove(l_x, l_x + 1, l_x_len - 2); \ - l_x[l_x_len - 2] = 0; \ - } \ - } while(0) - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDinqdims | -| | -| DESCRIPTION: Retrieve information about all dimensions defined in a grid. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nDim int32 Number of defined dimensions | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| | -| OUTPUTS: | -| dimnames char Dimension names (comma-separated) | -| dims int32 Dimension values | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nDim to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDinqdims(int32 gridID, char *dimnames, int32 dims[]) -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 size; /* Dimension size */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 nDim = 0; /* Number of dimensions */ - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr;/* Utility string */ - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"GDinqdims", __FILE__, __LINE__); - return(-1); - } - /* Check for valid grid id */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDinqdims", &fid, &sdInterfaceID, &gdVgrpID); - - if (status == 0) - { - /* If dimension names or sizes are requested */ - /* ----------------------------------------- */ - if (dimnames != NULL || dims != NULL) - { - /* Get grid name */ - /* ------------- */ - int gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlstr); - return -1; - } - Vgetname(GDXGrid[gID].IDTable, gridname); - - - /* Get pointers to "Dimension" section within SM */ - /* --------------------------------------------- */ - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "Dimension", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - - - /* If dimension names are requested then "clear" name buffer */ - /* --------------------------------------------------------- */ - if (dimnames != NULL) - { - dimnames[0] = 0; - } - - while (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - strcpy(utlstr, "\t\tOBJECT="); - metaptrs[0] = strstr(metaptrs[0], utlstr); - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - /* Get Dimension Name */ - /* ------------------ */ - if (dimnames != NULL) - { - /* Check 1st for old meta data then new */ - /* ------------------------------------ */ - EHgetmetavalue(metaptrs, "OBJECT", utlstr); - if (utlstr[0] != '"') - { - metaptrs[0] = - strstr(metaptrs[0], "\t\t\t\tDimensionName="); - EHgetmetavalue(metaptrs, "DimensionName", utlstr); - } - - /* Strip off double quotes */ - /* ----------------------- */ - memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); - utlstr[strlen(utlstr) - 2] = 0; - - if (nDim > 0) - { - strcat(dimnames, ","); - } - strcat(dimnames, utlstr); - } - - /* Get Dimension Size */ - /* ------------------ */ - if (dims != NULL) - { - EHgetmetavalue(metaptrs, "Size", utlstr); - size = atoi(utlstr); - dims[nDim] = size; - } - nDim++; - } - } - free(metabuf); - - } - } - - - /* Set nDim to -1 if error status exists */ - /* ------------------------------------- */ - if (status == -1) - { - nDim = -1; - } - free(utlstr); - return (nDim); -} - - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDinqfields | -| | -| DESCRIPTION: Retrieve information about all data fields defined in a grid. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nFld int32 Number of fields in swath | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| | -| | -| OUTPUTS: | -| fieldlist char Field names (comma-separated) | -| rank int32 Array of ranks | -| numbertype int32 Array of HDF number types | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nFld to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDinqfields(int32 gridID, char *fieldlist, int32 rank[], - int32 numbertype[]) -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 nFld = 0; /* Number of mappings */ - int32 slen[8]; /* String length array */ - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr;/* Utility string */ - char *ptr[8]; /* String pointer array */ - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"GDinqfields", __FILE__, __LINE__); - return(-1); - } - /* Check for valid grid id */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDinqfields", &fid, &sdInterfaceID, &gdVgrpID); - if (status == 0) - { - - /* If field names, ranks, or number types desired ... */ - /* --------------------------------------------------- */ - if (fieldlist != NULL || rank != NULL || numbertype != NULL) - { - /* Get grid name */ - /* ------------- */ - int gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlstr); - return -1; - } - Vgetname(GDXGrid[gID].IDTable, gridname); - - - /* Get pointers to "DataField" section within SM */ - /* --------------------------------------------- */ - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "DataField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - - - /* If field names are desired then "clear" name buffer */ - /* --------------------------------------------------- */ - if (fieldlist != NULL) - { - fieldlist[0] = 0; - } - - - /* Begin loop through mapping entries in metadata */ - /* ---------------------------------------------- */ - while (1) - { - /* Search for OBJECT string */ - /* ------------------------ */ - metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - - - /* If found within "Data" Field metadata section .. */ - /* ------------------------------------------------ */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - /* Get Fieldnames (if desired) */ - /* --------------------------- */ - if (fieldlist != NULL) - { - /* Check 1st for old meta data then new */ - /* ------------------------------------ */ - EHgetmetavalue(metaptrs, "OBJECT", utlstr); - - /* - * If OBJECT value begins with double quote then old - * metadata, field name is OBJECT value. Otherwise - * search for "DataFieldName" string - */ - - if (utlstr[0] != '"') - { - strcpy(utlstr, "\t\t\t\t"); - strcat(utlstr, "DataFieldName"); - strcat(utlstr, "="); - metaptrs[0] = strstr(metaptrs[0], utlstr); - EHgetmetavalue(metaptrs, "DataFieldName", utlstr); - } - - /* Strip off double quotes */ - /* ----------------------- */ - REMQUOTE(utlstr); - - - /* Add to fieldlist */ - /* ---------------- */ - if (nFld > 0) - { - strcat(fieldlist, ","); - } - strcat(fieldlist, utlstr); - - } - /* Get Numbertype */ - if (numbertype != NULL) - { - EHgetmetavalue(metaptrs, "DataType", utlstr); - numbertype[nFld] = EHnumstr(utlstr); - } - /* - * Get Rank (if desired) by counting # of dimensions in - * "DimList" string - */ - if (rank != NULL) - { - EHgetmetavalue(metaptrs, "DimList", utlstr); - rank[nFld] = EHparsestr(utlstr, ',', ptr, slen); - } - /* Increment number of fields */ - nFld++; - } - else - /* No more fields found */ - { - break; - } - } - free(metabuf); - } - } - - /* Set nFld to -1 if error status exists */ - /* ------------------------------------- */ - if (status == -1) - { - nFld = -1; - } - free(utlstr); - return (nFld); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDnentries | -| | -| DESCRIPTION: Returns number of entries and descriptive string buffer | -| size for a specified entity. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nEntries int32 Number of entries | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| entrycode int32 Entry code | -| HDFE_NENTDIM (0) | -| HDFE_NENTDFLD (4) | -| | -| | -| OUTPUTS: | -| strbufsize int32 Length of comma-separated list | -| (Does not include null-terminator | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nEntries to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize) - -{ - intn status; /* routine return status variable */ - intn i; /* Loop index */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 nEntries = 0; /* Number of entries */ - int32 metaflag; /* Old (0), New (1) metadata flag) */ - int32 nVal = 0; /* Number of strings to search for */ - - char *metabuf = NULL; /* Pointer to structural metadata (SM) */ - char *metaptrs[2] = {NULL, NULL};/* Pointers to begin and end of SM section */ - char gridname[80]; /* Grid Name */ - char *utlstr;/* Utility string */ - char valName[2][32]; /* Strings to search for */ - - memset(valName, 0, sizeof(valName)); - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"GDnentries", __FILE__, __LINE__); - return(-1); - } - status = GDchkgdid(gridID, "GDnentries", &fid, &sdInterfaceID, &gdVgrpID); - - if (status == 0) - { - /* Get grid name */ - int gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlstr); - return -1; - } - - Vgetname(GDXGrid[gID].IDTable, gridname); - - /* Zero out string buffer size */ - *strbufsize = 0; - - - /* - * Get pointer to relevant section within SM and Get names of - * metadata strings to inquire about - */ - switch (entrycode) - { - case HDFE_NENTDIM: - { - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "Dimension", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - - nVal = 1; - strcpy(&valName[0][0], "DimensionName"); - } - break; - - case HDFE_NENTDFLD: - { - metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", - "DataField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - - nVal = 1; - strcpy(&valName[0][0], "DataFieldName"); - } - break; - } - - if (!metabuf || metaptrs[0] == NULL) - { - free(metabuf); - return -1; - } - - /* - * Check for presence of 'GROUP="' string If found then old metadata, - * search on OBJECT string - */ - metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0; - if (metaflag == 0) - { - nVal = 1; - strcpy(&valName[0][0], "\t\tOBJECT"); - } - - - /* Begin loop through entries in metadata */ - /* -------------------------------------- */ - while (1) - { - /* Search for first string */ - strcpy(utlstr, &valName[0][0]); - strcat(utlstr, "="); - metaptrs[0] = strstr(metaptrs[0], utlstr); - - /* If found within relevant metadata section ... */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - for (i = 0; i < nVal; i++) - { - /* - * Get all string values Don't count quotes - */ - EHgetmetavalue(metaptrs, &valName[i][0], utlstr); - *strbufsize += (int32)strlen(utlstr) - 2; - } - /* Increment number of entries */ - nEntries++; - - /* Go to end of OBJECT */ - metaptrs[0] = strstr(metaptrs[0], "END_OBJECT"); - } - else - /* No more entries found */ - { - break; - } - } - free(metabuf); - - - /* Count comma separators & slashes (if mappings) */ - /* ---------------------------------------------- */ - if (nEntries > 0) - { - *strbufsize += nEntries - 1; - *strbufsize += (nVal - 1) * nEntries; - } - } - - - /* Set nEntries to -1 if error status exists */ - /* ----------------------------------------- */ - if (status == -1) - { - nEntries = -1; - } - - free(utlstr); - return (nEntries); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDinqgrid | -| | -| DESCRIPTION: Returns number and names of grid structures in file | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nGrid int32 Number of grid structures in file | -| | -| INPUTS: | -| filename char HDF-EOS filename | -| | -| OUTPUTS: | -| gridlist char List of grid names (comma-separated) | -| strbufsize int32 Length of gridlist | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDinqgrid(const char *filename, char *gridlist, int32 * strbufsize) -{ - int32 nGrid; /* Number of grid structures in file */ - - /* Call "EHinquire" routine */ - /* ------------------------ */ - nGrid = EHinquire(filename, "GRID", gridlist, strbufsize); - - return (nGrid); -} - - -#ifdef HDFEOS_GD_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDsetfillvalue | -| | -| DESCRIPTION: Sets fill value for the specified field. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| fieldname char field name | -| fillval void fill value | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDsetfillvalue(int32 gridID, const char *fieldname, VOIDP fillval) -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 sdid; /* SDS id */ - int32 nt; /* Number type */ - int32 dims[8]; /* Dimensions array */ - int32 dum; /* Dummy variable */ - int32 solo; /* "Solo" (non-merged) field flag */ - - char name[80]; /* Fill value "attribute" name */ - - /* Check for valid grid ID and get SDS interface ID */ - status = GDchkgdid(gridID, "GDsetfillvalue", - &fid, &sdInterfaceID, &gdVgrpID); - - if (status == 0) - { - /* Get field info */ - status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL); - - if (status == 0) - { - /* Get SDS ID and solo flag */ - status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, - &sdid, &dum, &dum, &dum, - dims, &solo); - - /* If unmerged field then call HDF set field routine */ - if (solo == 1) - { - /* status = */ SDsetfillvalue(sdid, fillval); - } - - /* - * Store fill value in attribute. Name is given by fieldname - * prepended with "_FV_" - */ - strcpy(name, "_FV_"); - strcat(name, fieldname); - status = GDwriteattr(gridID, name, nt, 1, fillval); - - - } - else - { - HEpush(DFE_GENAPP, "GDsetfillvalue", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", fieldname); - } - } - return (status); -} -#endif - - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDgetfillvalue | -| | -| DESCRIPTION: Retrieves fill value for a specified field. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| fieldname char field name | -| | -| OUTPUTS: | -| fillval void fill value | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDgetfillvalue(int32 gridID, const char *fieldname, VOIDP fillval) -{ - intn status; /* routine return status variable */ - - int32 nt; /* Number type */ - int32 dims[8]; /* Dimensions array */ - int32 dum; /* Dummy variable */ - - char name[80]; /* Fill value "attribute" name */ - - status = GDchkgdid(gridID, "GDgetfillvalue", &dum, &dum, &dum); - - /* Check for valid grid ID */ - if (status == 0) - { - /* Get field info */ - status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL); - - if (status == 0) - { - /* Read fill value attribute */ - strcpy(name, "_FV_"); - strcat(name, fieldname); - status = GDreadattr(gridID, name, fillval); - } - else - { - HEpush(DFE_GENAPP, "GDgetfillvalue", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", fieldname); - } - - } - return (status); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDdetach | -| | -| DESCRIPTION: Detaches from grid interface and performs file housekeeping. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Sep 96 Joel Gales Setup dim names for SDsetdimname in dimbuf1 rather | -| that utlstr | -| Oct 96 Joel Gales Detach Grid Vgroups | -| Oct 96 Joel Gales "Detach" from SDS | -| Nov 96 Joel Gales Call GDchkgdid to check for proper grid ID | -| Dec 96 Joel Gales Add multiple vertical subsetting garbage collection | -| Oct 98 Abe Taaheri Added GDXRegion[k]->DimNamePtr[i] =0; after freeing | -| memory | -| Sep 99 Abe Taaheri Changed memcpy to memmove because of overlapping | -| source and destination for GDXSDcomb, nameptr, and | -| dimptr. memcpy may cause unexpected results. | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDdetach(int32 gridID) - -{ - intn i; /* Loop index */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - intn j; /* Loop index */ -#endif - intn k; /* Loop index */ - intn status = 0; /* routine return status variable */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - intn statusFill = 0; /* return status from GDgetfillvalue */ -#endif - - int32 *namelen = NULL; /* Pointer to name string length array */ - int32 *dimlen = NULL; /* Pointer to dim string length array */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - int32 slen1[3]; /* String length array 1 */ - int32 slen2[3]; /* String length array 2 */ - int32 nflds; /* Number of fields */ - int32 match[5]; /* Merged field match array */ - int32 cmbfldcnt; /* Number of fields combined */ - int32 sdid; /* SDS ID */ - int32 vgid; /* Vgroup ID */ - int32 dims[3]; /* Dimension array */ -#endif - int32 *offset = NULL; /* Pointer to merged field offset array */ - int32 *indvdims = NULL; /* Pointer to merged field size array */ - int32 sdInterfaceID; /* SDS interface ID */ - int32 gID; /* Grid ID - offset */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - int32 nflds0; /* Number of fields */ -#endif - int32 *namelen0 = NULL; /* Pointer to name string length array */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - int32 rank; /* Rank of merged field */ - int32 truerank; /* True rank of merged field */ -#endif - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - int32 dum; /* Dummy variable */ - -#if defined(HDFEOS_GD_WRITE_SUPPORT) - char *nambuf; /* Pointer to name buffer */ -#endif - char **nameptr = NULL; /* Pointer to name string pointer array */ - char **dimptr = NULL; /* Pointer to dim string pointer array */ - char **nameptr0 = NULL; /* Pointer to name string pointer array */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - char *ptr1[3]; /* String pointer array */ - char *ptr2[3]; /* String pointer array */ - char dimbuf1[128]; /* Dimension buffer 1 */ - char dimbuf2[128]; /* Dimension buffer 2 */ -#endif - char gridname[VGNAMELENMAX + 1]; /* Grid name */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - char *utlbuf; /* Utility buffer */ - char fillval[32];/* Fill value buffer */ -#endif - - - status = GDchkgdid(gridID, "GDdetach", &dum, &sdInterfaceID, &dum); - - if (status == 0) - { - gID = gridID % idOffset; - if (gID >= NGRID) - { - return -1; - } - Vgetname(GDXGrid[gID].IDTable, gridname); - - /* SDS combined fields */ - /* ------------------- */ -#if defined(HDFEOS_GD_WRITE_SUPPORT) - if (strlen(GDXSDname) == 0) -#endif - { -#if defined(HDFEOS_GD_WRITE_SUPPORT) - nflds = 0; -#endif - - /* Allocate "dummy" arrays so free() doesn't bomb later */ - /* ---------------------------------------------------- */ - nameptr = (char **) calloc(1, sizeof(char *)); - if(nameptr == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - return(-1); - } - namelen = (int32 *) calloc(1, sizeof(int32)); - if(namelen == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - return(-1); - } - nameptr0 = (char **) calloc(1, sizeof(char *)); - if(nameptr0 == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - return(-1); - } - namelen0 = (int32 *) calloc(1, sizeof(int32)); - if(namelen0 == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - return(-1); - } - dimptr = (char **) calloc(1, sizeof(char *)); - if(dimptr == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - return(-1); - } - dimlen = (int32 *) calloc(1, sizeof(int32)); - if(dimlen == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - return(-1); - } - offset = (int32 *) calloc(1, sizeof(int32)); - if(offset == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - return(-1); - } - indvdims = (int32 *) calloc(1, sizeof(int32)); - if(indvdims == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - return(-1); - } - } -#if defined(HDFEOS_GD_WRITE_SUPPORT) - else - { - /* - * "Trim Off" trailing "," and ";" in GDXSDname & GDXSDdims - * respectively - */ - GDXSDname[strlen(GDXSDname) - 1] = 0; - GDXSDdims[strlen(GDXSDdims) - 1] = 0; - - - /* Get number of fields from GDXSDname string */ - /* ------------------------------------------ */ - nflds = EHparsestr(GDXSDname, ',', NULL, NULL); - - /* Allocate space for various dynamic arrays */ - /* ----------------------------------------- */ - nameptr = (char **) calloc(nflds, sizeof(char *)); - if(nameptr == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - return(-1); - } - namelen = (int32 *) calloc(nflds, sizeof(int32)); - if(namelen == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - return(-1); - } - nameptr0 = (char **) calloc(nflds, sizeof(char *)); - if(nameptr0 == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - return(-1); - } - namelen0 = (int32 *) calloc(nflds, sizeof(int32)); - if(namelen0 == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - return(-1); - } - dimptr = (char **) calloc(nflds, sizeof(char *)); - if(dimptr == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - return(-1); - } - dimlen = (int32 *) calloc(nflds, sizeof(int32)); - if(dimlen == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - return(-1); - } - offset = (int32 *) calloc(nflds, sizeof(int32)); - if(offset == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - return(-1); - } - indvdims = (int32 *) calloc(nflds, sizeof(int32)); - if(indvdims == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - return(-1); - } - - /* Parse GDXSDname and GDXSDdims strings */ - /* ------------------------------------- */ - nflds = EHparsestr(GDXSDname, ',', nameptr, namelen); - nflds = EHparsestr(GDXSDdims, ';', dimptr, dimlen); - } - - for (i = 0; i < nflds; i++) - { - if (GDXSDcomb[5 * i] != 0 && - GDXSDcomb[5 * i + 3] == GDXGrid[gID].IDTable) - { - nambuf = (char *) calloc(strlen(GDXSDname) + 1, 1); - if(nambuf == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - free(indvdims); - return(-1); - } - utlbuf = (char *) calloc(strlen(GDXSDname) * 2 + 7, 1); - if(utlbuf == NULL) - { - HEpush(DFE_NOSPACE,"GDdetach", __FILE__, __LINE__); - free(nambuf); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - free(indvdims); - return(-1); - } - - for (k = 0; k < (intn)sizeof(dimbuf1); k++) - dimbuf1[k] = 0; - - - /* Load array to match, name & parse dims */ - /* -------------------------------------- */ - memcpy(match, &GDXSDcomb[5 * i], 20); - memcpy(nambuf, nameptr[i], namelen[i]); - - memcpy(dimbuf1, dimptr[i], dimlen[i]); - dum = EHparsestr(dimbuf1, ',', ptr1, slen1); - - - /* Separate combined dimension from others */ - /* --------------------------------------- */ - dimbuf1[slen1[0]] = 0; - - offset[0] = 0; - indvdims[0] = abs(match[0]); - - for (j = i + 1, cmbfldcnt = 0; j < nflds; j++) - { - for (k = 0; k < (intn)sizeof(dimbuf2); k++) - dimbuf2[k] = 0; - memcpy(dimbuf2, dimptr[j], dimlen[j]); - dum = EHparsestr(dimbuf2, ',', ptr2, slen2); - dimbuf2[slen2[0]] = 0; - - - if (GDXSDcomb[5 * j] != 0 && - strcmp(dimbuf1 + slen1[0], - dimbuf2 + slen2[0]) == 0 && - match[1] == GDXSDcomb[5 * j + 1] && - match[2] == GDXSDcomb[5 * j + 2] && - match[3] == GDXSDcomb[5 * j + 3] && - match[4] == GDXSDcomb[5 * j + 4]) - { - /* Add to combined dimension size */ - match[0] += GDXSDcomb[5 * j]; - - /* Concatenate name */ - strcat(nambuf, ","); - memcpy(nambuf + strlen(nambuf), - nameptr[j], namelen[j]); - - /* Store individual dims and dim offsets */ - cmbfldcnt++; - indvdims[cmbfldcnt] = abs(GDXSDcomb[5 * j]); - offset[cmbfldcnt] = - offset[cmbfldcnt - 1] + indvdims[cmbfldcnt - 1]; - - GDXSDcomb[5 * j] = 0; - } - } - - - /* Create SDS */ - /* ---------- */ - nflds0 = EHparsestr(nambuf, ',', nameptr0, namelen0); - - if (abs(match[0]) == 1) - { - for (k = 0; k < 2; k++) - dims[k] = abs(match[k + 1]); - - rank = 2; - - sdid = SDcreate(sdInterfaceID, nambuf, - GDXSDcomb[5 * i + 4], 2, dims); - } - else - { - for (k = 0; k < 3; k++) - dims[k] = abs(match[k]); - - rank = 3; - - if (cmbfldcnt > 0) - { - strcpy(utlbuf, "MRGFLD_"); - memcpy(utlbuf + 7, nameptr0[0], namelen0[0]); - utlbuf[7 + namelen0[0]] = 0; - strcat(utlbuf, ":"); - strcat(utlbuf, nambuf); - - status = EHinsertmeta(sdInterfaceID, gridname, "g", - 6L, utlbuf, NULL); - } - else - { - strcpy(utlbuf, nambuf); - } - - sdid = SDcreate(sdInterfaceID, utlbuf, - GDXSDcomb[5 * i + 4], 3, dims); - - - if (cmbfldcnt > 0) - { - SDsetattr(sdid, "Field Dims", DFNT_INT32, - cmbfldcnt + 1, (VOIDP) indvdims); - - SDsetattr(sdid, "Field Offsets", DFNT_INT32, - cmbfldcnt + 1, (VOIDP) offset); - } - - } - - - - /* Register Dimensions in SDS */ - /* -------------------------- */ - for (k = 0; k < rank; k++) - { - if (rank == 2) - { - memcpy(dimbuf2, ptr1[k + 1], slen1[k + 1]); - dimbuf2[slen1[k + 1]] = 0; - } - else - { - memcpy(dimbuf2, ptr1[k], slen1[k]); - dimbuf2[slen1[k]] = 0; - } - - - if (k == 0 && rank > 2 && cmbfldcnt > 0) - { - snprintf(dimbuf2, sizeof(dimbuf2), "%s%s_%d", "MRGDIM:", - gridname, (int)dims[0]); - } - else - { - strcat(dimbuf2, ":"); - strcat(dimbuf2, gridname); - } - SDsetdimname(SDgetdimid(sdid, k), (char *) dimbuf2); - } - - - - /* Write Fill Value */ - /* ---------------- */ - for (k = 0; k < nflds0; k++) - { - memcpy(utlbuf, nameptr0[k], namelen0[k]); - utlbuf[namelen[k]] = 0; - statusFill = GDgetfillvalue(gridID, utlbuf, fillval); - - if (statusFill == 0) - { - if (cmbfldcnt > 0) - { - dims[0] = indvdims[k]; - truerank = (dims[0] == 1) ? 2 : 3; - EHfillfld(sdid, rank, truerank, - DFKNTsize(match[4]), offset[k], - dims, fillval); - } - else - { - status = SDsetfillvalue(sdid, fillval); - } - } - } - - - vgid = GDXGrid[gID].VIDTable[0]; - Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid)); - SDendaccess(sdid); - - free(nambuf); - free(utlbuf); - - } - } - - - for (i = 0; i < nflds; i++) - { - if (GDXSDcomb[5 * i + 3] == GDXGrid[gID].IDTable) - { - if (i == (nflds - 1)) - { - GDXSDcomb[5 * i] = 0; - *(nameptr[i] - (nflds != 1)) = 0; - *(dimptr[i] - (nflds != 1)) = 0; - } - else - { - /* memcpy(&GDXSDcomb[5 * i], - &GDXSDcomb[5 * (i + 1)], - (512 - i - 1) * 5 * 4);*/ - memmove(&GDXSDcomb[5 * i], - &GDXSDcomb[5 * (i + 1)], - (512 - i - 1) * 5 * 4); - /* memcpy(nameptr[i], - nameptr[i + 1], - nameptr[0] + 2048 - nameptr[i + 1] - 1);*/ - memmove(nameptr[i], - nameptr[i + 1], - nameptr[0] + 2048 - nameptr[i + 1] - 1); - /* memcpy(dimptr[i], - dimptr[i + 1], - dimptr[0] + 2048 * 2 - dimptr[i + 1] - 1);*/ - memmove(dimptr[i], - dimptr[i + 1], - dimptr[0] + 2048 * 2 - dimptr[i + 1] - 1); - } - - i--; - nflds = EHparsestr(GDXSDname, ',', nameptr, namelen); - nflds = EHparsestr(GDXSDdims, ';', dimptr, dimlen); - } - } - - if (nflds != 0) - { - strcat(GDXSDname, ","); - strcat(GDXSDdims, ";"); - } -#endif - - - /* Free up a bunch of dynamically allocated arrays */ - /* ----------------------------------------------- */ - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - free(indvdims); - - - - /* "Detach" from previously attached SDSs */ - /* -------------------------------------- */ - for (k = 0; k < GDXGrid[gID].nSDS; k++) - { - SDendaccess(GDXGrid[gID].sdsID[k]); - } - free(GDXGrid[gID].sdsID); - GDXGrid[gID].sdsID = 0; - GDXGrid[gID].nSDS = 0; - - - - /* Detach Grid Vgroups */ - /* ------------------- */ - Vdetach(GDXGrid[gID].VIDTable[0]); - Vdetach(GDXGrid[gID].VIDTable[1]); - Vdetach(GDXGrid[gID].IDTable); - - GDXGrid[gID].active = 0; - GDXGrid[gID].VIDTable[0] = 0; - GDXGrid[gID].VIDTable[1] = 0; - GDXGrid[gID].IDTable = 0; - GDXGrid[gID].fid = 0; - - - - - /* Free Region Pointers */ - /* -------------------- */ - for (k = 0; k < NGRIDREGN; k++) - { - if (GDXRegion[k] != 0 && - GDXRegion[k]->gridID == gridID) - { - for (i = 0; i < 8; i++) - { - if (GDXRegion[k]->DimNamePtr[i] != 0) - { - free(GDXRegion[k]->DimNamePtr[i]); - GDXRegion[k]->DimNamePtr[i] = 0; - } - } - - free(GDXRegion[k]); - GDXRegion[k] = 0; - } - } - } - return (status); -} - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDclose | -| | -| DESCRIPTION: Closes file. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| fid int32 File ID | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDclose(int32 fid) - -{ - intn status = 0; /* routine return status variable */ - - /* Call EHclose to perform file close */ - /* ---------------------------------- */ - status = EHclose(fid); - - return (status); -} - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDgetdefaults | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| projcode int32 GCTP projection code | -| zonecode int32 UTM zone code | -| projparm float64 Projection parameters | -| spherecode int32 GCTP spheriod code | -| upleftpt float64 upper left corner coordinates | -| lowrightpt float64 lower right corner coordinates | -| | -| | -| OUTPUTS: | -| upleftpt float64 upper left corner coordinates | -| lowrightpt float64 lower right corner coordinates | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Aug 96 Joel Gales Original Programmer | -| Sep 96 Raj Gejjaga Fixed bugs in Polar Stereographic and Goode | | Homolosine default calculations. | -| Sep 96 Raj Gejjaga Added code to compute default boundary points | -| for Lambert Azimuthal Polar and Equatorial | -| projections. | -| Feb 97 Raj Gejjaga Added code to compute default boundary points | -| for Integerized Sinusoidal Grid. Added error | -| handling code. | -| Jun 00 Abe Taaheri Added support for EASE grid | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], - int32 spherecode, float64 upleftpt[], float64 lowrightpt[]) -{ - int32 errorcode = 0, status = 0; - int32(*for_trans[100]) (double, double, double*, double*); - - float64 lon, lat, plat, x, y; - float64 plon, tlon, llon, rlon, pplon, LLon, LLat, RLon, RLat; - - - /* invoke GCTP initialization routine */ - /* ---------------------------------- */ - for_init(projcode, zonecode, projparm, spherecode, NULL, NULL, - &errorcode, for_trans); - - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - /* Compute Default Boundary Points for EASE Grid */ - /* Use Global coverage */ - /* ------------------------------------------------------ */ - if (projcode == GCTP_BCEA && - upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0) - { - upleftpt[0] = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LON, HDFE_DEG_DMS); - upleftpt[1] = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LAT, HDFE_DEG_DMS); - lowrightpt[0] = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LON, HDFE_DEG_DMS); - lowrightpt[1] = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LAT, HDFE_DEG_DMS); - } - -/* Compute Default Boundary Points for CEA */ - /* --------------------------------------------*/ - if (projcode == GCTP_CEA && - upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0) - { - LLon = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LON, HDFE_DEG_RAD); - LLat = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LAT, HDFE_DEG_RAD); - RLon = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LON, HDFE_DEG_RAD); - RLat = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LAT, HDFE_DEG_RAD); - - errorcode = for_trans[projcode] (LLon, LLat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - upleftpt[0] = x; - upleftpt[1] = y; - - errorcode = for_trans[projcode] (RLon, RLat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - lowrightpt[0] = x; - lowrightpt[1] = y; - - } - - - /* Compute Default Boundary Points for Polar Sterographic */ - /* ------------------------------------------------------ */ - if (projcode == GCTP_PS && - upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0) - { - /* - * Convert the longitude and latitude from the DMS to decimal degree - * format. - */ - plon = EHconvAng(projparm[4], HDFE_DMS_DEG); - plat = EHconvAng(projparm[5], HDFE_DMS_DEG); - - /* - * Compute the longitudes at 90, 180 and 270 degrees from the central - * longitude. - */ - - if (plon <= 0.0) - { - tlon = 180.0 + plon; - pplon = plon + 360.0; - } - else - { - tlon = plon - 180.0; - pplon = plon; - } - - rlon = pplon + 90.0; - if (rlon > 360.0) - rlon = rlon - 360; - - if (rlon > 180.0) - rlon = rlon - 360.0; - - if (rlon <= 0.0) - llon = 180.0 + rlon; - else - llon = rlon - 180.0; - - - /* Convert all four longitudes from decimal degrees to radians */ - plon = EHconvAng(plon, HDFE_DEG_RAD); - tlon = EHconvAng(tlon, HDFE_DEG_RAD); - llon = EHconvAng(llon, HDFE_DEG_RAD); - rlon = EHconvAng(rlon, HDFE_DEG_RAD); - - errorcode = for_trans[projcode] (llon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[0] = x; - - errorcode = for_trans[projcode] (rlon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - lowrightpt[0] = x; - - /* - * Compute the upperleft and lowright y values based on the south or - * north polar projection - */ - - if (plat < 0.0) - { - errorcode = for_trans[projcode] (plon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[1] = y; - - errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - lowrightpt[1] = y; - - } - else - { - errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[1] = y; - - errorcode = for_trans[projcode] (plon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - lowrightpt[1] = y; - - } - } - - - /* Compute Default Boundary Points for Goode Homolosine */ - /* ---------------------------------------------------- */ - if (projcode == GCTP_GOOD && - upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0) - { - lon = EHconvAng(-180, HDFE_DEG_RAD); - lat = 0.0; - - errorcode = for_trans[projcode] (lon, lat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[0] = -fabs(x); - lowrightpt[0] = +fabs(x); - - lat = EHconvAng(90, HDFE_DEG_RAD); - - errorcode = for_trans[projcode] (lon, lat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[1] = +fabs(y); - lowrightpt[1] = -fabs(y); - } - - /* Compute Default Boundary Points for Lambert Azimuthal */ - /* ----------------------------------------------------- */ - if (projcode == GCTP_LAMAZ && - upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0) - { - /* - * Convert the longitude and latitude from the DMS to decimal degree - * format. - */ - plon = EHconvAng(projparm[4], HDFE_DMS_DEG); - plat = EHconvAng(projparm[5], HDFE_DMS_DEG); - - /* - * Compute the longitudes at 90, 180 and 270 degrees from the central - * longitude. - */ - - if (plon <= 0.0) - { - tlon = 180.0 + plon; - pplon = plon + 360.0; - } - else - { - tlon = plon - 180.0; - pplon = plon; - } - - rlon = pplon + 90.0; - if (rlon > 360.0) - rlon = rlon - 360; - - if (rlon > 180.0) - rlon = rlon - 360.0; - - if (rlon <= 0.0) - llon = 180.0 + rlon; - else - llon = rlon - 180.0; - - /* Convert all four longitudes from decimal degrees to radians */ - plon = EHconvAng(plon, HDFE_DEG_RAD); - tlon = EHconvAng(tlon, HDFE_DEG_RAD); - llon = EHconvAng(llon, HDFE_DEG_RAD); - rlon = EHconvAng(rlon, HDFE_DEG_RAD); - - errorcode = for_trans[projcode] (llon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[0] = x; - - errorcode = for_trans[projcode] (rlon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - lowrightpt[0] = x; - - /* - * Compute upperleft and lowerright values based on whether the - * projection is south polar, north polar or equatorial - */ - - if (plat == -90.0) - { - errorcode = for_trans[projcode] (plon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[1] = y; - - errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - lowrightpt[1] = y; - } - else if (plat == 90.0) - { - errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[1] = y; - - errorcode = for_trans[projcode] (plon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - lowrightpt[1] = y; - } - else - { - lat = EHconvAng(90, HDFE_DEG_RAD); - errorcode = for_trans[projcode] (plon, lat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[1] = y; - - lat = EHconvAng(-90, HDFE_DEG_RAD); - errorcode = for_trans[projcode] (plon, lat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - lowrightpt[1] = y; - } - } - - /* Compute Default Boundary Points for Integerized Sinusoidal Grid */ - /* --------------------------------------------------------------- */ - if (((projcode == GCTP_ISINUS) || (projcode == GCTP_ISINUS1)) && - upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0) - { - /* - * Convert the longitude and latitude from the DMS to decimal degree - * format. - */ - plon = EHconvAng(projparm[4], HDFE_DMS_DEG); - /*plat = EHconvAng(projparm[5], HDFE_DMS_DEG); */ - - /* - * Compute the longitudes at 90, 180 and 270 degrees from the central - * longitude. - */ - - if (plon <= 0.0) - { - tlon = 180.0 + plon; - pplon = plon + 360.0; - } - else - { - tlon = plon - 180.0; - pplon = plon; - } - - rlon = pplon + 90.0; - if (rlon > 360.0) - rlon = rlon - 360; - - if (rlon > 180.0) - rlon = rlon - 360.0; - - if (rlon <= 0.0) - llon = 180.0 + rlon; - else - llon = rlon - 180.0; - - /* Convert all four longitudes from decimal degrees to radians */ - plon = EHconvAng(plon, HDFE_DEG_RAD); - tlon = EHconvAng(tlon, HDFE_DEG_RAD); - llon = EHconvAng(llon, HDFE_DEG_RAD); - rlon = EHconvAng(rlon, HDFE_DEG_RAD); - - errorcode = for_trans[projcode] (llon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - upleftpt[0] = x; - - errorcode = for_trans[projcode] (rlon, 0.0, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } + /* Call GDwrrdattr routine to read attribute */ + /* ----------------------------------------- */ + status = GDwrrdattr(gridID, attrname, dum, dum, "r", datbuf); - lowrightpt[0] = x; + return (status); +} - lat = EHconvAng(90, HDFE_DEG_RAD); - errorcode = for_trans[projcode] (plon, lat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - upleftpt[1] = y; - lat = EHconvAng(-90, HDFE_DEG_RAD); - errorcode = for_trans[projcode] (plon, lat, &x, &y); - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - lowrightpt[1] = y; - } - return (errorcode); -} /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDll2ij | +| FUNCTION: GDattrinfo | | | | DESCRIPTION: | | | @@ -6441,1318 +2512,705 @@ GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| projcode int32 GCTP projection code | -| zonecode int32 UTM zone code | -| projparm float64 Projection parameters | -| spherecode int32 GCTP spheriod code | -| xdimsize int32 xdimsize from GDcreate | -| ydimsize int32 ydimsize from GDcreate | -| upleftpt float64 upper left corner coordinates | -| lowrightpt float64 lower right corner coordinates | -| npnts int32 number of lon-lat points | -| longitude float64 longitude array (radians) | -| latitude float64 latitude array (radians) | +| gridID int32 grid structure ID | +| attrname char attribute name | | | | OUTPUTS: | -| row int32 Row array | -| col int32 Column array | -| xval float64 X value array | -| yval float64 Y value array | +| numbertype int32 attribute HDF numbertype | +| count int32 Number of attribute elements | | | | | +| OUTPUTS: | +| None | +| | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Return x and y values if requested | -| Jun 00 Abe Taaheri Added support for EASE grid | +| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -static intn -GDll2ij(int32 projcode, int32 zonecode, float64 projparm[], - int32 spherecode, int32 xdimsize, int32 ydimsize, - float64 upleftpt[], float64 lowrightpt[], - int32 npnts, float64 longitude[], float64 latitude[], - int32 row[], int32 col[], float64 xval[], float64 yval[]) - - +intn +GDattrinfo(int32 gridID, const char *attrname, int32 * numbertype, int32 * count) { - intn i; /* Loop index */ intn status = 0; /* routine return status variable */ - int32 errorcode = 0; /* GCTP error code */ - int32(*for_trans[100]) (double, double, double*, double*); /* GCTP function pointer */ - - float64 xVal; /* Scaled x distance */ - float64 yVal; /* Scaled y distance */ - float64 xMtr; /* X value in meters from GCTP */ - float64 yMtr; /* Y value in meters from GCTP */ - float64 lonrad0; /* Longitude in radians of upleft point */ - float64 latrad0 = 0; /* Latitude in radians of upleft point */ - float64 lonrad; /* Longitude in radians of point */ - float64 latrad; /* Latitude in radians of point */ - float64 scaleX; /* X scale factor */ - float64 scaleY; /* Y scale factor */ - float64 xMtr0 = 0, xMtr1, yMtr0 = 0, yMtr1; - float64 lonrad1; /* Longitude in radians of lowright point */ - - /* If projection not GEO call GCTP initialization routine */ - /* ------------------------------------------------------ */ - if (projcode != GCTP_GEO) - { - for_init(projcode, zonecode, projparm, spherecode, NULL, NULL, - &errorcode, for_trans); - - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - } - } - - - if (status == 0) - { - /* GEO projection */ - /* -------------- */ - if (projcode == GCTP_GEO) - { - /* Convert upleft and lowright X coords from DMS to radians */ - /* -------------------------------------------------------- */ - lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); - lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); - - /* Compute x scale factor */ - /* ---------------------- */ - scaleX = (lonrad - lonrad0) / xdimsize; - - - /* Convert upleft and lowright Y coords from DMS to radians */ - /* -------------------------------------------------------- */ - latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD); - latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD); - - - /* Compute y scale factor */ - /* ---------------------- */ - scaleY = (latrad - latrad0) / ydimsize; - } - - /* BCEA projection */ - /* -------------- */ - else if ( projcode == GCTP_BCEA) - { - /* Convert upleft and lowright X coords from DMS to radians */ - /* -------------------------------------------------------- */ - - lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); - lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); - - /* Convert upleft and lowright Y coords from DMS to radians */ - /* -------------------------------------------------------- */ - latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD); - latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD); - - /* Convert from lon/lat to meters(or whatever unit is, i.e unit - of r_major and r_minor) using GCTP */ - /* ----------------------------------------- */ - errorcode = for_trans[projcode] (lonrad0, latrad0, &xMtr0, &yMtr0); - - - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - /* Convert from lon/lat to meters(or whatever unit is, i.e unit - of r_major and r_minor) using GCTP */ - /* ----------------------------------------- */ - errorcode = for_trans[projcode] (lonrad, latrad, &xMtr1, &yMtr1); - - - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - /* Compute x scale factor */ - /* ---------------------- */ - scaleX = (xMtr1 - xMtr0) / xdimsize; - - /* Compute y scale factor */ - /* ---------------------- */ - scaleY = (yMtr1 - yMtr0) / ydimsize; - } - else - { - /* Non-GEO, Non_BCEA projections */ - /* ---------------------------- */ + int32 fid; /* HDF-EOS file ID */ + int32 attrVgrpID; /* Grid attribute ID */ + int32 dum; /* dummy variable */ + int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - /* Compute x & y scale factors */ - /* --------------------------- */ - scaleX = (lowrightpt[0] - upleftpt[0]) / xdimsize; - scaleY = (lowrightpt[1] - upleftpt[1]) / ydimsize; - } + status = GDchkgdid(gridID, "GDattrinfo", &fid, &dum, &dum); + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + attrVgrpID = GDXGrid[gID].VIDTable[1]; + status = EHattrinfo(fid, attrVgrpID, attrname, numbertype, + count); - /* Loop through all points */ - /* ----------------------- */ - for (i = 0; i < npnts; i++) - { - /* Convert lon & lat from decimal degrees to radians */ - /* ------------------------------------------------- */ - lonrad = EHconvAng(longitude[i], HDFE_DEG_RAD); - latrad = EHconvAng(latitude[i], HDFE_DEG_RAD); + return (status); +} - /* GEO projection */ - /* -------------- */ - if (projcode == GCTP_GEO) - { - /* allow map to span dateline */ - lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); - lonrad1 = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); - /* if time-line is passed */ - if(lonrad < lonrad1) - { - if (lonrad < lonrad0) lonrad += 2.0 * M_PI; - if (lonrad > lonrad1) lonrad -= 2.0 * M_PI; - } - /* Compute scaled distance to point from origin */ - /* -------------------------------------------- */ - xVal = (lonrad - lonrad0) / scaleX; - yVal = (latrad - latrad0) / scaleY; - } - else - { - /* Convert from lon/lat to meters using GCTP */ - /* ----------------------------------------- */ - errorcode = for_trans[projcode] (lonrad, latrad, &xMtr, &yMtr); - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - /*status = -1; - HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); */ /* Bruce Beaumont */ - xVal = -2147483648.0; /* Bruce Beaumont */ - yVal = -2147483648.0; /* Bruce Beaumont */ - }/* (Note: MAXLONG is defined as 2147483647.0 in - function cproj.c of GCTP) */ - else { - /* if projection is BCEA normalize x and y by cell size and - measure it from the upperleft corner of the grid */ - /* Compute scaled distance to point from origin */ - /* -------------------------------------------- */ - if( projcode == GCTP_BCEA) - { - xVal = (xMtr - xMtr0) / scaleX; - yVal = (yMtr - yMtr0) / scaleY; - } - else - { - xVal = (xMtr - upleftpt[0]) / scaleX; - yVal = (yMtr - upleftpt[1]) / scaleY; - } - } - } +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: GDinqattrs | +| | +| DESCRIPTION: | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| nattr int32 Number of attributes in swath struct | +| | +| INPUTS: | +| grid ID int32 grid structure ID | +| | +| OUTPUTS: | +| attrnames char Attribute names in swath struct | +| (Comma-separated list) | +| strbufsize int32 Attributes name list string length | +| | +| OUTPUTS: | +| None | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Jun 96 Joel Gales Original Programmer | +| Oct 96 Joel Gales Initialize nattr | +| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +int32 +GDinqattrs(int32 gridID, char *attrnames, int32 * strbufsize) +{ + intn status; /* routine return status variable */ + int32 fid; /* HDF-EOS file ID */ + int32 attrVgrpID; /* Grid attribute ID */ + int32 dum; /* dummy variable */ + int32 nattr = 0; /* Number of attributes */ + int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - /* Compute row and col from scaled distance */ - /* ---------------------------------------- */ - col[i] = (int32) xVal; - row[i] = (int32) yVal; - /* Store scaled distances if requested */ - /* ----------------------------------- */ - if (xval != NULL) - { - xval[i] = xVal; - } + /* Check Grid id */ + status = GDchkgdid(gridID, "GDinqattrs", &fid, &dum, &dum); - if (yval != NULL) - { - yval[i] = yVal; - } - } + if (status == 0) + { + int gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; } - return (status); + attrVgrpID = GDXGrid[gID].VIDTable[1]; + nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize); + } + + return (nattr); } -#ifdef UNUSED_BY_GDAL + + + +#define REMQUOTE(x) do { \ + char* l_x = x; \ + const size_t l_x_len = strlen(l_x); \ + if (l_x_len >= 2 && l_x[0] == '"' && l_x[l_x_len - 1] == '"') {\ + memmove(l_x, l_x + 1, l_x_len - 2); \ + l_x[l_x_len - 2] = 0; \ + } \ + } while(0) + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDrs2ll | +| FUNCTION: GDinqdims | | | -| DESCRIPTION: Converts EASE grid's (r,s) coordinates to longitude and | -| latitude (in decimal degrees). | +| DESCRIPTION: Retrieve information about all dimensions defined in a grid. | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nDim int32 Number of defined dimensions | | | | INPUTS: | -| projcode int32 GCTP projection code | -| projparm float64 Projection parameters | -| xdimsize int32 xdimsize from GDcreate | -| ydimsize int32 ydimsize from GDcreate | -| pixcen int32 pixel center code | -| npnts int32 number of lon-lat points | -| s int32 s coordinate | -| r int32 r coordinate | -| pixcen int32 Code from GDpixreginfo | -| pixcnr int32 Code from GDorigininfo | -| upleft float64 upper left corner coordinates (DMS) | -| lowright float64 lower right corner coordinates (DMS) | +| gridID int32 grid structure ID | +| | +| OUTPUTS: | +| dimnames char Dimension names (comma-separated) | +| dims int32 Dimension values | | | | | | OUTPUTS: | -| longitude float64 longitude array (decimal degrees) | -| latitude float64 latitude array (decimal degrees) | +| None | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Jul 00 Abe Taaheri Original Programmer | +| Jun 96 Joel Gales Original Programmer | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nDim to -1 if status = -1 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -GDrs2ll(int32 projcode, float64 projparm[], - int32 xdimsize, int32 ydimsize, - float64 upleft[], float64 lowright[], - int32 npnts, float64 r[], float64 s[], - float64 longitude[], float64 latitude[], int32 pixcen, int32 pixcnr) +int32 +GDinqdims(int32 gridID, char *dimnames, int32 dims[]) { - intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - - int32 errorcode = 0; /* GCTP error code */ - int32(*inv_trans[100]) (double, double, double*, double*); /* GCTP function pointer */ - - float64 pixadjX = 0.0; /* Pixel adjustment (x) */ - float64 pixadjY = 0.0; /* Pixel adjustment (y) */ - float64 lonrad; /* Longitude in radians of point */ - float64 latrad; /* Latitude in radians of point */ - float64 xMtr; /* X value in meters from GCTP */ - float64 yMtr; /* Y value in meters from GCTP */ - float64 epsilon; - float64 beta; - float64 qp_cea = 0; - float64 kz_cea = 0; - float64 eccen, eccen_sq; - float64 phi1, sinphi1, cosphi1; - float64 scaleX, scaleY; - - int32 zonecode=0; - - int32 spherecode=0; - float64 lon[2],lat[2]; - float64 xcor[2], ycor[2]; - int32 nlatlon; - - /* If projection is BCEA define scale, r0 and s0 */ - if (projcode == GCTP_BCEA) - { - eccen_sq = 1.0 - SQUARE(projparm[1]/projparm[0]); - eccen = sqrt(eccen_sq); - if(eccen < 0.00001) - { - qp_cea = 2.0; - } - else - { - qp_cea = - (1.0 - eccen_sq)*((1.0/(1.0 - eccen_sq))-(1.0/(2.0*eccen))* - log((1.0 - eccen)/(1.0 + eccen))); - } - phi1 = EHconvAng(projparm[5],HDFE_DMS_RAD); - cosphi1 = cos(phi1); - sinphi1 = sin(phi1); - kz_cea = cosphi1/(sqrt(1.0 - (eccen_sq*sinphi1*sinphi1))); - } - + intn status; /* routine return status variable */ + int32 fid; /* HDF-EOS file ID */ + int32 sdInterfaceID; /* HDF SDS interface ID */ + int32 gdVgrpID; /* Grid root Vgroup ID */ + int32 size; /* Dimension size */ + int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 nDim = 0; /* Number of dimensions */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char gridname[80]; /* Grid Name */ + char *utlstr;/* Utility string */ - /* Compute adjustment of position within pixel */ - /* ------------------------------------------- */ - if (pixcen == HDFE_CENTER) + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) { - /* Pixel defined at center */ - /* ----------------------- */ - pixadjX = 0.5; - pixadjY = 0.5; + HEpush(DFE_NOSPACE,"GDinqdims", __FILE__, __LINE__); + return(-1); } - else + /* Check for valid grid id */ + /* ----------------------- */ + status = GDchkgdid(gridID, "GDinqdims", &fid, &sdInterfaceID, &gdVgrpID); + + if (status == 0) { - switch (pixcnr) + /* If dimension names or sizes are requested */ + /* ----------------------------------------- */ + if (dimnames != NULL || dims != NULL) { - case HDFE_GD_UL: - { - /* Pixel defined at upper left corner */ - /* ---------------------------------- */ - pixadjX = 0.0; - pixadjY = 0.0; - break; - } - - case HDFE_GD_UR: - { - /* Pixel defined at upper right corner */ - /* ----------------------------------- */ - pixadjX = 1.0; - pixadjY = 0.0; - break; - } - - case HDFE_GD_LL: - { - /* Pixel defined at lower left corner */ - /* ---------------------------------- */ - pixadjX = 0.0; - pixadjY = 1.0; - break; - } - - case HDFE_GD_LR: + /* Get grid name */ + /* ------------- */ + int gID = gridID % idOffset; + if (gID >= NGRID) { - /* Pixel defined at lower right corner */ - /* ----------------------------------- */ - pixadjX = 1.0; - pixadjY = 1.0; - break; + free(utlstr); + return -1; } + Vgetname(GDXGrid[gID].IDTable, gridname); - } - } - - /* If projection is BCEA call GCTP initialization routine */ - /* ------------------------------------------------------ */ - if (projcode == GCTP_BCEA) - { - - inv_init(projcode, 0, projparm, 0, NULL, NULL, - &errorcode, inv_trans); - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - } - else - { - /* For each point ... */ - /* ------------------ */ - for (i = 0; i < npnts; i++) + /* Get pointers to "Dimension" section within SM */ + /* --------------------------------------------- */ + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + "Dimension", metaptrs); + if(metabuf == NULL) { - /* Convert from EASE grid's (r,s) to lon/lat (radians) - using GCTP */ - /* --------------------------------------------------- */ - nlatlon = 2; - lon[0] = upleft[0]; - lon[1] = lowright[0]; - lat[0] = upleft[1]; - lat[1] = lowright[1]; - status = - GDll2mm_cea(projcode,zonecode,spherecode,projparm, - xdimsize, ydimsize, - upleft, lowright, nlatlon, - lon, lat, - xcor, ycor, &scaleX, &scaleY); - - if (status == -1) - { - HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__); - return (status); - } + free(utlstr); + return(-1); + } - xMtr = (r[i]/ scaleX + pixadjX - 0.5)* scaleX; - yMtr = - (s[i]/fabs(scaleY) + pixadjY - 0.5)* fabs(scaleY); + /* If dimension names are requested then "clear" name buffer */ + /* --------------------------------------------------------- */ + if (dimnames != NULL) + { + dimnames[0] = 0; + } - /* allow .5 cell tolerance in arcsin function - (used in bceainv function) so that grid - coordinates which are less than .5 cells - above 90.00N or below 90.00S are given lat of 90.00 - */ + while (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) + { + strcpy(utlstr, "\t\tOBJECT="); + metaptrs[0] = strstr(metaptrs[0], utlstr); + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) + { + /* Get Dimension Name */ + /* ------------------ */ + if (dimnames != NULL) + { + /* Check 1st for old meta data then new */ + /* ------------------------------------ */ + EHgetmetavalue(metaptrs, "OBJECT", utlstr); + if (utlstr[0] != '"') + { + metaptrs[0] = + strstr(metaptrs[0], "\t\t\t\tDimensionName="); + EHgetmetavalue(metaptrs, "DimensionName", utlstr); + } - epsilon = 1 + 0.5 * (fabs(scaleY)/projparm[0]); - beta = 2.0 * (yMtr - projparm[7]) * kz_cea/(projparm[0] * qp_cea); + /* Strip off double quotes */ + /* ----------------------- */ + memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); + utlstr[strlen(utlstr) - 2] = 0; - if( fabs (beta) > epsilon) - { - status = -1; - HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__); - HEreport("GCTP Error: %s %s %s\n", "grid coordinates", - "are more than .5 cells", - "above 90.00N or below 90.00S. "); - return (status); - } - else if( beta <= -1) - { - errorcode = inv_trans[projcode] (xMtr, 0.0, - &lonrad, &latrad); - latrad = - M_PI/2; - } - else if( beta >= 1) - { - errorcode = inv_trans[projcode] (xMtr, 0.0, - &lonrad, &latrad); - latrad = M_PI/2; - } - else - { - errorcode = inv_trans[projcode] (xMtr, yMtr, - &lonrad, &latrad); - } + if (nDim > 0) + { + strcat(dimnames, ","); + } + strcat(dimnames, utlstr); + } - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDrs2ll", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); + /* Get Dimension Size */ + /* ------------------ */ + if (dims != NULL) + { + EHgetmetavalue(metaptrs, "Size", utlstr); + size = atoi(utlstr); + dims[nDim] = size; + } + nDim++; } - - /* Convert from radians to decimal degrees */ - /* --------------------------------------- */ - longitude[i] = EHconvAng(lonrad, HDFE_RAD_DEG); - latitude[i] = EHconvAng(latrad, HDFE_RAD_DEG); } + free(metabuf); + } } + /* Set nDim to -1 if error status exists */ + /* ------------------------------------- */ + if (status == -1) + { + nDim = -1; + } + free(utlstr); + return (nDim); +} + + - return (status); -} -#endif -#ifdef HDFEOS_GD_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: lamaxDxDtheta | +| FUNCTION: GDinqfields | | | -| DESCRIPTION: Partial derivative along longitude line for Lambert Azimuthal | +| DESCRIPTION: Retrieve information about all data fields defined in a grid. | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| float64 Dx/D(theta) for LAMAZ projection | +| nFld int32 Number of fields in swath | | | | INPUTS: | -| parms float64 Parameters defining partial derivative | +| gridID int32 grid structure ID | +| | | | | OUTPUTS: | -| None | +| fieldlist char Field names (comma-separated) | +| rank int32 Array of ranks | +| numbertype int32 Array of HDF number types | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Nov 96 Joel Gales Original Programmer | +| Jun 96 Joel Gales Original Programmer | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nFld to -1 if status = -1 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -static float64 -lamazDxDtheta(float64 parms[]) +int32 +GDinqfields(int32 gridID, char *fieldlist, int32 rank[], + int32 numbertype[]) { - float64 snTheta, sn2Theta, snTheta1, csTheta1, csLamda; - - snTheta = sin(EHconvAng(parms[0], HDFE_DEG_RAD)); - sn2Theta = sin(2 * EHconvAng(parms[0], HDFE_DEG_RAD)); - snTheta1 = sin(EHconvAng(parms[1], HDFE_DEG_RAD)); - csTheta1 = cos(EHconvAng(parms[1], HDFE_DEG_RAD)); - csLamda = cos(EHconvAng(parms[2], HDFE_DEG_RAD) - - EHconvAng(parms[3], HDFE_DEG_RAD)); - - return (4 * snTheta + - (csTheta1 * csLamda * sn2Theta) + - (2 * snTheta1 * (1 + (snTheta * snTheta)))); -} + intn status; /* routine return status variable */ + int32 fid; /* HDF-EOS file ID */ + int32 sdInterfaceID; /* HDF SDS interface ID */ + int32 gdVgrpID; /* Grid root Vgroup ID */ + int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 nFld = 0; /* Number of mappings */ + int32 slen[8]; /* String length array */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char gridname[80]; /* Grid Name */ + char *utlstr;/* Utility string */ + char *ptr[8]; /* String pointer array */ + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) + { + HEpush(DFE_NOSPACE,"GDinqfields", __FILE__, __LINE__); + return(-1); + } + /* Check for valid grid id */ + /* ----------------------- */ + status = GDchkgdid(gridID, "GDinqfields", &fid, &sdInterfaceID, &gdVgrpID); + if (status == 0) + { -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: lamaxDxDlamda | -| | -| DESCRIPTION: Partial derivative along latitude line for Lambert Azimuthal | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| float64 Dx/D(lamda) for LAMAZ projection | -| | -| INPUTS: | -| parms float64 Parameters defining partial derivative | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Nov 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static float64 -lamazDxDlamda(float64 parms[]) -{ - float64 snTheta, csTheta, snTheta1, csTheta1, csLamda; - float64 cs, sn; + /* If field names, ranks, or number types desired ... */ + /* --------------------------------------------------- */ + if (fieldlist != NULL || rank != NULL || numbertype != NULL) + { + /* Get grid name */ + /* ------------- */ + int gID = gridID % idOffset; + if (gID >= NGRID) + { + free(utlstr); + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); - snTheta = sin(EHconvAng(parms[2], HDFE_DEG_RAD)); - csTheta = cos(EHconvAng(parms[2], HDFE_DEG_RAD)); - snTheta1 = sin(EHconvAng(parms[1], HDFE_DEG_RAD)); - csTheta1 = cos(EHconvAng(parms[1], HDFE_DEG_RAD)); - csLamda = cos(EHconvAng(parms[0], HDFE_DEG_RAD) - - EHconvAng(parms[3], HDFE_DEG_RAD)); - cs = csTheta * csTheta1; - sn = snTheta * snTheta1; + /* Get pointers to "DataField" section within SM */ + /* --------------------------------------------- */ + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + "DataField", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } - return (cs + (2 * (1 + sn) + (cs * csLamda)) * csLamda); -} + /* If field names are desired then "clear" name buffer */ + /* --------------------------------------------------- */ + if (fieldlist != NULL) + { + fieldlist[0] = 0; + } -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: lamaxDyDtheta | -| | -| DESCRIPTION: Partial derivative along longitude line for Lambert Azimuthal | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| float64 Dy/D(theta) for LAMAZ projection | -| | -| INPUTS: | -| parms float64 Parameters defining partial derivative | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Nov 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static float64 -lamazDyDtheta(float64 parms[]) -{ - float64 snTheta, csTheta, snTheta1, csTheta1, csLamda; - float64 sn2, cs2, sndiff; - - snTheta = sin(EHconvAng(parms[0], HDFE_DEG_RAD)); - csTheta = cos(EHconvAng(parms[0], HDFE_DEG_RAD)); - snTheta1 = sin(EHconvAng(parms[1], HDFE_DEG_RAD)); - csTheta1 = cos(EHconvAng(parms[1], HDFE_DEG_RAD)); - csLamda = cos(EHconvAng(parms[2], HDFE_DEG_RAD) - - EHconvAng(parms[3], HDFE_DEG_RAD)); - - sn2 = snTheta1 * snTheta; - cs2 = csTheta1 * csTheta; - sndiff = snTheta1 - snTheta; - - return (cs2 * (sn2 * (1 + (csLamda * csLamda)) + 2) + - csLamda * (2 * (1 + sn2 * sn2) - (sndiff * sndiff))); -} + /* Begin loop through mapping entries in metadata */ + /* ---------------------------------------------- */ + while (1) + { + /* Search for OBJECT string */ + /* ------------------------ */ + metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); + /* If found within "Data" Field metadata section .. */ + /* ------------------------------------------------ */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) + { + /* Get Fieldnames (if desired) */ + /* --------------------------- */ + if (fieldlist != NULL) + { + /* Check 1st for old meta data then new */ + /* ------------------------------------ */ + EHgetmetavalue(metaptrs, "OBJECT", utlstr); -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: homDyDtheta | -| | -| DESCRIPTION: Partial derivative along longitude line for Oblique Mercator | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| float64 Dx/D(theta) for HOM projection | -| | -| INPUTS: | -| parms float64 Parameters defining partial derivative | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Mar 97 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static float64 -homDyDtheta(float64 parms[]) -{ - float64 tnTheta, tnTheta1, snLamda; + /* + * If OBJECT value begins with double quote then old + * metadata, field name is OBJECT value. Otherwise + * search for "DataFieldName" string + */ + + if (utlstr[0] != '"') + { + strcpy(utlstr, "\t\t\t\t"); + strcat(utlstr, "DataFieldName"); + strcat(utlstr, "="); + metaptrs[0] = strstr(metaptrs[0], utlstr); + EHgetmetavalue(metaptrs, "DataFieldName", utlstr); + } + + /* Strip off double quotes */ + /* ----------------------- */ + REMQUOTE(utlstr); + + + /* Add to fieldlist */ + /* ---------------- */ + if (nFld > 0) + { + strcat(fieldlist, ","); + } + strcat(fieldlist, utlstr); - tnTheta = tan(EHconvAng(parms[0], HDFE_DEG_RAD)); - tnTheta1 = tan(EHconvAng(parms[1], HDFE_DEG_RAD)); - snLamda = cos(EHconvAng(parms[2], HDFE_DEG_RAD) - - EHconvAng(parms[3], HDFE_DEG_RAD)); + } + /* Get Numbertype */ + if (numbertype != NULL) + { + EHgetmetavalue(metaptrs, "DataType", utlstr); + numbertype[nFld] = EHnumstr(utlstr); + } + /* + * Get Rank (if desired) by counting # of dimensions in + * "DimList" string + */ + if (rank != NULL) + { + EHgetmetavalue(metaptrs, "DimList", utlstr); + rank[nFld] = EHparsestr(utlstr, ',', ptr, slen); + } + /* Increment number of fields */ + nFld++; + } + else + /* No more fields found */ + { + break; + } + } + free(metabuf); + } + } - return (tnTheta * snLamda + tnTheta1); + /* Set nFld to -1 if error status exists */ + /* ------------------------------------- */ + if (status == -1) + { + nFld = -1; + } + free(utlstr); + return (nFld); } + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDtangentpnts | +| FUNCTION: GDnentries | | | -| DESCRIPTION: Finds tangent points along lon/lat lines | +| DESCRIPTION: Returns number of entries and descriptive string buffer | +| size for a specified entity. | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nEntries int32 Number of entries | | | | INPUTS: | -| projcode int32 Projection code | -| projparm float64 Projection parameters | -| cornerlon float64 dec deg Longitude of opposite corners of box | -| cornerlat float64 dec deg Latitude of opposite corners of box | -| longitude float64 dec deg Longitude of points to check | -| latitude float64 dec deg Latitude of points to check | +| gridID int32 grid structure ID | +| entrycode int32 Entry code | +| HDFE_NENTDIM (0) | +| HDFE_NENTDFLD (4) | | | | | | OUTPUTS: | -| npnts int32 Number of points to check in subset | +| strbufsize int32 Length of comma-separated list | +| (Does not include null-terminator | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Nov 96 Joel Gales Original Programmer | -| Mar 97 Joel Gales Add support for LAMCC, POLYC, TM | -| Aug 99 Abe Taaheri Add support for ALBERS, and MERCAT projections. | -| Also changed mistyped bisectParm[2] to | -| bisectParm[3] for HOM projection. | -| Jun 00 Abe Taaheri Added support for EASE grid | +| Jun 96 Joel Gales Original Programmer | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nEntries to -1 if status = -1 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -static intn -GDtangentpnts(int32 projcode, float64 projparm[], float64 cornerlon[], - float64 cornerlat[], float64 longitude[], float64 latitude[], - int32 * npnts) +int32 +GDnentries(int32 gridID, int32 entrycode, int32 * strbufsize) + { + intn status; /* routine return status variable */ intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - float64 lonrad; /* Longitude (radians) */ - float64 latrad; /* Latitude (radians) */ - float64 cs[4]; /* Cosine array */ - float64 sn[4]; /* Sine array */ - float64 csTest; /* Cosine test value */ - float64 snTest; /* Sine test value */ - float64 crs01; /* Cross product */ - float64 crsTest[2]; /* Cross product array */ - float64 longPol; /* Longitude beneath pole */ - float64 minLat; /* Minimum latitude */ - float64 bisectParm[4]; /* Bisection parameters */ - float64 tanLat; /* Tangent latitude */ - float64 tanLon; /* Tangent longitude */ - float64 dotPrd; /* Dot product */ - float64 centMerd; /* Central Meridian */ - float64 orgLat; /* Latitude of origin */ - float64 dpi; /* Double precision pi */ - -#if 0 - float64 lamazDxDtheta(); /* Lambert Azimuthal Dx/Dtheta */ - float64 lamazDxDlamda(); /* Lambert Azimuthal Dx/Dlamda */ - float64 lamazDyDtheta(); /* Lambert Azimuthal Dy/Dtheta */ - float64 homDyDtheta(); /* Oblique Mercator Dy/Dtheta */ -#endif + int32 fid; /* HDF-EOS file ID */ + int32 sdInterfaceID; /* HDF SDS interface ID */ + int32 gdVgrpID; /* Grid root Vgroup ID */ + int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 nEntries = 0; /* Number of entries */ + int32 metaflag; /* Old (0), New (1) metadata flag) */ + int32 nVal = 0; /* Number of strings to search for */ - /* Compute pi (double precision) */ - /* ----------------------------- */ - dpi = atan(1.0) * 4; + char *metabuf = NULL; /* Pointer to structural metadata (SM) */ + char *metaptrs[2] = {NULL, NULL};/* Pointers to begin and end of SM section */ + char gridname[80]; /* Grid Name */ + char *utlstr;/* Utility string */ + char valName[2][32]; /* Strings to search for */ + memset(valName, 0, sizeof(valName)); - switch (projcode) + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) { - case GCTP_MERCAT: - { - /* No need for tangent points, since MERCAT projection - is rectangular */ - } - break; - case GCTP_BCEA: - { - /* No need for tangent points, since BCEA projection - is rectangular */ - } - break; - case GCTP_CEA: - { - /* No need for tangent points, since CEA projection - is rectangular */ - } - break; + HEpush(DFE_NOSPACE,"GDnentries", __FILE__, __LINE__); + return(-1); + } + status = GDchkgdid(gridID, "GDnentries", &fid, &sdInterfaceID, &gdVgrpID); - case GCTP_PS: + if (status == 0) + { + /* Get grid name */ + int gID = gridID % idOffset; + if (gID >= NGRID) { - /* Add "xy axis" points for Polar Stereographic if necessary */ - /* --------------------------------------------------------- */ - - - /* Get minimum of corner latitudes */ - /* ------------------------------- */ - minLat = (fabs(cornerlat[0]) <= fabs(cornerlat[1])) - ? cornerlat[0] : cornerlat[1]; - - - /* Compute sine and cosine of corner longitudes */ - /* -------------------------------------------- */ - for (i = 0; i < 2; i++) - { - lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD); - cs[i] = cos(lonrad); - sn[i] = sin(lonrad); - } - - - /* Compute cross product */ - /* --------------------- */ - crs01 = cs[0] * sn[1] - cs[1] * sn[0]; - - - /* Convert longitude beneath pole from DMS to DEG */ - /* ---------------------------------------------- */ - longPol = EHconvAng(projparm[4], HDFE_DMS_RAD); - - - - for (i = 0; i < 4; i++) - { - csTest = cos(longPol); - snTest = sin(longPol); + free(utlstr); + return -1; + } - crsTest[0] = cs[0] * snTest - csTest * sn[0]; - crsTest[1] = cs[1] * snTest - csTest * sn[1]; + Vgetname(GDXGrid[gID].IDTable, gridname); - if ((crs01 > 0 && crsTest[0] > 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] < 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] > 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] > 0 && crsTest[1] > 0)) - { - longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG); - latitude[*npnts] = minLat; - (*npnts)++; - } - longPol += 0.5 * dpi; - } - } - break; + /* Zero out string buffer size */ + *strbufsize = 0; - case GCTP_LAMAZ: + /* + * Get pointer to relevant section within SM and Get names of + * metadata strings to inquire about + */ + switch (entrycode) { - if ((int32) projparm[5] == +90000000 || - (int32) projparm[5] == -90000000) + case HDFE_NENTDIM: { - /* Add "xy axis" points for Polar Lambert Azimuthal */ - /* ------------------------------------------------ */ - minLat = (fabs(cornerlat[0]) <= fabs(cornerlat[1])) - ? cornerlat[0] : cornerlat[1]; - - for (i = 0; i < 2; i++) + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + "Dimension", metaptrs); + if(metabuf == NULL) { - lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD); - cs[i] = cos(lonrad); - sn[i] = sin(lonrad); + free(utlstr); + return(-1); } - crs01 = cs[0] * sn[1] - cs[1] * sn[0]; - - longPol = EHconvAng(projparm[4], HDFE_DMS_RAD); - for (i = 0; i < 4; i++) - { - csTest = cos(longPol); - snTest = sin(longPol); - - crsTest[0] = cs[0] * snTest - csTest * sn[0]; - crsTest[1] = cs[1] * snTest - csTest * sn[1]; - if ((crs01 > 0 && crsTest[0] > 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] < 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] > 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] > 0 && crsTest[1] > 0)) - { - longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG); - latitude[*npnts] = minLat; - (*npnts)++; - } - longPol += 0.5 * dpi; - } + nVal = 1; + strcpy(&valName[0][0], "DimensionName"); } - else if ((int32) projparm[5] == 0) - { - /* Add "Equator" points for Equatorial Lambert Azimuthal */ - /* ----------------------------------------------------- */ - if (cornerlat[0] * cornerlat[1] < 0) - { - longitude[4] = cornerlon[0]; - latitude[4] = 0; - - longitude[5] = cornerlon[1]; - latitude[5] = 0; + break; - *npnts = 6; - } - } - else + case HDFE_NENTDFLD: { - /* Add tangent points for Oblique Lambert Azimuthal */ - /* ------------------------------------------------ */ - bisectParm[0] = EHconvAng(projparm[5], HDFE_DMS_DEG); - bisectParm[2] = EHconvAng(projparm[4], HDFE_DMS_DEG); - - - /* Tangent to y-axis along longitude */ - /* --------------------------------- */ - for (i = 0; i < 2; i++) - { - bisectParm[1] = cornerlon[i]; - - if (EHbisect(lamazDxDtheta, bisectParm, 3, - cornerlat[0], cornerlat[1], - 0.0001, &tanLat) == 0) - { - longitude[*npnts] = cornerlon[i]; - latitude[*npnts] = tanLat; - (*npnts)++; - } - } - - /* Tangent to y-axis along latitude */ - /* -------------------------------- */ - for (i = 0; i < 2; i++) - { - bisectParm[1] = cornerlat[i]; - - if (EHbisect(lamazDxDlamda, bisectParm, 3, - cornerlon[0], cornerlon[1], - 0.0001, &tanLon) == 0) - { - longitude[*npnts] = tanLon; - latitude[*npnts] = cornerlat[i]; - (*npnts)++; - } - } - - - /* Tangent to x-axis along longitude */ - /* --------------------------------- */ - for (i = 0; i < 2; i++) - { - bisectParm[1] = cornerlon[i]; - - if (EHbisect(lamazDyDtheta, bisectParm, 3, - cornerlat[0], cornerlat[1], - 0.0001, &tanLat) == 0) - { - longitude[*npnts] = cornerlon[i]; - latitude[*npnts] = tanLat; - (*npnts)++; - } - } - - /* Tangent to x-axis along latitude */ - /* -------------------------------- */ - for (i = 0; i < 2; i++) - { - lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD); - cs[i] = cos(lonrad); - sn[i] = sin(lonrad); - } - crs01 = cs[0] * sn[1] - cs[1] * sn[0]; - - longPol = EHconvAng(projparm[4], HDFE_DMS_RAD); - for (i = 0; i < 2; i++) + metabuf = (char *) EHmetagroup(sdInterfaceID, gridname, "g", + "DataField", metaptrs); + if(metabuf == NULL) { - csTest = cos(longPol); - snTest = sin(longPol); - - crsTest[0] = cs[0] * snTest - csTest * sn[0]; - crsTest[1] = cs[1] * snTest - csTest * sn[1]; - - if ((crs01 > 0 && crsTest[0] > 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] < 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] > 0 && crsTest[1] < 0) || - (crs01 < 0 && crsTest[0] > 0 && crsTest[1] > 0)) - { - longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG); - latitude[*npnts] = cornerlat[0]; - (*npnts)++; - longitude[*npnts] = EHconvAng(longPol, HDFE_RAD_DEG); - latitude[*npnts] = cornerlat[1]; - (*npnts)++; - } - longPol += dpi; + free(utlstr); + return(-1); } - } - } - break; - - - case GCTP_GOOD: - { - /* Add "Equator" points for Goode Homolosine if necessary */ - /* ------------------------------------------------------ */ - if (cornerlat[0] * cornerlat[1] < 0) - { - longitude[4] = cornerlon[0]; - latitude[4] = 0; - - longitude[5] = cornerlon[1]; - latitude[5] = 0; - - *npnts = 6; - } - } - break; - - - case GCTP_LAMCC: - { - /* Compute sine and cosine of corner longitudes */ - /* -------------------------------------------- */ - for (i = 0; i < 2; i++) - { - lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD); - cs[i] = cos(lonrad); - sn[i] = sin(lonrad); - } - - - /* Compute dot product */ - /* ------------------- */ - dotPrd = cs[0] * cs[1] + sn[0] * sn[1]; - - - /* Convert central meridian (DMS to DEG) & compute sin & cos */ - /* --------------------------------------------------------- */ - centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG); - lonrad = EHconvAng(centMerd, HDFE_DEG_RAD); - cs[1] = cos(lonrad); - sn[1] = sin(lonrad); - - - /* If box brackets central meridian ... */ - /* ------------------------------------ */ - if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd) - { - latitude[4] = cornerlat[0]; - longitude[4] = centMerd; - - latitude[5] = cornerlat[1]; - longitude[5] = centMerd; - - *npnts = 6; - } - } - break; - - - case GCTP_ALBERS: - { - /* Compute sine and cosine of corner longitudes */ - /* -------------------------------------------- */ - for (i = 0; i < 2; i++) - { - lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD); - cs[i] = cos(lonrad); - sn[i] = sin(lonrad); - } - - - /* Compute dot product */ - /* ------------------- */ - dotPrd = cs[0] * cs[1] + sn[0] * sn[1]; - - - /* Convert central meridian (DMS to DEG) & compute sin & cos */ - /* --------------------------------------------------------- */ - centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG); - lonrad = EHconvAng(centMerd, HDFE_DEG_RAD); - cs[1] = cos(lonrad); - sn[1] = sin(lonrad); - - - /* If box brackets central meridian ... */ - /* ------------------------------------ */ - if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd) - { - latitude[4] = cornerlat[0]; - longitude[4] = centMerd; - - latitude[5] = cornerlat[1]; - longitude[5] = centMerd; - - *npnts = 6; - } - } - break; - - case GCTP_POLYC: - { - /* Compute sine and cosine of corner longitudes */ - /* -------------------------------------------- */ - for (i = 0; i < 2; i++) - { - lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD); - cs[i] = cos(lonrad); - sn[i] = sin(lonrad); - } - - - /* Compute dot product */ - /* ------------------- */ - dotPrd = cs[0] * cs[1] + sn[0] * sn[1]; - - - /* Convert central meridian (DMS to DEG) & compute sin & cos */ - /* --------------------------------------------------------- */ - centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG); - lonrad = EHconvAng(centMerd, HDFE_DEG_RAD); - cs[1] = cos(lonrad); - sn[1] = sin(lonrad); - - - /* If box brackets central meridian ... */ - /* ------------------------------------ */ - if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd) - { - latitude[4] = cornerlat[0]; - longitude[4] = centMerd; - - latitude[5] = cornerlat[1]; - longitude[5] = centMerd; - - *npnts = 6; + nVal = 1; + strcpy(&valName[0][0], "DataFieldName"); } + break; } - break; + if (!metabuf || metaptrs[0] == NULL) + { + free(metabuf); + return -1; + } - case GCTP_TM: + /* + * Check for presence of 'GROUP="' string If found then old metadata, + * search on OBJECT string + */ + metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0; + if (metaflag == 0) { - /* Compute sine and cosine of corner longitudes */ - /* -------------------------------------------- */ - for (i = 0; i < 2; i++) - { - lonrad = EHconvAng(cornerlon[i], HDFE_DEG_RAD); - cs[i] = cos(lonrad); - sn[i] = sin(lonrad); - } - + nVal = 1; + strcpy(&valName[0][0], "\t\tOBJECT"); + } - /* Compute dot product */ - /* ------------------- */ - dotPrd = cs[0] * cs[1] + sn[0] * sn[1]; + /* Begin loop through entries in metadata */ + /* -------------------------------------- */ + while (1) + { + /* Search for first string */ + strcpy(utlstr, &valName[0][0]); + strcat(utlstr, "="); + metaptrs[0] = strstr(metaptrs[0], utlstr); - for (i = -1; i <= 1; i++) + /* If found within relevant metadata section ... */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) { - centMerd = EHconvAng(projparm[4], HDFE_DMS_DEG); - lonrad = EHconvAng(centMerd + 90 * i, HDFE_DEG_RAD); - csTest = cos(lonrad); - snTest = sin(lonrad); - - - /* If box brackets meridian ... */ - /* ---------------------------- */ - if (csTest * cs[1] + snTest * sn[1] > dotPrd) + for (i = 0; i < nVal; i++) { - latitude[*npnts] = cornerlat[0]; - longitude[*npnts] = centMerd; - (*npnts)++; - - latitude[*npnts] = cornerlat[1]; - longitude[*npnts] = centMerd; - (*npnts)++; + /* + * Get all string values Don't count quotes + */ + EHgetmetavalue(metaptrs, &valName[i][0], utlstr); + *strbufsize += (int32)strlen(utlstr) - 2; } - } - - + /* Increment number of entries */ + nEntries++; - /* Compute sine and cosine of corner latitudes */ - /* ------------------------------------------- */ - for (i = 0; i < 2; i++) - { - latrad = EHconvAng(cornerlat[i], HDFE_DEG_RAD); - cs[i] = cos(latrad); - sn[i] = sin(latrad); + /* Go to end of OBJECT */ + metaptrs[0] = strstr(metaptrs[0], "END_OBJECT"); } - - - /* Compute dot product */ - /* ------------------- */ - dotPrd = cs[0] * cs[1] + sn[0] * sn[1]; - - - /* Convert origin latitude (DMS to DEG) & compute sin & cos */ - /* -------------------------------------------------------- */ - orgLat = EHconvAng(projparm[5], HDFE_DMS_DEG); - latrad = EHconvAng(orgLat, HDFE_DEG_RAD); - cs[1] = cos(latrad); - sn[1] = sin(latrad); - - - /* If box brackets origin latitude ... */ - /* ----------------------------------- */ - if (cs[0] * cs[1] + sn[0] * sn[1] > dotPrd) + else + /* No more entries found */ { - latitude[*npnts] = orgLat; - longitude[*npnts] = cornerlon[0]; - (*npnts)++; - - latitude[*npnts] = orgLat; - longitude[*npnts] = cornerlon[1]; - (*npnts)++; + break; } } - break; + free(metabuf); - case GCTP_HOM: + /* Count comma separators & slashes (if mappings) */ + /* ---------------------------------------------- */ + if (nEntries > 0) { - /* Tangent to y-axis along longitude */ - /* --------------------------------- */ - if (projparm[12] == 0) - { - cs[0] = cos(EHconvAng(projparm[8], HDFE_DMS_RAD)); - sn[0] = sin(EHconvAng(projparm[8], HDFE_DMS_RAD)); - cs[1] = cos(EHconvAng(projparm[9], HDFE_DMS_RAD)); - sn[1] = sin(EHconvAng(projparm[9], HDFE_DMS_RAD)); - cs[2] = cos(EHconvAng(projparm[10], HDFE_DMS_RAD)); - sn[2] = sin(EHconvAng(projparm[10], HDFE_DMS_RAD)); - cs[3] = cos(EHconvAng(projparm[11], HDFE_DMS_RAD)); - sn[3] = sin(EHconvAng(projparm[11], HDFE_DMS_RAD)); - - bisectParm[3] = atan2( - (cs[1] * sn[3] * cs[0] - sn[1] * cs[3] * cs[2]), - (sn[1] * cs[3] * sn[2] - cs[1] * sn[3] * sn[0])); - bisectParm[0] = atan( - (sin(bisectParm[3]) * sn[0] - cos(bisectParm[3]) * cs[0]) / - (sn[1] / cs[1])); - bisectParm[2] = bisectParm[3] + 0.5 * dpi; - } - else - { - cs[0] = cos(EHconvAng(projparm[3], HDFE_DMS_RAD)); - sn[0] = sin(EHconvAng(projparm[3], HDFE_DMS_RAD)); - cs[1] = cos(EHconvAng(projparm[4], HDFE_DMS_RAD)); - sn[1] = sin(EHconvAng(projparm[4], HDFE_DMS_RAD)); - - bisectParm[0] = asin(cs[1] * sn[0]); - bisectParm[2] = atan2(-cs[0], (-sn[1] * sn[0])) + 0.5 * dpi; - } - - for (i = 0; i < 2; i++) - { - bisectParm[1] = cornerlon[i]; - - if (EHbisect(homDyDtheta, bisectParm, 3, - cornerlat[0], cornerlat[1], - 0.0001, &tanLat) == 0) - { - longitude[*npnts] = cornerlon[i]; - latitude[*npnts] = tanLat; - (*npnts)++; - } - } - + *strbufsize += nEntries - 1; + *strbufsize += (nVal - 1) * nEntries; } - break; } - return (status); + /* Set nEntries to -1 if error status exists */ + /* ----------------------------------------- */ + if (status == -1) + { + nEntries = -1; + } + + free(utlstr); + return (nEntries); } + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDdefboxregion | +| FUNCTION: GDinqgrid | | | -| DESCRIPTION: Defines region for subsetting in a grid. | +| DESCRIPTION: Returns number and names of grid structures in file | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| regionID int32 Region ID | +| nGrid int32 Number of grid structures in file | | | | INPUTS: | -| gridID int32 Grid structure ID | -| cornerlon float64 dec deg Longitude of opposite corners of box | -| cornerlat float64 dec deg Latitude of opposite corners of box | -| | +| filename char HDF-EOS filename | | | | OUTPUTS: | -| None | +| gridlist char List of grid names (comma-separated) | +| strbufsize int32 Length of gridlist | | | | NOTES: | | | @@ -7760,616 +3218,216 @@ GDtangentpnts(int32 projcode, float64 projparm[], float64 cornerlon[], | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales "Clamp" subset region around grid | -| Oct 96 Joel Gales Fix "outside region" check | -| Nov 96 Joel Gales Add check for "tangent" points (GDtangentpnts) | -| Dec 96 Joel Gales Trap if no projection code defined | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Mar 99 David Wynne Fix for NCR 21195, allow subsetting of MISR SOM | -| data sets | -| Jun 00 Abe Taaheri Added support for EASE grid | | | | END_PROLOG | -----------------------------------------------------------------------------*/ int32 -GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) +GDinqgrid(const char *filename, char *gridlist, int32 * strbufsize) { - intn i; /* Loop index */ - intn j; /* Loop index */ - intn k; /* Loop index */ - intn n; /* Loop index */ - intn status = 0; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 regionID = -1; /* Region ID */ - int32 xdimsize; /* XDim size */ - int32 ydimsize; /* YDim size */ - int32 projcode; /* Projection code */ - int32 zonecode; /* Zone code */ - int32 spherecode; /* Sphere code */ - int32 row[32]; /* Row array */ - int32 col[32]; /* Column array */ - int32 minCol = 0; /* Minimum column value */ - int32 minRow = 0; /* Minimum row value */ - int32 maxCol = 0; /* Maximum column value */ - int32 maxRow = 0; /* Maximum row value */ - int32 npnts; /* Number of boundary - (edge & tangent) pnts */ - - float64 longitude[32]; /* Longitude array */ - float64 latitude[32]; /* Latitude array */ - float64 upleftpt[2]; /* Upper left pt coordinates */ - float64 lowrightpt[2]; /* Lower right pt coordinates */ - float64 somupleftpt[2]; /* temporary Upper left pt coordinates - for SOM projection */ - float64 somlowrightpt[2]; /* temporary Lower right pt - coordinates for SOM projection */ - float64 projparm[16]; /* Projection parameters */ - float64 xscale; /* X scale */ - float64 yscale; /* Y scale */ - float64 lonrad0; /* Longitude of upper left point - (radians) */ - float64 latrad0; /* Latitude of upper left point (radians) */ - float64 lonrad2; /* Longitude of point (radians) */ - float64 latrad2; /* Latitude of point (radians) */ - - /* Used for SOM projection */ - char *utlbuf; - char *gridname; - int32 blockindexstart = -1; - int32 blockindexstop = -1; - float32 offset[180]; - float64 templeftpt[2]; - float64 temprightpt[2]; - int32 idOffset = GDIDOFFSET; /* Grid ID offset */ - float64 xmtr[2], ymtr[2]; - float64 lon[2],lat[2]; - float64 xcor[2], ycor[2]; - int32 nlatlon; - float64 upleftpt_m[2]; - + int32 nGrid; /* Number of grid structures in file */ - utlbuf = (char *)calloc(128, sizeof(char)); - if(utlbuf == NULL) - { - HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__); - return(-1); - } - gridname = (char *)calloc(128, sizeof(char)); - if(gridname == NULL) - { - HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__); - free(utlbuf); - return(-1); - } + /* Call "EHinquire" routine */ + /* ------------------------ */ + nGrid = EHinquire(filename, "GRID", gridlist, strbufsize); - /* Check for valid grid ID */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDdefboxregion", - &fid, &sdInterfaceID, &gdVgrpID); + return (nGrid); +} - if (status == 0) - { - /* Get grid info */ - /* ------------- */ - status = GDgridinfo(gridID, &xdimsize, &ydimsize, - upleftpt, lowrightpt); +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: GDgetfillvalue | +| | +| DESCRIPTION: Retrieves fill value for a specified field. | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| status intn return status (0) SUCCEED, (-1) FAIL | +| | +| INPUTS: | +| gridID int32 grid structure ID | +| fieldname char field name | +| | +| OUTPUTS: | +| fillval void fill value | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Jun 96 Joel Gales Original Programmer | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +intn +GDgetfillvalue(int32 gridID, const char *fieldname, VOIDP fillval) +{ + intn status; /* routine return status variable */ - /* If error then bail */ - /* ------------------ */ - if (status != 0) - { - regionID = -1; - free(utlbuf); - free(gridname); - return (regionID); - } + int32 nt; /* Number type */ + int32 dims[8]; /* Dimensions array */ + int32 dum; /* Dummy variable */ + char name[80]; /* Fill value "attribute" name */ - /* Get proj info */ - /* ------------- */ - status = GDprojinfo(gridID, &projcode, &zonecode, - &spherecode, projparm); + status = GDchkgdid(gridID, "GDgetfillvalue", &dum, &dum, &dum); + /* Check for valid grid ID */ + if (status == 0) + { + /* Get field info */ + status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL); - /* If no projection code defined then bail */ - /* --------------------------------------- */ - if (projcode == -1) + if (status == 0) { - regionID = -1; - free(utlbuf); - free(gridname); - return (regionID); + /* Read fill value attribute */ + strcpy(name, "_FV_"); + strcat(name, fieldname); + status = GDreadattr(gridID, name, fillval); } - - - /* Get default values for upleft and lowright if necessary */ - /* ------------------------------------------------------- */ - if (upleftpt[0] == 0 && upleftpt[1] == 0 && - lowrightpt[0] == 0 && lowrightpt[1] == 0) + else { - status = GDgetdefaults(projcode, zonecode, projparm, spherecode, - upleftpt, lowrightpt); - - /* If error then bail */ - /* ------------------ */ - if (status != 0) - { - regionID = -1; - free(utlbuf); - free(gridname); - return (regionID); - } + HEpush(DFE_GENAPP, "GDgetfillvalue", __FILE__, __LINE__); + HEreport("Fieldname \"%s\" does not exist.\n", fieldname); } + } + return (status); +} - /* Fill-up longitude and latitude arrays */ - /* ------------------------------------- */ - longitude[0] = cornerlon[0]; - latitude[0] = cornerlat[0]; - - longitude[1] = cornerlon[0]; - latitude[1] = cornerlat[1]; - - longitude[2] = cornerlon[1]; - latitude[2] = cornerlat[0]; - - longitude[3] = cornerlon[1]; - latitude[3] = cornerlat[1]; - - npnts = 4; - - - /* Find additional tangent points from GDtangentpnts */ - /* ------------------------------------------------- */ - status = GDtangentpnts(projcode, projparm, cornerlon, cornerlat, - longitude, latitude, &npnts); - - /* If SOM projection with projparm[11] non-zero ... */ - if (projcode == GCTP_SOM && projparm[11] != 0) - { - int gID = gridID % idOffset; - if (gID >= NGRID) - { - free(utlbuf); - free(gridname); - return -1; - } - - Vgetname(GDXGrid[gID].IDTable, gridname); - snprintf(utlbuf, 128, "%s%s", "_BLKSOM:", gridname); - status = GDreadattr(gridID, utlbuf, offset); - - somupleftpt[0] = upleftpt[0]; - somupleftpt[1] = upleftpt[1]; - somlowrightpt[0]= lowrightpt[0]; - somlowrightpt[1] = lowrightpt[1]; - - k = 0; - n = 2; - - for (j = 0; j <= projparm[11] - 1; j++) - { - - /* Convert from lon/lat to row/col */ - /* ------------------------------- */ - status = GDll2ij(projcode, zonecode, projparm, spherecode, - xdimsize, ydimsize, somupleftpt, somlowrightpt, - npnts, longitude, latitude, row, col, NULL, NULL); - - - /* Find min/max values for row & col */ - /* --------------------------------- */ - minCol = col[0]; - minRow = row[0]; - maxCol = col[0]; - maxRow = row[0]; - for (i = 1; i < npnts; i++) - { - if (col[i] < minCol) - { - minCol = col[i]; - } - - if (col[i] > maxCol) - { - maxCol = col[i]; - } - - if (row[i] < minRow) - { - minRow = row[i]; - } - - if (row[i] > maxRow) - { - maxRow = row[i]; - } - } - - - - /* "Clamp" if outside Grid */ - /* ----------------------- */ - minCol = (minCol < 0) ? 0 : minCol; - minRow = (minRow < 0) ? 0 : minRow; - - maxCol = (maxCol >= xdimsize) ? xdimsize - 1 : maxCol; - maxRow = (maxRow >= ydimsize) ? ydimsize - 1 : maxRow; - - - /* Check whether subset region is outside grid region */ - /* -------------------------------------------------- */ - if (minCol >= xdimsize || minRow >= ydimsize || - maxCol < 0 || maxRow < 0) - { - if ( blockindexstart == -1 && (projparm[11]) == j) - { - status = -1; - HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__); - HEreport("Subset Region outside of Grid Region\n"); - regionID = -1; - } - } - else - { - if (k == 0) - { - blockindexstart = j; - blockindexstop = j; - k = 1; - } - else - { - blockindexstop = j; - } - } - - // E. Rouault: FIXME: was really abs(int) indented here ? Forcing the cast to int to please compilers - templeftpt[0] = upleftpt[0] + ((offset[j]/xdimsize)*abs((int)(upleftpt[0] - lowrightpt[0]))) + abs((int)(upleftpt[0] - lowrightpt[0]))*(n-1); - templeftpt[1] = upleftpt[1] + ((lowrightpt[1] - upleftpt[1]))*(n-1); - - temprightpt[0] = lowrightpt[0] + ((offset[j]/xdimsize)*abs((int)(lowrightpt[0] - upleftpt[0]))) + abs((int)(lowrightpt[0] - upleftpt[0]))*(n-1); - temprightpt[1] = lowrightpt[1] + ((upleftpt[1] - lowrightpt[1]))*(n-1); - - somupleftpt[0] = templeftpt[0]; - somupleftpt[1] = templeftpt[1]; - - somlowrightpt[0] = temprightpt[0]; - somlowrightpt[1] = temprightpt[1]; - n++; - } - } - else - { - - /* Convert from lon/lat to row/col */ - /* ------------------------------- */ - - status = GDll2ij(projcode, zonecode, projparm, spherecode, - xdimsize, ydimsize, upleftpt, lowrightpt, - npnts, longitude, latitude, row, col, NULL, NULL); - - /* Find min/max values for row & col */ - /* --------------------------------- */ - minCol = col[0]; - minRow = row[0]; - maxCol = col[0]; - maxRow = row[0]; - for (i = 1; i < npnts; i++) - { - if (col[i] < minCol) - { - minCol = col[i]; - } - - if (col[i] > maxCol) - { - maxCol = col[i]; - } - - if (row[i] < minRow) - { - minRow = row[i]; - } - - if (row[i] > maxRow) - { - maxRow = row[i]; - } - } - - - - /* "Clamp" if outside Grid */ - /* ----------------------- */ - minCol = (minCol < 0) ? 0 : minCol; - minRow = (minRow < 0) ? 0 : minRow; - - maxCol = (maxCol >= xdimsize) ? xdimsize - 1 : maxCol; - maxRow = (maxRow >= ydimsize) ? ydimsize - 1 : maxRow; - - - /* Check whether subset region is outside grid region */ - /* -------------------------------------------------- */ - if (minCol >= xdimsize || minRow >= ydimsize || maxCol < 0 || maxRow < 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__); - HEreport("Subset Region outside of Grid Region\n"); - regionID = -1; - - } - } - if (status == 0) - { - /* Store grid region info */ - /* ---------------------- */ - for (i = 0; i < NGRIDREGN; i++) - { - /* Find first empty grid region */ - /* ---------------------------- */ - if (GDXRegion[i] == 0) - { - /* Allocate space for grid region entry */ - /* ------------------------------------ */ - GDXRegion[i] = (struct gridRegion *) - calloc(1, sizeof(struct gridRegion)); - if(GDXRegion[i] == NULL) - { - HEpush(DFE_NOSPACE,"GDdefboxregion", __FILE__, __LINE__); - free(utlbuf); - free(gridname); - return(-1); - } - - - /* Store file and grid ID */ - /* ---------------------- */ - GDXRegion[i]->fid = fid; - GDXRegion[i]->gridID = gridID; - - - /* Initialize vertical subset entries to -1 */ - /* ---------------------------------------- */ - for (j = 0; j < 8; j++) - { - GDXRegion[i]->StartVertical[j] = -1; - GDXRegion[i]->StopVertical[j] = -1; - } - - /* Store start & count along x & y */ - /* ------------------------------- */ - GDXRegion[i]->xStart = minCol; - GDXRegion[i]->xCount = maxCol - minCol + 1; - GDXRegion[i]->yStart = minRow; - GDXRegion[i]->yCount = maxRow - minRow + 1; - /* Store upleft and lowright points of subset region */ - /* ------------------------------------------------- */ - if (projcode == GCTP_GEO ) - { - /* GEO projection */ - /* ------------------------ */ +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: GDdetach | +| | +| DESCRIPTION: Detaches from grid interface and performs file housekeeping. | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| status intn return status (0) SUCCEED, (-1) FAIL | +| | +| INPUTS: | +| gridID int32 grid structure ID | +| | +| | +| OUTPUTS: | +| None | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Jun 96 Joel Gales Original Programmer | +| Sep 96 Joel Gales Setup dim names for SDsetdimname in dimbuf1 rather | +| that utlstr | +| Oct 96 Joel Gales Detach Grid Vgroups | +| Oct 96 Joel Gales "Detach" from SDS | +| Nov 96 Joel Gales Call GDchkgdid to check for proper grid ID | +| Dec 96 Joel Gales Add multiple vertical subsetting garbage collection | +| Oct 98 Abe Taaheri Added GDXRegion[k]->DimNamePtr[i] =0; after freeing | +| memory | +| Sep 99 Abe Taaheri Changed memcpy to memmove because of overlapping | +| source and destination for GDXSDcomb, nameptr, and | +| dimptr. memcpy may cause unexpected results. | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +intn +GDdetach(int32 gridID) - /* Convert upleft & lowright lon from DMS to radians */ - /* ------------------------------------------------- */ - lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); - lonrad2 = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); +{ + intn i; /* Loop index */ + intn k; /* Loop index */ + intn status = 0; /* routine return status variable */ - /* Compute X scale */ - /* --------------- */ - xscale = (lonrad2 - lonrad0) / xdimsize; + int32 sdInterfaceID; /* SDS interface ID */ + int32 gID; /* Grid ID - offset */ + int32 idOffset = GDIDOFFSET; /* Grid ID offset */ + int32 dum; /* Dummy variable */ - /* Convert upleft & lowright lat from DMS to radians */ - /* ------------------------------------------------- */ - latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD); - latrad2 = EHconvAng(lowrightpt[1], HDFE_DMS_RAD); + char gridname[VGNAMELENMAX + 1]; /* Grid name */ - /* Compute Y scale */ - /* --------------- */ - yscale = (latrad2 - latrad0) / ydimsize; + status = GDchkgdid(gridID, "GDdetach", &dum, &sdInterfaceID, &dum); - /* MinCol -> radians -> DMS -> upleftpt[0] */ - /* --------------------------------------- */ - GDXRegion[i]->upleftpt[0] = - EHconvAng(lonrad0 + xscale * minCol, - HDFE_RAD_DMS); + if (status == 0) + { + gID = gridID % idOffset; + if (gID >= NGRID) + { + return -1; + } + Vgetname(GDXGrid[gID].IDTable, gridname); - /* MinRow -> radians -> DMS -> upleftpt[1] */ - /* --------------------------------------- */ - GDXRegion[i]->upleftpt[1] = - EHconvAng(latrad0 + yscale * minRow, - HDFE_RAD_DMS); + /* "Detach" from previously attached SDSs */ + /* -------------------------------------- */ + for (k = 0; k < GDXGrid[gID].nSDS; k++) + { + SDendaccess(GDXGrid[gID].sdsID[k]); + } + free(GDXGrid[gID].sdsID); + GDXGrid[gID].sdsID = 0; + GDXGrid[gID].nSDS = 0; - /* MinCol + 1 -> radians -> DMS -> lowrightpt[0] */ - /* --------------------------------------------- */ - GDXRegion[i]->lowrightpt[0] = - EHconvAng(lonrad0 + xscale * (maxCol + 1), - HDFE_RAD_DMS); + /* Detach Grid Vgroups */ + /* ------------------- */ + Vdetach(GDXGrid[gID].VIDTable[0]); + Vdetach(GDXGrid[gID].VIDTable[1]); + Vdetach(GDXGrid[gID].IDTable); - /* MinRow + 1 -> radians -> DMS -> lowrightpt[1] */ - /* --------------------------------------------- */ - GDXRegion[i]->lowrightpt[1] = - EHconvAng(latrad0 + yscale * (maxRow + 1), - HDFE_RAD_DMS); - } - else if (projcode == GCTP_BCEA) - { - /* BCEA projection */ - /* -------------- */ - nlatlon = 2; - lon[0] = upleftpt[0]; - lon[1] = lowrightpt[0]; - lat[0] = upleftpt[1]; - lat[1] = lowrightpt[1]; - status = - GDll2mm_cea(projcode,zonecode,spherecode,projparm, - xdimsize, ydimsize, - upleftpt, lowrightpt,nlatlon, - lon, lat, - xcor, ycor, &xscale, &yscale); - upleftpt_m[0] = xcor[0]; - upleftpt_m[1] = ycor[0]; - - - if (status == -1) - { - HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__); - free(utlbuf); - free(gridname); - return (status); - } + GDXGrid[gID].active = 0; + GDXGrid[gID].VIDTable[0] = 0; + GDXGrid[gID].VIDTable[1] = 0; + GDXGrid[gID].IDTable = 0; + GDXGrid[gID].fid = 0; - /* MinCol -> meters -> upleftpt[0] */ - /* ------------------------------- */ - xmtr[0] = upleftpt_m[0] + xscale * minCol; - - /* MinRow -> meters -> upleftpt[1] */ - /* ------------------------------- */ - ymtr[0] = upleftpt_m[1] + yscale * minRow; - - /* MinCol + 1 -> meters -> lowrightpt[0] */ - /* ------------------------------------- */ - xmtr[1] = upleftpt_m[0] + xscale * (maxCol + 1); - - /* MinRow + 1 -> meters -> lowrightpt[1] */ - /* ------------------------------------- */ - ymtr[1] = upleftpt_m[1] + yscale * (maxRow + 1); - - /* Convert upleft & lowright lon from DMS to radians */ - /* ------------------------------------------------- */ - npnts = 2; - status = GDmm2ll_cea(projcode, zonecode, spherecode, - projparm, xdimsize, ydimsize, - upleftpt, lowrightpt, npnts, - xmtr, ymtr, - longitude, latitude); - if (status == -1) - { - HEpush(DFE_GENAPP, "GDdefboxregion", __FILE__, __LINE__); - free(utlbuf); - free(gridname); - return (status); - } - GDXRegion[i]->upleftpt[0] = longitude[0]; - GDXRegion[i]->upleftpt[1] = latitude[0]; - GDXRegion[i]->lowrightpt[0] = longitude[1]; - GDXRegion[i]->lowrightpt[1] = latitude[1]; - } - else if (projcode == GCTP_SOM) - { - /* Store start & count along x & y */ - /* ------------------------------- */ - GDXRegion[i]->xStart = 0; - GDXRegion[i]->xCount = xdimsize; - GDXRegion[i]->yStart = 0; - GDXRegion[i]->yCount = ydimsize; - - GDXRegion[i]->somStart = blockindexstart; - GDXRegion[i]->somCount = blockindexstop - blockindexstart + 1; - - /* Store upleft and lowright points of subset region */ - /* ------------------------------------------------- */ - if (blockindexstart == 0) - { - GDXRegion[i]->upleftpt[0] = upleftpt[0]; - GDXRegion[i]->upleftpt[1] = upleftpt[1]; - GDXRegion[i]->lowrightpt[0] = lowrightpt[0]; - GDXRegion[i]->lowrightpt[1] = lowrightpt[1]; - } - else - { - GDXRegion[i]->upleftpt[0] = - (lowrightpt[0] - upleftpt[0])* - (offset[blockindexstart-1]/xdimsize) + upleftpt[0]; - GDXRegion[i]->upleftpt[1] = - (lowrightpt[1] - upleftpt[1])* - (blockindexstart+1-1) + upleftpt[1]; - - GDXRegion[i]->lowrightpt[0] = - (lowrightpt[0] - upleftpt[0])* - (offset[blockindexstart-1]/xdimsize) + lowrightpt[0]; - GDXRegion[i]->lowrightpt[1] = - (lowrightpt[1] - upleftpt[1])* - (blockindexstart+1-1) + lowrightpt[1]; - - } - } - else + /* Free Region Pointers */ + /* -------------------- */ + for (k = 0; k < NGRIDREGN; k++) + { + if (GDXRegion[k] != 0 && + GDXRegion[k]->gridID == gridID) + { + for (i = 0; i < 8; i++) + { + if (GDXRegion[k]->DimNamePtr[i] != 0) { - /* Non-GEO, Non-BCEA projections */ - /* ---------------------------- */ - - /* Compute X & Y scale */ - /* ------------------- */ - xscale = (lowrightpt[0] - upleftpt[0]) / xdimsize; - yscale = (lowrightpt[1] - upleftpt[1]) / ydimsize; - - - /* MinCol -> meters -> upleftpt[0] */ - /* ------------------------------- */ - GDXRegion[i]->upleftpt[0] = upleftpt[0] + - xscale * minCol; - - - /* MinRow -> meters -> upleftpt[1] */ - /* ------------------------------- */ - GDXRegion[i]->upleftpt[1] = upleftpt[1] + - yscale * minRow; - - - /* MinCol + 1 -> meters -> lowrightpt[0] */ - /* ------------------------------------- */ - GDXRegion[i]->lowrightpt[0] = upleftpt[0] + - xscale * (maxCol + 1); - - - /* MinRow + 1 -> meters -> lowrightpt[1] */ - /* ------------------------------------- */ - GDXRegion[i]->lowrightpt[1] = upleftpt[1] + - yscale * (maxRow + 1); + free(GDXRegion[k]->DimNamePtr[i]); + GDXRegion[k]->DimNamePtr[i] = 0; } - - /* Store region ID */ - /* --------------- */ - regionID = i; - break; } + free(GDXRegion[k]); + GDXRegion[k] = 0; + } } - } - } - free(utlbuf); - free(gridname); - return (regionID); + return (status); } -#endif - -#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDregioninfo | +| FUNCTION: GDclose | | | -| DESCRIPTION: Retrieves size of region in bytes. | +| DESCRIPTION: Closes file. | | | | | | Return Value Type Units Description | @@ -8377,18 +3435,11 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 Grid structure ID | -| regionID int32 Region ID | -| fieldname char Fieldname | +| fid int32 File ID | | | | | | OUTPUTS: | -| ntype int32 field number type | -| rank int32 field rank | -| dims int32 dimensions of field region | -| size int32 size in bytes of field region | -| upleftpt float64 Upper left corner coord for region | -| lowrightpt float64 Lower right corner coord for region | +| None | | | | NOTES: | | | @@ -8396,1014 +3447,822 @@ GDdefboxregion(int32 gridID, float64 cornerlon[], float64 cornerlat[]) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Add vertical subsetting | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Apr 99 David Wynne Added support for MISR SOM projection, NCR 21195 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -GDregioninfo(int32 gridID, int32 regionID, const char *fieldname, - int32 * ntype, int32 * rank, int32 dims[], int32 * size, - float64 upleftpt[], float64 lowrightpt[]) +GDclose(int32 fid) + { - intn j; /* Loop index */ intn status = 0; /* routine return status variable */ - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 l_index; /* Dimension l_index */ + /* Call EHclose to perform file close */ + /* ---------------------------------- */ + status = EHclose(fid); - char dimlist[256]; /* Dimension list */ - const char *errMesg = "Vertical Dimension Not Found: \"%s\".\n"; - const char *errM1 = "Both \"XDim\" and \"YDim\" must be present "; - const char *errM2 = "in the dimension list for \"%s\".\n"; - char errbuf[256];/* Error buffer */ + return (status); +} - /* Check for valid grid ID */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDregioninfo", &fid, &sdInterfaceID, - &gdVgrpID); +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: GDgetdefaults | +| | +| DESCRIPTION: | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| status intn return status (0) SUCCEED, (-1) FAIL | +| | +| INPUTS: | +| projcode int32 GCTP projection code | +| zonecode int32 UTM zone code | +| projparm float64 Projection parameters | +| spherecode int32 GCTP spheriod code | +| upleftpt float64 upper left corner coordinates | +| lowrightpt float64 lower right corner coordinates | +| | +| | +| OUTPUTS: | +| upleftpt float64 upper left corner coordinates | +| lowrightpt float64 lower right corner coordinates | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Aug 96 Joel Gales Original Programmer | +| Sep 96 Raj Gejjaga Fixed bugs in Polar Stereographic and Goode | | Homolosine default calculations. | +| Sep 96 Raj Gejjaga Added code to compute default boundary points | +| for Lambert Azimuthal Polar and Equatorial | +| projections. | +| Feb 97 Raj Gejjaga Added code to compute default boundary points | +| for Integerized Sinusoidal Grid. Added error | +| handling code. | +| Jun 00 Abe Taaheri Added support for EASE grid | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +static intn +GDgetdefaults(int32 projcode, int32 zonecode, float64 projparm[], + int32 spherecode, float64 upleftpt[], float64 lowrightpt[]) +{ + int32 errorcode = 0, status = 0; + int32(*for_trans[100]) (double, double, double*, double*); + + float64 lon, lat, plat, x, y; + float64 plon, tlon, llon, rlon, pplon, LLon, LLat, RLon, RLat; - /* Check for valid region ID */ - /* ------------------------- */ - if (status == 0) + /* invoke GCTP initialization routine */ + /* ---------------------------------- */ + for_init(projcode, zonecode, projparm, spherecode, NULL, NULL, + &errorcode, for_trans); + + /* Report error if any */ + /* ------------------- */ + if (errorcode != 0) { - if (regionID < 0 || regionID >= NGRIDREGN) - { - status = -1; - HEpush(DFE_RANGE, "GDregioninfo", __FILE__, __LINE__); - HEreport("Invalid Region id: %d.\n", regionID); - } + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } + /* Compute Default Boundary Points for EASE Grid */ + /* Use Global coverage */ + /* ------------------------------------------------------ */ + if (projcode == GCTP_BCEA && + upleftpt[0] == 0 && upleftpt[1] == 0 && + lowrightpt[0] == 0 && lowrightpt[1] == 0) + { + upleftpt[0] = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LON, HDFE_DEG_DMS); + upleftpt[1] = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LAT, HDFE_DEG_DMS); + lowrightpt[0] = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LON, HDFE_DEG_DMS); + lowrightpt[1] = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LAT, HDFE_DEG_DMS); + } - /* Check for active region ID */ - /* -------------------------- */ - if (status == 0) +/* Compute Default Boundary Points for CEA */ + /* --------------------------------------------*/ + if (projcode == GCTP_CEA && + upleftpt[0] == 0 && upleftpt[1] == 0 && + lowrightpt[0] == 0 && lowrightpt[1] == 0) + { + LLon = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LON, HDFE_DEG_RAD); + LLat = EHconvAng(EASE_GRID_DEFAULT_UPLEFT_LAT, HDFE_DEG_RAD); + RLon = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LON, HDFE_DEG_RAD); + RLat = EHconvAng(EASE_GRID_DEFAULT_LOWRGT_LAT, HDFE_DEG_RAD); + + errorcode = for_trans[projcode] (LLon, LLat, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + upleftpt[0] = x; + upleftpt[1] = y; + + errorcode = for_trans[projcode] (RLon, RLat, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + lowrightpt[0] = x; + lowrightpt[1] = y; + + } + + + /* Compute Default Boundary Points for Polar Sterographic */ + /* ------------------------------------------------------ */ + if (projcode == GCTP_PS && + upleftpt[0] == 0 && upleftpt[1] == 0 && + lowrightpt[0] == 0 && lowrightpt[1] == 0) { - if (GDXRegion[regionID] == 0) + /* + * Convert the longitude and latitude from the DMS to decimal degree + * format. + */ + plon = EHconvAng(projparm[4], HDFE_DMS_DEG); + plat = EHconvAng(projparm[5], HDFE_DMS_DEG); + + /* + * Compute the longitudes at 90, 180 and 270 degrees from the central + * longitude. + */ + + if (plon <= 0.0) { - status = -1; - HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__); - HEreport("Inactive Region ID: %d.\n", regionID); + tlon = 180.0 + plon; + pplon = plon + 360.0; } - } + else + { + tlon = plon - 180.0; + pplon = plon; + } + + rlon = pplon + 90.0; + if (rlon > 360.0) + rlon = rlon - 360; + + if (rlon > 180.0) + rlon = rlon - 360.0; + + if (rlon <= 0.0) + llon = 180.0 + rlon; + else + llon = rlon - 180.0; + /* Convert all four longitudes from decimal degrees to radians */ + plon = EHconvAng(plon, HDFE_DEG_RAD); + tlon = EHconvAng(tlon, HDFE_DEG_RAD); + llon = EHconvAng(llon, HDFE_DEG_RAD); + rlon = EHconvAng(rlon, HDFE_DEG_RAD); - /* Check that region defined for this file */ - /* --------------------------------------- */ - if (status == 0) - { - if (GDXRegion[regionID]->fid != fid) + errorcode = for_trans[projcode] (llon, 0.0, &x, &y); + if (errorcode != 0) { status = -1; - HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__); - HEreport("Region is not defined for this file.\n"); + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } + upleftpt[0] = x; - /* Check that region defined for this grid */ - /* --------------------------------------- */ - if (status == 0) - { - if (GDXRegion[regionID]->gridID != gridID) + errorcode = for_trans[projcode] (rlon, 0.0, &x, &y); + if (errorcode != 0) { status = -1; - HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__); - HEreport("Region is not defined for this Grid.\n"); + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } - + lowrightpt[0] = x; - /* Check for valid fieldname */ - /* ------------------------- */ - if (status == 0) - { - status = GDfieldinfo(gridID, fieldname, rank, dims, ntype, dimlist); + /* + * Compute the upperleft and lowright y values based on the south or + * north polar projection + */ - if (status != 0) - { - /* Fieldname not found in grid */ - /* --------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" not found.\n", - fieldname); - } - else if (*rank == 1) - { - /* Field is 1 dimensional */ - /* ---------------------- */ - status = -1; - HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__); - HEreport( - "One-Dimesional fields \"%s\" may not be subsetted.\n", - fieldname); - } - else + if (plat < 0.0) { - /* "XDim" and/or "YDim" not found */ - /* ------------------------------ */ - if (EHstrwithin("XDim", dimlist, ',') == -1 || - EHstrwithin("YDim", dimlist, ',') == -1) + errorcode = for_trans[projcode] (plon, 0.0, &x, &y); + if (errorcode != 0) { status = -1; - HEpush(DFE_GENAPP, "GDregioninfo", __FILE__, __LINE__); - snprintf(errbuf, sizeof(errbuf), "%s%s", errM1, errM2); - HEreport(errbuf, fieldname); + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } - } + upleftpt[1] = y; + errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } - /* If no problems ... */ - /* ------------------ */ - if (status == 0) - { - /* Check if SOM projection */ - /* ----------------------- */ - if (EHstrwithin("SOMBlockDim", dimlist, ',') == 0) - { - dims[EHstrwithin("SOMBlockDim", dimlist, ',')] = - GDXRegion[regionID]->somCount; - } - - /* Load XDim dimension from region entry */ - /* ------------------------------------- */ - if (GDXRegion[regionID]->xCount != 0) - { - dims[EHstrwithin("XDim", dimlist, ',')] = - GDXRegion[regionID]->xCount; - } + lowrightpt[1] = y; - /* Load YDim dimension from region entry */ - /* ------------------------------------- */ - if (GDXRegion[regionID]->yCount != 0) - { - dims[EHstrwithin("YDim", dimlist, ',')] = - GDXRegion[regionID]->yCount; } - - - /* Vertical Subset */ - /* --------------- */ - for (j = 0; j < 8; j++) + else { - - /* If active vertical subset ... */ - /* ----------------------------- */ - if (GDXRegion[regionID]->StartVertical[j] != -1) + errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); + if (errorcode != 0) { - /* Find vertical dimension within dimlist */ - /* -------------------------------------- */ - l_index = EHstrwithin(GDXRegion[regionID]->DimNamePtr[j], - dimlist, ','); - - /* If dimension found ... */ - /* ---------------------- */ - if (l_index != -1) - { - /* Compute dimension size */ - /* ---------------------- */ - dims[l_index] = - GDXRegion[regionID]->StopVertical[j] - - GDXRegion[regionID]->StartVertical[j] + 1; - } - else - { - /* Vertical dimension not found */ - /* ---------------------------- */ - status = -1; - *size = -1; - HEpush(DFE_GENAPP, "GDregioninfo", - __FILE__, __LINE__); - HEreport(errMesg, - GDXRegion[regionID]->DimNamePtr[j]); - } + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } + upleftpt[1] = y; - if (status == 0) - { - /* Compute number of total elements */ - /* -------------------------------- */ - *size = dims[0]; - for (j = 1; j < *rank; j++) + errorcode = for_trans[projcode] (plon, 0.0, &x, &y); + if (errorcode != 0) { - *size *= dims[j]; + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - /* Multiply by size in bytes of numbertype */ - /* --------------------------------------- */ - *size *= DFKNTsize(*ntype); - + lowrightpt[1] = y; - /* Return upper left and lower right subset values */ - /* ----------------------------------------------- */ - upleftpt[0] = GDXRegion[regionID]->upleftpt[0]; - upleftpt[1] = GDXRegion[regionID]->upleftpt[1]; - lowrightpt[0] = GDXRegion[regionID]->lowrightpt[0]; - lowrightpt[1] = GDXRegion[regionID]->lowrightpt[1]; } } - return (status); -} + /* Compute Default Boundary Points for Goode Homolosine */ + /* ---------------------------------------------------- */ + if (projcode == GCTP_GOOD && + upleftpt[0] == 0 && upleftpt[1] == 0 && + lowrightpt[0] == 0 && lowrightpt[1] == 0) + { + lon = EHconvAng(-180, HDFE_DEG_RAD); + lat = 0.0; + errorcode = for_trans[projcode] (lon, lat, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + upleftpt[0] = -fabs(x); + lowrightpt[0] = +fabs(x); -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDextractregion | -| | -| DESCRIPTION: Retrieves data from specified region. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 Grid structure ID | -| regionID int32 Region ID | -| fieldname char Fieldname | -| | -| OUTPUTS: | -| buffer void Data buffer containing subsetted region | -| | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Add vertical subsetting | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Apr 99 David Wynne Add support for MISR SOM projection, NCR 21195 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDextractregion(int32 gridID, int32 regionID, const char *fieldname, - VOIDP buffer) -{ - intn i; /* Loop index */ - intn j; /* Loop index */ - intn status = 0; /* routine return status variable */ + lat = EHconvAng(90, HDFE_DEG_RAD); - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 l_index; /* Dimension l_index */ - int32 start[8]; /* Start array for data read */ - int32 edge[8]; /* Edge array for data read */ - int32 dims[8]; /* Dimensions */ - int32 rank = 0; /* Field rank */ - int32 ntype; /* Field number type */ - int32 origincode; /* Pixel origin code */ + errorcode = for_trans[projcode] (lon, lat, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + + upleftpt[1] = +fabs(y); + lowrightpt[1] = -fabs(y); + } - char dimlist[256]; /* Dimension list */ - const char *errMesg = "Vertical Dimension Not Found: \"%s\".\n"; - const char *errM1 = "Both \"XDim\" and \"YDim\" must be present "; - const char *errM2 = "in the dimension list for \"%s\".\n"; - char errbuf[256];/* Error buffer */ + /* Compute Default Boundary Points for Lambert Azimuthal */ + /* ----------------------------------------------------- */ + if (projcode == GCTP_LAMAZ && + upleftpt[0] == 0 && upleftpt[1] == 0 && + lowrightpt[0] == 0 && lowrightpt[1] == 0) + { + /* + * Convert the longitude and latitude from the DMS to decimal degree + * format. + */ + plon = EHconvAng(projparm[4], HDFE_DMS_DEG); + plat = EHconvAng(projparm[5], HDFE_DMS_DEG); + /* + * Compute the longitudes at 90, 180 and 270 degrees from the central + * longitude. + */ - /* Check for valid grid ID */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDextractregion", &fid, &sdInterfaceID, - &gdVgrpID); + if (plon <= 0.0) + { + tlon = 180.0 + plon; + pplon = plon + 360.0; + } + else + { + tlon = plon - 180.0; + pplon = plon; + } + rlon = pplon + 90.0; + if (rlon > 360.0) + rlon = rlon - 360; - /* Check for valid region ID */ - /* ------------------------- */ - if (status == 0) - { - if (regionID < 0 || regionID >= NGRIDREGN) + if (rlon > 180.0) + rlon = rlon - 360.0; + + if (rlon <= 0.0) + llon = 180.0 + rlon; + else + llon = rlon - 180.0; + + /* Convert all four longitudes from decimal degrees to radians */ + plon = EHconvAng(plon, HDFE_DEG_RAD); + tlon = EHconvAng(tlon, HDFE_DEG_RAD); + llon = EHconvAng(llon, HDFE_DEG_RAD); + rlon = EHconvAng(rlon, HDFE_DEG_RAD); + + errorcode = for_trans[projcode] (llon, 0.0, &x, &y); + if (errorcode != 0) { status = -1; - HEpush(DFE_RANGE, "GDextractregion", __FILE__, __LINE__); - HEreport("Invalid Region id: %d.\n", regionID); + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } + upleftpt[0] = x; - /* Check for active region ID */ - /* -------------------------- */ - if (status == 0) - { - if (GDXRegion[regionID] == 0) + errorcode = for_trans[projcode] (rlon, 0.0, &x, &y); + if (errorcode != 0) { status = -1; - HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__); - HEreport("Inactive Region ID: %d.\n", regionID); + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } + lowrightpt[0] = x; + + /* + * Compute upperleft and lowerright values based on whether the + * projection is south polar, north polar or equatorial + */ + + if (plat == -90.0) + { + errorcode = for_trans[projcode] (plon, 0.0, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + + upleftpt[1] = y; + errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } - /* Check that region defined for this file */ - /* --------------------------------------- */ - if (status == 0) - { - if (GDXRegion[regionID]->fid != fid) - { - status = -1; - HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__); - HEreport("Region is not defined for this file.\n"); + lowrightpt[1] = y; } - } - - - /* Check that region defined for this grid */ - /* --------------------------------------- */ - if (status == 0) - { - if (GDXRegion[regionID]->gridID != gridID) + else if (plat == 90.0) { - status = -1; - HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__); - HEreport("Region is not defined for this Grid.\n"); - } - } - + errorcode = for_trans[projcode] (tlon, 0.0, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + upleftpt[1] = y; - /* Check for valid fieldname */ - /* ------------------------- */ - if (status == 0) - { - status = GDfieldinfo(gridID, fieldname, &rank, dims, &ntype, dimlist); + errorcode = for_trans[projcode] (plon, 0.0, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } - if (status != 0) - { - /* Fieldname not found in grid */ - /* --------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" not found.\n", - fieldname); - } - else if (rank == 1) - { - /* Field is 1 dimensional */ - /* ---------------------- */ - status = -1; - HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__); - HEreport( - "One-Dimesional fields \"%s\" may not be subsetted.\n", - fieldname); + lowrightpt[1] = y; } else { - /* "XDim" and/or "YDim" not found */ - /* ------------------------------ */ - if (EHstrwithin("XDim", dimlist, ',') == -1 || - EHstrwithin("YDim", dimlist, ',') == -1) + lat = EHconvAng(90, HDFE_DEG_RAD); + errorcode = for_trans[projcode] (plon, lat, &x, &y); + if (errorcode != 0) { status = -1; - HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__); - snprintf(errbuf, sizeof(errbuf), "%s%s", errM1, errM2); - HEreport(errbuf, fieldname); + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } - } + upleftpt[1] = y; + + lat = EHconvAng(-90, HDFE_DEG_RAD); + errorcode = for_trans[projcode] (plon, lat, &x, &y); + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + lowrightpt[1] = y; + } + } - if (status == 0) + /* Compute Default Boundary Points for Integerized Sinusoidal Grid */ + /* --------------------------------------------------------------- */ + if (((projcode == GCTP_ISINUS) || (projcode == GCTP_ISINUS1)) && + upleftpt[0] == 0 && upleftpt[1] == 0 && + lowrightpt[0] == 0 && lowrightpt[1] == 0) { + /* + * Convert the longitude and latitude from the DMS to decimal degree + * format. + */ + plon = EHconvAng(projparm[4], HDFE_DMS_DEG); + /*plat = EHconvAng(projparm[5], HDFE_DMS_DEG); */ - /* Get origin order info */ - /* --------------------- */ - status = GDorigininfo(gridID, &origincode); - + /* + * Compute the longitudes at 90, 180 and 270 degrees from the central + * longitude. + */ - /* Initialize start & edge arrays */ - /* ------------------------------ */ - for (i = 0; i < rank; i++) + if (plon <= 0.0) { - start[i] = 0; - edge[i] = dims[i]; + tlon = 180.0 + plon; + pplon = plon + 360.0; } - - - /* if MISR SOM projection, set start */ - /* & edge arrays for SOMBlockDim */ - /* --------------------------------- */ - if (EHstrwithin("SOMBlockDim", dimlist, ',') == 0) + else { - l_index = EHstrwithin("SOMBlockDim", dimlist, ','); - edge[l_index] = GDXRegion[regionID]->somCount; - start[l_index] = GDXRegion[regionID]->somStart; + tlon = plon - 180.0; + pplon = plon; } + rlon = pplon + 90.0; + if (rlon > 360.0) + rlon = rlon - 360; - /* Set start & edge arrays for XDim */ - /* -------------------------------- */ - l_index = EHstrwithin("XDim", dimlist, ','); - if (GDXRegion[regionID]->xCount != 0) - { - edge[l_index] = GDXRegion[regionID]->xCount; - start[l_index] = GDXRegion[regionID]->xStart; - } - - /* Adjust X-dim start if origin on right edge */ - /* ------------------------------------------ */ - if ((origincode & 1) == 1) - { - start[l_index] = dims[l_index] - (start[l_index] + edge[l_index]); - } + if (rlon > 180.0) + rlon = rlon - 360.0; + if (rlon <= 0.0) + llon = 180.0 + rlon; + else + llon = rlon - 180.0; - /* Set start & edge arrays for YDim */ - /* -------------------------------- */ - l_index = EHstrwithin("YDim", dimlist, ','); - if (GDXRegion[regionID]->yCount != 0) - { - start[l_index] = GDXRegion[regionID]->yStart; - edge[l_index] = GDXRegion[regionID]->yCount; - } + /* Convert all four longitudes from decimal degrees to radians */ + plon = EHconvAng(plon, HDFE_DEG_RAD); + tlon = EHconvAng(tlon, HDFE_DEG_RAD); + llon = EHconvAng(llon, HDFE_DEG_RAD); + rlon = EHconvAng(rlon, HDFE_DEG_RAD); - /* Adjust Y-dim start if origin on lower edge */ - /* ------------------------------------------ */ - if ((origincode & 2) == 2) + errorcode = for_trans[projcode] (llon, 0.0, &x, &y); + if (errorcode != 0) { - start[l_index] = dims[l_index] - (start[l_index] + edge[l_index]); + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } + upleftpt[0] = x; - - /* Vertical Subset */ - /* --------------- */ - for (j = 0; j < 8; j++) + errorcode = for_trans[projcode] (rlon, 0.0, &x, &y); + if (errorcode != 0) { - /* If active vertical subset ... */ - /* ----------------------------- */ - if (GDXRegion[regionID]->StartVertical[j] != -1) - { - - /* Find vertical dimension within dimlist */ - /* -------------------------------------- */ - l_index = EHstrwithin(GDXRegion[regionID]->DimNamePtr[j], - dimlist, ','); - - /* If dimension found ... */ - /* ---------------------- */ - if (l_index != -1) - { - /* Compute start and edge for vertical dimension */ - /* --------------------------------------------- */ - start[l_index] = GDXRegion[regionID]->StartVertical[j]; - edge[l_index] = GDXRegion[regionID]->StopVertical[j] - - GDXRegion[regionID]->StartVertical[j] + 1; - } - else - { - /* Vertical dimension not found */ - /* ---------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "GDextractregion", __FILE__, __LINE__); - HEreport(errMesg, - GDXRegion[regionID]->DimNamePtr[j]); - } - } + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } + lowrightpt[0] = x; - /* Read into data buffer */ - /* --------------------- */ - if (status == 0) + lat = EHconvAng(90, HDFE_DEG_RAD); + errorcode = for_trans[projcode] (plon, lat, &x, &y); + if (errorcode != 0) { - status = GDreadfield(gridID, fieldname, start, NULL, edge, buffer); + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } - } - return (status); -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDdupregion | -| | -| DESCRIPTION: Duplicates a region | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| newregionID int32 New region ID | -| | -| INPUTS: | -| oldregionID int32 Old region ID | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jan 97 Joel Gales Original Programmer | -| Oct 98 Abe Taaheri changed *GDXRegion[i] = *GDXRegion[oldregionID]; | -| to copy elements of structure one by one to avoid | -| copying pointer for DimNamePtr to another place that| -| causes "Freeing Unallocated Memory" in purify when | -| using GDdetach | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDdupregion(int32 oldregionID) -{ - intn i; /* Loop index */ - intn j; /* Loop index */ - int32 slendupregion; - int32 newregionID = -1; /* New region ID */ + upleftpt[1] = y; - /* Find first empty (inactive) region */ - /* ---------------------------------- */ - for (i = 0; i < NGRIDREGN; i++) - { - if (GDXRegion[i] == 0) + lat = EHconvAng(-90, HDFE_DEG_RAD); + errorcode = for_trans[projcode] (plon, lat, &x, &y); + if (errorcode != 0) { - /* Allocate space for new grid region entry */ - /* ---------------------------------------- */ - GDXRegion[i] = (struct gridRegion *) - calloc(1, sizeof(struct gridRegion)); - if(GDXRegion[i] == NULL) - { - HEpush(DFE_NOSPACE,"GDdupregion", __FILE__, __LINE__); - return(-1); - } - - - /* Copy old region structure data to new region */ - /* -------------------------------------------- */ - - GDXRegion[i]->fid = GDXRegion[oldregionID]->fid; - GDXRegion[i]->gridID = GDXRegion[oldregionID]->gridID; - GDXRegion[i]->xStart = GDXRegion[oldregionID]->xStart; - GDXRegion[i]->xCount = GDXRegion[oldregionID]->xCount; - GDXRegion[i]->yStart = GDXRegion[oldregionID]->yStart; - GDXRegion[i]->yCount = GDXRegion[oldregionID]->yCount; - GDXRegion[i]->upleftpt[0] = GDXRegion[oldregionID]->upleftpt[0]; - GDXRegion[i]->upleftpt[1] = GDXRegion[oldregionID]->upleftpt[1]; - GDXRegion[i]->lowrightpt[0] = GDXRegion[oldregionID]->lowrightpt[0]; - GDXRegion[i]->lowrightpt[1] = GDXRegion[oldregionID]->lowrightpt[1]; - for (j = 0; j < 8; j++) - { - GDXRegion[i]->StartVertical[j] = GDXRegion[oldregionID]->StartVertical[j]; - GDXRegion[i]->StopVertical[j] = GDXRegion[oldregionID]->StopVertical[j]; - } - - for (j=0; j<8; j++) - { - if(GDXRegion[oldregionID]->DimNamePtr[j] != NULL) - { - slendupregion = (int)strlen(GDXRegion[oldregionID]->DimNamePtr[j]); - GDXRegion[i]->DimNamePtr[j] = (char *) malloc(slendupregion + 1); - strcpy(GDXRegion[i]->DimNamePtr[j],GDXRegion[oldregionID]->DimNamePtr[j]); - } - } - - - /* Define new region ID */ - /* -------------------- */ - newregionID = i; - - break; + status = -1; + HEpush(DFE_GENAPP, "GDgetdefaults", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); } + + lowrightpt[1] = y; } - return (newregionID); + return (errorcode); } -#endif - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: GDdefvrtregion | +| FUNCTION: GDll2ij | | | -| DESCRIPTION: Finds elements of a monotonic field within a vertical subset | -| region. | +| DESCRIPTION: | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| regionID int32 Region ID | +| status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| gridID int32 Grid structure ID | -| regionID int32 Region ID | -| vertObj char Vertical object to subset | -| range float64 Vertical subsetting range | +| projcode int32 GCTP projection code | +| zonecode int32 UTM zone code | +| projparm float64 Projection parameters | +| spherecode int32 GCTP spheriod code | +| xdimsize int32 xdimsize from GDcreate | +| ydimsize int32 ydimsize from GDcreate | +| upleftpt float64 upper left corner coordinates | +| lowrightpt float64 lower right corner coordinates | +| npnts int32 number of lon-lat points | +| longitude float64 longitude array (radians) | +| latitude float64 latitude array (radians) | | | | OUTPUTS: | -| None | +| row int32 Row array | +| col int32 Column array | +| xval float64 X value array | +| yval float64 Y value array | +| | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Aug 96 Joel Gales Original Programmer | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Feb 97 Joel Gales Store XDim, YDim, upleftpt, lowrightpt in GDXRegion | +| Jun 96 Joel Gales Original Programmer | +| Aug 96 Joel Gales Return x and y values if requested | +| Jun 00 Abe Taaheri Added support for EASE grid | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -#define SETGRIDREG \ -\ -regionID = 0; \ -status = GDgridinfo(gridID, &xdimsize, &ydimsize, upleftpt, lowrightpt); \ -for (k = 0; k < NGRIDREGN; k++) \ -{ \ - if (GDXRegion[k] == 0) \ - { \ - GDXRegion[k] = (struct gridRegion *) \ - calloc(1, sizeof(struct gridRegion)); \ - GDXRegion[k]->fid = fid; \ - GDXRegion[k]->gridID = gridID; \ - GDXRegion[k]->xStart = 0; \ - GDXRegion[k]->xCount = xdimsize; \ - GDXRegion[k]->yStart = 0; \ - GDXRegion[k]->yCount = ydimsize; \ - GDXRegion[k]->upleftpt[0] = upleftpt[0]; \ - GDXRegion[k]->upleftpt[1] = upleftpt[1]; \ - GDXRegion[k]->lowrightpt[0] = lowrightpt[0]; \ - GDXRegion[k]->lowrightpt[1] = lowrightpt[1]; \ - regionID = k; \ - for (j=0; j<8; j++) \ - { \ - GDXRegion[k]->StartVertical[j] = -1; \ - GDXRegion[k]->StopVertical[j] = -1; \ - } \ - break; \ - } \ -} - -#define FILLVERTREG \ -for (j=0; j<8; j++) \ -{ \ - if (GDXRegion[regionID]->StartVertical[j] == -1) \ - { \ - GDXRegion[regionID]->StartVertical[j] = i; \ - GDXRegion[regionID]->DimNamePtr[j] = \ - (char *) malloc(slen + 1); \ - memcpy(GDXRegion[regionID]->DimNamePtr[j], \ - dimlist, slen + 1); \ - break; \ - } \ -} \ +static intn +GDll2ij(int32 projcode, int32 zonecode, float64 projparm[], + int32 spherecode, int32 xdimsize, int32 ydimsize, + float64 upleftpt[], float64 lowrightpt[], + int32 npnts, float64 longitude[], float64 latitude[], + int32 row[], int32 col[], float64 xval[], float64 yval[]) -#ifdef HDFEOS_GD_WRITE_SUPPORT -int32 -GDdefvrtregion(int32 gridID, int32 regionID, const char *vertObj, float64 range[]) { - intn i, j = 0, k, status; - uint8 found = 0; - - int16 vertINT16; - - int32 fid, sdInterfaceID, slen; - int32 gdVgrpID, rank, nt, dims[8], size; - int32 vertINT32; - int32 xdimsize; - int32 ydimsize; + intn i; /* Loop index */ + intn status = 0; /* routine return status variable */ - float32 vertFLT32; - float64 vertFLT64; - float64 upleftpt[2]; - float64 lowrightpt[2]; + int32 errorcode = 0; /* GCTP error code */ + int32(*for_trans[100]) (double, double, double*, double*); /* GCTP function pointer */ - char *vertArr; - char *dimlist; + float64 xVal; /* Scaled x distance */ + float64 yVal; /* Scaled y distance */ + float64 xMtr; /* X value in meters from GCTP */ + float64 yMtr; /* Y value in meters from GCTP */ + float64 lonrad0; /* Longitude in radians of upleft point */ + float64 latrad0 = 0; /* Latitude in radians of upleft point */ + float64 lonrad; /* Longitude in radians of point */ + float64 latrad; /* Latitude in radians of point */ + float64 scaleX; /* X scale factor */ + float64 scaleY; /* Y scale factor */ + float64 xMtr0 = 0, xMtr1, yMtr0 = 0, yMtr1; + float64 lonrad1; /* Longitude in radians of lowright point */ - /* Allocate space for dimlist */ - /* --------------------------------- */ - dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(dimlist == NULL) + /* If projection not GEO call GCTP initialization routine */ + /* ------------------------------------------------------ */ + if (projcode != GCTP_GEO) { - HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__); - return(-1); + for_init(projcode, zonecode, projparm, spherecode, NULL, NULL, + &errorcode, for_trans); + + /* Report error if any */ + /* ------------------- */ + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + } } - /* Check for valid grid ID */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDdefvrtregion", - &fid, &sdInterfaceID, &gdVgrpID); + if (status == 0) { - memcpy(dimlist, vertObj, 4); - dimlist[4] = 0; - - if (strcmp(dimlist, "DIM:") == 0) - { - slen = (int)strlen(vertObj) - 4; - if (regionID == -1) - { - SETGRIDREG; - } - for (j = 0; j < 8; j++) - { - if (GDXRegion[regionID]->StartVertical[j] == -1) - { - GDXRegion[regionID]->StartVertical[j] = (int32) range[0]; - GDXRegion[regionID]->StopVertical[j] = (int32) range[1]; - GDXRegion[regionID]->DimNamePtr[j] = - (char *) malloc(slen + 1); - if(GDXRegion[regionID]->DimNamePtr[j] == NULL) - { - HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__); - free(dimlist); - return(-1); - } - memcpy(GDXRegion[regionID]->DimNamePtr[j], - vertObj + 4, slen + 1); - break; - } - } - } - else + /* GEO projection */ + /* -------------- */ + if (projcode == GCTP_GEO) { - status = GDfieldinfo(gridID, vertObj, &rank, dims, &nt, dimlist); - if (status != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDdefvrtregion", __FILE__, __LINE__); - HEreport("Vertical Field: \"%s\" not found.\n", vertObj); - } - else - { - if (rank != 1) - { - status = -1; - HEpush(DFE_GENAPP, "GDdefvrtregion", __FILE__, __LINE__); - HEreport("Vertical Field: \"%s\" must be 1-dim.\n", - vertObj); - } - else - { - slen = (int)strlen(dimlist); - size = DFKNTsize(nt); - vertArr = (char *) calloc(dims[0], size); - if(vertArr == NULL) - { - HEpush(DFE_NOSPACE,"GDdefvrtregion", __FILE__, __LINE__); - free(dimlist); - return(-1); - } - - status = GDreadfield(gridID, vertObj, - NULL, NULL, NULL, vertArr); + /* Convert upleft and lowright X coords from DMS to radians */ + /* -------------------------------------------------------- */ + lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); + lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); - switch (nt) - { - case DFNT_INT16: + /* Compute x scale factor */ + /* ---------------------- */ + scaleX = (lonrad - lonrad0) / xdimsize; - for (i = 0; i < dims[0]; i++) - { - memcpy(&vertINT16, vertArr + i * size, size); - if (vertINT16 >= range[0] && - vertINT16 <= range[1]) - { - found = 1; - if (regionID == -1) - { - SETGRIDREG; - } - FILLVERTREG; - - break; - } - } + /* Convert upleft and lowright Y coords from DMS to radians */ + /* -------------------------------------------------------- */ + latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD); + latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD); - if (found == 1) - { - for (i = dims[0] - 1; i >= 0; i--) - { - memcpy(&vertINT16, vertArr + i * size, size); - - if (vertINT16 >= range[0] && - vertINT16 <= range[1]) - { - GDXRegion[regionID]->StopVertical[j] = i; - break; - } - } - } - else - { - status = -1; - } - break; + /* Compute y scale factor */ + /* ---------------------- */ + scaleY = (latrad - latrad0) / ydimsize; + } - case DFNT_INT32: + /* BCEA projection */ + /* -------------- */ + else if ( projcode == GCTP_BCEA) + { + /* Convert upleft and lowright X coords from DMS to radians */ + /* -------------------------------------------------------- */ - for (i = 0; i < dims[0]; i++) - { - memcpy(&vertINT32, vertArr + i * size, size); + lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); + lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); - if (vertINT32 >= range[0] && - vertINT32 <= range[1]) - { - found = 1; - if (regionID == -1) - { - SETGRIDREG; - } - FILLVERTREG; - - break; - } - } + /* Convert upleft and lowright Y coords from DMS to radians */ + /* -------------------------------------------------------- */ + latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD); + latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD); - if (found == 1) - { - for (i = dims[0] - 1; i >= 0; i--) - { - memcpy(&vertINT32, vertArr + i * size, size); - - if (vertINT32 >= range[0] && - vertINT32 <= range[1]) - { - GDXRegion[regionID]->StopVertical[j] = i; - break; - } - } - } - else - { - status = -1; - } - break; + /* Convert from lon/lat to meters(or whatever unit is, i.e unit + of r_major and r_minor) using GCTP */ + /* ----------------------------------------- */ + errorcode = for_trans[projcode] (lonrad0, latrad0, &xMtr0, &yMtr0); - case DFNT_FLOAT32: + /* Report error if any */ + /* ------------------- */ + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } - for (i = 0; i < dims[0]; i++) - { - memcpy(&vertFLT32, vertArr + i * size, size); + /* Convert from lon/lat to meters(or whatever unit is, i.e unit + of r_major and r_minor) using GCTP */ + /* ----------------------------------------- */ + errorcode = for_trans[projcode] (lonrad, latrad, &xMtr1, &yMtr1); - if (vertFLT32 >= range[0] && - vertFLT32 <= range[1]) - { - found = 1; - if (regionID == -1) - { - SETGRIDREG; - } - FILLVERTREG; - - break; - } - } - if (found == 1) - { - for (i = dims[0] - 1; i >= 0; i--) - { - memcpy(&vertFLT32, vertArr + i * size, size); - - if (vertFLT32 >= range[0] && - vertFLT32 <= range[1]) - { - GDXRegion[regionID]->StopVertical[j] = i; - break; - } - } - } - else - { - status = -1; - } - break; + /* Report error if any */ + /* ------------------- */ + if (errorcode != 0) + { + status = -1; + HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); + } + /* Compute x scale factor */ + /* ---------------------- */ + scaleX = (xMtr1 - xMtr0) / xdimsize; - case DFNT_FLOAT64: + /* Compute y scale factor */ + /* ---------------------- */ + scaleY = (yMtr1 - yMtr0) / ydimsize; + } + else + { + /* Non-GEO, Non_BCEA projections */ + /* ---------------------------- */ - for (i = 0; i < dims[0]; i++) - { - memcpy(&vertFLT64, vertArr + i * size, size); + /* Compute x & y scale factors */ + /* --------------------------- */ + scaleX = (lowrightpt[0] - upleftpt[0]) / xdimsize; + scaleY = (lowrightpt[1] - upleftpt[1]) / ydimsize; + } - if (vertFLT64 >= range[0] && - vertFLT64 <= range[1]) - { - found = 1; - if (regionID == -1) - { - SETGRIDREG; - } - FILLVERTREG; - - break; - } - } - if (found == 1) - { - for (i = dims[0] - 1; i >= 0; i--) - { - memcpy(&vertFLT64, vertArr + i * size, size); - - if (vertFLT64 >= range[0] && - vertFLT64 <= range[1]) - { - GDXRegion[regionID]->StopVertical[j] = i; - break; - } - } - } - else - { - status = -1; - } - break; - } - free(vertArr); - } - } - } - } - if (status == -1) - { - regionID = -1; - } - free(dimlist); - return (regionID); -} + /* Loop through all points */ + /* ----------------------- */ + for (i = 0; i < npnts; i++) + { + /* Convert lon & lat from decimal degrees to radians */ + /* ------------------------------------------------- */ + lonrad = EHconvAng(longitude[i], HDFE_DEG_RAD); + latrad = EHconvAng(latitude[i], HDFE_DEG_RAD); + /* GEO projection */ + /* -------------- */ + if (projcode == GCTP_GEO) + { + /* allow map to span dateline */ + lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); + lonrad1 = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); + /* if time-line is passed */ + if(lonrad < lonrad1) + { + if (lonrad < lonrad0) lonrad += 2.0 * M_PI; + if (lonrad > lonrad1) lonrad -= 2.0 * M_PI; + } + /* Compute scaled distance to point from origin */ + /* -------------------------------------------- */ + xVal = (lonrad - lonrad0) / scaleX; + yVal = (latrad - latrad0) / scaleY; + } + else + { + /* Convert from lon/lat to meters using GCTP */ + /* ----------------------------------------- */ + errorcode = for_trans[projcode] (lonrad, latrad, &xMtr, &yMtr); -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDdeftimeperiod | -| | -| DESCRIPTION: Finds elements of the "Time" field within a given time | -| period. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| periodID int32 Period ID | -| | -| INPUTS: | -| gridID int32 Grid structure ID | -| periodID int32 Period ID | -| starttime float64 TAI sec Start of time period | -| stoptime float64 TAI sec Stop of time period | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Aug 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDdeftimeperiod(int32 gridID, int32 periodID, float64 starttime, - float64 stoptime) -{ - float64 timerange[2]; + /* Report error if any */ + /* ------------------- */ + if (errorcode != 0) + { + /*status = -1; + HEpush(DFE_GENAPP, "GDll2ij", __FILE__, __LINE__); + HEreport("GCTP Error: %d\n", errorcode); + return (status); */ /* Bruce Beaumont */ + xVal = -2147483648.0; /* Bruce Beaumont */ + yVal = -2147483648.0; /* Bruce Beaumont */ + }/* (Note: MAXLONG is defined as 2147483647.0 in + function cproj.c of GCTP) */ + else { + /* if projection is BCEA normalize x and y by cell size and + measure it from the upperleft corner of the grid */ + + /* Compute scaled distance to point from origin */ + /* -------------------------------------------- */ + if( projcode == GCTP_BCEA) + { + xVal = (xMtr - xMtr0) / scaleX; + yVal = (yMtr - yMtr0) / scaleY; + } + else + { + xVal = (xMtr - upleftpt[0]) / scaleX; + yVal = (yMtr - upleftpt[1]) / scaleY; + } + } + } - timerange[0] = starttime; - timerange[1] = stoptime; - periodID = GDdefvrtregion(gridID, periodID, "Time", timerange); + /* Compute row and col from scaled distance */ + /* ---------------------------------------- */ + col[i] = (int32) xVal; + row[i] = (int32) yVal; - return (periodID); -} -#endif + /* Store scaled distances if requested */ + /* ----------------------------------- */ + if (xval != NULL) + { + xval[i] = xVal; + } + if (yval != NULL) + { + yval[i] = yVal; + } + } + } + return (status); +} /*----------------------------------------------------------------------------| | BEGIN_PROLOG | @@ -9842,443 +4701,15 @@ GDgetpixvalues(int32 gridID, int32 nPixels, int32 pixRow[], int32 pixCol[], for (j = 0; j < rankSDS; j++) { incr[j] = 1; - } - - - /* Read into data buffer */ - /* --------------------- */ - status = SDreaddata(sdid, - offset, incr, count, - (VOIDP) ((uint8 *) buffer + bufOffset)); - } - } - } - } - } - - - /* If successful return size of returned data in bytes */ - /* --------------------------------------------------- */ - if (status == 0) - { - free(dimlist); - return (size * nPixels); - } - else - { - free(dimlist); - return ((int32) status); - } -} - - -#ifdef UNUSED_BY_GDAL -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDinterpolate | -| | -| DESCRIPTION: Performs bilinear interpolate on a set of xy values | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nRetn*nValues* int32 Size of data buffer | -| sizeof(float64) | -| | -| INPUTS: | -| gridID int32 Grid structure ID | -| nValues int32 Number of lon/lat points to interpolate | -| xyValues float64 XY values of points to interpolate | -| fieldname char Fieldname | -| | -| OUTPUTS: | -| interpVal float64 Interpolated Data Values | -| | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Aug 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Fix array l_index problem with interpVal write | -| Apr 97 Joel Gales Trap interpolation boundary out of bounds error | -| Jun 98 Abe Taaheri changed the return value so that the Return Value | -| is size in bytes for the data buffer which is | -| float64. -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -GDinterpolate(int32 gridID, int32 nValues, float64 lonVal[], float64 latVal[], - const char *fieldname, float64 interpVal[]) -{ - intn i; /* Loop index */ - intn j; /* Loop index */ - intn k; /* Loop index */ - intn status = 0; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int32 xdimsize; /* XDim size */ - int32 ydimsize; /* YDim size */ - int32 projcode; /* Projection code */ - int32 zonecode; /* Zone code */ - int32 spherecode; /* Sphere code */ - int32 pixregcode; /* Pixel registration code */ - int32 origincode; /* Origin code */ - int32 dims[8]; /* Field dimensions */ - int32 numsize; /* Size in bytes of number type */ - int32 rank; /* Field rank */ - int32 xdum = 0; /* Location of "XDim" within field list */ - int32 ydum = 0; /* Location of "YDim" within field list */ - int32 ntype; /* Number type */ - int32 dum; /* Dummy variable */ - int32 size; /* Size of returned data buffer for each - * value in bytes */ - int32 pixCol[4]; /* Pixel columns for 4 nearest neighbors */ - int32 pixRow[4]; /* Pixel rows for 4 nearest neighbors */ - int32 tDen; /* Interpolation denominator value 1 */ - int32 uDen; /* Interpolation denominator value 2 */ - int32 nRetn = 0; /* Number of data values returned */ - - float64 upleftpt[2];/* Upper left pt coordinates */ - float64 lowrightpt[2]; /* Lower right pt coordinates */ - float64 projparm[16]; /* Projection parameters */ - float64 xVal = 0.0; /* "Exact" x location of interpolated point */ - float64 yVal = 0.0; /* "Exact" y location of interpolated point */ - float64 tNum = 0.0; /* Interpolation numerator value 1 */ - float64 uNum = 0.0; /* Interpolation numerator value 2 */ - - int16 i16[4]; /* Working buffer (int16) */ - int32 i32[4]; /* Working buffer (int132) */ - float32 f32[4]; /* Working buffer (float32) */ - float64 f64[4]; /* Working buffer (float64) */ - - char *pixVal; /* Nearest neighbor values */ - char *dimlist; /* Dimension list */ - - /* Allocate space for dimlist */ - /* --------------------------------- */ - dimlist = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(dimlist == NULL) - { - HEpush(DFE_NOSPACE,"GDinterpolate", __FILE__, __LINE__); - return(-1); - } - /* Check for valid grid ID */ - /* ----------------------- */ - status = GDchkgdid(gridID, "GDinterpolate", - &fid, &sdInterfaceID, &gdVgrpID); - - - /* If no problems ... */ - /* ------------------ */ - if (status == 0) - { - /* Get field information */ - /* --------------------- */ - status = GDfieldinfo(gridID, fieldname, &rank, dims, &ntype, dimlist); - - - /* Check for "XDim" & "YDim" in dimension list */ - /* ------------------------------------------- */ - if (status == 0) - { - xdum = EHstrwithin("XDim", dimlist, ','); - ydum = EHstrwithin("YDim", dimlist, ','); - - if (xdum == -1) - { - status = -1; - HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__); - HEreport( - "\"XDim\" not present in dimlist for field: \"%s\".\n", - fieldname); - } - - - if (ydum == -1) - { - status = -1; - HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__); - HEreport( - "\"YDim\" not present in dimlist for field: \"%s\".\n", - fieldname); - } - } - else - { - /* Fieldname not found in grid */ - /* --------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" not found.\n", fieldname); - } - - - /* If no problems ... */ - /* ------------------ */ - if (status == 0) - { - /* Compute size of data buffer for each interpolated value */ - /* ------------------------------------------------------- */ - dims[xdum] = 1; - dims[ydum] = 1; - size = dims[0]; - for (i = 1; i < rank; i++) - { - size *= dims[i]; - } - numsize = DFKNTsize(ntype); - size *= numsize; - - nRetn = size / numsize; - - - - /* If interpolated values are requested ... */ - /* ---------------------------------------- */ - if (interpVal != NULL) - { - /* Get grid info */ - /* ------------- */ - status = GDgridinfo(gridID, &xdimsize, &ydimsize, - upleftpt, lowrightpt); - - - /* Get projection info */ - /* ------------------- */ - status = GDprojinfo(gridID, &projcode, &zonecode, - &spherecode, projparm); - - - /* Get explicit upleftpt & lowrightpt if defaults are used */ - /* ------------------------------------------------------- */ - status = GDgetdefaults(projcode, zonecode, projparm, - spherecode, upleftpt, lowrightpt); - - - /* Get pixel registration and origin info */ - /* -------------------------------------- */ - status = GDpixreginfo(gridID, &pixregcode); - status = GDorigininfo(gridID, &origincode); - - - - /* Loop through all interpolated points */ - /* ------------------------------------ */ - for (i = 0; i < nValues; i++) - { - /* Get row & column of point pixel */ - /* ------------------------------- */ - status = GDll2ij(projcode, zonecode, projparm, spherecode, - xdimsize, ydimsize, upleftpt, lowrightpt, - 1, &lonVal[i], &latVal[i], - pixRow, pixCol, &xVal, &yVal); - - - /* Get diff of interp. point from pixel location */ - /* --------------------------------------------- */ - if (pixregcode == HDFE_CENTER) - { - tNum = xVal - (pixCol[0] + 0.5); - uNum = yVal - (pixRow[0] + 0.5); - } - else if (origincode == HDFE_GD_UL) - { - tNum = xVal - pixCol[0]; - uNum = yVal - pixRow[0]; - } - else if (origincode == HDFE_GD_UR) - { - tNum = xVal - (pixCol[0] + 1); - uNum = yVal - pixRow[0]; - } - else if (origincode == HDFE_GD_LL) - { - tNum = xVal - pixCol[0]; - uNum = yVal - (pixRow[0] + 1); - } - else if (origincode == HDFE_GD_LR) - { - tNum = xVal - (pixCol[0] + 1); - uNum = yVal - (pixRow[0] + 1); - } - - - /* Get rows and columns of other nearest neighbor pixels */ - /* ----------------------------------------------------- */ - pixCol[1] = pixCol[0]; - pixRow[3] = pixRow[0]; - - if (tNum >= 0) - { - pixCol[2] = pixCol[0] + 1; - pixCol[3] = pixCol[0] + 1; - } - - if (tNum < 0) - { - pixCol[2] = pixCol[0] - 1; - pixCol[3] = pixCol[0] - 1; - } - - if (uNum >= 0) - { - pixRow[2] = pixRow[0] + 1; - pixRow[1] = pixRow[0] + 1; - } - - if (uNum < 0) - { - pixRow[2] = pixRow[0] - 1; - pixRow[1] = pixRow[0] - 1; - } - - - /* Get values of nearest neighbors */ - /* -------------------------------- */ - pixVal = (char *) malloc(4 * size); - if(pixVal == NULL) - { - HEpush(DFE_NOSPACE,"GDinterpolate", __FILE__, __LINE__); - free(dimlist); - return(-1); - } - dum = GDgetpixvalues(gridID, 4, pixRow, pixCol, - fieldname, pixVal); - - - /* Trap interpolation boundary out of range error */ - /* ---------------------------------------------- */ - if (dum == -1) - { - status = -1; - HEpush(DFE_GENAPP, "GDinterpolate", __FILE__, __LINE__); - HEreport("Interpolation boundary outside of grid.\n"); - } - else - { - - /* - * Algorithm taken for Numerical Recipes in C, 2nd - * edition, Section 3.6 - */ - - /* Perform bilinear interpolation */ - /* ------------------------------ */ - tDen = pixCol[3] - pixCol[0]; - uDen = pixRow[1] - pixRow[0]; - - switch (ntype) - { - case DFNT_INT16: - - - /* Loop through all returned data values */ - /* ------------------------------------- */ - for (j = 0; j < nRetn; j++) - { - /* Copy 4 NN values into working array */ - /* ----------------------------------- */ - for (k = 0; k < 4; k++) - { - memcpy(&i16[k], - pixVal + j * numsize + k * size, - sizeof(int16)); - } - - /* Compute interpolated value */ - /* -------------------------- */ - interpVal[i * nRetn + j] = - (1 - tNum / tDen) * (1 - uNum / uDen) * - i16[0] + - (tNum / tDen) * (1 - uNum / uDen) * - i16[3] + - (tNum / tDen) * (uNum / uDen) * - i16[2] + - (1 - tNum / tDen) * (uNum / uDen) * - i16[1]; - } - break; - - - case DFNT_INT32: - - for (j = 0; j < nRetn; j++) - { - for (k = 0; k < 4; k++) - { - memcpy(&i32[k], - pixVal + j * numsize + k * size, - sizeof(int32)); - } - - interpVal[i * nRetn + j] = - (1 - tNum / tDen) * (1 - uNum / uDen) * - i32[0] + - (tNum / tDen) * (1 - uNum / uDen) * - i32[3] + - (tNum / tDen) * (uNum / uDen) * - i32[2] + - (1 - tNum / tDen) * (uNum / uDen) * - i32[1]; - } - break; - - - case DFNT_FLOAT32: - - for (j = 0; j < nRetn; j++) - { - for (k = 0; k < 4; k++) - { - memcpy(&f32[k], - pixVal + j * numsize + k * size, - sizeof(float32)); - } - - interpVal[i * nRetn + j] = - (1 - tNum / tDen) * (1 - uNum / uDen) * - f32[0] + - (tNum / tDen) * (1 - uNum / uDen) * - f32[3] + - (tNum / tDen) * (uNum / uDen) * - f32[2] + - (1 - tNum / tDen) * (uNum / uDen) * - f32[1]; - } - break; - + } - case DFNT_FLOAT64: - for (j = 0; j < nRetn; j++) - { - for (k = 0; k < 4; k++) - { - memcpy(&f64[k], - pixVal + j * numsize + k * size, - sizeof(float64)); - } - - interpVal[i * nRetn + j] = - (1 - tNum / tDen) * (1 - uNum / uDen) * - f64[0] + - (tNum / tDen) * (1 - uNum / uDen) * - f64[3] + - (tNum / tDen) * (uNum / uDen) * - f64[2] + - (1 - tNum / tDen) * (uNum / uDen) * - f64[1]; - } - break; - } + /* Read into data buffer */ + /* --------------------- */ + status = SDreaddata(sdid, + offset, incr, count, + (VOIDP) ((uint8 *) buffer + bufOffset)); } - free(pixVal); } } } @@ -10289,18 +4720,15 @@ GDinterpolate(int32 gridID, int32 nValues, float64 lonVal[], float64 latVal[], /* --------------------------------------------------- */ if (status == 0) { - /*always return size of float64 buffer */ free(dimlist); - return (nRetn * nValues * sizeof(float64)); + return (size * nPixels); } else { free(dimlist); return ((int32) status); } - } -#endif /*********************************************** GDwrrdtile -- @@ -10516,29 +4944,6 @@ GDtileinfo(int32 gridID, const char *fieldname, int32 * tilecode, int32 * tilera } return (status); } -/*********************************************** -GDwritetile -- - This function writes one tile to a particular field. - - -Author-- -Alexis Zubrow - -********************************************************/ - -#ifdef HDFEOS_GD_WRITE_SUPPORT -intn -GDwritetile(int32 gridID, const char *fieldname, int32 tilecoords[], - VOIDP tileData) -{ - char code[] = "w"; /* write tile code */ - intn status = 0; /* routine return status variable */ - - status = GDwrrdtile(gridID, fieldname, code, tilecoords, tileData); - - return (status); -} -#endif /*********************************************** GDreadtile -- @@ -10562,463 +4967,6 @@ GDreadtile(int32 gridID, const char *fieldname, int32 tilecoords[], return (status); } -#ifdef UNUSED_BY_GDAL -/*********************************************** -GDsettilecache -- - This function sets the cache size for a tiled field. - - -Author-- -Alexis Zubrow - -********************************************************/ - -intn -GDsettilecache(int32 gridID, const char *fieldname, int32 maxcache, CPL_UNUSED int32 cachecode) -{ - - intn status = 0; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 sdid; /* SDS ID */ - - int32 dum; /* Dummy variable */ - - int32 dims[8]; /* Field/SDS dimensions */ - - - /* Check gridID */ - status = GDchkgdid(gridID, "GDwrrdtile", &fid, &sdInterfaceID, &dum); - if (status == 0) - { - - /* Get field info */ - status = GDfieldinfo(gridID, fieldname, &dum, dims, &dum, NULL); - - if (status == 0) - { - - /* Check whether fieldname is in SDS (multi-dim field) */ - /* --------------------------------------------------- */ - status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, &sdid, - &dum, &dum, &dum, dims, &dum); - - - /* Check if maxcache is less than or equal to zero */ - if (maxcache <= 0) - { - HEpush(DFE_GENAPP, "GDsettilecache", __FILE__, __LINE__); - HEreport("Improper maxcache \"%d\"... \n", maxcache); - HEreport("maxcache must be greater than zero.\n"); - status = -1; - return (status); - } - - - /* Set the number of tiles to cache */ - /* Presently, the only cache flag allowed is 0 */ - status = SDsetchunkcache(sdid, maxcache, 0); - - - } - - /* Non-existent fieldname */ - else - { - HEpush(DFE_GENAPP, "GDwrrdtile", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", fieldname); - status = -1; - } - - } - - return (status); -} -#endif - - -#ifdef HDFEOS_GD_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDsettilecomp | -| | -| DESCRIPTION: Sets the tiling/compression parameters for the specified | -| field. This can be called after GDsetfillvalue and assumes | -| that the field was defined with no compression/tiling set | -| by GDdeftile or GDdefcomp. | -| | -| This function replaces the following sequence: | -| GDdefcomp | -| GDdeftile | -| GDdeffield | -| GDsetfillvalue | -| with: | -| GDdeffield | -| GDsetfillvalue | -| GDsettilecomp | -| so that fill values will work correctly. | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| gridID int32 grid structure ID | -| fieldname char field name | -| tilerank int32 number of tiling dimensions | -| tiledims int32 tiling dimensions | -| compcode int32 compression code | -| compparm intn compression parameters | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 98 MISR Used GDsetfillvalue as a template and copied | -| tiling/comp portions of GDdeffield.(NCR15866). | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -GDsettilecomp(int32 gridID, const char *fieldname, int32 tilerank, int32* - tiledims, int32 compcode, intn* compparm) -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 gdVgrpID; /* Grid root Vgroup ID */ - int i; /* Looping variable. */ - int32 sdid; /* SDS id */ - int32 nt; /* Number type */ - int32 dims[8]; /* Dimensions array */ - int32 dum; /* Dummy variable */ - int32 solo; /* "Solo" (non-merged) field flag */ - comp_info c_info; /* Compression parameter structure */ - HDF_CHUNK_DEF chunkDef; /* Tiling structure */ - int32 chunkFlag; /* Chunking (Tiling) flag */ - - c_info.nbit.nt = 0; - - /* Check for valid grid ID and get SDS interface ID */ - status = GDchkgdid(gridID, "GDsetfillvalue", - &fid, &sdInterfaceID, &gdVgrpID); - - if (status == 0) - { - /* Get field info */ - status = GDfieldinfo(gridID, fieldname, &dum, dims, &nt, NULL); - - if (status == 0) - { - /* Get SDS ID and solo flag */ - status = GDSDfldsrch(gridID, sdInterfaceID, fieldname, - &sdid, &dum, &dum, &dum, - dims, &solo); - if (status !=0) { - HEpush(DFE_GENAPP, "GDsettilecomp", __FILE__, __LINE__); - HEreport("GDSDfldsrch failed\n", fieldname); - return FAIL; - } - /* Tiling with Compression */ - /* ----------------------- */ - - - /* Setup Compression */ - /* ----------------- */ - if (compcode == HDFE_COMP_NBIT) - { - c_info.nbit.nt = nt; - c_info.nbit.sign_ext = compparm[0]; - c_info.nbit.fill_one = compparm[1]; - c_info.nbit.start_bit = compparm[2]; - c_info.nbit.bit_len = compparm[3]; - } - else if (compcode == HDFE_COMP_SKPHUFF) - { - c_info.skphuff.skp_size = (intn) DFKNTsize(nt); - } - else if (compcode == HDFE_COMP_DEFLATE) - { - c_info.deflate.level = compparm[0]; - } - - /* Setup chunk lengths */ - /* ------------------- */ - for (i = 0; i < tilerank; i++) - { - chunkDef.comp.chunk_lengths[i] = tiledims[i]; - } - - /* Setup chunk flag & chunk compression type */ - /* ----------------------------------------- */ - chunkFlag = HDF_CHUNK | HDF_COMP; - chunkDef.comp.comp_type = compcode; - - /* Setup chunk compression parameters */ - /* ---------------------------------- */ - if (compcode == HDFE_COMP_SKPHUFF) - { - chunkDef.comp.cinfo.skphuff.skp_size = - c_info.skphuff.skp_size; - } - else if (compcode == HDFE_COMP_DEFLATE) - { - chunkDef.comp.cinfo.deflate.level = - c_info.deflate.level; - } - /* Call SDsetchunk routine */ - /* ----------------------- */ - status = SDsetchunk(sdid, chunkDef, chunkFlag); - if (status ==FAIL) { - HEpush(DFE_GENAPP, "GDsettilecomp", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", - fieldname); - return status; - } - } - else - { - HEpush(DFE_GENAPP, "GDsettilecomp", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", fieldname); - } - } - return (status); -} -#endif - -#ifdef UNUSED_BY_GDAL -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDll2mm_cea | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| projcode int32 GCTP projection code | -| zonecode int32 UTM zone code | -| projparm float64 Projection parameters | -| spherecode int32 GCTP spheriod code | -| xdimsize int32 xdimsize from GDcreate | -| ydimsize int32 ydimsize from GDcreate | -| upleftpt float64 upper left corner coordinates (DMS) | -| lowrightpt float64 lower right corner coordinates (DMS) | -| longitude float64 longitude array (DMS) | -| latitude float64 latitude array (DMS) | -| npnts int32 number of lon-lat points | -| | -| OUTPUTS: | -| x float64 X value array | -| y float64 Y value array | -| scaleX float64 X grid size | -| scaley float64 Y grid size | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Oct 02 Abe Taaheri Added support for EASE grid | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn GDll2mm_cea(int32 projcode,int32 zonecode, int32 spherecode, - float64 projparm[], - int32 xdimsize, int32 ydimsize, - float64 upleftpt[], float64 lowrightpt[], int32 npnts, - CPL_UNUSED float64 lon[],CPL_UNUSED float64 lat[], - float64 x[],float64 y[], float64 *scaleX,float64 *scaleY) -{ - intn status = 0; /* routine return status variable */ - int32 errorcode = 0; /* GCTP error code */ - float64 xMtr0, xMtr1, yMtr0, yMtr1; - float64 lonrad0; /* Longitude in radians of upleft point */ - float64 latrad0; /* Latitude in radians of upleft point */ - float64 lonrad; /* Longitude in radians of point */ - float64 latrad; /* Latitude in radians of point */ - int32(*for_trans[100]) (double, double, double*, double*); /* GCTP function pointer */ - - if(npnts <= 0) - { - HEpush(DFE_GENAPP, " GDll2mm_cea", __FILE__, __LINE__); - HEreport("Improper npnts value\"%d\"... \n", npnts); - HEreport("npnts must be greater than zero.\n"); - status = -1; - return (status); - } - if ( projcode == GCTP_BCEA) - { - for_init(projcode, zonecode, projparm, spherecode, NULL, NULL, - &errorcode, for_trans); - /* Convert upleft and lowright X coords from DMS to radians */ - /* -------------------------------------------------------- */ - - lonrad0 = EHconvAng(upleftpt[0], HDFE_DMS_RAD); - lonrad = EHconvAng(lowrightpt[0], HDFE_DMS_RAD); - - /* Convert upleft and lowright Y coords from DMS to radians */ - /* -------------------------------------------------------- */ - latrad0 = EHconvAng(upleftpt[1], HDFE_DMS_RAD); - latrad = EHconvAng(lowrightpt[1], HDFE_DMS_RAD); - - /* Convert from lon/lat to meters(or whatever unit is, i.e unit - of r_major and r_minor) using GCTP */ - /* ----------------------------------------- */ - errorcode = for_trans[projcode] (lonrad0, latrad0, &xMtr0, &yMtr0); - x[0] = xMtr0; - y[0] = yMtr0; - - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDll2mm_cea", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - /* Convert from lon/lat to meters(or whatever unit is, i.e unit - of r_major and r_minor) using GCTP */ - /* ----------------------------------------- */ - errorcode = for_trans[projcode] (lonrad, latrad, &xMtr1, &yMtr1); - x[1] = xMtr1; - y[1] = yMtr1; - - /* Report error if any */ - /* ------------------- */ - if (errorcode != 0) - { - status = -1; - HEpush(DFE_GENAPP, "GDll2mm_cea", __FILE__, __LINE__); - HEreport("GCTP Error: %d\n", errorcode); - return (status); - } - - /* Compute x scale factor */ - /* ---------------------- */ - *scaleX = (xMtr1 - xMtr0) / xdimsize; - - /* Compute y scale factor */ - /* ---------------------- */ - *scaleY = (yMtr1 - yMtr0) / ydimsize; - } - else - { - status = -1; - HEpush(DFE_GENAPP, "GDll2mm_cea", __FILE__, __LINE__); - HEreport("Wrong projection code; this function is only for EASE grid"); - return (status); - } - return (0); -} -#endif - - -#ifdef HDFEOS_GD_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: GDmm2ll_cea | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| projcode int32 GCTP projection code | -| zonecode int32 UTM zone code | -| projparm float64 Projection parameters | -| spherecode int32 GCTP spheriod code | -| xdimsize int32 xdimsize from GDcreate | -| ydimsize int32 ydimsize from GDcreate | -| upleftpt float64 upper left corner coordinates (DMS) | -| lowrightpt float64 lower right corner coordinates (DMS) | -| x float64 X value array | -| y float64 Y value array | -| npnts int32 number of x-y points | -| | -| OUTPUTS: | -| longitude float64 longitude array (DMS) | -| latitude float64 latitude array (DMS) | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Oct 02 Abe Taaheri Added support for EASE grid | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn GDmm2ll_cea(int32 projcode,int32 zonecode, int32 spherecode, - float64 projparm[], - CPL_UNUSED int32 xdimsize, CPL_UNUSED int32 ydimsize, - CPL_UNUSED float64 upleftpt[], CPL_UNUSED float64 lowrightpt[], int32 npnts, - float64 x[], float64 y[], - float64 longitude[], float64 latitude[]) -{ - intn status = 0; /* routine return status variable */ - int32 errorcode = 0; /* GCTP error code */ - int32(*inv_trans[100]) (double, double, double*, double*); /* GCTP function pointer */ - int32 i; - - if(npnts <= 0) - { - HEpush(DFE_GENAPP, " GDmm2ll_cea", __FILE__, __LINE__); - HEreport("Improper npnts value\"%d\"... \n", npnts); - HEreport("npnts must be greater than zero.\n"); - status = -1; - return (status); - } - if ( projcode == GCTP_BCEA) - { - inv_init(projcode, zonecode, projparm, spherecode, NULL, NULL, - &errorcode, inv_trans); - - /* Convert from meters(or whatever unit is, i.e unit - of r_major and r_minor) to lat/lon using GCTP */ - /* ----------------------------------------- */ - for(i=0; i VGNAMELENMAX) - { - status = -1; - HEpush(DFE_GENAPP, "SWcreate", __FILE__, __LINE__); - HEreport("Swathname \"%s\" must be less than %d characters.\n", - swathname, VGNAMELENMAX); - } - - - if (status == 0) - { - - /* Determine number of swaths currently opened */ - /* ------------------------------------------- */ - for (i = 0; i < NSWATH; i++) - { - nswathopen += SWXSwath[i].active; - } - - - /* Setup file interface */ - /* -------------------- */ - if (nswathopen < NSWATH) - { - - /* Check that swath has not been previously opened */ - /* ----------------------------------------------- */ - vgRef = -1; - - while (1) - { - vgRef = Vgetid(HDFfid, vgRef); - - /* If no more Vgroups then exist while loop */ - /* ---------------------------------------- */ - if (vgRef == -1) - { - break; - } - - /* Get name and class of Vgroup */ - /* ---------------------------- */ - vgid[0] = Vattach(HDFfid, vgRef, "r"); - Vgetname(vgid[0], name); - Vgetclass(vgid[0], class); - Vdetach(vgid[0]); - - /* If SWATH then increment # swath counter */ - /* --------------------------------------- */ - if (strcmp(class, "SWATH") == 0) - { - nSwath++; - } - - /* If swath already exist, return error */ - /* ------------------------------------ */ - if (strcmp(name, swathname) == 0 && - strcmp(class, "SWATH") == 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWcreate", __FILE__, __LINE__); - HEreport("\"%s\" already exists.\n", swathname); - break; - } - } - - - if (status == 0) - { - - /* Create Root Vgroup for Swath */ - /* ---------------------------- */ - vgid[0] = Vattach(HDFfid, -1, "w"); - - - /* Set Name and Class (SWATH) */ - /* -------------------------- */ - Vsetname(vgid[0], swathname); - Vsetclass(vgid[0], "SWATH"); - - - - /* Create Geolocation Fields Vgroup */ - /* -------------------------------- */ - vgid[1] = Vattach(HDFfid, -1, "w"); - Vsetname(vgid[1], "Geolocation Fields"); - Vsetclass(vgid[1], "SWATH Vgroup"); - Vinsert(vgid[0], vgid[1]); - - - - /* Create Data Fields Vgroup */ - /* ------------------------- */ - vgid[2] = Vattach(HDFfid, -1, "w"); - Vsetname(vgid[2], "Data Fields"); - Vsetclass(vgid[2], "SWATH Vgroup"); - Vinsert(vgid[0], vgid[2]); - - - - /* Create Attributes Vgroup */ - /* ------------------------ */ - vgid[3] = Vattach(HDFfid, -1, "w"); - Vsetname(vgid[3], "Swath Attributes"); - Vsetclass(vgid[3], "SWATH Vgroup"); - Vinsert(vgid[0], vgid[3]); - - - - /* Establish Swath in Structural MetaData Block */ - /* -------------------------------------------- */ - snprintf(utlbuf, sizeof(utlbuf), "%s%ld%s%s%s", - "\tGROUP=SWATH_", (long)nSwath + 1, - "\n\t\tSwathName=\"", swathname, "\"\n"); - - strcat(utlbuf, "\t\tGROUP=Dimension\n"); - strcat(utlbuf, "\t\tEND_GROUP=Dimension\n"); - strcat(utlbuf, "\t\tGROUP=DimensionMap\n"); - strcat(utlbuf, "\t\tEND_GROUP=DimensionMap\n"); - strcat(utlbuf, "\t\tGROUP=IndexDimensionMap\n"); - strcat(utlbuf, "\t\tEND_GROUP=IndexDimensionMap\n"); - strcat(utlbuf, "\t\tGROUP=GeoField\n"); - strcat(utlbuf, "\t\tEND_GROUP=GeoField\n"); - strcat(utlbuf, "\t\tGROUP=DataField\n"); - strcat(utlbuf, "\t\tEND_GROUP=DataField\n"); - strcat(utlbuf, "\t\tGROUP=MergedFields\n"); - strcat(utlbuf, "\t\tEND_GROUP=MergedFields\n"); - CPLsnprintf(utlbuf2, sizeof(utlbuf2), "%s%ld%s", - "\tEND_GROUP=SWATH_", (long)nSwath + 1, "\n"); - strcat(utlbuf, utlbuf2); - - - status = EHinsertmeta(sdInterfaceID, "", "s", 1001L, - utlbuf, NULL); - } - } - else - { - /* Too many files opened */ - /* --------------------- */ - status = -1; - strcpy(errbuf, - "No more than %d swaths may be open simultaneously"); - strcat(errbuf, " (%s)"); - HEpush(DFE_DENIED, "SWcreate", __FILE__, __LINE__); - HEreport(errbuf, NSWATH, swathname); - } - - - /* Assign swathID # & Load swath and SWXSwath table entries */ - /* -------------------------------------------------------- */ - if (status == 0) - { - - for (i = 0; i < NSWATH; i++) - { - if (SWXSwath[i].active == 0) - { - /* - * Set swathID, Set swath entry active, Store root Vgroup - * ID, Store sub Vgroup IDs, Store HDF-EOS file ID - */ - swathID = i + idOffset; - SWXSwath[i].active = 1; - SWXSwath[i].IDTable = vgid[0]; - SWXSwath[i].VIDTable[0] = vgid[1]; - SWXSwath[i].VIDTable[1] = vgid[2]; - SWXSwath[i].VIDTable[2] = vgid[3]; - SWXSwath[i].fid = fid; - status = 0; - break; - } - } - - } - } - return (swathID); -} -#endif - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -881,89 +613,6 @@ SWchkswid(int32 swathID, const char *routname, - - - -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWdefdim | -| | -| DESCRIPTION: Defines numerical value of dimension | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| dimname char Dimension name to define | -| dim int32 Dimension value | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Dec 96 Joel Gales Check that dim value >= 0 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWdefdim(int32 swathID, char *dimname, int32 dim) - -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file id */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - - char swathname[80] /* Swath name */ ; - - - /* Check for valid swath id */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWdefdim", &fid, &sdInterfaceID, &swVgrpID); - - - /* Make sure dimension >= 0 */ - /* ------------------------ */ - if (dim < 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefdim", __FILE__, __LINE__); - HEreport("Dimension value for \"%s\" less than zero: %d.\n", - dimname, dim); - } - - - /* Write Dimension to Structural MetaData */ - /* -------------------------------------- */ - if (status == 0) - { - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - int32 dimArray[1] = { dim }; - status = EHinsertmeta(sdInterfaceID, swathname, "s", 0L, - dimname, dimArray); - } - return (status); -} -#endif - - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | @@ -1848,15 +1497,12 @@ SWfieldinfo(int32 swathID, const char *fieldname, int32 * rank, int32 dims[], - - -#ifdef HDFEOS_SW_WRITE_SUPPORT /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWdefdimmap | +| FUNCTION: SWwrrdattr | | | -| DESCRIPTION: Defines mapping between geolocation and data dimensions | +| DESCRIPTION: | | | | | | Return Value Type Units Description | @@ -1865,13 +1511,14 @@ SWfieldinfo(int32 swathID, const char *fieldname, int32 * rank, int32 dims[], | | | INPUTS: | | swathID int32 swath structure ID | -| geodim char Geolocation dimension | -| datadim char Data dimension | -| offset int32 Mapping offset | -| increment int32 Mapping increment | +| attrname char attribute name | +| numbertype int32 attribute HDF numbertype | +| count int32 Number of attribute elements | +| wrcode char Read/Write Code "w/r" | +| datbuf void I/O buffer | | | | OUTPUTS: | -| None | +| datbuf | | | | NOTES: | | | @@ -1879,87 +1526,93 @@ SWfieldinfo(int32 swathID, const char *fieldname, int32 * rank, int32 dims[], | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | +| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -SWdefdimmap(int32 swathID, const char *geodim, const char *datadim, int32 offset, - int32 increment) +static intn +SWwrrdattr(int32 swathID, const char *attrname, int32 numbertype, int32 count, + const char *wrcode, VOIDP datbuf) { intn status; /* routine return status variable */ int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 size; /* Size of geo dim */ + int32 attrVgrpID; /* Swath attribute ID */ + int32 dum; /* dummy variable */ int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 dum; /* Dummy variable */ - int32 metadata[2];/* Offset & Increment (passed to metadata) */ - - char mapname[80];/* Mapping name (geodim/datadim) */ - char swathname[80]; /* Swath name */ - /* Check Swath ID */ - status = SWchkswid(swathID, "SWdefdimmap", &fid, &sdInterfaceID, &dum); + /* Check Swath id */ + status = SWchkswid(swathID, "SWwrrdattr", &fid, &dum, &dum); if (status == 0) { - - /* Search Dimension Vdata for dimension entries */ - /* -------------------------------------------- */ - size = SWdiminfo(swathID, geodim); - if (size == -1) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefdimmap", __FILE__, __LINE__); - HEreport("Geolocation dimension name: \"%s\" not found.\n", - geodim); - } - /* Data Dimension Search */ - /* --------------------- */ - if (status == 0) + /* Get attribute Vgroup ID and call EHattr to perform I/O */ + /* ------------------------------------------------------ */ + int sID = swathID % idOffset; + if (sID >= NSWATH) { - size = SWdiminfo(swathID, datadim); - if (size == -1) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefdimmap", __FILE__, __LINE__); - HEreport("Data dimension name: \"%s\" not found.\n", - datadim); - } + return -1; } + attrVgrpID = SWXSwath[sID].VIDTable[2]; + status = EHattr(fid, attrVgrpID, attrname, numbertype, count, + wrcode, datbuf); + } + return (status); +} - /* Write Dimension Map to Structural MetaData */ - /* ------------------------------------------ */ - if (status == 0) - { - snprintf(mapname, sizeof(mapname), "%s%s%s", geodim, "/", datadim); - metadata[0] = offset; - metadata[1] = increment; - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - status = EHinsertmeta(sdInterfaceID, swathname, "s", 1L, - mapname, metadata); +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: SWreadattr | +| | +| DESCRIPTION: Reads attribute from a swath. | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| status intn return status (0) SUCCEED, (-1) FAIL | +| | +| INPUTS: | +| swathID int32 swath structure ID | +| attrname char attribute name | +| | +| OUTPUTS: | +| datbuf void I/O buffer | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Jun 96 Joel Gales Original Programmer | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +intn +SWreadattr(int32 swathID, const char *attrname, VOIDP datbuf) +{ + intn status = 0; /* routine return status variable */ + int32 dum = 0; /* dummy variable */ + + /* Call SWwrrdattr routine to read attribute */ + /* ----------------------------------------- */ + status = SWwrrdattr(swathID, attrname, dum, dum, "r", datbuf); - } - } return (status); } + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWdefidxmap | +| FUNCTION: SWattrinfo | | | -| DESCRIPTION: Defines l_indexed (non-linear) mapping between geolocation | -| and data dimensions | +| DESCRIPTION: | | | | | | Return Value Type Units Description | @@ -1968,9 +1621,12 @@ SWdefdimmap(int32 swathID, const char *geodim, const char *datadim, int32 offset | | | INPUTS: | | swathID int32 swath structure ID | -| geodim char Geolocation dimension | -| datadim char Data dimension | -| l_index int32 Index mapping array | +| attrname char attribute name | +| | +| OUTPUTS: | +| numbertype int32 attribute HDF numbertype | +| count int32 Number of attribute elements | +| | | | | OUTPUTS: | | None | @@ -1981,135 +1637,65 @@ SWdefdimmap(int32 swathID, const char *geodim, const char *datadim, int32 offset | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | +| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | | | | END_PROLOG | -----------------------------------------------------------------------------*/ intn -SWdefidxmap(int32 swathID, const char *geodim, const char *datadim, int32 l_index[]) - +SWattrinfo(int32 swathID, const char *attrname, int32 * numbertype, int32 * count) { - intn status; /* routine return status variable */ + intn status = 0; /* routine return status variable */ int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ int32 attrVgrpID; /* Swath attribute ID */ - int32 vdataID; /* Mapping Index Vdata ID */ - int32 gsize; /* Size of geo dim */ - int32 dsize; /* Size of data dim */ + int32 dum; /* dummy variable */ int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 dum; /* Dummy variable */ - - uint8 *buf; /* Vdata field buffer */ - - char mapname[80];/* Mapping name (geodim/datadim) */ - char swathname[80]; /* Swath name */ - char utlbuf[256];/* Utility buffer */ + /* Check for valid swath ID */ + /* ------------------------ */ + status = SWchkswid(swathID, "SWattrinfo", &fid, &dum, &dum); - /* Check Swath ID */ - status = SWchkswid(swathID, "SWdefidxmap", &fid, &sdInterfaceID, &dum); if (status == 0) { - /* Search Dimension Vdata for dimension entries */ - /* -------------------------------------------- */ - - /* Geo Dimension Search */ - /* -------------------- */ - - gsize = SWdiminfo(swathID, geodim); - - if (gsize == -1) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefidxmap", __FILE__, __LINE__); - HEreport("Geolocation dimension name: \"%s\" not found.\n", - geodim); - } - /* Data Dimension Search */ - /* --------------------- */ - if (status == 0) + /* Get attribute Vgroup ID and call EHattrinfo */ + /* ------------------------------------------- */ + int sID = swathID % idOffset; + if (sID >= NSWATH) { - dsize = SWdiminfo(swathID, datadim); - if (dsize == -1) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefidxmap", __FILE__, __LINE__); - HEreport("Data dimension name: \"%s\" not found.\n", - datadim); - } + return -1; } - /* Define Index Vdata and Store Index Array */ - /* ---------------------------------------- */ - if (status == 0) - { - /* Get attribute Vgroup ID and allocate data buffer */ - /* ------------------------------------------------ */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - attrVgrpID = SWXSwath[sID].VIDTable[2]; - buf = (uint8 *) calloc(4 * gsize, 1); - if(buf == NULL) - { - HEpush(DFE_NOSPACE,"SWdefidxmap", __FILE__, __LINE__); - return(-1); - } - - /* Name: "INDXMAP:" + geodim + "/" + datadim */ - snprintf(utlbuf, sizeof(utlbuf), "%s%s%s%s", "INDXMAP:", geodim, "/", datadim); - - vdataID = VSattach(fid, -1, "w"); - VSsetname(vdataID, utlbuf); - - /* Attribute Class */ - VSsetclass(vdataID, "Attr0.0"); - - /* Fieldname is "Index" */ - VSfdefine(vdataID, "Index", DFNT_INT32, gsize); - VSsetfields(vdataID, "Index"); - memcpy(buf, l_index, 4 * gsize); - - /* Write to vdata and free data buffer */ - VSwrite(vdataID, buf, 1, FULL_INTERLACE); - free(buf); + attrVgrpID = SWXSwath[sID].VIDTable[2]; - /* Insert in Attribute Vgroup and detach Vdata */ - Vinsert(attrVgrpID, vdataID); - VSdetach(vdataID); + status = EHattrinfo(fid, attrVgrpID, attrname, numbertype, + count); + } + return (status); +} - /* Write to Structural Metadata */ - snprintf(mapname, sizeof(mapname), "%s%s%s", geodim, "/", datadim); - Vgetname(SWXSwath[sID].IDTable, swathname); - int32 dumArray[1] = { dum }; - status = EHinsertmeta(sdInterfaceID, swathname, "s", 2L, - mapname, dumArray); - } - } - return (status); -} /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWdefcomp | +| FUNCTION: SWinqattrs | | | -| DESCRIPTION: Defines compression type and parameters | +| DESCRIPTION: | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nattr int32 Number of attributes in swath struct | | | | INPUTS: | -| swathID int32 swath structure ID | -| compcode int32 compression code | -| compparm intn compression parameters | +| swath ID int32 swath structure ID | +| | +| OUTPUTS: | +| attrnames char Attribute names in swath struct | +| (Comma-separated list) | +| strbufsize int32 Attributes name list string length | | | | OUTPUTS: | | None | @@ -2119,84 +1705,71 @@ SWdefidxmap(int32 swathID, const char *geodim, const char *datadim, int32 l_inde | | | Date Programmer Description | | ====== ============ ================================================= | -| Sep 96 Joel Gales Original Programmer | +| Jun 96 Joel Gales Original Programmer | +| Oct 96 Joel Gales Initialize nattr | +| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -SWdefcomp(int32 swathID, int32 compcode, intn compparm[]) +int32 +SWinqattrs(int32 swathID, char *attrnames, int32 * strbufsize) { - intn status = 0; /* routine return status variable */ + intn status; /* routine return status variable */ - int32 fid; /* HDF-EOS file id */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ + int32 fid; /* HDF-EOS file ID */ + int32 attrVgrpID; /* Swath attribute ID */ + int32 dum; /* dummy variable */ + int32 nattr = 0; /* Number of attributes */ int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 sID; /* swathID - offset */ - - /* Check for valid swath id */ - status = SWchkswid(swathID, "SWdefcomp", &fid, &sdInterfaceID, &swVgrpID); + /* Check Swath id */ + status = SWchkswid(swathID, "SWinqattrs", &fid, &dum, &dum); if (status == 0) { - sID = swathID % idOffset; + /* Get attribute Vgroup ID and call EHattrcat */ + /* ------------------------------------------ */ + int sID = swathID % idOffset; if (sID >= NSWATH) { return -1; } + attrVgrpID = SWXSwath[sID].VIDTable[2]; - /* Set compression code in compression external array */ - SWXSwath[sID].compcode = compcode; - - switch (compcode) - { - /* Set NBIT compression parameters in compression external array */ - case HDFE_COMP_NBIT: - - SWXSwath[sID].compparm[0] = compparm[0]; - SWXSwath[sID].compparm[1] = compparm[1]; - SWXSwath[sID].compparm[2] = compparm[2]; - SWXSwath[sID].compparm[3] = compparm[3]; - - break; - - /* Set GZIP compression parameter in compression external array */ - case HDFE_COMP_DEFLATE: - - SWXSwath[sID].compparm[0] = compparm[0]; - - break; - - } + nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize); } - return (status); + return (nattr); } +#define REMQUOTE(x) do { \ + char* l_x = x; \ + const size_t l_x_len = strlen(l_x); \ + if (l_x_len >= 2 && l_x[0] == '"' && l_x[l_x_len - 1] == '"') {\ + memmove(l_x, l_x + 1, l_x_len - 2); \ + l_x[l_x_len - 2] = 0; \ + } \ + } while(0) + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWdefinefield | +| FUNCTION: SWinqdims | | | -| DESCRIPTION: Defines geolocation or data field within swath structure | +| DESCRIPTION: Returns dimension names and values defined in swath structure | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nDim int32 Number of defined dimensions | | | | INPUTS: | | swathID int32 swath structure ID | -| fieldtype char geo/data fieldtype | -| fieldname char fieldname | -| dimlist char Dimension list (comma-separated list) | -| numbertype int32 field type | -| merge int32 merge code | -| | | | | OUTPUTS: | -| None | +| dimnames char Dimension names (comma-separated) | +| dims int32 Dimension values | | | | NOTES: | | | @@ -2204,675 +1777,169 @@ SWdefcomp(int32 swathID, int32 compcode, intn compparm[]) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Check name for length | -| Sep 96 Joel Gales Make string array "dimbuf" dynamic | -| Oct 96 Joel Gales Make sure total length of "merged" Vdata < 64 | -| Jun 03 Abe Taaheri Supplied cast comp_coder_t in call to SDsetcompress | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nDim to -1 if status = -1 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -static intn -SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const char *dimlist, - int32 numbertype, int32 merge) +int32 +SWinqdims(int32 swathID, char *dimnames, int32 dims[]) { - intn i; /* Loop index */ intn status; /* routine return status variable */ - intn found; /* utility found flag */ - intn foundNT = 0;/* found number type flag */ - intn foundAllDim = 1; /* found all dimensions flag */ - intn first = 1; /* first entry flag */ - intn fac; /* Geo (-1), Data (+1) field factor */ - int32 cnt = 0; int32 fid; /* HDF-EOS file ID */ - int32 vdataID = 0; /* Vdata ID */ - int32 vgid; /* Geo/Data field Vgroup ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 sdid; /* SDS object ID */ - int32 dimid; /* SDS dimension ID */ - int32 recSize; /* Vdata record size */ int32 swVgrpID; /* Swath root Vgroup ID */ - int32 dims[8]; /* Dimension size array */ - int32 dimsize; /* Dimension size */ - int32 rank = 0; /* Field rank */ - int32 slen[32]; /* String length array */ + int32 size; /* Dimension size */ int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 compcode; /* Compression code */ - int32 sID; /* SwathID - offset */ - - uint8 *oneDbuf; /* Vdata record buffer */ - char *dimbuf; /* Dimension buffer */ - char *comma; /* Pointer to comma */ - char *dimcheck; /* Dimension check buffer */ - char utlbuf[512];/* Utility buffer */ - char utlbuf2[256]; /* Utility buffer 2 */ - char *ptr[32]; /* String pointer array */ - char swathname[80]; /* Swath name */ - char errbuf1[128]; /* Error message buffer 1 */ - char errbuf2[128]; /* Error message buffer 2 */ - char compparmbuf[128]; /* Compression parameter string buffer */ - - const char *HDFcomp[5] = {"HDFE_COMP_NONE", "HDFE_COMP_RLE", - "HDFE_COMP_NBIT", "HDFE_COMP_SKPHUFF", - "HDFE_COMP_DEFLATE"}; - /* Compression code names */ - - uint16 good_number[10] = {3, 4, 5, 6, 20, 21, 22, 23, 24, 25}; - /* Valid number types */ - comp_info c_info; /* Compression parameter structure */ - - - - /* Setup error message strings */ - /* --------------------------- */ - strcpy(errbuf1, "SWXSDname array too small.\nPlease increase "); - strcat(errbuf1, "size of HDFE_NAMBUFSIZE in \"HdfEosDef.h\".\n"); - strcpy(errbuf2, "SWXSDdims array too small.\nPlease increase "); - strcat(errbuf2, "size of HDFE_DIMBUFSIZE in \"HdfEosDef.h\".\n"); + int32 nDim = 0; /* Number of dimensions */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char swathname[80]; /* Swath Name */ + char *utlstr; /* Utility string */ - /* - * Check for proper swath ID and return HDF-EOS file ID, SDinterface ID, - * and swath root Vgroup ID - */ - status = SWchkswid(swathID, "SWdefinefield", - &fid, &sdInterfaceID, &swVgrpID); + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) + { + HEpush(DFE_NOSPACE,"SWinqdims", __FILE__, __LINE__); + return(-1); + } + /* Check for valid swath id */ + status = SWchkswid(swathID, "SWinqdims", &fid, &sdInterfaceID, &swVgrpID); if (status == 0) { - /* Remove offset from swath ID & get swathname */ - sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - Vgetname(swVgrpID, swathname); - - /* Allocate space for dimbuf, copy dimlist into it, & append comma */ - dimbuf = (char *) calloc(strlen(dimlist) + 64, 1); - if(dimbuf == NULL) + /* If dimension names or sizes are desired ... */ + /* ------------------------------------------- */ + if (dimnames != NULL || dims != NULL) { - HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); - return(-1); - } - strcpy(dimbuf, dimlist); - strcat(dimbuf, ","); - - /* Find comma */ - comma = strchr(dimbuf, ','); - + /* Get swath name */ + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); - /* - * Loop through entries in dimension list to make sure they are - * defined in swath - */ - while (comma != NULL) - { - /* Copy dimension list entry to dimcheck */ - dimcheck = (char *) calloc(comma - dimbuf + 1, 1); - if(dimcheck == NULL) + /* Get pointers to "Dimension" section within SM */ + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "Dimension", metaptrs); + if(metabuf == NULL) { - HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); - free(dimbuf); + free(utlstr); return(-1); } - memcpy(dimcheck, dimbuf, comma - dimbuf); - - /* Get dimension size */ - dimsize = SWdiminfo(swathID, dimcheck); - - /* if != -1 then sent found flag, store size and increment rank */ - if (dimsize != -1) + /* If dimension names are desired then "clear" name buffer */ + if (dimnames != NULL) { - dims[rank] = dimsize; - rank++; + dimnames[0] = 0; } - else + + + /* Begin loop through dimension entries in metadata */ + /* ------------------------------------------------ */ + while (1) { - /* - * If dimension list entry not found - set error return - * status, append name to utility buffer for error report - */ - status = -1; - foundAllDim = 0; - if (first == 1) - { - strcpy(utlbuf, dimcheck); - } - else - { - strcat(utlbuf, ","); - strcat(utlbuf, dimcheck); - } - first = 0; - } - - /* - * Go to next dimension entry, find next comma, & free up - * dimcheck buffer - */ - *comma = '\0'; /* zero out first comma */ - comma++; - comma = strchr(comma, ','); - if (comma != NULL) - { - for (i=0; i<(intn)strlen(dimcheck) + 1; i++) - { - dimbuf++; - cnt++; - } - } - free(dimcheck); - } - for(i=0; i MAX_NC_NAME - 7) -** this was changed because HDF4.1r3 made a change in the -** hlimits.h file. We have notified NCSA and asked to have -** it made the same as in previous versions of HDF -** see ncr 26314. DaW Apr 2000 -*/ - - if (((intn) strlen(fieldname) > VSNAMELENMAX && rank == 1) || - ((intn) strlen(fieldname) > (256 - 7) && rank > 1)) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" too long.\n", fieldname); - } - } - - /* Check for valid numbertype */ - /* -------------------------- */ - if (status == 0) - { - for (i = 0; i < 10; i++) - { - if (numbertype == good_number[i]) - { - foundNT = 1; - } - } - - if (foundNT == 0) - { - HEpush(DFE_BADNUMTYPE, "SWdefinefield", __FILE__, __LINE__); - HEreport("Invalid number type: %d (%s).\n", - numbertype, fieldname); - status = -1; - } - } - - - /* Define Field */ - /* ------------ */ - if (status == 0) - { - /* Set factor & get Field Vgroup id */ - /* -------------------------------- */ - if (strcmp(fieldtype, "Geolocation Fields") == 0) - { - fac = -1; - vgid = SWXSwath[sID].VIDTable[0]; - } - else - { - fac = +1; - vgid = SWXSwath[sID].VIDTable[1]; - } - /* - * Note: "fac" is used to distinguish geo fields from data fields - * so that they are not merged together - */ - - - /* One D Fields */ - /* ------------ */ - if (rank == 1) - { - /* No Compression for 1D (Vdata) fields */ - compcode = HDFE_COMP_NONE; - + /* Search for OBJECT string */ + metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - /* If field non-appendable and merge set to AUTOMERGE ... */ - if (dims[0] != 0 && merge == HDFE_AUTOMERGE) + /* If found within "Dimension" metadata section ... */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) { - i = 0; - found = 0; - - /* Loop through previous entries in 1d combination array */ - while (SWX1dcomb[3 * i] != 0) - { - /* Get name of previous 1d combined field */ - vdataID = SWX1dcomb[3 * i + 2]; - VSgetname(vdataID, utlbuf); - - /* - * If dimension, field type (geo/data), and swath - * structure if current entry match a previous entry - * and combined name is less than max allowed then - * set "found" flag and exit loop - */ - if (SWX1dcomb[3 * i] == fac * dims[0] && - SWX1dcomb[3 * i + 1] == swVgrpID && - (intn) strlen(utlbuf) + - (intn) strlen(fieldname) + 1 <= - VSNAMELENMAX) - { - found = 1; - break; - } - /* Increment loop index */ - i++; - } - - - if (found == 0) - { - /* - * If no matching entry found then start new Vdata - * and store dimension size, swath root Vgroup ID, - * field Vdata and fieldname in external array - * "SWX1dcomb" - */ - vdataID = VSattach(fid, -1, "w"); - SWX1dcomb[3 * i] = fac * dims[0]; - SWX1dcomb[3 * i + 1] = swVgrpID; - SWX1dcomb[3 * i + 2] = vdataID; - VSsetname(vdataID, fieldname); - } - else + /* Get Dimension Name (if desired) */ + if (dimnames != NULL) { - /* - * If match then concatenate current fieldname to - * previous matching fieldnames. - */ - strcat(utlbuf, ","); - strcat(utlbuf, fieldname); - VSsetname(vdataID, utlbuf); - } - - /* Define field as field within Vdata */ - VSfdefine(vdataID, fieldname, numbertype, 1); - Vinsert(vgid, vdataID); - - } - else - { - /* 1d No Merge Section */ - - /* Get new vdata ID and establish field within Vdata */ - vdataID = VSattach(fid, -1, "w"); - VSsetname(vdataID, fieldname); - VSfdefine(vdataID, fieldname, numbertype, 1); - VSsetfields(vdataID, fieldname); + /* Check 1st for old meta data then new */ + /* ------------------------------------ */ + EHgetmetavalue(metaptrs, "OBJECT", utlstr); - recSize = VSsizeof(vdataID, (char*) fieldname); - if (dims[0] == 0) - { - /* - * If appendable field then write single record - * filled with 255 - */ - oneDbuf = (uint8 *) calloc(recSize, 1); - if(oneDbuf == NULL) - { - HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); - return(-1); - } - for (i = 0; i < recSize; i++) - oneDbuf[i] = 255; - VSwrite(vdataID, oneDbuf, 1, FULL_INTERLACE); - } - else - { /* - * If non-appendable then write entire field with - * blank records + * If OBJECT value begins with double quote then old + * metadata, dimension name is OBJECT value. + * Otherwise search for "DimensionName" string */ - oneDbuf = (uint8 *) calloc(recSize, dims[0]); - if(oneDbuf == NULL) + if (utlstr[0] != '"') { - HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); - return(-1); + metaptrs[0] = + strstr(metaptrs[0], "\t\t\t\tDimensionName="); + EHgetmetavalue(metaptrs, "DimensionName", utlstr); } - VSwrite(vdataID, oneDbuf, dims[0], FULL_INTERLACE); - } - free(oneDbuf); - - /* Insert Vdata into field Vgroup & detach */ - Vinsert(vgid, vdataID); - VSdetach(vdataID); - - } /* End No Merge Section */ - - } /* End 1d field Section */ - else - { - /* SDS Interface (Multi-dim fields) */ - /* -------------------------------- */ - - /* Get current compression code */ - compcode = SWXSwath[sID].compcode; - - /* - * If rank is less than or equal to 3 (and greater than 1) - * and AUTOMERGE is set and the first dimension is not - * appendable and the compression code is set to none then - * ... - */ - if (rank <= 3 && merge == HDFE_AUTOMERGE && dims[0] != 0 - && compcode == HDFE_COMP_NONE) - { - /* Find first empty slot in external combination array */ - /* --------------------------------------------------- */ - i = 0; - while (SWXSDcomb[5 * i] != 0) - { - i++; - } - - /* - * Store dimensions (with geo/data factor), swath root - * Vgroup ID, and number type in external combination - * array "SWXSDcomb" - */ - - if (rank == 2) - { - /* If 2-dim field then set lowest dimension to +/- 1 */ - SWXSDcomb[5 * i] = fac; - SWXSDcomb[5 * i + 1] = fac * dims[0]; - SWXSDcomb[5 * i + 2] = fac * dims[1]; - } - else - { - SWXSDcomb[5 * i] = fac * dims[0]; - SWXSDcomb[5 * i + 1] = fac * dims[1]; - SWXSDcomb[5 * i + 2] = fac * dims[2]; - } - - SWXSDcomb[5 * i + 3] = swVgrpID; - SWXSDcomb[5 * i + 4] = numbertype; - - - /* Concatenate fieldname with combined name string */ - /* ----------------------------------------------- */ - if ((intn) strlen(SWXSDname) + - (intn) strlen(fieldname) + 2 < HDFE_NAMBUFSIZE) - { - strcat(SWXSDname, fieldname); - strcat(SWXSDname, ","); - } - else - { - /* SWXSDname array too small! */ - /* -------------------------- */ - HEpush(DFE_GENAPP, "SWdefinefield", - __FILE__, __LINE__); - HEreport(errbuf1); - status = -1; - return (status); - } - + /* Strip off double quotes */ + /* ----------------------- */ + REMQUOTE(utlstr); - /* - * If 2-dim field then set lowest dimension (in 3-dim - * array) to "ONE" - */ - if (rank == 2) - { - if ((intn) strlen(SWXSDdims) + 5 < HDFE_DIMBUFSIZE) - { - strcat(SWXSDdims, "ONE,"); - } - else + /* If not first name then add comma delimiter */ + if (nDim > 0) { - /* SWXSDdims array too small! */ - /* -------------------------- */ - HEpush(DFE_GENAPP, "SWdefinefield", - __FILE__, __LINE__); - HEreport(errbuf2); - status = -1; - return (status); + strcat(dimnames, ","); } - + /* Add dimension name to dimension list */ + strcat(dimnames, utlstr); } - /* - * Concatenate field dimlist to merged dimlist and - * separate fields with semi-colon. - */ - if ((intn) strlen(SWXSDdims) + - (intn) strlen(dimlist) + 2 < HDFE_DIMBUFSIZE) - { - strcat(SWXSDdims, dimlist); - strcat(SWXSDdims, ";"); - } - else + /* Get Dimension Size (if desired) */ + if (dims != NULL) { - /* SWXSDdims array too small! */ - /* -------------------------- */ - HEpush(DFE_GENAPP, "SWdefinefield", - __FILE__, __LINE__); - HEreport(errbuf2); - status = -1; - return (status); + EHgetmetavalue(metaptrs, "Size", utlstr); + size = atoi(utlstr); + dims[nDim] = size; } - - } /* End Multi-Dim Merge Section */ + /* Increment number of dimensions */ + nDim++; + } else + /* No more dimensions found */ { - /* Multi-Dim No Merge Section */ - /* ========================== */ - - /* Create SDS dataset */ - /* ------------------ */ - sdid = SDcreate(sdInterfaceID, fieldname, - numbertype, rank, dims); - - - /* Store Dimension Names in SDS */ - /* ---------------------------- */ - rank = EHparsestr(dimlist, ',', ptr, slen); - for (i = 0; i < rank; i++) - { - /* Dimension name = Swathname:Dimname */ - memcpy(utlbuf, ptr[i], slen[i]); - utlbuf[slen[i]] = 0; - strcat(utlbuf, ":"); - strcat(utlbuf, swathname); - - dimid = SDgetdimid(sdid, i); - SDsetdimname(dimid, utlbuf); - } - - - /* Setup compression parameters */ - if (compcode == HDFE_COMP_NBIT) - { - c_info.nbit.nt = numbertype; - c_info.nbit.sign_ext = SWXSwath[sID].compparm[0]; - c_info.nbit.fill_one = SWXSwath[sID].compparm[1]; - c_info.nbit.start_bit = SWXSwath[sID].compparm[2]; - c_info.nbit.bit_len = SWXSwath[sID].compparm[3]; - } - else if (compcode == HDFE_COMP_SKPHUFF) - { - c_info.skphuff.skp_size = (intn) DFKNTsize(numbertype); - } - else if (compcode == HDFE_COMP_DEFLATE) - { - c_info.deflate.level = SWXSwath[sID].compparm[0]; - } - - /* If field is compressed then call SDsetcompress */ - /* ---------------------------------------------- */ - if (compcode != HDFE_COMP_NONE) - { - /* status = */ SDsetcompress(sdid, (comp_coder_t) compcode, &c_info); - } - - - /* Attach to Vgroup */ - /* ---------------- */ - Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid)); - - - /* Store SDS dataset IDs */ - /* --------------------- */ - if (SWXSwath[sID].nSDS > 0) - { - SWXSwath[sID].sdsID = (int32 *) - realloc((void *) SWXSwath[sID].sdsID, - (SWXSwath[sID].nSDS + 1) * 4); - if(SWXSwath[sID].sdsID == NULL) - { - HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); - return(-1); - } - - } - else - { - SWXSwath[sID].sdsID = (int32 *) calloc(1, 4); - if(SWXSwath[sID].sdsID == NULL) - { - HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__); - return(-1); - } - } - SWXSwath[sID].sdsID[SWXSwath[sID].nSDS] = sdid; - SWXSwath[sID].nSDS++; - - } /* End Multi-Dim No Merge Section */ - - } /* End Multi-Dim Section */ - - - - /* Setup metadata string */ - /* --------------------- */ - snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist); - - - /* Setup compression metadata */ - /* -------------------------- */ - if (compcode != HDFE_COMP_NONE) - { - snprintf(utlbuf2, sizeof(utlbuf2), - "%s%s", - ":\n\t\t\t\tCompressionType=", HDFcomp[compcode]); - - switch (compcode) - { - case HDFE_COMP_NBIT: - - snprintf(compparmbuf, sizeof(compparmbuf), - "%s%d,%d,%d,%d%s", - "\n\t\t\t\tCompressionParams=(", - SWXSwath[sID].compparm[0], - SWXSwath[sID].compparm[1], - SWXSwath[sID].compparm[2], - SWXSwath[sID].compparm[3], ")"); - strcat(utlbuf2, compparmbuf); - break; - - - case HDFE_COMP_DEFLATE: - - snprintf(compparmbuf, sizeof(compparmbuf), - "%s%d", - "\n\t\t\t\tDeflateLevel=", - SWXSwath[sID].compparm[0]); - strcat(utlbuf2, compparmbuf); break; } - - /* Concatenate compression parameters with compression code */ - strcat(utlbuf, utlbuf2); - } - - - /* Insert field metadata within File Structural Metadata */ - /* ----------------------------------------------------- */ - if (strcmp(fieldtype, "Geolocation Fields") == 0) - { - status = EHinsertmeta(sdInterfaceID, swathname, "s", 3L, - utlbuf, &numbertype); - } - else - { - status = EHinsertmeta(sdInterfaceID, swathname, "s", 4L, - utlbuf, &numbertype); } - + free(metabuf); } } - /* If all dimensions not found then report error */ - /* --------------------------------------------- */ - if (foundAllDim == 0) + + /* Set nDim to -1 if error status exists */ + /* ------------------------------------- */ + if (status == -1) { - HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__); - HEreport("Dimension(s): \"%s\" not found (%s).\n", - utlbuf, fieldname); - status = -1; + nDim = -1; } + free(utlstr); - return (status); + return (nDim); } + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWdefgeofield | +| FUNCTION: SWinqmaps | | | -| DESCRIPTION: Defines geolocation field within swath structure (wrapper) | +| DESCRIPTION: Returns dimension mappings and offsets and increments | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nMap int32 Number of dimension mappings | | | | INPUTS: | | swathID int32 swath structure ID | -| fieldname char fieldname | -| dimlist char Dimension list (comma-separated list) | -| numbertype int32 field type | -| merge int32 merge code | | | | OUTPUTS: | -| None | +| dimmaps char dimension mappings (comma-separated) | +| offset int32 array of offsets | +| increment int32 array of increments | | | | NOTES: | | | @@ -2880,168 +1947,172 @@ SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nMap to -1 if status = -1 | +| | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -SWdefgeofield(int32 swathID, const char *fieldname, const char *dimlist, - int32 numbertype, int32 merge) +int32 +SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[]) + { intn status; /* routine return status variable */ - /* Call SWdefinefield routine */ - /* -------------------------- */ - status = SWdefinefield(swathID, "Geolocation Fields", fieldname, dimlist, - numbertype, merge); + int32 fid; /* HDF-EOS file ID */ + int32 sdInterfaceID; /* HDF SDS interface ID */ + int32 swVgrpID; /* Swath root Vgroup ID */ + int32 off; /* Mapping Offset */ + int32 incr; /* Mapping Increment */ + int32 idOffset = SWIDOFFSET; /* Swath ID offset */ + int32 nMap = 0; /* Number of mappings */ - return (status); -} + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char swathname[80]; /* Swath Name */ + char *utlstr; /* Utility string */ + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) + { + HEpush(DFE_NOSPACE,"SWinqmaps", __FILE__, __LINE__); + return(-1); + } + /* Check for valid swath id */ + status = SWchkswid(swathID, "SWinqmaps", &fid, &sdInterfaceID, &swVgrpID); + if (status == 0) + { + /* If mapping names or offsets or increments desired ... */ + /* ----------------------------------------------------- */ + if (dimmaps != NULL || offset != NULL || increment != NULL) + { -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWdefdatafield | -| | -| DESCRIPTION: Defines data field within swath structure (wrapper) | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| fieldname char fieldname | -| dimlist char Dimension list (comma-separated list) | -| numbertype int32 field type | -| merge int32 merge code | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWdefdatafield(int32 swathID, const char *fieldname, const char *dimlist, - int32 numbertype, int32 merge) -{ - intn status; /* routine return status variable */ + /* Get swath name */ + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); - /* Call SWdefinefield routine */ - /* -------------------------- */ - status = SWdefinefield(swathID, "Data Fields", fieldname, dimlist, - numbertype, merge); + /* Get pointers to "DimensionMap" section within SM */ + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "DimensionMap", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } + /* If mapping names are desired then "clear" name buffer */ + if (dimmaps != NULL) + { + dimmaps[0] = 0; + } - return (status); -} + /* Begin loop through mapping entries in metadata */ + /* ---------------------------------------------- */ + while (1) + { + /* Search for OBJECT string */ + metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); + /* If found within "DimensionMap" metadata section ... */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) + { + /* Get Geo & Data Dimensions (if desired) */ + if (dimmaps != NULL) + { + /* Get Geo Dim, remove quotes, add "/" */ + EHgetmetavalue(metaptrs, "GeoDimension", utlstr); + REMQUOTE(utlstr); + strcat(utlstr, "/"); + /* If not first map then add comma delimiter. */ + if (nMap > 0) + { + strcat(dimmaps, ","); + } + /* Add to map list */ + strcat(dimmaps, utlstr); + /* Get Data Dim, remove quotes */ + EHgetmetavalue(metaptrs, "DataDimension", utlstr); + REMQUOTE(utlstr); -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWwritegeometa | -| | -| DESCRIPTION: Defines structural metadata for pre-existing geolocation | -| field within swath structure | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| fieldname char fieldname | -| dimlist char Dimension list (comma-separated list) | -| numbertype int32 field type | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWwritegeometa(int32 swathID, const char *fieldname, const char *dimlist, - int32 numbertype) -{ - intn status = 0; /* routine return status variable */ + /* Add to map list */ + strcat(dimmaps, utlstr); + } - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 dum; /* dummy variable */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ + /* Get Offset (if desired) */ + if (offset != NULL) + { + EHgetmetavalue(metaptrs, "Offset", utlstr); + off = atoi(utlstr); + offset[nMap] = off; + } - char utlbuf[256];/* Utility buffer */ - char swathname[80]; /* Swath name */ + /* Get Increment (if desired) */ + if (increment != NULL) + { + EHgetmetavalue(metaptrs, "Increment", utlstr); + incr = atoi(utlstr); + increment[nMap] = incr; + } + + /* Increment number of maps */ + nMap++; + } + else + /* No more mappings found */ + { + break; + } + } + free(metabuf); + } + } - status = SWchkswid(swathID, "SWwritegeometa", &dum, &sdInterfaceID, - &dum); - if (status == 0) + /* Set nMap to -1 if error status exists */ + /* ------------------------------------- */ + if (status == -1) { - /* Setup and write field metadata */ - /* ------------------------------ */ - snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist); - - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - int32 numbertypeArray[1] = { numbertype }; - status = EHinsertmeta(sdInterfaceID, swathname, "s", 3L, - utlbuf, numbertypeArray); + nMap = -1; } + free(utlstr); - return (status); + return (nMap); } + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWwritedatameta | +| FUNCTION: SWinqidxmaps | | | -| DESCRIPTION: Defines structural metadata for pre-existing data | -| field within swath structure | +| DESCRIPTION: Returns l_indexed mappings and l_index sizes | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nMap int32 Number of l_indexed dimension mappings | | | | INPUTS: | | swathID int32 swath structure ID | -| fieldname char fieldname | -| dimlist char Dimension list (comma-separated list) | -| numbertype int32 field type | -| | | | | OUTPUTS: | -| None | +| idxmaps char l_indexed dimension mappings | +| (comma-separated) | +| idxsizes int32 Number of elements in each mapping | +| | | | | NOTES: | | | @@ -3049,3921 +2120,166 @@ SWwritegeometa(int32 swathID, const char *fieldname, const char *dimlist, | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nMap to -1 if status = -1 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -SWwritedatameta(int32 swathID, const char *fieldname, const char *dimlist, - int32 numbertype) +int32 +SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[]) + { - intn status = 0; /* routine return status variable */ + intn status; /* routine return status variable */ + int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 dum; /* dummy variable */ + int32 swVgrpID; /* Swath root Vgroup ID */ int32 idOffset = SWIDOFFSET; /* Swath ID offset */ + int32 nMap = 0; /* Number of mappings */ - char utlbuf[256];/* Utility buffer */ - char swathname[80]; /* Swath name */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char swathname[80]; /* Swath Name */ + char *utlstr; /* Utility string */ + char *slash; /* Pointer to slash */ - status = SWchkswid(swathID, "SWwritedatameta", &dum, &sdInterfaceID, - &dum); - if (status == 0) + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) { - /* Setup and write field metadata */ - /* ------------------------------ */ - snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist); - - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - int32 numbertypeArray[1] = { numbertype }; - status = EHinsertmeta(sdInterfaceID, swathname, "s", 4L, - utlbuf, numbertypeArray); + HEpush(DFE_NOSPACE,"SWinqidxmaps", __FILE__, __LINE__); + return(-1); } - return (status); -} -#endif - + /* Check for valid swath id */ + status = SWchkswid(swathID, "SWinqidxmaps", &fid, + &sdInterfaceID, &swVgrpID); + if (status == 0) + { + /* If mapping names or l_index sizes desired ... */ + /* ------------------------------------------- */ + if (idxmaps != NULL || idxsizes != NULL) + { + /* Get swath name */ + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWwrrdattr | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| attrname char attribute name | -| numbertype int32 attribute HDF numbertype | -| count int32 Number of attribute elements | -| wrcode char Read/Write Code "w/r" | -| datbuf void I/O buffer | -| | -| OUTPUTS: | -| datbuf | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -SWwrrdattr(int32 swathID, const char *attrname, int32 numbertype, int32 count, - const char *wrcode, VOIDP datbuf) - -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 attrVgrpID; /* Swath attribute ID */ - int32 dum; /* dummy variable */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - - /* Check Swath id */ - status = SWchkswid(swathID, "SWwrrdattr", &fid, &dum, &dum); - - if (status == 0) - { - /* Get attribute Vgroup ID and call EHattr to perform I/O */ - /* ------------------------------------------------------ */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - attrVgrpID = SWXSwath[sID].VIDTable[2]; - status = EHattr(fid, attrVgrpID, attrname, numbertype, count, - wrcode, datbuf); - } - return (status); -} - - -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWwriteattr | -| | -| DESCRIPTION: Writes/updates attribute in a swath. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| attrname char attribute name | -| numbertype int32 attribute HDF numbertype | -| count int32 Number of attribute elements | -| datbuf void I/O buffer | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWwriteattr(int32 swathID, const char *attrname, int32 numbertype, int32 count, - VOIDP datbuf) -{ - intn status = 0; /* routine return status variable */ - - /* Call SWwrrdattr routine to write attribute */ - /* ------------------------------------------ */ - status = SWwrrdattr(swathID, attrname, numbertype, count, "w", datbuf); - - return (status); -} -#endif - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWreadattr | -| | -| DESCRIPTION: Reads attribute from a swath. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| attrname char attribute name | -| | -| OUTPUTS: | -| datbuf void I/O buffer | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWreadattr(int32 swathID, const char *attrname, VOIDP datbuf) -{ - intn status = 0; /* routine return status variable */ - int32 dum = 0; /* dummy variable */ - - /* Call SWwrrdattr routine to read attribute */ - /* ----------------------------------------- */ - status = SWwrrdattr(swathID, attrname, dum, dum, "r", datbuf); - - return (status); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWattrinfo | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| attrname char attribute name | -| | -| OUTPUTS: | -| numbertype int32 attribute HDF numbertype | -| count int32 Number of attribute elements | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWattrinfo(int32 swathID, const char *attrname, int32 * numbertype, int32 * count) -{ - intn status = 0; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 attrVgrpID; /* Swath attribute ID */ - int32 dum; /* dummy variable */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWattrinfo", &fid, &dum, &dum); - - if (status == 0) - { - /* Get attribute Vgroup ID and call EHattrinfo */ - /* ------------------------------------------- */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - attrVgrpID = SWXSwath[sID].VIDTable[2]; - - status = EHattrinfo(fid, attrVgrpID, attrname, numbertype, - count); - } - return (status); -} - - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqattrs | -| | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nattr int32 Number of attributes in swath struct | -| | -| INPUTS: | -| swath ID int32 swath structure ID | -| | -| OUTPUTS: | -| attrnames char Attribute names in swath struct | -| (Comma-separated list) | -| strbufsize int32 Attributes name list string length | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Initialize nattr | -| Oct 96 Joel Gales Get Attribute Vgroup ID from external array | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWinqattrs(int32 swathID, char *attrnames, int32 * strbufsize) -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 attrVgrpID; /* Swath attribute ID */ - int32 dum; /* dummy variable */ - int32 nattr = 0; /* Number of attributes */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - - /* Check Swath id */ - status = SWchkswid(swathID, "SWinqattrs", &fid, &dum, &dum); - - if (status == 0) - { - /* Get attribute Vgroup ID and call EHattrcat */ - /* ------------------------------------------ */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - attrVgrpID = SWXSwath[sID].VIDTable[2]; - - nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize); - } - - return (nattr); -} - -#define REMQUOTE(x) do { \ - char* l_x = x; \ - const size_t l_x_len = strlen(l_x); \ - if (l_x_len >= 2 && l_x[0] == '"' && l_x[l_x_len - 1] == '"') {\ - memmove(l_x, l_x + 1, l_x_len - 2); \ - l_x[l_x_len - 2] = 0; \ - } \ - } while(0) - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqdims | -| | -| DESCRIPTION: Returns dimension names and values defined in swath structure | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nDim int32 Number of defined dimensions | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| | -| OUTPUTS: | -| dimnames char Dimension names (comma-separated) | -| dims int32 Dimension values | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nDim to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWinqdims(int32 swathID, char *dimnames, int32 dims[]) - -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ - int32 size; /* Dimension size */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 nDim = 0; /* Number of dimensions */ - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char swathname[80]; /* Swath Name */ - char *utlstr; /* Utility string */ - - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"SWinqdims", __FILE__, __LINE__); - return(-1); - } - - /* Check for valid swath id */ - status = SWchkswid(swathID, "SWinqdims", &fid, &sdInterfaceID, &swVgrpID); - - if (status == 0) - { - /* If dimension names or sizes are desired ... */ - /* ------------------------------------------- */ - if (dimnames != NULL || dims != NULL) - { - /* Get swath name */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - free(utlstr); - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - - /* Get pointers to "Dimension" section within SM */ - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "Dimension", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - /* If dimension names are desired then "clear" name buffer */ - if (dimnames != NULL) - { - dimnames[0] = 0; - } - - - /* Begin loop through dimension entries in metadata */ - /* ------------------------------------------------ */ - while (1) - { - /* Search for OBJECT string */ - metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - - /* If found within "Dimension" metadata section ... */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - /* Get Dimension Name (if desired) */ - if (dimnames != NULL) - { - /* Check 1st for old meta data then new */ - /* ------------------------------------ */ - EHgetmetavalue(metaptrs, "OBJECT", utlstr); - - /* - * If OBJECT value begins with double quote then old - * metadata, dimension name is OBJECT value. - * Otherwise search for "DimensionName" string - */ - if (utlstr[0] != '"') - { - metaptrs[0] = - strstr(metaptrs[0], "\t\t\t\tDimensionName="); - EHgetmetavalue(metaptrs, "DimensionName", utlstr); - } - - /* Strip off double quotes */ - /* ----------------------- */ - REMQUOTE(utlstr); - - /* If not first name then add comma delimiter */ - if (nDim > 0) - { - strcat(dimnames, ","); - } - /* Add dimension name to dimension list */ - strcat(dimnames, utlstr); - } - - /* Get Dimension Size (if desired) */ - if (dims != NULL) - { - EHgetmetavalue(metaptrs, "Size", utlstr); - size = atoi(utlstr); - dims[nDim] = size; - } - /* Increment number of dimensions */ - nDim++; - } - else - /* No more dimensions found */ - { - break; - } - } - free(metabuf); - } - } - - - /* Set nDim to -1 if error status exists */ - /* ------------------------------------- */ - if (status == -1) - { - nDim = -1; - } - free(utlstr); - - return (nDim); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqmaps | -| | -| DESCRIPTION: Returns dimension mappings and offsets and increments | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nMap int32 Number of dimension mappings | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| | -| OUTPUTS: | -| dimmaps char dimension mappings (comma-separated) | -| offset int32 array of offsets | -| increment int32 array of increments | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nMap to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[]) - -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ - int32 off; /* Mapping Offset */ - int32 incr; /* Mapping Increment */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 nMap = 0; /* Number of mappings */ - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char swathname[80]; /* Swath Name */ - char *utlstr; /* Utility string */ - - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"SWinqmaps", __FILE__, __LINE__); - return(-1); - } - - /* Check for valid swath id */ - status = SWchkswid(swathID, "SWinqmaps", &fid, &sdInterfaceID, &swVgrpID); - if (status == 0) - { - /* If mapping names or offsets or increments desired ... */ - /* ----------------------------------------------------- */ - if (dimmaps != NULL || offset != NULL || increment != NULL) - { - - /* Get swath name */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - free(utlstr); - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - - /* Get pointers to "DimensionMap" section within SM */ - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "DimensionMap", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - /* If mapping names are desired then "clear" name buffer */ - if (dimmaps != NULL) - { - dimmaps[0] = 0; - } - - /* Begin loop through mapping entries in metadata */ - /* ---------------------------------------------- */ - while (1) - { - /* Search for OBJECT string */ - metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - - /* If found within "DimensionMap" metadata section ... */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - /* Get Geo & Data Dimensions (if desired) */ - if (dimmaps != NULL) - { - /* Get Geo Dim, remove quotes, add "/" */ - EHgetmetavalue(metaptrs, "GeoDimension", utlstr); - REMQUOTE(utlstr); - strcat(utlstr, "/"); - - /* If not first map then add comma delimiter. */ - if (nMap > 0) - { - strcat(dimmaps, ","); - } - - /* Add to map list */ - strcat(dimmaps, utlstr); - - /* Get Data Dim, remove quotes */ - EHgetmetavalue(metaptrs, "DataDimension", utlstr); - REMQUOTE(utlstr); - - /* Add to map list */ - strcat(dimmaps, utlstr); - } - - /* Get Offset (if desired) */ - if (offset != NULL) - { - EHgetmetavalue(metaptrs, "Offset", utlstr); - off = atoi(utlstr); - offset[nMap] = off; - } - - /* Get Increment (if desired) */ - if (increment != NULL) - { - EHgetmetavalue(metaptrs, "Increment", utlstr); - incr = atoi(utlstr); - increment[nMap] = incr; - } - - /* Increment number of maps */ - nMap++; - } - else - /* No more mappings found */ - { - break; - } - } - free(metabuf); - } - } - - - /* Set nMap to -1 if error status exists */ - /* ------------------------------------- */ - if (status == -1) - { - nMap = -1; - } - free(utlstr); - - return (nMap); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqidxmaps | -| | -| DESCRIPTION: Returns l_indexed mappings and l_index sizes | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nMap int32 Number of l_indexed dimension mappings | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| | -| OUTPUTS: | -| idxmaps char l_indexed dimension mappings | -| (comma-separated) | -| idxsizes int32 Number of elements in each mapping | -| | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nMap to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[]) - -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 nMap = 0; /* Number of mappings */ - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char swathname[80]; /* Swath Name */ - char *utlstr; /* Utility string */ - char *slash; /* Pointer to slash */ - - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"SWinqidxmaps", __FILE__, __LINE__); - return(-1); - } - /* Check for valid swath id */ - status = SWchkswid(swathID, "SWinqidxmaps", &fid, - &sdInterfaceID, &swVgrpID); - - if (status == 0) - { - /* If mapping names or l_index sizes desired ... */ - /* ------------------------------------------- */ - if (idxmaps != NULL || idxsizes != NULL) - { - /* Get swath name */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - free(utlstr); - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - - /* Get pointers to "IndexDimensionMap" section within SM */ - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "IndexDimensionMap", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - /* If mapping names are desired then "clear" name buffer */ - if (idxmaps != NULL) - { - idxmaps[0] = 0; - } - - /* Begin loop through mapping entries in metadata */ - /* ---------------------------------------------- */ - while (1) - { - /* Search for OBJECT string */ - metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - - /* If found within "IndexDimensionMap" metadata section ... */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - /* Get Geo & Data Dimensions and # of indices */ - if (idxmaps != NULL) - { - /* Get Geo Dim, remove quotes, add "/" */ - EHgetmetavalue(metaptrs, "GeoDimension", utlstr); - REMQUOTE(utlstr); - strcat(utlstr, "/"); - - /* If not first map then add comma delimiter. */ - if (nMap > 0) - { - strcat(idxmaps, ","); - } - - /* Add to map list */ - strcat(idxmaps, utlstr); - - - /* Get Index size (if desired) */ - if (idxsizes != NULL) - { - /* Parse off geo dimension and find its size */ - slash = strchr(utlstr, '/'); - if (slash) *slash = 0; - idxsizes[nMap] = SWdiminfo(swathID, utlstr); - } - - - /* Get Data Dim, remove quotes */ - EHgetmetavalue(metaptrs, "DataDimension", utlstr); - REMQUOTE(utlstr); - - /* Add to map list */ - strcat(idxmaps, utlstr); - } - - /* Increment number of maps */ - nMap++; - } - else - /* No more mappings found */ - { - break; - } - } - free(metabuf); - } - } - - - /* Set nMap to -1 if error status exists */ - /* ------------------------------------- */ - if (status == -1) - { - nMap = -1; - } - free(utlstr); - - return (nMap); -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqfields | -| | -| DESCRIPTION: Returns fieldnames, ranks and numbertypes defined in swath. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nFld int32 Number of (geo/data) fields in swath | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| fieldtype char field type (geo or data) | -| | -| | -| OUTPUTS: | -| fieldlist char Field names (comma-separated) | -| rank int32 Array of ranks | -| numbertype int32 Array of HDF number types | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nFld to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static int32 -SWinqfields(int32 swathID, const char *fieldtype, char *fieldlist, int32 rank[], - int32 numbertype[]) - -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 nFld = 0; /* Number of mappings */ - int32 slen[8]; /* String length array */ - - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char swathname[80]; /* Swath Name */ - char *utlstr; /* Utility string */ - char *utlstr2; /* Utility string 2 */ - char *ptr[8]; /* String pointer array */ - - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__); - return(-1); - } - - utlstr2 = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr2 == NULL) - { - HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__); - free(utlstr); - return(-1); - } - - /* Check for valid swath id */ - status = SWchkswid(swathID, "SWinqfields", - &fid, &sdInterfaceID, &swVgrpID); - - if (status == 0) - { - /* If field names, ranks, or number types desired ... */ - /* --------------------------------------------------- */ - if (fieldlist != NULL || rank != NULL || numbertype != NULL) - { - /* Get swath name */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - free(utlstr); - free(utlstr2); - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - - /* Get pointers to "GeoField" or "DataField" section within SM */ - if (strcmp(fieldtype, "Geolocation Fields") == 0) - { - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "GeoField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - free(utlstr2); - return(-1); - } - strcpy(utlstr2, "GeoFieldName"); - } - else - { - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "DataField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - free(utlstr2); - return(-1); - } - strcpy(utlstr2, "DataFieldName"); - } - - - /* If field names are desired then "clear" name buffer */ - if (fieldlist != NULL) - { - fieldlist[0] = 0; - } - - - /* Begin loop through mapping entries in metadata */ - /* ---------------------------------------------- */ - while (1) - { - /* Search for OBJECT string */ - metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - - /* If found within "Geo" or "Data" Field metadata section .. */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - /* Get Fieldnames (if desired) */ - if (fieldlist != NULL) - { - /* Check 1st for old meta data then new */ - /* ------------------------------------ */ - EHgetmetavalue(metaptrs, "OBJECT", utlstr); - - /* - * If OBJECT value begins with double quote then old - * metadata, field name is OBJECT value. Otherwise - * search for "GeoFieldName" or "DataFieldName" - * string - */ - - if (utlstr[0] != '"') - { - strcpy(utlstr, "\t\t\t\t"); - strcat(utlstr, utlstr2); - strcat(utlstr, "="); - metaptrs[0] = strstr(metaptrs[0], utlstr); - EHgetmetavalue(metaptrs, utlstr2, utlstr); - } - - /* Strip off double quotes */ - /* ----------------------- */ - REMQUOTE(utlstr); - - - /* Add to fieldlist */ - /* ---------------- */ - if (nFld > 0) - { - strcat(fieldlist, ","); - } - strcat(fieldlist, utlstr); - - } - /* Get Numbertype */ - if (numbertype != NULL) - { - EHgetmetavalue(metaptrs, "DataType", utlstr); - numbertype[nFld] = EHnumstr(utlstr); - } - /* - * Get Rank (if desired) by counting # of dimensions in - * "DimList" string - */ - if (rank != NULL) - { - EHgetmetavalue(metaptrs, "DimList", utlstr); - rank[nFld] = EHparsestr(utlstr, ',', ptr, slen); - } - /* Increment number of fields */ - nFld++; - } - else - /* No more fields found */ - { - break; - } - } - free(metabuf); - } - } - - /* Set nFld to -1 if error status exists */ - /* ------------------------------------- */ - if (status == -1) - { - nFld = -1; - } - - free(utlstr); - free(utlstr2); - - return (nFld); -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqgeofields | -| | -| DESCRIPTION: Inquires about geo fields in swath | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nflds int32 Number of geo fields in swath | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| | -| OUTPUTS: | -| fieldlist char Field names (comma-separated) | -| rank int32 Array of ranks | -| numbertype int32 Array of HDF number types | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWinqgeofields(int32 swathID, char *fieldlist, int32 rank[], - int32 numbertype[]) -{ - - int32 nflds; /* Number of Geolocation fields */ - - /* Call "SWinqfields" routine */ - /* -------------------------- */ - nflds = SWinqfields(swathID, "Geolocation Fields", fieldlist, rank, - numbertype); - - return (nflds); - -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqdatafields | -| | -| DESCRIPTION: Inquires about data fields in swath | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nflds int32 Number of data fields in swath | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| | -| OUTPUTS: | -| fieldlist char Field names (comma-separated) | -| rank int32 Array of ranks | -| numbertype int32 Array of HDF number types | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWinqdatafields(int32 swathID, char *fieldlist, int32 rank[], - int32 numbertype[]) -{ - - int32 nflds; /* Number of Data fields */ - - /* Call "SWinqfields" routine */ - /* -------------------------- */ - nflds = SWinqfields(swathID, "Data Fields", fieldlist, rank, - numbertype); - - return (nflds); - -} - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWnentries | -| | -| DESCRIPTION: Returns number of entries and string buffer size | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nEntries int32 Number of entries | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| entrycode int32 Entry code | -| HDFE_NENTDIM (0) | -| HDFE_NENTMAP (1) | -| HDFE_NENTIMAP (2) | -| HDFE_NENTGFLD (3) | -| HDFE_NENTDFLD (4) | -| | -| | -| OUTPUTS: | -| strbufsize int32 Length of comma-separated list | -| (Does not include null-terminator | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| Feb 97 Joel Gales Set nEntries to -1 if status = -1 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWnentries(int32 swathID, int32 entrycode, int32 * strbufsize) - -{ - intn status; /* routine return status variable */ - intn i; /* Loop index */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 nEntries = 0; /* Number of entries */ - int32 metaflag; /* Old (0), New (1) metadata flag) */ - int32 nVal = 0; /* Number of strings to search for */ - - char *metabuf = NULL; /* Pointer to structural metadata (SM) */ - char *metaptrs[2]; /* Pointers to begin and end of SM section */ - char swathname[80]; /* Swath Name */ - char *utlstr; /* Utility string */ - char valName[2][32]; /* Strings to search for */ - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"SWnemtries", __FILE__, __LINE__); - return(-1); - } - /* Check for valid swath id */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWnentries", &fid, &sdInterfaceID, &swVgrpID); - - if (status == 0) - { - /* Get swath name */ - int sID = swathID % idOffset; - if (sID >= NSWATH) - { - free(utlstr); - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - - /* Zero out string buffer size */ - *strbufsize = 0; - - - /* - * Get pointer to relevant section within SM and Get names of - * metadata strings to inquire about - */ - switch (entrycode) - { - case HDFE_NENTDIM: - /* Dimensions */ - { - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "Dimension", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - nVal = 1; - strcpy(&valName[0][0], "DimensionName"); - } - break; - - case HDFE_NENTMAP: - /* Dimension Maps */ - { - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "DimensionMap", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - nVal = 2; - strcpy(&valName[0][0], "GeoDimension"); - strcpy(&valName[1][0], "DataDimension"); - } - break; - - case HDFE_NENTIMAP: - /* Indexed Dimension Maps */ - { - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "IndexDimensionMap", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - nVal = 2; - strcpy(&valName[0][0], "GeoDimension"); - strcpy(&valName[1][0], "DataDimension"); - } - break; - - case HDFE_NENTGFLD: - /* Geolocation Fields */ - { - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "GeoField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - nVal = 1; - strcpy(&valName[0][0], "GeoFieldName"); - } - break; - - case HDFE_NENTDFLD: - /* Data Fields */ - { - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "DataField", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - nVal = 1; - strcpy(&valName[0][0], "DataFieldName"); - } - break; - } - - - /* - * Check for presence of 'GROUP="' string If found then old metadata, - * search on OBJECT string - */ - if (metabuf) - { - metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0; - if (metaflag == 0) - { - nVal = 1; - strcpy(&valName[0][0], "\t\tOBJECT"); - } - - - /* Begin loop through entries in metadata */ - /* -------------------------------------- */ - while (1) - { - /* Search for first string */ - strcpy(utlstr, &valName[0][0]); - strcat(utlstr, "="); - metaptrs[0] = strstr(metaptrs[0], utlstr); - - /* If found within relevant metadata section ... */ - if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) - { - for (i = 0; i < nVal; i++) - { - /* - * Get all string values Don't count quotes - */ - EHgetmetavalue(metaptrs, &valName[i][0], utlstr); - *strbufsize += (int32)strlen(utlstr) - 2; - } - /* Increment number of entries */ - nEntries++; - - /* Go to end of OBJECT */ - metaptrs[0] = strstr(metaptrs[0], "END_OBJECT"); - } - else - /* No more entries found */ - { - break; - } - } - free(metabuf); - } - - - /* Count comma separators & slashes (if mappings) */ - /* ---------------------------------------------- */ - if (nEntries > 0) - { - *strbufsize += nEntries - 1; - *strbufsize += (nVal - 1) * nEntries; - } - } - - - /* Set nEntries to -1 if error status exists */ - /* ----------------------------------------- */ - if (status == -1) - nEntries = -1; - - free(utlstr); - - return (nEntries); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWinqswath | -| | -| DESCRIPTION: Returns number and names of swath structures in file | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nSwath int32 Number of swath structures in file | -| | -| INPUTS: | -| filename char HDF-EOS filename | -| | -| OUTPUTS: | -| swathlist char List of swath names (comma-separated) | -| strbufsize int32 Length of swathlist | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWinqswath(const char *filename, char *swathlist, int32 * strbufsize) -{ - int32 nSwath; /* Number of swath structures in file */ - - /* Call "EHinquire" routine */ - /* ------------------------ */ - nSwath = EHinquire(filename, "SWATH", swathlist, strbufsize); - - return (nSwath); -} - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SW1dfldsrch | -| | -| DESCRIPTION: Retrieves information about a 1D field | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| fid int32 HDF-EOS file ID | -| swathID int32 swath structure ID | -| fieldname const char field name | -| access const char Access code (w/r) | -| | -| | -| OUTPUTS: | -| vgidout int32 Field (geo/data) vgroup ID | -| vdataIDout int32 Field Vdata ID | -| fldtype int32 Field type | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -SW1dfldsrch(int32 fid, int32 swathID, const char *fieldname, const char *i_access, - int32 * vgidout, int32 * vdataIDout, int32 * fldtype) - -{ - intn status = 0; /* routine return status variable */ - - int32 sID; /* SwathID - offset */ - int32 vgid; /* Swath Geo or Data Vgroup ID */ - int32 vdataID; /* 1d field vdata */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - - - /* Compute "reduced" swath ID */ - /* -------------------------- */ - sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - - /* Get Geolocation Vgroup id and 1D field name Vdata id */ - /* ---------------------------------------------------- */ - vgid = SWXSwath[sID].VIDTable[0]; - vdataID = EHgetid(fid, vgid, fieldname, 1, i_access); - *fldtype = 0; - - - /* - * If name not found in Geolocation Vgroup then detach Geolocation Vgroup - * and search in Data Vgroup - */ - if (vdataID == -1) - { - vgid = SWXSwath[sID].VIDTable[1];; - vdataID = EHgetid(fid, vgid, fieldname, 1, i_access); - *fldtype = 1; - - /* If field also not found in Data Vgroup then set error status */ - /* ------------------------------------------------------------ */ - if (vdataID == -1) - { - status = -1; - vgid = -1; - vdataID = -1; - } - } - *vgidout = vgid; - *vdataIDout = vdataID; - - return (status); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWSDfldsrch | -| | -| DESCRIPTION: Retrieves information SDS field | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| sdInterfaceID int32 SD interface ID | -| fieldname const char field name | -| | -| | -| OUTPUTS: | -| sdid int32 SD element ID | -| rankSDS int32 Rank of SDS | -| rankFld int32 True rank of field (merging) | -| offset int32 Offset of field within merged field | -| dims int32 Dimensions of field | -| solo int32 Solo field flag | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Make metadata ODL compliant | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -SWSDfldsrch(int32 swathID, int32 sdInterfaceID, const char *fieldname, - int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset, - int32 dims[], int32 * solo) -{ - intn i; /* Loop index */ - intn status = -1;/* routine return status variable */ - - int32 sID; /* SwathID - offset */ - int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - int32 dum; /* Dummy variable */ - int32 dums[128]; /* Dummy array */ - int32 attrIndex; /* Attribute l_index */ - - char name[2048]; /* Merged-Field Names */ - char swathname[80]; /* Swath Name */ - char *utlstr; /* Utility string */ - char *metabuf; /* Pointer to structural metadata (SM) */ - char *metaptrs[2];/* Pointers to begin and end of SM section */ - char *oldmetaptr; /* Pointer within SM section */ - - - /* Allocate space for utility string */ - /* --------------------------------- */ - utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); - if(utlstr == NULL) - { - HEpush(DFE_NOSPACE,"SWSDfldsrch", __FILE__, __LINE__); - return(-1); - } - /* Set solo flag to 0 (no) */ - /* ----------------------- */ - *solo = 0; - - - /* Compute "reduced" swath ID */ - /* -------------------------- */ - sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - - /* Loop through all SDSs in swath */ - /* ------------------------------ */ - for (i = 0; i < SWXSwath[sID].nSDS; i++) - { - /* If active SDS ... */ - /* ----------------- */ - if (SWXSwath[sID].sdsID[i] != 0) - { - /* Get SDS ID, name, rankSDS, and dimensions */ - /* ----------------------------------------- */ - *sdid = SWXSwath[sID].sdsID[i]; - SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum); - *rankFld = *rankSDS; - - /* If merged field ... */ - /* ------------------- */ - if (strstr(name, "MRGFLD_") == &name[0]) - { - /* Get swath name */ - /* -------------- */ - Vgetname(SWXSwath[sID].IDTable, swathname); - - - /* Get pointers to "MergedFields" section within SM */ - /* ------------------------------------------------ */ - metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", - "MergedFields", metaptrs); - if(metabuf == NULL) - { - free(utlstr); - return(-1); - } - - /* Store metaptr in order to recover */ - /* --------------------------------- */ - oldmetaptr = metaptrs[0]; - - - /* Search for Merged field name */ - /* ---------------------------- */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "MergedFieldName=\"", - name, "\"\n"); - metaptrs[0] = strstr(metaptrs[0], utlstr); - - - /* If not found check for old metadata */ - /* ----------------------------------- */ - if (metaptrs[0] == NULL) - { - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "OBJECT=\"", name, "\"\n"); - metaptrs[0] = strstr(oldmetaptr, utlstr); - } - - - /* Get field list and strip off leading and trailing quotes */ - EHgetmetavalue(metaptrs, "FieldList", name); /* not return status --xhua */ - memmove(name, name + 1, strlen(name) - 2); - name[strlen(name) - 2] = 0; - - /* Search for desired field within merged field list */ - snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\""); - dum = EHstrwithin(utlstr, name, ','); - - free(metabuf); - } - else - { - /* If solo (unmerged) check if SDS name matches fieldname */ - /* ------------------------------------------------------ */ - dum = EHstrwithin(fieldname, name, ','); - if (dum != -1) - { - *solo = 1; - *offset = 0; - } - } - - - /* If field found ... */ - /* ------------------ */ - if (dum != -1) - { - status = 0; - - /* If merged field ... */ - /* ------------------- */ - if (*solo == 0) - { - /* Get "Field Offsets" SDS attribute l_index */ - /* --------------------------------------- */ - attrIndex = SDfindattr(*sdid, "Field Offsets"); - - /* - * If attribute exists then get offset of desired field - * within merged field - */ - if (attrIndex != -1) - { - SDreadattr(*sdid, attrIndex, (VOIDP) dums); - *offset = dums[dum]; - } - - - /* Get "Field Dims" SDS attribute l_index */ - /* ------------------------------------ */ - attrIndex = SDfindattr(*sdid, "Field Dims"); - - /* - * If attribute exists then get 0th dimension of desired - * field within merged field - */ - if (attrIndex != -1) - { - SDreadattr(*sdid, attrIndex, (VOIDP) dums); - dims[0] = dums[dum]; - - /* If this dimension = 1 then field is really 2 dim */ - /* ------------------------------------------------ */ - if (dums[dum] == 1) - { - *rankFld = 2; - } - } - } - - - /* Break out of SDS loop */ - /* --------------------- */ - break; - } /* End of found field section */ - } - else - { - /* First non-active SDS signifies no more, break out of SDS loop */ - /* ------------------------------------------------------------- */ - break; - } - } - - free(utlstr); - - return (status); -} - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWwrrdfield | -| | -| DESCRIPTION: Writes/Reads fields | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| fieldname const char fieldname | -| code const char Write/Read code (w/r) | -| start int32 start array | -| stride int32 stride array | -| edge int32 edge array | -| datbuf void data buffer for read | -| | -| | -| OUTPUTS: | -| datbuf void data buffer for write | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Feb 97 Joel Gales Stride = 1 HDF compression workaround | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -static intn -SWwrrdfield(int32 swathID, const char *fieldname, const char *code, - int32 start[], int32 stride[], int32 edge[], VOIDP datbuf) - -{ - intn i; /* Loop index */ - intn status = 0; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 vgid; /* Swath Geo or Data Vgroup ID */ - int32 sdid; /* SDS ID */ - int32 dum; /* Dummy variable */ - int32 rankSDS; /* Rank of SDS */ - int32 rankFld; /* Rank of field */ - - int32 vdataID; /* 1d field vdata */ - int32 recsize; /* Vdata record size */ - int32 fldsize; /* Field size */ - int32 nrec; /* Number of records in Vdata */ - - int32 offset[8]; /* I/O offset (start) */ - int32 incr[8]; /* I/O increment (stride) */ - int32 count[8]; /* I/O count (edge) */ - int32 dims[8]; /* Field/SDS dimensions */ - int32 mrgOffset; /* Merged field offset */ - int32 nflds; /* Number of fields in Vdata */ - int32 strideOne; /* Strides = 1 flag */ - - uint8 *buf; /* I/O (transfer) buffer */ - uint8 *fillbuf; /* Fill value buffer */ - - char attrName[80]; /* Name of fill value attribute */ - char *ptr[64]; /* String pointer array */ - char fieldlist[256]; /* Vdata field list */ - - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWwrrdfield", &fid, &sdInterfaceID, &dum); - - - if (status == 0) - { - - /* Check whether fieldname is in SDS (multi-dim field) */ - /* --------------------------------------------------- */ - status = SWSDfldsrch(swathID, sdInterfaceID, fieldname, &sdid, - &rankSDS, &rankFld, &mrgOffset, dims, &dum); - - /* Multi-Dimensional Field Section */ - /* ------------------------------- */ - if (status != -1) - { - /* Set I/O offset Section */ - /* ---------------------- */ - - /* - * If start == NULL (default) set I/O offset of 0th field to - * offset within merged field (if any) and the rest to 0 - */ - if (start == NULL) - { - for (i = 0; i < rankSDS; i++) - { - offset[i] = 0; - } - offset[0] = mrgOffset; - } - else - { - /* - * ... otherwise set I/O offset to user values, adjusting the - * 0th field with the merged field offset (if any) - */ - if (rankFld == rankSDS) - { - for (i = 0; i < rankSDS; i++) - { - offset[i] = start[i]; - } - offset[0] += mrgOffset; - } - else - { - /* - * If field really 2-dim merged in 3-dim field then set - * 0th field offset to merge offset and then next two to - * the user values - */ - for (i = 0; i < rankFld; i++) - { - offset[i + 1] = start[i]; - } - offset[0] = mrgOffset; - } - } - - - - /* Set I/O stride Section */ - /* ---------------------- */ - - /* - * If stride == NULL (default) set I/O stride to 1 - */ - if (stride == NULL) - { - for (i = 0; i < rankSDS; i++) - { - incr[i] = 1; - } - } - else - { - /* - * ... otherwise set I/O stride to user values - */ - if (rankFld == rankSDS) - { - for (i = 0; i < rankSDS; i++) - { - incr[i] = stride[i]; - } - } - else - { - /* - * If field really 2-dim merged in 3-dim field then set - * 0th field stride to 1 and then next two to the user - * values. - */ - for (i = 0; i < rankFld; i++) - { - incr[i + 1] = stride[i]; - } - incr[0] = 1; - } - } - - - - /* Set I/O count Section */ - /* --------------------- */ - - /* - * If edge == NULL (default) set I/O count to number of remaining - * entries (dims - start) / increment. Note that 0th field - * offset corrected for merged field offset (if any). - */ - if (edge == NULL) - { - for (i = 1; i < rankSDS; i++) - { - count[i] = (dims[i] - offset[i]) / incr[i]; - } - count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0]; - } - else - { - /* - * ... otherwise set I/O count to user values - */ - if (rankFld == rankSDS) - { - for (i = 0; i < rankSDS; i++) - { - count[i] = edge[i]; - } - } - else - { - /* - * If field really 2-dim merged in 3-dim field then set - * 0th field count to 1 and then next two to the user - * values. - */ - for (i = 0; i < rankFld; i++) - { - count[i + 1] = edge[i]; - } - count[0] = 1; - } - } - - /* Perform I/O with relevant HDF I/O routine */ - /* ----------------------------------------- */ - if (strcmp(code, "w") == 0) - { - /* Set strideOne to true (1) */ - /* ------------------------- */ - strideOne = 1; - - - /* If incr[i] != 1 set strideOne to false (0) */ - /* ------------------------------------------ */ - for (i = 0; i < rankSDS; i++) - { - if (incr[i] != 1) - { - strideOne = 0; - break; - } - } - - - /* - * If strideOne is true use NULL parameter for stride. This - * is a work-around to HDF compression problem - */ - if (strideOne == 1) - { - status = SDwritedata(sdid, offset, NULL, count, - (VOIDP) datbuf); - } - else - { - status = SDwritedata(sdid, offset, incr, count, - (VOIDP) datbuf); - } - } - else - { - status = SDreaddata(sdid, offset, incr, count, - (VOIDP) datbuf); - } - } /* End of Multi-Dimensional Field Section */ - else - { - - /* One-Dimensional Field Section */ - /* ----------------------------- */ - - /* Check fieldname within 1d field Vgroups */ - /* --------------------------------------- */ - status = SW1dfldsrch(fid, swathID, fieldname, code, - &vgid, &vdataID, &dum); - - if (status != -1) - { - - /* Get number of records */ - /* --------------------- */ - nrec = VSelts(vdataID); - - - /* Set offset, increment, & count */ - /* ------------------------------ */ - offset[0] = (start == NULL) ? 0 : start[0]; - incr[0] = (stride == NULL) ? 1 : stride[0]; - count[0] = (edge == NULL) - ? (nrec - offset[0]) / incr[0] - : edge[0]; - - - - /* Write Section */ - /* ------------- */ - if (strcmp(code, "w") == 0) - { - /* Get size of field and setup fill buffer */ - /* --------------------------------------- */ - fldsize = VSsizeof(vdataID, (char *)fieldname); - fillbuf = (uint8 *) calloc(fldsize, 1); - if(fillbuf == NULL) - { - HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); - return(-1); - } - - /* Get size of record in Vdata and setup I/O buffer */ - /* ------------------------------------------------ */ - VSQueryvsize(vdataID, &recsize); - buf = (uint8 *) calloc(recsize, count[0] * incr[0]); - if(buf == NULL) - { - HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); - free(fillbuf); - return(-1); - } - - - /* Get names and number of fields in each record */ - /* ---------------------------------------------- */ - VSgetfields(vdataID, fieldlist); - dum = EHstrwithin(fieldname, fieldlist, ','); - nflds = EHparsestr(fieldlist, ',', ptr, NULL); - - - /* Get Merged Field Offset (if any) */ - /* -------------------------------- */ - if (nflds > 1) - { - if (dum > 0) - { - *(ptr[dum] - 1) = 0; - mrgOffset = VSsizeof(vdataID, fieldlist); - *(ptr[dum] - 1) = ','; - } - else - { - mrgOffset = 0; - } - - /* Read records to recover previously written data */ - /* status = */ VSsetfields(vdataID, fieldlist); - /* status = */ VSseek(vdataID, offset[0]); - nrec = VSread(vdataID, buf, count[0] * incr[0], - FULL_INTERLACE); - } - else - { - mrgOffset = 0; - } - - - - /* Fill buffer with "Fill" value (if any) */ - /* -------------------------------------- */ - snprintf( attrName, sizeof(attrName), "_FV_%s", fieldname); - - status = SWreadattr(swathID, attrName, (char *) fillbuf); - if (status == 0) - { - for (i = 0; i < count[0] * incr[0]; i++) - { - memcpy(buf + i * recsize + mrgOffset, - fillbuf, fldsize); - } - } - - - /* Write new data into buffer */ - /* -------------------------- */ - if (incr[0] == 1 && nflds == 1) - { - memcpy(buf, datbuf, count[0] * recsize); - } - else - { - for (i = 0; i < count[0]; i++) - { - memcpy(buf + i * recsize * incr[0] + mrgOffset, - (uint8 *) datbuf + i * fldsize, fldsize); - } - } - - - /* If append read last record */ - /* -------------------------- */ - if (offset[0] == nrec) - { - /* abe added "status =" to next line 8/8/97 */ - status = VSseek(vdataID, offset[0] - 1); - VSread(vdataID, fillbuf, 1, FULL_INTERLACE); - } - else - { - status = VSseek(vdataID, offset[0]); - } - - - /* Write data into Vdata */ - /* --------------------- */ - nrec = VSwrite(vdataID, buf, count[0] * incr[0], - FULL_INTERLACE); - - free(fillbuf); - if (status > 0) - status = 0; - - } /* End Write Section */ - else - { - /* Read Section */ - /* ------------ */ - status = VSsetfields(vdataID, fieldname); - fldsize = VSsizeof(vdataID, (char *)fieldname); - buf = (uint8 *) calloc(fldsize, count[0] * incr[0]); - if(buf == NULL) - { - HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); - return(-1); - } - - (void) VSseek(vdataID, offset[0]); - (void) VSread(vdataID, buf, count[0] * incr[0], - FULL_INTERLACE); - - - /* Copy from input buffer to returned data buffer */ - /* ---------------------------------------------- */ - if (incr[0] == 1) - { - memcpy(datbuf, buf, count[0] * fldsize); - } - else - { - for (i = 0; i < count[0]; i++) - { - memcpy((uint8 *) datbuf + i * fldsize, - buf + i * fldsize * incr[0], fldsize); - } - } - - } /* End Read Section */ - - free(buf); - VSdetach(vdataID); - } - else - { - HEpush(DFE_GENAPP, "SWwrrdfield", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", fieldname); - } - } /* End One-D Field Section */ - - } - return (status); -} - - -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWwritefield | -| | -| DESCRIPTION: Writes data to field | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| fieldname char fieldname | -| start int32 start array | -| stride int32 stride array | -| edge int32 edge array | -| | -| | -| OUTPUTS: | -| data void data buffer for write | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWwritefield(int32 swathID, const char *fieldname, - int32 start[], int32 stride[], int32 edge[], VOIDP data) - -{ - intn status = 0; /* routine return status variable */ - - status = SWwrrdfield(swathID, fieldname, "w", start, stride, edge, - data); - return (status); -} -#endif - - - - - -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWreadfield | -| | -| DESCRIPTION: Reads data from field | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 swath structure ID | -| fieldname const char fieldname | -| start int32 start array | -| stride int32 stride array | -| edge int32 edge array | -| buffer void data buffer for read | -| | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWreadfield(int32 swathID, const char *fieldname, - int32 start[], int32 stride[], int32 edge[], VOIDP buffer) - -{ - intn status = 0; /* routine return status variable */ - - status = SWwrrdfield(swathID, fieldname, "r", start, stride, edge, - buffer); - return (status); -} - - - -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWdefboxregion | -| | -| DESCRIPTION: Finds swath cross tracks within area of interest and returns | -| region ID | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| regionID int32 Region ID | -| | -| INPUTS: | -| swathID int32 Swath structure ID | -| cornerlon float64 dec deg Longitude of opposite corners of box | -| cornerlat float64 dec deg Latitude of opposite corners of box | -| mode int32 Search mode | -| HDFE_MIDPOINT - Use midpoint of Xtrack | -| HDFE_ENDPOINT - Use endpoints of Xtrack | -| HDFE_ANYPOINT - Use all points of Xtrack| -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Add ability to handle regions crossing date line | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Jul 98 Abe Taaheri Fixed core dump in SWregioninfo associated with | -| SWXRegion[k]->nRegions exceeding MAXNREGIONS in | -| this function | -| Aug 99 Abe Taaheri Fixed the code so that all cross tracks or all | -| points on the along track that fall inside the box | -| are identified. At the same time added code to | -| function "updatescene" to reject cases where there | -| is single cross track in the box (for LANDSAT) | -| Jun 03 Abe Taaheri Added a few lines to report error and return -1 if | -| regionID exceeded NSWATHREGN | -| Mar 04 Abe Taaheri Added recognition for GeodeticLatitude | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[], - int32 mode) -{ - intn i; /* Loop index */ - intn j = 0; /* Loop index */ - intn k; /* Loop index */ - - intn status; /* routine return status variable */ - intn statLon; /* Status from SWfieldinfo for longitude */ - intn statLat; /* Status from SWfieldinfo for latitude */ - intn statCoLat = -1; /* Status from SWfieldinfo for - * Colatitude */ - intn statGeodeticLat = -1; /* Status from SWfieldinfo for - * GeodeticLatitude */ - - uint8 found = 0; /* Found flag */ - uint8 *flag; /* Pointer to track flag array */ - intn validReg = -1; /* -1 is invalid validReg */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - int32 rank; /* Rank of geolocation fields */ - int32 nt; /* Number type of geolocation fields */ - int32 dims[8]; /* Dimensions of geolocation fields */ - int32 nElem; /* Number of elements to read */ - int32 bndflag; /* +/-180 longitude boundary flag */ - int32 lonTest; /* Longitude test flag */ - int32 latTest; /* Latitude test flag */ - int32 start[2]; /* Start array (read) */ - int32 stride[2] = {1, 1}; /* Stride array (read) */ - int32 edge[2]; /* Edge array (read) */ - int32 regionID = -1; /* Region ID (return) */ - int32 anyStart[2];/* ANYPOINT start array (read) */ - int32 anyEdge[2]; /* ANYPOINT edge array (read) */ - - float32 temp32; /* Temporary float32 variable */ - - float64 lonTestVal; /* Longitude test value */ - float64 latTestVal; /* Latitude test value */ - float64 temp64; /* Temporary float64 variable */ - - char *lonArr; /* Longitude data array */ - char *latArr; /* Latitude data array */ - char dimlist[256]; /* Dimension list (geolocation - * fields) */ - char latName[17];/* Latitude field name */ - - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWdefboxregion", &fid, &sdInterfaceID, - &swVgrpID); - - - /* Inclusion mode must be between 0 and 2 */ - /* -------------------------------------- */ - if (mode < 0 || mode > 2) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__); - HEreport("Improper Inclusion Mode: %d.\n", mode); - } - - - if (status == 0) - { - /* Get "Longitude" field info */ - /* -------------------------- */ - statLon = SWfieldinfo(swathID, "Longitude", &rank, dims, &nt, dimlist); - if (statLon != 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__); - HEreport("\"Longitude\" field not found.\n"); - } - - /* Get "Latitude" field info */ - /* -------------------------- */ - statLat = SWfieldinfo(swathID, "Latitude", &rank, dims, &nt, dimlist); - if (statLat != 0) - { - /* If not found check for "Colatitude" field info */ - /* ---------------------------------------------- */ - statCoLat = SWfieldinfo(swathID, "Colatitude", &rank, dims, &nt, - dimlist); - if (statCoLat != 0) - { - /* Check again for Geodeticlatitude */ - statGeodeticLat = SWfieldinfo(swathID, - "GeodeticLatitude", &rank, - dims, &nt, dimlist); - if (statGeodeticLat != 0) - { - /* Neither "Latitude" nor "Colatitude" nor - "GeodeticLatitude" field found */ - /* ----------------------------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__); - HEreport( - "Neither \"Latitude\" nor \"Colatitude\" nor \"GeodeticLatitude\" fields found.\n"); - } - else - { - /* Latitude field is "GeodeticLatitude" */ - /* ------------------------------ */ - strcpy(latName, "GeodeticLatitude"); - } - } - else - { - /* Latitude field is "Colatitude" */ - /* ------------------------------ */ - strcpy(latName, "Colatitude"); - } - } - else - { - /* Latitude field is "Latitude" */ - /* ---------------------------- */ - strcpy(latName, "Latitude"); - } - - - if (status == 0) - { - /* Search along entire "Track" dimension from beginning to end */ - /* ----------------------------------------------------------- */ - start[0] = 0; - edge[0] = dims[0]; - - - /* If 1D geolocation fields then set mode to MIDPOINT */ - /* -------------------------------------------------- */ - if (rank == 1) - { - mode = HDFE_MIDPOINT; - } - - - switch (mode) - { - /* If MIDPOINT search single point in middle of "CrossTrack" */ - /* --------------------------------------------------------- */ - case HDFE_MIDPOINT: - - start[1] = dims[1] / 2; - edge[1] = 1; - - break; - - /* If ENDPOINT search 2 points at either end of "CrossTrack" */ - /* --------------------------------------------------------- */ - case HDFE_ENDPOINT: - - start[1] = 0; - stride[1] = dims[1] - 1; - edge[1] = 2; - - break; - - /* If ANYPOINT do initial MIDPOINT search */ - /* -------------------------------------- */ - case HDFE_ANYPOINT: - - start[1] = dims[1] / 2; - edge[1] = 1; - - break; - } - - - /* Compute number of elements */ - /* -------------------------- */ - nElem = edge[0] * edge[1]; - - - /* Allocate space for longitude and latitude (float64) */ - /* --------------------------------------------------- */ - lonArr = (char *) calloc(nElem, sizeof(float64)); - if(lonArr == NULL) - { - HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); - return(-1); - } - - latArr = (char *) calloc(nElem, sizeof(float64)); - if(latArr == NULL) - { - HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); - free(lonArr); - return(-1); - } - - - /* Allocate space for flag array (uint8) */ - /* ------------------------------------- */ - flag = (uint8 *) calloc(edge[0] + 1, 1); - if(flag == NULL) - { - HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); - free(lonArr); - free(latArr); - return(-1); - } - - - /* Read Longitude and Latitude fields */ - /* ---------------------------------- */ - status = SWreadfield(swathID, "Longitude", - start, stride, edge, lonArr); - status = SWreadfield(swathID, latName, - start, stride, edge, latArr); - - - - /* - * If geolocation fields are FLOAT32 then cast each entry as - * FLOAT64 - */ - if (nt == DFNT_FLOAT32) - { - for (i = nElem - 1; i >= 0; i--) - { - memcpy(&temp32, lonArr + 4 * i, 4); - temp64 = (float64) temp32; - memcpy(lonArr + 8 * i, &temp64, 8); - - memcpy(&temp32, latArr + 4 * i, 4); - temp64 = (float64) temp32; - memcpy(latArr + 8 * i, &temp64, 8); - } - } - - - /* Set boundary flag */ - /* ----------------- */ - - /* - * This variable is set to 1 if the region of interest crosses - * the +/- 180 longitude boundary - */ - bndflag = (cornerlon[0] < cornerlon[1]) ? 0 : 1; - - - - /* Main Search Loop */ - /* ---------------- */ - - /* For each track ... */ - /* ------------------ */ - - for (i = 0; i < edge[0]; i++) - { - /* For each value from Cross Track ... */ - /* ----------------------------------- */ - for (j = 0; j < edge[1]; j++) - { - /* Read in single lon & lat values from data buffers */ - /* ------------------------------------------------- */ - memcpy(&lonTestVal, &lonArr[8 * (i * edge[1] + j)], 8); - memcpy(&latTestVal, &latArr[8 * (i * edge[1] + j)], 8); - - - /* If longitude value > 180 convert to -180 to 180 range */ - /* ----------------------------------------------------- */ - if (lonTestVal > 180) - { - lonTestVal = lonTestVal - 360; - } - - /* If Colatitude value convert to latitude value */ - /* --------------------------------------------- */ - if (statCoLat == 0) - { - latTestVal = 90 - latTestVal; - } - - - /* Test if lat value is within range */ - /* --------------------------------- */ - latTest = (latTestVal >= cornerlat[0] && - latTestVal <= cornerlat[1]); - - - if (bndflag == 1) - { - /* - * If boundary flag set test whether longitude value - * is outside region and then flip - */ - lonTest = (lonTestVal >= cornerlon[1] && - lonTestVal <= cornerlon[0]); - lonTest = 1 - lonTest; - } - else - { - lonTest = (lonTestVal >= cornerlon[0] && - lonTestVal <= cornerlon[1]); - } - - - /* - * If both longitude and latitude are within region set - * flag on for this track - */ - if (lonTest + latTest == 2) - { - flag[i] = 1; - found = 1; - break; - } - } - } - - - - /* ANYPOINT search */ - /* --------------- */ - if (mode == HDFE_ANYPOINT && rank > 1) - { - free(lonArr); - free(latArr); - - /* Allocate space for an entire single cross track */ - /* ----------------------------------------------- */ - lonArr = (char *) calloc(dims[1], sizeof(float64)); - if(lonArr == NULL) - { - HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); - free(flag); - return(-1); - } - - latArr = (char *) calloc(dims[1], sizeof(float64)); - if(latArr == NULL) - { - HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); - free(lonArr); - free(flag); - return(-1); - } - - - /* Setup start and edge */ - /* -------------------- */ - anyStart[1] = 0; - anyEdge[0] = 1; - anyEdge[1] = dims[1]; - - - /* For each track starting from 0 */ - /* ------------------------------ */ - for (i = 0; i < edge[0]; i++) - { - - /* If cross track not in region (with MIDPOINT search ... */ - /* ------------------------------------------------------ */ - if (flag[i] == 0) - { - /* Setup track start */ - /* ----------------- */ - anyStart[0] = i; - - - /* Read in lon and lat values for cross track */ - /* ------------------------------------------ */ - status = SWreadfield(swathID, "Longitude", - anyStart, NULL, anyEdge, lonArr); - status = SWreadfield(swathID, latName, - anyStart, NULL, anyEdge, latArr); - - - - /* - * If geolocation fields are FLOAT32 then cast each - * entry as FLOAT64 - */ - if (nt == DFNT_FLOAT32) - { - for (j = dims[1] - 1; j >= 0; j--) - { - memcpy(&temp32, lonArr + 4 * j, 4); - temp64 = (float64) temp32; - memcpy(lonArr + 8 * j, &temp64, 8); - - memcpy(&temp32, latArr + 4 * j, 4); - temp64 = (float64) temp32; - memcpy(latArr + 8 * j, &temp64, 8); - } - } - - - /* For each value from Cross Track ... */ - /* ----------------------------------- */ - for (j = 0; j < dims[1]; j++) - { - /* Read in single lon & lat values from buffers */ - /* -------------------------------------------- */ - memcpy(&lonTestVal, &lonArr[8 * j], 8); - memcpy(&latTestVal, &latArr[8 * j], 8); - - - /* If lon value > 180 convert to -180 - 180 range */ - /* ---------------------------------------------- */ - if (lonTestVal > 180) - { - lonTestVal = lonTestVal - 360; - } - - /* If Colatitude value convert to latitude value */ - /* --------------------------------------------- */ - if (statCoLat == 0) - { - latTestVal = 90 - latTestVal; - } - - - /* Test if lat value is within range */ - /* --------------------------------- */ - latTest = (latTestVal >= cornerlat[0] && - latTestVal <= cornerlat[1]); - - - if (bndflag == 1) - { - /* - * If boundary flag set test whether - * longitude value is outside region and then - * flip - */ - lonTest = (lonTestVal >= cornerlon[1] && - lonTestVal <= cornerlon[0]); - lonTest = 1 - lonTest; - } - else - { - lonTest = (lonTestVal >= cornerlon[0] && - lonTestVal <= cornerlon[1]); - } - - - /* - * If both longitude and latitude are within - * region set flag on for this track - */ - if (lonTest + latTest == 2) - { - flag[i] = 1; - found = 1; - break; - } - } - } - } - } - - /* If within region setup Region Structure */ - /* --------------------------------------- */ - if (found == 1) - { - /* For all entries in SWXRegion array ... */ - /* -------------------------------------- */ - for (k = 0; k < NSWATHREGN; k++) - { - /* If empty region ... */ - /* ------------------- */ - if (SWXRegion[k] == 0) - { - /* Allocate space for region entry */ - /* ------------------------------- */ - SWXRegion[k] = (struct swathRegion *) - calloc(1, sizeof(struct swathRegion)); - if(SWXRegion[k] == NULL) - { - HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__); - free(lonArr); - free(latArr); - free(flag); - return(-1); - } - - /* Store file and swath ID */ - /* ----------------------- */ - SWXRegion[k]->fid = fid; - SWXRegion[k]->swathID = swathID; - - - /* Set Start & Stop Vertical arrays to -1 */ - /* -------------------------------------- */ - for (j = 0; j < 8; j++) - { - SWXRegion[k]->StartVertical[j] = -1; - SWXRegion[k]->StopVertical[j] = -1; - SWXRegion[k]->StartScan[j] = -1; - SWXRegion[k]->StopScan[j] = -1; - } - - - /* Set region ID */ - /* ------------- */ - regionID = k; - break; - } - } - if (k >= NSWATHREGN) - { - HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__); - HEreport("regionID exceeded NSWATHREGN.\n"); - free(lonArr); - free(latArr); - free(flag); - return (-1); - } - - /* Find start and stop of regions */ - /* ------------------------------ */ - - /* Subtract previous flag value from current one */ - /* --------------------------------------------- */ - - /* - * Transition points will have flag value (+1) start or - * (255 = (uint8) -1) stop of region - */ - for (i = edge[0]; i > 0; i--) - { - flag[i] -= flag[i - 1]; - } - - - for (i = 0; i <= edge[0]; i++) - { - /* Start of region */ - /* --------------- */ - if (flag[i] == 1) - { - /* Increment (multiple) region counter */ - /* ----------------------------------- */ - j = ++SWXRegion[k]->nRegions; - - /* if SWXRegion[k]->nRegions greater than MAXNREGIONS */ - /* free allocated memory and return FAIL */ - - if ((SWXRegion[k]->nRegions) > MAXNREGIONS) - { - HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__); - HEreport("SWXRegion[%d]->nRegions exceeds MAXNREGIONS= %d.\n", k, MAXNREGIONS); - free(lonArr); - free(latArr); - free(flag); - return(-1); - } - - SWXRegion[k]->StartRegion[j - 1] = i; - } - - /* End of region */ - /* ------------- */ - if (flag[i] == 255) - { - SWXRegion[k]->StopRegion[j - 1] = i - 1; - validReg = 0; - } - } - } - free(lonArr); - free(latArr); - free(flag); - } - } - if(validReg==0) - { - return (regionID); - } - else - { - return (-1); - } - -} -#endif - - - -#ifdef UNUSED_BY_GDAL -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWregionl_index | -| | -| DESCRIPTION: Finds swath cross tracks within area of interest and returns | -| region l_index and region ID | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| regionID int32 Region ID | -| | -| INPUTS: | -| swathID int32 Swath structure ID | -| cornerlon float64 dec deg Longitude of opposite corners of box | -| cornerlat float64 dec deg Latitude of opposite corners of box | -| mode int32 Search mode | -| HDFE_MIDPOINT - Use midpoint of Xtrack | -| HDFE_ENDPOINT - Use endpoints of Xtrack | -| HDFE_ANYPOINT - Use all points of Xtrack| -| | -| OUTPUTS: | -| geodim char geolocation track dimension | -| idxrange int32 indices of region for along track dim. | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Oct 96 Joel Gales Add ability to handle regions crossing date line | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Nov 97 Daw Add multiple vertical subsetting capability | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[], - int32 mode, char *geodim, int32 idxrange[]) -{ - intn i; /* Loop index */ - intn j; /* Loop index */ - intn k; /* Loop index */ - - intn l=0; /* Loop index */ - intn tmpVal = 0; /* temp value for start region Delyth Jones*/ - /*intn j1; */ /* Loop index */ - intn status; /* routine return status variable */ - intn mapstatus; /* status for type of mapping */ - intn statLon; /* Status from SWfieldinfo for longitude */ - intn statLat; /* Status from SWfieldinfo for latitude */ - intn statCoLat = -1; /* Status from SWfieldinfo for - * Colatitude */ - intn statGeodeticLat = -1; /* Status from SWfieldinfo for - * GeodeticLatitude */ - - uint8 found = 0; /* Found flag */ - uint8 *flag; /* Pointer to track flag array */ - intn validReg = -1; /* -1 is invalid validReg */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - int32 rank; /* Rank of geolocation fields */ - int32 nt; /* Number type of geolocation fields */ - int32 dims[8]; /* Dimensions of geolocation fields */ - int32 nElem; /* Number of elements to read */ - int32 bndflag; /* +/-180 longitude boundary flag */ - int32 lonTest; /* Longitude test flag */ - int32 latTest; /* Latitude test flag */ - int32 start[2]; /* Start array (read) */ - int32 stride[2] = {1, 1}; /* Stride array (read) */ - int32 edge[2]; /* Edge array (read) */ - int32 regionID = -1; /* Region ID (return) */ - int32 anyStart[2];/* ANYPOINT start array (read) */ - int32 anyEdge[2]; /* ANYPOINT edge array (read) */ - - float32 temp32; /* Temporary float32 variable */ - - float64 lonTestVal; /* Longitude test value */ - float64 latTestVal; /* Latitude test value */ - float64 temp64; /* Temporary float64 variable */ - - char *lonArr; /* Longitude data array */ - char *latArr; /* Latitude data array */ - char dimlist[256]; /* Dimension list (geolocation - * fields) */ - char latName[17];/* Latitude field name */ - - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWregionl_index", &fid, &sdInterfaceID, - &swVgrpID); - - - /* Inclusion mode must be between 0 and 2 */ - /* -------------------------------------- */ - if (mode < 0 || mode > 2) - { - status = -1; - HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__); - HEreport("Improper Inclusion Mode: %d.\n", mode); - } - - - if (status == 0) - { - /* Get "Longitude" field info */ - /* -------------------------- */ - statLon = SWfieldinfo(swathID, "Longitude", &rank, dims, &nt, dimlist); - if (statLon != 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__); - HEreport("\"Longitude\" field not found.\n"); - } - - /* Get "Latitude" field info */ - /* -------------------------- */ - statLat = SWfieldinfo(swathID, "Latitude", &rank, dims, &nt, dimlist); - if (statLat != 0) - { - /* If not found check for "Colatitude" field info */ - /* ---------------------------------------------- */ - statCoLat = SWfieldinfo(swathID, "Colatitude", &rank, dims, &nt, - dimlist); - if (statCoLat != 0) - { - /* Check again for Geodeticlatitude */ - statGeodeticLat = SWfieldinfo(swathID, - "GeodeticLatitude", &rank, - dims, &nt, dimlist); - if (statGeodeticLat != 0) - { - /* Neither "Latitude" nor "Colatitude" field found */ - /* ----------------------------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__); - HEreport( - "Neither \"Latitude\" nor \"Colatitude\" fields found.\n"); - } - else - { - /* Latitude field is "Colatitude" */ - /* ------------------------------ */ - strcpy(latName, "GeodeticLatitude"); - } - } - else - { - /* Latitude field is "Colatitude" */ - /* ------------------------------ */ - strcpy(latName, "Colatitude"); - } - } - else - { - /* Latitude field is "Latitude" */ - /* ---------------------------- */ - strcpy(latName, "Latitude"); - } - - /* This line modifies the dimlist variable so only the along-track */ - /* dimension remains. */ - /* --------------------------------------------------------------- */ - (void) strtok(dimlist,","); - mapstatus = SWgeomapinfo(swathID,dimlist); - (void) strcpy(geodim,dimlist); - - if (status == 0) - { - /* Search along entire "Track" dimension from beginning to end */ - /* ----------------------------------------------------------- */ - start[0] = 0; - edge[0] = dims[0]; - - - /* If 1D geolocation fields then set mode to MIDPOINT */ - /* -------------------------------------------------- */ - if (rank == 1) - { - mode = HDFE_MIDPOINT; - } - - - switch (mode) - { - /* If MIDPOINT search single point in middle of "CrossTrack" */ - /* --------------------------------------------------------- */ - case HDFE_MIDPOINT: - - start[1] = dims[1] / 2; - edge[1] = 1; - - break; - - /* If ENDPOINT search 2 points at either end of "CrossTrack" */ - /* --------------------------------------------------------- */ - case HDFE_ENDPOINT: - - start[1] = 0; - stride[1] = dims[1] - 1; - edge[1] = 2; - - break; - - /* If ANYPOINT do initial MIDPOINT search */ - /* -------------------------------------- */ - case HDFE_ANYPOINT: - - start[1] = dims[1] / 2; - edge[1] = 1; - - break; - } - - - /* Compute number of elements */ - /* -------------------------- */ - nElem = edge[0] * edge[1]; - - - /* Allocate space for longitude and latitude (float64) */ - /* --------------------------------------------------- */ - lonArr = (char *) calloc(nElem, sizeof(float64)); - if(lonArr == NULL) - { - HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); - return(-1); - } - - latArr = (char *) calloc(nElem, sizeof(float64)); - if(latArr == NULL) - { - HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); - free(lonArr); - return(-1); - } - - - /* Allocate space for flag array (uint8) */ - /* ------------------------------------- */ - flag = (uint8 *) calloc(edge[0] + 1, 1); - if(flag == NULL) - { - HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); - free(lonArr); - free(latArr); - return(-1); - } - - - /* Read Longitude and Latitude fields */ - /* ---------------------------------- */ - status = SWreadfield(swathID, "Longitude", - start, stride, edge, lonArr); - status = SWreadfield(swathID, latName, - start, stride, edge, latArr); - - - - /* - * If geolocation fields are FLOAT32 then cast each entry as - * FLOAT64 - */ - if (nt == DFNT_FLOAT32) - { - for (i = nElem - 1; i >= 0; i--) - { - memcpy(&temp32, lonArr + 4 * i, 4); - temp64 = (float64) temp32; - memcpy(lonArr + 8 * i, &temp64, 8); - - memcpy(&temp32, latArr + 4 * i, 4); - temp64 = (float64) temp32; - memcpy(latArr + 8 * i, &temp64, 8); - } + /* Get pointers to "IndexDimensionMap" section within SM */ + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "IndexDimensionMap", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); } - - - /* Set boundary flag */ - /* ----------------- */ - - /* - * This variable is set to 1 if the region of interest crosses - * the +/- 180 longitude boundary - */ - bndflag = (cornerlon[0] < cornerlon[1]) ? 0 : 1; - - - - /* Main Search Loop */ - /* ---------------- */ - - /* For each track ... */ - /* ------------------ */ - for (i = 0; i < edge[0]; i++) + /* If mapping names are desired then "clear" name buffer */ + if (idxmaps != NULL) { - /* For each value from Cross Track ... */ - /* ----------------------------------- */ - for (j = 0; j < edge[1]; j++) - { - /* Read in single lon & lat values from data buffers */ - /* ------------------------------------------------- */ - memcpy(&lonTestVal, &lonArr[8 * (i * edge[1] + j)], 8); - memcpy(&latTestVal, &latArr[8 * (i * edge[1] + j)], 8); - - - /* If longitude value > 180 convert to -180 to 180 range */ - /* ----------------------------------------------------- */ - if (lonTestVal > 180) - { - lonTestVal = lonTestVal - 360; - } - - /* If Colatitude value convert to latitude value */ - /* --------------------------------------------- */ - if (statCoLat == 0) - { - latTestVal = 90 - latTestVal; - } - - - /* Test if lat value is within range */ - /* --------------------------------- */ - latTest = (latTestVal >= cornerlat[0] && - latTestVal <= cornerlat[1]); - - - if (bndflag == 1) - { - /* - * If boundary flag set test whether longitude value - * is outside region and then flip - */ - lonTest = (lonTestVal >= cornerlon[1] && - lonTestVal <= cornerlon[0]); - lonTest = 1 - lonTest; - } - else - { - lonTest = (lonTestVal >= cornerlon[0] && - lonTestVal <= cornerlon[1]); - } - - - /* - * If both longitude and latitude are within region set - * flag on for this track - */ - if (lonTest + latTest == 2) - { - flag[i] = 1; - found = 1; - break; - } - } + idxmaps[0] = 0; } - - - /* ANYPOINT search */ - /* --------------- */ - if (mode == HDFE_ANYPOINT && rank > 1) + /* Begin loop through mapping entries in metadata */ + /* ---------------------------------------------- */ + while (1) { - free(lonArr); - free(latArr); - - /* Allocate space for an entire single cross track */ - /* ----------------------------------------------- */ - lonArr = (char *) calloc(dims[1], sizeof(float64)); - if(lonArr == NULL) - { - HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); - return(-1); - } - latArr = (char *) calloc(dims[1], sizeof(float64)); - if(latArr == NULL) - { - HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); - free(lonArr); - return(-1); - } - - /* Setup start and edge */ - /* -------------------- */ - anyStart[1] = 0; - anyEdge[0] = 1; - anyEdge[1] = dims[1]; - + /* Search for OBJECT string */ + metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - /* For each track ... */ - /* ------------------ */ - for (i = 0; i < edge[0]; i++) + /* If found within "IndexDimensionMap" metadata section ... */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) { - - /* If cross track not in region (with MIDPOINT search ... */ - /* ------------------------------------------------------ */ - if (flag[i] == 0) + /* Get Geo & Data Dimensions and # of indices */ + if (idxmaps != NULL) { - /* Setup track start */ - /* ----------------- */ - anyStart[0] = i; - - - /* Read in lon and lat values for cross track */ - /* ------------------------------------------ */ - status = SWreadfield(swathID, "Longitude", - anyStart, NULL, anyEdge, lonArr); - status = SWreadfield(swathID, latName, - anyStart, NULL, anyEdge, latArr); - - - - /* - * If geolocation fields are FLOAT32 then cast each - * entry as FLOAT64 - */ - if (nt == DFNT_FLOAT32) - { - for (j = dims[1] - 1; j >= 0; j--) - { - memcpy(&temp32, lonArr + 4 * j, 4); - temp64 = (float64) temp32; - memcpy(lonArr + 8 * j, &temp64, 8); - - memcpy(&temp32, latArr + 4 * j, 4); - temp64 = (float64) temp32; - memcpy(latArr + 8 * j, &temp64, 8); - } - } - - - /* For each value from Cross Track ... */ - /* ----------------------------------- */ - for (j = 0; j < dims[1]; j++) - { - /* Read in single lon & lat values from buffers */ - /* -------------------------------------------- */ - memcpy(&lonTestVal, &lonArr[8 * j], 8); - memcpy(&latTestVal, &latArr[8 * j], 8); - - - /* If lon value > 180 convert to -180 - 180 range */ - /* ---------------------------------------------- */ - if (lonTestVal > 180) - { - lonTestVal = lonTestVal - 360; - } - - /* If Colatitude value convert to latitude value */ - /* --------------------------------------------- */ - if (statCoLat == 0) - { - latTestVal = 90 - latTestVal; - } - - - /* Test if lat value is within range */ - /* --------------------------------- */ - latTest = (latTestVal >= cornerlat[0] && - latTestVal <= cornerlat[1]); - - - if (bndflag == 1) - { - /* - * If boundary flag set test whether - * longitude value is outside region and then - * flip - */ - lonTest = (lonTestVal >= cornerlon[1] && - lonTestVal <= cornerlon[0]); - lonTest = 1 - lonTest; - } - else - { - lonTest = (lonTestVal >= cornerlon[0] && - lonTestVal <= cornerlon[1]); - } - + /* Get Geo Dim, remove quotes, add "/" */ + EHgetmetavalue(metaptrs, "GeoDimension", utlstr); + REMQUOTE(utlstr); + strcat(utlstr, "/"); - /* - * If both longitude and latitude are within - * region set flag on for this track - */ - if (lonTest + latTest == 2) - { - flag[i] = 1; - found = 1; - break; - } - } - } - } - } - /* - for (j1 = 0; j1 < edge[0]; j1++) - { - idxrange[j1] = (int32) flag[j1]; - } - */ - /* If within region setup Region Structure */ - /* --------------------------------------- */ - if (found == 1) - { - /* For all entries in SWXRegion array ... */ - /* -------------------------------------- */ - for (k = 0; k < NSWATHREGN; k++) - { - /* If empty region ... */ - /* ------------------- */ - if (SWXRegion[k] == 0) - { - /* Allocate space for region entry */ - /* ------------------------------- */ - SWXRegion[k] = (struct swathRegion *) - calloc(1, sizeof(struct swathRegion)); - if(SWXRegion[k] == NULL) + /* If not first map then add comma delimiter. */ + if (nMap > 0) { - HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__); - free(lonArr); - free(latArr); - free(flag); - return(-1); + strcat(idxmaps, ","); } - /* Store file and swath ID */ - /* ----------------------- */ - SWXRegion[k]->fid = fid; - SWXRegion[k]->swathID = swathID; + /* Add to map list */ + strcat(idxmaps, utlstr); - /* Set Start & Stop Vertical arrays to -1 */ - /* -------------------------------------- */ - for (j = 0; j < 8; j++) + /* Get Index size (if desired) */ + if (idxsizes != NULL) { - SWXRegion[k]->StartVertical[j] = -1; - SWXRegion[k]->StopVertical[j] = -1; - SWXRegion[k]->StartScan[j] = -1; - SWXRegion[k]->StopScan[j] = -1; + /* Parse off geo dimension and find its size */ + slash = strchr(utlstr, '/'); + if (slash) *slash = 0; + idxsizes[nMap] = SWdiminfo(swathID, utlstr); } - /* Set region ID */ - /* ------------- */ - regionID = k; - break; - } - } - if (k >= NSWATHREGN) - { - HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__); - HEreport( - "regionID exceeded NSWATHREGN.\n"); - return (-1); - } - - /* Find start and stop of regions */ - /* ------------------------------ */ - - /* Subtract previous flag value from current one */ - /* --------------------------------------------- */ - - /* - * Transition points will have flag value (+1) start or - * (255 = (uint8) -1) stop of region - */ - for (i = edge[0]; i > 0; i--) - { - flag[i] -= flag[i - 1]; - } - - - for (i = 0; i <= edge[0]; i++) - { - /* Start of region */ - /* --------------- */ - if (flag[i] == 1) - { - /* Delyth Jones Moved the increment of the region down - to next if statement j = ++SWXRegion[k]->nRegions; */ - - /* using temp value, if not equal to stop region - invalid region otherwise ok Delyth Jones */ - tmpVal = i+1; - } + /* Get Data Dim, remove quotes */ + EHgetmetavalue(metaptrs, "DataDimension", utlstr); + REMQUOTE(utlstr); - /* End of region */ - /* ------------- */ - if (flag[i] == 255) - { - if( tmpVal!=i ) - { - /* Increment (multiple) region counter */ - /* ----------------------------------- */ - j = ++SWXRegion[k]->nRegions; - - if (mapstatus == 2) - { - l = i; - if ((tmpVal-1) % 2 == 1) - { - tmpVal = tmpVal + 1; - } - - if ((l-1) % 2 == 0) - { - l = l - 1; - } - } - SWXRegion[k]->StartRegion[j - 1] = tmpVal-1; - idxrange[0] = tmpVal - 1; - SWXRegion[k]->StopRegion[j - 1] = l - 1; - idxrange[1] = l - 1; - validReg = 0; - } + /* Add to map list */ + strcat(idxmaps, utlstr); } + /* Increment number of maps */ + nMap++; } - - } - free(lonArr); - free(latArr); - free(flag); - } - } - if(validReg==0) - { - return (regionID); - } - else - { - return (-1); - } - -} -#endif - -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWdeftimeperiod | -| | -| DESCRIPTION: Finds swath cross tracks observed during time period and | -| returns period ID | -| | -| region ID | -| DESCRIPTION: | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| periodID int32 (Period ID) or (-1) if failed | -| | -| INPUTS: | -| swathID int32 Swath structure ID | -| starttime float64 TAI sec Start of time period | -| stoptime float64 TAI sec Stop of time period | -| mode int32 Search mode | -| HDFE_MIDPOINT - Use midpoint of Xtrack | -| HDFE_ENDPOINT - Use endpoints of Xtrack | -| | -| OUTPUTS: | -| None | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime, - int32 mode) -{ - - intn i; /* Loop index */ - intn j; /* Loop index */ - intn k = 0; /* Loop index */ - intn status; /* routine return status variable */ - intn statTime; /* Status from SWfieldinfo for time */ - - uint8 found = 0; /* Found flag */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - int32 rank; /* Rank of geolocation fields */ - int32 nt; /* Number type of geolocation fields */ - int32 dims[8]; /* Dimensions of geolocation fields */ - int32 start[2]; /* Start array (read) */ - int32 stride[2] = {1, 1}; /* Stride array (read) */ - int32 edge[2]; /* Edge array (read) */ - int32 periodID = -1; /* Period ID (return) */ - int32 dum; /* Dummy (loop) variable */ - - float64 time64Test; /* Time test value */ - float64 *time64 = NULL; /* Time data array */ - - char dimlist[256]; /* Dimension list (geolocation fields) */ - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWdeftimeperiod", &fid, &sdInterfaceID, - &swVgrpID); - - if (status == 0) - { - /* Get "Time" field info */ - /* --------------------- */ - statTime = SWfieldinfo(swathID, "Time", &rank, dims, &nt, dimlist); - if (statTime != 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWdeftimeperiod", __FILE__, __LINE__); - HEreport("\"Time\" field not found.\n"); - } - - if (status == 0) - { - /* Search along entire "Track" dimension from beginning to end */ - /* ----------------------------------------------------------- */ - start[0] = 0; - edge[0] = dims[0]; - - - /* If 1D geolocation fields then set mode to MIDPOINT */ - /* -------------------------------------------------- */ - if (rank == 1) - { - mode = HDFE_MIDPOINT; - } - - - switch (mode) - { - - /* If MIDPOINT search single point in middle of "CrossTrack" */ - /* --------------------------------------------------------- */ - case HDFE_MIDPOINT: - - start[1] = dims[1] / 2; - edge[1] = 1; - - - /* Allocate space for time data */ - /* ---------------------------- */ - time64 = (float64 *) calloc(edge[0], 8); - if(time64 == NULL) - { - HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__); - return(-1); - } - - - /* Read "Time" field */ - /* ----------------- */ - status = SWreadfield(swathID, "Time", - start, NULL, edge, time64); - break; - - - /* If ENDPOINT search 2 points at either end of "CrossTrack" */ - /* --------------------------------------------------------- */ - case HDFE_ENDPOINT: - start[1] = 0; - stride[1] = dims[1] - 1; - edge[1] = 2; - - - /* Allocate space for time data */ - /* ---------------------------- */ - time64 = (float64 *) calloc(edge[0] * 2, 8); - if(time64 == NULL) + else + /* No more mappings found */ { - HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__); - return(-1); - } - - /* Read "Time" field */ - /* ----------------- */ - status = SWreadfield(swathID, "Time", - start, stride, edge, time64); - break; - - } - - if (time64) - { - /* For each track (from top) ... */ - /* ----------------------------- */ - for (i = 0; i < edge[0]; i++) - { - /* For each value from Cross Track ... */ - /* ----------------------------------- */ - for (j = 0; j < edge[1]; j++) - { - - /* Get time test value */ - /* ------------------- */ - time64Test = time64[i * edge[1] + j]; - - - /* If within time period ... */ - /* ------------------------- */ - if (time64Test >= starttime && - time64Test <= stoptime) - { - /* Set found flag */ - /* -------------- */ - found = 1; - - - /* For all entries in SWXRegion array ... */ - /* -------------------------------------- */ - for (k = 0; k < NSWATHREGN; k++) - { - /* If empty region ... */ - /* ------------------- */ - if (SWXRegion[k] == 0) - { - /* Allocate space for region entry */ - /* ------------------------------- */ - SWXRegion[k] = (struct swathRegion *) - calloc(1, sizeof(struct swathRegion)); - if(SWXRegion[k] == NULL) - { - HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__); - return(-1); - } - - /* Store file and swath ID */ - /* ----------------------- */ - SWXRegion[k]->fid = fid; - SWXRegion[k]->swathID = swathID; - - - /* Set number of isolated regions to 1 */ - /* ----------------------------------- */ - SWXRegion[k]->nRegions = 1; - - - /* Set start of region to first track found */ - /* ---------------------------------------- */ - SWXRegion[k]->StartRegion[0] = i; - - - /* Set Start & Stop Vertical arrays to -1 */ - /* -------------------------------------- */ - for (dum = 0; dum < 8; dum++) - { - SWXRegion[k]->StartVertical[dum] = -1; - SWXRegion[k]->StopVertical[dum] = -1; - SWXRegion[k]->StartScan[dum] = -1; - SWXRegion[k]->StopScan[dum] = -1; - } - - - /* Set period ID */ - /* ------------- */ - periodID = k; - - break; /* Break from "k" loop */ - } - } - } - if (found == 1) - { - break; /* Break from "j" loop */ - } - } - if (found == 1) - { - break; /* Break from "i" loop */ - } - } + break; + } + } + free(metabuf); + } + } - if (periodID >= 0) - { - /* Clear found flag */ - /* ---------------- */ - found = 0; + /* Set nMap to -1 if error status exists */ + /* ------------------------------------- */ + if (status == -1) + { + nMap = -1; + } + free(utlstr); + return (nMap); +} - /* For each track (from bottom) ... */ - /* -------------------------------- */ - for (i = edge[0] - 1; i >= 0; i--) - { - /* For each value from Cross Track ... */ - /* ----------------------------------- */ - for (j = 0; j < edge[1]; j++) - { - - /* Get time test value */ - /* ------------------- */ - time64Test = time64[i * edge[1] + j]; - - - /* If within time period ... */ - /* ------------------------- */ - if (time64Test >= starttime && - time64Test <= stoptime) - { - /* Set found flag */ - /* -------------- */ - found = 1; - - /* Set start of region to first track found */ - /* ---------------------------------------- */ - SWXRegion[periodID]->StopRegion[0] = i; - - break; /* Break from "j" loop */ - } - } - if (found == 1) - { - break; /* Break from "i" loop */ - } - } - } - free(time64); - } - } - } - return (periodID); -} -#endif -#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWextractregion | +| FUNCTION: SWinqfields | | | -| DESCRIPTION: Retrieves data from specified region. | +| DESCRIPTION: Returns fieldnames, ranks and numbertypes defined in swath. | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nFld int32 Number of (geo/data) fields in swath | | | | INPUTS: | -| swathID int32 Swath structure ID | -| regionID int32 Region ID | -| fieldname char Fieldname | -| externalflag int32 External geolocation fields flag | -| HDFE_INTERNAL (0) | -| HDFE_EXTERNAL (1) | +| swathID int32 swath structure ID | +| fieldtype char field type (geo or data) | +| | | | | OUTPUTS: | -| buffer void Data buffer containing subsetted region | +| fieldlist char Field names (comma-separated) | +| rank int32 Array of ranks | +| numbertype int32 Array of HDF number types | | | | NOTES: | | | @@ -6971,1181 +2287,553 @@ SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime, | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Add vertical subsetting | -| Oct 96 Joel Gales Mapping offset value not read from SWmapinfo | -| Dec 96 Joel Gales Vert Subset overwriting data buffer | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Mar 97 Joel Gales Add support for l_index mapping | -| Jul 99 DaW Add support for floating scene subsetting | -| Feb 03 Terry Haran/ | -| Abe Taaheri Forced map offset to 0 so that data is extracted | -| without offset consideration. This will preserve | -| original mapping between geofields and the data | -| field. | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nFld to -1 if status = -1 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -SWextractregion(int32 swathID, int32 regionID, const char *fieldname, - int32 externalflag, VOIDP buffer) +static int32 +SWinqfields(int32 swathID, const char *fieldtype, char *fieldlist, int32 rank[], + int32 numbertype[]) { - intn i; /* Loop index */ - intn j; /* Loop index */ - intn k; /* Loop index */ - intn l; /* Loop index */ intn status; /* routine return status variable */ - intn long_status = 3; /* routine return status variable */ - /* for longitude */ - intn land_status = 3; /* Used for L7 float scene sub. */ - intn statMap = -1; /* Status from SWmapinfo */ - - uint8 found = 0; /* Found flag */ - uint8 vfound = 0; /* Found flag for vertical subsetting*/ - /* --- xhua */ - uint8 scene_cnt = 0; /* Used for L7 float scene sub. */ - uint8 detect_cnt = 0; /* Used to convert scan to scanline */ - /* L7 float scene sub. */ int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - - int32 numtype = 0; /* Used for L7 float scene sub. */ - int32 count = 0; /* Used for L7 float scene sub. */ - int32 l_index = 0; /* Geo Dim Index */ - int32 nDim; /* Number of dimensions */ - int32 slen[64]; /* String length array */ - int32 dum; /* Dummy variable */ - int32 offset = 0; /* Mapping offset */ - int32 incr = 0; /* Mapping increment */ - int32 nXtrk; /* Number of cross tracks */ - int32 scan_shift = 0; /* Used to take out partial scans */ - int32 dumdims[8]; /* Dimensions from SWfieldinfo */ - int32 start[8]; /* Start array for data read */ - int32 edge[8]; /* Edge array for data read */ - int32 dims[8]; /* Dimensions */ - int32 rank = 0; /* Field rank */ - int32 rk = 0; /* Field rank */ - int32 ntype = 0; /* Field number type */ - int32 bufOffset = 0; /* Output buffer offset */ - int32 size; /* Size of data buffer */ - int32 idxMapElem = -1; /* Number of l_index map elements */ - int32 *idxmap = NULL; /* Pointer to l_index mapping array */ - - int32 startscanline = 0; - int32 stopscanline = 0; - char *dfieldlist = (char *)NULL; - int32 strbufsize = 0; - int32 dfrank[8]; - int32 numtype2[8]; - uint16 *buffer2 = (uint16 *)NULL; - uint16 *tbuffer = (uint16 *)NULL; - int32 dims2[8]; - int32 nt = 0; - int32 startscandim = -1; - int32 stopscandim = -1; - int32 rank2 = 0; - - char dimlist[256]; /* Dimension list */ - char geodim[256];/* Geolocation field dimension list */ - char tgeodim[256];/* Time field dimension list */ - char dgeodim[256];/* Data field dimension list for subsetting */ - char utlbuf[256];/* Utility buffer */ - char *ptr[64]; /* String pointer array */ - - - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWextractregion", &fid, &sdInterfaceID, - &swVgrpID); - - - /* Check for valid region ID */ - /* ------------------------- */ - if (status == 0) - { - if (regionID < 0 || regionID >= NSWATHREGN) - { - status = -1; - HEpush(DFE_RANGE, "SWextractregion", __FILE__, __LINE__); - HEreport("Invalid Region id: %d.\n", regionID); - } - } - - - - /* Check for active region ID */ - /* -------------------------- */ - if (status == 0) - { - if (SWXRegion[regionID] == 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); - HEreport("Inactive Region ID: %d.\n", regionID); - } - } - - - /* This code checks for the attribute detector_count */ - /* which is found in Landsat 7 files. It is used */ - /* for some of the loops. */ - /* ================================================= */ - if (status == 0 && SWXRegion[regionID]->scanflag == 1) - { - land_status = SWattrinfo(swathID, "detector_count", &numtype, &count); - if (land_status == 0) - land_status = SWreadattr(swathID, "detector_count", &detect_cnt); - } - - /* Check that geo file and data file are same for INTERNAL subsetting */ - /* ------------------------------------------------------------------ */ - if (status == 0) - { - if (SWXRegion[regionID]->fid != fid && externalflag != HDFE_EXTERNAL) - { - status = -1; - HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); - HEreport("Region is not defined for this file.\n"); - } - } + int32 swVgrpID; /* Swath root Vgroup ID */ + int32 idOffset = SWIDOFFSET; /* Swath ID offset */ + int32 nFld = 0; /* Number of mappings */ + int32 slen[8]; /* String length array */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char swathname[80]; /* Swath Name */ + char *utlstr; /* Utility string */ + char *utlstr2; /* Utility string 2 */ + char *ptr[8]; /* String pointer array */ - /* Check that geo swath and data swath are same for INTERNAL subsetting */ - /* -------------------------------------------------------------------- */ - if (status == 0) + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) { - if (SWXRegion[regionID]->swathID != swathID && - externalflag != HDFE_EXTERNAL) - { - status = -1; - HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); - HEreport("Region is not defined for this Swath.\n"); - } + HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__); + return(-1); } - - - /* Check for valid fieldname */ - /* ------------------------- */ - if (status == 0) + utlstr2 = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr2 == NULL) { - - /* Get data field info */ - /* ------------------- */ - status = SWfieldinfo(swathID, fieldname, &rank, - dims, &ntype, dimlist); - - if (status != 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); - HEreport("Field \"%s\" Not Found.\n", fieldname); - } + HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__); + free(utlstr); + return(-1); } + /* Check for valid swath id */ + status = SWchkswid(swathID, "SWinqfields", + &fid, &sdInterfaceID, &swVgrpID); - /* No problems so proceed ... */ - /* -------------------------- */ if (status == 0) { - - - /* Initialize start and edge for all dimensions */ - /* -------------------------------------------- */ - for (j = 0; j < rank; j++) - { - start[j] = 0; - edge[j] = dims[j]; - } - - - /* Vertical Subset */ - /* --------------- */ - for (j = 0; j < 8; j++) + /* If field names, ranks, or number types desired ... */ + /* --------------------------------------------------- */ + if (fieldlist != NULL || rank != NULL || numbertype != NULL) { - /* If active vertical subset ... */ - /* ----------------------------- */ - if (SWXRegion[regionID]->StartVertical[j] != -1) + /* Get swath name */ + int sID = swathID % idOffset; + if (sID >= NSWATH) { + free(utlstr); + free(utlstr2); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); - /* Find vertical dimension within dimlist */ - /* -------------------------------------- */ - dum = EHstrwithin(SWXRegion[regionID]->DimNamePtr[j], - dimlist, ','); - - /* If dimension found ... */ - /* ---------------------- */ - if (dum != -1) + /* Get pointers to "GeoField" or "DataField" section within SM */ + if (strcmp(fieldtype, "Geolocation Fields") == 0) + { + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "GeoField", metaptrs); + if(metabuf == NULL) { - /* Compute start and edge for vertical dimension */ - /* --------------------------------------------- */ - vfound = 1; /* xhua */ - start[dum] = SWXRegion[regionID]->StartVertical[j]; - edge[dum] = SWXRegion[regionID]->StopVertical[j] - - SWXRegion[regionID]->StartVertical[j] + 1; + free(utlstr); + free(utlstr2); + return(-1); } - else + strcpy(utlstr2, "GeoFieldName"); + } + else + { + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "DataField", metaptrs); + if(metabuf == NULL) { - /* Vertical dimension not found */ - /* ---------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); - HEreport("Vertical Dimension Not Found: \"%s\".\n", - SWXRegion[regionID]->DimNamePtr); + free(utlstr); + free(utlstr2); + return(-1); } + strcpy(utlstr2, "DataFieldName"); } - } /* End of Vertical Subset loop */ - - /* No problems so proceed ... */ - /* -------------------------- */ - if (status == 0) - { - /* If non-vertical subset regions defined ... */ - /* ------------------------------------------ */ - if (SWXRegion[regionID]->nRegions > 0) + /* If field names are desired then "clear" name buffer */ + if (fieldlist != NULL) { + fieldlist[0] = 0; + } - /* Get geolocation dimension name */ - /* ------------------------------ */ - status = SWfieldinfo(SWXRegion[regionID]->swathID, - "Longitude", &dum, - dumdims, &dum, geodim); - long_status = status; - - /* If Time field being used, check for dimensions */ - /* ---------------------------------------------- */ - if (timeflag == 1) - { - /* code change to fix time subset bug for Landsat7 */ - - status = SWfieldinfo(SWXRegion[regionID]->swathID, - "Time", &dum, - dumdims, &dum, tgeodim); - - if (strcmp(geodim, tgeodim) != 0) - { - strcpy(geodim, tgeodim); - } - } - timeflag = 0; - - /* If defscanregion being used, get dimensions */ - /* of field being used */ - /* ---------------------------------------------- */ - if (SWXRegion[regionID]->scanflag == 1) - { - (void) SWnentries(SWXRegion[regionID]->swathID,4,&strbufsize); - dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char)); - (void) SWinqdatafields(SWXRegion[regionID]->swathID,dfieldlist,dfrank,numtype2); - status = SWfieldinfo(SWXRegion[regionID]->swathID,dfieldlist,&dum,dumdims,&dum,dgeodim); - - /* The dimensions have to be switched, because */ - /* the mappings force a geodim and datadim */ - /* so to find the mapping, the dimensions must */ - /* be switched, but the subsetting will still */ - /* be based on the correct dimensions */ - /* ------------------------------------------- */ - if (strcmp(dgeodim,dimlist) != 0 || long_status == -1) - { - strcpy(geodim,dimlist); - strcpy(dimlist,dgeodim); - } - } - - - /* Get "Track" (first) Dimension from geo dimlist */ - /* ---------------------------------------------- */ - nDim = EHparsestr(geodim, ',', ptr, slen); - geodim[slen[0]] = 0; - - - /* Parse Data Field Dimlist & find mapping */ - /* --------------------------------------- */ - nDim = EHparsestr(dimlist, ',', ptr, slen); + /* Begin loop through mapping entries in metadata */ + /* ---------------------------------------------- */ + while (1) + { + /* Search for OBJECT string */ + metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT="); - /* Loop through all dimensions and search for mapping */ - /* -------------------------------------------------- */ - for (i = 0; i < nDim; i++) + /* If found within "Geo" or "Data" Field metadata section .. */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) { - memcpy(utlbuf, ptr[i], slen[i]); - utlbuf[slen[i]] = 0; - statMap = SWmapinfo(swathID, geodim, utlbuf, - &offset, &incr); - - - /* - * Force offset to 0. - * We're not changing the mapping, so we want - * the original offset to apply to the subsetted data. - * Otherwise, bad things happen, such as subsetting - * past the end of the original data, and being unable - * to read the first elements of the - * original data. - * The offset is only important for aligning the - * data with interpolated (incr > 0) or decimated - * (incr < 0) geolocation information for the data. - */ - - offset = 0; - - - /* Mapping found */ - /* ------------- */ - if (statMap == 0) + /* Get Fieldnames (if desired) */ + if (fieldlist != NULL) { - found = 1; - l_index = i; - break; - } - } - + /* Check 1st for old meta data then new */ + /* ------------------------------------ */ + EHgetmetavalue(metaptrs, "OBJECT", utlstr); - /* If mapping not found check for geodim within dimlist */ - /* ---------------------------------------------------- */ - if (found == 0) - { - l_index = EHstrwithin(geodim, dimlist, ','); + /* + * If OBJECT value begins with double quote then old + * metadata, field name is OBJECT value. Otherwise + * search for "GeoFieldName" or "DataFieldName" + * string + */ - /* Geo dimension found within subset field dimlist */ - /* ----------------------------------------------- */ - if (l_index != -1) - { - found = 1; - offset = 0; - incr = 1; - } - } + if (utlstr[0] != '"') + { + strcpy(utlstr, "\t\t\t\t"); + strcat(utlstr, utlstr2); + strcat(utlstr, "="); + metaptrs[0] = strstr(metaptrs[0], utlstr); + EHgetmetavalue(metaptrs, utlstr2, utlstr); + } + /* Strip off double quotes */ + /* ----------------------- */ + REMQUOTE(utlstr); - /* If mapping not found check for l_indexed mapping */ - /* ---------------------------------------------- */ - if (found == 0) - { - /* Get size of geo dim & allocate space of l_index mapping */ - /* ----------------------------------------------------- */ - dum = SWdiminfo(swathID, geodim); - if( dum < 0 ) - { - return -1; - } + /* Add to fieldlist */ + /* ---------------- */ + if (nFld > 0) + { + strcat(fieldlist, ","); + } + strcat(fieldlist, utlstr); - /* For Landsat files, the l_index mapping has two values */ - /* for each point, a left and right point. So for a 37 */ - /* scene band file there are 2x2 points for each scene */ - /* meaning, 2x2x37 = 148 values. The above function */ - /* only returns the number of values in the track */ - /* dimension. */ - /* ----------------------------------------------------- */ - if(land_status == 0) - if(strcmp(fieldname, "Latitude") == 0 || - strcmp(fieldname, "Longitude") == 0) - { - dum = dum * 2; - } - idxmap = (int32 *) calloc(dum, sizeof(int32)); - if(idxmap == NULL) + } + /* Get Numbertype */ + if (numbertype != NULL) { - HEpush(DFE_NOSPACE,"SWextractregion", __FILE__, __LINE__); - return(-1); + EHgetmetavalue(metaptrs, "DataType", utlstr); + numbertype[nFld] = EHnumstr(utlstr); } - - /* Loop through all dimensions and search for mapping */ - /* -------------------------------------------------- */ - for (i = 0; i < nDim; i++) + /* + * Get Rank (if desired) by counting # of dimensions in + * "DimList" string + */ + if (rank != NULL) { - memcpy(utlbuf, ptr[i], slen[i]); - utlbuf[slen[i]] = 0; - - idxMapElem = - SWidxmapinfo(swathID, geodim, utlbuf, idxmap); - - - /* Mapping found */ - /* ------------- */ - if (idxMapElem != -1) - { - found = 1; - l_index = i; - break; - } + EHgetmetavalue(metaptrs, "DimList", utlstr); + rank[nFld] = EHparsestr(utlstr, ',', ptr, slen); } + /* Increment number of fields */ + nFld++; + } + else + /* No more fields found */ + { + break; } + } + free(metabuf); + } + } + + /* Set nFld to -1 if error status exists */ + /* ------------------------------------- */ + if (status == -1) + { + nFld = -1; + } + free(utlstr); + free(utlstr2); - /* If regular mapping found ... */ - /* ---------------------------- */ - if (found == 1 && idxMapElem == -1) - { - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - if (k > 0) - { - /* Compute size in bytes of previous region */ - /* ---------------------------------------- */ - size = edge[0]; - for (j = 1; j < rank; j++) - { - size *= edge[j]; - } - size *= DFKNTsize(ntype); + return (nFld); +} - /* Compute output buffer offset */ - /* ---------------------------- */ - bufOffset += size; - } - else - { - /* Initialize output buffer offset */ - /* ------------------------------- */ - bufOffset = 0; - } - /* Compute number of cross tracks in region */ - /* ---------------------------------------- */ - nXtrk = SWXRegion[regionID]->StopRegion[k] - - SWXRegion[regionID]->StartRegion[k] + 1; +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: SWinqgeofields | +| | +| DESCRIPTION: Inquires about geo fields in swath | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| nflds int32 Number of geo fields in swath | +| | +| INPUTS: | +| swathID int32 swath structure ID | +| | +| OUTPUTS: | +| fieldlist char Field names (comma-separated) | +| rank int32 Array of ranks | +| numbertype int32 Array of HDF number types | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Jun 96 Joel Gales Original Programmer | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +int32 +SWinqgeofields(int32 swathID, char *fieldlist, int32 rank[], + int32 numbertype[]) +{ + int32 nflds; /* Number of Geolocation fields */ - /* Positive increment (geodim <= datadim) */ - /* -------------------------------------- */ - if (incr > 0) - { - if (SWXRegion[regionID]->scanflag == 1) - { - start[l_index] = SWXRegion[regionID]->StartRegion[k]/incr; - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - start[l_index] = (SWXRegion[regionID]->StartRegion[k]+detect_cnt)/incr; - status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank,dims2,&nt,dimlist); - buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2); - if(incr == 1) - start[l_index] = start[l_index] - (buffer2[0] * detect_cnt); - else - start[l_index] = start[l_index] - buffer2[0]; - free(buffer2); - } - scan_shift = nXtrk % incr; - if(scan_shift != 0) - nXtrk = nXtrk - scan_shift; - edge[l_index] = nXtrk / incr; - if (nXtrk % incr != 0) - edge[l_index]++; - if(long_status == -1 || incr == 1) - { - if( detect_cnt == 0 ) - return -1; - scan_shift = nXtrk % detect_cnt; - if(scan_shift != 0) - edge[l_index] = nXtrk - scan_shift; - } - - } - else - { - start[l_index] = SWXRegion[regionID]->StartRegion[k] * incr + offset; - edge[l_index] = nXtrk * incr - offset; - } - } - else - { - /* Negative increment (geodim > datadim) */ - /* ------------------------------------- */ - start[l_index] = SWXRegion[regionID]->StartRegion[k] - / (-incr) + offset; - edge[l_index] = nXtrk / (-incr); + /* Call "SWinqfields" routine */ + /* -------------------------- */ + nflds = SWinqfields(swathID, "Geolocation Fields", fieldlist, rank, + numbertype); - /* - * If Xtrk not exactly divisible by incr, round - * edge to next highest integer - */ + return (nflds); - if (nXtrk % (-incr) != 0) - { - edge[l_index]++; - } - } +} - /* Read Data into output buffer */ - /* ---------------------------- */ - status = SWreadfield(swathID, fieldname, - start, NULL, edge, - (uint8 *) buffer + bufOffset); - } - } - else if (found == 1 && idxMapElem != -1) - { - /* Indexed Mapping */ - /* --------------- */ - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - if (k > 0) - { - /* Compute size in bytes of previous region */ - /* ---------------------------------------- */ - size = edge[0]; - for (j = 1; j < rank; j++) - { - size *= edge[j]; - } - size *= DFKNTsize(ntype); - /* Compute output buffer offset */ - /* ---------------------------- */ - bufOffset += size; - } - else - { - /* Initialize output buffer offset */ - /* ------------------------------- */ - bufOffset = 0; - } +/*----------------------------------------------------------------------------| +| BEGIN_PROLOG | +| | +| FUNCTION: SWinqdatafields | +| | +| DESCRIPTION: Inquires about data fields in swath | +| | +| | +| Return Value Type Units Description | +| ============ ====== ========= ===================================== | +| nflds int32 Number of data fields in swath | +| | +| INPUTS: | +| swathID int32 swath structure ID | +| | +| OUTPUTS: | +| fieldlist char Field names (comma-separated) | +| rank int32 Array of ranks | +| numbertype int32 Array of HDF number types | +| | +| NOTES: | +| | +| | +| Date Programmer Description | +| ====== ============ ================================================= | +| Jun 96 Joel Gales Original Programmer | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +int32 +SWinqdatafields(int32 swathID, char *fieldlist, int32 rank[], + int32 numbertype[]) +{ + int32 nflds; /* Number of Data fields */ - /* Compute start & edge from l_index mappings */ - /* ---------------------------------------- */ - if (SWXRegion[regionID]->scanflag == 1 && - (strcmp(fieldname, "Latitude") == 0 || - strcmp(fieldname, "Longitude") == 0)) - { - if (land_status == 0) - status = SWreadattr(swathID, "scene_count", &scene_cnt); - startscanline = SWXRegion[regionID]->StartRegion[k]; - stopscanline = SWXRegion[regionID]->StopRegion[k]; - if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3) - { - status = SWfieldinfo(swathID,"scan_no",&rk,dims2,&nt,dimlist); - tbuffer = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status =SWreadfield(swathID,"scan_no",NULL,NULL,NULL,tbuffer); - startscanline = startscanline - ((tbuffer[0] * detect_cnt) - detect_cnt); - stopscanline = stopscanline - ((tbuffer[0] * detect_cnt) - 1); - } - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - if(startscandim == -1) - if(startscanline < idxmap[0]) - { - startscandim = 0; - start[l_index] = 0; - if(stopscanline > idxmap[scene_cnt * 2 - 1]) - { - stopscandim = scene_cnt*2 - startscandim; - edge[l_index] = scene_cnt*2 - startscandim; - } - } - } - j = 0; - for (l = 0; l < scene_cnt; l++) - { - if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline) - if(startscandim == -1) - { - start[l_index] = j; - startscandim = j; - } - if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline) - if(startscandim != -1) - { - edge[l_index] = j - start[l_index] + 2; - stopscandim = j - start[l_index] + 1; - } - j = j + 2; - } - if(SWXRegion[regionID]->band8flag == 1 || - SWXRegion[regionID]->band8flag == 2) - { - if(startscandim == -1) - if(startscanline < idxmap[0]) - { - startscandim = 0; - start[l_index] = 0; - } - if(stopscandim == -1) - if(stopscanline > idxmap[scene_cnt * 2 - 1]) - { - stopscandim = scene_cnt*2 - start[l_index]; - edge[l_index] = scene_cnt*2 - start[l_index]; - } - } - if(SWXRegion[regionID]->band8flag == 2) - { - if(startscandim == -1) - if(startscanline > idxmap[j - 1]) - { - status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank2,dims2,&nt,dimlist); - buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2); - startscanline = startscanline - (buffer2[0] * detect_cnt); - stopscanline = stopscanline - (buffer2[0] * detect_cnt); - free(buffer2); - j = 0; - for (l = 0; l < scene_cnt; l++) - { - if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline) - { - start[l_index] = j; - } - if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline) - edge[l_index] = j - start[l_index] + 2; - j = j + 2; - if(idxmap[j] == 0 || idxmap[j+1] == 0) - l = scene_cnt; - } - - } - } - - } - else if(SWXRegion[regionID]->scanflag == 1 && - (strcmp(fieldname, "scene_center_latitude") == 0 || - strcmp(fieldname, "scene_center_longitude") == 0)) - { - if (land_status == 0) - status = SWreadattr(swathID, "scene_count", &scene_cnt); - startscanline = SWXRegion[regionID]->StartRegion[k]; - stopscanline = SWXRegion[regionID]->StopRegion[k]; - if(startscanline < idxmap[0]) - { - startscandim = 0; - start[l_index] = 0; - } - for (l = 0; l < scene_cnt-1; l++) - { - if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline) - if(startscandim == -1) - { - start[l_index] = l; - startscandim = l; - } - if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline) - if(stopscandim == -1) - { - edge[l_index] = l - start[l_index] + 2; - stopscandim = l + 1; - } - } - if(stopscandim == -1) - { - if(stopscanline > idxmap[scene_cnt - 1]) - { - edge[l_index] = scene_cnt - start[l_index]; - stopscandim = scene_cnt - 1; - } - } - - if(SWXRegion[regionID]->band8flag == 1) - { - if(stopscandim == -1) - if(stopscanline > idxmap[scene_cnt - 1]) - { - edge[l_index] = scene_cnt - start[l_index]; - stopscandim = scene_cnt -1; - } - } - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - if(startscandim == -1) - { - if(startscanline < idxmap[0]) - { - startscandim = 0; - start[l_index] = 0; - edge[l_index] = stopscandim - startscandim + 1; - } - } - if(startscandim == -1) - { - startscanline = SWXRegion[regionID]->StartScan[k] * detect_cnt - detect_cnt; - stopscanline = SWXRegion[regionID]->StopScan[k] * detect_cnt - 1; - for (l = 0; l < scene_cnt-1; l++) - { - if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline) - start[l_index] = l; - if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline) - edge[l_index] = l - start[l_index] + 1; - } - } - } - } - else - { - if (SWXRegion[regionID]->scanflag == 1 && - strcmp(fieldname,dfieldlist) == 0) - { - start[l_index] = SWXRegion[regionID]->StartRegion[k]; - edge[l_index] = SWXRegion[regionID]->StopRegion[k] - - SWXRegion[regionID]->StartRegion[k] + 1; - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3 ) - { - status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank,dims2,&nt,dimlist); - buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2); - start[l_index] = start[l_index] - (buffer2[0] * detect_cnt - detect_cnt); - free(buffer2); - } - } - else - { - start[l_index] = idxmap[SWXRegion[regionID]->StartRegion[k]]; - - edge[l_index] = idxmap[SWXRegion[regionID]->StopRegion[k]] - - idxmap[SWXRegion[regionID]->StartRegion[k]] + 1; - } - } - /* Read Data into output buffer */ - /* ---------------------------- */ - status = SWreadfield(swathID, fieldname, - start, NULL, edge, - buffer); - if (SWXRegion[regionID]->scanflag == 1) - { - - if (strcmp(fieldname,"Longitude") == 0) - { - status = SWscan2longlat(swathID, fieldname, buffer, start, - edge, idxmap, startscanline, stopscanline); - } - if (strcmp(fieldname,"Latitude") == 0) - { - status = SWscan2longlat(swathID, fieldname, buffer, start, - edge, idxmap, startscanline, stopscanline); - } - } - } - } - else if(vfound == 1) /* Vertical subsetting */ - { /* found previously, */ - status = SWreadfield(swathID, fieldname, /* perform the vertical*/ - start, NULL, edge, /* subsetting. */ - (uint8 *) buffer); /* -- xhua */ - } - else - { - /* Mapping not found */ - /* ----------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); - HEreport("Mapping Not Defined for \"%s\" Dimension.\n", - geodim); - } - } - else - { - /* Read Data (Vert SS only) */ - /* ------------------------ */ - status = SWreadfield(swathID, fieldname, - start, NULL, edge, - (uint8 *) buffer); - } - } - } + /* Call "SWinqfields" routine */ + /* -------------------------- */ + nflds = SWinqfields(swathID, "Data Fields", fieldlist, rank, + numbertype); - /* Free l_index mappings if applicable */ - /* --------------------------------- */ - if (idxmap != NULL) - { - free(idxmap); - } - if(dfieldlist != NULL) - free(dfieldlist); + return (nflds); - return (status); } + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWscan2longlat | +| FUNCTION: SWnentries | | | -| DESCRIPTION: Convert scanline to Long/Lat for floating scene subsetting. | -| This will calculate/interpolate the long/lat for a given | -| scanline. | +| DESCRIPTION: Returns number of entries and string buffer size | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nEntries int32 Number of entries | | | | INPUTS: | -| swathID int32 Swath structure ID | -| fieldname char Fieldname | -| buffer void Values to update | -| start int32 | -| edge int32 | -| idxmap int32 * Buffer of l_index mapping values | -| startscanline int32 Start of scan region | -| stopscanline int32 Stop of scan region | +| swathID int32 swath structure ID | +| entrycode int32 Entry code | +| HDFE_NENTDIM (0) | +| HDFE_NENTMAP (1) | +| HDFE_NENTIMAP (2) | +| HDFE_NENTGFLD (3) | +| HDFE_NENTDFLD (4) | +| | | | | OUTPUTS: | +| strbufsize int32 Length of comma-separated list | +| (Does not include null-terminator | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Jul 99 DaW Original Programmer | +| Jun 96 Joel Gales Original Programmer | +| Aug 96 Joel Gales Make metadata ODL compliant | +| Feb 97 Joel Gales Set nEntries to -1 if status = -1 | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -static intn -SWscan2longlat(int32 swathID, const char *fieldname, VOIDP buffer, int32 start[], -int32 edge[], int32 *idxmap, int32 startscanline, int32 stopscanline) +int32 +SWnentries(int32 swathID, int32 entrycode, int32 * strbufsize) + { + intn status; /* routine return status variable */ + intn i; /* Loop index */ + + int32 fid; /* HDF-EOS file ID */ + int32 sdInterfaceID; /* HDF SDS interface ID */ + int32 swVgrpID; /* Swath root Vgroup ID */ + int32 idOffset = SWIDOFFSET; /* Swath ID offset */ + int32 nEntries = 0; /* Number of entries */ + int32 metaflag; /* Old (0), New (1) metadata flag) */ + int32 nVal = 0; /* Number of strings to search for */ + + char *metabuf = NULL; /* Pointer to structural metadata (SM) */ + char *metaptrs[2]; /* Pointers to begin and end of SM section */ + char swathname[80]; /* Swath Name */ + char *utlstr; /* Utility string */ + char valName[2][32]; /* Strings to search for */ + + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) + { + HEpush(DFE_NOSPACE,"SWnemtries", __FILE__, __LINE__); + return(-1); + } + /* Check for valid swath id */ + /* ------------------------ */ + status = SWchkswid(swathID, "SWnentries", &fid, &sdInterfaceID, &swVgrpID); + + if (status == 0) + { + /* Get swath name */ + int sID = swathID % idOffset; + if (sID >= NSWATH) + { + free(utlstr); + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); + + /* Zero out string buffer size */ + *strbufsize = 0; - enum corner {UL, UR, LL, LR}; - enum corner pos = UL; - enum corner pos2 = UL; - uint8 scene_cnt = 0; /* Used to convert scan to scanline */ - /* L7 float scene sub. */ - float32 *buffer2; - float32 *bufferc; - float32 deg2rad = (float32)(M_PI/180.00); + /* + * Get pointer to relevant section within SM and Get names of + * metadata strings to inquire about + */ + switch (entrycode) + { + case HDFE_NENTDIM: + /* Dimensions */ + { + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "Dimension", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } + nVal = 1; + strcpy(&valName[0][0], "DimensionName"); + } + break; - float32 p1_long = 0.0; /* point 1, longitude */ - float32 p2_long = 0.0; /* point 2, longitude */ - float32 pi_long = 0.0; /* interpolated point, longitude */ - int32 scanline_p1 = 0; + case HDFE_NENTMAP: + /* Dimension Maps */ + { + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "DimensionMap", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } + nVal = 2; + strcpy(&valName[0][0], "GeoDimension"); + strcpy(&valName[1][0], "DataDimension"); + } + break; - float32 p1_lat = 0.0; /* point 1, latitude */ - float32 p2_lat = 0.0; /* point 2, latitude */ - float32 pi_lat = 0.0; /* interpolated point, latitude */ - int32 scanline_p2 = 0; + case HDFE_NENTIMAP: + /* Indexed Dimension Maps */ + { + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "IndexDimensionMap", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } + nVal = 2; + strcpy(&valName[0][0], "GeoDimension"); + strcpy(&valName[1][0], "DataDimension"); + } + break; + case HDFE_NENTGFLD: + /* Geolocation Fields */ + { + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "GeoField", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } + nVal = 1; + strcpy(&valName[0][0], "GeoFieldName"); + } + break; - float32 x_p1 = 0.0; /* Cartesian coordinates */ - float32 y_p1 = 0.0; /* point 1 */ - float32 z_p1 = 0.0; + case HDFE_NENTDFLD: + /* Data Fields */ + { + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "DataField", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } + nVal = 1; + strcpy(&valName[0][0], "DataFieldName"); + } + break; + } - float32 x_p2 = 0.0; /* Cartesian coordinates */ - float32 y_p2 = 0.0; /* point 2 */ - float32 z_p2 = 0.0; - float32 x_pi = 0.0; /* Cartesian coordinates */ - float32 y_pi = 0.0; /* interpolated point */ - float32 z_pi = 0.0; - int32 scanline_pi = 0; + /* + * Check for presence of 'GROUP="' string If found then old metadata, + * search on OBJECT string + */ + if (metabuf) + { + metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0; + if (metaflag == 0) + { + nVal = 1; + strcpy(&valName[0][0], "\t\tOBJECT"); + } - intn status = -1; - int i = 0; - int p1_long_l90_flag = 0; - int p1_long_g90_flag = 0; - int p2_long_l90_flag = 0; - int p2_long_g90_flag = 0; - int fieldflag = 0; + /* Begin loop through entries in metadata */ + /* -------------------------------------- */ + while (1) + { + /* Search for first string */ + strcpy(utlstr, &valName[0][0]); + strcat(utlstr, "="); + metaptrs[0] = strstr(metaptrs[0], utlstr); - int numofval = 0; + /* If found within relevant metadata section ... */ + if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL) + { + for (i = 0; i < nVal; i++) + { + /* + * Get all string values Don't count quotes + */ + EHgetmetavalue(metaptrs, &valName[i][0], utlstr); + *strbufsize += (int32)strlen(utlstr) - 2; + } + /* Increment number of entries */ + nEntries++; + /* Go to end of OBJECT */ + metaptrs[0] = strstr(metaptrs[0], "END_OBJECT"); + } + else + /* No more entries found */ + { + break; + } + } + free(metabuf); + } - numofval = edge[0] * 2; + /* Count comma separators & slashes (if mappings) */ + /* ---------------------------------------------- */ + if (nEntries > 0) + { + *strbufsize += nEntries - 1; + *strbufsize += (nVal - 1) * nEntries; + } + } - buffer2 = (float32 *)calloc(numofval, sizeof(float32)); - bufferc = (float32 *)calloc(numofval, sizeof(float32)); - memmove(bufferc, buffer, numofval*sizeof(float32)); + /* Set nEntries to -1 if error status exists */ + /* ----------------------------------------- */ + if (status == -1) + nEntries = -1; - (void) SWreadattr(swathID, "scene_count", &scene_cnt); + free(utlstr); - if (strcmp(fieldname, "Longitude") == 0) - { - fieldflag = 1; - status = SWreadfield(swathID, "Latitude", start, NULL, edge, buffer2); - } - else if (strcmp(fieldname, "Latitude") == 0) - { - fieldflag = 2; - status = SWreadfield(swathID, "Longitude", start, NULL, edge, buffer2); - } + return (nEntries); +} - for(i=0; i<4; i++) - { - switch(pos) - { - case UL: - if (fieldflag == 1) - { - p1_long = bufferc[0]; - p2_long = bufferc[2]; - p1_lat = buffer2[0]; - p2_lat = buffer2[2]; - } - if (fieldflag == 2) - { - p1_long = buffer2[0]; - p2_long = buffer2[2]; - p1_lat = bufferc[0]; - p2_lat = bufferc[2]; - } - scanline_p1 = idxmap[start[0]]; - scanline_p2 = idxmap[start[0]+1]; - scanline_pi = startscanline; - pos = UR; - break; - case UR: - if (fieldflag == 1) - { - p1_long = bufferc[1]; - p2_long = bufferc[3]; - p1_lat = buffer2[1]; - p2_lat = buffer2[3]; - } - if (fieldflag == 2) - { - p1_long = buffer2[1]; - p2_long = buffer2[3]; - p1_lat = bufferc[1]; - p2_lat = bufferc[3]; - } - scanline_p1 = idxmap[start[0]]; - scanline_p2 = idxmap[start[0]+1]; - scanline_pi = startscanline; - pos = LL; - break; - case LL: - if (fieldflag == 1) - { - p1_long = bufferc[numofval-4]; - p2_long = bufferc[numofval-2]; - p1_lat = buffer2[numofval-4]; - p2_lat = buffer2[numofval-2]; - } - if (fieldflag == 2) - { - p1_long = buffer2[numofval-4]; - p2_long = buffer2[numofval-2]; - p1_lat = bufferc[numofval-4]; - p2_lat = bufferc[numofval-2]; - } - scanline_p1 = idxmap[start[0] + edge[0] - 2]; - scanline_p2 = idxmap[start[0] + edge[0] - 1]; - scanline_pi = stopscanline; - pos = LR; - break; - case LR: - if (fieldflag == 1) - { - p1_long = bufferc[numofval-3]; - p2_long = bufferc[numofval-1]; - p1_lat = buffer2[numofval-3]; - p2_lat = buffer2[numofval-1]; - } - if (fieldflag == 2) - { - p1_long = buffer2[numofval-3]; - p2_long = buffer2[numofval-1]; - p1_lat = bufferc[numofval-3]; - p2_lat = bufferc[numofval-1]; - } - scanline_p1 = idxmap[start[0] + edge[0] - 2]; - scanline_p2 = idxmap[start[0] + edge[0] - 1]; - scanline_pi = stopscanline; - break; - } - - - - if (p1_long <= -90.0) - { - if (p2_long >= 90.0) - { - p1_long = p1_long + 180.0f; - p2_long = p2_long - 180.0f; - p1_long_l90_flag = 2; - } - else - { - p1_long = p1_long + 180.0f; - p1_long_l90_flag = 1; - } - } - if (p1_long >= 90.0 && p1_long_l90_flag != 2) - { - if(p2_long <= -90.0) - { - p1_long = p1_long - 180.0f; - p2_long = p2_long + 180.0f; - p1_long_g90_flag = 2; - } - else - { - p1_long = p1_long - 90.0f; - p1_long_g90_flag = 1; - } - } - if (p2_long <= -90.0) - { - if (p1_long < 0.0) - { - p2_long = p2_long + 90.0f; - p1_long = p1_long + 90.0f; - p2_long_l90_flag = 2; - } - else - { - p2_long = p2_long + 180.0f; - p2_long_l90_flag = 1; - } - } - if (p2_long >= 90.0 && p1_long_l90_flag != 2) - { - p2_long = p2_long - 90.0f; - p2_long_g90_flag = 1; - } - - - x_p1 = (float)(RADOE * cos((p1_long*deg2rad)) * sin((p1_lat*deg2rad))); - y_p1 = (float)(RADOE * sin((p1_long*deg2rad)) * sin((p1_lat*deg2rad))); - z_p1 = (float)(RADOE * cos((p1_lat*deg2rad))); - - - x_p2 = (float)(RADOE * cos((p2_long*deg2rad)) * sin((p2_lat*deg2rad))); - y_p2 = (float)(RADOE * sin((p2_long*deg2rad)) * sin((p2_lat*deg2rad))); - z_p2 = (float)(RADOE * cos((p2_lat*deg2rad))); - - x_pi = x_p1 + (x_p2 - x_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); - y_pi = y_p1 + (y_p2 - y_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); - z_pi = z_p1 + (z_p2 - z_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); - - if (fieldflag == 1) - { - pi_long = (float)(atan(y_pi/x_pi)*180.0/M_PI); - if (p1_long_l90_flag == 1 || p2_long_l90_flag == 1) - { - pi_long = pi_long - 180.0f; - p1_long_l90_flag = 0; - p2_long_l90_flag = 0; - } - if (p1_long_g90_flag == 1 || p2_long_g90_flag == 1) - { - pi_long = pi_long + 90.0f; - p1_long_g90_flag = 0; - p2_long_g90_flag = 0; - } - if (p1_long_l90_flag == 2) - { - if (pi_long > 0.0) - pi_long = pi_long - 180.0f; - else if (pi_long < 0.0) - pi_long = pi_long + 180.0f; - p1_long_l90_flag = 0; - } - if (p1_long_g90_flag == 2) - { - if (pi_long > 0.0) - pi_long = pi_long - 180.0f; - else if (pi_long < 0.0) - pi_long = pi_long + 180.0f; - p1_long_g90_flag = 0; - } - if (p2_long_l90_flag == 2) - { - pi_long = pi_long - 90.0f; - p2_long_l90_flag = 0; - } - - - - switch(pos2) - { - case UL: - bufferc[0] = pi_long; - pos2 = UR; - break; - case UR: - bufferc[1] = pi_long; - pos2 = LL; - break; - case LL: - if(stopscanline > idxmap[scene_cnt*2 - 1]) - break; - bufferc[numofval-2] = pi_long; - pos2 = LR; - break; - case LR: - if(stopscanline > idxmap[scene_cnt*2 - 1]) - break; - bufferc[numofval-1] = pi_long; - break; - } - } - if (fieldflag == 2) - { - pi_lat = (float)(atan((sqrt(x_pi*x_pi + y_pi*y_pi)/z_pi))*180.0/M_PI); - switch(pos2) - { - case UL: - bufferc[0] = pi_lat; - pos2 = UR; - break; - case UR: - bufferc[1] = pi_lat; - pos2 = LL; - break; - case LL: - if(stopscanline > idxmap[scene_cnt*2 - 1]) - break; - bufferc[numofval-2] = pi_lat; - pos2 = LR; - break; - case LR: - if(stopscanline > idxmap[scene_cnt*2 - 1]) - break; - bufferc[numofval-1] = pi_lat; - break; - } - } - } - memmove(buffer, bufferc, numofval*sizeof(float32)); - free(buffer2); - free(bufferc); - return(status); -} /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWextractperiod | +| FUNCTION: SWinqswath | | | -| DESCRIPTION: Retrieves data from specified period. | +| DESCRIPTION: Returns number and names of swath structures in file | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | +| nSwath int32 Number of swath structures in file | | | | INPUTS: | -| swathID int32 Swath structure ID | -| periodID int32 Period ID | -| fieldname char Fieldname | -| externalflag int32 External geolocation fields flag | -| HDFE_INTERNAL (0) | -| HDFE_EXTERNAL (1) | +| filename char HDF-EOS filename | | | | OUTPUTS: | -| buffer void Data buffer containing subsetted region | +| swathlist char List of swath names (comma-separated) | +| strbufsize int32 Length of swathlist | | | | NOTES: | | | @@ -8153,109 +2841,119 @@ int32 edge[], int32 *idxmap, int32 startscanline, int32 stopscanline) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Jun 03 Abe Taaheri added clearing timeflag if SWextractregion fails | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -SWextractperiod(int32 swathID, int32 periodID, const char *fieldname, - int32 externalflag, VOIDP buffer) - +int32 +SWinqswath(const char *filename, char *swathlist, int32 * strbufsize) { - intn status; /* routine return status variable */ + int32 nSwath; /* Number of swath structures in file */ - timeflag = 1; + /* Call "EHinquire" routine */ + /* ------------------------ */ + nSwath = EHinquire(filename, "SWATH", swathlist, strbufsize); - /* Call SWextractregion routine */ - /* ---------------------------- */ - status = SWextractregion(swathID, periodID, fieldname, externalflag, - (char *) buffer); - if (status != 0) timeflag = 0; /*clear timeflag if SWextractregion failed*/ - return (status); + return (nSwath); } -#endif - - -#ifdef UNUSED_BY_GDAL /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWdupregion | +| FUNCTION: SW1dfldsrch | | | -| DESCRIPTION: Duplicates a region | +| DESCRIPTION: Retrieves information about a 1D field | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| newregionID int32 New region ID | +| status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| oldregionID int32 Old region ID | +| fid int32 HDF-EOS file ID | +| swathID int32 swath structure ID | +| fieldname const char field name | +| access const char Access code (w/r) | +| | | | | OUTPUTS: | -| None | +| vgidout int32 Field (geo/data) vgroup ID | +| vdataIDout int32 Field Vdata ID | +| fldtype int32 Field type | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Jan 97 Joel Gales Original Programmer | +| Jun 96 Joel Gales Original Programmer | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -int32 -SWdupregion(int32 oldregionID) +static intn +SW1dfldsrch(int32 fid, int32 swathID, const char *fieldname, const char *i_access, + int32 * vgidout, int32 * vdataIDout, int32 * fldtype) + { - intn i; /* Loop index */ + intn status = 0; /* routine return status variable */ - int32 newregionID = -1; /* New region ID */ + int32 sID; /* SwathID - offset */ + int32 vgid; /* Swath Geo or Data Vgroup ID */ + int32 vdataID; /* 1d field vdata */ + int32 idOffset = SWIDOFFSET; /* Swath ID offset */ - /* Find first empty (inactive) region */ - /* ---------------------------------- */ - for (i = 0; i < NSWATHREGN; i++) + /* Compute "reduced" swath ID */ + /* -------------------------- */ + sID = swathID % idOffset; + if (sID >= NSWATH) { - if (SWXRegion[i] == 0) - { - /* Allocate space for new swath region entry */ - /* ----------------------------------------- */ - SWXRegion[i] = (struct swathRegion *) - calloc(1, sizeof(struct swathRegion)); - if(SWXRegion[i] == NULL) - { - HEpush(DFE_NOSPACE,"SWdupregion", __FILE__, __LINE__); - return(-1); - } + return -1; + } - /* Copy old region structure data to new region */ - /* -------------------------------------------- */ - *SWXRegion[i] = *SWXRegion[oldregionID]; + /* Get Geolocation Vgroup id and 1D field name Vdata id */ + /* ---------------------------------------------------- */ + vgid = SWXSwath[sID].VIDTable[0]; + vdataID = EHgetid(fid, vgid, fieldname, 1, i_access); + *fldtype = 0; - /* Define new region ID */ - /* -------------------- */ - newregionID = i; + /* + * If name not found in Geolocation Vgroup then detach Geolocation Vgroup + * and search in Data Vgroup + */ + if (vdataID == -1) + { + vgid = SWXSwath[sID].VIDTable[1];; + vdataID = EHgetid(fid, vgid, fieldname, 1, i_access); + *fldtype = 1; - break; + /* If field also not found in Data Vgroup then set error status */ + /* ------------------------------------------------------------ */ + if (vdataID == -1) + { + status = -1; + vgid = -1; + vdataID = -1; } } + *vgidout = vgid; + *vdataIDout = vdataID; - return (newregionID); + return (status); } -#endif -#ifdef UNUSED_BY_GDAL + + + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWregioninfo | +| FUNCTION: SWSDfldsrch | | | -| DESCRIPTION: Returns size of region in bytes | +| DESCRIPTION: Retrieves information SDS field | | | | | | Return Value Type Units Description | @@ -8263,16 +2961,18 @@ SWdupregion(int32 oldregionID) | status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| swathID int32 Swath structure ID | -| regionID int32 Region ID | -| fieldname char Fieldname | +| swathID int32 swath structure ID | +| sdInterfaceID int32 SD interface ID | +| fieldname const char field name | | | | | | OUTPUTS: | -| ntype int32 field number type | -| rank int32 field rank | -| dims int32 dimensions of field region | -| size int32 size in bytes of field region | +| sdid int32 SD element ID | +| rankSDS int32 Rank of SDS | +| rankFld int32 True rank of field (merging) | +| offset int32 Offset of field within merged field | +| dims int32 Dimensions of field | +| solo int32 Solo field flag | | | | NOTES: | | | @@ -8280,691 +2980,194 @@ SWdupregion(int32 oldregionID) | Date Programmer Description | | ====== ============ ================================================= | | Jun 96 Joel Gales Original Programmer | -| Aug 96 Joel Gales Add vertical subsetting | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| Mar 97 Joel Gales Add support for l_index mapping | -| Jul 99 DaW Add support for floating scene subsetting | +| Aug 96 Joel Gales Make metadata ODL compliant | | | | END_PROLOG | -----------------------------------------------------------------------------*/ -intn -SWregioninfo(int32 swathID, int32 regionID, const char *fieldname, - int32 * ntype, int32 * rank, int32 dims[], int32 * size) - +static intn +SWSDfldsrch(int32 swathID, int32 sdInterfaceID, const char *fieldname, + int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset, + int32 dims[], int32 * solo) { intn i; /* Loop index */ - intn j; /* Loop index */ - intn k; /* Loop index */ - intn l = 0; /* Loop index */ - intn status; /* routine return status variable */ - intn long_status = 3; /* routine return status variable for longitude */ - intn land_status = 3; /* Used for L7 float scene sub. */ - intn statMap = -1; /* Status from SWmapinfo */ - - uint8 found = 0; /* Found flag */ - uint8 detect_cnt = 0; /* Used for L7 float scene sub. */ - - int32 numtype = 0; /* Used for L7 float scene sub. */ - int32 count = 0; /* Used for L7 float scene sub. */ - - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ + intn status = -1;/* routine return status variable */ - int32 l_index = 0; /* Geo Dim Index */ - int32 nDim; /* Number of dimensions */ - int32 slen[64]; /* String length array */ + int32 sID; /* SwathID - offset */ + int32 idOffset = SWIDOFFSET; /* Swath ID offset */ int32 dum; /* Dummy variable */ - int32 incr = 0; /* Mapping increment */ - int32 nXtrk = 0; /* Number of cross tracks */ - int32 scan_shift = 0; /* Used to take out partial scans */ - int32 startscandim = -1; /* Used for floating scene region size */ - int32 stopscandim = -1; /* Used for floating scene region size */ - int32 dumdims[8]; /* Dimensions from SWfieldinfo */ - int32 idxMapElem = -1; /* Number of l_index map elements */ - int32 *idxmap = NULL; /* Pointer to l_index mapping array */ - int32 datafld = 0; - - uint8 scene_cnt = 0; /* Number of scenes in swath */ - int32 startscanline = 0; - int32 stopscanline = 0; - char *dfieldlist = (char *)NULL; - int32 strbufsize = 0; - int32 dfrank[8]; - int32 numtype2[8]; - int32 rank2 = 0; - int32 rk = 0; - int32 dims2[8]; - int32 nt = 0; - uint16 *buffer2 = (uint16 *)NULL; - uint16 *tbuffer = (uint16 *)NULL; - - char dimlist[256]; /* Dimension list */ - char geodim[256];/* Geolocation field dimension list */ - char tgeodim[256];/* Time Geolocation field dimension list */ - char dgeodim[256];/* Data Subsetting field dimension list */ - char utlbuf[256];/* Utility buffer */ - char *ptr[64]; /* String pointer array */ - static const char errMesg[] = "Vertical Dimension Not Found: \"%s\".\n"; - - - - /* Set region size to -1 */ - /* --------------------- */ - *size = -1; - - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWregioninfo", &fid, &sdInterfaceID, - &swVgrpID); - - - /* Check for valid region ID */ - /* ------------------------- */ - if (status == 0) - { - if (regionID < 0 || regionID >= NSWATHREGN) - { - status = -1; - HEpush(DFE_RANGE, "SWregioninfo", __FILE__, __LINE__); - HEreport("Invalid Region id: %d.\n", regionID); - } - } + int32 dums[128]; /* Dummy array */ + int32 attrIndex; /* Attribute l_index */ + char name[2048]; /* Merged-Field Names */ + char swathname[80]; /* Swath Name */ + char *utlstr; /* Utility string */ + char *metabuf; /* Pointer to structural metadata (SM) */ + char *metaptrs[2];/* Pointers to begin and end of SM section */ + char *oldmetaptr; /* Pointer within SM section */ - /* Check for active region ID */ - /* -------------------------- */ - if (status == 0) + /* Allocate space for utility string */ + /* --------------------------------- */ + utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char)); + if(utlstr == NULL) { - if (SWXRegion[regionID] == 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__); - HEreport("Inactive Region ID: %d.\n", regionID); - } + HEpush(DFE_NOSPACE,"SWSDfldsrch", __FILE__, __LINE__); + return(-1); } - - /* This code checks for the attribute detector_count */ - /* which is found in Landsat 7 files. It is used */ - /* for some of the loops. */ - /* ================================================= */ - if (status == 0 && SWXRegion[regionID]->scanflag == 1) - { - land_status = SWattrinfo(swathID, "detector_count", &numtype, &count); - if (land_status == 0) - { - land_status = SWreadattr(swathID, "detector_count", &detect_cnt); - land_status = SWreadattr(swathID, "scene_count", &scene_cnt); - - } - } - - + /* Set solo flag to 0 (no) */ + /* ----------------------- */ + *solo = 0; - /* Check for valid fieldname */ - /* ------------------------- */ - if (status == 0) + /* Compute "reduced" swath ID */ + /* -------------------------- */ + sID = swathID % idOffset; + if (sID >= NSWATH) { - /* Get data field info */ - /* ------------------- */ - status = SWfieldinfo(swathID, fieldname, rank, - dims, ntype, dimlist); - - if (status != 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__); - HEreport("Field \"%s\" Not Found.\n", fieldname); - } + return -1; } - - - /* No problems so proceed ... */ - /* -------------------------- */ - if (status == 0) + /* Loop through all SDSs in swath */ + /* ------------------------------ */ + for (i = 0; i < SWXSwath[sID].nSDS; i++) { - /* If non-vertical subset regions defined ... */ - /* ------------------------------------------ */ - if (SWXRegion[regionID]->nRegions > 0 || SWXRegion[regionID]->scanflag == 1) + /* If active SDS ... */ + /* ----------------- */ + if (SWXSwath[sID].sdsID[i] != 0) { + /* Get SDS ID, name, rankSDS, and dimensions */ + /* ----------------------------------------- */ + *sdid = SWXSwath[sID].sdsID[i]; + SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum); + *rankFld = *rankSDS; - /* Get geolocation dimension name */ - /* ------------------------------ */ - status = SWfieldinfo(SWXRegion[regionID]->swathID, - "Longitude", &dum, - dumdims, &dum, geodim); - long_status = status; + /* If merged field ... */ + /* ------------------- */ + if (strstr(name, "MRGFLD_") == &name[0]) + { + /* Get swath name */ + /* -------------- */ + Vgetname(SWXSwath[sID].IDTable, swathname); - /* If Time field being used, check for dimensions */ - /* ---------------------------------------------- */ - if (timeflag == 1) - { - /* code change to fix time subset bug for Landsat7 */ - status = SWfieldinfo(SWXRegion[regionID]->swathID, - "Time", &dum, - dumdims, &dum, tgeodim); + /* Get pointers to "MergedFields" section within SM */ + /* ------------------------------------------------ */ + metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s", + "MergedFields", metaptrs); + if(metabuf == NULL) + { + free(utlstr); + return(-1); + } - if (strcmp(geodim, tgeodim) != 0) - { - strcpy(geodim, tgeodim); - } - timeflag = 0; - } + /* Store metaptr in order to recover */ + /* --------------------------------- */ + oldmetaptr = metaptrs[0]; - /* If defscanregion being used, get dimensions */ - /* of field being used */ - /* ---------------------------------------------- */ - if (SWXRegion[regionID]->scanflag == 1) - { - (void) SWnentries(SWXRegion[regionID]->swathID,4,&strbufsize); - dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char)); - (void) SWinqdatafields(SWXRegion[regionID]->swathID,dfieldlist,dfrank,numtype2); - status = SWfieldinfo(SWXRegion[regionID]->swathID,dfieldlist,&dum,dumdims,&dum,dgeodim); - - /* The dimensions have to be switched, because */ - /* the mappings force a geodim and datadim */ - /* so to find the mapping, the dimensions must */ - /* be switched, but the subsetting will still */ - /* be based on the correct dimensions */ - /* "long_status == -1" added for CAL file which */ - /* doesn't have a Traditional geolocation field */ - /* ---------------------------------------------- */ - if (strcmp(dgeodim,dimlist) != 0 || long_status == -1) - { - strcpy(geodim,dimlist); - strcpy(dimlist,dgeodim); - } - } + /* Search for Merged field name */ + /* ---------------------------- */ + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "MergedFieldName=\"", + name, "\"\n"); + metaptrs[0] = strstr(metaptrs[0], utlstr); - /* Get "Track" (first) Dimension from geo dimlist */ - /* ---------------------------------------------- */ - nDim = EHparsestr(geodim, ',', ptr, slen); - geodim[slen[0]] = 0; + /* If not found check for old metadata */ + /* ----------------------------------- */ + if (metaptrs[0] == NULL) + { + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "OBJECT=\"", name, "\"\n"); + metaptrs[0] = strstr(oldmetaptr, utlstr); + } - /* Parse Data Field Dimlist & find mapping */ - /* --------------------------------------- */ - nDim = EHparsestr(dimlist, ',', ptr, slen); + /* Get field list and strip off leading and trailing quotes */ + EHgetmetavalue(metaptrs, "FieldList", name); /* not return status --xhua */ + memmove(name, name + 1, strlen(name) - 2); + name[strlen(name) - 2] = 0; - /* Loop through all dimensions and search for mapping */ - /* -------------------------------------------------- */ - for (i = 0; i < nDim; i++) - { - memcpy(utlbuf, ptr[i], slen[i]); - utlbuf[slen[i]] = 0; - statMap = SWmapinfo(swathID, geodim, utlbuf, - &dum, &incr); + /* Search for desired field within merged field list */ + snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\""); + dum = EHstrwithin(utlstr, name, ','); - /* Mapping found */ - /* ------------- */ - if (statMap == 0) - { - found = 1; - l_index = i; - break; - } + free(metabuf); } - - - /* If mapping not found check for geodim within dimlist */ - /* ---------------------------------------------------- */ - if (found == 0) + else { - l_index = EHstrwithin(geodim, dimlist, ','); - - /* Geo dimension found within subset field dimlist */ - /* ----------------------------------------------- */ - if (l_index != -1) + /* If solo (unmerged) check if SDS name matches fieldname */ + /* ------------------------------------------------------ */ + dum = EHstrwithin(fieldname, name, ','); + if (dum != -1) { - found = 1; - incr = 1; + *solo = 1; + *offset = 0; } } - - /* If mapping not found check for l_indexed mapping */ - /* ---------------------------------------------- */ - if (found == 0) + /* If field found ... */ + /* ------------------ */ + if (dum != -1) { - /* Get size of geo dim & allocate space of l_index mapping */ - /* ----------------------------------------------------- */ - dum = SWdiminfo(swathID, geodim); - if (dum < 0) - return -1; - idxmap = (int32 *) calloc(dum, sizeof(int32)); - if(idxmap == NULL) - { - HEpush(DFE_NOSPACE,"SWregioninfo", __FILE__, __LINE__); - return(-1); - } + status = 0; - /* Loop through all dimensions and search for mapping */ - /* -------------------------------------------------- */ - for (i = 0; i < nDim; i++) + /* If merged field ... */ + /* ------------------- */ + if (*solo == 0) { - memcpy(utlbuf, ptr[i], slen[i]); - utlbuf[slen[i]] = 0; - - idxMapElem = SWidxmapinfo(swathID, geodim, utlbuf, idxmap); - + /* Get "Field Offsets" SDS attribute l_index */ + /* --------------------------------------- */ + attrIndex = SDfindattr(*sdid, "Field Offsets"); - /* Mapping found */ - /* ------------- */ - if (idxMapElem != -1) + /* + * If attribute exists then get offset of desired field + * within merged field + */ + if (attrIndex != -1) { - found = 1; - l_index = i; - break; + SDreadattr(*sdid, attrIndex, (VOIDP) dums); + *offset = dums[dum]; } - } - } - - - /* Regular Mapping Found */ - /* --------------------- */ - if (found == 1 && idxMapElem == -1) - { - dims[l_index] = 0; - /* Loop through all regions */ - /* ------------------------ */ - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - /* Get number of cross tracks in particular region */ - /* ----------------------------------------------- */ - nXtrk = SWXRegion[regionID]->StopRegion[k] - - SWXRegion[regionID]->StartRegion[k] + 1; + /* Get "Field Dims" SDS attribute l_index */ + /* ------------------------------------ */ + attrIndex = SDfindattr(*sdid, "Field Dims"); - /* If increment is positive (geodim <= datadim) ... */ - /* ------------------------------------------------ */ - if (incr > 0) - { - if (SWXRegion[regionID]->scanflag == 1) - { - scan_shift = nXtrk % incr; - if(scan_shift != 0) - nXtrk = nXtrk - scan_shift; - dims[l_index] += nXtrk/incr; - if(long_status == -1 || incr == 1) - { - if( detect_cnt == 0 ) - return -1; - scan_shift = nXtrk % detect_cnt; - if(scan_shift != 0) - dims[l_index] = nXtrk - scan_shift; - } - } - else - { - dims[l_index] += nXtrk * incr; - } - } - else + /* + * If attribute exists then get 0th dimension of desired + * field within merged field + */ + if (attrIndex != -1) { - /* Negative increment (geodim > datadim) */ - /* ------------------------------------- */ - dims[l_index] += nXtrk / (-incr); + SDreadattr(*sdid, attrIndex, (VOIDP) dums); + dims[0] = dums[dum]; - /* - * If Xtrk not exactly divisible by incr, round dims - * to next highest integer - */ - if (nXtrk % (-incr) != 0) + /* If this dimension = 1 then field is really 2 dim */ + /* ------------------------------------------------ */ + if (dums[dum] == 1) { - dims[l_index]++; + *rankFld = 2; } } } - } - else if (found == 1 && idxMapElem != -1) - { - /* Indexed Mapping */ - /* --------------- */ - dims[l_index] = 0; - - /* Loop through all regions */ - /* ------------------------ */ - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - j = 0; - if(SWXRegion[regionID]->scanflag == 1) - { - startscanline = SWXRegion[regionID]->StartRegion[k]; - stopscanline = SWXRegion[regionID]->StopRegion[k]; - if (strcmp(fieldname,dfieldlist) == 0) - { - dims[l_index] = stopscanline - startscanline + 1; - datafld = 1; - } - if (strcmp(fieldname, "Latitude") == 0 || - strcmp(fieldname, "Longitude") == 0) - { - if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3) - { - status = SWfieldinfo(swathID,"scan_no",&rk,dims2,&nt,dimlist); - tbuffer = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status =SWreadfield(swathID,"scan_no",NULL,NULL,NULL,tbuffer); - startscanline = startscanline - ((tbuffer[0] * detect_cnt) - detect_cnt); - stopscanline = stopscanline - ((tbuffer[0] * detect_cnt) - 1); - } - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - if(startscandim == -1) - if(startscanline < idxmap[0]) - { - startscandim = 0; - dims[l_index] = 0; - if(stopscanline > idxmap[scene_cnt *2 - 1]) - { - stopscandim = scene_cnt*2 - startscandim; - dims[l_index] = scene_cnt*2 - startscandim; - } - } - } - for (l = 0; l < scene_cnt; l++) - { - if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline) - if(startscandim == -1) - { - dims[l_index] = j; - startscandim = j; - } - if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline) - if(startscandim != -1) - { - dims[l_index] = j - startscandim + 2; - stopscandim = j + 1; - } - j = j + 2; - if(idxmap[j] == 0 || idxmap[j+1] == 0) - l = scene_cnt; - } - if(SWXRegion[regionID]->band8flag == 1 || - SWXRegion[regionID]->band8flag == 2) - { - if(stopscandim == -1) - if(stopscanline > idxmap[scene_cnt * 2 - 1]) - { - stopscandim = scene_cnt*2 - dims[l_index]; - dims[l_index] = scene_cnt*2 - dims[l_index]; - } - } - if(SWXRegion[regionID]->band8flag == 3) - { - if(startscandim == -1) - if(startscanline < idxmap[0]) - { - startscandim = 0; - if(stopscandim != -1) - dims[l_index] = stopscandim - startscandim + 1; - } - } - if(SWXRegion[regionID]->band8flag == 2) - { - if(startscandim == -1) - if(startscanline > idxmap[j - 1]) - { - status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank2,dims2,&nt,dimlist); - buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2); - startscanline = startscanline - (buffer2[0] * detect_cnt); - stopscanline = stopscanline - (buffer2[0] * detect_cnt); - free(buffer2); - j = 0; - for (l = 0; l < scene_cnt; l++) - { - if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline) - { - dims[l_index] = j; - startscandim = j; - } - if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline) - dims[l_index] = j - startscandim + 2; - j = j + 2; - if(idxmap[j] == 0 || idxmap[j+1] == 0) - l = scene_cnt; - } - - } - } - } - if (strcmp(fieldname, "scene_center_latitude") == 0 || - strcmp(fieldname, "scene_center_longitude") == 0) - { - startscanline = SWXRegion[regionID]->StartRegion[k]; - stopscanline = SWXRegion[regionID]->StopRegion[k]; - if(startscanline < idxmap[0]) - { - startscandim = 0; - dims[l_index] = 0; - } - for (l = 0; l < scene_cnt-1; l++) - { - if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline) - if(startscandim == -1) - { - dims[l_index] = l; - startscandim = l; - } - if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline) - { - dims[l_index] = l - startscandim + 2; - stopscandim = l + 1; - } - } - if(stopscandim == -1) - { - if(stopscanline > idxmap[scene_cnt - 1]) - { - dims[l_index] = scene_cnt - startscandim; - stopscandim = scene_cnt - 1; - } - } - if(SWXRegion[regionID]->band8flag == 1) - { - if(stopscandim == -1) - if(stopscanline > idxmap[scene_cnt - 1]) - { - dims[l_index] = scene_cnt - startscandim; - stopscandim = scene_cnt - 1; - } - } - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - if(startscandim == -1) - { - if(startscanline < idxmap[0]) - { - startscandim = 0; - dims[l_index] = stopscandim - startscandim + 1; - } - } - if(startscandim == -1) - { - startscanline = SWXRegion[regionID]->StartScan[k] * detect_cnt; - stopscanline = SWXRegion[regionID]->StopScan[k] * detect_cnt; - for (l = 0; l < scene_cnt-1; l++) - { - if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline) - dims[l_index] = l; - if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline) - dims[l_index] = l - dims[l_index] + 1; - } - } - } - } - } - else - { - if (datafld != 1) - { - /* Get number of cross tracks in particular region */ - /* ----------------------------------------------- */ - nXtrk = idxmap[SWXRegion[regionID]->StopRegion[k]] - - idxmap[SWXRegion[regionID]->StartRegion[k]] + 1; - - dims[l_index] += nXtrk; - } - } - } - } - else - { - /* Mapping not found */ - /* ----------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWregioninfo", - __FILE__, __LINE__); - HEreport( - "Mapping Not Defined for \"%s\" Dimension.\n", - geodim); - } + /* Break out of SDS loop */ + /* --------------------- */ + break; + } /* End of found field section */ } - - - - /* Vertical Subset */ - /* --------------- */ - if (status == 0 || status == -1) /* check the vertical subset in any case -- xhua */ + else { - for (j = 0; j < 8; j++) - { - /* If active vertical subset ... */ - /* ----------------------------- */ - if (SWXRegion[regionID]->StartVertical[j] != -1) - { - - /* Find vertical dimension within dimlist */ - /* -------------------------------------- */ - l_index = EHstrwithin(SWXRegion[regionID]->DimNamePtr[j], - dimlist, ','); - - /* If dimension found ... */ - /* ---------------------- */ - if (l_index != -1) - { - /* Compute dimension size */ - /* ---------------------- */ - dims[l_index] = - SWXRegion[regionID]->StopVertical[j] - - SWXRegion[regionID]->StartVertical[j] + 1; - } - else - { - /* Vertical dimension not found */ - /* ---------------------------- */ - status = -1; - *size = -1; - HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__); - HEreport(errMesg, SWXRegion[regionID]->DimNamePtr[j]); - } - } - } - - - - /* Compute size of region data buffer */ - /* ---------------------------------- */ - if (status == 0) - { - if(idxMapElem == 1 && SWXRegion[regionID]->scanflag == 1 && land_status == 0) - { - if(startscandim == dims[0]) - dims[0] = scene_cnt*2 - startscandim; - } - - /* Compute number of total elements */ - /* -------------------------------- */ - *size = dims[0]; - for (j = 1; j < *rank; j++) - { - *size *= dims[j]; - } - - /* Multiply by size in bytes of numbertype */ - /* --------------------------------------- */ - *size *= DFKNTsize(*ntype); - } + /* First non-active SDS signifies no more, break out of SDS loop */ + /* ------------------------------------------------------------- */ + break; } } + free(utlstr); - - /* Free l_index mappings if applicable */ - /* --------------------------------- */ - if (idxmap != NULL) - { - free(idxmap); - } - if(dfieldlist != NULL) - free(dfieldlist); - - return (status); -} -#endif - -#ifdef UNUSED_BY_GDAL -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWperiodinfo | -| | -| DESCRIPTION: Returns size in bytes of region | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 Swath structure ID | -| periodID int32 Period ID | -| fieldname char Fieldname | -| | -| | -| OUTPUTS: | -| ntype int32 field number type | -| rank int32 field rank | -| dims int32 dimensions of field region | -| size int32 size in bytes of field region | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jun 96 Joel Gales Original Programmer | -| Jun 03 Abe Taaheri added clearing timeflag if SWregioninfo fails | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWperiodinfo(int32 swathID, int32 periodID, const char *fieldname, - int32 * ntype, int32 * rank, int32 dims[], int32 * size) -{ - intn status; /* routine return status variable */ - - - timeflag = 1; - /* Call SWregioninfo */ - /* ----------------- */ - status = SWregioninfo(swathID, periodID, fieldname, ntype, rank, - dims, size); - if (status != 0) timeflag = 0;/* clear timeflag if SWregioninfo failed */ return (status); } @@ -8975,879 +3178,483 @@ SWperiodinfo(int32 swathID, int32 periodID, const char *fieldname, /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWdefvrtregion | +| FUNCTION: SWwrrdfield | | | -| DESCRIPTION: Finds elements of a monotonic field within a given range. | +| DESCRIPTION: Writes/Reads fields | | | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | -| regionID int32 Region ID | +| status intn return status (0) SUCCEED, (-1) FAIL | | | | INPUTS: | -| swathID int32 Swath structure ID | -| regionID int32 Region ID | -| vertObj char Vertical object to subset | -| range float64 Vertical subsetting range | +| swathID int32 swath structure ID | +| fieldname const char fieldname | +| code const char Write/Read code (w/r) | +| start int32 start array | +| stride int32 stride array | +| edge int32 edge array | +| datbuf void data buffer for read | +| | | | | OUTPUTS: | -| None | +| datbuf void data buffer for write | | | | NOTES: | | | | | | Date Programmer Description | | ====== ============ ================================================= | -| Aug 96 Joel Gales Original Programmer | -| Dec 96 Joel Gales Add multiple vertical subsetting capability | -| May 97 Joel Gales Check for supported field types | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ - - -/* Macro to initialize swath region entry */ -/* -------------------------------------- */ - -/* - * 1) Find empty (inactive) region. 2) Allocate space for region entry. 3) - * Store file ID and swath ID. 4) Set region ID. 5) Initialize vertical - * subset entries to -1. - */ - -#define SETSWTHREG \ -\ -regionID = 0; \ -for (k = 0; k < NSWATHREGN; k++) \ -{ \ - if (SWXRegion[k] == 0) \ - { \ - SWXRegion[k] = (struct swathRegion *) \ - calloc(1, sizeof(struct swathRegion)); \ - SWXRegion[k]->fid = fid; \ - SWXRegion[k]->swathID = swathID; \ - regionID = k; \ - for (j=0; j<8; j++) \ - { \ - SWXRegion[k]->StartVertical[j] = -1; \ - SWXRegion[k]->StopVertical[j] = -1; \ - SWXRegion[k]->StartScan[j] = -1; \ - SWXRegion[k]->StopScan[j] = -1; \ - SWXRegion[k]->band8flag = -1; \ - } \ - break; \ - } \ -} - - -/* Macro to fill vertical subset entry */ -/* ----------------------------------- */ - -/* - * 1) Find empty (inactive) vertical region. 2) Set start of vertical region. - * 3) Allocate space for name of vertical dimension. 4) Write vertical - * dimension name. - */ - -#define FILLVERTREG \ -for (j=0; j<8; j++) \ -{ \ - if (SWXRegion[regionID]->StartVertical[j] == -1) \ - { \ - SWXRegion[regionID]->StartVertical[j] = i; \ - SWXRegion[regionID]->DimNamePtr[j] = \ - (char *) malloc(slen + 1); \ - memcpy(SWXRegion[regionID]->DimNamePtr[j], \ - dimlist, slen + 1); \ - break; \ - } \ -} \ - - +| Jun 96 Joel Gales Original Programmer | +| Feb 97 Joel Gales Stride = 1 HDF compression workaround | +| | +| END_PROLOG | +-----------------------------------------------------------------------------*/ +static intn +SWwrrdfield(int32 swathID, const char *fieldname, const char *code, + int32 start[], int32 stride[], int32 edge[], VOIDP datbuf) -int32 -SWdefvrtregion(int32 swathID, int32 regionID, const char *vertObj, float64 range[]) { intn i; /* Loop index */ - intn j = 0; /* Loop index */ - intn k; /* Loop index */ - intn status; /* routine return status variable */ - - uint8 found = 0; /* Found flag */ - - int16 vertINT16; /* Temporary INT16 variable */ + intn status = 0; /* routine return status variable */ int32 fid; /* HDF-EOS file ID */ int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - int32 slen; /* String length */ - int32 rank; /* Field rank */ - int32 nt; /* Field numbertype */ - int32 dims[8]; /* Field dimensions */ - int32 size; /* Size of numbertype in bytes */ - int32 vertINT32; /* Temporary INT32 variable */ + int32 vgid; /* Swath Geo or Data Vgroup ID */ + int32 sdid; /* SDS ID */ + int32 dum; /* Dummy variable */ + int32 rankSDS; /* Rank of SDS */ + int32 rankFld; /* Rank of field */ + + int32 vdataID; /* 1d field vdata */ + int32 recsize; /* Vdata record size */ + int32 fldsize; /* Field size */ + int32 nrec; /* Number of records in Vdata */ - float32 vertFLT32; /* Temporary FLT32 variable */ + int32 offset[8]; /* I/O offset (start) */ + int32 incr[8]; /* I/O increment (stride) */ + int32 count[8]; /* I/O count (edge) */ + int32 dims[8]; /* Field/SDS dimensions */ + int32 mrgOffset; /* Merged field offset */ + int32 nflds; /* Number of fields in Vdata */ + int32 strideOne; /* Strides = 1 flag */ - float64 vertFLT64; /* Temporary FLT64 variable */ + uint8 *buf; /* I/O (transfer) buffer */ + uint8 *fillbuf; /* Fill value buffer */ - char *vertArr; /* Pointer to vertical field data buffer */ - char dimlist[256]; /* Dimension list */ + char attrName[80]; /* Name of fill value attribute */ + char *ptr[64]; /* String pointer array */ + char fieldlist[256]; /* Vdata field list */ /* Check for valid swath ID */ /* ------------------------ */ - status = SWchkswid(swathID, "SWdefvrtregion", &fid, &sdInterfaceID, - &swVgrpID); + status = SWchkswid(swathID, "SWwrrdfield", &fid, &sdInterfaceID, &dum); + if (status == 0) { - /* Copy first 4 characters of vertObj into dimlist */ - /* ----------------------------------------------- */ - memcpy(dimlist, vertObj, 4); - dimlist[4] = 0; - - - /* If first 4 characters of vertObj = "DIM:" ... */ - /* --------------------------------------------- */ + /* Check whether fieldname is in SDS (multi-dim field) */ + /* --------------------------------------------------- */ + status = SWSDfldsrch(swathID, sdInterfaceID, fieldname, &sdid, + &rankSDS, &rankFld, &mrgOffset, dims, &dum); - /* Vertical Object is dimension name */ - /* --------------------------------- */ - if (strcmp(dimlist, "DIM:") == 0) + /* Multi-Dimensional Field Section */ + /* ------------------------------- */ + if (status != -1) { - /* Get string length of vertObj (minus "DIM:) */ - /* ------------------------------------------ */ - slen = (int)strlen(vertObj) - 4; - + /* Set I/O offset Section */ + /* ---------------------- */ - /* If regionID = -1 then setup swath region entry */ - /* ---------------------------------------------- */ - if (regionID == -1) + /* + * If start == NULL (default) set I/O offset of 0th field to + * offset within merged field (if any) and the rest to 0 + */ + if (start == NULL) { - SETSWTHREG; + for (i = 0; i < rankSDS; i++) + { + offset[i] = 0; + } + offset[0] = mrgOffset; } - - - /* Find first empty (inactive) vertical subset entry */ - /* ------------------------------------------------- */ - for (j = 0; j < 8; j++) + else { - if (SWXRegion[regionID]->StartVertical[j] == -1) + /* + * ... otherwise set I/O offset to user values, adjusting the + * 0th field with the merged field offset (if any) + */ + if (rankFld == rankSDS) { - /* Store start & stop of vertical region */ - /* ------------------------------------- */ - SWXRegion[regionID]->StartVertical[j] = (int32) range[0]; - SWXRegion[regionID]->StopVertical[j] = (int32) range[1]; - - /* Store vertical dimension name */ - /* ----------------------------- */ - SWXRegion[regionID]->DimNamePtr[j] = - (char *) malloc(slen + 1); - if(SWXRegion[regionID]->DimNamePtr[j] == NULL) + for (i = 0; i < rankSDS; i++) { - HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__); - return(-1); + offset[i] = start[i]; } - memcpy(SWXRegion[regionID]->DimNamePtr[j], - vertObj + 4, slen + 1); - break; + offset[0] += mrgOffset; + } + else + { + /* + * If field really 2-dim merged in 3-dim field then set + * 0th field offset to merge offset and then next two to + * the user values + */ + for (i = 0; i < rankFld; i++) + { + offset[i + 1] = start[i]; + } + offset[0] = mrgOffset; } } - } - else - { - /* Vertical Object is fieldname */ - /* ---------------------------- */ - /* Check for valid fieldname */ - /* ------------------------- */ - status = SWfieldinfo(swathID, vertObj, &rank, dims, &nt, - dimlist); + /* Set I/O stride Section */ + /* ---------------------- */ - if (status != 0) + /* + * If stride == NULL (default) set I/O stride to 1 + */ + if (stride == NULL) { - status = -1; - HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__); - HEreport("Vertical Field: \"%s\" not found.\n", vertObj); + for (i = 0; i < rankSDS; i++) + { + incr[i] = 1; + } + } + else + { + /* + * ... otherwise set I/O stride to user values + */ + if (rankFld == rankSDS) + { + for (i = 0; i < rankSDS; i++) + { + incr[i] = stride[i]; + } + } + else + { + /* + * If field really 2-dim merged in 3-dim field then set + * 0th field stride to 1 and then next two to the user + * values. + */ + for (i = 0; i < rankFld; i++) + { + incr[i + 1] = stride[i]; + } + incr[0] = 1; + } } - /* Check for supported field types */ - /* ------------------------------- */ - if (nt != DFNT_INT16 && - nt != DFNT_INT32 && - nt != DFNT_FLOAT32 && - nt != DFNT_FLOAT64) + /* Set I/O count Section */ + /* --------------------- */ + + /* + * If edge == NULL (default) set I/O count to number of remaining + * entries (dims - start) / increment. Note that 0th field + * offset corrected for merged field offset (if any). + */ + if (edge == NULL) { - status = -1; - HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__); - HEreport("Fieldtype: %d not supported for vertical subsetting.\n", nt); + for (i = 1; i < rankSDS; i++) + { + count[i] = (dims[i] - offset[i]) / incr[i]; + } + count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0]; + } + else + { + /* + * ... otherwise set I/O count to user values + */ + if (rankFld == rankSDS) + { + for (i = 0; i < rankSDS; i++) + { + count[i] = edge[i]; + } + } + else + { + /* + * If field really 2-dim merged in 3-dim field then set + * 0th field count to 1 and then next two to the user + * values. + */ + for (i = 0; i < rankFld; i++) + { + count[i + 1] = edge[i]; + } + count[0] = 1; + } } + /* Perform I/O with relevant HDF I/O routine */ + /* ----------------------------------------- */ + if (strcmp(code, "w") == 0) + { + /* Set strideOne to true (1) */ + /* ------------------------- */ + strideOne = 1; + + + /* If incr[i] != 1 set strideOne to false (0) */ + /* ------------------------------------------ */ + for (i = 0; i < rankSDS; i++) + { + if (incr[i] != 1) + { + strideOne = 0; + break; + } + } - /* Check that vertical dimension is 1D */ - /* ----------------------------------- */ - if (status == 0) - { - if (rank != 1) + /* + * If strideOne is true use NULL parameter for stride. This + * is a work-around to HDF compression problem + */ + if (strideOne == 1) + { + status = SDwritedata(sdid, offset, NULL, count, + (VOIDP) datbuf); + } + else { - status = -1; - HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__); - HEreport("Vertical Field: \"%s\" must be 1-dim.\n", - vertObj); + status = SDwritedata(sdid, offset, incr, count, + (VOIDP) datbuf); } } + else + { + status = SDreaddata(sdid, offset, incr, count, + (VOIDP) datbuf); + } + } /* End of Multi-Dimensional Field Section */ + else + { + /* One-Dimensional Field Section */ + /* ----------------------------- */ - /* If no problems then continue */ - /* ---------------------------- */ - if (status == 0) + /* Check fieldname within 1d field Vgroups */ + /* --------------------------------------- */ + status = SW1dfldsrch(fid, swathID, fieldname, code, + &vgid, &vdataID, &dum); + + if (status != -1) { - /* Get string length of vertical dimension */ - /* --------------------------------------- */ - slen = (int)strlen(dimlist); + + /* Get number of records */ + /* --------------------- */ + nrec = VSelts(vdataID); - /* Get size in bytes of vertical field numbertype */ - /* ---------------------------------------------- */ - size = DFKNTsize(nt); + /* Set offset, increment, & count */ + /* ------------------------------ */ + offset[0] = (start == NULL) ? 0 : start[0]; + incr[0] = (stride == NULL) ? 1 : stride[0]; + count[0] = (edge == NULL) + ? (nrec - offset[0]) / incr[0] + : edge[0]; - /* Allocate space for vertical field */ - /* --------------------------------- */ - vertArr = (char *) calloc(dims[0], size); - if(vertArr == NULL) + + /* Write Section */ + /* ------------- */ + if (strcmp(code, "w") == 0) { - HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__); - return(-1); - } + /* Get size of field and setup fill buffer */ + /* --------------------------------------- */ + fldsize = VSsizeof(vdataID, (char *)fieldname); + fillbuf = (uint8 *) calloc(fldsize, 1); + if(fillbuf == NULL) + { + HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); + return(-1); + } - /* Read vertical field */ - /* ------------------- */ - status = SWreadfield(swathID, vertObj, - NULL, NULL, NULL, vertArr); + /* Get size of record in Vdata and setup I/O buffer */ + /* ------------------------------------------------ */ + VSQueryvsize(vdataID, &recsize); + buf = (uint8 *) calloc(recsize, count[0] * incr[0]); + if(buf == NULL) + { + HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); + free(fillbuf); + return(-1); + } + /* Get names and number of fields in each record */ + /* ---------------------------------------------- */ + VSgetfields(vdataID, fieldlist); + dum = EHstrwithin(fieldname, fieldlist, ','); + nflds = EHparsestr(fieldlist, ',', ptr, NULL); - switch (nt) - { - case DFNT_INT16: - for (i = 0; i < dims[0]; i++) + /* Get Merged Field Offset (if any) */ + /* -------------------------------- */ + if (nflds > 1) { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertINT16, vertArr + i * size, size); - - - /* If within range ... */ - /* ------------------- */ - if (vertINT16 >= range[0] && - vertINT16 <= range[1]) + if (dum > 0) { - /* Set found flag */ - /* -------------- */ - found = 1; - - - /* If regionID=-1 then setup swath region entry */ - /* -------------------------------------------- */ - if (regionID == -1) - { - SETSWTHREG; - } - - - /* Fill-in vertical region entries */ - /* ------------------------------- */ - FILLVERTREG; - - break; + *(ptr[dum] - 1) = 0; + mrgOffset = VSsizeof(vdataID, fieldlist); + *(ptr[dum] - 1) = ','; } - } - - - /* If found read from "bottom" of data field */ - /* ----------------------------------------- */ - if (found == 1) - { - for (i = dims[0] - 1; i >= 0; i--) + else { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertINT16, vertArr + i * size, size); - - - /* If within range ... */ - /* ------------------- */ - if (vertINT16 >= range[0] && - vertINT16 <= range[1]) - { - /* Set end of vertical region */ - /* -------------------------- */ - SWXRegion[regionID]->StopVertical[j] = i; - break; - } + mrgOffset = 0; } + + /* Read records to recover previously written data */ + /* status = */ VSsetfields(vdataID, fieldlist); + /* status = */ VSseek(vdataID, offset[0]); + nrec = VSread(vdataID, buf, count[0] * incr[0], + FULL_INTERLACE); } else { - /* No vertical entries within region */ - /* --------------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWdefvrtregion", - __FILE__, __LINE__); - HEreport("No vertical field entries within region.\n"); + mrgOffset = 0; } - break; - - case DFNT_INT32: - for (i = 0; i < dims[0]; i++) - { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertINT32, vertArr + i * size, size); + /* Fill buffer with "Fill" value (if any) */ + /* -------------------------------------- */ + snprintf( attrName, sizeof(attrName), "_FV_%s", fieldname); - /* If within range ... */ - /* ------------------- */ - if (vertINT32 >= range[0] && - vertINT32 <= range[1]) + status = SWreadattr(swathID, attrName, (char *) fillbuf); + if (status == 0) + { + for (i = 0; i < count[0] * incr[0]; i++) { - /* Set found flag */ - /* -------------- */ - found = 1; - - - /* If regionID=-1 then setup swath region entry */ - /* -------------------------------------------- */ - if (regionID == -1) - { - SETSWTHREG; - } - - - /* Fill-in vertical region entries */ - /* ------------------------------- */ - FILLVERTREG; - - break; + memcpy(buf + i * recsize + mrgOffset, + fillbuf, fldsize); } } - /* If found read from "bottom" of data field */ - /* ----------------------------------------- */ - if (found == 1) + /* Write new data into buffer */ + /* -------------------------- */ + if (incr[0] == 1 && nflds == 1) { - for (i = dims[0] - 1; i >= 0; i--) - { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertINT32, vertArr + i * size, size); - - - /* If within range ... */ - /* ------------------- */ - if (vertINT32 >= range[0] && - vertINT32 <= range[1]) - { - /* Set end of vertical region */ - /* -------------------------- */ - SWXRegion[regionID]->StopVertical[j] = i; - break; - } - } + memcpy(buf, datbuf, count[0] * recsize); } else { - /* No vertical entries within region */ - /* --------------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWdefvrtregion", - __FILE__, __LINE__); - HEreport("No vertical field entries within region.\n"); - } - break; - - - case DFNT_FLOAT32: - - for (i = 0; i < dims[0]; i++) - { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertFLT32, vertArr + i * size, size); - - - /* If within range ... */ - /* ------------------- */ - if (vertFLT32 >= range[0] && - vertFLT32 <= range[1]) + for (i = 0; i < count[0]; i++) { - /* Set found flag */ - /* -------------- */ - found = 1; - - - /* If regionID=-1 then setup swath region entry */ - /* -------------------------------------------- */ - if (regionID == -1) - { - SETSWTHREG; - } - - - /* Fill-in vertical region entries */ - /* ------------------------------- */ - FILLVERTREG; - - break; + memcpy(buf + i * recsize * incr[0] + mrgOffset, + (uint8 *) datbuf + i * fldsize, fldsize); } } - /* If found read from "bottom" of data field */ - /* ----------------------------------------- */ - if (found == 1) + /* If append read last record */ + /* -------------------------- */ + if (offset[0] == nrec) { - for (i = dims[0] - 1; i >= 0; i--) - { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertFLT32, vertArr + i * size, size); - - - /* If within range ... */ - /* ------------------- */ - if (vertFLT32 >= range[0] && - vertFLT32 <= range[1]) - { - /* Set end of vertical region */ - /* -------------------------- */ - SWXRegion[regionID]->StopVertical[j] = i; - break; - } - } + /* abe added "status =" to next line 8/8/97 */ + status = VSseek(vdataID, offset[0] - 1); + VSread(vdataID, fillbuf, 1, FULL_INTERLACE); } else { - /* No vertical entries within region */ - /* --------------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWdefvrtregion", - __FILE__, __LINE__); - HEreport("No vertical field entries within region.\n"); + status = VSseek(vdataID, offset[0]); } - break; - - case DFNT_FLOAT64: - - for (i = 0; i < dims[0]; i++) - { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertFLT64, vertArr + i * size, size); - - - /* If within range ... */ - /* ------------------- */ - if (vertFLT64 >= range[0] && - vertFLT64 <= range[1]) - { - /* Set found flag */ - /* -------------- */ - found = 1; - - - /* If regionID=-1 then setup swath region entry */ - /* -------------------------------------------- */ - if (regionID == -1) - { - SETSWTHREG; - } + /* Write data into Vdata */ + /* --------------------- */ + nrec = VSwrite(vdataID, buf, count[0] * incr[0], + FULL_INTERLACE); - /* Fill-in vertical region entries */ - /* ------------------------------- */ - FILLVERTREG; + free(fillbuf); + if (status > 0) + status = 0; - break; - } + } /* End Write Section */ + else + { + /* Read Section */ + /* ------------ */ + status = VSsetfields(vdataID, fieldname); + fldsize = VSsizeof(vdataID, (char *)fieldname); + buf = (uint8 *) calloc(fldsize, count[0] * incr[0]); + if(buf == NULL) + { + HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__); + return(-1); } + (void) VSseek(vdataID, offset[0]); + (void) VSread(vdataID, buf, count[0] * incr[0], + FULL_INTERLACE); + - /* If found read from "bottom" of data field */ - /* ----------------------------------------- */ - if (found == 1) + /* Copy from input buffer to returned data buffer */ + /* ---------------------------------------------- */ + if (incr[0] == 1) { - for (i = dims[0] - 1; i >= 0; i--) - { - /* Get single element of vertical field */ - /* ------------------------------------ */ - memcpy(&vertFLT64, vertArr + i * size, size); - - /* If within range ... */ - /* ------------------- */ - if (vertFLT64 >= range[0] && - vertFLT64 <= range[1]) - { - /* Set end of vertical region */ - /* -------------------------- */ - SWXRegion[regionID]->StopVertical[j] = i; - break; - } - } + memcpy(datbuf, buf, count[0] * fldsize); } else { - /* No vertical entries within region */ - /* --------------------------------- */ - status = -1; - HEpush(DFE_GENAPP, "SWdefvrtregion", - __FILE__, __LINE__); - HEreport("No vertical field entries within region.\n"); + for (i = 0; i < count[0]; i++) + { + memcpy((uint8 *) datbuf + i * fldsize, + buf + i * fldsize * incr[0], fldsize); + } } - break; - - } /* End of switch */ - free(vertArr); - } - } - } - - - /* Set regionID to -1 if bad return status */ - /* --------------------------------------- */ - if (status == -1) - { - regionID = -1; - } - - - return (regionID); -} -#endif - - -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWdefscanregion | -| | -| DESCRIPTION: Initialize the region structure for Landsat 7 float scene | -| subset | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| regionID int32 Region ID | -| | -| INPUTS: | -| swathID int32 Swath structure ID | -| fieldname char Field name to subset | -| range float64 subsetting range | -| mode int32 HDFE_ENDPOINT, HDFE_MIDPOINT or | -| HDFE_ANYPOINT | -| | -| OUTPUTS: | -| regionID int32 Region ID | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Jul 99 DaW Original Programmer | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWdefscanregion(int32 swathID, const char *fieldname, float64 range[], CPL_UNUSED int32 mode) -{ - intn j; /* Loop index */ - intn k; /* Loop index */ - intn status; /* routine return status variable */ - intn land_status = 3; /* routine return status variable */ - intn band81flag = 0; - intn band82flag = 0; - intn band83flag = 0; - uint8 detect_cnt = 0; /* Used to convert scan to scanline */ - /* L7 float scene sub. */ - uint8 scene_cnt = 0; - - - int32 nmtype = 0; /* Used for L7 float scene sub. */ - int32 count = 0; /* Used for L7 float scene sub. */ - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - int32 slen; /* String length */ - int32 dfrank[8]; /* data fields rank */ - int32 rank; /* Field rank */ - int32 numtype[8]; /* number type of data fields */ - int32 nt; /* Field numbertype */ - int32 dims[8]; /* Field dimensions */ - int32 dims2[8]; /* Field dimensions */ - int32 strbufsize = 0; /* string buffer size */ - int32 tmprange0 = 0; - - - uint16 *buffer = (uint16 *)NULL; - int32 *idxmap = (int32 *)NULL; - - int32 dimsize = 0; - - int32 regionID = -1; /* Region ID (return) */ - - float64 scan[2] = {0,0}; - float64 original_scan[2] = {0,0}; - - char dimlist[256]; /* Dimension list */ - char swathname[80]; - char *dfieldlist = (char *)NULL; /* data field list */ - char *tfieldname = (char *)NULL; /* temp field buffer */ - char *band81 = (char *)NULL; - char *band82 = (char *)NULL; - char *band83 = (char *)NULL; - - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWdefscanregion", &fid, &sdInterfaceID, - &swVgrpID); - - /* This code checks for the attribute detector_count */ - /* which is found in Landsat 7 files. It is used */ - /* for some of the loops. The other code checks if */ - /* one scan is requested. */ - /* ================================================= */ - land_status = SWattrinfo(swathID, "detector_count", &nmtype, &count); - if (land_status == 0) - { - scan[0] = range[0]; - scan[1] = range[1]; - original_scan[0] = range[0]; - original_scan[1] = range[1]; - - land_status = SWreadattr(swathID, "scene_count", &scene_cnt); - land_status = SWreadattr(swathID, "detector_count", &detect_cnt); - if (range[0] == range[1]) - { - range[0] = range[0] * detect_cnt - detect_cnt; - range[1] = range[0] + detect_cnt - 1; - } - else - { - range[0] = range[0] * detect_cnt - detect_cnt; - range[1] = range[1] * detect_cnt - 1; - } - - Vgetname(SWXSwath[0].IDTable, swathname); - band81 = strstr(swathname, "B81"); - if (band81 != (char *)NULL) - band81flag = 1; - band82 = strstr(swathname, "B82"); - if (band82 != (char *)NULL) - band82flag = 1; - band83 = strstr(swathname, "B83"); - if (band83 != (char *)NULL) - band83flag = 1; - } - - - /* If fieldname is null then subsetting Landsat 7 */ - /* floating scene. Get data field name, assume */ - /* only one data field in swath */ - /* ---------------------------------------------- */ - - if (fieldname == (char *)NULL) - { - (void) SWnentries(swathID, 4, &strbufsize); - dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char)); - (void) SWinqdatafields(swathID, dfieldlist, dfrank, numtype); - tfieldname = (char *)calloc(strbufsize + 1, sizeof(char)); - strcpy(tfieldname, dfieldlist); - } - else - { - slen = (int)strlen(fieldname); - tfieldname = (char *)calloc(slen + 1, sizeof(char)); - strcpy(tfieldname, fieldname); - } - - /* Check for valid fieldname */ - /* ------------------------- */ - status = SWfieldinfo(swathID, tfieldname, &rank, dims, &nt, - dimlist); - - if (status != 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__); - HEreport("Field: \"%s\" not found.\n", tfieldname); - } + } /* End Read Section */ - /* Check if input range values are within range of */ - /* data field */ - /* ----------------------------------------------- */ - if(status == 0) - { - status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist); - buffer = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer); - if(scan[0] > buffer[dims2[0]-1]) - { - HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__); - HEreport("Range values not within bounds of data field\n"); - free(buffer); - buffer = (uint16 *)NULL; - if (dfieldlist != NULL) - free(dfieldlist); - free(tfieldname); - return(-1); - } - if(scan[0] < buffer[0]) - { - if(scan[1] < buffer[0]) - { - HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__); - HEreport("Range values not within bounds of data field\n"); - free(buffer); - buffer = (uint16 *)NULL; - if (dfieldlist != NULL) - free(dfieldlist); - free(tfieldname); - return(-1); - } - else - { - scan[0] = buffer[0]; - range[0] = scan[0] * detect_cnt - detect_cnt; - } - } - if(scan[1] > buffer[dims2[0] - 1]) - { - scan[1] = buffer[dims2[0] - 1]; - range[1] = scan[1] * detect_cnt - 1; - } - } - - if(status == 0) - { - dimsize = SWdiminfo(swathID, "GeoTrack"); - if(dimsize > 0) - { - idxmap = (int32 *)calloc(dimsize, sizeof(int32)); - (void) SWidxmapinfo(swathID, "GeoTrack", "ScanLineTrack", idxmap); - tmprange0 = (int32)range[0]; - if(band82flag != 1 && band83flag != 1) - { - if (range[1] > idxmap[scene_cnt*2 - 1]) - { - range[1] = idxmap[scene_cnt*2 - 1]; - HEreport("Data length compared to geolocation length\n"); - } - } - if(band82flag == 1 || band83flag == 1) - { - tmprange0 = (int32)(range[0] - (buffer[0] * detect_cnt - detect_cnt)); - } - if(tmprange0 >= idxmap[scene_cnt * 2 - 1]) - { - HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__); - HEreport( - "Range values not within bounds of Latitude/Longitude field(s)\n"); - if (dfieldlist != NULL) - free(dfieldlist); - free(tfieldname); - free(buffer); - free(idxmap); - return(-1); - } - } - } - - if (status == 0) - { - slen = (int)strlen(tfieldname); - - SETSWTHREG; - - /* Find first empty (inactive) vertical subset entry */ - /* ------------------------------------------------- */ - for (j = 0; j < 8; j++) - { - if (SWXRegion[regionID]->StartVertical[j] == -1) - { - /* Store start & stop of region */ - /* ------------------------------------- */ - SWXRegion[regionID]->StartScan[j] = (int32) original_scan[0]; - SWXRegion[regionID]->StopScan[j] = (int32) original_scan[1]; - SWXRegion[regionID]->StartRegion[j] = (int32) range[0]; - SWXRegion[regionID]->StopRegion[j] = (int32) range[1]; - ++SWXRegion[regionID]->nRegions; - SWXRegion[regionID]->scanflag = 1; - if(band81flag == 1) - SWXRegion[regionID]->band8flag = 1; - if(band82flag == 1) - SWXRegion[regionID]->band8flag = 2; - if(band83flag == 1) - SWXRegion[regionID]->band8flag = 3; - break; - } + free(buf); + VSdetach(vdataID); } - } - + else + { + HEpush(DFE_GENAPP, "SWwrrdfield", __FILE__, __LINE__); + HEreport("Fieldname \"%s\" does not exist.\n", fieldname); + } + } /* End One-D Field Section */ - /* Set regionID to -1 if bad return status */ - /* --------------------------------------- */ - if (status == -1) - { - regionID = -1; } - - if (dfieldlist != NULL) - free(dfieldlist); - free(tfieldname); - if (buffer != NULL) - free(buffer); - if (idxmap != NULL) - free(idxmap); - - return (regionID); + return (status); } + /*----------------------------------------------------------------------------| | BEGIN_PROLOG | | | -| FUNCTION: SWsetfillvalue | -| | -| DESCRIPTION: Sets fill value for the specified field. | +| FUNCTION: SWreadfield | | | +| DESCRIPTION: Reads data from field | | | | Return Value Type Units Description | | ============ ====== ========= ===================================== | | status intn return status (0) SUCCEED, (-1) FAIL | | | -| INPUTS: | -| swathID int32 swath structure ID | -| fieldname char field name | -| fillval void fill value | +| INPUTS: | +| swathID int32 swath structure ID | +| fieldname const char fieldname | +| start int32 start array | +| stride int32 stride array | +| edge int32 edge array | +| buffer void data buffer for read | +| | | | | OUTPUTS: | -| None | +| None | | | | NOTES: | | | @@ -9859,63 +3666,16 @@ SWdefscanregion(int32 swathID, const char *fieldname, float64 range[], CPL_UNUSE | END_PROLOG | -----------------------------------------------------------------------------*/ intn -SWsetfillvalue(int32 swathID, const char *fieldname, VOIDP fillval) -{ - intn status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath root Vgroup ID */ - int32 sdid; /* SDS id */ - int32 nt; /* Number type */ - int32 dims[8]; /* Dimensions array */ - int32 dum; /* Dummy variable */ - int32 solo; /* "Solo" (non-merged) field flag */ - - char name[80]; /* Fill value "attribute" name */ - - /* Check for valid swath ID and get SDS interface ID */ - status = SWchkswid(swathID, "SWsetfillvalue", - &fid, &sdInterfaceID, &swVgrpID); - - if (status == 0) - { - /* Get field info */ - status = SWfieldinfo(swathID, fieldname, &dum, dims, &nt, NULL); - - if (status == 0) - { - /* Get SDS ID and solo flag */ - status = SWSDfldsrch(swathID, sdInterfaceID, fieldname, - &sdid, &dum, &dum, &dum, - dims, &solo); +SWreadfield(int32 swathID, const char *fieldname, + int32 start[], int32 stride[], int32 edge[], VOIDP buffer) - /* If unmerged field then call HDF set field routine */ - if (solo == 1) - { - /* status = */ SDsetfillvalue(sdid, fillval); - } +{ + intn status = 0; /* routine return status variable */ - /* - * Store fill value in attribute. Name is given by fieldname - * prepended with "_FV_" - */ - strcpy(name, "_FV_"); - strcat(name, fieldname); - status = SWwriteattr(swathID, name, nt, 1, fillval); - } - else - { - HEpush(DFE_GENAPP, "SWsetfillvalue", __FILE__, __LINE__); - HEreport("Fieldname \"%s\" does not exist.\n", fieldname); - } - } + status = SWwrrdfield(swathID, fieldname, "r", start, stride, edge, + buffer); return (status); } -#endif - - - /*----------------------------------------------------------------------------| | BEGIN_PROLOG | @@ -10020,621 +3780,88 @@ SWdetach(int32 swathID) { intn i; /* Loop index */ -#ifdef HDFEOS_SW_WRITE_SUPPORT - intn j; /* Loop index */ -#endif intn k; /* Loop index */ intn status = 0; /* routine return status variable */ -#ifdef HDFEOS_SW_WRITE_SUPPORT - intn statusFill = 0; /* return status from SWgetfillvalue */ -#endif uint8 *buf; /* Buffer for blank (initial) 1D records */ int32 vdataID; /* Vdata ID */ - int32 *namelen = NULL; /* Pointer to name string length array */ - int32 *dimlen = NULL; /* Pointer to dim string length array */ -#ifdef HDFEOS_SW_WRITE_SUPPORT - int32 slen1[3]; /* String length array 1 */ - int32 slen2[3]; /* String length array 2 */ - int32 nflds; /* Number of fields */ - int32 match[5]; /* Merged field match array */ - int32 cmbfldcnt; /* Number of fields combined */ - int32 sdid; /* SDS ID */ - int32 vgid; /* Vgroup ID */ -#endif int32 dims[3]; /* Dimension array */ - int32 *offset = NULL; /* Pointer to merged field offset array */ - int32 *indvdims = NULL; /* Pointer to merged field size array */ int32 sdInterfaceID; /* SDS interface ID */ int32 sID; /* Swath ID - offset */ -#ifdef HDFEOS_SW_WRITE_SUPPORT - int32 nflds0; /* Number of fields */ -#endif - int32 *namelen0 = NULL; /* Pointer to name string length array */ -#ifdef HDFEOS_SW_WRITE_SUPPORT - int32 rank; /* Rank of merged field */ - int32 truerank; /* True rank of merged field */ -#endif int32 idOffset = SWIDOFFSET; /* Swath ID offset */ int32 dum; /* Dummy variable */ - char *nambuf = NULL; /* Pointer to name buffer */ - char **nameptr = NULL; /* Pointer to name string pointer array */ - char **dimptr = NULL; /* Pointer to dim string pointer array */ - char **nameptr0 = NULL; /* Pointer to name string pointer array */ -#ifdef HDFEOS_SW_WRITE_SUPPORT - char *ptr1[3]; /* String pointer array */ - char *ptr2[3]; /* String pointer array */ - char dimbuf1[128]; /* Dimension buffer 1 */ - char dimbuf2[128]; /* Dimension buffer 2 */ -#endif char swathname[VGNAMELENMAX + 1]; /* Swath name */ -#ifdef HDFEOS_SW_WRITE_SUPPORT - char *utlbuf; /* Utility buffer */ - char fillval[32];/* Fill value buffer */ -#endif /* Check for proper swath ID and get SD interface ID */ /* ------------------------------------------------- */ status = SWchkswid(swathID, "SWdetach", &dum, &sdInterfaceID, &dum); - if (status == 0) - { - /* Subtract off swath ID offset and get swath name */ - /* ----------------------------------------------- */ - sID = swathID % idOffset; - if (sID >= NSWATH) - { - return -1; - } - Vgetname(SWXSwath[sID].IDTable, swathname); - - - /* Create 1D "orphaned" fields */ - /* --------------------------- */ - i = 0; - - /* Find "active" entries in 1d combination array */ - /* --------------------------------------------- */ - while (SWX1dcomb[3 * i] != 0) - { - /* For fields defined within swath... */ - /* ---------------------------------- */ - if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable) - { - /* Get dimension size and vdata ID */ - /* ------------------------------- */ - dims[0] = abs(SWX1dcomb[3 * i]); - vdataID = SWX1dcomb[3 * i + 2]; - - /* Get fieldname (= vdata name) */ - /* ---------------------------- */ - nambuf = (char *) calloc(VSNAMELENMAX + 1, 1); - if(nambuf == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - return(-1); - } - - VSgetname(vdataID, nambuf); - - /* Set field within vdata */ - /* ---------------------- */ - VSsetfields(vdataID, nambuf); - - /* Write (blank) records */ - /* --------------------- */ - buf = (uint8 *) calloc(VSsizeof(vdataID, nambuf), dims[0]); - if(buf == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nambuf); - return(-1); - } - VSwrite(vdataID, buf, dims[0], FULL_INTERLACE); - - free(buf); - free(nambuf); - - /* Detach Vdata */ - /* ------------ */ - VSdetach(vdataID); - } - i++; - } - - - /* SDS combined fields */ - /* ------------------- */ - if (strlen(SWXSDname) == 0) - { -#ifdef HDFEOS_SW_WRITE_SUPPORT - nflds = 0; -#endif - - /* Allocate "dummy" arrays so free() doesn't bomb later */ - /* ---------------------------------------------------- */ - nameptr = (char **) calloc(1, sizeof(char *)); - if(nameptr == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - return(-1); - } - namelen = (int32 *) calloc(1, sizeof(int32)); - if(namelen == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - return(-1); - } - nameptr0 = (char **) calloc(1, sizeof(char *)); - if(nameptr0 == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - return(-1); - } - namelen0 = (int32 *) calloc(1, sizeof(int32)); - if(namelen0 == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - return(-1); - } - dimptr = (char **) calloc(1, sizeof(char *)); - if(dimptr == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - return(-1); - } - dimlen = (int32 *) calloc(1, sizeof(int32)); - if(dimlen == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - return(-1); - } - offset = (int32 *) calloc(1, sizeof(int32)); - if(offset == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - return(-1); - } - indvdims = (int32 *) calloc(1, sizeof(int32)); - if(indvdims == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - return(-1); - } - } -#if defined(HDFEOS_SW_WRITE_SUPPORT) - else - { - /* - * "Trim Off" trailing "," and ";" in SWXSDname & SWXSDdims - * respectively - */ - SWXSDname[strlen(SWXSDname) - 1] = 0; - SWXSDdims[strlen(SWXSDdims) - 1] = 0; - - - /* Get number of fields from SWXSDname string */ - /* ------------------------------------------ */ - nflds = EHparsestr(SWXSDname, ',', NULL, NULL); - - - /* Allocate space for various dynamic arrays */ - /* ----------------------------------------- */ - nameptr = (char **) calloc(nflds, sizeof(char *)); - if(nameptr == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - return(-1); - } - namelen = (int32 *) calloc(nflds, sizeof(int32)); - if(namelen == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - return(-1); - } - nameptr0 = (char **) calloc(nflds, sizeof(char *)); - if(nameptr0 == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - return(-1); - } - namelen0 = (int32 *) calloc(nflds, sizeof(int32)); - if(namelen0 == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - return(-1); - } - dimptr = (char **) calloc(nflds, sizeof(char *)); - if(dimptr == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - return(-1); - } - dimlen = (int32 *) calloc(nflds, sizeof(int32)); - if(dimlen == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - return(-1); - } - offset = (int32 *) calloc(nflds, sizeof(int32)); - if(offset == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - return(-1); - } - indvdims = (int32 *) calloc(nflds, sizeof(int32)); - if(indvdims == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - return(-1); - } - - - /* Parse SWXSDname and SWXSDdims strings */ - /* ------------------------------------- */ - nflds = EHparsestr(SWXSDname, ',', nameptr, namelen); - nflds = EHparsestr(SWXSDdims, ';', dimptr, dimlen); - } - - /* Loop through all the fields */ - /* --------------------------- */ - for (i = 0; i < nflds; i++) - { - /* If active entry and field is within swath to be detached ... */ - /* ------------------------------------------------------------ */ - if (SWXSDcomb[5 * i] != 0 && - SWXSDcomb[5 * i + 3] == SWXSwath[sID].IDTable) - { - nambuf = (char *) calloc(strlen(SWXSDname) + 1, 1); - if(nambuf == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - free(indvdims); - return(-1); - } - utlbuf = (char *) calloc(2 * strlen(SWXSDname) + 7, 1); - if(utlbuf == NULL) - { - HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); - free(nambuf); - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - free(indvdims); - return(-1); - } - /* Zero out dimbuf1 */ - /* ---------------- */ - for (k = 0; k < (intn)sizeof(dimbuf1); k++) - { - dimbuf1[k] = 0; - } - - - /* Load array to match, name & parse dims */ - /* -------------------------------------- */ - memcpy(match, &SWXSDcomb[5 * i], 20); - memcpy(nambuf, nameptr[i], namelen[i]); - - memcpy(dimbuf1, dimptr[i], dimlen[i]); - dum = EHparsestr(dimbuf1, ',', ptr1, slen1); - - - /* Separate combined (first) dimension from others */ - /* ----------------------------------------------- */ - dimbuf1[slen1[0]] = 0; - - offset[0] = 0; - indvdims[0] = abs(match[0]); - - /* - * Loop through remaining fields to check for matches with - * current one - */ - for (j = i + 1, cmbfldcnt = 0; j < nflds; j++) - { - if (SWXSDcomb[5 * j] != 0) - { - /* Zero out dimbuf2 */ - /* ---------------- */ - for (k = 0; k < (intn)sizeof(dimbuf2); k++) - { - dimbuf2[k] = 0; - } - - /* - * Parse the dimensions and separate first for this - * entry - */ - memcpy(dimbuf2, dimptr[j], dimlen[j]); - dum = EHparsestr(dimbuf2, ',', ptr2, slen2); - dimbuf2[slen2[0]] = 0; - - - /* - * If 2nd & 3rd dimension values and names (1st and - * 2nd for rank=2 array), swath ID, and numbertype - * are equal, then these fields can be combined. - */ - if (match[1] == SWXSDcomb[5 * j + 1] && - match[2] == SWXSDcomb[5 * j + 2] && - match[3] == SWXSDcomb[5 * j + 3] && - match[4] == SWXSDcomb[5 * j + 4] && - strcmp(dimbuf1 + slen1[0] + 1, - dimbuf2 + slen2[0] + 1) == 0) - { - /* Add to combined dimension size */ - /* ------------------------------ */ - match[0] += SWXSDcomb[5 * j]; - - /* Concatenate name */ - /* ---------------- */ - strcat(nambuf, ","); - memcpy(nambuf + strlen(nambuf), - nameptr[j], namelen[j]); - - /* - * Increment number of merged fields, store - * individual dims and dim offsets - */ - cmbfldcnt++; - indvdims[cmbfldcnt] = abs(SWXSDcomb[5 * j]); - offset[cmbfldcnt] = offset[cmbfldcnt - 1] + - indvdims[cmbfldcnt - 1]; - - /* Delete this field from combination list */ - /* --------------------------------------- */ - SWXSDcomb[5 * j] = 0; - } - } - } - - - /* Create SDS */ - /* ---------- */ - - /* Parse names string */ - /* ------------------ */ - nflds0 = EHparsestr(nambuf, ',', nameptr0, namelen0); - - if (abs(match[0]) == 1) - { - /* Two Dimensional Array (no merging has occurred) */ - /* ---------------------------------------------- */ - dims[0] = abs(match[1]); - dims[1] = abs(match[2]); - - /* Create SDS */ - /* ---------- */ - rank = 2; - sdid = SDcreate(sdInterfaceID, nambuf, - SWXSDcomb[5 * i + 4], 2, dims); - } - else - { - /* Three Dimensional Array */ - /* ----------------------- */ - dims[0] = abs(match[0]); - dims[1] = abs(match[1]); - dims[2] = abs(match[2]); - - rank = 3; - - /* - * If merged fields then form string consisting of - * "MRGFLD_" + 1st field in merge + ":" + entire merged - * field list and store in utlbuf. Then write to - * MergedField metadata section - */ - if (cmbfldcnt > 0) - { - strcpy(utlbuf, "MRGFLD_"); - memcpy(utlbuf + 7, nameptr0[0], namelen0[0]); - utlbuf[7 + namelen0[0]] = 0; - strcat(utlbuf, ":"); - strcat(utlbuf, nambuf); - - status = EHinsertmeta(sdInterfaceID, swathname, "s", - 6L, utlbuf, NULL); - } - else - { - /* - * If not merged field then store field name in - * utlbuf - */ - strcpy(utlbuf, nambuf); - } - - /* Create SDS */ - /* ---------- */ - sdid = SDcreate(sdInterfaceID, utlbuf, - SWXSDcomb[5 * i + 4], 3, dims); - - - /* - * If merged field then store dimensions and offsets as - * SD attributes - */ - if (cmbfldcnt > 0) - { - SDsetattr(sdid, "Field Dims", DFNT_INT32, - cmbfldcnt + 1, (VOIDP) indvdims); - - SDsetattr(sdid, "Field Offsets", DFNT_INT32, - cmbfldcnt + 1, (VOIDP) offset); - } - } - + if (status == 0) + { + /* Subtract off swath ID offset and get swath name */ + /* ----------------------------------------------- */ + sID = swathID % idOffset; + if (sID >= NSWATH) + { + return -1; + } + Vgetname(SWXSwath[sID].IDTable, swathname); - /* Register Dimensions in SDS */ - /* -------------------------- */ - for (k = 0; k < rank; k++) - { - if (rank == 2) - { - /* Copy k+1th dimension into dimbuf2 if rank = 2 */ - /* --------------------------------------------- */ - memcpy(dimbuf2, ptr1[k + 1], slen1[k + 1]); - dimbuf2[slen1[k + 1]] = 0; - } - else - { - /* Copy kth dimension into dimbuf2 if rank > 2 */ - /* ------------------------------------------- */ - memcpy(dimbuf2, ptr1[k], slen1[k]); - dimbuf2[slen1[k]] = 0; - } + /* Create 1D "orphaned" fields */ + /* --------------------------- */ + i = 0; - /* - * If first dimension and merged field then generate - * dimension name consisting of "MRGDIM:" + swathname + - * dimension size - */ - if (k == 0 && cmbfldcnt > 0) - { - snprintf(dimbuf2, sizeof(dimbuf2), "%s%s_%ld", "MRGDIM:", - swathname, (long)dims[0]); - } - else - { - /* Otherwise concatenate swathname to dim name */ - /* ------------------------------------------- */ - strcat(dimbuf2, ":"); - strcat(dimbuf2, swathname); - } + /* Find "active" entries in 1d combination array */ + /* --------------------------------------------- */ + while (SWX1dcomb[3 * i] != 0) + { + /* For fields defined within swath... */ + /* ---------------------------------- */ + if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable) + { + /* Get dimension size and vdata ID */ + /* ------------------------------- */ + dims[0] = abs(SWX1dcomb[3 * i]); + vdataID = SWX1dcomb[3 * i + 2]; - /* Register dimensions using "SDsetdimname" */ - /* ---------------------------------------- */ - SDsetdimname(SDgetdimid(sdid, k), (char *) dimbuf2); + /* Get fieldname (= vdata name) */ + /* ---------------------------- */ + char* nambuf = (char *) calloc(VSNAMELENMAX + 1, 1); + if(nambuf == NULL) + { + HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); + return(-1); } + VSgetname(vdataID, nambuf); + /* Set field within vdata */ + /* ---------------------- */ + VSsetfields(vdataID, nambuf); - /* Write Fill Value */ - /* ---------------- */ - for (k = 0; k < nflds0; k++) + /* Write (blank) records */ + /* --------------------- */ + buf = (uint8 *) calloc(VSsizeof(vdataID, nambuf), dims[0]); + if(buf == NULL) { - /* Check if fill values has been set */ - /* --------------------------------- */ - memcpy(utlbuf, nameptr0[k], namelen0[k]); - utlbuf[namelen[k]] = 0; - statusFill = SWgetfillvalue(swathID, utlbuf, fillval); - - if (statusFill == 0) - { - /* - * If merged field then fill value must be stored - * manually using EHfillfld - */ - if (cmbfldcnt > 0) - { - dims[0] = indvdims[k]; - truerank = (dims[0] == 1) ? 2 : 3; - EHfillfld(sdid, rank, truerank, - DFKNTsize(match[4]), offset[k], - dims, fillval); - } - /* - * If single field then just use the HDF set fill - * function - */ - else - { - status = SDsetfillvalue(sdid, fillval); - } - } + HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__); + free(nambuf); + return(-1); } + VSwrite(vdataID, buf, dims[0], FULL_INTERLACE); - - /* - * Insert SDS within the appropriate Vgroup (geo or data) and - * "detach" newly-created SDS - */ - vgid = (match[0] < 0) - ? SWXSwath[sID].VIDTable[0] - : SWXSwath[sID].VIDTable[1]; - - Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid)); - SDendaccess(sdid); - + free(buf); free(nambuf); - free(utlbuf); + + /* Detach Vdata */ + /* ------------ */ + VSdetach(vdataID); } + i++; } -#endif - - /* "Contract" 1dcomb array */ /* ----------------------- */ @@ -10651,65 +3878,6 @@ SWdetach(int32 swathID) i++; } -#ifdef HDFEOS_SW_WRITE_SUPPORT - /* "Contract" SDcomb array */ - /* ----------------------- */ - for (i = 0; i < nflds; i++) - { - if (SWXSDcomb[5 * i + 3] == SWXSwath[sID].IDTable) - { - if (i == (nflds - 1)) - { - SWXSDcomb[5 * i] = 0; - *(nameptr[i] - (nflds != 1)) = 0; - *(dimptr[i] - (nflds != 1)) = 0; - } - else - { - memmove(&SWXSDcomb[5 * i], - &SWXSDcomb[5 * (i + 1)], - (512 - i - 1) * 5 * 4); - - memmove(nameptr[i], - nameptr[i + 1], - nameptr[0] + 2048 - nameptr[i + 1] - 1); - - memmove(dimptr[i], - dimptr[i + 1], - dimptr[0] + 2048 * 2 - dimptr[i + 1] - 1); - } - - i--; - nflds = EHparsestr(SWXSDname, ',', nameptr, namelen); - nflds = EHparsestr(SWXSDdims, ';', dimptr, dimlen); - } - } - - - /* Replace trailing delimiters on SWXSDname & SWXSDdims */ - /* ---------------------------------------------------- */ - if (nflds != 0) - { - strcat(SWXSDname, ","); - strcat(SWXSDdims, ";"); - } -#endif - - - /* Free up a bunch of dynamically allocated arrays */ - /* ----------------------------------------------- */ - free(nameptr); - free(namelen); - free(nameptr0); - free(namelen0); - free(dimptr); - free(dimlen); - free(offset); - free(indvdims); - - - - /* "Detach" from previously attached SDSs */ /* -------------------------------------- */ for (k = 0; k < SWXSwath[sID].nSDS; k++) @@ -10803,660 +3971,6 @@ SWclose(int32 fid) return (status); } -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWupdatescene | -| | -| DESCRIPTION: Updates the StartRegion and StopRegion values | -| for a specified region. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| status intn return status (0) SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 Swath structure ID | -| regionID int32 Region ID | -| | -| NOTES: | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Nov 98 Xinmin Hua Original developing | -| Aug 99 Abe Taaheri Added code to exclude regions that have the same | -| start and stop. | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -intn -SWupdatescene(int32 swathID, int32 regionID) -{ - intn k; /* Loop index */ - int32 status; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - - int32 startReg; /* Indexed start region */ - int32 stopReg; /* Indexed stop region */ - int32 l_index[MAXNREGIONS]; /* to store indices when stop and - start are different */ - - int32 ind; /* l_index */ - int32 tempnRegions; /* temp number of regions */ - - memset(l_index, 0, sizeof(int32) * MAXNREGIONS); - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWupdatescene", &fid, &sdInterfaceID, - &swVgrpID); - - - /* Check for valid region ID */ - /* ------------------------- */ - if (status == 0) - { - if (regionID < 0 || regionID >= NSWATHREGN) - { - status = -1; - HEpush(DFE_RANGE, "SWupdatescene", __FILE__, __LINE__); - HEreport("Invalid Region id: %d.\n", regionID); - } - } - - /* Check for active region ID */ - /* -------------------------- */ - if (status == 0) - { - if (SWXRegion[regionID] == 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWupdatescene", __FILE__, __LINE__); - HEreport("Inactive Region ID: %d.\n", regionID); - } - } - - if (status == 0) - { - tempnRegions = SWXRegion[regionID]->nRegions; - ind =0; - - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - startReg = SWXRegion[regionID]->StartRegion[k]; - stopReg = SWXRegion[regionID]->StopRegion[k]; - if(startReg == stopReg) - { - /* reduce number of regions by 1, if tempnRegions is 0 issue - error and break from loop*/ - tempnRegions -= 1; - - if(tempnRegions == 0) - { - /* first free allocated memory for SWXRegion[regionID] - in the function SWdefboxregion and make regionID - inactive */ - free(SWXRegion[regionID]); - SWXRegion[regionID] = 0; - status = -1; - HEpush(DFE_GENAPP, "SWupdatescene", __FILE__, __LINE__); - HEreport("Inactive Region ID: %d.\n", regionID); - break; - } - } - else - { - /* store l_index number of regions that have different start and - stop */ - l_index[ind] = k; - ind += 1; - } - } - if (status != 0) - { - return (status); - } - else - { - SWXRegion[regionID]->nRegions = tempnRegions; - } - /* keep starts and stops that are different in the structure */ - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - SWXRegion[regionID]->StartRegion[k] = - SWXRegion[regionID]->StartRegion[l_index[k]]; - SWXRegion[regionID]->StopRegion[k] = - SWXRegion[regionID]->StopRegion[l_index[k]]; - } - - } - - - if (status == 0) - { - - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - - startReg = SWXRegion[regionID]->StartRegion[k]; - stopReg = SWXRegion[regionID]->StopRegion[k]; - - if(startReg % 2 == 1) { - - SWXRegion[regionID]->StartRegion[k] = ++startReg; - - } - if(stopReg % 2 == 0) { - - SWXRegion[regionID]->StopRegion[k] = --stopReg; - - } - - } - - } - - return(status); - -} -#endif - -#ifdef HDFEOS_SW_WRITE_SUPPORT -/*----------------------------------------------------------------------------| -| BEGIN_PROLOG | -| | -| FUNCTION: SWupdateidxmap | -| | -| DESCRIPTION: Updates the map l_index for a specified region. | -| | -| | -| Return Value Type Units Description | -| ============ ====== ========= ===================================== | -| nout int32 return Number of elements in output | -| l_index array if SUCCEED, (-1) FAIL | -| | -| INPUTS: | -| swathID int32 Swath structure ID | -| regionID int32 Region ID | -| l_indexin int32 array of l_index values | -| | -| OUTPUTS: | -| l_indexout int32 array of l_index values | -| indices int32 array of start and stop in region | -| | -| NOTES: | -| | -| | -| Date Programmer Description | -| ====== ============ ================================================= | -| Aug 97 Abe Taaheri Original Programmer | -| AUG 97 Abe Taaheri Add support for l_index mapping | -| Sep 99 DaW Add support for Floating Scene Subsetting Landsat 7 | -| | -| END_PROLOG | ------------------------------------------------------------------------------*/ -int32 -SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexout[], int32 indicies[]) -{ - intn i; /* Loop index */ - intn j; /* Loop index */ - intn k; /* Loop index */ - int32 status; /* routine return status variable */ - int32 land_status = 3; /* routine return status variable */ - - int32 fid; /* HDF-EOS file ID */ - int32 sdInterfaceID; /* HDF SDS interface ID */ - int32 swVgrpID; /* Swath Vgroup ID */ - - int32 numtype = 0; /* Used for L7 float scene sub. */ - int32 count = 0; /* Used for L7 float scene sub. */ - - int32 startReg = 0; /* Indexed start region */ - int32 stopReg = 0; /* Indexed stop region */ - int32 nout=-1; /* Number of elements in output l_index array */ - int32 indexoffset = 0; - uint8 scene_cnt = 0; /* Used for L7 float scene sub. */ - uint8 detect_cnt = 0; /* Used to convert scan to scanline */ - intn gtflag = 0; - intn ngtflag = 0; - int32 *buffer1 = (int32 *)NULL; - int32 *buffer2 = (int32 *)NULL; - - /* Check for valid swath ID */ - /* ------------------------ */ - status = SWchkswid(swathID, "SWupdateidxmap", &fid, &sdInterfaceID, - &swVgrpID); - - - /* Check for valid region ID */ - /* ------------------------- */ - if (status == 0) - { - if (regionID < 0 || regionID >= NSWATHREGN) - { - status = -1; - HEpush(DFE_RANGE, "SWupdateidxmap", __FILE__, __LINE__); - HEreport("Invalid Region id: %d.\n", regionID); - } - } - - /* Check for active region ID */ - /* -------------------------- */ - if (status == 0) - { - if (SWXRegion[regionID] == 0) - { - status = -1; - HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__); - HEreport("Inactive Region ID: %d.\n", regionID); - } - } - - if (status == 0) - { - /* Loop through all regions */ - /* ------------------------ */ - for (k = 0; k < SWXRegion[regionID]->nRegions; k++) - { - - /* fix overlap l_index mapping problem for Landsat 7 */ - - startReg = SWXRegion[regionID]->StartRegion[k]; - stopReg = SWXRegion[regionID]->StopRegion[k]; - - - if(SWXRegion[regionID]->scanflag == 1) - { - indicies[0] = -1; - indicies[1] = -1; - j = 0; - /* This code checks for the attribute detector_count */ - /* which is found in Landsat 7 files. It is used */ - /* for some of the loops. */ - /* ================================================= */ - land_status = SWattrinfo(swathID, "scene_count", &numtype, &count); - if (land_status == 0) - { - land_status = SWreadattr(swathID, "scene_count", &scene_cnt); - land_status = SWreadattr(swathID, "detector_count", &detect_cnt); - } - - - /* calculate the offsets first */ - buffer1 = (int32 *)calloc(74, sizeof(int32)); - buffer2 = (int32 *)calloc(74, sizeof(int32)); - - status = SWidxmapinfo(swathID,"GeoTrack", - "ScanLineTrack", (int32*)buffer1); - status = SWidxmapinfo(swathID,"UpperTrack", - "ScanLineTrack", (int32*)buffer2); - - indexoffset = buffer2[0] - buffer1[0]; - free(buffer1); - free(buffer2); - - if(SWXRegion[regionID]->band8flag == -1) - { - for(i=0; i= startReg) - if(indicies[0] == -1) - indicies[0] = j; - if(l_indexin[j] <= stopReg && l_indexin[j+1] >= stopReg) - indicies[1] = j + 1; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - if(indicies[0] == -1) - { - if(startReg <= l_indexin[0]) - indicies[0] = 0; - } - if(indicies[0] == -1) - { - j = 0; - for(i=0; i= startReg) - if(indicies[0] == -1) - indicies[0] = j; - j = j + 1; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - if(indicies[1] == -1) - { - j = 0; - for(i=0; i= stopReg) - if(indicies[1] == -1) - indicies[1] = j + 1; - j = j + 1; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - if(indicies[1] == -1) - if(stopReg > l_indexin[scene_cnt - 1]) - indicies[1] = scene_cnt - 1; - } - - /* This section of code handles exceptions in Landsat 7 */ - /* data. The Band 8 data - multiple files, data gaps */ - /* ===================================================== */ - if(SWXRegion[regionID]->band8flag == 1 || - SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - j = 0; - for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && - startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) - if(indicies[0] == -1) - indicies[0] = j; - if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && - stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) - indicies[1] = j + 1; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - if(SWXRegion[regionID]->band8flag == 1) - { - if(indicies[1] == -1) - if(stopReg > (l_indexin[j - 1] + indexoffset - detect_cnt)) - indicies[1] = j - 1; - } - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - - if(startReg >= (l_indexin[j - 1] + indexoffset - detect_cnt)) - { - indicies[0] = -1; - indicies[1] = -1; - /* status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist); - buffer = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer); - indexoffset = buffer[0] * detect_cnt; - free(buffer); - startReg = startReg - (indexoffset - detect_cnt); - stopReg = stopReg - (indexoffset - 1); */ - j = 0; - for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && - startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) - if(indicies[0] == -1) - indicies[0] = j; - if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && - stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) - indicies[1] = j + 1; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - - if(indicies[0] == -1) - { - j = 0; - for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && - startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) - if(indicies[0] == -1) - indicies[0] = j; - - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - if(indicies[1] == -1) - if(stopReg > (l_indexin[j - 1] + indexoffset - detect_cnt) ) - indicies[1] = j - 1; - } - if(indicies[1] == -1) - { - j = 0; - for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt ) && - stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) - indicies[1] = j; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - } - - if(ngtflag == 1) - { - for(i=0; i= l_indexin[j] && startReg <= l_indexin[j+1]) - if(indicies[0] == -1) - indicies[0] = j; - if( stopReg >= l_indexin[j] && stopReg <= l_indexin[j+1]) - indicies[1] = j + 1; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - if(SWXRegion[regionID]->band8flag == 2) - { - if(startReg >= l_indexin[j] ) - { - if(indicies[0] == -1) - indicies[0] = j; - if(indicies[1] == -1) - indicies[1] = j; - } - if(indicies[0] == -1) - if(startReg <= l_indexin[0]) - indicies[0] = 0; - if(indicies[1] == -1) - if(stopReg > l_indexin[j]) - indicies[1] = j; - } - if(indicies[0] == -1) - { - j = 0; - for(i=0; i= l_indexin[j] && startReg <= l_indexin[j+1]) - indicies[0] = j; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - if(indicies[1] == -1) - { - j = 0; - for(i=0; i= l_indexin[j] && stopReg <= l_indexin[j+1]) - indicies[1] = j; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - if(indicies[1] == -1) - { - if(stopReg > l_indexin[j]) - indicies[1] = j; - } - } - if(indicies[0] == -1) - { - if(startReg <= (l_indexin[0]+ indexoffset - detect_cnt) ) - indicies[0] = 0; - if(indicies[1] == -1) - if(stopReg > (l_indexin[j] + indexoffset - detect_cnt)) - indicies[1] = j; - } - } - if (indicies[1] == -1) - { - if(SWXRegion[regionID]->band8flag == 2 || - SWXRegion[regionID]->band8flag == 3) - { - if(stopReg < (l_indexin[0] + indexoffset - detect_cnt)) - { - /*status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist); - buffer = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer); - indexoffset = buffer[0] * detect_cnt; - free(buffer); - startReg = startReg + (indexoffset - detect_cnt); - stopReg = stopReg + (indexoffset - 1); */ - if(stopReg >= (l_indexin[scene_cnt - 1] + indexoffset - detect_cnt)) - { - indicies[1] = scene_cnt - 1; - } - else - { - j = 0; - for(i=0;i= (l_indexin[j] + indexoffset - detect_cnt ) && - stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) - indicies[1] = j; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - } - } - - if(startReg > (l_indexin[j - 1] + indexoffset - detect_cnt )) - { - indicies[0] = -1; - indicies[1] = -1; - /*status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist); - buffer = (uint16 *)calloc(dims2[0], sizeof(uint16)); - status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer); - indexoffset = buffer[0] * detect_cnt; - free(buffer); - startReg = startReg - (indexoffset - detect_cnt); - stopReg = stopReg - (indexoffset - 1);*/ - j = 0; - for(i=0; i= (l_indexin[j] + indexoffset - detect_cnt) && - startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) ) - if(indicies[0] == -1) - indicies[0] = j; - if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && - stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) ) - indicies[1] = j + 1; - j = j + 2; - if(l_indexin[j] == 0 || l_indexin[j+1] == 0) - i = scene_cnt; - } - if(indicies[0] == -1) - if(startReg < (l_indexin[0] + indexoffset - detect_cnt)) - indicies[0] = 0; - if(indicies[1] == -1) - if(stopReg > (l_indexin[j - 1] + indexoffset - detect_cnt)) - indicies[1] = j - 1; - } - } - } - } /* end of if for floating scene update */ - else - { - /* If start of region is odd then increment */ - /* ---------------------------------------- */ - if (startReg % 2 == 1) - { - startReg++; - } - - /* If end of region is even then decrement */ - /* --------------------------------------- */ - if (stopReg % 2 == 0) - { - stopReg--; - } - - indicies[0]=startReg; - indicies[1]=stopReg; - } - } - - if (l_indexout != NULL) - { - if(SWXRegion[regionID]->scanflag == 1) - { - nout = (indicies[1] - indicies[0] + 1); - j = 0; - if (nout == 1) - l_indexout[0] = l_indexin[indicies[0]]; - for(i=0; i