HDF: The Hierarchical Data Format 

by Brand Fortner



Example 1: 



Vdata Name:     Flux Table

Vdata Class:    Class_Table .

                Field #1        Field #2        Field #3

Field Types	int16		float32		char[5]

Field Names     ID#             Flux            Name

Record #1       1               2.34            CygX1

Record #2       2               -89.43          HerX1

Record #3       3               0.0023          CygX3

Record #4	4               1.115           Vela







Example 2: 



Location       Length   Value                           Comment



0 to 3          4       ^N^C^S^A                        Unique HDF number

4 to 5          2       3                               Number of DDs in block

6 to 9          4       0000                            Loc. of next DD block

                        Tag     Ref     Offset  Length

10 to 21        12      300     001     46      4       DD #1 (Image size)

22 to 33        12      1965    001     50      22      DD #2 (Vgroup #1)

34 to 45        12      1965    002     72      22      DD #3 (Vgroup #2)



                       X Size          Y Size

46 to 49        4       300             200             Image size



                         Name            Tag/Ref

50 to 71        22      'Vone'          1965/002        First VGroup

72 to 93        22      'Vtwo'           300/001        Second VGroup







Example 3: 



Tag Name                 Value(s)                                       Comments



DFTAG_DIL   'Neutron Star Accretion Simulation'		Title

DFTAG_SDD   2 Dimensions: 4 10                          Number and size of dimensions

            Float32         Float32         Float32	Number types

DFTAG_SDS               -1.67...1.70     30.75...32.11  Numerical scales

DFTAG_SDL   'Density'       'Time'          'Radius'    Labels

DFTAG_SDU   'gm/cm^3'       'sec'           'km'        Units

DFTAG_SDF     F7.4          F5.2            F5.2        Numerical Formats

DFTAG_SD    '0.5872,0.5872, ... ,1.4319,1.4352'		Actual data







Example 4: 



Tag Name   Field Name          Value(s)                  Comments



DFTAG_VH   <nvert>               4                   Number of records

           <isize>               11                  Row width in bytes

           <nfields>             3                   Number of fields

           <type>        int16   float32 char[5]     Field number types

           <isize>       2       4       5           Field size in bytes

           <offset>      0       2       6           Byte offset of field

           <fldnmlen>    5       4       4           Length of field Name

           <fldnm>       'ID.No' 'Flux'  'Name'      Field names

           <namelen>             19                  Length of Vdata name

           <name>         'Vdata Example Table'      Vdata name

DFTAG_VS                 1,2.34,'CygX1',...,'Vela'  Actual data







Listing One

#include "hdf.h"

#define WIDTH    5

#define HEIGHT    6

main(int argc, char *argv[])

{

    uint8 palette_data[768];

    intn i;

    /*  Initialize the image array  */

    static uint8 raster_data[HEIGHT][WIDTH] =

                { 1,  2,  3,  4,  5,

                  6,  7,  8,  9, 10,

                 11, 12, 13, 14, 15,

                 16, 17, 18, 19, 20,

                 21, 22, 23, 24, 25,

                 26, 27, 28, 29, 30 };



    /*  Initialize the palette to standard linear grayscale  */

    for (i=0; i<256; i++) {

    palette_data[i*3] = i;

    palette_data[i*3+1] = i;

    palette_data[i*3+2] = i;

    }

    /*  Associate the palette with the image  */

    DFR8setpalette(palette_data);

    /*  Write the 8-bit raster image to the file  */

    DFR8addimage("example.hdf", raster_data, WIDTH, HEIGHT, 0);

}





Listing Two 

#include "hdf.h"

#include "mfhdf.h"

#define LENGTH  3

#define HEIGHT  2

#define WIDTH   5



 main(int argc, char *argv[])

{

    /*  Initialize the image array  */

    static float64 scien_data[LENGTH][HEIGHT][WIDTH] =

                { 1.,  2.,  3.,  4.,  5.,

                  6.,  7.,  8.,  9., 10.,

                 11., 12., 13., 14., 15.,

                 16., 17., 18., 19., 20.,

                 21., 22., 23., 24., 25.,

                 26., 27., 28., 29., 30. };

    int32 dims[3]         = {LENGTH, HEIGHT, WIDTH};

    int16 scale0[LENGTH]  = {2, 4, 6};

    int32 scale1[HEIGHT]  = {1234567, 2345678};

    float32 scale2[WIDTH] = {2.2, 4.4, 6.6, 8.8, 11.0};

    float64 avg           = 15.0;

    int32 start[3]        = {0, 0, 0};

    int32 fid, sdid, dimid0, dimid1, dimid2;

    /*  Open file and initialize SD interface  */

    fid = SDstart("example.hdf", DFACC_CREATE);

    /*  Create named data set  */

    sdid = SDcreate(fid, "Sample Data Set", DFNT_FLOAT64, 3, dims);

    /*  Set up dimension zero  */

    dimid0 = SDgetdimid(sdid, 0);

    SDsetdimname(dimid0, "Dimension 0");

    SDsetdimstrs(dimid0, "The zeroth dimension", "mm", "2d");

    SDsetdimscale(dimid0, LENGTH, DFNT_INT16, (VOIDP)scale0);

    /*  Set up dimension one  */

    dimid1 = SDgetdimid(sdid, 1);

    SDsetdimname(dimid1, "Dimension 1");

    SDsetdimstrs(dimid1, "The first dimension", "cm", "8d");

    SDsetdimscale(dimid1, HEIGHT, DFNT_INT32, (VOIDP)scale1);

    /*  Set up dimension two  */

    dimid2 = SDgetdimid(sdid, 2);

    SDsetdimname(dimid2, "Dimension 2");

    SDsetdimstrs(dimid2, "The second dimension", "m", "4.1f");

    SDsetdimscale(dimid2, WIDTH, DFNT_FLOAT32, (VOIDP)scale2);

    /*  Write the data array to the data set  */

    SDwritedata(sdid, start, NULL, dims, (char *)scien_data);

    /*  Add one local attribute  */

   SDsetattr(sdid, "Average", DFNT_FLOAT64, 1, (char *)&avg);

    /*  Add one global attribute  */

    SDsetattr(fid, "Date", DFNT_CHAR8, 9, "10/29/93");

    /*  Close the data set, file, and interface  */

    SDendaccess(sdid);

    SDend(fid);

}





Listing Three 

#include <string.h>

#include "hdf.h"

#include "mfhdf.h"



#define MAXRANK   3

#define LENGTH    3

#define HEIGHT    2

#define WIDTH     5

#define DATESIZE  9



main(int argc, char *argv[])

{

    float64 scien_data[LENGTH][HEIGHT][WIDTH];

    int32 dims[MAXRANK];

    int16 scale0[LENGTH];

    int32 scale1[HEIGHT];

    float32 scale2[WIDTH];

    float64 avg;

    int32 start[MAXRANK] = {0, 0, 0};

    int32 fid, sdid, dimid;

    int32 i, index, rank, nattrs, ndatasets, nglobals;

    int32 nt, count, status;

    intn size;

    char name[80], date[80];



    /*  Open file and initialize SD interface  */

    fid = SDstart("example.hdf", DFACC_RDONLY);

    status = SDfileinfo(fid, &ndatasets, &nglobals);



    /*  Read global attribute  */

    if (nglobals == 1) {

        status = SDattrinfo(fid, 0, name, &nt, &size);

        if ((strcmp(name, "Date")) && (nt == DFNT_CHAR8) &&

            (size == DATESIZE))

            SDreadattr(fid, 0, date);

    }

    /*  Open first data set  */

    index = SDnametoindex(fid, "Sample Data Set");

    sdid = SDselect(fid, index);

    SDgetinfo(sdid, name, &rank, dims, &nt, &nattrs);

    /*  Read in data if everything looks okay  */

    if ((rank == MAXRANK) && (dims[0] == LENGTH) && (dims[1] == HEIGHT)

        && (dims[2] == WIDTH) && (nt == DFNT_FLOAT64))

        SDreaddata(sdid, start, NULL, dims, scien_data);

    /*  Read local attribute  */

    status = SDattrinfo(sdid, 0, name, &nt, &size);

    if ((strcmp(name, "Average")) && (nt == DFNT_FLOAT64) &&

        (size == 1))

        SDreadattr(sdid, 0, &avg);

    /*  Read dimensions  */

    dimid = SDgetdimid(sdid, 0);

    SDdiminfo(dimid, name, &count, &nt, &nattrs);

    if ((nt == DFNT_INT16) && (count == LENGTH))

        SDgetdimscale(dimid, scale0);

    dimid = SDgetdimid(sdid, 1);

    SDdiminfo(dimid, name, &count, &nt, &nattrs);

    if ((nt == DFNT_INT32) && (count == HEIGHT))

        SDgetdimscale(dimid, scale1);

    dimid = SDgetdimid(sdid, 2);

    SDdiminfo(dimid, name, &count, &nt, &nattrs);

    if ((nt == DFNT_FLOAT32) && (count == WIDTH))

        SDgetdimscale(dimid, scale2);

    /*  Close the data set, file, and interface  */

    SDendaccess(sdid);

    SDend(fid);

}







5



