diff options
author | Scot Breitenfeld <brtnfld@hdfgroup.org> | 2011-09-27 05:02:38 (GMT) |
---|---|---|
committer | Scot Breitenfeld <brtnfld@hdfgroup.org> | 2011-09-27 05:02:38 (GMT) |
commit | 4af3cd2b7a89b2eeed05d5ec0b0641ca7c2545bc (patch) | |
tree | 01f90619962c447280074bb8d10ae5c7b2b9acbc /fortran/examples/compound_complex_fortran2003.f90 | |
parent | a07004c825e3a4e4b61269fd3e5f2b57092f073c (diff) | |
download | hdf5-4af3cd2b7a89b2eeed05d5ec0b0641ca7c2545bc.zip hdf5-4af3cd2b7a89b2eeed05d5ec0b0641ca7c2545bc.tar.gz hdf5-4af3cd2b7a89b2eeed05d5ec0b0641ca7c2545bc.tar.bz2 |
[svn-r21421] Merged the Fortran 2003 changes from the trunk into the 1.8 branch, used:
svn merge -r 20506:21414 https://svn.hdfgroup.uiuc.edu/hdf5/trunk/fortran
Tested: jam (gnu, intel, pgi compilers)
Also merged effected non-Fortran files:
svn merge -r21247:r21248 https://svn.hdfgroup.uiuc.edu/hdf5/trunk/src/libhdf5.settings.in
Diffstat (limited to 'fortran/examples/compound_complex_fortran2003.f90')
-rw-r--r-- | fortran/examples/compound_complex_fortran2003.f90 | 117 |
1 files changed, 117 insertions, 0 deletions
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 |