[Top] [Prev] [Next]

create_sds.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf" 
#define SDS_NAME      "SDStemplate" 
#define X_LENGTH      5
#define Y_LENGTH      16
#define RANK          2  /* Number of dimensions of the SDS */

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id;     /* SD interface and data set identifiers */
   int32 dim_sizes[2];      /* sizes of the SDS dimensions */
   intn  status;            /* status returned by some routines; has value
                               SUCCEED or FAIL */

   /********************* End of variable declaration ***********************/

   /*
   * Create the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_CREATE);

   /*
   * Define the dimensions of the array to be created.
   */
   dim_sizes[0] = Y_LENGTH;
   dim_sizes[1] = X_LENGTH;

   /*
   * Create the data set with the name defined in SDS_NAME. Note that 
   * DFNT_INT32 indicates that the SDS data is of type int32. Refer to
   * Table 2E for definitions of other types. 
   */
   sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes);

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

create_sds.f

      program  create_SDS 
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      character*11 SDS_NAME
      integer      X_LENGTH, Y_LENGTH, RANK
      parameter   (FILE_NAME = 'SDS.hdf',
     +             SDS_NAME = 'SDStemplate',
     +             X_LENGTH = 5,
     +             Y_LENGTH = 16,
     +             RANK     = 2)
      integer      DFACC_CREATE, DFNT_INT32
      parameter   (DFACC_CREATE = 4,
     +             DFNT_INT32 = 24)
C
C     Function declaration.
C
      integer sfstart, sfcreate, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, dim_sizes(2)
      integer status
C
C**** End of variable declaration ************************************
C
C
C     Create the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_CREATE)
C
C     Define dimensions of the array to be created.
C
      dim_sizes(1) = X_LENGTH
      dim_sizes(2) = Y_LENGTH
C
C     Create the array with the name defined in SDS_NAME.
C     Note that DFNT_INT32 indicates that the SDS data is of type
C     integer. Refer to Tables 2E and 2I for the definition of other types.  
C
      sds_id = sfcreate(sd_id, SDS_NAME, DFNT_INT32, RANK, 
     .                  dim_sizes)
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end

write_to_sds.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"
#define X_LENGTH      5
#define Y_LENGTH      16

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index;
   intn  status;
   int32 start[2], edges[2];
   int32 data[Y_LENGTH][X_LENGTH];
   int   i, j;

   /********************* End of variable declaration ***********************/

   /*
   * Data set data initialization.
   */
   for (j = 0; j < Y_LENGTH; j++) {
       for (i = 0; i < X_LENGTH; i++)
            data[j][i] = (i + j) + 1;
   }

   /*
   * Open the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_WRITE);

   /*
   * Attach to the first data set.
   */ 
   sds_index = 0;
   sds_id = SDselect (sd_id, sds_index);

   /* 
   * Define the location and size of the data to be written to the data set.
   */
   start[0] = 0;
   start[1] = 0;
   edges[0] = Y_LENGTH;
   edges[1] = X_LENGTH;

   /*
   * Write the stored data to the data set. The third argument is set to NULL
   * to specify contiguous data elements. The last argument must
   * be explicitly cast to a generic pointer since SDwritedata is designed
   * to write generic data. 
   */
   status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)data);

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

write_to_sds.f

      program  write_data
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      character*11 SDS_NAME
      integer      X_LENGTH, Y_LENGTH, RANK
      parameter   (FILE_NAME = 'SDS.hdf',
     +             SDS_NAME = 'SDStemplate',
     +             X_LENGTH = 5,
     +             Y_LENGTH = 16,
     +             RANK     = 2)
      integer      DFACC_WRITE, DFNT_INT32
      parameter   (DFACC_WRITE = 2,
     +             DFNT_INT32 = 24)
C
C     Function declaration.
C

      integer sfstart, sfselect, sfwdata, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index, status
      integer start(2), edges(2), stride(2)
      integer i, j
      integer data(X_LENGTH, Y_LENGTH)
C
C**** End of variable declaration ************************************
C

C
C     Data set data initialization. 
C
      do 20 j = 1, Y_LENGTH
         do 10 i = 1, X_LENGTH
            data(i, j) = i + j - 1
10         continue
20    continue

C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_WRITE)

C
C     Attach to the first data set.
C
      sds_index = 0
      sds_id = sfselect(sd_id, sds_index)

C
C     Define the location and size of the data to be written
C     to the data set. Note that setting values of the array stride to 1
C     specifies the contiguous writing of data.
C
      start(1) = 0
      start(2) = 0
      edges(1) = X_LENGTH
      edges(2) = Y_LENGTH
      stride(1) = 1
      stride(2) = 1
C
C     Write the stored data to the data set named in SDS_NAME.
C     Note that the routine sfwdata is used instead of sfwcdata 
C     to write the numeric data.
C
      status = sfwdata(sds_id, start, stride, edges, data)
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end

write_slab.c

#include "mfhdf.h"

#define FILE_NAME     "SLABS.hdf"
#define SDS_NAME      "FilledBySlabs"
#define X_LENGTH      4
#define Y_LENGTH      5
#define Z_LENGTH      6
#define RANK          3  

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id;
   intn  status;
   int32 dim_sizes[3], start[3], edges[3];
   int32 data[Z_LENGTH][Y_LENGTH][X_LENGTH];
   int32 zx_data[Z_LENGTH][X_LENGTH];
   int   i, j, k;
 
   /********************* End of variable declaration ***********************/

   /*
   * Data initialization. 
   */
   for (k = 0; k < Z_LENGTH; k++)
       for (j = 0; j < Y_LENGTH; j++)
           for (i = 0; i < X_LENGTH; i++)
               data[k][j][i] = (i + 1) + (j + 1) + (k + 1);

   /*
   * Create the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_CREATE);

   /*
   * Define dimensions of the array to be created.
   */
   dim_sizes[0] = Z_LENGTH;
   dim_sizes[1] = Y_LENGTH;
   dim_sizes[2] = X_LENGTH;

   /*
   * Create the array with the name defined in SDS_NAME.
   */
   sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes);

   /*
   * Set the parameters start and edges to write  
   * a 6x4 element slab of data to the data set; note
   * that edges[1] is set to 1 to define a 2-dimensional slab
   * parallel to the ZX plane.  
   * start[1] (slab position in the array) is initialized inside
   * the for loop.
   */
   edges[0] = Z_LENGTH;
   edges[1] = 1;
   edges[2] = X_LENGTH;
   start[0] = start[2] = 0;
   for (j = 0; j < Y_LENGTH; j++)
   {
       start[1] = j;
    
       /*
       * Initialize zx_data buffer (data slab).
       */ 
       for ( k = 0; k < Z_LENGTH; k++)
       {
           for ( i = 0; i < X_LENGTH; i++)
           {
                 zx_data[k][i] = data[k][j][i];
           }
   }

   /*
   * Write the data slab into the SDS array defined in SDS_NAME. 
   * Note that the 3rd parameter is NULL which indicates that consecutive
   * slabs in the Y direction are written.
   */
   status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)zx_data); 
   }

   /*
   * Terminate access to the data set.
   */ 
   status = SDendaccess (sds_id);
    
   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

write_slab.f

      program  write_slab
      implicit none
C
C     Parameter declaration.
C
      character*9  FILE_NAME
      character*13 SDS_NAME
      integer      X_LENGTH, Y_LENGTH, Z_LENGTH, RANK
      parameter   (FILE_NAME = 'SLABS.hdf',
     +             SDS_NAME = 'FilledBySlabs',
     +             X_LENGTH = 4,
     +             Y_LENGTH = 5,
     +             Z_LENGTH = 6,
     +             RANK     = 3)
      integer      DFACC_CREATE, DFNT_INT32
      parameter   (DFACC_CREATE = 4,
     +             DFNT_INT32 = 24)
C
C     Function declaration.
C
      integer sfstart, sfcreate, sfwdata, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id
      integer dim_sizes(3), start(3), edges(3), stride(3)
      integer i, j, k, status
      integer data(X_LENGTH, Y_LENGTH, Z_LENGTH)
      integer xz_data(X_LENGTH, Z_LENGTH)
C
C**** End of variable declaration ************************************
C
C
C     Data initialization.
C
      do 30 k = 1, Z_LENGTH
         do 20 j = 1, Y_LENGTH
            do 10 i = 1, X_LENGTH
               data(i, j, k) = i + j + k 
10            continue
20         continue
30    continue
C
C     Create the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_CREATE)
C
C     Define dimensions of the array to be created. 
C
      dim_sizes(1) = X_LENGTH   
      dim_sizes(2) = Y_LENGTH   
      dim_sizes(3) = Z_LENGTH   
C
C     Create the data set with the name defined in SDS_NAME. 
C
      sds_id = sfcreate(sd_id, SDS_NAME, DFNT_INT32, RANK, 
     .                  dim_sizes)
C
C     Set the parameters start and edges to write 
C     a 4x6 element slab of data to the data set;
C     note that edges(2) is set to 1 to define a 2 dimensional slab
C     parallel to the XZ plane;
C     start(2) (slab position in the array) is initialized inside the
C     for loop.
C 
      edges(1) = X_LENGTH
      edges(2) = 1
      edges(3) = Z_LENGTH
      start(1) = 0 
      start(3) = 0
      stride(1) = 1
      stride(2) = 1
      stride(3) = 1

      do 60 j = 1, Y_LENGTH
       start(2) = j - 1 
C
C     Initialize the buffer xz_data (data slab).
C
       do 50 k = 1, Z_LENGTH
        do 40 i = 1, X_LENGTH
         xz_data(i, k) = data(i, j, k)
40      continue
50     continue
C
C     Write the data slab into SDS array defined in SDS_NAME. 
C     Note that the elements of array stride are set to 1 to
C     specify that the consecutive slabs in the Y direction are written.
C
         status = sfwdata(sds_id, start, stride, edges, xz_data)
60    continue
C
C     Terminate access to the data set. 
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file. 
C
      status = sfend(sd_id)

      end


alter_sds_values.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index;
   intn  status;
   int32 start[2], edges[2];
   int32 new_data[2];
   int   i, j;

   /********************* End of variable declaration ***********************/
   /* 
   * Open the file and initialize the SD interface with write access.
   */
   sd_id = SDstart (FILE_NAME, DFACC_WRITE);

   /*
   * Select the first data set.
   */
   sds_index = 0;
   sds_id = SDselect (sd_id, sds_index);

   /*
   * Set up the start and edge parameters to write new element values
   * into 10th row, 2nd column place, and 11th row, 2nd column place. 
   */
   start[0] = 9;     /* starting at 10th row   */
   start[1] = 1;     /* starting at 2nd column */
   edges[0] = 2;     /* rows 10th and 11th     */
   edges[1] = 1;     /* column 2nd only        */
       
   /*
   * Initialize buffer with the new values to be written.
   */
   new_data[0] = new_data[1] = 1000; 
        
   /*
   * Write the new values. 
   */
   status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)new_data);

   /*
   * Terminate access to the data set.
   */ 
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

alter_sds_values.f

      program  alter_data
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      integer      DFACC_WRITE
      parameter   (FILE_NAME = 'SDS.hdf',
     +             DFACC_WRITE = 2)
C
C     Function declaration.
C
      integer sfstart, sfselect, sfwdata, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index
      integer start(2), edges(2), stride(2)
      integer status
      integer new_data(2) 
C
C**** End of variable declaration ************************************
C

C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_WRITE)
C
C     Select the first data set.
C     
      sds_index = 0
      sds_id = sfselect(sd_id, sds_index)

C
C     Initialize the start, edge, and stride parameters to write 
C     two elements into 2nd row, 10th column and 11th column places.
C     
C     Specify 2nd row.
C
      start(1) = 1 
C
C     Specify 10th column.
C
      start(2) = 9 
      edges(1) = 1 
C
C     Two elements are written along 2nd row.
C
      edges(2) = 2 
      stride(1) = 1
      stride(2) = 1
C
C     Initialize the new values to be written.
C
      new_data(1) = 1000
      new_data(2) = 1000
C
C     Write the new values. 
C
      status = sfwdata(sds_id, start, stride, edges, new_data)
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end


unlimited_sds.c

#include "mfhdf.h"

#define FILE_NAME     "SDSUNLIMITED.hdf"
#define SDS_NAME      "AppendableData"
#define X_LENGTH      10
#define Y_LENGTH      10
#define RANK          2

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index;
   intn  status;
   int32 dim_sizes[2];
   int32 data[Y_LENGTH][X_LENGTH], append_data[X_LENGTH];
   int32 start[2], edges[2];
   int   i, j;

   /********************* End of variable declaration ***********************/

   /*
   * Data initialization.
   */
   for (j = 0; j < Y_LENGTH; j++) 
   {
       for (i = 0; i < X_LENGTH; i++)
           data[j][i] = (i + 1) + (j + 1);
   }

   /*
   * Create the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_CREATE);

   /*
   * Define dimensions of the array. Make the first dimension 
   * appendable by defining its length to be unlimited.
   */
   dim_sizes[0] = SD_UNLIMITED;
   dim_sizes[1] = X_LENGTH;

   /*
   * Create the array data set.
   */
   sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes);

   /*
   * Define the location and the size of the data to be written 
   * to the data set. 
   */
   start[0] = start[1] = 0;
   edges[0] = Y_LENGTH;
   edges[1] = X_LENGTH;

   /*
   * Write the data. 
   */
   status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)data);

   /*
   * Terminate access to the array data set, terminate access 
   * to the SD interface, and close the file.
   */
   status = SDendaccess (sds_id);
   status = SDend (sd_id);

   /*
   * Store the array values to be appended to the data set.
   */
   for (i = 0; i < X_LENGTH; i++)
       append_data[i] = 1000 + i;

   /*
   * Reopen the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_WRITE);

   /*
   * Select the first data set. 
   */ 
   sds_index = 0; 
   sds_id = SDselect (sd_id, sds_index);
 
   /*  
   * Check if selected SDS is unlimited. If it is not, then terminate access
   * to the SD interface and close the file. 
   */
   if ( SDisrecord (sds_id) ) 
   {

   /*
   * Define the location of the append to start at the first column 
   * of the 11th row of the data set and to stop at the end of the
   * eleventh row.
   */
   start[0] = Y_LENGTH;
   start[1] = 0;
   edges[0] = 1;
   edges[1] = X_LENGTH;

   /*
   * Append data to the data set.
   */
   status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)append_data);
   }

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

unlimited_sds.f

      program append_sds
      implicit none
C
C     Parameter declaration.
C
      character*16  FILE_NAME
      character*14  SDS_NAME
      integer       X_LENGTH, Y_LENGTH, RANK
      parameter    (FILE_NAME = 'SDSUNLIMITED.hdf',
     +              SDS_NAME = 'AppendableData',
     +              X_LENGTH = 10, 
     +              Y_LENGTH = 10,
     +              RANK     = 2)
      integer       DFACC_CREATE, DFACC_WRITE, SD_UNLIMITED,
     +              DFNT_INT32
      parameter    (DFACC_CREATE = 4,
     +              DFACC_WRITE  = 2,
     +              SD_UNLIMITED = 0,
     +              DFNT_INT32 =   24)
C
C     Function declaration.
C
      integer sfstart, sfcreate, sfwdata, sfselect 
      integer sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index, status
      integer dim_sizes(2)
      integer start(2), edges(2), stride(2)
      integer i, j
      integer data (X_LENGTH, Y_LENGTH), append_data(X_LENGTH)
C
C**** End of variable declaration ************************************
C
C
C     Data initialization.
C 
      do 20 j = 1, Y_LENGTH
         do 10 i = 1, X_LENGTH
            data(i, j) = i + j
10      continue
20    continue
C
C     Create the file and initialize the SD interface. 
C
      sd_id = sfstart(FILE_NAME, DFACC_CREATE)
C
C     Define dimensions of the array. Make the
C     last dimension appendable by defining its length as unlimited.
C
      dim_sizes(1) = X_LENGTH
      dim_sizes(2) = SD_UNLIMITED

C     Create the array data set. 
      sds_id = sfcreate(sd_id, SDS_NAME, DFNT_INT32, RANK, 
     .                  dim_sizes)
C
C     Define the location and the size of the data to be written
C     to the data set. Note that the elements of array stride are
C     set to 1 for contiguous writing.    
C
      start(1) = 0
      start(2) = 0
      edges(1) = X_LENGTH
      edges(2) = Y_LENGTH
      stride(1) = 1
      stride(2) = 1
C
C     Write the data. 
C
      status = sfwdata(sds_id, start, stride, edges, data)
C
C     Terminate access to the data set, terminate access
C     to the SD interface, and close the file. 
C
      status = sfendacc(sds_id)
      status = sfend(sd_id)
C
C     Store the array values to be appended to the data set. 
C
      do 30 i = 1, X_LENGTH
         append_data(i) = 1000 + i - 1 
30    continue
C
C     Reopen the file and initialize the SD. 
C
      sd_id = sfstart(FILE_NAME, DFACC_WRITE)
C
C     Select the first data set. 
C
      sds_index = 0 
      sds_id = sfselect(sd_id, sds_index)
C
C     Define the location of the append to start at the 11th 
C     column of the 1st row and to stop at the end of the 10th row.
C
      start(1) = 0
      start(2) = Y_LENGTH
      edges(1) = X_LENGTH
      edges(2) = 1
C
C     Append the data to the data set. 
C
      status = sfwdata(sds_id, start, stride, edges, append_data)
C
C     Terminate access to the data set. 
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file. 
C
      status = sfend(sd_id)

      end


compress_sds.c

#include "mfhdf.h"

#define FILE_NAME      "SDScompressed.hdf"
#define SDS_NAME       "SDSgzip"
#define X_LENGTH       5
#define Y_LENGTH       16
#define RANK           2

main( ) 
{
   /************************* Variable declaration **************************/

   int32     sd_id, sds_id, sds_index;
   intn      status;
   int32     comp_type;    /* Compression flag */
   comp_info c_info;   /* Compression structure */
   int32     start[2], edges[2], dim_sizes[2];
   int32     data[Y_LENGTH][X_LENGTH];
   int       i, j;

   /********************* End of variable declaration ***********************/

   /*
   * Buffer array data and define array dimensions.
   */
   for (j = 0; j < Y_LENGTH; j++) 
   {
	for (i = 0; i < X_LENGTH; i++)
		data[j][i] = (i + j) + 1;
   }
   dim_sizes[0] = Y_LENGTH;
   dim_sizes[1] = X_LENGTH;

   /*
   * Create the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_CREATE);

   /*
   * Create the data set with the name defined in SDS_NAME. 
   */ 
   sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes);

   /*
   * Ininitialize compression structure element and compression
   * flag for GZIP compression and call SDsetcompress.
   *
   *   To use the Skipping Huffman compression method, initialize
   *          comp_type = COMP_CODE_SKPHUFF
   *          c_info.skphuff.skp_size = value
   *
   *   To use the RLE compression method, initialize
   *          comp_type = COMP_CODE_RLE
   *   No structure element needs to be initialized.
   */
   comp_type = COMP_CODE_DEFLATE;
   c_info.deflate.level = 6;
   status = SDsetcompress (sds_id, comp_type, &c_info); 

   /* 
   * Define the location and size of the data set
   * to be written to the file.
   */
   start[0] = 0;
   start[1] = 0;
   edges[0] = Y_LENGTH;
   edges[1] = X_LENGTH;

   /*
   * Write the stored data to the data set. The last argument 
   * must be explicitly cast to a generic pointer since SDwritedata
   * is designed to write generic data. 
   */
   status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)data);

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);

}

compress_sds.f

      program  write_compressed_data
      implicit none
C
C     Parameter declaration.
C
      character*17  FILE_NAME
      character*7   SDS_NAME
      integer       X_LENGTH, Y_LENGTH, RANK
      parameter    (FILE_NAME = 'SDScompressed.hdf',
     +              SDS_NAME = 'SDSgzip',
     +              X_LENGTH = 5,
     +              Y_LENGTH = 16,
     +              RANK     = 2)
      integer       DFACC_CREATE, DFNT_INT32
      parameter    (DFACC_CREATE = 4,
     +              DFNT_INT32 = 24)
      integer       COMP_CODE_DEFLATE
      parameter    (COMP_CODE_DEFLATE = 4)
      integer       DEFLATE_LEVEL
      parameter    (DEFLATE_LEVEL = 6)
C     To use Skipping Huffman compression method, declare 
C            integer   COMP_CODE_SKPHUFF
C            parameter(COMP_CODE_SKPHUFF = 3) 
C     To use RLE compression method, declare
C            integer   COMP_CODE_RLE
C            parameter(COMP_CODE_RLE = 1)
C
C
C     Function declaration.
C
      integer sfstart, sfcreate, sfwdata, sfendacc, sfend,
     +        sfscompress
C
C**** Variable declaration *******************************************
C
      integer  sd_id, sds_id, status
      integer  start(2), edges(2), stride(2), dim_sizes(2)
      integer  comp_type
      integer  comp_prm(1)
      integer  data(X_LENGTH, Y_LENGTH)
      integer  i, j
C
C**** End of variable declaration ************************************
C
C
C     Buffer array data and define array dimensions. 
C
      do 20 j = 1, Y_LENGTH
         do 10 i = 1, X_LENGTH
            data(i, j) = i + j - 1
10       continue
20    continue
      dim_sizes(1) = X_LENGTH
      dim_sizes(2) = Y_LENGTH
C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_CREATE)
C
C     Create the data set with the name SDS_NAME.  
C
      sds_id = sfcreate(sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes) 
C
C     Initialize compression parameter (deflate level)
C     and call sfscompress function
C     For Skipping Huffman compression, comp_prm(1) should be set
C     to skipping sizes value (skp_size).
C 
      comp_type   = COMP_CODE_DEFLATE
      comp_prm(1) = deflate_level
      status      = sfscompress(sds_id, comp_type, comp_prm(1))
C
C     Define the location and size of the data that will be written to
C     the data set.
C
      start(1) = 0
      start(2) = 0
      edges(1) = X_LENGTH
      edges(2) = Y_LENGTH
      stride(1) = 1
      stride(2) = 1
C
C     Write the stored data to the data set.
C
      status = sfwdata(sds_id, start, stride, edges, data)
C
C     Terminate access to the  data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end


mv_sds_to_external.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"
#define EXT_FILE_NAME "ExternalSDS"
#define OFFSET        24

main( ) 
{

   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index, offset;
   intn  status;

   /********************* End of variable declaration ***********************/

   /*
   * Open the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_WRITE);

   /*
   * Select the first data set.
   */
   sds_index = 0;   
   sds_id = SDselect (sd_id, sds_index);

   /*
   * Create a file with the name EXT_FILE_NAME and move the data set
   * values into it, starting at byte location OFFSET.
   */
   status = SDsetexternalfile (sds_id, EXT_FILE_NAME, OFFSET);

   /*
   * Terminate access to the data set, SD interface, and file.
   */
   status = SDendaccess (sds_id);
   status = SDend (sd_id);
}

mv_sds_to_external.f

      program  write_extfile
      implicit none 
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      character*11 EXT_FILE_NAME
      integer      OFFSET
      integer      DFACC_WRITE
      parameter   (FILE_NAME      = 'SDS.hdf',
     +             EXT_FILE_NAME  = 'ExternalSDS',
     +             OFFSET         = 24,
     +             DFACC_WRITE    = 2)

C
C     Function declaration.
C
      integer sfstart, sfselect, sfsextf, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index, offset
      integer status
C
C**** End of variable declaration ************************************
C
C
C     Open the HDF file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_WRITE)
C
C     Select the first data set.
C
      sds_index = 0
      sds_id = sfselect(sd_id, sds_index)
C
C     Create a file with the name EXT_FILE_NAME and move the data set
C     into it, starting at byte location OFFSET.
C
      status = sfsextf(sds_id, EXT_FILE_NAME, OFFSET)
C
C     Terminate access to the data set. 
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file. 
C
      status = sfend(sd_id)

      end

read_from_sds.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"
#define X_LENGTH      5
#define Y_LENGTH      16

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index;
   intn  status;
   int32 start[2], edges[2];
   int32 data[Y_LENGTH][X_LENGTH];
   int   i, j;

   /********************* End of variable declaration ***********************/

   /*
   * Open the file for reading and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_READ);

   /*
   * Select the first data set.
   */ 
   sds_index = 0;
   sds_id = SDselect (sd_id, sds_index);

   /* 
   * Set elements of array start to 0, elements of array edges 
   * to SDS dimensions,and use NULL for the argument stride in SDreaddata
   * to read the entire data.
   */
   start[0] = 0;
   start[1] = 0;
   edges[0] = Y_LENGTH;
   edges[1] = X_LENGTH;

   /*
   * Read entire data into data array.
   */
   status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)data);

   /* 
   * Print 10th row; the following numbers should be displayed.
   *
   *         10 1000 12 13 14
   */
   for (j = 0; j < X_LENGTH; j++) printf ("%d ", data[9][j]);
   printf ("\n");

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

read_from_sds.f

      program  read_data
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      integer      X_LENGTH, Y_LENGTH
      parameter   (FILE_NAME  = 'SDS.hdf',
     +             X_LENGTH = 5, 
     +             Y_LENGTH = 16)
      integer      DFACC_READ, DFNT_INT32
      parameter   (DFACC_READ = 1,
     +             DFNT_INT32 = 24)

C
C     Function declaration.
C
      integer sfstart, sfselect, sfrdata, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index, status
      integer start(2), edges(2), stride(2)
      integer data(X_LENGTH, Y_LENGTH)
      integer j
C
C**** End of variable declaration ************************************
C
C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_READ)

C
C     Select the first data set.
C
      sds_index = 0
      sds_id = sfselect(sd_id, sds_index)

C
C     Set elements of the array start to 0, elements of the array edges to 
C     SDS dimensions, and elements of the array stride to 1 to read the
C     entire data. 
C
      start(1) = 0
      start(2) = 0
      edges(1) = X_LENGTH
      edges(2) = Y_LENGTH
      stride(1) = 1
      stride(2) = 1
C
C     Read entire data into data array. Note that sfrdata is used
C     to read the numeric data. 
C
      status = sfrdata(sds_id, start, stride, edges, data)
      
C
C     Print 10th column; the following numbers are displayed:
C
C           10 1000 12 13 14
C
      write(*,*) (data(j,10), j = 1, X_LENGTH)
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end

read_subsets.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"
#define SUB1_LENGTH   5
#define SUB2_LENGTH   4
#define SUB3_LENGTH1  2 
#define SUB3_LENGTH2  3 

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index;
   intn  status;
   int32 start[2], edges[2], stride[2];
   int32 sub1_data[SUB1_LENGTH];
   int32 sub2_data[SUB2_LENGTH];
   int32 sub3_data[SUB3_LENGTH2][SUB3_LENGTH1];
   int   i, j;

   /********************* End of variable declaration ***********************/
        
   /*
   * Open the file for reading and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_READ);

   /*
   * Select the first data set.
   */ 
   sds_index = 0;
   sds_id = SDselect (sd_id, sds_index);
   /*
   *         Reading the first subset.
   *
   * Set elements of start, edges, and stride arrays to read
   * every 3rd element in the 2nd column starting at 4th row.   
   */
   start[0] = 3;   /* 4th row */
   start[1] = 1;   /* 2nd column */
   edges[0] = SUB1_LENGTH; /* SUB1_LENGTH elements are read along 2nd column*/
   edges[1] = 1;
   stride[0] = 3;  /* every 3rd element is read along 2nd column */
   stride[1] = 1;

   /*
   * Read the data from the file into sub1_data array.
   */
   status = SDreaddata (sds_id, start, stride, edges, (VOIDP)sub1_data);

   /* 
   * Print what we have just read; the following numbers should be displayed:
   *
   *             5 8 1000 14 17   
   */
   for (j = 0; j < SUB1_LENGTH; j++) printf ("%d ", sub1_data[j]);
   printf ("\n");

   /* 
   *         Reading the second subset.
   *
   * Set elements of start and edges arrays to read
   * first 4 elements of the 10th row. 
   */
   start[0] = 9;  /* 10th row  */
   start[1] = 0;  /* 1st column */
   edges[0] = 1; 
   edges[1] = SUB2_LENGTH; /* SUB2_LENGTH elements are read along 10th row */

   /*
   * Read data from the file into sub2_data array. Note that the third
   * parameter is set to NULL for contiguous reading.
   */
   status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)sub2_data);

   /* 
   * Print what we have just read; the following numbers should be displayed:
   *
   *            10 1000 12 13 
   */
   for (j = 0; j < SUB2_LENGTH; j++) printf ("%d ", sub2_data[j]);
   printf ("\n");

   /* 
   *         Reading the third subset.
   *
   * Set elements of the arrays start, edges, and stride to read
   * every 6th element in the column and 4th element in the row
   * starting at 1st column, 3d row.    
   */
   start[0] = 2;  /* 3d row */
   start[1] = 0;  /* 1st column */
   edges[0] = SUB3_LENGTH2; /* SUB3_LENGTH2 elements are read along
                               each column */
   edges[1] = SUB3_LENGTH1; /* SUB3_LENGTH1 elements are read along  
                               each row */
   stride[0] = 6; /* read every 6th element along each column */
   stride[1] = 4; /* read every 4th element along each row */

   /*
   * Read the data from the file into sub3_data array.
   */
   status = SDreaddata (sds_id, start, stride, edges, (VOIDP)sub3_data);

   /* 
   * Print what we have just read; the following numbers should be displayed:
   *
   *            3 7 
   *            9 13  
   *            15 19 
   */
   for ( j = 0; j < SUB3_LENGTH2; j++ ) {
       for (i = 0; i < SUB3_LENGTH1; i++) printf ("%d ", sub3_data[j][i]);
       printf ("\n");
   }
   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

read_subsets.f

      program  read_subsets
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      parameter   (FILE_NAME  = 'SDS.hdf')
      integer      DFACC_READ, DFNT_INT32
      parameter   (DFACC_READ = 1,
     +             DFNT_INT32 = 24)
      integer      SUB1_LENGTH, SUB2_LENGTH, SUB3_LENGTH1,
     +             SUB3_LENGTH2 
      parameter   (SUB1_LENGTH  = 5,
     +             SUB2_LENGTH  = 4, 
     +             SUB3_LENGTH1 = 2,
     +             SUB3_LENGTH2 = 3)

C
C     Function declaration.
C
      integer sfstart, sfselect, sfrdata, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index, status
      integer start(2), edges(2), stride(2)
      integer sub1_data(SUB1_LENGTH)
      integer sub2_data(SUB2_LENGTH)
      integer sub3_data(SUB3_LENGTH1,SUB3_LENGTH2)
      integer i, j
C
C**** End of variable declaration ************************************
C
C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_READ)
C
C     Select the first data set.
C
      sds_index = 0
      sds_id =sfselect(sd_id, sds_index)
C
C            Reading the first subset.
C
C     Set elements of start, stride, and edges arrays to read 
C     every 3d element in in the 2nd row starting in the 4th column. 
C
      start(1) = 1 
      start(2) = 3 
      edges(1) = 1 
      edges(2) = SUB1_LENGTH
      stride(1) = 1
      stride(2) = 3 
C
C     Read the data from sub1_data array. 
C
      status = sfrdata(sds_id, start, stride, edges, sub1_data)
      
C
C     Print what we have just read, the following numbers should be displayed:
C
C           5 8 1000 14 17 
C
      write(*,*) (sub1_data(j), j = 1, SUB1_LENGTH)
C
C            Reading the second subset.
C
C     Set elements of start, stride, and edges arrays to read 
C     first 4 elements of 10th column. 
C
      start(1) = 0 
      start(2) = 9 
      edges(1) = SUB2_LENGTH 
      edges(2) = 1 
      stride(1) = 1
      stride(2) = 1 
C
C     Read the data into sub2_data array. 
C
      status = sfrdata(sds_id, start, stride, edges, sub2_data)
      
C
C     Print what we have just read; the following numbers should be displayed:
C
C          10 1000 12 13 
C
      write(*,*) (sub2_data(j), j = 1, SUB2_LENGTH)
C
C            Reading the third subset.
C
C     Set elements of start, stride and edges arrays to read 
C     every 6th element in the row and every 4th element in the column
C     starting at 1st row, 3rd column.  
C
      start(1) = 0 
      start(2) = 2 
      edges(1) = SUB3_LENGTH1 
      edges(2) = SUB3_LENGTH2 
      stride(1) = 4 
      stride(2) = 6 
C
C     Read the data from the file into sub3_data array. 
C
      status = sfrdata(sds_id, start, stride, edges, sub3_data)
      
C
C     Print what we have just read; the following numbers should be displayed:
C
C         3 9 15
C         7 13 19 
C
      do 50 i = 1, SUB3_LENGTH1
         write(*,*) (sub3_data(i,j), j = 1, SUB3_LENGTH2)
50    continue    
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end

get_info.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id;
   intn  status;
   int32 n_datasets, n_file_attrs, index;
   int32 dim_sizes[MAX_VAR_DIMS];
   int32 rank, data_type, n_attrs;
   char  name[MAX_NC_NAME];
   int   i;

   /********************* End of variable declaration ***********************/

   /*
   * Open the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_READ);

   /*
   * Determine the number of data sets in the file and the number
   * of file attributes. 
   */
   status = SDfileinfo (sd_id, &n_datasets, &n_file_attrs);

   /*  
   * Access every data set and print its name, rank, dimension sizes,
   * data type, and number of attributes. 
   * The following information should be displayed:
   *
   *               name = SDStemplate
   *               rank = 2
   *               dimension sizes are : 16  5  
   *               data type is  24
   *               number of attributes is  0
   */
   for (index = 0; index < n_datasets; index++)
   {
       sds_id = SDselect (sd_id, index);
       status = SDgetinfo (sds_id, name, &rank, dim_sizes, 
                           &data_type, &n_attrs);

       printf ("name = %s\n", name);
       printf ("rank = %d\n", rank);
       printf ("dimension sizes are : ");
       for (i=0; i< rank; i++) printf ("%d  ", dim_sizes[i]);
       printf ("\n");
       printf ("data type is  %d\n", data_type);
       printf ("number of attributes is  %d\n", n_attrs);

       /*
       * Terminate access to the data set.
       */
       status = SDendaccess (sds_id);
   }

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}


get_info.f

      program get_data_set_info 
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      parameter   (FILE_NAME = 'SDS.hdf')
      integer      DFACC_READ, DFNT_INT32
      parameter   (DFACC_READ = 1,
     +             DFNT_INT32 = 24)
      integer      MAX_NC_NAME, MAX_VAR_DIMS
      parameter   (MAX_NC_NAME  = 256,
     +             MAX_VAR_DIMS = 32)
C
C     Function declaration.
C
      integer sfstart, sffinfo, sfselect, sfginfo
      integer sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id
      integer n_datasets, n_file_attrs, index
      integer status, n_attrs
      integer rank, data_type
      integer dim_sizes(MAX_VAR_DIMS)
      character name *(MAX_NC_NAME)
      integer i
C
C**** End of variable declaration ************************************
C
C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_READ)
C
C     Determine the number of data sets in the file and the number of 
C     file attributes. 
C
      status = sffinfo(sd_id, n_datasets, n_file_attrs)
C
C     Access every data set in the file and print its name, rank,
C     dimension sizes, data type, and number of attributes.
C     The following information should be displayed:
C
C                name = SDStemplate    
C                rank =   2
C                dimension sizes are :   5  16
C                data type is   24
C                number of attributes is   0
C
      do 10 index = 0, n_datasets - 1
         sds_id = sfselect(sd_id, index)
         status = sfginfo(sds_id, name, rank, dim_sizes, data_type, 
     .                    n_attrs)
         write(*,*)  "name = ", name(1:15)
         write(*,*)  "rank = ", rank
         write(*,*)  "dimension sizes are : ", (dim_sizes(i), i=1, rank)
         write(*,*)  "data type is ", data_type
         write(*,*)  "number of attributes is ", n_attrs   
C
C     Terminate access to the current data set.
C
         status = sfendacc(sds_id)
10    continue
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end

find_sds_by_name.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"
#define SDS_NAME      "SDStemplate"
#define WRONG_NAME    "WrongName"
#define X_LENGTH      5
#define Y_LENGTH      16

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index;
   intn  status;
   int32 start[2], edges[2];
   int32 data[Y_LENGTH][X_LENGTH];
   int   i, j;

   /********************* End of variable declaration ***********************/

   /*
   * Open the file for reading and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_READ);

   /*
   * Find index of the data set with the name specified in WRONG_NAME.
   * Error condition occurs, since the data set with that name does not exist 
   * in the file.
   */ 
   sds_index = SDnametoindex (sd_id, WRONG_NAME);
   if (sds_index == FAIL)
   printf ("Data set with the name \"WrongName\" does not exist\n"); 

   /*
   * Find index of the data set with the name specified in SDS_NAME and use 
   * the index to select the data set.
   */ 
   sds_index = SDnametoindex (sd_id, SDS_NAME);
   sds_id = SDselect (sd_id, sds_index);

   /* 
   * Set elements of the array start to 0, elements of the array edges to 
   * SDS dimensions, and use NULL for stride argument in SDreaddata to read
   * the entire data.
   */
   start[0] = 0;
   start[1] = 0;
   edges[0] = Y_LENGTH;
   edges[1] = X_LENGTH;

   /*
   * Read the entire data into the buffer named data.
   */
   status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)data);

   /* 
   * Print 10th row; the following numbers should be displayed:
   *
   *             10 1000 12 13 14
   */
   for (j = 0; j < X_LENGTH; j++) printf ("%d ", data[9][j]);
   printf ("\n");

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

find_sds_by_name.f

      program  locate_by_name
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      character*11 SDS_NAME
      character*9  WRONG_NAME
      integer      X_LENGTH, Y_LENGTH
      parameter   (FILE_NAME  = 'SDS.hdf',
     +             SDS_NAME   = 'SDStemplate',
     +             WRONG_NAME = 'WrongName',
     +             X_LENGTH = 5,
     +             Y_LENGTH = 16)
      integer      DFACC_READ, DFNT_INT32
      parameter   (DFACC_READ = 1,
     +             DFNT_INT32 = 24)

C
C     Function declaration.
C
      integer sfstart, sfn2index, sfselect, sfrdata, sfendacc, sfend
C
C**** Variable declaration *******************************************
C 
      integer sd_id, sds_id, sds_index, status
      integer start(2), edges(2), stride(2)
      integer data(X_LENGTH, Y_LENGTH)
      integer j
C
C**** End of variable declaration ************************************
C
C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_READ)
C
C     Find index of the data set with the name specified in WRONG_NAME. 
C     Error condition occurs, since a data set with this name 
C     does not exist in the file.
C
      sds_index = sfn2index(sd_id, WRONG_NAME)
      if (sds_index .eq. -1) then
        write(*,*) "Data set with the name ", WRONG_NAME,
     +             " does not exist"        
      endif
C
C     Find index of the data set with the name specified in SDS_NAME  
C     and use the index to attach to the data set. 
C
      sds_index = sfn2index(sd_id, SDS_NAME)
      sds_id    = sfselect(sd_id, sds_index)
C
C     Set elements of start array to 0, elements of edges array 
C     to SDS dimensions, and elements of stride array to 1 to read entire data. 
C
      start(1) = 0
      start(2) = 0
      edges(1) = X_LENGTH
      edges(2) = Y_LENGTH
      stride(1) = 1
      stride(2) = 1
C
C     Read entire data into array named data. 
C
      status = sfrdata(sds_id, start, stride, edges, data)
C
C     Print 10th column; the following numbers should be displayed:
C
C           10 1000 12 13 14
C
      write(*,*) (data(j,10), j = 1, X_LENGTH)
C
C     Terminate access to the data set. 
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end

set_get_dim_info.c

#include "mfhdf.h"


#define FILE_NAME     "SDS.hdf"
#define SDS_NAME      "SDStemplate"
#define DIM_NAME_X     "X_Axis"
#define DIM_NAME_Y     "Y_Axis"
#define NAME_LENGTH   6
#define X_LENGTH      5
#define Y_LENGTH      16
#define RANK          2

main( ) 
{
   /************************* Variable declaration **************************/

   int32   sd_id, sds_id, sds_index;
   intn    status;
   int32   dim_index, dim_id;
   int32   n_values, data_type, n_attrs;
   int16   data_X[X_LENGTH];    /* X dimension dimension scale */
   int16   data_X_out[X_LENGTH];
   float64 data_Y[Y_LENGTH];  /* Y dimension dimension scale */
   float64 data_Y_out[Y_LENGTH]; 
   char    dim_name[NAME_LENGTH];
   int     i, j, nrow;

   /********************* End of variable declaration ***********************/

   /*
   * Initialize dimension scales.
   */
   for (i=0; i < X_LENGTH; i++) data_X[i] = i;
   for (i=0; i < Y_LENGTH; i++) data_Y[i] = 0.1 * i;
      
   /* 
   * Open the file and initialize SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_WRITE);

   /*
   * Get the index of the data set specified in SDS_NAME.
   */
   sds_index = SDnametoindex (sd_id, SDS_NAME);

   /*
   * Select the data set corresponding to the returned index.
   */
   sds_id = SDselect (sd_id, sds_index);

   /* For each dimension of the data set specified in SDS_NAME,
   *  get its dimension identifier and set dimension name
   *  and dimension scale. Note that data type of dimension scale 
   *  can be different between dimensions and can be different from 
   *  SDS data type.
   */
   for (dim_index = 0; dim_index < RANK; dim_index++) 
   {
       /* 
       * Select the dimension at position dim_index.
       */
       dim_id = SDgetdimid (sds_id, dim_index);

       /* 
       * Assign name and dimension scale to selected dimension.
       */
       switch (dim_index)
       {
	case 0:	status = SDsetdimname (dim_id, DIM_NAME_Y);
                n_values = Y_LENGTH;
                status = SDsetdimscale (dim_id,n_values,DFNT_FLOAT64, \
                                       (VOIDP)data_Y);  
		break;
	case 1:	status = SDsetdimname (dim_id, DIM_NAME_X);
                n_values = X_LENGTH; 
                status = SDsetdimscale (dim_id,n_values,DFNT_INT16, \
                                       (VOIDP)data_X);  
		break;
	default: break;
       }

       /*
       * Get and display info about the dimension and its scale values.
       * The following information is displayed:
       *                         
       *         Information about 1 dimension:
       *         dimension name is Y_Axis
       *         number of scale values is 16
       *         dimension scale data type is float64
       *         number of dimension attributes is 0
       *
       *         Scale values are :
       *               0.000    0.100    0.200    0.300  
       *               0.400    0.500    0.600    0.700  
       *               0.800    0.900    1.000    1.100  
       *               1.200    1.300    1.400    1.500  
       *
       *         Information about 2 dimension:
       *         dimension name is X_Axis
       *         number of scale values is 5
       *         dimension scale data type is int16
       *         number of dimension attributes is 0
       *
       *         Scale values are :
       *               0  1  2  3  4
       */

       status = SDdiminfo (dim_id, dim_name, &n_values, &data_type, &n_attrs);
       printf ("Information about %d dimension:\n", dim_index+1);
       printf ("dimension name is %s\n", dim_name);
       printf ("number of scale values is %d\n", n_values);
       if( data_type == DFNT_FLOAT64)
       printf ("dimension scale data type is float64\n");
       if( data_type == DFNT_INT16)
       printf ("dimension scale data type is int16\n");
       printf ("number of dimension attributes is %d\n", n_attrs);
       printf ("\n");
       printf ("Scale values are :\n");
       switch (dim_index) 
       {
         case 0:  status = SDgetdimscale (dim_id, (VOIDP)data_Y_out);
                  nrow = 4;
                  for (i=0; i<n_values/nrow; i++ )
                  {
                      for (j=0; j<nrow; j++)
                          printf ("  %-6.3f", data_Y_out[i*nrow + j]);
                          printf ("\n");
                  }
                  break; 
         case 1:  status = SDgetdimscale (dim_id, (VOIDP)data_X_out);
                  for (i=0; i<n_values; i++) printf ("  %d", data_X_out[i]);
                  break; 
         default: break;
        }
        printf ("\n");
   } /*for dim_index */

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

set_get_dim_info.f

      program  dimension_info 
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      character*11 SDS_NAME
      character*6  DIM_NAME_X
      character*6  DIM_NAME_Y
      integer      X_LENGTH, Y_LENGTH, RANK
      parameter   (FILE_NAME  = 'SDS.hdf',
     +             SDS_NAME   = 'SDStemplate',
     +             DIM_NAME_X  = 'X_Axis',
     +             DIM_NAME_Y  = 'Y_Axis',
     +             X_LENGTH = 5,
     +             Y_LENGTH = 16,
     +             RANK     = 2)
      integer      DFACC_WRITE, DFNT_INT16, DFNT_FLOAT64
      parameter   (DFACC_WRITE   = 2,
     +             DFNT_INT16   = 22,
     +             DFNT_FLOAT64 = 6)

C
C     Function declaration.
C
      integer sfstart, sfn2index, sfdimid, sfgdinfo
      integer sfsdscale, sfgdscale, sfsdmname, sfendacc
      integer sfend, sfselect
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index, status
      integer dim_index, dim_id
      integer n_values, n_attrs, data_type
      integer*2 data_X(X_LENGTH)
      integer*2 data_X_out(X_LENGTH)
      real*8    data_Y(Y_LENGTH)
      real*8    data_Y_out(Y_LENGTH)
      character*6 dim_name
      integer   i
C
C**** End of variable declaration ************************************
C
C
C     Initialize dimension scales.
C
      do 10 i = 1, X_LENGTH  
         data_X(i) = i - 1
10     continue

      do 20 i = 1, Y_LENGTH  
         data_Y(i) = 0.1 * (i - 1)
20     continue
C
C     Open the file and initialize SD interface.
C 
      sd_id = sfstart(FILE_NAME, DFACC_WRITE)
C
C     Get the index of the data set with the name specified in SDS_NAME. 
C
      sds_index = sfn2index(sd_id, SDS_NAME)
C
C     Select the data set corresponding to the returned index.
C
      sds_id = sfselect(sd_id, sds_index)
C
C     For each dimension of the data set,
C     get its dimension identifier and set dimension name
C     and dimension scales. Note that data type of dimension scale can
C     be different between dimensions and can be different from SDS data type.
C
      do 30 dim_index = 0, RANK - 1 
C
C        Select the dimension at position dim_index.
C
         dim_id = sfdimid(sds_id, dim_index)
C
C        Assign name and dimension scale to the dimension. 
C
         if (dim_index .eq. 0) then
            status = sfsdmname(dim_id, DIM_NAME_X) 
            n_values = X_LENGTH
            status = sfsdscale(dim_id, n_values, DFNT_INT16, data_X)
         end if
         if (dim_index .eq. 1) then
            status = sfsdmname(dim_id, DIM_NAME_Y)
            n_values = Y_LENGTH
            status = sfsdscale(dim_id, n_values, DFNT_FLOAT64, data_Y)
         end if
C
C      Get and display information about dimension and its scale values.
C      The following information is displayed:
C
C                    Information about 1 dimension :
C                    dimension name is X_Axis
C                    number of scale values is  5
C                    dimension scale data type is int16
C
C                    number of dimension attributes is   0
C                    Scale values are:
C                        0  1  2  3  4
C
C                    Information about 2 dimension :
C                    dimension name is Y_Axis
C                    number of scale values is  16
C                    dimension scale data type is float64
C                    number of dimension attributes is   0
C
C                    Scale values are:
C                        0.000      0.100      0.200      0.300
C                        0.400      0.500      0.600      0.700
C                        0.800      0.900      1.000      1.100
C                        1.200      1.300      1.400      1.500
C
       status = sfgdinfo(dim_id, dim_name, n_values, data_type, n_attrs)
C
       write(*,*) "Information about ", dim_index+1," dimension :"
       write(*,*) "dimension name is ", dim_name
       write(*,*) "number of scale values is", n_values
       if (data_type. eq. 22) then
           write(*,*) "dimension scale data type is int16"
       endif 
       if (data_type. eq. 6) then
           write(*,*) "dimension scale data type is float64"
       endif 
       write(*,*) "number of dimension attributes is ", n_attrs
C
       write(*,*) "Scale values are:"
       if (dim_index .eq. 0) then
          status = sfgdscale(dim_id, data_X_out)
          write(*,*) (data_X_out(i), i= 1, X_LENGTH)
       endif 
       if (dim_index .eq. 1)  then
          status = sfgdscale(dim_id, data_Y_out) 
          write(*,100) (data_Y_out(i), i= 1, Y_LENGTH)
100       format(4(1x,f10.3)/)
       endif 
30      continue
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)
      end

dimscale_vs_sds.c

#include "mfhdf.h"

#define FILE_NAME     "SDS.hdf"

main( ) 
{
   /************************* Variable declaration **************************/

   int32 sd_id, sds_id, sds_index;
   intn  status;
   int32 rank, data_type, dim_sizes[MAX_VAR_DIMS];
   int32 n_datasets, n_file_attr, n_attrs; 
   char  sds_name[MAX_NC_NAME];

   /********************* End of variable declaration ***********************/

   /*
   * Open the file and initialize the SD interface.
   */
   sd_id = SDstart(FILE_NAME, DFACC_READ);

   /*
   * Obtain information about the file.
   */ 
   status = SDfileinfo(sd_id, &n_datasets, &n_file_attr);

   /* Get information about each SDS in the file.
   *  Check whether it is a coordinate variable, then display retrieved 
   *  information.
   *  Output displayed:
   *
   *            SDS array with the name SDStemplate
   *            Coordinate variable with the name Y_Axis
   *            Coordinate variable with the name X_Axis
   *
   */
   for (sds_index=0; sds_index< n_datasets; sds_index++) 
   {
       sds_id = SDselect (sd_id, sds_index);
       status = SDgetinfo(sds_id, sds_name, &rank, dim_sizes, &data_type, &n_attrs);
       if (SDiscoordvar(sds_id)) 
          printf(" Coordinate variable with the name %s\n", sds_name);
       else
          printf(" SDS array with the name %s\n", sds_name);

   /*
   * Terminate access to the selected data set.
   */
   status = SDendaccess(sds_id);

   }

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend(sd_id);
}

dimscale_vs_sds.f

      program  sds_vrs_coordvar 
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      parameter   (FILE_NAME = 'SDS.hdf')
      integer      DFACC_READ, DFNT_INT32
      parameter   (DFACC_READ = 1,
     +             DFNT_INT32 = 24)
      integer      MAX_VAR_DIMS
      parameter   (MAX_VAR_DIMS = 32)
C
C     Function declaration.
C 
      integer sfstart, sfselect, sfiscvar, sffinfo, sfginfo
      integer sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer       sd_id, sds_id, sds_index, status
      integer       rank, data_type
      integer       n_datasets, n_file_attrs, n_attrs
      integer       dim_sizes(MAX_VAR_DIMS)
      character*256 sds_name
C
C**** End of variable declaration ************************************
C
C
C     Open the file and initialize the SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_READ)
C
C     Obtain information about the file.
C 
      status = sffinfo(sd_id, n_datasets, n_file_attrs) 
C
C     Get information about each SDS in the file.
C     Check whether it is a coordinate variable, then display retrieved 
C     information. 
C     Output displayed:
C 
C           SDS array with the name SDStemplate
C           Coordinate variable with the name X_Axis
C           Coordinate variable with the name Y_Axis
C
      do 10 sds_index = 0, n_datasets-1
         sds_id = sfselect(sd_id, sds_index)
         status = sfginfo(sds_id, sds_name, rank, dim_sizes,
     +                    data_type, n_attrs)
         status = sfiscvar(sds_id)
         if (status .eq. 1) then
             write(*,*) "Coordinate variable with the name ",
     +       sds_name(1:6) 
         else
             write(*,*) "SDS array with the name ", 
     +       sds_name(1:11) 
         endif
C
C        Terminate access to the data set.
C
         status = sfendacc(sds_id)
10    continue
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)
      end

set_attr.c

#include "mfhdf.h"

#define FILE_NAME      "SDS.hdf"
#define FILE_ATTR_NAME "File_contents"
#define SDS_ATTR_NAME  "Valid_range"
#define DIM_ATTR_NAME  "Dim_metric"

main( ) 
{
   /************************* Variable declaration **************************/

   int32   sd_id, sds_id, sds_index;
   intn    status;
   int32   dim_id, dim_index;
   int32   n_values;                /* number of values of the file, SDS or
                                       dimension attribute         */
   char8   file_values[] = "Storm_track_data"; 
                                   /* values of the file attribute */
   float32 sds_values[2] = {2., 10.};
                                   /* values of the SDS attribute  */
   char8   dim_values[]  = "Seconds"; 
                                  /* values of the dimension attribute */

   /********************* End of variable declaration ***********************/

   /*
   * Open the file and initialize the SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_WRITE);

   /*
   * Set an attribute that describes the file contents.
   */
   n_values = 16;
   status = SDsetattr (sd_id, FILE_ATTR_NAME, DFNT_CHAR8, n_values, 
                       (VOIDP)file_values);

   /*
   * Select the first data set.
   */
   sds_index = 0;
   sds_id = SDselect (sd_id, sds_index);

   /* 
   * Assign attribute to the first SDS. Note that attribute values
   * may have different data type than SDS data.
   */
   n_values  = 2;
   status = SDsetattr (sds_id, SDS_ATTR_NAME, DFNT_FLOAT32, n_values, 
                       (VOIDP)sds_values);

   /*
   * Get the the second dimension identifier of the SDS.
   */
   dim_index = 1;
   dim_id = SDgetdimid (sds_id, dim_index);

   /*
   * Set an attribute of the dimension that specifies the dimension metric.
   */
   n_values = 7;
   status = SDsetattr (dim_id, DIM_ATTR_NAME, DFNT_CHAR8, n_values, 
                       (VOIDP)dim_values);

   /*
   * Terminate access to the data set.
   */
   status = SDendaccess (sds_id);

   /*
   * Terminate access to the SD interface and close the file.
   */
   status = SDend (sd_id);
}

set_attr.f

      program  set_attribs
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      character*13 FILE_ATTR_NAME
      character*11 SDS_ATTR_NAME
      character*10 DIM_ATTR_NAME
      parameter   (FILE_NAME = 'SDS.hdf',
     +             FILE_ATTR_NAME = 'File_contents',
     +             SDS_ATTR_NAME  = 'Valid_range',
     +             DIM_ATTR_NAME  = 'Dim_metric')
      integer      DFACC_WRITE, DFNT_CHAR8, DFNT_FLOAT32
      parameter   (DFACC_WRITE = 2,
     +             DFNT_CHAR8  = 4,
     +             DFNT_FLOAT32 = 5)
C
C     Function declaration.
C
      integer sfstart, sfscatt, sfsnatt, sfselect, sfdimid
      integer sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer sd_id, sds_id, sds_index, status
      integer dim_id, dim_index 
      integer n_values
      character*16 file_values
      real         sds_values(2)
      character*7  dim_values
      file_values   = 'Storm_track_data'
      sds_values(1) = 2.
      sds_values(2) = 10.
      dim_values    = 'Seconds'
C
C**** End of variable declaration ************************************
C
C
C     Open the file and initialize the SD interface. 
C
      sd_id = sfstart(FILE_NAME, DFACC_WRITE)
C
C     Set an attribute that describes the file contents. 
C
      n_values = 16
      status = sfscatt(sd_id, FILE_ATTR_NAME, DFNT_CHAR8, n_values, 
     +                 file_values)
C
C     Select the first data set. 
C
      sds_index = 0
      sds_id = sfselect(sd_id, sds_index)
C
C     Assign attribute to the first SDS. Note that attribute values
C     may have different data type than SDS data.
C
      n_values = 2
      status = sfsnatt(sds_id, SDS_ATTR_NAME, DFNT_FLOAT32, n_values, 
     +                 sds_values)
C
C     Get the identifier for the first dimension. 
C
      dim_index = 0 
      dim_id = sfdimid(sds_id, dim_index)
C
C     Set an attribute to the dimension that specifies the
C     dimension metric. 
C
      n_values = 7
      status = sfscatt(dim_id, DIM_ATTR_NAME, DFNT_CHAR8, n_values, 
     +                 dim_values)
C
C     Terminate access to the data set. 
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.
C
      status = sfend(sd_id)

      end

get_attr.c

#include "mfhdf.h"

#define FILE_NAME      "SDS.hdf"
#define FILE_ATTR_NAME "File_contents"
#define SDS_ATTR_NAME  "Valid_range"
#define DIM_ATTR_NAME  "Dim_metric"

main( ) 
{
   /************************* Variable declaration **************************/

   int32   sd_id, sds_id, dim_id;
   intn    status;
   int32   attr_index, data_type, n_values; 
   char    attr_name[MAX_NC_NAME];
   int8    *file_data;
   int8    *dim_data;
   float32 *sds_data;
   int     i;
   
   /********************* End of variable declaration ***********************/

   /*
   * Open the file and initialize SD interface.
   */
   sd_id = SDstart (FILE_NAME, DFACC_READ);

   /*
   * Find the file attribute defined by FILE_ATTR_NAME.
   */
   attr_index = SDfindattr (sd_id, FILE_ATTR_NAME);

   /*
   * Get information about the file attribute. Note that the first
   * parameter is an SD interface identifier.
   */
   status = SDattrinfo (sd_id, attr_index, attr_name, &data_type, &n_values);

   /*
   * Allocate a buffer to hold the attribute data.
   */
   file_data = (int8 *)malloc (n_values * sizeof (data_type));

   /*
   * Read the file attribute data.
   */
   status = SDreadattr (sd_id, attr_index, file_data);

   /*
   * Print out file attribute value. 
   */
   printf ("File attribute value is : %s\n", file_data);

   /*
   * Select the first data set.
   */
   sds_id = SDselect (sd_id, 0);

   /*
   * Find the data set attribute defined by SDS_ATTR_NAME. Note that the
   * first parameter is a data set identifier.
   */
   attr_index = SDfindattr (sds_id, SDS_ATTR_NAME);

   /*
   * Get information about the data set attribute.
   */
   status = SDattrinfo (sds_id, attr_index, attr_name, &data_type, &n_values);

   /*
   * Allocate a buffer to hold the data set attribute data.
   */
   sds_data = (float32 *)malloc (n_values * sizeof (data_type));

   /*
   * Read the SDS attribute data.
   */
   status = SDreadattr (sds_id, attr_index, sds_data);

   /*
   * Print out SDS attribute data type and values. 
   */
   if (data_type == DFNT_FLOAT32) 
                 printf ("SDS attribute data type is : float32\n");
   printf ("SDS attribute values are :  ");
   for (i=0; i<n_values; i++) printf (" %f", sds_data[i]);
   printf ("\n"); 

   /*    
   * Get the identifier for the second dimension of the SDS.
   */
   dim_id = SDgetdimid (sds_id, 1);

   /*
   * Find dimension attribute defined by DIM_ATTR_NAME.
   */
   attr_index = SDfindattr (dim_id, DIM_ATTR_NAME);

   /*
   * Get information about the dimension attribute.
   */
   status = SDattrinfo (dim_id, attr_index, attr_name, &data_type, &n_values);

   /*
   * Allocate a buffer to hold the dimension attribute data.
   */
   dim_data = (int8 *)malloc (n_values * sizeof (data_type));

   /*
   * Read the dimension attribute data.
   */
   status = SDreadattr (dim_id, attr_index, dim_data);

   /*
   * Print out dimension attribute value. 
   */
   printf ("Dimensional attribute values is : %s\n", dim_data);

   /*
   * Terminate access to the data set and to the SD interface and 
   * close the file.
   */
   status = SDendaccess (sds_id);
   status = SDend (sd_id);

   /*
   * Free all buffers.
   */
   free (dim_data);
   free (sds_data);
   free (file_data);

   /*   Output of this program is :
   *
   *    File attribute value is : Storm_track_data
   *    SDS attribute data type is : float32
   *    SDS attribute values are :   2.000000 10.000000
   *    Dimensional attribute values is : Seconds
   */
}

get_attr.f

      program  attr_info
      implicit none
C
C     Parameter declaration.
C
      character*7  FILE_NAME
      character*13 FILE_ATTR_NAME
      character*11 SDS_ATTR_NAME
      character*10 DIM_ATTR_NAME
      parameter   (FILE_NAME = 'SDS.hdf',
     +             FILE_ATTR_NAME = 'File_contents',
     +             SDS_ATTR_NAME  = 'Valid_range',
     +             DIM_ATTR_NAME  = 'Dim_metric')
      integer      DFACC_READ, DFNT_FLOAT32
      parameter   (DFACC_READ   = 1,
     +             DFNT_FLOAT32 = 5)

C
C     Function declaration.
C
      integer sfstart, sffattr, sfgainfo, sfrattr, sfselect
      integer sfdimid, sfendacc, sfend
C
C**** Variable declaration *******************************************
C
      integer      sd_id, sds_id, dim_id
      integer      attr_index, data_type, n_values, status
      real         sds_data(2)
      character*20 attr_name 
      character*16 file_data
      character*7  dim_data
      integer      i
C
C**** End of variable declaration ************************************
C
C
C     Open the file and initialize SD interface. 
C
      sd_id = sfstart(FILE_NAME, DFACC_READ)
C
C     Find the file attribute defined by FILE_ATTR_NAME.
C     Note that the first parameter is an SD interface identifier.
C
      attr_index = sffattr(sd_id, FILE_ATTR_NAME)
C
C     Get information about the file attribute. 
C
      status = sfgainfo(sd_id, attr_index, attr_name, data_type, 
     +			n_values)
C
C     Read the file attribute data. 
C
      status = sfrattr(sd_id, attr_index, file_data)
C
C     Print file attribute value.
C
      write(*,*) "File attribute value is : ", file_data 
C
C     Select the first data set. 
C
      sds_id = sfselect(sd_id, 0)
C
C     Find the data set attribute defined by SDS_ATTR_NAME.
C     Note that the first parameter is a data set identifier. 
C
      attr_index = sffattr(sds_id, SDS_ATTR_NAME)
C
C     Get information about the data set attribute. 
C
      status = sfgainfo(sds_id, attr_index, attr_name, data_type, 
     +			n_values)
C
C     Read the SDS attribute data. 
C
      status = sfrattr(sds_id, attr_index, sds_data)

C
C     Print SDS attribute data type and values. 
C
      if (data_type .eq. DFNT_FLOAT32)  then
         write(*,*) "SDS attribute data type is : float32 "  
      endif
      write(*,*) "SDS attribute values are  : " 
      write(*,*)  (sds_data(i), i=1, n_values) 
C
C     Get the identifier for the first dimension of the SDS. 
C
      dim_id = sfdimid(sds_id, 0)
C
C     Find the dimensional attribute defined by DIM_ATTR_NAME.
C     Note that the first parameter is a dimension identifier.
C
      attr_index = sffattr(dim_id, DIM_ATTR_NAME)
C
C     Get information about dimension attribute. 
C
      status = sfgainfo(dim_id, attr_index, attr_name, data_type, 
     +			n_values)
C
C     Read the dimension attribute data. 
C
      status = sfrattr(dim_id, attr_index, dim_data)
C
C     Print dimension attribute value.
C
      write(*,*) "Dimensional attribute value is : ", dim_data 
C
C     Terminate access to the data set. 
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file. 
C
      status = sfend(sd_id)
C
C     Output of this program is :
C
C
C     File attribute value is : Storm_track_data
C     SDS attribute data type is : float32 
C     SDS attribute values are  : 
C         2.00000   10.00000
C      Dimensional attribute value is : Seconds
C
      end

chunking_example.c

#include "mfhdf.h"

#define FILE_NAME     "SDSchunked.hdf"
#define SDS_NAME      "ChunkedData"
#define RANK          2

main()
{
   /************************* Variable declaration **************************/

   int32         sd_id, sds_id, sds_index;
   intn          status;
   int32         flag, maxcache, new_maxcache;
   int32         dim_sizes[2], origin[2];
   HDF_CHUNK_DEF c_def, c_def_out; /* Chunking definitions */ 
   int32         comp_flag, c_flags;
   int16         all_data[9][4];
   int32         start[2], edges[2];
   int16         chunk_out[3][2];
   int16         row[2] = { 5, 5 };
   int16         column[3] = { 4, 4, 4 };
   int16         fill_value = 0;   /* Fill value */
   int           i,j;
   /*
   * Declare chunks data type and initialize some of them. 
   */
          int16 chunk1[3][2] = { 1, 1,
                                 1, 1,
                                 1, 1 }; 

          int16 chunk2[3][2] = { 2, 2,
                                 2, 2,
                                 2, 2 }; 

          int16 chunk3[3][2] = { 3, 3,
                                 3, 3,
                                 3, 3 }; 

          int16 chunk6[3][2] = { 6, 6,
                                 6, 6,
                                 6, 6 };

    /********************* End of variable declaration ***********************/
    /*
    * Define chunk's dimensions.
    *
    *         In this example we do not use compression. 
    *         To use chunking with RLE, Skipping Huffman, and GZIP
    *         compression, initialize
    *
    *                c_def.comp.chunk_lengths[0] = 3;
    *                c_def.comp.chunk_lengths[1] = 2; 
    *
    *         To use chunking with NBIT, initialize
    *
    *                c_def.nbit.chunk_lengths[0] = 3;
    *                c_def.nbit.chunk_lengths[1] = 2; 
    *
    */
    c_def.chunk_lengths[0] = 3;
    c_def.chunk_lengths[1] = 2;

    /*
    * Create the file and initialize SD interface.
    */
    sd_id = SDstart (FILE_NAME, DFACC_CREATE);

    /*
    * Create 9x4 SDS.
    */
    dim_sizes[0] = 9;
    dim_sizes[1] = 4;
    sds_id = SDcreate (sd_id, SDS_NAME,DFNT_INT16, RANK, dim_sizes);

    /*
    * Fill the SDS array with the fill value.
    */
    status = SDsetfillvalue (sds_id, (VOIDP)&fill_value);

    /*
    * Create chunked SDS.
    * In this example we do not use compression ( third
    * parameter of SDsetchunk is set to HDF_CHUNK).
    *
    * To use RLE compresssion, set compression type and flag
    *
    *            c_def.comp.comp_type = COMP_CODE_RLE;
    *            comp_flag = HDF_CHUNK | HDF_COMP;
    *          
    * To use Skipping Huffman compression, set compression type, flag
    * and skipping size skp_size
    *
    *            c_def.comp.comp_type = COMP_CODE_SKPHUFF;
    *            c_def.comp.cinfo.skphuff.skp_size = value;             
    *            comp_flag = HDF_CHUNK | HDF_COMP;
    *
    * To use GZIP compression, set compression type, flag and
    * deflate level
    * 
    *            c_def.comp.comp_type = COMP_CODE_DEFLATE;
    *            c_def.comp.cinfo.deflate.level = value;             
    *            comp_flag = HDF_CHUNK | HDF_COMP;
    *
    * To use NBIT compression, set compression flag and
    * compression parameters
    *          
    *            comp_flag = HDF_CHUNK | HDF_NBIT;
    *            c_def.nbit.start_bit = value1;             
    *            c_def.nbit.bit_len   = value2;             
    *            c_def.nbit.sign_ext  = value3;             
    *            c_def.nbit.fill_one  = value4;             
    */
    comp_flag = HDF_CHUNK;
    status = SDsetchunk (sds_id, c_def, comp_flag);

    /*
    * Set chunk cache to hold maximum of 3 chunks.
    */
    maxcache = 3;
    flag = 0;
    new_maxcache = SDsetchunkcache (sds_id, maxcache, flag);

    /* 
    * Write chunks using SDwritechunk function.
    * Chunks can be written in any order.
    */

    /* 
    * Write the chunk with the coordinates (0,0).
    */
    origin[0] = 0;
    origin[1] = 0;
    status = SDwritechunk (sds_id, origin, (VOIDP) chunk1);

    /*   
    * Write the chunk with the coordinates (1,0).
    */
    origin[0] = 1;
    origin[1] = 0;
    status = SDwritechunk (sds_id, origin, (VOIDP) chunk3);

    /*   
    * Write the chunk with the coordinates (0,1). 
    */
    origin[0] = 0;
    origin[1] = 1;
    status = SDwritechunk (sds_id, origin, (VOIDP) chunk2);

    /* 
    * Write chunk with the coordinates (1,2) using
    * SDwritedata function.
    */
    start[0] = 6;
    start[1] = 2;
    edges[0] = 3;
    edges[1] = 2;
    status = SDwritedata (sds_id, start, NULL, edges, (VOIDP) chunk6); 

    /* 
    * Fill second column in the chunk with the coordinates (1,1)
    * using SDwritedata function.
    */
    start[0] = 3;
    start[1] = 3;
    edges[0] = 3;
    edges[1] = 1;
    status = SDwritedata (sds_id, start, NULL, edges, (VOIDP) column); 

    /* 
    * Fill second row in the chunk with the coordinates (0,2)
    * using SDwritedata function.
    */
    start[0] = 7;
    start[1] = 0;
    edges[0] = 1;
    edges[1] = 2;
    status = SDwritedata (sds_id, start, NULL, edges, (VOIDP) row); 
           
    /* 
    * Terminate access to the data set.
    */
    status = SDendaccess (sds_id);

    /*
    * Terminate access to the SD interface and close the file.
    */
    status = SDend (sd_id);

    /*
    * Reopen the file and access the first data set.
    */
    sd_id = SDstart (FILE_NAME, DFACC_READ);
    sds_index = 0;
    sds_id = SDselect (sd_id, sds_index);

    /*
    * Get information about the SDS. Only chunk lengths and compression
    * flag can be returned. Compression information is not available if
    * NBIT, Skipping Huffman, or GZIP compression is used.
    */
    status = SDgetchunkinfo (sds_id, &c_def_out, &c_flags);
    if (c_flags == HDF_CHUNK )
       printf(" SDS is chunked\nChunk's dimensions %dx%d\n",
              c_def_out.chunk_lengths[0],
              c_def_out.chunk_lengths[1]);
    else if (c_flags == (HDF_CHUNK | HDF_COMP))
         printf("SDS is chunked and compressed\nChunk's dimensions %dx%d\n",
                  c_def_out.comp.chunk_lengths[0],
                  c_def_out.comp.chunk_lengths[1]);
    else if (c_flags == (HDF_CHUNK | HDF_NBIT))
         printf ("SDS is chunked (NBIT)\nChunk's dimensions %dx%d\n",
                  c_def_out.nbit.chunk_lengths[0],
                  c_def_out.nbit.chunk_lengths[1]);

    /*
    * Read the entire data set using SDreaddata function.
    */
    start[0] = 0;
    start[1] = 0;
    edges[0] = 9;
    edges[1] = 4;
    status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)all_data);

    /* 
    * Print out what we have read.
    * The following information should be displayed:
    *
    * SDS is chunked 
    * Chunk's dimensions 3x2
    *          1 1 2 
    *          1 1 2 2
    *          1 1 2 2
    *          3 3 0 4
    *          3 3 0 4
    *          3 3 0 4
    *          0 0 6 6
    *          5 5 6 6
    *          0 0 6 6
    */
    for (j=0; j<9; j++) 
    {
         for (i=0; i<4; i++) printf (" %d", all_data[j][i]);
         printf ("\n");
    }

    /* 
    * Read chunk with the coordinates (2,0) and display it.
    */
    origin[0] = 2;
    origin[1] = 0;    	
    status = SDreadchunk (sds_id, origin, chunk_out);
    printf (" Chunk (2,0) \n");
    for (j=0; j<3; j++) 
    {
         for (i=0; i<2; i++) printf (" %d", chunk_out[j][i]);
         printf ("\n");
    }

    /* 
    * Read chunk with the coordinates (1,1) and display it.
    */
    origin[0] = 1;
    origin[1] = 1;    	
    status = SDreadchunk (sds_id, origin, chunk_out);
    printf (" Chunk (1,1) \n");
    for (j=0; j<3; j++) 
    {
         for (i=0; i<2; i++) printf (" %d", chunk_out[j][i]);
         printf ("\n");
    }

    /*  The following information is displayed:
    *
    *   Chunk (2,0) 
    *   0 0
    *   5 5
    *   0 0
    *   Chunk (1,1) 
    *   0 4
    *   0 4
    *   0 4
    */

    /* 
    * Terminate access to the data set.
    */
    status = SDendaccess (sds_id);

    /*
    * Terminate access to the SD interface and close the file.
    */
    status = SDend (sd_id);
}            

chunking_example.f

      program  chunk_examples
      implicit none
C
C     Parameter declaration.
C
      character*14 FILE_NAME
      character*11 SDS_NAME
      integer      RANK
      parameter   (FILE_NAME = 'SDSchunked.hdf',
     +             SDS_NAME  = 'ChunkedData',
     +             RANK      = 2)
      integer      DFACC_CREATE, DFACC_READ, DFNT_INT16
      parameter   (DFACC_CREATE = 4,
     +             DFACC_READ   = 1,
     +             DFNT_INT16   = 22)
      integer      COMP_CODE_NONE
      parameter   (COMP_CODE_NONE = 0)
C
C     This example does not use compression.
C
C     To use RLE compression, declare:
C
C     integer      COMP_CODE_RLE
C     parameter   (COMP_CODE_RLE = 1)
C
C     To use NBIT compression, declare:
C
C     integer      COMP_CODE_NBIT
C     parameter   (COMP_CODE_NBIT = 2)
C
C     To use Skipping Huffman compression, declare:
C
C     integer      COMP_CODE_SKPHUFF
C     parameter   (COMP_CODE_SKPHUFF = 3)
C
C     To use GZIP compression, declare:
C
C     integer      COMP_CODE_DEFLATE
C     parameter   (COMP_CODE_DEFLATE = 4)
C
C
C     Function declaration.
C
      integer sfstart, sfcreate, sfendacc, sfend,
     +        sfselect, sfsfill, sfschnk, sfwchnk,
     +        sfrchnk, sfgichnk, sfwdata, sfrdata,
     +        sfscchnk
C
C**** Variable declaration *******************************************
C
      integer   sd_id, sds_id, sds_index, status          
      integer   dim_sizes(2), origin(2)
      integer   fill_value, maxcache, new_maxcache, flag
      integer   start(2), edges(2), stride(2)
      integer*2 all_data(4,9)
      integer*2 row(3), column(2)
      integer*2 chunk_out(2,3)
      integer*2 chunk1(2,3),
     +          chunk2(2,3),
     +          chunk3(2,3),
     +          chunk6(2,3)
      integer   i, j
C
C     Compression flag and parameters.
C
      integer comp_type, comp_flag, comp_prm(4)
C
C     Chunk's dimensions.
C
      integer dim_length(2), dim_length_out(2)
C
C     Initialize four chunks
C
      data chunk1 /6*1/
      data chunk2 /6*2/
      data chunk3 /6*3/
      data chunk6 /6*6/
C
C     Initialize row and column arrays.
C
      data row /3*4/
      data column /2*5/
C
C**** End of variable declaration ************************************
C
C
C     Define chunk's dimensions.
C
      dim_length(1) = 2 
      dim_length(2) = 3 
C 
C     Create the file and initialize SD interface.
C
      sd_id = sfstart(FILE_NAME, DFACC_CREATE)

C
C     Create 4x9 SDS
C
      dim_sizes(1) = 4 
      dim_sizes(2) = 9 
      sds_id = sfcreate(sd_id, SDS_NAME, DFNT_INT16,
     +                  RANK, dim_sizes)
C
C     Fill SDS array with the fill value.
C
      fill_value = 0
      status = sfsfill( sds_id, fill_value)
C
C     Create chunked SDS.
C
C     In this example we do not use compression.
C
C     To use RLE compression, initialize comp_type parameter
C     before the call to sfschnk function.
C              comp_type = COMP_CODE_RLE
C
C     To use NBIT, Skipping Huffman, or GZIP compression,
C     initialize comp_prm array and comp type parameter
C     before call to sfschnk function
C
C     NBIT:
C              comp_prm(1) = value_of(sign_ext)
C              comp_prm(2) = value_of(fill_one)
C              comp_prm(3) = value_of(start_bit)
C              comp_prm(4) = value_of(bit_len)
C              comp_type   = COMP_CODE_NBIT
C
C     Skipping Huffman:
C              comp_prm(1) = value_of(skp_size)
C              comp_type   = COMP_CODE_SKPHUFF
C
C     GZIP:
C              comp_prm(1) = value_of(deflate_level) 
C              comp_type   = COMP_CODE_DEFLATE
C       
C
      comp_type = COMP_CODE_NONE
      status = sfschnk(sds_id, dim_length, comp_type, comp_prm)
C
C     Set chunk cache to hold maximum 2 chunks.
C
      flag = 0
      maxcache = 2
      new_maxcache = sfscchnk(sds_id, maxcache, flag) 
C
C     Write chunks using SDwritechunk function.
C     Chunks can be written in any order.
C
C     Write chunk with the coordinates (1,1).
C
      origin(1) = 1
      origin(2) = 1
      status = sfwchnk(sds_id, origin, chunk1) 
C
C     Write chunk with the coordinates (1,2).
C
      origin(1) = 1 
      origin(2) = 2 
      status = sfwchnk(sds_id, origin, chunk3) 
C
C     Write chunk with the coordinates (2,1).
C
      origin(1) = 2 
      origin(2) = 1 
      status = sfwchnk(sds_id, origin, chunk2) 
C
C     Write chunk with the coordinates (2,3).
C
      origin(1) = 2 
      origin(2) = 3 
      status = sfwchnk(sds_id, origin, chunk6) 
C
C     Fill second row in the chunk with the coordinates (2,2).
C
      start(1) = 3
      start(2) = 3
      edges(1) = 1 
      edges(2) = 3 
      stride(1) = 1
      stride(2) = 1
      status = sfwdata(sds_id, start, stride, edges, row)
C
C     Fill second column in the chunk with the coordinates (1,3).
C
      start(1) = 0 
      start(2) = 7 
      edges(1) = 2 
      edges(2) = 1 
      stride(1) = 1
      stride(2) = 1
      status = sfwdata(sds_id, start, stride, edges, column)
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.  
C
      status = sfend(sd_id)
C
C     Reopen the file and access the first data set.
C
      sd_id = sfstart(FILE_NAME, DFACC_READ)
      sds_index = 0
      sds_id = sfselect(sd_id, sds_index)
C
C     Get information about the SDS.
C
      status = sfgichnk(sds_id, dim_length_out, comp_flag)
      if (comp_flag .eq. 0) then
         write(*,*) 'SDS is chunked'
      endif
      if (comp_flag .eq. 1) then
         write(*,*) 'SDS is chunked and compressed'
      endif
      if (comp_flag .eq. 2) then
         write(*,*) 'SDS is chunked and NBIT compressed'
      endif
      write(*,*) 'Chunks dimensions are ', dim_length_out(1),
     + '  x' ,dim_length_out(2)
C
C     Read the whole SDS using sfrdata function and display
C     what we have read. The following information will be displayed:
C
C
C             SDS is chunked
C             Chunks dimensions are   2  x  3
C 
C             1  1  1  3  3  3  0  5  0
C             1  1  1  3  3  3  0  5  0
C             2  2  2  0  0  0  6  6  6
C             2  2  2  4  4  4  6  6  6
C
      start(1) = 0
      start(2) = 0
      edges(1) = 4
      edges(2) = 9
      stride(1) = 1
      stride(2) = 1
      status = sfrdata(sds_id, start, stride, edges, all_data)
C
C     Display the SDS.
C
      write(*,*)
      do 10 i = 1,4
         write(*,*) (all_data(i,j), j=1,9)
10    continue     
C
C     Read chunks with the coordinates (2,2) and (1,3) and display.
C     The following information will be shown:
C
C             Chunk (2,2)
C
C               0  0  0
C               4  4  4
C 
C             Chunk (1,3)
C
C               0  5  0
C               0  5  0
C
      origin(1) = 2
      origin(2) = 2
      status = sfrchnk(sds_id, origin, chunk_out)
      write(*,*) 
      write(*,*) 'Chunk (2,2)'
      write(*,*) 
      do 20 i = 1,2
         write(*,*) (chunk_out(i,j), j=1,3)
20    continue
C
      origin(1) = 1 
      origin(2) = 3 
      status = sfrchnk(sds_id, origin, chunk_out)
      write(*,*) 
      write(*,*) 'Chunk (1,3)'
      write(*,*) 
      do 30 i = 1,2
         write(*,*) (chunk_out(i,j), j=1,3)
30    continue
C
C     Terminate access to the data set.
C
      status = sfendacc(sds_id)
C
C     Terminate access to the SD interface and close the file.   
C
      status = sfend(sd_id)
      end 





[Top] [Prev] [Next]

hdfhelp@ncsa.uiuc.edu
HDF User's Guide - 07/21/98, NCSA HDF Development Group.