Example Programs


The best place to turn to for sample or example programs for using the HDF library to work with all HDF data types is the HDF4.1rX Users Guide, including a listing of programs dealing specifically with the SD API. The best source for sample programs which utilize the HDF-EOS library and the grid, point, and awath APIs is Chapter 7 of the HDF-EOS Library Users Guide for the ECS Project, Volume 1: Overview and Examples and the test driver programs that are available once the user downloads and install the HDF-EOS library as mentioned in Chapter 5: The HDF-EOS Library: Software and Hardware

The following is a list of a few sample programs that illustrate how the HDF library, and the SD API, can be used to work with HDF files. These example programs presented below are given in the FORTRAN programming language. However, the detailed steps for all languages are the same. Only the syntax code particular to each language should be different.


Previous Main Topic

Next Main Topic

Return to Main Topics

 

Writing an SDS in HDF

FORTRAN:

      PROGRAM WRITDATA
C
integer*4 sd_id, sds_id, rank
integer*4 XL, YL
integer dims(2), start (2), edges (2), stride (2)
integer i, j, k, retn
integer sfstart, sfcreate, sfwdata, sfendacc, sfend
real rwind(30, 30)
C
C DFACC_CREATE and DFNT_INT16 are defined in hdf.h but may have
C to be defined within the program for certain FORTRAN compilers
C
integer*4 DFACC_CREATE, DFNT_INT16
parameter (DFACC_CREATE = 4, DFNT_INT16=22)
rank = 2
XL = 30
YL = 30
C
C Create and open a new HDF file and initiate the SD interface
C
sd_id = sfstart('wind.hdf', DFACC_CREATE)
C
C Define the rank (number of dimensions) and dimensions (size) of the
C HDF Scientific Data Set (SDS) to be created.
C
dims(1) = XL
dims(2) = YL
C
C
C Create the HDF SDS (sfselect would be used if writing to an
C existing HDF file or data set)
C
sds_id = sfcreate(sd_id, 'winds', DFNT_INT16, rank, dims)
C
C Open and read the existing non-HDF data set into an array (rwind)
C
Open (unit=10, file='wind.dat', form='formatted')
Do j = 1,30
Read(10, 12) (rwind(i, j),i = 1,30)
12 Format(30(f4.1,1x))
Enddo
C
C Define where in the file to write the data set (start--location),
C the pattern of the data (stride--skip any values??), and the size
C of the data set (edges) to be written to. This is done for each
C dimension. start(x) = 0 is for writing at the beginning of the
C newly created SDS and stride(x) = 1 signifies that no data is to
C be skipped in the writing.
C
start(1) = 0
start(2) = 0
edges(1) = XL
edges(2) = YL
stride(1) = 1
stride(2) = 1
C
C Write the stored data (in the array rwind) to the new SDS
C
retn = sfwdata(sds_id, start, stride, edges, rwind)
C
C Terminate access to the array
C
retn = sfendacc(sds_id)
C
C Terminate access to the SD interface and close the HDF file
C
retn = sfend(sd_id)
Stop
End

Return to top

 

Writing Attributes in HDF

FORTRAN:

      PROGRAM WRITEATT
C
integer*4 sd_id, sds_id, dim_id, retn
integer dims(2), start(2), edges(2), stride(2)
integer sfstart, sfselect, sfdimid, sfscatt, sfendacc, sfend
C
C DFACC_RDWR, DFNT_INT16 and DFNT_CHAR8 are defined in hdf.h but
C may have to be defined within the program for certain FORTRAN
C compilers
C
integer*4 DFACC_RDWR, DFNT_INT32, DFNT_CHAR8
parameter (DFACC_RDWR = 3, DFNT_INT16 = 22, DFNT_CHAR8 = 4)
C
C Open the HDF file, initiate the SD interface, and get the
C identifier for the file
C
sd_id = sfstart('wind.hdf', DFACC_RDWR)
C
C Set an attribute the describe the contents of the file
C
retn = sfscatt(sd_id, 'file_contents', DFNT_CHAR8, 15,'lidar_LOS_winds')
C
C Get the identifier for the first data set (in this example, the
C only data set)
C
sds_id = sfselect(sd_id, 0)
C
C Set an attribute(s) for the data array itself. In this example, the
C units of the data are defined
C
retn = sfscatt(sds_id, 'units', DFNT_CHAR8, 13, 'units = m/sec')
C
C Terminate access to the data array
C
retn = sfendacc(sds_id)
C
C Terminate access to the SD interface and close the HDF file
C
retn = sfend(sd_id)
Stop
End

Return to top

 

Writing the SDS and attributes in HDF

FORTRAN:

      PROGRAM WRITESDS
C
integer*4 sd_id, sds_id, rank, dim_id
integer*4 XL, YL
integer dims(2), start(2), edges(2), stride(2)
integer i, j, k, retn
integer sfstart, sfcreate, sfwdata, sfendacc, sfscatt, sfend
real rwind(30, 30)
C
C DFACC_CREATE, DFACC_RDWR, DFNT_CHAR8 and DFNT_INT16 are defined
C in hdf.h but may have to be defined within the program for certain
C FORTRAN compilers
C
integer*4 DFACC_CREATE, DFNT_INT16, DFNT_CHAR8, DFACC_RDWR
parameter (DFACC_CREATE = 4, DFACC_RDWR = 3, DFNT_INT16 = 22, DFNT_CHAR8 = 4)
C
rank = 2
XL = 30
YL = 30
C
C Create and open a new HDF file and initiate the SD interface
C
sd_id = sfstart('wind.hdf', DFACC_CREATE)
C
C Define the rank (number of dimensions) and dimensions (size) of the
C HDF Scientific Data Set (SDS) to be created.
C
dims(1) = XL
dims(2) = YL
C
C Create the HDF SDS (sfselect would be used if writing to an
C existing HDF file or data set)
C
sds_id = sfcreate(sd_id, 'winds', DFNT_INT16, rank, dims)
C
C Open and read the existing non-HDF data set into an array (rwind)
C
Open (unit = 10, file = 'wind.dat', form = 'formatted')
Do j=1,30
Read (10, 12) (rwind(i, j),i=1,30)
12 Format (30(f4.1,1x))
enddo
C
C Define where in the file to write the data set (start--location),
C the pattern of the data (stride--skip any values??), and the size
C of the data set (edges) to be written to. This is done for each
C dimension. start(x) = 0 is for writing at the beginning of the
C newly created SDS and stride(x)=1 signifies that no data is to be
C skipped in the writing.
C
start(1) = 0
start(2) = 0
edges(1) = XL
edges(2) = YL
stride(1) = 1
stride(2) = 1
C
C Write the stored data (in the array rwind) to the new SDS
C
retn = sfwdata(sds_id, start, stride, edges, rwind)
C
C For writing attributes, set an attribute the describe the
C contents of the file
C
retn = sfscatt(sd_id, 'file_contents', DFNT_CHAR8, 15,'lidar_LOS_winds')
C
C Set an attribute(s) for the data array itself. In this example, the
C units of the data are defined
C
retn = sfscatt(sds_id, 'units', DFNT_CHAR8, 13, 'units = m/sec')
C
C Terminate access to the data array
C
retn = sfendacc(sds_id)
C
C Terminate access to the SD interface and close the HDF file
C
retn = sfend(sd_id)
Stop
End

Return to top

 

Reading an HDF file

FORTRAN:

      PROGRAM READDATA
C
integer*4 sd_id, sds_id
integer*4 XL, YL
integer start(2), edges(2), stride(2)
integer i, j, k, retn
integer sfstart, sfselect, sfrdata, sfendacc, sfend
real rwind(30, 30)
C
C DFACC_RDONLY is defined in hdf.h but may have to be defined
C within the program for certain FORTRAN compilers
C
integer*4 DFACC_RDONLY parameter (DFACC_RDONLY = 1)
C
C MAX_NC_NAME (maximum # of characters) and MAX_VAR_DIMS (maximum
C # of dimensions) are defined in netcdf.h but may have to be defined
C here.
C
integer*4 MAX_NC_NAME, MAX_VAR_DIMS
parameter (MAX_NC_NAME = 256, MAX_VAR_DIMS = 32)
integer dims(MAX_VAR_DIMS)
XL = 30
YL = 30
C
C Open the HDF file and initiate the SD interface
C
sd_id = sfstart('wind.hdf', DFACC_RDONLY)
C
C Select the first data set in the file (In this example, the only
C dataset).
C
sds_id= sfselect(sd_id, 0)
C
C To read from the data set, define the location (start--where in the
C file), the pattern (stride--skip any values??),and the size(edges)
C of the data. This is done for each dimension. start(x) = 0 is for
C reading at the beginning of the file and stride(x) = 1 signifies
C that no data is to be skipped in the reading.
C
dims(1) = XL
dims(2) = YL
start(1) = 0
start(2) = 0
stride(1) = 1
stride(2) = 1
edges(1) = dims(1)
edges(2) = dims(2)
C
C Read the array dataset
C
retn = sfrdata(sds_id, start, stride, edges, rwind)
C
C Optional - Print out data (ASCII) read from the HDF file
C In this example we are writing to the screen (*)
C
Do j = 1, 30
write(*,12) (rwind(i,j),i=1,30)
12 format(30(f4.1,1x))
enddo
C
C Terminate access to the array
C
retn = sfendacc(sds_id)
C
C Terminate access to the SD interface and close the HDF file
C
retn = sfend(sd_id)
Stop
End

Return to top

 

Reading HDF attributes (files and data sets)

FORTRAN:

      PROGRAM READATTR
C
integer*4 sd_id, sds_id, units_buffer
integer attr_index, data_type, count, retn
character attr_name * 13
character char_buffer * 20
integer sfstart, sfrnatt, sfrcatt, sfgainfo, sffatr, sfselect
integer sfendacc, sfend
C
C DFACC_RDWR is defined in hdf.h but may have to be defined
C within the program for certain FORTRAN compilers
C
integer*4 DFACC_RDWR, DFACC_RDONLY
parameter (DFACC_RDWR = 3, DFACC_RDONLY = 4)
C
C Open the HDF file and initiate the SD interface
C
sd_id = sfstart('wind.hdf', DFACC_RDONLY)
C
C Select the first data set in the file (In this example, the only
C dataset).
C
sds_id= sfselect(sd_id, 0)
C
C Find the attribute which describes the contents of the file
C (usually 'file_contents')
C
attr_index = sffattr(sd_id, 'file_contents')
C
C Get information about the file attribute
C
retn = sfgainfo(sd_id, attr_index, attr_name, data_type, count)
C
C Read the file attribute data
C
retn = sfrcatt(sd_id, attr_index, char_buffer)
C
C Read the attributes for the first data set. First step is to get
C the identifier.
C
sds_id = sfselect(sd_id, 0)
C
C Find the attribute which defines the units of the data set
C ('units')
C
attr_index = sffattr(sds_id, 'units')
C
C Get information about the data set attribute
C
retn = sfgainfo(sds_id, attr_index, attr_name, data_type, count)
C
C Read the data set attribute data
C
retn = sfrcatt(sds_id, attr_index, units_buffer)
C
C Terminate access to the array
C
retn = sfendacc(sds_id)
C
C Terminate access to the SD interface and close the HDF file
C
retn = sfend(sd_id)
Stop
End

Return to top