next up previous index
Next: Making Enquiries Against a Up: Working with Scientific Data Previous: Writing onto a Scientific

Reading Data from a Scientific Data Set

So far so good. How would we go about reading that data back into a Fortran array without having to look it up manually with hdfed?

Here's a program which does just that:

program read_SDS

  use hdf
  use dffunc

  implicit none

  ! constants

  character(len=7), parameter :: sd_file_name = "SDS.hdf"
  integer, parameter :: number_of_data = 10, sd_set_index = 0
  integer, dimension(1), parameter :: start = (/ 0 /), stride = (/ 1 /), &
       edges = (/ number_of_data /)

  ! variables

  integer :: sd_file_id, sd_set_id, sd_read_status, sd_set_status, &
       sd_file_status, i
  integer(kind=4), dimension(number_of_data) :: data = (/ (0, i=1, 10) /)

  sd_file_id = sfstart(sd_file_name, DFACC_READ)
  sd_set_id = sfselect(sd_file_id, sd_set_index)
  write (*, *) "sd_set_id      = ", sd_set_id
  write (*, *) "data           = ", data
  sd_read_status = sfrdata(sd_set_id, start, stride, edges, data)
  write (*, *) "sd_read_status = ", sd_read_status
  write (*, *) "data           = ", data
  sd_set_status = sfendacc(sd_set_id)
  write (*, *) "sd_set_status  = ", sd_set_status
  sd_file_status = sfend(sd_file_id)
  write (*, *) "sd_file_status = ", sd_file_status

end program read_SDS
The program  looks almost exactly like our writing program with the following differences:
1.
Variable data is no longer a parameter. Furthermore, on entry it is initialised to 10 zeros. Because the declarations begin with the implicit none statement we also have to declare variable i, which is used in the implicit DO of the array constructor.
2.
The file is open with the DFACC_READ flag, also defined on the hdf module.
3.
We print the values of the data array both before and after reading the data from the HDF file.
4.
The data is read with function sfrdata , which other than read versus write has the same synopsis as function sfwdata.
And here is how it compiles and runs:
gustav@blanc:../src 17:14:45 !605 $ make hdf-3
f90 -c -M/afs/ovpit.indiana.edu/@sys/HDF/modules hdf-3.f90
f90 -o hdf-3 hdf-3.o -L/afs/ovpit.indiana.edu/@sys/HDF/lib -lmfhdf -lnsl -ldf -ljpeg -lz -lm
gustav@blanc:../src 17:16:40 !606 $ ./hdf-3
 sd_set_id      = 262144
 data           = 10*0
 sd_read_status = 0
 data           = 1,  2,  3,  4,  5,  6,  7,  8,  9,  10
 sd_set_status  = 0
 sd_file_status = 0
gustav@blanc:../src 17:16:42 !607 $
And so we get our goodies back.


next up previous index
Next: Making Enquiries Against a Up: Working with Scientific Data Previous: Writing onto a Scientific
Zdzislaw Meglicki
2001-02-26