LCOV - code coverage report
Current view: top level - frmts/hdf4/hdf-eos - SWapi.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 311 1029 30.2 %
Date: 2026-04-19 18:43:50 Functions: 15 30 50.0 %

          Line data    Source code
       1             : /*****************************************************************************
       2             :  *
       3             :  * This module has a number of additions and improvements over the original
       4             :  * implementation to be suitable for usage in GDAL HDF driver.
       5             :  *
       6             :  * Andrey Kiselev <dron@ak4719.spb.edu> is responsible for all the changes.
       7             :  ****************************************************************************/
       8             : 
       9             : /*
      10             : Copyright (C) 1996 Hughes and Applied Research Corporation
      11             : 
      12             : Permission to use, modify, and distribute this software and its documentation
      13             : for any purpose without fee is hereby granted, provided that the above
      14             : copyright notice appear in all copies and that both that copyright notice and
      15             : this permission notice appear in supporting documentation.
      16             : */
      17             : /*****************************************************************************
      18             : REVISIONS:
      19             : 
      20             : Aug 31, 1999  Abe Taaheri    Changed memory allocation for utility strings to
      21             :                              the size of UTLSTR_MAX_SIZE.
      22             :            Added error check for memory unavailability in
      23             :            several functions.
      24             :            Added check for NULL metabuf returned from
      25             :            EHmeta... functions. NULL pointer returned from
      26             :            EHmeta... functions indicate that memory could not
      27             :            be allocated for metabuf.
      28             : 
      29             : June 05, 2003 Abe Taaheri / Bruce Beaumont
      30             : 
      31             :                             Changed MAXNREGIONS to 1024 to support MOPITT data
      32             :           Supplied cast for compcode in call to
      33             :             SDsetcompress to avoid compiler error
      34             :           Removed declaration for unused variable rstatus
      35             :             in SWwrrdfield
      36             :           Removed initialization code for unused variables
      37             :             in SWwrrdfield
      38             :           Removed declaration for unused variable tmpVal
      39             :             in SWdefboxregion
      40             :           Added code in SWdefboxregion to check for l_index k
      41             :             exceeding NSWATHREGN to avoid overwriting
      42             :             memory
      43             :           Removed declaration for unused variable retchar
      44             :             in SWregionl_index
      45             :           Removed initialization code for unused variables
      46             :             in SWregionl_index
      47             :           Removed declarations for unused variables tstatus,
      48             :             nfields, nflgs, and swathname in SWextractregion
      49             :           Removed initialization code for unused variables
      50             :             in SWextractregion
      51             :           Removed declaration for unused variable
      52             :             land_status in SWscan2longlat
      53             :           Removed initialization code for unused variables
      54             :             in SWscan2longlat
      55             :           Added clear (0) of timeflag in SWextractperiod if
      56             :             return status from SWextractregion is non-zero
      57             :           Removed declarations for unused variables tstatus,
      58             :             scandim, ndfields, ndflds, and swathname in
      59             :             SWregioninfo
      60             :           Removed initialization code for unused variables
      61             :             in SWregioninfo
      62             :           Added clear (0) of timeflag in SWperiodinfo if
      63             :             return status from SWregioninfo is non-zero
      64             :           Removed declarations for unused variables size,
      65             :             nfields, nflds, nswath, idxsz, cornerlon, and
      66             :             cornerlat in SWdefscanregion
      67             :           Removed initialization code for unused variables
      68             :             in SWdefscanregion
      69             :           Removed declarations for unused variables dims2,
      70             :             rank, nt, swathname, dimlist, and buffer in
      71             :             SWupdateidxmap
      72             :           Removed declaration for unused variable statmeta
      73             :             in SWgeomapinfo
      74             : ******************************************************************************/
      75             : 
      76             : #include "cpl_port.h" /* for M_PI */
      77             : #include "cpl_string.h" /* for CPLsnprintf */
      78             : 
      79             : #include "mfhdf.h"
      80             : #include "hcomp.h"
      81             : #include "HdfEosDef.h"
      82             : #include <math.h>
      83             : 
      84             : #include "hdf4compat.h"
      85             : 
      86             : #define SWIDOFFSET 1048576
      87             : 
      88             : 
      89             : static int32 SWX1dcomb[512*3];
      90             : 
      91             : /* Added for routine that converts scanline to Lat/long
      92             : ** for floating scene subsetting
      93             : ** Jul 1999 DaW
      94             : */
      95             : #define RADOE 6371.0    /* Radius of Earth in Km */
      96             : 
      97             : #define NSWATH 200
      98             : /* Swath Structure External Arrays */
      99             : struct swathStructure
     100             : {
     101             :     int32 active;
     102             :     int32 IDTable;
     103             :     int32 VIDTable[3];
     104             :     int32 fid;
     105             :     int32 nSDS;
     106             :     int32 *sdsID;
     107             :     int32 compcode;
     108             :     intn  compparm[5];
     109             :     int32 tilecode;
     110             :     int32 tilerank;
     111             :     int32 tiledims[8];
     112             : };
     113             : static struct swathStructure SWXSwath[NSWATH];
     114             : 
     115             : 
     116             : 
     117             : #define NSWATHREGN 256
     118             : #define MAXNREGIONS 1024
     119             : struct swathRegion
     120             : {
     121             :     int32 fid;
     122             :     int32 swathID;
     123             :     int32 nRegions;
     124             :     int32 StartRegion[MAXNREGIONS];
     125             :     int32 StopRegion[MAXNREGIONS];
     126             :     int32 StartVertical[8];
     127             :     int32 StopVertical[8];
     128             :     int32 StartScan[8];
     129             :     int32 StopScan[8];
     130             :     char *DimNamePtr[8];
     131             :     intn band8flag;
     132             :     intn  scanflag;
     133             : };
     134             : static struct swathRegion *SWXRegion[NSWATHREGN];
     135             : 
     136             : /* define a macro for the string size of the utility strings. The value
     137             :    of 80 in previous version of this code was resulting in core dump (Array
     138             :    Bounds Write and Array Bounds Read problem in SWfinfo function and the
     139             :    functions called from there) for 7-8 dimensional fields where the
     140             :    string length for "DimList" can exceed 80 characters, including " and
     141             :    commas in the string. The length now is 512 which seems to be more
     142             :    than enough to avoid the problem mentioned above. */
     143             : 
     144             : #define UTLSTR_MAX_SIZE 512
     145             : 
     146             : /* Swath Prototypes (internal routines) */
     147             : static intn SWchkswid(int32, const char *, int32 *, int32 *, int32 *);
     148             : static int32 SWfinfo(int32, const char *, const char *, int32 *,
     149             :                      int32 [], int32 *, char *);
     150             : static intn SWwrrdattr(int32, const char *, int32, int32, const char *, VOIDP);
     151             : static intn SW1dfldsrch(int32, int32, const char *, const char *, int32 *,
     152             :                         int32 *, int32 *);
     153             : static intn SWSDfldsrch(int32, int32, const char *, int32 *, int32 *,
     154             :                         int32 *, int32 *, int32 [], int32 *);
     155             : static intn SWwrrdfield(int32, const char *, const char *,
     156             :                         int32 [], int32 [], int32 [], VOIDP);
     157             : static int32 SWinqfields(int32, const char *, char *, int32 [], int32 []);
     158             : 
     159             : /*----------------------------------------------------------------------------|
     160             : |  BEGIN_PROLOG                                                               |
     161             : |                                                                             |
     162             : |  FUNCTION: SWopen                                                           |
     163             : |                                                                             |
     164             : |  DESCRIPTION:                                                               |
     165             : |                                                                             |
     166             : |                                                                             |
     167             : |  Return Value    Type     Units     Description                             |
     168             : |  ============   ======  =========   =====================================   |
     169             : |  fid            int32               HDF-EOS file ID                         |
     170             : |                                                                             |
     171             : |  INPUTS:                                                                    |
     172             : |  filename       char                Filename                                |
     173             : |  access         intn                HDF access code                         |
     174             : |                                                                             |
     175             : |                                                                             |
     176             : |  OUTPUTS:                                                                   |
     177             : |             None                                                            |
     178             : |                                                                             |
     179             : |  NOTES:                                                                     |
     180             : |                                                                             |
     181             : |                                                                             |
     182             : |   Date     Programmer   Description                                         |
     183             : |  ======   ============  =================================================   |
     184             : |  Jun 96   Joel Gales    Original Programmer                                 |
     185             : |                                                                             |
     186             : |  END_PROLOG                                                                 |
     187             : -----------------------------------------------------------------------------*/
     188             : int32
     189          11 : SWopen(const char *filename, intn i_access)
     190             : 
     191             : {
     192             :     int32           fid /* HDF-EOS file ID */ ;
     193             : 
     194             :     /* Call EHopen to perform file access */
     195             :     /* ---------------------------------- */
     196          11 :     fid = EHopen(filename, i_access);
     197             : 
     198          11 :     return (fid);
     199             : }
     200             : 
     201             : /*----------------------------------------------------------------------------|
     202             : |  BEGIN_PROLOG                                                               |
     203             : |                                                                             |
     204             : |  FUNCTION: SWattach                                                         |
     205             : |                                                                             |
     206             : |  DESCRIPTION:  Attaches to an existing swath within the file.               |
     207             : |                                                                             |
     208             : |                                                                             |
     209             : |  Return Value    Type     Units     Description                             |
     210             : |  ============   ======  =========   =====================================   |
     211             : |  swathID        int32               swath structure ID                      |
     212             : |                                                                             |
     213             : |  INPUTS:                                                                    |
     214             : |  fid            int32               HDF-EOS file ID                         |
     215             : |  swathname      char                swath structure name                    |
     216             : |                                                                             |
     217             : |  OUTPUTS:                                                                   |
     218             : |             None                                                            |
     219             : |                                                                             |
     220             : |  NOTES:                                                                     |
     221             : |                                                                             |
     222             : |                                                                             |
     223             : |   Date     Programmer   Description                                         |
     224             : |  ======   ============  =================================================   |
     225             : |  Jun 96   Joel Gales    Original Programmer                                 |
     226             : |  Apr 99   David Wynne   Modified test for memory allocation check when no   |
     227             : |                         SDSs are in the Swath, NCR22513                     |
     228             : |                                                                             |
     229             : |  END_PROLOG                                                                 |
     230             : -----------------------------------------------------------------------------*/
     231             : int32
     232           3 : SWattach(int32 fid, const char *swathname)
     233             : 
     234             : {
     235             :     intn            i;    /* Loop index */
     236             :     intn            j;    /* Loop index */
     237           3 :     intn            nswathopen = 0; /* # of swath structures open */
     238             :     intn            status; /* routine return status variable */
     239             : 
     240             :     uint8           acs;  /* Read/Write file access code */
     241             : 
     242             :     int32           HDFfid; /* HDF file id */
     243             :     int32           vgRef;  /* Vgroup reference number */
     244             :     int32           vgid[4];  /* Vgroup ID array */
     245           3 :     int32           swathID = -1; /* HDF-EOS swath ID */
     246             :     int32          *tags; /* Pnt to Vgroup object tags array */
     247             :     int32          *refs; /* Pnt to Vgroup object refs array */
     248             :     int32           dum;  /* dummy variable */
     249             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     250             :     int32           nObjects; /* # of objects in Vgroup */
     251             :     int32           nSDS; /* SDS counter */
     252             :     int32           l_index;  /* SDS l_index */
     253             :     int32           sdid; /* SDS object ID */
     254           3 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     255             : 
     256             :     char            name[80]; /* Vgroup name */
     257             :     char            class[80];  /* Vgroup class */
     258             :     char            errbuf[256];/* Buffer for error message */
     259             :     char            acsCode[1]; /* Read/Write access char: "r/w" */
     260             : 
     261             : 
     262             :     /* Check HDF-EOS file ID, get back HDF file ID and access code */
     263             :     /* ----------------------------------------------------------- */
     264           3 :     status = EHchkfid(fid, swathname, &HDFfid, &dum, &acs);
     265             : 
     266             : 
     267           3 :     if (status == 0)
     268             :     {
     269             :   /* Convert numeric access code to character */
     270             :   /* ---------------------------------------- */
     271           3 :   acsCode[0] = (acs == 1) ? 'w' : 'r';
     272             : 
     273             :   /* Determine number of swaths currently opened */
     274             :   /* ------------------------------------------- */
     275         603 :   for (i = 0; i < NSWATH; i++)
     276             :   {
     277         600 :       nswathopen += SWXSwath[i].active;
     278             :   }
     279             : 
     280             :   /* If room for more ... */
     281             :   /* -------------------- */
     282           3 :   if (nswathopen < NSWATH)
     283             :   {
     284             : 
     285             :       /* Search Vgroups for Swath */
     286             :       /* ------------------------ */
     287           3 :       vgRef = -1;
     288             : 
     289             :       while (1)
     290             :       {
     291          15 :     vgRef = Vgetid(HDFfid, vgRef);
     292             : 
     293             : 
     294             :     /* If no more Vgroups then exist while loop */
     295             :     /* ---------------------------------------- */
     296          15 :     if (vgRef == -1)
     297             :     {
     298           0 :         break;
     299             :     }
     300             : 
     301             :     /* Get name and class of Vgroup */
     302             :     /* ---------------------------- */
     303          15 :     vgid[0] = Vattach(HDFfid, vgRef, "r");
     304          15 :     Vgetname(vgid[0], name);
     305          15 :     Vgetclass(vgid[0], class);
     306             : 
     307             : 
     308             :     /*
     309             :      * If Vgroup with swathname and class SWATH found, load
     310             :      * tables
     311             :      */
     312             : 
     313          15 :     if (strcmp(name, swathname) == 0 &&
     314           3 :         strcmp(class, "SWATH") == 0)
     315             :     {
     316             :         /* Attach to "Fields" and "Swath Attributes" Vgroups */
     317             :         /* ------------------------------------------------- */
     318           3 :         tags = (int32 *) malloc(sizeof(int32) * 3);
     319           3 :         if(tags == NULL)
     320             :         {
     321           0 :       HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     322           0 :       return(-1);
     323             :         }
     324           3 :         refs = (int32 *) malloc(sizeof(int32) * 3);
     325           3 :         if(refs == NULL)
     326             :         {
     327           0 :       HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     328           0 :       free(tags);
     329           0 :       return(-1);
     330             :         }
     331           3 :         Vgettagrefs(vgid[0], tags, refs, 3);
     332           3 :         vgid[1] = Vattach(HDFfid, refs[0], acsCode);
     333           3 :         vgid[2] = Vattach(HDFfid, refs[1], acsCode);
     334           3 :         vgid[3] = Vattach(HDFfid, refs[2], acsCode);
     335           3 :         free(tags);
     336           3 :         free(refs);
     337             : 
     338             :         /* Setup External Arrays */
     339             :         /* --------------------- */
     340           3 :         for (i = 0; i < NSWATH; i++)
     341             :         {
     342             :       /* Find empty entry in array */
     343             :       /* ------------------------- */
     344           3 :       if (SWXSwath[i].active == 0)
     345             :       {
     346             :           /*
     347             :            * Set swathID, Set swath entry active, Store
     348             :            * root Vgroup ID, Store sub Vgroup IDs, Store
     349             :            * HDF-EOS file ID
     350             :            */
     351           3 :           swathID = i + idOffset;
     352           3 :           SWXSwath[i].active = 1;
     353           3 :           SWXSwath[i].IDTable = vgid[0];
     354           3 :           SWXSwath[i].VIDTable[0] = vgid[1];
     355           3 :           SWXSwath[i].VIDTable[1] = vgid[2];
     356           3 :           SWXSwath[i].VIDTable[2] = vgid[3];
     357           3 :           SWXSwath[i].fid = fid;
     358           3 :           break;
     359             :       }
     360             :         }
     361             : 
     362             :         /* Get SDS interface ID */
     363             :         /* -------------------- */
     364           3 :         status = SWchkswid(swathID, "SWattach", &dum,
     365             :                &sdInterfaceID, &dum);
     366             : 
     367             : 
     368             :         /* Access swath "Geolocation" SDS */
     369             :         /* ------------------------------ */
     370             : 
     371             :         /* Get # of entries within this Vgroup & search for SDS */
     372             :         /* ---------------------------------------------------- */
     373           3 :         nObjects = Vntagrefs(vgid[1]);
     374             : 
     375           3 :         if (nObjects > 0)
     376             :         {
     377             :       /* Get tag and ref # for Geolocation Vgroup objects */
     378             :       /* ------------------------------------------------ */
     379           0 :       tags = (int32 *) malloc(sizeof(int32) * nObjects);
     380           0 :       if(tags == NULL)
     381             :       {
     382           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     383           0 :           return(-1);
     384             :       }
     385           0 :       refs = (int32 *) malloc(sizeof(int32) * nObjects);
     386           0 :       if(refs == NULL)
     387             :       {
     388           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     389           0 :           free(tags);
     390           0 :           return(-1);
     391             :       }
     392           0 :       Vgettagrefs(vgid[1], tags, refs, nObjects);
     393             : 
     394             :       /* Count number of SDS & allocate SDS ID array */
     395             :       /* ------------------------------------------- */
     396           0 :       nSDS = 0;
     397           0 :       for (j = 0; j < nObjects; j++)
     398             :       {
     399           0 :           if (tags[j] == DFTAG_NDG)
     400             :           {
     401           0 :         nSDS++;
     402             :           }
     403             :       }
     404           0 :       SWXSwath[i].sdsID = (int32 *) calloc(nSDS, 4);
     405           0 :       if(SWXSwath[i].sdsID == NULL && nSDS != 0)
     406             :       {
     407           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     408           0 :           free(tags);
     409           0 :           free(refs);
     410           0 :           return(-1);
     411             :       }
     412           0 :       nSDS = 0;
     413             : 
     414             : 
     415             :       /* Fill SDS ID array */
     416             :       /* ----------------- */
     417           0 :       for (j = 0; j < nObjects; j++)
     418             :       {
     419             :           /* If object is SDS then get id */
     420             :           /* ---------------------------- */
     421           0 :           if (tags[j] == DFTAG_NDG)
     422             :           {
     423           0 :         l_index = SDreftoindex(sdInterfaceID, refs[j]);
     424           0 :         sdid = SDselect(sdInterfaceID, l_index);
     425           0 :         SWXSwath[i].sdsID[nSDS] = sdid;
     426           0 :         nSDS++;
     427           0 :         SWXSwath[i].nSDS++;
     428             :           }
     429             :       }
     430           0 :       free(tags);
     431           0 :       free(refs);
     432             :         }
     433             : 
     434             :         /* Access swath "Data" SDS */
     435             :         /* ----------------------- */
     436             : 
     437             :         /* Get # of entries within this Vgroup & search for SDS */
     438             :         /* ---------------------------------------------------- */
     439           3 :         nObjects = Vntagrefs(vgid[2]);
     440             : 
     441           3 :         if (nObjects > 0)
     442             :         {
     443             :       /* Get tag and ref # for Data Vgroup objects */
     444             :       /* ----------------------------------------- */
     445           0 :       tags = (int32 *) malloc(sizeof(int32) * nObjects);
     446           0 :       if(tags == NULL)
     447             :       {
     448           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     449           0 :           return(-1);
     450             :       }
     451           0 :       refs = (int32 *) malloc(sizeof(int32) * nObjects);
     452           0 :       if(refs == NULL)
     453             :       {
     454           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     455           0 :           free(tags);
     456           0 :           return(-1);
     457             :       }
     458           0 :       Vgettagrefs(vgid[2], tags, refs, nObjects);
     459             : 
     460             : 
     461             :       /* Count number of SDS & allocate SDS ID array */
     462             :       /* ------------------------------------------- */
     463           0 :       nSDS = 0;
     464           0 :       for (j = 0; j < nObjects; j++)
     465             :       {
     466           0 :           if (tags[j] == DFTAG_NDG)
     467             :           {
     468           0 :         nSDS++;
     469             :           }
     470             :       }
     471           0 :       SWXSwath[i].sdsID = (int32 *)
     472           0 :           realloc((void *) SWXSwath[i].sdsID,
     473           0 :             (SWXSwath[i].nSDS + nSDS) * 4);
     474           0 :       if(SWXSwath[i].sdsID == NULL && nSDS != 0)
     475             :       {
     476           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     477           0 :           return(-1);
     478             :       }
     479             : 
     480             :       /* Fill SDS ID array */
     481             :       /* ----------------- */
     482           0 :       for (j = 0; j < nObjects; j++)
     483             :       {
     484             :           /* If object is SDS then get id */
     485             :           /* ---------------------------- */
     486           0 :           if (tags[j] == DFTAG_NDG)
     487             :           {
     488           0 :         l_index = SDreftoindex(sdInterfaceID, refs[j]);
     489           0 :         sdid = SDselect(sdInterfaceID, l_index);
     490           0 :         SWXSwath[i].sdsID[SWXSwath[i].nSDS] = sdid;
     491           0 :         SWXSwath[i].nSDS++;
     492             :           }
     493             :       }
     494           0 :       free(tags);
     495           0 :       free(refs);
     496             :         }
     497           3 :         break;
     498             :     }
     499             : 
     500             :     /* Detach Vgroup if not desired Swath */
     501             :     /* ---------------------------------- */
     502          12 :     Vdetach(vgid[0]);
     503             :       }
     504             : 
     505             :       /* If Swath not found then set up error message */
     506             :       /* -------------------------------------------- */
     507           3 :       if (swathID == -1)
     508             :       {
     509           0 :     HEpush(DFE_RANGE, "SWattach", __FILE__, __LINE__);
     510           0 :     HEreport("Swath: \"%s\" does not exist within HDF file.\n",
     511             :        swathname);
     512             :       }
     513             :   }
     514             :   else
     515             :   {
     516             :       /* Too many files opened */
     517             :       /* --------------------- */
     518           0 :       swathID = -1;
     519           0 :       strcpy(errbuf,
     520             :        "No more than %d swaths may be open simultaneously");
     521           0 :       strcat(errbuf, " (%s)");
     522           0 :       HEpush(DFE_DENIED, "SWattach", __FILE__, __LINE__);
     523           0 :       HEreport(errbuf, NSWATH, swathname);
     524             :   }
     525             : 
     526             :     }
     527           3 :     return (swathID);
     528             : }
     529             : 
     530             : /*----------------------------------------------------------------------------|
     531             : |  BEGIN_PROLOG                                                               |
     532             : |                                                                             |
     533             : |  FUNCTION: SWchkswid                                                        |
     534             : |                                                                             |
     535             : |  DESCRIPTION: Checks for valid swathID and returns file ID, SDS ID, and     |
     536             : |               swath Vgroup ID                                               |
     537             : |                                                                             |
     538             : |                                                                             |
     539             : |  Return Value    Type     Units     Description                             |
     540             : |  ============   ======  =========   =====================================   |
     541             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     542             : |                                                                             |
     543             : |  INPUTS:                                                                    |
     544             : |  swathID        int32               swath structure ID                      |
     545             : |  routname       char                Name of routine calling SWchkswid       |
     546             : |                                                                             |
     547             : |  OUTPUTS:                                                                   |
     548             : |  fid            int32               File ID                                 |
     549             : |  sdInterfaceID  int32               SDS interface ID                        |
     550             : |  swVgrpID       int32               swath Vgroup ID                         |
     551             : |                                                                             |
     552             : |  NOTES:                                                                     |
     553             : |                                                                             |
     554             : |                                                                             |
     555             : |   Date     Programmer   Description                                         |
     556             : |  ======   ============  =================================================   |
     557             : |  Jun 96   Joel Gales    Original Programmer                                 |
     558             : |                                                                             |
     559             : |  END_PROLOG                                                                 |
     560             : -----------------------------------------------------------------------------*/
     561             : static intn
     562          21 : SWchkswid(int32 swathID, const char *routname,
     563             :     int32 * fid, int32 * sdInterfaceID, int32 * swVgrpID)
     564             : 
     565             : {
     566          21 :     intn            status = 0; /* routine return status variable */
     567             :     uint8           l_access; /* Read/Write access code */
     568             : 
     569          21 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     570             : 
     571          21 :     char            message1[] =
     572             :     "Invalid swath id: %d in routine \"%s\".  ID must be >= %d and < %d.\n";
     573          21 :     char            message2[] =
     574             :     "Swath id %d in routine \"%s\" not active.\n";
     575             : 
     576             : 
     577             :     /* Check for valid swath id */
     578             :     /* ------------------------ */
     579          21 :     if (swathID < idOffset || swathID >= NSWATH + idOffset)
     580             :     {
     581           0 :   status = -1;
     582           0 :   HEpush(DFE_RANGE, "SWchkswid", __FILE__, __LINE__);
     583           0 :   HEreport(message1, swathID, routname, idOffset, NSWATH + idOffset);
     584             :     }
     585             :     else
     586             :     {
     587          21 :   int sID = swathID % idOffset;
     588             :   /* Check for active swath ID */
     589             :   /* ------------------------- */
     590          21 :   if (SWXSwath[sID].active == 0)
     591             :   {
     592           0 :       status = -1;
     593           0 :       HEpush(DFE_GENAPP, "SWchkswid", __FILE__, __LINE__);
     594           0 :       HEreport(message2, swathID, routname);
     595             :   }
     596             :   else
     597             :   {
     598             : 
     599             :       /* Get file & SDS ids and Swath Vgroup */
     600             :       /* ----------------------------------- */
     601          21 :       status = EHchkfid(SWXSwath[sID].fid, " ", fid,
     602             :             sdInterfaceID, &l_access);
     603          21 :       *swVgrpID = SWXSwath[sID].IDTable;
     604             :   }
     605             :     }
     606          21 :     return (status);
     607             : }
     608             : 
     609             : 
     610             : 
     611             : /*----------------------------------------------------------------------------|
     612             : |  BEGIN_PROLOG                                                               |
     613             : |                                                                             |
     614             : |  FUNCTION: SWdiminfo                                                        |
     615             : |                                                                             |
     616             : |  DESCRIPTION: Returns size in bytes of named dimension                      |
     617             : |                                                                             |
     618             : |                                                                             |
     619             : |  Return Value    Type     Units     Description                             |
     620             : |  ============   ======  =========   =====================================   |
     621             : |  size           int32               Size of dimension                       |
     622             : |                                                                             |
     623             : |  INPUTS:                                                                    |
     624             : |  swathID        int32               swath structure id                      |
     625             : |  dimname        char                Dimension name                          |
     626             : |                                                                             |
     627             : |                                                                             |
     628             : |  OUTPUTS:                                                                   |
     629             : |             None                                                            |
     630             : |                                                                             |
     631             : |  NOTES:                                                                     |
     632             : |                                                                             |
     633             : |                                                                             |
     634             : |   Date     Programmer   Description                                         |
     635             : |  ======   ============  =================================================   |
     636             : |  Jun 96   Joel Gales    Original Programmer                                 |
     637             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
     638             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
     639             : |                                                                             |
     640             : |  END_PROLOG                                                                 |
     641             : -----------------------------------------------------------------------------*/
     642             : int32
     643           0 : SWdiminfo(int32 swathID, const char *dimname)
     644             : 
     645             : {
     646             :     intn            status; /* routine return status variable */
     647             : 
     648             :     int32           fid;  /* HDF-EOS file ID */
     649             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     650             :     int32           swVgrpID; /* Swath root Vgroup ID */
     651             :     int32           size; /* Dimension size */
     652           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     653             : 
     654             : 
     655             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
     656             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
     657             :     char            swathname[80];  /* Swath Name */
     658             :     char           *utlstr; /* Utility string */
     659             : 
     660             : 
     661             :     /* Allocate space for utility string */
     662             :     /* --------------------------------- */
     663           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
     664           0 :     if(utlstr == NULL)
     665             :     {
     666           0 :   HEpush(DFE_NOSPACE,"SWdiminfo", __FILE__, __LINE__);
     667           0 :   return(-1);
     668             :     }
     669             :     /* Initialize return value */
     670           0 :     size = -1;
     671             : 
     672             :     /* Check Swath ID */
     673           0 :     status = SWchkswid(swathID, "SWdiminfo", &fid, &sdInterfaceID, &swVgrpID);
     674             : 
     675           0 :     if (status == 0)
     676             :     {
     677             :   /* Get swath name */
     678           0 :   int sID = swathID % idOffset;
     679           0 :   if (sID >= NSWATH)
     680             :   {
     681           0 :       free(utlstr);
     682           0 :       return -1;
     683             :   }
     684           0 :   Vgetname(SWXSwath[sID].IDTable, swathname);
     685             : 
     686             :   /* Get pointers to "Dimension" section within SM */
     687           0 :   metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
     688             :                "Dimension", metaptrs);
     689             : 
     690           0 :   if(metabuf == NULL)
     691             :   {
     692           0 :       free(utlstr);
     693           0 :       return(-1);
     694             :   }
     695             : 
     696             :   /* Search for dimension name (surrounded by quotes) */
     697           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", dimname, "\"\n");
     698           0 :   metaptrs[0] = strstr(metaptrs[0], utlstr);
     699             : 
     700             :   /*
     701             :    * If dimension found within swath structure then get dimension value
     702             :    */
     703           0 :   if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
     704             :   {
     705             :       /* Set endptr at end of dimension definition entry */
     706           0 :       metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
     707             : 
     708           0 :       status = EHgetmetavalue(metaptrs, "Size", utlstr);
     709             : 
     710           0 :       if (status == 0)
     711             :       {
     712           0 :     size = atoi(utlstr);
     713             :       }
     714             :       else
     715             :       {
     716           0 :     HEpush(DFE_GENAPP, "SWdiminfo", __FILE__, __LINE__);
     717           0 :     HEreport("\"Size\" string not found in metadata.\n");
     718             :       }
     719             :   }
     720             :   else
     721             :   {
     722           0 :       HEpush(DFE_GENAPP, "SWdiminfo", __FILE__, __LINE__);
     723           0 :       HEreport("Dimension \"%s\" not found.\n", dimname);
     724             :   }
     725             : 
     726           0 :   free(metabuf);
     727             :     }
     728           0 :     free(utlstr);
     729             : 
     730           0 :     return (size);
     731             : }
     732             : 
     733             : 
     734             : 
     735             : 
     736             : /*----------------------------------------------------------------------------|
     737             : |  BEGIN_PROLOG                                                               |
     738             : |                                                                             |
     739             : |  FUNCTION: SWmapinfo                                                        |
     740             : |                                                                             |
     741             : |  DESCRIPTION: Returns dimension mapping information                         |
     742             : |                                                                             |
     743             : |                                                                             |
     744             : |  Return Value    Type     Units     Description                             |
     745             : |  ============   ======  =========   =====================================   |
     746             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     747             : |                                                                             |
     748             : |  INPUTS:                                                                    |
     749             : |  swathID        int32               swath structure id                      |
     750             : |  geodim         char                geolocation dimension name              |
     751             : |  datadim        char                data dimension name                     |
     752             : |                                                                             |
     753             : |  OUTPUTS:                                                                   |
     754             : |  offset         int32               mapping offset                          |
     755             : |  increment      int32               mapping increment                       |
     756             : |                                                                             |
     757             : |  NOTES:                                                                     |
     758             : |                                                                             |
     759             : |                                                                             |
     760             : |   Date     Programmer   Description                                         |
     761             : |  ======   ============  =================================================   |
     762             : |  Jun 96   Joel Gales    Original Programmer                                 |
     763             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
     764             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
     765             : |                                                                             |
     766             : |  END_PROLOG                                                                 |
     767             : -----------------------------------------------------------------------------*/
     768             : intn
     769           0 : SWmapinfo(int32 swathID, const char *geodim, const char *datadim, int32 * offset,
     770             :     int32 * increment)
     771             : 
     772             : {
     773             :     intn            status; /* routine return status variable */
     774           0 :     intn            statmeta = 0; /* EHgetmetavalue return status */
     775             : 
     776             :     int32           fid;  /* HDF-EOS file ID */
     777             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     778             :     int32           swVgrpID; /* Swath root Vgroup ID */
     779           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     780             : 
     781             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
     782             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
     783             :     char            swathname[80];  /* Swath Name */
     784             :     char           *utlstr; /* Utility string */
     785             : 
     786             : 
     787             :     /* Allocate space for utility string */
     788             :     /* --------------------------------- */
     789           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
     790           0 :     if(utlstr == NULL)
     791             :     {
     792           0 :   HEpush(DFE_NOSPACE,"SWmapinfo", __FILE__, __LINE__);
     793           0 :   return(-1);
     794             :     }
     795             :     /* Initialize return values */
     796           0 :     *offset = -1;
     797           0 :     *increment = -1;
     798             : 
     799             :     /* Check Swath ID */
     800           0 :     status = SWchkswid(swathID, "SWmapinfo", &fid, &sdInterfaceID, &swVgrpID);
     801             : 
     802           0 :     if (status == 0)
     803             :     {
     804             :   /* Get swath name */
     805           0 :   int sID = swathID % idOffset;
     806           0 :   if (sID >= NSWATH)
     807             :   {
     808           0 :       free(utlstr);
     809           0 :       return -1;
     810             :   }
     811           0 :   Vgetname(SWXSwath[sID].IDTable, swathname);
     812             : 
     813             :   /* Get pointers to "DimensionMap" section within SM */
     814           0 :   metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
     815             :                "DimensionMap", metaptrs);
     816           0 :   if(metabuf == NULL)
     817             :   {
     818           0 :       free(utlstr);
     819           0 :       return(-1);
     820             :   }
     821             : 
     822             :   /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
     823           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
     824             :     "\"\n\t\t\t\tDataDimension=\"", datadim, "\"\n");
     825           0 :   metaptrs[0] = strstr(metaptrs[0], utlstr);
     826             : 
     827             :   /*
     828             :    * If mapping found within swath structure then get offset and
     829             :    * increment value
     830             :    */
     831           0 :   if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
     832             :   {
     833             :       /* Get Offset */
     834           0 :       statmeta = EHgetmetavalue(metaptrs, "Offset", utlstr);
     835           0 :       if (statmeta == 0)
     836             :       {
     837           0 :     *offset = atoi(utlstr);
     838             :       }
     839             :       else
     840             :       {
     841           0 :     status = -1;
     842           0 :     HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
     843           0 :     HEreport("\"Offset\" string not found in metadata.\n");
     844             :       }
     845             : 
     846             : 
     847             :       /* Get Increment */
     848           0 :       statmeta = EHgetmetavalue(metaptrs, "Increment", utlstr);
     849           0 :       if (statmeta == 0)
     850             :       {
     851           0 :     *increment = atoi(utlstr);
     852             :       }
     853             :       else
     854             :       {
     855           0 :     status = -1;
     856           0 :     HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
     857           0 :     HEreport("\"Increment\" string not found in metadata.\n");
     858             :       }
     859             :   }
     860             :   else
     861             :   {
     862           0 :       status = -1;
     863           0 :       HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
     864           0 :       HEreport("Mapping \"%s/%s\" not found.\n", geodim, datadim);
     865             :   }
     866             : 
     867           0 :   free(metabuf);
     868             :     }
     869           0 :     free(utlstr);
     870           0 :     return (status);
     871             : }
     872             : 
     873             : 
     874             : 
     875             : 
     876             : /*----------------------------------------------------------------------------|
     877             : |  BEGIN_PROLOG                                                               |
     878             : |                                                                             |
     879             : |  FUNCTION: SWidxmapinfo                                                     |
     880             : |                                                                             |
     881             : |  DESCRIPTION: Returns l_indexed mapping information                           |
     882             : |                                                                             |
     883             : |                                                                             |
     884             : |  Return Value    Type     Units     Description                             |
     885             : |  ============   ======  =========   =====================================   |
     886             : |  gsize          int32               Number of l_index values (sz of geo dim)  |
     887             : |                                                                             |
     888             : |  INPUTS:                                                                    |
     889             : |  swathID        int32               swath structure id                      |
     890             : |  geodim         char                geolocation dimension name              |
     891             : |  datadim        char                data dimension name                     |
     892             : |                                                                             |
     893             : |                                                                             |
     894             : |  OUTPUTS:                                                                   |
     895             : |  l_index          int32               array of l_index values                   |
     896             : |                                                                             |
     897             : |  NOTES:                                                                     |
     898             : |                                                                             |
     899             : |                                                                             |
     900             : |   Date     Programmer   Description                                         |
     901             : |  ======   ============  =================================================   |
     902             : |  Jun 96   Joel Gales    Original Programmer                                 |
     903             : |                                                                             |
     904             : |  END_PROLOG                                                                 |
     905             : -----------------------------------------------------------------------------*/
     906             : int32
     907           0 : SWidxmapinfo(int32 swathID, const char *geodim, const char *datadim, int32 l_index[])
     908             : {
     909             :     intn            status; /* routine return status variable */
     910             : 
     911             :     int32           fid;  /* HDF-EOS file ID */
     912             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     913             :     int32           swVgrpID; /* Swath root Vgroup ID */
     914           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     915             :     int32           vgid; /* Swath Attributes Vgroup ID */
     916             :     int32           vdataID;  /* Index Mapping Vdata ID */
     917           0 :     int32           gsize = -1; /* Size of geo dim */
     918             : 
     919             :     char            utlbuf[256];/* Utility buffer */
     920             : 
     921             : 
     922             :     /* Check Swath ID */
     923           0 :     status = SWchkswid(swathID, "SWidxmapinfo",
     924             :            &fid, &sdInterfaceID, &swVgrpID);
     925             : 
     926           0 :     if (status == 0)
     927             :     {
     928             :   /* Find Index Mapping Vdata with Swath Attributes Vgroup */
     929           0 :   snprintf(utlbuf, sizeof(utlbuf), "%s%s%s%s", "INDXMAP:", geodim, "/", datadim);
     930           0 :   int sID = swathID % idOffset;
     931           0 :   if (sID >= NSWATH)
     932             :   {
     933           0 :       return -1;
     934             :   }
     935           0 :   vgid = SWXSwath[sID].VIDTable[2];
     936           0 :   vdataID = EHgetid(fid, vgid, utlbuf, 1, "r");
     937             : 
     938             :   /* If found then get geodim size & read l_index mapping values */
     939           0 :   if (vdataID != -1)
     940             :   {
     941           0 :       gsize = SWdiminfo(swathID, geodim);
     942             : 
     943           0 :       VSsetfields(vdataID, "Index");
     944           0 :       VSread(vdataID, (uint8 *) l_index, 1, FULL_INTERLACE);
     945           0 :       VSdetach(vdataID);
     946             :   }
     947             :   else
     948             :   {
     949             :       /*status = -1;*/
     950           0 :       HEpush(DFE_GENAPP, "SWidxmapinfo", __FILE__, __LINE__);
     951           0 :       HEreport("Index Mapping \"%s\" not found.\n", utlbuf);
     952             :   }
     953             :     }
     954           0 :     return (gsize);
     955             : }
     956             : 
     957             : 
     958             : 
     959             : 
     960             : /*----------------------------------------------------------------------------|
     961             : |  BEGIN_PROLOG                                                               |
     962             : |                                                                             |
     963             : |  FUNCTION: SWcompinfo                                                       |
     964             : |                                                                             |
     965             : |  DESCRIPTION:                                                               |
     966             : |                                                                             |
     967             : |                                                                             |
     968             : |  Return Value    Type     Units     Description                             |
     969             : |  ============   ======  =========   =====================================   |
     970             : |  status         intn                                                        |
     971             : |                                                                             |
     972             : |  INPUTS:                                                                    |
     973             : |  swathID        int32                                                       |
     974             : |  compcode       int32                                                       |
     975             : |  compparm       intn                                                        |
     976             : |                                                                             |
     977             : |                                                                             |
     978             : |  OUTPUTS:                                                                   |
     979             : |             None                                                            |
     980             : |                                                                             |
     981             : |  NOTES:                                                                     |
     982             : |                                                                             |
     983             : |                                                                             |
     984             : |   Date     Programmer   Description                                         |
     985             : |  ======   ============  =================================================   |
     986             : |  Oct 96   Joel Gales    Original Programmer                                 |
     987             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
     988             : |                                                                             |
     989             : |  END_PROLOG                                                                 |
     990             : -----------------------------------------------------------------------------*/
     991             : intn
     992           0 : SWcompinfo(int32 swathID, const char *fieldname, int32 * compcode, intn compparm[])
     993             : {
     994             :     intn            i;    /* Loop Index */
     995             :     intn            status; /* routine return status variable */
     996           0 :     intn            statmeta = 0; /* EHgetmetavalue return status */
     997             : 
     998             :     int32           fid;  /* HDF-EOS file ID */
     999             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1000             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1001           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1002             : 
    1003             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    1004             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    1005             :     char            swathname[80];  /* Swath Name */
    1006             :     char           *utlstr;     /* Utility string */
    1007             : 
    1008           0 :     const char           *HDFcomp[5] = {"HDFE_COMP_NONE", "HDFE_COMP_RLE",
    1009             :   "HDFE_COMP_NBIT", "HDFE_COMP_SKPHUFF",
    1010             :     "HDFE_COMP_DEFLATE"}; /* Compression Codes */
    1011             : 
    1012             :     /* Allocate space for utility string */
    1013             :     /* --------------------------------- */
    1014           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    1015           0 :     if(utlstr == NULL)
    1016             :     {
    1017           0 :   HEpush(DFE_NOSPACE,"SWcompinfo", __FILE__, __LINE__);
    1018           0 :   return(-1);
    1019             :     }
    1020             : 
    1021             :     /* Check Swath ID */
    1022           0 :     status = SWchkswid(swathID, "SWcompinfo",
    1023             :            &fid, &sdInterfaceID, &swVgrpID);
    1024             : 
    1025           0 :     if (status == 0)
    1026             :     {
    1027             :   /* Get swath name */
    1028           0 :   int sID = swathID % idOffset;
    1029           0 :   if (sID >= NSWATH)
    1030             :   {
    1031           0 :       free(utlstr);
    1032           0 :       return -1;
    1033             :   }
    1034           0 :   Vgetname(SWXSwath[sID].IDTable, swathname);
    1035             : 
    1036             :   /* Get pointers to "DataField" section within SM */
    1037           0 :   metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    1038             :                "DataField", metaptrs);
    1039           0 :   if(metabuf == NULL)
    1040             :   {
    1041           0 :       free(utlstr);
    1042           0 :       return(-1);
    1043             :   }
    1044             :   /* Search for field */
    1045           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n");
    1046           0 :   metaptrs[0] = strstr(metaptrs[0], utlstr);
    1047             : 
    1048             :   /* If not found then search in "GeoField" section */
    1049           0 :   if (metaptrs[0] > metaptrs[1] || metaptrs[0] == NULL)
    1050             :   {
    1051           0 :       free(metabuf);
    1052             : 
    1053             :       /* Get pointers to "GeoField" section within SM */
    1054           0 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    1055             :              "GeoField", metaptrs);
    1056           0 :       if(metabuf == NULL)
    1057             :       {
    1058           0 :     free(utlstr);
    1059           0 :     return(-1);
    1060             :       }
    1061             :       /* Search for field */
    1062           0 :       snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n");
    1063           0 :       metaptrs[0] = strstr(metaptrs[0], utlstr);
    1064             :   }
    1065             : 
    1066             : 
    1067             :   /* If field found and user wants compression code ... */
    1068           0 :   if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    1069             :   {
    1070           0 :       if (compcode != NULL)
    1071             :       {
    1072             :     /* Set endptr at end of field's definition entry */
    1073           0 :     metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
    1074             : 
    1075             :     /* Get compression type */
    1076           0 :     statmeta = EHgetmetavalue(metaptrs, "CompressionType", utlstr);
    1077             : 
    1078             :     /*
    1079             :      * Default is no compression if "CompressionType" string not
    1080             :      * in metadata
    1081             :      */
    1082           0 :     *compcode = HDFE_COMP_NONE;
    1083             : 
    1084             :     /* If compression code is found ... */
    1085           0 :     if (statmeta == 0)
    1086             :     {
    1087             :         /* Loop through compression types until match */
    1088           0 :         for (i = 0; i < 5; i++)
    1089             :         {
    1090           0 :       if (strcmp(utlstr, HDFcomp[i]) == 0)
    1091             :       {
    1092           0 :           *compcode = i;
    1093           0 :           break;
    1094             :       }
    1095             :         }
    1096             :     }
    1097             :       }
    1098             : 
    1099             :       /* If user wants compression parameters ... */
    1100           0 :       if (compparm != NULL && compcode != NULL)
    1101             :       {
    1102             :     /* Initialize to zero */
    1103           0 :     for (i = 0; i < 4; i++)
    1104             :     {
    1105           0 :         compparm[i] = 0;
    1106             :     }
    1107             : 
    1108             :     /*
    1109             :      * Get compression parameters if NBIT or DEFLATE compression
    1110             :      */
    1111           0 :     if (*compcode == HDFE_COMP_NBIT)
    1112             :     {
    1113             :         statmeta =
    1114           0 :       EHgetmetavalue(metaptrs, "CompressionParams", utlstr);
    1115           0 :         if (statmeta == 0)
    1116             :         {
    1117           0 :       sscanf(utlstr, "(%d,%d,%d,%d)",
    1118             :              &compparm[0], &compparm[1],
    1119             :              &compparm[2], &compparm[3]);
    1120             :         }
    1121             :         else
    1122             :         {
    1123           0 :       status = -1;
    1124           0 :       HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
    1125           0 :       HEreport(
    1126             :          "\"CompressionParams\" string not found in metadata.\n");
    1127             :         }
    1128             :     }
    1129           0 :     else if (*compcode == HDFE_COMP_DEFLATE)
    1130             :     {
    1131             :         statmeta =
    1132           0 :       EHgetmetavalue(metaptrs, "DeflateLevel", utlstr);
    1133           0 :         if (statmeta == 0)
    1134             :         {
    1135           0 :       sscanf(utlstr, "%d", &compparm[0]);
    1136             :         }
    1137             :         else
    1138             :         {
    1139           0 :       status = -1;
    1140           0 :       HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
    1141           0 :       HEreport(
    1142             :       "\"DeflateLevel\" string not found in metadata.\n");
    1143             :         }
    1144             :     }
    1145             :       }
    1146             :   }
    1147             :   else
    1148             :   {
    1149           0 :       HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
    1150           0 :       HEreport("Fieldname \"%s\" not found.\n", fieldname);
    1151             :   }
    1152             : 
    1153           0 :   free(metabuf);
    1154             :     }
    1155           0 :     free(utlstr);
    1156             : 
    1157           0 :     return (status);
    1158             : }
    1159             : 
    1160             : 
    1161             : 
    1162             : /*----------------------------------------------------------------------------|
    1163             : |  BEGIN_PROLOG                                                               |
    1164             : |                                                                             |
    1165             : |  FUNCTION: SWfinfo                                                          |
    1166             : |                                                                             |
    1167             : |  DESCRIPTION: Returns field info                                            |
    1168             : |                                                                             |
    1169             : |                                                                             |
    1170             : |  Return Value    Type     Units     Description                             |
    1171             : |  ============   ======  =========   =====================================   |
    1172             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1173             : |                                                                             |
    1174             : |  INPUTS:                                                                    |
    1175             : |  swathID        int32               swath structure id                      |
    1176             : |  fieldtype      const char          fieldtype (geo or data)                 |
    1177             : |  fieldname      const char          name of field                           |
    1178             : |                                                                             |
    1179             : |                                                                             |
    1180             : |  OUTPUTS:                                                                   |
    1181             : |  rank           int32               rank of field (# of dims)               |
    1182             : |  dims           int32               field dimensions                        |
    1183             : |  numbertype     int32               field number type                       |
    1184             : |  dimlist        char                field dimension list                    |
    1185             : |                                                                             |
    1186             : |  NOTES:                                                                     |
    1187             : |                                                                             |
    1188             : |                                                                             |
    1189             : |   Date     Programmer   Description                                         |
    1190             : |  ======   ============  =================================================   |
    1191             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1192             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    1193             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
    1194             : |                                                                             |
    1195             : |  END_PROLOG                                                                 |
    1196             : -----------------------------------------------------------------------------*/
    1197             : static int32
    1198           2 : SWfinfo(int32 swathID, const char *fieldtype, const char *fieldname,
    1199             :         int32 *rank, int32 dims[], int32 *numbertype, char *dimlist)
    1200             : 
    1201             : {
    1202             :     intn            i;    /* Loop index */
    1203             :     intn            j;    /* Loop index */
    1204             :     intn            status; /* routine return status variable */
    1205           2 :     intn            statmeta = 0; /* EHgetmetavalue return status */
    1206             : 
    1207             :     int32           fid;  /* HDF-EOS file ID */
    1208             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1209           2 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1210             :     int32           fsize;  /* field size in bytes */
    1211           2 :     int32           ndims = 0;  /* Number of dimensions */
    1212             :     int32           slen[8];  /* Length of each entry in parsed string */
    1213             :     int32           dum;  /* Dummy variable */
    1214             :     int32           vdataID;  /* 1d field vdata ID */
    1215             : 
    1216             :     uint8          *buf;  /* One-Dim field buffer */
    1217             : 
    1218             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    1219             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    1220             :     char            swathname[80];  /* Swath Name */
    1221             :     char           *utlstr; /* Utility string */
    1222             :     char           *ptr[8]; /* String pointers for parsed string */
    1223             :     char            dimstr[64]; /* Individual dimension entry string */
    1224             : 
    1225             : 
    1226             :     /* Allocate space for utility string */
    1227             :     /* --------------------------------- */
    1228           2 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    1229           2 :     if(utlstr == NULL)
    1230             :     {
    1231           0 :   HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__);
    1232           0 :   return(-1);
    1233             :     }
    1234             : 
    1235             :     /* Initialize rank and numbertype to -1 (error) */
    1236             :     /* -------------------------------------------- */
    1237           2 :     *rank = -1;
    1238           2 :     *numbertype = -1;
    1239             : 
    1240             :     /* Get HDF-EOS file ID and SDS interface ID */
    1241           2 :     status = SWchkswid(swathID, "SWfinfo", &fid, &sdInterfaceID, &dum);
    1242             : 
    1243             :     /* Get swath name */
    1244           2 :     int sID = swathID % idOffset;
    1245           2 :     if (sID >= NSWATH)
    1246             :     {
    1247           0 :         free(utlstr);
    1248           0 :         return -1;
    1249             :     }
    1250           2 :     Vgetname(SWXSwath[sID].IDTable, swathname);
    1251             : 
    1252             :     /* Get pointers to appropriate "Field" section within SM */
    1253           2 :     if (strcmp(fieldtype, "Geolocation Fields") == 0)
    1254             :     {
    1255           1 :   metabuf = EHmetagroup(sdInterfaceID, swathname, "s",
    1256             :                "GeoField", metaptrs);
    1257             : 
    1258           1 :   if(metabuf == NULL)
    1259             :   {
    1260           0 :       free(utlstr);
    1261           0 :       return(-1);
    1262             :   }
    1263             :     }
    1264             :     else
    1265             :     {
    1266           1 :   metabuf = EHmetagroup(sdInterfaceID, swathname, "s",
    1267             :                "DataField", metaptrs);
    1268           1 :   if(metabuf == NULL)
    1269             :   {
    1270           0 :       free(utlstr);
    1271           0 :       return(-1);
    1272             :   }
    1273             :     }
    1274             : 
    1275             : 
    1276             :     /* Search for field */
    1277           2 :     snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n");
    1278           2 :     metaptrs[0] = strstr(metaptrs[0], utlstr);
    1279             : 
    1280             :     /* If field found ... */
    1281           2 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    1282             :     {
    1283             :   /* Get DataType string */
    1284           1 :   statmeta = EHgetmetavalue(metaptrs, "DataType", utlstr);
    1285             : 
    1286             :   /* Convert to numbertype code */
    1287           1 :   if (statmeta == 0)
    1288           1 :       *numbertype = EHnumstr(utlstr);
    1289             :   else
    1290             :   {
    1291           0 :       status = -1;
    1292           0 :       HEpush(DFE_GENAPP, "SWfinfo", __FILE__, __LINE__);
    1293           0 :       HEreport("\"DataType\" string not found in metadata.\n");
    1294             :   }
    1295             : 
    1296             : 
    1297             :   /*
    1298             :    * Get DimList string and trim off leading and trailing parens "()"
    1299             :    */
    1300           1 :   statmeta = EHgetmetavalue(metaptrs, "DimList", utlstr);
    1301             : 
    1302           1 :   if (statmeta == 0)
    1303             :   {
    1304           1 :       const size_t len = strlen(utlstr);
    1305           1 :       if (len >= 2 && utlstr[0] == '(' && utlstr[len-1] == ')')
    1306             :       {
    1307           0 :           memmove(utlstr, utlstr + 1, len - 2);
    1308           0 :           utlstr[len - 2] = '\0';
    1309             :       }
    1310             : 
    1311             :       /* Parse trimmed DimList string and get rank */
    1312           1 :       ndims = EHparsestr(utlstr, ',', ptr, slen);
    1313           1 :       *rank = ndims;
    1314             :   }
    1315             :   else
    1316             :   {
    1317           0 :       status = -1;
    1318           0 :       HEpush(DFE_GENAPP, "SWfinfo", __FILE__, __LINE__);
    1319           0 :       HEreport("\"DimList\" string not found in metadata.\n");
    1320             :   }
    1321             : 
    1322             :   /* If dimension list is desired by user then initialize length to 0 */
    1323           1 :   if (dimlist != NULL)
    1324             :   {
    1325           0 :       dimlist[0] = 0;
    1326             :   }
    1327             : 
    1328             :   /*
    1329             :    * Copy each entry in DimList and remove leading and trailing quotes,
    1330             :    * Get dimension sizes and concatenate dimension names to dimension
    1331             :    * list
    1332             :    */
    1333           1 :   for (i = 0; i < ndims; i++)
    1334             :   {
    1335           0 :       memcpy(dimstr, ptr[i] + 1, slen[i] - 2);
    1336           0 :       dimstr[slen[i] - 2] = 0;
    1337           0 :       dims[i] = SWdiminfo(swathID, dimstr);
    1338           0 :       if (dimlist != NULL)
    1339             :       {
    1340           0 :     if (i > 0)
    1341             :     {
    1342           0 :         strcat(dimlist, ",");
    1343             :     }
    1344           0 :     strcat(dimlist, dimstr);
    1345             :       }
    1346             : 
    1347             :   }
    1348             : 
    1349             : 
    1350             :   /* Appendable Field Section */
    1351             :   /* ------------------------ */
    1352           1 :   if (dims[0] == 0)
    1353             :   {
    1354             :       /* One-Dimensional Field */
    1355           1 :       if (*rank == 1)
    1356             :       {
    1357             :     /* Get vdata ID */
    1358           0 :     status = SW1dfldsrch(fid, swathID, fieldname, "r",
    1359             :              &dum, &vdataID, &dum);
    1360             : 
    1361             :     /* Get actual size of field */
    1362           0 :     dims[0] = VSelts(vdataID);
    1363             : 
    1364             :     /*
    1365             :      * If size=1 then check where actual record of
    1366             :      * "initialization" record
    1367             :      */
    1368           0 :     if (dims[0] == 1)
    1369             :     {
    1370             :         /* Get record size and read 1st record */
    1371           0 :         fsize = VSsizeof(vdataID, (char *)fieldname);
    1372           0 :         buf = (uint8 *) calloc(fsize, 1);
    1373           0 :         if(buf == NULL)
    1374             :         {
    1375           0 :       HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__);
    1376           0 :       free(utlstr);
    1377           0 :       return(-1);
    1378             :         }
    1379           0 :         VSsetfields(vdataID, fieldname);
    1380           0 :         VSseek(vdataID, 0);
    1381           0 :         VSread(vdataID, (uint8 *) buf, 1, FULL_INTERLACE);
    1382             : 
    1383             :         /* Sum up "bytes" in record */
    1384           0 :         for (i = 0, j = 0; i < fsize; i++)
    1385             :         {
    1386           0 :       j += buf[i];
    1387             :         }
    1388             : 
    1389             :         /*
    1390             :          * If filled with 255 then "initialization" record,
    1391             :          * actual number of records = 0
    1392             :          */
    1393           0 :         if (j == 255 * fsize)
    1394             :         {
    1395           0 :       dims[0] = 0;
    1396             :         }
    1397             : 
    1398           0 :         free(buf);
    1399             :     }
    1400             :     /* Detach from 1d field */
    1401           0 :     VSdetach(vdataID);
    1402             :       }
    1403             :       else
    1404             :       {
    1405             :     /* Get actual size of Multi-Dimensional Field */
    1406           1 :     status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
    1407             :              &dum, &dum, &dum, &dum, dims,
    1408             :              &dum);
    1409             :       }
    1410             :   }
    1411             :     }
    1412           2 :     free(metabuf);
    1413             : 
    1414           2 :     if (*rank == -1)
    1415             :     {
    1416           1 :   status = -1;
    1417             :     }
    1418           2 :     free(utlstr);
    1419             : 
    1420           2 :     return (status);
    1421             : }
    1422             : 
    1423             : 
    1424             : 
    1425             : 
    1426             : 
    1427             : /*----------------------------------------------------------------------------|
    1428             : |  BEGIN_PROLOG                                                               |
    1429             : |                                                                             |
    1430             : |  FUNCTION: SWfieldinfo                                                      |
    1431             : |                                                                             |
    1432             : |  DESCRIPTION: Wrapper around SWfinfo                                        |
    1433             : |                                                                             |
    1434             : |                                                                             |
    1435             : |  Return Value    Type     Units     Description                             |
    1436             : |  ============   ======  =========   =====================================   |
    1437             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1438             : |                                                                             |
    1439             : |  INPUTS:                                                                    |
    1440             : |  swathID        int32               swath structure id                      |
    1441             : |  fieldname      const char          name of field                           |
    1442             : |                                                                             |
    1443             : |                                                                             |
    1444             : |  OUTPUTS:                                                                   |
    1445             : |  rank           int32               rank of field (# of dims)               |
    1446             : |  dims           int32               field dimensions                        |
    1447             : |  numbertype     int32               field number type                       |
    1448             : |  dimlist        char                field dimension list                    |
    1449             : |                                                                             |
    1450             : |  NOTES:                                                                     |
    1451             : |                                                                             |
    1452             : |                                                                             |
    1453             : |   Date     Programmer   Description                                         |
    1454             : |  ======   ============  =================================================   |
    1455             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1456             : |                                                                             |
    1457             : |  END_PROLOG                                                                 |
    1458             : -----------------------------------------------------------------------------*/
    1459             : intn
    1460           1 : SWfieldinfo(int32 swathID, const char *fieldname, int32 * rank, int32 dims[],
    1461             :       int32 * numbertype, char *dimlist)
    1462             : 
    1463             : {
    1464             :     intn            status; /* routine return status variable */
    1465             : 
    1466             :     int32           fid;  /* HDF-EOS file ID */
    1467             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1468             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1469             : 
    1470             : 
    1471             :     /* Check for valid swath id */
    1472           1 :     status = SWchkswid(swathID, "SWfieldinfo", &fid,
    1473             :            &sdInterfaceID, &swVgrpID);
    1474           1 :     if (status == 0)
    1475             :     {
    1476             :   /* Check for field within Geolocatation Fields */
    1477           1 :   status = SWfinfo(swathID, "Geolocation Fields", fieldname,
    1478             :        rank, dims, numbertype, dimlist);
    1479             : 
    1480             :   /* If not there then check within Data Fields */
    1481           1 :   if (status == -1)
    1482             :   {
    1483           1 :       status = SWfinfo(swathID, "Data Fields", fieldname,
    1484             :            rank, dims, numbertype, dimlist);
    1485             :   }
    1486             : 
    1487             :   /* If not there either then can't be found */
    1488           1 :   if (status == -1)
    1489             :   {
    1490           1 :       HEpush(DFE_GENAPP, "SWfieldinfo", __FILE__, __LINE__);
    1491           1 :       HEreport("Fieldname \"%s\" not found.\n", fieldname);
    1492             :   }
    1493             :     }
    1494           1 :     return (status);
    1495             : }
    1496             : 
    1497             : 
    1498             : 
    1499             : /*----------------------------------------------------------------------------|
    1500             : |  BEGIN_PROLOG                                                               |
    1501             : |                                                                             |
    1502             : |  FUNCTION: SWwrrdattr                                                       |
    1503             : |                                                                             |
    1504             : |  DESCRIPTION:                                                               |
    1505             : |                                                                             |
    1506             : |                                                                             |
    1507             : |  Return Value    Type     Units     Description                             |
    1508             : |  ============   ======  =========   =====================================   |
    1509             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1510             : |                                                                             |
    1511             : |  INPUTS:                                                                    |
    1512             : |  swathID        int32               swath structure ID                      |
    1513             : |  attrname       char                attribute name                          |
    1514             : |  numbertype     int32               attribute HDF numbertype                |
    1515             : |  count          int32               Number of attribute elements            |
    1516             : |  wrcode         char                Read/Write Code "w/r"                   |
    1517             : |  datbuf         void                I/O buffer                              |
    1518             : |                                                                             |
    1519             : |  OUTPUTS:                                                                   |
    1520             : |  datbuf                                                                     |
    1521             : |                                                                             |
    1522             : |  NOTES:                                                                     |
    1523             : |                                                                             |
    1524             : |                                                                             |
    1525             : |   Date     Programmer   Description                                         |
    1526             : |  ======   ============  =================================================   |
    1527             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1528             : |  Oct 96   Joel Gales    Get Attribute Vgroup ID from external array         |
    1529             : |                                                                             |
    1530             : |  END_PROLOG                                                                 |
    1531             : -----------------------------------------------------------------------------*/
    1532             : static intn
    1533           0 : SWwrrdattr(int32 swathID, const char *attrname, int32 numbertype, int32 count,
    1534             :      const char *wrcode, VOIDP datbuf)
    1535             : 
    1536             : {
    1537             :     intn            status; /* routine return status variable */
    1538             : 
    1539             :     int32           fid;  /* HDF-EOS file ID */
    1540             :     int32           attrVgrpID; /* Swath attribute ID */
    1541             :     int32           dum;  /* dummy variable */
    1542           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1543             : 
    1544             :     /* Check Swath id */
    1545           0 :     status = SWchkswid(swathID, "SWwrrdattr", &fid, &dum, &dum);
    1546             : 
    1547           0 :     if (status == 0)
    1548             :     {
    1549             :   /* Get attribute Vgroup ID and call EHattr to perform I/O */
    1550             :   /* ------------------------------------------------------ */
    1551           0 :   int sID = swathID % idOffset;
    1552           0 :   if (sID >= NSWATH)
    1553             :   {
    1554           0 :       return -1;
    1555             :   }
    1556           0 :   attrVgrpID = SWXSwath[sID].VIDTable[2];
    1557           0 :   status = EHattr(fid, attrVgrpID, attrname, numbertype, count,
    1558             :       wrcode, datbuf);
    1559             :     }
    1560           0 :     return (status);
    1561             : }
    1562             : 
    1563             : 
    1564             : /*----------------------------------------------------------------------------|
    1565             : |  BEGIN_PROLOG                                                               |
    1566             : |                                                                             |
    1567             : |  FUNCTION: SWreadattr                                                       |
    1568             : |                                                                             |
    1569             : |  DESCRIPTION: Reads attribute from a swath.                                 |
    1570             : |                                                                             |
    1571             : |                                                                             |
    1572             : |  Return Value    Type     Units     Description                             |
    1573             : |  ============   ======  =========   =====================================   |
    1574             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1575             : |                                                                             |
    1576             : |  INPUTS:                                                                    |
    1577             : |  swathID        int32               swath structure ID                      |
    1578             : |  attrname       char                attribute name                          |
    1579             : |                                                                             |
    1580             : |  OUTPUTS:                                                                   |
    1581             : |  datbuf         void                I/O buffer                              |
    1582             : |                                                                             |
    1583             : |  NOTES:                                                                     |
    1584             : |                                                                             |
    1585             : |                                                                             |
    1586             : |   Date     Programmer   Description                                         |
    1587             : |  ======   ============  =================================================   |
    1588             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1589             : |                                                                             |
    1590             : |  END_PROLOG                                                                 |
    1591             : -----------------------------------------------------------------------------*/
    1592             : intn
    1593           0 : SWreadattr(int32 swathID, const char *attrname, VOIDP datbuf)
    1594             : {
    1595           0 :     intn            status = 0; /* routine return status variable */
    1596           0 :     int32           dum = 0;  /* dummy variable */
    1597             : 
    1598             :     /* Call SWwrrdattr routine to read attribute */
    1599             :     /* ----------------------------------------- */
    1600           0 :     status = SWwrrdattr(swathID, attrname, dum, dum, "r", datbuf);
    1601             : 
    1602           0 :     return (status);
    1603             : }
    1604             : 
    1605             : 
    1606             : 
    1607             : 
    1608             : 
    1609             : /*----------------------------------------------------------------------------|
    1610             : |  BEGIN_PROLOG                                                               |
    1611             : |                                                                             |
    1612             : |  FUNCTION: SWattrinfo                                                       |
    1613             : |                                                                             |
    1614             : |  DESCRIPTION:                                                               |
    1615             : |                                                                             |
    1616             : |                                                                             |
    1617             : |  Return Value    Type     Units     Description                             |
    1618             : |  ============   ======  =========   =====================================   |
    1619             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1620             : |                                                                             |
    1621             : |  INPUTS:                                                                    |
    1622             : |  swathID        int32               swath structure ID                      |
    1623             : |  attrname       char                attribute name                          |
    1624             : |                                                                             |
    1625             : |  OUTPUTS:                                                                   |
    1626             : |  numbertype     int32               attribute HDF numbertype                |
    1627             : |  count          int32               Number of attribute elements            |
    1628             : |                                                                             |
    1629             : |                                                                             |
    1630             : |  OUTPUTS:                                                                   |
    1631             : |             None                                                            |
    1632             : |                                                                             |
    1633             : |  NOTES:                                                                     |
    1634             : |                                                                             |
    1635             : |                                                                             |
    1636             : |   Date     Programmer   Description                                         |
    1637             : |  ======   ============  =================================================   |
    1638             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1639             : |  Oct 96   Joel Gales    Get Attribute Vgroup ID from external array         |
    1640             : |                                                                             |
    1641             : |  END_PROLOG                                                                 |
    1642             : -----------------------------------------------------------------------------*/
    1643             : intn
    1644           0 : SWattrinfo(int32 swathID, const char *attrname, int32 * numbertype, int32 * count)
    1645             : {
    1646           0 :     intn            status = 0; /* routine return status variable */
    1647             : 
    1648             :     int32           fid;  /* HDF-EOS file ID */
    1649             :     int32           attrVgrpID; /* Swath attribute ID */
    1650             :     int32           dum;  /* dummy variable */
    1651           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1652             : 
    1653             :     /* Check for valid swath ID */
    1654             :     /* ------------------------ */
    1655           0 :     status = SWchkswid(swathID, "SWattrinfo", &fid, &dum, &dum);
    1656             : 
    1657           0 :     if (status == 0)
    1658             :     {
    1659             :   /* Get attribute Vgroup ID and call EHattrinfo */
    1660             :   /* ------------------------------------------- */
    1661           0 :   int sID = swathID % idOffset;
    1662           0 :   if (sID >= NSWATH)
    1663             :   {
    1664           0 :       return -1;
    1665             :   }
    1666           0 :   attrVgrpID = SWXSwath[sID].VIDTable[2];
    1667             : 
    1668           0 :   status = EHattrinfo(fid, attrVgrpID, attrname, numbertype,
    1669             :           count);
    1670             :     }
    1671           0 :     return (status);
    1672             : }
    1673             : 
    1674             : 
    1675             : 
    1676             : 
    1677             : 
    1678             : 
    1679             : /*----------------------------------------------------------------------------|
    1680             : |  BEGIN_PROLOG                                                               |
    1681             : |                                                                             |
    1682             : |  FUNCTION: SWinqattrs                                                       |
    1683             : |                                                                             |
    1684             : |  DESCRIPTION:                                                               |
    1685             : |                                                                             |
    1686             : |                                                                             |
    1687             : |  Return Value    Type     Units     Description                             |
    1688             : |  ============   ======  =========   =====================================   |
    1689             : |  nattr          int32               Number of attributes in swath struct    |
    1690             : |                                                                             |
    1691             : |  INPUTS:                                                                    |
    1692             : |  swath ID       int32               swath structure ID                      |
    1693             : |                                                                             |
    1694             : |  OUTPUTS:                                                                   |
    1695             : |  attrnames      char                Attribute names in swath struct         |
    1696             : |                                     (Comma-separated list)                  |
    1697             : |  strbufsize     int32               Attributes name list string length      |
    1698             : |                                                                             |
    1699             : |  OUTPUTS:                                                                   |
    1700             : |             None                                                            |
    1701             : |                                                                             |
    1702             : |  NOTES:                                                                     |
    1703             : |                                                                             |
    1704             : |                                                                             |
    1705             : |   Date     Programmer   Description                                         |
    1706             : |  ======   ============  =================================================   |
    1707             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1708             : |  Oct 96   Joel Gales    Initialize nattr                                    |
    1709             : |  Oct 96   Joel Gales    Get Attribute Vgroup ID from external array         |
    1710             : |                                                                             |
    1711             : |  END_PROLOG                                                                 |
    1712             : -----------------------------------------------------------------------------*/
    1713             : int32
    1714           1 : SWinqattrs(int32 swathID, char *attrnames, int32 * strbufsize)
    1715             : {
    1716             :     intn            status; /* routine return status variable */
    1717             : 
    1718             :     int32           fid;  /* HDF-EOS file ID */
    1719             :     int32           attrVgrpID; /* Swath attribute ID */
    1720             :     int32           dum;  /* dummy variable */
    1721           1 :     int32           nattr = 0;  /* Number of attributes */
    1722           1 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1723             : 
    1724             :     /* Check Swath id */
    1725           1 :     status = SWchkswid(swathID, "SWinqattrs", &fid, &dum, &dum);
    1726             : 
    1727           1 :     if (status == 0)
    1728             :     {
    1729             :   /* Get attribute Vgroup ID and call EHattrcat */
    1730             :   /* ------------------------------------------ */
    1731           1 :   int sID = swathID % idOffset;
    1732           1 :   if (sID >= NSWATH)
    1733             :   {
    1734           0 :       return -1;
    1735             :   }
    1736           1 :   attrVgrpID = SWXSwath[sID].VIDTable[2];
    1737             : 
    1738           1 :   nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize);
    1739             :     }
    1740             : 
    1741           1 :     return (nattr);
    1742             : }
    1743             : 
    1744             : #define REMQUOTE(x) do { \
    1745             :     char* l_x = x; \
    1746             :     const size_t l_x_len = strlen(l_x); \
    1747             :     if (l_x_len >= 2 && l_x[0] == '"' && l_x[l_x_len - 1] == '"') {\
    1748             :         memmove(l_x, l_x + 1, l_x_len - 2); \
    1749             :         l_x[l_x_len - 2] = 0; \
    1750             :     } \
    1751             :   } while(0)
    1752             : 
    1753             : 
    1754             : /*----------------------------------------------------------------------------|
    1755             : |  BEGIN_PROLOG                                                               |
    1756             : |                                                                             |
    1757             : |  FUNCTION: SWinqdims                                                        |
    1758             : |                                                                             |
    1759             : |  DESCRIPTION: Returns dimension names and values defined in swath structure |
    1760             : |                                                                             |
    1761             : |                                                                             |
    1762             : |  Return Value    Type     Units     Description                             |
    1763             : |  ============   ======  =========   =====================================   |
    1764             : |  nDim           int32               Number of defined dimensions            |
    1765             : |                                                                             |
    1766             : |  INPUTS:                                                                    |
    1767             : |  swathID        int32               swath structure ID                      |
    1768             : |                                                                             |
    1769             : |  OUTPUTS:                                                                   |
    1770             : |  dimnames       char                Dimension names (comma-separated)       |
    1771             : |  dims           int32               Dimension values                        |
    1772             : |                                                                             |
    1773             : |  NOTES:                                                                     |
    1774             : |                                                                             |
    1775             : |                                                                             |
    1776             : |   Date     Programmer   Description                                         |
    1777             : |  ======   ============  =================================================   |
    1778             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1779             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    1780             : |  Feb 97   Joel Gales    Set nDim to -1 if status = -1                       |
    1781             : |                                                                             |
    1782             : |  END_PROLOG                                                                 |
    1783             : -----------------------------------------------------------------------------*/
    1784             : int32
    1785           2 : SWinqdims(int32 swathID, char *dimnames, int32 dims[])
    1786             : 
    1787             : {
    1788             :     intn            status; /* routine return status variable */
    1789             : 
    1790             :     int32           fid;  /* HDF-EOS file ID */
    1791             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1792             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1793             :     int32           size; /* Dimension size */
    1794           2 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1795           2 :     int32           nDim = 0; /* Number of dimensions */
    1796             : 
    1797             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    1798             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    1799             :     char            swathname[80];  /* Swath Name */
    1800             :     char           *utlstr;     /* Utility string */
    1801             : 
    1802             : 
    1803             :     /* Allocate space for utility string */
    1804             :     /* --------------------------------- */
    1805           2 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    1806           2 :     if(utlstr == NULL)
    1807             :     {
    1808           0 :   HEpush(DFE_NOSPACE,"SWinqdims", __FILE__, __LINE__);
    1809           0 :   return(-1);
    1810             :     }
    1811             : 
    1812             :     /* Check for valid swath id */
    1813           2 :     status = SWchkswid(swathID, "SWinqdims", &fid, &sdInterfaceID, &swVgrpID);
    1814             : 
    1815           2 :     if (status == 0)
    1816             :     {
    1817             :   /* If dimension names or sizes are desired ... */
    1818             :   /* ------------------------------------------- */
    1819           2 :   if (dimnames != NULL || dims != NULL)
    1820             :   {
    1821             :       /* Get swath name */
    1822           2 :       int sID = swathID % idOffset;
    1823           2 :       if (sID >= NSWATH)
    1824             :       {
    1825           0 :           free(utlstr);
    1826           0 :           return -1;
    1827             :       }
    1828           2 :       Vgetname(SWXSwath[sID].IDTable, swathname);
    1829             : 
    1830             :       /* Get pointers to "Dimension" section within SM */
    1831           2 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    1832             :              "Dimension", metaptrs);
    1833           2 :       if(metabuf == NULL)
    1834             :       {
    1835           0 :     free(utlstr);
    1836           0 :     return(-1);
    1837             :       }
    1838             :       /* If dimension names are desired then "clear" name buffer */
    1839           2 :       if (dimnames != NULL)
    1840             :       {
    1841           2 :     dimnames[0] = 0;
    1842             :       }
    1843             : 
    1844             : 
    1845             :       /* Begin loop through dimension entries in metadata */
    1846             :       /* ------------------------------------------------ */
    1847             :       while (1)
    1848             :       {
    1849             :     /* Search for OBJECT string */
    1850         102 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    1851             : 
    1852             :     /* If found within "Dimension" metadata section ... */
    1853         102 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    1854             :     {
    1855             :         /* Get Dimension Name (if desired) */
    1856         100 :         if (dimnames != NULL)
    1857             :         {
    1858             :       /* Check 1st for old meta data then new */
    1859             :       /* ------------------------------------ */
    1860         100 :       EHgetmetavalue(metaptrs, "OBJECT", utlstr);
    1861             : 
    1862             :       /*
    1863             :        * If OBJECT value begins with double quote then old
    1864             :        * metadata, dimension name is OBJECT value.
    1865             :        * Otherwise search for "DimensionName" string
    1866             :        */
    1867         100 :       if (utlstr[0] != '"')
    1868             :       {
    1869         100 :           metaptrs[0] =
    1870         100 :         strstr(metaptrs[0], "\t\t\t\tDimensionName=");
    1871         100 :           EHgetmetavalue(metaptrs, "DimensionName", utlstr);
    1872             :       }
    1873             : 
    1874             :       /* Strip off double quotes */
    1875             :       /* ----------------------- */
    1876         100 :       REMQUOTE(utlstr);
    1877             : 
    1878             :       /* If not first name then add comma delimiter */
    1879         100 :           if (nDim > 0)
    1880             :       {
    1881          98 :           strcat(dimnames, ",");
    1882             :       }
    1883             :       /* Add dimension name to dimension list */
    1884         100 :       strcat(dimnames, utlstr);
    1885             :         }
    1886             : 
    1887             :         /* Get Dimension Size (if desired) */
    1888         100 :         if (dims != NULL)
    1889             :         {
    1890          50 :       EHgetmetavalue(metaptrs, "Size", utlstr);
    1891          50 :       size = atoi(utlstr);
    1892          50 :       dims[nDim] = size;
    1893             :         }
    1894             :         /* Increment number of dimensions */
    1895         100 :         nDim++;
    1896             :     }
    1897             :     else
    1898             :         /* No more dimensions found */
    1899             :     {
    1900             :         break;
    1901             :     }
    1902             :       }
    1903           2 :       free(metabuf);
    1904             :   }
    1905             :     }
    1906             : 
    1907             : 
    1908             :     /* Set nDim to -1 if error status exists */
    1909             :     /* ------------------------------------- */
    1910           2 :     if (status == -1)
    1911             :     {
    1912           0 :   nDim = -1;
    1913             :     }
    1914           2 :     free(utlstr);
    1915             : 
    1916           2 :     return (nDim);
    1917             : }
    1918             : 
    1919             : 
    1920             : 
    1921             : 
    1922             : 
    1923             : /*----------------------------------------------------------------------------|
    1924             : |  BEGIN_PROLOG                                                               |
    1925             : |                                                                             |
    1926             : |  FUNCTION: SWinqmaps                                                        |
    1927             : |                                                                             |
    1928             : |  DESCRIPTION: Returns dimension mappings and offsets and increments         |
    1929             : |                                                                             |
    1930             : |                                                                             |
    1931             : |  Return Value    Type     Units     Description                             |
    1932             : |  ============   ======  =========   =====================================   |
    1933             : |  nMap           int32               Number of dimension mappings            |
    1934             : |                                                                             |
    1935             : |  INPUTS:                                                                    |
    1936             : |  swathID        int32               swath structure ID                      |
    1937             : |                                                                             |
    1938             : |  OUTPUTS:                                                                   |
    1939             : |  dimmaps        char                dimension mappings (comma-separated)    |
    1940             : |  offset         int32               array of offsets                        |
    1941             : |  increment      int32               array of increments                     |
    1942             : |                                                                             |
    1943             : |  NOTES:                                                                     |
    1944             : |                                                                             |
    1945             : |                                                                             |
    1946             : |   Date     Programmer   Description                                         |
    1947             : |  ======   ============  =================================================   |
    1948             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1949             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    1950             : |  Feb 97   Joel Gales    Set nMap to -1 if status = -1                       |
    1951             : |                                                                             |
    1952             : |  END_PROLOG                                                                 |
    1953             : -----------------------------------------------------------------------------*/
    1954             : int32
    1955           0 : SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[])
    1956             : 
    1957             : {
    1958             :     intn            status; /* routine return status variable */
    1959             : 
    1960             :     int32           fid;  /* HDF-EOS file ID */
    1961             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1962             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1963             :     int32           off;  /* Mapping Offset */
    1964             :     int32           incr; /* Mapping Increment */
    1965           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1966           0 :     int32           nMap = 0; /* Number of mappings */
    1967             : 
    1968             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    1969             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    1970             :     char            swathname[80];  /* Swath Name */
    1971             :     char           *utlstr;     /* Utility string */
    1972             : 
    1973             : 
    1974             :     /* Allocate space for utility string */
    1975             :     /* --------------------------------- */
    1976           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    1977           0 :     if(utlstr == NULL)
    1978             :     {
    1979           0 :   HEpush(DFE_NOSPACE,"SWinqmaps", __FILE__, __LINE__);
    1980           0 :   return(-1);
    1981             :     }
    1982             : 
    1983             :     /* Check for valid swath id */
    1984           0 :     status = SWchkswid(swathID, "SWinqmaps", &fid, &sdInterfaceID, &swVgrpID);
    1985           0 :     if (status == 0)
    1986             :     {
    1987             :   /* If mapping names or offsets or increments desired ... */
    1988             :   /* ----------------------------------------------------- */
    1989           0 :   if (dimmaps != NULL || offset != NULL || increment != NULL)
    1990             :   {
    1991             : 
    1992             :       /* Get swath name */
    1993           0 :       int sID = swathID % idOffset;
    1994           0 :       if (sID >= NSWATH)
    1995             :       {
    1996           0 :           free(utlstr);
    1997           0 :           return -1;
    1998             :       }
    1999           0 :       Vgetname(SWXSwath[sID].IDTable, swathname);
    2000             : 
    2001             :       /* Get pointers to "DimensionMap" section within SM */
    2002           0 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2003             :              "DimensionMap", metaptrs);
    2004           0 :       if(metabuf == NULL)
    2005             :       {
    2006           0 :     free(utlstr);
    2007           0 :     return(-1);
    2008             :       }
    2009             :       /* If mapping names are desired then "clear" name buffer */
    2010           0 :       if (dimmaps != NULL)
    2011             :       {
    2012           0 :     dimmaps[0] = 0;
    2013             :       }
    2014             : 
    2015             :       /* Begin loop through mapping entries in metadata */
    2016             :       /* ---------------------------------------------- */
    2017             :       while (1)
    2018             :       {
    2019             :     /* Search for OBJECT string */
    2020           0 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    2021             : 
    2022             :     /* If found within "DimensionMap" metadata section ... */
    2023           0 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    2024             :     {
    2025             :         /* Get Geo & Data Dimensions (if desired) */
    2026           0 :         if (dimmaps != NULL)
    2027             :         {
    2028             :       /* Get Geo Dim, remove quotes, add "/" */
    2029           0 :       EHgetmetavalue(metaptrs, "GeoDimension", utlstr);
    2030           0 :       REMQUOTE(utlstr);
    2031           0 :       strcat(utlstr, "/");
    2032             : 
    2033             :       /* If not first map then add comma delimiter. */
    2034           0 :       if (nMap > 0)
    2035             :       {
    2036           0 :           strcat(dimmaps, ",");
    2037             :       }
    2038             : 
    2039             :       /* Add to map list */
    2040           0 :       strcat(dimmaps, utlstr);
    2041             : 
    2042             :       /* Get Data Dim, remove quotes */
    2043           0 :       EHgetmetavalue(metaptrs, "DataDimension", utlstr);
    2044           0 :       REMQUOTE(utlstr);
    2045             : 
    2046             :       /* Add to map list */
    2047           0 :           strcat(dimmaps, utlstr);
    2048             :         }
    2049             : 
    2050             :         /* Get Offset (if desired) */
    2051           0 :         if (offset != NULL)
    2052             :         {
    2053           0 :       EHgetmetavalue(metaptrs, "Offset", utlstr);
    2054           0 :       off = atoi(utlstr);
    2055           0 :       offset[nMap] = off;
    2056             :         }
    2057             : 
    2058             :         /* Get Increment (if desired) */
    2059           0 :         if (increment != NULL)
    2060             :         {
    2061           0 :       EHgetmetavalue(metaptrs, "Increment", utlstr);
    2062           0 :       incr = atoi(utlstr);
    2063           0 :       increment[nMap] = incr;
    2064             :         }
    2065             : 
    2066             :         /* Increment number of maps */
    2067           0 :         nMap++;
    2068             :     }
    2069             :     else
    2070             :         /* No more mappings found */
    2071             :     {
    2072             :         break;
    2073             :     }
    2074             :       }
    2075           0 :       free(metabuf);
    2076             :   }
    2077             :     }
    2078             : 
    2079             : 
    2080             :     /* Set nMap to -1 if error status exists */
    2081             :     /* ------------------------------------- */
    2082           0 :     if (status == -1)
    2083             :     {
    2084           0 :   nMap = -1;
    2085             :     }
    2086           0 :     free(utlstr);
    2087             : 
    2088           0 :     return (nMap);
    2089             : }
    2090             : 
    2091             : 
    2092             : 
    2093             : 
    2094             : 
    2095             : /*----------------------------------------------------------------------------|
    2096             : |  BEGIN_PROLOG                                                               |
    2097             : |                                                                             |
    2098             : |  FUNCTION: SWinqidxmaps                                                     |
    2099             : |                                                                             |
    2100             : |  DESCRIPTION: Returns l_indexed mappings and l_index sizes                      |
    2101             : |                                                                             |
    2102             : |                                                                             |
    2103             : |  Return Value    Type     Units     Description                             |
    2104             : |  ============   ======  =========   =====================================   |
    2105             : |  nMap           int32               Number of l_indexed dimension mappings    |
    2106             : |                                                                             |
    2107             : |  INPUTS:                                                                    |
    2108             : |  swathID        int32               swath structure ID                      |
    2109             : |                                                                             |
    2110             : |  OUTPUTS:                                                                   |
    2111             : |  idxmaps        char                l_indexed dimension mappings              |
    2112             : |                                     (comma-separated)                       |
    2113             : |  idxsizes       int32               Number of elements in each mapping      |
    2114             : |                                                                             |
    2115             : |                                                                             |
    2116             : |  NOTES:                                                                     |
    2117             : |                                                                             |
    2118             : |                                                                             |
    2119             : |   Date     Programmer   Description                                         |
    2120             : |  ======   ============  =================================================   |
    2121             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2122             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    2123             : |  Feb 97   Joel Gales    Set nMap to -1 if status = -1                       |
    2124             : |                                                                             |
    2125             : |  END_PROLOG                                                                 |
    2126             : -----------------------------------------------------------------------------*/
    2127             : int32
    2128           0 : SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[])
    2129             : 
    2130             : {
    2131             :     intn            status; /* routine return status variable */
    2132             : 
    2133             :     int32           fid;  /* HDF-EOS file ID */
    2134             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    2135             :     int32           swVgrpID; /* Swath root Vgroup ID */
    2136           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2137           0 :     int32           nMap = 0; /* Number of mappings */
    2138             : 
    2139             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    2140             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    2141             :     char            swathname[80];  /* Swath Name */
    2142             :     char           *utlstr;     /* Utility string */
    2143             :     char           *slash;  /* Pointer to slash */
    2144             : 
    2145             : 
    2146             :     /* Allocate space for utility string */
    2147             :     /* --------------------------------- */
    2148           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    2149           0 :     if(utlstr == NULL)
    2150             :     {
    2151           0 :   HEpush(DFE_NOSPACE,"SWinqidxmaps", __FILE__, __LINE__);
    2152           0 :   return(-1);
    2153             :     }
    2154             :     /* Check for valid swath id */
    2155           0 :     status = SWchkswid(swathID, "SWinqidxmaps", &fid,
    2156             :            &sdInterfaceID, &swVgrpID);
    2157             : 
    2158           0 :     if (status == 0)
    2159             :     {
    2160             :   /* If mapping names or l_index sizes desired ... */
    2161             :   /* ------------------------------------------- */
    2162           0 :   if (idxmaps != NULL || idxsizes != NULL)
    2163             :   {
    2164             :       /* Get swath name */
    2165           0 :       int sID = swathID % idOffset;
    2166           0 :       if (sID >= NSWATH)
    2167             :       {
    2168           0 :           free(utlstr);
    2169           0 :           return -1;
    2170             :       }
    2171           0 :       Vgetname(SWXSwath[sID].IDTable, swathname);
    2172             : 
    2173             :       /* Get pointers to "IndexDimensionMap" section within SM */
    2174           0 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2175             :              "IndexDimensionMap", metaptrs);
    2176           0 :       if(metabuf == NULL)
    2177             :       {
    2178           0 :     free(utlstr);
    2179           0 :     return(-1);
    2180             :       }
    2181             :       /* If mapping names are desired then "clear" name buffer */
    2182           0 :       if (idxmaps != NULL)
    2183             :       {
    2184           0 :     idxmaps[0] = 0;
    2185             :       }
    2186             : 
    2187             :       /* Begin loop through mapping entries in metadata */
    2188             :       /* ---------------------------------------------- */
    2189             :       while (1)
    2190             :       {
    2191             :     /* Search for OBJECT string */
    2192           0 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    2193             : 
    2194             :     /* If found within "IndexDimensionMap" metadata section ... */
    2195           0 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    2196             :     {
    2197             :         /* Get Geo & Data Dimensions and # of indices */
    2198           0 :         if (idxmaps != NULL)
    2199             :         {
    2200             :       /* Get Geo Dim, remove quotes, add "/" */
    2201           0 :       EHgetmetavalue(metaptrs, "GeoDimension", utlstr);
    2202           0 :       REMQUOTE(utlstr);
    2203           0 :       strcat(utlstr, "/");
    2204             : 
    2205             :       /* If not first map then add comma delimiter. */
    2206           0 :       if (nMap > 0)
    2207             :       {
    2208           0 :           strcat(idxmaps, ",");
    2209             :       }
    2210             : 
    2211             :       /* Add to map list */
    2212           0 :       strcat(idxmaps, utlstr);
    2213             : 
    2214             : 
    2215             :       /* Get Index size (if desired) */
    2216           0 :       if (idxsizes != NULL)
    2217             :       {
    2218             :           /* Parse off geo dimension and find its size */
    2219           0 :           slash = strchr(utlstr, '/');
    2220           0 :           if (slash) *slash = 0;
    2221           0 :           idxsizes[nMap] = SWdiminfo(swathID, utlstr);
    2222             :       }
    2223             : 
    2224             : 
    2225             :       /* Get Data Dim, remove quotes */
    2226           0 :       EHgetmetavalue(metaptrs, "DataDimension", utlstr);
    2227           0 :       REMQUOTE(utlstr);
    2228             : 
    2229             :       /* Add to map list */
    2230           0 :           strcat(idxmaps, utlstr);
    2231             :         }
    2232             : 
    2233             :         /* Increment number of maps */
    2234           0 :         nMap++;
    2235             :     }
    2236             :     else
    2237             :         /* No more mappings found */
    2238             :     {
    2239             :         break;
    2240             :     }
    2241             :       }
    2242           0 :       free(metabuf);
    2243             :   }
    2244             :     }
    2245             : 
    2246             : 
    2247             :     /* Set nMap to -1 if error status exists */
    2248             :     /* ------------------------------------- */
    2249           0 :     if (status == -1)
    2250             :     {
    2251           0 :   nMap = -1;
    2252             :     }
    2253           0 :     free(utlstr);
    2254             : 
    2255           0 :     return (nMap);
    2256             : }
    2257             : 
    2258             : 
    2259             : 
    2260             : 
    2261             : /*----------------------------------------------------------------------------|
    2262             : |  BEGIN_PROLOG                                                               |
    2263             : |                                                                             |
    2264             : |  FUNCTION: SWinqfields                                                      |
    2265             : |                                                                             |
    2266             : |  DESCRIPTION: Returns fieldnames, ranks and numbertypes defined in swath.   |
    2267             : |                                                                             |
    2268             : |                                                                             |
    2269             : |  Return Value    Type     Units     Description                             |
    2270             : |  ============   ======  =========   =====================================   |
    2271             : |  nFld           int32               Number of (geo/data) fields in swath    |
    2272             : |                                                                             |
    2273             : |  INPUTS:                                                                    |
    2274             : |  swathID        int32               swath structure ID                      |
    2275             : |  fieldtype      char                field type (geo or data)                |
    2276             : |                                                                             |
    2277             : |                                                                             |
    2278             : |  OUTPUTS:                                                                   |
    2279             : |  fieldlist      char                Field names (comma-separated)           |
    2280             : |  rank           int32               Array of ranks                          |
    2281             : |  numbertype     int32               Array of HDF number types               |
    2282             : |                                                                             |
    2283             : |  NOTES:                                                                     |
    2284             : |                                                                             |
    2285             : |                                                                             |
    2286             : |   Date     Programmer   Description                                         |
    2287             : |  ======   ============  =================================================   |
    2288             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2289             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    2290             : |  Feb 97   Joel Gales    Set nFld to -1 if status = -1                       |
    2291             : |                                                                             |
    2292             : |  END_PROLOG                                                                 |
    2293             : -----------------------------------------------------------------------------*/
    2294             : static int32
    2295           4 : SWinqfields(int32 swathID, const char *fieldtype, char *fieldlist, int32 rank[],
    2296             :       int32 numbertype[])
    2297             : 
    2298             : {
    2299             :     intn            status; /* routine return status variable */
    2300             : 
    2301             :     int32           fid;  /* HDF-EOS file ID */
    2302             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    2303             :     int32           swVgrpID; /* Swath root Vgroup ID */
    2304           4 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2305           4 :     int32           nFld = 0; /* Number of mappings */
    2306             :     int32           slen[8];  /* String length array */
    2307             : 
    2308             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    2309             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    2310             :     char            swathname[80];  /* Swath Name */
    2311             :     char           *utlstr; /* Utility string */
    2312             :     char           *utlstr2;  /* Utility string 2 */
    2313             :     char           *ptr[8]; /* String pointer array */
    2314             : 
    2315             : 
    2316             :     /* Allocate space for utility string */
    2317             :     /* --------------------------------- */
    2318           4 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    2319           4 :     if(utlstr == NULL)
    2320             :     {
    2321           0 :   HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__);
    2322           0 :   return(-1);
    2323             :     }
    2324             : 
    2325           4 :     utlstr2 = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    2326           4 :     if(utlstr2 == NULL)
    2327             :     {
    2328           0 :   HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__);
    2329           0 :   free(utlstr);
    2330           0 :   return(-1);
    2331             :     }
    2332             : 
    2333             :     /* Check for valid swath id */
    2334           4 :     status = SWchkswid(swathID, "SWinqfields",
    2335             :            &fid, &sdInterfaceID, &swVgrpID);
    2336             : 
    2337           4 :     if (status == 0)
    2338             :     {
    2339             :   /* If field names, ranks,  or number types desired ... */
    2340             :   /* --------------------------------------------------- */
    2341           4 :   if (fieldlist != NULL || rank != NULL || numbertype != NULL)
    2342             :   {
    2343             :       /* Get swath name */
    2344           4 :       int sID = swathID % idOffset;
    2345           4 :       if (sID >= NSWATH)
    2346             :       {
    2347           0 :           free(utlstr);
    2348           0 :           free(utlstr2);
    2349           0 :           return -1;
    2350             :       }
    2351           4 :       Vgetname(SWXSwath[sID].IDTable, swathname);
    2352             : 
    2353             :       /* Get pointers to "GeoField" or "DataField" section within SM */
    2354           4 :       if (strcmp(fieldtype, "Geolocation Fields") == 0)
    2355             :       {
    2356           1 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2357             :                  "GeoField", metaptrs);
    2358           1 :     if(metabuf == NULL)
    2359             :     {
    2360           0 :         free(utlstr);
    2361           0 :         free(utlstr2);
    2362           0 :         return(-1);
    2363             :     }
    2364           1 :     strcpy(utlstr2, "GeoFieldName");
    2365             :       }
    2366             :       else
    2367             :       {
    2368           3 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2369             :                  "DataField", metaptrs);
    2370           3 :     if(metabuf == NULL)
    2371             :     {
    2372           0 :         free(utlstr);
    2373           0 :         free(utlstr2);
    2374           0 :         return(-1);
    2375             :     }
    2376           3 :     strcpy(utlstr2, "DataFieldName");
    2377             :       }
    2378             : 
    2379             : 
    2380             :       /* If field names are desired then "clear" name buffer */
    2381           4 :       if (fieldlist != NULL)
    2382             :       {
    2383           4 :     fieldlist[0] = 0;
    2384             :       }
    2385             : 
    2386             : 
    2387             :       /* Begin loop through mapping entries in metadata */
    2388             :       /* ---------------------------------------------- */
    2389           5 :       while (metaptrs[0])
    2390             :       {
    2391             :     /* Search for OBJECT string */
    2392           2 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    2393             : 
    2394             :     /* If found within "Geo" or "Data" Field metadata section .. */
    2395           2 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    2396             :     {
    2397             :         /* Get Fieldnames (if desired) */
    2398           1 :         if (fieldlist != NULL)
    2399             :         {
    2400             :       /* Check 1st for old meta data then new */
    2401             :       /* ------------------------------------ */
    2402           1 :       EHgetmetavalue(metaptrs, "OBJECT", utlstr);
    2403             : 
    2404             :       /*
    2405             :        * If OBJECT value begins with double quote then old
    2406             :        * metadata, field name is OBJECT value. Otherwise
    2407             :        * search for "GeoFieldName" or "DataFieldName"
    2408             :        * string
    2409             :        */
    2410             : 
    2411           1 :       if (utlstr[0] != '"')
    2412             :       {
    2413           1 :           strcpy(utlstr, "\t\t\t\t");
    2414           1 :           strcat(utlstr, utlstr2);
    2415           1 :           strcat(utlstr, "=");
    2416           1 :           metaptrs[0] = strstr(metaptrs[0], utlstr);
    2417           1 :           EHgetmetavalue(metaptrs, utlstr2, utlstr);
    2418             :       }
    2419             : 
    2420             :       /* Strip off double quotes */
    2421             :       /* ----------------------- */
    2422           1 :       REMQUOTE(utlstr);
    2423             : 
    2424             : 
    2425             :       /* Add to fieldlist */
    2426             :       /* ---------------- */
    2427           1 :           if (nFld > 0)
    2428             :       {
    2429           0 :           strcat(fieldlist, ",");
    2430             :       }
    2431           1 :       strcat(fieldlist, utlstr);
    2432             : 
    2433             :         }
    2434             :         /* Get Numbertype */
    2435           1 :         if (numbertype != NULL)
    2436             :         {
    2437           1 :       EHgetmetavalue(metaptrs, "DataType", utlstr);
    2438           1 :       numbertype[nFld] = EHnumstr(utlstr);
    2439             :         }
    2440             :         /*
    2441             :          * Get Rank (if desired) by counting # of dimensions in
    2442             :          * "DimList" string
    2443             :          */
    2444           1 :         if (rank != NULL)
    2445             :         {
    2446           1 :       EHgetmetavalue(metaptrs, "DimList", utlstr);
    2447           1 :       rank[nFld] = EHparsestr(utlstr, ',', ptr, slen);
    2448             :         }
    2449             :         /* Increment number of fields */
    2450           1 :         nFld++;
    2451             :     }
    2452             :     else
    2453             :         /* No more fields found */
    2454             :     {
    2455             :         break;
    2456             :     }
    2457             :       }
    2458           4 :       free(metabuf);
    2459             :   }
    2460             :     }
    2461             : 
    2462             :     /* Set nFld to -1 if error status exists */
    2463             :     /* ------------------------------------- */
    2464           4 :     if (status == -1)
    2465             :     {
    2466           0 :   nFld = -1;
    2467             :     }
    2468             : 
    2469           4 :     free(utlstr);
    2470           4 :     free(utlstr2);
    2471             : 
    2472           4 :     return (nFld);
    2473             : }
    2474             : 
    2475             : 
    2476             : 
    2477             : 
    2478             : /*----------------------------------------------------------------------------|
    2479             : |  BEGIN_PROLOG                                                               |
    2480             : |                                                                             |
    2481             : |  FUNCTION: SWinqgeofields                                                   |
    2482             : |                                                                             |
    2483             : |  DESCRIPTION: Inquires about geo fields in swath                            |
    2484             : |                                                                             |
    2485             : |                                                                             |
    2486             : |  Return Value    Type     Units     Description                             |
    2487             : |  ============   ======  =========   =====================================   |
    2488             : |  nflds          int32               Number of geo fields in swath           |
    2489             : |                                                                             |
    2490             : |  INPUTS:                                                                    |
    2491             : |  swathID        int32               swath structure ID                      |
    2492             : |                                                                             |
    2493             : |  OUTPUTS:                                                                   |
    2494             : |  fieldlist      char                Field names (comma-separated)           |
    2495             : |  rank           int32               Array of ranks                          |
    2496             : |  numbertype     int32               Array of HDF number types               |
    2497             : |                                                                             |
    2498             : |  NOTES:                                                                     |
    2499             : |                                                                             |
    2500             : |                                                                             |
    2501             : |   Date     Programmer   Description                                         |
    2502             : |  ======   ============  =================================================   |
    2503             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2504             : |                                                                             |
    2505             : |  END_PROLOG                                                                 |
    2506             : -----------------------------------------------------------------------------*/
    2507             : int32
    2508           1 : SWinqgeofields(int32 swathID, char *fieldlist, int32 rank[],
    2509             :          int32 numbertype[])
    2510             : {
    2511             : 
    2512             :     int32           nflds;  /* Number of Geolocation fields */
    2513             : 
    2514             :     /* Call "SWinqfields" routine */
    2515             :     /* -------------------------- */
    2516           1 :     nflds = SWinqfields(swathID, "Geolocation Fields", fieldlist, rank,
    2517             :       numbertype);
    2518             : 
    2519           1 :     return (nflds);
    2520             : 
    2521             : }
    2522             : 
    2523             : 
    2524             : 
    2525             : 
    2526             : /*----------------------------------------------------------------------------|
    2527             : |  BEGIN_PROLOG                                                               |
    2528             : |                                                                             |
    2529             : |  FUNCTION: SWinqdatafields                                                  |
    2530             : |                                                                             |
    2531             : |  DESCRIPTION: Inquires about data fields in swath                           |
    2532             : |                                                                             |
    2533             : |                                                                             |
    2534             : |  Return Value    Type     Units     Description                             |
    2535             : |  ============   ======  =========   =====================================   |
    2536             : |  nflds          int32               Number of data fields in swath          |
    2537             : |                                                                             |
    2538             : |  INPUTS:                                                                    |
    2539             : |  swathID        int32               swath structure ID                      |
    2540             : |                                                                             |
    2541             : |  OUTPUTS:                                                                   |
    2542             : |  fieldlist      char                Field names (comma-separated)           |
    2543             : |  rank           int32               Array of ranks                          |
    2544             : |  numbertype     int32               Array of HDF number types               |
    2545             : |                                                                             |
    2546             : |  NOTES:                                                                     |
    2547             : |                                                                             |
    2548             : |                                                                             |
    2549             : |   Date     Programmer   Description                                         |
    2550             : |  ======   ============  =================================================   |
    2551             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2552             : |                                                                             |
    2553             : |  END_PROLOG                                                                 |
    2554             : -----------------------------------------------------------------------------*/
    2555             : int32
    2556           3 : SWinqdatafields(int32 swathID, char *fieldlist, int32 rank[],
    2557             :     int32 numbertype[])
    2558             : {
    2559             : 
    2560             :     int32           nflds;  /* Number of Data fields */
    2561             : 
    2562             :     /* Call "SWinqfields" routine */
    2563             :     /* -------------------------- */
    2564           3 :     nflds = SWinqfields(swathID, "Data Fields", fieldlist, rank,
    2565             :       numbertype);
    2566             : 
    2567           3 :     return (nflds);
    2568             : 
    2569             : }
    2570             : 
    2571             : 
    2572             : 
    2573             : 
    2574             : /*----------------------------------------------------------------------------|
    2575             : |  BEGIN_PROLOG                                                               |
    2576             : |                                                                             |
    2577             : |  FUNCTION: SWnentries                                                       |
    2578             : |                                                                             |
    2579             : |  DESCRIPTION: Returns number of entries and string buffer size              |
    2580             : |                                                                             |
    2581             : |                                                                             |
    2582             : |  Return Value    Type     Units     Description                             |
    2583             : |  ============   ======  =========   =====================================   |
    2584             : |  nEntries       int32               Number of entries                       |
    2585             : |                                                                             |
    2586             : |  INPUTS:                                                                    |
    2587             : |  swathID        int32               swath structure ID                      |
    2588             : |  entrycode      int32               Entry code                              |
    2589             : |                               HDFE_NENTDIM  (0)                       |
    2590             : |                               HDFE_NENTMAP  (1)                       |
    2591             : |                               HDFE_NENTIMAP (2)                       |
    2592             : |                               HDFE_NENTGFLD (3)                       |
    2593             : |                               HDFE_NENTDFLD (4)                       |
    2594             : |                                                                             |
    2595             : |                                                                             |
    2596             : |  OUTPUTS:                                                                   |
    2597             : |  strbufsize     int32               Length of comma-separated list          |
    2598             : |                                     (Does not include null-terminator       |
    2599             : |                                                                             |
    2600             : |  NOTES:                                                                     |
    2601             : |                                                                             |
    2602             : |                                                                             |
    2603             : |   Date     Programmer   Description                                         |
    2604             : |  ======   ============  =================================================   |
    2605             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2606             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    2607             : |  Feb 97   Joel Gales    Set nEntries to -1 if status = -1                   |
    2608             : |                                                                             |
    2609             : |  END_PROLOG                                                                 |
    2610             : -----------------------------------------------------------------------------*/
    2611             : int32
    2612           5 : SWnentries(int32 swathID, int32 entrycode, int32 * strbufsize)
    2613             : 
    2614             : {
    2615             :     intn            status;     /* routine return status variable */
    2616             :     intn            i;        /* Loop index */
    2617             : 
    2618             :     int32           fid;      /* HDF-EOS file ID */
    2619             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    2620             :     int32           swVgrpID;     /* Swath root Vgroup ID */
    2621           5 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2622           5 :     int32           nEntries = 0;   /* Number of entries */
    2623             :     int32           metaflag;     /* Old (0), New (1) metadata flag) */
    2624           5 :     int32           nVal = 0;     /* Number of strings to search for */
    2625             : 
    2626           5 :     char           *metabuf = NULL; /* Pointer to structural metadata (SM) */
    2627             :     char           *metaptrs[2];    /* Pointers to begin and end of SM section */
    2628             :     char            swathname[80];  /* Swath Name */
    2629             :     char           *utlstr;     /* Utility string */
    2630             :     char            valName[2][32]; /* Strings to search for */
    2631             : 
    2632             :     /* Allocate space for utility string */
    2633             :     /* --------------------------------- */
    2634           5 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    2635           5 :     if(utlstr == NULL)
    2636             :     {
    2637           0 :   HEpush(DFE_NOSPACE,"SWnemtries", __FILE__, __LINE__);
    2638           0 :   return(-1);
    2639             :     }
    2640             :     /* Check for valid swath id */
    2641             :     /* ------------------------ */
    2642           5 :     status = SWchkswid(swathID, "SWnentries", &fid, &sdInterfaceID, &swVgrpID);
    2643             : 
    2644           5 :     if (status == 0)
    2645             :     {
    2646             :   /* Get swath name */
    2647           5 :   int sID = swathID % idOffset;
    2648           5 :   if (sID >= NSWATH)
    2649             :   {
    2650           0 :       free(utlstr);
    2651           0 :       return -1;
    2652             :   }
    2653           5 :   Vgetname(SWXSwath[sID].IDTable, swathname);
    2654             : 
    2655             :   /* Zero out string buffer size */
    2656           5 :   *strbufsize = 0;
    2657             : 
    2658             : 
    2659             :   /*
    2660             :    * Get pointer to relevant section within SM and Get names of
    2661             :    * metadata strings to inquire about
    2662             :    */
    2663           5 :   switch (entrycode)
    2664             :   {
    2665           1 :   case HDFE_NENTDIM:
    2666             :       /* Dimensions */
    2667             :       {
    2668           1 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2669             :                  "Dimension", metaptrs);
    2670           1 :     if(metabuf == NULL)
    2671             :     {
    2672           0 :         free(utlstr);
    2673           0 :         return(-1);
    2674             :     }
    2675           1 :     nVal = 1;
    2676           1 :     strcpy(&valName[0][0], "DimensionName");
    2677             :       }
    2678           1 :       break;
    2679             : 
    2680           0 :   case HDFE_NENTMAP:
    2681             :       /* Dimension Maps */
    2682             :       {
    2683           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2684             :                  "DimensionMap", metaptrs);
    2685           0 :     if(metabuf == NULL)
    2686             :     {
    2687           0 :         free(utlstr);
    2688           0 :         return(-1);
    2689             :     }
    2690           0 :     nVal = 2;
    2691           0 :     strcpy(&valName[0][0], "GeoDimension");
    2692           0 :     strcpy(&valName[1][0], "DataDimension");
    2693             :       }
    2694           0 :       break;
    2695             : 
    2696           0 :   case HDFE_NENTIMAP:
    2697             :       /* Indexed Dimension Maps */
    2698             :       {
    2699           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2700             :                "IndexDimensionMap", metaptrs);
    2701           0 :     if(metabuf == NULL)
    2702             :     {
    2703           0 :         free(utlstr);
    2704           0 :         return(-1);
    2705             :     }
    2706           0 :     nVal = 2;
    2707           0 :     strcpy(&valName[0][0], "GeoDimension");
    2708           0 :     strcpy(&valName[1][0], "DataDimension");
    2709             :       }
    2710           0 :       break;
    2711             : 
    2712           1 :   case HDFE_NENTGFLD:
    2713             :       /* Geolocation Fields */
    2714             :       {
    2715           1 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2716             :                  "GeoField", metaptrs);
    2717           1 :     if(metabuf == NULL)
    2718             :     {
    2719           0 :         free(utlstr);
    2720           0 :         return(-1);
    2721             :     }
    2722           1 :     nVal = 1;
    2723           1 :     strcpy(&valName[0][0], "GeoFieldName");
    2724             :       }
    2725           1 :       break;
    2726             : 
    2727           3 :   case HDFE_NENTDFLD:
    2728             :       /* Data Fields */
    2729             :       {
    2730           3 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    2731             :                  "DataField", metaptrs);
    2732           3 :     if(metabuf == NULL)
    2733             :     {
    2734           0 :         free(utlstr);
    2735           0 :         return(-1);
    2736             :     }
    2737           3 :     nVal = 1;
    2738           3 :     strcpy(&valName[0][0], "DataFieldName");
    2739             :       }
    2740           3 :       break;
    2741             :   }
    2742             : 
    2743             : 
    2744             :   /*
    2745             :    * Check for presence of 'GROUP="' string If found then old metadata,
    2746             :    * search on OBJECT string
    2747             :    */
    2748           5 :         if (metabuf)
    2749             :         {
    2750           5 :             metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0;
    2751           5 :             if (metaflag == 0)
    2752             :             {
    2753           0 :                 nVal = 1;
    2754           0 :                 strcpy(&valName[0][0], "\t\tOBJECT");
    2755             :             }
    2756             : 
    2757             : 
    2758             :             /* Begin loop through entries in metadata */
    2759             :             /* -------------------------------------- */
    2760          56 :             while (metaptrs[0])
    2761             :             {
    2762             :                 /* Search for first string */
    2763          53 :                 strcpy(utlstr, &valName[0][0]);
    2764          53 :                 strcat(utlstr, "=");
    2765          53 :                 metaptrs[0] = strstr(metaptrs[0], utlstr);
    2766             : 
    2767             :                 /* If found within relevant metadata section ... */
    2768          53 :                 if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    2769             :                 {
    2770         102 :                     for (i = 0; i < nVal; i++)
    2771             :                     {
    2772             :                         /*
    2773             :                          * Get all string values Don't count quotes
    2774             :                          */
    2775          51 :                         EHgetmetavalue(metaptrs, &valName[i][0], utlstr);
    2776          51 :                         if( utlstr[0] == '"' && utlstr[strlen(utlstr)-1] == '"' )
    2777           1 :                             *strbufsize += (int32)strlen(utlstr) - 2;
    2778             :                         else
    2779          50 :                             *strbufsize += (int32)strlen(utlstr);
    2780             :                     }
    2781             :                     /* Increment number of entries */
    2782          51 :                     nEntries++;
    2783             : 
    2784             :                     /* Go to end of OBJECT */
    2785          51 :                     metaptrs[0] = strstr(metaptrs[0], "END_OBJECT");
    2786             :                 }
    2787             :                 else
    2788             :                     /* No more entries found */
    2789             :                 {
    2790             :                     break;
    2791             :                 }
    2792             :             }
    2793           5 :             free(metabuf);
    2794             :         }
    2795             : 
    2796             : 
    2797             :   /* Count comma separators & slashes (if mappings) */
    2798             :   /* ---------------------------------------------- */
    2799           5 :   if (nEntries > 0)
    2800             :   {
    2801           2 :       *strbufsize += nEntries - 1;
    2802           2 :       *strbufsize += (nVal - 1) * nEntries;
    2803             :   }
    2804             :     }
    2805             : 
    2806             : 
    2807             :     /* Set nEntries to -1 if error status exists */
    2808             :     /* ----------------------------------------- */
    2809           5 :     if (status == -1)
    2810           0 :   nEntries = -1;
    2811             : 
    2812           5 :     free(utlstr);
    2813             : 
    2814           5 :     return (nEntries);
    2815             : }
    2816             : 
    2817             : 
    2818             : 
    2819             : 
    2820             : 
    2821             : /*----------------------------------------------------------------------------|
    2822             : |  BEGIN_PROLOG                                                               |
    2823             : |                                                                             |
    2824             : |  FUNCTION: SWinqswath                                                       |
    2825             : |                                                                             |
    2826             : |  DESCRIPTION: Returns number and names of swath structures in file          |
    2827             : |                                                                             |
    2828             : |                                                                             |
    2829             : |  Return Value    Type     Units     Description                             |
    2830             : |  ============   ======  =========   =====================================   |
    2831             : |  nSwath         int32               Number of swath structures in file      |
    2832             : |                                                                             |
    2833             : |  INPUTS:                                                                    |
    2834             : |  filename       char                HDF-EOS filename                        |
    2835             : |                                                                             |
    2836             : |  OUTPUTS:                                                                   |
    2837             : |  swathlist      char                List of swath names (comma-separated)   |
    2838             : |  strbufsize     int32               Length of swathlist                     |
    2839             : |                                                                             |
    2840             : |  NOTES:                                                                     |
    2841             : |                                                                             |
    2842             : |                                                                             |
    2843             : |   Date     Programmer   Description                                         |
    2844             : |  ======   ============  =================================================   |
    2845             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2846             : |                                                                             |
    2847             : |  END_PROLOG                                                                 |
    2848             : -----------------------------------------------------------------------------*/
    2849             : int32
    2850          14 : SWinqswath(const char *filename, char *swathlist, int32 * strbufsize)
    2851             : {
    2852             :     int32           nSwath; /* Number of swath structures in file */
    2853             : 
    2854             :     /* Call "EHinquire" routine */
    2855             :     /* ------------------------ */
    2856          14 :     nSwath = EHinquire(filename, "SWATH", swathlist, strbufsize);
    2857             : 
    2858          14 :     return (nSwath);
    2859             : }
    2860             : 
    2861             : 
    2862             : 
    2863             : /*----------------------------------------------------------------------------|
    2864             : |  BEGIN_PROLOG                                                               |
    2865             : |                                                                             |
    2866             : |  FUNCTION: SW1dfldsrch                                                      |
    2867             : |                                                                             |
    2868             : |  DESCRIPTION: Retrieves information about a 1D field                        |
    2869             : |                                                                             |
    2870             : |                                                                             |
    2871             : |  Return Value    Type     Units     Description                             |
    2872             : |  ============   ======  =========   =====================================   |
    2873             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2874             : |                                                                             |
    2875             : |  INPUTS:                                                                    |
    2876             : |  fid            int32               HDF-EOS file ID                         |
    2877             : |  swathID        int32               swath structure ID                      |
    2878             : |  fieldname      const char          field name                              |
    2879             : |  access         const char          Access code (w/r)                       |
    2880             : |                                                                             |
    2881             : |                                                                             |
    2882             : |  OUTPUTS:                                                                   |
    2883             : |  vgidout        int32               Field (geo/data) vgroup ID              |
    2884             : |  vdataIDout     int32               Field Vdata ID                          |
    2885             : |  fldtype        int32               Field type                              |
    2886             : |                                                                             |
    2887             : |  NOTES:                                                                     |
    2888             : |                                                                             |
    2889             : |                                                                             |
    2890             : |   Date     Programmer   Description                                         |
    2891             : |  ======   ============  =================================================   |
    2892             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2893             : |                                                                             |
    2894             : |  END_PROLOG                                                                 |
    2895             : -----------------------------------------------------------------------------*/
    2896             : static intn
    2897           0 : SW1dfldsrch(int32 fid, int32 swathID, const char *fieldname, const char *i_access,
    2898             :       int32 * vgidout, int32 * vdataIDout, int32 * fldtype)
    2899             : 
    2900             : {
    2901           0 :     intn            status = 0; /* routine return status variable */
    2902             : 
    2903             :     int32           sID;  /* SwathID - offset */
    2904             :     int32           vgid; /* Swath Geo or Data Vgroup ID */
    2905             :     int32           vdataID;  /* 1d field vdata */
    2906           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2907             : 
    2908             : 
    2909             :     /* Compute "reduced" swath ID */
    2910             :     /* -------------------------- */
    2911           0 :     sID = swathID % idOffset;
    2912           0 :     if (sID >= NSWATH)
    2913             :     {
    2914           0 :         return -1;
    2915             :     }
    2916             : 
    2917             :     /* Get Geolocation Vgroup id and 1D field name Vdata id */
    2918             :     /* ---------------------------------------------------- */
    2919           0 :     vgid = SWXSwath[sID].VIDTable[0];
    2920           0 :     vdataID = EHgetid(fid, vgid, fieldname, 1, i_access);
    2921           0 :     *fldtype = 0;
    2922             : 
    2923             : 
    2924             :     /*
    2925             :      * If name not found in Geolocation Vgroup then detach Geolocation Vgroup
    2926             :      * and search in Data Vgroup
    2927             :      */
    2928           0 :     if (vdataID == -1)
    2929             :     {
    2930           0 :   vgid = SWXSwath[sID].VIDTable[1];;
    2931           0 :   vdataID = EHgetid(fid, vgid, fieldname, 1, i_access);
    2932           0 :   *fldtype = 1;
    2933             : 
    2934             :   /* If field also not found in Data Vgroup then set error status */
    2935             :   /* ------------------------------------------------------------ */
    2936           0 :   if (vdataID == -1)
    2937             :   {
    2938           0 :       status = -1;
    2939           0 :       vgid = -1;
    2940           0 :       vdataID = -1;
    2941             :   }
    2942             :     }
    2943           0 :     *vgidout = vgid;
    2944           0 :     *vdataIDout = vdataID;
    2945             : 
    2946           0 :     return (status);
    2947             : }
    2948             : 
    2949             : 
    2950             : 
    2951             : 
    2952             : 
    2953             : /*----------------------------------------------------------------------------|
    2954             : |  BEGIN_PROLOG                                                               |
    2955             : |                                                                             |
    2956             : |  FUNCTION: SWSDfldsrch                                                      |
    2957             : |                                                                             |
    2958             : |  DESCRIPTION: Retrieves information SDS field                               |
    2959             : |                                                                             |
    2960             : |                                                                             |
    2961             : |  Return Value    Type     Units     Description                             |
    2962             : |  ============   ======  =========   =====================================   |
    2963             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2964             : |                                                                             |
    2965             : |  INPUTS:                                                                    |
    2966             : |  swathID        int32               swath structure ID                      |
    2967             : |  sdInterfaceID  int32               SD interface ID                         |
    2968             : |  fieldname      const char          field name                              |
    2969             : |                                                                             |
    2970             : |                                                                             |
    2971             : |  OUTPUTS:                                                                   |
    2972             : |  sdid           int32               SD element ID                           |
    2973             : |  rankSDS        int32               Rank of SDS                             |
    2974             : |  rankFld        int32               True rank of field (merging)            |
    2975             : |  offset         int32               Offset of field within merged field     |
    2976             : |  dims           int32               Dimensions of field                     |
    2977             : |  solo           int32               Solo field flag                         |
    2978             : |                                                                             |
    2979             : |  NOTES:                                                                     |
    2980             : |                                                                             |
    2981             : |                                                                             |
    2982             : |   Date     Programmer   Description                                         |
    2983             : |  ======   ============  =================================================   |
    2984             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2985             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    2986             : |                                                                             |
    2987             : |  END_PROLOG                                                                 |
    2988             : -----------------------------------------------------------------------------*/
    2989             : static intn
    2990           1 : SWSDfldsrch(int32 swathID, int32 sdInterfaceID, const char *fieldname,
    2991             :             int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset,
    2992             :             int32 dims[], int32 * solo)
    2993             : {
    2994             :     intn            i;    /* Loop index */
    2995           1 :     intn            status = -1;/* routine return status variable */
    2996             : 
    2997             :     int32           sID;  /* SwathID - offset */
    2998           1 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2999             :     int32           dum;  /* Dummy variable */
    3000             :     int32           dums[128];  /* Dummy array */
    3001             :     int32           attrIndex;  /* Attribute l_index */
    3002             : 
    3003             :     char            name[2048]; /* Merged-Field Names */
    3004             :     char            swathname[80];  /* Swath Name */
    3005             :     char           *utlstr; /* Utility string */
    3006             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    3007             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    3008             :     char           *oldmetaptr; /* Pointer within SM section */
    3009             : 
    3010             : 
    3011             :     /* Allocate space for utility string */
    3012             :     /* --------------------------------- */
    3013           1 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    3014           1 :     if(utlstr == NULL)
    3015             :     {
    3016           0 :   HEpush(DFE_NOSPACE,"SWSDfldsrch", __FILE__, __LINE__);
    3017           0 :   return(-1);
    3018             :     }
    3019             :     /* Set solo flag to 0 (no) */
    3020             :     /* ----------------------- */
    3021           1 :     *solo = 0;
    3022             : 
    3023             : 
    3024             :     /* Compute "reduced" swath ID */
    3025             :     /* -------------------------- */
    3026           1 :     sID = swathID % idOffset;
    3027           1 :     if (sID >= NSWATH)
    3028             :     {
    3029           0 :         free(utlstr);
    3030           0 :         return -1;
    3031             :     }
    3032             : 
    3033             :     /* Loop through all SDSs in swath */
    3034             :     /* ------------------------------ */
    3035           1 :     for (i = 0; i < SWXSwath[sID].nSDS; i++)
    3036             :     {
    3037             :   /* If active SDS ... */
    3038             :   /* ----------------- */
    3039           0 :   if (SWXSwath[sID].sdsID[i] != 0)
    3040             :   {
    3041             :       /* Get SDS ID, name, rankSDS, and dimensions */
    3042             :       /* ----------------------------------------- */
    3043           0 :       *sdid = SWXSwath[sID].sdsID[i];
    3044           0 :       SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum);
    3045           0 :       *rankFld = *rankSDS;
    3046             : 
    3047             :       /* If merged field ... */
    3048             :       /* ------------------- */
    3049           0 :       if (strstr(name, "MRGFLD_") == &name[0])
    3050             :       {
    3051             :     /* Get swath name */
    3052             :     /* -------------- */
    3053           0 :     Vgetname(SWXSwath[sID].IDTable, swathname);
    3054             : 
    3055             : 
    3056             :     /* Get pointers to "MergedFields" section within SM */
    3057             :     /* ------------------------------------------------ */
    3058           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    3059             :                  "MergedFields", metaptrs);
    3060           0 :     if(metabuf == NULL)
    3061             :     {
    3062           0 :         free(utlstr);
    3063           0 :         return(-1);
    3064             :     }
    3065             : 
    3066             :     /* Store metaptr in order to recover */
    3067             :     /* --------------------------------- */
    3068           0 :     oldmetaptr = metaptrs[0];
    3069             : 
    3070             : 
    3071             :     /* Search for Merged field name */
    3072             :     /* ---------------------------- */
    3073           0 :     snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "MergedFieldName=\"",
    3074             :       name, "\"\n");
    3075           0 :     metaptrs[0] = strstr(metaptrs[0], utlstr);
    3076             : 
    3077             : 
    3078             :     /* If not found check for old metadata */
    3079             :     /* ----------------------------------- */
    3080           0 :     if (metaptrs[0] == NULL)
    3081             :     {
    3082           0 :         snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "OBJECT=\"", name, "\"\n");
    3083           0 :         metaptrs[0] = strstr(oldmetaptr, utlstr);
    3084             :     }
    3085             : 
    3086             : 
    3087             :     /* Get field list and strip off leading and trailing quotes */
    3088           0 :     EHgetmetavalue(metaptrs, "FieldList", name);  /* not return status --xhua */
    3089           0 :     memmove(name, name + 1, strlen(name) - 2);
    3090           0 :     name[strlen(name) - 2] = 0;
    3091             : 
    3092             :     /* Search for desired field within merged field list */
    3093           0 :     snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"");
    3094           0 :     dum = EHstrwithin(utlstr, name, ',');
    3095             : 
    3096           0 :     free(metabuf);
    3097             :       }
    3098             :       else
    3099             :       {
    3100             :     /* If solo (unmerged) check if SDS name matches fieldname */
    3101             :     /* ------------------------------------------------------ */
    3102           0 :     dum = EHstrwithin(fieldname, name, ',');
    3103           0 :     if (dum != -1)
    3104             :     {
    3105           0 :         *solo = 1;
    3106           0 :         *offset = 0;
    3107             :     }
    3108             :       }
    3109             : 
    3110             : 
    3111             :       /* If field found ... */
    3112             :       /* ------------------ */
    3113           0 :       if (dum != -1)
    3114             :       {
    3115           0 :     status = 0;
    3116             : 
    3117             :     /* If merged field ... */
    3118             :     /* ------------------- */
    3119           0 :     if (*solo == 0)
    3120             :     {
    3121             :         /* Get "Field Offsets" SDS attribute l_index */
    3122             :         /* --------------------------------------- */
    3123           0 :         attrIndex = SDfindattr(*sdid, "Field Offsets");
    3124             : 
    3125             :         /*
    3126             :          * If attribute exists then get offset of desired field
    3127             :          * within merged field
    3128             :          */
    3129           0 :         if (attrIndex != -1)
    3130             :         {
    3131           0 :       SDreadattr(*sdid, attrIndex, (VOIDP) dums);
    3132           0 :       *offset = dums[dum];
    3133             :         }
    3134             : 
    3135             : 
    3136             :         /* Get "Field Dims" SDS attribute l_index */
    3137             :         /* ------------------------------------ */
    3138           0 :         attrIndex = SDfindattr(*sdid, "Field Dims");
    3139             : 
    3140             :         /*
    3141             :          * If attribute exists then get 0th dimension of desired
    3142             :          * field within merged field
    3143             :          */
    3144           0 :         if (attrIndex != -1)
    3145             :         {
    3146           0 :       SDreadattr(*sdid, attrIndex, (VOIDP) dums);
    3147           0 :       dims[0] = dums[dum];
    3148             : 
    3149             :       /* If this dimension = 1 then field is really 2 dim */
    3150             :       /* ------------------------------------------------ */
    3151           0 :       if (dums[dum] == 1)
    3152             :       {
    3153           0 :           *rankFld = 2;
    3154             :       }
    3155             :         }
    3156             :     }
    3157             : 
    3158             : 
    3159             :     /* Break out of SDS loop */
    3160             :     /* --------------------- */
    3161           0 :     break;
    3162             :       }     /* End of found field section */
    3163             :   }
    3164             :   else
    3165             :   {
    3166             :       /* First non-active SDS signifies no more, break out of SDS loop */
    3167             :       /* ------------------------------------------------------------- */
    3168           0 :       break;
    3169             :   }
    3170             :     }
    3171             : 
    3172           1 :     free(utlstr);
    3173             : 
    3174           1 :     return (status);
    3175             : }
    3176             : 
    3177             : 
    3178             : 
    3179             : 
    3180             : 
    3181             : /*----------------------------------------------------------------------------|
    3182             : |  BEGIN_PROLOG                                                               |
    3183             : |                                                                             |
    3184             : |  FUNCTION: SWwrrdfield                                                      |
    3185             : |                                                                             |
    3186             : |  DESCRIPTION: Writes/Reads fields                                           |
    3187             : |                                                                             |
    3188             : |                                                                             |
    3189             : |  Return Value    Type     Units     Description                             |
    3190             : |  ============   ======  =========   =====================================   |
    3191             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3192             : |                                                                             |
    3193             : |  INPUTS:                                                                    |
    3194             : |  swathID        int32               swath structure ID                      |
    3195             : |  fieldname      const char          fieldname                               |
    3196             : |  code           const char          Write/Read code (w/r)                   |
    3197             : |  start          int32               start array                             |
    3198             : |  stride         int32               stride array                            |
    3199             : |  edge           int32               edge array                              |
    3200             : |  datbuf         void                data buffer for read                    |
    3201             : |                                                                             |
    3202             : |                                                                             |
    3203             : |  OUTPUTS:                                                                   |
    3204             : |  datbuf         void                data buffer for write                   |
    3205             : |                                                                             |
    3206             : |  NOTES:                                                                     |
    3207             : |                                                                             |
    3208             : |                                                                             |
    3209             : |   Date     Programmer   Description                                         |
    3210             : |  ======   ============  =================================================   |
    3211             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3212             : |  Feb 97   Joel Gales    Stride = 1 HDF compression workaround               |
    3213             : |                                                                             |
    3214             : |  END_PROLOG                                                                 |
    3215             : -----------------------------------------------------------------------------*/
    3216             : static intn
    3217           0 : SWwrrdfield(int32 swathID, const char *fieldname, const char *code,
    3218             :       int32 start[], int32 stride[], int32 edge[], VOIDP datbuf)
    3219             : 
    3220             : {
    3221             :     intn            i;    /* Loop index */
    3222           0 :     intn            status = 0; /* routine return status variable */
    3223             : 
    3224             :     int32           fid;  /* HDF-EOS file ID */
    3225             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    3226             :     int32           vgid; /* Swath Geo or Data Vgroup ID */
    3227             :     int32           sdid; /* SDS ID */
    3228             :     int32           dum;  /* Dummy variable */
    3229             :     int32           rankSDS;  /* Rank of SDS */
    3230             :     int32           rankFld;  /* Rank of field */
    3231             : 
    3232             :     int32           vdataID;  /* 1d field vdata */
    3233             :     int32           recsize;  /* Vdata record size */
    3234             :     int32           fldsize;  /* Field size */
    3235             :     int32           nrec; /* Number of records in Vdata */
    3236             : 
    3237             :     int32           offset[8];  /* I/O offset (start) */
    3238             :     int32           incr[8];  /* I/O increment (stride) */
    3239             :     int32           count[8]; /* I/O count (edge) */
    3240             :     int32           dims[8];  /* Field/SDS dimensions */
    3241             :     int32           mrgOffset;  /* Merged field offset */
    3242             :     int32           nflds;  /* Number of fields in Vdata */
    3243             :     int32           strideOne;  /* Strides = 1 flag */
    3244             : 
    3245             :     uint8          *buf;  /* I/O (transfer) buffer */
    3246             :     uint8          *fillbuf;  /* Fill value buffer */
    3247             : 
    3248             :     char            attrName[80]; /* Name of fill value attribute */
    3249             :     char           *ptr[64];  /* String pointer array */
    3250             :     char            fieldlist[256]; /* Vdata field list */
    3251             : 
    3252             : 
    3253             :     /* Check for valid swath ID */
    3254             :     /* ------------------------ */
    3255           0 :     status = SWchkswid(swathID, "SWwrrdfield", &fid, &sdInterfaceID, &dum);
    3256             : 
    3257             : 
    3258           0 :     if (status == 0)
    3259             :     {
    3260             : 
    3261             :   /* Check whether fieldname is in SDS (multi-dim field) */
    3262             :   /* --------------------------------------------------- */
    3263           0 :   status = SWSDfldsrch(swathID, sdInterfaceID, fieldname, &sdid,
    3264             :            &rankSDS, &rankFld, &mrgOffset, dims, &dum);
    3265             : 
    3266             :   /* Multi-Dimensional Field Section */
    3267             :   /* ------------------------------- */
    3268           0 :   if (status != -1)
    3269             :   {
    3270             :       /* Set I/O offset Section */
    3271             :       /* ---------------------- */
    3272             : 
    3273             :       /*
    3274             :        * If start == NULL (default) set I/O offset of 0th field to
    3275             :        * offset within merged field (if any) and the rest to 0
    3276             :        */
    3277           0 :       if (start == NULL)
    3278             :       {
    3279           0 :     for (i = 0; i < rankSDS; i++)
    3280             :     {
    3281           0 :         offset[i] = 0;
    3282             :     }
    3283           0 :     offset[0] = mrgOffset;
    3284             :       }
    3285             :       else
    3286             :       {
    3287             :     /*
    3288             :      * ... otherwise set I/O offset to user values, adjusting the
    3289             :      * 0th field with the merged field offset (if any)
    3290             :      */
    3291           0 :     if (rankFld == rankSDS)
    3292             :     {
    3293           0 :         for (i = 0; i < rankSDS; i++)
    3294             :         {
    3295           0 :       offset[i] = start[i];
    3296             :         }
    3297           0 :         offset[0] += mrgOffset;
    3298             :     }
    3299             :     else
    3300             :     {
    3301             :         /*
    3302             :          * If field really 2-dim merged in 3-dim field then set
    3303             :          * 0th field offset to merge offset and then next two to
    3304             :          * the user values
    3305             :          */
    3306           0 :         for (i = 0; i < rankFld; i++)
    3307             :         {
    3308           0 :       offset[i + 1] = start[i];
    3309             :         }
    3310           0 :         offset[0] = mrgOffset;
    3311             :     }
    3312             :       }
    3313             : 
    3314             : 
    3315             : 
    3316             :       /* Set I/O stride Section */
    3317             :       /* ---------------------- */
    3318             : 
    3319             :       /*
    3320             :        * If stride == NULL (default) set I/O stride to 1
    3321             :        */
    3322           0 :       if (stride == NULL)
    3323             :       {
    3324           0 :     for (i = 0; i < rankSDS; i++)
    3325             :     {
    3326           0 :         incr[i] = 1;
    3327             :     }
    3328             :       }
    3329             :       else
    3330             :       {
    3331             :     /*
    3332             :      * ... otherwise set I/O stride to user values
    3333             :      */
    3334           0 :     if (rankFld == rankSDS)
    3335             :     {
    3336           0 :         for (i = 0; i < rankSDS; i++)
    3337             :         {
    3338           0 :       incr[i] = stride[i];
    3339             :         }
    3340             :     }
    3341             :     else
    3342             :     {
    3343             :         /*
    3344             :          * If field really 2-dim merged in 3-dim field then set
    3345             :          * 0th field stride to 1 and then next two to the user
    3346             :          * values.
    3347             :          */
    3348           0 :         for (i = 0; i < rankFld; i++)
    3349             :         {
    3350           0 :       incr[i + 1] = stride[i];
    3351             :         }
    3352           0 :         incr[0] = 1;
    3353             :     }
    3354             :       }
    3355             : 
    3356             : 
    3357             : 
    3358             :       /* Set I/O count Section */
    3359             :       /* --------------------- */
    3360             : 
    3361             :       /*
    3362             :        * If edge == NULL (default) set I/O count to number of remaining
    3363             :        * entries (dims - start) / increment.  Note that 0th field
    3364             :        * offset corrected for merged field offset (if any).
    3365             :        */
    3366           0 :       if (edge == NULL)
    3367             :       {
    3368           0 :     for (i = 1; i < rankSDS; i++)
    3369             :     {
    3370           0 :         count[i] = (dims[i] - offset[i]) / incr[i];
    3371             :     }
    3372           0 :     count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0];
    3373             :       }
    3374             :       else
    3375             :       {
    3376             :     /*
    3377             :      * ... otherwise set I/O count to user values
    3378             :      */
    3379           0 :     if (rankFld == rankSDS)
    3380             :     {
    3381           0 :         for (i = 0; i < rankSDS; i++)
    3382             :         {
    3383           0 :       count[i] = edge[i];
    3384             :         }
    3385             :     }
    3386             :     else
    3387             :     {
    3388             :         /*
    3389             :          * If field really 2-dim merged in 3-dim field then set
    3390             :          * 0th field count to 1 and then next two to the user
    3391             :          * values.
    3392             :          */
    3393           0 :         for (i = 0; i < rankFld; i++)
    3394             :         {
    3395           0 :       count[i + 1] = edge[i];
    3396             :         }
    3397           0 :         count[0] = 1;
    3398             :     }
    3399             :       }
    3400             : 
    3401             :       /* Perform I/O with relevant HDF I/O routine */
    3402             :       /* ----------------------------------------- */
    3403           0 :       if (strcmp(code, "w") == 0)
    3404             :       {
    3405             :     /* Set strideOne to true (1) */
    3406             :     /* ------------------------- */
    3407           0 :     strideOne = 1;
    3408             : 
    3409             : 
    3410             :     /* If incr[i] != 1 set strideOne to false (0) */
    3411             :     /* ------------------------------------------ */
    3412           0 :     for (i = 0; i < rankSDS; i++)
    3413             :     {
    3414           0 :         if (incr[i] != 1)
    3415             :         {
    3416           0 :       strideOne = 0;
    3417           0 :       break;
    3418             :         }
    3419             :     }
    3420             : 
    3421             : 
    3422             :     /*
    3423             :      * If strideOne is true use NULL parameter for stride. This
    3424             :      * is a work-around to HDF compression problem
    3425             :      */
    3426           0 :     if (strideOne == 1)
    3427             :     {
    3428           0 :         status = SDwritedata(sdid, offset, NULL, count,
    3429             :            (VOIDP) datbuf);
    3430             :     }
    3431             :     else
    3432             :     {
    3433           0 :         status = SDwritedata(sdid, offset, incr, count,
    3434             :            (VOIDP) datbuf);
    3435             :     }
    3436             :       }
    3437             :       else
    3438             :       {
    3439           0 :     status = SDreaddata(sdid, offset, incr, count,
    3440             :             (VOIDP) datbuf);
    3441             :       }
    3442             :   }     /* End of Multi-Dimensional Field Section */
    3443             :   else
    3444             :   {
    3445             : 
    3446             :       /* One-Dimensional Field Section */
    3447             :       /* ----------------------------- */
    3448             : 
    3449             :       /* Check fieldname within 1d field Vgroups */
    3450             :       /* --------------------------------------- */
    3451           0 :       status = SW1dfldsrch(fid, swathID, fieldname, code,
    3452             :          &vgid, &vdataID, &dum);
    3453             : 
    3454           0 :       if (status != -1)
    3455             :       {
    3456             : 
    3457             :     /* Get number of records */
    3458             :     /* --------------------- */
    3459           0 :     nrec = VSelts(vdataID);
    3460             : 
    3461             : 
    3462             :     /* Set offset, increment, & count */
    3463             :     /* ------------------------------ */
    3464           0 :     offset[0] = (start == NULL) ? 0 : start[0];
    3465           0 :     incr[0] = (stride == NULL) ? 1 : stride[0];
    3466           0 :     count[0] = (edge == NULL)
    3467           0 :         ? (nrec - offset[0]) / incr[0]
    3468           0 :         : edge[0];
    3469             : 
    3470             : 
    3471             : 
    3472             :     /* Write Section */
    3473             :     /* ------------- */
    3474           0 :     if (strcmp(code, "w") == 0)
    3475             :     {
    3476             :         /* Get size of field and setup fill buffer */
    3477             :         /* --------------------------------------- */
    3478           0 :         fldsize = VSsizeof(vdataID, (char *)fieldname);
    3479           0 :         fillbuf = (uint8 *) calloc(fldsize, 1);
    3480           0 :         if(fillbuf == NULL)
    3481             :         {
    3482           0 :       HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
    3483           0 :       return(-1);
    3484             :         }
    3485             : 
    3486             :         /* Get size of record in Vdata and setup I/O buffer */
    3487             :         /* ------------------------------------------------ */
    3488           0 :         VSQueryvsize(vdataID, &recsize);
    3489           0 :         buf = (uint8 *) calloc(recsize, count[0] * incr[0]);
    3490           0 :         if(buf == NULL)
    3491             :         {
    3492           0 :       HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
    3493           0 :       free(fillbuf);
    3494           0 :       return(-1);
    3495             :         }
    3496             : 
    3497             : 
    3498             :         /* Get names and number of fields in each record  */
    3499             :         /* ---------------------------------------------- */
    3500           0 :         VSgetfields(vdataID, fieldlist);
    3501           0 :         dum = EHstrwithin(fieldname, fieldlist, ',');
    3502           0 :         nflds = EHparsestr(fieldlist, ',', ptr, NULL);
    3503             : 
    3504             : 
    3505             :         /* Get Merged Field Offset (if any) */
    3506             :         /* -------------------------------- */
    3507           0 :         if (nflds > 1)
    3508             :         {
    3509           0 :       if (dum > 0)
    3510             :       {
    3511           0 :           *(ptr[dum] - 1) = 0;
    3512           0 :           mrgOffset = VSsizeof(vdataID, fieldlist);
    3513           0 :           *(ptr[dum] - 1) = ',';
    3514             :       }
    3515             :       else
    3516             :       {
    3517           0 :           mrgOffset = 0;
    3518             :       }
    3519             : 
    3520             :       /* Read records to recover previously written data */
    3521           0 :       /* status = */ VSsetfields(vdataID, fieldlist);
    3522           0 :       /* status = */ VSseek(vdataID, offset[0]);
    3523           0 :       nrec = VSread(vdataID, buf, count[0] * incr[0],
    3524             :               FULL_INTERLACE);
    3525             :         }
    3526             :         else
    3527             :         {
    3528           0 :       mrgOffset = 0;
    3529             :         }
    3530             : 
    3531             : 
    3532             : 
    3533             :         /* Fill buffer with "Fill" value (if any) */
    3534             :         /* -------------------------------------- */
    3535           0 :                     snprintf( attrName, sizeof(attrName), "_FV_%s", fieldname);
    3536             : 
    3537           0 :         status = SWreadattr(swathID, attrName, (char *) fillbuf);
    3538           0 :         if (status == 0)
    3539             :         {
    3540           0 :       for (i = 0; i < count[0] * incr[0]; i++)
    3541             :       {
    3542           0 :           memcpy(buf + i * recsize + mrgOffset,
    3543             :            fillbuf, fldsize);
    3544             :       }
    3545             :         }
    3546             : 
    3547             : 
    3548             :         /* Write new data into buffer */
    3549             :         /* -------------------------- */
    3550           0 :         if (incr[0] == 1 && nflds == 1)
    3551             :         {
    3552           0 :       memcpy(buf, datbuf, count[0] * recsize);
    3553             :         }
    3554             :         else
    3555             :         {
    3556           0 :       for (i = 0; i < count[0]; i++)
    3557             :       {
    3558           0 :           memcpy(buf + i * recsize * incr[0] + mrgOffset,
    3559           0 :            (uint8 *) datbuf + i * fldsize, fldsize);
    3560             :       }
    3561             :         }
    3562             : 
    3563             : 
    3564             :         /* If append read last record */
    3565             :         /* -------------------------- */
    3566           0 :         if (offset[0] == nrec)
    3567             :         {
    3568             :       /* abe added "status =" to next line 8/8/97 */
    3569           0 :       status = VSseek(vdataID, offset[0] - 1);
    3570           0 :       VSread(vdataID, fillbuf, 1, FULL_INTERLACE);
    3571             :         }
    3572             :         else
    3573             :         {
    3574           0 :       status = VSseek(vdataID, offset[0]);
    3575             :         }
    3576             : 
    3577             : 
    3578             :         /* Write data into Vdata */
    3579             :         /* --------------------- */
    3580           0 :         nrec = VSwrite(vdataID, buf, count[0] * incr[0],
    3581             :            FULL_INTERLACE);
    3582             : 
    3583           0 :         free(fillbuf);
    3584           0 :                     if (status > 0)
    3585           0 :                        status = 0;
    3586             : 
    3587             :     }   /* End Write Section */
    3588             :     else
    3589             :     {
    3590             :         /* Read Section */
    3591             :         /* ------------ */
    3592           0 :         status = VSsetfields(vdataID, fieldname);
    3593           0 :         fldsize = VSsizeof(vdataID, (char *)fieldname);
    3594           0 :         buf = (uint8 *) calloc(fldsize, count[0] * incr[0]);
    3595           0 :         if(buf == NULL)
    3596             :         {
    3597           0 :       HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
    3598           0 :       return(-1);
    3599             :         }
    3600             : 
    3601           0 :         (void) VSseek(vdataID, offset[0]);
    3602           0 :         (void) VSread(vdataID, buf, count[0] * incr[0],
    3603             :          FULL_INTERLACE);
    3604             : 
    3605             : 
    3606             :         /* Copy from input buffer to returned data buffer */
    3607             :         /* ---------------------------------------------- */
    3608           0 :         if (incr[0] == 1)
    3609             :         {
    3610           0 :       memcpy(datbuf, buf, count[0] * fldsize);
    3611             :         }
    3612             :         else
    3613             :         {
    3614           0 :       for (i = 0; i < count[0]; i++)
    3615             :       {
    3616           0 :           memcpy((uint8 *) datbuf + i * fldsize,
    3617           0 :            buf + i * fldsize * incr[0], fldsize);
    3618             :       }
    3619             :         }
    3620             : 
    3621             :     }   /* End Read Section */
    3622             : 
    3623           0 :     free(buf);
    3624           0 :     VSdetach(vdataID);
    3625             :       }
    3626             :       else
    3627             :       {
    3628           0 :     HEpush(DFE_GENAPP, "SWwrrdfield", __FILE__, __LINE__);
    3629           0 :     HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
    3630             :       }
    3631             :   }     /* End One-D Field Section */
    3632             : 
    3633             :     }
    3634           0 :     return (status);
    3635             : }
    3636             : 
    3637             : 
    3638             : 
    3639             : /*----------------------------------------------------------------------------|
    3640             : |  BEGIN_PROLOG                                                               |
    3641             : |                                                                             |
    3642             : |  FUNCTION: SWreadfield                                                      |
    3643             : |                                                                             |
    3644             : |  DESCRIPTION: Reads data from field                                         |
    3645             : |                                                                             |
    3646             : |  Return Value    Type     Units     Description                             |
    3647             : |  ============   ======  =========   =====================================   |
    3648             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3649             : |                                                                             |
    3650             : |  INPUTS:                                                                    |
    3651             : |  swathID        int32               swath structure ID                      |
    3652             : |  fieldname      const char          fieldname                               |
    3653             : |  start          int32               start array                             |
    3654             : |  stride         int32               stride array                            |
    3655             : |  edge           int32               edge array                              |
    3656             : |  buffer         void                data buffer for read                    |
    3657             : |                                                                             |
    3658             : |                                                                             |
    3659             : |  OUTPUTS:                                                                   |
    3660             : |     None                                                                    |
    3661             : |                                                                             |
    3662             : |  NOTES:                                                                     |
    3663             : |                                                                             |
    3664             : |                                                                             |
    3665             : |   Date     Programmer   Description                                         |
    3666             : |  ======   ============  =================================================   |
    3667             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3668             : |                                                                             |
    3669             : |  END_PROLOG                                                                 |
    3670             : -----------------------------------------------------------------------------*/
    3671             : intn
    3672           0 : SWreadfield(int32 swathID, const char *fieldname,
    3673             :       int32 start[], int32 stride[], int32 edge[], VOIDP buffer)
    3674             : 
    3675             : {
    3676           0 :     intn            status = 0; /* routine return status variable */
    3677             : 
    3678           0 :     status = SWwrrdfield(swathID, fieldname, "r", start, stride, edge,
    3679             :        buffer);
    3680           0 :     return (status);
    3681             : }
    3682             : 
    3683             : /*----------------------------------------------------------------------------|
    3684             : |  BEGIN_PROLOG                                                               |
    3685             : |                                                                             |
    3686             : |  FUNCTION: SWgetfillvalue                                                   |
    3687             : |                                                                             |
    3688             : |  DESCRIPTION: Retrieves fill value for a specified field.                   |
    3689             : |                                                                             |
    3690             : |                                                                             |
    3691             : |  Return Value    Type     Units     Description                             |
    3692             : |  ============   ======  =========   =====================================   |
    3693             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3694             : |                                                                             |
    3695             : |  INPUTS:                                                                    |
    3696             : |  swathID        int32               swath structure ID                      |
    3697             : |  fieldname      char                field name                              |
    3698             : |                                                                             |
    3699             : |  OUTPUTS:                                                                   |
    3700             : |  fillval        void                fill value                              |
    3701             : |                                                                             |
    3702             : |  NOTES:                                                                     |
    3703             : |                                                                             |
    3704             : |                                                                             |
    3705             : |   Date     Programmer   Description                                         |
    3706             : |  ======   ============  =================================================   |
    3707             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3708             : |                                                                             |
    3709             : |  END_PROLOG                                                                 |
    3710             : -----------------------------------------------------------------------------*/
    3711             : intn
    3712           0 : SWgetfillvalue(int32 swathID, const char *fieldname, VOIDP fillval)
    3713             : {
    3714             :     intn            status; /* routine return status variable */
    3715             : 
    3716             :     int32           nt;   /* Number type */
    3717             :     int32           dims[8];  /* Dimensions array */
    3718             :     int32           dum;  /* Dummy variable */
    3719             : 
    3720             :     char            name[80]; /* Fill value "attribute" name */
    3721             : 
    3722             :     /* Check for valid swath ID */
    3723           0 :     status = SWchkswid(swathID, "SWgetfillvalue", &dum, &dum, &dum);
    3724             : 
    3725           0 :     if (status == 0)
    3726             :     {
    3727             :   /* Get field info */
    3728           0 :   status = SWfieldinfo(swathID, fieldname, &dum, dims, &nt, NULL);
    3729             : 
    3730           0 :   if (status == 0)
    3731             :   {
    3732             :       /* Read fill value attribute */
    3733           0 :       strcpy(name, "_FV_");
    3734           0 :       strcat(name, fieldname);
    3735           0 :       status = SWreadattr(swathID, name, fillval);
    3736             :   }
    3737             :   else
    3738             :   {
    3739           0 :       HEpush(DFE_GENAPP, "SWgetfillvalue", __FILE__, __LINE__);
    3740           0 :       HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
    3741             :   }
    3742             : 
    3743             :     }
    3744           0 :     return (status);
    3745             : }
    3746             : 
    3747             : 
    3748             : /*----------------------------------------------------------------------------|
    3749             : |  BEGIN_PROLOG                                                               |
    3750             : |                                                                             |
    3751             : |  FUNCTION: SWdetach                                                         |
    3752             : |                                                                             |
    3753             : |  DESCRIPTION: Detaches swath structure and performs housekeeping            |
    3754             : |                                                                             |
    3755             : |                                                                             |
    3756             : |  Return Value    Type     Units     Description                             |
    3757             : |  ============   ======  =========   =====================================   |
    3758             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3759             : |                                                                             |
    3760             : |  INPUTS:                                                                    |
    3761             : |  swathID        int32               swath structure ID                      |
    3762             : |                                                                             |
    3763             : |                                                                             |
    3764             : |  OUTPUTS:                                                                   |
    3765             : |             None                                                            |
    3766             : |                                                                             |
    3767             : |  NOTES:                                                                     |
    3768             : |                                                                             |
    3769             : |                                                                             |
    3770             : |   Date     Programmer   Description                                         |
    3771             : |  ======   ============  =================================================   |
    3772             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3773             : |  Aug 96   Joel Gales    Cleanup Region External Structure                   |
    3774             : |  Sep 96   Joel Gales    Setup dim names for SDsetdimnane in dimbuf1 rather  |
    3775             : |                         than utlstr                                         |
    3776             : |  Nov 96   Joel Gales    Call SWchkgdid to check for proper swath ID         |
    3777             : |  Dec 96   Joel Gales    Add multiple vertical subsetting garbage collection |
    3778             : |                                                                             |
    3779             : |  END_PROLOG                                                                 |
    3780             : -----------------------------------------------------------------------------*/
    3781             : intn
    3782           3 : SWdetach(int32 swathID)
    3783             : 
    3784             : {
    3785             :     intn            i;    /* Loop index */
    3786             :     intn            k;    /* Loop index */
    3787           3 :     intn            status = 0; /* routine return status variable */
    3788             : 
    3789             :     uint8          *buf;  /* Buffer for blank (initial) 1D records */
    3790             : 
    3791             :     int32           vdataID;  /* Vdata ID */
    3792             :     int32           dims[3];  /* Dimension array */
    3793             :     int32           sdInterfaceID;  /* SDS interface ID */
    3794             :     int32           sID;  /* Swath ID - offset */
    3795           3 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3796             :     int32           dum;  /* Dummy variable */
    3797             : 
    3798             :     char            swathname[VGNAMELENMAX + 1];  /* Swath name */
    3799             : 
    3800             :     /* Check for proper swath ID and get SD interface ID */
    3801             :     /* ------------------------------------------------- */
    3802           3 :     status = SWchkswid(swathID, "SWdetach", &dum, &sdInterfaceID, &dum);
    3803             : 
    3804           3 :     if (status == 0)
    3805             :     {
    3806             :   /* Subtract off swath ID offset and get swath name */
    3807             :   /* ----------------------------------------------- */
    3808           3 :   sID = swathID % idOffset;
    3809           3 :   if (sID >= NSWATH)
    3810             :   {
    3811           0 :       return -1;
    3812             :   }
    3813           3 :   Vgetname(SWXSwath[sID].IDTable, swathname);
    3814             : 
    3815             : 
    3816             :   /* Create 1D "orphaned" fields */
    3817             :   /* --------------------------- */
    3818           3 :   i = 0;
    3819             : 
    3820             :   /* Find "active" entries in 1d combination array */
    3821             :   /* --------------------------------------------- */
    3822           3 :   while (SWX1dcomb[3 * i] != 0)
    3823             :   {
    3824             :       /* For fields defined within swath... */
    3825             :       /* ---------------------------------- */
    3826           0 :       if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable)
    3827             :       {
    3828             :     /* Get dimension size and vdata ID */
    3829             :     /* ------------------------------- */
    3830           0 :     dims[0] = abs(SWX1dcomb[3 * i]);
    3831           0 :     vdataID = SWX1dcomb[3 * i + 2];
    3832             : 
    3833             :     /* Get fieldname (= vdata name) */
    3834             :     /* ---------------------------- */
    3835           0 :     char* nambuf = (char *) calloc(VSNAMELENMAX + 1, 1);
    3836           0 :     if(nambuf == NULL)
    3837             :     {
    3838           0 :         HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    3839           0 :         return(-1);
    3840             :     }
    3841             : 
    3842           0 :     VSgetname(vdataID, nambuf);
    3843             : 
    3844             :     /* Set field within vdata */
    3845             :     /* ---------------------- */
    3846           0 :     VSsetfields(vdataID, nambuf);
    3847             : 
    3848             :     /* Write (blank) records */
    3849             :     /* --------------------- */
    3850           0 :     buf = (uint8 *) calloc(VSsizeof(vdataID, nambuf), dims[0]);
    3851           0 :     if(buf == NULL)
    3852             :     {
    3853           0 :         HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    3854           0 :         free(nambuf);
    3855           0 :         return(-1);
    3856             :     }
    3857           0 :     VSwrite(vdataID, buf, dims[0], FULL_INTERLACE);
    3858             : 
    3859           0 :     free(buf);
    3860           0 :     free(nambuf);
    3861             : 
    3862             :     /* Detach Vdata */
    3863             :     /* ------------ */
    3864           0 :     VSdetach(vdataID);
    3865             :       }
    3866           0 :       i++;
    3867             :   }
    3868             : 
    3869             :   /* "Contract" 1dcomb array */
    3870             :   /* ----------------------- */
    3871           3 :   i = 0;
    3872           3 :   while (SWX1dcomb[3 * i] != 0)
    3873             :   {
    3874           0 :       if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable)
    3875             :       {
    3876           0 :     memcpy(&SWX1dcomb[3 * i],
    3877           0 :            &SWX1dcomb[3 * (i + 1)],
    3878           0 :            (512 - i - 1) * 3 * 4);
    3879             :       }
    3880             :       else
    3881           0 :     i++;
    3882             :   }
    3883             : 
    3884             :   /* "Detach" from previously attached SDSs */
    3885             :   /* -------------------------------------- */
    3886           3 :   for (k = 0; k < SWXSwath[sID].nSDS; k++)
    3887             :   {
    3888           0 :       SDendaccess(SWXSwath[sID].sdsID[k]);
    3889             :   }
    3890           3 :   free(SWXSwath[sID].sdsID);
    3891           3 :   SWXSwath[sID].sdsID = 0;
    3892           3 :   SWXSwath[sID].nSDS = 0;
    3893             : 
    3894             : 
    3895             :   /* Detach Swath Vgroups */
    3896             :   /* -------------------- */
    3897           3 :   Vdetach(SWXSwath[sID].VIDTable[0]);
    3898           3 :   Vdetach(SWXSwath[sID].VIDTable[1]);
    3899           3 :   Vdetach(SWXSwath[sID].VIDTable[2]);
    3900           3 :   Vdetach(SWXSwath[sID].IDTable);
    3901             : 
    3902             : 
    3903             :   /* Delete entries from External Arrays */
    3904             :   /* ----------------------------------- */
    3905           3 :   SWXSwath[sID].active = 0;
    3906           3 :   SWXSwath[sID].VIDTable[0] = 0;
    3907           3 :   SWXSwath[sID].VIDTable[1] = 0;
    3908           3 :   SWXSwath[sID].VIDTable[2] = 0;
    3909           3 :   SWXSwath[sID].IDTable = 0;
    3910           3 :   SWXSwath[sID].fid = 0;
    3911             : 
    3912             : 
    3913             :   /* Free Region Pointers */
    3914             :   /* -------------------- */
    3915         771 :   for (k = 0; k < NSWATHREGN; k++)
    3916             :   {
    3917         768 :       if (SWXRegion[k] != 0 &&
    3918           0 :     SWXRegion[k]->swathID == swathID)
    3919             :       {
    3920           0 :     for (i = 0; i < 8; i++)
    3921             :     {
    3922           0 :         if (SWXRegion[k]->DimNamePtr[i] != 0)
    3923             :         {
    3924           0 :       free(SWXRegion[k]->DimNamePtr[i]);
    3925             :         }
    3926             :     }
    3927             : 
    3928           0 :     free(SWXRegion[k]);
    3929           0 :     SWXRegion[k] = 0;
    3930             :       }
    3931             :   }
    3932             : 
    3933             :     }
    3934           3 :     return (status);
    3935             : }
    3936             : 
    3937             : /*----------------------------------------------------------------------------|
    3938             : |  BEGIN_PROLOG                                                               |
    3939             : |                                                                             |
    3940             : |  FUNCTION: SWclose                                                          |
    3941             : |                                                                             |
    3942             : |  DESCRIPTION: Closes HDF-EOS file                                           |
    3943             : |                                                                             |
    3944             : |                                                                             |
    3945             : |  Return Value    Type     Units     Description                             |
    3946             : |  ============   ======  =========   =====================================   |
    3947             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3948             : |                                                                             |
    3949             : |  INPUTS:                                                                    |
    3950             : |  fid            int32               File ID                                 |
    3951             : |                                                                             |
    3952             : |  OUTPUTS:                                                                   |
    3953             : |             None                                                            |
    3954             : |                                                                             |
    3955             : |  NOTES:                                                                     |
    3956             : |                                                                             |
    3957             : |                                                                             |
    3958             : |   Date     Programmer   Description                                         |
    3959             : |  ======   ============  =================================================   |
    3960             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3961             : |                                                                             |
    3962             : |  END_PROLOG                                                                 |
    3963             : -----------------------------------------------------------------------------*/
    3964             : intn
    3965          11 : SWclose(int32 fid)
    3966             : 
    3967             : {
    3968          11 :     intn            status = 0; /* routine return status variable */
    3969             : 
    3970             :     /* Call EHclose to perform file close */
    3971             :     /* ---------------------------------- */
    3972          11 :     status = EHclose(fid);
    3973             : 
    3974          11 :     return (status);
    3975             : }
    3976             : 
    3977             : /*----------------------------------------------------------------------------|
    3978             : |  BEGIN_PROLOG                                                               |
    3979             : |                                                                             |
    3980             : |  FUNCTION: SWgeomapinfo                                                     |
    3981             : |                                                                             |
    3982             : |  DESCRIPTION: Returns mapping information for dimension                     |
    3983             : |                                                                             |
    3984             : |                                                                             |
    3985             : |  Return Value    Type     Units     Description                             |
    3986             : |  ============   ======  =========   =====================================   |
    3987             : |  status         intn                2 for l_indexed mapping, 1 for regular    |
    3988             : |                                     mapping, 0 if the dimension is not      |
    3989             : |                                     and (-1) FAIL                           |
    3990             : |                                                                             |
    3991             : |  INPUTS:                                                                    |
    3992             : |  swathID        int32               swath structure id                      |
    3993             : |  geodim         char                geolocation dimension name              |
    3994             : |                                                                             |
    3995             : |  OUTPUTS:                                                                   |
    3996             : |                                                                             |
    3997             : |  NONE                                                                       |
    3998             : |                                                                             |
    3999             : |  NOTES:                                                                     |
    4000             : |                                                                             |
    4001             : |                                                                             |
    4002             : |   Date     Programmer   Description                                         |
    4003             : |  ======   ============  =================================================   |
    4004             : |  Aug 97   Abe Taaheri   Original Programmer                                 |
    4005             : |  Sept 97  DaW           Modified return value so errors can be trapped      |
    4006             : |                                                                             |
    4007             : |  END_PROLOG                                                                 |
    4008             : -----------------------------------------------------------------------------*/
    4009             : intn
    4010           0 : SWgeomapinfo(int32 swathID, const char *geodim)
    4011             : 
    4012             : {
    4013             :     intn            status; /* routine return status variable */
    4014             : 
    4015             :     int32           fid;  /* HDF-EOS file ID */
    4016             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    4017             :     int32           swVgrpID; /* Swath root Vgroup ID */
    4018           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    4019             : 
    4020             :     char           *metabufr; /* Pointer to structural metadata (SM) */
    4021             :     char           *metabufi; /* Pointer to structural metadata (SM) */
    4022             :     char           *metaptrsr[2];/* Pointers to begin and end of SM section */
    4023             :     char           *metaptrsi[2];/* Pointers to begin and end of SM section */
    4024             :     char            swathname[80];  /* Swath Name */
    4025             :     char           *utlstrr;   /* Utility string */
    4026             :     char           *utlstri;   /* Utility string */
    4027             : 
    4028             : 
    4029             :     /* Allocate space for utility string */
    4030             :     /* --------------------------------- */
    4031           0 :     utlstrr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    4032           0 :     if(utlstrr == NULL)
    4033             :     {
    4034           0 :   HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__);
    4035           0 :   return(-1);
    4036             :     }
    4037           0 :     utlstri = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    4038           0 :     if(utlstri == NULL)
    4039             :     {
    4040           0 :   HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__);
    4041           0 :   free(utlstrr);
    4042           0 :   return(-1);
    4043             :     }
    4044           0 :     status = -1;
    4045             : 
    4046             :     /* Check for valid swath id */
    4047           0 :     status = SWchkswid(swathID, "SWgeomapinfo", &fid, &sdInterfaceID, &swVgrpID);
    4048           0 :     if (status == 0)
    4049             :     {
    4050             :   /* Get swath name */
    4051           0 :   int sID = swathID % idOffset;
    4052           0 :   if (sID >= NSWATH)
    4053             :   {
    4054           0 :       free(utlstrr);
    4055           0 :       free(utlstri);
    4056           0 :       return -1;
    4057             :   }
    4058           0 :   Vgetname(SWXSwath[sID].IDTable, swathname);
    4059             : 
    4060             :   /* Get pointers to "DimensionMap" section within SM */
    4061           0 :   metabufr = EHmetagroup(sdInterfaceID, swathname, "s",
    4062             :                "DimensionMap", metaptrsr);
    4063             : 
    4064           0 :   if(metabufr == NULL)
    4065             :   {
    4066           0 :       free(utlstrr);
    4067           0 :       free(utlstri);
    4068           0 :       return(-1);
    4069             :   }
    4070             :   /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
    4071           0 :   snprintf(utlstrr, UTLSTR_MAX_SIZE, "%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
    4072             :     "\"\n\t\t\t\tDataDimension=");
    4073           0 :   metaptrsr[0] = strstr(metaptrsr[0], utlstrr);
    4074             : 
    4075             :   /* Get pointers to "IndexDimensionMap" section within SM */
    4076           0 :   metabufi = EHmetagroup(sdInterfaceID, swathname, "s",
    4077             :                "IndexDimensionMap", metaptrsi);
    4078           0 :   if(metabufi == NULL)
    4079             :   {
    4080           0 :       free(utlstrr);
    4081           0 :       free(utlstri);
    4082           0 :       return(-1);
    4083             :   }
    4084             :   /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
    4085           0 :   snprintf(utlstri, UTLSTR_MAX_SIZE, "%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
    4086             :     "\"\n\t\t\t\tDataDimension=");
    4087           0 :   metaptrsi[0] = strstr(metaptrsi[0], utlstri);
    4088             : 
    4089             :   /*
    4090             :   ** If regular mapping found add 1 to status
    4091             :         ** If l_indexed mapping found add 2
    4092             :         */
    4093           0 :   if (metaptrsr[0] < metaptrsr[1] && metaptrsr[0] != NULL)
    4094             :   {
    4095           0 :       status = status + 1;
    4096             :         }
    4097             : 
    4098           0 :         if (metaptrsi[0] < metaptrsi[1] && metaptrsi[0] != NULL)
    4099             :         {
    4100           0 :            status = status + 2;
    4101             :         }
    4102             : 
    4103           0 :   free(metabufr);
    4104           0 :   free(metabufi);
    4105             :     }
    4106             : 
    4107           0 :     free(utlstrr);
    4108           0 :     free(utlstri);
    4109             : 
    4110           0 :     return (status);
    4111             : }
    4112             : 
    4113             : /*----------------------------------------------------------------------------|
    4114             : |  BEGIN_PROLOG                                                               |
    4115             : |                                                                             |
    4116             : |  FUNCTION: SWsdid                                                           |
    4117             : |                                                                             |
    4118             : |  DESCRIPTION: Returns SD element ID for swath field                         |
    4119             : |                                                                             |
    4120             : |                                                                             |
    4121             : |  Return Value    Type     Units     Description                             |
    4122             : |  ============   ======  =========   =====================================   |
    4123             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    4124             : |                                                                             |
    4125             : |  INPUTS:                                                                    |
    4126             : |  swathID        int32               swath structure ID                      |
    4127             : |  fieldname      const char          field name                              |
    4128             : |                                                                             |
    4129             : |                                                                             |
    4130             : |  OUTPUTS:                                                                   |
    4131             : |  sdid           int32               SD element ID                           |
    4132             : |                                                                             |
    4133             : |  NOTES:                                                                     |
    4134             : |                                                                             |
    4135             : |                                                                             |
    4136             : |   Date     Programmer   Description                                         |
    4137             : |  ======   ============  =================================================   |
    4138             : |  Oct 07   Andrey Kiselev  Original Programmer                               |
    4139             : |                                                                             |
    4140             : |  END_PROLOG                                                                 |
    4141             : -----------------------------------------------------------------------------*/
    4142             : intn
    4143           0 : SWsdid(int32 swathID, const char *fieldname, int32 *sdid)
    4144             : {
    4145             :     intn            status;         /* routine return status variable */
    4146             :     int32           fid;          /* HDF-EOS file ID */
    4147             :     int32           sdInterfaceID;      /* HDF SDS interface ID */
    4148             :     int32           dum;          /* Dummy variable */
    4149             :     int32           dims[H4_MAX_VAR_DIMS]; /* Field/SDS dimensions */
    4150             : 
    4151           0 :     status = SWchkswid(swathID, "SWsdid", &fid, &sdInterfaceID, &dum);
    4152           0 :     if (status != -1)
    4153             :     {
    4154           0 :         status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
    4155             :                              sdid, &dum, &dum, &dum, dims, &dum);
    4156             :     }
    4157             : 
    4158           0 :     return (status);
    4159             : }

Generated by: LCOV version 1.14