diff options
author | Scot Breitenfeld <brtnfld@hdfgroup.org> | 2011-08-18 14:32:47 (GMT) |
---|---|---|
committer | Scot Breitenfeld <brtnfld@hdfgroup.org> | 2011-08-18 14:32:47 (GMT) |
commit | a9c065c5ce65bb7dca560d53642574dba608dc78 (patch) | |
tree | 2d36b7afd3f3a83314db25aba081e95254d28841 /fortran/examples | |
parent | a968e2d409d975ac5b584680620d2589b0409f88 (diff) | |
download | hdf5-a9c065c5ce65bb7dca560d53642574dba608dc78.zip hdf5-a9c065c5ce65bb7dca560d53642574dba608dc78.tar.gz hdf5-a9c065c5ce65bb7dca560d53642574dba608dc78.tar.bz2 |
[svn-r21248] Mereged the F2003 branch into the trunk.
Items merged: fortran directory,
src/libhdf5.settings.in
configure.in configure
MANIFEST
Tested: (all platforms used by daily tests, both with --enable-fortran and --enable-fortran2003)
Diffstat (limited to 'fortran/examples')
-rw-r--r-- | fortran/examples/Makefile.am | 33 | ||||
-rw-r--r-- | fortran/examples/Makefile.in | 35 | ||||
-rw-r--r-- | fortran/examples/compound_complex_fortran2003.f90 | 117 | ||||
-rw-r--r-- | fortran/examples/compound_fortran2003.f90 | 208 | ||||
-rw-r--r-- | fortran/examples/nested_derived_type.f90 | 212 | ||||
-rw-r--r-- | fortran/examples/rwdset_fortran2003.f90 | 181 |
6 files changed, 768 insertions, 18 deletions
diff --git a/fortran/examples/Makefile.am b/fortran/examples/Makefile.am index 57a8f94..d767490 100644 --- a/fortran/examples/Makefile.am +++ b/fortran/examples/Makefile.am @@ -31,17 +31,28 @@ endif # be run. # We don't tell automake about these programs so that it doesn't try to # compile them with the regular fortran compiler. -TEST_PROG=dsetexample fileexample rwdsetexample attrexample groupexample \ - grpsexample grpdsetexample hyperslab selectele grpit refobjexample \ - refregexample mountexample compound -TEST_SCRIPT=testh5fc.sh + +TEST_PROG=dsetexample fileexample rwdsetexample attrexample groupexample \ + grpsexample grpdsetexample hyperslab selectele grpit refobjexample \ + refregexample mountexample compound # List files to be installed here -INSTALL_FILES=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 ph5example.f90 +INSTALL_FILES=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 ph5example.f90 INSTALL_SCRIPT_FILES = run-fortran-ex.sh +INSTALL_SCRIPT_FILES = run-fortran-ex.sh + + +# Add attention tests for Fortran 2003 features +if FORTRAN_2003_CONDITIONAL_F + TEST_PROG += rwdset_fortran2003 nested_derived_type \ + compound_fortran2003 compound_complex_fortran2003 + INSTALL_FILES += rwdset_fortran2003.f90 nested_derived_type.f90 \ + compound_fortran2003.f90 compound_complex_fortran2003.f90 +endif +TEST_SCRIPT=testh5fc.sh # Mark this directory as part of the Fortran API FORTRAN_API=yes @@ -89,6 +100,12 @@ refregexample: refregexample.f90 mountexample: mountexample.f90 compound: compound.f90 ph5example: ph5example.f90 +if FORTRAN_2003_CONDITIONAL_F +rwdset_fortran2003: rwdset_fortran2003.f90 +nested_derived_type: nested_derived_type.f90 +compound_fortran2003: compound_fortran2003.f90 +compound_complex_fortran2003: compound_complex_fortran2003.f90 +endif include $(top_srcdir)/config/examples.am include $(top_srcdir)/config/conclude.am diff --git a/fortran/examples/Makefile.in b/fortran/examples/Makefile.in index c0f89c3..12460f3 100644 --- a/fortran/examples/Makefile.in +++ b/fortran/examples/Makefile.in @@ -59,6 +59,14 @@ DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in \ $(top_srcdir)/config/commence.am \ $(top_srcdir)/config/conclude.am \ $(top_srcdir)/config/examples.am + +# Add attention tests for Fortran 2003 features +@FORTRAN_2003_CONDITIONAL_F_TRUE@am__append_1 = rwdset_fortran2003 nested_derived_type \ +@FORTRAN_2003_CONDITIONAL_F_TRUE@ compound_fortran2003 compound_complex_fortran2003 + +@FORTRAN_2003_CONDITIONAL_F_TRUE@am__append_2 = rwdset_fortran2003.f90 nested_derived_type.f90 \ +@FORTRAN_2003_CONDITIONAL_F_TRUE@ compound_fortran2003.f90 compound_complex_fortran2003.f90 + TESTS = subdir = fortran/examples ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -145,6 +153,7 @@ F9XMODEXT = @F9XMODEXT@ F9XMODFLAG = @F9XMODFLAG@ F9XSUFFIXFLAG = @F9XSUFFIXFLAG@ FC = @FC@ +FC2003 = @FC2003@ FCFLAGS = @FCFLAGS@ FCFLAGS_f90 = @FCFLAGS_f90@ FCLIBS = @FCLIBS@ @@ -165,10 +174,12 @@ H5_LONE_COLON = @H5_LONE_COLON@ H5_VERSION = @H5_VERSION@ HADDR_T = @HADDR_T@ HAVE_DMALLOC = @HAVE_DMALLOC@ +HAVE_FORTRAN_2003 = @HAVE_FORTRAN_2003@ HDF5_HL = @HDF5_HL@ HDF5_INTERFACES = @HDF5_INTERFACES@ HDF_CXX = @HDF_CXX@ HDF_FORTRAN = @HDF_FORTRAN@ +HDF_FORTRAN2003 = @HDF_FORTRAN2003@ HID_T = @HID_T@ HL = @HL@ HL_FOR = @HL_FOR@ @@ -367,19 +378,19 @@ CHECK_CLEANFILES = *.chkexe *.chklog *.clog *.h5 # be run. # We don't tell automake about these programs so that it doesn't try to # compile them with the regular fortran compiler. -TEST_PROG = dsetexample fileexample rwdsetexample attrexample groupexample \ - grpsexample grpdsetexample hyperslab selectele grpit refobjexample \ - refregexample mountexample compound - -TEST_SCRIPT = testh5fc.sh +TEST_PROG = dsetexample fileexample rwdsetexample attrexample \ + groupexample grpsexample grpdsetexample hyperslab selectele \ + grpit refobjexample refregexample mountexample compound \ + $(am__append_1) # List files to be installed here -INSTALL_FILES = 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 ph5example.f90 - +INSTALL_FILES = 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 ph5example.f90 $(am__append_2) INSTALL_SCRIPT_FILES = run-fortran-ex.sh +TEST_SCRIPT = testh5fc.sh # Mark this directory as part of the Fortran API FORTRAN_API = yes @@ -654,6 +665,10 @@ refregexample: refregexample.f90 mountexample: mountexample.f90 compound: compound.f90 ph5example: ph5example.f90 +@FORTRAN_2003_CONDITIONAL_F_TRUE@rwdset_fortran2003: rwdset_fortran2003.f90 +@FORTRAN_2003_CONDITIONAL_F_TRUE@nested_derived_type: nested_derived_type.f90 +@FORTRAN_2003_CONDITIONAL_F_TRUE@compound_fortran2003: compound_fortran2003.f90 +@FORTRAN_2003_CONDITIONAL_F_TRUE@compound_complex_fortran2003: compound_complex_fortran2003.f90 # How to create EXAMPLEDIR if it doesn't already exist $(EXAMPLEDIR): diff --git a/fortran/examples/compound_complex_fortran2003.f90 b/fortran/examples/compound_complex_fortran2003.f90 new file mode 100644 index 0000000..19671f7 --- /dev/null +++ b/fortran/examples/compound_complex_fortran2003.f90 @@ -0,0 +1,117 @@ +! This is the F2003 version of the h5_compound.c example source code. +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! Copyright by the Board of Trustees of the University of Illinois. * +! All rights reserved. * +! * +! This file is part of HDF5. The full HDF5 copyright notice, including * +! terms governing use, modification, and redistribution, is contained in * +! the files COPYING and Copyright.html. COPYING can be found at the root * +! of the source code distribution tree; Copyright.html can be found at the * +! root level of an installed copy of the electronic HDF5 document set and * +! is linked from the top-level documents page. It can also be found at * +! http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have * +! access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. * +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! +! This example shows how to create an array of a compound datatype which +! contains an array of type complex and how to write it to hdf5 +! and how to read it back into a compound datatype for hdf5. +! + +PROGRAM compound_complex_fortran2003 + + USE hdf5 + USE ISO_C_BINDING + IMPLICIT NONE + + INTEGER, PARAMETER :: r_k8 = KIND(0.0d0) + INTEGER, PARAMETER :: NMAX = 3 + + TYPE sample + COMPLEX(KIND=r_k8), DIMENSION(1:NMAX) :: nlev + REAL(KIND=r_k8) :: N + END TYPE sample + + INTEGER(HID_T) :: sample_type_id, dset_id, dspace_id, file_id + INTEGER(HSIZE_T) :: dims(1) = (/NMAX/) + INTEGER :: error + + TYPE(sample), DIMENSION(1:NMAX), TARGET :: samples, read_samples + INTEGER :: i + + TYPE(C_PTR) :: f_ptr + INTEGER(HSIZE_T), DIMENSION(1) :: array_dims=(/2*NMAX/) ! complex is really (real,real) so double size of array + INTEGER(hid_t) :: array_type_id ! Nested Array Datatype ID + + ! Initialize data + DO i=1,NMAX + samples(i)%nlev(1:NMAX) = (3.14159_r_k8, 2.71828_r_k8) + samples(i)%N = i + END DO + + ! Initialize FORTRAN interface. + CALL h5open_f(error) + + ! Create a new file using default properties. + CALL h5fcreate_f("test.h5", H5F_ACC_TRUNC_F, file_id, error) + ! + ! Create the memory data type. + ! + CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(samples(1)), C_LOC(samples(2))), sample_type_id, error) + + ! Create the array type + CALL h5Tarray_create_f(H5T_NATIVE_DOUBLE, 1, array_dims, array_type_id, error) + ! Then use that array type to insert values into + CALL H5Tinsert_f( sample_type_id, "nlev", & + H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%nlev(1))), array_type_id, error) + CALL H5Tinsert_f( sample_type_id, "N", & + H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%N)), h5kind_to_type(r_k8,H5_REAL_KIND), error) + ! + ! Create dataspace + ! + CALL h5screate_simple_f(1, dims, dspace_id, error) + ! + ! Create the dataset. + ! + CALL H5Dcreate_f(file_id, "samples", sample_type_id, dspace_id, dset_id, error) + ! + ! Write data to the dataset + ! + f_ptr = C_LOC(samples(1)) + CALL H5Dwrite_f(dset_id, sample_type_id, f_ptr, error) + ! Close up + CALL h5dclose_f(dset_id, error) + CALL h5sclose_f(dspace_id, error) + CALL h5fclose_f(file_id, error) + ! + ! Open the file and the dataset. + ! + CALL H5Fopen_f("test.h5", H5F_ACC_RDONLY_F, file_id, error) + + CALL H5Dopen_f(file_id, "samples", dset_id, error) + ! + ! Create the memory data type. + ! + CALL H5Tcreate_f(H5T_COMPOUND_F,H5OFFSETOF(C_LOC(samples(1)), C_LOC(samples(2))), sample_type_id,error) + + CALL H5Tinsert_f( sample_type_id, "nlev", & + H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%nlev(1))), array_type_id, error) + CALL H5Tinsert_f( sample_type_id, "N", & + H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%N)), h5kind_to_type(r_k8,H5_REAL_KIND), error) + + f_ptr = C_LOC(read_samples(1)) + CALL H5Dread_f(dset_id, sample_type_id, f_ptr, error) + + ! + ! Display the fields + ! + DO i=1,NMAX + WRITE(*,'(A,3(" (",F8.5,",",F8.5,")"))') "SAMPLES =",read_samples(i)%nlev(1:NMAX) + WRITE(*,'(A,F8.5)') "N =", read_samples(i)%N + END DO + + CALL H5Tclose_f(sample_type_id, error) + CALL H5Dclose_f(dset_id, error) + CALL H5Fclose_f(file_id, error) + +END PROGRAM compound_complex_fortran2003 diff --git a/fortran/examples/compound_fortran2003.f90 b/fortran/examples/compound_fortran2003.f90 new file mode 100644 index 0000000..579be61 --- /dev/null +++ b/fortran/examples/compound_fortran2003.f90 @@ -0,0 +1,208 @@ +! This is the F2003 version of the h5_compound.c example source code. +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! Copyright by the Board of Trustees of the University of Illinois. * +! All rights reserved. * +! * +! This file is part of HDF5. The full HDF5 copyright notice, including * +! terms governing use, modification, and redistribution, is contained in * +! the files COPYING and Copyright.html. COPYING can be found at the root * +! of the source code distribution tree; Copyright.html can be found at the * +! root level of an installed copy of the electronic HDF5 document set and * +! is linked from the top-level documents page. It can also be found at * +! http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have * +! access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. * +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! +! This example shows how to create a compound data type, +! write an array which has the compound data type to the file, +! and read back fields' subsets. +! + +PROGRAM main + USE hdf5 + USE ISO_C_BINDING + IMPLICIT NONE + +! KIND parameters + + INTEGER, PARAMETER :: int_k1 = SELECTED_INT_KIND(1) ! This should map to INTEGER*1 on most modern processors + INTEGER, PARAMETER :: int_k2 = SELECTED_INT_KIND(4) ! This should map to INTEGER*2 on most modern processors + INTEGER, PARAMETER :: int_k4 = SELECTED_INT_KIND(8) ! This should map to INTEGER*4 on most modern processors + INTEGER, PARAMETER :: int_k8 = SELECTED_INT_KIND(16) ! This should map to INTEGER*8 on most modern processors + + INTEGER, PARAMETER :: r_k4 = SELECTED_REAL_KIND(5) ! This should map to REAL*4 on most modern processors + INTEGER, PARAMETER :: r_k8 = SELECTED_REAL_KIND(10) ! This should map to REAL*8 on most modern processors + +! FILES + + CHARACTER(LEN=*), PARAMETER :: H5FILE_NAME = "SDScompound.h5" + CHARACTER(LEN=*), PARAMETER :: DATASETNAME = "ArrayOfStructures" + + INTEGER, PARAMETER :: LENGTH = 10 + INTEGER, PARAMETER :: RANK = 1 + +!---------------------------------------------------------------- +! First derived-type and dataset + TYPE s1_t + CHARACTER(LEN=1), DIMENSION(1:13) :: chr + INTEGER(KIND=int_k1) :: a + REAL(KIND=r_k4) :: b + REAL(KIND=r_k8) :: c + END TYPE s1_t + + TYPE(s1_t), TARGET :: s1(LENGTH) + INTEGER(hid_t) :: s1_tid ! File datatype identifier + +!---------------------------------------------------------------- +! Second derived-type (subset of s1_t) and dataset + TYPE s2_t + CHARACTER(LEN=1), DIMENSION(1:13) :: chr + REAL(KIND=r_k8) :: c + INTEGER(KIND=int_k1) :: a + END TYPE s2_t + + type(s2_t), target :: s2(LENGTH) + integer(hid_t) :: s2_tid ! Memory datatype handle + +!---------------------------------------------------------------- +! Third "derived-type" (will be used to read float field of s1) + INTEGER(hid_t) :: s3_tid ! Memory datatype handle + REAL(KIND=r_k4), TARGET :: s3(LENGTH) + + INTEGER :: i + INTEGER(hid_t) :: file, dataset, space + !type(H5F_fileid_type) :: file + !type(H5D_dsetid_type) :: dataset + !type(H5S_spaceid_type) :: space + INTEGER(hsize_t) :: DIM(1) = (/LENGTH/) ! Dataspace dimensions + INTEGER(SIZE_T) :: type_size ! Size of the datatype + INTEGER(SIZE_T) :: offset, sizeof_compound + INTEGER :: hdferr + TYPE(C_PTR) :: f_ptr + + INTEGER(SIZE_T) :: type_sizei ! Size of the integer datatype + INTEGER(SIZE_T) :: type_sizer ! Size of the real datatype + INTEGER(SIZE_T) :: type_sized ! Size of the double datatype + INTEGER(hid_t) :: tid3 ! /* Nested Array Datatype ID */ + INTEGER(HSIZE_T), DIMENSION(1) :: tdims1=(/13/) + ! + ! Initialize FORTRAN interface. + ! + + CALL h5open_f(hdferr) + + ! + ! Initialize the data + ! + DO i = 0, LENGTH-1 + s1(i+1)%chr(1)(1:1) = 'a' + s1(i+1)%chr(2)(1:1) = 'b' + s1(i+1)%chr(3)(1:1) = 'c' + s1(i+1)%chr(4:12)(1:1) = ' ' + s1(i+1)%chr(13)(1:1) = 'd' + s1(i+1)%a = i + s1(i+1)%b = i*i + s1(i+1)%c = 1./REAL(i+1) + END DO + ! + ! Create the data space. + ! + ! + CALL H5Screate_simple_f(RANK, dim, space, hdferr) + + ! + ! Create the file. + ! + CALL H5Fcreate_f(H5FILE_NAME, H5F_ACC_TRUNC_F, file, hdferr) + + ! + ! Create the memory data type. + ! + CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(s1(1)), C_LOC(s1(2))), s1_tid, hdferr) + + CALL h5tarray_create_f(H5T_NATIVE_CHARACTER, 1, tdims1, tid3, hdferr) + + CALL H5Tinsert_f(s1_tid, "chr_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%chr)),tid3, hdferr) + CALL H5Tinsert_f(s1_tid, "a_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%a)), h5kind_to_type(int_k1,H5_INTEGER_KIND), hdferr) + CALL H5Tinsert_f(s1_tid, "c_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%c)), h5kind_to_type(r_k8,H5_REAL_KIND), hdferr) + CALL H5Tinsert_f(s1_tid, "b_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%b)), h5kind_to_type(r_k4,H5_REAL_KIND), hdferr) + + ! + ! Create the dataset. + ! + CALL H5Dcreate_f(file, DATASETNAME, s1_tid, space, dataset, hdferr) + + ! + ! Write data to the dataset + ! + + f_ptr = C_LOC(s1(1)) + CALL H5Dwrite_f(dataset, s1_tid, f_ptr, hdferr) + + ! + ! Release resources + ! + CALL H5Tclose_f(s1_tid, hdferr) + CALL H5Sclose_f(space, hdferr) + CALL H5Dclose_f(dataset, hdferr) + CALL H5Fclose_f(file, hdferr) + + ! + ! Open the file and the dataset. + ! + + CALL H5Fopen_f(H5FILE_NAME, H5F_ACC_RDONLY_F, file, hdferr) + + CALL H5Dopen_f(file, DATASETNAME, dataset,hdferr) + + ! + ! Create a data type for s2 + ! + CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(s2(1)), C_LOC(s2(2))), s2_tid, hdferr) + + CALL H5Tinsert_f(s2_tid, "chr_name", H5OFFSETOF(C_LOC(s2(1)),C_LOC(s2(1)%chr)), tid3, hdferr) + CALL H5Tinsert_f(s2_tid, "c_name", H5OFFSETOF(C_LOC(s2(1)),C_LOC(s2(1)%c)), h5kind_to_type(r_k8,H5_REAL_KIND), hdferr) + CALL H5Tinsert_f(s2_tid, "a_name", H5OFFSETOF(C_LOC(s2(1)),C_LOC(s2(1)%a)), h5kind_to_type(int_k1,H5_INTEGER_KIND), hdferr) + + ! + ! Read two fields c and a from s1 dataset. Fields in the file + ! are found by their names "c_name" and "a_name". + s2(:)%c=-1; s2(:)%a=-1; + + + f_ptr = C_LOC(s2(1)) + CALL H5Dread_f(dataset, s2_tid, f_ptr, hdferr) + + ! + ! Display the fields + ! + DO i = 1, length + WRITE(*,'(/,A,/,999(A,1X))') "Field chr :", s2(i)%chr(1:13)(1:1) + ENDDO + WRITE(*,'(/,A,/,999(F8.4,1X))') "Field c :", s2(:)%c + WRITE(*,'(/,A,/,999(I0,1X))') "Field a :", s2(:)%a + ! + ! Create a data type for s3. + ! + CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(s3(1)),C_LOC(s3(2))),s3_tid, hdferr) + + CALL H5Tinsert_f(s3_tid, "b_name", 0, h5kind_to_type(r_k4,H5_REAL_KIND), hdferr) + ! + ! Read field b from s1 dataset. Field in the file is found by its name. + ! + s3(:)=-1 + f_ptr = C_LOC(s3(1)) + CALL H5Dread_f(dataset, s3_tid, f_ptr, hdferr) + ! + ! Display the field + ! + WRITE(*,'(/,A,/,999(F8.4,1X))') "Field b :",s3(:) + ! + ! Release resources + ! + CALL H5Tclose_f(s2_tid, hdferr) + CALL H5Tclose_f(s3_tid, hdferr) + CALL H5Dclose_f(dataset, hdferr) + CALL H5Fclose_f(file, hdferr) + +END PROGRAM main diff --git a/fortran/examples/nested_derived_type.f90 b/fortran/examples/nested_derived_type.f90 new file mode 100644 index 0000000..b79c208 --- /dev/null +++ b/fortran/examples/nested_derived_type.f90 @@ -0,0 +1,212 @@ +! +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! Copyright by the Board of Trustees of the University of Illinois. * +! All rights reserved. * +! * +! This file is part of HDF5. The full HDF5 copyright notice, including * +! terms governing use, modification, and redistribution, is contained in * +! the files COPYING and Copyright.html. COPYING can be found at the root * +! of the source code distribution tree; Copyright.html can be found at the * +! root level of an installed copy of the electronic HDF5 document set and * +! is linked from the top-level documents page. It can also be found at * +! http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have * +! access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. * +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! +! This example shows how to create a nested compound data type, +! write an array which has the compound data type to the file, +! and read back fields' subsets. + +PROGRAM main + USE HDF5 + USE ISO_C_BINDING + IMPLICIT NONE + +! KIND parameters + INTEGER, PARAMETER :: int_k1 = SELECTED_INT_KIND(Fortran_INTEGER_1) ! This should map to INTEGER*1 on most modern processors + INTEGER, PARAMETER :: int_k4 = SELECTED_INT_KIND(Fortran_INTEGER_2) ! This should map to INTEGER*2 on most modern processors + INTEGER, PARAMETER :: int_k8 = SELECTED_INT_KIND(Fortran_INTEGER_4) ! This should map to INTEGER*4 on most modern processors + INTEGER, PARAMETER :: int_k16 = SELECTED_INT_KIND(Fortran_INTEGER_8) ! This should map to INTEGER*8 on most modern processors + + INTEGER, PARAMETER :: r_k4 = SELECTED_REAL_KIND(Fortran_REAL_4) ! This should map to REAL*4 on most modern processors + INTEGER, PARAMETER :: r_k8 = SELECTED_REAL_KIND(Fortran_REAL_8) ! This should map to REAL*8 on most modern processors + +! FILES + + CHARACTER(LEN=*), PARAMETER :: H5FILE_NAME = "SDScompound.h5" + CHARACTER(LEN=*), PARAMETER :: DATASETNAME = "ArrayOfStructures" + + INTEGER, PARAMETER :: LENGTH = 10 + INTEGER, PARAMETER :: RANK = 1 + +!---------------------------------------------------------------- +! Nested derived-type and dataset + + TYPE s4_t + INTEGER(int_k8), DIMENSION(1:4) :: x + ENDTYPE s4_t + + TYPE s1_t + CHARACTER(LEN=1), DIMENSION(1:7) :: chr + INTEGER(KIND=int_k1) :: a + REAL(KIND=r_k4) :: b + TYPE(s4_t) :: d + REAL(KIND=r_k8) :: c + END TYPE s1_t + + TYPE(s1_t), TARGET, DIMENSION(1:length) :: s1 + INTEGER(hid_t) :: s1_tid ! File datatype identifier + +!---------------------------------------------------------------- +! Second derived-type (subset of s1_t) and dataset + TYPE s2_t + CHARACTER(LEN=1), DIMENSION(1:7) :: chr + REAL(KIND=r_k8) :: c + INTEGER(KIND=int_k1) :: a + TYPE(s4_t) :: d + END TYPE s2_t + + TYPE(s2_t), TARGET :: s2(LENGTH) + integer(hid_t) :: s2_tid ! Memory datatype handle + INTEGER(hid_t) :: tid3a +!---------------------------------------------------------------- +! Third "derived-type" (will be used to read float field of s1) + INTEGER(hid_t) :: s3_tid ! Memory datatype handle + REAL(KIND=r_k4), TARGET :: s3(LENGTH) + + INTEGER :: i + INTEGER(hid_t) :: file, dataset, space + !type(H5F_fileid_type) :: file + !type(H5D_dsetid_type) :: dataset + !type(H5S_spaceid_type) :: space + INTEGER(hsize_t) :: DIM(1) = (/LENGTH/) ! Dataspace dimensions + INTEGER(SIZE_T) :: type_size ! Size of the datatype + INTEGER(SIZE_T) :: offset, sizeof_compound + INTEGER :: hdferr + TYPE(C_PTR) :: f_ptr + + INTEGER(SIZE_T) :: type_sizei ! Size of the integer datatype + INTEGER(SIZE_T) :: type_sizer ! Size of the real datatype + INTEGER(SIZE_T) :: type_sized ! Size of the double datatype + INTEGER(hid_t) :: tid3 ! /* Nested Array Datatype ID */ + INTEGER(HSIZE_T), DIMENSION(1) :: tdims1=(/7/) + INTEGER(HSIZE_T), DIMENSION(1) :: tdims1a=(/4/) + ! + ! Initialize FORTRAN interface. + ! + CALL h5open_f(hdferr) + ! + ! Initialize the data + ! + DO i = 0, LENGTH-1 + s1(i+1)%chr(1:7)(1:1) = (/'a','b','c',' ',' ',' ','d'/) + s1(i+1)%a = i + s1(i+1)%b = i*i + s1(i+1)%c = 1./REAL(i+1) + s1(i+1)%d%x(1:4) = (/i,i*10,i*100,i*1000/) + END DO + ! + ! Create the data space. + ! + ! + CALL H5Screate_simple_f(RANK, dim, space, hdferr) + ! + ! Create the file. + ! + CALL H5Fcreate_f(H5FILE_NAME, H5F_ACC_TRUNC_F, file, hdferr) + ! + ! Create the memory data type. + ! + CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(s1(1)), C_LOC(s1(2))), s1_tid, hdferr) + + CALL h5tarray_create_f(H5T_NATIVE_CHARACTER, 1, tdims1, tid3, hdferr) + + CALL H5Tinsert_f(s1_tid, "chr_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%chr)),tid3, hdferr) + CALL H5Tinsert_f(s1_tid, "a_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%a)), h5kind_to_type(int_k1,H5_INTEGER_KIND), hdferr) + CALL H5Tinsert_f(s1_tid, "c_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%c)), h5kind_to_type(r_k8,H5_REAL_KIND), hdferr) + CALL H5Tinsert_f(s1_tid, "b_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%b)), h5kind_to_type(r_k4,H5_REAL_KIND), hdferr) + + ! Create an array of integer datatype + CALL h5tarray_create_f(h5kind_to_type(int_k8,H5_INTEGER_KIND), 1, tdims1a, tid3a, hdferr) + CALL H5Tinsert_f(s1_tid, "d_name", H5OFFSETOF(C_LOC(s1(1)),C_LOC(s1(1)%d%x)), tid3a, hdferr) + + ! + ! Create the dataset. + ! + CALL H5Dcreate_f(file, DATASETNAME, s1_tid, space, dataset, hdferr) + + ! + ! Write data to the dataset + ! + + f_ptr = C_LOC(s1(1)) + CALL H5Dwrite_f(dataset, s1_tid, f_ptr, hdferr) + + ! + ! Release resources + ! + CALL H5Tclose_f(s1_tid, hdferr) + CALL H5Sclose_f(space, hdferr) + CALL H5Dclose_f(dataset, hdferr) + CALL H5Fclose_f(file, hdferr) + + ! + ! Open the file and the dataset. + ! + + CALL H5Fopen_f(H5FILE_NAME, H5F_ACC_RDONLY_F, file, hdferr) + + CALL H5Dopen_f(file, DATASETNAME, dataset,hdferr) + ! + ! Create a data type for s2 + ! + CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(s2(1)), C_LOC(s2(2))), s2_tid, hdferr) + + CALL H5Tinsert_f(s2_tid, "chr_name", H5OFFSETOF(C_LOC(s2(1)),C_LOC(s2(1)%chr)), tid3, hdferr) + CALL H5Tinsert_f(s2_tid, "c_name", H5OFFSETOF(C_LOC(s2(1)),C_LOC(s2(1)%c)), h5kind_to_type(r_k8,H5_REAL_KIND), hdferr) + CALL H5Tinsert_f(s2_tid, "a_name", H5OFFSETOF(C_LOC(s2(1)),C_LOC(s2(1)%a)), h5kind_to_type(int_k1,H5_INTEGER_KIND), hdferr) + CALL H5Tinsert_f(s2_tid, "d_name", H5OFFSETOF(C_LOC(s2(1)),C_LOC(s2(1)%d%x)), tid3a, hdferr) + + ! + ! Read two fields c and a from s1 dataset. Fields in the file + ! are found by their names "c_name" and "a_name". + + f_ptr = C_LOC(s2(1)) + CALL H5Dread_f(dataset, s2_tid, f_ptr, hdferr) + + ! + ! Display the fields + ! + DO i = 1, length + WRITE(*,'(/,A,/,999(A,1X))') "Field chr :", s2(i)%chr(1:7)(1:1) + ENDDO + WRITE(*,'(/,A,/,999(F8.4,1X))') "Field c :", s2(:)%c + WRITE(*,'(/,A,/,999(I0,1X))') "Field a :", s2(:)%a + DO i = 1, length + WRITE(*,'(/,A,/,999(I0,1X))') "Field d%x :", s2(i)%d%x(:) + ENDDO + ! + ! Create a data type for s3. + ! + CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(s3(1)),C_LOC(s3(2))),s3_tid, hdferr) + + CALL H5Tinsert_f(s3_tid, "b_name", 0, h5kind_to_type(r_k4,H5_REAL_KIND), hdferr) + ! + ! Read field b from s1 dataset. Field in the file is found by its name. + ! + s3(:)=-1 + f_ptr = C_LOC(s3(1)) + CALL H5Dread_f(dataset, s3_tid, f_ptr, hdferr) + ! + ! Display the field + ! + WRITE(*,'(/,A,/,999(F8.4,1X))') "Field b :",s3(:) + ! + ! Release resources + ! + CALL H5Tclose_f(s2_tid, hdferr) + CALL H5Tclose_f(s3_tid, hdferr) + CALL H5Dclose_f(dataset, hdferr) + CALL H5Fclose_f(file, hdferr) + +END PROGRAM main diff --git a/fortran/examples/rwdset_fortran2003.f90 b/fortran/examples/rwdset_fortran2003.f90 new file mode 100644 index 0000000..74bda85 --- /dev/null +++ b/fortran/examples/rwdset_fortran2003.f90 @@ -0,0 +1,181 @@ +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! Copyright by The HDF Group. * +! Copyright by the Board of Trustees of the University of Illinois. * +! All rights reserved. * +! * +! This file is part of HDF5. The full HDF5 copyright notice, including * +! terms governing use, modification, and redistribution, is contained in * +! the files COPYING and Copyright.html. COPYING can be found at the root * +! of the source code distribution tree; Copyright.html can be found at the * +! root level of an installed copy of the electronic HDF5 document set and * +! is linked from the top-level documents page. It can also be found at * +! http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * +! access to either file, you may request a copy from help@hdfgroup.org. * +! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +! +! +! 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. Uses updated Fortran 2003 interface +! with different KINDs of integers and reals. +! +PROGRAM RWDSET_FORTRAN2003 + + USE ISO_C_BINDING + + USE HDF5 ! This module contains all necessary modules + + IMPLICIT NONE + + INTEGER, PARAMETER :: int_kind_1 = SELECTED_INT_KIND(Fortran_INTEGER_1) !should map to INTEGER*1 on most modern processors + INTEGER, PARAMETER :: int_kind_4 = SELECTED_INT_KIND(Fortran_INTEGER_2) !should map to INTEGER*2 on most modern processors + INTEGER, PARAMETER :: int_kind_8 = SELECTED_INT_KIND(Fortran_INTEGER_4) !should map to INTEGER*4 on most modern processors + INTEGER, PARAMETER :: int_kind_16 = SELECTED_INT_KIND(Fortran_INTEGER_8) !should map to INTEGER*8 on most modern processors + + INTEGER, PARAMETER :: real_kind_7 = SELECTED_REAL_KIND(Fortran_REAL_4) !should map to REAL*4 on most modern processors + INTEGER, PARAMETER :: real_kind_15 = SELECTED_REAL_KIND(Fortran_REAL_8) !should map to REAL*8 on most modern processors + + CHARACTER(LEN=8), PARAMETER :: filename = "dsetf.h5" ! File name + CHARACTER(LEN=5), PARAMETER :: dsetname1 = "dset1" ! Dataset name + CHARACTER(LEN=5), PARAMETER :: dsetname2 = "dset2" ! Dataset name + CHARACTER(LEN=5), PARAMETER :: dsetname4 = "dset4" ! Dataset name + CHARACTER(LEN=5), PARAMETER :: dsetname8 = "dset8" ! Dataset name + CHARACTER(LEN=6), PARAMETER :: dsetnamer4 = "dsetr4" ! Dataset name + CHARACTER(LEN=6), PARAMETER :: dsetnamer8 = "dsetr8" ! Dataset name + + INTEGER(HID_T) :: file_id ! File identifier + INTEGER(HID_T) :: dset_id1 ! Dataset identifier + INTEGER(HID_T) :: dset_id4 ! Dataset identifier + INTEGER(HID_T) :: dset_id8 ! Dataset identifier + INTEGER(HID_T) :: dset_id16 ! Dataset identifier + INTEGER(HID_T) :: dset_idr4 ! Dataset identifier + INTEGER(HID_T) :: dset_idr8 ! Dataset identifier + + INTEGER :: error ! Error flag + INTEGER :: i, j + +! Data buffers: + + INTEGER, DIMENSION(1:4) :: dset_data + + INTEGER(int_kind_1), DIMENSION(1:4), TARGET :: dset_data_i1, data_out_i1 + INTEGER(int_kind_4), DIMENSION(1:4), TARGET :: dset_data_i4, data_out_i4 + INTEGER(int_kind_8), DIMENSION(1:4), TARGET :: dset_data_i8, data_out_i8 + INTEGER(int_kind_16), DIMENSION(1:4), TARGET :: dset_data_i16, data_out_i16 + + INTEGER(int_kind_8), DIMENSION(1:4), TARGET :: data_out_i8a + + REAL(real_kind_7), DIMENSION(1:4), TARGET :: dset_data_r7, data_out_r7 + REAL(real_kind_15), DIMENSION(1:4), TARGET :: dset_data_r15, data_out_r15 + + INTEGER(HSIZE_T), DIMENSION(1:1) :: data_dims = (/4/) + INTEGER(HID_T) :: dspace_id ! Dataspace identifier + + TYPE(C_PTR) :: f_ptr + INTEGER(hid_t) :: datatype !/* Common datatype ID */ + + ! + ! Initialize FORTRAN interface. + ! + CALL h5open_f(error) + ! + ! Initialize the dset_data array. + ! + DO i = 1, 4 + dset_data_i1(i) = i + dset_data_i4(i) = i + dset_data_i8(i) = i + dset_data_i16(i) = i + + dset_data_r7(i) = (i)*100. + dset_data_r15(i) = (i)*1000. + + END DO + + CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error) + ! + ! Create dataspaces for datasets + ! + CALL h5screate_simple_f(1, data_dims , dspace_id, error) + ! + ! Create the dataset. + ! + CALL H5Dcreate_f(file_id, dsetname1, h5kind_to_type(int_kind_1,H5_INTEGER_KIND), dspace_id, dset_id1, error) + CALL H5Dcreate_f(file_id, dsetname2, h5kind_to_type(int_kind_4,H5_INTEGER_KIND), dspace_id, dset_id4, error) + CALL H5Dcreate_f(file_id, dsetname4, h5kind_to_type(int_kind_8,H5_INTEGER_KIND), dspace_id, dset_id8, error) + CALL H5Dcreate_f(file_id, dsetname8, h5kind_to_type(int_kind_16,H5_INTEGER_KIND), dspace_id, dset_id16, error) + + CALL H5Dcreate_f(file_id, dsetnamer4, h5kind_to_type(real_kind_7,H5_REAL_KIND), dspace_id, dset_idr4, error) + CALL H5Dcreate_f(file_id, dsetnamer8, h5kind_to_type(real_kind_15,H5_REAL_KIND), dspace_id, dset_idr8, error) + + ! + ! Write the dataset. + ! + f_ptr = C_LOC(dset_data_i1(1)) + CALL h5dwrite_f(dset_id1, h5kind_to_type(int_kind_1,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(dset_data_i4(1)) + CALL h5dwrite_f(dset_id4, h5kind_to_type(int_kind_4,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(dset_data_i8(1)) + CALL h5dwrite_f(dset_id8, h5kind_to_type(int_kind_8,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(dset_data_i16(1)) + CALL h5dwrite_f(dset_id16, h5kind_to_type(int_kind_16,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(dset_data_r7(1)) + CALL h5dwrite_f(dset_idr4, h5kind_to_type(real_kind_7,H5_REAL_KIND), f_ptr, error) + f_ptr = C_LOC(dset_data_r15(1)) + CALL h5dwrite_f(dset_idr8, h5kind_to_type(real_kind_15,H5_REAL_KIND), f_ptr, error) + ! + ! Close the file + ! + CALL h5fclose_f(file_id, error) + + ! Open the file + + CALL h5fopen_f(filename, H5F_ACC_RDWR_F, file_id, error) + ! + ! Read the dataset. + ! + ! Read data back into an integer size that is larger then the original size used for + ! writing the data + f_ptr = C_LOC(data_out_i8a(1)) + CALL h5dread_f(dset_id1, h5kind_to_type(int_kind_8,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(data_out_i4(1)) + CALL h5dread_f(dset_id4, h5kind_to_type(int_kind_4,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(data_out_i8(1)) + CALL h5dread_f(dset_id8, h5kind_to_type(int_kind_8,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(data_out_i16(1)) + CALL h5dread_f(dset_id16, h5kind_to_type(int_kind_16,H5_INTEGER_KIND), f_ptr, error) + f_ptr = C_LOC(data_out_r7(1)) + CALL h5dread_f(dset_idr4, h5kind_to_type(real_kind_7,H5_REAL_KIND), f_ptr, error) + f_ptr = C_LOC(data_out_r15(1)) + CALL h5dread_f(dset_idr8, h5kind_to_type(real_kind_15,H5_REAL_KIND), f_ptr, error) + +! memory type + WRITE(*,'(A,4i8)' )'SELECTED_INT_KIND(Fortran_INTEGER_1): ',data_out_i8a + WRITE(*,'(A,4i8)' )'SELECTED_INT_KIND(Fortran_INTEGER_4): ',data_out_i4 + WRITE(*,'(A,4i8)' )'SELECTED_INT_KIND(Fortran_INTEGER_8): ',data_out_i8 + WRITE(*,'(A,4i8)' )'SELECTED_INT_KIND(Fortran_INTEGER_16): ',data_out_i16 + WRITE(*,'(A,4(1x,f9.4))' )'SELECTED_REAL_KIND(Fortran_REAL_7): ',data_out_r7 + WRITE(*,'(A,4(1x,f16.10))' )'SELECTED_REAL_KIND(Fortran_REAL_15): ',data_out_r15 + ! + ! Close the dataset. + ! + CALL h5dclose_f(dset_id1, error) + CALL h5dclose_f(dset_id4, error) + CALL h5dclose_f(dset_id8, error) + CALL h5dclose_f(dset_id16, error) + CALL h5dclose_f(dset_idr4, error) + CALL h5dclose_f(dset_idr8, error) + ! + ! Close the file. + ! + CALL h5fclose_f(file_id, error) + ! + ! Close FORTRAN interface. + ! + CALL h5close_f(error) + +END PROGRAM RWDSET_FORTRAN2003 + + + |