summaryrefslogtreecommitdiffstats
path: root/fortran/test/tH5R.f90
blob: 56b7a21cc58c929390a9908f67b74209e83fbd48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
!
! 
!    Testing Reference Interface functionality.
!
!      MODULE H5RTEST

!        USE HDF5 ! This module contains all necessary modules 
        
!      CONTAINS
!      
!The following subroutine tests h5rcreate_f, h5rdereference_f
!and H5Rget_object_type functions
!
        SUBROUTINE refobjtest(total_error)
        USE HDF5 ! This module contains all necessary modules 
          IMPLICIT NONE
          INTEGER, INTENT(OUT) :: total_error 

          CHARACTER(LEN=12), PARAMETER :: filename = "reference.h5"
          CHARACTER(LEN=8), PARAMETER :: dsetnamei = "INTEGERS"
          CHARACTER(LEN=17), PARAMETER :: dsetnamer = "OBJECT_REFERENCES"
          CHARACTER(LEN=6), PARAMETER :: groupname1 = "GROUP1"
          CHARACTER(LEN=6), PARAMETER :: groupname2 = "GROUP2"

          INTEGER(HID_T) :: file_id       ! File identifier 
          INTEGER(HID_T) :: grp1_id       ! Group identifier 
          INTEGER(HID_T) :: grp2_id       ! Group identifier 
          INTEGER(HID_T) :: dset1_id      ! Dataset identifier 
          INTEGER(HID_T) :: dsetr_id      ! Dataset identifier 
          INTEGER(HID_T) :: type_id       ! Type identifier  
          INTEGER(HID_T) :: space_id      ! Dataspace identifier 
          INTEGER(HID_T) :: spacer_id     ! Dataspace identifier 
          INTEGER     ::   error, obj_type
          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/)

          !
          !  Initialize FORTRAN predefined datatypes
          !
!          CALL h5init_types_f(error)
!              CALL check("h5init_types_f",error,total_error)

          !
          !Create a new file with Default file access and
          !file creation properties . 
          !
          CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)
              CALL check("h5fcreate_f",error,total_error)


          !
          ! Create a group inside the file
          !
          CALL h5gcreate_f(file_id, groupname1, grp1_id, error)
              CALL check("h5gcreate_f",error,total_error)

          !
          ! Create a group inside the group GROUP1
          !
          CALL h5gcreate_f(grp1_id, groupname2, grp2_id, error)
              CALL check("h5gcreate_f",error,total_error)

          ! 
          ! Create dataspaces for datasets   
          !
          CALL h5screate_simple_f(rank, dims, space_id, error, maxdims=my_maxdims)
              CALL check("h5screate_simple_f",error,total_error)
          CALL h5screate_simple_f(rankr, dimsr, spacer_id, error)
              CALL check("h5screate_simple_f",error,total_error)

          !
          ! Create integer dataset
          !
          CALL h5dcreate_f(file_id, dsetnamei, H5T_NATIVE_INTEGER, space_id, &
                      dset1_id, error)
              CALL check("h5dcreate_f",error,total_error)
          !
          ! Create dataset to store references to the objects
          !
          CALL h5dcreate_f(file_id, dsetnamer, H5T_STD_REF_OBJ, spacer_id, &
                      dsetr_id, error)
              CALL check("h5dcreate_f",error,total_error)
          !
          ! Create a datatype and store in the file
          !
          CALL h5tcopy_f(H5T_NATIVE_REAL, type_id, error)
              CALL check("h5tcopy_f",error,total_error)
          CALL h5tcommit_f(file_id, "MyType", type_id, error)
              CALL check("h5tcommit_f",error,total_error)

          !
          !  Close dataspaces, groups and integer dataset
          ! 
          CALL h5sclose_f(space_id, error)
              CALL check("h5sclose_f",error,total_error)
          CALL h5sclose_f(spacer_id, error)         
              CALL check("h5sclose_f",error,total_error)
          CALL h5dclose_f(dset1_id, error)
              CALL check("h5dclose_f",error,total_error)
          CALL h5tclose_f(type_id, error)
              CALL check("h5tclose_f",error,total_error)
          CALL h5gclose_f(grp1_id, error)
              CALL check("h5gclose_f",error,total_error)
          CALL h5gclose_f(grp2_id, error)
              CALL check("h5gclose_f",error,total_error)


          !
          ! Craete 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 check("h5rcreate_f",error,total_error)
          CALL h5rcreate_f(file_id, "/GROUP1/GROUP2", ref(2), error)
              CALL check("h5rcreate_f",error,total_error)
          CALL h5rcreate_f(file_id, dsetnamei, ref(3), error)
              CALL check("h5rcreate_f",error,total_error)
          CALL h5rcreate_f(file_id, "MyType", ref(4), error)
              CALL check("h5rcreate_f",error,total_error)

          CALL h5dwrite_f(dsetr_id, H5T_STD_REF_OBJ, ref, error)
              CALL check("h5dwrite_f",error,total_error)

          !
          !Close the dataset
          ! 
          CALL h5dclose_f(dsetr_id, error)
              CALL check("h5dclose_f",error,total_error)

          ! 
          ! Reopen the dataset with object references
          !
          CALL h5dopen_f(file_id, dsetnamer,dsetr_id,error)
              CALL check("h5dopen_f",error,total_error)
          CALL h5dread_f(dsetr_id, H5T_STD_REF_OBJ, ref_out, error)
              CALL check("h5dread_f",error,total_error)
   
          !
          !get the third reference's type and Dereference it
          !
          CALL h5rget_object_type_obj_f(dsetr_id, ref(3), obj_type, error) 
              CALL check("h5rget_object_type_obj_f",error,total_error)
          if (obj_type == 2) then 
              CALL h5rdereference_f(dsetr_id, ref(3), dset1_id, error)
                  CALL check("h5rdereference_f",error,total_error)
          
              CALL h5dwrite_f(dset1_id, H5T_NATIVE_INTEGER, data, error)
                  CALL check("h5dwrite_f",error,total_error)
          end if

          !
          !get the fourth reference's type and Dereference it
          !
          CALL h5rget_object_type_obj_f(dsetr_id, ref(4), obj_type, error) 
              CALL check("h5rget_object_type_obj_f",error,total_error)
          if (obj_type == 3) then 
              CALL h5rdereference_f(dsetr_id, ref(4), type_id, error)
                  CALL check("h5rdereference_f",error,total_error)
          end if

          !
          ! Close all objects.
          !          
          CALL h5dclose_f(dset1_id, error)
              CALL check("h5dclose_f",error,total_error)
          CALL h5tclose_f(type_id, error)
              CALL check("h5tclose_f",error,total_error)

          CALL h5dclose_f(dsetr_id, error)
              CALL check("h5dclose_f",error,total_error)
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f",error,total_error)
          !
          ! Close FORTRAN predefined datatypes.
          !
!          CALL h5close_types_f(error) 
!              CALL check("h5close_types_f",error,total_error)
          RETURN

        END SUBROUTINE refobjtest
!
!The following subroutine tests h5rget_region_f, h5rcreate_f
!and h5rdereference_f functionalities
! 
        SUBROUTINE refregtest(total_error)
        USE HDF5 ! This module contains all necessary modules 
          IMPLICIT NONE
          INTEGER, INTENT(OUT) :: total_error 

          CHARACTER(LEN=9), PARAMETER :: filename = "Refreg.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)
!              CALL check("h5init_types_f", error, total_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. 
              CALL check("h5fcreate_f", error, total_error)
          ! 
          ! Create  dataspaces:
          ! 
          ! for dataset with references to dataset regions 
          !
          CALL h5screate_simple_f(rankr, dimsr, spacer_id, error)
              CALL check("h5screate_simple_f", error, total_error)
          !
          ! for integer dataset 
          !
          CALL h5screate_simple_f(rank, dims, space_id, error)
              CALL check("h5screate_simple_f", error, total_error)
          !
          ! Create  and write datasets:
          !
          ! Integer dataset 
          !
          CALL h5dcreate_f(file_id, dsetnamev, H5T_NATIVE_INTEGER, space_id, &
               dsetv_id, error)
              CALL check("h5dcreate_f", error, total_error)
          CALL h5dwrite_f(dsetv_id, H5T_NATIVE_INTEGER, data, error)
              CALL check("h5dwrite_f", error, total_error)

          CALL h5dclose_f(dsetv_id, error)
              CALL check("h5dclose_f", error, total_error)
          !
          ! Dataset with references
          !
          CALL h5dcreate_f(file_id, dsetnamer, H5T_STD_REF_DSETREG, spacer_id, &
               dsetr_id, error)
              CALL check("h5dcreate_f", error, total_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 check("h5sselect_hyperslab_f", error, total_error)
          CALL h5rcreate_f(file_id, dsetnamev, space_id, ref(1), error) 
              CALL check("h5rcreate_f", error, total_error)
          !
          ! Create a reference to elements selection.
          !
          CALL h5sselect_none_f(space_id, error)
              CALL check("h5sselect_none_f", error, total_error)
          CALL h5sselect_elements_f(space_id, H5S_SELECT_SET_F, rank, num_points,&
               coord, error) 
              CALL check("h5sselect_elements_f", error, total_error)
          CALL h5rcreate_f(file_id, dsetnamev, space_id, ref(2), error) 
              CALL check("h5rcreate_f", error, total_error)
          !
          ! Write dataset with the references. 
          !
          CALL h5dwrite_f(dsetr_id, H5T_STD_REF_DSETREG, ref, error) 
              CALL check("h5dwrite_f", error, total_error)
          !
          ! Close all objects.
          !
          CALL h5sclose_f(space_id, error)
              CALL check("h5sclose_f", error, total_error)
          CALL h5sclose_f(spacer_id, error)
              CALL check("h5sclose_f", error, total_error)
          CALL h5dclose_f(dsetr_id, error)
              CALL check("h5dclose_f", error, total_error)
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)
          !
          ! Reopen the file to test selections.
          !
          CALL h5fopen_f (filename, H5F_ACC_RDWR_F, file_id, error)
              CALL check("h5fopen_f", error, total_error)
          CALL h5dopen_f(file_id, dsetnamer, dsetr_id, error)
              CALL check("h5dopen_f", error, total_error)
          !
          ! Read references to the dataset regions.
          !
          CALL h5dread_f(dsetr_id, H5T_STD_REF_DSETREG, ref_out, error) 
              CALL check("h5dread_f", error, total_error)
          ! 
          ! Dereference the first reference.
          ! 
          CALL H5rdereference_f(dsetr_id, ref_out(1), dsetv_id, error)
              CALL check("h5rdereference_f", error, total_error)
          CALL H5rget_region_f(dsetr_id, ref_out(1), space_id, error) 
              CALL check("h5rget_region_f", error, total_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)
              CALL check("h5dread_f", error, total_error)
          CALL h5sclose_f(space_id, error)
              CALL check("h5sclose_f", error, total_error)
          CALL h5dclose_f(dsetv_id, error)
              CALL check("h5dclose_f", error, total_error)
          data_out = 0
          !  
          ! Dereference the second reference.
          ! 
          CALL H5rdereference_f(dsetr_id, ref_out(2), dsetv_id, error)
              CALL check("h5rdereference_f", error, total_error)
          CALL H5rget_region_f(dsetr_id, ref_out(2), space_id, error) 
              CALL check("h5rget_region_f", error, total_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)
              CALL check("h5dread_f", error, total_error)
          !
          ! Close all objects
          !
          CALL h5sclose_f(space_id, error)
              CALL check("h5sclose_f", error, total_error)
          CALL h5dclose_f(dsetv_id, error)
              CALL check("h5dclose_f", error, total_error)
          CALL h5dclose_f(dsetr_id, error)
              CALL check("h5dclose_f", error, total_error)
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)
          !
          ! Close FORTRAN predefined datatypes.
          !
!          CALL h5close_types_f(error) 
!              CALL check("h5close_types_f",error,total_error)
          RETURN

        END SUBROUTINE refregtest


!      END MODULE H5RTEST