diff options
Diffstat (limited to 'fortran/examples')
-rw-r--r-- | fortran/examples/Dependencies | 0 | ||||
-rw-r--r-- | fortran/examples/Makefile.in | 86 | ||||
-rw-r--r-- | fortran/examples/attrexample.f90 | 102 | ||||
-rw-r--r-- | fortran/examples/compound.f90 | 215 | ||||
-rw-r--r-- | fortran/examples/dsetexample.f90 | 70 | ||||
-rw-r--r-- | fortran/examples/fileexample.f90 | 27 | ||||
-rw-r--r-- | fortran/examples/groupexample.f90 | 42 | ||||
-rw-r--r-- | fortran/examples/grpdsetexample.f90 | 136 | ||||
-rw-r--r-- | fortran/examples/grpit.f90 | 189 | ||||
-rw-r--r-- | fortran/examples/grpsexample.f90 | 59 | ||||
-rw-r--r-- | fortran/examples/hyperslab.f90 | 199 | ||||
-rw-r--r-- | fortran/examples/mountexample.f90 | 183 | ||||
-rw-r--r-- | fortran/examples/refobjexample.f90 | 136 | ||||
-rw-r--r-- | fortran/examples/refregexample.f90 | 159 | ||||
-rwxr-xr-x | fortran/examples/run_examples.sh | 13 | ||||
-rw-r--r-- | fortran/examples/rwdsetexample.f90 | 78 | ||||
-rw-r--r-- | fortran/examples/selectele.f90 | 282 |
17 files changed, 1976 insertions, 0 deletions
diff --git a/fortran/examples/Dependencies b/fortran/examples/Dependencies new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/fortran/examples/Dependencies diff --git a/fortran/examples/Makefile.in b/fortran/examples/Makefile.in new file mode 100644 index 0000000..0ef7d55 --- /dev/null +++ b/fortran/examples/Makefile.in @@ -0,0 +1,86 @@ +## HDF5-Fortran test/Makefile(.in) +## +## Copyright (C) 2000 National Center for Supercomputing Applications. +## All rights reserved. +## +## +top_srcdir=@top_srcdir@ +top_builddir=.. +srcdir=@srcdir@ +@COMMENCE@ + +hdf5_dir=$(top_srcdir)/../src + +## Add include directory to the C preprocessor flags and the h5test and hdf5 +## libraries to the library list. +LT_LINK_LIB=$(LT) --mode=link $(F9X) -static -rpath $(libdir) +MFLAG=@F9XMODFLAG@ +FFLAGS=$(MFLAG). $(MFLAG)../src +FLIB=../src/libhdf5_fortran.la +HDF5LIB=$(hdf5_dir)/libhdf5.la + +## These are the programs that `make all' or `make tests' will build and which +## `make check' will run. List them in the order they should be run. +TEST_PROGS=dsetexample fileexample rwdsetexample attrexample groupexample \ + grpsexample grpdsetexample hyperslab selectele grpit refobjexample \ + refregexample mountexample compound + +## These are the files that `make clean' (and derivatives) will remove from +## this directory. +CLEAN=*.h5 + +## List all source files here. The list of object files will be +## created by replacing the `.f90' with a `.o'. This list is necessary +## for building automatic dependencies. +TEST_SRC=dsetexample.f90 fileexample.f90 rwdsetexample.f90 attrexample.f90 \ + groupexample.f90 grpsexample.f90 grpdsetexample.f90 hyperslab.f90 \ + selectele.f90 grpit.f90 refobjexample.f90 refregexample.f90 \ + mountexample.f90 compound.f90 +TEST_OBJ=$(TEST_SRC:.f90=.lo) + +## How to build the programs... they all depend on the Fortran HDF5 library +$(TEST_PROGS): $(FLIB) + +dsetexample: dsetexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ dsetexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +fileexample: fileexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ fileexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +rwdsetexample: rwdsetexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ rwdsetexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +attrexample: attrexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ attrexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +groupexample: groupexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ groupexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +grpsexample: grpsexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ grpsexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +grpdsetexample: grpdsetexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ grpdsetexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +hyperslab: hyperslab.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ hyperslab.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +selectele: selectele.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ selectele.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +grpit: grpit.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ grpit.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +refobjexample: refobjexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ refobjexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +refregexample: refregexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ refregexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +mountexample: mountexample.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ mountexample.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +compound: compound.f90 + @$(LT_LINK_FEXE) $(FFLAGS) -o $@ compound.f90 $(FLIB) $(LIBS) $(HDF5LIB) + +@CONCLUDE@ diff --git a/fortran/examples/attrexample.f90 b/fortran/examples/attrexample.f90 new file mode 100644 index 0000000..c71939e --- /dev/null +++ b/fortran/examples/attrexample.f90 @@ -0,0 +1,102 @@ +! This example shows how to create and write a dataset attribute. +! It opens the existing file 'dset.h5', obtains the identifier of +! the dataset "/dset", defines attribute's dataspace, +! creates dataset attribute, writes the attribute, and then closes +! the attribute's dataspace, attribute, dataset, and file. + + PROGRAM ATTREXAMPLE + + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=8), PARAMETER :: filename = "dsetf.h5" ! File name + CHARACTER(LEN=4), PARAMETER :: dsetname = "dset" ! Dataset name + CHARACTER(LEN=9), PARAMETER :: aname = "attr_long" ! Attribute name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: dset_id ! Dataset identifier + INTEGER(HID_T) :: attr_id ! Attribute identifier + INTEGER(HID_T) :: aspace_id ! Attribute Dataspace identifier + INTEGER(HID_T) :: atype_id ! Attribute Dataspace identifier + INTEGER(HSIZE_T), DIMENSION(1) :: adims = (/2/) ! Attribute dimension + INTEGER :: arank = 1 ! Attribure rank + INTEGER(SIZE_T) :: attrlen ! Length of the attribute string + +! CHARACTER*27, DIMENSION(2) :: attr_data ! Attribute data + CHARACTER*80, DIMENSION(2) :: attr_data ! Attribute data + + INTEGER :: error ! Error flag + + + ! + ! Initialize attribute's data + ! + attr_data(1) = "Dataset character attribute" + attr_data(2) = "Some other string here " +! attrlen = 27 + attrlen = 80 + ! + ! Initialize FORTRAN predefined datatypes. + ! + CALL h5init_types_f(error) + + ! + ! Open an existing file. + ! + CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error) + + ! + ! Open an existing dataset. + ! + CALL h5dopen_f(file_id, dsetname, dset_id, error) + + ! + ! Create scalar data space for the attribute. + ! + CALL h5screate_simple_f(arank, adims, aspace_id, error) + ! + ! Create datatype for the attribute. + ! + CALL h5tcopy_f(H5T_NATIVE_CHARACTER, atype_id, error) + CALL h5tset_size_f(atype_id, attrlen, error) + + ! + ! Create dataset attribute. + ! + CALL h5acreate_f(dset_id, aname, atype_id, aspace_id, & + attr_id, error) + + ! + ! Write the attribute data. + ! + CALL h5awrite_f(attr_id, atype_id, attr_data, error) + + ! + ! Close the attribute. + ! + CALL h5aclose_f(attr_id, error) + + ! + ! Terminate access to the data space. + ! + CALL h5sclose_f(aspace_id, error) + + ! + ! End access to the dataset and release resources used by it. + ! + CALL h5dclose_f(dset_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM ATTREXAMPLE + diff --git a/fortran/examples/compound.f90 b/fortran/examples/compound.f90 new file mode 100644 index 0000000..f2954e8 --- /dev/null +++ b/fortran/examples/compound.f90 @@ -0,0 +1,215 @@ +! +! This program creates a dataset that is one dimensional array of +! structures { +! character*2 +! integer +! double precision +! real +! } +! Data is written and read back by fields. +! + + PROGRAM COMPOUNDEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=11), PARAMETER :: filename = "compound.h5" ! File name + CHARACTER(LEN=8), PARAMETER :: dsetname = "Compound" ! Dataset name + INTEGER, PARAMETER :: dimsize = 6 ! Size of the dataset + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: dset_id ! Dataset identifier + INTEGER(HID_T) :: dspace_id ! Dataspace identifier + INTEGER(HID_T) :: dtype_id ! Compound datatype identifier + INTEGER(HID_T) :: dt1_id ! Memory datatype identifier (for character field) + INTEGER(HID_T) :: dt2_id ! Memory datatype identifier (for integer field) + INTEGER(HID_T) :: dt3_id ! Memory datatype identifier (for double precision field) + INTEGER(HID_T) :: dt4_id ! Memory datatype identifier (for real field) + INTEGER(HID_T) :: dt5_id ! Memory datatype identifier + INTEGER(HID_T) :: plist_id ! Dataset trasfer property + INTEGER(SIZE_T) :: typesize + + + INTEGER(HSIZE_T), DIMENSION(1) :: dims = (/dimsize/) ! Dataset dimensions + INTEGER :: rank = 1 ! Dataset rank + + INTEGER :: error ! Error flag + INTEGER(SIZE_T) :: type_size ! Size of the datatype + INTEGER(SIZE_T) :: type_sizec ! Size of the character datatype + INTEGER(SIZE_T) :: type_sizei ! Size of the integer datatype + INTEGER(SIZE_T) :: type_sized ! Size of the double precision datatype + INTEGER(SIZE_T) :: type_sizer ! Size of the real datatype + INTEGER(SIZE_T) :: offset ! Member's offset + CHARACTER*2, DIMENSION(dimsize) :: char_member + CHARACTER*2, DIMENSION(dimsize) :: char_member_out ! Buffer to read data out + INTEGER, DIMENSION(dimsize) :: int_member + DOUBLE PRECISION, DIMENSION(dimsize) :: double_member + REAL, DIMENSION(dimsize) :: real_member + INTEGER :: i + ! + ! Initialize data buffer. + ! + do i = 1, dimsize + char_member(i)(1:1) = char(65+i) + char_member(i)(2:2) = char(65+i) + char_member_out(i)(1:1) = char(65) + char_member_out(i)(2:2) = char(65) + int_member(i) = i + double_member(i) = 2.* i + real_member(i) = 3. * i + enddo + + ! + ! Initialize FORTRAN predefined datatypes. + ! + CALL h5init_types_f(error) + ! + ! Set dataset transfer property to preserve partially initialized fields + ! during write/read to/from dataset with compound datatype. + ! + CALL h5pcreate_f(H5P_DATASET_XFER_F, plist_id, error) + CALL h5pset_preserve_f(plist_id, 1, error) + + ! + ! Create a new file using default properties. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + + ! + ! Create the dataspace. + ! + CALL h5screate_simple_f(rank, dims, dspace_id, error) + ! + ! Create compound datatype. + ! + ! First calculate total size by calculating sizes of each member + ! + CALL h5tcopy_f(H5T_NATIVE_CHARACTER, dt5_id, error) + typesize = 2 + CALL h5tset_size_f(dt5_id, typesize, error) + CALL h5tget_size_f(dt5_id, type_sizec, error) + CALL h5tget_size_f(H5T_NATIVE_INTEGER, type_sizei, error) + CALL h5tget_size_f(H5T_NATIVE_DOUBLE, type_sized, error) + CALL h5tget_size_f(H5T_NATIVE_REAL, type_sizer, error) + type_size = type_sizec + type_sizei + type_sized + type_sizer + CALL h5tcreate_f(H5T_COMPOUND_F, type_size, dtype_id, error) + ! + ! Insert memebers + ! + ! CHARACTER*2 memeber + ! + offset = 0 + CALL h5tinsert_f(dtype_id, "char_field", offset, dt5_id, error) + ! + ! INTEGER member + ! + offset = offset + type_sizec ! Offset of the second memeber is 2 + CALL h5tinsert_f(dtype_id, "integer_field", offset, H5T_NATIVE_INTEGER, error) + ! + ! DOUBLE PRECISION member + ! + offset = offset + type_sizei ! Offset of the third memeber is 6 + CALL h5tinsert_f(dtype_id, "double_field", offset, H5T_NATIVE_DOUBLE, error) + ! + ! REAL member + ! + offset = offset + type_sized ! Offset of the last member is 14 + CALL h5tinsert_f(dtype_id, "real_field", offset, H5T_NATIVE_REAL, error) + + ! + ! Create the dataset with compound datatype. + ! + CALL h5dcreate_f(file_id, dsetname, dtype_id, dspace_id, & + dset_id, error) + ! + ! Create memory types. We have to create a compound datatype + ! for each member we want to write. + ! + CALL h5tcreate_f(H5T_COMPOUND_F, type_sizec, dt1_id, error) + offset = 0 + CALL h5tinsert_f(dt1_id, "char_field", offset, dt5_id, error) + ! + CALL h5tcreate_f(H5T_COMPOUND_F, type_sizei, dt2_id, error) + offset = 0 + CALL h5tinsert_f(dt2_id, "integer_field", offset, H5T_NATIVE_INTEGER, error) + ! + CALL h5tcreate_f(H5T_COMPOUND_F, type_sized, dt3_id, error) + offset = 0 + CALL h5tinsert_f(dt3_id, "double_field", offset, H5T_NATIVE_DOUBLE, error) + ! + CALL h5tcreate_f(H5T_COMPOUND_F, type_sizer, dt4_id, error) + offset = 0 + CALL h5tinsert_f(dt4_id, "real_field", offset, H5T_NATIVE_REAL, error) + ! + ! Write data by fields in the datatype. Fields order is not important. + ! + CALL h5dwrite_f(dset_id, dt4_id, real_member, error, xfer_prp = plist_id) + CALL h5dwrite_f(dset_id, dt1_id, char_member, error, xfer_prp = plist_id) + CALL h5dwrite_f(dset_id, dt3_id, double_member, error, xfer_prp = plist_id) + CALL h5dwrite_f(dset_id, dt2_id, int_member, error, xfer_prp = plist_id) + + ! + ! End access to the dataset and release resources used by it. + ! + CALL h5dclose_f(dset_id, error) + + ! + ! Terminate access to the data space. + ! + CALL h5sclose_f(dspace_id, error) + ! + ! Terminate access to the datatype + ! + CALL h5tclose_f(dtype_id, error) + CALL h5tclose_f(dt1_id, error) + CALL h5tclose_f(dt2_id, error) + CALL h5tclose_f(dt3_id, error) + CALL h5tclose_f(dt4_id, error) + CALL h5tclose_f(dt5_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + ! + ! Open the file. + ! + CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error) + ! + ! Open the dataset. + ! + CALL h5dopen_f(file_id, dsetname, dset_id, error) + ! + ! Create memeory datatyoe to read character member of the compound datatype. + ! + CALL h5tcopy_f(H5T_NATIVE_CHARACTER, dt2_id, error) + typesize = 2 + CALL h5tset_size_f(dt2_id, typesize, error) + CALL h5tget_size_f(dt2_id, type_size, error) + CALL h5tcreate_f(H5T_COMPOUND_F, type_size, dt1_id, error) + offset = 0 + CALL h5tinsert_f(dt1_id, "char_field", offset, dt2_id, error) + ! + ! Read part of the datatset and display it. + ! + CALL h5dread_f(dset_id, dt1_id, char_member_out, error) + write(*,*) (char_member_out(i), i=1, dimsize) + + ! + ! Close all open objects. + ! + CALL h5dclose_f(dset_id, error) + CALL h5tclose_f(dt1_id, error) + CALL h5tclose_f(dt2_id, error) + CALL h5fclose_f(file_id, error) + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM COMPOUNDEXAMPLE + + diff --git a/fortran/examples/dsetexample.f90 b/fortran/examples/dsetexample.f90 new file mode 100644 index 0000000..00974a9 --- /dev/null +++ b/fortran/examples/dsetexample.f90 @@ -0,0 +1,70 @@ +! +! The following example shows how to create an empty dataset. +! It creates a file called 'dsetf.h5', defines the +! dataset dataspace, creates a dataset which is a 4x6 integer array, +! and then closes the dataspace, the dataset, and the file. +! + + PROGRAM DSETEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=8), PARAMETER :: filename = "dsetf.h5" ! File name + CHARACTER(LEN=4), PARAMETER :: dsetname = "dset" ! Dataset name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: dset_id ! Dataset identifier + INTEGER(HID_T) :: dspace_id ! Dataspace identifier + + + INTEGER(HSIZE_T), DIMENSION(2) :: dims = (/4,6/) ! Dataset dimensions + INTEGER :: rank = 2 ! Dataset rank + + INTEGER :: error ! Error flag + + ! + ! Initialize FORTRAN predefined datatypes. + ! + CALL h5init_types_f(error) + + ! + ! Create a new file using default properties. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + + ! + ! Create the dataspace. + ! + CALL h5screate_simple_f(rank, dims, dspace_id, error) + + ! + ! Create the dataset with default properties. + ! + CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dspace_id, & + dset_id, error) + + ! + ! End access to the dataset and release resources used by it. + ! + CALL h5dclose_f(dset_id, error) + + ! + ! Terminate access to the data space. + ! + CALL h5sclose_f(dspace_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM DSETEXAMPLE + + diff --git a/fortran/examples/fileexample.f90 b/fortran/examples/fileexample.f90 new file mode 100644 index 0000000..33c3bc2 --- /dev/null +++ b/fortran/examples/fileexample.f90 @@ -0,0 +1,27 @@ +! +! The following example demonstrates how to create and close an HDF5 file. +! It creates a file called 'file.h5', and then closes the file. +! + + PROGRAM FILEEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=8), PARAMETER :: filename = "filef.h5" ! File name + INTEGER(HID_T) :: file_id ! File identifier + + INTEGER :: error ! Error flag + + ! + ! Create a new file using default properties. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + + ! + ! Terminate access to the file. + ! + CALL h5fclose_f(file_id, error) + + END PROGRAM FILEEXAMPLE diff --git a/fortran/examples/groupexample.f90 b/fortran/examples/groupexample.f90 new file mode 100644 index 0000000..148a796 --- /dev/null +++ b/fortran/examples/groupexample.f90 @@ -0,0 +1,42 @@ +! +! The following example shows how to create and close a group. +! It creates a file called 'group.h5', creates a group +! called MyGroup in the root group, and then closes the group and file. +! + + + PROGRAM GROUPEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=9), PARAMETER :: filename = "groupf.h5" ! File name + CHARACTER(LEN=7), PARAMETER :: groupname = "MyGroup" ! Group name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: group_id ! Group identifier + + INTEGER :: error ! Error flag + + ! + ! Create a new file using default properties. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + + ! + ! Create a group named "/MyGroup" in the file. + ! + CALL h5gcreate_f(file_id, groupname, group_id, error) + + ! + ! Close the group. + ! + CALL h5gclose_f(group_id, error) + + ! + ! Terminate access to the file. + ! + CALL h5fclose_f(file_id, error) + + END PROGRAM GROUPEXAMPLE diff --git a/fortran/examples/grpdsetexample.f90 b/fortran/examples/grpdsetexample.f90 new file mode 100644 index 0000000..75bc335 --- /dev/null +++ b/fortran/examples/grpdsetexample.f90 @@ -0,0 +1,136 @@ +! +! This example shows how to create a dataset in a particular group. +! It opens the file created in the previous example and creates two datasets. +! Absolute and relative dataset names are used. +! + + + PROGRAM GRPDSETEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=10), PARAMETER :: filename = "groupsf.h5" ! File name + CHARACTER(LEN=15), PARAMETER :: groupname = "MyGroup/Group_A" ! Group name + CHARACTER(LEN=13), PARAMETER :: dsetname1 = "MyGroup/dset1" ! Dataset name + CHARACTER(LEN=5), PARAMETER :: dsetname2 = "dset2" ! dataset name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: group_id ! Group identifier + INTEGER(HID_T) :: dataset_id ! Dataset identifier + INTEGER(HID_T) :: dataspace_id ! Data space identifier + + INTEGER :: i, j + INTEGER :: error ! Error flag + + INTEGER, DIMENSION(3,3) :: dset1_data ! Data arrays + INTEGER, DIMENSION(2,10) :: dset2_data ! + + INTEGER(HSIZE_T), DIMENSION(2) :: dims1 = (/3,3/) ! Datasets dimensions + INTEGER(HSIZE_T), DIMENSION(2) :: dims2 = (/2,10/)! + + INTEGER :: rank = 2 ! Datasets rank + + ! + !Initialize dset1_data array + ! + do i = 1, 3 + do j = 1, 3 + dset1_data(i,j) = j; + end do + end do + + + ! + !Initialize dset2_data array + ! + do i = 1, 2 + do j = 1, 10 + dset2_data(i,j) = j; + end do + end do + + ! + ! Initialize FORTRAN predefined datatypes. + ! + CALL h5init_types_f(error) + + ! + ! Open an existing file. + ! + CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error) + + ! + ! Create the data space for the first dataset. + ! + CALL h5screate_simple_f(rank, dims1, dataspace_id, error) + + ! + ! Create a dataset in group "MyGroup" with default properties. + ! + CALL h5dcreate_f(file_id, dsetname1, H5T_NATIVE_INTEGER, dataspace_id, & + dataset_id, error) + + ! + ! Write the first dataset. + ! + CALL h5dwrite_f(dataset_id, H5T_NATIVE_INTEGER, dset1_data, error) + + ! + ! Close the dataspace for the first dataset. + ! + CALL h5sclose_f(dataspace_id, error) + + ! + ! Close the first dataset. + ! + CALL h5dclose_f(dataset_id, error) + + ! + ! Open an existing group in the specified file. + ! + CALL h5gopen_f(file_id, groupname, group_id, error) + + ! + !Create the data space for the second dataset. + ! + CALL h5screate_simple_f(rank, dims2, dataspace_id, error) + + ! + ! Create the second dataset in group "Group_A" with default properties. + ! + CALL h5dcreate_f(group_id, dsetname2, H5T_NATIVE_INTEGER, dataspace_id, & + dataset_id, error) + + ! + ! Write the second dataset. + ! + CALL h5dwrite_f(dataset_id, H5T_NATIVE_INTEGER, dset2_data, error) + + ! + ! Close the dataspace for the second dataset. + ! + CALL h5sclose_f(dataspace_id, error) + + ! + ! Close the second dataset. + ! + CALL h5dclose_f(dataset_id, error) + + ! + ! Close the group. + ! + CALL h5gclose_f(group_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM GRPDSETEXAMPLE diff --git a/fortran/examples/grpit.f90 b/fortran/examples/grpit.f90 new file mode 100644 index 0000000..66fb09e --- /dev/null +++ b/fortran/examples/grpit.f90 @@ -0,0 +1,189 @@ +! +! In this example we iterate through the members of the groups. +! + + + PROGRAM GRPITEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=11), PARAMETER :: filename = "iteratef.h5" ! File name + CHARACTER(LEN=7), PARAMETER :: groupname1 = "MyGroup" ! Group name + CHARACTER(LEN=15), PARAMETER :: groupname2 = "Group_A" ! Group name + CHARACTER(LEN=13), PARAMETER :: dsetname1 = "dset1" ! Dataset name + CHARACTER(LEN=5), PARAMETER :: dsetname2 = "dset2" ! + + CHARACTER(LEN=20) :: name_buffer ! Buffer to hold object's name + INTEGER :: type ! Type of the object + INTEGER :: nmembers ! Number of group members + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: dataset1_id ! Dataset1 identifier + INTEGER(HID_T) :: dataset2_id ! Dataset2 identifier + INTEGER(HID_T) :: dataspace1_id ! Data space identifier + INTEGER(HID_T) :: dataspace2_id ! Data space identifier + INTEGER(HID_T) :: group1_id, group2_id ! Group identifiers + + INTEGER :: i, j + + INTEGER :: error ! Error flag + + INTEGER, DIMENSION(3,3) :: dset1_data ! Arrays to hold data + INTEGER, DIMENSION(2,10) :: dset2_data ! + + INTEGER(HSIZE_T), DIMENSION(2) :: dims1 = (/3,3/) ! Dataset dimensions + INTEGER(HSIZE_T), DIMENSION(2) :: dims2 = (/2,10/)! + INTEGER :: rank = 2 ! Datasets rank + + ! + ! Initialize dset1_data array. + ! + do i = 1, 3 + do j = 1, 3 + dset1_data(i,j) = j; + end do + end do + + + ! + ! Initialize dset2_data array. + ! + do i = 1, 2 + do j = 1, 10 + dset2_data(i,j) = j; + end do + end do + + ! + ! Initialize FORTRAN predefined datatypes. + ! + CALL h5init_types_f(error) + + ! + ! Create a new file using default properties. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + + ! + ! Create group "MyGroup" in the root group using absolute name. + ! + CALL h5gcreate_f(file_id, groupname1, group1_id, error) + + ! + ! Create group "Group_A" in group "MyGroup" using relative name. + ! + CALL h5gcreate_f(group1_id, groupname2, group2_id, error) + + ! + ! Create the data space for the first dataset. + ! + CALL h5screate_simple_f(rank, dims1, dataspace1_id, error) + + ! + ! Create a dataset in group "MyGroup" with default properties. + ! + CALL h5dcreate_f(group1_id, dsetname1, H5T_NATIVE_INTEGER, dataspace1_id, & + dataset1_id, error) + + ! + ! Write the first dataset. + ! + CALL h5dwrite_f(dataset1_id, H5T_NATIVE_INTEGER, dset1_data, error) + + ! + ! Create the data space for the second dataset. + ! + CALL h5screate_simple_f(rank, dims2, dataspace2_id, error) + + ! + ! Create the second dataset in group "Group_A" with default properties + ! + CALL h5dcreate_f(group2_id, dsetname2, H5T_NATIVE_INTEGER, dataspace2_id, & + dataset2_id, error) + + ! + ! Write the second dataset + ! + CALL h5dwrite_f(dataset2_id, H5T_NATIVE_INTEGER, dset2_data, error) + + ! + ! Get number of members in the root group. + ! + CALL h5gn_members_f(file_id, "/", nmembers, error) + write(*,*) "Number of root group member is " , nmembers + + ! + ! Print each group member's name and type. + ! + do i = 0, nmembers - 1 + CALL h5gget_obj_info_idx_f(file_id, "/", i, name_buffer, type, & + error) + write(*,*) name_buffer, type + end do + + ! + ! Get number of members in MyGroup. + ! + CALL h5gn_members_f(file_id, "MyGroup", nmembers, error) + write(*,*) "Number of group MyGroup member is ", nmembers + + ! + ! Print each group member's name and type in "MyGroup" group. + ! + do i = 0, nmembers - 1 + CALL h5gget_obj_info_idx_f(file_id, groupname1, i, name_buffer, type, & + error) + write(*,*) name_buffer, type + end do + + + ! + ! Get number of members in MyGroup/Group_A. + ! + CALL h5gn_members_f(file_id, "MyGroup/Group_A", nmembers, error) + write(*,*) "Number of group MyGroup/Group_A member is ", nmembers + + ! + ! Print each group member's name and type in "MyGroup/Group_A" group. + ! + do i = 0, nmembers - 1 + CALL h5gget_obj_info_idx_f(file_id,"MyGroup/Group_A" , i, name_buffer, type, & + error) + write(*,*) name_buffer, type + end do + + ! + ! Close the dataspace for the first dataset. + ! + CALL h5sclose_f(dataspace1_id, error) + + ! + ! Close the first dataset. + ! + CALL h5dclose_f(dataset1_id, error) + + ! + ! Close the dataspace for the second dataset. + ! + CALL h5sclose_f(dataspace2_id, error) + + ! + ! Close the second dataset. + ! + CALL h5dclose_f(dataset2_id, error) + + ! + ! Close the groups. + ! + CALL h5gclose_f(group1_id, error) + + CALL h5gclose_f(group2_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + END PROGRAM GRPITEXAMPLE diff --git a/fortran/examples/grpsexample.f90 b/fortran/examples/grpsexample.f90 new file mode 100644 index 0000000..abbc6ea --- /dev/null +++ b/fortran/examples/grpsexample.f90 @@ -0,0 +1,59 @@ +! +! The following example code shows how to create groups +! using absolute and relative names. It creates three groups: +! the first two groups are created using the file identifier and +! the group absolute names, and the third group is created using +! a group identifier and the name relative to the specified group. +! + + + PROGRAM GRPSEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=10), PARAMETER :: filename = "groupsf.h5" ! File name + CHARACTER(LEN=8), PARAMETER :: groupname1 = "/MyGroup" ! Group name + CHARACTER(LEN=16), PARAMETER :: groupname2 = "/MyGroup/Group_A" + ! Group name + CHARACTER(LEN=7), PARAMETER :: groupname3 = "Group_B" ! Group name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: group1_id, group2_id, group3_id ! Group identifiers + + INTEGER :: error ! Error flag + + ! + ! Create a new file using default properties. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + + ! + ! Create group "MyGroup" in the root group using absolute name. + ! + CALL h5gcreate_f(file_id, groupname1, group1_id, error) + + ! + ! Create group "Group_A" in group "MyGroup" using absolute name. + ! + CALL h5gcreate_f(file_id, groupname2, group2_id, error) + + ! + ! Create group "Group_B" in group "MyGroup" using relative name. + ! + CALL h5gcreate_f(group1_id, groupname3, group3_id, error) + + ! + ! Close the groups. + ! + CALL h5gclose_f(group1_id, error) + CALL h5gclose_f(group2_id, error) + CALL h5gclose_f(group3_id, error) + + ! + ! Terminate access to the file. + ! + CALL h5fclose_f(file_id, error) + + END PROGRAM GRPSEXAMPLE diff --git a/fortran/examples/hyperslab.f90 b/fortran/examples/hyperslab.f90 new file mode 100644 index 0000000..0be4b60 --- /dev/null +++ b/fortran/examples/hyperslab.f90 @@ -0,0 +1,199 @@ +! +! This example shows how to write and read a hyperslab. +! + + PROGRAM SELECTEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=7), PARAMETER :: filename = "sdsf.h5" ! File name + CHARACTER(LEN=8), PARAMETER :: dsetname = "IntArray" ! Dataset name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: dset_id ! Dataset identifier + INTEGER(HID_T) :: dataspace ! Dataspace identifier + INTEGER(HID_T) :: memspace ! memspace identifier + + INTEGER(HSIZE_T), DIMENSION(3) :: dimsm = (/7,7,3/) ! Dataset dimensions + ! in memory + INTEGER(HSIZE_T), DIMENSION(2) :: dims_out ! Buffer to read in dataset + ! dimesions + INTEGER(HSIZE_T), DIMENSION(2) :: dimsf = (/5,6/) ! Dataset dimensions. + + INTEGER(HSIZE_T), DIMENSION(2) :: count = (/3,4/) + ! Size of the hyperslab in the file + INTEGER(HSIZE_T), DIMENSION(2) :: offset = (/1,2/) + !hyperslab offset in the file + INTEGER(HSIZE_T), DIMENSION(3) :: count_out = (/3,4,1/) + !Size of the hyperslab in memory + INTEGER(HSIZE_T), DIMENSION(3) :: offset_out = (/3,0,0/) + !hyperslab offset in memory + INTEGER, DIMENSION(5,6) :: data ! Data to write + INTEGER, DIMENSION(7,7,3) :: data_out ! Output buffer + INTEGER :: dsetrank = 2 ! Dataset rank ( in file ) + INTEGER :: memrank = 3 ! Dataset rank ( in memory ) + INTEGER :: rank + INTEGER :: i, j, k + + INTEGER :: error, error_n ! Error flags + + + ! + ! Write data to the HDF5 file. + ! + + ! + ! Data initialization. + ! + do i = 1, 5 + do j = 1, 6 + data(i,j) = (i-1) + (j-1); + end do + end do + ! + ! 0, 1, 2, 3, 4, 5 + ! 1, 2, 3, 4, 5, 6 + ! 2, 3, 4, 5, 6, 7 + ! 3, 4, 5, 6, 7, 8 + ! 4, 5, 6, 7, 8, 9 + ! + + ! + ! Initialize FORTRAN predefined datatypes + ! + CALL h5init_types_f(error) + + ! + ! Create a new file using default properties. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + + ! + ! Create the data space for the dataset. + ! + CALL h5screate_simple_f(dsetrank, dimsf, dataspace, error) + + ! + ! Create the dataset with default properties. + ! + CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dataspace, & + dset_id, error) + + ! + ! Write the dataset. + ! + CALL h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, data, error) + + ! + ! Close the dataspace for the dataset. + ! + CALL h5sclose_f(dataspace, error) + + ! + ! Close the dataset. + ! + CALL h5dclose_f(dset_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + ! + ! This part of the code reads the hyperslab from the sds.h5 file just + ! created, into a 2-dimensional plane of the 3-dimensional dataset. + ! + + ! + ! Initialize data_out array. + ! + do i = 1, 7 + do j = 1, 7 + do k = 1,3 + data_out(i,j,k) = 0; + end do + end do + end do + + ! + ! Open the file. + ! + CALL h5fopen_f (filename, H5F_ACC_RDONLY_F, file_id, error) + + ! + ! Open the dataset. + ! + CALL h5dopen_f(file_id, dsetname, dset_id, error) + + ! + ! Get dataset's dataspace identifier. + ! + CALL h5dget_space_f(dset_id, dataspace, error) + + ! + ! Select hyperslab in the dataset. + ! + CALL h5sselect_hyperslab_f(dataspace, H5S_SELECT_SET_F, & + offset, count, error) + ! + ! Create memory dataspace. + ! + CALL h5screate_simple_f(memrank, dimsm, memspace, error) + + ! + ! Select hyperslab in memory. + ! + CALL h5sselect_hyperslab_f(memspace, H5S_SELECT_SET_F, & + offset_out, count_out, error) + + ! + ! Read data from hyperslab in the file into the hyperslab in + ! memory and display. + ! + CALL H5Dread_f(dset_id, H5T_NATIVE_INTEGER, data_out, error, & + memspace, dataspace) + + ! + ! Display data_out array + ! + do i = 1, 7 + print *, (data_out(i,j,1), j = 1,7) + end do + + ! 0 0 0 0 0 0 0 + ! 0 0 0 0 0 0 0 + ! 0 0 0 0 0 0 0 + ! 3 4 5 6 0 0 0 + ! 4 5 6 7 0 0 0 + ! 5 6 7 8 0 0 0 + ! 0 0 0 0 0 0 0 + ! + + ! + ! Close the dataspace for the dataset. + ! + CALL h5sclose_f(dataspace, error) + + ! + ! Close the memoryspace. + ! + CALL h5sclose_f(memspace, error) + + ! + ! Close the dataset. + ! + CALL h5dclose_f(dset_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM SELECTEXAMPLE diff --git a/fortran/examples/mountexample.f90 b/fortran/examples/mountexample.f90 new file mode 100644 index 0000000..561b018 --- /dev/null +++ b/fortran/examples/mountexample.f90 @@ -0,0 +1,183 @@ +! +!In the following example we create one file with a group in it, +!and another file with a dataset. Mounting is used to +!access the dataset from the second file as a member of a group +!in the first file. +! + + PROGRAM MOUNTEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + ! + !the respective filename is "mount1.h5" and "mount2.h5" + ! + CHARACTER(LEN=9), PARAMETER :: filename1 = "mount1.h5" + CHARACTER(LEN=9), PARAMETER :: filename2 = "mount2.h5" + + ! + !data space rank and dimensions + ! + INTEGER, PARAMETER :: RANK = 2 + INTEGER, PARAMETER :: NX = 4 + INTEGER, PARAMETER :: NY = 5 + + ! + ! File identifiers + ! + INTEGER(HID_T) :: file1_id, file2_id + + ! + ! Group identifier + ! + INTEGER(HID_T) :: gid + + ! + ! dataset identifier + ! + INTEGER(HID_T) :: dset_id + + ! + ! data space identifier + ! + INTEGER(HID_T) :: dataspace + + ! + ! data type identifier + ! + INTEGER(HID_T) :: dtype_id + + ! + !The dimensions for the dataset. + ! + INTEGER(HSIZE_T), DIMENSION(2) :: dims = (/NX,NY/) + + ! + !flag to check operation success + ! + INTEGER :: error + + ! + !general purpose integer + ! + INTEGER :: i, j + + ! + !data buffers + ! + INTEGER, DIMENSION(NX,NY) :: data_in, data_out + + ! + !Initialize FORTRAN predifined datatypes + ! + CALL h5init_types_f(error) + + ! + !Initialize data_in buffer + ! + do i = 1, NX + do j = 1, NY + data_in(i,j) = (i-1) + (j-1) + end do + end do + + ! + !Create first file "mount1.h5" using default properties. + ! + CALL h5fcreate_f(filename1, H5F_ACC_TRUNC_F, file1_id, error) + + ! + !Create group "/G" inside file "mount1.h5". + ! + CALL h5gcreate_f(file1_id, "/G", gid, error) + + ! + !close file and group identifiers. + ! + CALL h5gclose_f(gid, error) + CALL h5fclose_f(file1_id, error) + + ! + !Create second file "mount2.h5" using default properties. + ! + CALL h5fcreate_f(filename2, H5F_ACC_TRUNC_F, file2_id, error) + + ! + !Create data space for the dataset. + ! + CALL h5screate_simple_f(RANK, dims, dataspace, error) + + ! + !Create dataset "/D" inside file "mount2.h5". + ! + CALL h5dcreate_f(file2_id, "/D", H5T_NATIVE_INTEGER, dataspace, & + dset_id, error) + + ! + ! Write data_in to the dataset + ! + CALL h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, data_in, error) + + ! + !close file, dataset and dataspace identifiers. + ! + CALL h5sclose_f(dataspace, error) + CALL h5dclose_f(dset_id, error) + CALL h5fclose_f(file2_id, error) + + ! + !reopen both files. + ! + CALL h5fopen_f (filename1, H5F_ACC_RDWR_F, file1_id, error) + CALL h5fopen_f (filename2, H5F_ACC_RDWR_F, file2_id, error) + + ! + !mount the second file under the first file's "/G" group. + ! + CALL h5fmount_f (file1_id, "/G", file2_id, error) + + + ! + !Access dataset D in the first file under /G/D name. + ! + CALL h5dopen_f(file1_id, "/G/D", dset_id, error) + + ! + !Get dataset's data type. + ! + CALL h5dget_type_f(dset_id, dtype_id, error) + + ! + !Read the dataset. + ! + CALL h5dread_f(dset_id, dtype_id, data_out, error) + + ! + !Print out the data. + ! + do i = 1, NX + print *, (data_out(i,j), j = 1, NY) + end do + + + ! + !Close dset_id and dtype_id. + ! + CALL h5dclose_f(dset_id, error) + CALL h5tclose_f(dtype_id, error) + + ! + !unmount the second file. + ! + CALL h5funmount_f(file1_id, "/G", error); + + ! + !Close both files. + ! + CALL h5fclose_f(file1_id, error) + CALL h5fclose_f(file2_id, error) + + END PROGRAM MOUNTEXAMPLE + diff --git a/fortran/examples/refobjexample.f90 b/fortran/examples/refobjexample.f90 new file mode 100644 index 0000000..0050cbe --- /dev/null +++ b/fortran/examples/refobjexample.f90 @@ -0,0 +1,136 @@ +! +! This program shows how to create and store references to the objects. +! Program creates a file, two groups, a dataset to store integer data and +! a dataset to store references to the objects. +! Stored references are used to open the objects they are point to. +! Data is written to the dereferenced dataset, and class type is displayed for +! the shared datatype. +! + PROGRAM OBJ_REFERENCES + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + CHARACTER(LEN=10), PARAMETER :: filename = "FORTRAN.h5" ! File + CHARACTER(LEN=8), PARAMETER :: dsetnamei = "INTEGERS" ! Dataset with the integer data + CHARACTER(LEN=17), PARAMETER :: dsetnamer = "OBJECT_REFERENCES" ! Dataset wtih object + ! references + CHARACTER(LEN=6), PARAMETER :: groupname1 = "GROUP1" ! Groups in the file + CHARACTER(LEN=6), PARAMETER :: groupname2 = "GROUP2" ! + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: grp1_id ! Group identifiers + INTEGER(HID_T) :: grp2_id ! + INTEGER(HID_T) :: dset_id ! Dataset identifiers + INTEGER(HID_T) :: dsetr_id ! + INTEGER(HID_T) :: type_id ! Type identifier + INTEGER(HID_T) :: space_id ! Dataspace identifiers + INTEGER(HID_T) :: spacer_id ! + INTEGER :: error + INTEGER(HSIZE_T), DIMENSION(1) :: dims = (/5/) + INTEGER(HSIZE_T), DIMENSION(1) :: dimsr= (/4/) + INTEGER(HSIZE_T), DIMENSION(1) :: my_maxdims = (/5/) + INTEGER :: rank = 1 + INTEGER :: rankr = 1 + TYPE(hobj_ref_t_f), DIMENSION(4) :: ref + TYPE(hobj_ref_t_f), DIMENSION(4) :: ref_out + INTEGER, DIMENSION(5) :: data = (/1, 2, 3, 4, 5/) + INTEGER :: class + ! + ! Initialize FORTRAN predefined datatypes + ! + CALL h5init_types_f(error) + ! + ! Create a file + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + ! Default file access and file creation + ! properties are used. + ! + ! Create a group in the file + ! + CALL h5gcreate_f(file_id, groupname1, grp1_id, error) + ! + ! Create a group inside the created gorup + ! + CALL h5gcreate_f(grp1_id, groupname2, grp2_id, error) + ! + ! Create dataspaces for datasets + ! + CALL h5screate_simple_f(rank, dims, space_id, error, maxdims=my_maxdims) + CALL h5screate_simple_f(rankr, dimsr, spacer_id, error) + ! + ! Create integer dataset + ! + CALL h5dcreate_f(file_id, dsetnamei, H5T_NATIVE_INTEGER, space_id, & + dset_id, error) + ! + ! Create dataset to store references to the objects + ! + CALL h5dcreate_f(file_id, dsetnamer, H5T_STD_REF_OBJ, spacer_id, & + dsetr_id, error) + ! + ! Create a datatype and store in the file + ! + CALL h5tcopy_f(H5T_NATIVE_REAL, type_id, error) + CALL h5tcommit_f(file_id, "MyType", type_id, error) + ! + ! Close dataspaces, groups and integer dataset + ! + CALL h5sclose_f(space_id, error) + CALL h5sclose_f(spacer_id, error) + CALL h5tclose_f(type_id, error) + CALL h5dclose_f(dset_id, error) + CALL h5gclose_f(grp1_id, error) + CALL h5gclose_f(grp2_id, error) + ! + ! Create references to two groups, integer dataset and shared datatype + ! and write it to the dataset in the file + ! + CALL h5rcreate_f(file_id, groupname1, ref(1), error) + CALL h5rcreate_f(file_id, "/GROUP1/GROUP2", ref(2), error) + CALL h5rcreate_f(file_id, dsetnamei, ref(3), error) + CALL h5rcreate_f(file_id, "MyType", ref(4), error) + CALL h5dwrite_f(dsetr_id, H5T_STD_REF_OBJ, ref, error) + ! + ! Close the dataset + ! + CALL h5dclose_f(dsetr_id, error) + ! + ! Reopen the dataset with object references and read references to the buffer + ! + CALL h5dopen_f(file_id, dsetnamer,dsetr_id,error) + CALL h5dread_f(dsetr_id, H5T_STD_REF_OBJ, ref_out, error) + ! + ! Dereference the third reference. We know that it is a dataset. On practice + ! one should use h5rget_object_type_f function to find out + ! the type of an object the reference points to. + ! + CALL h5rdereference_f(dsetr_id, ref(3), dset_id, error) + ! + ! Write data to the dataset. + ! + CALL h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, data, error) + if (error .eq. 0) write(*,*) "Data has been successfully written to the dataset " + ! + ! Dereference the fourth reference. We know that it is a datatype. On practice + ! one should use h5rget_object_type_f function to find out + ! the type of an object the reference points to. + ! + CALL h5rdereference_f(dsetr_id, ref(4), type_id, error) + ! + ! Get datatype class and display it if it is of a FLOAT class. + ! + CALL h5tget_class_f(type_id, class, error) + if(class .eq. H5T_FLOAT_F) write(*,*) "Stored datatype is of a FLOAT class" + ! + ! Close all objects. + ! + CALL h5dclose_f(dset_id, error) + CALL h5tclose_f(type_id, error) + CALL h5dclose_f(dsetr_id, error) + CALL h5fclose_f(file_id, error) + + END PROGRAM OBJ_REFERENCES + + diff --git a/fortran/examples/refregexample.f90 b/fortran/examples/refregexample.f90 new file mode 100644 index 0000000..47ebaac --- /dev/null +++ b/fortran/examples/refregexample.f90 @@ -0,0 +1,159 @@ +! +! This program shows how to create, store and dereference references +! to the dataset regions. +! Program creates a file and writes two dimensional integer dataset +! to it. Then program creates and stores references to the hyperslab +! and 3 points selected in the integer dataset, in the second dataset. +! Program reopens the second dataset, reads and dereferences region +! references, and then reads and displays selected data from the +! integer dataset. +! + PROGRAM REG_REFERENCE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + CHARACTER(LEN=10), PARAMETER :: filename = "FORTRAN.h5" + CHARACTER(LEN=6), PARAMETER :: dsetnamev = "MATRIX" + CHARACTER(LEN=17), PARAMETER :: dsetnamer = "REGION_REFERENCES" + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: space_id ! Dataspace identifier + INTEGER(HID_T) :: spacer_id ! Dataspace identifier + INTEGER(HID_T) :: dsetv_id ! Dataset identifier + INTEGER(HID_T) :: dsetr_id ! Dataset identifier + INTEGER :: error + TYPE(hdset_reg_ref_t_f) , DIMENSION(2) :: ref ! Buffers to store references + TYPE(hdset_reg_ref_t_f) , DIMENSION(2) :: ref_out ! + INTEGER(HSIZE_T), DIMENSION(2) :: dims = (/2,9/) ! Datasets dimensions + INTEGER(HSIZE_T), DIMENSION(1) :: dimsr = (/2/) ! + INTEGER(HSSIZE_T), DIMENSION(2) :: start + INTEGER(HSIZE_T), DIMENSION(2) :: count + INTEGER :: rankr = 1 + INTEGER :: rank = 2 + INTEGER , DIMENSION(2,9) :: data + INTEGER , DIMENSION(2,9) :: data_out = 0 + INTEGER(HSSIZE_T) , DIMENSION(2,3) :: coord + INTEGER(SIZE_T) ::num_points = 3 ! Number of selected points + INTEGER :: i, j + coord = reshape((/1,1,2,7,1,9/), (/2,3/)) ! Coordinates of selected points + data = reshape ((/1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6/), (/2,9/)) + ! + ! Initialize FORTRAN predefined datatypes. + ! + CALL h5init_types_f(error) + ! + ! Create a new file. + ! + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + ! Default file access and file creation + ! properties are used. + ! + ! Create dataspaces: + ! + ! for dataset with references to dataset regions + ! + CALL h5screate_simple_f(rankr, dimsr, spacer_id, error) + ! + ! for integer dataset + ! + CALL h5screate_simple_f(rank, dims, space_id, error) + ! + ! Create and write datasets: + ! + ! Integer dataset + ! + CALL h5dcreate_f(file_id, dsetnamev, H5T_NATIVE_INTEGER, space_id, & + dsetv_id, error) + CALL h5dwrite_f(dsetv_id, H5T_NATIVE_INTEGER, data, error) + CALL h5dclose_f(dsetv_id, error) + ! + ! Dataset with references + ! + CALL h5dcreate_f(file_id, dsetnamer, H5T_STD_REF_DSETREG, spacer_id, & + dsetr_id, error) + ! + ! Create a reference to the hyperslab selection. + ! + start(1) = 0 + start(2) = 3 + count(1) = 2 + count(2) = 3 + CALL h5sselect_hyperslab_f(space_id, H5S_SELECT_SET_F, & + start, count, error) + CALL h5rcreate_f(file_id, dsetnamev, space_id, ref(1), error) + ! + ! Create a reference to elements selection. + ! + CALL h5sselect_none_f(space_id, error) + CALL h5sselect_elements_f(space_id, H5S_SELECT_SET_F, rank, num_points,& + coord, error) + CALL h5rcreate_f(file_id, dsetnamev, space_id, ref(2), error) + ! + ! Write dataset with the references. + ! + CALL h5dwrite_f(dsetr_id, H5T_STD_REF_DSETREG, ref, error) + ! + ! Close all objects. + ! + CALL h5sclose_f(space_id, error) + CALL h5sclose_f(spacer_id, error) + CALL h5dclose_f(dsetr_id, error) + CALL h5fclose_f(file_id, error) + ! + ! Reopen the file to test selections. + ! + CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error) + CALL h5dopen_f(file_id, dsetnamer, dsetr_id, error) + ! + ! Read references to the dataset regions. + ! + CALL h5dread_f(dsetr_id, H5T_STD_REF_DSETREG, ref_out, error) + ! + ! Dereference the first reference. + ! + CALL H5rdereference_f(dsetr_id, ref_out(1), dsetv_id, error) + CALL H5rget_region_f(dsetr_id, ref_out(1), space_id, error) + ! + ! Read selected data from the dataset. + ! + CALL h5dread_f(dsetv_id, H5T_NATIVE_INTEGER, data_out, error, & + mem_space_id = space_id, file_space_id = space_id) + write(*,*) "Hypeslab selection" + write(*,*) + do i = 1,2 + write(*,*) (data_out (i,j), j = 1,9) + enddo + write(*,*) + CALL h5sclose_f(space_id, error) + CALL h5dclose_f(dsetv_id, error) + data_out = 0 + ! + ! Dereference the second reference. + ! + CALL H5rdereference_f(dsetr_id, ref_out(2), dsetv_id, error) + CALL H5rget_region_f(dsetr_id, ref_out(2), space_id, error) + ! + ! Read selected data from the dataset. + ! + CALL h5dread_f(dsetv_id, H5T_NATIVE_INTEGER, data_out, error, & + mem_space_id = space_id, file_space_id = space_id) + write(*,*) "Point selection" + write(*,*) + do i = 1,2 + write(*,*) (data_out (i,j), j = 1,9) + enddo + ! + ! Close all objects + ! + CALL h5sclose_f(space_id, error) + CALL h5dclose_f(dsetv_id, error) + CALL h5dclose_f(dsetr_id, error) + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM REG_REFERENCE + + diff --git a/fortran/examples/run_examples.sh b/fortran/examples/run_examples.sh new file mode 100755 index 0000000..3126a6c --- /dev/null +++ b/fortran/examples/run_examples.sh @@ -0,0 +1,13 @@ +#!/bin/csh +./dsetexample +./fileexample +./rwdsetexample +./attrexample +./groupexample +./grpsexample +./grpdsetexample +./hyperslab +./selectele +./grpit +./refobjexample +./refregexample diff --git a/fortran/examples/rwdsetexample.f90 b/fortran/examples/rwdsetexample.f90 new file mode 100644 index 0000000..085b4e8 --- /dev/null +++ b/fortran/examples/rwdsetexample.f90 @@ -0,0 +1,78 @@ +! +! The following example shows how to write and read to/from an existing dataset. +! It opens the file created in the previous example, obtains the dataset +! identifier, writes the data to the dataset in the file, +! then reads the dataset to memory. +! + + + PROGRAM RWDSETEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=8), PARAMETER :: filename = "dsetf.h5" ! File name + CHARACTER(LEN=4), PARAMETER :: dsetname = "dset" ! Dataset name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: dset_id ! Dataset identifier + + INTEGER :: error ! Error flag + INTEGER :: i, j + + INTEGER, DIMENSION(4,6) :: dset_data, data_out ! Data buffers + + ! + ! Initialize the dset_data array. + ! + do i = 1, 4 + do j = 1, 6 + dset_data(i,j) = (i-1)*6 + j; + end do + end do + + ! + ! Initialize FORTRAN predefined datatypes + ! + CALL h5init_types_f(error) + + ! + ! Open an existing file. + ! + CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error) + + ! + ! Open an existing dataset. + ! + CALL h5dopen_f(file_id, dsetname, dset_id, error) + + ! + ! Write the dataset. + ! + CALL h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, dset_data, error) + + ! + ! Read the dataset. + ! + CALL h5dread_f(dset_id, H5T_NATIVE_INTEGER, data_out, error) + + ! + ! Close the dataset. + ! + CALL h5dclose_f(dset_id, error) + + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM RWDSETEXAMPLE + + + diff --git a/fortran/examples/selectele.f90 b/fortran/examples/selectele.f90 new file mode 100644 index 0000000..db91a77 --- /dev/null +++ b/fortran/examples/selectele.f90 @@ -0,0 +1,282 @@ +! +! This program creates two files, copy1.h5, and copy2.h5. +! In copy1.h5, it creates a 3x4 dataset called 'Copy1', +! and write 0's to this dataset. +! In copy2.h5, it create a 3x4 dataset called 'Copy2', +! and write 1's to this dataset. +! It closes both files, reopens both files, selects two +! points in copy1.h5 and writes values to them. Then it +! uses an H5Scopy to write the same selection to copy2.h5. +! Program reopens the files, and reads and prints the contents of +! the two datasets. +! + + PROGRAM SELECTEXAMPLE + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + CHARACTER(LEN=8), PARAMETER :: filename1 = "copy1.h5" ! File name + CHARACTER(LEN=8), PARAMETER :: filename2 = "copy2.h5" ! + CHARACTER(LEN=5), PARAMETER :: dsetname1 = "Copy1" ! Dataset name + CHARACTER(LEN=5), PARAMETER :: dsetname2 = "Copy2" ! + + INTEGER, PARAMETER :: RANK = 2 ! Dataset rank + + INTEGER(SIZE_T), PARAMETER :: NUMP = 2 ! Number of points selected + + INTEGER(HID_T) :: file1_id ! File1 identifier + INTEGER(HID_T) :: file2_id ! File2 identifier + INTEGER(HID_T) :: dset1_id ! Dataset1 identifier + INTEGER(HID_T) :: dset2_id ! Dataset2 identifier + INTEGER(HID_T) :: dataspace1 ! Dataspace identifier + INTEGER(HID_T) :: dataspace2 ! Dataspace identifier + INTEGER(HID_T) :: memspace ! memspace identifier + + INTEGER(HSIZE_T), DIMENSION(1) :: dimsm = (/2/) + ! Memory dataspace dimensions + INTEGER(HSIZE_T), DIMENSION(2) :: dimsf = (/3,4/) + ! File dataspace dimensions + INTEGER(HSSIZE_T), DIMENSION(RANK,NUMP) :: coord ! Elements coordinates + ! in the file + + INTEGER, DIMENSION(3,4) :: buf1, buf2, bufnew ! Data buffers + INTEGER, DIMENSION(2) :: val = (/53, 59/) ! Values to write + + INTEGER :: memrank = 1 ! Rank of the dataset in memory + + INTEGER :: i, j + + INTEGER :: error ! Error flag + LOGICAL :: status + + + ! + ! Create two files containing identical datasets. Write 0's to one + ! and 1's to the other. + ! + + ! + ! Data initialization. + ! + do i = 1, 3 + do j = 1, 4 + buf1(i,j) = 0; + end do + end do + + do i = 1, 3 + do j = 1, 4 + buf2(i,j) = 1; + end do + end do + + ! + ! Initialize FORTRAN predefined datatypes + ! + CALL h5init_types_f(error) + + ! + ! Create file1, file2 using default properties. + ! + CALL h5fcreate_f(filename1, H5F_ACC_TRUNC_F, file1_id, error) + + CALL h5fcreate_f(filename2, H5F_ACC_TRUNC_F, file2_id, error) + + ! + ! Create the data space for the datasets. + ! + CALL h5screate_simple_f(RANK, dimsf, dataspace1, error) + + CALL h5screate_simple_f(RANK, dimsf, dataspace2, error) + + ! + ! Create the datasets with default properties. + ! + CALL h5dcreate_f(file1_id, dsetname1, H5T_NATIVE_INTEGER, dataspace1, & + dset1_id, error) + + CALL h5dcreate_f(file2_id, dsetname2, H5T_NATIVE_INTEGER, dataspace2, & + dset2_id, error) + + ! + ! Write the datasets. + ! + CALL h5dwrite_f(dset1_id, H5T_NATIVE_INTEGER, buf1, error) + + CALL h5dwrite_f(dset2_id, H5T_NATIVE_INTEGER, buf2, error) + + ! + ! Close the dataspace for the datasets. + ! + CALL h5sclose_f(dataspace1, error) + + CALL h5sclose_f(dataspace2, error) + + ! + ! Close the datasets. + ! + CALL h5dclose_f(dset1_id, error) + + CALL h5dclose_f(dset2_id, error) + + ! + ! Close the files. + ! + CALL h5fclose_f(file1_id, error) + + CALL h5fclose_f(file2_id, error) + + ! + ! Open the two files. Select two points in one file, write values to + ! those point locations, then do H5Scopy and write the values to the + ! other file. Close files. + ! + + ! + ! Open the files. + ! + CALL h5fopen_f (filename1, H5F_ACC_RDWR_F, file1_id, error) + + CALL h5fopen_f (filename2, H5F_ACC_RDWR_F, file2_id, error) + + ! + ! Open the datasets. + ! + CALL h5dopen_f(file1_id, dsetname1, dset1_id, error) + + CALL h5dopen_f(file2_id, dsetname2, dset2_id, error) + + ! + ! Get dataset1's dataspace identifier. + ! + CALL h5dget_space_f(dset1_id, dataspace1, error) + + ! + ! Create memory dataspace. + ! + CALL h5screate_simple_f(memrank, dimsm, memspace, error) + + ! + ! Set the selected point positions. Because Fortran array index starts + ! from 1, so add one to the actual select points in C. + ! + coord(1,1) = 1 + coord(2,1) = 2 + coord(1,2) = 1 + coord(2,2) = 4 + + ! + ! Select the elements in file space. + ! + CALL h5sselect_elements_f(dataspace1, H5S_SELECT_SET_F, RANK, NUMP,& + coord, error) + + ! + ! Write value into the selected points in dataset1. + ! + CALL H5dwrite_f(dset1_id, H5T_NATIVE_INTEGER, val, error, & + mem_space_id=memspace, file_space_id=dataspace1) + + ! + ! Copy the daspace1 into dataspace2. + ! + CALL h5scopy_f(dataspace1, dataspace2, error) + + ! + ! Write value into the selected points in dataset2. + ! + CALL H5dwrite_f(dset2_id, H5T_NATIVE_INTEGER, val, error, & + mem_space_id=memspace, file_space_id=dataspace2) + + ! + ! Close the dataspace for the datasets. + ! + CALL h5sclose_f(dataspace1, error) + + CALL h5sclose_f(dataspace2, error) + + ! + ! Close the memoryspace. + ! + CALL h5sclose_f(memspace, error) + + ! + ! Close the datasets. + ! + CALL h5dclose_f(dset1_id, error) + + CALL h5dclose_f(dset2_id, error) + + ! + ! Close the files. + ! + CALL h5fclose_f(file1_id, error) + + CALL h5fclose_f(file2_id, error) + + ! + ! Open both files and print the contents of the datasets. + ! + + ! + ! Open the files. + ! + CALL h5fopen_f (filename1, H5F_ACC_RDWR_F, file1_id, error) + + CALL h5fopen_f (filename2, H5F_ACC_RDWR_F, file2_id, error) + + ! + ! Open the datasets. + ! + CALL h5dopen_f(file1_id, dsetname1, dset1_id, error) + + CALL h5dopen_f(file2_id, dsetname2, dset2_id, error) + + ! + ! Read dataset from the first file. + ! + CALL h5dread_f(dset1_id, H5T_NATIVE_INTEGER, bufnew, error) + + ! + ! Display the data read from dataset "Copy1" + ! + write(*,*) "The data in dataset Copy1 is: " + do i = 1, 3 + print *, (bufnew(i,j), j = 1,4) + end do + + ! + ! Read dataset from the second file. + ! + CALL h5dread_f(dset2_id, H5T_NATIVE_INTEGER, bufnew, error) + + ! + ! Display the data read from dataset "Copy2" + ! + write(*,*) "The data in dataset Copy2 is: " + do i = 1, 3 + print *, (bufnew(i,j), j = 1,4) + end do + + ! + ! Close datasets. + ! + CALL h5dclose_f(dset1_id, error) + + CALL h5dclose_f(dset2_id, error) + + ! + ! Close files. + ! + CALL h5fclose_f(file1_id, error) + + CALL h5fclose_f(file2_id, error) + + ! + ! Close FORTRAN predefined datatypes. + ! + CALL h5close_types_f(error) + + END PROGRAM SELECTEXAMPLE |