LCOV - code coverage report
Current view: top level - frmts/hdf4/hdf-eos - SWapi.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 10 3493 0.3 %
Date: 2024-05-07 17:03:27 Functions: 3 56 5.4 %

          Line data    Source code
       1             : /*****************************************************************************
       2             :  * $Id$
       3             :  *
       4             :  * This module has a number of additions and improvements over the original
       5             :  * implementation to be suitable for usage in GDAL HDF driver.
       6             :  *
       7             :  * Andrey Kiselev <dron@ak4719.spb.edu> is responsible for all the changes.
       8             :  ****************************************************************************/
       9             : 
      10             : /*
      11             : Copyright (C) 1996 Hughes and Applied Research Corporation
      12             : 
      13             : Permission to use, modify, and distribute this software and its documentation 
      14             : for any purpose without fee is hereby granted, provided that the above 
      15             : copyright notice appear in all copies and that both that copyright notice and 
      16             : this permission notice appear in supporting documentation.
      17             : */
      18             : /*****************************************************************************
      19             : REVISIONS:
      20             : 
      21             : Aug 31, 1999  Abe Taaheri    Changed memory allocation for utility strings to
      22             :                              the size of UTLSTR_MAX_SIZE.
      23             :            Added error check for memory unavailability in
      24             :            several functions.
      25             :            Added check for NULL metabuf returned from
      26             :            EHmeta... functions. NULL pointer returned from
      27             :            EHmeta... functions indicate that memory could not
      28             :            be allocated for metabuf.
      29             : 
      30             : June 05, 2003 Abe Taaheri / Bruce Beaumont
      31             : 
      32             :                             Changed MAXNREGIONS to 1024 to support MOPITT data
      33             :           Supplied cast for compcode in call to 
      34             :             SDsetcompress to avoid compiler error
      35             :           Removed declaration for unused variable rstatus 
      36             :             in SWwrrdfield
      37             :           Removed initialization code for unused variables 
      38             :             in SWwrrdfield
      39             :           Removed declaration for unused variable tmpVal 
      40             :             in SWdefboxregion
      41             :           Added code in SWdefboxregion to check for l_index k
      42             :             exceeding NSWATHREGN to avoid overwriting 
      43             :             memory
      44             :           Removed declaration for unused variable retchar 
      45             :             in SWregionl_index
      46             :           Removed initialization code for unused variables 
      47             :             in SWregionl_index
      48             :           Removed declarations for unused variables tstatus,
      49             :             nfields, nflgs, and swathname in SWextractregion
      50             :           Removed initialization code for unused variables 
      51             :             in SWextractregion
      52             :           Removed declaration for unused variable 
      53             :             land_status in SWscan2longlat
      54             :           Removed initialization code for unused variables 
      55             :             in SWscan2longlat
      56             :           Added clear (0) of timeflag in SWextractperiod if 
      57             :             return status from SWextractregion is non-zero
      58             :           Removed declarations for unused variables tstatus,
      59             :             scandim, ndfields, ndflds, and swathname in 
      60             :             SWregioninfo
      61             :           Removed initialization code for unused variables 
      62             :             in SWregioninfo
      63             :           Added clear (0) of timeflag in SWperiodinfo if 
      64             :             return status from SWregioninfo is non-zero
      65             :           Removed declarations for unused variables size, 
      66             :             nfields, nflds, nswath, idxsz, cornerlon, and 
      67             :             cornerlat in SWdefscanregion
      68             :           Removed initialization code for unused variables 
      69             :             in SWdefscanregion
      70             :           Removed declarations for unused variables dims2, 
      71             :             rank, nt, swathname, dimlist, and buffer in 
      72             :             SWupdateidxmap
      73             :           Removed declaration for unused variable statmeta 
      74             :             in SWgeomapinfo
      75             : ******************************************************************************/
      76             : 
      77             : #include "cpl_port.h" /* for M_PI */
      78             : #include "cpl_string.h" /* for CPLsnprintf */
      79             : 
      80             : #include "mfhdf.h"
      81             : #include "hcomp.h"
      82             : #include "HdfEosDef.h"
      83             : #include <math.h>
      84             : 
      85             : #include "hdf4compat.h"
      86             : 
      87             : #define SWIDOFFSET 1048576
      88             : 
      89             : 
      90             : static int32 SWX1dcomb[512*3];
      91             : static int32 SWXSDcomb[512*5];
      92             : static char  SWXSDname[HDFE_NAMBUFSIZE];
      93             : static char  SWXSDdims[HDFE_DIMBUFSIZE];
      94             : 
      95             : /* This flag was added to allow the Time field to have different Dimensions
      96             : ** than Longitude and Latitude and still be used for subsetting
      97             : ** 23 June,1997  DaW
      98             : */
      99             : static intn  timeflag = 0;
     100             : 
     101             : 
     102             : /* Added for routine that converts scanline to Lat/long
     103             : ** for floating scene subsetting
     104             : ** Jul 1999 DaW
     105             : */
     106             : #define RADOE 6371.0    /* Radius of Earth in Km */
     107             : 
     108             : #define NSWATH 200
     109             : /* Swath Structure External Arrays */
     110             : struct swathStructure 
     111             : {
     112             :     int32 active;
     113             :     int32 IDTable;
     114             :     int32 VIDTable[3];
     115             :     int32 fid;
     116             :     int32 nSDS;
     117             :     int32 *sdsID;
     118             :     int32 compcode;
     119             :     intn  compparm[5];
     120             :     int32 tilecode;
     121             :     int32 tilerank;
     122             :     int32 tiledims[8];
     123             : };
     124             : static struct swathStructure SWXSwath[NSWATH];
     125             : 
     126             : 
     127             : 
     128             : #define NSWATHREGN 256
     129             : #define MAXNREGIONS 1024
     130             : struct swathRegion
     131             : {
     132             :     int32 fid;
     133             :     int32 swathID;
     134             :     int32 nRegions;
     135             :     int32 StartRegion[MAXNREGIONS];
     136             :     int32 StopRegion[MAXNREGIONS];
     137             :     int32 StartVertical[8];
     138             :     int32 StopVertical[8];
     139             :     int32 StartScan[8];
     140             :     int32 StopScan[8];
     141             :     char *DimNamePtr[8];
     142             :     intn band8flag;
     143             :     intn  scanflag;
     144             : };
     145             : static struct swathRegion *SWXRegion[NSWATHREGN];
     146             : 
     147             : /* define a macro for the string size of the utility strings. The value
     148             :    of 80 in previous version of this code was resulting in core dump (Array 
     149             :    Bounds Write and Array Bounds Read problem in SWfinfo function and the 
     150             :    functions called from there) for 7-8 dimensional fields where the 
     151             :    string length for "DimList" can exceed 80 characters, including " and 
     152             :    commas in the string. The length now is 512 which seems to be more 
     153             :    than enough to avoid the problem mentioned above. */
     154             :    
     155             : #define UTLSTR_MAX_SIZE 512
     156             : 
     157             : /* Swath Prototypes (internal routines) */
     158             : static intn SWchkswid(int32, const char *, int32 *, int32 *, int32 *);
     159             : static int32 SWfinfo(int32, const char *, const char *, int32 *,
     160             :                      int32 [], int32 *, char *);
     161             : static intn SWdefinefield(int32, const char *, const char *, const char *, int32, int32);
     162             : static intn SWwrrdattr(int32, const char *, int32, int32, const char *, VOIDP);
     163             : static intn SW1dfldsrch(int32, int32, const char *, const char *, int32 *,
     164             :                         int32 *, int32 *);
     165             : static intn SWSDfldsrch(int32, int32, const char *, int32 *, int32 *, 
     166             :                         int32 *, int32 *, int32 [], int32 *);
     167             : static intn SWwrrdfield(int32, const char *, const char *,
     168             :                         int32 [], int32 [], int32 [], VOIDP);
     169             : static int32 SWinqfields(int32, const char *, char *, int32 [], int32 []);
     170             : static intn SWscan2longlat(int32, const char *, VOIDP, int32 [], int32 [],
     171             :                            int32 *, int32, int32);
     172             : 
     173             : 
     174             : /*----------------------------------------------------------------------------|
     175             : |  BEGIN_PROLOG                                                               |
     176             : |                                                                             |
     177             : |  FUNCTION: SWopen                                                           |
     178             : |                                                                             |
     179             : |  DESCRIPTION:                                                               |
     180             : |                                                                             |
     181             : |                                                                             |
     182             : |  Return Value    Type     Units     Description                             |
     183             : |  ============   ======  =========   =====================================   |
     184             : |  fid            int32               HDF-EOS file ID                         |
     185             : |                                                                             |
     186             : |  INPUTS:                                                                    |
     187             : |  filename       char                Filename                                |
     188             : |  access         intn                HDF access code                         |
     189             : |                                                                             |
     190             : |                                                                             |
     191             : |  OUTPUTS:                                                                   |
     192             : |             None                                                            |
     193             : |                                                                             |
     194             : |  NOTES:                                                                     |
     195             : |                                                                             |
     196             : |                                                                             |
     197             : |   Date     Programmer   Description                                         |
     198             : |  ======   ============  =================================================   |
     199             : |  Jun 96   Joel Gales    Original Programmer                                 |
     200             : |                                                                             |
     201             : |  END_PROLOG                                                                 |
     202             : -----------------------------------------------------------------------------*/
     203             : int32
     204           4 : SWopen(const char *filename, intn i_access)
     205             : 
     206             : {
     207             :     int32           fid /* HDF-EOS file ID */ ;
     208             : 
     209             :     /* Call EHopen to perform file access */
     210             :     /* ---------------------------------- */
     211           4 :     fid = EHopen(filename, i_access);
     212             : 
     213           4 :     return (fid);
     214             : }
     215             : 
     216             : /*----------------------------------------------------------------------------|
     217             : |  BEGIN_PROLOG                                                               |
     218             : |                                                                             |
     219             : |  FUNCTION: SWcreate                                                         |
     220             : |                                                                             |
     221             : |  DESCRIPTION: Creates a new swath structure and returns swath ID            |
     222             : |                                                                             |
     223             : |                                                                             |
     224             : |  Return Value    Type     Units     Description                             |
     225             : |  ============   ======  =========   =====================================   |
     226             : |  swathID        int32               Swath structure ID                      |
     227             : |                                                                             |
     228             : |  INPUTS:                                                                    |
     229             : |  fid            int32               File ID                                 |
     230             : |  swathname      char                Swath structure name                    |
     231             : |                                                                             |
     232             : |  OUTPUTS:                                                                   |
     233             : |             None                                                            |
     234             : |                                                                             |
     235             : |  NOTES:                                                                     |
     236             : |                                                                             |
     237             : |                                                                             |
     238             : |   Date     Programmer   Description                                         |
     239             : |  ======   ============  =================================================   |
     240             : |  Jun 96   Joel Gales    Original Programmer                                 |
     241             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
     242             : |  Sep 96   Joel Gales    Check swath name for length                         |
     243             : |  Mar 97   Joel Gales    Enlarge utlbuf to 512                               |
     244             : |                                                                             |
     245             : |  END_PROLOG                                                                 |
     246             : -----------------------------------------------------------------------------*/
     247             : int32
     248           0 : SWcreate(int32 fid, const char *swathname)
     249             : {
     250             :     intn            i;    /* Loop index */
     251           0 :     intn            nswathopen = 0; /* # of swath structures open */
     252           0 :     intn            status = 0; /* routine return status variable */
     253             : 
     254             :     uint8           l_access; /* Read/Write file access code */
     255             : 
     256             :     int32           HDFfid; /* HDF file id */
     257             :     int32           vgRef;  /* Vgroup reference number */
     258             :     int32           vgid[4];  /* Vgroup ID array */
     259           0 :     int32           swathID = -1; /* HDF-EOS swath ID */
     260             : 
     261             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     262           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     263           0 :     int32           nSwath = 0; /* Swath counter */
     264             : 
     265             :     char            name[80]; /* Vgroup name */
     266             :     char            class[80];  /* Vgroup class */
     267             :     char            errbuf[256];/* Buffer for error message */
     268             :     char            utlbuf[512];/* Utility buffer */
     269             :     char            utlbuf2[32];/* Utility buffer 2 */
     270             : 
     271             :     /*
     272             :      * Check HDF-EOS file ID, get back HDF file ID, SD interface ID  and
     273             :      * access code
     274             :      */
     275           0 :     status = EHchkfid(fid, swathname, &HDFfid, &sdInterfaceID, &l_access);
     276             : 
     277             : 
     278             :     /* Check swathname for length */
     279             :     /* -------------------------- */
     280           0 :     if ((intn) strlen(swathname) > VGNAMELENMAX)
     281             :     {
     282           0 :   status = -1;
     283           0 :   HEpush(DFE_GENAPP, "SWcreate", __FILE__, __LINE__);
     284           0 :   HEreport("Swathname \"%s\" must be less than %d characters.\n",
     285             :      swathname, VGNAMELENMAX);
     286             :     }
     287             : 
     288             : 
     289           0 :     if (status == 0)
     290             :     {
     291             : 
     292             :   /* Determine number of swaths currently opened */
     293             :   /* ------------------------------------------- */
     294           0 :   for (i = 0; i < NSWATH; i++)
     295             :   {
     296           0 :       nswathopen += SWXSwath[i].active;
     297             :   }
     298             : 
     299             : 
     300             :   /* Setup file interface */
     301             :   /* -------------------- */
     302           0 :   if (nswathopen < NSWATH)
     303             :   {
     304             : 
     305             :       /* Check that swath has not been previously opened */
     306             :       /* ----------------------------------------------- */
     307           0 :       vgRef = -1;
     308             : 
     309             :       while (1)
     310             :       {
     311           0 :     vgRef = Vgetid(HDFfid, vgRef);
     312             : 
     313             :     /* If no more Vgroups then exist while loop */
     314             :     /* ---------------------------------------- */
     315           0 :     if (vgRef == -1)
     316             :     {
     317           0 :         break;
     318             :     }
     319             : 
     320             :     /* Get name and class of Vgroup */
     321             :     /* ---------------------------- */
     322           0 :     vgid[0] = Vattach(HDFfid, vgRef, "r");
     323           0 :     Vgetname(vgid[0], name);
     324           0 :     Vgetclass(vgid[0], class);
     325           0 :     Vdetach(vgid[0]);
     326             : 
     327             :     /* If SWATH then increment # swath counter */
     328             :     /* --------------------------------------- */
     329           0 :     if (strcmp(class, "SWATH") == 0)
     330             :     {
     331           0 :         nSwath++;
     332             :     }
     333             : 
     334             :     /* If swath already exist, return error */
     335             :     /* ------------------------------------ */
     336           0 :     if (strcmp(name, swathname) == 0 &&
     337           0 :         strcmp(class, "SWATH") == 0)
     338             :     {
     339           0 :         status = -1;
     340           0 :         HEpush(DFE_GENAPP, "SWcreate", __FILE__, __LINE__);
     341           0 :         HEreport("\"%s\" already exists.\n", swathname);
     342           0 :         break;
     343             :     }
     344             :       }
     345             : 
     346             : 
     347           0 :       if (status == 0)
     348             :       {
     349             : 
     350             :     /* Create Root Vgroup for Swath */
     351             :     /* ---------------------------- */
     352           0 :     vgid[0] = Vattach(HDFfid, -1, "w");
     353             : 
     354             : 
     355             :     /* Set Name and Class (SWATH) */
     356             :     /* -------------------------- */
     357           0 :     Vsetname(vgid[0], swathname);
     358           0 :     Vsetclass(vgid[0], "SWATH");
     359             : 
     360             : 
     361             : 
     362             :     /* Create Geolocation Fields Vgroup */
     363             :     /* -------------------------------- */
     364           0 :     vgid[1] = Vattach(HDFfid, -1, "w");
     365           0 :     Vsetname(vgid[1], "Geolocation Fields");
     366           0 :     Vsetclass(vgid[1], "SWATH Vgroup");
     367           0 :     Vinsert(vgid[0], vgid[1]);
     368             : 
     369             : 
     370             : 
     371             :     /* Create Data Fields Vgroup */
     372             :     /* ------------------------- */
     373           0 :     vgid[2] = Vattach(HDFfid, -1, "w");
     374           0 :     Vsetname(vgid[2], "Data Fields");
     375           0 :     Vsetclass(vgid[2], "SWATH Vgroup");
     376           0 :     Vinsert(vgid[0], vgid[2]);
     377             : 
     378             : 
     379             : 
     380             :     /* Create Attributes Vgroup */
     381             :     /* ------------------------ */
     382           0 :     vgid[3] = Vattach(HDFfid, -1, "w");
     383           0 :     Vsetname(vgid[3], "Swath Attributes");
     384           0 :     Vsetclass(vgid[3], "SWATH Vgroup");
     385           0 :     Vinsert(vgid[0], vgid[3]);
     386             : 
     387             : 
     388             : 
     389             :     /* Establish Swath in Structural MetaData Block */
     390             :     /* -------------------------------------------- */
     391           0 :     snprintf(utlbuf, sizeof(utlbuf), "%s%ld%s%s%s",
     392           0 :       "\tGROUP=SWATH_", (long)nSwath + 1,
     393             :       "\n\t\tSwathName=\"", swathname, "\"\n");
     394             : 
     395           0 :     strcat(utlbuf, "\t\tGROUP=Dimension\n");
     396           0 :     strcat(utlbuf, "\t\tEND_GROUP=Dimension\n");
     397           0 :     strcat(utlbuf, "\t\tGROUP=DimensionMap\n");
     398           0 :     strcat(utlbuf, "\t\tEND_GROUP=DimensionMap\n");
     399           0 :     strcat(utlbuf, "\t\tGROUP=IndexDimensionMap\n");
     400           0 :     strcat(utlbuf, "\t\tEND_GROUP=IndexDimensionMap\n");
     401           0 :     strcat(utlbuf, "\t\tGROUP=GeoField\n");
     402           0 :     strcat(utlbuf, "\t\tEND_GROUP=GeoField\n");
     403           0 :     strcat(utlbuf, "\t\tGROUP=DataField\n");
     404           0 :     strcat(utlbuf, "\t\tEND_GROUP=DataField\n");
     405           0 :     strcat(utlbuf, "\t\tGROUP=MergedFields\n");
     406           0 :     strcat(utlbuf, "\t\tEND_GROUP=MergedFields\n");
     407           0 :     CPLsnprintf(utlbuf2, sizeof(utlbuf2), "%s%ld%s",
     408           0 :       "\tEND_GROUP=SWATH_", (long)nSwath + 1, "\n");
     409           0 :     strcat(utlbuf, utlbuf2);
     410             : 
     411             : 
     412           0 :     status = EHinsertmeta(sdInterfaceID, "", "s", 1001L,
     413             :               utlbuf, NULL);
     414             :       }
     415             :   }
     416             :   else
     417             :   {
     418             :       /* Too many files opened */
     419             :       /* --------------------- */
     420           0 :       status = -1;
     421           0 :       strcpy(errbuf,
     422             :        "No more than %d swaths may be open simultaneously");
     423           0 :       strcat(errbuf, " (%s)");
     424           0 :       HEpush(DFE_DENIED, "SWcreate", __FILE__, __LINE__);
     425           0 :       HEreport(errbuf, NSWATH, swathname);
     426             :   }
     427             : 
     428             : 
     429             :   /* Assign swathID # & Load swath and SWXSwath table entries */
     430             :   /* -------------------------------------------------------- */
     431           0 :   if (status == 0)
     432             :   {
     433             : 
     434           0 :       for (i = 0; i < NSWATH; i++)
     435             :       {
     436           0 :     if (SWXSwath[i].active == 0)
     437             :     {
     438             :         /*
     439             :          * Set swathID, Set swath entry active, Store root Vgroup
     440             :          * ID, Store sub Vgroup IDs, Store HDF-EOS file ID
     441             :          */
     442           0 :         swathID = i + idOffset;
     443           0 :         SWXSwath[i].active = 1;
     444           0 :         SWXSwath[i].IDTable = vgid[0];
     445           0 :         SWXSwath[i].VIDTable[0] = vgid[1];
     446           0 :         SWXSwath[i].VIDTable[1] = vgid[2];
     447           0 :         SWXSwath[i].VIDTable[2] = vgid[3];
     448           0 :         SWXSwath[i].fid = fid;
     449           0 :         status = 0;
     450           0 :         break;
     451             :     }
     452             :       }
     453             : 
     454             :   }
     455             :     }
     456           0 :     return (swathID);
     457             : }
     458             : 
     459             : 
     460             : /*----------------------------------------------------------------------------|
     461             : |  BEGIN_PROLOG                                                               |
     462             : |                                                                             |
     463             : |  FUNCTION: SWattach                                                         |
     464             : |                                                                             |
     465             : |  DESCRIPTION:  Attaches to an existing swath within the file.               |
     466             : |                                                                             |
     467             : |                                                                             |
     468             : |  Return Value    Type     Units     Description                             |
     469             : |  ============   ======  =========   =====================================   |
     470             : |  swathID        int32               swath structure ID                      |
     471             : |                                                                             |
     472             : |  INPUTS:                                                                    |
     473             : |  fid            int32               HDF-EOS file ID                         |
     474             : |  swathname      char                swath structure name                    |
     475             : |                                                                             |
     476             : |  OUTPUTS:                                                                   |
     477             : |             None                                                            |
     478             : |                                                                             |
     479             : |  NOTES:                                                                     |
     480             : |                                                                             |
     481             : |                                                                             |
     482             : |   Date     Programmer   Description                                         |
     483             : |  ======   ============  =================================================   |
     484             : |  Jun 96   Joel Gales    Original Programmer                                 |
     485             : |  Apr 99   David Wynne   Modified test for memory allocation check when no   |
     486             : |                         SDSs are in the Swath, NCR22513                     |
     487             : |                                                                             |
     488             : |  END_PROLOG                                                                 |
     489             : -----------------------------------------------------------------------------*/
     490             : int32
     491           0 : SWattach(int32 fid, const char *swathname)
     492             : 
     493             : {
     494             :     intn            i;    /* Loop index */
     495             :     intn            j;    /* Loop index */
     496           0 :     intn            nswathopen = 0; /* # of swath structures open */
     497             :     intn            status; /* routine return status variable */
     498             : 
     499             :     uint8           acs;  /* Read/Write file access code */
     500             : 
     501             :     int32           HDFfid; /* HDF file id */
     502             :     int32           vgRef;  /* Vgroup reference number */
     503             :     int32           vgid[4];  /* Vgroup ID array */
     504           0 :     int32           swathID = -1; /* HDF-EOS swath ID */
     505             :     int32          *tags; /* Pnt to Vgroup object tags array */
     506             :     int32          *refs; /* Pnt to Vgroup object refs array */
     507             :     int32           dum;  /* dummy variable */
     508             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     509             :     int32           nObjects; /* # of objects in Vgroup */
     510             :     int32           nSDS; /* SDS counter */
     511             :     int32           l_index;  /* SDS l_index */
     512             :     int32           sdid; /* SDS object ID */
     513           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     514             : 
     515             :     char            name[80]; /* Vgroup name */
     516             :     char            class[80];  /* Vgroup class */
     517             :     char            errbuf[256];/* Buffer for error message */
     518             :     char            acsCode[1]; /* Read/Write access char: "r/w" */
     519             : 
     520             : 
     521             :     /* Check HDF-EOS file ID, get back HDF file ID and access code */
     522             :     /* ----------------------------------------------------------- */
     523           0 :     status = EHchkfid(fid, swathname, &HDFfid, &dum, &acs);
     524             : 
     525             : 
     526           0 :     if (status == 0)
     527             :     {
     528             :   /* Convert numeric access code to character */
     529             :   /* ---------------------------------------- */
     530           0 :   acsCode[0] = (acs == 1) ? 'w' : 'r';
     531             : 
     532             :   /* Determine number of swaths currently opened */
     533             :   /* ------------------------------------------- */
     534           0 :   for (i = 0; i < NSWATH; i++)
     535             :   {
     536           0 :       nswathopen += SWXSwath[i].active;
     537             :   }
     538             : 
     539             :   /* If room for more ... */
     540             :   /* -------------------- */
     541           0 :   if (nswathopen < NSWATH)
     542             :   {
     543             : 
     544             :       /* Search Vgroups for Swath */
     545             :       /* ------------------------ */
     546           0 :       vgRef = -1;
     547             : 
     548             :       while (1)
     549             :       {
     550           0 :     vgRef = Vgetid(HDFfid, vgRef);
     551             : 
     552             : 
     553             :     /* If no more Vgroups then exist while loop */
     554             :     /* ---------------------------------------- */
     555           0 :     if (vgRef == -1)
     556             :     {
     557           0 :         break;
     558             :     }
     559             : 
     560             :     /* Get name and class of Vgroup */
     561             :     /* ---------------------------- */
     562           0 :     vgid[0] = Vattach(HDFfid, vgRef, "r");
     563           0 :     Vgetname(vgid[0], name);
     564           0 :     Vgetclass(vgid[0], class);
     565             : 
     566             : 
     567             :     /*
     568             :      * If Vgroup with swathname and class SWATH found, load
     569             :      * tables
     570             :      */
     571             : 
     572           0 :     if (strcmp(name, swathname) == 0 &&
     573           0 :         strcmp(class, "SWATH") == 0)
     574             :     {
     575             :         /* Attach to "Fields" and "Swath Attributes" Vgroups */
     576             :         /* ------------------------------------------------- */
     577           0 :         tags = (int32 *) malloc(sizeof(int32) * 3);
     578           0 :         if(tags == NULL)
     579             :         { 
     580           0 :       HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     581           0 :       return(-1);
     582             :         }
     583           0 :         refs = (int32 *) malloc(sizeof(int32) * 3);
     584           0 :         if(refs == NULL)
     585             :         { 
     586           0 :       HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     587           0 :       free(tags);
     588           0 :       return(-1);
     589             :         }
     590           0 :         Vgettagrefs(vgid[0], tags, refs, 3);
     591           0 :         vgid[1] = Vattach(HDFfid, refs[0], acsCode);
     592           0 :         vgid[2] = Vattach(HDFfid, refs[1], acsCode);
     593           0 :         vgid[3] = Vattach(HDFfid, refs[2], acsCode);
     594           0 :         free(tags);
     595           0 :         free(refs);
     596             : 
     597             :         /* Setup External Arrays */
     598             :         /* --------------------- */
     599           0 :         for (i = 0; i < NSWATH; i++)
     600             :         {
     601             :       /* Find empty entry in array */
     602             :       /* ------------------------- */
     603           0 :       if (SWXSwath[i].active == 0)
     604             :       {
     605             :           /*
     606             :            * Set swathID, Set swath entry active, Store
     607             :            * root Vgroup ID, Store sub Vgroup IDs, Store
     608             :            * HDF-EOS file ID
     609             :            */
     610           0 :           swathID = i + idOffset;
     611           0 :           SWXSwath[i].active = 1;
     612           0 :           SWXSwath[i].IDTable = vgid[0];
     613           0 :           SWXSwath[i].VIDTable[0] = vgid[1];
     614           0 :           SWXSwath[i].VIDTable[1] = vgid[2];
     615           0 :           SWXSwath[i].VIDTable[2] = vgid[3];
     616           0 :           SWXSwath[i].fid = fid;
     617           0 :           break;
     618             :       }
     619             :         }
     620             : 
     621             :         /* Get SDS interface ID */
     622             :         /* -------------------- */
     623           0 :         status = SWchkswid(swathID, "SWattach", &dum,
     624             :                &sdInterfaceID, &dum);
     625             : 
     626             : 
     627             :         /* Access swath "Geolocation" SDS */
     628             :         /* ------------------------------ */
     629             : 
     630             :         /* Get # of entries within this Vgroup & search for SDS */
     631             :         /* ---------------------------------------------------- */
     632           0 :         nObjects = Vntagrefs(vgid[1]);
     633             : 
     634           0 :         if (nObjects > 0)
     635             :         {
     636             :       /* Get tag and ref # for Geolocation Vgroup objects */
     637             :       /* ------------------------------------------------ */
     638           0 :       tags = (int32 *) malloc(sizeof(int32) * nObjects);
     639           0 :       if(tags == NULL)
     640             :       { 
     641           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     642           0 :           return(-1);
     643             :       }
     644           0 :       refs = (int32 *) malloc(sizeof(int32) * nObjects);
     645           0 :       if(refs == NULL)
     646             :       { 
     647           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     648           0 :           free(tags);
     649           0 :           return(-1);
     650             :       }
     651           0 :       Vgettagrefs(vgid[1], tags, refs, nObjects);
     652             : 
     653             :       /* Count number of SDS & allocate SDS ID array */
     654             :       /* ------------------------------------------- */
     655           0 :       nSDS = 0;
     656           0 :       for (j = 0; j < nObjects; j++)
     657             :       {
     658           0 :           if (tags[j] == DFTAG_NDG)
     659             :           {
     660           0 :         nSDS++;
     661             :           }
     662             :       }
     663           0 :       SWXSwath[i].sdsID = (int32 *) calloc(nSDS, 4);
     664           0 :       if(SWXSwath[i].sdsID == NULL && nSDS != 0)
     665             :       { 
     666           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     667           0 :           free(tags);
     668           0 :           free(refs);
     669           0 :           return(-1);
     670             :       }
     671           0 :       nSDS = 0;
     672             : 
     673             : 
     674             :       /* Fill SDS ID array */
     675             :       /* ----------------- */
     676           0 :       for (j = 0; j < nObjects; j++)
     677             :       {
     678             :           /* If object is SDS then get id */
     679             :           /* ---------------------------- */
     680           0 :           if (tags[j] == DFTAG_NDG)
     681             :           {
     682           0 :         l_index = SDreftoindex(sdInterfaceID, refs[j]);
     683           0 :         sdid = SDselect(sdInterfaceID, l_index);
     684           0 :         SWXSwath[i].sdsID[nSDS] = sdid;
     685           0 :         nSDS++;
     686           0 :         SWXSwath[i].nSDS++;
     687             :           }
     688             :       }
     689           0 :       free(tags);
     690           0 :       free(refs);
     691             :         }
     692             : 
     693             :         /* Access swath "Data" SDS */
     694             :         /* ----------------------- */
     695             : 
     696             :         /* Get # of entries within this Vgroup & search for SDS */
     697             :         /* ---------------------------------------------------- */
     698           0 :         nObjects = Vntagrefs(vgid[2]);
     699             : 
     700           0 :         if (nObjects > 0)
     701             :         {
     702             :       /* Get tag and ref # for Data Vgroup objects */
     703             :       /* ----------------------------------------- */
     704           0 :       tags = (int32 *) malloc(sizeof(int32) * nObjects);
     705           0 :       if(tags == NULL)
     706             :       { 
     707           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     708           0 :           return(-1);
     709             :       }
     710           0 :       refs = (int32 *) malloc(sizeof(int32) * nObjects);
     711           0 :       if(refs == NULL)
     712             :       { 
     713           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     714           0 :           free(tags);
     715           0 :           return(-1);
     716             :       }
     717           0 :       Vgettagrefs(vgid[2], tags, refs, nObjects);
     718             : 
     719             : 
     720             :       /* Count number of SDS & allocate SDS ID array */
     721             :       /* ------------------------------------------- */
     722           0 :       nSDS = 0;
     723           0 :       for (j = 0; j < nObjects; j++)
     724             :       {
     725           0 :           if (tags[j] == DFTAG_NDG)
     726             :           {
     727           0 :         nSDS++;
     728             :           }
     729             :       }
     730           0 :       SWXSwath[i].sdsID = (int32 *)
     731           0 :           realloc((void *) SWXSwath[i].sdsID,
     732           0 :             (SWXSwath[i].nSDS + nSDS) * 4);
     733           0 :       if(SWXSwath[i].sdsID == NULL && nSDS != 0)
     734             :       { 
     735           0 :           HEpush(DFE_NOSPACE,"SWattach", __FILE__, __LINE__);
     736           0 :           return(-1);
     737             :       }
     738             : 
     739             :       /* Fill SDS ID array */
     740             :       /* ----------------- */
     741           0 :       for (j = 0; j < nObjects; j++)
     742             :       {
     743             :           /* If object is SDS then get id */
     744             :           /* ---------------------------- */
     745           0 :           if (tags[j] == DFTAG_NDG)
     746             :           {
     747           0 :         l_index = SDreftoindex(sdInterfaceID, refs[j]);
     748           0 :         sdid = SDselect(sdInterfaceID, l_index);
     749           0 :         SWXSwath[i].sdsID[SWXSwath[i].nSDS] = sdid;
     750           0 :         SWXSwath[i].nSDS++;
     751             :           }
     752             :       }
     753           0 :       free(tags);
     754           0 :       free(refs);
     755             :         }
     756           0 :         break;
     757             :     }
     758             : 
     759             :     /* Detach Vgroup if not desired Swath */
     760             :     /* ---------------------------------- */
     761           0 :     Vdetach(vgid[0]);
     762             :       }
     763             : 
     764             :       /* If Swath not found then set up error message */
     765             :       /* -------------------------------------------- */
     766           0 :       if (swathID == -1)
     767             :       {
     768           0 :     HEpush(DFE_RANGE, "SWattach", __FILE__, __LINE__);
     769           0 :     HEreport("Swath: \"%s\" does not exist within HDF file.\n",
     770             :        swathname);
     771             :       }
     772             :   }
     773             :   else
     774             :   {
     775             :       /* Too many files opened */
     776             :       /* --------------------- */
     777           0 :       swathID = -1;
     778           0 :       strcpy(errbuf,
     779             :        "No more than %d swaths may be open simultaneously");
     780           0 :       strcat(errbuf, " (%s)");
     781           0 :       HEpush(DFE_DENIED, "SWattach", __FILE__, __LINE__);
     782           0 :       HEreport(errbuf, NSWATH, swathname);
     783             :   }
     784             : 
     785             :     }
     786           0 :     return (swathID);
     787             : }
     788             : 
     789             : /*----------------------------------------------------------------------------|
     790             : |  BEGIN_PROLOG                                                               |
     791             : |                                                                             |
     792             : |  FUNCTION: SWchkswid                                                        |
     793             : |                                                                             |
     794             : |  DESCRIPTION: Checks for valid swathID and returns file ID, SDS ID, and     |
     795             : |               swath Vgroup ID                                               |
     796             : |                                                                             |
     797             : |                                                                             |
     798             : |  Return Value    Type     Units     Description                             |
     799             : |  ============   ======  =========   =====================================   |
     800             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     801             : |                                                                             |
     802             : |  INPUTS:                                                                    |
     803             : |  swathID        int32               swath structure ID                      |
     804             : |  routname       char                Name of routine calling SWchkswid       |
     805             : |                                                                             |
     806             : |  OUTPUTS:                                                                   |
     807             : |  fid            int32               File ID                                 |
     808             : |  sdInterfaceID  int32               SDS interface ID                        |
     809             : |  swVgrpID       int32               swath Vgroup ID                         |
     810             : |                                                                             |
     811             : |  NOTES:                                                                     |
     812             : |                                                                             |
     813             : |                                                                             |
     814             : |   Date     Programmer   Description                                         |
     815             : |  ======   ============  =================================================   |
     816             : |  Jun 96   Joel Gales    Original Programmer                                 |
     817             : |                                                                             |
     818             : |  END_PROLOG                                                                 |
     819             : -----------------------------------------------------------------------------*/
     820             : static intn
     821           0 : SWchkswid(int32 swathID, const char *routname,
     822             :     int32 * fid, int32 * sdInterfaceID, int32 * swVgrpID)
     823             : 
     824             : {
     825           0 :     intn            status = 0; /* routine return status variable */
     826             :     uint8           l_access; /* Read/Write access code */
     827             : 
     828           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     829             : 
     830           0 :     char            message1[] =
     831             :     "Invalid swath id: %d in routine \"%s\".  ID must be >= %d and < %d.\n";
     832           0 :     char            message2[] =
     833             :     "Swath id %d in routine \"%s\" not active.\n";
     834             : 
     835             : 
     836             :     /* Check for valid swath id */
     837             :     /* ------------------------ */
     838           0 :     if (swathID < idOffset || swathID >= NSWATH + idOffset)
     839             :     {
     840           0 :   status = -1;
     841           0 :   HEpush(DFE_RANGE, "SWchkswid", __FILE__, __LINE__);
     842           0 :   HEreport(message1, swathID, routname, idOffset, NSWATH + idOffset);
     843             :     }
     844             :     else
     845             :     {
     846             :   /* Check for active swath ID */
     847             :   /* ------------------------- */
     848           0 :   if (SWXSwath[swathID % idOffset].active == 0)
     849             :   {
     850           0 :       status = -1;
     851           0 :       HEpush(DFE_GENAPP, "SWchkswid", __FILE__, __LINE__);
     852           0 :       HEreport(message2, swathID, routname);
     853             :   }
     854             :   else
     855             :   {
     856             : 
     857             :       /* Get file & SDS ids and Swath Vgroup */
     858             :       /* ----------------------------------- */
     859           0 :       status = EHchkfid(SWXSwath[swathID % idOffset].fid, " ", fid,
     860             :             sdInterfaceID, &l_access);
     861           0 :       *swVgrpID = SWXSwath[swathID % idOffset].IDTable;
     862             :   }
     863             :     }
     864           0 :     return (status);
     865             : }
     866             : 
     867             : 
     868             : 
     869             : 
     870             : 
     871             : 
     872             : 
     873             : /*----------------------------------------------------------------------------|
     874             : |  BEGIN_PROLOG                                                               |
     875             : |                                                                             |
     876             : |  FUNCTION: SWdefdim                                                         |
     877             : |                                                                             |
     878             : |  DESCRIPTION: Defines numerical value of dimension                          |
     879             : |                                                                             |
     880             : |                                                                             |
     881             : |  Return Value    Type     Units     Description                             |
     882             : |  ============   ======  =========   =====================================   |
     883             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     884             : |                                                                             |
     885             : |  INPUTS:                                                                    |
     886             : |  swathID        int32               swath structure ID                      |
     887             : |  dimname        char                Dimension name to define                |
     888             : |  dim            int32               Dimension value                         |
     889             : |                                                                             |
     890             : |  OUTPUTS:                                                                   |
     891             : |             None                                                            |
     892             : |                                                                             |
     893             : |  NOTES:                                                                     |
     894             : |                                                                             |
     895             : |                                                                             |
     896             : |   Date     Programmer   Description                                         |
     897             : |  ======   ============  =================================================   |
     898             : |  Jun 96   Joel Gales    Original Programmer                                 |
     899             : |  Dec 96   Joel Gales    Check that dim value >= 0                           |
     900             : |                                                                             |
     901             : |  END_PROLOG                                                                 |
     902             : -----------------------------------------------------------------------------*/
     903             : intn
     904           0 : SWdefdim(int32 swathID, char *dimname, int32 dim)
     905             : 
     906             : {
     907             :     intn            status; /* routine return status variable */
     908             : 
     909             :     int32           fid;  /* HDF-EOS file id */
     910             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     911             :     int32           swVgrpID; /* Swath root Vgroup ID */
     912           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     913             : 
     914             :     char            swathname[80] /* Swath name */ ;
     915             : 
     916             : 
     917             :     /* Check for valid swath id */
     918             :     /* ------------------------ */
     919           0 :     status = SWchkswid(swathID, "SWdefdim", &fid, &sdInterfaceID, &swVgrpID);
     920             : 
     921             : 
     922             :     /* Make sure dimension >= 0 */
     923             :     /* ------------------------ */
     924           0 :     if (dim < 0)
     925             :     {
     926           0 :   status = -1;
     927           0 :   HEpush(DFE_GENAPP, "SWdefdim", __FILE__, __LINE__);
     928           0 :   HEreport("Dimension value for \"%s\" less than zero: %d.\n",
     929             :      dimname, dim);
     930             :     }
     931             : 
     932             : 
     933             :     /* Write Dimension to Structural MetaData */
     934             :     /* -------------------------------------- */
     935           0 :     if (status == 0)
     936             :     {
     937           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
     938           0 :   status = EHinsertmeta(sdInterfaceID, swathname, "s", 0L,
     939             :             dimname, &dim);
     940             :     }
     941           0 :     return (status);
     942             : }
     943             : 
     944             : 
     945             : 
     946             : /*----------------------------------------------------------------------------|
     947             : |  BEGIN_PROLOG                                                               |
     948             : |                                                                             |
     949             : |  FUNCTION: SWdiminfo                                                        |
     950             : |                                                                             |
     951             : |  DESCRIPTION: Returns size in bytes of named dimension                      |
     952             : |                                                                             |
     953             : |                                                                             |
     954             : |  Return Value    Type     Units     Description                             |
     955             : |  ============   ======  =========   =====================================   |
     956             : |  size           int32               Size of dimension                       |
     957             : |                                                                             |
     958             : |  INPUTS:                                                                    |
     959             : |  swathID        int32               swath structure id                      |
     960             : |  dimname        char                Dimension name                          |
     961             : |                                                                             |
     962             : |                                                                             |
     963             : |  OUTPUTS:                                                                   |
     964             : |             None                                                            |
     965             : |                                                                             |
     966             : |  NOTES:                                                                     |
     967             : |                                                                             |
     968             : |                                                                             |
     969             : |   Date     Programmer   Description                                         |
     970             : |  ======   ============  =================================================   |
     971             : |  Jun 96   Joel Gales    Original Programmer                                 |
     972             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
     973             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
     974             : |                                                                             |
     975             : |  END_PROLOG                                                                 |
     976             : -----------------------------------------------------------------------------*/
     977             : int32
     978           0 : SWdiminfo(int32 swathID, const char *dimname)
     979             : 
     980             : {
     981             :     intn            status; /* routine return status variable */
     982             : 
     983             :     int32           fid;  /* HDF-EOS file ID */
     984             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     985             :     int32           swVgrpID; /* Swath root Vgroup ID */
     986             :     int32           size; /* Dimension size */
     987           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
     988             : 
     989             : 
     990             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
     991             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
     992             :     char            swathname[80];  /* Swath Name */
     993             :     char           *utlstr; /* Utility string */
     994             : 
     995             : 
     996             :     /* Allocate space for utility string */
     997             :     /* --------------------------------- */
     998           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
     999           0 :     if(utlstr == NULL)
    1000             :     { 
    1001           0 :   HEpush(DFE_NOSPACE,"SWdiminfo", __FILE__, __LINE__);
    1002           0 :   return(-1);
    1003             :     }
    1004             :     /* Initialize return value */
    1005           0 :     size = -1;
    1006             : 
    1007             :     /* Check Swath ID */
    1008           0 :     status = SWchkswid(swathID, "SWdiminfo", &fid, &sdInterfaceID, &swVgrpID);
    1009             : 
    1010           0 :     if (status == 0)
    1011             :     {
    1012             :   /* Get swath name */
    1013           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    1014             : 
    1015             :   /* Get pointers to "Dimension" section within SM */
    1016           0 :   metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    1017             :                "Dimension", metaptrs);
    1018             : 
    1019           0 :   if(metabuf == NULL)
    1020             :   {
    1021           0 :       free(utlstr);
    1022           0 :       return(-1);
    1023             :   }  
    1024             : 
    1025             :   /* Search for dimension name (surrounded by quotes) */
    1026           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", dimname, "\"\n");
    1027           0 :   metaptrs[0] = strstr(metaptrs[0], utlstr);
    1028             : 
    1029             :   /*
    1030             :    * If dimension found within swath structure then get dimension value
    1031             :    */
    1032           0 :   if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    1033             :   {
    1034             :       /* Set endptr at end of dimension definition entry */
    1035           0 :       metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
    1036             : 
    1037           0 :       status = EHgetmetavalue(metaptrs, "Size", utlstr);
    1038             : 
    1039           0 :       if (status == 0)
    1040             :       {
    1041           0 :     size = atoi(utlstr);
    1042             :       }
    1043             :       else
    1044             :       {
    1045           0 :     HEpush(DFE_GENAPP, "SWdiminfo", __FILE__, __LINE__);
    1046           0 :     HEreport("\"Size\" string not found in metadata.\n");
    1047             :       }
    1048             :   }
    1049             :   else
    1050             :   {
    1051           0 :       HEpush(DFE_GENAPP, "SWdiminfo", __FILE__, __LINE__);
    1052           0 :       HEreport("Dimension \"%s\" not found.\n", dimname);
    1053             :   }
    1054             : 
    1055           0 :   free(metabuf);
    1056             :     }
    1057           0 :     free(utlstr);
    1058             : 
    1059           0 :     return (size);
    1060             : }
    1061             : 
    1062             : 
    1063             : 
    1064             : 
    1065             : /*----------------------------------------------------------------------------|
    1066             : |  BEGIN_PROLOG                                                               |
    1067             : |                                                                             |
    1068             : |  FUNCTION: SWmapinfo                                                        |
    1069             : |                                                                             |
    1070             : |  DESCRIPTION: Returns dimension mapping information                         |
    1071             : |                                                                             |
    1072             : |                                                                             |
    1073             : |  Return Value    Type     Units     Description                             |
    1074             : |  ============   ======  =========   =====================================   |
    1075             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1076             : |                                                                             |
    1077             : |  INPUTS:                                                                    |
    1078             : |  swathID        int32               swath structure id                      |
    1079             : |  geodim         char                geolocation dimension name              |
    1080             : |  datadim        char                data dimension name                     |
    1081             : |                                                                             |
    1082             : |  OUTPUTS:                                                                   |
    1083             : |  offset         int32               mapping offset                          |
    1084             : |  increment      int32               mapping increment                       |
    1085             : |                                                                             |
    1086             : |  NOTES:                                                                     |
    1087             : |                                                                             |
    1088             : |                                                                             |
    1089             : |   Date     Programmer   Description                                         |
    1090             : |  ======   ============  =================================================   |
    1091             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1092             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    1093             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
    1094             : |                                                                             |
    1095             : |  END_PROLOG                                                                 |
    1096             : -----------------------------------------------------------------------------*/
    1097             : intn
    1098           0 : SWmapinfo(int32 swathID, const char *geodim, const char *datadim, int32 * offset,
    1099             :     int32 * increment)
    1100             : 
    1101             : {
    1102             :     intn            status; /* routine return status variable */
    1103           0 :     intn            statmeta = 0; /* EHgetmetavalue return status */
    1104             : 
    1105             :     int32           fid;  /* HDF-EOS file ID */
    1106             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1107             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1108           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1109             : 
    1110             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    1111             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    1112             :     char            swathname[80];  /* Swath Name */
    1113             :     char           *utlstr; /* Utility string */
    1114             : 
    1115             : 
    1116             :     /* Allocate space for utility string */
    1117             :     /* --------------------------------- */
    1118           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    1119           0 :     if(utlstr == NULL)
    1120             :     { 
    1121           0 :   HEpush(DFE_NOSPACE,"SWmapinfo", __FILE__, __LINE__);
    1122           0 :   return(-1);
    1123             :     }
    1124             :     /* Initialize return values */
    1125           0 :     *offset = -1;
    1126           0 :     *increment = -1;
    1127             : 
    1128             :     /* Check Swath ID */
    1129           0 :     status = SWchkswid(swathID, "SWmapinfo", &fid, &sdInterfaceID, &swVgrpID);
    1130             : 
    1131           0 :     if (status == 0)
    1132             :     {
    1133             :   /* Get swath name */
    1134           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    1135             : 
    1136             :   /* Get pointers to "DimensionMap" section within SM */
    1137           0 :   metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    1138             :                "DimensionMap", metaptrs);
    1139           0 :   if(metabuf == NULL)
    1140             :   {
    1141           0 :       free(utlstr);
    1142           0 :       return(-1);
    1143             :   }
    1144             : 
    1145             :   /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
    1146           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
    1147             :     "\"\n\t\t\t\tDataDimension=\"", datadim, "\"\n");
    1148           0 :   metaptrs[0] = strstr(metaptrs[0], utlstr);
    1149             : 
    1150             :   /*
    1151             :    * If mapping found within swath structure then get offset and
    1152             :    * increment value
    1153             :    */
    1154           0 :   if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    1155             :   {
    1156             :       /* Get Offset */
    1157           0 :       statmeta = EHgetmetavalue(metaptrs, "Offset", utlstr);
    1158           0 :       if (statmeta == 0)
    1159             :       {
    1160           0 :     *offset = atoi(utlstr);
    1161             :       }
    1162             :       else
    1163             :       {
    1164           0 :     status = -1;
    1165           0 :     HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
    1166           0 :     HEreport("\"Offset\" string not found in metadata.\n");
    1167             :       }
    1168             : 
    1169             : 
    1170             :       /* Get Increment */
    1171           0 :       statmeta = EHgetmetavalue(metaptrs, "Increment", utlstr);
    1172           0 :       if (statmeta == 0)
    1173             :       {
    1174           0 :     *increment = atoi(utlstr);
    1175             :       }
    1176             :       else
    1177             :       {
    1178           0 :     status = -1;
    1179           0 :     HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
    1180           0 :     HEreport("\"Increment\" string not found in metadata.\n");
    1181             :       }
    1182             :   }
    1183             :   else
    1184             :   {
    1185           0 :       status = -1;
    1186           0 :       HEpush(DFE_GENAPP, "SWmapinfo", __FILE__, __LINE__);
    1187           0 :       HEreport("Mapping \"%s/%s\" not found.\n", geodim, datadim);
    1188             :   }
    1189             : 
    1190           0 :   free(metabuf);
    1191             :     }
    1192           0 :     free(utlstr);
    1193           0 :     return (status);
    1194             : }
    1195             : 
    1196             : 
    1197             : 
    1198             : 
    1199             : /*----------------------------------------------------------------------------|
    1200             : |  BEGIN_PROLOG                                                               |
    1201             : |                                                                             |
    1202             : |  FUNCTION: SWidxmapinfo                                                     |
    1203             : |                                                                             |
    1204             : |  DESCRIPTION: Returns l_indexed mapping information                           |
    1205             : |                                                                             |
    1206             : |                                                                             |
    1207             : |  Return Value    Type     Units     Description                             |
    1208             : |  ============   ======  =========   =====================================   |
    1209             : |  gsize          int32               Number of l_index values (sz of geo dim)  |
    1210             : |                                                                             |
    1211             : |  INPUTS:                                                                    |
    1212             : |  swathID        int32               swath structure id                      |
    1213             : |  geodim         char                geolocation dimension name              |
    1214             : |  datadim        char                data dimension name                     |
    1215             : |                                                                             |
    1216             : |                                                                             |
    1217             : |  OUTPUTS:                                                                   |
    1218             : |  l_index          int32               array of l_index values                   |
    1219             : |                                                                             |
    1220             : |  NOTES:                                                                     |
    1221             : |                                                                             |
    1222             : |                                                                             |
    1223             : |   Date     Programmer   Description                                         |
    1224             : |  ======   ============  =================================================   |
    1225             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1226             : |                                                                             |
    1227             : |  END_PROLOG                                                                 |
    1228             : -----------------------------------------------------------------------------*/
    1229             : int32
    1230           0 : SWidxmapinfo(int32 swathID, const char *geodim, const char *datadim, int32 l_index[])
    1231             : {
    1232             :     intn            status; /* routine return status variable */
    1233             : 
    1234             :     int32           fid;  /* HDF-EOS file ID */
    1235             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1236             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1237           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1238             :     int32           vgid; /* Swath Attributes Vgroup ID */
    1239             :     int32           vdataID;  /* Index Mapping Vdata ID */
    1240           0 :     int32           gsize = -1; /* Size of geo dim */
    1241             : 
    1242             :     char            utlbuf[256];/* Utility buffer */
    1243             : 
    1244             : 
    1245             :     /* Check Swath ID */
    1246           0 :     status = SWchkswid(swathID, "SWidxmapinfo",
    1247             :            &fid, &sdInterfaceID, &swVgrpID);
    1248             : 
    1249           0 :     if (status == 0)
    1250             :     {
    1251             :   /* Find Index Mapping Vdata with Swath Attributes Vgroup */
    1252           0 :   snprintf(utlbuf, sizeof(utlbuf), "%s%s%s%s", "INDXMAP:", geodim, "/", datadim);
    1253           0 :   vgid = SWXSwath[swathID % idOffset].VIDTable[2];
    1254           0 :   vdataID = EHgetid(fid, vgid, utlbuf, 1, "r");
    1255             : 
    1256             :   /* If found then get geodim size & read l_index mapping values */
    1257           0 :   if (vdataID != -1)
    1258             :   {
    1259           0 :       gsize = SWdiminfo(swathID, geodim);
    1260             : 
    1261           0 :       VSsetfields(vdataID, "Index");
    1262           0 :       VSread(vdataID, (uint8 *) l_index, 1, FULL_INTERLACE);
    1263           0 :       VSdetach(vdataID);
    1264             :   }
    1265             :   else
    1266             :   {
    1267             :       /*status = -1;*/
    1268           0 :       HEpush(DFE_GENAPP, "SWidxmapinfo", __FILE__, __LINE__);
    1269           0 :       HEreport("Index Mapping \"%s\" not found.\n", utlbuf);
    1270             :   }
    1271             :     }
    1272           0 :     return (gsize);
    1273             : }
    1274             : 
    1275             : 
    1276             : 
    1277             : 
    1278             : /*----------------------------------------------------------------------------|
    1279             : |  BEGIN_PROLOG                                                               |
    1280             : |                                                                             |
    1281             : |  FUNCTION: SWcompinfo                                                       |
    1282             : |                                                                             |
    1283             : |  DESCRIPTION:                                                               |
    1284             : |                                                                             |
    1285             : |                                                                             |
    1286             : |  Return Value    Type     Units     Description                             |
    1287             : |  ============   ======  =========   =====================================   |
    1288             : |  status         intn                                                        |
    1289             : |                                                                             |
    1290             : |  INPUTS:                                                                    |
    1291             : |  swathID        int32                                                       |
    1292             : |  compcode       int32                                                       |
    1293             : |  compparm       intn                                                        |
    1294             : |                                                                             |
    1295             : |                                                                             |
    1296             : |  OUTPUTS:                                                                   |
    1297             : |             None                                                            |
    1298             : |                                                                             |
    1299             : |  NOTES:                                                                     |
    1300             : |                                                                             |
    1301             : |                                                                             |
    1302             : |   Date     Programmer   Description                                         |
    1303             : |  ======   ============  =================================================   |
    1304             : |  Oct 96   Joel Gales    Original Programmer                                 |
    1305             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
    1306             : |                                                                             |
    1307             : |  END_PROLOG                                                                 |
    1308             : -----------------------------------------------------------------------------*/
    1309             : intn
    1310           0 : SWcompinfo(int32 swathID, const char *fieldname, int32 * compcode, intn compparm[])
    1311             : {
    1312             :     intn            i;    /* Loop Index */
    1313             :     intn            status; /* routine return status variable */
    1314           0 :     intn            statmeta = 0; /* EHgetmetavalue return status */
    1315             : 
    1316             :     int32           fid;  /* HDF-EOS file ID */
    1317             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1318             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1319           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1320             : 
    1321             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    1322             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    1323             :     char            swathname[80];  /* Swath Name */
    1324             :     char           *utlstr;     /* Utility string */
    1325             : 
    1326           0 :     const char           *HDFcomp[5] = {"HDFE_COMP_NONE", "HDFE_COMP_RLE",
    1327             :   "HDFE_COMP_NBIT", "HDFE_COMP_SKPHUFF",
    1328             :     "HDFE_COMP_DEFLATE"}; /* Compression Codes */
    1329             : 
    1330             :     /* Allocate space for utility string */
    1331             :     /* --------------------------------- */
    1332           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    1333           0 :     if(utlstr == NULL)
    1334             :     { 
    1335           0 :   HEpush(DFE_NOSPACE,"SWcompinfo", __FILE__, __LINE__);
    1336           0 :   return(-1);
    1337             :     }
    1338             : 
    1339             :     /* Check Swath ID */
    1340           0 :     status = SWchkswid(swathID, "SWcompinfo",
    1341             :            &fid, &sdInterfaceID, &swVgrpID);
    1342             : 
    1343           0 :     if (status == 0)
    1344             :     {
    1345             :   /* Get swath name */
    1346           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    1347             : 
    1348             :   /* Get pointers to "DataField" section within SM */
    1349           0 :   metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    1350             :                "DataField", metaptrs);
    1351           0 :   if(metabuf == NULL)
    1352             :   {
    1353           0 :       free(utlstr);
    1354           0 :       return(-1);
    1355             :   }
    1356             :   /* Search for field */
    1357           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n");
    1358           0 :   metaptrs[0] = strstr(metaptrs[0], utlstr);
    1359             : 
    1360             :   /* If not found then search in "GeoField" section */
    1361           0 :   if (metaptrs[0] > metaptrs[1] || metaptrs[0] == NULL)
    1362             :   {
    1363           0 :       free(metabuf);
    1364             : 
    1365             :       /* Get pointers to "GeoField" section within SM */
    1366           0 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    1367             :              "GeoField", metaptrs);
    1368           0 :       if(metabuf == NULL)
    1369             :       {
    1370           0 :     free(utlstr);
    1371           0 :     return(-1);
    1372             :       }
    1373             :       /* Search for field */
    1374           0 :       snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n");
    1375           0 :       metaptrs[0] = strstr(metaptrs[0], utlstr);
    1376             :   }
    1377             : 
    1378             : 
    1379             :   /* If field found and user wants compression code ... */
    1380           0 :   if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    1381             :   {
    1382           0 :       if (compcode != NULL)
    1383             :       {
    1384             :     /* Set endptr at end of field's definition entry */
    1385           0 :     metaptrs[1] = strstr(metaptrs[0], "\t\t\tEND_OBJECT");
    1386             : 
    1387             :     /* Get compression type */
    1388           0 :     statmeta = EHgetmetavalue(metaptrs, "CompressionType", utlstr);
    1389             : 
    1390             :     /*
    1391             :      * Default is no compression if "CompressionType" string not
    1392             :      * in metadata
    1393             :      */
    1394           0 :     *compcode = HDFE_COMP_NONE;
    1395             : 
    1396             :     /* If compression code is found ... */
    1397           0 :     if (statmeta == 0)
    1398             :     {
    1399             :         /* Loop through compression types until match */
    1400           0 :         for (i = 0; i < 5; i++)
    1401             :         {
    1402           0 :       if (strcmp(utlstr, HDFcomp[i]) == 0)
    1403             :       {
    1404           0 :           *compcode = i;
    1405           0 :           break;
    1406             :       }
    1407             :         }
    1408             :     }
    1409             :       }
    1410             : 
    1411             :       /* If user wants compression parameters ... */
    1412           0 :       if (compparm != NULL && compcode != NULL)
    1413             :       {
    1414             :     /* Initialize to zero */
    1415           0 :     for (i = 0; i < 4; i++)
    1416             :     {
    1417           0 :         compparm[i] = 0;
    1418             :     }
    1419             : 
    1420             :     /*
    1421             :      * Get compression parameters if NBIT or DEFLATE compression
    1422             :      */
    1423           0 :     if (*compcode == HDFE_COMP_NBIT)
    1424             :     {
    1425             :         statmeta =
    1426           0 :       EHgetmetavalue(metaptrs, "CompressionParams", utlstr);
    1427           0 :         if (statmeta == 0)
    1428             :         {
    1429           0 :       sscanf(utlstr, "(%d,%d,%d,%d)",
    1430             :              &compparm[0], &compparm[1],
    1431             :              &compparm[2], &compparm[3]);
    1432             :         }
    1433             :         else
    1434             :         {
    1435           0 :       status = -1;
    1436           0 :       HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
    1437           0 :       HEreport(
    1438             :          "\"CompressionParams\" string not found in metadata.\n");
    1439             :         }
    1440             :     }
    1441           0 :     else if (*compcode == HDFE_COMP_DEFLATE)
    1442             :     {
    1443             :         statmeta =
    1444           0 :       EHgetmetavalue(metaptrs, "DeflateLevel", utlstr);
    1445           0 :         if (statmeta == 0)
    1446             :         {
    1447           0 :       sscanf(utlstr, "%d", &compparm[0]);
    1448             :         }
    1449             :         else
    1450             :         {
    1451           0 :       status = -1;
    1452           0 :       HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
    1453           0 :       HEreport(
    1454             :       "\"DeflateLevel\" string not found in metadata.\n");
    1455             :         }
    1456             :     }
    1457             :       }
    1458             :   }
    1459             :   else
    1460             :   {
    1461           0 :       HEpush(DFE_GENAPP, "SWcompinfo", __FILE__, __LINE__);
    1462           0 :       HEreport("Fieldname \"%s\" not found.\n", fieldname);
    1463             :   }
    1464             : 
    1465           0 :   free(metabuf);
    1466             :     }
    1467           0 :     free(utlstr);
    1468             :     
    1469           0 :     return (status);
    1470             : }
    1471             : 
    1472             : 
    1473             : 
    1474             : /*----------------------------------------------------------------------------|
    1475             : |  BEGIN_PROLOG                                                               |
    1476             : |                                                                             |
    1477             : |  FUNCTION: SWfinfo                                                          |
    1478             : |                                                                             |
    1479             : |  DESCRIPTION: Returns field info                                            |
    1480             : |                                                                             |
    1481             : |                                                                             |
    1482             : |  Return Value    Type     Units     Description                             |
    1483             : |  ============   ======  =========   =====================================   |
    1484             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1485             : |                                                                             |
    1486             : |  INPUTS:                                                                    |
    1487             : |  swathID        int32               swath structure id                      |
    1488             : |  fieldtype      const char          fieldtype (geo or data)                 |
    1489             : |  fieldname      const char          name of field                           |
    1490             : |                                                                             |
    1491             : |                                                                             |
    1492             : |  OUTPUTS:                                                                   |
    1493             : |  rank           int32               rank of field (# of dims)               |
    1494             : |  dims           int32               field dimensions                        |
    1495             : |  numbertype     int32               field number type                       |
    1496             : |  dimlist        char                field dimension list                    |
    1497             : |                                                                             |
    1498             : |  NOTES:                                                                     |
    1499             : |                                                                             |
    1500             : |                                                                             |
    1501             : |   Date     Programmer   Description                                         |
    1502             : |  ======   ============  =================================================   |
    1503             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1504             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    1505             : |  Jan 97   Joel Gales    Check for metadata error status from EHgetmetavalue |
    1506             : |                                                                             |
    1507             : |  END_PROLOG                                                                 |
    1508             : -----------------------------------------------------------------------------*/
    1509             : static int32
    1510           0 : SWfinfo(int32 swathID, const char *fieldtype, const char *fieldname,
    1511             :         int32 *rank, int32 dims[], int32 *numbertype, char *dimlist)
    1512             : 
    1513             : {
    1514             :     intn            i;    /* Loop index */
    1515             :     intn            j;    /* Loop index */
    1516             :     intn            status; /* routine return status variable */
    1517           0 :     intn            statmeta = 0; /* EHgetmetavalue return status */
    1518             : 
    1519             :     int32           fid;  /* HDF-EOS file ID */
    1520             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1521           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1522             :     int32           fsize;  /* field size in bytes */
    1523           0 :     int32           ndims = 0;  /* Number of dimensions */
    1524             :     int32           slen[8];  /* Length of each entry in parsed string */
    1525             :     int32           dum;  /* Dummy variable */
    1526             :     int32           vdataID;  /* 1d field vdata ID */
    1527             : 
    1528             :     uint8          *buf;  /* One-Dim field buffer */
    1529             : 
    1530             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    1531             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    1532             :     char            swathname[80];  /* Swath Name */
    1533             :     char           *utlstr; /* Utility string */
    1534             :     char           *ptr[8]; /* String pointers for parsed string */
    1535             :     char            dimstr[64]; /* Individual dimension entry string */
    1536             : 
    1537             : 
    1538             :     /* Allocate space for utility string */
    1539             :     /* --------------------------------- */
    1540           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    1541           0 :     if(utlstr == NULL)
    1542             :     { 
    1543           0 :   HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__);
    1544           0 :   return(-1);
    1545             :     }
    1546             : 
    1547             :     /* Initialize rank and numbertype to -1 (error) */
    1548             :     /* -------------------------------------------- */
    1549           0 :     *rank = -1;
    1550           0 :     *numbertype = -1;
    1551             : 
    1552             :     /* Get HDF-EOS file ID and SDS interface ID */
    1553           0 :     status = SWchkswid(swathID, "SWfinfo", &fid, &sdInterfaceID, &dum);
    1554             : 
    1555             :     /* Get swath name */
    1556           0 :     Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    1557             : 
    1558             :     /* Get pointers to appropriate "Field" section within SM */
    1559           0 :     if (strcmp(fieldtype, "Geolocation Fields") == 0)
    1560             :     {
    1561           0 :   metabuf = EHmetagroup(sdInterfaceID, swathname, "s",
    1562             :                "GeoField", metaptrs);
    1563             :   
    1564           0 :   if(metabuf == NULL)
    1565             :   {
    1566           0 :       free(utlstr);
    1567           0 :       return(-1);
    1568             :   }
    1569             :     }
    1570             :     else
    1571             :     {
    1572           0 :   metabuf = EHmetagroup(sdInterfaceID, swathname, "s",
    1573             :                "DataField", metaptrs);
    1574           0 :   if(metabuf == NULL)
    1575             :   {
    1576           0 :       free(utlstr);
    1577           0 :       return(-1);
    1578             :   }
    1579             :     }
    1580             : 
    1581             : 
    1582             :     /* Search for field */
    1583           0 :     snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"\n");
    1584           0 :     metaptrs[0] = strstr(metaptrs[0], utlstr);
    1585             : 
    1586             :     /* If field found ... */
    1587           0 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    1588             :     {
    1589             :   /* Get DataType string */
    1590           0 :   statmeta = EHgetmetavalue(metaptrs, "DataType", utlstr);
    1591             : 
    1592             :   /* Convert to numbertype code */
    1593           0 :   if (statmeta == 0)
    1594           0 :       *numbertype = EHnumstr(utlstr);
    1595             :   else
    1596             :   {
    1597           0 :       status = -1;
    1598           0 :       HEpush(DFE_GENAPP, "SWfinfo", __FILE__, __LINE__);
    1599           0 :       HEreport("\"DataType\" string not found in metadata.\n");
    1600             :   }
    1601             : 
    1602             : 
    1603             :   /*
    1604             :    * Get DimList string and trim off leading and trailing parens "()"
    1605             :    */
    1606           0 :   statmeta = EHgetmetavalue(metaptrs, "DimList", utlstr);
    1607             : 
    1608           0 :   if (statmeta == 0)
    1609             :   {
    1610           0 :       memmove(utlstr, utlstr + 1, strlen(utlstr) - 2);
    1611           0 :       utlstr[strlen(utlstr) - 2] = 0;
    1612             : 
    1613             :       /* Parse trimmed DimList string and get rank */
    1614           0 :       ndims = EHparsestr(utlstr, ',', ptr, slen);
    1615           0 :       *rank = ndims;
    1616             :   }
    1617             :   else
    1618             :   {
    1619           0 :       status = -1;
    1620           0 :       HEpush(DFE_GENAPP, "SWfinfo", __FILE__, __LINE__);
    1621           0 :       HEreport("\"DimList\" string not found in metadata.\n");
    1622             :   }
    1623             : 
    1624             :   /* If dimension list is desired by user then initialize length to 0 */
    1625           0 :   if (dimlist != NULL)
    1626             :   {
    1627           0 :       dimlist[0] = 0;
    1628             :   }
    1629             : 
    1630             :   /*
    1631             :    * Copy each entry in DimList and remove leading and trailing quotes,
    1632             :    * Get dimension sizes and concatenate dimension names to dimension
    1633             :    * list
    1634             :    */
    1635           0 :   for (i = 0; i < ndims; i++)
    1636             :   {
    1637           0 :       memcpy(dimstr, ptr[i] + 1, slen[i] - 2);
    1638           0 :       dimstr[slen[i] - 2] = 0;
    1639           0 :       dims[i] = SWdiminfo(swathID, dimstr);
    1640           0 :       if (dimlist != NULL)
    1641             :       {
    1642           0 :     if (i > 0)
    1643             :     {
    1644           0 :         strcat(dimlist, ",");
    1645             :     }
    1646           0 :     strcat(dimlist, dimstr);
    1647             :       }
    1648             : 
    1649             :   }
    1650             : 
    1651             : 
    1652             :   /* Appendable Field Section */
    1653             :   /* ------------------------ */
    1654           0 :   if (dims[0] == 0)
    1655             :   {
    1656             :       /* One-Dimensional Field */
    1657           0 :       if (*rank == 1)
    1658             :       {
    1659             :     /* Get vdata ID */
    1660           0 :     status = SW1dfldsrch(fid, swathID, fieldname, "r",
    1661             :              &dum, &vdataID, &dum);
    1662             : 
    1663             :     /* Get actual size of field */
    1664           0 :     dims[0] = VSelts(vdataID);
    1665             : 
    1666             :     /*
    1667             :      * If size=1 then check where actual record of
    1668             :      * "initialization" record
    1669             :      */
    1670           0 :     if (dims[0] == 1)
    1671             :     {
    1672             :         /* Get record size and read 1st record */
    1673           0 :         fsize = VSsizeof(vdataID, (char *)fieldname);
    1674           0 :         buf = (uint8 *) calloc(fsize, 1);
    1675           0 :         if(buf == NULL)
    1676             :         { 
    1677           0 :       HEpush(DFE_NOSPACE,"SWfinfo", __FILE__, __LINE__);
    1678           0 :       free(utlstr);
    1679           0 :       return(-1);
    1680             :         }
    1681           0 :         VSsetfields(vdataID, fieldname);
    1682           0 :         VSseek(vdataID, 0);
    1683           0 :         VSread(vdataID, (uint8 *) buf, 1, FULL_INTERLACE);
    1684             : 
    1685             :         /* Sum up "bytes" in record */
    1686           0 :         for (i = 0, j = 0; i < fsize; i++)
    1687             :         {
    1688           0 :       j += buf[i];
    1689             :         }
    1690             : 
    1691             :         /*
    1692             :          * If filled with 255 then "initialization" record,
    1693             :          * actual number of records = 0
    1694             :          */
    1695           0 :         if (j == 255 * fsize)
    1696             :         {
    1697           0 :       dims[0] = 0;
    1698             :         }
    1699             : 
    1700           0 :         free(buf);
    1701             :     }
    1702             :     /* Detach from 1d field */
    1703           0 :     VSdetach(vdataID);
    1704             :       }
    1705             :       else
    1706             :       {
    1707             :     /* Get actual size of Multi-Dimensional Field */
    1708           0 :     status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
    1709             :              &dum, &dum, &dum, &dum, dims,
    1710             :              &dum);
    1711             :       }
    1712             :   }
    1713             :     }
    1714           0 :     free(metabuf);
    1715             : 
    1716           0 :     if (*rank == -1)
    1717             :     {
    1718           0 :   status = -1;
    1719             :     }
    1720           0 :     free(utlstr);
    1721             :      
    1722           0 :     return (status);
    1723             : }
    1724             : 
    1725             : 
    1726             : 
    1727             : 
    1728             : 
    1729             : /*----------------------------------------------------------------------------|
    1730             : |  BEGIN_PROLOG                                                               |
    1731             : |                                                                             |
    1732             : |  FUNCTION: SWfieldinfo                                                      |
    1733             : |                                                                             |
    1734             : |  DESCRIPTION: Wrapper around SWfinfo                                        |
    1735             : |                                                                             |
    1736             : |                                                                             |
    1737             : |  Return Value    Type     Units     Description                             |
    1738             : |  ============   ======  =========   =====================================   |
    1739             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1740             : |                                                                             |
    1741             : |  INPUTS:                                                                    |
    1742             : |  swathID        int32               swath structure id                      |
    1743             : |  fieldname      const char          name of field                           |
    1744             : |                                                                             |
    1745             : |                                                                             |
    1746             : |  OUTPUTS:                                                                   |
    1747             : |  rank           int32               rank of field (# of dims)               |
    1748             : |  dims           int32               field dimensions                        |
    1749             : |  numbertype     int32               field number type                       |
    1750             : |  dimlist        char                field dimension list                    |
    1751             : |                                                                             |
    1752             : |  NOTES:                                                                     |
    1753             : |                                                                             |
    1754             : |                                                                             |
    1755             : |   Date     Programmer   Description                                         |
    1756             : |  ======   ============  =================================================   |
    1757             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1758             : |                                                                             |
    1759             : |  END_PROLOG                                                                 |
    1760             : -----------------------------------------------------------------------------*/
    1761             : intn
    1762           0 : SWfieldinfo(int32 swathID, const char *fieldname, int32 * rank, int32 dims[],
    1763             :       int32 * numbertype, char *dimlist)
    1764             : 
    1765             : {
    1766             :     intn            status; /* routine return status variable */
    1767             : 
    1768             :     int32           fid;  /* HDF-EOS file ID */
    1769             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1770             :     int32           swVgrpID; /* Swath root Vgroup ID */
    1771             : 
    1772             : 
    1773             :     /* Check for valid swath id */
    1774           0 :     status = SWchkswid(swathID, "SWfieldinfo", &fid,
    1775             :            &sdInterfaceID, &swVgrpID);
    1776           0 :     if (status == 0)
    1777             :     {
    1778             :   /* Check for field within Geolocatation Fields */
    1779           0 :   status = SWfinfo(swathID, "Geolocation Fields", fieldname,
    1780             :        rank, dims, numbertype, dimlist);
    1781             : 
    1782             :   /* If not there then check within Data Fields */
    1783           0 :   if (status == -1)
    1784             :   {
    1785           0 :       status = SWfinfo(swathID, "Data Fields", fieldname,
    1786             :            rank, dims, numbertype, dimlist);
    1787             :   }
    1788             : 
    1789             :   /* If not there either then can't be found */
    1790           0 :   if (status == -1)
    1791             :   {
    1792           0 :       HEpush(DFE_GENAPP, "SWfieldinfo", __FILE__, __LINE__);
    1793           0 :       HEreport("Fieldname \"%s\" not found.\n", fieldname);
    1794             :   }
    1795             :     }
    1796           0 :     return (status);
    1797             : }
    1798             : 
    1799             : 
    1800             : 
    1801             : 
    1802             : 
    1803             : 
    1804             : /*----------------------------------------------------------------------------|
    1805             : |  BEGIN_PROLOG                                                               |
    1806             : |                                                                             |
    1807             : |  FUNCTION: SWdefdimmap                                                      |
    1808             : |                                                                             |
    1809             : |  DESCRIPTION: Defines mapping between geolocation and data dimensions       |
    1810             : |                                                                             |
    1811             : |                                                                             |
    1812             : |  Return Value    Type     Units     Description                             |
    1813             : |  ============   ======  =========   =====================================   |
    1814             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1815             : |                                                                             |
    1816             : |  INPUTS:                                                                    |
    1817             : |  swathID        int32               swath structure ID                      |
    1818             : |  geodim         char                Geolocation dimension                   |
    1819             : |  datadim        char                Data dimension                          |
    1820             : |  offset         int32               Mapping offset                          |
    1821             : |  increment      int32               Mapping increment                       |
    1822             : |                                                                             |
    1823             : |  OUTPUTS:                                                                   |
    1824             : |             None                                                            |
    1825             : |                                                                             |
    1826             : |  NOTES:                                                                     |
    1827             : |                                                                             |
    1828             : |                                                                             |
    1829             : |   Date     Programmer   Description                                         |
    1830             : |  ======   ============  =================================================   |
    1831             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1832             : |                                                                             |
    1833             : |  END_PROLOG                                                                 |
    1834             : -----------------------------------------------------------------------------*/
    1835             : intn
    1836           0 : SWdefdimmap(int32 swathID, const char *geodim, const char *datadim, int32 offset,
    1837             :       int32 increment)
    1838             : 
    1839             : {
    1840             :     intn            status; /* routine return status variable */
    1841             : 
    1842             :     int32           fid;  /* HDF-EOS file ID */
    1843             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1844             :     int32           size; /* Size of geo dim */
    1845           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1846             :     int32           dum;  /* Dummy variable */
    1847             :     int32           metadata[2];/* Offset & Increment (passed to metadata) */
    1848             : 
    1849             :     char            mapname[80];/* Mapping name (geodim/datadim) */
    1850             :     char            swathname[80];  /* Swath name */
    1851             : 
    1852             :     /* Check Swath ID */
    1853           0 :     status = SWchkswid(swathID, "SWdefdimmap", &fid, &sdInterfaceID, &dum);
    1854             : 
    1855           0 :     if (status == 0)
    1856             :     {
    1857             : 
    1858             :   /* Search Dimension Vdata for dimension entries */
    1859             :   /* -------------------------------------------- */
    1860           0 :   size = SWdiminfo(swathID, geodim);
    1861           0 :   if (size == -1)
    1862             :   {
    1863           0 :       status = -1;
    1864           0 :       HEpush(DFE_GENAPP, "SWdefdimmap", __FILE__, __LINE__);
    1865           0 :       HEreport("Geolocation dimension name: \"%s\" not found.\n",
    1866             :          geodim);
    1867             :   }
    1868             :   /* Data Dimension Search */
    1869             :   /* --------------------- */
    1870           0 :   if (status == 0)
    1871             :   {
    1872           0 :       size = SWdiminfo(swathID, datadim);
    1873           0 :       if (size == -1)
    1874             :       {
    1875           0 :     status = -1;
    1876           0 :     HEpush(DFE_GENAPP, "SWdefdimmap", __FILE__, __LINE__);
    1877           0 :     HEreport("Data dimension name: \"%s\" not found.\n",
    1878             :        datadim);
    1879             :       }
    1880             :   }
    1881             : 
    1882             :   /* Write Dimension Map to Structural MetaData */
    1883             :   /* ------------------------------------------ */
    1884           0 :   if (status == 0)
    1885             :   {
    1886           0 :       snprintf(mapname, sizeof(mapname), "%s%s%s", geodim, "/", datadim);
    1887           0 :       metadata[0] = offset;
    1888           0 :       metadata[1] = increment;
    1889             : 
    1890           0 :       Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    1891           0 :       status = EHinsertmeta(sdInterfaceID, swathname, "s", 1L,
    1892             :           mapname, metadata);
    1893             : 
    1894             :   }
    1895             :     }
    1896           0 :     return (status);
    1897             : }
    1898             : 
    1899             : 
    1900             : 
    1901             : /*----------------------------------------------------------------------------|
    1902             : |  BEGIN_PROLOG                                                               |
    1903             : |                                                                             |
    1904             : |  FUNCTION: SWdefidxmap                                                      |
    1905             : |                                                                             |
    1906             : |  DESCRIPTION: Defines l_indexed (non-linear) mapping between geolocation      |
    1907             : |               and data dimensions                                           |
    1908             : |                                                                             |
    1909             : |                                                                             |
    1910             : |  Return Value    Type     Units     Description                             |
    1911             : |  ============   ======  =========   =====================================   |
    1912             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1913             : |                                                                             |
    1914             : |  INPUTS:                                                                    |
    1915             : |  swathID        int32               swath structure ID                      |
    1916             : |  geodim         char                Geolocation dimension                   |
    1917             : |  datadim        char                Data dimension                          |
    1918             : |  l_index          int32               Index mapping array                     |
    1919             : |                                                                             |
    1920             : |  OUTPUTS:                                                                   |
    1921             : |             None                                                            |
    1922             : |                                                                             |
    1923             : |  NOTES:                                                                     |
    1924             : |                                                                             |
    1925             : |                                                                             |
    1926             : |   Date     Programmer   Description                                         |
    1927             : |  ======   ============  =================================================   |
    1928             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1929             : |                                                                             |
    1930             : |  END_PROLOG                                                                 |
    1931             : -----------------------------------------------------------------------------*/
    1932             : intn
    1933           0 : SWdefidxmap(int32 swathID, const char *geodim, const char *datadim, int32 l_index[])
    1934             : 
    1935             : {
    1936             :     intn            status; /* routine return status variable */
    1937             : 
    1938             :     int32           fid;  /* HDF-EOS file ID */
    1939             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1940             :     int32           attrVgrpID; /* Swath attribute ID */
    1941             :     int32           vdataID;  /* Mapping Index Vdata ID */
    1942             :     int32           gsize;  /* Size of geo dim */
    1943             :     int32           dsize;  /* Size of data dim */
    1944           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    1945             :     int32           dum;  /* Dummy variable */
    1946             : 
    1947             :     uint8          *buf;  /* Vdata field buffer */
    1948             : 
    1949             :     char            mapname[80];/* Mapping name (geodim/datadim) */
    1950             :     char            swathname[80];  /* Swath name */
    1951             :     char            utlbuf[256];/* Utility buffer */
    1952             : 
    1953             : 
    1954             :     /* Check Swath ID */
    1955           0 :     status = SWchkswid(swathID, "SWdefidxmap", &fid, &sdInterfaceID, &dum);
    1956           0 :     if (status == 0)
    1957             :     {
    1958             :   /* Search Dimension Vdata for dimension entries */
    1959             :   /* -------------------------------------------- */
    1960             : 
    1961             :   /* Geo Dimension Search */
    1962             :   /* -------------------- */
    1963             :        
    1964           0 :   gsize = SWdiminfo(swathID, geodim);
    1965             : 
    1966           0 :   if (gsize == -1)
    1967             :   {
    1968           0 :       status = -1;
    1969           0 :       HEpush(DFE_GENAPP, "SWdefidxmap", __FILE__, __LINE__);
    1970           0 :       HEreport("Geolocation dimension name: \"%s\" not found.\n",
    1971             :          geodim);
    1972             :   }
    1973             :   /* Data Dimension Search */
    1974             :   /* --------------------- */
    1975           0 :   if (status == 0)
    1976             :   {
    1977           0 :       dsize = SWdiminfo(swathID, datadim);
    1978           0 :       if (dsize == -1)
    1979             :       {
    1980           0 :     status = -1;
    1981           0 :     HEpush(DFE_GENAPP, "SWdefidxmap", __FILE__, __LINE__);
    1982           0 :     HEreport("Data dimension name: \"%s\" not found.\n",
    1983             :        datadim);
    1984             :       }
    1985             :   }
    1986             :   /* Define Index Vdata and Store Index Array */
    1987             :   /* ---------------------------------------- */
    1988           0 :   if (status == 0)
    1989             :   {
    1990             :       /* Get attribute Vgroup ID and allocate data buffer */
    1991             :       /* ------------------------------------------------ */
    1992           0 :       attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
    1993           0 :       buf = (uint8 *) calloc(4 * gsize, 1);
    1994           0 :       if(buf == NULL)
    1995             :       { 
    1996           0 :     HEpush(DFE_NOSPACE,"SWdefidxmap", __FILE__, __LINE__);
    1997           0 :     return(-1);
    1998             :       }
    1999             : 
    2000             :       /* Name: "INDXMAP:" + geodim + "/" + datadim */
    2001           0 :       snprintf(utlbuf, sizeof(utlbuf), "%s%s%s%s", "INDXMAP:", geodim, "/", datadim);
    2002             : 
    2003           0 :       vdataID = VSattach(fid, -1, "w");
    2004           0 :       VSsetname(vdataID, utlbuf);
    2005             : 
    2006             :       /* Attribute Class */
    2007           0 :       VSsetclass(vdataID, "Attr0.0");
    2008             : 
    2009             :       /* Fieldname is "Index" */
    2010           0 :       VSfdefine(vdataID, "Index", DFNT_INT32, gsize);
    2011           0 :       VSsetfields(vdataID, "Index");
    2012           0 :       memcpy(buf, l_index, 4 * gsize);
    2013             : 
    2014             :       /* Write to vdata and free data buffer */
    2015           0 :       VSwrite(vdataID, buf, 1, FULL_INTERLACE);
    2016           0 :       free(buf);
    2017             : 
    2018             :       /* Insert in Attribute Vgroup and detach Vdata */
    2019           0 :       Vinsert(attrVgrpID, vdataID);
    2020           0 :       VSdetach(vdataID);
    2021             : 
    2022             : 
    2023             :       /* Write to Structural Metadata */
    2024           0 :       snprintf(mapname, sizeof(mapname), "%s%s%s", geodim, "/", datadim);
    2025           0 :       Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    2026           0 :       status = EHinsertmeta(sdInterfaceID, swathname, "s", 2L,
    2027             :           mapname, &dum);
    2028             : 
    2029             :   }
    2030             :     }
    2031           0 :     return (status);
    2032             : 
    2033             : }
    2034             : 
    2035             : 
    2036             : /*----------------------------------------------------------------------------|
    2037             : |  BEGIN_PROLOG                                                               |
    2038             : |                                                                             |
    2039             : |  FUNCTION: SWdefcomp                                                        |
    2040             : |                                                                             |
    2041             : |  DESCRIPTION: Defines compression type and parameters                       |
    2042             : |                                                                             |
    2043             : |                                                                             |
    2044             : |  Return Value    Type     Units     Description                             |
    2045             : |  ============   ======  =========   =====================================   |
    2046             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2047             : |                                                                             |
    2048             : |  INPUTS:                                                                    |
    2049             : |  swathID        int32               swath structure ID                      |
    2050             : |  compcode       int32               compression code                        |
    2051             : |  compparm       intn                compression parameters                  |
    2052             : |                                                                             |
    2053             : |  OUTPUTS:                                                                   |
    2054             : |             None                                                            |
    2055             : |                                                                             |
    2056             : |  NOTES:                                                                     |
    2057             : |                                                                             |
    2058             : |                                                                             |
    2059             : |   Date     Programmer   Description                                         |
    2060             : |  ======   ============  =================================================   |
    2061             : |  Sep 96   Joel Gales    Original Programmer                                 |
    2062             : |                                                                             |
    2063             : |  END_PROLOG                                                                 |
    2064             : -----------------------------------------------------------------------------*/
    2065             : intn
    2066           0 : SWdefcomp(int32 swathID, int32 compcode, intn compparm[])
    2067             : {
    2068           0 :     intn            status = 0; /* routine return status variable */
    2069             : 
    2070             :     int32           fid;  /* HDF-EOS file id */
    2071             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    2072             :     int32           swVgrpID; /* Swath root Vgroup ID */
    2073           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2074             :     int32           sID;  /* swathID - offset */
    2075             : 
    2076             : 
    2077             :     /* Check for valid swath id */
    2078           0 :     status = SWchkswid(swathID, "SWdefcomp", &fid, &sdInterfaceID, &swVgrpID);
    2079             : 
    2080           0 :     if (status == 0)
    2081             :     {
    2082           0 :   sID = swathID % idOffset;
    2083             : 
    2084             :   /* Set compression code in compression external array */
    2085           0 :   SWXSwath[sID].compcode = compcode;
    2086             : 
    2087           0 :   switch (compcode)
    2088             :   {
    2089             :       /* Set NBIT compression parameters in compression external array */
    2090           0 :   case HDFE_COMP_NBIT:
    2091             : 
    2092           0 :       SWXSwath[sID].compparm[0] = compparm[0];
    2093           0 :       SWXSwath[sID].compparm[1] = compparm[1];
    2094           0 :       SWXSwath[sID].compparm[2] = compparm[2];
    2095           0 :       SWXSwath[sID].compparm[3] = compparm[3];
    2096             : 
    2097           0 :       break;
    2098             : 
    2099             :       /* Set GZIP compression parameter in compression external array */
    2100           0 :   case HDFE_COMP_DEFLATE:
    2101             : 
    2102           0 :       SWXSwath[sID].compparm[0] = compparm[0];
    2103             : 
    2104           0 :       break;
    2105             : 
    2106             :   }
    2107           0 :     }
    2108             : 
    2109           0 :     return (status);
    2110             : }
    2111             : 
    2112             : /*----------------------------------------------------------------------------|
    2113             : |  BEGIN_PROLOG                                                               |
    2114             : |                                                                             |
    2115             : |  FUNCTION: SWdefinefield                                                    |
    2116             : |                                                                             |
    2117             : |  DESCRIPTION: Defines geolocation or data field within swath structure      |
    2118             : |                                                                             |
    2119             : |                                                                             |
    2120             : |  Return Value    Type     Units     Description                             |
    2121             : |  ============   ======  =========   =====================================   |
    2122             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2123             : |                                                                             |
    2124             : |  INPUTS:                                                                    |
    2125             : |  swathID        int32               swath structure ID                      |
    2126             : |  fieldtype      char                geo/data fieldtype                      |
    2127             : |  fieldname      char                fieldname                               |
    2128             : |  dimlist        char                Dimension list (comma-separated list)   |
    2129             : |  numbertype     int32               field type                              |
    2130             : |  merge          int32               merge code                              |
    2131             : |                                                                             |
    2132             : |                                                                             |
    2133             : |  OUTPUTS:                                                                   |
    2134             : |             None                                                            |
    2135             : |                                                                             |
    2136             : |  NOTES:                                                                     |
    2137             : |                                                                             |
    2138             : |                                                                             |
    2139             : |   Date     Programmer   Description                                         |
    2140             : |  ======   ============  =================================================   |
    2141             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2142             : |  Aug 96   Joel Gales    Check name for length                               |
    2143             : |  Sep 96   Joel Gales    Make string array "dimbuf" dynamic                  |
    2144             : |  Oct 96   Joel Gales    Make sure total length of "merged" Vdata < 64       |
    2145             : |  Jun 03   Abe Taaheri   Supplied cast comp_coder_t in call to SDsetcompress |
    2146             : |                                                                             |
    2147             : |  END_PROLOG                                                                 |
    2148             : -----------------------------------------------------------------------------*/
    2149             : static intn
    2150           0 : SWdefinefield(int32 swathID, const char *fieldtype, const char *fieldname, const char *dimlist,
    2151             :         int32 numbertype, int32 merge)
    2152             : 
    2153             : {
    2154             :     intn            i;    /* Loop index */
    2155             :     intn            status; /* routine return status variable */
    2156             :     intn            found;  /* utility found flag */
    2157           0 :     intn            foundNT = 0;/* found number type flag */
    2158           0 :     intn            foundAllDim = 1;  /* found all dimensions flag */
    2159           0 :     intn            first = 1;  /* first entry flag */
    2160             :     intn            fac;  /* Geo (-1), Data (+1) field factor */
    2161           0 :     int32           cnt = 0;
    2162             : 
    2163             :     int32           fid;  /* HDF-EOS file ID */
    2164           0 :     int32           vdataID = 0;  /* Vdata ID */
    2165             :     int32           vgid; /* Geo/Data field Vgroup ID */
    2166             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    2167             :     int32           sdid; /* SDS object ID */
    2168             :     int32           dimid;  /* SDS dimension ID */
    2169             :     int32           recSize;  /* Vdata record size */
    2170             :     int32           swVgrpID; /* Swath root Vgroup ID */
    2171             :     int32           dims[8];  /* Dimension size array */
    2172             :     int32           dimsize;  /* Dimension size */
    2173           0 :     int32           rank = 0; /* Field rank */
    2174             :     int32           slen[32]; /* String length array */
    2175           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2176             :     int32           compcode; /* Compression code */
    2177             :     int32           sID;  /* SwathID - offset */
    2178             : 
    2179             :     uint8          *oneDbuf;  /* Vdata record buffer */
    2180             :     char           *dimbuf; /* Dimension buffer */
    2181             :     char           *comma;  /* Pointer to comma */
    2182             :     char           *dimcheck; /* Dimension check buffer */
    2183             :     char            utlbuf[512];/* Utility buffer */
    2184             :     char            utlbuf2[256]; /* Utility buffer 2 */
    2185             :     char           *ptr[32];  /* String pointer array */
    2186             :     char            swathname[80];  /* Swath name */
    2187             :     char            errbuf1[128]; /* Error message buffer 1 */
    2188             :     char            errbuf2[128]; /* Error message buffer 2 */
    2189             :     char            compparmbuf[128]; /* Compression parameter string buffer */
    2190             : 
    2191           0 :     const char           *HDFcomp[5] = {"HDFE_COMP_NONE", "HDFE_COMP_RLE",
    2192             :   "HDFE_COMP_NBIT", "HDFE_COMP_SKPHUFF",
    2193             :     "HDFE_COMP_DEFLATE"};
    2194             :     /* Compression code names */
    2195             : 
    2196           0 :     uint16          good_number[10] = {3, 4, 5, 6, 20, 21, 22, 23, 24, 25};
    2197             :     /* Valid number types */
    2198             :     comp_info       c_info; /* Compression parameter structure */
    2199             : 
    2200             : 
    2201             : 
    2202             :     /* Setup error message strings */
    2203             :     /* --------------------------- */
    2204           0 :     strcpy(errbuf1, "SWXSDname array too small.\nPlease increase ");
    2205           0 :     strcat(errbuf1, "size of HDFE_NAMBUFSIZE in \"HdfEosDef.h\".\n");
    2206           0 :     strcpy(errbuf2, "SWXSDdims array too small.\nPlease increase ");
    2207           0 :     strcat(errbuf2, "size of HDFE_DIMBUFSIZE in \"HdfEosDef.h\".\n");
    2208             : 
    2209             : 
    2210             : 
    2211             :     /*
    2212             :      * Check for proper swath ID and return HDF-EOS file ID, SDinterface ID,
    2213             :      * and swath root Vgroup ID
    2214             :      */
    2215           0 :     status = SWchkswid(swathID, "SWdefinefield",
    2216             :            &fid, &sdInterfaceID, &swVgrpID);
    2217             : 
    2218             : 
    2219           0 :     if (status == 0)
    2220             :     {
    2221             :   /* Remove offset from swath ID & get swathname */
    2222           0 :   sID = swathID % idOffset;
    2223           0 :   Vgetname(swVgrpID, swathname);
    2224             : 
    2225             :   /* Allocate space for dimbuf, copy dimlist into it, & append comma */
    2226           0 :   dimbuf = (char *) calloc(strlen(dimlist) + 64, 1);
    2227           0 :   if(dimbuf == NULL)
    2228             :   { 
    2229           0 :       HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
    2230           0 :       return(-1);
    2231             :   }
    2232           0 :   strcpy(dimbuf, dimlist);
    2233           0 :   strcat(dimbuf, ",");
    2234             : 
    2235             :   /* Find comma */
    2236           0 :   comma = strchr(dimbuf, ',');
    2237             : 
    2238             : 
    2239             :   /*
    2240             :    * Loop through entries in dimension list to make sure they are
    2241             :    * defined in swath
    2242             :    */
    2243           0 :   while (comma != NULL)
    2244             :   {
    2245             :       /* Copy dimension list entry to dimcheck */
    2246           0 :       dimcheck = (char *) calloc(comma - dimbuf + 1, 1);
    2247           0 :       if(dimcheck == NULL)
    2248             :       { 
    2249           0 :     HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
    2250           0 :     free(dimbuf);
    2251           0 :     return(-1);
    2252             :       }
    2253           0 :       memcpy(dimcheck, dimbuf, comma - dimbuf);
    2254             : 
    2255             :       /* Get dimension size */
    2256           0 :       dimsize = SWdiminfo(swathID, dimcheck);
    2257             : 
    2258             :       /* if != -1 then sent found flag, store size and increment rank */
    2259           0 :       if (dimsize != -1)
    2260             :       {
    2261           0 :     dims[rank] = dimsize;
    2262           0 :     rank++;
    2263             :       }
    2264             :       else
    2265             :       {
    2266             :     /*
    2267             :      * If dimension list entry not found - set error return
    2268             :      * status, append name to utility buffer for error report
    2269             :      */
    2270           0 :     status = -1;
    2271           0 :     foundAllDim = 0;
    2272           0 :     if (first == 1)
    2273             :     {
    2274           0 :         strcpy(utlbuf, dimcheck);
    2275             :     }
    2276             :     else
    2277             :     {
    2278           0 :         strcat(utlbuf, ",");
    2279           0 :         strcat(utlbuf, dimcheck);
    2280             :     }
    2281           0 :     first = 0;
    2282             :       }
    2283             : 
    2284             :       /*
    2285             :        * Go to next dimension entry, find next comma, & free up
    2286             :        * dimcheck buffer
    2287             :        */
    2288           0 :             *comma = '\0';  /* zero out first comma  */
    2289           0 :             comma++;
    2290           0 :             comma = strchr(comma, ',');
    2291           0 :             if (comma != NULL)
    2292             :             {
    2293           0 :                for (i=0; i<(intn)strlen(dimcheck) + 1; i++)
    2294             :                {
    2295           0 :                   dimbuf++;
    2296           0 :                   cnt++;
    2297             :                }
    2298             :             }
    2299           0 :       free(dimcheck);
    2300             :   }
    2301           0 :         for(i=0; i<cnt; i++)
    2302           0 :            dimbuf--;
    2303             : 
    2304           0 :   free(dimbuf);
    2305             : 
    2306             : 
    2307             :   /* Check that UNLIMITED dimension is first dimension if present */
    2308             :   /* ------------------------------------------------------------ */
    2309           0 :   if (status == 0)
    2310             :   {
    2311           0 :       for (i = 0; i < rank; i++)
    2312             :       {
    2313           0 :     if (dims[i] == 0 && i != 0)
    2314             :     {
    2315           0 :         status = -1;
    2316           0 :         HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__);
    2317           0 :         HEreport("UNLIMITED dimension must be first dimension.\n");
    2318             :     }
    2319             :       }
    2320             :   }
    2321             : 
    2322             :   /* Check fieldname length */
    2323             :   /* ---------------------- */
    2324           0 :   if (status == 0)
    2325             :   {
    2326             : /* ((intn) strlen(fieldname) > MAX_NC_NAME - 7)
    2327             : ** this was changed because HDF4.1r3 made a change in the
    2328             : ** hlimits.h file.  We have notified NCSA and asked to have 
    2329             : ** it made the same as in previous versions of HDF
    2330             : ** see ncr 26314.  DaW  Apr 2000
    2331             : */
    2332             : 
    2333           0 :       if (((intn) strlen(fieldname) > VSNAMELENMAX && rank == 1) ||
    2334           0 :     ((intn) strlen(fieldname) > (256 - 7) && rank > 1))
    2335             :       {
    2336           0 :     status = -1;
    2337           0 :     HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__);
    2338           0 :     HEreport("Fieldname \"%s\" too long.\n", fieldname);
    2339             :       }
    2340             :   }
    2341             : 
    2342             :   /* Check for valid numbertype */
    2343             :   /* -------------------------- */
    2344           0 :   if (status == 0)
    2345             :   {
    2346           0 :       for (i = 0; i < 10; i++)
    2347             :       {
    2348           0 :     if (numbertype == good_number[i])
    2349             :     {
    2350           0 :         foundNT = 1;
    2351             :     }
    2352             :       }
    2353             : 
    2354           0 :       if (foundNT == 0)
    2355             :       {
    2356           0 :     HEpush(DFE_BADNUMTYPE, "SWdefinefield", __FILE__, __LINE__);
    2357           0 :     HEreport("Invalid number type: %d (%s).\n",
    2358             :        numbertype, fieldname);
    2359           0 :     status = -1;
    2360             :       }
    2361             :   }
    2362             : 
    2363             : 
    2364             :   /* Define Field */
    2365             :   /* ------------ */
    2366           0 :   if (status == 0)
    2367             :   {
    2368             :       /* Set factor & get Field Vgroup id */
    2369             :       /* -------------------------------- */
    2370           0 :       if (strcmp(fieldtype, "Geolocation Fields") == 0)
    2371             :       {
    2372           0 :     fac = -1;
    2373           0 :     vgid = SWXSwath[sID].VIDTable[0];
    2374             :       }
    2375             :       else
    2376             :       {
    2377           0 :     fac = +1;
    2378           0 :     vgid = SWXSwath[sID].VIDTable[1];
    2379             :       }
    2380             :       /*
    2381             :        * Note: "fac" is used to distinguish geo fields from data fields
    2382             :        * so that they are not merged together
    2383             :        */
    2384             : 
    2385             : 
    2386             :       /* One D Fields */
    2387             :       /* ------------ */
    2388           0 :       if (rank == 1)
    2389             :       {
    2390             :     /* No Compression for 1D (Vdata) fields */
    2391           0 :     compcode = HDFE_COMP_NONE;
    2392             : 
    2393             : 
    2394             :     /* If field non-appendable and merge set to AUTOMERGE ... */
    2395           0 :     if (dims[0] != 0 && merge == HDFE_AUTOMERGE)
    2396             :     {
    2397           0 :         i = 0;
    2398           0 :         found = 0;
    2399             : 
    2400             :         /* Loop through previous entries in 1d combination array */
    2401           0 :         while (SWX1dcomb[3 * i] != 0)
    2402             :         {
    2403             :       /* Get name of previous 1d combined field */
    2404           0 :       vdataID = SWX1dcomb[3 * i + 2];
    2405           0 :       VSgetname(vdataID, utlbuf);
    2406             : 
    2407             :       /*
    2408             :        * If dimension, field type (geo/data), and swath
    2409             :        * structure if current entry match a previous entry
    2410             :        * and combined name is less than max allowed then
    2411             :        * set "found" flag and exit loop
    2412             :        */
    2413           0 :       if (SWX1dcomb[3 * i] == fac * dims[0] &&
    2414           0 :           SWX1dcomb[3 * i + 1] == swVgrpID &&
    2415           0 :           (intn) strlen(utlbuf) +
    2416           0 :           (intn) strlen(fieldname) + 1 <=
    2417             :           VSNAMELENMAX)
    2418             :       {
    2419           0 :           found = 1;
    2420           0 :           break;
    2421             :       }
    2422             :       /* Increment loop index */
    2423           0 :       i++;
    2424             :         }
    2425             : 
    2426             : 
    2427           0 :         if (found == 0)
    2428             :         {
    2429             :       /*
    2430             :        * If no matching entry found then start new Vdata
    2431             :        * and store dimension size, swath root Vgroup ID,
    2432             :        * field Vdata and fieldname in external array
    2433             :        * "SWX1dcomb"
    2434             :        */
    2435           0 :       vdataID = VSattach(fid, -1, "w");
    2436           0 :       SWX1dcomb[3 * i] = fac * dims[0];
    2437           0 :       SWX1dcomb[3 * i + 1] = swVgrpID;
    2438           0 :       SWX1dcomb[3 * i + 2] = vdataID;
    2439           0 :       VSsetname(vdataID, fieldname);
    2440             :         }
    2441             :         else
    2442             :         {
    2443             :       /*
    2444             :        * If match then concatenate current fieldname to
    2445             :        * previous matching fieldnames.
    2446             :        */
    2447           0 :       strcat(utlbuf, ",");
    2448           0 :       strcat(utlbuf, fieldname);
    2449           0 :       VSsetname(vdataID, utlbuf);
    2450             :         }
    2451             : 
    2452             :         /* Define field as field within Vdata */
    2453           0 :         VSfdefine(vdataID, fieldname, numbertype, 1);
    2454           0 :         Vinsert(vgid, vdataID);
    2455             : 
    2456             :     }
    2457             :     else
    2458             :     {
    2459             :         /* 1d No Merge Section */
    2460             : 
    2461             :         /* Get new vdata ID and establish field within Vdata */
    2462           0 :         vdataID = VSattach(fid, -1, "w");
    2463           0 :         VSsetname(vdataID, fieldname);
    2464           0 :         VSfdefine(vdataID, fieldname, numbertype, 1);
    2465           0 :         VSsetfields(vdataID, fieldname);
    2466             : 
    2467           0 :         recSize = VSsizeof(vdataID, (char*) fieldname);
    2468           0 :         if (dims[0] == 0)
    2469             :         {
    2470             :       /*
    2471             :        * If appendable field then write single record
    2472             :        * filled with 255
    2473             :        */
    2474           0 :       oneDbuf = (uint8 *) calloc(recSize, 1);
    2475           0 :       if(oneDbuf == NULL)
    2476             :       { 
    2477           0 :           HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
    2478           0 :           return(-1);
    2479             :       }
    2480           0 :       for (i = 0; i < recSize; i++)
    2481           0 :           oneDbuf[i] = 255;
    2482           0 :       VSwrite(vdataID, oneDbuf, 1, FULL_INTERLACE);
    2483             :         }
    2484             :         else
    2485             :         {
    2486             :       /*
    2487             :        * If non-appendable then write entire field with
    2488             :        * blank records
    2489             :        */
    2490           0 :       oneDbuf = (uint8 *) calloc(recSize, dims[0]);
    2491           0 :       if(oneDbuf == NULL)
    2492             :       { 
    2493           0 :           HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
    2494           0 :           return(-1);
    2495             :       }
    2496           0 :       VSwrite(vdataID, oneDbuf, dims[0], FULL_INTERLACE);
    2497             :         }
    2498           0 :         free(oneDbuf);
    2499             : 
    2500             :         /* Insert Vdata into field Vgroup & detach */
    2501           0 :         Vinsert(vgid, vdataID);
    2502           0 :         VSdetach(vdataID);
    2503             : 
    2504             :     }   /* End No Merge Section */
    2505             : 
    2506             :       }     /* End 1d field Section */
    2507             :       else
    2508             :       {
    2509             :     /* SDS Interface (Multi-dim fields) */
    2510             :     /* -------------------------------- */
    2511             : 
    2512             :     /* Get current compression code */
    2513           0 :     compcode = SWXSwath[sID].compcode;
    2514             : 
    2515             :     /*
    2516             :      * If rank is less than or equal to 3 (and greater than 1)
    2517             :      * and AUTOMERGE is set and the first dimension is not
    2518             :      * appendable and the compression code is set to none then
    2519             :      * ...
    2520             :      */
    2521           0 :     if (rank <= 3 && merge == HDFE_AUTOMERGE && dims[0] != 0
    2522           0 :         && compcode == HDFE_COMP_NONE)
    2523             :     {
    2524             :         /* Find first empty slot in external combination array */
    2525             :         /* --------------------------------------------------- */
    2526           0 :         i = 0;
    2527           0 :         while (SWXSDcomb[5 * i] != 0)
    2528             :         {
    2529           0 :       i++;
    2530             :         }
    2531             : 
    2532             :         /*
    2533             :          * Store dimensions (with geo/data factor), swath root
    2534             :          * Vgroup ID, and number type in external combination
    2535             :          * array "SWXSDcomb"
    2536             :          */
    2537             : 
    2538           0 :         if (rank == 2)
    2539             :         {
    2540             :       /* If 2-dim field then set lowest dimension to +/- 1 */
    2541           0 :       SWXSDcomb[5 * i] = fac;
    2542           0 :       SWXSDcomb[5 * i + 1] = fac * dims[0];
    2543           0 :       SWXSDcomb[5 * i + 2] = fac * dims[1];
    2544             :         }
    2545             :         else
    2546             :         {
    2547           0 :       SWXSDcomb[5 * i] = fac * dims[0];
    2548           0 :       SWXSDcomb[5 * i + 1] = fac * dims[1];
    2549           0 :       SWXSDcomb[5 * i + 2] = fac * dims[2];
    2550             :         }
    2551             : 
    2552           0 :         SWXSDcomb[5 * i + 3] = swVgrpID;
    2553           0 :         SWXSDcomb[5 * i + 4] = numbertype;
    2554             : 
    2555             : 
    2556             :         /* Concatenate fieldname with combined name string */
    2557             :         /* ----------------------------------------------- */
    2558           0 :         if ((intn) strlen(SWXSDname) +
    2559           0 :       (intn) strlen(fieldname) + 2 < HDFE_NAMBUFSIZE)
    2560             :         {
    2561           0 :       strcat(SWXSDname, fieldname);
    2562           0 :       strcat(SWXSDname, ",");
    2563             :         }
    2564             :         else
    2565             :         {
    2566             :       /* SWXSDname array too small! */
    2567             :       /* -------------------------- */
    2568           0 :       HEpush(DFE_GENAPP, "SWdefinefield",
    2569             :              __FILE__, __LINE__);
    2570           0 :       HEreport(errbuf1);
    2571           0 :       status = -1;
    2572           0 :       return (status);
    2573             :         }
    2574             : 
    2575             : 
    2576             : 
    2577             :         /*
    2578             :          * If 2-dim field then set lowest dimension (in 3-dim
    2579             :          * array) to "ONE"
    2580             :          */
    2581           0 :         if (rank == 2)
    2582             :         {
    2583           0 :       if ((intn) strlen(SWXSDdims) + 5 < HDFE_DIMBUFSIZE)
    2584             :       {
    2585           0 :           strcat(SWXSDdims, "ONE,");
    2586             :       }
    2587             :       else
    2588             :       {
    2589             :           /* SWXSDdims array too small! */
    2590             :           /* -------------------------- */
    2591           0 :           HEpush(DFE_GENAPP, "SWdefinefield",
    2592             :            __FILE__, __LINE__);
    2593           0 :           HEreport(errbuf2);
    2594           0 :           status = -1;
    2595           0 :           return (status);
    2596             :       }
    2597             : 
    2598             :         }
    2599             : 
    2600             :         /*
    2601             :          * Concatenate field dimlist to merged dimlist and
    2602             :          * separate fields with semi-colon.
    2603             :          */
    2604           0 :         if ((intn) strlen(SWXSDdims) +
    2605           0 :       (intn) strlen(dimlist) + 2 < HDFE_DIMBUFSIZE)
    2606             :         {
    2607           0 :       strcat(SWXSDdims, dimlist);
    2608           0 :       strcat(SWXSDdims, ";");
    2609             :         }
    2610             :         else
    2611             :         {
    2612             :       /* SWXSDdims array too small! */
    2613             :       /* -------------------------- */
    2614           0 :       HEpush(DFE_GENAPP, "SWdefinefield",
    2615             :              __FILE__, __LINE__);
    2616           0 :       HEreport(errbuf2);
    2617           0 :       status = -1;
    2618           0 :       return (status);
    2619             :         }
    2620             : 
    2621             :     }   /* End Multi-Dim Merge Section */
    2622             :     else
    2623             :     {
    2624             :         /* Multi-Dim No Merge Section */
    2625             :         /* ========================== */
    2626             : 
    2627             :         /* Create SDS dataset */
    2628             :         /* ------------------ */
    2629           0 :         sdid = SDcreate(sdInterfaceID, fieldname,
    2630             :             numbertype, rank, dims);
    2631             : 
    2632             : 
    2633             :         /* Store Dimension Names in SDS */
    2634             :         /* ---------------------------- */
    2635           0 :         rank = EHparsestr(dimlist, ',', ptr, slen);
    2636           0 :         for (i = 0; i < rank; i++)
    2637             :         {
    2638             :       /* Dimension name = Swathname:Dimname */
    2639           0 :       memcpy(utlbuf, ptr[i], slen[i]);
    2640           0 :       utlbuf[slen[i]] = 0;
    2641           0 :       strcat(utlbuf, ":");
    2642           0 :       strcat(utlbuf, swathname);
    2643             : 
    2644           0 :       dimid = SDgetdimid(sdid, i);
    2645           0 :       SDsetdimname(dimid, utlbuf);
    2646             :         }
    2647             : 
    2648             : 
    2649             :         /* Setup compression parameters */
    2650           0 :         if (compcode == HDFE_COMP_NBIT)
    2651             :         {
    2652           0 :       c_info.nbit.nt = numbertype;
    2653           0 :       c_info.nbit.sign_ext = SWXSwath[sID].compparm[0];
    2654           0 :       c_info.nbit.fill_one = SWXSwath[sID].compparm[1];
    2655           0 :       c_info.nbit.start_bit = SWXSwath[sID].compparm[2];
    2656           0 :       c_info.nbit.bit_len = SWXSwath[sID].compparm[3];
    2657             :         }
    2658           0 :         else if (compcode == HDFE_COMP_SKPHUFF)
    2659             :         {
    2660           0 :       c_info.skphuff.skp_size = (intn) DFKNTsize(numbertype);
    2661             :         }
    2662           0 :         else if (compcode == HDFE_COMP_DEFLATE)
    2663             :         {
    2664           0 :       c_info.deflate.level = SWXSwath[sID].compparm[0];
    2665             :         }
    2666             : 
    2667             :         /* If field is compressed then call SDsetcompress */
    2668             :         /* ---------------------------------------------- */
    2669           0 :         if (compcode != HDFE_COMP_NONE)
    2670             :         {
    2671           0 :                         /* status = */ SDsetcompress(sdid, (comp_coder_t) compcode, &c_info);
    2672             :         }
    2673             : 
    2674             : 
    2675             :         /* Attach to Vgroup */
    2676             :         /* ---------------- */
    2677           0 :         Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid));
    2678             : 
    2679             : 
    2680             :         /* Store SDS dataset IDs */
    2681             :         /* --------------------- */
    2682           0 :         if (SWXSwath[sID].nSDS > 0)
    2683             :         {
    2684           0 :       SWXSwath[sID].sdsID = (int32 *)
    2685           0 :           realloc((void *) SWXSwath[sID].sdsID,
    2686           0 :             (SWXSwath[sID].nSDS + 1) * 4);
    2687           0 :       if(SWXSwath[sID].sdsID == NULL)
    2688             :       { 
    2689           0 :           HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
    2690           0 :           return(-1);
    2691             :       }
    2692             : 
    2693             :         }
    2694             :         else
    2695             :         {
    2696           0 :       SWXSwath[sID].sdsID = (int32 *) calloc(1, 4);
    2697           0 :       if(SWXSwath[sID].sdsID == NULL)
    2698             :       { 
    2699           0 :           HEpush(DFE_NOSPACE,"SWdefinefield", __FILE__, __LINE__);
    2700           0 :           return(-1);
    2701             :       }
    2702             :         }
    2703           0 :         SWXSwath[sID].sdsID[SWXSwath[sID].nSDS] = sdid;
    2704           0 :         SWXSwath[sID].nSDS++;
    2705             : 
    2706             :     }   /* End Multi-Dim No Merge Section */
    2707             : 
    2708             :       }     /* End Multi-Dim Section */
    2709             : 
    2710             : 
    2711             : 
    2712             :       /* Setup metadata string */
    2713             :       /* --------------------- */
    2714           0 :       snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist);
    2715             : 
    2716             : 
    2717             :       /* Setup compression metadata */
    2718             :       /* -------------------------- */
    2719           0 :       if (compcode != HDFE_COMP_NONE)
    2720             :       {
    2721           0 :     snprintf(utlbuf2, sizeof(utlbuf2),
    2722             :       "%s%s",
    2723             :       ":\n\t\t\t\tCompressionType=", HDFcomp[compcode]);
    2724             : 
    2725           0 :     switch (compcode)
    2726             :     {
    2727           0 :     case HDFE_COMP_NBIT:
    2728             : 
    2729           0 :         snprintf(compparmbuf, sizeof(compparmbuf),
    2730             :           "%s%d,%d,%d,%d%s",
    2731             :           "\n\t\t\t\tCompressionParams=(",
    2732             :           SWXSwath[sID].compparm[0],
    2733             :           SWXSwath[sID].compparm[1],
    2734             :           SWXSwath[sID].compparm[2],
    2735             :           SWXSwath[sID].compparm[3], ")");
    2736           0 :         strcat(utlbuf2, compparmbuf);
    2737           0 :         break;
    2738             : 
    2739             : 
    2740           0 :     case HDFE_COMP_DEFLATE:
    2741             : 
    2742           0 :         snprintf(compparmbuf, sizeof(compparmbuf),
    2743             :           "%s%d",
    2744             :           "\n\t\t\t\tDeflateLevel=",
    2745             :           SWXSwath[sID].compparm[0]);
    2746           0 :         strcat(utlbuf2, compparmbuf);
    2747           0 :         break;
    2748             :     }
    2749             : 
    2750             :     /* Concatenate compression parameters with compression code */
    2751           0 :     strcat(utlbuf, utlbuf2);
    2752             :       }
    2753             : 
    2754             : 
    2755             :       /* Insert field metadata within File Structural Metadata */
    2756             :       /* ----------------------------------------------------- */
    2757           0 :       if (strcmp(fieldtype, "Geolocation Fields") == 0)
    2758             :       {
    2759           0 :     status = EHinsertmeta(sdInterfaceID, swathname, "s", 3L,
    2760             :               utlbuf, &numbertype);
    2761             :       }
    2762             :       else
    2763             :       {
    2764           0 :     status = EHinsertmeta(sdInterfaceID, swathname, "s", 4L,
    2765             :               utlbuf, &numbertype);
    2766             :       }
    2767             : 
    2768             :   }
    2769             :     }
    2770             : 
    2771             :     /* If all dimensions not found then report error */
    2772             :     /* --------------------------------------------- */
    2773           0 :     if (foundAllDim == 0)
    2774             :     {
    2775           0 :   HEpush(DFE_GENAPP, "SWdefinefield", __FILE__, __LINE__);
    2776           0 :   HEreport("Dimension(s): \"%s\" not found (%s).\n",
    2777             :      utlbuf, fieldname);
    2778           0 :   status = -1;
    2779             :     }
    2780             : 
    2781           0 :     return (status);
    2782             : }
    2783             : 
    2784             : 
    2785             : 
    2786             : 
    2787             : /*----------------------------------------------------------------------------|
    2788             : |  BEGIN_PROLOG                                                               |
    2789             : |                                                                             |
    2790             : |  FUNCTION: SWdefgeofield                                                    |
    2791             : |                                                                             |
    2792             : |  DESCRIPTION: Defines geolocation field within swath structure (wrapper)    |
    2793             : |                                                                             |
    2794             : |                                                                             |
    2795             : |  Return Value    Type     Units     Description                             |
    2796             : |  ============   ======  =========   =====================================   |
    2797             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2798             : |                                                                             |
    2799             : |  INPUTS:                                                                    |
    2800             : |  swathID        int32               swath structure ID                      |
    2801             : |  fieldname      char                fieldname                               |
    2802             : |  dimlist        char                Dimension list (comma-separated list)   |
    2803             : |  numbertype     int32               field type                              |
    2804             : |  merge          int32               merge code                              |
    2805             : |                                                                             |
    2806             : |  OUTPUTS:                                                                   |
    2807             : |             None                                                            |
    2808             : |                                                                             |
    2809             : |  NOTES:                                                                     |
    2810             : |                                                                             |
    2811             : |                                                                             |
    2812             : |   Date     Programmer   Description                                         |
    2813             : |  ======   ============  =================================================   |
    2814             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2815             : |                                                                             |
    2816             : |  END_PROLOG                                                                 |
    2817             : -----------------------------------------------------------------------------*/
    2818             : intn
    2819           0 : SWdefgeofield(int32 swathID, const char *fieldname, const char *dimlist,
    2820             :         int32 numbertype, int32 merge)
    2821             : {
    2822             :     intn            status; /* routine return status variable */
    2823             : 
    2824             :     /* Call SWdefinefield routine */
    2825             :     /* -------------------------- */
    2826           0 :     status = SWdefinefield(swathID, "Geolocation Fields", fieldname, dimlist,
    2827             :          numbertype, merge);
    2828             : 
    2829           0 :     return (status);
    2830             : }
    2831             : 
    2832             : 
    2833             : 
    2834             : 
    2835             : /*----------------------------------------------------------------------------|
    2836             : |  BEGIN_PROLOG                                                               |
    2837             : |                                                                             |
    2838             : |  FUNCTION: SWdefdatafield                                                   |
    2839             : |                                                                             |
    2840             : |  DESCRIPTION: Defines data field within swath structure (wrapper)           |
    2841             : |                                                                             |
    2842             : |                                                                             |
    2843             : |  Return Value    Type     Units     Description                             |
    2844             : |  ============   ======  =========   =====================================   |
    2845             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2846             : |                                                                             |
    2847             : |  INPUTS:                                                                    |
    2848             : |  swathID        int32               swath structure ID                      |
    2849             : |  fieldname      char                fieldname                               |
    2850             : |  dimlist        char                Dimension list (comma-separated list)   |
    2851             : |  numbertype     int32               field type                              |
    2852             : |  merge          int32               merge code                              |
    2853             : |                                                                             |
    2854             : |  OUTPUTS:                                                                   |
    2855             : |             None                                                            |
    2856             : |                                                                             |
    2857             : |  NOTES:                                                                     |
    2858             : |                                                                             |
    2859             : |                                                                             |
    2860             : |   Date     Programmer   Description                                         |
    2861             : |  ======   ============  =================================================   |
    2862             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2863             : |                                                                             |
    2864             : |  END_PROLOG                                                                 |
    2865             : -----------------------------------------------------------------------------*/
    2866             : intn
    2867           0 : SWdefdatafield(int32 swathID, const char *fieldname, const char *dimlist,
    2868             :          int32 numbertype, int32 merge)
    2869             : {
    2870             :     intn            status; /* routine return status variable */
    2871             : 
    2872             :     /* Call SWdefinefield routine */
    2873             :     /* -------------------------- */
    2874           0 :     status = SWdefinefield(swathID, "Data Fields", fieldname, dimlist,
    2875             :          numbertype, merge);
    2876             : 
    2877           0 :     return (status);
    2878             : }
    2879             : 
    2880             : 
    2881             : 
    2882             : 
    2883             : 
    2884             : /*----------------------------------------------------------------------------|
    2885             : |  BEGIN_PROLOG                                                               |
    2886             : |                                                                             |
    2887             : |  FUNCTION: SWwritegeometa                                                   |
    2888             : |                                                                             |
    2889             : |  DESCRIPTION: Defines structural metadata for pre-existing geolocation      |
    2890             : |               field within swath structure                                  |
    2891             : |                                                                             |
    2892             : |                                                                             |
    2893             : |  Return Value    Type     Units     Description                             |
    2894             : |  ============   ======  =========   =====================================   |
    2895             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2896             : |                                                                             |
    2897             : |  INPUTS:                                                                    |
    2898             : |  swathID        int32               swath structure ID                      |
    2899             : |  fieldname      char                fieldname                               |
    2900             : |  dimlist        char                Dimension list (comma-separated list)   |
    2901             : |  numbertype     int32               field type                              |
    2902             : |                                                                             |
    2903             : |                                                                             |
    2904             : |  OUTPUTS:                                                                   |
    2905             : |             None                                                            |
    2906             : |                                                                             |
    2907             : |  NOTES:                                                                     |
    2908             : |                                                                             |
    2909             : |                                                                             |
    2910             : |   Date     Programmer   Description                                         |
    2911             : |  ======   ============  =================================================   |
    2912             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2913             : |                                                                             |
    2914             : |  END_PROLOG                                                                 |
    2915             : -----------------------------------------------------------------------------*/
    2916             : intn
    2917           0 : SWwritegeometa(int32 swathID, const char *fieldname, const char *dimlist,
    2918             :          int32 numbertype)
    2919             : {
    2920           0 :     intn            status = 0; /* routine return status variable */
    2921             : 
    2922             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    2923             :     int32           dum;  /* dummy variable */
    2924           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2925             : 
    2926             :     char            utlbuf[256];/* Utility buffer */
    2927             :     char            swathname[80];  /* Swath name */
    2928             : 
    2929           0 :     status = SWchkswid(swathID, "SWwritegeometa", &dum, &sdInterfaceID,
    2930             :            &dum);
    2931             : 
    2932           0 :     if (status == 0)
    2933             :     {
    2934             :   /* Setup and write field metadata */
    2935             :   /* ------------------------------ */
    2936           0 :   snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist);
    2937             : 
    2938           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    2939           0 :   status = EHinsertmeta(sdInterfaceID, swathname, "s", 3L,
    2940             :             utlbuf, &numbertype);
    2941             :     }
    2942             : 
    2943           0 :     return (status);
    2944             : }
    2945             : 
    2946             : 
    2947             : 
    2948             : 
    2949             : /*----------------------------------------------------------------------------|
    2950             : |  BEGIN_PROLOG                                                               |
    2951             : |                                                                             |
    2952             : |  FUNCTION: SWwritedatameta                                                  |
    2953             : |                                                                             |
    2954             : |  DESCRIPTION: Defines structural metadata for pre-existing data             |
    2955             : |               field within swath structure                                  |
    2956             : |                                                                             |
    2957             : |                                                                             |
    2958             : |  Return Value    Type     Units     Description                             |
    2959             : |  ============   ======  =========   =====================================   |
    2960             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2961             : |                                                                             |
    2962             : |  INPUTS:                                                                    |
    2963             : |  swathID        int32               swath structure ID                      |
    2964             : |  fieldname      char                fieldname                               |
    2965             : |  dimlist        char                Dimension list (comma-separated list)   |
    2966             : |  numbertype     int32               field type                              |
    2967             : |                                                                             |
    2968             : |                                                                             |
    2969             : |  OUTPUTS:                                                                   |
    2970             : |             None                                                            |
    2971             : |                                                                             |
    2972             : |  NOTES:                                                                     |
    2973             : |                                                                             |
    2974             : |                                                                             |
    2975             : |   Date     Programmer   Description                                         |
    2976             : |  ======   ============  =================================================   |
    2977             : |  Jun 96   Joel Gales    Original Programmer                                 |
    2978             : |                                                                             |
    2979             : |  END_PROLOG                                                                 |
    2980             : -----------------------------------------------------------------------------*/
    2981             : intn
    2982           0 : SWwritedatameta(int32 swathID, const char *fieldname, const char *dimlist,
    2983             :     int32 numbertype)
    2984             : {
    2985           0 :     intn            status = 0; /* routine return status variable */
    2986             : 
    2987             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    2988             :     int32           dum;  /* dummy variable */
    2989           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    2990             : 
    2991             :     char            utlbuf[256];/* Utility buffer */
    2992             :     char            swathname[80];  /* Swath name */
    2993             : 
    2994           0 :     status = SWchkswid(swathID, "SWwritedatameta", &dum, &sdInterfaceID,
    2995             :            &dum);
    2996             : 
    2997           0 :     if (status == 0)
    2998             :     {
    2999             :   /* Setup and write field metadata */
    3000             :   /* ------------------------------ */
    3001           0 :   snprintf(utlbuf, sizeof(utlbuf), "%s%s%s", fieldname, ":", dimlist);
    3002             : 
    3003           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    3004           0 :   status = EHinsertmeta(sdInterfaceID, swathname, "s", 4L,
    3005             :             utlbuf, &numbertype);
    3006             :     }
    3007           0 :     return (status);
    3008             : }
    3009             : 
    3010             : 
    3011             : 
    3012             : /*----------------------------------------------------------------------------|
    3013             : |  BEGIN_PROLOG                                                               |
    3014             : |                                                                             |
    3015             : |  FUNCTION: SWwrrdattr                                                       |
    3016             : |                                                                             |
    3017             : |  DESCRIPTION:                                                               |
    3018             : |                                                                             |
    3019             : |                                                                             |
    3020             : |  Return Value    Type     Units     Description                             |
    3021             : |  ============   ======  =========   =====================================   |
    3022             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3023             : |                                                                             |
    3024             : |  INPUTS:                                                                    |
    3025             : |  swathID        int32               swath structure ID                      |
    3026             : |  attrname       char                attribute name                          |
    3027             : |  numbertype     int32               attribute HDF numbertype                |
    3028             : |  count          int32               Number of attribute elements            |
    3029             : |  wrcode         char                Read/Write Code "w/r"                   |
    3030             : |  datbuf         void                I/O buffer                              |
    3031             : |                                                                             |
    3032             : |  OUTPUTS:                                                                   |
    3033             : |  datbuf                                                                     |
    3034             : |                                                                             |
    3035             : |  NOTES:                                                                     |
    3036             : |                                                                             |
    3037             : |                                                                             |
    3038             : |   Date     Programmer   Description                                         |
    3039             : |  ======   ============  =================================================   |
    3040             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3041             : |  Oct 96   Joel Gales    Get Attribute Vgroup ID from external array         |
    3042             : |                                                                             |
    3043             : |  END_PROLOG                                                                 |
    3044             : -----------------------------------------------------------------------------*/
    3045             : static intn
    3046           0 : SWwrrdattr(int32 swathID, const char *attrname, int32 numbertype, int32 count,
    3047             :      const char *wrcode, VOIDP datbuf)
    3048             : 
    3049             : {
    3050             :     intn            status; /* routine return status variable */
    3051             : 
    3052             :     int32           fid;  /* HDF-EOS file ID */
    3053             :     int32           attrVgrpID; /* Swath attribute ID */
    3054             :     int32           dum;  /* dummy variable */
    3055           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3056             : 
    3057             :     /* Check Swath id */
    3058           0 :     status = SWchkswid(swathID, "SWwrrdattr", &fid, &dum, &dum);
    3059             : 
    3060           0 :     if (status == 0)
    3061             :     {
    3062             :   /* Get attribute Vgroup ID and call EHattr to perform I/O */
    3063             :   /* ------------------------------------------------------ */
    3064           0 :   attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
    3065           0 :   status = EHattr(fid, attrVgrpID, attrname, numbertype, count,
    3066             :       wrcode, datbuf);
    3067             :     }
    3068           0 :     return (status);
    3069             : }
    3070             : 
    3071             : 
    3072             : 
    3073             : /*----------------------------------------------------------------------------|
    3074             : |  BEGIN_PROLOG                                                               |
    3075             : |                                                                             |
    3076             : |  FUNCTION: SWwriteattr                                                      |
    3077             : |                                                                             |
    3078             : |  DESCRIPTION: Writes/updates attribute in a swath.                          |
    3079             : |                                                                             |
    3080             : |                                                                             |
    3081             : |  Return Value    Type     Units     Description                             |
    3082             : |  ============   ======  =========   =====================================   |
    3083             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3084             : |                                                                             |
    3085             : |  INPUTS:                                                                    |
    3086             : |  swathID        int32               swath structure ID                      |
    3087             : |  attrname       char                attribute name                          |
    3088             : |  numbertype     int32               attribute HDF numbertype                |
    3089             : |  count          int32               Number of attribute elements            |
    3090             : |  datbuf         void                I/O buffer                              |
    3091             : |                                                                             |
    3092             : |  OUTPUTS:                                                                   |
    3093             : |             None                                                            |
    3094             : |                                                                             |
    3095             : |  NOTES:                                                                     |
    3096             : |                                                                             |
    3097             : |                                                                             |
    3098             : |   Date     Programmer   Description                                         |
    3099             : |  ======   ============  =================================================   |
    3100             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3101             : |                                                                             |
    3102             : |  END_PROLOG                                                                 |
    3103             : -----------------------------------------------------------------------------*/
    3104             : intn
    3105           0 : SWwriteattr(int32 swathID, const char *attrname, int32 numbertype, int32 count,
    3106             :       VOIDP datbuf)
    3107             : {
    3108           0 :     intn            status = 0; /* routine return status variable */
    3109             : 
    3110             :     /* Call SWwrrdattr routine to write attribute */
    3111             :     /* ------------------------------------------ */
    3112           0 :     status = SWwrrdattr(swathID, attrname, numbertype, count, "w", datbuf);
    3113             : 
    3114           0 :     return (status);
    3115             : }
    3116             : 
    3117             : 
    3118             : 
    3119             : /*----------------------------------------------------------------------------|
    3120             : |  BEGIN_PROLOG                                                               |
    3121             : |                                                                             |
    3122             : |  FUNCTION: SWreadattr                                                       |
    3123             : |                                                                             |
    3124             : |  DESCRIPTION: Reads attribute from a swath.                                 |
    3125             : |                                                                             |
    3126             : |                                                                             |
    3127             : |  Return Value    Type     Units     Description                             |
    3128             : |  ============   ======  =========   =====================================   |
    3129             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3130             : |                                                                             |
    3131             : |  INPUTS:                                                                    |
    3132             : |  swathID        int32               swath structure ID                      |
    3133             : |  attrname       char                attribute name                          |
    3134             : |                                                                             |
    3135             : |  OUTPUTS:                                                                   |
    3136             : |  datbuf         void                I/O buffer                              |
    3137             : |                                                                             |
    3138             : |  NOTES:                                                                     |
    3139             : |                                                                             |
    3140             : |                                                                             |
    3141             : |   Date     Programmer   Description                                         |
    3142             : |  ======   ============  =================================================   |
    3143             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3144             : |                                                                             |
    3145             : |  END_PROLOG                                                                 |
    3146             : -----------------------------------------------------------------------------*/
    3147             : intn
    3148           0 : SWreadattr(int32 swathID, const char *attrname, VOIDP datbuf)
    3149             : {
    3150           0 :     intn            status = 0; /* routine return status variable */
    3151           0 :     int32           dum = 0;  /* dummy variable */
    3152             : 
    3153             :     /* Call SWwrrdattr routine to read attribute */
    3154             :     /* ----------------------------------------- */
    3155           0 :     status = SWwrrdattr(swathID, attrname, dum, dum, "r", datbuf);
    3156             : 
    3157           0 :     return (status);
    3158             : }
    3159             : 
    3160             : 
    3161             : 
    3162             : 
    3163             : 
    3164             : /*----------------------------------------------------------------------------|
    3165             : |  BEGIN_PROLOG                                                               |
    3166             : |                                                                             |
    3167             : |  FUNCTION: SWattrinfo                                                       |
    3168             : |                                                                             |
    3169             : |  DESCRIPTION:                                                               |
    3170             : |                                                                             |
    3171             : |                                                                             |
    3172             : |  Return Value    Type     Units     Description                             |
    3173             : |  ============   ======  =========   =====================================   |
    3174             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3175             : |                                                                             |
    3176             : |  INPUTS:                                                                    |
    3177             : |  swathID        int32               swath structure ID                      |
    3178             : |  attrname       char                attribute name                          |
    3179             : |                                                                             |
    3180             : |  OUTPUTS:                                                                   |
    3181             : |  numbertype     int32               attribute HDF numbertype                |
    3182             : |  count          int32               Number of attribute elements            |
    3183             : |                                                                             |
    3184             : |                                                                             |
    3185             : |  OUTPUTS:                                                                   |
    3186             : |             None                                                            |
    3187             : |                                                                             |
    3188             : |  NOTES:                                                                     |
    3189             : |                                                                             |
    3190             : |                                                                             |
    3191             : |   Date     Programmer   Description                                         |
    3192             : |  ======   ============  =================================================   |
    3193             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3194             : |  Oct 96   Joel Gales    Get Attribute Vgroup ID from external array         |
    3195             : |                                                                             |
    3196             : |  END_PROLOG                                                                 |
    3197             : -----------------------------------------------------------------------------*/
    3198             : intn
    3199           0 : SWattrinfo(int32 swathID, const char *attrname, int32 * numbertype, int32 * count)
    3200             : {
    3201           0 :     intn            status = 0; /* routine return status variable */
    3202             : 
    3203             :     int32           fid;  /* HDF-EOS file ID */
    3204             :     int32           attrVgrpID; /* Swath attribute ID */
    3205             :     int32           dum;  /* dummy variable */
    3206           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3207             : 
    3208             :     /* Check for valid swath ID */
    3209             :     /* ------------------------ */
    3210           0 :     status = SWchkswid(swathID, "SWattrinfo", &fid, &dum, &dum);
    3211             : 
    3212           0 :     if (status == 0)
    3213             :     {
    3214             :   /* Get attribute Vgroup ID and call EHattrinfo */
    3215             :   /* ------------------------------------------- */
    3216           0 :   attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
    3217             : 
    3218           0 :   status = EHattrinfo(fid, attrVgrpID, attrname, numbertype,
    3219             :           count);
    3220             :     }
    3221           0 :     return (status);
    3222             : }
    3223             : 
    3224             : 
    3225             : 
    3226             : 
    3227             : 
    3228             : 
    3229             : /*----------------------------------------------------------------------------|
    3230             : |  BEGIN_PROLOG                                                               |
    3231             : |                                                                             |
    3232             : |  FUNCTION: SWinqattrs                                                       |
    3233             : |                                                                             |
    3234             : |  DESCRIPTION:                                                               |
    3235             : |                                                                             |
    3236             : |                                                                             |
    3237             : |  Return Value    Type     Units     Description                             |
    3238             : |  ============   ======  =========   =====================================   |
    3239             : |  nattr          int32               Number of attributes in swath struct    |
    3240             : |                                                                             |
    3241             : |  INPUTS:                                                                    |
    3242             : |  swath ID       int32               swath structure ID                      |
    3243             : |                                                                             |
    3244             : |  OUTPUTS:                                                                   |
    3245             : |  attrnames      char                Attribute names in swath struct         |
    3246             : |                                     (Comma-separated list)                  |
    3247             : |  strbufsize     int32               Attributes name list string length      |
    3248             : |                                                                             |
    3249             : |  OUTPUTS:                                                                   |
    3250             : |             None                                                            |
    3251             : |                                                                             |
    3252             : |  NOTES:                                                                     |
    3253             : |                                                                             |
    3254             : |                                                                             |
    3255             : |   Date     Programmer   Description                                         |
    3256             : |  ======   ============  =================================================   |
    3257             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3258             : |  Oct 96   Joel Gales    Initialize nattr                                    |
    3259             : |  Oct 96   Joel Gales    Get Attribute Vgroup ID from external array         |
    3260             : |                                                                             |
    3261             : |  END_PROLOG                                                                 |
    3262             : -----------------------------------------------------------------------------*/
    3263             : int32
    3264           0 : SWinqattrs(int32 swathID, char *attrnames, int32 * strbufsize)
    3265             : {
    3266             :     intn            status; /* routine return status variable */
    3267             : 
    3268             :     int32           fid;  /* HDF-EOS file ID */
    3269             :     int32           attrVgrpID; /* Swath attribute ID */
    3270             :     int32           dum;  /* dummy variable */
    3271           0 :     int32           nattr = 0;  /* Number of attributes */
    3272           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3273             : 
    3274             :     /* Check Swath id */
    3275           0 :     status = SWchkswid(swathID, "SWinqattrs", &fid, &dum, &dum);
    3276             : 
    3277           0 :     if (status == 0)
    3278             :     {
    3279             :   /* Get attribute Vgroup ID and call EHattrcat */
    3280             :   /* ------------------------------------------ */
    3281           0 :   attrVgrpID = SWXSwath[swathID % idOffset].VIDTable[2];
    3282             : 
    3283           0 :   nattr = EHattrcat(fid, attrVgrpID, attrnames, strbufsize);
    3284             :     }
    3285             : 
    3286           0 :     return (nattr);
    3287             : }
    3288             : 
    3289             : #define REMQUOTE \
    3290             : \
    3291             : memmove(utlstr, utlstr + 1, strlen(utlstr) - 2); \
    3292             : utlstr[strlen(utlstr) - 2] = 0;
    3293             : 
    3294             : 
    3295             : /*----------------------------------------------------------------------------|
    3296             : |  BEGIN_PROLOG                                                               |
    3297             : |                                                                             |
    3298             : |  FUNCTION: SWinqdims                                                        |
    3299             : |                                                                             |
    3300             : |  DESCRIPTION: Returns dimension names and values defined in swath structure |
    3301             : |                                                                             |
    3302             : |                                                                             |
    3303             : |  Return Value    Type     Units     Description                             |
    3304             : |  ============   ======  =========   =====================================   |
    3305             : |  nDim           int32               Number of defined dimensions            |
    3306             : |                                                                             |
    3307             : |  INPUTS:                                                                    |
    3308             : |  swathID        int32               swath structure ID                      |
    3309             : |                                                                             |
    3310             : |  OUTPUTS:                                                                   |
    3311             : |  dimnames       char                Dimension names (comma-separated)       |
    3312             : |  dims           int32               Dimension values                        |
    3313             : |                                                                             |
    3314             : |  NOTES:                                                                     |
    3315             : |                                                                             |
    3316             : |                                                                             |
    3317             : |   Date     Programmer   Description                                         |
    3318             : |  ======   ============  =================================================   |
    3319             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3320             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    3321             : |  Feb 97   Joel Gales    Set nDim to -1 if status = -1                       |
    3322             : |                                                                             |
    3323             : |  END_PROLOG                                                                 |
    3324             : -----------------------------------------------------------------------------*/
    3325             : int32
    3326           0 : SWinqdims(int32 swathID, char *dimnames, int32 dims[])
    3327             : 
    3328             : {
    3329             :     intn            status; /* routine return status variable */
    3330             : 
    3331             :     int32           fid;  /* HDF-EOS file ID */
    3332             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    3333             :     int32           swVgrpID; /* Swath root Vgroup ID */
    3334             :     int32           size; /* Dimension size */
    3335           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3336           0 :     int32           nDim = 0; /* Number of dimensions */
    3337             : 
    3338             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    3339             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    3340             :     char            swathname[80];  /* Swath Name */
    3341             :     char           *utlstr;     /* Utility string */
    3342             : 
    3343             : 
    3344             :     /* Allocate space for utility string */
    3345             :     /* --------------------------------- */
    3346           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    3347           0 :     if(utlstr == NULL)
    3348             :     { 
    3349           0 :   HEpush(DFE_NOSPACE,"SWinqdims", __FILE__, __LINE__);
    3350           0 :   return(-1);
    3351             :     }
    3352             : 
    3353             :     /* Check for valid swath id */
    3354           0 :     status = SWchkswid(swathID, "SWinqdims", &fid, &sdInterfaceID, &swVgrpID);
    3355             : 
    3356           0 :     if (status == 0)
    3357             :     {
    3358             :   /* If dimension names or sizes are desired ... */
    3359             :   /* ------------------------------------------- */
    3360           0 :   if (dimnames != NULL || dims != NULL)
    3361             :   {
    3362             :       /* Get swath name */
    3363           0 :       Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    3364             : 
    3365             :       /* Get pointers to "Dimension" section within SM */
    3366           0 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    3367             :              "Dimension", metaptrs);
    3368           0 :       if(metabuf == NULL)
    3369             :       {
    3370           0 :     free(utlstr);
    3371           0 :     return(-1);
    3372             :       }
    3373             :       /* If dimension names are desired then "clear" name buffer */
    3374           0 :       if (dimnames != NULL)
    3375             :       {
    3376           0 :     dimnames[0] = 0;
    3377             :       }
    3378             : 
    3379             : 
    3380             :       /* Begin loop through dimension entries in metadata */
    3381             :       /* ------------------------------------------------ */
    3382             :       while (1)
    3383             :       {
    3384             :     /* Search for OBJECT string */
    3385           0 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    3386             : 
    3387             :     /* If found within "Dimension" metadata section ... */
    3388           0 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    3389             :     {
    3390             :         /* Get Dimension Name (if desired) */
    3391           0 :         if (dimnames != NULL)
    3392             :         {
    3393             :       /* Check 1st for old meta data then new */
    3394             :       /* ------------------------------------ */
    3395           0 :       EHgetmetavalue(metaptrs, "OBJECT", utlstr);
    3396             : 
    3397             :       /*
    3398             :        * If OBJECT value begins with double quote then old
    3399             :        * metadata, dimension name is OBJECT value.
    3400             :        * Otherwise search for "DimensionName" string
    3401             :        */
    3402           0 :       if (utlstr[0] != '"')
    3403             :       {
    3404           0 :           metaptrs[0] =
    3405           0 :         strstr(metaptrs[0], "\t\t\t\tDimensionName=");
    3406           0 :           EHgetmetavalue(metaptrs, "DimensionName", utlstr);
    3407             :       }
    3408             : 
    3409             :       /* Strip off double quotes */
    3410             :       /* ----------------------- */
    3411           0 :       REMQUOTE
    3412             : 
    3413             :       /* If not first name then add comma delimiter */
    3414           0 :           if (nDim > 0)
    3415             :       {
    3416           0 :           strcat(dimnames, ",");
    3417             :       }
    3418             :       /* Add dimension name to dimension list */
    3419           0 :       strcat(dimnames, utlstr);
    3420             :         }
    3421             : 
    3422             :         /* Get Dimension Size (if desired) */
    3423           0 :         if (dims != NULL)
    3424             :         {
    3425           0 :       EHgetmetavalue(metaptrs, "Size", utlstr);
    3426           0 :       size = atoi(utlstr);
    3427           0 :       dims[nDim] = size;
    3428             :         }
    3429             :         /* Increment number of dimensions */
    3430           0 :         nDim++;
    3431             :     }
    3432             :     else
    3433             :         /* No more dimensions found */
    3434             :     {
    3435             :         break;
    3436             :     }
    3437             :       }
    3438           0 :       free(metabuf);
    3439             :   }
    3440             :     }
    3441             : 
    3442             : 
    3443             :     /* Set nDim to -1 if error status exists */
    3444             :     /* ------------------------------------- */
    3445           0 :     if (status == -1)
    3446             :     {
    3447           0 :   nDim = -1;
    3448             :     }
    3449           0 :     free(utlstr);
    3450             : 
    3451           0 :     return (nDim);
    3452             : }
    3453             : 
    3454             : 
    3455             : 
    3456             : 
    3457             : 
    3458             : /*----------------------------------------------------------------------------|
    3459             : |  BEGIN_PROLOG                                                               |
    3460             : |                                                                             |
    3461             : |  FUNCTION: SWinqmaps                                                        |
    3462             : |                                                                             |
    3463             : |  DESCRIPTION: Returns dimension mappings and offsets and increments         |
    3464             : |                                                                             |
    3465             : |                                                                             |
    3466             : |  Return Value    Type     Units     Description                             |
    3467             : |  ============   ======  =========   =====================================   |
    3468             : |  nMap           int32               Number of dimension mappings            |
    3469             : |                                                                             |
    3470             : |  INPUTS:                                                                    |
    3471             : |  swathID        int32               swath structure ID                      |
    3472             : |                                                                             |
    3473             : |  OUTPUTS:                                                                   |
    3474             : |  dimmaps        char                dimension mappings (comma-separated)    |
    3475             : |  offset         int32               array of offsets                        |
    3476             : |  increment      int32               array of increments                     |
    3477             : |                                                                             |
    3478             : |  NOTES:                                                                     |
    3479             : |                                                                             |
    3480             : |                                                                             |
    3481             : |   Date     Programmer   Description                                         |
    3482             : |  ======   ============  =================================================   |
    3483             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3484             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    3485             : |  Feb 97   Joel Gales    Set nMap to -1 if status = -1                       |
    3486             : |                                                                             |
    3487             : |  END_PROLOG                                                                 |
    3488             : -----------------------------------------------------------------------------*/
    3489             : int32
    3490           0 : SWinqmaps(int32 swathID, char *dimmaps, int32 offset[], int32 increment[])
    3491             : 
    3492             : {
    3493             :     intn            status; /* routine return status variable */
    3494             : 
    3495             :     int32           fid;  /* HDF-EOS file ID */
    3496             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    3497             :     int32           swVgrpID; /* Swath root Vgroup ID */
    3498             :     int32           off;  /* Mapping Offset */
    3499             :     int32           incr; /* Mapping Increment */
    3500           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3501           0 :     int32           nMap = 0; /* Number of mappings */
    3502             : 
    3503             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    3504             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    3505             :     char            swathname[80];  /* Swath Name */
    3506             :     char           *utlstr;     /* Utility string */
    3507             : 
    3508             : 
    3509             :     /* Allocate space for utility string */
    3510             :     /* --------------------------------- */
    3511           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    3512           0 :     if(utlstr == NULL)
    3513             :     { 
    3514           0 :   HEpush(DFE_NOSPACE,"SWinqmaps", __FILE__, __LINE__);
    3515           0 :   return(-1);
    3516             :     }
    3517             : 
    3518             :     /* Check for valid swath id */
    3519           0 :     status = SWchkswid(swathID, "SWinqmaps", &fid, &sdInterfaceID, &swVgrpID);
    3520           0 :     if (status == 0)
    3521             :     {
    3522             :   /* If mapping names or offsets or increments desired ... */
    3523             :   /* ----------------------------------------------------- */
    3524           0 :   if (dimmaps != NULL || offset != NULL || increment != NULL)
    3525             :   {
    3526             : 
    3527             :       /* Get swath name */
    3528           0 :       Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    3529             : 
    3530             :       /* Get pointers to "DimensionMap" section within SM */
    3531           0 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    3532             :              "DimensionMap", metaptrs);
    3533           0 :       if(metabuf == NULL)
    3534             :       {
    3535           0 :     free(utlstr);
    3536           0 :     return(-1);
    3537             :       }
    3538             :       /* If mapping names are desired then "clear" name buffer */
    3539           0 :       if (dimmaps != NULL)
    3540             :       {
    3541           0 :     dimmaps[0] = 0;
    3542             :       }
    3543             : 
    3544             :       /* Begin loop through mapping entries in metadata */
    3545             :       /* ---------------------------------------------- */
    3546             :       while (1)
    3547             :       {
    3548             :     /* Search for OBJECT string */
    3549           0 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    3550             : 
    3551             :     /* If found within "DimensionMap" metadata section ... */
    3552           0 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    3553             :     {
    3554             :         /* Get Geo & Data Dimensions (if desired) */
    3555           0 :         if (dimmaps != NULL)
    3556             :         {
    3557             :       /* Get Geo Dim, remove quotes, add "/" */
    3558           0 :       EHgetmetavalue(metaptrs, "GeoDimension", utlstr);
    3559           0 :       REMQUOTE
    3560           0 :           strcat(utlstr, "/");
    3561             : 
    3562             :       /* If not first map then add comma delimiter. */
    3563           0 :       if (nMap > 0)
    3564             :       {
    3565           0 :           strcat(dimmaps, ",");
    3566             :       }
    3567             : 
    3568             :       /* Add to map list */
    3569           0 :       strcat(dimmaps, utlstr);
    3570             : 
    3571             :       /* Get Data Dim, remove quotes */
    3572           0 :       EHgetmetavalue(metaptrs, "DataDimension", utlstr);
    3573           0 :       REMQUOTE
    3574             : 
    3575             :       /* Add to map list */
    3576           0 :           strcat(dimmaps, utlstr);
    3577             :         }
    3578             : 
    3579             :         /* Get Offset (if desired) */
    3580           0 :         if (offset != NULL)
    3581             :         {
    3582           0 :       EHgetmetavalue(metaptrs, "Offset", utlstr);
    3583           0 :       off = atoi(utlstr);
    3584           0 :       offset[nMap] = off;
    3585             :         }
    3586             : 
    3587             :         /* Get Increment (if desired) */
    3588           0 :         if (increment != NULL)
    3589             :         {
    3590           0 :       EHgetmetavalue(metaptrs, "Increment", utlstr);
    3591           0 :       incr = atoi(utlstr);
    3592           0 :       increment[nMap] = incr;
    3593             :         }
    3594             : 
    3595             :         /* Increment number of maps */
    3596           0 :         nMap++;
    3597             :     }
    3598             :     else
    3599             :         /* No more mappings found */
    3600             :     {
    3601             :         break;
    3602             :     }
    3603             :       }
    3604           0 :       free(metabuf);
    3605             :   }
    3606             :     }
    3607             : 
    3608             : 
    3609             :     /* Set nMap to -1 if error status exists */
    3610             :     /* ------------------------------------- */
    3611           0 :     if (status == -1)
    3612             :     {
    3613           0 :   nMap = -1;
    3614             :     }
    3615           0 :     free(utlstr);
    3616             : 
    3617           0 :     return (nMap);
    3618             : }
    3619             : 
    3620             : 
    3621             : 
    3622             : 
    3623             : 
    3624             : /*----------------------------------------------------------------------------|
    3625             : |  BEGIN_PROLOG                                                               |
    3626             : |                                                                             |
    3627             : |  FUNCTION: SWinqidxmaps                                                     |
    3628             : |                                                                             |
    3629             : |  DESCRIPTION: Returns l_indexed mappings and l_index sizes                      |
    3630             : |                                                                             |
    3631             : |                                                                             |
    3632             : |  Return Value    Type     Units     Description                             |
    3633             : |  ============   ======  =========   =====================================   |
    3634             : |  nMap           int32               Number of l_indexed dimension mappings    |
    3635             : |                                                                             |
    3636             : |  INPUTS:                                                                    |
    3637             : |  swathID        int32               swath structure ID                      |
    3638             : |                                                                             |
    3639             : |  OUTPUTS:                                                                   |
    3640             : |  idxmaps        char                l_indexed dimension mappings              |
    3641             : |                                     (comma-separated)                       |
    3642             : |  idxsizes       int32               Number of elements in each mapping      |
    3643             : |                                                                             |
    3644             : |                                                                             |
    3645             : |  NOTES:                                                                     |
    3646             : |                                                                             |
    3647             : |                                                                             |
    3648             : |   Date     Programmer   Description                                         |
    3649             : |  ======   ============  =================================================   |
    3650             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3651             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    3652             : |  Feb 97   Joel Gales    Set nMap to -1 if status = -1                       |
    3653             : |                                                                             |
    3654             : |  END_PROLOG                                                                 |
    3655             : -----------------------------------------------------------------------------*/
    3656             : int32
    3657           0 : SWinqidxmaps(int32 swathID, char *idxmaps, int32 idxsizes[])
    3658             : 
    3659             : {
    3660             :     intn            status; /* routine return status variable */
    3661             : 
    3662             :     int32           fid;  /* HDF-EOS file ID */
    3663             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    3664             :     int32           swVgrpID; /* Swath root Vgroup ID */
    3665           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3666           0 :     int32           nMap = 0; /* Number of mappings */
    3667             : 
    3668             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    3669             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    3670             :     char            swathname[80];  /* Swath Name */
    3671             :     char           *utlstr;     /* Utility string */
    3672             :     char           *slash;  /* Pointer to slash */
    3673             : 
    3674             : 
    3675             :     /* Allocate space for utility string */
    3676             :     /* --------------------------------- */
    3677           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    3678           0 :     if(utlstr == NULL)
    3679             :     { 
    3680           0 :   HEpush(DFE_NOSPACE,"SWinqidxmaps", __FILE__, __LINE__);
    3681           0 :   return(-1);
    3682             :     }
    3683             :     /* Check for valid swath id */
    3684           0 :     status = SWchkswid(swathID, "SWinqidxmaps", &fid,
    3685             :            &sdInterfaceID, &swVgrpID);
    3686             : 
    3687           0 :     if (status == 0)
    3688             :     {
    3689             :   /* If mapping names or l_index sizes desired ... */
    3690             :   /* ------------------------------------------- */
    3691           0 :   if (idxmaps != NULL || idxsizes != NULL)
    3692             :   {
    3693             :       /* Get swath name */
    3694           0 :       Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    3695             : 
    3696             :       /* Get pointers to "IndexDimensionMap" section within SM */
    3697           0 :       metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    3698             :              "IndexDimensionMap", metaptrs);
    3699           0 :       if(metabuf == NULL)
    3700             :       {
    3701           0 :     free(utlstr);
    3702           0 :     return(-1);
    3703             :       }
    3704             :       /* If mapping names are desired then "clear" name buffer */
    3705           0 :       if (idxmaps != NULL)
    3706             :       {
    3707           0 :     idxmaps[0] = 0;
    3708             :       }
    3709             : 
    3710             :       /* Begin loop through mapping entries in metadata */
    3711             :       /* ---------------------------------------------- */
    3712             :       while (1)
    3713             :       {
    3714             :     /* Search for OBJECT string */
    3715           0 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    3716             : 
    3717             :     /* If found within "IndexDimensionMap" metadata section ... */
    3718           0 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    3719             :     {
    3720             :         /* Get Geo & Data Dimensions and # of indices */
    3721           0 :         if (idxmaps != NULL)
    3722             :         {
    3723             :       /* Get Geo Dim, remove quotes, add "/" */
    3724           0 :       EHgetmetavalue(metaptrs, "GeoDimension", utlstr);
    3725           0 :       REMQUOTE
    3726           0 :           strcat(utlstr, "/");
    3727             : 
    3728             :       /* If not first map then add comma delimiter. */
    3729           0 :       if (nMap > 0)
    3730             :       {
    3731           0 :           strcat(idxmaps, ",");
    3732             :       }
    3733             : 
    3734             :       /* Add to map list */
    3735           0 :       strcat(idxmaps, utlstr);
    3736             : 
    3737             : 
    3738             :       /* Get Index size (if desired) */
    3739           0 :       if (idxsizes != NULL)
    3740             :       {
    3741             :           /* Parse off geo dimension and find its size */
    3742           0 :           slash = strchr(utlstr, '/');
    3743           0 :           *slash = 0;
    3744           0 :           idxsizes[nMap] = SWdiminfo(swathID, utlstr);
    3745             :       }
    3746             : 
    3747             : 
    3748             :       /* Get Data Dim, remove quotes */
    3749           0 :       EHgetmetavalue(metaptrs, "DataDimension", utlstr);
    3750           0 :       REMQUOTE
    3751             : 
    3752             :       /* Add to map list */
    3753           0 :           strcat(idxmaps, utlstr);
    3754             :         }
    3755             : 
    3756             :         /* Increment number of maps */
    3757           0 :         nMap++;
    3758             :     }
    3759             :     else
    3760             :         /* No more mappings found */
    3761             :     {
    3762             :         break;
    3763             :     }
    3764             :       }
    3765           0 :       free(metabuf);
    3766             :   }
    3767             :     }
    3768             : 
    3769             : 
    3770             :     /* Set nMap to -1 if error status exists */
    3771             :     /* ------------------------------------- */
    3772           0 :     if (status == -1)
    3773             :     {
    3774           0 :   nMap = -1;
    3775             :     }
    3776           0 :     free(utlstr);
    3777             : 
    3778           0 :     return (nMap);
    3779             : }
    3780             : 
    3781             : 
    3782             : 
    3783             : 
    3784             : /*----------------------------------------------------------------------------|
    3785             : |  BEGIN_PROLOG                                                               |
    3786             : |                                                                             |
    3787             : |  FUNCTION: SWinqfields                                                      |
    3788             : |                                                                             |
    3789             : |  DESCRIPTION: Returns fieldnames, ranks and numbertypes defined in swath.   |
    3790             : |                                                                             |
    3791             : |                                                                             |
    3792             : |  Return Value    Type     Units     Description                             |
    3793             : |  ============   ======  =========   =====================================   |
    3794             : |  nFld           int32               Number of (geo/data) fields in swath    |
    3795             : |                                                                             |
    3796             : |  INPUTS:                                                                    |
    3797             : |  swathID        int32               swath structure ID                      |
    3798             : |  fieldtype      char                field type (geo or data)                |
    3799             : |                                                                             |
    3800             : |                                                                             |
    3801             : |  OUTPUTS:                                                                   |
    3802             : |  fieldlist      char                Field names (comma-separated)           |
    3803             : |  rank           int32               Array of ranks                          |
    3804             : |  numbertype     int32               Array of HDF number types               |
    3805             : |                                                                             |
    3806             : |  NOTES:                                                                     |
    3807             : |                                                                             |
    3808             : |                                                                             |
    3809             : |   Date     Programmer   Description                                         |
    3810             : |  ======   ============  =================================================   |
    3811             : |  Jun 96   Joel Gales    Original Programmer                                 |
    3812             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    3813             : |  Feb 97   Joel Gales    Set nFld to -1 if status = -1                       |
    3814             : |                                                                             |
    3815             : |  END_PROLOG                                                                 |
    3816             : -----------------------------------------------------------------------------*/
    3817             : static int32
    3818           0 : SWinqfields(int32 swathID, const char *fieldtype, char *fieldlist, int32 rank[],
    3819             :       int32 numbertype[])
    3820             : 
    3821             : {
    3822             :     intn            status; /* routine return status variable */
    3823             : 
    3824             :     int32           fid;  /* HDF-EOS file ID */
    3825             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    3826             :     int32           swVgrpID; /* Swath root Vgroup ID */
    3827           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    3828           0 :     int32           nFld = 0; /* Number of mappings */
    3829             :     int32           slen[8];  /* String length array */
    3830             : 
    3831             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    3832             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    3833             :     char            swathname[80];  /* Swath Name */
    3834             :     char           *utlstr; /* Utility string */
    3835             :     char           *utlstr2;  /* Utility string 2 */
    3836             :     char           *ptr[8]; /* String pointer array */
    3837             : 
    3838             : 
    3839             :     /* Allocate space for utility string */
    3840             :     /* --------------------------------- */
    3841           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    3842           0 :     if(utlstr == NULL)
    3843             :     { 
    3844           0 :   HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__);
    3845           0 :   return(-1);
    3846             :     }
    3847             : 
    3848           0 :     utlstr2 = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    3849           0 :     if(utlstr2 == NULL)
    3850             :     { 
    3851           0 :   HEpush(DFE_NOSPACE,"SWinqfields", __FILE__, __LINE__);
    3852           0 :   free(utlstr);
    3853           0 :   return(-1);
    3854             :     }
    3855             : 
    3856             :     /* Check for valid swath id */
    3857           0 :     status = SWchkswid(swathID, "SWinqfields",
    3858             :            &fid, &sdInterfaceID, &swVgrpID);
    3859             : 
    3860           0 :     if (status == 0)
    3861             :     {
    3862             :   /* If field names, ranks,  or number types desired ... */
    3863             :   /* --------------------------------------------------- */
    3864           0 :   if (fieldlist != NULL || rank != NULL || numbertype != NULL)
    3865             :   {
    3866             :       /* Get swath name */
    3867           0 :       Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    3868             : 
    3869             :       /* Get pointers to "GeoField" or "DataField" section within SM */
    3870           0 :       if (strcmp(fieldtype, "Geolocation Fields") == 0)
    3871             :       {
    3872           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    3873             :                  "GeoField", metaptrs);
    3874           0 :     if(metabuf == NULL)
    3875             :     {
    3876           0 :         free(utlstr);
    3877           0 :         free(utlstr2);
    3878           0 :         return(-1);
    3879             :     }
    3880           0 :     strcpy(utlstr2, "GeoFieldName");
    3881             :       }
    3882             :       else
    3883             :       {
    3884           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    3885             :                  "DataField", metaptrs);
    3886           0 :     if(metabuf == NULL)
    3887             :     {
    3888           0 :         free(utlstr);
    3889           0 :         free(utlstr2);
    3890           0 :         return(-1);
    3891             :     }
    3892           0 :     strcpy(utlstr2, "DataFieldName");
    3893             :       }
    3894             : 
    3895             : 
    3896             :       /* If field names are desired then "clear" name buffer */
    3897           0 :       if (fieldlist != NULL)
    3898             :       {
    3899           0 :     fieldlist[0] = 0;
    3900             :       }
    3901             : 
    3902             : 
    3903             :       /* Begin loop through mapping entries in metadata */
    3904             :       /* ---------------------------------------------- */
    3905             :       while (1)
    3906             :       {
    3907             :     /* Search for OBJECT string */
    3908           0 :     metaptrs[0] = strstr(metaptrs[0], "\t\tOBJECT=");
    3909             : 
    3910             :     /* If found within "Geo" or "Data" Field metadata section .. */
    3911           0 :     if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    3912             :     {
    3913             :         /* Get Fieldnames (if desired) */
    3914           0 :         if (fieldlist != NULL)
    3915             :         {
    3916             :       /* Check 1st for old meta data then new */
    3917             :       /* ------------------------------------ */
    3918           0 :       EHgetmetavalue(metaptrs, "OBJECT", utlstr);
    3919             : 
    3920             :       /*
    3921             :        * If OBJECT value begins with double quote then old
    3922             :        * metadata, field name is OBJECT value. Otherwise
    3923             :        * search for "GeoFieldName" or "DataFieldName"
    3924             :        * string
    3925             :        */
    3926             : 
    3927           0 :       if (utlstr[0] != '"')
    3928             :       {
    3929           0 :           strcpy(utlstr, "\t\t\t\t");
    3930           0 :           strcat(utlstr, utlstr2);
    3931           0 :           strcat(utlstr, "=");
    3932           0 :           metaptrs[0] = strstr(metaptrs[0], utlstr);
    3933           0 :           EHgetmetavalue(metaptrs, utlstr2, utlstr);
    3934             :       }
    3935             : 
    3936             :       /* Strip off double quotes */
    3937             :       /* ----------------------- */
    3938           0 :       REMQUOTE
    3939             : 
    3940             : 
    3941             :       /* Add to fieldlist */
    3942             :       /* ---------------- */
    3943           0 :           if (nFld > 0)
    3944             :       {
    3945           0 :           strcat(fieldlist, ",");
    3946             :       }
    3947           0 :       strcat(fieldlist, utlstr);
    3948             : 
    3949             :         }
    3950             :         /* Get Numbertype */
    3951           0 :         if (numbertype != NULL)
    3952             :         {
    3953           0 :       EHgetmetavalue(metaptrs, "DataType", utlstr);
    3954           0 :       numbertype[nFld] = EHnumstr(utlstr);
    3955             :         }
    3956             :         /*
    3957             :          * Get Rank (if desired) by counting # of dimensions in
    3958             :          * "DimList" string
    3959             :          */
    3960           0 :         if (rank != NULL)
    3961             :         {
    3962           0 :       EHgetmetavalue(metaptrs, "DimList", utlstr);
    3963           0 :       rank[nFld] = EHparsestr(utlstr, ',', ptr, slen);
    3964             :         }
    3965             :         /* Increment number of fields */
    3966           0 :         nFld++;
    3967             :     }
    3968             :     else
    3969             :         /* No more fields found */
    3970             :     {
    3971             :         break;
    3972             :     }
    3973             :       }
    3974           0 :       free(metabuf);
    3975             :   }
    3976             :     }
    3977             : 
    3978             :     /* Set nFld to -1 if error status exists */
    3979             :     /* ------------------------------------- */
    3980           0 :     if (status == -1)
    3981             :     {
    3982           0 :   nFld = -1;
    3983             :     }
    3984             : 
    3985           0 :     free(utlstr);
    3986           0 :     free(utlstr2);
    3987             : 
    3988           0 :     return (nFld);
    3989             : }
    3990             : 
    3991             : 
    3992             : 
    3993             : 
    3994             : /*----------------------------------------------------------------------------|
    3995             : |  BEGIN_PROLOG                                                               |
    3996             : |                                                                             |
    3997             : |  FUNCTION: SWinqgeofields                                                   |
    3998             : |                                                                             |
    3999             : |  DESCRIPTION: Inquires about geo fields in swath                            |
    4000             : |                                                                             |
    4001             : |                                                                             |
    4002             : |  Return Value    Type     Units     Description                             |
    4003             : |  ============   ======  =========   =====================================   |
    4004             : |  nflds          int32               Number of geo fields in swath           |
    4005             : |                                                                             |
    4006             : |  INPUTS:                                                                    |
    4007             : |  swathID        int32               swath structure ID                      |
    4008             : |                                                                             |
    4009             : |  OUTPUTS:                                                                   |
    4010             : |  fieldlist      char                Field names (comma-separated)           |
    4011             : |  rank           int32               Array of ranks                          |
    4012             : |  numbertype     int32               Array of HDF number types               |
    4013             : |                                                                             |
    4014             : |  NOTES:                                                                     |
    4015             : |                                                                             |
    4016             : |                                                                             |
    4017             : |   Date     Programmer   Description                                         |
    4018             : |  ======   ============  =================================================   |
    4019             : |  Jun 96   Joel Gales    Original Programmer                                 |
    4020             : |                                                                             |
    4021             : |  END_PROLOG                                                                 |
    4022             : -----------------------------------------------------------------------------*/
    4023             : int32
    4024           0 : SWinqgeofields(int32 swathID, char *fieldlist, int32 rank[],
    4025             :          int32 numbertype[])
    4026             : {
    4027             : 
    4028             :     int32           nflds;  /* Number of Geolocation fields */
    4029             : 
    4030             :     /* Call "SWinqfields" routine */
    4031             :     /* -------------------------- */
    4032           0 :     nflds = SWinqfields(swathID, "Geolocation Fields", fieldlist, rank,
    4033             :       numbertype);
    4034             : 
    4035           0 :     return (nflds);
    4036             : 
    4037             : }
    4038             : 
    4039             : 
    4040             : 
    4041             : 
    4042             : /*----------------------------------------------------------------------------|
    4043             : |  BEGIN_PROLOG                                                               |
    4044             : |                                                                             |
    4045             : |  FUNCTION: SWinqdatafields                                                  |
    4046             : |                                                                             |
    4047             : |  DESCRIPTION: Inquires about data fields in swath                           |
    4048             : |                                                                             |
    4049             : |                                                                             |
    4050             : |  Return Value    Type     Units     Description                             |
    4051             : |  ============   ======  =========   =====================================   |
    4052             : |  nflds          int32               Number of data fields in swath          |
    4053             : |                                                                             |
    4054             : |  INPUTS:                                                                    |
    4055             : |  swathID        int32               swath structure ID                      |
    4056             : |                                                                             |
    4057             : |  OUTPUTS:                                                                   |
    4058             : |  fieldlist      char                Field names (comma-separated)           |
    4059             : |  rank           int32               Array of ranks                          |
    4060             : |  numbertype     int32               Array of HDF number types               |
    4061             : |                                                                             |
    4062             : |  NOTES:                                                                     |
    4063             : |                                                                             |
    4064             : |                                                                             |
    4065             : |   Date     Programmer   Description                                         |
    4066             : |  ======   ============  =================================================   |
    4067             : |  Jun 96   Joel Gales    Original Programmer                                 |
    4068             : |                                                                             |
    4069             : |  END_PROLOG                                                                 |
    4070             : -----------------------------------------------------------------------------*/
    4071             : int32
    4072           0 : SWinqdatafields(int32 swathID, char *fieldlist, int32 rank[],
    4073             :     int32 numbertype[])
    4074             : {
    4075             : 
    4076             :     int32           nflds;  /* Number of Data fields */
    4077             : 
    4078             :     /* Call "SWinqfields" routine */
    4079             :     /* -------------------------- */
    4080           0 :     nflds = SWinqfields(swathID, "Data Fields", fieldlist, rank,
    4081             :       numbertype);
    4082             : 
    4083           0 :     return (nflds);
    4084             : 
    4085             : }
    4086             : 
    4087             : 
    4088             : 
    4089             : 
    4090             : /*----------------------------------------------------------------------------|
    4091             : |  BEGIN_PROLOG                                                               |
    4092             : |                                                                             |
    4093             : |  FUNCTION: SWnentries                                                       |
    4094             : |                                                                             |
    4095             : |  DESCRIPTION: Returns number of entries and string buffer size              |
    4096             : |                                                                             |
    4097             : |                                                                             |
    4098             : |  Return Value    Type     Units     Description                             |
    4099             : |  ============   ======  =========   =====================================   |
    4100             : |  nEntries       int32               Number of entries                       |
    4101             : |                                                                             |
    4102             : |  INPUTS:                                                                    |
    4103             : |  swathID        int32               swath structure ID                      |
    4104             : |  entrycode      int32               Entry code                              |
    4105             : |                               HDFE_NENTDIM  (0)                       |
    4106             : |                               HDFE_NENTMAP  (1)                       |
    4107             : |                               HDFE_NENTIMAP (2)                       |
    4108             : |                               HDFE_NENTGFLD (3)                       |
    4109             : |                               HDFE_NENTDFLD (4)                       |
    4110             : |                                                                             |
    4111             : |                                                                             |
    4112             : |  OUTPUTS:                                                                   |
    4113             : |  strbufsize     int32               Length of comma-separated list          |
    4114             : |                                     (Does not include null-terminator       |
    4115             : |                                                                             |
    4116             : |  NOTES:                                                                     |
    4117             : |                                                                             |
    4118             : |                                                                             |
    4119             : |   Date     Programmer   Description                                         |
    4120             : |  ======   ============  =================================================   |
    4121             : |  Jun 96   Joel Gales    Original Programmer                                 |
    4122             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    4123             : |  Feb 97   Joel Gales    Set nEntries to -1 if status = -1                   |
    4124             : |                                                                             |
    4125             : |  END_PROLOG                                                                 |
    4126             : -----------------------------------------------------------------------------*/
    4127             : int32
    4128           0 : SWnentries(int32 swathID, int32 entrycode, int32 * strbufsize)
    4129             : 
    4130             : {
    4131             :     intn            status;     /* routine return status variable */
    4132             :     intn            i;        /* Loop index */
    4133             : 
    4134             :     int32           fid;      /* HDF-EOS file ID */
    4135             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    4136             :     int32           swVgrpID;     /* Swath root Vgroup ID */
    4137           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    4138           0 :     int32           nEntries = 0;   /* Number of entries */
    4139             :     int32           metaflag;     /* Old (0), New (1) metadata flag) */
    4140           0 :     int32           nVal = 0;     /* Number of strings to search for */
    4141             : 
    4142           0 :     char           *metabuf = NULL; /* Pointer to structural metadata (SM) */
    4143             :     char           *metaptrs[2];    /* Pointers to begin and end of SM section */
    4144             :     char            swathname[80];  /* Swath Name */
    4145             :     char           *utlstr;     /* Utility string */
    4146             :     char            valName[2][32]; /* Strings to search for */
    4147             : 
    4148             :     /* Allocate space for utility string */
    4149             :     /* --------------------------------- */
    4150           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    4151           0 :     if(utlstr == NULL)
    4152             :     { 
    4153           0 :   HEpush(DFE_NOSPACE,"SWnemtries", __FILE__, __LINE__);
    4154           0 :   return(-1);
    4155             :     }
    4156             :     /* Check for valid swath id */
    4157             :     /* ------------------------ */
    4158           0 :     status = SWchkswid(swathID, "SWnentries", &fid, &sdInterfaceID, &swVgrpID);
    4159             : 
    4160           0 :     if (status == 0)
    4161             :     {
    4162             :   /* Get swath name */
    4163           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
    4164             : 
    4165             :   /* Zero out string buffer size */
    4166           0 :   *strbufsize = 0;
    4167             : 
    4168             : 
    4169             :   /*
    4170             :    * Get pointer to relevant section within SM and Get names of
    4171             :    * metadata strings to inquire about
    4172             :    */
    4173           0 :   switch (entrycode)
    4174             :   {
    4175           0 :   case HDFE_NENTDIM:
    4176             :       /* Dimensions */
    4177             :       {
    4178           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    4179             :                  "Dimension", metaptrs);
    4180           0 :     if(metabuf == NULL)
    4181             :     {
    4182           0 :         free(utlstr);
    4183           0 :         return(-1);
    4184             :     }
    4185           0 :     nVal = 1;
    4186           0 :     strcpy(&valName[0][0], "DimensionName");
    4187             :       }
    4188           0 :       break;
    4189             : 
    4190           0 :   case HDFE_NENTMAP:
    4191             :       /* Dimension Maps */
    4192             :       {
    4193           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    4194             :                  "DimensionMap", metaptrs);
    4195           0 :     if(metabuf == NULL)
    4196             :     {
    4197           0 :         free(utlstr);
    4198           0 :         return(-1);
    4199             :     }
    4200           0 :     nVal = 2;
    4201           0 :     strcpy(&valName[0][0], "GeoDimension");
    4202           0 :     strcpy(&valName[1][0], "DataDimension");
    4203             :       }
    4204           0 :       break;
    4205             : 
    4206           0 :   case HDFE_NENTIMAP:
    4207             :       /* Indexed Dimension Maps */
    4208             :       {
    4209           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    4210             :                "IndexDimensionMap", metaptrs);
    4211           0 :     if(metabuf == NULL)
    4212             :     {
    4213           0 :         free(utlstr);
    4214           0 :         return(-1);
    4215             :     }
    4216           0 :     nVal = 2;
    4217           0 :     strcpy(&valName[0][0], "GeoDimension");
    4218           0 :     strcpy(&valName[1][0], "DataDimension");
    4219             :       }
    4220           0 :       break;
    4221             : 
    4222           0 :   case HDFE_NENTGFLD:
    4223             :       /* Geolocation Fields */
    4224             :       {
    4225           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    4226             :                  "GeoField", metaptrs);
    4227           0 :     if(metabuf == NULL)
    4228             :     {
    4229           0 :         free(utlstr);
    4230           0 :         return(-1);
    4231             :     }
    4232           0 :     nVal = 1;
    4233           0 :     strcpy(&valName[0][0], "GeoFieldName");
    4234             :       }
    4235           0 :       break;
    4236             : 
    4237           0 :   case HDFE_NENTDFLD:
    4238             :       /* Data Fields */
    4239             :       {
    4240           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    4241             :                  "DataField", metaptrs);
    4242           0 :     if(metabuf == NULL)
    4243             :     {
    4244           0 :         free(utlstr);
    4245           0 :         return(-1);
    4246             :     }
    4247           0 :     nVal = 1;
    4248           0 :     strcpy(&valName[0][0], "DataFieldName");
    4249             :       }
    4250           0 :       break;
    4251             :   }
    4252             : 
    4253             : 
    4254             :   /*
    4255             :    * Check for presence of 'GROUP="' string If found then old metadata,
    4256             :    * search on OBJECT string
    4257             :    */
    4258           0 :         if (metabuf)
    4259             :         {
    4260           0 :             metaflag = (strstr(metabuf, "GROUP=\"") == NULL) ? 1 : 0;
    4261           0 :             if (metaflag == 0)
    4262             :             {
    4263           0 :                 nVal = 1;
    4264           0 :                 strcpy(&valName[0][0], "\t\tOBJECT");
    4265             :             }
    4266             : 
    4267             : 
    4268             :             /* Begin loop through entries in metadata */
    4269             :             /* -------------------------------------- */
    4270             :             while (1)
    4271             :             {
    4272             :                 /* Search for first string */
    4273           0 :                 strcpy(utlstr, &valName[0][0]);
    4274           0 :                 strcat(utlstr, "=");
    4275           0 :                 metaptrs[0] = strstr(metaptrs[0], utlstr);
    4276             : 
    4277             :                 /* If found within relevant metadata section ... */
    4278           0 :                 if (metaptrs[0] < metaptrs[1] && metaptrs[0] != NULL)
    4279             :                 {
    4280           0 :                     for (i = 0; i < nVal; i++)
    4281             :                     {
    4282             :                         /*
    4283             :                          * Get all string values Don't count quotes
    4284             :                          */
    4285           0 :                         EHgetmetavalue(metaptrs, &valName[i][0], utlstr);
    4286           0 :                         *strbufsize += (int32)strlen(utlstr) - 2;
    4287             :                     }
    4288             :                     /* Increment number of entries */
    4289           0 :                     nEntries++;
    4290             : 
    4291             :                     /* Go to end of OBJECT */
    4292           0 :                     metaptrs[0] = strstr(metaptrs[0], "END_OBJECT");
    4293             :                 }
    4294             :                 else
    4295             :                     /* No more entries found */
    4296             :                 {
    4297             :                     break;
    4298             :                 }
    4299             :             }
    4300           0 :             free(metabuf);
    4301             :         }
    4302             : 
    4303             : 
    4304             :   /* Count comma separators & slashes (if mappings) */
    4305             :   /* ---------------------------------------------- */
    4306           0 :   if (nEntries > 0)
    4307             :   {
    4308           0 :       *strbufsize += nEntries - 1;
    4309           0 :       *strbufsize += (nVal - 1) * nEntries;
    4310             :   }
    4311             :     }
    4312             : 
    4313             : 
    4314             :     /* Set nEntries to -1 if error status exists */
    4315             :     /* ----------------------------------------- */
    4316           0 :     if (status == -1)
    4317           0 :   nEntries = -1;
    4318             : 
    4319           0 :     free(utlstr);
    4320             : 
    4321           0 :     return (nEntries);
    4322             : }
    4323             : 
    4324             : 
    4325             : 
    4326             : 
    4327             : 
    4328             : /*----------------------------------------------------------------------------|
    4329             : |  BEGIN_PROLOG                                                               |
    4330             : |                                                                             |
    4331             : |  FUNCTION: SWinqswath                                                       |
    4332             : |                                                                             |
    4333             : |  DESCRIPTION: Returns number and names of swath structures in file          |
    4334             : |                                                                             |
    4335             : |                                                                             |
    4336             : |  Return Value    Type     Units     Description                             |
    4337             : |  ============   ======  =========   =====================================   |
    4338             : |  nSwath         int32               Number of swath structures in file      |
    4339             : |                                                                             |
    4340             : |  INPUTS:                                                                    |
    4341             : |  filename       char                HDF-EOS filename                        |
    4342             : |                                                                             |
    4343             : |  OUTPUTS:                                                                   |
    4344             : |  swathlist      char                List of swath names (comma-separated)   |
    4345             : |  strbufsize     int32               Length of swathlist                     |
    4346             : |                                                                             |
    4347             : |  NOTES:                                                                     |
    4348             : |                                                                             |
    4349             : |                                                                             |
    4350             : |   Date     Programmer   Description                                         |
    4351             : |  ======   ============  =================================================   |
    4352             : |  Jun 96   Joel Gales    Original Programmer                                 |
    4353             : |                                                                             |
    4354             : |  END_PROLOG                                                                 |
    4355             : -----------------------------------------------------------------------------*/
    4356             : int32
    4357           4 : SWinqswath(const char *filename, char *swathlist, int32 * strbufsize)
    4358             : {
    4359             :     int32           nSwath; /* Number of swath structures in file */
    4360             : 
    4361             :     /* Call "EHinquire" routine */
    4362             :     /* ------------------------ */
    4363           4 :     nSwath = EHinquire(filename, "SWATH", swathlist, strbufsize);
    4364             : 
    4365           4 :     return (nSwath);
    4366             : }
    4367             : 
    4368             : 
    4369             : 
    4370             : /*----------------------------------------------------------------------------|
    4371             : |  BEGIN_PROLOG                                                               |
    4372             : |                                                                             |
    4373             : |  FUNCTION: SW1dfldsrch                                                      |
    4374             : |                                                                             |
    4375             : |  DESCRIPTION: Retrieves information about a 1D field                        |
    4376             : |                                                                             |
    4377             : |                                                                             |
    4378             : |  Return Value    Type     Units     Description                             |
    4379             : |  ============   ======  =========   =====================================   |
    4380             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    4381             : |                                                                             |
    4382             : |  INPUTS:                                                                    |
    4383             : |  fid            int32               HDF-EOS file ID                         |
    4384             : |  swathID        int32               swath structure ID                      |
    4385             : |  fieldname      const char          field name                              |
    4386             : |  access         const char          Access code (w/r)                       |
    4387             : |                                                                             |
    4388             : |                                                                             |
    4389             : |  OUTPUTS:                                                                   |
    4390             : |  vgidout        int32               Field (geo/data) vgroup ID              |
    4391             : |  vdataIDout     int32               Field Vdata ID                          |
    4392             : |  fldtype        int32               Field type                              |
    4393             : |                                                                             |
    4394             : |  NOTES:                                                                     |
    4395             : |                                                                             |
    4396             : |                                                                             |
    4397             : |   Date     Programmer   Description                                         |
    4398             : |  ======   ============  =================================================   |
    4399             : |  Jun 96   Joel Gales    Original Programmer                                 |
    4400             : |                                                                             |
    4401             : |  END_PROLOG                                                                 |
    4402             : -----------------------------------------------------------------------------*/
    4403             : static intn
    4404           0 : SW1dfldsrch(int32 fid, int32 swathID, const char *fieldname, const char *i_access,
    4405             :       int32 * vgidout, int32 * vdataIDout, int32 * fldtype)
    4406             : 
    4407             : {
    4408           0 :     intn            status = 0; /* routine return status variable */
    4409             : 
    4410             :     int32           sID;  /* SwathID - offset */
    4411             :     int32           vgid; /* Swath Geo or Data Vgroup ID */
    4412             :     int32           vdataID;  /* 1d field vdata */
    4413           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    4414             : 
    4415             : 
    4416             :     /* Compute "reduced" swath ID */
    4417             :     /* -------------------------- */
    4418           0 :     sID = swathID % idOffset;
    4419             : 
    4420             : 
    4421             :     /* Get Geolocation Vgroup id and 1D field name Vdata id */
    4422             :     /* ---------------------------------------------------- */
    4423           0 :     vgid = SWXSwath[sID].VIDTable[0];
    4424           0 :     vdataID = EHgetid(fid, vgid, fieldname, 1, i_access);
    4425           0 :     *fldtype = 0;
    4426             : 
    4427             : 
    4428             :     /*
    4429             :      * If name not found in Geolocation Vgroup then detach Geolocation Vgroup
    4430             :      * and search in Data Vgroup
    4431             :      */
    4432           0 :     if (vdataID == -1)
    4433             :     {
    4434           0 :   vgid = SWXSwath[sID].VIDTable[1];;
    4435           0 :   vdataID = EHgetid(fid, vgid, fieldname, 1, i_access);
    4436           0 :   *fldtype = 1;
    4437             : 
    4438             :   /* If field also not found in Data Vgroup then set error status */
    4439             :   /* ------------------------------------------------------------ */
    4440           0 :   if (vdataID == -1)
    4441             :   {
    4442           0 :       status = -1;
    4443           0 :       vgid = -1;
    4444           0 :       vdataID = -1;
    4445             :   }
    4446             :     }
    4447           0 :     *vgidout = vgid;
    4448           0 :     *vdataIDout = vdataID;
    4449             : 
    4450           0 :     return (status);
    4451             : }
    4452             : 
    4453             : 
    4454             : 
    4455             : 
    4456             : 
    4457             : /*----------------------------------------------------------------------------|
    4458             : |  BEGIN_PROLOG                                                               |
    4459             : |                                                                             |
    4460             : |  FUNCTION: SWSDfldsrch                                                      |
    4461             : |                                                                             |
    4462             : |  DESCRIPTION: Retrieves information SDS field                               |
    4463             : |                                                                             |
    4464             : |                                                                             |
    4465             : |  Return Value    Type     Units     Description                             |
    4466             : |  ============   ======  =========   =====================================   |
    4467             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    4468             : |                                                                             |
    4469             : |  INPUTS:                                                                    |
    4470             : |  swathID        int32               swath structure ID                      |
    4471             : |  sdInterfaceID  int32               SD interface ID                         |
    4472             : |  fieldname      const char          field name                              |
    4473             : |                                                                             |
    4474             : |                                                                             |
    4475             : |  OUTPUTS:                                                                   |
    4476             : |  sdid           int32               SD element ID                           |
    4477             : |  rankSDS        int32               Rank of SDS                             |
    4478             : |  rankFld        int32               True rank of field (merging)            |
    4479             : |  offset         int32               Offset of field within merged field     |
    4480             : |  dims           int32               Dimensions of field                     |
    4481             : |  solo           int32               Solo field flag                         |
    4482             : |                                                                             |
    4483             : |  NOTES:                                                                     |
    4484             : |                                                                             |
    4485             : |                                                                             |
    4486             : |   Date     Programmer   Description                                         |
    4487             : |  ======   ============  =================================================   |
    4488             : |  Jun 96   Joel Gales    Original Programmer                                 |
    4489             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    4490             : |                                                                             |
    4491             : |  END_PROLOG                                                                 |
    4492             : -----------------------------------------------------------------------------*/
    4493             : static intn
    4494           0 : SWSDfldsrch(int32 swathID, int32 sdInterfaceID, const char *fieldname,
    4495             :             int32 * sdid, int32 * rankSDS, int32 * rankFld, int32 * offset,
    4496             :             int32 dims[], int32 * solo)
    4497             : {
    4498             :     intn            i;    /* Loop index */
    4499           0 :     intn            status = -1;/* routine return status variable */
    4500             : 
    4501             :     int32           sID;  /* SwathID - offset */
    4502           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    4503             :     int32           dum;  /* Dummy variable */
    4504             :     int32           dums[128];  /* Dummy array */
    4505             :     int32           attrIndex;  /* Attribute l_index */
    4506             : 
    4507             :     char            name[2048]; /* Merged-Field Names */
    4508             :     char            swathname[80];  /* Swath Name */
    4509             :     char           *utlstr; /* Utility string */
    4510             :     char           *metabuf;  /* Pointer to structural metadata (SM) */
    4511             :     char           *metaptrs[2];/* Pointers to begin and end of SM section */
    4512             :     char           *oldmetaptr; /* Pointer within SM section */
    4513             : 
    4514             : 
    4515             :     /* Allocate space for utility string */
    4516             :     /* --------------------------------- */
    4517           0 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
    4518           0 :     if(utlstr == NULL)
    4519             :     { 
    4520           0 :   HEpush(DFE_NOSPACE,"SWSDfldsrch", __FILE__, __LINE__);
    4521           0 :   return(-1);
    4522             :     }
    4523             :     /* Set solo flag to 0 (no) */
    4524             :     /* ----------------------- */
    4525           0 :     *solo = 0;
    4526             : 
    4527             : 
    4528             :     /* Compute "reduced" swath ID */
    4529             :     /* -------------------------- */
    4530           0 :     sID = swathID % idOffset;
    4531             : 
    4532             : 
    4533             :     /* Loop through all SDSs in swath */
    4534             :     /* ------------------------------ */
    4535           0 :     for (i = 0; i < SWXSwath[sID].nSDS; i++)
    4536             :     {
    4537             :   /* If active SDS ... */
    4538             :   /* ----------------- */
    4539           0 :   if (SWXSwath[sID].sdsID[i] != 0)
    4540             :   {
    4541             :       /* Get SDS ID, name, rankSDS, and dimensions */
    4542             :       /* ----------------------------------------- */
    4543           0 :       *sdid = SWXSwath[sID].sdsID[i];
    4544           0 :       SDgetinfo(*sdid, name, rankSDS, dims, &dum, &dum);
    4545           0 :       *rankFld = *rankSDS;
    4546             : 
    4547             :       /* If merged field ... */
    4548             :       /* ------------------- */
    4549           0 :       if (strstr(name, "MRGFLD_") == &name[0])
    4550             :       {
    4551             :     /* Get swath name */
    4552             :     /* -------------- */
    4553           0 :     Vgetname(SWXSwath[sID].IDTable, swathname);
    4554             : 
    4555             : 
    4556             :     /* Get pointers to "MergedFields" section within SM */
    4557             :     /* ------------------------------------------------ */
    4558           0 :     metabuf = (char *) EHmetagroup(sdInterfaceID, swathname, "s",
    4559             :                  "MergedFields", metaptrs);
    4560           0 :     if(metabuf == NULL)
    4561             :     {
    4562           0 :         free(utlstr);
    4563           0 :         return(-1);
    4564             :     }
    4565             : 
    4566             :     /* Store metaptr in order to recover */
    4567             :     /* --------------------------------- */
    4568           0 :     oldmetaptr = metaptrs[0];
    4569             : 
    4570             : 
    4571             :     /* Search for Merged field name */
    4572             :     /* ---------------------------- */
    4573           0 :     snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "MergedFieldName=\"",
    4574             :       name, "\"\n");
    4575           0 :     metaptrs[0] = strstr(metaptrs[0], utlstr);
    4576             : 
    4577             : 
    4578             :     /* If not found check for old metadata */
    4579             :     /* ----------------------------------- */
    4580           0 :     if (metaptrs[0] == NULL)
    4581             :     {
    4582           0 :         snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%.480s%s", "OBJECT=\"", name, "\"\n");
    4583           0 :         metaptrs[0] = strstr(oldmetaptr, utlstr);
    4584             :     }
    4585             : 
    4586             : 
    4587             :     /* Get field list and strip off leading and trailing quotes */
    4588           0 :     EHgetmetavalue(metaptrs, "FieldList", name);  /* not return status --xhua */
    4589           0 :     memmove(name, name + 1, strlen(name) - 2);
    4590           0 :     name[strlen(name) - 2] = 0;
    4591             : 
    4592             :     /* Search for desired field within merged field list */
    4593           0 :     snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s%s", "\"", fieldname, "\"");
    4594           0 :     dum = EHstrwithin(utlstr, name, ',');
    4595             : 
    4596           0 :     free(metabuf);
    4597             :       }
    4598             :       else
    4599             :       {
    4600             :     /* If solo (unmerged) check if SDS name matches fieldname */
    4601             :     /* ------------------------------------------------------ */
    4602           0 :     dum = EHstrwithin(fieldname, name, ',');
    4603           0 :     if (dum != -1)
    4604             :     {
    4605           0 :         *solo = 1;
    4606           0 :         *offset = 0;
    4607             :     }
    4608             :       }
    4609             : 
    4610             : 
    4611             :       /* If field found ... */
    4612             :       /* ------------------ */
    4613           0 :       if (dum != -1)
    4614             :       {
    4615           0 :     status = 0;
    4616             : 
    4617             :     /* If merged field ... */
    4618             :     /* ------------------- */
    4619           0 :     if (*solo == 0)
    4620             :     {
    4621             :         /* Get "Field Offsets" SDS attribute l_index */
    4622             :         /* --------------------------------------- */
    4623           0 :         attrIndex = SDfindattr(*sdid, "Field Offsets");
    4624             : 
    4625             :         /*
    4626             :          * If attribute exists then get offset of desired field
    4627             :          * within merged field
    4628             :          */
    4629           0 :         if (attrIndex != -1)
    4630             :         {
    4631           0 :       SDreadattr(*sdid, attrIndex, (VOIDP) dums);
    4632           0 :       *offset = dums[dum];
    4633             :         }
    4634             : 
    4635             : 
    4636             :         /* Get "Field Dims" SDS attribute l_index */
    4637             :         /* ------------------------------------ */
    4638           0 :         attrIndex = SDfindattr(*sdid, "Field Dims");
    4639             : 
    4640             :         /*
    4641             :          * If attribute exists then get 0th dimension of desired
    4642             :          * field within merged field
    4643             :          */
    4644           0 :         if (attrIndex != -1)
    4645             :         {
    4646           0 :       SDreadattr(*sdid, attrIndex, (VOIDP) dums);
    4647           0 :       dims[0] = dums[dum];
    4648             : 
    4649             :       /* If this dimension = 1 then field is really 2 dim */
    4650             :       /* ------------------------------------------------ */
    4651           0 :       if (dums[dum] == 1)
    4652             :       {
    4653           0 :           *rankFld = 2;
    4654             :       }
    4655             :         }
    4656             :     }
    4657             : 
    4658             : 
    4659             :     /* Break out of SDS loop */
    4660             :     /* --------------------- */
    4661           0 :     break;
    4662             :       }     /* End of found field section */
    4663             :   }
    4664             :   else
    4665             :   {
    4666             :       /* First non-active SDS signifies no more, break out of SDS loop */
    4667             :       /* ------------------------------------------------------------- */
    4668           0 :       break;
    4669             :   }
    4670             :     }
    4671             : 
    4672           0 :     free(utlstr);
    4673             : 
    4674           0 :     return (status);
    4675             : }
    4676             : 
    4677             : 
    4678             : 
    4679             : 
    4680             : 
    4681             : /*----------------------------------------------------------------------------|
    4682             : |  BEGIN_PROLOG                                                               |
    4683             : |                                                                             |
    4684             : |  FUNCTION: SWwrrdfield                                                      |
    4685             : |                                                                             |
    4686             : |  DESCRIPTION: Writes/Reads fields                                           |
    4687             : |                                                                             |
    4688             : |                                                                             |
    4689             : |  Return Value    Type     Units     Description                             |
    4690             : |  ============   ======  =========   =====================================   |
    4691             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    4692             : |                                                                             |
    4693             : |  INPUTS:                                                                    |
    4694             : |  swathID        int32               swath structure ID                      |
    4695             : |  fieldname      const char          fieldname                               |
    4696             : |  code           const char          Write/Read code (w/r)                   |
    4697             : |  start          int32               start array                             |
    4698             : |  stride         int32               stride array                            |
    4699             : |  edge           int32               edge array                              |
    4700             : |  datbuf         void                data buffer for read                    |
    4701             : |                                                                             |
    4702             : |                                                                             |
    4703             : |  OUTPUTS:                                                                   |
    4704             : |  datbuf         void                data buffer for write                   |
    4705             : |                                                                             |
    4706             : |  NOTES:                                                                     |
    4707             : |                                                                             |
    4708             : |                                                                             |
    4709             : |   Date     Programmer   Description                                         |
    4710             : |  ======   ============  =================================================   |
    4711             : |  Jun 96   Joel Gales    Original Programmer                                 |
    4712             : |  Feb 97   Joel Gales    Stride = 1 HDF compression workaround               |
    4713             : |                                                                             |
    4714             : |  END_PROLOG                                                                 |
    4715             : -----------------------------------------------------------------------------*/
    4716             : static intn
    4717           0 : SWwrrdfield(int32 swathID, const char *fieldname, const char *code,
    4718             :       int32 start[], int32 stride[], int32 edge[], VOIDP datbuf)
    4719             : 
    4720             : {
    4721             :     intn            i;    /* Loop index */
    4722           0 :     intn            status = 0; /* routine return status variable */
    4723             : 
    4724             :     int32           fid;  /* HDF-EOS file ID */
    4725             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    4726             :     int32           vgid; /* Swath Geo or Data Vgroup ID */
    4727             :     int32           sdid; /* SDS ID */
    4728             :     int32           dum;  /* Dummy variable */
    4729             :     int32           rankSDS;  /* Rank of SDS */
    4730             :     int32           rankFld;  /* Rank of field */
    4731             : 
    4732             :     int32           vdataID;  /* 1d field vdata */
    4733             :     int32           recsize;  /* Vdata record size */
    4734             :     int32           fldsize;  /* Field size */
    4735             :     int32           nrec; /* Number of records in Vdata */
    4736             : 
    4737             :     int32           offset[8];  /* I/O offset (start) */
    4738             :     int32           incr[8];  /* I/O increment (stride) */
    4739             :     int32           count[8]; /* I/O count (edge) */
    4740             :     int32           dims[8];  /* Field/SDS dimensions */
    4741             :     int32           mrgOffset;  /* Merged field offset */
    4742             :     int32           nflds;  /* Number of fields in Vdata */
    4743             :     int32           strideOne;  /* Strides = 1 flag */
    4744             : 
    4745             :     uint8          *buf;  /* I/O (transfer) buffer */
    4746             :     uint8          *fillbuf;  /* Fill value buffer */
    4747             : 
    4748             :     char            attrName[80]; /* Name of fill value attribute */
    4749             :     char           *ptr[64];  /* String pointer array */
    4750             :     char            fieldlist[256]; /* Vdata field list */
    4751             : 
    4752             : 
    4753             :     /* Check for valid swath ID */
    4754             :     /* ------------------------ */
    4755           0 :     status = SWchkswid(swathID, "SWwrrdfield", &fid, &sdInterfaceID, &dum);
    4756             : 
    4757             : 
    4758           0 :     if (status == 0)
    4759             :     {
    4760             : 
    4761             :   /* Check whether fieldname is in SDS (multi-dim field) */
    4762             :   /* --------------------------------------------------- */
    4763           0 :   status = SWSDfldsrch(swathID, sdInterfaceID, fieldname, &sdid,
    4764             :            &rankSDS, &rankFld, &mrgOffset, dims, &dum);
    4765             : 
    4766             :   /* Multi-Dimensional Field Section */
    4767             :   /* ------------------------------- */
    4768           0 :   if (status != -1)
    4769             :   {
    4770             :       /* Set I/O offset Section */
    4771             :       /* ---------------------- */
    4772             : 
    4773             :       /*
    4774             :        * If start == NULL (default) set I/O offset of 0th field to
    4775             :        * offset within merged field (if any) and the rest to 0
    4776             :        */
    4777           0 :       if (start == NULL)
    4778             :       {
    4779           0 :     for (i = 0; i < rankSDS; i++)
    4780             :     {
    4781           0 :         offset[i] = 0;
    4782             :     }
    4783           0 :     offset[0] = mrgOffset;
    4784             :       }
    4785             :       else
    4786             :       {
    4787             :     /*
    4788             :      * ... otherwise set I/O offset to user values, adjusting the
    4789             :      * 0th field with the merged field offset (if any)
    4790             :      */
    4791           0 :     if (rankFld == rankSDS)
    4792             :     {
    4793           0 :         for (i = 0; i < rankSDS; i++)
    4794             :         {
    4795           0 :       offset[i] = start[i];
    4796             :         }
    4797           0 :         offset[0] += mrgOffset;
    4798             :     }
    4799             :     else
    4800             :     {
    4801             :         /*
    4802             :          * If field really 2-dim merged in 3-dim field then set
    4803             :          * 0th field offset to merge offset and then next two to
    4804             :          * the user values
    4805             :          */
    4806           0 :         for (i = 0; i < rankFld; i++)
    4807             :         {
    4808           0 :       offset[i + 1] = start[i];
    4809             :         }
    4810           0 :         offset[0] = mrgOffset;
    4811             :     }
    4812             :       }
    4813             : 
    4814             : 
    4815             : 
    4816             :       /* Set I/O stride Section */
    4817             :       /* ---------------------- */
    4818             : 
    4819             :       /*
    4820             :        * If stride == NULL (default) set I/O stride to 1
    4821             :        */
    4822           0 :       if (stride == NULL)
    4823             :       {
    4824           0 :     for (i = 0; i < rankSDS; i++)
    4825             :     {
    4826           0 :         incr[i] = 1;
    4827             :     }
    4828             :       }
    4829             :       else
    4830             :       {
    4831             :     /*
    4832             :      * ... otherwise set I/O stride to user values
    4833             :      */
    4834           0 :     if (rankFld == rankSDS)
    4835             :     {
    4836           0 :         for (i = 0; i < rankSDS; i++)
    4837             :         {
    4838           0 :       incr[i] = stride[i];
    4839             :         }
    4840             :     }
    4841             :     else
    4842             :     {
    4843             :         /*
    4844             :          * If field really 2-dim merged in 3-dim field then set
    4845             :          * 0th field stride to 1 and then next two to the user
    4846             :          * values.
    4847             :          */
    4848           0 :         for (i = 0; i < rankFld; i++)
    4849             :         {
    4850           0 :       incr[i + 1] = stride[i];
    4851             :         }
    4852           0 :         incr[0] = 1;
    4853             :     }
    4854             :       }
    4855             : 
    4856             : 
    4857             : 
    4858             :       /* Set I/O count Section */
    4859             :       /* --------------------- */
    4860             : 
    4861             :       /*
    4862             :        * If edge == NULL (default) set I/O count to number of remaining
    4863             :        * entries (dims - start) / increment.  Note that 0th field
    4864             :        * offset corrected for merged field offset (if any).
    4865             :        */
    4866           0 :       if (edge == NULL)
    4867             :       {
    4868           0 :     for (i = 1; i < rankSDS; i++)
    4869             :     {
    4870           0 :         count[i] = (dims[i] - offset[i]) / incr[i];
    4871             :     }
    4872           0 :     count[0] = (dims[0] - (offset[0] - mrgOffset)) / incr[0];
    4873             :       }
    4874             :       else
    4875             :       {
    4876             :     /*
    4877             :      * ... otherwise set I/O count to user values
    4878             :      */
    4879           0 :     if (rankFld == rankSDS)
    4880             :     {
    4881           0 :         for (i = 0; i < rankSDS; i++)
    4882             :         {
    4883           0 :       count[i] = edge[i];
    4884             :         }
    4885             :     }
    4886             :     else
    4887             :     {
    4888             :         /*
    4889             :          * If field really 2-dim merged in 3-dim field then set
    4890             :          * 0th field count to 1 and then next two to the user
    4891             :          * values.
    4892             :          */
    4893           0 :         for (i = 0; i < rankFld; i++)
    4894             :         {
    4895           0 :       count[i + 1] = edge[i];
    4896             :         }
    4897           0 :         count[0] = 1;
    4898             :     }
    4899             :       }
    4900             : 
    4901             :       /* Perform I/O with relevant HDF I/O routine */
    4902             :       /* ----------------------------------------- */
    4903           0 :       if (strcmp(code, "w") == 0)
    4904             :       {
    4905             :     /* Set strideOne to true (1) */
    4906             :     /* ------------------------- */
    4907           0 :     strideOne = 1;
    4908             : 
    4909             : 
    4910             :     /* If incr[i] != 1 set strideOne to false (0) */
    4911             :     /* ------------------------------------------ */
    4912           0 :     for (i = 0; i < rankSDS; i++)
    4913             :     {
    4914           0 :         if (incr[i] != 1)
    4915             :         {
    4916           0 :       strideOne = 0;
    4917           0 :       break;
    4918             :         }
    4919             :     }
    4920             : 
    4921             : 
    4922             :     /*
    4923             :      * If strideOne is true use NULL parameter for stride. This
    4924             :      * is a work-around to HDF compression problem
    4925             :      */
    4926           0 :     if (strideOne == 1)
    4927             :     {
    4928           0 :         status = SDwritedata(sdid, offset, NULL, count,
    4929             :            (VOIDP) datbuf);
    4930             :     }
    4931             :     else
    4932             :     {
    4933           0 :         status = SDwritedata(sdid, offset, incr, count,
    4934             :            (VOIDP) datbuf);
    4935             :     }
    4936             :       }
    4937             :       else
    4938             :       {
    4939           0 :     status = SDreaddata(sdid, offset, incr, count,
    4940             :             (VOIDP) datbuf);
    4941             :       }
    4942             :   }     /* End of Multi-Dimensional Field Section */
    4943             :   else
    4944             :   {
    4945             : 
    4946             :       /* One-Dimensional Field Section */
    4947             :       /* ----------------------------- */
    4948             : 
    4949             :       /* Check fieldname within 1d field Vgroups */
    4950             :       /* --------------------------------------- */
    4951           0 :       status = SW1dfldsrch(fid, swathID, fieldname, code,
    4952             :          &vgid, &vdataID, &dum);
    4953             : 
    4954           0 :       if (status != -1)
    4955             :       {
    4956             : 
    4957             :     /* Get number of records */
    4958             :     /* --------------------- */
    4959           0 :     nrec = VSelts(vdataID);
    4960             : 
    4961             : 
    4962             :     /* Set offset, increment, & count */
    4963             :     /* ------------------------------ */
    4964           0 :     offset[0] = (start == NULL) ? 0 : start[0];
    4965           0 :     incr[0] = (stride == NULL) ? 1 : stride[0];
    4966           0 :     count[0] = (edge == NULL)
    4967           0 :         ? (nrec - offset[0]) / incr[0]
    4968           0 :         : edge[0];
    4969             : 
    4970             : 
    4971             : 
    4972             :     /* Write Section */
    4973             :     /* ------------- */
    4974           0 :     if (strcmp(code, "w") == 0)
    4975             :     {
    4976             :         /* Get size of field and setup fill buffer */
    4977             :         /* --------------------------------------- */
    4978           0 :         fldsize = VSsizeof(vdataID, (char *)fieldname);
    4979           0 :         fillbuf = (uint8 *) calloc(fldsize, 1);
    4980           0 :         if(fillbuf == NULL)
    4981             :         { 
    4982           0 :       HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
    4983           0 :       return(-1);
    4984             :         }
    4985             : 
    4986             :         /* Get size of record in Vdata and setup I/O buffer */
    4987             :         /* ------------------------------------------------ */
    4988           0 :         VSQueryvsize(vdataID, &recsize);
    4989           0 :         buf = (uint8 *) calloc(recsize, count[0] * incr[0]);
    4990           0 :         if(buf == NULL)
    4991             :         { 
    4992           0 :       HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
    4993           0 :       return(-1);
    4994             :         }
    4995             : 
    4996             : 
    4997             :         /* Get names and number of fields in each record  */
    4998             :         /* ---------------------------------------------- */
    4999           0 :         VSgetfields(vdataID, fieldlist);
    5000           0 :         dum = EHstrwithin(fieldname, fieldlist, ',');
    5001           0 :         nflds = EHparsestr(fieldlist, ',', ptr, NULL);
    5002             : 
    5003             : 
    5004             :         /* Get Merged Field Offset (if any) */
    5005             :         /* -------------------------------- */
    5006           0 :         if (nflds > 1)
    5007             :         {
    5008           0 :       if (dum > 0)
    5009             :       {
    5010           0 :           *(ptr[dum] - 1) = 0;
    5011           0 :           mrgOffset = VSsizeof(vdataID, fieldlist);
    5012           0 :           *(ptr[dum] - 1) = ',';
    5013             :       }
    5014             :       else
    5015             :       {
    5016           0 :           mrgOffset = 0;
    5017             :       }
    5018             : 
    5019             :       /* Read records to recover previously written data */
    5020           0 :       /* status = */ VSsetfields(vdataID, fieldlist);
    5021           0 :       /* status = */ VSseek(vdataID, offset[0]);
    5022           0 :       nrec = VSread(vdataID, buf, count[0] * incr[0],
    5023             :               FULL_INTERLACE);
    5024             :         }
    5025             :         else
    5026             :         {
    5027           0 :       mrgOffset = 0;
    5028             :         }
    5029             : 
    5030             : 
    5031             : 
    5032             :         /* Fill buffer with "Fill" value (if any) */
    5033             :         /* -------------------------------------- */
    5034           0 :                     snprintf( attrName, sizeof(attrName), "_FV_%s", fieldname);
    5035             : 
    5036           0 :         status = SWreadattr(swathID, attrName, (char *) fillbuf);
    5037           0 :         if (status == 0)
    5038             :         {
    5039           0 :       for (i = 0; i < count[0] * incr[0]; i++)
    5040             :       {
    5041           0 :           memcpy(buf + i * recsize + mrgOffset,
    5042             :            fillbuf, fldsize);
    5043             :       }
    5044             :         }
    5045             : 
    5046             : 
    5047             :         /* Write new data into buffer */
    5048             :         /* -------------------------- */
    5049           0 :         if (incr[0] == 1 && nflds == 1)
    5050             :         {
    5051           0 :       memcpy(buf, datbuf, count[0] * recsize);
    5052             :         }
    5053             :         else
    5054             :         {
    5055           0 :       for (i = 0; i < count[0]; i++)
    5056             :       {
    5057           0 :           memcpy(buf + i * recsize * incr[0] + mrgOffset,
    5058           0 :            (uint8 *) datbuf + i * fldsize, fldsize);
    5059             :       }
    5060             :         }
    5061             : 
    5062             : 
    5063             :         /* If append read last record */
    5064             :         /* -------------------------- */
    5065           0 :         if (offset[0] == nrec)
    5066             :         {
    5067             :       /* abe added "status =" to next line 8/8/97 */
    5068           0 :       status = VSseek(vdataID, offset[0] - 1);
    5069           0 :       VSread(vdataID, fillbuf, 1, FULL_INTERLACE);
    5070             :         }
    5071             :         else
    5072             :         {
    5073           0 :       status = VSseek(vdataID, offset[0]);
    5074             :         }
    5075             : 
    5076             : 
    5077             :         /* Write data into Vdata */
    5078             :         /* --------------------- */
    5079           0 :         nrec = VSwrite(vdataID, buf, count[0] * incr[0],
    5080             :            FULL_INTERLACE);
    5081             : 
    5082           0 :         free(fillbuf);
    5083           0 :                     if (status > 0)
    5084           0 :                        status = 0;
    5085             : 
    5086             :     }   /* End Write Section */
    5087             :     else
    5088             :     {
    5089             :         /* Read Section */
    5090             :         /* ------------ */
    5091           0 :         status = VSsetfields(vdataID, fieldname);
    5092           0 :         fldsize = VSsizeof(vdataID, (char *)fieldname);
    5093           0 :         buf = (uint8 *) calloc(fldsize, count[0] * incr[0]);
    5094           0 :         if(buf == NULL)
    5095             :         { 
    5096           0 :       HEpush(DFE_NOSPACE,"SWwrrdfield", __FILE__, __LINE__);
    5097           0 :       return(-1);
    5098             :         }
    5099             : 
    5100           0 :         (void) VSseek(vdataID, offset[0]);
    5101           0 :         (void) VSread(vdataID, buf, count[0] * incr[0],
    5102             :          FULL_INTERLACE);
    5103             : 
    5104             : 
    5105             :         /* Copy from input buffer to returned data buffer */
    5106             :         /* ---------------------------------------------- */
    5107           0 :         if (incr[0] == 1)
    5108             :         {
    5109           0 :       memcpy(datbuf, buf, count[0] * fldsize);
    5110             :         }
    5111             :         else
    5112             :         {
    5113           0 :       for (i = 0; i < count[0]; i++)
    5114             :       {
    5115           0 :           memcpy((uint8 *) datbuf + i * fldsize,
    5116           0 :            buf + i * fldsize * incr[0], fldsize);
    5117             :       }
    5118             :         }
    5119             : 
    5120             :     }   /* End Read Section */
    5121             : 
    5122           0 :     free(buf);
    5123           0 :     VSdetach(vdataID);
    5124             :       }
    5125             :       else
    5126             :       {
    5127           0 :     HEpush(DFE_GENAPP, "SWwrrdfield", __FILE__, __LINE__);
    5128           0 :     HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
    5129             :       }
    5130             :   }     /* End One-D Field Section */
    5131             : 
    5132             :     }
    5133           0 :     return (status);
    5134             : }
    5135             : 
    5136             : 
    5137             : 
    5138             : /*----------------------------------------------------------------------------|
    5139             : |  BEGIN_PROLOG                                                               |
    5140             : |                                                                             |
    5141             : |  FUNCTION: SWwritefield                                                     |
    5142             : |                                                                             |
    5143             : |  DESCRIPTION: Writes data to field                                          |
    5144             : |                                                                             |
    5145             : |                                                                             |
    5146             : |  Return Value    Type     Units     Description                             |
    5147             : |  ============   ======  =========   =====================================   |
    5148             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    5149             : |                                                                             |
    5150             : |  INPUTS:                                                                    |
    5151             : |  swathID        int32               swath structure ID                      |
    5152             : |  fieldname      char                fieldname                               |
    5153             : |  start          int32               start array                             |
    5154             : |  stride         int32               stride array                            |
    5155             : |  edge           int32               edge array                              |
    5156             : |                                                                             |
    5157             : |                                                                             |
    5158             : |  OUTPUTS:                                                                   |
    5159             : |  data           void                data buffer for write                   |
    5160             : |                                                                             |
    5161             : |  NOTES:                                                                     |
    5162             : |                                                                             |
    5163             : |                                                                             |
    5164             : |   Date     Programmer   Description                                         |
    5165             : |  ======   ============  =================================================   |
    5166             : |  Jun 96   Joel Gales    Original Programmer                                 |
    5167             : |                                                                             |
    5168             : |  END_PROLOG                                                                 |
    5169             : -----------------------------------------------------------------------------*/
    5170             : intn
    5171           0 : SWwritefield(int32 swathID, const char *fieldname,
    5172             :        int32 start[], int32 stride[], int32 edge[], VOIDP data)
    5173             : 
    5174             : {
    5175           0 :     intn            status = 0; /* routine return status variable */
    5176             : 
    5177           0 :     status = SWwrrdfield(swathID, fieldname, "w", start, stride, edge,
    5178             :        data);
    5179           0 :     return (status);
    5180             : }
    5181             : 
    5182             : 
    5183             : 
    5184             : 
    5185             : 
    5186             : 
    5187             : /*----------------------------------------------------------------------------|
    5188             : |  BEGIN_PROLOG                                                               |
    5189             : |                                                                             |
    5190             : |  FUNCTION: SWreadfield                                                      |
    5191             : |                                                                             |
    5192             : |  DESCRIPTION: Reads data from field                                         |
    5193             : |                                                                             |
    5194             : |  Return Value    Type     Units     Description                             |
    5195             : |  ============   ======  =========   =====================================   |
    5196             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    5197             : |                                                                             |
    5198             : |  INPUTS:                                                                    |
    5199             : |  swathID        int32               swath structure ID                      |
    5200             : |  fieldname      const char          fieldname                               |
    5201             : |  start          int32               start array                             |
    5202             : |  stride         int32               stride array                            |
    5203             : |  edge           int32               edge array                              |
    5204             : |  buffer         void                data buffer for read                    |
    5205             : |                                                                             |
    5206             : |                                                                             |
    5207             : |  OUTPUTS:                                                                   |
    5208             : |     None                                                                    |
    5209             : |                                                                             |
    5210             : |  NOTES:                                                                     |
    5211             : |                                                                             |
    5212             : |                                                                             |
    5213             : |   Date     Programmer   Description                                         |
    5214             : |  ======   ============  =================================================   |
    5215             : |  Jun 96   Joel Gales    Original Programmer                                 |
    5216             : |                                                                             |
    5217             : |  END_PROLOG                                                                 |
    5218             : -----------------------------------------------------------------------------*/
    5219             : intn
    5220           0 : SWreadfield(int32 swathID, const char *fieldname,
    5221             :       int32 start[], int32 stride[], int32 edge[], VOIDP buffer)
    5222             : 
    5223             : {
    5224           0 :     intn            status = 0; /* routine return status variable */
    5225             : 
    5226           0 :     status = SWwrrdfield(swathID, fieldname, "r", start, stride, edge,
    5227             :        buffer);
    5228           0 :     return (status);
    5229             : }
    5230             : 
    5231             : 
    5232             : 
    5233             : 
    5234             : /*----------------------------------------------------------------------------|
    5235             : |  BEGIN_PROLOG                                                               |
    5236             : |                                                                             |
    5237             : |  FUNCTION: SWdefboxregion                                                   |
    5238             : |                                                                             |
    5239             : |  DESCRIPTION: Finds swath cross tracks within area of interest and returns  |
    5240             : |               region ID                                                     |
    5241             : |                                                                             |
    5242             : |                                                                             |
    5243             : |  Return Value    Type     Units     Description                             |
    5244             : |  ============   ======  =========   =====================================   |
    5245             : |  regionID       int32               Region ID                               |
    5246             : |                                                                             |
    5247             : |  INPUTS:                                                                    |
    5248             : |  swathID        int32               Swath structure ID                      |
    5249             : |  cornerlon      float64  dec deg    Longitude of opposite corners of box    |
    5250             : |  cornerlat      float64  dec deg    Latitude of opposite corners of box     |
    5251             : |  mode           int32               Search mode                             |
    5252             : |                                     HDFE_MIDPOINT - Use midpoint of Xtrack  |
    5253             : |                                     HDFE_ENDPOINT - Use endpoints of Xtrack |
    5254             : |                                     HDFE_ANYPOINT - Use all points of Xtrack|
    5255             : |                                                                             |
    5256             : |  OUTPUTS:                                                                   |
    5257             : |             None                                                            |
    5258             : |                                                                             |
    5259             : |  NOTES:                                                                     |
    5260             : |                                                                             |
    5261             : |                                                                             |
    5262             : |   Date     Programmer   Description                                         |
    5263             : |  ======   ============  =================================================   |
    5264             : |  Jun 96   Joel Gales    Original Programmer                                 |
    5265             : |  Oct 96   Joel Gales    Add ability to handle regions crossing date line    |
    5266             : |  Dec 96   Joel Gales    Add multiple vertical subsetting capability         |
    5267             : |  Jul 98   Abe Taaheri   Fixed core dump in SWregioninfo associated with     |
    5268             : |                         SWXRegion[k]->nRegions exceeding MAXNREGIONS in     |
    5269             : |                         this function                                       |
    5270             : |  Aug 99   Abe Taaheri   Fixed the code so that all cross tracks or all      |
    5271             : |                         points on the along track that fall inside the box  |
    5272             : |                         are identified. At the same time added code to      |
    5273             : |                         function "updatescene" to reject cases where there  |
    5274             : |                         is single cross track in the box (for LANDSAT)      |
    5275             : |  Jun 03   Abe Taaheri   Added a few lines to report error and return -1 if  |
    5276             : |                         regionID exceeded NSWATHREGN                        |
    5277             : |  Mar 04   Abe Taaheri   Added recognition for GeodeticLatitude              |
    5278             : |                                                                             |
    5279             : |  END_PROLOG                                                                 |
    5280             : -----------------------------------------------------------------------------*/
    5281             : int32
    5282           0 : SWdefboxregion(int32 swathID, float64 cornerlon[], float64 cornerlat[],
    5283             :          int32 mode)
    5284             : {
    5285             :     intn            i;    /* Loop index */
    5286           0 :     intn            j = 0;    /* Loop index */
    5287             :     intn            k;    /* Loop index */
    5288             :     
    5289             :     intn            status; /* routine return status variable */
    5290             :     intn            statLon;  /* Status from SWfieldinfo for longitude */
    5291             :     intn            statLat;  /* Status from SWfieldinfo for latitude */
    5292           0 :     intn            statCoLat = -1; /* Status from SWfieldinfo for
    5293             :            * Colatitude */
    5294           0 :     intn            statGeodeticLat = -1; /* Status from SWfieldinfo for
    5295             :            * GeodeticLatitude */
    5296             : 
    5297           0 :     uint8           found = 0;  /* Found flag */
    5298             :     uint8          *flag; /* Pointer to track flag array */
    5299           0 :     intn           validReg = -1; /* -1 is invalid validReg */ 
    5300             :     
    5301             :     int32           fid;  /* HDF-EOS file ID */
    5302             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    5303             :     int32           swVgrpID; /* Swath Vgroup ID */
    5304             :     int32           rank; /* Rank of geolocation fields */
    5305             :     int32           nt;   /* Number type of geolocation fields */
    5306             :     int32           dims[8];  /* Dimensions of geolocation fields */
    5307             :     int32           nElem;  /* Number of elements to read */
    5308             :     int32           bndflag;  /* +/-180 longitude boundary flag */
    5309             :     int32           lonTest;  /* Longitude test flag */
    5310             :     int32           latTest;  /* Latitude test flag */
    5311             :     int32           start[2]; /* Start array (read) */
    5312           0 :     int32           stride[2] = {1, 1}; /* Stride array (read) */
    5313             :     int32           edge[2];  /* Edge array (read) */
    5314           0 :     int32           regionID = -1;  /* Region ID (return) */
    5315             :     int32           anyStart[2];/* ANYPOINT start array (read) */
    5316             :     int32           anyEdge[2]; /* ANYPOINT edge array (read) */
    5317             : 
    5318             :     float32         temp32; /* Temporary float32 variable */
    5319             : 
    5320             :     float64         lonTestVal; /* Longitude test value */
    5321             :     float64         latTestVal; /* Latitude test value */
    5322             :     float64         temp64; /* Temporary float64 variable */
    5323             : 
    5324             :     char           *lonArr; /* Longitude data array */
    5325             :     char           *latArr; /* Latitude data array */
    5326             :     char            dimlist[256]; /* Dimension list (geolocation
    5327             :            * fields) */
    5328             :     char            latName[17];/* Latitude field name */
    5329             :     
    5330             : 
    5331             :     /* Check for valid swath ID */
    5332             :     /* ------------------------ */
    5333           0 :     status = SWchkswid(swathID, "SWdefboxregion", &fid, &sdInterfaceID,
    5334             :            &swVgrpID);
    5335             : 
    5336             : 
    5337             :     /* Inclusion mode must be between 0 and 2 */
    5338             :     /* -------------------------------------- */
    5339           0 :     if (mode < 0 || mode > 2)
    5340             :     {
    5341           0 :   status = -1;
    5342           0 :   HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
    5343           0 :   HEreport("Improper Inclusion Mode: %d.\n", mode);
    5344             :     }
    5345             : 
    5346             : 
    5347           0 :     if (status == 0)
    5348             :     {
    5349             :   /* Get "Longitude" field info */
    5350             :   /* -------------------------- */
    5351           0 :   statLon = SWfieldinfo(swathID, "Longitude", &rank, dims, &nt, dimlist);
    5352           0 :   if (statLon != 0)
    5353             :   {
    5354           0 :       status = -1;
    5355           0 :       HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
    5356           0 :       HEreport("\"Longitude\" field not found.\n");
    5357             :   }
    5358             : 
    5359             :   /* Get "Latitude" field info */
    5360             :   /* -------------------------- */
    5361           0 :   statLat = SWfieldinfo(swathID, "Latitude", &rank, dims, &nt, dimlist);
    5362           0 :   if (statLat != 0)
    5363             :   {
    5364             :       /* If not found check for "Colatitude" field info */
    5365             :       /* ---------------------------------------------- */
    5366           0 :       statCoLat = SWfieldinfo(swathID, "Colatitude", &rank, dims, &nt,
    5367             :             dimlist);
    5368           0 :       if (statCoLat != 0)
    5369             :       {
    5370             :         /* Check again for Geodeticlatitude */            
    5371           0 :         statGeodeticLat = SWfieldinfo(swathID, 
    5372             :               "GeodeticLatitude", &rank, 
    5373             :               dims, &nt, dimlist);
    5374           0 :         if (statGeodeticLat != 0)
    5375             :     {
    5376             :       /* Neither "Latitude" nor "Colatitude" nor
    5377             :          "GeodeticLatitude" field found */
    5378             :       /* ----------------------------------------------- */
    5379           0 :       status = -1;
    5380           0 :       HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
    5381           0 :       HEreport(
    5382             :          "Neither \"Latitude\" nor \"Colatitude\" nor \"GeodeticLatitude\" fields found.\n");
    5383             :     }
    5384             :         else
    5385             :     {
    5386             :       /* Latitude field is "GeodeticLatitude" */
    5387             :       /* ------------------------------ */
    5388           0 :       strcpy(latName, "GeodeticLatitude");
    5389             :     }
    5390             :       }
    5391             :       else
    5392             :       {
    5393             :     /* Latitude field is "Colatitude" */
    5394             :     /* ------------------------------ */
    5395           0 :     strcpy(latName, "Colatitude");
    5396             :       }
    5397             :   }
    5398             :   else
    5399             :   {
    5400             :       /* Latitude field is "Latitude" */
    5401             :       /* ---------------------------- */
    5402           0 :       strcpy(latName, "Latitude");
    5403             :   }
    5404             : 
    5405             : 
    5406           0 :   if (status == 0)
    5407             :   {
    5408             :       /* Search along entire "Track" dimension from beginning to end */
    5409             :       /* ----------------------------------------------------------- */
    5410           0 :       start[0] = 0;
    5411           0 :       edge[0] = dims[0];
    5412             : 
    5413             : 
    5414             :       /* If 1D geolocation fields then set mode to MIDPOINT */
    5415             :       /* -------------------------------------------------- */
    5416           0 :       if (rank == 1)
    5417             :       {
    5418           0 :     mode = HDFE_MIDPOINT;
    5419             :       }
    5420             : 
    5421             : 
    5422           0 :       switch (mode)
    5423             :       {
    5424             :     /* If MIDPOINT search single point in middle of "CrossTrack" */
    5425             :     /* --------------------------------------------------------- */
    5426           0 :       case HDFE_MIDPOINT:
    5427             : 
    5428           0 :     start[1] = dims[1] / 2;
    5429           0 :     edge[1] = 1;
    5430             : 
    5431           0 :     break;
    5432             : 
    5433             :     /* If ENDPOINT search 2 points at either end of "CrossTrack" */
    5434             :     /* --------------------------------------------------------- */
    5435           0 :       case HDFE_ENDPOINT:
    5436             : 
    5437           0 :     start[1] = 0;
    5438           0 :     stride[1] = dims[1] - 1;
    5439           0 :     edge[1] = 2;
    5440             : 
    5441           0 :     break;
    5442             : 
    5443             :     /* If ANYPOINT do initial MIDPOINT search */
    5444             :     /* -------------------------------------- */
    5445           0 :       case HDFE_ANYPOINT:
    5446             : 
    5447           0 :     start[1] = dims[1] / 2;
    5448           0 :     edge[1] = 1;
    5449             : 
    5450           0 :     break;
    5451             :       }
    5452             : 
    5453             : 
    5454             :       /* Compute number of elements */
    5455             :       /* -------------------------- */
    5456           0 :       nElem = edge[0] * edge[1];
    5457             : 
    5458             : 
    5459             :       /* Allocate space for longitude and latitude (float64) */
    5460             :       /* --------------------------------------------------- */
    5461           0 :       lonArr = (char *) calloc(nElem, sizeof(float64));
    5462           0 :       if(lonArr == NULL)
    5463             :       { 
    5464           0 :     HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
    5465           0 :     return(-1);
    5466             :       }
    5467             : 
    5468           0 :       latArr = (char *) calloc(nElem, sizeof(float64));
    5469           0 :       if(latArr == NULL)
    5470             :       { 
    5471           0 :     HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
    5472           0 :     free(lonArr);
    5473           0 :     return(-1);
    5474             :       }
    5475             : 
    5476             : 
    5477             :       /* Allocate space for flag array (uint8) */
    5478             :       /* ------------------------------------- */
    5479           0 :       flag = (uint8 *) calloc(edge[0] + 1, 1);
    5480           0 :       if(flag == NULL)
    5481             :       { 
    5482           0 :     HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
    5483           0 :     free(lonArr);
    5484           0 :     free(latArr);
    5485           0 :     return(-1);
    5486             :       }
    5487             : 
    5488             : 
    5489             :       /* Read Longitude and Latitude fields */
    5490             :       /* ---------------------------------- */
    5491           0 :       status = SWreadfield(swathID, "Longitude",
    5492             :          start, stride, edge, lonArr);
    5493           0 :       status = SWreadfield(swathID, latName,
    5494             :          start, stride, edge, latArr);
    5495             : 
    5496             : 
    5497             : 
    5498             :       /*
    5499             :        * If geolocation fields are FLOAT32 then cast each entry as
    5500             :        * FLOAT64
    5501             :        */
    5502           0 :       if (nt == DFNT_FLOAT32)
    5503             :       {
    5504           0 :     for (i = nElem - 1; i >= 0; i--)
    5505             :     {
    5506           0 :         memcpy(&temp32, lonArr + 4 * i, 4);
    5507           0 :         temp64 = (float64) temp32;
    5508           0 :         memcpy(lonArr + 8 * i, &temp64, 8);
    5509             : 
    5510           0 :         memcpy(&temp32, latArr + 4 * i, 4);
    5511           0 :         temp64 = (float64) temp32;
    5512           0 :         memcpy(latArr + 8 * i, &temp64, 8);
    5513             :     }
    5514             :       }   
    5515             : 
    5516             : 
    5517             :       /* Set boundary flag */
    5518             :       /* ----------------- */
    5519             : 
    5520             :       /*
    5521             :        * This variable is set to 1 if the region of interest crosses
    5522             :        * the +/- 180 longitude boundary
    5523             :        */
    5524           0 :       bndflag = (cornerlon[0] < cornerlon[1]) ? 0 : 1;
    5525             : 
    5526             : 
    5527             : 
    5528             :       /* Main Search Loop */
    5529             :       /* ---------------- */
    5530             : 
    5531             :       /* For each track ... */
    5532             :       /* ------------------ */
    5533             : 
    5534           0 :       for (i = 0; i < edge[0]; i++)
    5535             :       {   
    5536             :     /* For each value from Cross Track ... */
    5537             :     /* ----------------------------------- */
    5538           0 :     for (j = 0; j < edge[1]; j++)
    5539             :     {
    5540             :         /* Read in single lon & lat values from data buffers */
    5541             :         /* ------------------------------------------------- */
    5542           0 :         memcpy(&lonTestVal, &lonArr[8 * (i * edge[1] + j)], 8);
    5543           0 :         memcpy(&latTestVal, &latArr[8 * (i * edge[1] + j)], 8);
    5544             : 
    5545             : 
    5546             :         /* If longitude value > 180 convert to -180 to 180 range */
    5547             :         /* ----------------------------------------------------- */
    5548           0 :         if (lonTestVal > 180)
    5549             :         {
    5550           0 :       lonTestVal = lonTestVal - 360;
    5551             :         }
    5552             : 
    5553             :         /* If Colatitude value convert to latitude value */
    5554             :         /* --------------------------------------------- */
    5555           0 :         if (statCoLat == 0)
    5556             :         {
    5557           0 :       latTestVal = 90 - latTestVal;
    5558             :         }
    5559             : 
    5560             : 
    5561             :         /* Test if lat value is within range */
    5562             :         /* --------------------------------- */
    5563           0 :         latTest = (latTestVal >= cornerlat[0] &&
    5564           0 :              latTestVal <= cornerlat[1]);
    5565             : 
    5566             : 
    5567           0 :         if (bndflag == 1)
    5568             :         {
    5569             :       /*
    5570             :        * If boundary flag set test whether longitude value
    5571             :        * is outside region and then flip
    5572             :        */
    5573           0 :       lonTest = (lonTestVal >= cornerlon[1] &&
    5574           0 :            lonTestVal <= cornerlon[0]);
    5575           0 :       lonTest = 1 - lonTest;
    5576             :         }
    5577             :         else
    5578             :         {
    5579           0 :       lonTest = (lonTestVal >= cornerlon[0] &&
    5580           0 :            lonTestVal <= cornerlon[1]);
    5581             :         }
    5582             : 
    5583             : 
    5584             :         /*
    5585             :          * If both longitude and latitude are within region set
    5586             :          * flag on for this track
    5587             :          */
    5588           0 :         if (lonTest + latTest == 2)
    5589             :         {
    5590           0 :       flag[i] = 1;
    5591           0 :       found = 1;
    5592           0 :       break;
    5593             :         }
    5594             :     }
    5595             :       }
    5596             : 
    5597             : 
    5598             : 
    5599             :       /* ANYPOINT search */
    5600             :       /* --------------- */
    5601           0 :       if (mode == HDFE_ANYPOINT && rank > 1)
    5602             :       {
    5603           0 :     free(lonArr);
    5604           0 :     free(latArr);
    5605             : 
    5606             :     /* Allocate space for an entire single cross track */
    5607             :     /* ----------------------------------------------- */
    5608           0 :     lonArr = (char *) calloc(dims[1], sizeof(float64));
    5609           0 :     if(lonArr == NULL)
    5610             :     { 
    5611           0 :         HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
    5612           0 :         return(-1);
    5613             :     }
    5614             : 
    5615           0 :     latArr = (char *) calloc(dims[1], sizeof(float64));
    5616           0 :     if(latArr == NULL)
    5617             :     { 
    5618           0 :         HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
    5619           0 :         free(lonArr);
    5620           0 :         return(-1);
    5621             :     }
    5622             : 
    5623             : 
    5624             :     /* Setup start and edge */
    5625             :     /* -------------------- */
    5626           0 :     anyStart[1] = 0;
    5627           0 :     anyEdge[0] = 1;
    5628           0 :     anyEdge[1] = dims[1];
    5629             :   
    5630             :                
    5631             :     /* For each track starting from 0 */
    5632             :     /* ------------------------------ */
    5633           0 :     for (i = 0; i < edge[0]; i++)    
    5634             :     {
    5635             :                
    5636             :         /* If cross track not in region (with MIDPOINT search ... */
    5637             :         /* ------------------------------------------------------ */
    5638           0 :         if (flag[i] == 0)
    5639             :         {
    5640             :       /* Setup track start */
    5641             :       /* ----------------- */
    5642           0 :       anyStart[0] = i;
    5643             : 
    5644             : 
    5645             :       /* Read in lon and lat values for cross track */
    5646             :       /* ------------------------------------------ */
    5647           0 :       status = SWreadfield(swathID, "Longitude",
    5648             :              anyStart, NULL, anyEdge, lonArr);
    5649           0 :       status = SWreadfield(swathID, latName,
    5650             :              anyStart, NULL, anyEdge, latArr);
    5651             : 
    5652             : 
    5653             : 
    5654             :       /*
    5655             :        * If geolocation fields are FLOAT32 then cast each
    5656             :        * entry as FLOAT64
    5657             :        */
    5658           0 :       if (nt == DFNT_FLOAT32)
    5659             :       {
    5660           0 :           for (j = dims[1] - 1; j >= 0; j--)
    5661             :           {
    5662           0 :         memcpy(&temp32, lonArr + 4 * j, 4);
    5663           0 :         temp64 = (float64) temp32;
    5664           0 :         memcpy(lonArr + 8 * j, &temp64, 8);
    5665             : 
    5666           0 :         memcpy(&temp32, latArr + 4 * j, 4);
    5667           0 :         temp64 = (float64) temp32;
    5668           0 :         memcpy(latArr + 8 * j, &temp64, 8);
    5669             :           }
    5670             :       }
    5671             : 
    5672             : 
    5673             :       /* For each value from Cross Track ... */
    5674             :       /* ----------------------------------- */
    5675           0 :       for (j = 0; j < dims[1]; j++)
    5676             :       {
    5677             :           /* Read in single lon & lat values from buffers */
    5678             :           /* -------------------------------------------- */
    5679           0 :           memcpy(&lonTestVal, &lonArr[8 * j], 8);
    5680           0 :           memcpy(&latTestVal, &latArr[8 * j], 8);
    5681             : 
    5682             : 
    5683             :           /* If lon value > 180 convert to -180 - 180 range */
    5684             :           /* ---------------------------------------------- */
    5685           0 :           if (lonTestVal > 180)
    5686             :           {
    5687           0 :         lonTestVal = lonTestVal - 360;
    5688             :           }
    5689             : 
    5690             :           /* If Colatitude value convert to latitude value */
    5691             :           /* --------------------------------------------- */
    5692           0 :           if (statCoLat == 0)
    5693             :           {
    5694           0 :         latTestVal = 90 - latTestVal;
    5695             :           }
    5696             : 
    5697             : 
    5698             :           /* Test if lat value is within range */
    5699             :           /* --------------------------------- */
    5700           0 :           latTest = (latTestVal >= cornerlat[0] &&
    5701           0 :                latTestVal <= cornerlat[1]);
    5702             : 
    5703             : 
    5704           0 :           if (bndflag == 1)
    5705             :           {
    5706             :         /*
    5707             :          * If boundary flag set test whether
    5708             :          * longitude value is outside region and then
    5709             :          * flip
    5710             :          */
    5711           0 :         lonTest = (lonTestVal >= cornerlon[1] &&
    5712           0 :              lonTestVal <= cornerlon[0]);
    5713           0 :         lonTest = 1 - lonTest;
    5714             :           }
    5715             :           else
    5716             :           {
    5717           0 :         lonTest = (lonTestVal >= cornerlon[0] &&
    5718           0 :              lonTestVal <= cornerlon[1]);
    5719             :           }
    5720             : 
    5721             : 
    5722             :           /*
    5723             :            * If both longitude and latitude are within
    5724             :            * region set flag on for this track
    5725             :            */
    5726           0 :           if (lonTest + latTest == 2)
    5727             :           {
    5728           0 :         flag[i] = 1;
    5729           0 :         found = 1;
    5730           0 :         break;
    5731             :           }
    5732             :       }
    5733             :         }
    5734             :     }
    5735             :             }
    5736             : 
    5737             :       /* If within region setup Region Structure */
    5738             :       /* --------------------------------------- */
    5739           0 :       if (found == 1)
    5740             :       {
    5741             :     /* For all entries in SWXRegion array ... */
    5742             :     /* -------------------------------------- */
    5743           0 :     for (k = 0; k < NSWATHREGN; k++)
    5744             :     {
    5745             :         /* If empty region ... */
    5746             :         /* ------------------- */
    5747           0 :         if (SWXRegion[k] == 0)
    5748             :         {
    5749             :       /* Allocate space for region entry */
    5750             :       /* ------------------------------- */
    5751           0 :       SWXRegion[k] = (struct swathRegion *)
    5752           0 :           calloc(1, sizeof(struct swathRegion));
    5753           0 :       if(SWXRegion[k] == NULL)
    5754             :       { 
    5755           0 :           HEpush(DFE_NOSPACE,"SWdefboxregion", __FILE__, __LINE__);
    5756           0 :           return(-1);
    5757             :       }
    5758             : 
    5759             :       /* Store file and swath ID */
    5760             :       /* ----------------------- */
    5761           0 :       SWXRegion[k]->fid = fid;
    5762           0 :       SWXRegion[k]->swathID = swathID;
    5763             : 
    5764             : 
    5765             :       /* Set Start & Stop Vertical arrays to -1 */
    5766             :       /* -------------------------------------- */
    5767           0 :       for (j = 0; j < 8; j++)
    5768             :       {
    5769           0 :           SWXRegion[k]->StartVertical[j] = -1;
    5770           0 :           SWXRegion[k]->StopVertical[j] = -1;
    5771           0 :           SWXRegion[k]->StartScan[j] = -1;
    5772           0 :           SWXRegion[k]->StopScan[j] = -1;
    5773             :       }
    5774             : 
    5775             : 
    5776             :       /* Set region ID */
    5777             :       /* ------------- */
    5778           0 :       regionID = k;
    5779           0 :       break;
    5780             :         }
    5781             :     }
    5782           0 :     if (k >= NSWATHREGN)
    5783             :       {
    5784           0 :         HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
    5785           0 :         HEreport(
    5786             :            "regionID exceeded NSWATHREGN.\n");
    5787           0 :                     return (-1);
    5788             :       }
    5789             : 
    5790             :     /* Find start and stop of regions */
    5791             :     /* ------------------------------ */
    5792             : 
    5793             :     /* Subtract previous flag value from current one */
    5794             :     /* --------------------------------------------- */
    5795             : 
    5796             :     /*
    5797             :      * Transition points will have flag value (+1) start or
    5798             :      * (255 = (uint8) -1) stop of region
    5799             :      */
    5800           0 :     for (i = edge[0]; i > 0; i--)
    5801             :     {
    5802           0 :         flag[i] -= flag[i - 1];
    5803             :     }
    5804             : 
    5805             : 
    5806           0 :     for (i = 0; i <= edge[0]; i++)
    5807             :     {
    5808             :         /* Start of region */
    5809             :         /* --------------- */
    5810           0 :         if (flag[i] == 1)
    5811             :         {
    5812             :       /* Increment (multiple) region counter */
    5813             :       /* ----------------------------------- */
    5814           0 :       j = ++SWXRegion[k]->nRegions;
    5815             :       
    5816             :       /* if SWXRegion[k]->nRegions greater than MAXNREGIONS */
    5817             :       /* free allocated memory and return FAIL */
    5818             :       
    5819           0 :       if ((SWXRegion[k]->nRegions) > MAXNREGIONS)
    5820             :       {
    5821           0 :           HEpush(DFE_GENAPP, "SWdefboxregion", __FILE__, __LINE__);
    5822           0 :           HEreport("SWXRegion[%d]->nRegions exceeds MAXNREGIONS= %d.\n", k, MAXNREGIONS);
    5823           0 :           free(lonArr);
    5824           0 :           free(latArr);
    5825           0 :           free(flag);
    5826           0 :           return(-1);
    5827             :       }
    5828             :       
    5829           0 :       SWXRegion[k]->StartRegion[j - 1] = i;
    5830             :         }
    5831             :         
    5832             :         /* End of region */
    5833             :         /* ------------- */
    5834           0 :         if (flag[i] == 255)
    5835             :         {
    5836           0 :       SWXRegion[k]->StopRegion[j - 1] = i - 1;
    5837           0 :       validReg = 0;
    5838             :         }
    5839             :     }
    5840             :       }
    5841           0 :       free(lonArr);
    5842           0 :       free(latArr);
    5843           0 :       free(flag);
    5844             :   }
    5845             :     }
    5846           0 :     if(validReg==0)
    5847             :     {
    5848           0 :   return (regionID);
    5849             :     }
    5850             :     else
    5851             :     {
    5852           0 :   return (-1);
    5853             :     }
    5854             :     
    5855             : }
    5856             : 
    5857             : 
    5858             : 
    5859             : 
    5860             : /*----------------------------------------------------------------------------|
    5861             : |  BEGIN_PROLOG                                                               |
    5862             : |                                                                             |
    5863             : |  FUNCTION: SWregionl_index                                                    |
    5864             : |                                                                             |
    5865             : |  DESCRIPTION: Finds swath cross tracks within area of interest and returns  |
    5866             : |               region l_index and region ID                                    |
    5867             : |                                                                             |
    5868             : |                                                                             |
    5869             : |  Return Value    Type     Units     Description                             |
    5870             : |  ============   ======  =========   =====================================   |
    5871             : |  regionID       int32               Region ID                               |
    5872             : |                                                                             |
    5873             : |  INPUTS:                                                                    |
    5874             : |  swathID        int32               Swath structure ID                      |
    5875             : |  cornerlon      float64  dec deg    Longitude of opposite corners of box    |
    5876             : |  cornerlat      float64  dec deg    Latitude of opposite corners of box     |
    5877             : |  mode           int32               Search mode                             |
    5878             : |                                     HDFE_MIDPOINT - Use midpoint of Xtrack  |
    5879             : |                                     HDFE_ENDPOINT - Use endpoints of Xtrack |
    5880             : |                                     HDFE_ANYPOINT - Use all points of Xtrack|
    5881             : |                                                                             |
    5882             : |  OUTPUTS:                                                                   |
    5883             : |  geodim   char          geolocation track dimension             |
    5884             : |  idxrange   int32         indices of region for along track dim.  |
    5885             : |                                                                             |
    5886             : |  NOTES:                                                                     |
    5887             : |                                                                             |
    5888             : |                                                                             |
    5889             : |   Date     Programmer   Description                                         |
    5890             : |  ======   ============  =================================================   |
    5891             : |  Jun 96   Joel Gales    Original Programmer                                 |
    5892             : |  Oct 96   Joel Gales    Add ability to handle regions crossing date line    |
    5893             : |  Dec 96   Joel Gales    Add multiple vertical subsetting capability         |
    5894             : |  Nov 97   Daw           Add multiple vertical subsetting capability         |
    5895             : |  END_PROLOG                                                                 |
    5896             : -----------------------------------------------------------------------------*/
    5897             : int32
    5898           0 : SWregionindex(int32 swathID, float64 cornerlon[], float64 cornerlat[],
    5899             :          int32 mode, char *geodim, int32 idxrange[])
    5900             : {
    5901             :     intn            i;    /* Loop index */
    5902             :     intn            j;    /* Loop index */
    5903             :     intn            k;    /* Loop index */
    5904             : 
    5905           0 :     intn            l=0;  /* Loop index */
    5906           0 :     intn            tmpVal = 0;     /* temp value for start region Delyth Jones*/
    5907             :   /*intn            j1;  */     /* Loop index */
    5908             :     intn            status; /* routine return status variable */
    5909             :     intn      mapstatus;  /* status for type of mapping */
    5910             :     intn            statLon;  /* Status from SWfieldinfo for longitude */
    5911             :     intn            statLat;  /* Status from SWfieldinfo for latitude */
    5912           0 :     intn            statCoLat = -1; /* Status from SWfieldinfo for
    5913             :            * Colatitude */
    5914           0 :     intn            statGeodeticLat = -1; /* Status from SWfieldinfo for
    5915             :            * GeodeticLatitude */
    5916             : 
    5917           0 :     uint8           found = 0;  /* Found flag */
    5918             :     uint8          *flag; /* Pointer to track flag array */
    5919           0 :     intn           validReg = -1; /* -1 is invalid validReg */ 
    5920             :     
    5921             :     int32           fid;  /* HDF-EOS file ID */
    5922             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    5923             :     int32           swVgrpID; /* Swath Vgroup ID */
    5924             :     int32           rank; /* Rank of geolocation fields */
    5925             :     int32           nt;   /* Number type of geolocation fields */
    5926             :     int32           dims[8];  /* Dimensions of geolocation fields */
    5927             :     int32           nElem;  /* Number of elements to read */
    5928             :     int32           bndflag;  /* +/-180 longitude boundary flag */
    5929             :     int32           lonTest;  /* Longitude test flag */
    5930             :     int32           latTest;  /* Latitude test flag */
    5931             :     int32           start[2]; /* Start array (read) */
    5932           0 :     int32           stride[2] = {1, 1}; /* Stride array (read) */
    5933             :     int32           edge[2];  /* Edge array (read) */
    5934           0 :     int32           regionID = -1;  /* Region ID (return) */
    5935             :     int32           anyStart[2];/* ANYPOINT start array (read) */
    5936             :     int32           anyEdge[2]; /* ANYPOINT edge array (read) */
    5937             : 
    5938             :     float32         temp32; /* Temporary float32 variable */
    5939             : 
    5940             :     float64         lonTestVal; /* Longitude test value */
    5941             :     float64         latTestVal; /* Latitude test value */
    5942             :     float64         temp64; /* Temporary float64 variable */
    5943             : 
    5944             :     char           *lonArr; /* Longitude data array */
    5945             :     char           *latArr; /* Latitude data array */
    5946             :     char            dimlist[256]; /* Dimension list (geolocation
    5947             :            * fields) */
    5948             :     char            latName[17];/* Latitude field name */
    5949             : 
    5950             : 
    5951             :     /* Check for valid swath ID */
    5952             :     /* ------------------------ */
    5953           0 :     status = SWchkswid(swathID, "SWregionl_index", &fid, &sdInterfaceID,
    5954             :            &swVgrpID);
    5955             : 
    5956             : 
    5957             :     /* Inclusion mode must be between 0 and 2 */
    5958             :     /* -------------------------------------- */
    5959           0 :     if (mode < 0 || mode > 2)
    5960             :     {
    5961           0 :   status = -1;
    5962           0 :   HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__);
    5963           0 :   HEreport("Improper Inclusion Mode: %d.\n", mode);
    5964             :     }
    5965             : 
    5966             : 
    5967           0 :     if (status == 0)
    5968             :     {
    5969             :   /* Get "Longitude" field info */
    5970             :   /* -------------------------- */
    5971           0 :   statLon = SWfieldinfo(swathID, "Longitude", &rank, dims, &nt, dimlist);
    5972           0 :   if (statLon != 0)
    5973             :   {
    5974           0 :       status = -1;
    5975           0 :       HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__);
    5976           0 :       HEreport("\"Longitude\" field not found.\n");
    5977             :   }
    5978             : 
    5979             :   /* Get "Latitude" field info */
    5980             :   /* -------------------------- */
    5981           0 :   statLat = SWfieldinfo(swathID, "Latitude", &rank, dims, &nt, dimlist);
    5982           0 :   if (statLat != 0)
    5983             :   {
    5984             :       /* If not found check for "Colatitude" field info */
    5985             :       /* ---------------------------------------------- */
    5986           0 :       statCoLat = SWfieldinfo(swathID, "Colatitude", &rank, dims, &nt,
    5987             :             dimlist);
    5988           0 :       if (statCoLat != 0)
    5989             :       {
    5990             :         /* Check again for Geodeticlatitude */            
    5991           0 :         statGeodeticLat = SWfieldinfo(swathID, 
    5992             :               "GeodeticLatitude", &rank, 
    5993             :               dims, &nt, dimlist);
    5994           0 :         if (statGeodeticLat != 0)
    5995             :           {
    5996             :       /* Neither "Latitude" nor "Colatitude" field found */
    5997             :       /* ----------------------------------------------- */
    5998           0 :       status = -1;
    5999           0 :       HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__);
    6000           0 :       HEreport(
    6001             :          "Neither \"Latitude\" nor \"Colatitude\" fields found.\n");
    6002             :     }
    6003             :         else
    6004             :     {
    6005             :          /* Latitude field is "Colatitude" */
    6006             :          /* ------------------------------ */
    6007           0 :          strcpy(latName, "GeodeticLatitude");
    6008             :     }
    6009             :       }
    6010             :       else
    6011             :       {
    6012             :     /* Latitude field is "Colatitude" */
    6013             :     /* ------------------------------ */
    6014           0 :     strcpy(latName, "Colatitude");
    6015             :       }
    6016             :   }
    6017             :   else
    6018             :   {
    6019             :       /* Latitude field is "Latitude" */
    6020             :       /* ---------------------------- */
    6021           0 :       strcpy(latName, "Latitude");
    6022             :   }
    6023             : 
    6024             :         /* This line modifies the dimlist variable so only the along-track */
    6025             :         /* dimension remains.                                              */
    6026             :         /* --------------------------------------------------------------- */
    6027           0 :         (void) strtok(dimlist,",");
    6028           0 :         mapstatus = SWgeomapinfo(swathID,dimlist);
    6029           0 :         (void) strcpy(geodim,dimlist);
    6030             : 
    6031           0 :   if (status == 0)
    6032             :   {
    6033             :       /* Search along entire "Track" dimension from beginning to end */
    6034             :       /* ----------------------------------------------------------- */
    6035           0 :       start[0] = 0;
    6036           0 :       edge[0] = dims[0];
    6037             : 
    6038             : 
    6039             :       /* If 1D geolocation fields then set mode to MIDPOINT */
    6040             :       /* -------------------------------------------------- */
    6041           0 :       if (rank == 1)
    6042             :       {
    6043           0 :     mode = HDFE_MIDPOINT;
    6044             :       }
    6045             : 
    6046             : 
    6047           0 :       switch (mode)
    6048             :       {
    6049             :     /* If MIDPOINT search single point in middle of "CrossTrack" */
    6050             :     /* --------------------------------------------------------- */
    6051           0 :       case HDFE_MIDPOINT:
    6052             : 
    6053           0 :     start[1] = dims[1] / 2;
    6054           0 :     edge[1] = 1;
    6055             : 
    6056           0 :     break;
    6057             : 
    6058             :     /* If ENDPOINT search 2 points at either end of "CrossTrack" */
    6059             :     /* --------------------------------------------------------- */
    6060           0 :       case HDFE_ENDPOINT:
    6061             : 
    6062           0 :     start[1] = 0;
    6063           0 :     stride[1] = dims[1] - 1;
    6064           0 :     edge[1] = 2;
    6065             : 
    6066           0 :     break;
    6067             : 
    6068             :     /* If ANYPOINT do initial MIDPOINT search */
    6069             :     /* -------------------------------------- */
    6070           0 :       case HDFE_ANYPOINT:
    6071             : 
    6072           0 :     start[1] = dims[1] / 2;
    6073           0 :     edge[1] = 1;
    6074             : 
    6075           0 :     break;
    6076             :       }
    6077             : 
    6078             : 
    6079             :       /* Compute number of elements */
    6080             :       /* -------------------------- */
    6081           0 :       nElem = edge[0] * edge[1];
    6082             : 
    6083             : 
    6084             :       /* Allocate space for longitude and latitude (float64) */
    6085             :       /* --------------------------------------------------- */
    6086           0 :       lonArr = (char *) calloc(nElem, sizeof(float64));
    6087           0 :       if(lonArr == NULL)
    6088             :       { 
    6089           0 :     HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__);
    6090           0 :     return(-1);
    6091             :       }
    6092             :       
    6093           0 :       latArr = (char *) calloc(nElem, sizeof(float64));
    6094           0 :       if(latArr == NULL)
    6095             :       { 
    6096           0 :     HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__);
    6097           0 :     free(lonArr);
    6098           0 :     return(-1);
    6099             :       }
    6100             : 
    6101             : 
    6102             :       /* Allocate space for flag array (uint8) */
    6103             :       /* ------------------------------------- */
    6104           0 :       flag = (uint8 *) calloc(edge[0] + 1, 1);
    6105           0 :       if(flag == NULL)
    6106             :       { 
    6107           0 :     HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__);
    6108           0 :     free(lonArr);
    6109           0 :     free(latArr);
    6110           0 :     return(-1);
    6111             :       }
    6112             : 
    6113             : 
    6114             :       /* Read Longitude and Latitude fields */
    6115             :       /* ---------------------------------- */
    6116           0 :       status = SWreadfield(swathID, "Longitude",
    6117             :          start, stride, edge, lonArr);
    6118           0 :       status = SWreadfield(swathID, latName,
    6119             :          start, stride, edge, latArr);
    6120             : 
    6121             : 
    6122             : 
    6123             :       /*
    6124             :        * If geolocation fields are FLOAT32 then cast each entry as
    6125             :        * FLOAT64
    6126             :        */
    6127           0 :       if (nt == DFNT_FLOAT32)
    6128             :       {
    6129           0 :     for (i = nElem - 1; i >= 0; i--)
    6130             :     {
    6131           0 :         memcpy(&temp32, lonArr + 4 * i, 4);
    6132           0 :         temp64 = (float64) temp32;
    6133           0 :         memcpy(lonArr + 8 * i, &temp64, 8);
    6134             : 
    6135           0 :         memcpy(&temp32, latArr + 4 * i, 4);
    6136           0 :         temp64 = (float64) temp32;
    6137           0 :         memcpy(latArr + 8 * i, &temp64, 8);
    6138             :     }
    6139             :       }
    6140             : 
    6141             : 
    6142             :       /* Set boundary flag */
    6143             :       /* ----------------- */
    6144             : 
    6145             :       /*
    6146             :        * This variable is set to 1 if the region of interest crosses
    6147             :        * the +/- 180 longitude boundary
    6148             :        */
    6149           0 :       bndflag = (cornerlon[0] < cornerlon[1]) ? 0 : 1;
    6150             : 
    6151             : 
    6152             : 
    6153             :       /* Main Search Loop */
    6154             :       /* ---------------- */
    6155             : 
    6156             :       /* For each track ... */
    6157             :       /* ------------------ */
    6158           0 :       for (i = 0; i < edge[0]; i++)
    6159             :       {
    6160             :     /* For each value from Cross Track ... */
    6161             :     /* ----------------------------------- */
    6162           0 :     for (j = 0; j < edge[1]; j++)
    6163             :     {
    6164             :         /* Read in single lon & lat values from data buffers */
    6165             :         /* ------------------------------------------------- */
    6166           0 :         memcpy(&lonTestVal, &lonArr[8 * (i * edge[1] + j)], 8);
    6167           0 :         memcpy(&latTestVal, &latArr[8 * (i * edge[1] + j)], 8);
    6168             : 
    6169             : 
    6170             :         /* If longitude value > 180 convert to -180 to 180 range */
    6171             :         /* ----------------------------------------------------- */
    6172           0 :         if (lonTestVal > 180)
    6173             :         {
    6174           0 :       lonTestVal = lonTestVal - 360;
    6175             :         }
    6176             : 
    6177             :         /* If Colatitude value convert to latitude value */
    6178             :         /* --------------------------------------------- */
    6179           0 :         if (statCoLat == 0)
    6180             :         {
    6181           0 :       latTestVal = 90 - latTestVal;
    6182             :         }
    6183             : 
    6184             : 
    6185             :         /* Test if lat value is within range */
    6186             :         /* --------------------------------- */
    6187           0 :         latTest = (latTestVal >= cornerlat[0] &&
    6188           0 :              latTestVal <= cornerlat[1]);
    6189             : 
    6190             : 
    6191           0 :         if (bndflag == 1)
    6192             :         {
    6193             :       /*
    6194             :        * If boundary flag set test whether longitude value
    6195             :        * is outside region and then flip
    6196             :        */
    6197           0 :       lonTest = (lonTestVal >= cornerlon[1] &&
    6198           0 :            lonTestVal <= cornerlon[0]);
    6199           0 :       lonTest = 1 - lonTest;
    6200             :         }
    6201             :         else
    6202             :         {
    6203           0 :       lonTest = (lonTestVal >= cornerlon[0] &&
    6204           0 :            lonTestVal <= cornerlon[1]);
    6205             :         }
    6206             : 
    6207             : 
    6208             :         /*
    6209             :          * If both longitude and latitude are within region set
    6210             :          * flag on for this track
    6211             :          */
    6212           0 :         if (lonTest + latTest == 2)
    6213             :         {
    6214           0 :       flag[i] = 1;
    6215           0 :       found = 1;
    6216           0 :       break;
    6217             :         }
    6218             :     }
    6219             :       }
    6220             : 
    6221             : 
    6222             : 
    6223             :       /* ANYPOINT search */
    6224             :       /* --------------- */
    6225           0 :       if (mode == HDFE_ANYPOINT && rank > 1)
    6226             :       {
    6227           0 :     free(lonArr);
    6228           0 :     free(latArr);
    6229             : 
    6230             :     /* Allocate space for an entire single cross track */
    6231             :     /* ----------------------------------------------- */
    6232           0 :     lonArr = (char *) calloc(dims[1], sizeof(float64));
    6233           0 :     if(lonArr == NULL)
    6234             :     { 
    6235           0 :         HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__);
    6236           0 :         return(-1);
    6237             :     }
    6238           0 :     latArr = (char *) calloc(dims[1], sizeof(float64));
    6239           0 :     if(latArr == NULL)
    6240             :     { 
    6241           0 :         HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__);
    6242           0 :         free(lonArr);
    6243           0 :         return(-1);
    6244             :     }
    6245             : 
    6246             :     /* Setup start and edge */
    6247             :     /* -------------------- */
    6248           0 :     anyStart[1] = 0;
    6249           0 :     anyEdge[0] = 1;
    6250           0 :     anyEdge[1] = dims[1];
    6251             : 
    6252             : 
    6253             :     /* For each track ... */
    6254             :     /* ------------------ */
    6255           0 :     for (i = 0; i < edge[0]; i++)
    6256             :     {
    6257             : 
    6258             :         /* If cross track not in region (with MIDPOINT search ... */
    6259             :         /* ------------------------------------------------------ */
    6260           0 :         if (flag[i] == 0)
    6261             :         {
    6262             :       /* Setup track start */
    6263             :       /* ----------------- */
    6264           0 :       anyStart[0] = i;
    6265             : 
    6266             : 
    6267             :       /* Read in lon and lat values for cross track */
    6268             :       /* ------------------------------------------ */
    6269           0 :       status = SWreadfield(swathID, "Longitude",
    6270             :              anyStart, NULL, anyEdge, lonArr);
    6271           0 :       status = SWreadfield(swathID, latName,
    6272             :              anyStart, NULL, anyEdge, latArr);
    6273             : 
    6274             : 
    6275             : 
    6276             :       /*
    6277             :        * If geolocation fields are FLOAT32 then cast each
    6278             :        * entry as FLOAT64
    6279             :        */
    6280           0 :       if (nt == DFNT_FLOAT32)
    6281             :       {
    6282           0 :           for (j = dims[1] - 1; j >= 0; j--)
    6283             :           {
    6284           0 :         memcpy(&temp32, lonArr + 4 * j, 4);
    6285           0 :         temp64 = (float64) temp32;
    6286           0 :         memcpy(lonArr + 8 * j, &temp64, 8);
    6287             : 
    6288           0 :         memcpy(&temp32, latArr + 4 * j, 4);
    6289           0 :         temp64 = (float64) temp32;
    6290           0 :         memcpy(latArr + 8 * j, &temp64, 8);
    6291             :           }
    6292             :       }
    6293             : 
    6294             : 
    6295             :       /* For each value from Cross Track ... */
    6296             :       /* ----------------------------------- */
    6297           0 :       for (j = 0; j < dims[1]; j++)
    6298             :       {
    6299             :           /* Read in single lon & lat values from buffers */
    6300             :           /* -------------------------------------------- */
    6301           0 :           memcpy(&lonTestVal, &lonArr[8 * j], 8);
    6302           0 :           memcpy(&latTestVal, &latArr[8 * j], 8);
    6303             : 
    6304             : 
    6305             :           /* If lon value > 180 convert to -180 - 180 range */
    6306             :           /* ---------------------------------------------- */
    6307           0 :           if (lonTestVal > 180)
    6308             :           {
    6309           0 :         lonTestVal = lonTestVal - 360;
    6310             :           }
    6311             : 
    6312             :           /* If Colatitude value convert to latitude value */
    6313             :           /* --------------------------------------------- */
    6314           0 :           if (statCoLat == 0)
    6315             :           {
    6316           0 :         latTestVal = 90 - latTestVal;
    6317             :           }
    6318             : 
    6319             : 
    6320             :           /* Test if lat value is within range */
    6321             :           /* --------------------------------- */
    6322           0 :           latTest = (latTestVal >= cornerlat[0] &&
    6323           0 :                latTestVal <= cornerlat[1]);
    6324             : 
    6325             : 
    6326           0 :           if (bndflag == 1)
    6327             :           {
    6328             :         /*
    6329             :          * If boundary flag set test whether
    6330             :          * longitude value is outside region and then
    6331             :          * flip
    6332             :          */
    6333           0 :         lonTest = (lonTestVal >= cornerlon[1] &&
    6334           0 :              lonTestVal <= cornerlon[0]);
    6335           0 :         lonTest = 1 - lonTest;
    6336             :           }
    6337             :           else
    6338             :           {
    6339           0 :         lonTest = (lonTestVal >= cornerlon[0] &&
    6340           0 :              lonTestVal <= cornerlon[1]);
    6341             :           }
    6342             : 
    6343             : 
    6344             :           /*
    6345             :            * If both longitude and latitude are within
    6346             :            * region set flag on for this track
    6347             :            */
    6348           0 :           if (lonTest + latTest == 2)
    6349             :           {
    6350           0 :         flag[i] = 1;
    6351           0 :         found = 1;
    6352           0 :         break;
    6353             :           }
    6354             :       }
    6355             :         }
    6356             :     }
    6357             :       }
    6358             :       /*
    6359             :       for (j1 = 0; j1 < edge[0]; j1++)
    6360             :         {
    6361             :     idxrange[j1] = (int32) flag[j1];
    6362             :         }
    6363             :       */
    6364             :       /* If within region setup Region Structure */
    6365             :       /* --------------------------------------- */
    6366           0 :       if (found == 1)
    6367             :       {
    6368             :     /* For all entries in SWXRegion array ... */
    6369             :     /* -------------------------------------- */
    6370           0 :     for (k = 0; k < NSWATHREGN; k++)
    6371             :     {
    6372             :         /* If empty region ... */
    6373             :         /* ------------------- */
    6374           0 :         if (SWXRegion[k] == 0)
    6375             :         {
    6376             :       /* Allocate space for region entry */
    6377             :       /* ------------------------------- */
    6378           0 :       SWXRegion[k] = (struct swathRegion *)
    6379           0 :           calloc(1, sizeof(struct swathRegion));
    6380           0 :       if(SWXRegion[k] == NULL)
    6381             :       { 
    6382           0 :           HEpush(DFE_NOSPACE,"SWregionl_index", __FILE__, __LINE__);
    6383           0 :           return(-1);
    6384             :       }
    6385             : 
    6386             :       /* Store file and swath ID */
    6387             :       /* ----------------------- */
    6388           0 :       SWXRegion[k]->fid = fid;
    6389           0 :       SWXRegion[k]->swathID = swathID;
    6390             : 
    6391             : 
    6392             :       /* Set Start & Stop Vertical arrays to -1 */
    6393             :       /* -------------------------------------- */
    6394           0 :       for (j = 0; j < 8; j++)
    6395             :       {
    6396           0 :           SWXRegion[k]->StartVertical[j] = -1;
    6397           0 :           SWXRegion[k]->StopVertical[j] = -1;
    6398           0 :           SWXRegion[k]->StartScan[j] = -1;
    6399           0 :           SWXRegion[k]->StopScan[j] = -1;
    6400             :       }
    6401             : 
    6402             : 
    6403             :       /* Set region ID */
    6404             :       /* ------------- */
    6405           0 :       regionID = k;
    6406           0 :       break;
    6407             :         }
    6408             :     }
    6409           0 :     if (k >= NSWATHREGN)
    6410             :       {
    6411           0 :         HEpush(DFE_GENAPP, "SWregionl_index", __FILE__, __LINE__);
    6412           0 :         HEreport(
    6413             :            "regionID exceeded NSWATHREGN.\n");
    6414           0 :                     return (-1);
    6415             :       }
    6416             : 
    6417             :     /* Find start and stop of regions */
    6418             :     /* ------------------------------ */
    6419             : 
    6420             :     /* Subtract previous flag value from current one */
    6421             :     /* --------------------------------------------- */
    6422             : 
    6423             :     /*
    6424             :      * Transition points will have flag value (+1) start or
    6425             :      * (255 = (uint8) -1) stop of region
    6426             :      */
    6427           0 :     for (i = edge[0]; i > 0; i--)
    6428             :     {
    6429           0 :         flag[i] -= flag[i - 1];
    6430             :     }
    6431             : 
    6432             : 
    6433           0 :     for (i = 0; i <= edge[0]; i++)
    6434             :     {
    6435             :         /* Start of region */
    6436             :         /* --------------- */
    6437           0 :         if (flag[i] == 1)
    6438             :         {
    6439             :       /* Delyth Jones Moved the increment of the region down
    6440             :          to next if statement j = ++SWXRegion[k]->nRegions; */
    6441             : 
    6442             :       /* using temp value, if not equal to stop region
    6443             :          invalid region otherwise ok Delyth Jones */
    6444           0 :       tmpVal = i+1;
    6445             :         }
    6446             : 
    6447             :         /* End of region */
    6448             :         /* ------------- */
    6449           0 :         if (flag[i] == 255)
    6450             :         {
    6451           0 :       if( tmpVal!=i )
    6452             :       {
    6453             :           /* Increment (multiple) region counter */
    6454             :           /* ----------------------------------- */
    6455           0 :           j = ++SWXRegion[k]->nRegions;
    6456             :           
    6457           0 :                             if (mapstatus == 2)
    6458             :                             {
    6459           0 :                                l = i;
    6460           0 :                                if ((tmpVal-1) % 2 == 1)
    6461             :                                {
    6462           0 :                                   tmpVal = tmpVal + 1;
    6463             :                                }
    6464             : 
    6465           0 :                                if ((l-1) % 2 == 0)
    6466             :                                {
    6467           0 :                                   l = l - 1;
    6468             :                                }
    6469             :                             }
    6470           0 :           SWXRegion[k]->StartRegion[j - 1] = tmpVal-1;
    6471           0 :                             idxrange[0] = tmpVal - 1;
    6472           0 :           SWXRegion[k]->StopRegion[j - 1] = l - 1;
    6473           0 :                             idxrange[1] = l - 1;
    6474           0 :           validReg = 0;
    6475             :       }
    6476             :         }
    6477             :         
    6478             :     }
    6479             : 
    6480             :       }
    6481           0 :       free(lonArr);
    6482           0 :       free(latArr);
    6483           0 :       free(flag);
    6484             :   }
    6485             :     }
    6486           0 :     if(validReg==0)
    6487             :     {
    6488           0 :   return (regionID);
    6489             :     }
    6490             :     else
    6491             :     {
    6492           0 :   return (-1);
    6493             :     }
    6494             :     
    6495             : }
    6496             : 
    6497             : 
    6498             : 
    6499             : /*----------------------------------------------------------------------------|
    6500             : |  BEGIN_PROLOG                                                               |
    6501             : |                                                                             |
    6502             : |  FUNCTION: SWdeftimeperiod                                                  |
    6503             : |                                                                             |
    6504             : |  DESCRIPTION: Finds swath cross tracks observed during time period and      |
    6505             : |               returns  period ID                                            |
    6506             : |                                                                             |
    6507             : |               region ID                                                     |
    6508             : |  DESCRIPTION:                                                               |
    6509             : |                                                                             |
    6510             : |                                                                             |
    6511             : |  Return Value    Type     Units     Description                             |
    6512             : |  ============   ======  =========   =====================================   |
    6513             : |  periodID       int32               (Period ID) or (-1) if failed           |
    6514             : |                                                                             |
    6515             : |  INPUTS:                                                                    |
    6516             : |  swathID        int32               Swath structure ID                      |
    6517             : |  starttime      float64 TAI sec     Start of time period                    |
    6518             : |  stoptime       float64 TAI sec     Stop of time period                     |
    6519             : |  mode           int32               Search mode                             |
    6520             : |                                     HDFE_MIDPOINT - Use midpoint of Xtrack  |
    6521             : |                                     HDFE_ENDPOINT - Use endpoints of Xtrack |
    6522             : |                                                                             |
    6523             : |  OUTPUTS:                                                                   |
    6524             : |             None                                                            |
    6525             : |                                                                             |
    6526             : |  NOTES:                                                                     |
    6527             : |                                                                             |
    6528             : |                                                                             |
    6529             : |   Date     Programmer   Description                                         |
    6530             : |  ======   ============  =================================================   |
    6531             : |  Jun 96   Joel Gales    Original Programmer                                 |
    6532             : |                                                                             |
    6533             : |  END_PROLOG                                                                 |
    6534             : -----------------------------------------------------------------------------*/
    6535             : int32
    6536           0 : SWdeftimeperiod(int32 swathID, float64 starttime, float64 stoptime,
    6537             :     int32 mode)
    6538             : {
    6539             : 
    6540             :     intn            i;        /* Loop index */
    6541             :     intn            j;        /* Loop index */
    6542           0 :     intn            k = 0;        /* Loop index */
    6543             :     intn            status;     /* routine return status variable */
    6544             :     intn            statTime;     /* Status from SWfieldinfo for time */
    6545             : 
    6546           0 :     uint8           found = 0;      /* Found flag */
    6547             : 
    6548             :     int32           fid;      /* HDF-EOS file ID */
    6549             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    6550             :     int32           swVgrpID;     /* Swath Vgroup ID */
    6551             :     int32           rank;     /* Rank of geolocation fields */
    6552             :     int32           nt;       /* Number type of geolocation fields */
    6553             :     int32           dims[8];      /* Dimensions of geolocation fields */
    6554             :     int32           start[2];     /* Start array (read) */
    6555           0 :     int32           stride[2] = {1, 1}; /* Stride array (read) */
    6556             :     int32           edge[2];      /* Edge array (read) */
    6557           0 :     int32           periodID = -1;  /* Period ID (return) */
    6558             :     int32           dum;      /* Dummy (loop) variable */
    6559             : 
    6560             :     float64         time64Test;     /* Time test value */
    6561           0 :     float64        *time64 = NULL;  /* Time data array */
    6562             : 
    6563             :     char            dimlist[256];   /* Dimension list (geolocation fields) */
    6564             : 
    6565             :     /* Check for valid swath ID */
    6566             :     /* ------------------------ */
    6567           0 :     status = SWchkswid(swathID, "SWdeftimeperiod", &fid, &sdInterfaceID,
    6568             :            &swVgrpID);
    6569             : 
    6570           0 :     if (status == 0)
    6571             :     {
    6572             :   /* Get "Time" field info */
    6573             :   /* --------------------- */
    6574           0 :   statTime = SWfieldinfo(swathID, "Time", &rank, dims, &nt, dimlist);
    6575           0 :   if (statTime != 0)
    6576             :   {
    6577           0 :       status = -1;
    6578           0 :       HEpush(DFE_GENAPP, "SWdeftimeperiod", __FILE__, __LINE__);
    6579           0 :       HEreport("\"Time\" field not found.\n");
    6580             :   }
    6581             : 
    6582           0 :   if (status == 0)
    6583             :   {
    6584             :       /* Search along entire "Track" dimension from beginning to end */
    6585             :       /* ----------------------------------------------------------- */
    6586           0 :       start[0] = 0;
    6587           0 :       edge[0] = dims[0];
    6588             : 
    6589             : 
    6590             :       /* If 1D geolocation fields then set mode to MIDPOINT */
    6591             :       /* -------------------------------------------------- */
    6592           0 :       if (rank == 1)
    6593             :       {
    6594           0 :     mode = HDFE_MIDPOINT;
    6595             :       }
    6596             : 
    6597             : 
    6598           0 :       switch (mode)
    6599             :       {
    6600             : 
    6601             :     /* If MIDPOINT search single point in middle of "CrossTrack" */
    6602             :     /* --------------------------------------------------------- */
    6603           0 :       case HDFE_MIDPOINT:
    6604             : 
    6605           0 :     start[1] = dims[1] / 2;
    6606           0 :     edge[1] = 1;
    6607             : 
    6608             : 
    6609             :     /* Allocate space for time data */
    6610             :     /* ---------------------------- */
    6611           0 :     time64 = (float64 *) calloc(edge[0], 8);
    6612           0 :     if(time64 == NULL)
    6613             :     { 
    6614           0 :         HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__);
    6615           0 :         return(-1);
    6616             :     }
    6617             : 
    6618             : 
    6619             :     /* Read "Time" field */
    6620             :     /* ----------------- */
    6621           0 :     status = SWreadfield(swathID, "Time",
    6622             :              start, NULL, edge, time64);
    6623           0 :     break;
    6624             : 
    6625             : 
    6626             :     /* If ENDPOINT search 2 points at either end of "CrossTrack" */
    6627             :     /* --------------------------------------------------------- */
    6628           0 :       case HDFE_ENDPOINT:
    6629           0 :     start[1] = 0;
    6630           0 :     stride[1] = dims[1] - 1;
    6631           0 :     edge[1] = 2;
    6632             : 
    6633             : 
    6634             :     /* Allocate space for time data */
    6635             :     /* ---------------------------- */
    6636           0 :     time64 = (float64 *) calloc(edge[0] * 2, 8);
    6637           0 :     if(time64 == NULL)
    6638             :     { 
    6639           0 :         HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__);
    6640           0 :         return(-1);
    6641             :     }
    6642             : 
    6643             :     /* Read "Time" field */
    6644             :     /* ----------------- */
    6645           0 :     status = SWreadfield(swathID, "Time",
    6646             :              start, stride, edge, time64);
    6647           0 :     break;
    6648             : 
    6649             :       }
    6650             : 
    6651           0 :             if (time64)
    6652             :             {
    6653             :                 /* For each track (from top) ... */
    6654             :                 /* ----------------------------- */
    6655           0 :                 for (i = 0; i < edge[0]; i++)
    6656             :                 {
    6657             :                     /* For each value from Cross Track ... */
    6658             :                     /* ----------------------------------- */
    6659           0 :                     for (j = 0; j < edge[1]; j++)
    6660             :                     {
    6661             : 
    6662             :                         /* Get time test value */
    6663             :                         /* ------------------- */
    6664           0 :                         time64Test = time64[i * edge[1] + j];
    6665             : 
    6666             : 
    6667             :                         /* If within time period ... */
    6668             :                         /* ------------------------- */
    6669           0 :                         if (time64Test >= starttime &&
    6670             :                             time64Test <= stoptime)
    6671             :                         {
    6672             :                             /* Set found flag */
    6673             :                             /* -------------- */
    6674           0 :                             found = 1;
    6675             : 
    6676             : 
    6677             :                             /* For all entries in SWXRegion array ... */
    6678             :                             /* -------------------------------------- */
    6679           0 :                             for (k = 0; k < NSWATHREGN; k++)
    6680             :                             {
    6681             :                                 /* If empty region ... */
    6682             :                                 /* ------------------- */
    6683           0 :                                 if (SWXRegion[k] == 0)
    6684             :                                 {
    6685             :                                     /* Allocate space for region entry */
    6686             :                                     /* ------------------------------- */
    6687           0 :                                     SWXRegion[k] = (struct swathRegion *)
    6688           0 :                                         calloc(1, sizeof(struct swathRegion));
    6689           0 :                                     if(SWXRegion[k] == NULL)
    6690             :                                     { 
    6691           0 :                                         HEpush(DFE_NOSPACE,"SWdeftimeperiod", __FILE__, __LINE__);
    6692           0 :                                         return(-1);
    6693             :                                     }
    6694             : 
    6695             :                                     /* Store file and swath ID */
    6696             :                                     /* ----------------------- */
    6697           0 :                                     SWXRegion[k]->fid = fid;
    6698           0 :                                     SWXRegion[k]->swathID = swathID;
    6699             : 
    6700             : 
    6701             :                                     /* Set number of isolated regions to 1 */
    6702             :                                     /* ----------------------------------- */
    6703           0 :                                     SWXRegion[k]->nRegions = 1;
    6704             : 
    6705             : 
    6706             :                                     /* Set start of region to first track found */
    6707             :                                     /* ---------------------------------------- */
    6708           0 :                                     SWXRegion[k]->StartRegion[0] = i;
    6709             : 
    6710             : 
    6711             :                                     /* Set Start & Stop Vertical arrays to -1 */
    6712             :                                     /* -------------------------------------- */
    6713           0 :                                     for (dum = 0; dum < 8; dum++)
    6714             :                                     {
    6715           0 :                                         SWXRegion[k]->StartVertical[dum] = -1;
    6716           0 :                                         SWXRegion[k]->StopVertical[dum] = -1;
    6717           0 :                                         SWXRegion[k]->StartScan[dum] = -1;
    6718           0 :                                         SWXRegion[k]->StopScan[dum] = -1;
    6719             :                                     }
    6720             : 
    6721             : 
    6722             :                                     /* Set period ID */
    6723             :                                     /* ------------- */
    6724           0 :                                     periodID = k;
    6725             : 
    6726           0 :                                     break;  /* Break from "k" loop */
    6727             :                                 }
    6728             :                             }
    6729             :                         }
    6730           0 :                         if (found == 1)
    6731             :                         {
    6732           0 :                             break;  /* Break from "j" loop */
    6733             :                         }
    6734             :                     }
    6735           0 :                     if (found == 1)
    6736             :                     {
    6737           0 :                         break;  /* Break from "i" loop */
    6738             :                     }
    6739             :                 }
    6740             : 
    6741             : 
    6742             : 
    6743             :                 /* Clear found flag */
    6744             :                 /* ---------------- */
    6745           0 :                 found = 0;
    6746             : 
    6747             : 
    6748             :                 /* For each track (from bottom) ... */
    6749             :                 /* -------------------------------- */
    6750           0 :                 for (i = edge[0] - 1; i >= 0; i--)
    6751             :                 {
    6752             :                     /* For each value from Cross Track ... */
    6753             :                     /* ----------------------------------- */
    6754           0 :                     for (j = 0; j < edge[1]; j++)
    6755             :                     {
    6756             : 
    6757             :                         /* Get time test value */
    6758             :                         /* ------------------- */
    6759           0 :                         time64Test = time64[i * edge[1] + j];
    6760             : 
    6761             : 
    6762             :                         /* If within time period ... */
    6763             :                         /* ------------------------- */
    6764           0 :                         if (time64Test >= starttime &&
    6765             :                             time64Test <= stoptime)
    6766             :                         {
    6767             :                             /* Set found flag */
    6768             :                             /* -------------- */
    6769           0 :                             found = 1;
    6770             : 
    6771             :                             /* Set start of region to first track found */
    6772             :                             /* ---------------------------------------- */
    6773           0 :                             SWXRegion[k]->StopRegion[0] = i;
    6774             : 
    6775           0 :                             break;  /* Break from "j" loop */
    6776             :                         }
    6777             :                     }
    6778           0 :                     if (found == 1)
    6779             :                     {
    6780           0 :                         break;  /* Break from "i" loop */
    6781             :                     }
    6782             :                 }
    6783             : 
    6784           0 :                 free(time64);
    6785             :             }
    6786             :   }
    6787             :     }
    6788             : 
    6789           0 :     return (periodID);
    6790             : }
    6791             : 
    6792             : /*----------------------------------------------------------------------------|
    6793             : |  BEGIN_PROLOG                                                               |
    6794             : |                                                                             |
    6795             : |  FUNCTION: SWextractregion                                                  |
    6796             : |                                                                             |
    6797             : |  DESCRIPTION: Retrieves data from specified region.                         |
    6798             : |                                                                             |
    6799             : |                                                                             |
    6800             : |  Return Value    Type     Units     Description                             |
    6801             : |  ============   ======  =========   =====================================   |
    6802             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    6803             : |                                                                             |
    6804             : |  INPUTS:                                                                    |
    6805             : |  swathID        int32               Swath structure ID                      |
    6806             : |  regionID       int32               Region ID                               |
    6807             : |  fieldname      char                Fieldname                               |
    6808             : |  externalflag   int32               External geolocation fields flag        |
    6809             : |                                     HDFE_INTERNAL (0)                       |
    6810             : |                                     HDFE_EXTERNAL (1)                       |
    6811             : |                                                                             |
    6812             : |  OUTPUTS:                                                                   |
    6813             : |  buffer         void                Data buffer containing subsetted region |
    6814             : |                                                                             |
    6815             : |  NOTES:                                                                     |
    6816             : |                                                                             |
    6817             : |                                                                             |
    6818             : |   Date     Programmer   Description                                         |
    6819             : |  ======   ============  =================================================   |
    6820             : |  Jun 96   Joel Gales    Original Programmer                                 |
    6821             : |  Aug 96   Joel Gales    Add vertical subsetting                             |
    6822             : |  Oct 96   Joel Gales    Mapping offset value not read from SWmapinfo        |
    6823             : |  Dec 96   Joel Gales    Vert Subset overwriting data buffer                 |
    6824             : |  Dec 96   Joel Gales    Add multiple vertical subsetting capability         |
    6825             : |  Mar 97   Joel Gales    Add support for l_index mapping                       |
    6826             : |  Jul 99   DaW     Add support for floating scene subsetting         |
    6827             : |  Feb 03   Terry Haran/                                                      |
    6828             : |           Abe Taaheri   Forced map offset to 0 so that data is extracted    |
    6829             : |                         without offset consideration. This will preserve    |
    6830             : |                         original mapping between geofields and the data     |
    6831             : |                         field.                                              |
    6832             : |                                                                             |
    6833             : |  END_PROLOG                                                                 |
    6834             : -----------------------------------------------------------------------------*/
    6835             : intn
    6836           0 : SWextractregion(int32 swathID, int32 regionID, const char *fieldname,
    6837             :     int32 externalflag, VOIDP buffer)
    6838             : 
    6839             : {
    6840             :     intn            i;    /* Loop index */
    6841             :     intn            j;    /* Loop index */
    6842             :     intn            k;    /* Loop index */
    6843             :     intn            l;    /* Loop index */
    6844             :     intn            status; /* routine return status variable */
    6845           0 :     intn            long_status = 3;  /* routine return status variable    */
    6846             :                                         /* for longitude                     */
    6847           0 :     intn      land_status = 3;  /* Used for L7 float scene sub.      */
    6848           0 :     intn            statMap = -1; /* Status from SWmapinfo  */
    6849             : 
    6850           0 :     uint8           found = 0;    /* Found flag */
    6851           0 :     uint8           vfound = 0;   /* Found flag for vertical subsetting*/
    6852             :                                         /*  --- xhua                         */
    6853           0 :     uint8     scene_cnt = 0;  /* Used for L7 float scene sub.      */
    6854           0 :     uint8           detect_cnt = 0;     /* Used to convert scan to scanline  */
    6855             :                                         /*  L7 float scene sub.              */
    6856             : 
    6857             :     int32           fid;  /* HDF-EOS file ID */
    6858             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    6859             :     int32           swVgrpID; /* Swath Vgroup ID */
    6860             : 
    6861           0 :     int32     numtype = 0; /* Used for L7 float scene sub. */
    6862           0 :     int32     count = 0;   /* Used for L7 float scene sub. */
    6863           0 :     int32           l_index = 0;  /* Geo Dim Index */
    6864             :     int32           nDim; /* Number of dimensions */
    6865             :     int32           slen[64]; /* String length array */
    6866             :     int32           dum;  /* Dummy variable */
    6867           0 :     int32           offset = 0; /* Mapping offset */
    6868           0 :     int32           incr = 0; /* Mapping increment */
    6869             :     int32           nXtrk;  /* Number of cross tracks */
    6870           0 :     int32           scan_shift = 0; /* Used to take out partial scans */
    6871             :     int32           dumdims[8]; /* Dimensions from SWfieldinfo */
    6872             :     int32           start[8]; /* Start array for data read */
    6873             :     int32           edge[8];  /* Edge array for data read */
    6874             :     int32           dims[8];  /* Dimensions */
    6875           0 :     int32           rank = 0; /* Field rank */
    6876           0 :     int32           rk = 0; /* Field rank */
    6877           0 :     int32           ntype = 0;  /* Field number type */
    6878           0 :     int32           bufOffset = 0;  /* Output buffer offset */
    6879             :     int32           size; /* Size of data buffer */
    6880           0 :     int32           idxMapElem = -1;  /* Number of l_index map elements  */
    6881           0 :     int32          *idxmap = NULL;  /* Pointer to l_index mapping array */
    6882             : 
    6883           0 :     int32 startscanline = 0;
    6884           0 :     int32 stopscanline = 0;
    6885           0 :     char  *dfieldlist = (char *)NULL;
    6886           0 :     int32 strbufsize = 0;
    6887             :     int32 dfrank[8];
    6888             :     int32 numtype2[8];
    6889           0 :     uint16  *buffer2 = (uint16 *)NULL;
    6890           0 :     uint16  *tbuffer = (uint16 *)NULL;
    6891             :     int32 dims2[8];
    6892           0 :     int32 nt = 0;
    6893           0 :     int32 startscandim = -1;
    6894           0 :     int32 stopscandim = -1;
    6895           0 :     int32 rank2 = 0;
    6896             : 
    6897             :     char            dimlist[256]; /* Dimension list */
    6898             :     char            geodim[256];/* Geolocation field dimension list */
    6899             :     char            tgeodim[256];/* Time field dimension list */
    6900             :     char            dgeodim[256];/* Data field dimension list for subsetting */
    6901             :     char            utlbuf[256];/* Utility buffer */
    6902             :     char           *ptr[64];  /* String pointer array */
    6903             : 
    6904             : 
    6905             : 
    6906             :     /* Check for valid swath ID */
    6907             :     /* ------------------------ */
    6908           0 :     status = SWchkswid(swathID, "SWextractregion", &fid, &sdInterfaceID,
    6909             :            &swVgrpID);
    6910             : 
    6911             : 
    6912             :     /* Check for valid region ID */
    6913             :     /* ------------------------- */
    6914           0 :     if (status == 0)
    6915             :     {
    6916           0 :   if (regionID < 0 || regionID >= NSWATHREGN)
    6917             :   {
    6918           0 :       status = -1;
    6919           0 :       HEpush(DFE_RANGE, "SWextractregion", __FILE__, __LINE__);
    6920           0 :       HEreport("Invalid Region id: %d.\n", regionID);
    6921             :   }
    6922             :     }
    6923             : 
    6924             : 
    6925             : 
    6926             :     /* Check for active region ID */
    6927             :     /* -------------------------- */
    6928           0 :     if (status == 0)
    6929             :     {
    6930           0 :   if (SWXRegion[regionID] == 0)
    6931             :   {
    6932           0 :       status = -1;
    6933           0 :       HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
    6934           0 :       HEreport("Inactive Region ID: %d.\n", regionID);
    6935             :   }
    6936             :     }
    6937             : 
    6938             : 
    6939             :    /* This code checks for the attribute detector_count */
    6940             :    /* which is found in Landsat 7 files.  It is used    */
    6941             :    /* for some of the loops.                            */
    6942             :    /* ================================================= */
    6943           0 :    if (status == 0 && SWXRegion[regionID]->scanflag == 1)
    6944             :    {
    6945           0 :       land_status = SWattrinfo(swathID, "detector_count", &numtype, &count);
    6946           0 :       if (land_status == 0)
    6947           0 :          land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
    6948             :    }
    6949             : 
    6950             :     /* Check that geo file and data file are same for INTERNAL subsetting */
    6951             :     /* ------------------------------------------------------------------ */
    6952           0 :     if (status == 0)
    6953             :     {
    6954           0 :   if (SWXRegion[regionID]->fid != fid && externalflag != HDFE_EXTERNAL)
    6955             :   {
    6956           0 :       status = -1;
    6957           0 :       HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
    6958           0 :       HEreport("Region is not defined for this file.\n");
    6959             :   }
    6960             :     }
    6961             : 
    6962             : 
    6963             : 
    6964             :     /* Check that geo swath and data swath are same for INTERNAL subsetting */
    6965             :     /* -------------------------------------------------------------------- */
    6966           0 :     if (status == 0)
    6967             :     {
    6968           0 :   if (SWXRegion[regionID]->swathID != swathID &&
    6969             :       externalflag != HDFE_EXTERNAL)
    6970             :   {
    6971           0 :       status = -1;
    6972           0 :       HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
    6973           0 :       HEreport("Region is not defined for this Swath.\n");
    6974             :   }
    6975             :     }
    6976             : 
    6977             : 
    6978             : 
    6979             :     /* Check for valid fieldname */
    6980             :     /* ------------------------- */
    6981           0 :     if (status == 0)
    6982             :     {
    6983             : 
    6984             :   /* Get data field info */
    6985             :   /* ------------------- */
    6986           0 :   status = SWfieldinfo(swathID, fieldname, &rank,
    6987             :            dims, &ntype, dimlist);
    6988             : 
    6989           0 :   if (status != 0)
    6990             :   {
    6991           0 :       status = -1;
    6992           0 :       HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
    6993           0 :       HEreport("Field \"%s\" Not Found.\n", fieldname);
    6994             :   }
    6995             :     }
    6996             : 
    6997             : 
    6998             :     /* No problems so proceed ... */
    6999             :     /* -------------------------- */
    7000           0 :     if (status == 0)
    7001             :     {
    7002             : 
    7003             : 
    7004             :   /* Initialize start and edge for all dimensions */
    7005             :   /* -------------------------------------------- */
    7006           0 :   for (j = 0; j < rank; j++)
    7007             :   {
    7008           0 :       start[j] = 0;
    7009           0 :       edge[j] = dims[j];
    7010             :   }
    7011             : 
    7012             : 
    7013             :   /* Vertical Subset */
    7014             :   /* --------------- */
    7015           0 :   for (j = 0; j < 8; j++)
    7016             :   {
    7017             :       /* If active vertical subset ... */
    7018             :       /* ----------------------------- */
    7019           0 :       if (SWXRegion[regionID]->StartVertical[j] != -1)
    7020             :       {
    7021             : 
    7022             :     /* Find vertical dimension within dimlist */
    7023             :     /* -------------------------------------- */
    7024           0 :     dum = EHstrwithin(SWXRegion[regionID]->DimNamePtr[j],
    7025             :           dimlist, ',');
    7026             : 
    7027             :     /* If dimension found ... */
    7028             :     /* ---------------------- */
    7029           0 :     if (dum != -1)
    7030             :     {
    7031             :         /* Compute start and edge for vertical dimension */
    7032             :         /* --------------------------------------------- */
    7033           0 :                     vfound = 1;                   /* xhua */
    7034           0 :         start[dum] = SWXRegion[regionID]->StartVertical[j];
    7035           0 :         edge[dum] = SWXRegion[regionID]->StopVertical[j] -
    7036           0 :       SWXRegion[regionID]->StartVertical[j] + 1;
    7037             :     }
    7038             :     else
    7039             :     {
    7040             :         /* Vertical dimension not found */
    7041             :         /* ---------------------------- */
    7042           0 :         status = -1;
    7043           0 :         HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
    7044           0 :         HEreport("Vertical Dimension Not Found: \"%s\".\n",
    7045           0 :            SWXRegion[regionID]->DimNamePtr);
    7046             :     }
    7047             :       }
    7048             :   } /* End of Vertical Subset loop  */
    7049             : 
    7050             : 
    7051             : 
    7052             :   /* No problems so proceed ... */
    7053             :   /* -------------------------- */
    7054           0 :   if (status == 0)
    7055             :   {
    7056             :       /* If non-vertical subset regions defined ... */
    7057             :       /* ------------------------------------------ */
    7058           0 :       if (SWXRegion[regionID]->nRegions > 0)    
    7059             :       {
    7060             : 
    7061             :     /* Get geolocation dimension name */
    7062             :     /* ------------------------------ */
    7063           0 :     status = SWfieldinfo(SWXRegion[regionID]->swathID,
    7064             :              "Longitude", &dum,
    7065             :              dumdims, &dum, geodim);
    7066           0 :                 long_status = status;
    7067             : 
    7068             :                 /* If Time field being used, check for dimensions */
    7069             :                 /* ---------------------------------------------- */
    7070           0 :                 if (timeflag == 1)
    7071             :                 {
    7072             :                    /* code change to fix time subset bug for Landsat7 */
    7073             : 
    7074           0 :                    status = SWfieldinfo(SWXRegion[regionID]->swathID,
    7075             :                                         "Time", &dum,
    7076             :                                         dumdims, &dum, tgeodim);
    7077             :                    
    7078           0 :                    if (strcmp(geodim, tgeodim) != 0)
    7079             :                    {
    7080           0 :                       strcpy(geodim, tgeodim);
    7081             :                    }
    7082             :                 }
    7083           0 :                 timeflag = 0;
    7084             : 
    7085             :                 /* If defscanregion being used, get dimensions    */
    7086             :                 /* of field being used                            */
    7087             :                 /* ---------------------------------------------- */
    7088           0 :                 if (SWXRegion[regionID]->scanflag == 1)
    7089             :                 {
    7090           0 :                    (void) SWnentries(SWXRegion[regionID]->swathID,4,&strbufsize);
    7091           0 :                    dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char));
    7092           0 :                    (void) SWinqdatafields(SWXRegion[regionID]->swathID,dfieldlist,dfrank,numtype2);
    7093           0 :                    status = SWfieldinfo(SWXRegion[regionID]->swathID,dfieldlist,&dum,dumdims,&dum,dgeodim);
    7094             : 
    7095             :                    /* The dimensions have to be switched, because */
    7096             :                    /* the mappings force a geodim and datadim     */
    7097             :                    /* so to find the mapping, the dimensions must */
    7098             :                    /* be switched, but the subsetting will still  */
    7099             :                    /* be based on the correct dimensions          */
    7100             :                    /* ------------------------------------------- */
    7101           0 :                    if (strcmp(dgeodim,dimlist) != 0 || long_status == -1)
    7102             :                    {
    7103           0 :                       strcpy(geodim,dimlist);
    7104           0 :                       strcpy(dimlist,dgeodim);
    7105             :                    }
    7106             :                 }
    7107             : 
    7108             : 
    7109             :     /* Get "Track" (first) Dimension from geo dimlist */
    7110             :     /* ---------------------------------------------- */
    7111           0 :     nDim = EHparsestr(geodim, ',', ptr, slen);
    7112           0 :     geodim[slen[0]] = 0;
    7113             : 
    7114             : 
    7115             :     /* Parse Data Field Dimlist & find mapping */
    7116             :     /* --------------------------------------- */
    7117           0 :     nDim = EHparsestr(dimlist, ',', ptr, slen);
    7118             : 
    7119             : 
    7120             :     /* Loop through all dimensions and search for mapping */
    7121             :     /* -------------------------------------------------- */
    7122           0 :     for (i = 0; i < nDim; i++)
    7123             :     {
    7124           0 :         memcpy(utlbuf, ptr[i], slen[i]);
    7125           0 :         utlbuf[slen[i]] = 0;
    7126           0 :         statMap = SWmapinfo(swathID, geodim, utlbuf,
    7127             :           &offset, &incr);
    7128             : 
    7129             : 
    7130             :                     /*
    7131             :                      *  Force offset to 0.
    7132             :                      *  We're not changing the mapping, so we want
    7133             :                      *  the original offset to apply to the subsetted data.
    7134             :                      *  Otherwise, bad things happen, such as subsetting
    7135             :                      *  past the end of the original data, and being unable
    7136             :                      *  to read the first <offset> elements of the
    7137             :                      *  original data.
    7138             :                      *  The offset is only important for aligning the
    7139             :                      *  data with interpolated (incr > 0) or decimated
    7140             :                      *  (incr < 0) geolocation information for the data.
    7141             :                      */
    7142             : 
    7143           0 :                     offset = 0;
    7144             : 
    7145             : 
    7146             :         /* Mapping found */
    7147             :         /* ------------- */
    7148           0 :         if (statMap == 0)
    7149             :         {
    7150           0 :       found = 1;
    7151           0 :       l_index = i;
    7152           0 :       break;
    7153             :         }
    7154             :     }
    7155             : 
    7156             : 
    7157             :     /* If mapping not found check for geodim within dimlist */
    7158             :     /* ---------------------------------------------------- */
    7159           0 :     if (found == 0)
    7160             :     {
    7161           0 :         l_index = EHstrwithin(geodim, dimlist, ',');
    7162             : 
    7163             :         /* Geo dimension found within subset field dimlist */
    7164             :         /* ----------------------------------------------- */
    7165           0 :         if (l_index != -1)
    7166             :         {
    7167           0 :       found = 1;
    7168           0 :       offset = 0;
    7169           0 :       incr = 1;
    7170             :         }
    7171             :     }
    7172             : 
    7173             : 
    7174             : 
    7175             :     /* If mapping not found check for l_indexed mapping */
    7176             :     /* ---------------------------------------------- */
    7177           0 :     if (found == 0)
    7178             :     {
    7179             :         /* Get size of geo dim & allocate space of l_index mapping */
    7180             :         /* ----------------------------------------------------- */
    7181           0 :         dum = SWdiminfo(swathID, geodim);
    7182             : 
    7183             :                     /* For Landsat files, the l_index mapping has two values   */
    7184             :                     /* for each point, a left and right point.  So for a 37  */
    7185             :                     /* scene band file there are 2x2 points for each scene   */
    7186             :                     /* meaning, 2x2x37 = 148 values.  The above function     */
    7187             :                     /* only returns the number of values in the track        */
    7188             :                     /* dimension.                                            */
    7189             :                     /* ----------------------------------------------------- */
    7190           0 :                     if(land_status == 0)
    7191           0 :                        if(strcmp(fieldname, "Latitude") == 0 || 
    7192           0 :                           strcmp(fieldname, "Longitude") == 0)
    7193             :                        {
    7194           0 :                           dum = dum * 2;
    7195             :                        }
    7196           0 :         idxmap = (int32 *) calloc(dum, sizeof(int32));
    7197           0 :         if(idxmap == NULL)
    7198             :         { 
    7199           0 :       HEpush(DFE_NOSPACE,"SWextractregion", __FILE__, __LINE__);
    7200           0 :       return(-1);
    7201             :         }
    7202             : 
    7203             :         /* Loop through all dimensions and search for mapping */
    7204             :         /* -------------------------------------------------- */
    7205           0 :         for (i = 0; i < nDim; i++)
    7206             :         {
    7207           0 :       memcpy(utlbuf, ptr[i], slen[i]);
    7208           0 :       utlbuf[slen[i]] = 0;
    7209             : 
    7210             :       idxMapElem =
    7211           0 :           SWidxmapinfo(swathID, geodim, utlbuf, idxmap);
    7212             : 
    7213             : 
    7214             :       /* Mapping found */
    7215             :       /* ------------- */
    7216           0 :       if (idxMapElem != -1)
    7217             :       {
    7218           0 :           found = 1;
    7219           0 :           l_index = i;
    7220           0 :           break;
    7221             :       }
    7222             :         }
    7223             :     }
    7224             : 
    7225             : 
    7226             :     /* If regular mapping found ... */
    7227             :     /* ---------------------------- */
    7228           0 :     if (found == 1 && idxMapElem == -1)
    7229             :     {
    7230           0 :         for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
    7231             :         {
    7232           0 :       if (k > 0)
    7233             :       {
    7234             :           /* Compute size in bytes of previous region */
    7235             :           /* ---------------------------------------- */
    7236           0 :           size = edge[0];
    7237           0 :           for (j = 1; j < rank; j++)
    7238             :           {
    7239           0 :         size *= edge[j];
    7240             :           }
    7241           0 :           size *= DFKNTsize(ntype);
    7242             : 
    7243             : 
    7244             :           /* Compute output buffer offset */
    7245             :           /* ---------------------------- */
    7246           0 :           bufOffset += size;
    7247             :       }
    7248             :       else
    7249             :       {
    7250             :           /* Initialize output buffer offset */
    7251             :           /* ------------------------------- */
    7252           0 :           bufOffset = 0;
    7253             :       }
    7254             : 
    7255             : 
    7256             :       /* Compute number of cross tracks in region */
    7257             :       /* ---------------------------------------- */
    7258           0 :       nXtrk = SWXRegion[regionID]->StopRegion[k] -
    7259           0 :           SWXRegion[regionID]->StartRegion[k] + 1;
    7260             : 
    7261             : 
    7262             :       /* Positive increment (geodim <= datadim) */
    7263             :       /* -------------------------------------- */
    7264           0 :       if (incr > 0)
    7265             :       {
    7266           0 :                            if (SWXRegion[regionID]->scanflag == 1)
    7267             :                            {
    7268           0 :                               start[l_index] = SWXRegion[regionID]->StartRegion[k]/incr;
    7269           0 :                               if(SWXRegion[regionID]->band8flag == 2 || 
    7270           0 :                                  SWXRegion[regionID]->band8flag == 3)
    7271             :                               {
    7272           0 :                                  start[l_index] = (SWXRegion[regionID]->StartRegion[k]+detect_cnt)/incr;
    7273           0 :                                  status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank,dims2,&nt,dimlist);
    7274           0 :                                  buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
    7275           0 :                                  status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
    7276           0 :                                  if(incr == 1)
    7277           0 :                                     start[l_index] = start[l_index] - (buffer2[0] * detect_cnt);
    7278             :                                  else
    7279           0 :                                     start[l_index] = start[l_index] - buffer2[0];
    7280           0 :                                  free(buffer2);
    7281             :                               }
    7282           0 :                               scan_shift = nXtrk % incr;
    7283           0 :                               if(scan_shift != 0)
    7284           0 :                                  nXtrk = nXtrk - scan_shift;
    7285           0 :             edge[l_index] = nXtrk / incr;
    7286           0 :                               if (nXtrk % incr != 0)
    7287           0 :                                  edge[l_index]++;
    7288           0 :                               if(long_status == -1 || incr == 1)
    7289             :                               {
    7290           0 :                                  if( detect_cnt == 0 )
    7291           0 :                                      return -1;
    7292           0 :                                  scan_shift = nXtrk % detect_cnt;
    7293           0 :                                  if(scan_shift != 0)
    7294           0 :                                     edge[l_index] = nXtrk - scan_shift;
    7295             :                               }
    7296             : 
    7297             :                            }
    7298             :                            else
    7299             :                            {
    7300           0 :             start[l_index] = SWXRegion[regionID]->StartRegion[k] * incr + offset;
    7301           0 :             edge[l_index] = nXtrk * incr - offset;
    7302             :                            }
    7303             :       }
    7304             :       else
    7305             :       {
    7306             :           /* Negative increment (geodim > datadim) */
    7307             :           /* ------------------------------------- */
    7308           0 :           start[l_index] = SWXRegion[regionID]->StartRegion[k]
    7309           0 :         / (-incr) + offset;
    7310           0 :           edge[l_index] = nXtrk / (-incr);
    7311             : 
    7312             :           /*
    7313             :            * If Xtrk not exactly divisible by incr, round
    7314             :            * edge to next highest integer
    7315             :            */
    7316             : 
    7317           0 :           if (nXtrk % (-incr) != 0)
    7318             :           {
    7319           0 :         edge[l_index]++;
    7320             :           }
    7321             :       }
    7322             : 
    7323             : 
    7324             :       /* Read Data into output buffer */
    7325             :       /* ---------------------------- */
    7326           0 :       status = SWreadfield(swathID, fieldname,
    7327             :                start, NULL, edge,
    7328             :                (uint8 *) buffer + bufOffset);
    7329             :         }
    7330             :     }
    7331           0 :     else if (found == 1 && idxMapElem != -1)
    7332             :     {
    7333             :         /* Indexed Mapping */
    7334             :         /* --------------- */
    7335           0 :         for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
    7336             :         {
    7337           0 :       if (k > 0)
    7338             :       {
    7339             :           /* Compute size in bytes of previous region */
    7340             :           /* ---------------------------------------- */
    7341           0 :           size = edge[0];
    7342           0 :           for (j = 1; j < rank; j++)
    7343             :           {
    7344           0 :         size *= edge[j];
    7345             :           }
    7346           0 :           size *= DFKNTsize(ntype);
    7347             : 
    7348             : 
    7349             :           /* Compute output buffer offset */
    7350             :           /* ---------------------------- */
    7351           0 :           bufOffset += size;
    7352             :       }
    7353             :       else
    7354             :       {
    7355             :           /* Initialize output buffer offset */
    7356             :           /* ------------------------------- */
    7357           0 :           bufOffset = 0;
    7358             :       }
    7359             : 
    7360             : 
    7361             :       /* Compute start & edge from l_index mappings */
    7362             :       /* ---------------------------------------- */
    7363           0 :                         if (SWXRegion[regionID]->scanflag == 1 &&
    7364           0 :                             (strcmp(fieldname, "Latitude") == 0 ||
    7365           0 :                              strcmp(fieldname, "Longitude") == 0))
    7366             :                         {
    7367           0 :                            if (land_status == 0)
    7368           0 :                               status = SWreadattr(swathID, "scene_count", &scene_cnt);
    7369           0 :                            startscanline = SWXRegion[regionID]->StartRegion[k];
    7370           0 :                            stopscanline = SWXRegion[regionID]->StopRegion[k];
    7371           0 :                            if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3)
    7372             :                            {
    7373           0 :                                status = SWfieldinfo(swathID,"scan_no",&rk,dims2,&nt,dimlist);
    7374           0 :                                tbuffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
    7375           0 :                                status =SWreadfield(swathID,"scan_no",NULL,NULL,NULL,tbuffer);
    7376           0 :                                startscanline = startscanline - ((tbuffer[0] * detect_cnt) - detect_cnt);
    7377           0 :                                stopscanline = stopscanline - ((tbuffer[0] * detect_cnt) - 1);
    7378             :                            }
    7379           0 :                            if(SWXRegion[regionID]->band8flag == 2 ||
    7380           0 :                               SWXRegion[regionID]->band8flag == 3)
    7381             :                            {
    7382           0 :                               if(startscandim == -1)
    7383           0 :                                  if(startscanline < idxmap[0])
    7384             :                                  {
    7385           0 :                                     startscandim = 0;
    7386           0 :                                     start[l_index] = 0;
    7387           0 :                                     if(stopscanline > idxmap[scene_cnt * 2 - 1])
    7388             :                                     {
    7389           0 :                                        stopscandim = scene_cnt*2 - startscandim;
    7390           0 :                                        edge[l_index] = scene_cnt*2 - startscandim;
    7391             :                                     }
    7392             :                                  }
    7393             :                            }
    7394           0 :                            j = 0;
    7395           0 :                            for (l = 0; l < scene_cnt; l++)
    7396             :                            {
    7397           0 :                               if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
    7398           0 :                                  if(startscandim == -1)
    7399             :                                  {
    7400           0 :                                     start[l_index] = j;
    7401           0 :                                     startscandim = j;
    7402             :                                  }
    7403           0 :                               if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
    7404           0 :                                  if(startscandim != -1)
    7405             :                                  {
    7406           0 :                                     edge[l_index] = j - start[l_index] + 2;
    7407           0 :                                     stopscandim = j - start[l_index] + 1;
    7408             :                                  }
    7409           0 :                               j = j + 2;
    7410             :                            }
    7411           0 :                            if(SWXRegion[regionID]->band8flag == 1 ||
    7412           0 :                               SWXRegion[regionID]->band8flag == 2)
    7413             :                            {
    7414           0 :                               if(startscandim == -1)
    7415           0 :                                  if(startscanline < idxmap[0])
    7416             :                                  {
    7417           0 :                                     startscandim = 0;
    7418           0 :                                     start[l_index] = 0;
    7419             :                                  }
    7420           0 :                               if(stopscandim == -1)
    7421           0 :                                  if(stopscanline > idxmap[scene_cnt * 2 - 1])
    7422             :                                  {
    7423           0 :                                     stopscandim = scene_cnt*2 - start[l_index];
    7424           0 :                                     edge[l_index] = scene_cnt*2 - start[l_index];
    7425             :                                  }
    7426             :                            }
    7427           0 :                            if(SWXRegion[regionID]->band8flag == 2)
    7428             :                            {
    7429           0 :                               if(startscandim == -1)
    7430           0 :                                  if(startscanline > idxmap[j - 1])
    7431             :                                  {
    7432           0 :                                     status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank2,dims2,&nt,dimlist);
    7433           0 :                                     buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
    7434           0 :                                     status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
    7435           0 :                                     startscanline = startscanline - (buffer2[0] * detect_cnt);
    7436           0 :                                     stopscanline = stopscanline - (buffer2[0] * detect_cnt);
    7437           0 :                                     free(buffer2);
    7438           0 :                                     j = 0;
    7439           0 :                                     for (l = 0; l < scene_cnt; l++)
    7440             :                                     {
    7441           0 :                                        if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
    7442             :                                        {
    7443           0 :                                           start[l_index] = j;
    7444             :                                        }
    7445           0 :                                        if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
    7446           0 :                                           edge[l_index] = j - start[l_index] + 2;
    7447           0 :                                        j = j + 2;
    7448           0 :                                        if(idxmap[j] == 0  || idxmap[j+1] == 0)
    7449           0 :                                           l = scene_cnt;
    7450             :                                     }
    7451             :  
    7452             :                                  }
    7453             :                            }
    7454             : 
    7455             :                         }
    7456           0 :                         else if(SWXRegion[regionID]->scanflag == 1 &&
    7457           0 :                                 (strcmp(fieldname, "scene_center_latitude") == 0 ||
    7458           0 :                                  strcmp(fieldname, "scene_center_longitude") == 0))
    7459             :                         {
    7460           0 :                            if (land_status == 0)
    7461           0 :                               status = SWreadattr(swathID, "scene_count", &scene_cnt);
    7462           0 :                            startscanline = SWXRegion[regionID]->StartRegion[k];
    7463           0 :                            stopscanline = SWXRegion[regionID]->StopRegion[k];
    7464           0 :                            if(startscanline < idxmap[0])
    7465             :                            {
    7466           0 :                               startscandim = 0;
    7467           0 :                               start[l_index] = 0;
    7468             :                            }
    7469           0 :                            for (l = 0; l < scene_cnt-1; l++)
    7470             :                            {
    7471           0 :                               if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
    7472           0 :                                  if(startscandim == -1)
    7473             :                                  {
    7474           0 :                                     start[l_index] = l;
    7475           0 :                                     startscandim = l;
    7476             :                                  }
    7477           0 :                               if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
    7478           0 :                                  if(stopscandim == -1)
    7479             :                                  {
    7480           0 :                                     edge[l_index] = l - start[l_index] + 2;
    7481           0 :                                     stopscandim = l + 1;
    7482             :                                  }
    7483             :                             }
    7484           0 :                             if(stopscandim == -1)
    7485             :                             {
    7486           0 :                                if(stopscanline > idxmap[scene_cnt - 1])
    7487             :                                {
    7488           0 :                                   edge[l_index] = scene_cnt - start[l_index];
    7489           0 :                                   stopscandim = scene_cnt - 1;
    7490             :                                }
    7491             :                             }
    7492             : 
    7493           0 :                             if(SWXRegion[regionID]->band8flag == 1)
    7494             :                             {
    7495           0 :                                if(stopscandim == -1)
    7496           0 :                                   if(stopscanline > idxmap[scene_cnt - 1])
    7497             :                                   {
    7498           0 :                                      edge[l_index] = scene_cnt - start[l_index];
    7499           0 :                                      stopscandim = scene_cnt -1;
    7500             :                                   }
    7501             :                             }
    7502           0 :                             if(SWXRegion[regionID]->band8flag == 2 ||
    7503           0 :                                SWXRegion[regionID]->band8flag == 3)
    7504             :                             {
    7505           0 :                                if(startscandim == -1)
    7506             :                                {
    7507           0 :                                   if(startscanline < idxmap[0])
    7508             :                                   {
    7509           0 :                                      startscandim = 0;
    7510           0 :                                      start[l_index] = 0;
    7511           0 :                                      edge[l_index] = stopscandim - startscandim + 1;
    7512             :                                   }
    7513             :                                }
    7514           0 :                                if(startscandim == -1)
    7515             :                                {
    7516           0 :                                   startscanline = SWXRegion[regionID]->StartScan[k] * detect_cnt - detect_cnt;
    7517           0 :                                   stopscanline = SWXRegion[regionID]->StopScan[k] * detect_cnt - 1;
    7518           0 :                                   for (l = 0; l < scene_cnt-1; l++)
    7519             :                                   {
    7520           0 :                                      if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
    7521           0 :                                         start[l_index] = l;
    7522           0 :                                      if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
    7523           0 :                                         edge[l_index] = l - start[l_index] + 1;
    7524             :                                   }
    7525             :                                }
    7526             :                            }
    7527             :                         }
    7528             :                         else
    7529             :                         {
    7530           0 :                            if (SWXRegion[regionID]->scanflag == 1 && 
    7531           0 :                                strcmp(fieldname,dfieldlist) == 0)
    7532             :                            {
    7533           0 :                               start[l_index] = SWXRegion[regionID]->StartRegion[k];
    7534           0 :                               edge[l_index] = SWXRegion[regionID]->StopRegion[k] - 
    7535           0 :                                              SWXRegion[regionID]->StartRegion[k] + 1;
    7536           0 :                               if(SWXRegion[regionID]->band8flag == 2 ||
    7537           0 :          SWXRegion[regionID]->band8flag == 3 )
    7538             :                               {
    7539           0 :                                  status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank,dims2,&nt,dimlist);
    7540           0 :                                  buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
    7541           0 :                                  status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
    7542           0 :                                  start[l_index] = start[l_index] - (buffer2[0] * detect_cnt - detect_cnt);
    7543           0 :                                  free(buffer2);
    7544             :                               }
    7545             :                            }
    7546             :                            else
    7547             :                            {
    7548           0 :             start[l_index] = idxmap[SWXRegion[regionID]->StartRegion[k]];
    7549             : 
    7550           0 :             edge[l_index] = idxmap[SWXRegion[regionID]->StopRegion[k]] -
    7551           0 :                        idxmap[SWXRegion[regionID]->StartRegion[k]] + 1;
    7552             :                            }
    7553             :                         }
    7554             :       /* Read Data into output buffer */
    7555             :       /* ---------------------------- */
    7556           0 :       status = SWreadfield(swathID, fieldname,
    7557             :                start, NULL, edge,
    7558             :                buffer);
    7559           0 :                         if (SWXRegion[regionID]->scanflag == 1)
    7560             :                         {
    7561             :                            
    7562           0 :                            if (strcmp(fieldname,"Longitude") == 0)
    7563             :                            {
    7564           0 :                               status = SWscan2longlat(swathID, fieldname, buffer, start, 
    7565             :                                               edge, idxmap, startscanline, stopscanline);
    7566             :                            }
    7567           0 :                            if (strcmp(fieldname,"Latitude") == 0)
    7568             :                            {
    7569           0 :                               status = SWscan2longlat(swathID, fieldname, buffer, start, 
    7570             :                                               edge, idxmap, startscanline, stopscanline);
    7571             :                            }
    7572             :                         }
    7573             :         }
    7574             :     }
    7575           0 :     else if(vfound == 1)                          /* Vertical subsetting */
    7576             :                 {                                             /* found previously,   */
    7577           0 :                    status = SWreadfield(swathID, fieldname,   /* perform the vertical*/
    7578             :                                         start, NULL, edge,    /* subsetting.         */
    7579             :                                         (uint8 *) buffer);    /* -- xhua             */
    7580             :                 }
    7581             :                 else
    7582             :     {
    7583             :         /* Mapping not found */
    7584             :         /* ----------------- */
    7585           0 :         status = -1; 
    7586           0 :         HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
    7587           0 :         HEreport("Mapping Not Defined for \"%s\" Dimension.\n",
    7588             :            geodim);
    7589             :     }                                           
    7590             :       }
    7591             :       else
    7592             :       {
    7593             :     /* Read Data (Vert SS only) */
    7594             :     /* ------------------------ */
    7595           0 :     status = SWreadfield(swathID, fieldname,
    7596             :              start, NULL, edge,
    7597             :              (uint8 *) buffer);
    7598             :       }
    7599             :   }
    7600             :     }
    7601             : 
    7602             :     /* Free l_index mappings if applicable */
    7603             :     /* --------------------------------- */
    7604           0 :     if (idxmap != NULL)
    7605             :     {
    7606           0 :   free(idxmap);
    7607             :     }
    7608           0 :     if(dfieldlist != NULL)
    7609           0 :        free(dfieldlist);
    7610             : 
    7611           0 :     return (status);
    7612             : }
    7613             : 
    7614             : 
    7615             : /*----------------------------------------------------------------------------|
    7616             : |  BEGIN_PROLOG                                                               |
    7617             : |                                                                             |
    7618             : |  FUNCTION: SWscan2longlat                                                   |
    7619             : |                                                                             |
    7620             : |  DESCRIPTION:  Convert scanline to Long/Lat for floating scene subsetting.  |
    7621             : |                This will calculate/interpolate the long/lat for a given     |
    7622             : |                scanline.                                                    |
    7623             : |                                                                             |
    7624             : |                                                                             |
    7625             : |  Return Value    Type     Units     Description                             |
    7626             : |  ============   ======  =========   =====================================   |
    7627             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    7628             : |                                                                             |
    7629             : |  INPUTS:                                                                    |
    7630             : |  swathID        int32               Swath structure ID                      |
    7631             : |  fieldname      char                Fieldname                               |
    7632             : |  buffer   void          Values to update                        |
    7633             : |  start    int32                                                 |
    7634             : |  edge     int32                   |
    7635             : |  idxmap   int32 *       Buffer of l_index mapping values          |
    7636             : |  startscanline  int32         Start of scan region          |
    7637             : |  stopscanline   int32         Stop of scan region         |
    7638             : |                                                                             |
    7639             : |  OUTPUTS:                                                                   |
    7640             : |                                                                             |
    7641             : |  NOTES:                                                                     |
    7642             : |                                                                             |
    7643             : |                                                                             |
    7644             : |   Date     Programmer   Description                                         |
    7645             : |  ======   ============  =================================================   |
    7646             : |  Jul 99   DaW           Original Programmer                                 |
    7647             : |                                                                             |
    7648             : |  END_PROLOG                                                                 |
    7649             : -----------------------------------------------------------------------------*/
    7650             : static intn
    7651           0 : SWscan2longlat(int32 swathID, const char *fieldname, VOIDP buffer, int32 start[], 
    7652             : int32 edge[], int32 *idxmap, int32 startscanline, int32 stopscanline)
    7653             : {
    7654             : 
    7655             :    enum corner {UL, UR, LL, LR};
    7656           0 :    enum corner pos = UL;
    7657           0 :    enum corner pos2 = UL;
    7658             : 
    7659           0 :    uint8  scene_cnt = 0;    /* Used to convert scan to scanline  */
    7660             :                                         /*  L7 float scene sub.              */
    7661             :    float32  *buffer2;
    7662             :    float32  *bufferc;
    7663           0 :    float32  deg2rad = (float32)(M_PI/180.00);
    7664             : 
    7665           0 :    float32  p1_long = 0.0;  /* point 1, longitude */
    7666           0 :    float32  p2_long = 0.0;  /* point 2, longitude */
    7667           0 :    float32  pi_long = 0.0;  /* interpolated point, longitude */
    7668           0 :    int32  scanline_p1 = 0;
    7669             : 
    7670           0 :    float32  p1_lat = 0.0; /* point 1, latitude */
    7671           0 :    float32  p2_lat = 0.0; /* point 2, latitude */
    7672           0 :    float32  pi_lat = 0.0; /* interpolated point, latitude */
    7673           0 :    int32  scanline_p2 = 0;
    7674             : 
    7675             : 
    7676           0 :    float32  x_p1 = 0.0;   /* Cartesian coordinates */
    7677           0 :    float32  y_p1 = 0.0;   /* point 1               */
    7678           0 :    float32  z_p1 = 0.0;
    7679             : 
    7680           0 :    float32  x_p2 = 0.0;   /* Cartesian coordinates */
    7681           0 :    float32  y_p2 = 0.0;   /* point 2     */
    7682           0 :    float32  z_p2 = 0.0;
    7683             : 
    7684           0 :    float32  x_pi = 0.0;   /* Cartesian coordinates */
    7685           0 :    float32  y_pi = 0.0;   /* interpolated point    */
    7686           0 :    float32  z_pi = 0.0;
    7687           0 :    int32  scanline_pi = 0;
    7688             : 
    7689           0 :    intn   status = -1;
    7690             : 
    7691           0 :    int    i = 0;
    7692           0 :    int    p1_long_l90_flag = 0;
    7693           0 :    int    p1_long_g90_flag = 0;
    7694           0 :    int    p2_long_l90_flag = 0;
    7695           0 :    int    p2_long_g90_flag = 0;
    7696           0 :    int    fieldflag = 0;
    7697             : 
    7698           0 :    int  numofval = 0;
    7699             : 
    7700             : 
    7701             : 
    7702           0 :    numofval = edge[0] * 2;
    7703             : 
    7704             : 
    7705           0 :    buffer2 = (float32 *)calloc(numofval, sizeof(float32));
    7706           0 :    bufferc = (float32 *)calloc(numofval, sizeof(float32));
    7707           0 :    memmove(bufferc, buffer, numofval*sizeof(float32));
    7708             : 
    7709           0 :    (void) SWreadattr(swathID, "scene_count", &scene_cnt);
    7710             : 
    7711           0 :    if (strcmp(fieldname, "Longitude") == 0)
    7712             :    {
    7713           0 :       fieldflag = 1;
    7714           0 :       status = SWreadfield(swathID, "Latitude", start, NULL, edge, buffer2);
    7715             :    }
    7716           0 :    else if (strcmp(fieldname, "Latitude") == 0)
    7717             :    {
    7718           0 :       fieldflag = 2;
    7719           0 :       status = SWreadfield(swathID, "Longitude", start, NULL, edge, buffer2);
    7720             :    }
    7721             : 
    7722           0 :    for(i=0; i<4; i++)
    7723             :    {
    7724           0 :       switch(pos)
    7725             :       {
    7726           0 :          case UL:
    7727           0 :             if (fieldflag == 1)
    7728             :             {
    7729           0 :                p1_long = bufferc[0];
    7730           0 :                p2_long = bufferc[2];
    7731           0 :                p1_lat = buffer2[0];
    7732           0 :                p2_lat = buffer2[2];
    7733             :             }
    7734           0 :             if (fieldflag == 2)
    7735             :             {
    7736           0 :                p1_long = buffer2[0];
    7737           0 :                p2_long = buffer2[2];
    7738           0 :                p1_lat = bufferc[0];
    7739           0 :                p2_lat = bufferc[2];
    7740             :             }
    7741           0 :             scanline_p1 = idxmap[start[0]];
    7742           0 :             scanline_p2 = idxmap[start[0]+1];
    7743           0 :             scanline_pi = startscanline;
    7744           0 :             pos = UR;
    7745           0 :             break;
    7746           0 :          case UR:
    7747           0 :             if (fieldflag == 1)
    7748             :             {
    7749           0 :                p1_long = bufferc[1];
    7750           0 :                p2_long = bufferc[3];
    7751           0 :                p1_lat = buffer2[1];
    7752           0 :                p2_lat = buffer2[3];
    7753             :             }
    7754           0 :             if (fieldflag == 2)
    7755             :             {
    7756           0 :                p1_long = buffer2[1];
    7757           0 :                p2_long = buffer2[3];
    7758           0 :                p1_lat = bufferc[1];
    7759           0 :                p2_lat = bufferc[3];
    7760             :             }
    7761           0 :             scanline_p1 = idxmap[start[0]];
    7762           0 :             scanline_p2 = idxmap[start[0]+1];
    7763           0 :             scanline_pi = startscanline;
    7764           0 :             pos = LL;
    7765           0 :             break;
    7766           0 :          case LL:
    7767           0 :             if (fieldflag == 1)
    7768             :             {
    7769           0 :                p1_long = bufferc[numofval-4];
    7770           0 :                p2_long = bufferc[numofval-2];
    7771           0 :                p1_lat = buffer2[numofval-4];
    7772           0 :                p2_lat = buffer2[numofval-2];
    7773             :             }
    7774           0 :             if (fieldflag == 2)
    7775             :             {
    7776           0 :                p1_long = buffer2[numofval-4];
    7777           0 :                p2_long = buffer2[numofval-2];
    7778           0 :                p1_lat = bufferc[numofval-4];
    7779           0 :                p2_lat = bufferc[numofval-2];
    7780             :             }
    7781           0 :             scanline_p1 = idxmap[start[0] + edge[0] - 2];
    7782           0 :             scanline_p2 = idxmap[start[0] + edge[0] - 1];
    7783           0 :             scanline_pi = stopscanline;
    7784           0 :             pos = LR;
    7785           0 :             break;
    7786           0 :          case LR:
    7787           0 :             if (fieldflag == 1)
    7788             :             {
    7789           0 :                p1_long = bufferc[numofval-3];
    7790           0 :                p2_long = bufferc[numofval-1];
    7791           0 :                p1_lat = buffer2[numofval-3];
    7792           0 :                p2_lat = buffer2[numofval-1];
    7793             :             }
    7794           0 :             if (fieldflag == 2)
    7795             :             {
    7796           0 :                p1_long = buffer2[numofval-3];
    7797           0 :                p2_long = buffer2[numofval-1];
    7798           0 :                p1_lat = bufferc[numofval-3];
    7799           0 :                p2_lat = bufferc[numofval-1];
    7800             :             }
    7801           0 :             scanline_p1 = idxmap[start[0] + edge[0] - 2];
    7802           0 :             scanline_p2 = idxmap[start[0] + edge[0] - 1];
    7803           0 :             scanline_pi = stopscanline;
    7804           0 :             break;
    7805             :       }
    7806             : 
    7807             : 
    7808             : 
    7809           0 :    if (p1_long <= -90.0)
    7810             :    {
    7811           0 :       if (p2_long >= 90.0)
    7812             :       {
    7813           0 :          p1_long = p1_long + 180.0f;
    7814           0 :          p2_long = p2_long - 180.0f;
    7815           0 :          p1_long_l90_flag = 2;
    7816             :       }
    7817             :       else
    7818             :       {
    7819           0 :          p1_long = p1_long + 180.0f;
    7820           0 :          p1_long_l90_flag = 1;
    7821             :       }
    7822             :    }
    7823           0 :    if (p1_long >= 90.0 && p1_long_l90_flag != 2)
    7824             :    {
    7825           0 :       if(p2_long <= -90.0)
    7826             :       {
    7827           0 :          p1_long = p1_long - 180.0f;
    7828           0 :          p2_long = p2_long + 180.0f;
    7829           0 :          p1_long_g90_flag = 2;
    7830             :       }
    7831             :       else
    7832             :       {
    7833           0 :          p1_long = p1_long - 90.0f;
    7834           0 :          p1_long_g90_flag = 1;
    7835             :       }
    7836             :    }
    7837           0 :    if (p2_long <= -90.0)
    7838             :    {
    7839           0 :       if (p1_long < 0.0)
    7840             :       {
    7841           0 :          p2_long = p2_long + 90.0f;
    7842           0 :          p1_long = p1_long + 90.0f;
    7843           0 :          p2_long_l90_flag = 2;
    7844             :       }
    7845             :       else
    7846             :       {
    7847           0 :          p2_long = p2_long + 180.0f;
    7848           0 :          p2_long_l90_flag = 1;
    7849             :       }
    7850             :    }
    7851           0 :    if (p2_long >= 90.0 && p1_long_l90_flag != 2)
    7852             :    {
    7853           0 :       p2_long = p2_long - 90.0f;
    7854           0 :       p2_long_g90_flag = 1;
    7855             :    }
    7856             : 
    7857             : 
    7858           0 :    x_p1 = (float)(RADOE * cos((p1_long*deg2rad)) * sin((p1_lat*deg2rad)));
    7859           0 :    y_p1 = (float)(RADOE * sin((p1_long*deg2rad)) * sin((p1_lat*deg2rad)));
    7860           0 :    z_p1 = (float)(RADOE * cos((p1_lat*deg2rad)));
    7861             : 
    7862             :    
    7863           0 :    x_p2 = (float)(RADOE * cos((p2_long*deg2rad)) * sin((p2_lat*deg2rad)));
    7864           0 :    y_p2 = (float)(RADOE * sin((p2_long*deg2rad)) * sin((p2_lat*deg2rad)));
    7865           0 :    z_p2 = (float)(RADOE * cos((p2_lat*deg2rad)));
    7866             : 
    7867           0 :    x_pi = x_p1 + (x_p2 - x_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1);
    7868           0 :    y_pi = y_p1 + (y_p2 - y_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1);
    7869           0 :    z_pi = z_p1 + (z_p2 - z_p1)*(scanline_pi-scanline_p1)/(scanline_p2-scanline_p1); 
    7870             : 
    7871           0 :    if (fieldflag == 1)
    7872             :    {
    7873           0 :       pi_long = (float)(atan(y_pi/x_pi)*180.0/M_PI);
    7874           0 :       if (p1_long_l90_flag == 1 || p2_long_l90_flag == 1)
    7875             :       {
    7876           0 :          pi_long = pi_long - 180.0f;
    7877           0 :          p1_long_l90_flag = 0;
    7878           0 :    p2_long_l90_flag = 0;
    7879             :       }
    7880           0 :       if (p1_long_g90_flag == 1 || p2_long_g90_flag == 1)
    7881             :       {
    7882           0 :          pi_long = pi_long + 90.0f;
    7883           0 :          p1_long_g90_flag = 0;
    7884           0 :          p2_long_g90_flag = 0;
    7885             :       }
    7886           0 :       if (p1_long_l90_flag == 2)
    7887             :       {
    7888           0 :          if (pi_long > 0.0)
    7889           0 :             pi_long = pi_long - 180.0f;
    7890           0 :          else if (pi_long < 0.0)
    7891           0 :             pi_long = pi_long + 180.0f;
    7892           0 :          p1_long_l90_flag = 0;
    7893             :       }
    7894           0 :       if (p1_long_g90_flag == 2)
    7895             :       {
    7896           0 :          if (pi_long > 0.0)
    7897           0 :             pi_long = pi_long - 180.0f;
    7898           0 :          else if (pi_long < 0.0)
    7899           0 :             pi_long = pi_long + 180.0f;
    7900           0 :          p1_long_g90_flag = 0;
    7901             :       }
    7902           0 :       if (p2_long_l90_flag == 2)
    7903             :       {
    7904           0 :          pi_long = pi_long - 90.0f;
    7905           0 :          p2_long_l90_flag = 0;
    7906             :       }
    7907             : 
    7908             : 
    7909             : 
    7910           0 :       switch(pos2)
    7911             :       {
    7912           0 :       case UL:
    7913           0 :          bufferc[0] = pi_long;
    7914           0 :          pos2 = UR;
    7915           0 :          break;
    7916           0 :       case UR:
    7917           0 :          bufferc[1] = pi_long;
    7918           0 :          pos2 = LL;
    7919           0 :          break;
    7920           0 :       case LL:
    7921           0 :          if(stopscanline > idxmap[scene_cnt*2 - 1])
    7922           0 :             break;
    7923           0 :          bufferc[numofval-2] = pi_long;
    7924           0 :          pos2 = LR;
    7925           0 :          break;
    7926           0 :       case LR:
    7927           0 :          if(stopscanline > idxmap[scene_cnt*2 - 1])
    7928           0 :             break;
    7929           0 :          bufferc[numofval-1] = pi_long;
    7930           0 :          break;
    7931             :       }
    7932             : 
    7933           0 :     }
    7934           0 :     if (fieldflag == 2)
    7935             :     {
    7936           0 :       pi_lat = (float)(atan((sqrt(x_pi*x_pi + y_pi*y_pi)/z_pi))*180.0/M_PI);
    7937           0 :       switch(pos2)
    7938             :       {
    7939           0 :       case UL:
    7940           0 :          bufferc[0] = pi_lat;
    7941           0 :          pos2 = UR;
    7942           0 :          break;
    7943           0 :       case UR:
    7944           0 :          bufferc[1] = pi_lat;
    7945           0 :          pos2 = LL;
    7946           0 :          break;
    7947           0 :       case LL:
    7948           0 :          if(stopscanline > idxmap[scene_cnt*2 - 1])
    7949           0 :             break;
    7950           0 :          bufferc[numofval-2] = pi_lat;
    7951           0 :          pos2 = LR;
    7952           0 :          break;
    7953           0 :       case LR:
    7954           0 :          if(stopscanline > idxmap[scene_cnt*2 - 1])
    7955           0 :             break;
    7956           0 :          bufferc[numofval-1] = pi_lat;
    7957           0 :          break;
    7958             :       }
    7959           0 :    }
    7960             :    }
    7961           0 :    memmove(buffer, bufferc, numofval*sizeof(float32));
    7962           0 :    free(buffer2);
    7963           0 :    free(bufferc);
    7964           0 :    return(status);
    7965             : }
    7966             : 
    7967             : 
    7968             : /*----------------------------------------------------------------------------|
    7969             : |  BEGIN_PROLOG                                                               |
    7970             : |                                                                             |
    7971             : |  FUNCTION: SWextractperiod                                                  |
    7972             : |                                                                             |
    7973             : |  DESCRIPTION: Retrieves data from specified period.                         |
    7974             : |                                                                             |
    7975             : |                                                                             |
    7976             : |  Return Value    Type     Units     Description                             |
    7977             : |  ============   ======  =========   =====================================   |
    7978             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    7979             : |                                                                             |
    7980             : |  INPUTS:                                                                    |
    7981             : |  swathID        int32               Swath structure ID                      |
    7982             : |  periodID       int32               Period ID                               |
    7983             : |  fieldname      char                Fieldname                               |
    7984             : |  externalflag   int32               External geolocation fields flag        |
    7985             : |                                     HDFE_INTERNAL (0)                       |
    7986             : |                                     HDFE_EXTERNAL (1)                       |
    7987             : |                                                                             |
    7988             : |  OUTPUTS:                                                                   |
    7989             : |  buffer         void                Data buffer containing subsetted region |
    7990             : |                                                                             |
    7991             : |  NOTES:                                                                     |
    7992             : |                                                                             |
    7993             : |                                                                             |
    7994             : |   Date     Programmer   Description                                         |
    7995             : |  ======   ============  =================================================   |
    7996             : |  Jun 96   Joel Gales    Original Programmer                                 |
    7997             : |  Jun 03   Abe Taaheri   added clearing timeflag if SWextractregion fails    |
    7998             : |                                                                             |
    7999             : |  END_PROLOG                                                                 |
    8000             : -----------------------------------------------------------------------------*/
    8001             : intn
    8002           0 : SWextractperiod(int32 swathID, int32 periodID, const char *fieldname,
    8003             :     int32 externalflag, VOIDP buffer)
    8004             : 
    8005             : {
    8006             :     intn            status; /* routine return status variable */
    8007             : 
    8008           0 :     timeflag = 1;
    8009             : 
    8010             :     /* Call SWextractregion routine */
    8011             :     /* ---------------------------- */
    8012           0 :     status = SWextractregion(swathID, periodID, fieldname, externalflag,
    8013             :            (char *) buffer);
    8014           0 :     if (status != 0) timeflag = 0; /*clear timeflag if SWextractregion failed*/
    8015           0 :     return (status);
    8016             : }
    8017             : 
    8018             : 
    8019             : 
    8020             : 
    8021             : 
    8022             : 
    8023             : 
    8024             : /*----------------------------------------------------------------------------|
    8025             : |  BEGIN_PROLOG                                                               |
    8026             : |                                                                             |
    8027             : |  FUNCTION: SWdupregion                                                      |
    8028             : |                                                                             |
    8029             : |  DESCRIPTION: Duplicates a region                                           |
    8030             : |                                                                             |
    8031             : |                                                                             |
    8032             : |  Return Value    Type     Units     Description                             |
    8033             : |  ============   ======  =========   =====================================   |
    8034             : |  newregionID    int32               New region ID                           |
    8035             : |                                                                             |
    8036             : |  INPUTS:                                                                    |
    8037             : |  oldregionID    int32               Old region ID                           |
    8038             : |                                                                             |
    8039             : |  OUTPUTS:                                                                   |
    8040             : |             None                                                            |
    8041             : |                                                                             |
    8042             : |  NOTES:                                                                     |
    8043             : |                                                                             |
    8044             : |                                                                             |
    8045             : |   Date     Programmer   Description                                         |
    8046             : |  ======   ============  =================================================   |
    8047             : |  Jan 97   Joel Gales    Original Programmer                                 |
    8048             : |                                                                             |
    8049             : |  END_PROLOG                                                                 |
    8050             : -----------------------------------------------------------------------------*/
    8051             : int32
    8052           0 : SWdupregion(int32 oldregionID)
    8053             : {
    8054             :     intn            i;    /* Loop index */
    8055             : 
    8056           0 :     int32           newregionID = -1; /* New region ID */
    8057             : 
    8058             : 
    8059             :     /* Find first empty (inactive) region */
    8060             :     /* ---------------------------------- */
    8061           0 :     for (i = 0; i < NSWATHREGN; i++)
    8062             :     {
    8063           0 :   if (SWXRegion[i] == 0)
    8064             :   {
    8065             :       /* Allocate space for new swath region entry */
    8066             :       /* ----------------------------------------- */
    8067           0 :       SWXRegion[i] = (struct swathRegion *)
    8068           0 :     calloc(1, sizeof(struct swathRegion));
    8069           0 :       if(SWXRegion[i] == NULL)
    8070             :       { 
    8071           0 :     HEpush(DFE_NOSPACE,"SWdupregion", __FILE__, __LINE__);
    8072           0 :     return(-1);
    8073             :       }
    8074             : 
    8075             :       /* Copy old region structure data to new region */
    8076             :       /* -------------------------------------------- */
    8077           0 :       *SWXRegion[i] = *SWXRegion[oldregionID];
    8078             : 
    8079             : 
    8080             :       /* Define new region ID */
    8081             :       /* -------------------- */
    8082           0 :       newregionID = i;
    8083             : 
    8084           0 :       break;
    8085             :   }
    8086             :     }
    8087             : 
    8088           0 :     return (newregionID);
    8089             : }
    8090             : 
    8091             : 
    8092             : /*----------------------------------------------------------------------------|
    8093             : |  BEGIN_PROLOG                                                               |
    8094             : |                                                                             |
    8095             : |  FUNCTION: SWregioninfo                                                     |
    8096             : |                                                                             |
    8097             : |  DESCRIPTION: Returns size of region in bytes                               |
    8098             : |                                                                             |
    8099             : |                                                                             |
    8100             : |  Return Value    Type     Units     Description                             |
    8101             : |  ============   ======  =========   =====================================   |
    8102             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    8103             : |                                                                             |
    8104             : |  INPUTS:                                                                    |
    8105             : |  swathID        int32               Swath structure ID                      |
    8106             : |  regionID       int32               Region ID                               |
    8107             : |  fieldname      char                Fieldname                               |
    8108             : |                                                                             |
    8109             : |                                                                             |
    8110             : |  OUTPUTS:                                                                   |
    8111             : |  ntype          int32               field number type                       |
    8112             : |  rank           int32               field rank                              |
    8113             : |  dims           int32               dimensions of field region              |
    8114             : |  size           int32               size in bytes of field region           |
    8115             : |                                                                             |
    8116             : |  NOTES:                                                                     |
    8117             : |                                                                             |
    8118             : |                                                                             |
    8119             : |   Date     Programmer   Description                                         |
    8120             : |  ======   ============  =================================================   |
    8121             : |  Jun 96   Joel Gales    Original Programmer                                 |
    8122             : |  Aug 96   Joel Gales    Add vertical subsetting                             |
    8123             : |  Dec 96   Joel Gales    Add multiple vertical subsetting capability         |
    8124             : |  Mar 97   Joel Gales    Add support for l_index mapping                       |
    8125             : |  Jul 99   DaW           Add support for floating scene subsetting           |
    8126             : |                                                                             |
    8127             : |  END_PROLOG                                                                 |
    8128             : -----------------------------------------------------------------------------*/
    8129             : intn
    8130           0 : SWregioninfo(int32 swathID, int32 regionID, const char *fieldname,
    8131             :        int32 * ntype, int32 * rank, int32 dims[], int32 * size)
    8132             : 
    8133             : {
    8134             :     intn            i;    /* Loop index */
    8135             :     intn            j;    /* Loop index */
    8136             :     intn            k;    /* Loop index */
    8137           0 :     intn            l = 0;    /* Loop index */
    8138             :     intn            status; /* routine return status variable */
    8139           0 :     intn            long_status = 3;  /* routine return status variable for longitude */
    8140           0 :     intn            land_status = 3;    /* Used for L7 float scene sub.      */
    8141           0 :     intn            statMap = -1;   /* Status from SWmapinfo  */
    8142             :     
    8143           0 :     uint8           found = 0;  /* Found flag */
    8144           0 :     uint8           detect_cnt = 0; /* Used for L7 float scene sub.      */
    8145             : 
    8146           0 :     int32           numtype = 0; /* Used for L7 float scene sub. */
    8147           0 :     int32           count = 0;   /* Used for L7 float scene sub. */
    8148             : 
    8149             : 
    8150             :     int32           fid;  /* HDF-EOS file ID */
    8151             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    8152             :     int32           swVgrpID; /* Swath Vgroup ID */
    8153             : 
    8154           0 :     int32           l_index = 0;  /* Geo Dim Index */
    8155             :     int32           nDim; /* Number of dimensions */
    8156             :     int32           slen[64]; /* String length array */
    8157             :     int32           dum;  /* Dummy variable */
    8158           0 :     int32           incr = 0; /* Mapping increment */
    8159           0 :     int32           nXtrk = 0;  /* Number of cross tracks */
    8160           0 :     int32     scan_shift = 0; /* Used to take out partial scans */
    8161           0 :     int32     startscandim = -1;    /* Used for floating scene region size */
    8162           0 :     int32     stopscandim = -1;    /* Used for floating scene region size */
    8163             :     int32           dumdims[8]; /* Dimensions from SWfieldinfo */
    8164           0 :     int32           idxMapElem = -1;  /* Number of l_index map elements  */
    8165           0 :     int32          *idxmap = NULL;  /* Pointer to l_index mapping array */
    8166           0 :     int32     datafld = 0;
    8167             : 
    8168           0 :    uint8  scene_cnt = 0;    /* Number of scenes in swath */
    8169           0 :    int32  startscanline = 0;
    8170           0 :    int32  stopscanline = 0;
    8171           0 :    char   *dfieldlist = (char *)NULL;
    8172           0 :    int32  strbufsize = 0;
    8173             :    int32  dfrank[8];
    8174             :    int32  numtype2[8];
    8175           0 :    int32  rank2 = 0;
    8176           0 :    int32  rk = 0;
    8177             :    int32  dims2[8];
    8178           0 :    int32  nt = 0;
    8179           0 :    uint16 *buffer2 = (uint16 *)NULL;
    8180           0 :    uint16 *tbuffer = (uint16 *)NULL;
    8181             : 
    8182             :     char            dimlist[256]; /* Dimension list */
    8183             :     char            geodim[256];/* Geolocation field dimension list */
    8184             :     char            tgeodim[256];/* Time Geolocation field dimension list */
    8185             :     char            dgeodim[256];/* Data Subsetting field dimension list */
    8186             :     char            utlbuf[256];/* Utility buffer */
    8187             :     char           *ptr[64];  /* String pointer array */
    8188             :     static const char errMesg[] = "Vertical Dimension Not Found: \"%s\".\n";
    8189             : 
    8190             : 
    8191             : 
    8192             :     /* Set region size to -1 */
    8193             :     /* --------------------- */
    8194           0 :     *size = -1;
    8195             : 
    8196             : 
    8197             :     /* Check for valid swath ID */
    8198             :     /* ------------------------ */
    8199           0 :     status = SWchkswid(swathID, "SWregioninfo", &fid, &sdInterfaceID,
    8200             :            &swVgrpID);
    8201             : 
    8202             : 
    8203             :     /* Check for valid region ID */
    8204             :     /* ------------------------- */
    8205           0 :     if (status == 0)
    8206             :     {
    8207           0 :   if (regionID < 0 || regionID >= NSWATHREGN)
    8208             :   {
    8209           0 :       status = -1;
    8210           0 :       HEpush(DFE_RANGE, "SWregioninfo", __FILE__, __LINE__);
    8211           0 :       HEreport("Invalid Region id: %d.\n", regionID);
    8212             :   }
    8213             :     }
    8214             : 
    8215             : 
    8216             : 
    8217             :     /* Check for active region ID */
    8218             :     /* -------------------------- */
    8219           0 :     if (status == 0)
    8220             :     {
    8221           0 :   if (SWXRegion[regionID] == 0)
    8222             :   {
    8223           0 :       status = -1;
    8224           0 :       HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__);
    8225           0 :       HEreport("Inactive Region ID: %d.\n", regionID);
    8226             :   }
    8227             :     }
    8228             : 
    8229             :    /* This code checks for the attribute detector_count */
    8230             :    /* which is found in Landsat 7 files.  It is used    */
    8231             :    /* for some of the loops.                            */
    8232             :    /* ================================================= */
    8233           0 :    if (status == 0 && SWXRegion[regionID]->scanflag == 1)
    8234             :    {
    8235           0 :       land_status = SWattrinfo(swathID, "detector_count", &numtype, &count);
    8236           0 :       if (land_status == 0)
    8237             :       {
    8238           0 :          land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
    8239           0 :          land_status = SWreadattr(swathID, "scene_count", &scene_cnt);
    8240             : 
    8241             :       }
    8242             :    }
    8243             : 
    8244             : 
    8245             : 
    8246             : 
    8247             :     /* Check for valid fieldname */
    8248             :     /* ------------------------- */
    8249           0 :     if (status == 0)
    8250             :     {
    8251             :   /* Get data field info */
    8252             :   /* ------------------- */
    8253           0 :   status = SWfieldinfo(swathID, fieldname, rank,
    8254             :            dims, ntype, dimlist);
    8255             : 
    8256           0 :   if (status != 0)
    8257             :   {
    8258           0 :       status = -1;
    8259           0 :       HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__);
    8260           0 :       HEreport("Field \"%s\" Not Found.\n", fieldname);
    8261             :   }
    8262             :     }
    8263             : 
    8264             : 
    8265             : 
    8266             :     /* No problems so proceed ... */
    8267             :     /* -------------------------- */
    8268           0 :     if (status == 0)
    8269             :     {
    8270             :   /* If non-vertical subset regions defined ... */
    8271             :   /* ------------------------------------------ */
    8272           0 :   if (SWXRegion[regionID]->nRegions > 0 || SWXRegion[regionID]->scanflag == 1)
    8273             :   {
    8274             : 
    8275             :       /* Get geolocation dimension name */
    8276             :       /* ------------------------------ */
    8277           0 :       status = SWfieldinfo(SWXRegion[regionID]->swathID,
    8278             :          "Longitude", &dum,
    8279             :          dumdims, &dum, geodim);
    8280           0 :             long_status = status;
    8281             : 
    8282             :             /* If Time field being used, check for dimensions */
    8283             :             /* ---------------------------------------------- */
    8284           0 :             if (timeflag == 1)
    8285             :             {
    8286             :                /* code change to fix time subset bug for Landsat7 */
    8287             : 
    8288           0 :                status = SWfieldinfo(SWXRegion[regionID]->swathID,
    8289             :                                     "Time", &dum,
    8290             :                                     dumdims, &dum, tgeodim);
    8291             :                
    8292           0 :                if (strcmp(geodim, tgeodim) != 0)
    8293             :                {
    8294           0 :                   strcpy(geodim, tgeodim);
    8295             :                }
    8296           0 :             timeflag = 0;
    8297             :             }
    8298             : 
    8299             :             /* If defscanregion being used, get dimensions    */
    8300             :             /* of field being used                            */
    8301             :             /* ---------------------------------------------- */
    8302           0 :             if (SWXRegion[regionID]->scanflag == 1)
    8303             :             {
    8304           0 :                (void) SWnentries(SWXRegion[regionID]->swathID,4,&strbufsize);
    8305           0 :                dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char));
    8306           0 :                (void) SWinqdatafields(SWXRegion[regionID]->swathID,dfieldlist,dfrank,numtype2);
    8307           0 :                status = SWfieldinfo(SWXRegion[regionID]->swathID,dfieldlist,&dum,dumdims,&dum,dgeodim);
    8308             : 
    8309             :                   /* The dimensions have to be switched, because */
    8310             :                   /* the mappings force a geodim and datadim     */
    8311             :                   /* so to find the mapping, the dimensions must */
    8312             :                   /* be switched, but the subsetting will still  */
    8313             :                   /* be based on the correct dimensions          */
    8314             :                   /* "long_status == -1" added for CAL file which   */
    8315             :                   /* doesn't have a Traditional geolocation field   */
    8316             :                   /* ---------------------------------------------- */
    8317           0 :                   if (strcmp(dgeodim,dimlist) != 0 || long_status == -1)
    8318             :                   {
    8319           0 :                       strcpy(geodim,dimlist);
    8320           0 :                       strcpy(dimlist,dgeodim);
    8321             :                   }
    8322             :             }
    8323             : 
    8324             : 
    8325             :       /* Get "Track" (first) Dimension from geo dimlist */
    8326             :       /* ---------------------------------------------- */
    8327           0 :       nDim = EHparsestr(geodim, ',', ptr, slen);
    8328           0 :       geodim[slen[0]] = 0;
    8329             : 
    8330             : 
    8331             :       /* Parse Data Field Dimlist & find mapping */
    8332             :       /* --------------------------------------- */
    8333           0 :       nDim = EHparsestr(dimlist, ',', ptr, slen);
    8334             : 
    8335             : 
    8336             :       /* Loop through all dimensions and search for mapping */
    8337             :       /* -------------------------------------------------- */
    8338           0 :       for (i = 0; i < nDim; i++)
    8339             :       {
    8340           0 :     memcpy(utlbuf, ptr[i], slen[i]);
    8341           0 :     utlbuf[slen[i]] = 0;
    8342           0 :     statMap = SWmapinfo(swathID, geodim, utlbuf,
    8343             :             &dum, &incr);
    8344             : 
    8345             :     /* Mapping found */
    8346             :     /* ------------- */
    8347           0 :     if (statMap == 0)
    8348             :     {
    8349           0 :         found = 1;
    8350           0 :         l_index = i;
    8351           0 :         break;
    8352             :     }
    8353             :       }
    8354             : 
    8355             : 
    8356             :       /* If mapping not found check for geodim within dimlist */
    8357             :       /* ---------------------------------------------------- */
    8358           0 :       if (found == 0)
    8359             :       {
    8360           0 :     l_index = EHstrwithin(geodim, dimlist, ',');
    8361             : 
    8362             :     /* Geo dimension found within subset field dimlist */
    8363             :     /* ----------------------------------------------- */
    8364           0 :     if (l_index != -1)
    8365             :     {
    8366           0 :         found = 1;
    8367           0 :         incr = 1;
    8368             :     }
    8369             :       }
    8370             : 
    8371             : 
    8372             : 
    8373             :       /* If mapping not found check for l_indexed mapping */
    8374             :       /* ---------------------------------------------- */
    8375           0 :       if (found == 0)
    8376             :       {
    8377             :     /* Get size of geo dim & allocate space of l_index mapping */
    8378             :     /* ----------------------------------------------------- */
    8379           0 :     dum = SWdiminfo(swathID, geodim);
    8380           0 :     idxmap = (int32 *) calloc(dum, sizeof(int32));
    8381           0 :     if(idxmap == NULL)
    8382             :     { 
    8383           0 :         HEpush(DFE_NOSPACE,"SWregioninfo", __FILE__, __LINE__);
    8384           0 :         return(-1);
    8385             :     }
    8386             : 
    8387             :     /* Loop through all dimensions and search for mapping */
    8388             :     /* -------------------------------------------------- */
    8389           0 :     for (i = 0; i < nDim; i++)
    8390             :     {
    8391           0 :         memcpy(utlbuf, ptr[i], slen[i]);
    8392           0 :         utlbuf[slen[i]] = 0;
    8393             : 
    8394           0 :         idxMapElem = SWidxmapinfo(swathID, geodim, utlbuf, idxmap);
    8395             : 
    8396             : 
    8397             :         /* Mapping found */
    8398             :         /* ------------- */
    8399           0 :         if (idxMapElem != -1)
    8400             :         {
    8401           0 :       found = 1;
    8402           0 :       l_index = i;
    8403           0 :       break;
    8404             :         }
    8405             :     }
    8406             :       }
    8407             : 
    8408             : 
    8409             :       /* Regular Mapping Found */
    8410             :       /* --------------------- */
    8411           0 :       if (found == 1 && idxMapElem == -1)
    8412             :       {
    8413           0 :     dims[l_index] = 0;
    8414             : 
    8415             :     /* Loop through all regions */
    8416             :     /* ------------------------ */
    8417           0 :     for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
    8418             :     {
    8419             :         /* Get number of cross tracks in particular region */
    8420             :         /* ----------------------------------------------- */
    8421           0 :         nXtrk = SWXRegion[regionID]->StopRegion[k] -
    8422           0 :       SWXRegion[regionID]->StartRegion[k] + 1;
    8423             : 
    8424             : 
    8425             :         /* If increment is positive (geodim <= datadim) ... */
    8426             :         /* ------------------------------------------------ */
    8427           0 :         if (incr > 0)
    8428             :         {
    8429           0 :                         if (SWXRegion[regionID]->scanflag == 1)
    8430             :                         {
    8431           0 :                             scan_shift = nXtrk % incr;
    8432           0 :                             if(scan_shift != 0)
    8433           0 :                                nXtrk = nXtrk - scan_shift;
    8434           0 :                             dims[l_index] += nXtrk/incr;
    8435           0 :                             if(long_status == -1 || incr == 1)
    8436             :                             {
    8437           0 :                                if( detect_cnt == 0 )
    8438           0 :                                    return -1;
    8439           0 :                                scan_shift = nXtrk % detect_cnt;
    8440           0 :                                if(scan_shift != 0)
    8441           0 :                                   dims[l_index] = nXtrk - scan_shift;
    8442             :                             }
    8443             :                         }
    8444             :       else
    8445             :                         {
    8446           0 :          dims[l_index] += nXtrk * incr;
    8447             :                         }
    8448             :         }
    8449             :         else
    8450             :         {
    8451             :       /* Negative increment (geodim > datadim) */
    8452             :       /* ------------------------------------- */
    8453           0 :       dims[l_index] += nXtrk / (-incr);
    8454             : 
    8455             :       /*
    8456             :        * If Xtrk not exactly divisible by incr, round dims
    8457             :        * to next highest integer
    8458             :        */
    8459           0 :       if (nXtrk % (-incr) != 0)
    8460             :       {
    8461           0 :           dims[l_index]++;
    8462             :       }
    8463             :         }
    8464             :     }
    8465             :       }
    8466           0 :       else if (found == 1 && idxMapElem != -1)
    8467             :       {
    8468             : 
    8469             :     /* Indexed Mapping */
    8470             :     /* --------------- */
    8471             : 
    8472           0 :     dims[l_index] = 0;
    8473             : 
    8474             :     /* Loop through all regions */
    8475             :     /* ------------------------ */
    8476           0 :     for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
    8477             :     {
    8478           0 :                    j = 0;
    8479           0 :                    if(SWXRegion[regionID]->scanflag == 1)
    8480             :                    {
    8481           0 :                       startscanline = SWXRegion[regionID]->StartRegion[k];
    8482           0 :                       stopscanline = SWXRegion[regionID]->StopRegion[k];
    8483           0 :                       if (strcmp(fieldname,dfieldlist) == 0)
    8484             :                       {
    8485           0 :                          dims[l_index] = stopscanline - startscanline + 1;
    8486           0 :                          datafld = 1;
    8487             :                       }
    8488           0 :                       if (strcmp(fieldname, "Latitude") == 0 ||
    8489           0 :                           strcmp(fieldname, "Longitude") == 0)
    8490             :                       {
    8491           0 :                          if(SWXRegion[regionID]->band8flag == 2 || SWXRegion[regionID]->band8flag == 3)
    8492             :                          {
    8493           0 :                             status = SWfieldinfo(swathID,"scan_no",&rk,dims2,&nt,dimlist);
    8494           0 :                             tbuffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
    8495           0 :                             status =SWreadfield(swathID,"scan_no",NULL,NULL,NULL,tbuffer);
    8496           0 :                             startscanline = startscanline - ((tbuffer[0] * detect_cnt) - detect_cnt);
    8497           0 :                             stopscanline = stopscanline - ((tbuffer[0] * detect_cnt) - 1);
    8498             :                          }
    8499           0 :                          if(SWXRegion[regionID]->band8flag == 2 ||
    8500           0 :                             SWXRegion[regionID]->band8flag == 3)
    8501             :                          {
    8502           0 :                             if(startscandim == -1)
    8503           0 :                                if(startscanline < idxmap[0])
    8504             :                                {
    8505           0 :                                   startscandim = 0;
    8506           0 :                                   dims[l_index] = 0;
    8507           0 :                                   if(stopscanline > idxmap[scene_cnt *2 - 1])
    8508             :                                   {
    8509           0 :                                      stopscandim = scene_cnt*2 - startscandim;
    8510           0 :                                      dims[l_index] = scene_cnt*2 - startscandim;
    8511             :                                   }
    8512             :                                }
    8513             :                          }
    8514           0 :                          for (l = 0; l < scene_cnt; l++)
    8515             :                          {
    8516           0 :                             if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
    8517           0 :                                if(startscandim == -1)
    8518             :                                {
    8519           0 :                                   dims[l_index] = j;
    8520           0 :                                   startscandim = j;
    8521             :                                }
    8522           0 :                             if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
    8523           0 :                                if(startscandim != -1)
    8524             :                                {
    8525           0 :                                   dims[l_index] = j - startscandim + 2;
    8526           0 :                                   stopscandim = j + 1;
    8527             :                                }
    8528           0 :                             j = j + 2;
    8529           0 :                             if(idxmap[j] == 0  || idxmap[j+1] == 0)
    8530           0 :                                l = scene_cnt;
    8531             :                          }
    8532           0 :                          if(SWXRegion[regionID]->band8flag == 1 ||
    8533           0 :                             SWXRegion[regionID]->band8flag == 2)
    8534             :                          {
    8535           0 :                             if(stopscandim == -1)
    8536           0 :                                if(stopscanline > idxmap[scene_cnt * 2 - 1])
    8537             :                                {
    8538           0 :                                   stopscandim = scene_cnt*2 - dims[l_index];
    8539           0 :                                   dims[l_index] = scene_cnt*2 - dims[l_index];
    8540             :                                }
    8541             :                          }
    8542           0 :                          if(SWXRegion[regionID]->band8flag == 3)
    8543             :                          {
    8544           0 :                             if(startscandim == -1)
    8545           0 :                                if(startscanline < idxmap[0])
    8546             :                                {
    8547           0 :                                   startscandim = 0;
    8548           0 :                                   if(stopscandim != -1)
    8549           0 :                                      dims[l_index] = stopscandim - startscandim + 1;
    8550             :                                }
    8551             :                          }
    8552           0 :                          if(SWXRegion[regionID]->band8flag == 2)
    8553             :                          {
    8554           0 :                             if(startscandim == -1)
    8555           0 :                                if(startscanline > idxmap[j - 1])
    8556             :                                {
    8557           0 :                                   status = SWfieldinfo(SWXRegion[regionID]->swathID,"scan_no",&rank2,dims2,&nt,dimlist);
    8558           0 :                                   buffer2 = (uint16 *)calloc(dims2[0], sizeof(uint16));
    8559           0 :                                   status = SWreadfield(SWXRegion[regionID]->swathID,"scan_no",NULL,NULL,NULL,buffer2);
    8560           0 :                                   startscanline = startscanline - (buffer2[0] * detect_cnt);
    8561           0 :                                   stopscanline = stopscanline - (buffer2[0] * detect_cnt);
    8562           0 :                                   free(buffer2);
    8563           0 :                                   j = 0;
    8564           0 :                                   for (l = 0; l < scene_cnt; l++)
    8565             :                                   {
    8566           0 :                                      if(idxmap[j] <= startscanline && idxmap[j+1] >= startscanline)
    8567             :                                      {
    8568           0 :                                         dims[l_index] = j;
    8569           0 :                                         startscandim = j;
    8570             :                                      }
    8571           0 :                                      if(idxmap[j] <= stopscanline && idxmap[j+1] >= stopscanline)
    8572           0 :                                         dims[l_index] = j - startscandim + 2;
    8573           0 :                                      j = j + 2;
    8574           0 :                                      if(idxmap[j] == 0  || idxmap[j+1] == 0)
    8575           0 :                                         l = scene_cnt;
    8576             :                                   }
    8577             : 
    8578             :                                }
    8579             :                          }
    8580             :                       }
    8581           0 :                       if (strcmp(fieldname, "scene_center_latitude") == 0 ||
    8582           0 :                           strcmp(fieldname, "scene_center_longitude") == 0)
    8583             :                       {
    8584           0 :                          startscanline = SWXRegion[regionID]->StartRegion[k];
    8585           0 :                          stopscanline = SWXRegion[regionID]->StopRegion[k];
    8586           0 :                          if(startscanline < idxmap[0])
    8587             :                          {
    8588           0 :                             startscandim = 0;
    8589           0 :                             dims[l_index] = 0;
    8590             :                          }
    8591           0 :                          for (l = 0; l < scene_cnt-1; l++)
    8592             :                          {
    8593           0 :                             if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
    8594           0 :                                if(startscandim == -1)
    8595             :                                {
    8596           0 :                                   dims[l_index] = l;
    8597           0 :                                   startscandim = l;
    8598             :                                }
    8599           0 :                             if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
    8600             :                             {
    8601           0 :                                dims[l_index] = l - startscandim + 2;
    8602           0 :                                stopscandim = l + 1;
    8603             :                             }
    8604             :                          }
    8605           0 :                          if(stopscandim == -1)
    8606             :                          {
    8607           0 :                             if(stopscanline > idxmap[scene_cnt - 1])
    8608             :                             {
    8609           0 :                                dims[l_index] = scene_cnt - startscandim;
    8610           0 :                                stopscandim = scene_cnt - 1;
    8611             :                             }
    8612             :                          }
    8613           0 :                          if(SWXRegion[regionID]->band8flag == 1)
    8614             :                          {
    8615           0 :                             if(stopscandim == -1)
    8616           0 :                                if(stopscanline > idxmap[scene_cnt - 1])
    8617             :                                {
    8618           0 :                                   dims[l_index] = scene_cnt - startscandim;
    8619           0 :                                   stopscandim = scene_cnt - 1;
    8620             :                                }
    8621             :                          }
    8622           0 :                          if(SWXRegion[regionID]->band8flag == 2 ||
    8623           0 :                             SWXRegion[regionID]->band8flag == 3)
    8624             :                          {
    8625           0 :                             if(startscandim == -1)
    8626             :                             {
    8627           0 :                                if(startscanline < idxmap[0])
    8628             :                                {
    8629           0 :                                   startscandim = 0;
    8630           0 :                                   dims[l_index] = stopscandim - startscandim + 1;
    8631             :                                }
    8632             :                             }
    8633           0 :                             if(startscandim == -1)
    8634             :                             {
    8635           0 :                                startscanline = SWXRegion[regionID]->StartScan[k] * detect_cnt;
    8636           0 :                                stopscanline = SWXRegion[regionID]->StopScan[k] * detect_cnt;
    8637           0 :                                for (l = 0; l < scene_cnt-1; l++)
    8638             :                                {
    8639           0 :                                   if(idxmap[l] <= startscanline && idxmap[l+1] >= startscanline)
    8640           0 :                                      dims[l_index] = l;
    8641           0 :                                   if(idxmap[l] <= stopscanline && idxmap[l+1] >= stopscanline)
    8642           0 :                                      dims[l_index] = l - dims[l_index] + 1;
    8643             :                                }
    8644             :                             }
    8645             :                          }
    8646             :                       }
    8647             :                    }
    8648             :                    else
    8649             :                    {
    8650           0 :                       if (datafld != 1)
    8651             :                       {
    8652             :              /* Get number of cross tracks in particular region */
    8653             :              /* ----------------------------------------------- */
    8654           0 :              nXtrk = idxmap[SWXRegion[regionID]->StopRegion[k]] -
    8655           0 :                idxmap[SWXRegion[regionID]->StartRegion[k]] + 1;
    8656             : 
    8657           0 :              dims[l_index] += nXtrk;
    8658             :                       }
    8659             :                     }
    8660             :     }
    8661             :       }
    8662             :       else
    8663             :       {
    8664             :     /* Mapping not found */
    8665             :     /* ----------------- */
    8666           0 :           status = -1;  
    8667           0 :     HEpush(DFE_GENAPP, "SWregioninfo",
    8668             :            __FILE__, __LINE__);
    8669           0 :     HEreport(
    8670             :        "Mapping Not Defined for \"%s\" Dimension.\n",
    8671             :        geodim);
    8672             :       }
    8673             :   }
    8674             : 
    8675             : 
    8676             : 
    8677             :   /* Vertical Subset */
    8678             :   /* --------------- */
    8679           0 :   if (status == 0 || status == -1)  /* check the vertical subset in any case -- xhua */
    8680             :   {
    8681           0 :       for (j = 0; j < 8; j++)
    8682             :       {
    8683             :     /* If active vertical subset ... */
    8684             :     /* ----------------------------- */
    8685           0 :     if (SWXRegion[regionID]->StartVertical[j] != -1)
    8686             :     {
    8687             : 
    8688             :         /* Find vertical dimension within dimlist */
    8689             :         /* -------------------------------------- */
    8690           0 :         l_index = EHstrwithin(SWXRegion[regionID]->DimNamePtr[j],
    8691             :           dimlist, ',');
    8692             : 
    8693             :         /* If dimension found ... */
    8694             :         /* ---------------------- */
    8695           0 :         if (l_index != -1)
    8696             :         {
    8697             :       /* Compute dimension size */
    8698             :       /* ---------------------- */
    8699           0 :       dims[l_index] =
    8700           0 :           SWXRegion[regionID]->StopVertical[j] -
    8701           0 :           SWXRegion[regionID]->StartVertical[j] + 1;
    8702             :         }
    8703             :         else
    8704             :         {
    8705             :       /* Vertical dimension not found */
    8706             :       /* ---------------------------- */
    8707           0 :       status = -1;
    8708           0 :       *size = -1;
    8709           0 :       HEpush(DFE_GENAPP, "SWregioninfo", __FILE__, __LINE__);
    8710           0 :       HEreport(errMesg, SWXRegion[regionID]->DimNamePtr[j]);
    8711             :         }
    8712             :     }
    8713             :       }
    8714             : 
    8715             : 
    8716             : 
    8717             :       /* Compute size of region data buffer */
    8718             :       /* ---------------------------------- */
    8719           0 :       if (status == 0)
    8720             :       {
    8721           0 :                 if(idxMapElem == 1 && SWXRegion[regionID]->scanflag == 1 && land_status == 0)
    8722             :                 {
    8723           0 :                     if(startscandim == dims[0])
    8724           0 :                        dims[0] = scene_cnt*2 - startscandim;
    8725             :                 }
    8726             : 
    8727             :     /* Compute number of total elements */
    8728             :     /* -------------------------------- */
    8729           0 :     *size = dims[0];
    8730           0 :     for (j = 1; j < *rank; j++)
    8731             :     {
    8732           0 :         *size *= dims[j];
    8733             :     }
    8734             : 
    8735             :     /* Multiply by size in bytes of numbertype */
    8736             :     /* --------------------------------------- */
    8737           0 :     *size *= DFKNTsize(*ntype);
    8738             :       }
    8739             :   }
    8740             :     }
    8741             : 
    8742             : 
    8743             : 
    8744             :     /* Free l_index mappings if applicable */
    8745             :     /* --------------------------------- */
    8746           0 :     if (idxmap != NULL)
    8747             :     {
    8748           0 :   free(idxmap);
    8749             :     }
    8750           0 :     if(dfieldlist != NULL)
    8751           0 :        free(dfieldlist);
    8752             : 
    8753           0 :     return (status);
    8754             : }
    8755             : 
    8756             : 
    8757             : 
    8758             : /*----------------------------------------------------------------------------|
    8759             : |  BEGIN_PROLOG                                                               |
    8760             : |                                                                             |
    8761             : |  FUNCTION: SWperiodinfo                                                     |
    8762             : |                                                                             |
    8763             : |  DESCRIPTION: Returns size in bytes of region                               |
    8764             : |                                                                             |
    8765             : |                                                                             |
    8766             : |  Return Value    Type     Units     Description                             |
    8767             : |  ============   ======  =========   =====================================   |
    8768             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    8769             : |                                                                             |
    8770             : |  INPUTS:                                                                    |
    8771             : |  swathID        int32               Swath structure ID                      |
    8772             : |  periodID       int32               Period ID                               |
    8773             : |  fieldname      char                Fieldname                               |
    8774             : |                                                                             |
    8775             : |                                                                             |
    8776             : |  OUTPUTS:                                                                   |
    8777             : |  ntype          int32               field number type                       |
    8778             : |  rank           int32               field rank                              |
    8779             : |  dims           int32               dimensions of field region              |
    8780             : |  size           int32               size in bytes of field region           |
    8781             : |                                                                             |
    8782             : |  NOTES:                                                                     |
    8783             : |                                                                             |
    8784             : |                                                                             |
    8785             : |   Date     Programmer   Description                                         |
    8786             : |  ======   ============  =================================================   |
    8787             : |  Jun 96   Joel Gales    Original Programmer                                 |
    8788             : |  Jun 03   Abe Taaheri   added clearing timeflag if SWregioninfo fails       |
    8789             : |                                                                             |
    8790             : |  END_PROLOG                                                                 |
    8791             : -----------------------------------------------------------------------------*/
    8792             : intn
    8793           0 : SWperiodinfo(int32 swathID, int32 periodID, const char *fieldname,
    8794             :        int32 * ntype, int32 * rank, int32 dims[], int32 * size)
    8795             : {
    8796             :     intn            status; /* routine return status variable */
    8797             : 
    8798             : 
    8799           0 :     timeflag = 1;
    8800             :     /* Call SWregioninfo */
    8801             :     /* ----------------- */
    8802           0 :     status = SWregioninfo(swathID, periodID, fieldname, ntype, rank,
    8803             :         dims, size);
    8804           0 :     if (status != 0) timeflag = 0;/* clear timeflag if SWregioninfo failed */
    8805           0 :     return (status);
    8806             : }
    8807             : 
    8808             : 
    8809             : 
    8810             : 
    8811             : 
    8812             : /*----------------------------------------------------------------------------|
    8813             : |  BEGIN_PROLOG                                                               |
    8814             : |                                                                             |
    8815             : |  FUNCTION: SWdefvrtregion                                                   |
    8816             : |                                                                             |
    8817             : |  DESCRIPTION: Finds elements of a monotonic field within a given range.     |
    8818             : |                                                                             |
    8819             : |                                                                             |
    8820             : |  Return Value    Type     Units     Description                             |
    8821             : |  ============   ======  =========   =====================================   |
    8822             : |  regionID       int32               Region ID                               |
    8823             : |                                                                             |
    8824             : |  INPUTS:                                                                    |
    8825             : |  swathID        int32               Swath structure ID                      |
    8826             : |  regionID       int32               Region ID                               |
    8827             : |  vertObj        char                Vertical object to subset               |
    8828             : |  range          float64             Vertical subsetting range               |
    8829             : |                                                                             |
    8830             : |  OUTPUTS:                                                                   |
    8831             : |             None                                                            |
    8832             : |                                                                             |
    8833             : |  NOTES:                                                                     |
    8834             : |                                                                             |
    8835             : |                                                                             |
    8836             : |   Date     Programmer   Description                                         |
    8837             : |  ======   ============  =================================================   |
    8838             : |  Aug 96   Joel Gales    Original Programmer                                 |
    8839             : |  Dec 96   Joel Gales    Add multiple vertical subsetting capability         |
    8840             : |  May 97   Joel Gales    Check for supported field types                     |
    8841             : |                                                                             |
    8842             : |  END_PROLOG                                                                 |
    8843             : -----------------------------------------------------------------------------*/
    8844             : 
    8845             : 
    8846             : /* Macro to initialize swath region entry */
    8847             : /* -------------------------------------- */
    8848             : 
    8849             : /*
    8850             :  * 1) Find empty (inactive) region. 2) Allocate space for region entry. 3)
    8851             :  * Store file ID and swath ID. 4) Set region ID. 5) Initialize vertical
    8852             :  * subset entries to -1.
    8853             :  */
    8854             : 
    8855             : #define SETSWTHREG \
    8856             : \
    8857             : for (k = 0; k < NSWATHREGN; k++) \
    8858             : { \
    8859             :     if (SWXRegion[k] == 0) \
    8860             :     { \
    8861             :         SWXRegion[k] = (struct swathRegion *) \
    8862             :     calloc(1, sizeof(struct swathRegion)); \
    8863             :   SWXRegion[k]->fid = fid; \
    8864             :   SWXRegion[k]->swathID = swathID; \
    8865             :   regionID = k; \
    8866             :   for (j=0; j<8; j++) \
    8867             :         { \
    8868             :              SWXRegion[k]->StartVertical[j] = -1; \
    8869             :              SWXRegion[k]->StopVertical[j]  = -1; \
    8870             :              SWXRegion[k]->StartScan[j] = -1; \
    8871             :              SWXRegion[k]->StopScan[j]  = -1; \
    8872             :              SWXRegion[k]->band8flag  = -1; \
    8873             :         } \
    8874             :   break; \
    8875             :      } \
    8876             : }
    8877             : 
    8878             : 
    8879             : /* Macro to fill vertical subset entry */
    8880             : /* ----------------------------------- */
    8881             : 
    8882             : /*
    8883             :  * 1) Find empty (inactive) vertical region. 2) Set start of vertical region.
    8884             :  * 3) Allocate space for name of vertical dimension. 4) Write vertical
    8885             :  * dimension name.
    8886             :  */
    8887             : 
    8888             : #define FILLVERTREG \
    8889             : for (j=0; j<8; j++) \
    8890             : { \
    8891             :     if (SWXRegion[regionID]->StartVertical[j] == -1) \
    8892             :     { \
    8893             :   SWXRegion[regionID]->StartVertical[j] = i; \
    8894             :   SWXRegion[regionID]->DimNamePtr[j] = \
    8895             :       (char *) malloc(slen + 1); \
    8896             :   memcpy(SWXRegion[regionID]->DimNamePtr[j], \
    8897             :          dimlist, slen + 1); \
    8898             :   break; \
    8899             :     } \
    8900             : } \
    8901             : 
    8902             : 
    8903             : 
    8904             : int32
    8905           0 : SWdefvrtregion(int32 swathID, int32 regionID, const char *vertObj, float64 range[])
    8906             : {
    8907             :     intn            i;    /* Loop index */
    8908           0 :     intn            j = 0;    /* Loop index */
    8909             :     intn            k;    /* Loop index */
    8910             :     intn            status; /* routine return status variable */
    8911             : 
    8912           0 :     uint8           found = 0;  /* Found flag */
    8913             : 
    8914             :     int16           vertINT16;  /* Temporary INT16 variable */
    8915             : 
    8916             :     int32           fid;  /* HDF-EOS file ID */
    8917             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    8918             :     int32           swVgrpID; /* Swath Vgroup ID */
    8919             :     int32           slen; /* String length */
    8920             :     int32           rank; /* Field rank */
    8921             :     int32           nt;   /* Field numbertype */
    8922             :     int32           dims[8];  /* Field dimensions */
    8923             :     int32           size; /* Size of numbertype in bytes */
    8924             :     int32           vertINT32;  /* Temporary INT32 variable */
    8925             : 
    8926             :     float32         vertFLT32;  /* Temporary FLT32 variable */
    8927             : 
    8928             :     float64         vertFLT64;  /* Temporary FLT64 variable */
    8929             : 
    8930             :     char           *vertArr;  /* Pointer to vertical field data buffer */
    8931             :     char            dimlist[256]; /* Dimension list */
    8932             : 
    8933             : 
    8934             :     /* Check for valid swath ID */
    8935             :     /* ------------------------ */
    8936           0 :     status = SWchkswid(swathID, "SWdefvrtregion", &fid, &sdInterfaceID,
    8937             :            &swVgrpID);
    8938             : 
    8939           0 :     if (status == 0)
    8940             :     {
    8941             :   /* Copy first 4 characters of vertObj into dimlist */
    8942             :   /* ----------------------------------------------- */
    8943           0 :   memcpy(dimlist, vertObj, 4);
    8944           0 :   dimlist[4] = 0;
    8945             : 
    8946             : 
    8947             : 
    8948             :   /* If first 4 characters of vertObj = "DIM:" ... */
    8949             :   /* --------------------------------------------- */
    8950             : 
    8951             :   /* Vertical Object is dimension name */
    8952             :   /* --------------------------------- */
    8953           0 :   if (strcmp(dimlist, "DIM:") == 0)
    8954             :   {
    8955             :       /* Get string length of vertObj (minus "DIM:) */
    8956             :       /* ------------------------------------------ */
    8957           0 :       slen = (int)strlen(vertObj) - 4;
    8958             : 
    8959             : 
    8960             :       /* If regionID = -1 then setup swath region entry */
    8961             :       /* ---------------------------------------------- */
    8962           0 :       if (regionID == -1)
    8963             :       {
    8964           0 :     SETSWTHREG;
    8965             :       }
    8966             : 
    8967             : 
    8968             :       /* Find first empty (inactive) vertical subset entry */
    8969             :       /* ------------------------------------------------- */
    8970           0 :       for (j = 0; j < 8; j++)
    8971             :       {
    8972           0 :     if (SWXRegion[regionID]->StartVertical[j] == -1)
    8973             :     {
    8974             :         /* Store start & stop of vertical region */
    8975             :         /* ------------------------------------- */
    8976           0 :         SWXRegion[regionID]->StartVertical[j] = (int32) range[0];
    8977           0 :         SWXRegion[regionID]->StopVertical[j] = (int32) range[1];
    8978             : 
    8979             :         /* Store vertical dimension name */
    8980             :         /* ----------------------------- */
    8981           0 :         SWXRegion[regionID]->DimNamePtr[j] =
    8982           0 :       (char *) malloc(slen + 1);
    8983           0 :         if(SWXRegion[regionID]->DimNamePtr[j] == NULL)
    8984             :         { 
    8985           0 :       HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__);
    8986           0 :       return(-1);
    8987             :         }
    8988           0 :         memcpy(SWXRegion[regionID]->DimNamePtr[j],
    8989           0 :          vertObj + 4, slen + 1);
    8990           0 :         break;
    8991             :     }
    8992             :       }
    8993             :   }
    8994             :   else
    8995             :   {
    8996             : 
    8997             :       /* Vertical Object is fieldname */
    8998             :       /* ---------------------------- */
    8999             : 
    9000             : 
    9001             :       /* Check for valid fieldname */
    9002             :       /* ------------------------- */
    9003           0 :       status = SWfieldinfo(swathID, vertObj, &rank, dims, &nt,
    9004             :          dimlist);
    9005             : 
    9006           0 :       if (status != 0)
    9007             :       {
    9008           0 :     status = -1;
    9009           0 :     HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__);
    9010           0 :     HEreport("Vertical Field: \"%s\" not found.\n", vertObj);
    9011             :       }
    9012             : 
    9013             : 
    9014             : 
    9015             :       /* Check for supported field types */
    9016             :       /* ------------------------------- */
    9017           0 :       if (nt != DFNT_INT16 &&
    9018           0 :     nt != DFNT_INT32 &&
    9019           0 :     nt != DFNT_FLOAT32 &&
    9020           0 :     nt != DFNT_FLOAT64)
    9021             :       {
    9022           0 :     status = -1;
    9023           0 :     HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__);
    9024           0 :     HEreport("Fieldtype: %d not supported for vertical subsetting.\n", nt);
    9025             :       }
    9026             : 
    9027             : 
    9028             : 
    9029             :       /* Check that vertical dimension is 1D */
    9030             :       /* ----------------------------------- */
    9031           0 :       if (status == 0)
    9032             :       {
    9033           0 :     if (rank != 1)
    9034             :     {
    9035           0 :         status = -1;
    9036           0 :         HEpush(DFE_GENAPP, "SWdefvrtregion", __FILE__, __LINE__);
    9037           0 :         HEreport("Vertical Field: \"%s\" must be 1-dim.\n",
    9038             :            vertObj);
    9039             :     }
    9040             :       }
    9041             : 
    9042             : 
    9043             :       /* If no problems then continue */
    9044             :       /* ---------------------------- */
    9045           0 :       if (status == 0)
    9046             :       {
    9047             :     /* Get string length of vertical dimension */
    9048             :     /* --------------------------------------- */
    9049           0 :     slen = (int)strlen(dimlist);
    9050             : 
    9051             : 
    9052             :     /* Get size in bytes of vertical field numbertype */
    9053             :     /* ---------------------------------------------- */
    9054           0 :     size = DFKNTsize(nt);
    9055             : 
    9056             : 
    9057             :     /* Allocate space for vertical field */
    9058             :     /* --------------------------------- */
    9059           0 :     vertArr = (char *) calloc(dims[0], size);
    9060           0 :     if(vertArr == NULL)
    9061             :     { 
    9062           0 :         HEpush(DFE_NOSPACE,"SWdefvrtregion", __FILE__, __LINE__);
    9063           0 :         return(-1);
    9064             :     }
    9065             : 
    9066             :     /* Read vertical field */
    9067             :     /* ------------------- */
    9068           0 :     status = SWreadfield(swathID, vertObj,
    9069             :              NULL, NULL, NULL, vertArr);
    9070             : 
    9071             : 
    9072             : 
    9073           0 :     switch (nt)
    9074             :     {
    9075           0 :     case DFNT_INT16:
    9076             : 
    9077           0 :         for (i = 0; i < dims[0]; i++)
    9078             :         {
    9079             :       /* Get single element of vertical field */
    9080             :       /* ------------------------------------ */
    9081           0 :       memcpy(&vertINT16, vertArr + i * size, size);
    9082             : 
    9083             : 
    9084             :       /* If within range ... */
    9085             :       /* ------------------- */
    9086           0 :       if (vertINT16 >= range[0] &&
    9087           0 :           vertINT16 <= range[1])
    9088             :       {
    9089             :           /* Set found flag */
    9090             :           /* -------------- */
    9091           0 :           found = 1;
    9092             : 
    9093             : 
    9094             :           /* If regionID=-1 then setup swath region entry */
    9095             :           /* -------------------------------------------- */
    9096           0 :           if (regionID == -1)
    9097             :           {
    9098           0 :         SETSWTHREG;
    9099             :           }
    9100             : 
    9101             : 
    9102             :           /* Fill-in vertical region entries */
    9103             :           /* ------------------------------- */
    9104           0 :           FILLVERTREG;
    9105             : 
    9106           0 :           break;
    9107             :       }
    9108             :         }
    9109             : 
    9110             : 
    9111             :         /* If found read from "bottom" of data field */
    9112             :         /* ----------------------------------------- */
    9113           0 :         if (found == 1)
    9114             :         {
    9115           0 :       for (i = dims[0] - 1; i >= 0; i--)
    9116             :       {
    9117             :           /* Get single element of vertical field */
    9118             :           /* ------------------------------------ */
    9119           0 :           memcpy(&vertINT16, vertArr + i * size, size);
    9120             : 
    9121             : 
    9122             :           /* If within range ... */
    9123             :           /* ------------------- */
    9124           0 :           if (vertINT16 >= range[0] &&
    9125           0 :         vertINT16 <= range[1])
    9126             :           {
    9127             :         /* Set end of vertical region */
    9128             :         /* -------------------------- */
    9129           0 :         SWXRegion[regionID]->StopVertical[j] = i;
    9130           0 :         break;
    9131             :           }
    9132             :       }
    9133             :         }
    9134             :         else
    9135             :         {
    9136             :       /* No vertical entries within region */
    9137             :       /* --------------------------------- */
    9138           0 :       status = -1;
    9139           0 :       HEpush(DFE_GENAPP, "SWdefvrtregion",
    9140             :              __FILE__, __LINE__);
    9141           0 :       HEreport("No vertical field entries within region.\n");
    9142             :         }
    9143           0 :         break;
    9144             : 
    9145             : 
    9146           0 :     case DFNT_INT32:
    9147             : 
    9148           0 :         for (i = 0; i < dims[0]; i++)
    9149             :         {
    9150             :       /* Get single element of vertical field */
    9151             :       /* ------------------------------------ */
    9152           0 :       memcpy(&vertINT32, vertArr + i * size, size);
    9153             : 
    9154             : 
    9155             :       /* If within range ... */
    9156             :       /* ------------------- */
    9157           0 :       if (vertINT32 >= range[0] &&
    9158           0 :           vertINT32 <= range[1])
    9159             :       {
    9160             :           /* Set found flag */
    9161             :           /* -------------- */
    9162           0 :           found = 1;
    9163             : 
    9164             : 
    9165             :           /* If regionID=-1 then setup swath region entry */
    9166             :           /* -------------------------------------------- */
    9167           0 :           if (regionID == -1)
    9168             :           {
    9169           0 :         SETSWTHREG;
    9170             :           }
    9171             : 
    9172             : 
    9173             :           /* Fill-in vertical region entries */
    9174             :           /* ------------------------------- */
    9175           0 :           FILLVERTREG;
    9176             : 
    9177           0 :           break;
    9178             :       }
    9179             :         }
    9180             : 
    9181             : 
    9182             :         /* If found read from "bottom" of data field */
    9183             :         /* ----------------------------------------- */
    9184           0 :         if (found == 1)
    9185             :         {
    9186           0 :       for (i = dims[0] - 1; i >= 0; i--)
    9187             :       {
    9188             :           /* Get single element of vertical field */
    9189             :           /* ------------------------------------ */
    9190           0 :           memcpy(&vertINT32, vertArr + i * size, size);
    9191             : 
    9192             : 
    9193             :           /* If within range ... */
    9194             :           /* ------------------- */
    9195           0 :           if (vertINT32 >= range[0] &&
    9196           0 :         vertINT32 <= range[1])
    9197             :           {
    9198             :         /* Set end of vertical region */
    9199             :         /* -------------------------- */
    9200           0 :         SWXRegion[regionID]->StopVertical[j] = i;
    9201           0 :         break;
    9202             :           }
    9203             :       }
    9204             :         }
    9205             :         else
    9206             :         {
    9207             :       /* No vertical entries within region */
    9208             :       /* --------------------------------- */
    9209           0 :       status = -1;
    9210           0 :       HEpush(DFE_GENAPP, "SWdefvrtregion",
    9211             :              __FILE__, __LINE__);
    9212           0 :       HEreport("No vertical field entries within region.\n");
    9213             :         }
    9214           0 :         break;
    9215             : 
    9216             : 
    9217           0 :     case DFNT_FLOAT32:
    9218             : 
    9219           0 :         for (i = 0; i < dims[0]; i++)
    9220             :         {
    9221             :       /* Get single element of vertical field */
    9222             :       /* ------------------------------------ */
    9223           0 :       memcpy(&vertFLT32, vertArr + i * size, size);
    9224             : 
    9225             : 
    9226             :       /* If within range ... */
    9227             :       /* ------------------- */
    9228           0 :       if (vertFLT32 >= range[0] &&
    9229           0 :           vertFLT32 <= range[1])
    9230             :       {
    9231             :           /* Set found flag */
    9232             :           /* -------------- */
    9233           0 :           found = 1;
    9234             : 
    9235             : 
    9236             :           /* If regionID=-1 then setup swath region entry */
    9237             :           /* -------------------------------------------- */
    9238           0 :           if (regionID == -1)
    9239             :           {
    9240           0 :         SETSWTHREG;
    9241             :           }
    9242             : 
    9243             : 
    9244             :           /* Fill-in vertical region entries */
    9245             :           /* ------------------------------- */
    9246           0 :           FILLVERTREG;
    9247             : 
    9248           0 :           break;
    9249             :       }
    9250             :         }
    9251             : 
    9252             : 
    9253             :         /* If found read from "bottom" of data field */
    9254             :         /* ----------------------------------------- */
    9255           0 :         if (found == 1)
    9256             :         {
    9257           0 :       for (i = dims[0] - 1; i >= 0; i--)
    9258             :       {
    9259             :           /* Get single element of vertical field */
    9260             :           /* ------------------------------------ */
    9261           0 :           memcpy(&vertFLT32, vertArr + i * size, size);
    9262             : 
    9263             : 
    9264             :           /* If within range ... */
    9265             :           /* ------------------- */
    9266           0 :           if (vertFLT32 >= range[0] &&
    9267           0 :         vertFLT32 <= range[1])
    9268             :           {
    9269             :         /* Set end of vertical region */
    9270             :         /* -------------------------- */
    9271           0 :         SWXRegion[regionID]->StopVertical[j] = i;
    9272           0 :         break;
    9273             :           }
    9274             :       }
    9275             :         }
    9276             :         else
    9277             :         {
    9278             :       /* No vertical entries within region */
    9279             :       /* --------------------------------- */
    9280           0 :       status = -1;
    9281           0 :       HEpush(DFE_GENAPP, "SWdefvrtregion",
    9282             :              __FILE__, __LINE__);
    9283           0 :       HEreport("No vertical field entries within region.\n");
    9284             :         }
    9285           0 :         break;
    9286             : 
    9287             : 
    9288           0 :     case DFNT_FLOAT64:
    9289             : 
    9290           0 :         for (i = 0; i < dims[0]; i++)
    9291             :         {
    9292             :       /* Get single element of vertical field */
    9293             :       /* ------------------------------------ */
    9294           0 :       memcpy(&vertFLT64, vertArr + i * size, size);
    9295             : 
    9296             : 
    9297             :       /* If within range ... */
    9298             :       /* ------------------- */
    9299           0 :       if (vertFLT64 >= range[0] &&
    9300           0 :           vertFLT64 <= range[1])
    9301             :       {
    9302             :           /* Set found flag */
    9303             :           /* -------------- */
    9304           0 :           found = 1;
    9305             : 
    9306             : 
    9307             :           /* If regionID=-1 then setup swath region entry */
    9308             :           /* -------------------------------------------- */
    9309           0 :           if (regionID == -1)
    9310             :           {
    9311           0 :         SETSWTHREG;
    9312             :           }
    9313             : 
    9314             : 
    9315             :           /* Fill-in vertical region entries */
    9316             :           /* ------------------------------- */
    9317           0 :           FILLVERTREG;
    9318             : 
    9319           0 :           break;
    9320             :       }
    9321             :         }
    9322             : 
    9323             : 
    9324             :         /* If found read from "bottom" of data field */
    9325             :         /* ----------------------------------------- */
    9326           0 :         if (found == 1)
    9327             :         {
    9328           0 :       for (i = dims[0] - 1; i >= 0; i--)
    9329             :       {
    9330             :           /* Get single element of vertical field */
    9331             :           /* ------------------------------------ */
    9332           0 :           memcpy(&vertFLT64, vertArr + i * size, size);
    9333             : 
    9334             :           /* If within range ... */
    9335             :           /* ------------------- */
    9336           0 :           if (vertFLT64 >= range[0] &&
    9337           0 :         vertFLT64 <= range[1])
    9338             :           {
    9339             :         /* Set end of vertical region */
    9340             :         /* -------------------------- */
    9341           0 :         SWXRegion[regionID]->StopVertical[j] = i;
    9342           0 :         break;
    9343             :           }
    9344             :       }
    9345             :         }
    9346             :         else
    9347             :         {
    9348             :       /* No vertical entries within region */
    9349             :       /* --------------------------------- */
    9350           0 :       status = -1;
    9351           0 :       HEpush(DFE_GENAPP, "SWdefvrtregion",
    9352             :              __FILE__, __LINE__);
    9353           0 :       HEreport("No vertical field entries within region.\n");
    9354             :         }
    9355           0 :         break;
    9356             : 
    9357             :     }   /* End of switch */
    9358           0 :     free(vertArr);
    9359             :       }
    9360             :   }
    9361             :     }
    9362             : 
    9363             : 
    9364             :     /* Set regionID to -1 if bad return status */
    9365             :     /* --------------------------------------- */
    9366           0 :     if (status == -1)
    9367             :     {
    9368           0 :   regionID = -1;
    9369             :     }
    9370             : 
    9371             : 
    9372           0 :     return (regionID);
    9373             : }
    9374             : 
    9375             : 
    9376             : 
    9377             : /*----------------------------------------------------------------------------|
    9378             : |  BEGIN_PROLOG                                                               |
    9379             : |                                                                             |
    9380             : |  FUNCTION: SWdefscanregion                                                  |
    9381             : |                                                                             |
    9382             : |  DESCRIPTION: Initialize the region structure for Landsat 7 float scene     |
    9383             : |               subset                    |
    9384             : |                                                                             |
    9385             : |                                                                             |
    9386             : |  Return Value    Type     Units     Description                             |
    9387             : |  ============   ======  =========   =====================================   |
    9388             : |  regionID       int32               Region ID                               |
    9389             : |                                                                             |
    9390             : |  INPUTS:                                                                    |
    9391             : |  swathID        int32               Swath structure ID                      |
    9392             : |  fieldname      char                Field name to subset                    |
    9393             : |  range          float64             subsetting range                        |
    9394             : |  mode     int32         HDFE_ENDPOINT, HDFE_MIDPOINT or         |
    9395             : |             HDFE_ANYPOINT                           |
    9396             : |                                                                             |
    9397             : |  OUTPUTS:                                                                   |
    9398             : |  regionID   int32         Region ID                               |
    9399             : |                                                                             |
    9400             : |  NOTES:                                                                     |
    9401             : |                                                                             |
    9402             : |                                                                             |
    9403             : |   Date     Programmer   Description                                         |
    9404             : |  ======   ============  =================================================   |
    9405             : |  Jul 99   DaW     Original Programmer                                 |
    9406             : |                                                                             |
    9407             : |  END_PROLOG                                                                 |
    9408             : -----------------------------------------------------------------------------*/
    9409             : int32
    9410           0 : SWdefscanregion(int32 swathID, const char *fieldname, float64 range[], CPL_UNUSED int32 mode)
    9411             : {
    9412             :     intn            j;    /* Loop index */
    9413             :     intn            k;    /* Loop index */
    9414             :     intn            status; /* routine return status variable */
    9415           0 :     intn            land_status = 3;  /* routine return status variable */
    9416           0 :     intn      band81flag = 0;
    9417           0 :     intn      band82flag = 0;
    9418           0 :     intn      band83flag = 0;
    9419           0 :     uint8           detect_cnt = 0;     /* Used to convert scan to scanline  */
    9420             :                                         /*  L7 float scene sub.              */
    9421           0 :     uint8           scene_cnt = 0;
    9422             : 
    9423             : 
    9424           0 :     int32           nmtype = 0; /* Used for L7 float scene sub. */
    9425           0 :     int32           count = 0;   /* Used for L7 float scene sub. */
    9426             :     int32           fid;  /* HDF-EOS file ID */
    9427             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    9428             :     int32           swVgrpID; /* Swath Vgroup ID */
    9429             :     int32           slen; /* String length */
    9430             :     int32     dfrank[8];  /* data fields rank */
    9431             :     int32           rank; /* Field rank */
    9432             :     int32     numtype[8]; /* number type of data fields */
    9433             :     int32           nt;   /* Field numbertype */
    9434             :     int32           dims[8];  /* Field dimensions */
    9435             :     int32           dims2[8]; /* Field dimensions */
    9436           0 :     int32     strbufsize = 0; /* string buffer size */
    9437           0 :     int32     tmprange0 = 0;
    9438             : 
    9439             : 
    9440           0 :     uint16      *buffer = (uint16 *)NULL;
    9441           0 :     int32     *idxmap = (int32 *)NULL;
    9442             : 
    9443           0 :     int32     dimsize = 0;
    9444             : 
    9445           0 :     int32           regionID = -1;  /* Region ID (return) */
    9446             : 
    9447           0 :     float64     scan[2] = {0,0};
    9448           0 :     float64     original_scan[2] = {0,0};
    9449             : 
    9450             :     char            dimlist[256]; /* Dimension list */
    9451             :     char      swathname[80];
    9452           0 :     char      *dfieldlist = (char *)NULL;  /* data field list  */
    9453           0 :     char      *tfieldname = (char *)NULL;  /* temp field buffer  */
    9454           0 :     char      *band81 = (char *)NULL;
    9455           0 :     char      *band82 = (char *)NULL;
    9456           0 :     char      *band83 = (char *)NULL;
    9457             : 
    9458             : 
    9459             :     /* Check for valid swath ID */
    9460             :     /* ------------------------ */
    9461           0 :     status = SWchkswid(swathID, "SWdefscanregion", &fid, &sdInterfaceID,
    9462             :            &swVgrpID);
    9463             : 
    9464             :    /* This code checks for the attribute detector_count */
    9465             :    /* which is found in Landsat 7 files.  It is used    */
    9466             :    /* for some of the loops. The other code checks if   */
    9467             :    /* one scan is requested.                            */
    9468             :    /* ================================================= */
    9469           0 :       land_status = SWattrinfo(swathID, "detector_count", &nmtype, &count);
    9470           0 :       if (land_status == 0)
    9471             :       {
    9472           0 :          scan[0] = range[0];
    9473           0 :          scan[1] = range[1];
    9474           0 :          original_scan[0] = range[0];
    9475           0 :          original_scan[1] = range[1];
    9476             : 
    9477           0 :          land_status = SWreadattr(swathID, "scene_count", &scene_cnt);
    9478           0 :          land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
    9479           0 :          if (range[0] == range[1])
    9480             :          {
    9481           0 :             range[0] = range[0] * detect_cnt - detect_cnt;
    9482           0 :             range[1] = range[0] + detect_cnt - 1;
    9483             :          }
    9484             :          else
    9485             :          {
    9486           0 :             range[0] = range[0] * detect_cnt - detect_cnt;
    9487           0 :             range[1] = range[1] * detect_cnt - 1;
    9488             :          }
    9489             : 
    9490           0 :          Vgetname(SWXSwath[0].IDTable, swathname);
    9491           0 :          band81 = strstr(swathname, "B81");
    9492           0 :          if (band81 != (char *)NULL)
    9493           0 :             band81flag = 1;
    9494           0 :          band82 = strstr(swathname, "B82");
    9495           0 :          if (band82 != (char *)NULL)
    9496           0 :             band82flag = 1;
    9497           0 :          band83 = strstr(swathname, "B83");
    9498           0 :          if (band83 != (char *)NULL)
    9499           0 :             band83flag = 1;
    9500             :       }
    9501             : 
    9502             : 
    9503             :     /* If fieldname is null then subsetting Landsat 7 */
    9504             :     /* floating scene. Get data field name, assume    */
    9505             :     /* only one data field in swath                   */
    9506             :     /* ---------------------------------------------- */
    9507             : 
    9508           0 :     if (fieldname == (char *)NULL)
    9509             :     {
    9510           0 :        (void) SWnentries(swathID, 4, &strbufsize);
    9511           0 :        dfieldlist = (char *)calloc(strbufsize + 1, sizeof(char));
    9512           0 :        (void) SWinqdatafields(swathID, dfieldlist, dfrank, numtype);
    9513           0 :        tfieldname = (char *)calloc(strbufsize + 1, sizeof(char));
    9514           0 :        strcpy(tfieldname, dfieldlist);
    9515             :     }
    9516             :     else
    9517             :     {
    9518           0 :        slen = (int)strlen(fieldname);
    9519           0 :        tfieldname = (char *)calloc(slen + 1, sizeof(char));
    9520           0 :        strcpy(tfieldname, fieldname);
    9521             :     }
    9522             : 
    9523             :     /* Check for valid fieldname */
    9524             :     /* ------------------------- */
    9525           0 :     status = SWfieldinfo(swathID, tfieldname, &rank, dims, &nt,
    9526             :    dimlist);
    9527             : 
    9528           0 :     if (status != 0)
    9529             :     {
    9530           0 :   status = -1;
    9531           0 :   HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
    9532           0 :   HEreport("Field: \"%s\" not found.\n", tfieldname);
    9533             :     }
    9534             : 
    9535             : 
    9536             :        /* Check if input range values are within range of */
    9537             :        /* data field                                      */
    9538             :        /* ----------------------------------------------- */
    9539           0 :        if(status == 0)
    9540             :        {
    9541           0 :           status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
    9542           0 :           buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
    9543           0 :           status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
    9544           0 :           if(scan[0] > buffer[dims2[0]-1])
    9545             :           {
    9546           0 :              HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
    9547           0 :              HEreport("Range values not within bounds of data field\n");
    9548           0 :              free(buffer);
    9549           0 :              buffer = (uint16 *)NULL;
    9550           0 :              if (dfieldlist != NULL)
    9551           0 :                 free(dfieldlist);
    9552           0 :              free(tfieldname);
    9553           0 :              return(-1);
    9554             :           }
    9555           0 :           if(scan[0] < buffer[0])
    9556             :           {
    9557           0 :              if(scan[1] < buffer[0])
    9558             :              {
    9559           0 :                 HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
    9560           0 :                 HEreport("Range values not within bounds of data field\n");
    9561           0 :                 free(buffer);
    9562           0 :                 buffer = (uint16 *)NULL;
    9563           0 :                 if (dfieldlist != NULL)
    9564           0 :                    free(dfieldlist);
    9565           0 :                 free(tfieldname);
    9566           0 :                 return(-1);
    9567             :              }
    9568             :              else
    9569             :              {
    9570           0 :                 scan[0] = buffer[0];
    9571           0 :                 range[0] = scan[0] * detect_cnt - detect_cnt;
    9572             :              }
    9573             :           }
    9574           0 :           if(scan[1] > buffer[dims2[0] - 1])
    9575             :           {
    9576           0 :              scan[1] = buffer[dims2[0] - 1];
    9577           0 :              range[1] = scan[1] * detect_cnt - 1;
    9578             :           }
    9579             :        }
    9580             : 
    9581           0 :        if(status == 0)
    9582             :        {
    9583           0 :           dimsize = SWdiminfo(swathID, "GeoTrack");
    9584           0 :           if(dimsize > 0)
    9585             :           {
    9586           0 :              idxmap = (int32 *)calloc(dimsize, sizeof(int32));
    9587           0 :              (void) SWidxmapinfo(swathID, "GeoTrack", "ScanLineTrack", idxmap);
    9588           0 :              tmprange0 = (int32)range[0];
    9589           0 :              if(band82flag != 1 && band83flag != 1)
    9590             :              {
    9591           0 :                 if (range[1] > idxmap[scene_cnt*2 - 1])
    9592             :                 {
    9593           0 :                    range[1] = idxmap[scene_cnt*2 - 1];
    9594           0 :                    HEreport("Data length compared to geolocation length\n");
    9595             :                 }
    9596             :              }
    9597           0 :              if(band82flag == 1 || band83flag == 1)
    9598             :              {
    9599           0 :                 tmprange0 = (int32)(range[0] - (buffer[0] * detect_cnt - detect_cnt));
    9600             :              }
    9601           0 :              if(tmprange0 >= idxmap[scene_cnt * 2 - 1])
    9602             :              {
    9603           0 :                 HEpush(DFE_GENAPP, "SWdefscanregion", __FILE__, __LINE__);
    9604           0 :                 HEreport(
    9605             :             "Range values not within bounds of Latitude/Longitude field(s)\n");
    9606           0 :                 if (dfieldlist != NULL)
    9607           0 :                    free(dfieldlist);
    9608           0 :                 free(tfieldname);
    9609           0 :                 free(buffer);
    9610           0 :                 free(idxmap);
    9611           0 :                 return(-1);
    9612             :              }
    9613             :           }
    9614             :        }
    9615             : 
    9616           0 :     if (status == 0)
    9617             :     {
    9618           0 :           slen = (int)strlen(tfieldname);
    9619             : 
    9620           0 :           SETSWTHREG;
    9621             : 
    9622             :     /* Find first empty (inactive) vertical subset entry */
    9623             :     /* ------------------------------------------------- */
    9624           0 :     for (j = 0; j < 8; j++)
    9625             :     {
    9626           0 :        if (SWXRegion[regionID]->StartVertical[j] == -1)
    9627             :        {
    9628             :         /* Store start & stop of region          */
    9629             :         /* ------------------------------------- */
    9630           0 :                 SWXRegion[regionID]->StartScan[j] = (int32) original_scan[0];
    9631           0 :                 SWXRegion[regionID]->StopScan[j] = (int32) original_scan[1];
    9632           0 :         SWXRegion[regionID]->StartRegion[j] = (int32) range[0];
    9633           0 :         SWXRegion[regionID]->StopRegion[j] = (int32) range[1];
    9634           0 :                     ++SWXRegion[regionID]->nRegions;
    9635           0 :                     SWXRegion[regionID]->scanflag = 1;
    9636           0 :                     if(band81flag == 1)
    9637           0 :                        SWXRegion[regionID]->band8flag = 1;
    9638           0 :                     if(band82flag == 1)
    9639           0 :                        SWXRegion[regionID]->band8flag = 2;
    9640           0 :                     if(band83flag == 1)
    9641           0 :                        SWXRegion[regionID]->band8flag = 3;
    9642           0 :                    break;
    9643             :     }
    9644             :       }
    9645             :        }
    9646             : 
    9647             : 
    9648             :     /* Set regionID to -1 if bad return status */
    9649             :     /* --------------------------------------- */
    9650           0 :     if (status == -1)
    9651             :     {
    9652           0 :   regionID = -1;
    9653             :     }
    9654             : 
    9655           0 :     if (dfieldlist != NULL)
    9656           0 :        free(dfieldlist);
    9657           0 :     free(tfieldname);
    9658           0 :     if (buffer != NULL)
    9659           0 :        free(buffer);
    9660           0 :     if (idxmap != NULL)
    9661           0 :     free(idxmap);
    9662             : 
    9663           0 :     return (regionID);
    9664             : }
    9665             : 
    9666             : 
    9667             : /*----------------------------------------------------------------------------|
    9668             : |  BEGIN_PROLOG                                                               |
    9669             : |                                                                             |
    9670             : |  FUNCTION: SWsetfillvalue                                                   |
    9671             : |                                                                             |
    9672             : |  DESCRIPTION: Sets fill value for the specified field.                      |
    9673             : |                                                                             |
    9674             : |                                                                             |
    9675             : |  Return Value    Type     Units     Description                             |
    9676             : |  ============   ======  =========   =====================================   |
    9677             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    9678             : |                                                                             |
    9679             : |  INPUTS:                                                                    |
    9680             : |  swathID        int32               swath structure ID                      |
    9681             : |  fieldname      char                field name                              |
    9682             : |  fillval        void                fill value                              |
    9683             : |                                                                             |
    9684             : |  OUTPUTS:                                                                   |
    9685             : |             None                                                            |
    9686             : |                                                                             |
    9687             : |  NOTES:                                                                     |
    9688             : |                                                                             |
    9689             : |                                                                             |
    9690             : |   Date     Programmer   Description                                         |
    9691             : |  ======   ============  =================================================   |
    9692             : |  Jun 96   Joel Gales    Original Programmer                                 |
    9693             : |                                                                             |
    9694             : |  END_PROLOG                                                                 |
    9695             : -----------------------------------------------------------------------------*/
    9696             : intn
    9697           0 : SWsetfillvalue(int32 swathID, const char *fieldname, VOIDP fillval)
    9698             : {
    9699             :     intn            status; /* routine return status variable */
    9700             : 
    9701             :     int32           fid;  /* HDF-EOS file ID */
    9702             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    9703             :     int32           swVgrpID; /* Swath root Vgroup ID */
    9704             :     int32           sdid; /* SDS id */
    9705             :     int32           nt;   /* Number type */
    9706             :     int32           dims[8];  /* Dimensions array */
    9707             :     int32           dum;  /* Dummy variable */
    9708             :     int32           solo; /* "Solo" (non-merged) field flag */
    9709             : 
    9710             :     char            name[80]; /* Fill value "attribute" name */
    9711             : 
    9712             :     /* Check for valid swath ID and get SDS interface ID */
    9713           0 :     status = SWchkswid(swathID, "SWsetfillvalue",
    9714             :            &fid, &sdInterfaceID, &swVgrpID);
    9715             : 
    9716           0 :     if (status == 0)
    9717             :     {
    9718             :   /* Get field info */
    9719           0 :   status = SWfieldinfo(swathID, fieldname, &dum, dims, &nt, NULL);
    9720             : 
    9721           0 :   if (status == 0)
    9722             :   {
    9723             :       /* Get SDS ID and solo flag */
    9724           0 :       status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
    9725             :          &sdid, &dum, &dum, &dum,
    9726             :          dims, &solo);
    9727             : 
    9728             :       /* If unmerged field then call HDF set field routine */
    9729           0 :       if (solo == 1)
    9730             :       {
    9731           0 :                 /* status = */ SDsetfillvalue(sdid, fillval);
    9732             :       }
    9733             : 
    9734             :       /*
    9735             :        * Store fill value in attribute.  Name is given by fieldname
    9736             :        * prepended with "_FV_"
    9737             :        */
    9738           0 :       strcpy(name, "_FV_");
    9739           0 :       strcat(name, fieldname);
    9740           0 :       status = SWwriteattr(swathID, name, nt, 1, fillval);
    9741             :   }
    9742             :   else
    9743             :   {
    9744           0 :       HEpush(DFE_GENAPP, "SWsetfillvalue", __FILE__, __LINE__);
    9745           0 :       HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
    9746             :   }
    9747             :     }
    9748           0 :     return (status);
    9749             : }
    9750             : 
    9751             : 
    9752             : 
    9753             : 
    9754             : 
    9755             : /*----------------------------------------------------------------------------|
    9756             : |  BEGIN_PROLOG                                                               |
    9757             : |                                                                             |
    9758             : |  FUNCTION: SWgetfillvalue                                                   |
    9759             : |                                                                             |
    9760             : |  DESCRIPTION: Retrieves fill value for a specified field.                   |
    9761             : |                                                                             |
    9762             : |                                                                             |
    9763             : |  Return Value    Type     Units     Description                             |
    9764             : |  ============   ======  =========   =====================================   |
    9765             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    9766             : |                                                                             |
    9767             : |  INPUTS:                                                                    |
    9768             : |  swathID        int32               swath structure ID                      |
    9769             : |  fieldname      char                field name                              |
    9770             : |                                                                             |
    9771             : |  OUTPUTS:                                                                   |
    9772             : |  fillval        void                fill value                              |
    9773             : |                                                                             |
    9774             : |  NOTES:                                                                     |
    9775             : |                                                                             |
    9776             : |                                                                             |
    9777             : |   Date     Programmer   Description                                         |
    9778             : |  ======   ============  =================================================   |
    9779             : |  Jun 96   Joel Gales    Original Programmer                                 |
    9780             : |                                                                             |
    9781             : |  END_PROLOG                                                                 |
    9782             : -----------------------------------------------------------------------------*/
    9783             : intn
    9784           0 : SWgetfillvalue(int32 swathID, const char *fieldname, VOIDP fillval)
    9785             : {
    9786             :     intn            status; /* routine return status variable */
    9787             : 
    9788             :     int32           nt;   /* Number type */
    9789             :     int32           dims[8];  /* Dimensions array */
    9790             :     int32           dum;  /* Dummy variable */
    9791             : 
    9792             :     char            name[80]; /* Fill value "attribute" name */
    9793             : 
    9794             :     /* Check for valid swath ID */
    9795           0 :     status = SWchkswid(swathID, "SWgetfillvalue", &dum, &dum, &dum);
    9796             : 
    9797           0 :     if (status == 0)
    9798             :     {
    9799             :   /* Get field info */
    9800           0 :   status = SWfieldinfo(swathID, fieldname, &dum, dims, &nt, NULL);
    9801             : 
    9802           0 :   if (status == 0)
    9803             :   {
    9804             :       /* Read fill value attribute */
    9805           0 :       strcpy(name, "_FV_");
    9806           0 :       strcat(name, fieldname);
    9807           0 :       status = SWreadattr(swathID, name, fillval);
    9808             :   }
    9809             :   else
    9810             :   {
    9811           0 :       HEpush(DFE_GENAPP, "SWgetfillvalue", __FILE__, __LINE__);
    9812           0 :       HEreport("Fieldname \"%s\" does not exist.\n", fieldname);
    9813             :   }
    9814             : 
    9815             :     }
    9816           0 :     return (status);
    9817             : }
    9818             : 
    9819             : 
    9820             : /*----------------------------------------------------------------------------|
    9821             : |  BEGIN_PROLOG                                                               |
    9822             : |                                                                             |
    9823             : |  FUNCTION: SWdetach                                                         |
    9824             : |                                                                             |
    9825             : |  DESCRIPTION: Detaches swath structure and performs housekeeping            |
    9826             : |                                                                             |
    9827             : |                                                                             |
    9828             : |  Return Value    Type     Units     Description                             |
    9829             : |  ============   ======  =========   =====================================   |
    9830             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    9831             : |                                                                             |
    9832             : |  INPUTS:                                                                    |
    9833             : |  swathID        int32               swath structure ID                      |
    9834             : |                                                                             |
    9835             : |                                                                             |
    9836             : |  OUTPUTS:                                                                   |
    9837             : |             None                                                            |
    9838             : |                                                                             |
    9839             : |  NOTES:                                                                     |
    9840             : |                                                                             |
    9841             : |                                                                             |
    9842             : |   Date     Programmer   Description                                         |
    9843             : |  ======   ============  =================================================   |
    9844             : |  Jun 96   Joel Gales    Original Programmer                                 |
    9845             : |  Aug 96   Joel Gales    Cleanup Region External Structure                   |
    9846             : |  Sep 96   Joel Gales    Setup dim names for SDsetdimnane in dimbuf1 rather  |
    9847             : |                         than utlstr                                         |
    9848             : |  Nov 96   Joel Gales    Call SWchkgdid to check for proper swath ID         |
    9849             : |  Dec 96   Joel Gales    Add multiple vertical subsetting garbage collection |
    9850             : |                                                                             |
    9851             : |  END_PROLOG                                                                 |
    9852             : -----------------------------------------------------------------------------*/
    9853             : intn
    9854           0 : SWdetach(int32 swathID)
    9855             : 
    9856             : {
    9857             :     intn            i;    /* Loop index */
    9858             :     intn            j;    /* Loop index */
    9859             :     intn            k;    /* Loop index */
    9860           0 :     intn            status = 0; /* routine return status variable */
    9861           0 :     intn            statusFill = 0; /* return status from SWgetfillvalue */
    9862             : 
    9863             :     uint8          *buf;  /* Buffer for blank (initial) 1D records */
    9864             : 
    9865             :     int32           vdataID;  /* Vdata ID */
    9866             :     int32          *namelen;  /* Pointer to name string length array */
    9867             :     int32          *dimlen; /* Pointer to dim string length array */
    9868             :     int32           slen1[3]; /* String length array 1 */
    9869             :     int32           slen2[3]; /* String length array 2 */
    9870             :     int32           nflds;  /* Number of fields */
    9871             :     int32           match[5]; /* Merged field match array */
    9872             :     int32           cmbfldcnt;  /* Number of fields combined */
    9873             :     int32           sdid; /* SDS ID */
    9874             :     int32           vgid; /* Vgroup ID */
    9875             :     int32           dims[3];  /* Dimension array */
    9876             :     int32          *offset; /* Pointer to merged field offset array */
    9877             :     int32          *indvdims; /* Pointer to merged field size array */
    9878             :     int32           sdInterfaceID;  /* SDS interface ID */
    9879             :     int32           sID;  /* Swath ID - offset */
    9880             :     int32           nflds0; /* Number of fields */
    9881             :     int32          *namelen0; /* Pointer to name string length array */
    9882             :     int32           rank; /* Rank of merged field */
    9883             :     int32           truerank; /* True rank of merged field */
    9884           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
    9885             :     int32           dum;  /* Dummy variable */
    9886             : 
    9887             :     char           *nambuf; /* Pointer to name buffer */
    9888             :     char          **nameptr;  /* Pointer to name string pointer array */
    9889             :     char          **dimptr; /* Pointer to dim string pointer array */
    9890             :     char          **nameptr0; /* Pointer to name string pointer array */
    9891             :     char           *ptr1[3];  /* String pointer array */
    9892             :     char           *ptr2[3];  /* String pointer array */
    9893             :     char            dimbuf1[128]; /* Dimension buffer 1 */
    9894             :     char            dimbuf2[128]; /* Dimension buffer 2 */
    9895             :     char            swathname[VGNAMELENMAX + 1];  /* Swath name */
    9896             :     char           *utlbuf; /* Utility buffer */
    9897             :     char            fillval[32];/* Fill value buffer */
    9898             : 
    9899             :     /* Check for proper swath ID and get SD interface ID */
    9900             :     /* ------------------------------------------------- */
    9901           0 :     status = SWchkswid(swathID, "SWdetach", &dum, &sdInterfaceID, &dum);
    9902             : 
    9903           0 :     if (status == 0)
    9904             :     {
    9905             :   /* Subtract off swath ID offset and get swath name */
    9906             :   /* ----------------------------------------------- */
    9907           0 :   sID = swathID % idOffset;
    9908           0 :   Vgetname(SWXSwath[sID].IDTable, swathname);
    9909             : 
    9910             : 
    9911             :   /* Create 1D "orphaned" fields */
    9912             :   /* --------------------------- */
    9913           0 :   i = 0;
    9914             : 
    9915             :   /* Find "active" entries in 1d combination array */
    9916             :   /* --------------------------------------------- */
    9917           0 :   while (SWX1dcomb[3 * i] != 0)
    9918             :   {
    9919             :       /* For fields defined within swath... */
    9920             :       /* ---------------------------------- */
    9921           0 :       if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable)
    9922             :       {
    9923             :     /* Get dimension size and vdata ID */
    9924             :     /* ------------------------------- */
    9925           0 :     dims[0] = abs(SWX1dcomb[3 * i]);
    9926           0 :     vdataID = SWX1dcomb[3 * i + 2];
    9927             : 
    9928             :     /* Get fieldname (= vdata name) */
    9929             :     /* ---------------------------- */
    9930           0 :     nambuf = (char *) calloc(VSNAMELENMAX + 1, 1);
    9931           0 :     if(nambuf == NULL)
    9932             :     { 
    9933           0 :         HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    9934           0 :         return(-1);
    9935             :     }
    9936             : 
    9937           0 :     VSgetname(vdataID, nambuf);
    9938             : 
    9939             :     /* Set field within vdata */
    9940             :     /* ---------------------- */
    9941           0 :     VSsetfields(vdataID, nambuf);
    9942             : 
    9943             :     /* Write (blank) records */
    9944             :     /* --------------------- */
    9945           0 :     buf = (uint8 *) calloc(VSsizeof(vdataID, nambuf), dims[0]);
    9946           0 :     if(buf == NULL)
    9947             :     { 
    9948           0 :         HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    9949           0 :         free(nambuf);
    9950           0 :         return(-1);
    9951             :     }
    9952           0 :     VSwrite(vdataID, buf, dims[0], FULL_INTERLACE);
    9953             : 
    9954           0 :     free(buf);
    9955           0 :     free(nambuf);
    9956             : 
    9957             :     /* Detach Vdata */
    9958             :     /* ------------ */
    9959           0 :     VSdetach(vdataID);
    9960             :       }
    9961           0 :       i++;
    9962             :   }
    9963             : 
    9964             : 
    9965             :   /* SDS combined fields */
    9966             :   /* ------------------- */
    9967           0 :   if (strlen(SWXSDname) == 0)
    9968             :   {
    9969           0 :       nflds = 0;
    9970             : 
    9971             :       /* Allocate "dummy" arrays so free() doesn't bomb later */
    9972             :       /* ---------------------------------------------------- */
    9973           0 :       nameptr = (char **) calloc(1, sizeof(char *));
    9974           0 :       if(nameptr == NULL)
    9975             :       { 
    9976           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    9977           0 :     return(-1);
    9978             :       }
    9979           0 :       namelen = (int32 *) calloc(1, sizeof(int32));
    9980           0 :       if(namelen == NULL)
    9981             :       { 
    9982           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    9983           0 :     free(nameptr);
    9984           0 :     return(-1);
    9985             :       }
    9986           0 :       nameptr0 = (char **) calloc(1, sizeof(char *));
    9987           0 :       if(nameptr0 == NULL)
    9988             :       { 
    9989           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    9990           0 :     free(nameptr);
    9991           0 :     free(namelen);
    9992           0 :     return(-1);
    9993             :       }
    9994           0 :       namelen0 = (int32 *) calloc(1, sizeof(int32));
    9995           0 :       if(namelen0 == NULL)
    9996             :       { 
    9997           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
    9998           0 :     free(nameptr);
    9999           0 :     free(namelen);
   10000           0 :     free(nameptr0);
   10001           0 :     return(-1);
   10002             :       }
   10003           0 :       dimptr = (char **) calloc(1, sizeof(char *));
   10004           0 :       if(dimptr == NULL)
   10005             :       { 
   10006           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10007           0 :     free(nameptr);
   10008           0 :     free(namelen);
   10009           0 :     free(nameptr0);
   10010           0 :     free(namelen0);
   10011           0 :     return(-1);
   10012             :       }
   10013           0 :       dimlen = (int32 *) calloc(1, sizeof(int32));
   10014           0 :       if(dimlen == NULL)
   10015             :       { 
   10016           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10017           0 :     free(nameptr);
   10018           0 :     free(namelen);
   10019           0 :     free(nameptr0);
   10020           0 :     free(namelen0);
   10021           0 :     free(dimptr);
   10022           0 :     return(-1);
   10023             :       }
   10024           0 :       offset = (int32 *) calloc(1, sizeof(int32));
   10025           0 :       if(offset == NULL)
   10026             :       { 
   10027           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10028           0 :     free(nameptr);
   10029           0 :     free(namelen);
   10030           0 :     free(nameptr0);
   10031           0 :     free(namelen0);
   10032           0 :     free(dimptr);
   10033           0 :     free(dimlen);
   10034           0 :     return(-1);
   10035             :       }
   10036           0 :       indvdims = (int32 *) calloc(1, sizeof(int32));
   10037           0 :       if(indvdims == NULL)
   10038             :       { 
   10039           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10040           0 :     free(nameptr);
   10041           0 :     free(namelen);
   10042           0 :     free(nameptr0);
   10043           0 :     free(namelen0);
   10044           0 :     free(dimptr);
   10045           0 :     free(dimlen);
   10046           0 :     free(offset);
   10047           0 :     return(-1);       
   10048             :       }
   10049             :   }
   10050             :   else
   10051             :   {
   10052             :       /*
   10053             :        * "Trim Off" trailing "," and ";" in SWXSDname & SWXSDdims
   10054             :        * respectively
   10055             :        */
   10056           0 :       SWXSDname[strlen(SWXSDname) - 1] = 0;
   10057           0 :       SWXSDdims[strlen(SWXSDdims) - 1] = 0;
   10058             : 
   10059             : 
   10060             :       /* Get number of fields from SWXSDname string */
   10061             :       /* ------------------------------------------ */
   10062           0 :       nflds = EHparsestr(SWXSDname, ',', NULL, NULL);
   10063             : 
   10064             : 
   10065             :       /* Allocate space for various dynamic arrays */
   10066             :       /* ----------------------------------------- */
   10067           0 :       nameptr = (char **) calloc(nflds, sizeof(char *));
   10068           0 :       if(nameptr == NULL)
   10069             :       { 
   10070           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10071           0 :     return(-1);
   10072             :       }
   10073           0 :       namelen = (int32 *) calloc(nflds, sizeof(int32));
   10074           0 :       if(namelen == NULL)
   10075             :       { 
   10076           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10077           0 :     free(nameptr);
   10078           0 :     return(-1);
   10079             :       }
   10080           0 :       nameptr0 = (char **) calloc(nflds, sizeof(char *));
   10081           0 :       if(nameptr0 == NULL)
   10082             :       { 
   10083           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10084           0 :     free(nameptr);
   10085           0 :     free(namelen);
   10086           0 :     return(-1);
   10087             :       }
   10088           0 :       namelen0 = (int32 *) calloc(nflds, sizeof(int32));
   10089           0 :       if(namelen0 == NULL)
   10090             :       { 
   10091           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10092           0 :     free(nameptr);
   10093           0 :     free(namelen);
   10094           0 :     free(nameptr0);
   10095           0 :     return(-1);
   10096             :       }
   10097           0 :       dimptr = (char **) calloc(nflds, sizeof(char *));
   10098           0 :       if(dimptr == NULL)
   10099             :       { 
   10100           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10101           0 :     free(nameptr);
   10102           0 :     free(namelen);
   10103           0 :     free(nameptr0);
   10104           0 :     free(namelen0);
   10105           0 :     return(-1);
   10106             :       }
   10107           0 :       dimlen = (int32 *) calloc(nflds, sizeof(int32));
   10108           0 :       if(dimlen == NULL)
   10109             :       { 
   10110           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10111           0 :     free(nameptr);
   10112           0 :     free(namelen);
   10113           0 :     free(nameptr0);
   10114           0 :     free(namelen0);
   10115           0 :     free(dimptr);
   10116           0 :     return(-1);
   10117             :       }
   10118           0 :       offset = (int32 *) calloc(nflds, sizeof(int32));
   10119           0 :       if(offset == NULL)
   10120             :       { 
   10121           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10122           0 :     free(nameptr);
   10123           0 :     free(namelen);
   10124           0 :     free(nameptr0);
   10125           0 :     free(namelen0);
   10126           0 :     free(dimptr);
   10127           0 :     free(dimlen);
   10128           0 :     return(-1);
   10129             :       }
   10130           0 :       indvdims = (int32 *) calloc(nflds, sizeof(int32));
   10131           0 :       if(indvdims == NULL)
   10132             :       { 
   10133           0 :     HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10134           0 :     free(nameptr);
   10135           0 :     free(namelen);
   10136           0 :     free(nameptr0);
   10137           0 :     free(namelen0);
   10138           0 :     free(dimptr);
   10139           0 :     free(dimlen);
   10140           0 :     free(offset);
   10141           0 :     return(-1);       
   10142             :       }
   10143             : 
   10144             : 
   10145             :       /* Parse SWXSDname and SWXSDdims strings */
   10146             :       /* ------------------------------------- */
   10147           0 :       nflds = EHparsestr(SWXSDname, ',', nameptr, namelen);
   10148           0 :       nflds = EHparsestr(SWXSDdims, ';', dimptr, dimlen);
   10149             :   }
   10150             : 
   10151             : 
   10152             :   /* Loop through all the fields */
   10153             :   /* --------------------------- */
   10154           0 :   for (i = 0; i < nflds; i++)
   10155             :   {
   10156             :       /* If active entry and field is within swath to be detached ... */
   10157             :       /* ------------------------------------------------------------ */
   10158           0 :       if (SWXSDcomb[5 * i] != 0 &&
   10159           0 :     SWXSDcomb[5 * i + 3] == SWXSwath[sID].IDTable)
   10160             :       {
   10161           0 :     nambuf = (char *) calloc(strlen(SWXSDname) + 1, 1);
   10162           0 :     if(nambuf == NULL)
   10163             :     { 
   10164           0 :         HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10165           0 :         return(-1);
   10166             :     }
   10167           0 :     utlbuf = (char *) calloc(2 * strlen(SWXSDname) + 7, 1);
   10168           0 :     if(utlbuf == NULL)
   10169             :     { 
   10170           0 :         HEpush(DFE_NOSPACE,"SWdetach", __FILE__, __LINE__);
   10171           0 :         free(nambuf);
   10172           0 :         return(-1);
   10173             :     }
   10174             :     /* Zero out dimbuf1 */
   10175             :     /* ---------------- */
   10176           0 :     for (k = 0; k < (intn)sizeof(dimbuf1); k++)
   10177             :     {
   10178           0 :         dimbuf1[k] = 0;
   10179             :     }
   10180             : 
   10181             : 
   10182             :     /* Load array to match, name & parse dims */
   10183             :     /* -------------------------------------- */
   10184           0 :     memcpy(match, &SWXSDcomb[5 * i], 20);
   10185           0 :     memcpy(nambuf, nameptr[i], namelen[i]);
   10186             : 
   10187           0 :     memcpy(dimbuf1, dimptr[i], dimlen[i]);
   10188           0 :     dum = EHparsestr(dimbuf1, ',', ptr1, slen1);
   10189             : 
   10190             : 
   10191             :     /* Separate combined (first) dimension from others */
   10192             :     /* ----------------------------------------------- */
   10193           0 :     dimbuf1[slen1[0]] = 0;
   10194             : 
   10195           0 :     offset[0] = 0;
   10196           0 :     indvdims[0] = abs(match[0]);
   10197             : 
   10198             :     /*
   10199             :      * Loop through remaining fields to check for matches with
   10200             :      * current one
   10201             :      */
   10202           0 :     for (j = i + 1, cmbfldcnt = 0; j < nflds; j++)
   10203             :     {
   10204           0 :         if (SWXSDcomb[5 * j] != 0)
   10205             :         {
   10206             :       /* Zero out dimbuf2 */
   10207             :       /* ---------------- */
   10208           0 :       for (k = 0; k < (intn)sizeof(dimbuf2); k++)
   10209             :       {
   10210           0 :           dimbuf2[k] = 0;
   10211             :       }
   10212             : 
   10213             :       /*
   10214             :        * Parse the dimensions and separate first for this
   10215             :        * entry
   10216             :        */
   10217           0 :       memcpy(dimbuf2, dimptr[j], dimlen[j]);
   10218           0 :       dum = EHparsestr(dimbuf2, ',', ptr2, slen2);
   10219           0 :       dimbuf2[slen2[0]] = 0;
   10220             : 
   10221             : 
   10222             :       /*
   10223             :        * If 2nd & 3rd dimension values and names (1st and
   10224             :        * 2nd for rank=2 array), swath ID, and numbertype
   10225             :        * are equal, then these fields can be combined.
   10226             :        */
   10227           0 :       if (match[1] == SWXSDcomb[5 * j + 1] &&
   10228           0 :           match[2] == SWXSDcomb[5 * j + 2] &&
   10229           0 :           match[3] == SWXSDcomb[5 * j + 3] &&
   10230           0 :           match[4] == SWXSDcomb[5 * j + 4] &&
   10231           0 :           strcmp(dimbuf1 + slen1[0] + 1,
   10232           0 :            dimbuf2 + slen2[0] + 1) == 0)
   10233             :       {
   10234             :           /* Add to combined dimension size */
   10235             :           /* ------------------------------ */
   10236           0 :           match[0] += SWXSDcomb[5 * j];
   10237             : 
   10238             :           /* Concatenate name */
   10239             :           /* ---------------- */
   10240           0 :           strcat(nambuf, ",");
   10241           0 :           memcpy(nambuf + strlen(nambuf),
   10242           0 :            nameptr[j], namelen[j]);
   10243             : 
   10244             :           /*
   10245             :            * Increment number of merged fields, store
   10246             :            * individual dims and dim offsets
   10247             :            */
   10248           0 :           cmbfldcnt++;
   10249           0 :           indvdims[cmbfldcnt] = abs(SWXSDcomb[5 * j]);
   10250           0 :           offset[cmbfldcnt] = offset[cmbfldcnt - 1] +
   10251           0 :         indvdims[cmbfldcnt - 1];
   10252             : 
   10253             :           /* Delete this field from combination list */
   10254             :           /* --------------------------------------- */
   10255           0 :           SWXSDcomb[5 * j] = 0;
   10256             :       }
   10257             :         }
   10258             :     }
   10259             : 
   10260             : 
   10261             :     /* Create SDS */
   10262             :     /* ---------- */
   10263             : 
   10264             :     /* Parse names string */
   10265             :     /* ------------------ */
   10266           0 :     nflds0 = EHparsestr(nambuf, ',', nameptr0, namelen0);
   10267             : 
   10268           0 :     if (abs(match[0]) == 1)
   10269             :     {
   10270             :         /* Two Dimensional Array (no merging has occurred) */
   10271             :         /* ---------------------------------------------- */
   10272           0 :         dims[0] = abs(match[1]);
   10273           0 :         dims[1] = abs(match[2]);
   10274             : 
   10275             :         /* Create SDS */
   10276             :         /* ---------- */
   10277           0 :         rank = 2;
   10278           0 :         sdid = SDcreate(sdInterfaceID, nambuf,
   10279           0 :             SWXSDcomb[5 * i + 4], 2, dims);
   10280             :     }
   10281             :     else
   10282             :     {
   10283             :         /* Three Dimensional Array */
   10284             :         /* ----------------------- */
   10285           0 :         dims[0] = abs(match[0]);
   10286           0 :         dims[1] = abs(match[1]);
   10287           0 :         dims[2] = abs(match[2]);
   10288             : 
   10289           0 :         rank = 3;
   10290             : 
   10291             :         /*
   10292             :          * If merged fields then form string consisting of
   10293             :          * "MRGFLD_" + 1st field in merge + ":" + entire merged
   10294             :          * field list and store in utlbuf. Then write to
   10295             :          * MergedField metadata section
   10296             :          */
   10297           0 :         if (cmbfldcnt > 0)
   10298             :         {
   10299           0 :       strcpy(utlbuf, "MRGFLD_");
   10300           0 :       memcpy(utlbuf + 7, nameptr0[0], namelen0[0]);
   10301           0 :       utlbuf[7 + namelen0[0]] = 0;
   10302           0 :       strcat(utlbuf, ":");
   10303           0 :       strcat(utlbuf, nambuf);
   10304             : 
   10305           0 :       status = EHinsertmeta(sdInterfaceID, swathname, "s",
   10306             :                 6L, utlbuf, NULL);
   10307             :         }
   10308             :         else
   10309             :         {
   10310             :       /*
   10311             :        * If not merged field then store field name in
   10312             :        * utlbuf
   10313             :        */
   10314           0 :       strcpy(utlbuf, nambuf);
   10315             :         }
   10316             : 
   10317             :         /* Create SDS */
   10318             :         /* ---------- */
   10319           0 :         sdid = SDcreate(sdInterfaceID, utlbuf,
   10320           0 :             SWXSDcomb[5 * i + 4], 3, dims);
   10321             : 
   10322             : 
   10323             :         /*
   10324             :          * If merged field then store dimensions and offsets as
   10325             :          * SD attributes
   10326             :          */
   10327           0 :         if (cmbfldcnt > 0)
   10328             :         {
   10329           0 :       SDsetattr(sdid, "Field Dims", DFNT_INT32,
   10330             :           cmbfldcnt + 1, (VOIDP) indvdims);
   10331             : 
   10332           0 :       SDsetattr(sdid, "Field Offsets", DFNT_INT32,
   10333             :           cmbfldcnt + 1, (VOIDP) offset);
   10334             :         }
   10335             :     }
   10336             : 
   10337             : 
   10338             : 
   10339             :     /* Register Dimensions in SDS */
   10340             :     /* -------------------------- */
   10341           0 :     for (k = 0; k < rank; k++)
   10342             :     {
   10343           0 :         if (rank == 2)
   10344             :         {
   10345             :       /* Copy k+1th dimension into dimbuf2 if rank = 2 */
   10346             :       /* --------------------------------------------- */
   10347           0 :       memcpy(dimbuf2, ptr1[k + 1], slen1[k + 1]);
   10348           0 :       dimbuf2[slen1[k + 1]] = 0;
   10349             :         }
   10350             :         else
   10351             :         {
   10352             :       /* Copy kth dimension into dimbuf2 if rank > 2 */
   10353             :       /* ------------------------------------------- */
   10354           0 :       memcpy(dimbuf2, ptr1[k], slen1[k]);
   10355           0 :       dimbuf2[slen1[k]] = 0;
   10356             :         }
   10357             : 
   10358             :         /*
   10359             :          * If first dimension and merged field then generate
   10360             :          * dimension name consisting of "MRGDIM:" + swathname +
   10361             :          * dimension size
   10362             :          */
   10363           0 :         if (k == 0 && cmbfldcnt > 0)
   10364             :         {
   10365           0 :       snprintf(dimbuf2, sizeof(dimbuf2), "%s%s_%ld", "MRGDIM:",
   10366           0 :         swathname, (long)dims[0]);
   10367             :         }
   10368             :         else
   10369             :         {
   10370             :       /* Otherwise concatenate swathname to dim name */
   10371             :       /* ------------------------------------------- */
   10372           0 :       strcat(dimbuf2, ":");
   10373           0 :       strcat(dimbuf2, swathname);
   10374             :         }
   10375             : 
   10376             :         /* Register dimensions using "SDsetdimname" */
   10377             :         /* ---------------------------------------- */
   10378           0 :         SDsetdimname(SDgetdimid(sdid, k), (char *) dimbuf2);
   10379             :     }
   10380             : 
   10381             : 
   10382             : 
   10383             :     /* Write Fill Value */
   10384             :     /* ---------------- */
   10385           0 :     for (k = 0; k < nflds0; k++)
   10386             :     {
   10387             :         /* Check if fill values has been set */
   10388             :         /* --------------------------------- */
   10389           0 :         memcpy(utlbuf, nameptr0[k], namelen0[k]);
   10390           0 :         utlbuf[namelen[k]] = 0;
   10391           0 :         statusFill = SWgetfillvalue(swathID, utlbuf, fillval);
   10392             : 
   10393           0 :         if (statusFill == 0)
   10394             :         {
   10395             :       /*
   10396             :        * If merged field then fill value must be stored
   10397             :        * manually using EHfillfld
   10398             :        */
   10399           0 :       if (cmbfldcnt > 0)
   10400             :       {
   10401           0 :           dims[0] = indvdims[k];
   10402           0 :           truerank = (dims[0] == 1) ? 2 : 3;
   10403           0 :           EHfillfld(sdid, rank, truerank,
   10404           0 :               DFKNTsize(match[4]), offset[k],
   10405             :               dims, fillval);
   10406             :       }
   10407             :       /*
   10408             :        * If single field then just use the HDF set fill
   10409             :        * function
   10410             :        */
   10411             :       else
   10412             :       {
   10413           0 :           status = SDsetfillvalue(sdid, fillval);
   10414             :       }
   10415             :         }
   10416             :     }
   10417             : 
   10418             : 
   10419             :     /*
   10420             :      * Insert SDS within the appropriate Vgroup (geo or data) and
   10421             :      * "detach" newly-created SDS
   10422             :      */
   10423           0 :     vgid = (match[0] < 0)
   10424             :         ? SWXSwath[sID].VIDTable[0]
   10425           0 :         : SWXSwath[sID].VIDTable[1];
   10426             : 
   10427           0 :     Vaddtagref(vgid, DFTAG_NDG, SDidtoref(sdid));
   10428           0 :     SDendaccess(sdid);
   10429             : 
   10430           0 :     free(nambuf);
   10431           0 :     free(utlbuf);
   10432             :       }
   10433             :   }
   10434             : 
   10435             : 
   10436             : 
   10437             :   /* "Contract" 1dcomb array */
   10438             :   /* ----------------------- */
   10439           0 :   i = 0;
   10440           0 :   while (SWX1dcomb[3 * i] != 0)
   10441             :   {
   10442           0 :       if (SWX1dcomb[3 * i + 1] == SWXSwath[sID].IDTable)
   10443             :       {
   10444           0 :     memcpy(&SWX1dcomb[3 * i],
   10445           0 :            &SWX1dcomb[3 * (i + 1)],
   10446           0 :            (512 - i - 1) * 3 * 4);
   10447             :       }
   10448             :       else
   10449           0 :     i++;
   10450             :   }
   10451             : 
   10452             : 
   10453             :   /* "Contract" SDcomb array */
   10454             :   /* ----------------------- */
   10455           0 :   for (i = 0; i < nflds; i++)
   10456             :   {
   10457           0 :       if (SWXSDcomb[5 * i + 3] == SWXSwath[sID].IDTable)
   10458             :       {
   10459           0 :     if (i == (nflds - 1))
   10460             :     {
   10461           0 :         SWXSDcomb[5 * i] = 0;
   10462           0 :         *(nameptr[i] - (nflds != 1)) = 0;
   10463           0 :         *(dimptr[i] - (nflds != 1)) = 0;
   10464             :     }
   10465             :     else
   10466             :     {
   10467           0 :         memmove(&SWXSDcomb[5 * i],
   10468           0 :          &SWXSDcomb[5 * (i + 1)],
   10469           0 :          (512 - i - 1) * 5 * 4);
   10470             : 
   10471           0 :         memmove(nameptr[i],
   10472           0 :          nameptr[i + 1],
   10473           0 :          nameptr[0] + 2048 - nameptr[i + 1] - 1);
   10474             : 
   10475           0 :         memmove(dimptr[i],
   10476           0 :          dimptr[i + 1],
   10477           0 :          dimptr[0] + 2048 * 2 - dimptr[i + 1] - 1);
   10478             :     }
   10479             : 
   10480           0 :     i--;
   10481           0 :     nflds = EHparsestr(SWXSDname, ',', nameptr, namelen);
   10482           0 :     nflds = EHparsestr(SWXSDdims, ';', dimptr, dimlen);
   10483             :       }
   10484             :   }
   10485             : 
   10486             : 
   10487             :   /* Replace trailing delimiters on SWXSDname & SWXSDdims */
   10488             :   /* ---------------------------------------------------- */
   10489           0 :   if (nflds != 0)
   10490             :   {
   10491           0 :       strcat(SWXSDname, ",");
   10492           0 :       strcat(SWXSDdims, ";");
   10493             :   }
   10494             : 
   10495             : 
   10496             : 
   10497             :   /* Free up a bunch of dynamically allocated arrays */
   10498             :   /* ----------------------------------------------- */
   10499           0 :   free(nameptr);
   10500           0 :   free(namelen);
   10501           0 :   free(nameptr0);
   10502           0 :   free(namelen0);
   10503           0 :   free(dimptr);
   10504           0 :   free(dimlen);
   10505           0 :   free(offset);
   10506           0 :   free(indvdims);
   10507             : 
   10508             : 
   10509             : 
   10510             : 
   10511             :   /* "Detach" from previously attached SDSs */
   10512             :   /* -------------------------------------- */
   10513           0 :   for (k = 0; k < SWXSwath[sID].nSDS; k++)
   10514             :   {
   10515           0 :       SDendaccess(SWXSwath[sID].sdsID[k]);
   10516             :   }
   10517           0 :   free(SWXSwath[sID].sdsID);
   10518           0 :   SWXSwath[sID].sdsID = 0;
   10519           0 :   SWXSwath[sID].nSDS = 0;
   10520             : 
   10521             : 
   10522             :   /* Detach Swath Vgroups */
   10523             :   /* -------------------- */
   10524           0 :   Vdetach(SWXSwath[sID].VIDTable[0]);
   10525           0 :   Vdetach(SWXSwath[sID].VIDTable[1]);
   10526           0 :   Vdetach(SWXSwath[sID].VIDTable[2]);
   10527           0 :   Vdetach(SWXSwath[sID].IDTable);
   10528             : 
   10529             : 
   10530             :   /* Delete entries from External Arrays */
   10531             :   /* ----------------------------------- */
   10532           0 :   SWXSwath[sID].active = 0;
   10533           0 :   SWXSwath[sID].VIDTable[0] = 0;
   10534           0 :   SWXSwath[sID].VIDTable[1] = 0;
   10535           0 :   SWXSwath[sID].VIDTable[2] = 0;
   10536           0 :   SWXSwath[sID].IDTable = 0;
   10537           0 :   SWXSwath[sID].fid = 0;
   10538             : 
   10539             : 
   10540             :   /* Free Region Pointers */
   10541             :   /* -------------------- */
   10542           0 :   for (k = 0; k < NSWATHREGN; k++)
   10543             :   {
   10544           0 :       if (SWXRegion[k] != 0 &&
   10545           0 :     SWXRegion[k]->swathID == swathID)
   10546             :       {
   10547           0 :     for (i = 0; i < 8; i++)
   10548             :     {
   10549           0 :         if (SWXRegion[k]->DimNamePtr[i] != 0)
   10550             :         {
   10551           0 :       free(SWXRegion[k]->DimNamePtr[i]);
   10552             :         }
   10553             :     }
   10554             : 
   10555           0 :     free(SWXRegion[k]);
   10556           0 :     SWXRegion[k] = 0;
   10557             :       }
   10558             :   }
   10559             : 
   10560             :     }
   10561           0 :     return (status);
   10562             : }
   10563             : 
   10564             : /*----------------------------------------------------------------------------|
   10565             : |  BEGIN_PROLOG                                                               |
   10566             : |                                                                             |
   10567             : |  FUNCTION: SWclose                                                          |
   10568             : |                                                                             |
   10569             : |  DESCRIPTION: Closes HDF-EOS file                                           |
   10570             : |                                                                             |
   10571             : |                                                                             |
   10572             : |  Return Value    Type     Units     Description                             |
   10573             : |  ============   ======  =========   =====================================   |
   10574             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
   10575             : |                                                                             |
   10576             : |  INPUTS:                                                                    |
   10577             : |  fid            int32               File ID                                 |
   10578             : |                                                                             |
   10579             : |  OUTPUTS:                                                                   |
   10580             : |             None                                                            |
   10581             : |                                                                             |
   10582             : |  NOTES:                                                                     |
   10583             : |                                                                             |
   10584             : |                                                                             |
   10585             : |   Date     Programmer   Description                                         |
   10586             : |  ======   ============  =================================================   |
   10587             : |  Jun 96   Joel Gales    Original Programmer                                 |
   10588             : |                                                                             |
   10589             : |  END_PROLOG                                                                 |
   10590             : -----------------------------------------------------------------------------*/
   10591             : intn
   10592           4 : SWclose(int32 fid)
   10593             : 
   10594             : {
   10595           4 :     intn            status = 0; /* routine return status variable */
   10596             : 
   10597             :     /* Call EHclose to perform file close */
   10598             :     /* ---------------------------------- */
   10599           4 :     status = EHclose(fid);
   10600             : 
   10601           4 :     return (status);
   10602             : }
   10603             : 
   10604             : /*----------------------------------------------------------------------------|
   10605             : |  BEGIN_PROLOG                                                               |
   10606             : |                                                                             |
   10607             : |  FUNCTION: SWupdatescene                                                    |
   10608             : |                                                                             |
   10609             : |  DESCRIPTION: Updates the StartRegion and StopRegion values                 |
   10610             : |               for a specified region.                                       |
   10611             : |                                                                             |
   10612             : |                                                                             |
   10613             : |  Return Value    Type     Units     Description                             |
   10614             : |  ============   ======  =========   =====================================   |
   10615             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
   10616             : |                                                                             |
   10617             : |  INPUTS:                                                                    |
   10618             : |  swathID        int32               Swath structure ID                      |
   10619             : |  regionID       int32               Region ID                               |
   10620             : |                                                                             |
   10621             : |  NOTES:                                                                     |
   10622             : |                                                                             |
   10623             : |   Date     Programmer   Description                                         |
   10624             : |  ======   ============  =================================================   |
   10625             : |  Nov 98   Xinmin Hua    Original developing                                 |
   10626             : |  Aug 99   Abe Taaheri   Added code to exclude regions that have the same    |
   10627             : |                         start and stop.                                     |
   10628             : |                                                                             |
   10629             : |  END_PROLOG                                                                 |
   10630             : -----------------------------------------------------------------------------*/
   10631             : intn
   10632           0 : SWupdatescene(int32 swathID, int32 regionID)
   10633             : {
   10634             :     intn            k;          /* Loop index */
   10635             :     int32           status;     /* routine return status variable */
   10636             :  
   10637             :     int32           fid;        /* HDF-EOS file ID */
   10638             :     int32           sdInterfaceID;      /* HDF SDS interface ID */
   10639             :     int32           swVgrpID;   /* Swath Vgroup ID */
   10640             :  
   10641             :     int32           startReg;   /* Indexed start region */
   10642             :     int32           stopReg;    /* Indexed stop region */
   10643             :     int32           l_index[MAXNREGIONS]; /* to store indices when stop and 
   10644             :              start are different */
   10645             :              
   10646             :     int32           ind;        /* l_index */
   10647             :     int32           tempnRegions; /* temp number of regions */
   10648             : 
   10649           0 :     memset(l_index, 0, sizeof(int32) * MAXNREGIONS);
   10650             : 
   10651             :     /* Check for valid swath ID */
   10652             :     /* ------------------------ */
   10653           0 :     status = SWchkswid(swathID, "SWupdatescene", &fid, &sdInterfaceID,
   10654             :                        &swVgrpID);
   10655             :  
   10656             :  
   10657             :     /* Check for valid region ID */
   10658             :     /* ------------------------- */
   10659           0 :     if (status == 0)
   10660             :     {
   10661           0 :         if (regionID < 0 || regionID >= NSWATHREGN)
   10662             :         {
   10663           0 :             status = -1;
   10664           0 :             HEpush(DFE_RANGE, "SWupdatescene", __FILE__, __LINE__);
   10665           0 :             HEreport("Invalid Region id: %d.\n", regionID);
   10666             :         }
   10667             :     }
   10668             :  
   10669             :     /* Check for active region ID */
   10670             :     /* -------------------------- */
   10671           0 :     if (status == 0)
   10672             :     {
   10673           0 :         if (SWXRegion[regionID] == 0)
   10674             :         {
   10675           0 :             status = -1;
   10676           0 :             HEpush(DFE_GENAPP, "SWupdatescene", __FILE__, __LINE__);
   10677           0 :             HEreport("Inactive Region ID: %d.\n", regionID);
   10678             :         }
   10679             :     }
   10680             :   
   10681           0 :     if (status == 0)
   10682             :     {
   10683           0 :   tempnRegions = SWXRegion[regionID]->nRegions;
   10684           0 :   ind =0;
   10685             :   
   10686           0 :   for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
   10687             :         {
   10688           0 :            startReg = SWXRegion[regionID]->StartRegion[k];
   10689           0 :            stopReg = SWXRegion[regionID]->StopRegion[k];
   10690           0 :      if(startReg == stopReg)
   10691             :      {
   10692             :          /* reduce number of regions by 1, if tempnRegions is 0 issue
   10693             :       error and break from loop*/
   10694           0 :          tempnRegions -= 1;
   10695             :          
   10696           0 :          if(tempnRegions == 0)
   10697             :          {
   10698             :        /* first free allocated memory for SWXRegion[regionID] 
   10699             :           in the function SWdefboxregion and make regionID
   10700             :           inactive */
   10701           0 :        free(SWXRegion[regionID]);
   10702           0 :        SWXRegion[regionID] = 0;
   10703           0 :        status = -1;
   10704           0 :        HEpush(DFE_GENAPP, "SWupdatescene", __FILE__, __LINE__);
   10705           0 :        HEreport("Inactive Region ID: %d.\n", regionID);
   10706           0 :        break;
   10707             :          }
   10708             :      }
   10709             :      else
   10710             :      {
   10711             :          /* store l_index number of regions that have different start and
   10712             :       stop */
   10713           0 :          l_index[ind] = k;
   10714           0 :          ind += 1;
   10715             :      }
   10716             :   }
   10717           0 :   if (status != 0)
   10718             :   {
   10719           0 :       return (status);
   10720             :   }
   10721             :   else
   10722             :   {
   10723           0 :       SWXRegion[regionID]->nRegions = tempnRegions;
   10724             :   }
   10725             :   /* keep starts and stops that are different in the structure  */   
   10726           0 :   for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
   10727             :   {
   10728           0 :       SWXRegion[regionID]->StartRegion[k] =
   10729           0 :         SWXRegion[regionID]->StartRegion[l_index[k]];
   10730           0 :       SWXRegion[regionID]->StopRegion[k] =
   10731           0 :         SWXRegion[regionID]->StopRegion[l_index[k]];
   10732             :   }
   10733             :   
   10734             :     }
   10735             : 
   10736             :     
   10737           0 :     if (status == 0)
   10738             :     {
   10739             :  
   10740           0 :         for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
   10741             :         {
   10742             :  
   10743           0 :            startReg = SWXRegion[regionID]->StartRegion[k];
   10744           0 :            stopReg = SWXRegion[regionID]->StopRegion[k];
   10745             :  
   10746           0 :            if(startReg % 2 == 1) {
   10747             :  
   10748           0 :               SWXRegion[regionID]->StartRegion[k] = ++startReg;
   10749             :  
   10750             :            }
   10751           0 :            if(stopReg % 2 == 0) {
   10752             :  
   10753           0 :               SWXRegion[regionID]->StopRegion[k] = --stopReg;
   10754             :  
   10755             :            }
   10756             :  
   10757             :         }
   10758             :  
   10759             :     }
   10760             :     
   10761           0 :     return(status);
   10762             :  
   10763             : }
   10764             : 
   10765             : /*----------------------------------------------------------------------------|
   10766             : |  BEGIN_PROLOG                                                               |
   10767             : |                                                                             |
   10768             : |  FUNCTION: SWupdateidxmap                                                   |
   10769             : |                                                                             |
   10770             : |  DESCRIPTION: Updates the map l_index for a specified region.                 |
   10771             : |                                                                             |
   10772             : |                                                                             |
   10773             : |  Return Value    Type     Units     Description                             |
   10774             : |  ============   ======  =========   =====================================   |
   10775             : |  nout           int32               return Number of elements in output     |
   10776             : |                                     l_index array if SUCCEED, (-1) FAIL       |
   10777             : |                                                                             |
   10778             : |  INPUTS:                                                                    |
   10779             : |  swathID        int32               Swath structure ID                      |
   10780             : |  regionID       int32               Region ID                               |
   10781             : |  l_indexin        int32               array of l_index values                   |
   10782             : |                                                                             |
   10783             : |  OUTPUTS:                                                                   |
   10784             : |  l_indexout       int32               array of l_index values                   |
   10785             : |  indices    int32         array of start and stop in region       |
   10786             : |                                                                             |
   10787             : |  NOTES:                                                                     |
   10788             : |                                                                             |
   10789             : |                                                                             |
   10790             : |   Date     Programmer   Description                                         |
   10791             : |  ======   ============  =================================================   |
   10792             : |  Aug 97   Abe Taaheri   Original Programmer                                 |
   10793             : |  AUG 97   Abe Taaheri   Add support for l_index mapping                       |
   10794             : |  Sep 99   DaW     Add support for Floating Scene Subsetting Landsat 7 |
   10795             : |                                                                             |
   10796             : |  END_PROLOG                                                                 |
   10797             : -----------------------------------------------------------------------------*/
   10798             : int32
   10799           0 : SWupdateidxmap(int32 swathID, int32 regionID, int32 l_indexin[], int32 l_indexout[], int32 indicies[])
   10800             : {
   10801             :     intn            i;          /* Loop index */
   10802             :     intn            j;          /* Loop index */
   10803             :     intn            k;          /* Loop index */
   10804             :     int32           status;     /* routine return status variable */
   10805           0 :     int32           land_status = 3;     /* routine return status variable */
   10806             :  
   10807             :     int32           fid;        /* HDF-EOS file ID */
   10808             :     int32           sdInterfaceID;      /* HDF SDS interface ID */
   10809             :     int32           swVgrpID;   /* Swath Vgroup ID */
   10810             : 
   10811           0 :     int32           numtype = 0; /* Used for L7 float scene sub. */
   10812           0 :     int32           count = 0;   /* Used for L7 float scene sub. */
   10813             :  
   10814           0 :     int32           startReg = 0;   /* Indexed start region */
   10815           0 :     int32           stopReg = 0;    /* Indexed stop region */
   10816           0 :     int32           nout=-1;       /* Number of elements in output l_index array */
   10817           0 :     int32     indexoffset = 0;
   10818           0 :     uint8     scene_cnt = 0;  /* Used for L7 float scene sub.      */
   10819           0 :     uint8           detect_cnt = 0;     /* Used to convert scan to scanline  */
   10820           0 :     intn      gtflag = 0;
   10821           0 :     intn      ngtflag = 0;
   10822           0 :     int32           *buffer1 = (int32 *)NULL;
   10823           0 :     int32           *buffer2 = (int32 *)NULL;
   10824             :  
   10825             :     /* Check for valid swath ID */
   10826             :     /* ------------------------ */
   10827           0 :     status = SWchkswid(swathID, "SWupdateidxmap", &fid, &sdInterfaceID,
   10828             :                        &swVgrpID);
   10829             :  
   10830             :  
   10831             :     /* Check for valid region ID */
   10832             :     /* ------------------------- */
   10833           0 :     if (status == 0)
   10834             :     {
   10835           0 :         if (regionID < 0 || regionID >= NSWATHREGN)
   10836             :         {
   10837           0 :             status = -1;
   10838           0 :             HEpush(DFE_RANGE, "SWupdateidxmap", __FILE__, __LINE__);
   10839           0 :             HEreport("Invalid Region id: %d.\n", regionID);
   10840             :         }
   10841             :     }
   10842             :  
   10843             :     /* Check for active region ID */
   10844             :     /* -------------------------- */
   10845           0 :     if (status == 0)
   10846             :     {
   10847           0 :         if (SWXRegion[regionID] == 0)
   10848             :         {
   10849           0 :             status = -1;
   10850           0 :             HEpush(DFE_GENAPP, "SWextractregion", __FILE__, __LINE__);
   10851           0 :             HEreport("Inactive Region ID: %d.\n", regionID);
   10852             :         }
   10853             :     }
   10854             :  
   10855           0 :     if (status == 0)
   10856             :     {
   10857             :   /* Loop through all regions */
   10858             :   /* ------------------------ */
   10859           0 :   for (k = 0; k < SWXRegion[regionID]->nRegions; k++)
   10860             :   {
   10861             :       
   10862             :       /* fix overlap l_index mapping problem for Landsat 7 */
   10863             :       
   10864           0 :       startReg = SWXRegion[regionID]->StartRegion[k];
   10865           0 :       stopReg = SWXRegion[regionID]->StopRegion[k];
   10866             :       
   10867             :       
   10868           0 :             if(SWXRegion[regionID]->scanflag == 1)
   10869             :             {
   10870           0 :                indicies[0] = -1;
   10871           0 :                indicies[1] = -1;
   10872           0 :                j = 0;
   10873             :             /* This code checks for the attribute detector_count */
   10874             :             /* which is found in Landsat 7 files.  It is used    */
   10875             :             /* for some of the loops.                            */
   10876             :             /* ================================================= */
   10877           0 :                land_status = SWattrinfo(swathID, "scene_count", &numtype, &count);
   10878           0 :                if (land_status == 0)
   10879             :                {
   10880           0 :                   land_status = SWreadattr(swathID, "scene_count", &scene_cnt);
   10881           0 :                   land_status = SWreadattr(swathID, "detector_count", &detect_cnt);
   10882             :                }
   10883             : 
   10884             :         
   10885             :         /* calculate the offsets first */
   10886           0 :         buffer1 = (int32 *)calloc(74, sizeof(int32));
   10887           0 :         buffer2 = (int32 *)calloc(74, sizeof(int32));
   10888             :         
   10889           0 :         status = SWidxmapinfo(swathID,"GeoTrack",
   10890             :             "ScanLineTrack", (int32*)buffer1);
   10891           0 :         status = SWidxmapinfo(swathID,"UpperTrack",
   10892             :             "ScanLineTrack", (int32*)buffer2);
   10893             :         
   10894           0 :         indexoffset = buffer2[0] - buffer1[0];
   10895           0 :         free(buffer1);
   10896           0 :         free(buffer2);
   10897             : 
   10898           0 :                if(SWXRegion[regionID]->band8flag == -1)
   10899             :                {
   10900           0 :                   for(i=0; i<scene_cnt;i++)
   10901             :                   {
   10902           0 :                      if(l_indexin[j] <= startReg && l_indexin[j+1] >= startReg)
   10903           0 :                         if(indicies[0] == -1)
   10904           0 :                            indicies[0] = j;
   10905           0 :                      if(l_indexin[j] <= stopReg && l_indexin[j+1] >= stopReg)
   10906           0 :                         indicies[1] = j + 1;
   10907           0 :                      j = j + 2;
   10908           0 :                      if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   10909           0 :                         i = scene_cnt;
   10910             :                   }
   10911           0 :                   if(indicies[0] == -1)
   10912             :                   {
   10913           0 :                      if(startReg <= l_indexin[0])
   10914           0 :                         indicies[0] = 0;
   10915             :                   }
   10916           0 :                   if(indicies[0] == -1)
   10917             :                   {
   10918           0 :                      j = 0;
   10919           0 :                      for(i=0; i<scene_cnt; i++)
   10920             :                      {
   10921           0 :                         if(l_indexin[j] <= startReg && l_indexin[j+1] >= startReg)
   10922           0 :                            if(indicies[0] == -1)
   10923           0 :                               indicies[0] = j;
   10924           0 :                         j = j + 1;
   10925           0 :                         if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   10926           0 :                            i = scene_cnt;
   10927             :                      }
   10928             :                   }
   10929           0 :                   if(indicies[1] == -1)
   10930             :                   {
   10931           0 :                      j = 0;
   10932           0 :                      for(i=0; i<scene_cnt; i++)
   10933             :                      {
   10934           0 :                         if(l_indexin[j] <= stopReg && l_indexin[j+1] >= stopReg)
   10935           0 :                            if(indicies[1] == -1)
   10936           0 :                               indicies[1] = j + 1;
   10937           0 :                         j = j + 1;
   10938           0 :                         if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   10939           0 :                            i = scene_cnt;
   10940             :                      }
   10941             :                   }
   10942           0 :                   if(indicies[1] == -1)
   10943           0 :                      if(stopReg > l_indexin[scene_cnt - 1])
   10944           0 :                         indicies[1] = scene_cnt - 1;
   10945             :                }
   10946             : 
   10947             :          /* This section of code handles exceptions in Landsat 7  */
   10948             :          /* data.  The Band 8 data - multiple files, data gaps  */
   10949             :                /* ===================================================== */
   10950           0 :                if(SWXRegion[regionID]->band8flag == 1 ||
   10951           0 :                   SWXRegion[regionID]->band8flag == 2 ||
   10952           0 :                   SWXRegion[regionID]->band8flag == 3)
   10953             :                {
   10954           0 :                   j = 0;
   10955           0 :                   for(i=0; i<scene_cnt; i++)
   10956             :                   {
   10957           0 :                      j = j + 2;
   10958           0 :                      if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   10959             :                      {
   10960           0 :                         if(l_indexin[j] == 0)
   10961           0 :                            gtflag = 1;
   10962             :                         else
   10963           0 :                            ngtflag = 1;
   10964           0 :                         i = scene_cnt;
   10965             :                      }
   10966             :                   }
   10967           0 :                   j = 0;
   10968           0 :                   if(gtflag == 1)
   10969             :                   {
   10970           0 :                      for(i=0; i<scene_cnt; i++)
   10971             :                      {
   10972           0 :           if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && 
   10973           0 :         startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) )
   10974           0 :                            if(indicies[0] == -1)
   10975           0 :                               indicies[0] = j;
   10976           0 :           if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && 
   10977           0 :         stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) )
   10978           0 :                            indicies[1] = j + 1;
   10979           0 :                         j = j + 2;
   10980           0 :                         if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   10981           0 :                            i = scene_cnt;
   10982             :                      }
   10983           0 :                      if(SWXRegion[regionID]->band8flag == 1)
   10984             :                      {
   10985           0 :                         if(indicies[1] == -1)
   10986           0 :                            if(stopReg > (l_indexin[j - 1] + indexoffset - detect_cnt))
   10987           0 :                               indicies[1] = j - 1;
   10988             :                      }
   10989           0 :                      if(SWXRegion[regionID]->band8flag == 2 ||
   10990           0 :                         SWXRegion[regionID]->band8flag == 3)
   10991             :                      {
   10992             :            
   10993           0 :                         if(startReg >= (l_indexin[j - 1] + indexoffset - detect_cnt))
   10994             :                         {
   10995           0 :                            indicies[0] = -1;
   10996           0 :                            indicies[1] = -1;
   10997             :                            /* status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
   10998             :                            buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
   10999             :                            status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
   11000             :                            indexoffset = buffer[0] * detect_cnt;
   11001             :                            free(buffer);
   11002             :                            startReg = startReg - (indexoffset - detect_cnt);
   11003             :                            stopReg = stopReg - (indexoffset - 1); */
   11004           0 :                            j = 0;
   11005           0 :                            for(i=0; i<scene_cnt; i++)
   11006             :                            {
   11007           0 :            if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && 
   11008           0 :          startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) )
   11009           0 :              if(indicies[0] == -1)
   11010           0 :          indicies[0] = j;
   11011           0 :            if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && 
   11012           0 :          stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) )
   11013           0 :              indicies[1] = j + 1;
   11014           0 :                               j = j + 2;
   11015           0 :                               if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11016           0 :                                  i = scene_cnt;
   11017             :                            }
   11018             :                         }
   11019             : 
   11020           0 :                         if(indicies[0] == -1)
   11021             :                         {
   11022           0 :                            j = 0;
   11023           0 :                            for(i=0; i<scene_cnt; i++)
   11024             :                            {
   11025           0 :            if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && 
   11026           0 :          startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) )
   11027           0 :              if(indicies[0] == -1)
   11028           0 :          indicies[0] = j;
   11029             :           
   11030           0 :            j = j + 2;
   11031           0 :            if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11032           0 :              i = scene_cnt;
   11033             :                            }
   11034             :                         }
   11035           0 :                         if(indicies[1] == -1)
   11036           0 :                            if(stopReg > (l_indexin[j - 1] + indexoffset - detect_cnt) )
   11037           0 :                               indicies[1] = j - 1;
   11038             :                      }
   11039           0 :                      if(indicies[1] == -1)
   11040             :                      {
   11041           0 :                         j = 0;
   11042           0 :                         for(i=0; i<scene_cnt; i++)
   11043             :                         {
   11044           0 :         if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && 
   11045           0 :             stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) )
   11046           0 :           indicies[1] = j;
   11047           0 :         j = j + 2;
   11048           0 :         if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11049           0 :           i = scene_cnt;
   11050             :                         }
   11051             :                      }
   11052             :                   }
   11053             : 
   11054           0 :                   if(ngtflag == 1)
   11055             :                   {
   11056           0 :                      for(i=0; i<scene_cnt; i++)
   11057             :                      {
   11058           0 :           if( startReg >= l_indexin[j] && startReg <= l_indexin[j+1])
   11059           0 :                            if(indicies[0] == -1)
   11060           0 :                               indicies[0] = j;
   11061           0 :           if( stopReg >= l_indexin[j] && stopReg <= l_indexin[j+1])
   11062           0 :                            indicies[1] = j + 1;
   11063           0 :                         j = j + 2;
   11064           0 :                         if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11065           0 :                            i = scene_cnt;
   11066             :                      }
   11067           0 :                      if(SWXRegion[regionID]->band8flag == 2)
   11068             :                      {
   11069           0 :                         if(startReg >= l_indexin[j] )
   11070             :                         {
   11071           0 :                            if(indicies[0] == -1)
   11072           0 :                               indicies[0] = j;
   11073           0 :                            if(indicies[1] == -1)
   11074           0 :                               indicies[1] = j;
   11075             :                         }
   11076           0 :                         if(indicies[0] == -1)
   11077           0 :                            if(startReg <= l_indexin[0])
   11078           0 :                               indicies[0] = 0;
   11079           0 :                         if(indicies[1] == -1)
   11080           0 :                            if(stopReg > l_indexin[j])
   11081           0 :                               indicies[1] = j;
   11082             :                      }
   11083           0 :                      if(indicies[0] == -1)
   11084             :                      {
   11085           0 :                         j = 0;
   11086           0 :                         for(i=0; i<scene_cnt; i++)
   11087             :                         {
   11088           0 :         if( startReg >= l_indexin[j] && startReg <= l_indexin[j+1])
   11089           0 :           indicies[0] = j;
   11090           0 :                            j = j + 2;
   11091           0 :                            if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11092           0 :                               i = scene_cnt;
   11093             :                         }
   11094             :                      }
   11095           0 :                      if(indicies[1] == -1)
   11096             :                      {
   11097           0 :                         j = 0;
   11098           0 :                         for(i=0; i<scene_cnt; i++)
   11099             :                         {
   11100           0 :           if( stopReg >= l_indexin[j] && stopReg <= l_indexin[j+1])
   11101           0 :                               indicies[1] = j;
   11102           0 :                            j = j + 2;
   11103           0 :                            if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11104           0 :                               i = scene_cnt;
   11105             :                         }
   11106             :                      }
   11107           0 :                      if(indicies[1] == -1)
   11108             :                      {
   11109           0 :                         if(stopReg > l_indexin[j])
   11110           0 :                            indicies[1] = j;
   11111             :                      }
   11112             :                   }
   11113           0 :                   if(indicies[0] == -1)
   11114             :                   {
   11115           0 :                      if(startReg <= (l_indexin[0]+ indexoffset - detect_cnt) )
   11116           0 :                         indicies[0] = 0;
   11117           0 :                      if(indicies[1] == -1)
   11118           0 :                         if(stopReg > (l_indexin[j] + indexoffset - detect_cnt))
   11119           0 :                            indicies[1] = j;
   11120             :                   }
   11121             :                }
   11122           0 :                if (indicies[1] == -1)
   11123             :                {
   11124           0 :                   if(SWXRegion[regionID]->band8flag == 2 ||
   11125           0 :                      SWXRegion[regionID]->band8flag == 3)
   11126             :                   {
   11127           0 :                      if(stopReg < (l_indexin[0] + indexoffset - detect_cnt))
   11128             :                      {
   11129             :            /*status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
   11130             :                         buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
   11131             :                         status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
   11132             :                         indexoffset = buffer[0] * detect_cnt;
   11133             :                         free(buffer);
   11134             :                         startReg = startReg + (indexoffset - detect_cnt);
   11135             :                         stopReg = stopReg + (indexoffset - 1); */
   11136           0 :                         if(stopReg >= (l_indexin[scene_cnt - 1] + indexoffset - detect_cnt))
   11137             :                         {
   11138           0 :                            indicies[1] = scene_cnt - 1;
   11139             :                         }
   11140             :                         else
   11141             :                         {
   11142           0 :                            j = 0;
   11143           0 :                            for(i=0;i<scene_cnt;i++)
   11144             :                            {
   11145           0 :           if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && 
   11146           0 :         stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) )
   11147           0 :                indicies[1] = j;
   11148           0 :             j = j + 2;
   11149           0 :                               if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11150           0 :                                  i = scene_cnt;
   11151             :                            }
   11152             :                         } 
   11153             :                      }
   11154             : 
   11155           0 :                      if(startReg > (l_indexin[j - 1] + indexoffset - detect_cnt ))
   11156             :                      {
   11157           0 :                         indicies[0] = -1;
   11158           0 :                         indicies[1] = -1;
   11159             :                         /*status = SWfieldinfo(swathID, "scan_no", &rank, dims2, &nt, dimlist);
   11160             :                         buffer = (uint16 *)calloc(dims2[0], sizeof(uint16));
   11161             :                         status = SWreadfield(swathID,"scan_no", NULL, NULL, NULL, buffer);
   11162             :                         indexoffset = buffer[0] * detect_cnt;
   11163             :                         free(buffer);
   11164             :                         startReg = startReg - (indexoffset - detect_cnt);
   11165             :                         stopReg = stopReg - (indexoffset - 1);*/
   11166           0 :                         j = 0;
   11167           0 :                         for(i=0; i<scene_cnt; i++)
   11168             :                         {
   11169           0 :           if( startReg >= (l_indexin[j] + indexoffset - detect_cnt) && 
   11170           0 :         startReg <= (l_indexin[j+1] + indexoffset - detect_cnt) )
   11171           0 :                               if(indicies[0] == -1)
   11172           0 :                                  indicies[0] = j;
   11173           0 :           if( stopReg >= (l_indexin[j] + indexoffset - detect_cnt ) && 
   11174           0 :         stopReg <= (l_indexin[j+1] + indexoffset - detect_cnt ) )
   11175           0 :                               indicies[1] = j + 1;
   11176           0 :                            j = j + 2;
   11177           0 :                            if(l_indexin[j] == 0 || l_indexin[j+1] == 0)
   11178           0 :                               i = scene_cnt;
   11179             :                         }
   11180           0 :                         if(indicies[0] == -1)
   11181           0 :                            if(startReg < (l_indexin[0] +  indexoffset - detect_cnt))
   11182           0 :                               indicies[0] = 0;
   11183           0 :                         if(indicies[1] == -1)
   11184           0 :                            if(stopReg > (l_indexin[j - 1] + indexoffset - detect_cnt))
   11185           0 :                               indicies[1] = j - 1;
   11186             :                      }
   11187             :                   }
   11188             :                }
   11189             :             }   /* end of if for floating scene update */
   11190             :             else
   11191             :             {
   11192             :          /* If start of region is odd then increment */
   11193             :          /* ---------------------------------------- */
   11194           0 :          if (startReg % 2 == 1)
   11195             :          {
   11196           0 :       startReg++;
   11197             :          }
   11198             :       
   11199             :          /* If end of region is even then decrement */
   11200             :          /* --------------------------------------- */
   11201           0 :          if (stopReg % 2 == 0)
   11202             :          {
   11203           0 :       stopReg--;
   11204             :          }
   11205             : 
   11206           0 :                indicies[0]=startReg;
   11207           0 :                indicies[1]=stopReg;
   11208             :      }
   11209             :         }
   11210             :   
   11211           0 :   if (l_indexout != NULL)
   11212             :   { 
   11213           0 :            if(SWXRegion[regionID]->scanflag == 1)
   11214             :            {
   11215           0 :               nout = (indicies[1] - indicies[0] + 1);
   11216           0 :               j = 0;
   11217           0 :               if (nout == 1)
   11218           0 :                  l_indexout[0] = l_indexin[indicies[0]];
   11219           0 :               for(i=0; i<nout;i++)
   11220             :               {
   11221           0 :                  l_indexout[i] = l_indexin[indicies[0] + i];
   11222             :               }
   11223             :            }
   11224             :            else
   11225             :            {
   11226             :         /* get new l_index values */
   11227             :               /* ==================== */
   11228           0 :         for(i = startReg; i <= stopReg  ; i++)
   11229             :         {
   11230           0 :            l_indexout[i-startReg] = l_indexin[i];
   11231             :         }
   11232           0 :         nout = (stopReg - startReg) + 1;
   11233             :            }
   11234             :         }
   11235             :         else
   11236             :         {
   11237           0 :            nout = indicies[1] - indicies[0] + 1;
   11238             :   }
   11239             :      }
   11240             :    
   11241             :     
   11242           0 :     if(status == -1)
   11243             :     {  
   11244           0 :   return(status);
   11245             :     }
   11246             :     else
   11247             :     {
   11248           0 :         return(nout);
   11249             :     }
   11250             :     
   11251             : }
   11252             : 
   11253             : 
   11254             : 
   11255             : /*----------------------------------------------------------------------------|
   11256             : |  BEGIN_PROLOG                                                               |
   11257             : |                                                                             |
   11258             : |  FUNCTION: SWgeomapinfo                                                     |
   11259             : |                                                                             |
   11260             : |  DESCRIPTION: Returns mapping information for dimension                     |
   11261             : |                                                                             |
   11262             : |                                                                             |
   11263             : |  Return Value    Type     Units     Description                             |
   11264             : |  ============   ======  =========   =====================================   |
   11265             : |  status         intn                2 for l_indexed mapping, 1 for regular    |
   11266             : |                                     mapping, 0 if the dimension is not      |
   11267             : |                                     and (-1) FAIL                           |
   11268             : |                                                                             |
   11269             : |  INPUTS:                                                                    |
   11270             : |  swathID        int32               swath structure id                      |
   11271             : |  geodim         char                geolocation dimension name              |
   11272             : |                                                                             |
   11273             : |  OUTPUTS:                                                                   |
   11274             : |                                                                             |
   11275             : |  NONE                                                                       |
   11276             : |                                                                             |
   11277             : |  NOTES:                                                                     |
   11278             : |                                                                             |
   11279             : |                                                                             |
   11280             : |   Date     Programmer   Description                                         |
   11281             : |  ======   ============  =================================================   |
   11282             : |  Aug 97   Abe Taaheri   Original Programmer                                 |
   11283             : |  Sept 97  DaW           Modified return value so errors can be trapped      |
   11284             : |                                                                             |
   11285             : |  END_PROLOG                                                                 |
   11286             : -----------------------------------------------------------------------------*/
   11287             : intn
   11288           0 : SWgeomapinfo(int32 swathID, const char *geodim)
   11289             : 
   11290             : {
   11291             :     intn            status; /* routine return status variable */
   11292             :     
   11293             :     int32           fid;  /* HDF-EOS file ID */
   11294             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
   11295             :     int32           swVgrpID; /* Swath root Vgroup ID */
   11296           0 :     int32           idOffset = SWIDOFFSET;  /* Swath ID offset */
   11297             : 
   11298             :     char           *metabufr; /* Pointer to structural metadata (SM) */
   11299             :     char           *metabufi; /* Pointer to structural metadata (SM) */
   11300             :     char           *metaptrsr[2];/* Pointers to begin and end of SM section */
   11301             :     char           *metaptrsi[2];/* Pointers to begin and end of SM section */
   11302             :     char            swathname[80];  /* Swath Name */
   11303             :     char           *utlstrr;   /* Utility string */
   11304             :     char           *utlstri;   /* Utility string */
   11305             :    
   11306             :     
   11307             :     /* Allocate space for utility string */
   11308             :     /* --------------------------------- */
   11309           0 :     utlstrr = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
   11310           0 :     if(utlstrr == NULL)
   11311             :     { 
   11312           0 :   HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__);
   11313           0 :   return(-1);
   11314             :     }
   11315           0 :     utlstri = (char *) calloc(UTLSTR_MAX_SIZE, sizeof(char));
   11316           0 :     if(utlstri == NULL)
   11317             :     { 
   11318           0 :   HEpush(DFE_NOSPACE,"SWgeomapinfo", __FILE__, __LINE__);
   11319           0 :   free(utlstrr);
   11320           0 :   return(-1);
   11321             :     }
   11322           0 :     status = -1;
   11323             : 
   11324             :     /* Check for valid swath id */
   11325           0 :     status = SWchkswid(swathID, "SWgeomapinfo", &fid, &sdInterfaceID, &swVgrpID);
   11326           0 :     if (status == 0)
   11327             :     {
   11328             :   /* Get swath name */
   11329           0 :   Vgetname(SWXSwath[swathID % idOffset].IDTable, swathname);
   11330             : 
   11331             :   /* Get pointers to "DimensionMap" section within SM */
   11332           0 :   metabufr = EHmetagroup(sdInterfaceID, swathname, "s",
   11333             :                "DimensionMap", metaptrsr);
   11334             : 
   11335           0 :   if(metabufr == NULL)
   11336             :   {
   11337           0 :       free(utlstrr);
   11338           0 :       free(utlstri);
   11339           0 :       return(-1);
   11340             :   }
   11341             :   /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
   11342           0 :   snprintf(utlstrr, UTLSTR_MAX_SIZE, "%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
   11343             :     "\"\n\t\t\t\tDataDimension=");
   11344           0 :   metaptrsr[0] = strstr(metaptrsr[0], utlstrr);
   11345             :   
   11346             :   /* Get pointers to "IndexDimensionMap" section within SM */
   11347           0 :   metabufi = EHmetagroup(sdInterfaceID, swathname, "s",
   11348             :                "IndexDimensionMap", metaptrsi);
   11349           0 :   if(metabufi == NULL)
   11350             :   {
   11351           0 :       free(utlstrr);
   11352           0 :       free(utlstri);
   11353           0 :       return(-1);
   11354             :   }
   11355             :   /* Search for mapping - GeoDim/DataDim (surrounded by quotes) */
   11356           0 :   snprintf(utlstri, UTLSTR_MAX_SIZE, "%s%s%s", "\t\t\t\tGeoDimension=\"", geodim,
   11357             :     "\"\n\t\t\t\tDataDimension=");
   11358           0 :   metaptrsi[0] = strstr(metaptrsi[0], utlstri);
   11359             : 
   11360             :   /*
   11361             :   ** If regular mapping found add 1 to status
   11362             :         ** If l_indexed mapping found add 2
   11363             :         */
   11364           0 :   if (metaptrsr[0] < metaptrsr[1] && metaptrsr[0] != NULL)
   11365             :   {
   11366           0 :       status = status + 1;      
   11367             :         }
   11368             : 
   11369           0 :         if (metaptrsi[0] < metaptrsi[1] && metaptrsi[0] != NULL)
   11370             :         {
   11371           0 :            status = status + 2;     
   11372             :         }
   11373             : 
   11374           0 :   free(metabufr);
   11375           0 :   free(metabufi);
   11376             :     }
   11377             : 
   11378           0 :     free(utlstrr);
   11379           0 :     free(utlstri);
   11380             : 
   11381           0 :     return (status);
   11382             : }
   11383             : 
   11384             : /*----------------------------------------------------------------------------|
   11385             : |  BEGIN_PROLOG                                                               |
   11386             : |                                                                             |
   11387             : |  FUNCTION: SWsdid                                                           |
   11388             : |                                                                             |
   11389             : |  DESCRIPTION: Returns SD element ID for swath field                         |
   11390             : |                                                                             |
   11391             : |                                                                             |
   11392             : |  Return Value    Type     Units     Description                             |
   11393             : |  ============   ======  =========   =====================================   |
   11394             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
   11395             : |                                                                             |
   11396             : |  INPUTS:                                                                    |
   11397             : |  swathID        int32               swath structure ID                      |
   11398             : |  fieldname      const char          field name                              |
   11399             : |                                                                             |
   11400             : |                                                                             |
   11401             : |  OUTPUTS:                                                                   |
   11402             : |  sdid           int32               SD element ID                           |
   11403             : |                                                                             |
   11404             : |  NOTES:                                                                     |
   11405             : |                                                                             |
   11406             : |                                                                             |
   11407             : |   Date     Programmer   Description                                         |
   11408             : |  ======   ============  =================================================   |
   11409             : |  Oct 07   Andrey Kiselev  Original Programmer                               |
   11410             : |                                                                             |
   11411             : |  END_PROLOG                                                                 |
   11412             : -----------------------------------------------------------------------------*/
   11413             : intn
   11414           0 : SWsdid(int32 swathID, const char *fieldname, int32 *sdid)
   11415             : {
   11416             :     intn            status;         /* routine return status variable */
   11417             :     int32           fid;          /* HDF-EOS file ID */
   11418             :     int32           sdInterfaceID;      /* HDF SDS interface ID */
   11419             :     int32           dum;          /* Dummy variable */
   11420             :     int32           dims[H4_MAX_VAR_DIMS]; /* Field/SDS dimensions */
   11421             : 
   11422           0 :     status = SWchkswid(swathID, "SWsdid", &fid, &sdInterfaceID, &dum);
   11423           0 :     if (status != -1)
   11424             :     {
   11425           0 :         status = SWSDfldsrch(swathID, sdInterfaceID, fieldname,
   11426             :                              sdid, &dum, &dum, &dum, dims, &dum);
   11427             :     }
   11428             : 
   11429           0 :     return (status);
   11430             : }

Generated by: LCOV version 1.14