next up previous index
Next: Reading Data from a Up: Working with Scientific Data Previous: Viewing an HDF File

Writing onto a Scientific Data Set

The next step is to write some data on that file.

So let us do it with the following program:

program write_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(kind=4), dimension(number_of_data), parameter :: &
       data = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /)
  integer, dimension(1), parameter :: start = (/ 0 /), stride = (/ 1 /), &
       edges = (/ number_of_data /)

  ! variables

  integer :: sd_file_id, sd_set_id, sd_write_status, sd_set_status, &

  sd_file_id = sfstart(sd_file_name, DFACC_WRITE)
  sd_set_id = sfselect(sd_file_id, sd_set_index)
  write (*, *) "sd_set_id      = ", sd_set_id
  sd_write_status = sfwdata(sd_set_id, start, stride, edges, data)
  write(*, *) "sd_write_status = ", sd_write_status
  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 write_SDS
This program  is a little different from the previous one. We begin by opening file "SDS.hdf", but, this time the file is open for writing. This is what flag DFACC_WRITE, whose numerical value is defined on the hdf module, means. Next, we no longer create a scientific data set, because it has already been created by our previous program. This time we select  it instead. And we select the first such data set, in case there are more than one defined on that file. Data sets inside HDF files are numbered from 0 inclusive, which is why our parameter sd_set_id has value 0.

Once the data set has been located and a connection made to it, we write our data on the file by calling function  sfwdata:

sd_write_status = sfwdata(sd_set_id, start, stride, edges, data)
The parameters passed to the function call are as follows:
this is a dynamically assigned scientific data set handle, which we will use throughout the program while referring to that data set
this points to the location within the scientific data set at which data is to be written. In principle the originally defined data set could have been made much larger, and, with our program, we can overwrite only a selected fragment of it. Variable start is an array, whose dimension must match the rank of the data set, which we are about to write on.
this parameter tells sfwdata whether it should write data contiguously or whether it should jump over every second or third slot in the scientific data set. Like start this variable also must be an array whose dimension is the same as the rank of the data set.
this is an array which tells sfwdata how many numbers from the data array it will write on the scientific data set along each dimension. The dimension of this array must match the rank of the data set
this, finally, is the data that we're going to write on the HDF file.

After the write completes, we terminate our connection

first with the data set, by calling function sfendacc
and then with the file itself, by calling function sfend.

After each operation completes we write its exit status on standard output. For functions sfwdata, sfendacc, and sfend  exit status zero means that everything is fine. If problems occur, the returned exit status may be -1.

Let us compile and run this program and see what it is going to do to our HDF file SDS.hdf:

gustav@blanc:../src 16:08:02 !587 $ make hdf-2
f90 -c -M/afs/ hdf-2.f90
f90 -o hdf-2 hdf-2.o -L/afs/ -lmfhdf \
-lnsl -ldf -ljpeg -lz -lm
gustav@blanc:../src 15:37:52 !588 $ ./hdf-2
 sd_set_id      = 262144
 sd_write_status = 0
 sd_set_status   = 0
 sd_file_status  = 0
gustav@blanc:../src 15:37:58 !589 $
It seems that everything has worked just fine. Now we'll call hdfed to see what's new inside SDS.hdf:
gustav@blanc:../src 15:38:06 !652 $ hdfed SDS.hdf
hdfed> info -all
 (1)    Version Descriptor            : (Tag 30) Ref 1
 (2)    Vdata Storage                 : (Tag 1963) Ref 9
 (3)    Vdata                         : (Tag 1962) Ref 9
 (4)    Vgroup                        : (Tag 1965) Ref 10
 (5)    Number type                   : (Tag 106) Ref 11
 (6)    SciData dimension record      : (Tag 701) Ref 11
*(7)    Numeric Data Group            : (Tag 720) Ref 2
 (8)    Vgroup                        : (Tag 1965) Ref 12
 (9)    Vgroup                        : (Tag 1965) Ref 13
 (10)   Scientific Data               : (Tag 702) Ref 8
Look, there is a new record in the file called Scientific Data, which wasn't there before. Let's go there and see what's inside it:
hdfed> next tag=702
hdfed> info -long
 (10)   Scientific Data               : (Tag 702)
        Ref: 8, Offset: 2757, Length: 40 (bytes)
hdfed> dump -decimal
       0:           1           2           3           4           5 
      20:           6           7           8           9          10 
Here are our numbers: 1 through 10. Isn't that wonderful? I always get this overwhelming feeling of joy and surprise when these things actually work!

next up previous index
Next: Reading Data from a Up: Working with Scientific Data Previous: Viewing an HDF File
Zdzislaw Meglicki