summaryrefslogtreecommitdiffstats
path: root/fortran/test/tH5VL.f90
blob: d34b42cf708303c9a987777feb2a0faaec6d1d8c (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
!****h* root/fortran/test/tH5VL.f90
!
! NAME
!  tH5VL.f90
!
! FUNCTION
!  Basic testing of Fortran Variable_length datatypes APIs.
!
! COPYRIGHT
! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
!   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.     *
! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
!
! CONTAINS SUBROUTINES
!  vl_test_integer, vl_test_real, vl_test_string 
!
!*****

MODULE TH5VL

CONTAINS

        SUBROUTINE vl_test_integer(cleanup, total_error)
        USE HDF5 ! This module contains all necessary modules
        USE TH5_MISC

          IMPLICIT NONE
          LOGICAL, INTENT(IN) :: cleanup
          INTEGER, INTENT(OUT) :: total_error

          CHARACTER(LEN=7), PARAMETER :: filename = "VLtypes" ! File name
          CHARACTER(LEN=80) :: fix_filename
          CHARACTER(LEN=5), PARAMETER :: dsetname = "VLint"     ! Dataset name

          INTEGER(HID_T) :: file_id       ! File identifier
          INTEGER(HID_T) :: dset_id       ! Dataset identifier
          INTEGER(HID_T) :: dspace_id     ! Dataspace identifier
          INTEGER(HID_T) :: vltype_id     ! Datatype identifier


          INTEGER(HSIZE_T), DIMENSION(1) :: dims = (/6/) ! Dataset dimensions
          INTEGER(SIZE_T), DIMENSION(6) :: len           ! Elements lengths
          INTEGER(SIZE_T), DIMENSION(6) :: len_out
          INTEGER     ::   rank = 1                      ! Dataset rank

          INTEGER, DIMENSION(5,6) :: vl_int_data ! Data buffers
          INTEGER, DIMENSION(5,6) :: vl_int_data_out ! Data buffers
          INTEGER     ::   error ! Error flag

          INTEGER     :: i, j    !general purpose integers
          INTEGER(HSIZE_T) :: ih, jh    !general purpose integers
          INTEGER(HSIZE_T), DIMENSION(2) :: data_dims = (/5,6/)
          INTEGER(SIZE_T)  max_len

          !
          ! Initialize the vl_int_data array.
          !
          do i = 1, 6
             do j = 1, 5
                vl_int_data(j,i) = -100
             end do
          end do

          do i = 2, 6
             do j = 1,  i-1
                vl_int_data(j,i) = i-1
             end do
          end do

           do i = 1,6
              len(i) = i-1
           end do


          !
          ! Create a new file using default properties.
          !
          CALL h5_fixname_f(filename, fix_filename, H5P_DEFAULT_F, error)
          if (error .ne. 0) then
              write(*,*) "Cannot modify filename"
              stop
          endif
          CALL h5fcreate_f(fix_filename, H5F_ACC_TRUNC_F, file_id, error)
              CALL check("h5fcreate_f", error, total_error)


          !
          ! Create the dataspace.
          !
          CALL h5screate_simple_f(rank, dims, dspace_id, error)
              CALL check("h5screate_simple_f", error, total_error)


          !
          ! Create the dataset with default properties.
          !
          CALL h5tvlen_create_f(H5T_NATIVE_INTEGER, vltype_id, error)
              CALL check("h5dvlen_create_f", error, total_error)

          CALL h5dcreate_f(file_id, dsetname, vltype_id, dspace_id, &
                           dset_id, error)
              CALL check("h5dcreate_f", error, total_error)

          !
          ! Write the dataset.
          !
          CALL h5dwrite_vl_f(dset_id, vltype_id, vl_int_data, data_dims, len, error)
              CALL check("h5dwrite_int_f", error, total_error)



          !   
          ! End access to the dataset and release resources used by it.
          !
          CALL h5dclose_f(dset_id, error)
              CALL check("h5dclose_f", error, total_error)

          !
          ! Close the file.
          !
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)

          !
          ! Open the existing file.
          !
          CALL h5fopen_f (fix_filename, H5F_ACC_RDWR_F, file_id, error)
              CALL check("h5fopen_f", error, total_error)

          !
          ! Open the existing dataset.
          !
          CALL h5dopen_f(file_id, dsetname, dset_id, error)
              CALL check("h5dopen_f", error, total_error)

          CALL h5dvlen_get_max_len_f(dset_id, vltype_id, dspace_id, max_len, error)
              CALL check("h5dvlen_get_max_len_f", error, total_error)
              if(max_len .ne. data_dims(1)) then
                      total_error = total_error + 1
                      write(*,*) "Wrong number of elemets returned by h5dvlen_get_max_len_f"
              endif
          !
          ! Read the dataset.
          !
          CALL h5dread_vl_f(dset_id, vltype_id, vl_int_data_out, data_dims, len_out, &
                            error, mem_space_id = dspace_id, file_space_id = dspace_id)
              CALL check("h5dread_int_f", error, total_error)
              do ih = 1, data_dims(2)
              do jh = 1, len_out(ih)
              if(vl_int_data(jh,ih) .ne. vl_int_data_out(jh,ih))  then
                  total_error = total_error + 1
                  write(*,*) "h5dread_vl_f returned incorrect data"
              endif
              enddo
               if (len(ih) .ne. len_out(ih)) then
                  total_error = total_error + 1
                  write(*,*) "h5dread_vl_f returned incorrect data"
              endif
              enddo

          !
          CALL h5dclose_f(dset_id, error)
              CALL check("h5dclose_f", error, total_error)

          CALL h5tclose_f(vltype_id, error)
              CALL check("h5tclose_f", error, total_error)

          !
          ! Terminate access to the data space.
          !
          CALL h5sclose_f(dspace_id, error)
              CALL check("h5sclose_f", error, total_error)

          !
          ! Close the file.
          !
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)
          if(cleanup) CALL h5_cleanup_f(filename, H5P_DEFAULT_F, error)
              CALL check("h5_cleanup_f", error, total_error)

          RETURN
        END SUBROUTINE vl_test_integer

        SUBROUTINE vl_test_real(cleanup, total_error)
        USE HDF5 ! This module contains all necessary modules
        USE TH5_MISC

          IMPLICIT NONE
          LOGICAL, INTENT(IN) :: cleanup
          INTEGER, INTENT(OUT) :: total_error

          CHARACTER(LEN=8), PARAMETER :: filename = "VLtypesR" ! File name
          CHARACTER(LEN=80) :: fix_filename
          CHARACTER(LEN=6), PARAMETER :: dsetname = "VLreal"     ! Dataset name

          INTEGER(HID_T) :: file_id       ! File identifier
          INTEGER(HID_T) :: dset_id       ! Dataset identifier
          INTEGER(HID_T) :: dspace_id     ! Dataspace identifier
          INTEGER(HID_T) :: vltype_id     ! Datatype identifier


          INTEGER(HSIZE_T), DIMENSION(1) :: dims = (/6/) ! Dataset dimensions
          INTEGER(SIZE_T), DIMENSION(6) :: len           ! Elements lengths
          INTEGER(SIZE_T), DIMENSION(6) :: len_out
          INTEGER     ::   rank = 1                      ! Dataset rank

          REAL, DIMENSION(5,6) :: vl_real_data ! Data buffers
          REAL, DIMENSION(5,6) :: vl_real_data_out ! Data buffers
          INTEGER     ::   error ! Error flag

          INTEGER     :: i, j    !general purpose integers
          INTEGER(HSIZE_T)     :: ih, jh    !general purpose integers
          INTEGER(HSIZE_T), DIMENSION(2) :: data_dims = (/5,6/)
          INTEGER(SIZE_T)  max_len
          INTEGER(HID_T) ::  vl_type_id
          LOGICAL        ::  vl_flag
          LOGICAL        ::  differ

          !
          ! Initialize the vl_int_data array.
          !
          do i = 1, 6
             do j = 1, 5
                vl_real_data(j,i) = -100.
             end do
          end do

          do i = 2, 6
             do j = 1,  i-1
                vl_real_data(j,i) = i-1
             end do
          end do

           do i = 1,6
              len(i) = i-1
           end do


          !
          ! Create a new file using default properties.
          !
          CALL h5_fixname_f(filename, fix_filename, H5P_DEFAULT_F, error)
          if (error .ne. 0) then
              write(*,*) "Cannot modify filename"
              stop
          endif
          CALL h5fcreate_f(fix_filename, H5F_ACC_TRUNC_F, file_id, error)
              CALL check("h5fcreate_f", error, total_error)


          !
          ! Create the dataspace.
          !
          CALL h5screate_simple_f(rank, dims, dspace_id, error)
              CALL check("h5screate_simple_f", error, total_error)


          !
          ! Create the dataset with default properties.
          !
          CALL h5tvlen_create_f(H5T_NATIVE_REAL, vltype_id, error)
              CALL check("h5dvlen_create_f", error, total_error)

          CALL h5dcreate_f(file_id, dsetname, vltype_id, dspace_id, &
                           dset_id, error)
              CALL check("h5dcreate_f", error, total_error)
          CALL h5dget_type_f(dset_id, vl_type_id, error)
              CALL check("h5dget_type_f", error, total_error)
          CALL h5tis_variable_str_f( vl_type_id, vl_flag, error)
              CALL check("h5tis_variable_str_f", error, total_error)
              if( vl_flag ) then
                 write(*,*) "type is wrong"
                 total_error = total_error + 1
              endif


          !
          ! Write the dataset.
          !
          CALL h5dwrite_vl_f(dset_id, vltype_id, vl_real_data, data_dims, len, error)
              CALL check("h5dwrite_vl_real_f", error, total_error)


          !
          ! End access to the dataset and release resources used by it.
          !
          CALL h5dclose_f(dset_id, error)
              CALL check("h5dclose_f", error, total_error)

          !
          ! Close the file.
          !
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)

          !
          ! Open the existing file.
          !
          CALL h5fopen_f (fix_filename, H5F_ACC_RDWR_F, file_id, error)
              CALL check("h5fopen_f", error, total_error)

          !
          ! Open the existing dataset.
          !
          CALL h5dopen_f(file_id, dsetname, dset_id, error)
              CALL check("h5dopen_f", error, total_error)

          CALL h5dvlen_get_max_len_f(dset_id, vltype_id, dspace_id, max_len, error)
              CALL check("h5dvlen_get_max_len_f", error, total_error)
              if(max_len .ne. data_dims(1)) then
                      total_error = total_error + 1
                      write(*,*) "Wrong number of elemets returned by h5dvlen_get_max_len_f"
              endif
          !
          ! Read the dataset.
          !
          CALL h5dread_vl_f(dset_id, vltype_id, vl_real_data_out, data_dims, len_out, &
                            error, mem_space_id = dspace_id, file_space_id = dspace_id)
              CALL check("h5dread_real_f", error, total_error)
              do ih = 1, data_dims(2)
              do jh = 1, len_out(ih)
              CALL compare_floats(vl_real_data(jh,ih), vl_real_data_out(jh,ih), differ)  
              if(differ)  then
                  total_error = total_error + 1
                  write(*,*) "h5dread_vl_f returned incorrect data"
              endif
              enddo
               if (len(ih) .ne. len_out(ih)) then
                  total_error = total_error + 1
                  write(*,*) "h5dread_vl_f returned incorrect data"
              endif
              enddo


          !
          CALL h5dclose_f(dset_id, error)
              CALL check("h5dclose_f", error, total_error)

          CALL h5tclose_f(vltype_id, error)
              CALL check("h5tclose_f", error, total_error)

          !
          ! Terminate access to the data space.
          !
          CALL h5sclose_f(dspace_id, error)
              CALL check("h5sclose_f", error, total_error)

          !
          ! Close the file.
          !
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)
          if(cleanup) CALL h5_cleanup_f(filename, H5P_DEFAULT_F, error)
              CALL check("h5_cleanup_f", error, total_error)

          RETURN
        END SUBROUTINE vl_test_real

        SUBROUTINE vl_test_string(cleanup, total_error)
        USE HDF5 ! This module contains all necessary modules
        USE TH5_MISC

          IMPLICIT NONE
          LOGICAL, INTENT(IN) :: cleanup
          INTEGER, INTENT(OUT) :: total_error

          CHARACTER(LEN=8), PARAMETER :: filename = "VLtypesS" ! File name
          CHARACTER(LEN=80) :: fix_filename
          CHARACTER(LEN=9), PARAMETER :: dsetname = "VLstrings"     ! Dataset name

          INTEGER(HID_T) :: file_id       ! File identifier
          INTEGER(HID_T) :: dset_id       ! Dataset identifier
          INTEGER(HID_T) :: dspace_id     ! Dataspace identifier


          INTEGER(HSIZE_T), DIMENSION(1) :: dims = (/4/) ! Dataset dimensions
          INTEGER(SIZE_T), DIMENSION(4) :: str_len           ! Elements lengths
          INTEGER(SIZE_T), DIMENSION(4) :: str_len_out
          INTEGER     ::   rank = 1                      ! Dataset rank

          CHARACTER(LEN=10), DIMENSION(4) :: string_data ! Array of strings
          CHARACTER(LEN=10), DIMENSION(4) :: string_data_out     ! Data buffers
          INTEGER     ::   error ! Error flag

          INTEGER(HSIZE_T) :: ih    !general purpose integers
          INTEGER(HSIZE_T), DIMENSION(2) :: data_dims = (/10,4/)
          INTEGER(HID_T) :: vl_type_id
          LOGICAL        :: vl_flag

          !
          ! Initialize the string_data array.
          !
          string_data(1) = 'This is   '
          str_len(1) = 8
          string_data(2) = 'a fortran '
          str_len(2) = 10
          string_data(3) = 'strings   '
          str_len(3) = 8
          string_data(4) = 'test.     '
          str_len(4) = 5


          !
          ! Create a new file using default properties.
          !
          CALL h5_fixname_f(filename, fix_filename, H5P_DEFAULT_F, error)
          if (error .ne. 0) then
              write(*,*) "Cannot modify filename"
              stop
          endif
          CALL h5fcreate_f(fix_filename, H5F_ACC_TRUNC_F, file_id, error)
              CALL check("h5fcreate_f", error, total_error)


          !
          ! Create the dataspace.
          !
          CALL h5screate_simple_f(rank, dims, dspace_id, error)
              CALL check("h5screate_simple_f", error, total_error)


          !
          ! Create the dataset with default properties.
          !
          CALL h5dcreate_f(file_id, dsetname, H5T_STRING, dspace_id, &
                           dset_id, error)
              CALL check("h5dcreate_f", error, total_error)
          !
          ! Check that dataset has a string datatype
          !
          CALL h5dget_type_f(dset_id, vl_type_id, error)
              CALL check("h5dget_type_f", error, total_error)
          CALL h5tis_variable_str_f( vl_type_id, vl_flag, error)
              CALL check("h5tis_variable_str_f", error, total_error)
              if( .NOT. vl_flag ) then
                 write(*,*) "type is wrong"
                 total_error = total_error + 1
              endif

          !
          ! Write the dataset.
          !
          CALL h5dwrite_vl_f(dset_id, H5T_STRING, string_data, data_dims, str_len, error)
              CALL check("h5dwrite_string_f", error, total_error)


          !
          ! End access to the dataset and release resources used by it.
          !
          CALL h5dclose_f(dset_id, error)
              CALL check("h5dclose_f", error, total_error)

          !
          ! Close the file.
          !
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)

          !
          ! Open the existing file.
          !
          CALL h5fopen_f (fix_filename, H5F_ACC_RDWR_F, file_id, error)
              CALL check("h5fopen_f", error, total_error)

          !
          ! Open the existing dataset.
          !
          CALL h5dopen_f(file_id, dsetname, dset_id, error)
              CALL check("h5dopen_f", error, total_error)
          !
          !  Read the dataset.
          !
          CALL h5dread_vl_f(dset_id, H5T_STRING, string_data_out, data_dims,  &
                            str_len_out, error)
              CALL check("h5dread_string_f", error, total_error)
          do 100 ih = 1, data_dims(2)
             if(str_len(ih) .ne. str_len_out(ih)) then
                total_error=total_error + 1
                write(*,*) 'Returned string length is incorrect'
                goto 100
             endif
             if(string_data(1)(1:str_len(ih)) .ne. string_data_out(1)(1:str_len(ih))) then
             write(*,*) ' Returned string is wrong'
             total_error = total_error + 1
             endif
100       continue

          !
          CALL h5dclose_f(dset_id, error)
              CALL check("h5dclose_f", error, total_error)

          !
          ! Terminate access to the data space.
          !
          CALL h5sclose_f(dspace_id, error)
              CALL check("h5sclose_f", error, total_error)

          !
          ! Close the file.
          !
          CALL h5fclose_f(file_id, error)
              CALL check("h5fclose_f", error, total_error)
          if(cleanup) CALL h5_cleanup_f(filename, H5P_DEFAULT_F, error)
              CALL check("h5_cleanup_f", error, total_error)

          RETURN
        END SUBROUTINE vl_test_string
END MODULE TH5VL