|
3 | 3 | use, intrinsic :: iso_c_binding, only : C_CHAR, C_NULL_CHAR, C_F_POINTER |
4 | 4 |
|
5 | 5 | use hdf5, only : H5Aread_f, H5Aget_type_f, & |
6 | | -H5Tis_variable_str_f, H5Tget_class_f, H5Tget_native_type_f, H5Tget_size_f, H5Tget_strpad_f, & |
7 | | -H5T_DIR_ASCEND_F |
8 | | -use h5lt, only: h5ltget_attribute_float_f, h5ltget_attribute_double_f, h5ltget_attribute_int_f |
| 6 | +H5Tget_class_f, H5Tget_native_type_f, H5Tget_size_f, H5Tget_strpad_f, & |
| 7 | +H5T_DIR_ASCEND_F, & |
| 8 | +H5S_ALL_F |
9 | 9 |
|
10 | 10 | implicit none (type, external) |
11 | 11 |
|
12 | | -interface |
13 | | -module subroutine readattr_char_scalar(self, obj_name, attr_name, attr_id, space_id, A) |
14 | | -class(hdf5_file), intent(in) :: self |
15 | | -character(*), intent(in) :: obj_name, attr_name |
16 | | -integer(HID_T), intent(in) :: attr_id, space_id |
17 | | -character(*), intent(inout) :: A |
18 | | -end subroutine |
19 | | - |
20 | | -end interface |
21 | | - |
22 | 12 | contains |
23 | 13 |
|
24 | 14 | module procedure readattr_scalar |
25 | | - |
26 | | -integer(HSIZE_T) :: attr_dims(0) |
27 | | -integer(HID_T) :: attr_id, space_id |
28 | | -integer :: attr_class, ier |
29 | | -logical :: is_scalar, attr_exists |
30 | | - |
31 | | -if(.not.self%is_open()) error stop 'ERROR:h5fortran:attr_read: file handle is not open' |
32 | | - |
33 | | -call H5Aexists_by_name_f(self%file_id, obj_name, attr_name, attr_exists, ier) |
34 | | -if(ier /= 0) error stop "ERROR:h5fortran:attr_read:H5Aexists_by_name " // obj_name // ":" // attr_name // " : " // self%filename |
35 | | -if(.not.attr_exists) then |
36 | | - error stop 'ERROR:h5fortran:attr_read: attribute not exist: ' // obj_name // ":" // attr_name // " : " // self%filename |
37 | | -endif |
38 | | - |
39 | | -call H5Aopen_by_name_f(self%file_id, obj_name, attr_name, attr_id, ier) |
40 | | -if(ier/=0) error stop 'ERROR:h5fortran:readattr:H5Aopen ' // obj_name // ":" // attr_name // " : " // self%filename |
41 | | - |
42 | | -call H5Aget_space_f(attr_id, space_id, ier) |
43 | | -if(ier/=0) error stop 'ERROR:h5fortran:readattr:H5Aget_space ' // obj_name // ":" // attr_name |
44 | | - |
45 | | -call hdf_rank_check(self, obj_name // ":" // attr_name, space_id, rank(A), is_scalar) |
46 | | - |
47 | | -call get_obj_class(self, obj_name // ":" // attr_name, attr_id, attr_class) |
48 | | - |
49 | | -!> cast the dataset read from disk to the variable type presented by user h5f%readattr("/my_dataset", x, "y") |
50 | | -!> We only cast when needed to save memory. |
51 | | -!! select case doesn't allow H5T_* |
52 | | -if(attr_class == H5T_FLOAT_F) then |
53 | | - select type(A) |
54 | | - type is (real(real64)) |
55 | | - call H5Aread_f(attr_id, H5T_NATIVE_DOUBLE, A, attr_dims, ier) |
56 | | - type is (real(real32)) |
57 | | - call H5Aread_f(attr_id, H5T_NATIVE_REAL, A, attr_dims, ier) |
58 | | - class default |
59 | | - error stop 'ERROR:h5fortran:readattr: real disk dataset ' // obj_name // ':' // attr_name // ' needs real memory variable' |
60 | | - end select |
61 | | -elseif(attr_class == H5T_INTEGER_F) then |
62 | | - select type(A) |
63 | | - type is (integer(int32)) |
64 | | - call H5Aread_f(attr_id, H5T_NATIVE_INTEGER, A, attr_dims, ier) |
65 | | - type is (integer(int64)) |
66 | | - call H5Aread_f(attr_id, H5T_STD_I64LE, A, attr_dims, ier) |
67 | | - class default |
68 | | - error stop 'ERROR:h5fortran:readattr: integer disk dataset ' // obj_name // ':' // attr_name // ' needs integer memory variable' |
69 | | - end select |
70 | | -elseif(attr_class == H5T_STRING_F) then |
71 | | - select type(A) |
72 | | - type is (character(*)) !< kind=c_char too |
73 | | - call readattr_char_scalar(self, obj_name, attr_name, attr_id, space_id, A) |
74 | | - class default |
75 | | - error stop 'ERROR:h5fortran:readattr: string dataset ' // obj_name // ':' // attr_name // ' needs character memory variable' |
76 | | - end select |
77 | | -else |
78 | | - error stop 'ERROR:h5fortran:attr_read: non-handled datatype--please reach out to developers.' |
79 | | -end if |
80 | | -if(ier/=0) error stop 'ERROR:h5fortran:readattr: reading ' // obj_name // ':' // attr_name // ' from ' // self%filename |
81 | | - |
82 | | -call H5Aclose_f(attr_id, ier) |
83 | | -if(ier /= 0) error stop "ERROR:h5fortran:readattr_scalar:H5Aclose: " // obj_name // ':' // attr_name // " in " // self%filename |
84 | | - |
85 | | -call H5Sclose_f(space_id, ier) |
86 | | -if(ier /= 0) error stop "ERROR:h5fortran:readattr_scalar:H5Sclose: " // obj_name // ':' // attr_name // " in " // self%filename |
87 | | - |
88 | | -end procedure readattr_scalar |
89 | | - |
90 | | - |
91 | | - |
| 15 | +include 'attr_read.inc' |
| 16 | +end procedure |
92 | 17 |
|
93 | 18 | module procedure readattr_1d |
94 | 19 | include 'attr_read.inc' |
|
0 commit comments