LTP GCOV extension - code coverage report
Current view: directory - frmts/hdf4/hdf-eos - EHapi.c
Test: gdal_filtered.info
Date: 2010-07-12 Instrumented lines: 903
Code covered: 22.6 % Executed lines: 204

       1                 : /*****************************************************************************
       2                 :  * $Id: EHapi.c 12769 2007-11-14 15:12:27Z dron $
       3                 :  *
       4                 :  * This module has a number of additions and improvements over the original
       5                 :  * implementation to be suitable for usage in GDAL HDF driver.
       6                 :  *
       7                 :  * Andrey Kiselev <dron@ak4719.spb.edu> is responsible for all the changes.
       8                 :  ****************************************************************************/
       9                 : 
      10                 : /*
      11                 : Copyright (C) 1996 Hughes and Applied Research Corporation
      12                 : 
      13                 : Permission to use, modify, and distribute this software and its documentation 
      14                 : for any purpose without fee is hereby granted, provided that the above 
      15                 : copyright notice appear in all copies and that both that copyright notice and 
      16                 : this permission notice appear in supporting documentation.
      17                 : */
      18                 : 
      19                 : #include <errno.h>
      20                 : #include "mfhdf.h"
      21                 : #include "HdfEosDef.h"
      22                 : 
      23                 : /* Set maximun number of HDF-EOS files to HDF limit (MAX_FILE) */
      24                 : #define NEOSHDF MAX_FILE
      25                 : static uint8 EHXtypeTable[NEOSHDF];
      26                 : static uint8 EHXacsTable[NEOSHDF];
      27                 : static int32 EHXfidTable[NEOSHDF];
      28                 : static int32 EHXsdTable[NEOSHDF];
      29                 : 
      30                 : /* define a macro for the string size of the utility strings and some dimension
      31                 :    list strings. The value in previous versions of this code may not be 
      32                 :    enough in some cases. The length now is 512 which seems to be more than 
      33                 :    enough to hold larger strings. */
      34                 :    
      35                 : #define UTLSTR_MAX_SIZE 512
      36                 : #define UTLSTRSIZE  32000
      37                 : 
      38                 : #define EHIDOFFSET 524288
      39                 : 
      40                 : #define HDFEOSVERSION 2.12
      41                 : #define HDFEOSVERSION1 "2.12"
      42                 : #include <HDFEOSVersion.h>
      43                 : 
      44                 : #define MAX_RETRIES 10
      45                 : 
      46                 : /* Function Prototypes */
      47                 : static intn EHmetalist(char *, char *);
      48                 : 
      49                 : 
      50                 : /*----------------------------------------------------------------------------|
      51                 : |  BEGIN_PROLOG                                                               |
      52                 : |                                                                             |
      53                 : |  FUNCTION: EHopen                                                           |
      54                 : |                                                                             |
      55                 : |  DESCRIPTION: Opens HDF-EOS file and returns file handle                    |
      56                 : |                                                                             |
      57                 : |                                                                             |
      58                 : |  Return Value    Type     Units     Description                             |
      59                 : |  ============   ======  =========   =====================================   |
      60                 : |  fid            int32               HDF-EOS file ID                         |
      61                 : |                                                                             |
      62                 : |  INPUTS:                                                                    |
      63                 : |  filename       char                Filename                                |
      64                 : |  access         intn                HDF access code                         |
      65                 : |                                                                             |
      66                 : |  OUTPUTS:                                                                   |
      67                 : |             None                                                            |
      68                 : |                                                                             |
      69                 : |  NOTES:                                                                     |
      70                 : |                                                                             |
      71                 : |                                                                             |
      72                 : |   Date     Programmer   Description                                         |
      73                 : |  ======   ============  =================================================   |
      74                 : |  Jun 96   Joel Gales    Original Programmer                                 |
      75                 : |  Jul 96   Joel Gales    Add file id offset EHIDOFFSET                       |
      76                 : |  Aug 96   Joel Gales    Add "END" statment to structural metadata           |
      77                 : |  Sep 96   Joel Gales    Reverse order of Hopen ane SDstart statements       |
      78                 : |                         for RDWR and READ access                            |
      79                 : |  Oct 96   Joel Gales    Trap CREATE & RDWR (no write permission)            |
      80                 : |                         access errors                                       |
      81                 : |  Apr 97   Joel Gales    Fix problem with RDWR open when file previously     |
      82                 : |                         open for READONLY access                            |
      83                 : |                                                                             |
      84                 : |  END_PROLOG                                                                 |
      85                 : -----------------------------------------------------------------------------*/
      86                 : int32
      87                 : EHopen(char *filename, intn access)
      88                 : 
      89              12 : {
      90                 :     intn            i;    /* Loop index */
      91              12 :     intn            status = 0; /* routine return status variable */
      92                 :     intn            dum;  /* Dummy variable */
      93                 : 
      94                 :     int32           HDFfid; /* HDF file ID */
      95              12 :     int32           fid = -1; /* HDF-EOS file ID */
      96                 :     int32           sdInterfaceID;  /* HDF SDS interface ID */
      97              12 :     int32           nfileopen = 0;  /* # of HDF files open */
      98                 :     int32           attrIndex;  /* Structural Metadata attribute index */
      99                 : 
     100                 :     uint8           acs;  /* Read (0) / Write (1) access code */
     101                 : 
     102                 :     char           *testname; /* Test filename */
     103                 :     char            errbuf[256];/* Error report buffer */
     104                 :     char           *metabuf;  /* Pointer to structural metadata buffer */
     105                 :     char            hdfeosVersion[32];  /* HDFEOS version string */
     106                 : 
     107                 :     intn            retryCount;
     108                 : 
     109                 : 
     110                 :     /* Determine number of files currently opened */
     111                 :     /* ------------------------------------------ */
     112             396 :     for (i = 0; i < NEOSHDF; i++)
     113                 :     {
     114             384 :   nfileopen += EHXtypeTable[i];
     115                 :     }
     116                 : 
     117                 : 
     118                 : 
     119                 :     /* Setup file interface */
     120                 :     /* -------------------- */
     121              12 :     if (nfileopen < NEOSHDF)
     122                 :     {
     123                 : 
     124                 :   /*
     125                 :    * Check that file has not been previously opened for write access if
     126                 :    * current open request is not READONLY
     127                 :    */
     128              12 :   if (access != DFACC_READ)
     129                 :   {
     130                 :       /* Loop through all files */
     131                 :       /* ---------------------- */
     132               0 :       for (i = 0; i < NEOSHDF; i++)
     133                 :       {
     134                 :     /* if entry is active file opened for write access ... */
     135                 :     /* --------------------------------------------------- */
     136               0 :     if (EHXtypeTable[i] != 0 && EHXacsTable[i] == 1)
     137                 :     {
     138                 :         /* Get filename (testname) */
     139                 :         /* ----------------------- */
     140               0 :         Hfidinquire(EHXfidTable[i], &testname, &dum, &dum);
     141                 : 
     142                 : 
     143                 :         /* if same as filename then report error */
     144                 :         /* ------------------------------------- */
     145               0 :         if (strcmp(testname, filename) == 0)
     146                 :         {
     147               0 :       status = -1;
     148               0 :       fid = -1;
     149               0 :       HEpush(DFE_ALROPEN, "EHopen", __FILE__, __LINE__);
     150               0 :       HEreport("\"%s\" already open.\n", filename);
     151               0 :       break;
     152                 :         }
     153                 :     }
     154                 :       }
     155                 :   }
     156              12 :   if (status == 0)
     157                 :   {
     158                 :       /* Create HDF-EOS file */
     159                 :       /* ------------------- */
     160              12 :       switch (access)
     161                 :       {
     162                 :       case DFACC_CREATE:
     163                 : 
     164                 :     /* Get SDS interface ID */
     165                 :     /* -------------------- */
     166               0 :     sdInterfaceID = SDstart(filename, DFACC_CREATE);
     167                 : 
     168                 :     /* If SDstart successful ... */
     169                 :     /* ------------------------- */
     170               0 :     if (sdInterfaceID != -1)
     171                 :     {
     172                 :         /* Set HDFEOS version number in file */
     173                 :         /* --------------------------------- */
     174               0 :         sprintf(hdfeosVersion, "%s%s", "HDFEOS_V",
     175                 :           HDFEOSVERSION1);
     176               0 :         SDsetattr(sdInterfaceID, "HDFEOSVersion", DFNT_CHAR8,
     177                 :             strlen(hdfeosVersion), hdfeosVersion);
     178                 : 
     179                 : 
     180                 :         /* Get HDF file ID */
     181                 :         /* --------------- */
     182               0 :         HDFfid = Hopen(filename, DFACC_RDWR, 0);
     183                 : 
     184                 :         /* Set open access to write */
     185                 :         /* ------------------------ */
     186               0 :         acs = 1;
     187                 : 
     188                 :         /* Setup structural metadata */
     189                 :         /* ------------------------- */
     190               0 :         metabuf = (char *) calloc(32000, 1);
     191               0 :         if(metabuf == NULL)
     192                 :         { 
     193               0 :       HEpush(DFE_NOSPACE,"EHopen", __FILE__, __LINE__);
     194               0 :       return(-1);
     195                 :         }
     196                 : 
     197               0 :         strcpy(metabuf, "GROUP=SwathStructure\n");
     198               0 :         strcat(metabuf, "END_GROUP=SwathStructure\n");
     199               0 :         strcat(metabuf, "GROUP=GridStructure\n");
     200               0 :         strcat(metabuf, "END_GROUP=GridStructure\n");
     201               0 :         strcat(metabuf, "GROUP=PointStructure\n");
     202               0 :         strcat(metabuf, "END_GROUP=PointStructure\n");
     203               0 :         strcat(metabuf, "END\n");
     204                 : 
     205                 :         /* Write Structural metadata */
     206                 :         /* ------------------------- */
     207               0 :         SDsetattr(sdInterfaceID, "StructMetadata.0",
     208                 :             DFNT_CHAR8, 32000, metabuf);
     209               0 :         free(metabuf);
     210                 :     } else
     211                 :     {
     212                 :         /* If error in SDstart then report */
     213                 :         /* ------------------------------- */
     214               0 :         fid = -1;
     215               0 :         status = -1;
     216               0 :         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     217               0 :         sprintf(errbuf, "%s%s%s", "\"", filename,
     218                 :           "\" cannot be created.");
     219               0 :         HEreport("%s\n", errbuf);
     220                 :     }
     221                 : 
     222               0 :     break;
     223                 : 
     224                 :     /* Open existing HDF-EOS file for read/write access */
     225                 :     /* ------------------------------------------------ */
     226                 :       case DFACC_RDWR:
     227                 : 
     228                 :     /* Get HDF file ID */
     229                 :     /* --------------- */
     230                 : #ifndef _PGS_OLDNFS
     231                 : /* The following loop around the function Hopen is intended to deal with the NFS cache 
     232                 :    problem when opening file fails with errno = 150 or 151. When NFS cache is updated,
     233                 :    this part of change is no longer neccessary.              10/18/1999   */
     234               0 :                 retryCount = 0;
     235               0 :                 HDFfid = -1;
     236               0 :                 while ((HDFfid == -1) && (retryCount < MAX_RETRIES))
     237                 :                 {
     238               0 :                 HDFfid = Hopen(filename, DFACC_RDWR, 0);
     239               0 :                 if((HDFfid == -1) && (errno == 150 || errno == 151))
     240                 :                     {
     241               0 :                     HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     242               0 :                     sprintf(errbuf, "\"%s\" cannot be opened for READ/WRITE access, will retry %d times.", filename,  (MAX_RETRIES - retryCount - 1));
     243               0 :                     HEreport("%s\n", errbuf);
     244                 :                     }
     245               0 :                 retryCount++;
     246                 :                 }
     247                 : #else
     248                 :                 HDFfid = Hopen(filename, DFACC_RDWR, 0);
     249                 : #endif
     250                 : 
     251                 :     /* If Hopen successful ... */
     252                 :     /* ----------------------- */
     253               0 :     if (HDFfid != -1)
     254                 :     {
     255                 :         /* Get SDS interface ID */
     256                 :         /* -------------------- */
     257               0 :         sdInterfaceID = SDstart(filename, DFACC_RDWR);
     258                 : 
     259                 :                     /* If SDstart successful ... */
     260                 :                     /* ------------------------- */
     261               0 :                     if (sdInterfaceID != -1)
     262                 :                     {
     263                 :                        /* Set HDFEOS version number in file */
     264                 :                        /* --------------------------------- */
     265                 :           
     266               0 :           attrIndex = SDfindattr(sdInterfaceID, "HDFEOSVersion");
     267               0 :           if (attrIndex == -1)
     268                 :       {
     269               0 :         sprintf(hdfeosVersion, "%s%s", "HDFEOS_V",
     270                 :           HDFEOSVERSION1);
     271               0 :         SDsetattr(sdInterfaceID, "HDFEOSVersion", DFNT_CHAR8,
     272                 :             strlen(hdfeosVersion), hdfeosVersion);
     273                 :       }
     274                 :            /* Set open access to write */
     275                 :            /* ------------------------ */
     276               0 :            acs = 1;
     277                 : 
     278                 :            /* Get structural metadata attribute ID */
     279                 :            /* ------------------------------------ */
     280               0 :            attrIndex = SDfindattr(sdInterfaceID, "StructMetadata.0");
     281                 : 
     282                 :            /* Write structural metadata if it doesn't exist */
     283                 :            /* --------------------------------------------- */
     284               0 :            if (attrIndex == -1)
     285                 :            {
     286               0 :         metabuf = (char *) calloc(32000, 1);
     287               0 :         if(metabuf == NULL)
     288                 :         { 
     289               0 :             HEpush(DFE_NOSPACE,"EHopen", __FILE__, __LINE__);
     290               0 :             return(-1);
     291                 :         }
     292                 : 
     293               0 :         strcpy(metabuf, "GROUP=SwathStructure\n");
     294               0 :         strcat(metabuf, "END_GROUP=SwathStructure\n");
     295               0 :         strcat(metabuf, "GROUP=GridStructure\n");
     296               0 :         strcat(metabuf, "END_GROUP=GridStructure\n");
     297               0 :         strcat(metabuf, "GROUP=PointStructure\n");
     298               0 :         strcat(metabuf, "END_GROUP=PointStructure\n");
     299               0 :         strcat(metabuf, "END\n");
     300                 : 
     301               0 :         SDsetattr(sdInterfaceID, "StructMetadata.0",
     302                 :           DFNT_CHAR8, 32000, metabuf);
     303               0 :         free(metabuf);
     304                 :            }
     305                 :                     } else
     306                 :                     {
     307                 :                         /* If error in SDstart then report */
     308                 :                         /* ------------------------------- */
     309               0 :                         fid = -1;
     310               0 :                         status = -1;
     311               0 :                         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     312               0 :                         sprintf(errbuf, "%s%s%s", "\"", filename,
     313                 :                             "\" cannot be opened for read/write access.");
     314               0 :                         HEreport("%s\n", errbuf);
     315                 :                     }
     316                 :     } else
     317                 :     {
     318                 :         /* If error in Hopen then report */
     319                 :         /* ----------------------------- */
     320               0 :         fid = -1;
     321               0 :         status = -1;
     322               0 :         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     323               0 :         sprintf(errbuf, "%s%s%s", "\"", filename,
     324                 :           "\" cannot be opened for RDWR access.");
     325               0 :         HEreport("%s\n", errbuf);
     326                 :     }
     327                 : 
     328               0 :     break;
     329                 : 
     330                 : 
     331                 :     /* Open existing HDF-EOS file for read-only access */
     332                 :     /* ----------------------------------------------- */
     333                 :       case DFACC_READ:
     334                 : 
     335                 :     /* Get HDF file ID */
     336                 :     /* --------------- */
     337                 : #ifndef _PGS_OLDNFS
     338                 : /* The following loop around the function Hopen is intended to deal with the NFS cache 
     339                 :    problem when opening file fails with errno = 150 or 151. When NFS cache is updated,
     340                 :    this part of change is no longer neccessary.              10/18/1999   */
     341              12 :                 retryCount = 0;
     342              12 :                 HDFfid = -1;
     343              36 :                 while ((HDFfid == -1) && (retryCount < MAX_RETRIES))
     344                 :                 {
     345              12 :                 HDFfid = Hopen(filename, DFACC_READ, 0);
     346              12 :                 if((HDFfid == -1) && (errno == 150 || errno == 151))
     347                 :                     {
     348               0 :                     HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     349               0 :                     sprintf(errbuf, "\"%s\" cannot be opened for READONLY access, will retry %d times.", filename,  (MAX_RETRIES - retryCount - 1));
     350               0 :                     HEreport("%s\n", errbuf);
     351                 :                     }
     352              12 :                 retryCount++;
     353                 :                 }
     354                 : #else
     355                 :                 HDFfid = Hopen(filename, DFACC_READ, 0);
     356                 : #endif
     357                 : 
     358                 :     /* If file does not exist report error */
     359                 :     /* ----------------------------------- */
     360              12 :     if (HDFfid == -1)
     361                 :     {
     362               0 :         fid = -1;
     363               0 :         status = -1;
     364               0 :         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     365               0 :         strcpy(errbuf, "\"");
     366               0 :         strcat(errbuf, filename);
     367               0 :         strcat(errbuf, "\" (opened for READONLY access)");
     368               0 :         strcat(errbuf, " does not exist.");
     369               0 :         HEreport("%s\n", errbuf);
     370                 :     } else
     371                 :     {
     372                 :         /* If file exists then get SD interface ID */
     373                 :         /* --------------------------------------- */
     374              12 :         sdInterfaceID = SDstart(filename, DFACC_RDONLY);
     375                 : 
     376                 :                         /* If SDstart successful ... */
     377                 :                         /* ------------------------- */
     378              12 :                         if (sdInterfaceID != -1)
     379                 :                         {
     380                 :  
     381                 :                /* Set open access to read-only */
     382                 :                /* ---------------------------- */
     383              12 :                acs = 0;
     384                 :              } else
     385                 :                         {
     386                 :                             /* If error in SDstart then report */
     387                 :                             /* ------------------------------- */
     388               0 :                             fid = -1;
     389               0 :                             status = -1;
     390               0 :                             HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     391               0 :                             sprintf(errbuf, "%s%s%s", "\"", filename,
     392                 :                             "\" cannot be opened for read access.");
     393               0 :                             HEreport("%s\n", errbuf);
     394                 :                         }
     395                 :     }
     396                 : 
     397              12 :     break;
     398                 : 
     399                 :       default:
     400                 :     /* Invalid Access Code */
     401                 :     /* ------------------- */
     402               0 :     fid = -1;
     403               0 :     status = -1;
     404               0 :     HEpush(DFE_BADACC, "EHopen", __FILE__, __LINE__);
     405               0 :     HEreport("Access Code: %d (%s).\n", access, filename);
     406                 :       }
     407                 : 
     408                 :   }
     409                 :     } else
     410                 :     {
     411                 :   /* Too many files opened */
     412                 :   /* --------------------- */
     413               0 :   status = -1;
     414               0 :   fid = -1;
     415               0 :   HEpush(DFE_TOOMANY, "EHopen", __FILE__, __LINE__);
     416               0 :   HEreport("No more than %d files may be open simultaneously (%s).\n",
     417                 :      NEOSHDF, filename);
     418                 :     }
     419                 : 
     420                 : 
     421                 : 
     422                 : 
     423              12 :     if (status == 0)
     424                 :     {
     425                 :   /* Initialize Vgroup Access */
     426                 :   /* ------------------------ */
     427              12 :   Vstart(HDFfid);
     428                 : 
     429                 : 
     430                 :   /* Assign HDFEOS fid # & Load HDF fid and sdInterfaceID tables */
     431                 :   /* ----------------------------------------------------------- */
     432              12 :   for (i = 0; i < NEOSHDF; i++)
     433                 :   {
     434              12 :       if (EHXtypeTable[i] == 0)
     435                 :       {
     436              12 :     fid = i + EHIDOFFSET;
     437              12 :     EHXacsTable[i] = acs;
     438              12 :     EHXtypeTable[i] = 1;
     439              12 :     EHXfidTable[i] = HDFfid;
     440              12 :     EHXsdTable[i] = sdInterfaceID;
     441              12 :     break;
     442                 :       }
     443                 :   }
     444                 : 
     445                 :     }
     446              12 :     return (fid);
     447                 : }
     448                 : 
     449                 : 
     450                 : 
     451                 : 
     452                 : /*----------------------------------------------------------------------------|
     453                 : |  BEGIN_PROLOG                                                               |
     454                 : |                                                                             |
     455                 : |  FUNCTION: EHchkfid                                                         |
     456                 : |                                                                             |
     457                 : |  DESCRIPTION: Checks for valid file id and returns HDF file ID and          |
     458                 : |               SD interface ID                                               |
     459                 : |                                                                             |
     460                 : |                                                                             |
     461                 : |  Return Value    Type     Units     Description                             |
     462                 : |  ============   ======  =========   =====================================   |
     463                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     464                 : |                                                                             |
     465                 : |  INPUTS:                                                                    |
     466                 : |  fid            int32               HDF-EOS file ID                         |
     467                 : |  name           char                Structure name                          |
     468                 : |                                                                             |
     469                 : |  OUTPUTS:                                                                   |
     470                 : |  HDFfid         int32               HDF File ID                             |
     471                 : |  sdInterfaceID  int32               SDS interface ID                        |
     472                 : |  access         uint8               access code                             |
     473                 : |                                                                             |
     474                 : |  NOTES:                                                                     |
     475                 : |                                                                             |
     476                 : |                                                                             |
     477                 : |   Date     Programmer   Description                                         |
     478                 : |  ======   ============  =================================================   |
     479                 : |  Jun 96   Joel Gales    Original Programmer                                 |
     480                 : |  Jul 96   Joel Gales    set status=-1 if failure                            |
     481                 : |  Jul 96   Joel Gales    Add file id offset EHIDOFFSET                       |
     482                 : |                                                                             |
     483                 : |  END_PROLOG                                                                 |
     484                 : -----------------------------------------------------------------------------*/
     485                 : intn
     486                 : EHchkfid(int32 fid, char *name, int32 * HDFfid, int32 * sdInterfaceID,
     487                 :    uint8 * access)
     488                 : 
     489             742 : {
     490             742 :     intn            status = 0; /* routine return status variable */
     491                 :     intn            fid0; /* HDFEOS file ID - Offset */
     492                 : 
     493                 : 
     494                 :     /* Check for valid HDFEOS file ID range */
     495                 :     /* ------------------------------------ */
     496             742 :     if (fid < EHIDOFFSET || fid > NEOSHDF + EHIDOFFSET)
     497                 :     {
     498               0 :   status = -1;
     499               0 :   HEpush(DFE_RANGE, "EHchkfid", __FILE__, __LINE__);
     500               0 :   HEreport("Invalid file id: %d.  ID must be >= %d and < %d (%s).\n",
     501                 :      fid, EHIDOFFSET, NEOSHDF + EHIDOFFSET, name);
     502                 :     } else
     503                 :     {
     504                 :   /* Compute "reduced" file ID */
     505                 :   /* ------------------------- */
     506             742 :   fid0 = fid % EHIDOFFSET;
     507                 : 
     508                 : 
     509                 :   /* Check that HDFEOS file ID is active */
     510                 :   /* ----------------------------------- */
     511             742 :   if (EHXtypeTable[fid0] == 0)
     512                 :   {
     513               0 :       status = -1;
     514               0 :       HEpush(DFE_GENAPP, "EHchkfid", __FILE__, __LINE__);
     515               0 :       HEreport("File id %d not active (%s).\n", fid, name);
     516                 :   } else
     517                 :   {
     518                 :       /*
     519                 :        * Get HDF file ID, SD interface ID and file access from external
     520                 :        * arrays
     521                 :        */
     522             742 :       *HDFfid = EHXfidTable[fid0];
     523             742 :       *sdInterfaceID = EHXsdTable[fid0];
     524             742 :       *access = EHXacsTable[fid0];
     525                 :   }
     526                 :     }
     527                 : 
     528             742 :     return (status);
     529                 : }
     530                 : 
     531                 : 
     532                 : 
     533                 : 
     534                 : /*----------------------------------------------------------------------------|
     535                 : |  BEGIN_PROLOG                                                               |
     536                 : |                                                                             |
     537                 : |  FUNCTION: EHidinfo                                                         |
     538                 : |                                                                             |
     539                 : |  DESCRIPTION: Gets Hopen and SD intereface IDs from HDF-EOS id              |
     540                 : |                                                                             |
     541                 : |                                                                             |
     542                 : |  Return Value    Type     Units     Description                             |
     543                 : |  ============   ======  =========   =====================================   |
     544                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     545                 : |                                                                             |
     546                 : |  INPUTS:                                                                    |
     547                 : |  fid            int32               HDF-EOS file ID                         |
     548                 : |                                                                             |
     549                 : |  OUTPUTS:                                                                   |
     550                 : |  HDFfid         int32               HDF File ID                             |
     551                 : |  sdInterfaceID  int32               SDS interface ID                        |
     552                 : |                                                                             |
     553                 : |  NOTES:                                                                     |
     554                 : |                                                                             |
     555                 : |                                                                             |
     556                 : |   Date     Programmer   Description                                         |
     557                 : |  ======   ============  =================================================   |
     558                 : |  Jul 96   Joel Gales    Original Programmer                                 |
     559                 : |                                                                             |
     560                 : |  END_PROLOG                                                                 |
     561                 : -----------------------------------------------------------------------------*/
     562                 : intn
     563                 : EHidinfo(int32 fid, int32 * HDFfid, int32 * sdInterfaceID)
     564                 : 
     565               8 : {
     566               8 :     intn            status = 0; /* routine return status variable */
     567                 :     uint8           dum;  /* Dummy variable */
     568                 : 
     569                 :     /* Call EHchkfid to get HDF and SD interface IDs */
     570                 :     /* --------------------------------------------- */
     571               8 :     status = EHchkfid(fid, "EHidinfo", HDFfid, sdInterfaceID, &dum);
     572                 : 
     573               8 :     return (status);
     574                 : }
     575                 : 
     576                 : 
     577                 : 
     578                 : /*----------------------------------------------------------------------------|
     579                 : |  BEGIN_PROLOG                                                               |
     580                 : |                                                                             |
     581                 : |  FUNCTION: EHfilename                                                       |
     582                 : |                                                                             |
     583                 : |  DESCRIPTION: Returns HDF filename                                          |
     584                 : |                                                                             |
     585                 : |                                                                             |
     586                 : |  Return Value    Type     Units     Description                             |
     587                 : |  ============   ======  =========   =====================================   |
     588                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     589                 : |                                                                             |
     590                 : |  INPUTS:                                                                    |
     591                 : |  fid            int32               HDF-EOS file id                         |
     592                 : |                                                                             |
     593                 : |  OUTPUTS:                                                                   |
     594                 : |  filename       char                HDF-EOS file name                       |
     595                 : |                                                                             |
     596                 : |  NOTES:                                                                     |
     597                 : |                                                                             |
     598                 : |                                                                             |
     599                 : |   Date     Programmer   Description                                         |
     600                 : |  ======   ============  =================================================   |
     601                 : |  Sep 96   Joel Gales    Original Programmer                                 |
     602                 : |                                                                             |
     603                 : |  END_PROLOG                                                                 |
     604                 : -----------------------------------------------------------------------------*/
     605                 : intn
     606                 : EHfilename(int32 fid, char *filename)
     607               0 : {
     608               0 :     intn            status = 0; /* routine return status variable */
     609                 :     intn            dum;  /* Dummy variable */
     610                 : 
     611                 :     char           *fname;  /* Pointer to filename */
     612                 : 
     613                 :     /* Get point to filename from Hfidinquire */
     614                 :     /* -------------------------------------- */
     615               0 :     Hfidinquire(EHXfidTable[fid % EHIDOFFSET], &fname, &dum, &dum);
     616               0 :     strcpy(filename, fname);
     617                 : 
     618               0 :     return (status);
     619                 : }
     620                 : 
     621                 : 
     622                 : 
     623                 : 
     624                 : /*----------------------------------------------------------------------------|
     625                 : |  BEGIN_PROLOG                                                               |
     626                 : |                                                                             |
     627                 : |  FUNCTION: EHgetversion                                                     |
     628                 : |                                                                             |
     629                 : |  DESCRIPTION: Returns HDF-EOS version string                                |
     630                 : |                                                                             |
     631                 : |                                                                             |
     632                 : |  Return Value    Type     Units     Description                             |
     633                 : |  ============   ======  =========   =====================================   |
     634                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     635                 : |                                                                             |
     636                 : |  INPUTS:                                                                    |
     637                 : |  fid            int32               HDF-EOS file id                         |
     638                 : |                                                                             |
     639                 : |  OUTPUTS:                                                                   |
     640                 : |  version        char                HDF-EOS version string                  |
     641                 : |                                                                             |
     642                 : |  NOTES:                                                                     |
     643                 : |                                                                             |
     644                 : |                                                                             |
     645                 : |   Date     Programmer   Description                                         |
     646                 : |  ======   ============  =================================================   |
     647                 : |  Mar 97   Joel Gales    Original Programmer                                 |
     648                 : |                                                                             |
     649                 : |  END_PROLOG                                                                 |
     650                 : -----------------------------------------------------------------------------*/
     651                 : intn
     652                 : EHgetversion(int32 fid, char *version)
     653               0 : {
     654               0 :     intn            status = 0; /* routine return status variable */
     655                 : 
     656                 :     uint8           access; /* Access code */
     657                 :     int32           dum;  /* Dummy variable */
     658                 :     int32           sdInterfaceID;  /* HDF SDS interface ID */
     659                 :     int32           attrIndex;  /* HDFEOS version attribute index */
     660                 :     int32           count;  /* Version string size */
     661                 : 
     662                 :     char            attrname[16]; /* Attribute name */
     663                 : 
     664                 : 
     665                 :     /* Get SDS interface ID */
     666                 :     /* -------------------- */
     667               0 :     status = EHchkfid(fid, "EHgetversion", &dum, &sdInterfaceID, &access);
     668                 : 
     669                 : 
     670                 :     /* Get attribute index number */
     671                 :     /* -------------------------- */
     672               0 :     attrIndex = SDfindattr(sdInterfaceID, "HDFEOSVersion");
     673                 : 
     674                 :     /* No such attribute */
     675                 :     /* ----------------- */
     676               0 :     if (attrIndex < 0)
     677               0 :         return (-1);
     678                 : 
     679                 :     /* Get attribute size */
     680                 :     /* ------------------ */
     681               0 :     status = SDattrinfo(sdInterfaceID, attrIndex, attrname, &dum, &count);
     682                 : 
     683                 :     /* Check return status */
     684                 :     /* ------------------- */
     685               0 :     if (status < 0)
     686               0 :         return (-1);
     687                 : 
     688                 :     /* Read version attribute */
     689                 :     /* ---------------------- */
     690               0 :     status = SDreadattr(sdInterfaceID, attrIndex, (VOIDP) version);
     691                 : 
     692                 : 
     693                 :     /* Place string terminator on version string */
     694                 :     /* ----------------------------------------- */
     695               0 :     version[count] = 0;
     696                 : 
     697                 : 
     698               0 :     return (status);
     699                 : }
     700                 : 
     701                 : 
     702                 : 
     703                 : 
     704                 : /*----------------------------------------------------------------------------|
     705                 : |  BEGIN_PROLOG                                                               |
     706                 : |                                                                             |
     707                 : |  FUNCTION: EHconvAng                                                        |
     708                 : |                                                                             |
     709                 : |  DESCRIPTION: Angle conversion Utility                                      |
     710                 : |                                                                             |
     711                 : |                                                                             |
     712                 : |  Return Value    Type     Units     Description                             |
     713                 : |  ============   ======  =========   =====================================   |
     714                 : |  outAngle       float64             Output Angle value                      |
     715                 : |                                                                             |
     716                 : |  INPUTS:                                                                    |
     717                 : |  inAngle        float64             Input Angle value                       |
     718                 : |  code           intn                Conversion code                         |
     719                 : !                                       HDFE_RAD_DEG (0)                      |
     720                 : |                                       HDFE_DEG_RAD (1)                      |
     721                 : |                                       HDFE_DMS_DEG (2)                      |
     722                 : |                                       HDFE_DEG_DMS (3)                      |
     723                 : |                                       HDFE_RAD_DMS (4)                      |
     724                 : |                                       HDFE_DMS_RAD (5)                      |
     725                 : |                                                                             |
     726                 : |  OUTPUTS:                                                                   |
     727                 : |     None                                                                    |
     728                 : |                                                                             |
     729                 : |  NOTES:                                                                     |
     730                 : |                                                                             |
     731                 : |                                                                             |
     732                 : |   Date     Programmer   Description                                         |
     733                 : |  ======   ============  =================================================   |
     734                 : |  Jun 96   Joel Gales    Original Programmer                                 |
     735                 : |  Feb 97   Joel Gales    Correct "60" min & "60" sec in _DMS conversion      |
     736                 : |                                                                             |
     737                 : |  END_PROLOG                                                                 |
     738                 : -----------------------------------------------------------------------------*/
     739                 : float64
     740                 : EHconvAng(float64 inAngle, intn code)
     741               0 : {
     742                 : #define RADIANS_TO_DEGREES 180. / 3.14159265358979324
     743                 : #define DEGREES_TO_RADIANS 3.14159265358979324 / 180.
     744                 : 
     745                 :     int32           min;  /* Truncated Minutes */
     746                 :     int32           deg;  /* Truncated Degrees */
     747                 : 
     748                 :     float64         sec;  /* Seconds */
     749               0 :     float64         outAngle = 0.0; /* Angle in desired units */
     750                 : 
     751               0 :     switch (code)
     752                 :     {
     753                 : 
     754                 :   /* Convert radians to degrees */
     755                 :   /* -------------------------- */
     756                 :     case HDFE_RAD_DEG:
     757               0 :   outAngle = inAngle * RADIANS_TO_DEGREES;
     758               0 :   break;
     759                 : 
     760                 : 
     761                 :   /* Convert degrees to radians */
     762                 :   /* -------------------------- */
     763                 :     case HDFE_DEG_RAD:
     764               0 :   outAngle = inAngle * DEGREES_TO_RADIANS;
     765               0 :   break;
     766                 : 
     767                 : 
     768                 :   /* Convert packed degrees to degrees */
     769                 :   /* --------------------------------- */
     770                 :     case HDFE_DMS_DEG:
     771               0 :   deg = inAngle / 1000000;
     772               0 :   min = (inAngle - deg * 1000000) / 1000;
     773               0 :   sec = (inAngle - deg * 1000000 - min * 1000);
     774               0 :   outAngle = deg + min / 60.0 + sec / 3600.0;
     775               0 :   break;
     776                 : 
     777                 : 
     778                 :   /* Convert degrees to packed degrees */
     779                 :   /* --------------------------------- */
     780                 :     case HDFE_DEG_DMS:
     781               0 :   deg = inAngle;
     782               0 :   min = (inAngle - deg) * 60;
     783               0 :   sec = (inAngle - deg - min / 60.0) * 3600;
     784                 : 
     785               0 :   if ((intn) sec == 60)
     786                 :   {
     787               0 :       sec = sec - 60;
     788               0 :       min = min + 1;
     789                 :   }
     790               0 :   if (min == 60)
     791                 :   {
     792               0 :       min = min - 60;
     793               0 :       deg = deg + 1;
     794                 :   }
     795               0 :   outAngle = deg * 1000000 + min * 1000 + sec;
     796               0 :   break;
     797                 : 
     798                 : 
     799                 :   /* Convert radians to packed degrees */
     800                 :   /* --------------------------------- */
     801                 :     case HDFE_RAD_DMS:
     802               0 :   inAngle = inAngle * RADIANS_TO_DEGREES;
     803               0 :   deg = inAngle;
     804               0 :   min = (inAngle - deg) * 60;
     805               0 :   sec = (inAngle - deg - min / 60.0) * 3600;
     806                 : 
     807               0 :   if ((intn) sec == 60)
     808                 :   {
     809               0 :       sec = sec - 60;
     810               0 :       min = min + 1;
     811                 :   }
     812               0 :   if (min == 60)
     813                 :   {
     814               0 :       min = min - 60;
     815               0 :       deg = deg + 1;
     816                 :   }
     817               0 :   outAngle = deg * 1000000 + min * 1000 + sec;
     818               0 :   break;
     819                 : 
     820                 : 
     821                 :   /* Convert packed degrees to radians */
     822                 :   /* --------------------------------- */
     823                 :     case HDFE_DMS_RAD:
     824               0 :   deg = inAngle / 1000000;
     825               0 :   min = (inAngle - deg * 1000000) / 1000;
     826               0 :   sec = (inAngle - deg * 1000000 - min * 1000);
     827               0 :   outAngle = deg + min / 60.0 + sec / 3600.0;
     828               0 :   outAngle = outAngle * DEGREES_TO_RADIANS;
     829                 :   break;
     830                 :     }
     831               0 :     return (outAngle);
     832                 : }
     833                 : 
     834                 : #undef TO_DEGREES
     835                 : #undef TO_RADIANS
     836                 : 
     837                 : 
     838                 : /*----------------------------------------------------------------------------|
     839                 : |  BEGIN_PROLOG                                                               |
     840                 : |                                                                             |
     841                 : |  FUNCTION: EHparsestr                                                       |
     842                 : |                                                                             |
     843                 : |  DESCRIPTION: String Parser Utility                                         |
     844                 : |                                                                             |
     845                 : |                                                                             |
     846                 : |  Return Value    Type     Units     Description                             |
     847                 : |  ============   ======  =========   =====================================   |
     848                 : |  count          int32               Number of string entries                |
     849                 : |                                                                             |
     850                 : |  INPUTS:                                                                    |
     851                 : |  instring       const char          Input string                            |
     852                 : |  delim          const char          string delimitor                        |
     853                 : |                                                                             |
     854                 : |  OUTPUTS:                                                                   |
     855                 : |  pntr           char *              Pointer array to beginning of each      |
     856                 : |                                     string entry                            |
     857                 : |  len            int32               Array of string entry lengths           |
     858                 : |                                                                             |
     859                 : |  NOTES:                                                                     |
     860                 : |                                                                             |
     861                 : |                                                                             |
     862                 : |   Date     Programmer   Description                                         |
     863                 : |  ======   ============  =================================================   |
     864                 : |  Jun 96   Joel Gales    Original Programmer                                 |
     865                 : |  Aug 96   Joel Gales    NULL pointer array returns count only               |
     866                 : |                                                                             |
     867                 : |  END_PROLOG                                                                 |
     868                 : -----------------------------------------------------------------------------*/
     869                 : int32
     870                 : EHparsestr(const char *instring, const char delim, char *pntr[], int32 len[])
     871             373 : {
     872                 :     int32           i;    /* Loop index */
     873             373 :     int32           prevDelimPos = 0; /* Previous delimitor position */
     874                 :     int32           count;  /* Number of elements in string list */
     875                 :     int32           slen; /* String length */
     876                 : 
     877                 :     char           *delimitor;  /* Pointer to delimitor */
     878                 : 
     879                 : 
     880                 :     /* Get length of input string list & Point to first delimitor */
     881                 :     /* ---------------------------------------------------------- */
     882             373 :     slen = strlen(instring);
     883             373 :     delimitor = strchr(instring, delim);
     884                 : 
     885                 :     /* If NULL string set count to zero otherwise set to 1 */
     886                 :     /* --------------------------------------------------- */
     887             373 :     count = (slen == 0) ? 0 : 1;
     888                 : 
     889                 : 
     890                 :     /* if string pointers are requested set first one to beginning of string */
     891                 :     /* --------------------------------------------------------------------- */
     892             373 :     if (&pntr[0] != NULL)
     893                 :     {
     894             250 :   pntr[0] = (char *)instring;
     895                 :     }
     896                 :     /* If delimitor not found ... */
     897                 :     /* -------------------------- */
     898             373 :     if (delimitor == NULL)
     899                 :     {
     900                 :   /* if string length requested then set to input string length */
     901                 :   /* ---------------------------------------------------------- */
     902             246 :   if (len != NULL)
     903                 :   {
     904             123 :       len[0] = slen;
     905                 :   }
     906                 :     } else
     907                 :   /* Delimitors Found */
     908                 :   /* ---------------- */
     909                 :     {
     910                 :   /* Loop through all characters in string */
     911                 :   /* ------------------------------------- */
     912            1655 :   for (i = 1; i < slen; i++)
     913                 :   {
     914                 :       /* If character is a delimitor ... */
     915                 :       /* ------------------------------- */
     916            1528 :       if (instring[i] == delim)
     917                 :       {
     918                 : 
     919                 :     /* If string pointer requested */
     920                 :     /* --------------------------- */
     921             127 :     if (&pntr[0] != NULL)
     922                 :     {
     923                 :         /* if requested then compute string length of entry */
     924                 :         /* ------------------------------------------------ */
     925             127 :         if (len != NULL)
     926                 :         {
     927             127 :       len[count - 1] = i - prevDelimPos;
     928                 :         }
     929                 :         /* Point to beginning of string entry */
     930                 :         /* ---------------------------------- */
     931             127 :         pntr[count] = (char *)instring + i + 1;
     932                 :     }
     933                 :     /* Reset previous delimitor position and increment counter */
     934                 :     /* ------------------------------------------------------- */
     935             127 :     prevDelimPos = i + 1;
     936             127 :     count++;
     937                 :       }
     938                 :   }
     939                 : 
     940                 :   /* Compute string length of last entry */
     941                 :   /* ----------------------------------- */
     942             127 :   if (&pntr[0] != NULL && len != NULL)
     943                 :   {
     944             127 :       len[count - 1] = i - prevDelimPos;
     945                 :   }
     946                 :     }
     947                 : 
     948             373 :     return (count);
     949                 : }
     950                 : 
     951                 : 
     952                 : 
     953                 : 
     954                 : /*----------------------------------------------------------------------------|
     955                 : |  BEGIN_PROLOG                                                               |
     956                 : |                                                                             |
     957                 : |  FUNCTION: EHstrwithin                                                      |
     958                 : |                                                                             |
     959                 : |  DESCRIPTION: Searchs for string within target string                       |
     960                 : |                                                                             |
     961                 : |                                                                             |
     962                 : |  Return Value    Type     Units     Description                             |
     963                 : |  ============   ======  =========   =====================================   |
     964                 : |  indx           int32               Element index (0 - based)               |
     965                 : |                                                                             |
     966                 : |  INPUTS:                                                                    |
     967                 : |  target         const char          Target string                           |
     968                 : |  search         const char          Search string                           |
     969                 : |  delim          const char          Delimitor                               |
     970                 : |                                                                             |
     971                 : |  OUTPUTS:                                                                   |
     972                 : |             None                                                            |
     973                 : |                                                                             |
     974                 : |  NOTES:                                                                     |
     975                 : |                                                                             |
     976                 : |                                                                             |
     977                 : |   Date     Programmer   Description                                         |
     978                 : |  ======   ============  =================================================   |
     979                 : |  Jun 96   Joel Gales    Original Programmer                                 |
     980                 : |  Jan 97   Joel Gales    Change ptr & slen to dynamic arrays                 |
     981                 : |                                                                             |
     982                 : |  END_PROLOG                                                                 |
     983                 : -----------------------------------------------------------------------------*/
     984                 : int32
     985                 : EHstrwithin(const char *target, const char *search, const char delim)
     986             123 : {
     987             123 :     intn            found = 0;  /* Target string found flag */
     988                 : 
     989                 :     int32           indx; /* Loop index */
     990                 :     int32           nentries; /* Number of entries in search string */
     991                 :     int32          *slen; /* Pointer to string length array */
     992                 : 
     993                 :     char          **ptr;  /* Pointer to string pointer array */
     994                 :     char            buffer[128];/* Buffer to hold "test" string entry */
     995                 : 
     996                 : 
     997                 :     /* Count number of entries in search string list */
     998                 :     /* --------------------------------------------- */
     999             123 :     nentries = EHparsestr(search, delim, NULL, NULL);
    1000                 : 
    1001                 : 
    1002                 :     /* Allocate string pointer and length arrays */
    1003                 :     /* ----------------------------------------- */
    1004             123 :     ptr = (char **) calloc(nentries, sizeof(char *));
    1005             123 :     if(ptr == NULL)
    1006                 :     { 
    1007               0 :   HEpush(DFE_NOSPACE,"EHstrwithin", __FILE__, __LINE__);
    1008               0 :   return(-1);
    1009                 :     }
    1010             123 :     slen = (int32 *) calloc(nentries, sizeof(int32));
    1011             123 :     if(slen == NULL)
    1012                 :     { 
    1013               0 :   HEpush(DFE_NOSPACE,"EHstrwithin", __FILE__, __LINE__);
    1014               0 :   free(ptr);
    1015               0 :   return(-1);
    1016                 :     }
    1017                 : 
    1018                 : 
    1019                 :     /* Parse search string */
    1020                 :     /* ------------------- */
    1021             123 :     nentries = EHparsestr(search, delim, ptr, slen);
    1022                 : 
    1023                 : 
    1024                 :     /* Loop through all elements in search string list */
    1025                 :     /* ----------------------------------------------- */
    1026             123 :     for (indx = 0; indx < nentries; indx++)
    1027                 :     {
    1028                 :   /* Copy string entry into buffer */
    1029                 :   /* ----------------------------- */
    1030             123 :   memcpy(buffer, ptr[indx], slen[indx]);
    1031             123 :   buffer[slen[indx]] = 0;
    1032                 : 
    1033                 : 
    1034                 :   /* Compare target string with string entry */
    1035                 :   /* --------------------------------------- */
    1036             123 :   if (strcmp(target, buffer) == 0)
    1037                 :   {
    1038             123 :       found = 1;
    1039             123 :       break;
    1040                 :   }
    1041                 :     }
    1042                 : 
    1043                 :     /* If not found set return to -1 */
    1044                 :     /* ----------------------------- */
    1045             123 :     if (found == 0)
    1046                 :     {
    1047               0 :   indx = -1;
    1048                 :     }
    1049             123 :     free(slen);
    1050             123 :     free(ptr);
    1051                 : 
    1052             123 :     return (indx);
    1053                 : }
    1054                 : 
    1055                 : 
    1056                 : 
    1057                 : 
    1058                 : 
    1059                 : /*----------------------------------------------------------------------------|
    1060                 : |  BEGIN_PROLOG                                                               |
    1061                 : |                                                                             |
    1062                 : |  FUNCTION: EHloadliststr                                                    |
    1063                 : |                                                                             |
    1064                 : |  DESCRIPTION: Builds list string from string array                          |
    1065                 : |                                                                             |
    1066                 : |                                                                             |
    1067                 : |  Return Value    Type     Units     Description                             |
    1068                 : |  ============   ======  =========   =====================================   |
    1069                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1070                 : |                                                                             |
    1071                 : |  INPUTS:                                                                    |
    1072                 : |  ptr            char                String pointer array                    |
    1073                 : |  nentries       int32               Number of string array elements         |
    1074                 : |  delim          char                Delimitor                               |
    1075                 : |                                                                             |
    1076                 : |  OUTPUTS:                                                                   |
    1077                 : |  liststr        char                Output list string                      |
    1078                 : |                                                                             |
    1079                 : |  NOTES:                                                                     |
    1080                 : |                                                                             |
    1081                 : |                                                                             |
    1082                 : |   Date     Programmer   Description                                         |
    1083                 : |  ======   ============  =================================================   |
    1084                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    1085                 : |                                                                             |
    1086                 : |  END_PROLOG                                                                 |
    1087                 : -----------------------------------------------------------------------------*/
    1088                 : intn
    1089                 : EHloadliststr(char *ptr[], int32 nentries, char *liststr, char delim)
    1090               0 : {
    1091               0 :     intn            status = 0; /* routine return status variable */
    1092                 : 
    1093                 :     int32           i;    /* Loop index */
    1094                 :     int32           slen; /* String entry length */
    1095               0 :     int32           off = 0;  /* Position of next entry along string list */
    1096                 :     char            dstr[2];    /* string version of input variable "delim" */
    1097                 : 
    1098               0 :     dstr[0] = delim;
    1099               0 :     dstr[1] = '\0';
    1100                 : 
    1101                 : 
    1102                 :     /* Loop through all entries in string array */
    1103                 :     /* ---------------------------------------- */
    1104               0 :     for (i = 0; i < nentries; i++)
    1105                 :     {
    1106                 :   /* Get string length of string array entry */
    1107                 :   /* --------------------------------------- */
    1108               0 :   slen = strlen(ptr[i]);
    1109                 : 
    1110                 : 
    1111                 :   /* Copy string entry to string list */
    1112                 :   /* -------------------------------- */
    1113               0 :   memcpy(liststr + off, ptr[i], slen + 1);
    1114                 : 
    1115                 : 
    1116                 :   /* Concatenate with delimitor */
    1117                 :   /* -------------------------- */
    1118               0 :   if (i != nentries - 1)
    1119                 :   {
    1120               0 :       strcat(liststr, dstr);
    1121                 :   }
    1122                 :   /* Get position of next entry for string list */
    1123                 :   /* ------------------------------------------ */
    1124               0 :   off += slen + 1;
    1125                 :     }
    1126                 : 
    1127               0 :     return (status);
    1128                 : }
    1129                 : 
    1130                 : 
    1131                 : 
    1132                 : 
    1133                 : 
    1134                 : /*----------------------------------------------------------------------------|
    1135                 : |  BEGIN_PROLOG                                                               |
    1136                 : |                                                                             |
    1137                 : |  FUNCTION: EHgetid                                                          |
    1138                 : |                                                                             |
    1139                 : |  DESCRIPTION: Get Vgroup/Vdata ID from name                                 |
    1140                 : |                                                                             |
    1141                 : |                                                                             |
    1142                 : |  Return Value    Type     Units     Description                             |
    1143                 : |  ============   ======  =========   =====================================   |
    1144                 : |  outID          int32               Output ID                               |
    1145                 : |                                                                             |
    1146                 : |  INPUTS:                                                                    |
    1147                 : |  fid            int32               HDF-EOS file ID                         |
    1148                 : |  vgid           int32               Vgroup ID                               |
    1149                 : |  objectname     const char          object name                             |
    1150                 : |  code           intn                object code (0 - Vgroup, 1 - Vdata)     |
    1151                 : |  access         const char          access ("w/r")                          |
    1152                 : |                                                                             |
    1153                 : |                                                                             |
    1154                 : |  OUTPUTS:                                                                   |
    1155                 : |             None                                                            |
    1156                 : |                                                                             |
    1157                 : |  NOTES:                                                                     |
    1158                 : |                                                                             |
    1159                 : |                                                                             |
    1160                 : |   Date     Programmer   Description                                         |
    1161                 : |  ======   ============  =================================================   |
    1162                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    1163                 : |                                                                             |
    1164                 : |  END_PROLOG                                                                 |
    1165                 : -----------------------------------------------------------------------------*/
    1166                 : int32
    1167                 : EHgetid(int32 fid, int32 vgid, const char *objectname, intn code,
    1168                 :         const char *access)
    1169               0 : {
    1170                 :     intn            i;    /* Loop index */
    1171                 : 
    1172                 :     int32           nObjects; /* # of objects in Vgroup */
    1173                 :     int32          *tags; /* Pnt to Vgroup object tags array */
    1174                 :     int32          *refs; /* Pnt to Vgroup object refs array */
    1175                 :     int32           id;   /* Object ID */
    1176               0 :     int32           outID = -1; /* Desired object ID */
    1177                 : 
    1178                 :     char            name[128];  /* Object name */
    1179                 : 
    1180                 : 
    1181                 :     /* Get Number of objects */
    1182                 :     /* --------------------- */
    1183               0 :     nObjects = Vntagrefs(vgid);
    1184                 : 
    1185                 :     /* If objects exist ... */
    1186                 :     /* -------------------- */
    1187               0 :     if (nObjects != 0)
    1188                 :     {
    1189                 : 
    1190                 :   /* Get tags and references of objects */
    1191                 :   /* ---------------------------------- */
    1192               0 :   tags = (int32 *) malloc(sizeof(int32) * nObjects);
    1193               0 :   if(tags == NULL)
    1194                 :   { 
    1195               0 :       HEpush(DFE_NOSPACE,"EHgetid", __FILE__, __LINE__);
    1196               0 :       return(-1);
    1197                 :   }
    1198               0 :   refs = (int32 *) malloc(sizeof(int32) * nObjects);
    1199               0 :   if(refs == NULL)
    1200                 :   { 
    1201               0 :       HEpush(DFE_NOSPACE,"EHgetid", __FILE__, __LINE__);
    1202               0 :       free(tags);
    1203               0 :       return(-1);
    1204                 :   }
    1205                 : 
    1206               0 :   Vgettagrefs(vgid, tags, refs, nObjects);
    1207                 : 
    1208                 : 
    1209                 :   /* Vgroup ID Section */
    1210                 :   /* ----------------- */
    1211               0 :   if (code == 0)
    1212                 :   {
    1213                 :       /* Loop through objects */
    1214                 :       /* -------------------- */
    1215               0 :       for (i = 0; i < nObjects; i++)
    1216                 :       {
    1217                 : 
    1218                 :     /* If object is Vgroup ... */
    1219                 :     /* ----------------------- */
    1220               0 :     if (*(tags + i) == DFTAG_VG)
    1221                 :     {
    1222                 : 
    1223                 :         /* Get ID and name */
    1224                 :         /* --------------- */
    1225               0 :         id = Vattach(fid, *(refs + i), access);
    1226               0 :         Vgetname(id, name);
    1227                 : 
    1228                 :         /* If name equals desired object name get ID */
    1229                 :         /* ----------------------------------------- */
    1230               0 :         if (strcmp(name, objectname) == 0)
    1231                 :         {
    1232               0 :       outID = id;
    1233               0 :       break;
    1234                 :         }
    1235                 :         /* If not desired object then detach */
    1236                 :         /* --------------------------------- */
    1237               0 :         Vdetach(id);
    1238                 :     }
    1239                 :       }
    1240               0 :   } else if (code == 1)
    1241                 :   {
    1242                 : 
    1243                 :       /* Loop through objects */
    1244                 :       /* -------------------- */
    1245               0 :       for (i = 0; i < nObjects; i++)
    1246                 :       {
    1247                 : 
    1248                 :     /* If object is Vdata ... */
    1249                 :     /* ---------------------- */
    1250               0 :     if (*(tags + i) == DFTAG_VH)
    1251                 :     {
    1252                 : 
    1253                 :         /* Get ID and name */
    1254                 :         /* --------------- */
    1255               0 :         id = VSattach(fid, *(refs + i), access);
    1256               0 :         VSgetname(id, name);
    1257                 : 
    1258                 :         /* If name equals desired object name get ID */
    1259                 :         /* ----------------------------------------- */
    1260               0 :         if (EHstrwithin(objectname, name, ',') != -1)
    1261                 :         {
    1262               0 :       outID = id;
    1263               0 :       break;
    1264                 :         }
    1265                 :         /* If not desired object then detach */
    1266                 :         /* --------------------------------- */
    1267               0 :         VSdetach(id);
    1268                 :     }
    1269                 :       }
    1270                 :   }
    1271               0 :   free(tags);
    1272               0 :   free(refs);
    1273                 :     }
    1274               0 :     return (outID);
    1275                 : }
    1276                 : 
    1277                 : 
    1278                 : 
    1279                 : 
    1280                 : 
    1281                 : /*----------------------------------------------------------------------------|
    1282                 : |  BEGIN_PROLOG                                                               |
    1283                 : |                                                                             |
    1284                 : |  FUNCTION: EHrevflds                                                        |
    1285                 : |                                                                             |
    1286                 : |  DESCRIPTION: Reverses elements in a string list                            |
    1287                 : |                                                                             |
    1288                 : |                                                                             |
    1289                 : |  Return Value    Type     Units     Description                             |
    1290                 : |  ============   ======  =========   =====================================   |
    1291                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1292                 : |                                                                             |
    1293                 : |  INPUTS:                                                                    |
    1294                 : |  dimlist        char                Original dimension list                 |
    1295                 : |                                                                             |
    1296                 : |  OUTPUTS:                                                                   |
    1297                 : |  revdimlist     char                Reversed dimension list                 |
    1298                 : |                                                                             |
    1299                 : |  NOTES:                                                                     |
    1300                 : |                                                                             |
    1301                 : |                                                                             |
    1302                 : |   Date     Programmer   Description                                         |
    1303                 : |  ======   ============  =================================================   |
    1304                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    1305                 : |                                                                             |
    1306                 : |  END_PROLOG                                                                 |
    1307                 : -----------------------------------------------------------------------------*/
    1308                 : intn
    1309                 : EHrevflds(char *dimlist, char *revdimlist)
    1310               0 : {
    1311               0 :     intn            status = 0; /* routine return status variable */
    1312                 : 
    1313                 :     int32           indx; /* Loop index */
    1314                 :     int32           nentries; /* Number of entries in search string */
    1315                 :     int32          *slen; /* Pointer to string length array */
    1316                 : 
    1317                 :     char          **ptr;  /* Pointer to string pointer array */
    1318                 :     char           *tempPtr;  /* Temporary string pointer */
    1319                 :     char           *tempdimlist;/* Temporary dimension list */
    1320                 : 
    1321                 : 
    1322                 :     /* Copy dimlist into temp dimlist */
    1323                 :     /* ------------------------------ */
    1324               0 :     tempdimlist = (char *) malloc(strlen(dimlist) + 1);
    1325               0 :     if(tempdimlist == NULL)
    1326                 :     { 
    1327               0 :   HEpush(DFE_NOSPACE,"EHrevflds", __FILE__, __LINE__);
    1328               0 :   return(-1);
    1329                 :     }
    1330               0 :     strcpy(tempdimlist, dimlist);
    1331                 : 
    1332                 : 
    1333                 :     /* Count number of entries in search string list */
    1334                 :     /* --------------------------------------------- */
    1335               0 :     nentries = EHparsestr(tempdimlist, ',', NULL, NULL);
    1336                 : 
    1337                 : 
    1338                 :     /* Allocate string pointer and length arrays */
    1339                 :     /* ----------------------------------------- */
    1340               0 :     ptr = (char **) calloc(nentries, sizeof(char *));
    1341               0 :     if(ptr == NULL)
    1342                 :     { 
    1343               0 :   HEpush(DFE_NOSPACE,"EHrevflds", __FILE__, __LINE__);
    1344               0 :   free(tempdimlist);
    1345               0 :   return(-1);
    1346                 :     }
    1347               0 :     slen = (int32 *) calloc(nentries, sizeof(int32));
    1348               0 :     if(slen == NULL)
    1349                 :     { 
    1350               0 :   HEpush(DFE_NOSPACE,"EHrevflds", __FILE__, __LINE__);
    1351               0 :   free(ptr);
    1352               0 :   free(tempdimlist);
    1353               0 :   return(-1);
    1354                 :     }
    1355                 : 
    1356                 : 
    1357                 :     /* Parse search string */
    1358                 :     /* ------------------- */
    1359               0 :     nentries = EHparsestr(tempdimlist, ',', ptr, slen);
    1360                 : 
    1361                 : 
    1362                 :     /* Reverse entries in string pointer array */
    1363                 :     /* --------------------------------------- */
    1364               0 :     for (indx = 0; indx < nentries / 2; indx++)
    1365                 :     {
    1366               0 :   tempPtr = ptr[indx];
    1367               0 :   ptr[indx] = ptr[nentries - 1 - indx];
    1368               0 :   ptr[nentries - 1 - indx] = tempPtr;
    1369                 :     }
    1370                 : 
    1371                 : 
    1372                 :     /* Replace comma delimitors by nulls */
    1373                 :     /* --------------------------------- */
    1374               0 :     for (indx = 0; indx < nentries - 1; indx++)
    1375                 :     {
    1376               0 :   *(ptr[indx] - 1) = 0;
    1377                 :     }
    1378                 : 
    1379                 : 
    1380                 :     /* Build new string list */
    1381                 :     /* --------------------- */
    1382               0 :     status = EHloadliststr(ptr, nentries, revdimlist, ',');
    1383                 : 
    1384                 : 
    1385               0 :     free(slen);
    1386               0 :     free(ptr);
    1387               0 :     free(tempdimlist);
    1388                 : 
    1389               0 :     return (status);
    1390                 : }
    1391                 : 
    1392                 : 
    1393                 : /*----------------------------------------------------------------------------|
    1394                 : |  BEGIN_PROLOG                                                               |
    1395                 : |                                                                             |
    1396                 : |  FUNCTION: EHcntOBJECT                                                      |
    1397                 : |                                                                             |
    1398                 : |  DESCRIPTION: Determines number of OBJECTs in metadata GROUP                |
    1399                 : |                                                                             |
    1400                 : |                                                                             |
    1401                 : |  Return Value    Type     Units     Description                             |
    1402                 : |  ============   ======  =========   =====================================   |
    1403                 : |  count          int32               Number of OBJECTs in GROUP              |
    1404                 : |                                                                             |
    1405                 : |  INPUTS:                                                                    |
    1406                 : |  metabur        char                Begin & end metadata pointer array      |
    1407                 : |                                                                             |
    1408                 : |  OUTPUTS:                                                                   |
    1409                 : |             None                                                            |
    1410                 : |                                                                             |
    1411                 : |  NOTES:                                                                     |
    1412                 : |                                                                             |
    1413                 : |                                                                             |
    1414                 : |   Date     Programmer   Description                                         |
    1415                 : |  ======   ============  =================================================   |
    1416                 : |  Sep 96   Joel Gales    Original Programmer                                 |
    1417                 : |                                                                             |
    1418                 : |  END_PROLOG                                                                 |
    1419                 : -----------------------------------------------------------------------------*/
    1420                 : int32
    1421                 : EHcntOBJECT(char *metabuf[])
    1422               0 : {
    1423               0 :     int32           count = 0;  /* Counter */
    1424                 : 
    1425                 :     char           *metaptr;  /* Beginning of metadata section */
    1426                 :     char           *endptr; /* End of metadata section */
    1427                 :     char           *tempptr;  /* Pointer within metadata section */
    1428                 : 
    1429                 : 
    1430                 :     /* Get Pointers to beginning and ending of metadata section */
    1431                 :     /* -------------------------------------------------------- */
    1432               0 :     metaptr = metabuf[0];
    1433               0 :     endptr = metabuf[1];
    1434                 : 
    1435                 : 
    1436                 :     /* Find number of "END_OBJECT" strings within section */
    1437                 :     /* -------------------------------------------------- */
    1438               0 :     tempptr = metaptr;
    1439               0 :     while (tempptr < endptr && tempptr != NULL)
    1440                 :     {
    1441               0 :   tempptr = strstr(tempptr + 1, "END_OBJECT");
    1442               0 :   count++;
    1443                 :     }
    1444               0 :     count--;
    1445                 : 
    1446               0 :     return (count);
    1447                 : }
    1448                 : 
    1449                 : 
    1450                 : 
    1451                 : 
    1452                 : 
    1453                 : /*----------------------------------------------------------------------------|
    1454                 : |  BEGIN_PROLOG                                                               |
    1455                 : |                                                                             |
    1456                 : |  FUNCTION: EHcntGROUP                                                       |
    1457                 : |                                                                             |
    1458                 : |  DESCRIPTION: Determines number of GROUPs in metadata GROUP                 |
    1459                 : |                                                                             |
    1460                 : |                                                                             |
    1461                 : |  Return Value    Type     Units     Description                             |
    1462                 : |  ============   ======  =========   =====================================   |
    1463                 : |  count          int32               Number of GROUPs in GROUP               |
    1464                 : |                                                                             |
    1465                 : |  INPUTS:                                                                    |
    1466                 : |  metabur        char                Begin & end metadata pointer array      |
    1467                 : |                                                                             |
    1468                 : |  OUTPUTS:                                                                   |
    1469                 : |             None                                                            |
    1470                 : |                                                                             |
    1471                 : |  NOTES:                                                                     |
    1472                 : |                                                                             |
    1473                 : |                                                                             |
    1474                 : |   Date     Programmer   Description                                         |
    1475                 : |  ======   ============  =================================================   |
    1476                 : |  Sep 96   Joel Gales    Original Programmer                                 |
    1477                 : |                                                                             |
    1478                 : |  END_PROLOG                                                                 |
    1479                 : -----------------------------------------------------------------------------*/
    1480                 : int32
    1481                 : EHcntGROUP(char *metabuf[])
    1482               0 : {
    1483               0 :     int32           count = 0;  /* Counter */
    1484                 : 
    1485                 :     char           *metaptr;  /* Beginning of metadata section */
    1486                 :     char           *endptr; /* End of metadata section */
    1487                 :     char           *tempptr;  /* Pointer within metadata section */
    1488                 : 
    1489                 : 
    1490                 :     /* Get Pointers to beginning and ending of metadata section */
    1491                 :     /* -------------------------------------------------------- */
    1492               0 :     metaptr = metabuf[0];
    1493               0 :     endptr = metabuf[1];
    1494                 : 
    1495                 : 
    1496                 :     /* Find number of "END_GROUP" strings within section */
    1497                 :     /* ------------------------------------------------- */
    1498               0 :     tempptr = metaptr;
    1499               0 :     while (tempptr < endptr && tempptr != NULL)
    1500                 :     {
    1501               0 :   tempptr = strstr(tempptr + 1, "END_GROUP");
    1502               0 :   count++;
    1503                 :     }
    1504               0 :     count--;
    1505                 : 
    1506               0 :     return (count);
    1507                 : }
    1508                 : 
    1509                 : 
    1510                 : 
    1511                 : 
    1512                 : /*----------------------------------------------------------------------------|
    1513                 : |  BEGIN_PROLOG                                                               |
    1514                 : |                                                                             |
    1515                 : |  FUNCTION: EHmetalist                                                       |
    1516                 : |                                                                             |
    1517                 : |  DESCRIPTION: Converts string list to metadata list                         |
    1518                 : |                                                                             |
    1519                 : |                                                                             |
    1520                 : |  Return Value    Type     Units     Description                             |
    1521                 : |  ============   ======  =========   =====================================   |
    1522                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1523                 : |                                                                             |
    1524                 : |  INPUTS:                                                                    |
    1525                 : |  instring       char                Input string list                       |
    1526                 : |                                                                             |
    1527                 : |  OUTPUTS:                                                                   |
    1528                 : |  outstring      char                Output metadata string                  |
    1529                 : |                                                                             |
    1530                 : |  NOTES:                                                                     |
    1531                 : |                                                                             |
    1532                 : |                                                                             |
    1533                 : |   Date     Programmer   Description                                         |
    1534                 : |  ======   ============  =================================================   |
    1535                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    1536                 : |                                                                             |
    1537                 : |  END_PROLOG                                                                 |
    1538                 : -----------------------------------------------------------------------------*/
    1539                 : intn
    1540                 : EHmetalist(char *instring, char *outstring)
    1541               0 : {
    1542                 :     intn            i;    /* Loop index */
    1543               0 :     intn            status = 0; /* routine return status variable */
    1544                 : 
    1545                 :     int32           nentries; /* Number of entries in search string */
    1546               0 :     int32           listlen = 1;/* String list length */
    1547                 :     int32          *slen; /* Pointer to string length array */
    1548                 : 
    1549                 :     char          **ptr;  /* Pointer to string pointer array */
    1550                 : 
    1551                 : 
    1552                 :     /* Count number of entries in search string list */
    1553                 :     /* --------------------------------------------- */
    1554               0 :     nentries = EHparsestr(instring, ',', NULL, NULL);
    1555                 : 
    1556                 : 
    1557                 :     /* Allocate string pointer and length arrays */
    1558                 :     /* ----------------------------------------- */
    1559               0 :     ptr = (char **) calloc(nentries, sizeof(char *));
    1560               0 :     if(ptr == NULL)
    1561                 :     { 
    1562               0 :   HEpush(DFE_NOSPACE,"EHmetalist", __FILE__, __LINE__);
    1563               0 :   return(-1);
    1564                 :     }
    1565               0 :     slen = (int32 *) calloc(nentries, sizeof(int32));
    1566               0 :     if(slen == NULL)
    1567                 :     { 
    1568               0 :   HEpush(DFE_NOSPACE,"EHmetalist", __FILE__, __LINE__);
    1569               0 :   free(ptr);
    1570               0 :   return(-1);
    1571                 :     }
    1572                 : 
    1573                 : 
    1574                 :     /* Parse input string */
    1575                 :     /* ------------------ */
    1576               0 :     nentries = EHparsestr(instring, ',', ptr, slen);
    1577                 : 
    1578                 : 
    1579                 :     /* Start output string with leading "(" */
    1580                 :     /* ------------------------------------ */
    1581               0 :     strcpy(outstring, "(");
    1582                 : 
    1583                 : 
    1584                 :     /* Loop through all entries */
    1585                 :     /* ------------------------ */
    1586               0 :     for (i = 0; i < nentries; i++)
    1587                 :     {
    1588                 :   /* Add double quote (") to output string */
    1589                 :   /* ------------------------------------- */
    1590               0 :   strcat(outstring, "\"");
    1591               0 :   listlen++;
    1592                 : 
    1593                 :   /* Add input string entry to output string */
    1594                 :   /* --------------------------------------- */
    1595               0 :   memcpy(outstring + listlen, ptr[i], slen[i]);
    1596               0 :   listlen += slen[i];
    1597               0 :   outstring[listlen] = 0;
    1598                 : 
    1599                 : 
    1600                 :   /* Add closing double quote (") to output string */
    1601                 :   /* --------------------------------------------- */
    1602               0 :   strcat(outstring, "\"");
    1603               0 :   listlen++;
    1604               0 :   outstring[listlen] = 0;
    1605                 : 
    1606                 : 
    1607                 :   /* Add comma delimitor to output string */
    1608                 :   /* ------------------------------------ */
    1609               0 :   if (i != (nentries - 1))
    1610                 :   {
    1611               0 :       strcat(outstring, ",");
    1612               0 :       listlen++;
    1613                 :   }
    1614                 :   /* Place null terminator in output string */
    1615                 :   /* -------------------------------------- */
    1616               0 :   outstring[listlen] = 0;
    1617                 :     }
    1618                 : 
    1619                 : 
    1620                 :     /* End output string with trailing ")" */
    1621                 :     /* ----------------------------------- */
    1622               0 :     strcat(outstring, ")");
    1623                 : 
    1624               0 :     free(ptr);
    1625               0 :     free(slen);
    1626                 : 
    1627               0 :     return (status);
    1628                 : }
    1629                 : 
    1630                 : 
    1631                 : 
    1632                 : 
    1633                 : 
    1634                 : /*----------------------------------------------------------------------------|
    1635                 : |  BEGIN_PROLOG                                                               |
    1636                 : |                                                                             |
    1637                 : |  FUNCTION: EHinsertmeta                                                     |
    1638                 : |                                                                             |
    1639                 : |  DESCRIPTION: Writes metadata                                               |
    1640                 : |                                                                             |
    1641                 : |                                                                             |
    1642                 : |  Return Value    Type     Units     Description                             |
    1643                 : |  ============   ======  =========   =====================================   |
    1644                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1645                 : |                                                                             |
    1646                 : |  INPUTS:                                                                    |
    1647                 : |  sdInterfaceID  int32               SDS interface ID                        |
    1648                 : |  structname     char                HDF-EOS structure name                  |
    1649                 : |  structcode     char                Structure code ("s/g/p")                |
    1650                 : |  metacode       int32               Metadata code type                      |
    1651                 : |  metastr        char                Metadata input string                   |
    1652                 : |  metadata       int32               Metadata utility array                  |
    1653                 : |                                                                             |
    1654                 : |  OUTPUTS:                                                                   |
    1655                 : |             None                                                            |
    1656                 : |                                                                             |
    1657                 : |  NOTES:                                                                     |
    1658                 : |                                                                             |
    1659                 : |                                                                             |
    1660                 : |   Date     Programmer   Description                                         |
    1661                 : |  ======   ============  =================================================   |
    1662                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    1663                 : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    1664                 : |  Sep 96   Joel Gales    Allow new metadata object to be written in          |
    1665                 : |                         old metadata.                                       |
    1666                 : |  Dec 96   Joel Gales    Fix Point metadata problem                          |
    1667                 : |  Oct 98   David Wynne   Change utlstr/utlstr2 to dynamic allocation from    |
    1668                 : |                         static                                              |
    1669                 : |                                                                             |
    1670                 : |  END_PROLOG                                                                 |
    1671                 : -----------------------------------------------------------------------------*/
    1672                 : intn
    1673                 : EHinsertmeta(int32 sdInterfaceID, char *structname, char *structcode,
    1674                 :        int32 metacode, char *metastr, int32 metadata[])
    1675               0 : {
    1676                 :     intn            i;    /* Loop index */
    1677               0 :     intn            status = 0; /* routine return status variable */
    1678                 : 
    1679                 :     int32           attrIndex;  /* Structural metadata attribute index */
    1680                 :     int32           slen[8];  /* String length array (for dim map parsing) */
    1681                 :     int32           nmeta;  /* Number of 32000 byte metadata sections */
    1682                 :     int32           metalen;  /* Length of structural metadata */
    1683                 :     int32           seglen; /* Length of metadata string to insert */
    1684                 :     int32           count;  /* Objects/Groups counter */
    1685                 :     int32           offset; /* Offset insertion position of new metadata
    1686                 :          * section within existing metadata */
    1687                 : 
    1688                 :     char           *metabuf;  /* Pointer (handle) to structural metadata */
    1689                 :     char           *begptr; /* Pointer to beginning of metadata section */
    1690                 :     char           *metaptr;  /* Metadata pointer */
    1691                 :     char           *prevmetaptr;/* Previous position of metadata pointer */
    1692                 :     char           *ptr[8]; /* String pointer array (for dim map parsing) */
    1693                 :     char            type[32]; /* Number type descriptor string */
    1694                 :     char           *metaArr[2]; /* Array of metadata positions */
    1695                 :     char           *colon;  /* Colon position */
    1696                 :     char           *colon2; /* 2nd colon position */
    1697                 :     char           *slash;  /* Slash postion */
    1698                 :     char           *utlstr; /* Utility string */
    1699                 :     char           *utlstr2;  /* Utility string 2 */
    1700                 : 
    1701                 : 
    1702                 :     /* Allocate space for utility strings */
    1703                 :     /* ---------------------------------- */
    1704               0 :     utlstr = (char *) calloc(UTLSTRSIZE, sizeof(char));
    1705               0 :     if(utlstr == NULL)
    1706                 :     { 
    1707               0 :   HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__);
    1708               0 :   return(-1);
    1709                 :     }
    1710                 : 
    1711               0 :     utlstr2 = (char *) calloc(UTLSTRSIZE, sizeof(char));
    1712               0 :     if(utlstr2 == NULL)
    1713                 :     { 
    1714               0 :   HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__);
    1715               0 :   free(utlstr);
    1716               0 :   return(-1);
    1717                 :     }
    1718                 : 
    1719                 :     /* Determine number of structural metadata "sections" */
    1720                 :     /* -------------------------------------------------- */
    1721               0 :     nmeta = 0;
    1722                 :     while (1)
    1723                 :     {
    1724                 :   /* Search for "StructMetadata.x" attribute */
    1725                 :   /* --------------------------------------- */
    1726               0 :   sprintf(utlstr, "%s%d", "StructMetadata.", (int)nmeta);
    1727               0 :   attrIndex = SDfindattr(sdInterfaceID, utlstr);
    1728                 : 
    1729                 : 
    1730                 :   /* If found then increment metadata section counter else exit loop */
    1731                 :   /* --------------------------------------------------------------- */
    1732               0 :   if (attrIndex != -1)
    1733                 :   {
    1734               0 :       nmeta++;
    1735                 :   } else
    1736                 :   {
    1737               0 :       break;
    1738                 :   }
    1739               0 :     }
    1740                 : 
    1741                 : 
    1742                 :     /* Allocate space for metadata (in units of 32000 bytes) */
    1743                 :     /* ----------------------------------------------------- */
    1744               0 :     metabuf = (char *) calloc(32000 * nmeta, 1);
    1745               0 :     if(metabuf == NULL)
    1746                 :     { 
    1747               0 :   HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__);
    1748               0 :   free(utlstr);
    1749               0 :   free(utlstr2);
    1750               0 :   return(-1);
    1751                 :     }
    1752                 : 
    1753                 : 
    1754                 :     /* Read structural metadata */
    1755                 :     /* ------------------------ */
    1756               0 :     for (i = 0; i < nmeta; i++)
    1757                 :     {
    1758               0 :   sprintf(utlstr, "%s%d", "StructMetadata.", i);
    1759               0 :   attrIndex = SDfindattr(sdInterfaceID, utlstr);
    1760               0 :   metalen = strlen(metabuf);
    1761               0 :   SDreadattr(sdInterfaceID, attrIndex, metabuf + metalen);
    1762                 :     }
    1763                 : 
    1764                 :     /* Determine length (# of characters) of metadata */
    1765                 :     /* ---------------------------------------------- */
    1766               0 :     metalen = strlen(metabuf);
    1767                 : 
    1768                 : 
    1769                 : 
    1770                 :     /* Find HDF-EOS structure "root" group in metadata */
    1771                 :     /* ----------------------------------------------- */
    1772                 : 
    1773                 :     /* Setup proper search string */
    1774                 :     /* -------------------------- */
    1775               0 :     if (strcmp(structcode, "s") == 0)
    1776                 :     {
    1777               0 :   strcpy(utlstr, "GROUP=SwathStructure");
    1778               0 :     } else if (strcmp(structcode, "g") == 0)
    1779                 :     {
    1780               0 :   strcpy(utlstr, "GROUP=GridStructure");
    1781               0 :     } else if (strcmp(structcode, "p") == 0)
    1782                 :     {
    1783               0 :   strcpy(utlstr, "GROUP=PointStructure");
    1784                 :     }
    1785                 :     /* Use string search routine (strstr) to move through metadata */
    1786                 :     /* ----------------------------------------------------------- */
    1787               0 :     metaptr = strstr(metabuf, utlstr);
    1788                 : 
    1789                 : 
    1790                 : 
    1791                 :     /* Find specific (named) structure */
    1792                 :     /* ------------------------------- */
    1793               0 :     if (metacode < 1000)
    1794                 :     {
    1795                 :   /* Save current metadata pointer */
    1796                 :   /* ----------------------------- */
    1797               0 :   prevmetaptr = metaptr;
    1798                 : 
    1799                 : 
    1800                 :   /* First loop for "old-style" (non-ODL) metadata string */
    1801                 :   /* ---------------------------------------------------- */
    1802               0 :   if (strcmp(structcode, "s") == 0)
    1803                 :   {
    1804               0 :       sprintf(utlstr, "%s%s", "SwathName=\"", structname);
    1805               0 :   } else if (strcmp(structcode, "g") == 0)
    1806                 :   {
    1807               0 :       sprintf(utlstr, "%s%s", "GridName=\"", structname);
    1808               0 :   } else if (strcmp(structcode, "p") == 0)
    1809                 :   {
    1810               0 :       sprintf(utlstr, "%s%s", "PointName=\"", structname);
    1811                 :   }
    1812                 :   /* Do string search */
    1813                 :   /* ---------------- */
    1814               0 :   metaptr = strstr(metaptr, utlstr);
    1815                 : 
    1816                 : 
    1817                 :   /*
    1818                 :    * If not found then return to previous position in metadata and look
    1819                 :    * for "new-style" (ODL) metadata string
    1820                 :    */
    1821               0 :   if (metaptr == NULL)
    1822                 :   {
    1823               0 :       sprintf(utlstr, "%s%s", "GROUP=\"", structname);
    1824               0 :       metaptr = strstr(prevmetaptr, utlstr);
    1825                 :   }
    1826                 :     }
    1827                 :     /*
    1828                 :      * If searching for geo fields (3), data fields (4), or point fields (11)
    1829                 :      * convert type code to string designator.
    1830                 :      */
    1831               0 :     if (metacode == 3 || metacode == 4 || metacode == 11)
    1832                 :     {
    1833               0 :   switch (metadata[0])
    1834                 :   {
    1835                 :   case 3:
    1836               0 :       strcpy(type, "DFNT_UCHAR8");
    1837               0 :       break;
    1838                 :   case 4:
    1839               0 :       strcpy(type, "DFNT_CHAR8");
    1840               0 :       break;
    1841                 :   case 5:
    1842               0 :       strcpy(type, "DFNT_FLOAT32");
    1843               0 :       break;
    1844                 :   case 6:
    1845               0 :       strcpy(type, "DFNT_FLOAT64");
    1846               0 :       break;
    1847                 :   case 20:
    1848               0 :       strcpy(type, "DFNT_INT8");
    1849               0 :       break;
    1850                 :   case 21:
    1851               0 :       strcpy(type, "DFNT_UINT8");
    1852               0 :       break;
    1853                 :   case 22:
    1854               0 :       strcpy(type, "DFNT_INT16");
    1855               0 :       break;
    1856                 :   case 23:
    1857               0 :       strcpy(type, "DFNT_UINT16");
    1858               0 :       break;
    1859                 :   case 24:
    1860               0 :       strcpy(type, "DFNT_INT32");
    1861               0 :       break;
    1862                 :   case 25:
    1863               0 :       strcpy(type, "DFNT_UINT32");
    1864                 :       break;
    1865                 :   }
    1866                 :     }
    1867                 :     /* Metadata Section Switch */
    1868                 :     /* ----------------------- */
    1869               0 :     switch (abs(metacode))
    1870                 :     {
    1871                 : 
    1872                 :     case 0:
    1873                 :   /* Dimension Section */
    1874                 :   /* ----------------- */
    1875                 : 
    1876                 :   /* Find beginning and ending of metadata section */
    1877                 :   /* --------------------------------------------- */
    1878               0 :   strcpy(utlstr, "\t\tGROUP=Dimension");
    1879               0 :   begptr = strstr(metaptr, utlstr);
    1880                 : 
    1881               0 :   strcpy(utlstr, "\t\tEND_GROUP=Dimension");
    1882               0 :   metaptr = strstr(metaptr, utlstr);
    1883                 : 
    1884                 : 
    1885                 :   /* Count number of existing entries and increment */
    1886                 :   /* ---------------------------------------------- */
    1887               0 :   metaArr[0] = begptr;
    1888               0 :   metaArr[1] = metaptr;
    1889               0 :   count = EHcntOBJECT(metaArr) + 1;
    1890                 : 
    1891                 : 
    1892                 :   /* Build metadata entry string */
    1893                 :   /* --------------------------- */
    1894               0 :   sprintf(utlstr, "%s%d%s%s%s%d%s%d%s",
    1895                 :                 "\t\t\tOBJECT=Dimension_", (int)count,
    1896                 :     "\n\t\t\t\tDimensionName=\"", &metastr[0],
    1897                 :     "\"\n\t\t\t\tSize=", (int)metadata[0],
    1898                 :     "\n\t\t\tEND_OBJECT=Dimension_", (int)count, "\n");
    1899               0 :   break;
    1900                 : 
    1901                 : 
    1902                 :     case 1:
    1903                 :   /* Dimension Map Section */
    1904                 :   /* --------------------- */
    1905                 : 
    1906                 :   /* Find beginning and ending of metadata section */
    1907                 :   /* --------------------------------------------- */
    1908               0 :   strcpy(utlstr, "\t\tGROUP=DimensionMap");
    1909               0 :   begptr = strstr(metaptr, utlstr);
    1910                 : 
    1911               0 :   strcpy(utlstr, "\t\tEND_GROUP=DimensionMap");
    1912               0 :   metaptr = strstr(metaptr, utlstr);
    1913                 : 
    1914                 : 
    1915                 :   /* Count number of existing entries and increment */
    1916                 :   /* ---------------------------------------------- */
    1917               0 :   metaArr[0] = begptr;
    1918               0 :   metaArr[1] = metaptr;
    1919               0 :   count = EHcntOBJECT(metaArr) + 1;
    1920                 : 
    1921                 : 
    1922                 :   /* Find slash within input mapping string and replace with NULL */
    1923                 :   /* ------------------------------------------------------------ */
    1924               0 :   EHparsestr(metastr, '/', ptr, slen);
    1925               0 :   metastr[slen[0]] = 0;
    1926                 : 
    1927                 : 
    1928                 :   /* Build metadata entry string */
    1929                 :   /* --------------------------- */
    1930               0 :   sprintf(utlstr, "%s%d%s%s%s%s%s%d%s%d%s%d%s",
    1931                 :     "\t\t\tOBJECT=DimensionMap_", (int)count,
    1932                 :     "\n\t\t\t\tGeoDimension=\"", &metastr[0],
    1933                 :     "\"\n\t\t\t\tDataDimension=\"", &metastr[slen[0] + 1],
    1934                 :     "\"\n\t\t\t\tOffset=", (int)metadata[0],
    1935                 :     "\n\t\t\t\tIncrement=", (int)metadata[1],
    1936                 :     "\n\t\t\tEND_OBJECT=DimensionMap_", (int)count, "\n");
    1937               0 :   break;
    1938                 : 
    1939                 : 
    1940                 :     case 2:
    1941                 :   /* Index Dimension Map Section */
    1942                 :   /* --------------------------- */
    1943                 : 
    1944                 :   /* Find beginning and ending of metadata section */
    1945                 :   /* --------------------------------------------- */
    1946               0 :   strcpy(utlstr, "\t\tGROUP=IndexDimensionMap");
    1947               0 :   begptr = strstr(metaptr, utlstr);
    1948                 : 
    1949               0 :   strcpy(utlstr, "\t\tEND_GROUP=IndexDimensionMap");
    1950               0 :   metaptr = strstr(metaptr, utlstr);
    1951                 : 
    1952                 : 
    1953                 :   /* Count number of existing entries and increment */
    1954                 :   /* ---------------------------------------------- */
    1955               0 :   metaArr[0] = begptr;
    1956               0 :   metaArr[1] = metaptr;
    1957               0 :   count = EHcntOBJECT(metaArr) + 1;
    1958                 : 
    1959                 : 
    1960                 :   /* Find slash within input mapping string and replace with NULL */
    1961                 :   /* ------------------------------------------------------------ */
    1962               0 :   EHparsestr(metastr, '/', ptr, slen);
    1963               0 :   metastr[slen[0]] = 0;
    1964                 : 
    1965                 : 
    1966                 :   /* Build metadata entry string */
    1967                 :   /* --------------------------- */
    1968               0 :   sprintf(utlstr, "%s%d%s%s%s%s%s%d%s",
    1969                 :     "\t\t\tOBJECT=IndexDimensionMap_", (int)count,
    1970                 :     "\n\t\t\t\tGeoDimension=\"", &metastr[0],
    1971                 :     "\"\n\t\t\t\tDataDimension=\"", &metastr[slen[0] + 1],
    1972                 :     "\"\n\t\t\tEND_OBJECT=IndexDimensionMap_", (int)count, "\n");
    1973               0 :   break;
    1974                 : 
    1975                 : 
    1976                 :     case 3:
    1977                 :   /* Geolocation Field Section */
    1978                 :   /* ------------------------- */
    1979                 : 
    1980                 :   /* Find beginning and ending of metadata section */
    1981                 :   /* --------------------------------------------- */
    1982               0 :   strcpy(utlstr, "\t\tGROUP=GeoField");
    1983               0 :   begptr = strstr(metaptr, utlstr);
    1984                 : 
    1985               0 :   strcpy(utlstr, "\t\tEND_GROUP=GeoField");
    1986               0 :   metaptr = strstr(metaptr, utlstr);
    1987                 : 
    1988                 : 
    1989                 :   /* Count number of existing entries and increment */
    1990                 :   /* ---------------------------------------------- */
    1991               0 :   metaArr[0] = begptr;
    1992               0 :   metaArr[1] = metaptr;
    1993               0 :   count = EHcntOBJECT(metaArr) + 1;
    1994                 : 
    1995                 : 
    1996                 :   /* Find colon (parse off field name) */
    1997                 :   /* --------------------------------- */
    1998               0 :   colon = strchr(metastr, ':');
    1999               0 :   *colon = 0;
    2000                 : 
    2001                 : 
    2002                 :   /* Search for next colon (compression and/or tiling parameters) */
    2003                 :   /* ------------------------------------------------------------ */
    2004               0 :   colon2 = strchr(colon + 1, ':');
    2005               0 :   if (colon2 != NULL)
    2006                 :   {
    2007               0 :       *colon2 = 0;
    2008                 :   }
    2009                 :   /* Make metadata string list for dimension list */
    2010                 :   /* -------------------------------------------- */
    2011               0 :   EHmetalist(colon + 1, utlstr2);
    2012                 : 
    2013                 : 
    2014                 :   /* Build metadata entry string */
    2015                 :   /* --------------------------- */
    2016               0 :   sprintf(utlstr, "%s%d%s%s%s%s%s%s",
    2017                 :     "\t\t\tOBJECT=GeoField_", (int)count,
    2018                 :     "\n\t\t\t\tGeoFieldName=\"", metastr,
    2019                 :     "\"\n\t\t\t\tDataType=", type,
    2020                 :     "\n\t\t\t\tDimList=", utlstr2);
    2021                 : 
    2022                 : 
    2023                 :   /* If compression and/or tiling parameters add to string */
    2024                 :   /* ----------------------------------------------------- */
    2025               0 :   if (colon2 != NULL)
    2026                 :   {
    2027               0 :       strcat(utlstr, colon2 + 1);
    2028                 :   }
    2029                 :   /* Add END_OBJECT terminator to metadata string */
    2030                 :   /* -------------------------------------------- */
    2031               0 :   sprintf(utlstr2, "%s%d%s",
    2032                 :     "\n\t\t\tEND_OBJECT=GeoField_", (int)count, "\n");
    2033               0 :   strcat(utlstr, utlstr2);
    2034                 : 
    2035               0 :   break;
    2036                 : 
    2037                 : 
    2038                 :     case 4:
    2039                 :   /* Data Field Section */
    2040                 :   /* ------------------ */
    2041                 : 
    2042                 :   /* Find beginning and ending of metadata section */
    2043                 :   /* --------------------------------------------- */
    2044               0 :   strcpy(utlstr, "\t\tGROUP=DataField");
    2045               0 :   begptr = strstr(metaptr, utlstr);
    2046                 : 
    2047               0 :   strcpy(utlstr, "\t\tEND_GROUP=DataField");
    2048               0 :   metaptr = strstr(metaptr, utlstr);
    2049                 : 
    2050                 : 
    2051                 :   /* Count number of existing entries and increment */
    2052                 :   /* ---------------------------------------------- */
    2053               0 :   metaArr[0] = begptr;
    2054               0 :   metaArr[1] = metaptr;
    2055               0 :   count = EHcntOBJECT(metaArr) + 1;
    2056                 : 
    2057                 : 
    2058                 :   /* Find colon (parse off field name) */
    2059                 :   /* --------------------------------- */
    2060               0 :   colon = strchr(metastr, ':');
    2061               0 :   *colon = 0;
    2062                 : 
    2063                 : 
    2064                 :   /* Search for next colon (compression and/or tiling parameters) */
    2065                 :   /* ------------------------------------------------------------ */
    2066               0 :   colon2 = strchr(colon + 1, ':');
    2067               0 :   if (colon2 != NULL)
    2068                 :   {
    2069               0 :       *colon2 = 0;
    2070                 :   }
    2071                 :   /* Make metadata string list from dimension list */
    2072                 :   /* --------------------------------------------- */
    2073               0 :   EHmetalist(colon + 1, utlstr2);
    2074                 : 
    2075                 : 
    2076                 :   /* Build metadata entry string */
    2077                 :   /* --------------------------- */
    2078               0 :   sprintf(utlstr, "%s%d%s%s%s%s%s%s",
    2079                 :     "\t\t\tOBJECT=DataField_", (int)count,
    2080                 :     "\n\t\t\t\tDataFieldName=\"", metastr,
    2081                 :     "\"\n\t\t\t\tDataType=", type,
    2082                 :     "\n\t\t\t\tDimList=", utlstr2);
    2083                 : 
    2084                 : 
    2085                 :   /* If compression and/or tiling parameters add to string */
    2086                 :   /* ----------------------------------------------------- */
    2087               0 :   if (colon2 != NULL)
    2088                 :   {
    2089               0 :       strcat(utlstr, colon2 + 1);
    2090                 :   }
    2091                 :   /* Add END_OBJECT terminator to metadata string */
    2092                 :   /* -------------------------------------------- */
    2093               0 :   sprintf(utlstr2, "%s%d%s",
    2094                 :     "\n\t\t\tEND_OBJECT=DataField_", (int)count, "\n");
    2095               0 :   strcat(utlstr, utlstr2);
    2096                 : 
    2097               0 :   break;
    2098                 : 
    2099                 : 
    2100                 :     case 6:
    2101                 :   /* Merged Field Section */
    2102                 :   /* -------------------- */
    2103                 : 
    2104                 :   /* Find beginning and ending of metadata section */
    2105                 :   /* --------------------------------------------- */
    2106               0 :   strcpy(utlstr, "\t\tGROUP=MergedFields");
    2107               0 :   begptr = strstr(metaptr, utlstr);
    2108                 : 
    2109               0 :   strcpy(utlstr, "\t\tEND_GROUP=MergedFields");
    2110               0 :   metaptr = strstr(metaptr, utlstr);
    2111                 : 
    2112                 : 
    2113                 :   /* Count number of existing entries and increment */
    2114                 :   /* ---------------------------------------------- */
    2115               0 :   metaArr[0] = begptr;
    2116               0 :   metaArr[1] = metaptr;
    2117               0 :   count = EHcntOBJECT(metaArr) + 1;
    2118                 : 
    2119                 : 
    2120                 :   /* Find colon (parse off merged fieldname) */
    2121                 :   /* --------------------------------------- */
    2122               0 :   colon = strchr(metastr, ':');
    2123                 : 
    2124                 : 
    2125                 :   /* Make metadata string list from field list */
    2126                 :   /* ----------------------------------------- */
    2127               0 :   EHmetalist(colon + 1, utlstr2);
    2128               0 :   *colon = 0;
    2129                 : 
    2130                 : 
    2131                 :   /* Build metadata entry string */
    2132                 :   /* --------------------------- */
    2133               0 :   sprintf(utlstr, "%s%d%s%s%s%s%s%s%d%s",
    2134                 :     "\t\t\tOBJECT=MergedFields_", (int)count,
    2135                 :     "\n\t\t\t\tMergedFieldName=\"", metastr, "\"",
    2136                 :     "\n\t\t\t\tFieldList=", utlstr2,
    2137                 :     "\n\t\t\tEND_OBJECT=MergedFields_", (int)count, "\n");
    2138               0 :   break;
    2139                 : 
    2140                 : 
    2141                 :     case 10:
    2142                 :   /* Point Level Section */
    2143                 :   /* ------------------- */
    2144                 : 
    2145                 :   /* Find beginning and ending of metadata section */
    2146                 :   /* --------------------------------------------- */
    2147               0 :   strcpy(utlstr, "\t\tGROUP=Level");
    2148               0 :   begptr = strstr(metaptr, utlstr);
    2149                 : 
    2150               0 :   strcpy(utlstr, "\n\t\tEND_GROUP=Level");
    2151               0 :   metaptr = strstr(metaptr, utlstr) + 1;
    2152                 : 
    2153                 : 
    2154                 :   /* Count number of existing entries and increment */
    2155                 :   /* ---------------------------------------------- */
    2156               0 :   metaArr[0] = begptr;
    2157               0 :   metaArr[1] = metaptr;
    2158               0 :   count = EHcntGROUP(metaArr);
    2159                 : 
    2160                 : 
    2161                 :   /* Build metadata entry string */
    2162                 :   /* --------------------------- */
    2163               0 :   sprintf(utlstr, "%s%d%s%s%s%d%s",
    2164                 :     "\t\t\tGROUP=Level_", (int)count,
    2165                 :     "\n\t\t\t\tLevelName=\"", metastr,
    2166                 :     "\"\n\t\t\tEND_GROUP=Level_", (int)count, "\n");
    2167               0 :   break;
    2168                 : 
    2169                 : 
    2170                 :     case 11:
    2171                 :   /* Point Field Section */
    2172                 :   /* ------------------- */
    2173                 : 
    2174                 :   /* Find colon (parse off point field name) */
    2175                 :   /* --------------------------------------- */
    2176               0 :   colon = strchr(metastr, ':');
    2177               0 :   *colon = 0;
    2178                 : 
    2179                 : 
    2180                 :   /* Find beginning and ending of metadata section */
    2181                 :   /* --------------------------------------------- */
    2182               0 :   strcpy(utlstr, "\t\t\t\tLevelName=\"");
    2183               0 :   strcat(utlstr, colon + 1);
    2184               0 :   begptr = strstr(metaptr, utlstr);
    2185                 : 
    2186               0 :   strcpy(utlstr, "\t\t\tEND_GROUP=Level_");
    2187               0 :   metaptr = strstr(begptr, utlstr);
    2188                 : 
    2189                 : 
    2190                 :   /* Count number of existing entries and increment */
    2191                 :   /* ---------------------------------------------- */
    2192               0 :   metaArr[0] = begptr;
    2193               0 :   metaArr[1] = metaptr;
    2194               0 :   count = EHcntOBJECT(metaArr) + 1;
    2195                 : 
    2196                 : 
    2197                 :   /* Build metadata entry string */
    2198                 :   /* --------------------------- */
    2199               0 :   sprintf(utlstr, "%s%d%s%s%s%s%s%d%s%d%s",
    2200                 :     "\t\t\t\tOBJECT=PointField_", (int)count,
    2201                 :     "\n\t\t\t\t\tPointFieldName=\"", metastr,
    2202                 :     "\"\n\t\t\t\t\tDataType=", type,
    2203                 :     "\n\t\t\t\t\tOrder=", (int)metadata[1],
    2204                 :     "\n\t\t\t\tEND_OBJECT=PointField_", (int)count, "\n");
    2205               0 :   break;
    2206                 : 
    2207                 : 
    2208                 : 
    2209                 :     case 12:
    2210                 :   /* Level Link Section */
    2211                 :   /* ------------------ */
    2212                 : 
    2213                 :   /* Find beginning and ending of metadata section */
    2214                 :   /* --------------------------------------------- */
    2215               0 :   strcpy(utlstr, "\t\tGROUP=LevelLink");
    2216               0 :   begptr = strstr(metaptr, utlstr);
    2217                 : 
    2218               0 :   strcpy(utlstr, "\t\tEND_GROUP=LevelLink");
    2219               0 :   metaptr = strstr(metaptr, utlstr);
    2220                 : 
    2221                 : 
    2222                 :   /* Count number of existing entries and increment */
    2223                 :   /* ---------------------------------------------- */
    2224               0 :   metaArr[0] = begptr;
    2225               0 :   metaArr[1] = metaptr;
    2226               0 :   count = EHcntOBJECT(metaArr) + 1;
    2227                 : 
    2228                 : 
    2229                 :   /* Find colon (parse off parent/child level names from link field) */
    2230                 :   /* --------------------------------------------------------------- */
    2231               0 :   colon = strchr(metastr, ':');
    2232               0 :   *colon = 0;
    2233                 : 
    2234                 : 
    2235                 :   /* Find slash (divide parent and child levels) */
    2236                 :   /* ------------------------------------------- */
    2237               0 :   slash = strchr(metastr, '/');
    2238               0 :   *slash = 0;
    2239                 : 
    2240                 : 
    2241                 :   /* Build metadata entry string */
    2242                 :   /* --------------------------- */
    2243               0 :   sprintf(utlstr, "%s%d%s%s%s%s%s%s%s%d%s",
    2244                 :     "\t\t\tOBJECT=LevelLink_", (int)count,
    2245                 :     "\n\t\t\t\tParent=\"", metastr,
    2246                 :     "\"\n\t\t\t\tChild=\"", slash + 1,
    2247                 :     "\"\n\t\t\t\tLinkField=\"", colon + 1,
    2248                 :     "\"\n\t\t\tEND_OBJECT=LevelLink_", (int)count, "\n");
    2249                 : 
    2250               0 :   break;
    2251                 : 
    2252                 : 
    2253                 :     case 101:
    2254                 :   /* Position metadata pointer for Grid proj parms, pix reg, origin */
    2255                 :   /* -------------------------------------------------------------- */
    2256               0 :   strcpy(utlstr, "\t\tGROUP=Dimension");
    2257               0 :   metaptr = strstr(metaptr, utlstr);
    2258               0 :   strcpy(utlstr, metastr);
    2259                 : 
    2260               0 :   break;
    2261                 : 
    2262                 : 
    2263                 :     case 1001:
    2264                 :   /* Position metadata pointer for new swath structure (SWcreate) */
    2265                 :   /* ------------------------------------------------------------ */
    2266               0 :   strcpy(utlstr, "END_GROUP=SwathStructure");
    2267               0 :   metaptr = strstr(metaptr, utlstr);
    2268               0 :   strcpy(utlstr, metastr);
    2269               0 :   break;
    2270                 : 
    2271                 : 
    2272                 :     case 1002:
    2273                 :   /* Position metadata pointer for new grid structure (GDcreate) */
    2274                 :   /* ----------------------------------------------------------- */
    2275               0 :   strcpy(utlstr, "END_GROUP=GridStructure");
    2276               0 :   metaptr = strstr(metaptr, utlstr);
    2277               0 :   strcpy(utlstr, metastr);
    2278               0 :   break;
    2279                 : 
    2280                 : 
    2281                 :     case 1003:
    2282                 :   /* Position metadata pointer for new point structure (PTcreate) */
    2283                 :   /* ------------------------------------------------------------ */
    2284               0 :   strcpy(utlstr, "END_GROUP=PointStructure");
    2285               0 :   metaptr = strstr(metaptr, utlstr);
    2286               0 :   strcpy(utlstr, metastr);
    2287                 :   break;
    2288                 :     }
    2289                 : 
    2290                 : 
    2291                 : 
    2292                 :     /* Get length of metadata string to insert */
    2293                 :     /* --------------------------------------- */
    2294               0 :     seglen = strlen(utlstr);
    2295                 : 
    2296                 :     /* Get offset of entry postion within existing metadata */
    2297                 :     /* ---------------------------------------------------- */
    2298               0 :     offset = metaptr - metabuf;
    2299                 : 
    2300                 : 
    2301                 :     /* If end of new metadata string outside of current metadata buffer ... */
    2302                 :     /* -------------------------------------------------------------------- */
    2303               0 :     if (metalen + seglen > 32000 * nmeta - 1)
    2304                 :     {
    2305                 :   /* Reallocate metadata buffer with additional 32000 bytes */
    2306                 :   /* ------------------------------------------------------ */
    2307               0 :   metabuf = (char *) realloc((void *) metabuf, 32000 * (nmeta + 1));
    2308               0 :   if(metabuf == NULL)
    2309                 :   { 
    2310               0 :       HEpush(DFE_NOSPACE,"EHinsertmeta", __FILE__, __LINE__);
    2311               0 :       free(utlstr);
    2312               0 :       free(utlstr2);
    2313               0 :       return(-1);
    2314                 :   }
    2315                 : 
    2316                 :   /* Increment metadata section counter */
    2317                 :   /* ---------------------------------- */
    2318               0 :   nmeta++;
    2319                 : 
    2320                 :   /* Reposition metadata pointer (entry position) */
    2321                 :   /* -------------------------------------------- */
    2322               0 :   metaptr = metabuf + offset;
    2323                 :     }
    2324                 :     /* Move metadata following entry point to its new position */
    2325                 :     /* ------------------------------------------------------- */
    2326               0 :     for (i = metalen - 1; i > offset - 1; i--)
    2327                 :     {
    2328               0 :   *(metabuf + seglen + i) = *(metabuf + i);
    2329                 :     }
    2330                 : 
    2331                 :     /* Copy new metadat string (utlstr) into metadata */
    2332                 :     /* ---------------------------------------------- */
    2333               0 :     memcpy(metaptr, utlstr, seglen);
    2334                 : 
    2335                 :     /* set to null character remaining of the metabuf */
    2336                 : 
    2337               0 :     memset((metabuf + metalen + seglen), '\0', (nmeta*32000 -1 - (metalen +
    2338                 :                   seglen)));
    2339                 :     /* Add new null string terminator */
    2340                 :     /* ------------------------------ */
    2341               0 :     metabuf[metalen + seglen] = 0;
    2342                 : 
    2343                 : 
    2344                 :     /* Write Back to Global Attribute(s) */
    2345                 :     /* --------------------------------- */
    2346               0 :     for (i = 0; i < nmeta; i++)
    2347                 :     {
    2348               0 :   sprintf(utlstr, "%s%d", "StructMetadata.", i);
    2349               0 :   SDsetattr(sdInterfaceID, utlstr, DFNT_CHAR8,
    2350                 :       32000, metabuf + i * 32000);
    2351                 :     }
    2352                 : 
    2353                 : 
    2354                 : 
    2355               0 :     free(metabuf);
    2356               0 :     free(utlstr);
    2357               0 :     free(utlstr2);
    2358                 : 
    2359               0 :     return (status);
    2360                 : 
    2361                 : }
    2362                 : 
    2363                 : 
    2364                 : 
    2365                 : 
    2366                 : 
    2367                 : 
    2368                 : /*----------------------------------------------------------------------------|
    2369                 : |  BEGIN_PROLOG                                                               |
    2370                 : |                                                                             |
    2371                 : |  FUNCTION: EHgetmetavalue                                                   |
    2372                 : |                                                                             |
    2373                 : |  DESCRIPTION: Returns metadata value                                        |
    2374                 : |                                                                             |
    2375                 : |                                                                             |
    2376                 : |  Return Value    Type     Units     Description                             |
    2377                 : |  ============   ======  =========   =====================================   |
    2378                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2379                 : |                                                                             |
    2380                 : |  INPUTS:                                                                    |
    2381                 : |  metaptrs        char               Begin and end of metadata section       |
    2382                 : |  parameter      char                parameter to access                     |
    2383                 : |                                                                             |
    2384                 : |  OUTPUTS:                                                                   |
    2385                 : |  metaptr        char                Ptr to (updated) beginning of metadata  |
    2386                 : |  retstr         char                return string containing value          |
    2387                 : |                                                                             |
    2388                 : |  NOTES:                                                                     |
    2389                 : |                                                                             |
    2390                 : |                                                                             |
    2391                 : |   Date     Programmer   Description                                         |
    2392                 : |  ======   ============  =================================================   |
    2393                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    2394                 : |  Jan 97   Joel Gales    Check string pointer against end of meta section    |
    2395                 : |                                                                             |
    2396                 : |  END_PROLOG                                                                 |
    2397                 : -----------------------------------------------------------------------------*/
    2398                 : intn
    2399                 : EHgetmetavalue(char *metaptrs[], char *parameter, char *retstr)
    2400             562 : {
    2401             562 :     intn            status = 0; /* routine return status variable */
    2402                 : 
    2403                 :     int32           slen; /* String length */
    2404                 :     char           *newline;  /* Position of new line character */
    2405                 :     char           *sptr; /* string pointer within metadata */
    2406                 : 
    2407                 : 
    2408                 :     /* Get string length of parameter string + 1 */
    2409                 :     /* ----------------------------------------- */
    2410             562 :     slen = strlen(parameter) + 1;
    2411                 : 
    2412                 : 
    2413                 :     /* Build search string (parameter string + "=") */
    2414                 :     /* -------------------------------------------- */
    2415             562 :     strcpy(retstr, parameter);
    2416             562 :     strcat(retstr, "=");
    2417                 : 
    2418                 : 
    2419                 :     /* Search for string within metadata (beginning at metaptrs[0]) */
    2420                 :     /* ------------------------------------------------------------ */
    2421             562 :     sptr = strstr(metaptrs[0], retstr);
    2422                 : 
    2423                 : 
    2424                 :     /* If string found within desired section ... */
    2425                 :     /* ------------------------------------------ */
    2426            1124 :     if (sptr != NULL && sptr < metaptrs[1])
    2427                 :     {
    2428                 :   /* Store position of string within metadata */
    2429                 :   /* ---------------------------------------- */
    2430             562 :   metaptrs[0] = sptr;
    2431                 : 
    2432                 :   /* Find newline "\n" character */
    2433                 :   /* --------------------------- */
    2434             562 :   newline = strchr(metaptrs[0], '\n');
    2435                 : 
    2436                 :   /* Copy from "=" to "\n" (exclusive) into return string */
    2437                 :   /* ---------------------------------------------------- */
    2438             562 :   memcpy(retstr, metaptrs[0] + slen, newline - metaptrs[0] - slen);
    2439                 : 
    2440                 :   /* Terminate return string with null */
    2441                 :   /* --------------------------------- */
    2442             562 :   retstr[newline - metaptrs[0] - slen] = 0;
    2443                 :     } else
    2444                 :     {
    2445                 :   /*
    2446                 :    * if parameter string not found within section, null return string
    2447                 :    * and set status to -1.
    2448                 :    */
    2449               0 :   retstr[0] = 0;
    2450               0 :   status = -1;
    2451                 :     }
    2452                 : 
    2453             562 :     return (status);
    2454                 : }
    2455                 : 
    2456                 : 
    2457                 : 
    2458                 : 
    2459                 : /*----------------------------------------------------------------------------|
    2460                 : |  BEGIN_PROLOG                                                               |
    2461                 : |                                                                             |
    2462                 : |  FUNCTION: EHmetagroup                                                      |
    2463                 : |                                                                             |
    2464                 : |  DESCRIPTION: Returns pointers to beginning and end of metadata group       |
    2465                 : |                                                                             |
    2466                 : |                                                                             |
    2467                 : |  Return Value    Type     Units     Description                             |
    2468                 : |  ============   ======  =========   =====================================   |
    2469                 : |  metabuf        char                Pointer to HDF-EOS object in metadata   |
    2470                 : |                                                                             |
    2471                 : |  INPUTS:                                                                    |
    2472                 : |  sdInterfaceID  int32               SDS interface ID                        |
    2473                 : |  structname     char                HDF-EOS structure name                  |
    2474                 : |  structcode     char                Structure code ("s/g/p")                |
    2475                 : |  groupname      char                Metadata group name                     |
    2476                 : |                                                                             |
    2477                 : |  OUTPUTS:                                                                   |
    2478                 : |  metaptrs       char                pointers to begin and end of metadata   |
    2479                 : |                                                                             |
    2480                 : |  NOTES:                                                                     |
    2481                 : |                                                                             |
    2482                 : |                                                                             |
    2483                 : |   Date     Programmer   Description                                         |
    2484                 : |  ======   ============  =================================================   |
    2485                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    2486                 : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    2487                 : |                                                                             |
    2488                 : |  END_PROLOG                                                                 |
    2489                 : -----------------------------------------------------------------------------*/
    2490                 : char           *
    2491                 : EHmetagroup(int32 sdInterfaceID, char *structname, char *structcode,
    2492                 :       char *groupname, char *metaptrs[])
    2493             270 : {
    2494                 :     intn            i;    /* Loop index */
    2495                 : 
    2496                 :     int32           attrIndex;  /* Structural metadata attribute index */
    2497                 :     int32           nmeta;  /* Number of 32000 byte metadata sections */
    2498                 :     int32           metalen;  /* Length of structural metadata */
    2499                 : 
    2500                 :     char           *metabuf;  /* Pointer (handle) to structural metadata */
    2501                 :     char           *endptr; /* Pointer to end of metadata section */
    2502                 :     char           *metaptr;  /* Metadata pointer */
    2503                 :     char           *prevmetaptr;/* Previous position of metadata pointer */
    2504                 :     char           *utlstr;     /* Utility string */
    2505                 : 
    2506                 : 
    2507                 : 
    2508                 :      /* Allocate memory for utility string */
    2509                 :      /* ---------------------------------- */
    2510             270 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE,sizeof(char));
    2511             270 :     if(utlstr == NULL)
    2512                 :     { 
    2513               0 :         HEpush(DFE_NOSPACE,"EHEHmetagroup", __FILE__, __LINE__);
    2514                 :   
    2515               0 :         return( NULL);
    2516                 :     }
    2517                 :     /* Determine number of structural metadata "sections" */
    2518                 :     /* -------------------------------------------------- */
    2519             270 :     nmeta = 0;
    2520                 :     while (1)
    2521                 :     {
    2522                 :   /* Search for "StructMetadata.x" attribute */
    2523                 :   /* --------------------------------------- */
    2524             540 :   sprintf(utlstr, "%s%d", "StructMetadata.", (int)nmeta);
    2525             540 :   attrIndex = SDfindattr(sdInterfaceID, utlstr);
    2526                 : 
    2527                 : 
    2528                 :   /* If found then increment metadata section counter else exit loop */
    2529                 :   /* --------------------------------------------------------------- */
    2530             540 :   if (attrIndex != -1)
    2531                 :   {
    2532             270 :       nmeta++;
    2533                 :   } else
    2534                 :   {
    2535             270 :       break;
    2536                 :   }
    2537             270 :     }
    2538                 : 
    2539                 : 
    2540                 :     /* Allocate space for metadata (in units of 32000 bytes) */
    2541                 :     /* ----------------------------------------------------- */
    2542             270 :     metabuf = (char *) calloc(32000 * nmeta, 1);
    2543                 :     
    2544             270 :     if(metabuf == NULL)
    2545                 :     { 
    2546               0 :   HEpush(DFE_NOSPACE,"EHmetagroup", __FILE__, __LINE__);
    2547               0 :   free(utlstr);
    2548               0 :   return(metabuf);
    2549                 :     }
    2550                 :     
    2551                 : 
    2552                 :     /* Read structural metadata */
    2553                 :     /* ------------------------ */
    2554             540 :     for (i = 0; i < nmeta; i++)
    2555                 :     {
    2556             270 :   sprintf(utlstr, "%s%d", "StructMetadata.", i);
    2557             270 :   attrIndex = SDfindattr(sdInterfaceID, utlstr);
    2558             270 :   metalen = strlen(metabuf);
    2559             270 :   SDreadattr(sdInterfaceID, attrIndex, metabuf + metalen);
    2560                 :     }
    2561                 : 
    2562                 :     /* Determine length (# of characters) of metadata */
    2563                 :     /* ---------------------------------------------- */
    2564             270 :     metalen = strlen(metabuf);
    2565                 : 
    2566                 : 
    2567                 : 
    2568                 :     /* Find HDF-EOS structure "root" group in metadata */
    2569                 :     /* ----------------------------------------------- */
    2570                 : 
    2571                 :     /* Setup proper search string */
    2572                 :     /* -------------------------- */
    2573             270 :     if (strcmp(structcode, "s") == 0)
    2574                 :     {
    2575               0 :   strcpy(utlstr, "GROUP=SwathStructure");
    2576             270 :     } else if (strcmp(structcode, "g") == 0)
    2577                 :     {
    2578             270 :   strcpy(utlstr, "GROUP=GridStructure");
    2579               0 :     } else if (strcmp(structcode, "p") == 0)
    2580                 :     {
    2581               0 :   strcpy(utlstr, "GROUP=PointStructure");
    2582                 :     }
    2583                 :     /* Use string search routine (strstr) to move through metadata */
    2584                 :     /* ----------------------------------------------------------- */
    2585             270 :     metaptr = strstr(metabuf, utlstr);
    2586                 : 
    2587                 : 
    2588                 : 
    2589                 :     /* Save current metadata pointer */
    2590                 :     /* ----------------------------- */
    2591             270 :     prevmetaptr = metaptr;
    2592                 : 
    2593                 : 
    2594                 :     /* First loop for "old-style" (non-ODL) metadata string */
    2595                 :     /* ---------------------------------------------------- */
    2596             270 :     if (strcmp(structcode, "s") == 0)
    2597                 :     {
    2598               0 :   sprintf(utlstr, "%s%s", "SwathName=\"", structname);
    2599             270 :     } else if (strcmp(structcode, "g") == 0)
    2600                 :     {
    2601             270 :   sprintf(utlstr, "%s%s", "GridName=\"", structname);
    2602               0 :     } else if (strcmp(structcode, "p") == 0)
    2603                 :     {
    2604               0 :   sprintf(utlstr, "%s%s", "PointName=\"", structname);
    2605                 :     }
    2606                 :     /* Do string search */
    2607                 :     /* ---------------- */
    2608             270 :     metaptr = strstr(metaptr, utlstr);
    2609                 : 
    2610                 : 
    2611                 :     /*
    2612                 :      * If not found then return to previous position in metadata and look for
    2613                 :      * "new-style" (ODL) metadata string
    2614                 :      */
    2615             270 :     if (metaptr == NULL)
    2616                 :     {
    2617               0 :   sprintf(utlstr, "%s%s", "GROUP=\"", structname);
    2618               0 :   metaptr = strstr(prevmetaptr, utlstr);
    2619                 :     }
    2620                 :     /* Find group within structure */
    2621                 :     /* --------------------------- */
    2622             270 :     if (groupname != NULL)
    2623                 :     {
    2624             129 :   sprintf(utlstr, "%s%s", "GROUP=", groupname);
    2625             129 :   metaptr = strstr(metaptr, utlstr);
    2626                 : 
    2627             129 :   sprintf(utlstr, "%s%s", "\t\tEND_GROUP=", groupname);
    2628             129 :   endptr = strstr(metaptr, utlstr);
    2629                 :     } else
    2630                 :     {
    2631                 :   /* If groupname == NULL then find end of structure in metadata */
    2632                 :   /* ----------------------------------------------------------- */
    2633             141 :   sprintf(utlstr, "%s", "\n\tEND_GROUP=");
    2634             141 :   endptr = strstr(metaptr, utlstr);
    2635                 :     }
    2636                 : 
    2637                 : 
    2638                 :     /* Return beginning and ending pointers */
    2639                 :     /* ------------------------------------ */
    2640             270 :     metaptrs[0] = metaptr;
    2641             270 :     metaptrs[1] = endptr;
    2642                 : 
    2643             270 :     free(utlstr);
    2644                 : 
    2645             270 :     return (metabuf);
    2646                 : }
    2647                 : 
    2648                 : 
    2649                 : 
    2650                 : 
    2651                 : 
    2652                 : /*----------------------------------------------------------------------------|
    2653                 : |  BEGIN_PROLOG                                                               |
    2654                 : |                                                                             |
    2655                 : |  FUNCTION: EHfillfld                                                        |
    2656                 : |                                                                             |
    2657                 : |  DESCRIPTION: Fills field with fill value                                   |
    2658                 : |                                                                             |
    2659                 : |                                                                             |
    2660                 : |  Return Value    Type     Units     Description                             |
    2661                 : |  ============   ======  =========   =====================================   |
    2662                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2663                 : |                                                                             |
    2664                 : |  INPUTS:                                                                    |
    2665                 : |  sdid           int32               SD element ID                           |
    2666                 : |  rank           int32               Rank of field                           |
    2667                 : |  truerank       int32               True rank of field (merging)            |
    2668                 : |  size           int32               size of fill element                    |
    2669                 : |  off            int32               Offset of field within merged field     |
    2670                 : |  dims           int32               Dimensions of field                     |
    2671                 : |  fillval        void                fill value                              |
    2672                 : |                                                                             |
    2673                 : |                                                                             |
    2674                 : |  OUTPUTS:                                                                   |
    2675                 : |             None                                                            |
    2676                 : |                                                                             |
    2677                 : |  NOTES:                                                                     |
    2678                 : |                                                                             |
    2679                 : |                                                                             |
    2680                 : |   Date     Programmer   Description                                         |
    2681                 : |  ======   ============  =================================================   |
    2682                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    2683                 : |                                                                             |
    2684                 : |  END_PROLOG                                                                 |
    2685                 : -----------------------------------------------------------------------------*/
    2686                 : intn
    2687                 : EHfillfld(int32 sdid, int32 rank, int32 truerank, int32 size, int32 off,
    2688                 :     int32 dims[], VOIDP fillval)
    2689               0 : {
    2690                 :     intn            i;    /* Loop index */
    2691                 :     intn            j;    /* Loop index */
    2692               0 :     intn            status = 0; /* routine return status variable */
    2693                 : 
    2694                 :     int32           n;    /* Max number of planes or rows in fill
    2695                 :          * buffer */
    2696               0 :     int32           start[3] = {0, 0, 0}; /* Start array (SDwritedata) */
    2697                 :     int32           edge[3];  /* Edge (count) array (SDwritedata) */
    2698                 :     int32           totN; /* Total number of elements in field */
    2699                 :     int32           planeN; /* Number of elements in plane */
    2700                 : 
    2701                 :     char           *fillbuf;  /* Fill buffer */
    2702                 : 
    2703                 : 
    2704                 :     /* Get total number of elements in field */
    2705                 :     /* ------------------------------------- */
    2706               0 :     totN = dims[0];
    2707               0 :     for (i = 1; i < rank; i++)
    2708                 :     {
    2709               0 :   totN *= dims[i];
    2710                 :     }
    2711                 : 
    2712                 : 
    2713                 :     /* Get number of elements in a plane of the field */
    2714                 :     /* ---------------------------------------------- */
    2715               0 :     planeN = dims[1] * dims[2];
    2716                 : 
    2717                 : 
    2718                 : 
    2719                 :     /* Allocate & Write Fill buffer */
    2720                 :     /* ---------------------------- */
    2721               0 :     if (totN * size < HDFE_MAXMEMBUF)
    2722                 :     {
    2723                 :   /* Entire field size (in bytes) smaller than max fill buffer */
    2724                 :   /* --------------------------------------------------------- */
    2725                 : 
    2726                 : 
    2727                 :   /* Allocate fill buffer */
    2728                 :   /* -------------------- */
    2729               0 :   fillbuf = (char *) malloc(totN * size);
    2730               0 :   if(fillbuf == NULL)
    2731                 :   { 
    2732               0 :       HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__);
    2733               0 :       return(-1);
    2734                 :   }
    2735                 :   
    2736                 : 
    2737                 :   /* Fill buffer with fill value */
    2738                 :   /* --------------------------- */
    2739               0 :   for (i = 0; i < totN; i++)
    2740                 :   {
    2741               0 :       memcpy(fillbuf + i * size, fillval, size);
    2742                 :   }
    2743                 : 
    2744                 : 
    2745                 :   /* Write fill buffer to field */
    2746                 :   /* -------------------------- */
    2747               0 :   start[0] = off;
    2748               0 :   edge[0] = dims[0];
    2749               0 :   edge[1] = dims[1];
    2750               0 :   edge[2] = dims[2];
    2751               0 :   status = SDwritedata(sdid, start, NULL, edge,
    2752                 :            (VOIDP) fillbuf);
    2753                 : 
    2754               0 :   free(fillbuf);
    2755                 : 
    2756               0 :     } else if (planeN * size < HDFE_MAXMEMBUF)
    2757                 :     {
    2758                 :   /* Single plane size (in bytes) smaller than max fill buffer */
    2759                 :   /* --------------------------------------------------------- */
    2760                 : 
    2761                 : 
    2762                 :   /* Compute number of planes that can be written at one time */
    2763                 :   /* -------------------------------------------------------- */
    2764               0 :   n = HDFE_MAXMEMBUF / (planeN * size);
    2765                 : 
    2766                 : 
    2767                 :   /* Allocate fill buffer */
    2768                 :   /* -------------------- */
    2769               0 :   fillbuf = (char *) malloc(planeN * size * n);
    2770               0 :   if(fillbuf == NULL)
    2771                 :   { 
    2772               0 :       HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__);
    2773               0 :       return(-1);
    2774                 :   }
    2775                 : 
    2776                 : 
    2777                 :   /* Fill buffer with fill value */
    2778                 :   /* --------------------------- */
    2779               0 :   for (i = 0; i < planeN * n; i++)
    2780                 :   {
    2781               0 :       memcpy(fillbuf + i * size, fillval, size);
    2782                 :   }
    2783                 : 
    2784                 : 
    2785                 :   /* Write (full) fill buffer to field */
    2786                 :   /* --------------------------------- */
    2787               0 :   for (i = 0; i < (dims[0] / n); i++)
    2788                 :   {
    2789               0 :       start[0] = off + i * n;
    2790               0 :       edge[0] = n;
    2791               0 :       edge[1] = dims[1];
    2792               0 :       edge[2] = dims[2];
    2793               0 :       status = SDwritedata(sdid, start, NULL, edge,
    2794                 :          (VOIDP) fillbuf);
    2795                 :   }
    2796                 : 
    2797                 : 
    2798                 :   /* Write (partial) last fill buffer to field (if necessary) */
    2799                 :   /* -------------------------------------------------------- */
    2800               0 :   if (i * n != dims[0])
    2801                 :   {
    2802               0 :       start[0] = off + i * n;
    2803               0 :       edge[0] = dims[0] - i * n;
    2804               0 :       edge[1] = dims[1];
    2805               0 :       edge[2] = dims[2];
    2806               0 :       status = SDwritedata(sdid, start, NULL, edge,
    2807                 :          (VOIDP) fillbuf);
    2808                 :   }
    2809               0 :   free(fillbuf);
    2810                 : 
    2811                 :     } else
    2812                 :     {
    2813                 :   /* Single plane size (in bytes) greater than max fill buffer */
    2814                 :   /* --------------------------------------------------------- */
    2815                 : 
    2816                 : 
    2817                 :   /* Compute number of "rows" than can be written at one time */
    2818                 :   /* -------------------------------------------------------- */
    2819               0 :   n = HDFE_MAXMEMBUF / (dims[rank - 1] * size);
    2820                 : 
    2821                 : 
    2822                 :   /* Allocate fill buffer */
    2823                 :   /* -------------------- */
    2824               0 :   fillbuf = (char *) malloc(dims[rank - 1] * size * n);
    2825               0 :   if(fillbuf == NULL)
    2826                 :   { 
    2827               0 :       HEpush(DFE_NOSPACE,"EHfillfld", __FILE__, __LINE__);
    2828               0 :       return(-1);
    2829                 :   }
    2830                 : 
    2831                 : 
    2832                 :   /* Fill buffer with fill value */
    2833                 :   /* --------------------------- */
    2834               0 :   for (i = 0; i < dims[rank - 1] * n; i++)
    2835                 :   {
    2836               0 :       memcpy(fillbuf + i * size, fillval, size);
    2837                 :   }
    2838                 : 
    2839                 : 
    2840                 :   /* For every plane in field ... */
    2841                 :   /* ---------------------------- */
    2842               0 :   for (j = 0; j < dims[0]; j++)
    2843                 :   {
    2844                 : 
    2845                 :       /* Write (full) fill buffer to field */
    2846                 :       /* --------------------------------- */
    2847               0 :       for (i = 0; i < (dims[1] / n); i++)
    2848                 :       {
    2849               0 :     start[0] = off + j;
    2850               0 :     start[1] = i * n;
    2851               0 :     edge[0] = 1;
    2852               0 :     edge[1] = n;
    2853               0 :     edge[2] = dims[2];
    2854               0 :     status = SDwritedata(sdid, start, NULL, edge,
    2855                 :              (VOIDP) fillbuf);
    2856                 :       }
    2857                 : 
    2858                 : 
    2859                 :       /* Write (partial) last fill buffer to field (if necessary) */
    2860                 :       /* -------------------------------------------------------- */
    2861               0 :       if (i * n != dims[1])
    2862                 :       {
    2863               0 :     start[0] = off + j;
    2864               0 :     start[1] = i * n;
    2865               0 :     edge[0] = 1;
    2866               0 :     edge[1] = dims[1] - i * n;
    2867               0 :     edge[2] = dims[2];
    2868               0 :     status = SDwritedata(sdid, start, NULL, edge,
    2869                 :              (VOIDP) fillbuf);
    2870                 :       }
    2871                 :   }
    2872                 : 
    2873               0 :   free(fillbuf);
    2874                 : 
    2875                 :     }
    2876                 : 
    2877               0 :     return (status);
    2878                 : }
    2879                 : 
    2880                 : 
    2881                 : 
    2882                 : 
    2883                 : 
    2884                 : 
    2885                 : /*----------------------------------------------------------------------------|
    2886                 : |  BEGIN_PROLOG                                                               |
    2887                 : |                                                                             |
    2888                 : |  FUNCTION: EHbisect                                                         |
    2889                 : |                                                                             |
    2890                 : |  DESCRIPTION: Finds root of function using bisection                        |
    2891                 : |                                                                             |
    2892                 : |                                                                             |
    2893                 : |  Return Value    Type     Units     Description                             |
    2894                 : |  ============   ======  =========   =====================================   |
    2895                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    2896                 : |                                                                             |
    2897                 : |  INPUTS:                                                                    |
    2898                 : |  func()         float64             Function to bisect                      |
    2899                 : |  funcParms      float64             Function parameters (fixed)             |
    2900                 : |  nParms         int32               Number of function parameters           |
    2901                 : |  limLft         float64             Lower limit of function arguement       |
    2902                 : |  limRgt         float64             Upper limit of function arguement       |
    2903                 : |  convCrit       float64             Convergence criterion                   |
    2904                 : |                                                                             |
    2905                 : |  OUTPUTS:                                                                   |
    2906                 : |  root           float64             Function root                           |
    2907                 : |                                                                             |
    2908                 : |  NOTES:                                                                     |
    2909                 : |                                                                             |
    2910                 : |                                                                             |
    2911                 : |   Date     Programmer   Description                                         |
    2912                 : |  ======   ============  =================================================   |
    2913                 : |  Nov 96   Joel Gales    Original Programmer                                 |
    2914                 : |                                                                             |
    2915                 : |  END_PROLOG                                                                 |
    2916                 : -----------------------------------------------------------------------------*/
    2917                 : intn
    2918                 : EHbisect(float64(*func) (float64[]), float64 funcParms[], int32 nParms,
    2919                 :    float64 limLft, float64 limRgt, float64 convCrit, float64 * root)
    2920               0 : {
    2921                 :     intn            i;    /* Loop index */
    2922               0 :     intn            status = 0; /* routine return status variable */
    2923                 : 
    2924                 :     float64         midPnt; /* Mid-point value */
    2925                 :     float64         newmidPnt;  /* New mid-point value */
    2926                 :     float64         funcLft;  /* Function value at left-hand limit */
    2927                 :     float64         funcMid;  /* Function value at mid-point */
    2928                 :     float64         funcRgt;  /* Function value at right-hand limit */
    2929                 :     float64        *parms;  /* Function parameters */
    2930                 : 
    2931                 : 
    2932                 :     /* Allocate space for function parameters */
    2933                 :     /* -------------------------------------- */
    2934               0 :     parms = (float64 *) calloc(nParms + 1, sizeof(float64));
    2935               0 :     if(parms == NULL)
    2936                 :     { 
    2937               0 :   HEpush(DFE_NOSPACE, "EHbisect", __FILE__, __LINE__);
    2938               0 :   return(-1);
    2939                 :     }
    2940                 : 
    2941                 : 
    2942                 :     /* Copy (fixed) function parameters */
    2943                 :     /* -------------------------------- */
    2944               0 :     for (i = 0; i < nParms; i++)
    2945                 :     {
    2946               0 :   parms[i + 1] = funcParms[i];
    2947                 :     }
    2948                 : 
    2949                 : 
    2950                 :     /* Copy left-hand limit to "floating" parameter */
    2951                 :     /* -------------------------------------------- */
    2952               0 :     parms[0] = limLft;
    2953                 : 
    2954                 : 
    2955                 :     /* Determine function value */
    2956                 :     /* ------------------------ */
    2957               0 :     funcLft = (*func) (parms);
    2958                 : 
    2959                 : 
    2960                 :     /* Copy right-hand limit to "floating" parameter */
    2961                 :     /* --------------------------------------------- */
    2962               0 :     parms[0] = limRgt;
    2963                 : 
    2964                 : 
    2965                 :     /* Determine function value */
    2966                 :     /* ------------------------ */
    2967               0 :     funcRgt = (*func) (parms);
    2968                 : 
    2969                 : 
    2970                 :     /* If left and right limits function values of same sign then no root */
    2971                 :     /* ------------------------------------------------------------------ */
    2972               0 :     if (funcLft * funcRgt > 0)
    2973                 :     {
    2974               0 :   free(parms);
    2975               0 :   return (-1);
    2976                 :     }
    2977                 :     /* Compute (initial) mid-point */
    2978                 :     /* --------------------------- */
    2979               0 :     newmidPnt = 0.5 * (limLft + limRgt);
    2980                 : 
    2981                 : 
    2982                 :     /* Bisection Loop */
    2983                 :     /* -------------- */
    2984                 :     while (1)
    2985                 :     {
    2986                 :   /* Compute function at new mid-point */
    2987                 :   /* --------------------------------- */
    2988               0 :   midPnt = newmidPnt;
    2989               0 :   parms[0] = midPnt;
    2990               0 :   funcMid = (*func) (parms);
    2991                 : 
    2992                 : 
    2993                 :   /* If left limit same sign as mid-point move it to mid-point */
    2994                 :   /* --------------------------------------------------------- */
    2995               0 :   if (funcLft * funcMid > 0.0)
    2996                 :   {
    2997               0 :       limLft = midPnt;
    2998                 :   } else
    2999                 :   {
    3000                 :       /* Otherwise move over right-hand limit */
    3001                 :       /* ------------------------------------ */
    3002               0 :       limRgt = midPnt;
    3003                 :   }
    3004                 : 
    3005                 : 
    3006                 :   /* Compute new mid-point */
    3007                 :   /* --------------------- */
    3008               0 :   newmidPnt = 0.5 * (limLft + limRgt);
    3009                 : 
    3010                 : 
    3011                 :   /* If relative change in midpoint < convergence crit then exit loop */
    3012                 :   /* ---------------------------------------------------------------- */
    3013               0 :   if (fabs((newmidPnt - midPnt) / midPnt) < convCrit)
    3014                 :   {
    3015               0 :       break;
    3016                 :   }
    3017               0 :     }
    3018                 : 
    3019                 :     /* Save root */
    3020                 :     /* --------- */
    3021               0 :     *root = newmidPnt;
    3022                 : 
    3023                 : 
    3024               0 :     free(parms);
    3025                 : 
    3026               0 :     return (status);
    3027                 : }
    3028                 : 
    3029                 : 
    3030                 : 
    3031                 : 
    3032                 : /*----------------------------------------------------------------------------|
    3033                 : |  BEGIN_PROLOG                                                               |
    3034                 : |                                                                             |
    3035                 : |  FUNCTION: EHattr                                                           |
    3036                 : |                                                                             |
    3037                 : |  DESCRIPTION: Reads/Writes attributes for HDF-EOS structures                |
    3038                 : |                                                                             |
    3039                 : |                                                                             |
    3040                 : |  Return Value    Type     Units     Description                             |
    3041                 : |  ============   ======  =========   =====================================   |
    3042                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3043                 : |                                                                             |
    3044                 : |  INPUTS:                                                                    |
    3045                 : |  fid            int32               HDF-EOS file ID                         |
    3046                 : |  attrVgrpID     int32               Attribute Vgroup ID                     |
    3047                 : |  attrname       char                attribute name                          |
    3048                 : |  numbertype     int32               attribute HDF numbertype                |
    3049                 : |  count          int32               Number of attribute elements            |
    3050                 : |  wrcode         char                Read/Write Code "w/r"                   |
    3051                 : |  datbuf         void                I/O buffer                              |
    3052                 : |                                                                             |
    3053                 : |                                                                             |
    3054                 : |  OUTPUTS:                                                                   |
    3055                 : |  datbuf         void                I/O buffer                              |
    3056                 : |                                                                             |
    3057                 : |  NOTES:                                                                     |
    3058                 : |                                                                             |
    3059                 : |                                                                             |
    3060                 : |   Date     Programmer   Description                                         |
    3061                 : |  ======   ============  =================================================   |
    3062                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    3063                 : |  Oct 96   Joel Gales    Pass Vgroup id as routine parameter                 |
    3064                 : |  Oct 96   Joel Gales    Remove Vdetach call                                 |
    3065                 : |                                                                             |
    3066                 : |  END_PROLOG                                                                 |
    3067                 : -----------------------------------------------------------------------------*/
    3068                 : intn
    3069                 : EHattr(int32 fid, int32 attrVgrpID, char *attrname, int32 numbertype,
    3070                 :        int32 count, char *wrcode, VOIDP datbuf)
    3071                 : 
    3072               0 : {
    3073               0 :     intn            status = 0; /* routine return status variable */
    3074                 :     int32           vdataID;  /* Attribute Vdata ID */
    3075                 : 
    3076                 :     /*
    3077                 :      * Attributes are stored as Vdatas with name given by the user, class:
    3078                 :      * "Attr0.0" and fieldname: "AttrValues"
    3079                 :      */
    3080                 : 
    3081                 : 
    3082                 :     /* Get Attribute Vdata ID and "open" with approriate I/O code */
    3083                 :     /* ---------------------------------------------------------- */
    3084               0 :     vdataID = EHgetid(fid, attrVgrpID, attrname, 1, wrcode);
    3085                 : 
    3086                 :     /* Write Attribute Section */
    3087                 :     /* ----------------------- */
    3088               0 :     if (strcmp(wrcode, "w") == 0)
    3089                 :     {
    3090                 :   /* Create Attribute Vdata (if it doesn't exist) */
    3091                 :   /* -------------------------------------------- */
    3092               0 :   if (vdataID == -1)
    3093                 :   {
    3094               0 :       vdataID = VSattach(fid, -1, "w");
    3095               0 :       VSsetname(vdataID, attrname);
    3096               0 :       VSsetclass(vdataID, "Attr0.0");
    3097                 : 
    3098               0 :       VSfdefine(vdataID, "AttrValues", numbertype, count);
    3099               0 :       Vinsert(attrVgrpID, vdataID);
    3100                 :   }
    3101                 :   /* Write Attribute */
    3102                 :   /* --------------- */
    3103               0 :   VSsetfields(vdataID, "AttrValues");
    3104               0 :   (void) VSsizeof(vdataID, "AttrValues");
    3105               0 :   VSwrite(vdataID, datbuf, 1, FULL_INTERLACE);
    3106                 : 
    3107               0 :   VSdetach(vdataID);
    3108                 :     }
    3109                 :     /* Read Attribute Section */
    3110                 :     /* ---------------------- */
    3111               0 :     if (strcmp(wrcode, "r") == 0)
    3112                 :     {
    3113                 :   /* If attribute doesn't exist report error */
    3114                 :   /* --------------------------------------- */
    3115               0 :   if (vdataID == -1)
    3116                 :   {
    3117               0 :       status = -1;
    3118               0 :       HEpush(DFE_GENAPP, "EHattr", __FILE__, __LINE__);
    3119               0 :       HEreport("Attribute %s not defined.\n", attrname);
    3120                 :   } else
    3121                 :   {
    3122               0 :       VSsetfields(vdataID, "AttrValues");
    3123               0 :       (void) VSsizeof(vdataID, "AttrValues");
    3124               0 :       VSread(vdataID, datbuf, 1, FULL_INTERLACE);
    3125               0 :       VSdetach(vdataID);
    3126                 :   }
    3127                 :     }
    3128               0 :     return (status);
    3129                 : }
    3130                 : 
    3131                 : 
    3132                 : 
    3133                 : 
    3134                 : /*----------------------------------------------------------------------------|
    3135                 : |  BEGIN_PROLOG                                                               |
    3136                 : |                                                                             |
    3137                 : |  FUNCTION: EHattrinfo                                                       |
    3138                 : |                                                                             |
    3139                 : |  DESCRIPTION: Returns numbertype and count of given HDF-EOS attribute       |
    3140                 : |                                                                             |
    3141                 : |                                                                             |
    3142                 : |  Return Value    Type     Units     Description                             |
    3143                 : |  ============   ======  =========   =====================================   |
    3144                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3145                 : |                                                                             |
    3146                 : |  INPUTS:                                                                    |
    3147                 : |  fid            int32               HDF-EOS file ID                         |
    3148                 : |  attrVgrpID     int32               Attribute Vgroup ID                     |
    3149                 : |  attrname       char                attribute name                          |
    3150                 : |                                                                             |
    3151                 : |  OUTPUTS:                                                                   |
    3152                 : |  numbertype     int32               attribute HDF numbertype                |
    3153                 : |  count          int32               Number of attribute elements            |
    3154                 : |                                                                             |
    3155                 : |  NOTES:                                                                     |
    3156                 : |                                                                             |
    3157                 : |                                                                             |
    3158                 : |   Date     Programmer   Description                                         |
    3159                 : |  ======   ============  =================================================   |
    3160                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    3161                 : |  Oct 96   Joel Gales    Pass Vgroup id as routine parameter                 |
    3162                 : |  Oct 96   Joel Gales    Remove Vdetach call                                 |
    3163                 : |                                                                             |
    3164                 : |  END_PROLOG                                                                 |
    3165                 : -----------------------------------------------------------------------------*/
    3166                 : intn
    3167                 : EHattrinfo(int32 fid, int32 attrVgrpID, char *attrname, int32 * numbertype,
    3168                 :      int32 * count)
    3169                 : 
    3170               0 : {
    3171               0 :     intn            status = 0; /* routine return status variable */
    3172                 :     int32           vdataID;  /* Attribute Vdata ID */
    3173                 : 
    3174                 :     /* Get Attribute Vdata ID */
    3175                 :     /* ---------------------- */
    3176               0 :     vdataID = EHgetid(fid, attrVgrpID, attrname, 1, "r");
    3177                 : 
    3178                 :     /* If attribute not defined then report error */
    3179                 :     /* ------------------------------------------ */
    3180               0 :     if (vdataID == -1)
    3181                 :     {
    3182               0 :   status = -1;
    3183               0 :   HEpush(DFE_GENAPP, "EHattr", __FILE__, __LINE__);
    3184               0 :   HEreport("Attribute %s not defined.\n", attrname);
    3185                 :     } else
    3186                 :     {
    3187                 :   /* Get attribute info */
    3188                 :   /* ------------------ */
    3189               0 :   VSsetfields(vdataID, "AttrValues");
    3190               0 :   *count = VSsizeof(vdataID, "AttrValues");
    3191               0 :   *numbertype = VFfieldtype(vdataID, 0);
    3192               0 :   VSdetach(vdataID);
    3193                 :     }
    3194                 : 
    3195               0 :     return (status);
    3196                 : }
    3197                 : 
    3198                 : 
    3199                 : 
    3200                 : 
    3201                 : 
    3202                 : /*----------------------------------------------------------------------------|
    3203                 : |  BEGIN_PROLOG                                                               |
    3204                 : |                                                                             |
    3205                 : |  FUNCTION: EHattrcat                                                        |
    3206                 : |                                                                             |
    3207                 : |  DESCRIPTION: Returns a listing of attributes within an HDF-EOS structure   |
    3208                 : |                                                                             |
    3209                 : |                                                                             |
    3210                 : |  Return Value    Type     Units     Description                             |
    3211                 : |  ============   ======  =========   =====================================   |
    3212                 : |  nattr          int32               Number of attributes in swath struct    |
    3213                 : |                                                                             |
    3214                 : |  INPUTS:                                                                    |
    3215                 : |  fid            int32               HDF-EOS file ID                         |
    3216                 : |  attrVgrpID     int32               Attribute Vgroup ID                     |
    3217                 : |  structcode     char                Structure Code ("s/g/p")                |
    3218                 : |                                                                             |
    3219                 : |  OUTPUTS:                                                                   |
    3220                 : |  attrnames      char                Attribute names in swath struct         |
    3221                 : |                                     (Comma-separated list)                  |
    3222                 : |  strbufsize     int32               Attributes name list string length      |
    3223                 : |                                                                             |
    3224                 : |  NOTES:                                                                     |
    3225                 : |                                                                             |
    3226                 : |                                                                             |
    3227                 : |   Date     Programmer   Description                                         |
    3228                 : |  ======   ============  =================================================   |
    3229                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    3230                 : |  Oct 96   Joel Gales    Pass Vgroup id as routine parameter                 |
    3231                 : |  Oct 96   Joel Gales    Remove Vdetach call                                 |
    3232                 : |                                                                             |
    3233                 : |  END_PROLOG                                                                 |
    3234                 : -----------------------------------------------------------------------------*/
    3235                 : int32
    3236                 : EHattrcat(int32 fid, int32 attrVgrpID, char *attrnames, int32 * strbufsize)
    3237               8 : {
    3238                 :     intn            i;            /* Loop index */
    3239                 : 
    3240                 :     int32           nObjects;         /* # of objects in Vgroup */
    3241                 :     int32          *tags;         /* Pnt to Vgroup object tags array */
    3242                 :     int32          *refs;         /* Pnt to Vgroup object refs array */
    3243                 :     int32           vdataID;          /* Attribute Vdata ID */
    3244                 : 
    3245               8 :     int32           nattr = 0;          /* Number of attributes */
    3246                 :     int32           slen;         /* String length */
    3247                 : 
    3248                 :     char            name[80];         /* Attribute name */
    3249                 :     static const char indxstr[] = "INDXMAP:"; /* Index Mapping reserved
    3250                 :                                                  string */
    3251                 :     static const char fvstr[] = "_FV_"; /* Flag Value reserved string */
    3252                 :     static const char bsom[] = "_BLKSOM:";/* Block SOM Offset reserved string */
    3253                 : 
    3254                 : 
    3255                 :     /* Set string buffer size to 0 */
    3256                 :     /* --------------------------- */
    3257               8 :     *strbufsize = 0;
    3258                 : 
    3259                 : 
    3260                 :     /* Get number of attributes within Attribute Vgroup */
    3261                 :     /* ------------------------------------------------ */
    3262               8 :     nObjects = Vntagrefs(attrVgrpID);
    3263                 : 
    3264                 : 
    3265                 :     /* If attributes exist ... */
    3266                 :     /* ----------------------- */
    3267               8 :     if (nObjects > 0)
    3268                 :     {
    3269                 :   /* Get tags and references of attribute Vdatas */
    3270                 :   /* ------------------------------------------- */
    3271               2 :   tags = (int32 *) malloc(sizeof(int32) * nObjects);
    3272               2 :   if(tags == NULL)
    3273                 :   { 
    3274               0 :       HEpush(DFE_NOSPACE,"EHattrcat", __FILE__, __LINE__);
    3275               0 :       return(-1);
    3276                 :   }
    3277               2 :   refs = (int32 *) malloc(sizeof(int32) * nObjects);
    3278               2 :   if(refs == NULL)
    3279                 :   { 
    3280               0 :       HEpush(DFE_NOSPACE,"EHattrcat", __FILE__, __LINE__);
    3281               0 :       free(tags);
    3282               0 :       return(-1);
    3283                 :   }
    3284                 : 
    3285               2 :   Vgettagrefs(attrVgrpID, tags, refs, nObjects);
    3286                 : 
    3287                 :   /* Get attribute vdata IDs and names */
    3288                 :   /* --------------------------------- */
    3289              28 :   for (i = 0; i < nObjects; i++)
    3290                 :   {
    3291              26 :       vdataID = VSattach(fid, *(refs + i), "r");
    3292              26 :       VSgetname(vdataID, name);
    3293                 : 
    3294                 :       /*
    3295                 :        * Don't return fill value, index mapping & block SOM attributes
    3296                 :        */
    3297              26 :       if (memcmp(name, indxstr, strlen(indxstr)) != 0 &&
    3298                 :     memcmp(name, fvstr, strlen(fvstr)) != 0 &&
    3299                 :     memcmp(name, bsom, strlen(bsom)) != 0)
    3300                 :       {
    3301                 :     /* Increment attribute counter and add name to list */
    3302                 :     /* ------------------------------------------------ */
    3303               0 :     nattr++;
    3304               0 :     if (attrnames != NULL)
    3305                 :     {
    3306               0 :         if (nattr == 1)
    3307                 :         {
    3308               0 :       strcpy(attrnames, name);
    3309                 :         } else
    3310                 :         {
    3311               0 :       strcat(attrnames, ",");
    3312               0 :       strcat(attrnames, name);
    3313                 :         }
    3314                 :     }
    3315                 :     /* Increment attribute names string length */
    3316                 :     /* --------------------------------------- */
    3317               0 :     slen = (nattr == 1) ? strlen(name) : strlen(name) + 1;
    3318               0 :     *strbufsize += slen;
    3319                 :       }
    3320              26 :       VSdetach(vdataID);
    3321                 :   }
    3322               2 :   free(tags);
    3323               2 :   free(refs);
    3324                 :     }
    3325               8 :     return (nattr);
    3326                 : }
    3327                 : 
    3328                 : 
    3329                 : 
    3330                 : /*----------------------------------------------------------------------------|
    3331                 : |  BEGIN_PROLOG                                                               |
    3332                 : |                                                                             |
    3333                 : |  FUNCTION: EHinquire                                                        |
    3334                 : |                                                                             |
    3335                 : |  DESCRIPTION: Returns number and names of HDF-EOS structures in file        |
    3336                 : |                                                                             |
    3337                 : |                                                                             |
    3338                 : |  Return Value    Type     Units     Description                             |
    3339                 : |  ============   ======  =========   =====================================   |
    3340                 : |  nobj           int32               Number of HDF-EOS structures in file    |
    3341                 : |                                                                             |
    3342                 : |  INPUTS:                                                                    |
    3343                 : |  filename       char                HDF-EOS filename                        |
    3344                 : |  type           char                Object Type ("SWATH/GRID/POINT")        |
    3345                 : |                                                                             |
    3346                 : |  OUTPUTS:                                                                   |
    3347                 : |  objectlist     char                List of object names (comma-separated)  |
    3348                 : |  strbufsize     int32               Length of objectlist                    |
    3349                 : |                                                                             |
    3350                 : |  NOTES:                                                                     |
    3351                 : |                                                                             |
    3352                 : |                                                                             |
    3353                 : |   Date     Programmer   Description                                         |
    3354                 : |  ======   ============  =================================================   |
    3355                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    3356                 : |                                                                             |
    3357                 : |  END_PROLOG                                                                 |
    3358                 : -----------------------------------------------------------------------------*/
    3359                 : int32
    3360                 : EHinquire(char *filename, char *type, char *objectlist, int32 * strbufsize)
    3361               6 : {
    3362                 :     int32           HDFfid; /* HDF file ID */
    3363                 :     int32           vgRef;  /* Vgroup reference number */
    3364                 :     int32           vGrpID; /* Vgroup ID */
    3365               6 :     int32           nobj = 0; /* Number of HDFEOS objects in file */
    3366                 :     int32           slen; /* String length */
    3367                 : 
    3368                 :     char            name[80]; /* Object name */
    3369                 :     char            class[80];  /* Object class */
    3370                 : 
    3371                 : 
    3372                 :     /* Open HDFEOS file of read-only access */
    3373                 :     /* ------------------------------------ */
    3374               6 :     HDFfid = Hopen(filename, DFACC_READ, 0);
    3375                 : 
    3376                 : 
    3377                 :     /* Start Vgroup Interface */
    3378                 :     /* ---------------------- */
    3379               6 :     Vstart(HDFfid);
    3380                 : 
    3381                 : 
    3382                 :     /* If string buffer size is requested then zero out counter */
    3383                 :     /* -------------------------------------------------------- */
    3384               6 :     if (strbufsize != NULL)
    3385                 :     {
    3386               6 :   *strbufsize = 0;
    3387                 :     }
    3388                 :     /* Search for objects from begining of HDF file */
    3389                 :     /* -------------------------------------------- */
    3390               6 :     vgRef = -1;
    3391                 : 
    3392                 :     /* Loop through all objects */
    3393                 :     /* ------------------------ */
    3394                 :     while (1)
    3395                 :     {
    3396                 :   /* Get Vgroup reference number */
    3397                 :   /* --------------------------- */
    3398              48 :   vgRef = Vgetid(HDFfid, vgRef);
    3399                 : 
    3400                 :   /* If no more then exist search loop */
    3401                 :   /* --------------------------------- */
    3402              48 :   if (vgRef == -1)
    3403                 :   {
    3404               6 :       break;
    3405                 :   }
    3406                 :   /* Get Vgroup ID, name, and class */
    3407                 :   /* ------------------------------ */
    3408              42 :   vGrpID = Vattach(HDFfid, vgRef, "r");
    3409              42 :   Vgetname(vGrpID, name);
    3410              42 :   Vgetclass(vGrpID, class);
    3411                 : 
    3412                 : 
    3413                 :   /* If object of desired type (SWATH, POINT, GRID) ... */
    3414                 :   /* -------------------------------------------------- */
    3415              42 :   if (strcmp(class, type) == 0)
    3416                 :   {
    3417                 : 
    3418                 :       /* Increment counter */
    3419                 :       /* ----------------- */
    3420               4 :       nobj++;
    3421                 : 
    3422                 : 
    3423                 :       /* If object list requested add name to list */
    3424                 :       /* ----------------------------------------- */
    3425               4 :       if (objectlist != NULL)
    3426                 :       {
    3427               2 :     if (nobj == 1)
    3428                 :     {
    3429               2 :         strcpy(objectlist, name);
    3430                 :     } else
    3431                 :     {
    3432               0 :         strcat(objectlist, ",");
    3433               0 :         strcat(objectlist, name);
    3434                 :     }
    3435                 :       }
    3436                 :       /* Compute string length of object entry */
    3437                 :       /* ------------------------------------- */
    3438               4 :       slen = (nobj == 1) ? strlen(name) : strlen(name) + 1;
    3439                 : 
    3440                 : 
    3441                 :       /* If string buffer size is requested then increment buffer size */
    3442                 :       /* ------------------------------------------------------------- */
    3443               4 :       if (strbufsize != NULL)
    3444                 :       {
    3445               4 :     *strbufsize += slen;
    3446                 :       }
    3447                 :   }
    3448                 :   /* Detach Vgroup */
    3449                 :   /* ------------- */
    3450              42 :   Vdetach(vGrpID);
    3451              42 :     }
    3452                 : 
    3453                 :     /* "Close" Vgroup interface and HDFEOS file */
    3454                 :     /* ---------------------------------------- */
    3455               6 :     Vend(HDFfid);
    3456               6 :     Hclose(HDFfid);
    3457                 : 
    3458               6 :     return (nobj);
    3459                 : }
    3460                 : 
    3461                 : 
    3462                 : 
    3463                 : /*----------------------------------------------------------------------------|
    3464                 : |  BEGIN_PROLOG                                                               |
    3465                 : |                                                                             |
    3466                 : |  FUNCTION: EHclose                                                          |
    3467                 : |                                                                             |
    3468                 : |  DESCRIPTION: Closes HDF-EOS file                                           |
    3469                 : |                                                                             |
    3470                 : |                                                                             |
    3471                 : |  Return Value    Type     Units     Description                             |
    3472                 : |  ============   ======  =========   =====================================   |
    3473                 : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    3474                 : |                                                                             |
    3475                 : |  INPUTS:                                                                    |
    3476                 : |  fid            int32               HDF-EOS File ID                         |
    3477                 : |                                                                             |
    3478                 : |  OUTPUTS:                                                                   |
    3479                 : |             None                                                            |
    3480                 : |                                                                             |
    3481                 : |  NOTES:                                                                     |
    3482                 : |                                                                             |
    3483                 : |                                                                             |
    3484                 : |   Date     Programmer   Description                                         |
    3485                 : |  ======   ============  =================================================   |
    3486                 : |  Jun 96   Joel Gales    Original Programmer                                 |
    3487                 : |  Jul 96   Joel Gales    Add file id offset EHIDOFFSET                       |
    3488                 : |  Aug 96   Joel Gales    Add HE error report if file id out of bounds        |
    3489                 : |  Nov 96   Joel Gales    Add EHXacsTable array to "garbage collection"       |
    3490                 : |                                                                             |
    3491                 : |  END_PROLOG                                                                 |
    3492                 : -----------------------------------------------------------------------------*/
    3493                 : intn
    3494                 : EHclose(int32 fid)
    3495              14 : {
    3496              14 :     intn            status = 0; /* routine return status variable */
    3497                 : 
    3498                 :     int32           HDFfid; /* HDF file ID */
    3499                 :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    3500                 :     int32           fid0; /* HDF EOS file id - offset */
    3501                 : 
    3502                 : 
    3503                 :     /* Check for valid HDFEOS file ID range */
    3504                 :     /* ------------------------------------ */
    3505              28 :     if (fid >= EHIDOFFSET && fid < NEOSHDF + EHIDOFFSET)
    3506                 :     {
    3507                 :   /* Compute "reduced" file ID */
    3508                 :   /* ------------------------- */
    3509              14 :   fid0 = fid % EHIDOFFSET;
    3510                 : 
    3511                 : 
    3512                 :   /* Get HDF file ID and SD interface ID */
    3513                 :   /* ----------------------------------- */
    3514              14 :   HDFfid = EHXfidTable[fid0];
    3515              14 :   sdInterfaceID = EHXsdTable[fid0];
    3516                 : 
    3517                 :   /* "Close" SD interface, Vgroup interface, and HDF file */
    3518                 :   /* ---------------------------------------------------- */
    3519              14 :   status = SDend(sdInterfaceID);
    3520              14 :   status = Vend(HDFfid);
    3521              14 :   status = Hclose(HDFfid);
    3522                 : 
    3523                 :   /* Clear out external array entries */
    3524                 :   /* -------------------------------- */
    3525              14 :   EHXtypeTable[fid0] = 0;
    3526              14 :   EHXacsTable[fid0] = 0;
    3527              14 :   EHXfidTable[fid0] = 0;
    3528              14 :   EHXsdTable[fid0] = 0;
    3529                 :     } else
    3530                 :     {
    3531               0 :   status = -1;
    3532               0 :   HEpush(DFE_RANGE, "EHclose", __FILE__, __LINE__);
    3533               0 :   HEreport("Invalid file id: %d.  ID must be >= %d and < %d.\n",
    3534                 :      fid, EHIDOFFSET, NEOSHDF + EHIDOFFSET);
    3535                 :     }
    3536                 : 
    3537              14 :     return (status);
    3538                 : }
    3539                 : 
    3540                 : /*----------------------------------------------------------------------------|
    3541                 : |  BEGIN_PROLOG                                                               |
    3542                 : |                                                                             |
    3543                 : |  FUNCTION: EHnumstr                                                         |
    3544                 : |                                                                             |
    3545                 : |  DESCRIPTION: Returns numerical type code of the given string               |
    3546                 : |               representation.                                               |
    3547                 : |                                                                             |
    3548                 : |                                                                             |
    3549                 : |  Return Value    Type     Units     Description                             |
    3550                 : |  ============   ======  =========   =====================================   |
    3551                 : |  numbertype     int32               numerical type code                     |
    3552                 : |                                                                             |
    3553                 : |  INPUTS:                                                                    |
    3554                 : |  strcode        const char          string representation of the type code  |
    3555                 : |                                                                             |
    3556                 : |                                                                             |
    3557                 : |  OUTPUTS:                                                                   |
    3558                 : |             None                                                            |
    3559                 : |                                                                             |
    3560                 : |  NOTES:                                                                     |
    3561                 : |                                                                             |
    3562                 : |                                                                             |
    3563                 : |   Date     Programmer   Description                                         |
    3564                 : |  ======   ============  =================================================   |
    3565                 : |  Nov 07   Andrey Kiselev  Original Programmer                               |
    3566                 : |                                                                             |
    3567                 : |  END_PROLOG                                                                 |
    3568                 : -----------------------------------------------------------------------------*/
    3569                 : int32
    3570                 : EHnumstr(const char *strcode)
    3571             127 : {
    3572             127 :     if (strcmp(strcode, "DFNT_UCHAR8") == 0)
    3573               0 :         return DFNT_UCHAR8;
    3574             127 :     else if (strcmp(strcode, "DFNT_CHAR8") == 0)
    3575               0 :         return DFNT_CHAR8;
    3576             127 :     else if (strcmp(strcode, "DFNT_FLOAT32") == 0)
    3577               0 :         return DFNT_FLOAT32;
    3578             127 :     else if (strcmp(strcode, "DFNT_FLOAT64") == 0)
    3579               0 :         return DFNT_FLOAT64;
    3580             127 :     else if (strcmp(strcode, "DFNT_INT8") == 0)
    3581               0 :         return DFNT_INT8;
    3582             127 :     else if (strcmp(strcode, "DFNT_UINT8") == 0)
    3583               0 :         return DFNT_UINT8;
    3584             127 :     else if (strcmp(strcode, "DFNT_INT16") == 0)
    3585             118 :         return DFNT_INT16;
    3586               9 :     else if (strcmp(strcode, "DFNT_UINT16") == 0)
    3587               9 :         return DFNT_UINT16;
    3588               0 :     else if (strcmp(strcode, "DFNT_INT32") == 0)
    3589               0 :         return DFNT_INT32;
    3590               0 :     else if (strcmp(strcode, "DFNT_UINT32") == 0)
    3591               0 :         return DFNT_UINT32;
    3592                 :     else
    3593               0 :         return DFNT_NONE;
    3594                 : }
    3595                 : 

Generated by: LTP GCOV extension version 1.5