1! This is the F2003 version of the h5_compound.c example source code. 2! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 3! Copyright by The HDF Group. * 4! Copyright by the Board of Trustees of the University of Illinois. * 5! All rights reserved. * 6! * 7! This file is part of HDF5. The full HDF5 copyright notice, including * 8! terms governing use, modification, and redistribution, is contained in * 9! the COPYING file, which can be found at the root of the source code * 10! distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. * 11! If you do not have access to either file, you may request a copy from * 12! help@hdfgroup.org. * 13! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 14! 15! This example shows how to create an array of a compound datatype which 16! contains an array of type complex and how to write it to hdf5 17! and how to read it back into a compound datatype for hdf5. 18! 19 20PROGRAM compound_complex_fortran2003 21 22 USE hdf5 23 USE ISO_C_BINDING 24 IMPLICIT NONE 25 26 INTEGER, PARAMETER :: r_k8 = KIND(0.0d0) 27 INTEGER, PARAMETER :: NMAX = 3 28 29 TYPE sample 30 COMPLEX(KIND=r_k8), DIMENSION(1:NMAX) :: nlev 31 REAL(KIND=r_k8) :: N 32 END TYPE sample 33 34 INTEGER(HID_T) :: sample_type_id, dset_id, dspace_id, file_id 35 INTEGER(HSIZE_T) :: dims(1) = (/NMAX/) 36 INTEGER :: error 37 38 TYPE(sample), DIMENSION(1:NMAX), TARGET :: samples, read_samples 39 INTEGER :: i 40 41 TYPE(C_PTR) :: f_ptr 42 INTEGER(HSIZE_T), DIMENSION(1) :: array_dims=(/2*NMAX/) ! complex is really (real,real) so double size of array 43 INTEGER(hid_t) :: array_type_id ! Nested Array Datatype ID 44 45 ! Initialize data 46 DO i=1,NMAX 47 samples(i)%nlev(1:NMAX) = (3.14159_r_k8, 2.71828_r_k8) 48 samples(i)%N = i 49 END DO 50 51 ! Initialize FORTRAN interface. 52 CALL h5open_f(error) 53 54 ! Create a new file using default properties. 55 CALL h5fcreate_f("test.h5", H5F_ACC_TRUNC_F, file_id, error) 56 ! 57 ! Create the memory data type. 58 ! 59 CALL H5Tcreate_f(H5T_COMPOUND_F, H5OFFSETOF(C_LOC(samples(1)), C_LOC(samples(2))), sample_type_id, error) 60 61 ! Create the array type 62 CALL h5Tarray_create_f(H5T_NATIVE_DOUBLE, 1, array_dims, array_type_id, error) 63 ! Then use that array type to insert values into 64 CALL H5Tinsert_f( sample_type_id, "nlev", & 65 H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%nlev(1))), array_type_id, error) 66 CALL H5Tinsert_f( sample_type_id, "N", & 67 H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%N)), h5kind_to_type(r_k8,H5_REAL_KIND), error) 68 ! 69 ! Create dataspace 70 ! 71 CALL h5screate_simple_f(1, dims, dspace_id, error) 72 ! 73 ! Create the dataset. 74 ! 75 CALL H5Dcreate_f(file_id, "samples", sample_type_id, dspace_id, dset_id, error) 76 ! 77 ! Write data to the dataset 78 ! 79 f_ptr = C_LOC(samples(1)) 80 CALL H5Dwrite_f(dset_id, sample_type_id, f_ptr, error) 81 ! Close up 82 CALL h5dclose_f(dset_id, error) 83 CALL h5sclose_f(dspace_id, error) 84 CALL h5fclose_f(file_id, error) 85 ! 86 ! Open the file and the dataset. 87 ! 88 CALL H5Fopen_f("test.h5", H5F_ACC_RDONLY_F, file_id, error) 89 90 CALL H5Dopen_f(file_id, "samples", dset_id, error) 91 ! 92 ! Create the memory data type. 93 ! 94 CALL H5Tcreate_f(H5T_COMPOUND_F,H5OFFSETOF(C_LOC(samples(1)), C_LOC(samples(2))), sample_type_id,error) 95 96 CALL H5Tinsert_f( sample_type_id, "nlev", & 97 H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%nlev(1))), array_type_id, error) 98 CALL H5Tinsert_f( sample_type_id, "N", & 99 H5OFFSETOF(C_LOC(samples(1)),C_LOC(samples(1)%N)), h5kind_to_type(r_k8,H5_REAL_KIND), error) 100 101 f_ptr = C_LOC(read_samples(1)) 102 CALL H5Dread_f(dset_id, sample_type_id, f_ptr, error) 103 104 ! 105 ! Display the fields 106 ! 107 DO i=1,NMAX 108 WRITE(*,'(A,3(" (",F8.5,",",F8.5,")"))') "SAMPLES =",read_samples(i)%nlev(1:NMAX) 109 WRITE(*,'(A,F8.5)') "N =", read_samples(i)%N 110 END DO 111 112 CALL H5Tclose_f(sample_type_id, error) 113 CALL H5Dclose_f(dset_id, error) 114 CALL H5Fclose_f(file_id, error) 115 116END PROGRAM compound_complex_fortran2003 117