-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhdf5_utils.f90
3716 lines (2856 loc) · 122 KB
/
hdf5_utils.f90
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!> \brief A set of high level wrapper subroutines for HDF5
!>
!> \par \b Features:
!> - opening and closing files
!> - creating/opening/closing groups
!> - get rank and dimensions of dataset
!> - reading and writing dataset (integer, real, double)
!> - uses a generic interface to switch on rank and kind
!> - writing/reading attributes (integer, real, double, string)
!> - uses a generic interface to switch on rank and kind
!>
!> \todo
!> - reading and writing ( strings )
!> - hdf_exists (h5o_exist_by_name or h5l_exists)
!> - hdf_get_*
!> - hdf_get_obj_name (h5iget_name_f)
!> - hdf_get_obj_type (h5iget_type_f)
!> - hdf_get_dset_type (H5Dget_type)
!> - hdf_get_obj_id (not needed)
!> - error checking,
!> - check dims when reading
!> - check dataset/attribute name when reading
!> - check group name when reading/writing
!> - stop on error vs return error flag vs global error flag
!>
!> \note I might use H5T_STD_I32LE, H5T_IEEE_F32LE, H5T_IEEE_F64LE instead of H5T_NATIVE_DOUBLE when
!> creating a dataset. This would make the hdf5 file more portable?
!> \note should I support other integer types (ie 16 and 64)? I am not sure if hdf5 fortran supports these
!>
module HDF5_utils
use hdf5
implicit none
private
public :: hdf_open_file, hdf_close_file
public :: hdf_create_group, hdf_open_group, hdf_close_group
public :: hdf_exists, hdf_get_rank, hdf_get_dims
public :: hdf_write_dataset, hdf_read_dataset
public :: hdf_write_attribute, hdf_read_attribute
public :: hdf_create_dataset
public :: hdf_write_vector_to_dataset, hdf_read_vector_from_dataset
public :: HID_T, hdf_set_print_messages, hdf_set_default_filter
!> \brief Generic interface to write a dataset
!>
!> Supported types
!> - integers (scalar and 1d-6d arrays)
!> - doubles (scalar and 1d-6d arrays)
!> - reals (scalar and 1d-6d arrays)
! - string (scalar and 1d-6d arrays)
!>
!> \param[in] loc_id local id in file
!> \param[in] dset_name name of dataset
!> \param[in] array data array to be written
!> \param[in] chunks (optional) chunk size for dataset
!> \param[in] filter (optional) filter to use ('none', 'szip', 'gzip', 'gzip+shuffle')
interface hdf_write_dataset
module procedure hdf_write_dataset_integer_0
module procedure hdf_write_dataset_integer_1
module procedure hdf_write_dataset_integer_2
module procedure hdf_write_dataset_integer_3
module procedure hdf_write_dataset_integer_4
module procedure hdf_write_dataset_integer_5
module procedure hdf_write_dataset_integer_6
module procedure hdf_write_dataset_real_0
module procedure hdf_write_dataset_real_1
module procedure hdf_write_dataset_real_2
module procedure hdf_write_dataset_real_3
module procedure hdf_write_dataset_real_4
module procedure hdf_write_dataset_real_5
module procedure hdf_write_dataset_real_6
module procedure hdf_write_dataset_double_0
module procedure hdf_write_dataset_double_1
module procedure hdf_write_dataset_double_2
module procedure hdf_write_dataset_double_3
module procedure hdf_write_dataset_double_4
module procedure hdf_write_dataset_double_5
module procedure hdf_write_dataset_double_6
end interface hdf_write_dataset
!> \brief Generic interface to write a vector to dataset
!>
!> The vector is is written out in along the fast dimension
!> (column oriented in FORTRAN, row oriented in the HDF5 file).
!> So the vector should have the same length as the first dimension
!> of the dataset, and the offset should agree with dims(2:rank) of
!> of the dataset.
!>
!> Supported types
!> - integers (scalar and 1d-6d arrays)
!> - doubles (scalar and 1d-6d arrays)
!> - reals (scalar and 1d-6d arrays)
! - string (scalar and 1d-6d arrays)
!>
!> \param[in] loc_d local id in file
!> \param[in] dset_name name of dataset
!> \param[in] offset position within the dataset
!> \param[in] vector data array to be written
interface hdf_write_vector_to_dataset
module procedure hdf_write_vector_to_dataset_integer
module procedure hdf_write_vector_to_dataset_real
module procedure hdf_write_vector_to_dataset_double
end interface hdf_write_vector_to_dataset
!> \brief Generic interface to read a dataset of doubles
!>
!> Supported types
!> - integers (scalar and 1d-6d arrays)
!> - doubles (scalar and 1d-6d arrays)
!> - reals (scalar and 1d-6d arrays)
! - string (scalar and 1d-6d arrays)
!>
!> \param[in] loc_d local id in file
!> \param[in] dset_name name of dataset
!> \param[out] array data array to be read
interface hdf_read_dataset
module procedure hdf_read_dataset_integer_0
module procedure hdf_read_dataset_integer_1
module procedure hdf_read_dataset_integer_2
module procedure hdf_read_dataset_integer_3
module procedure hdf_read_dataset_integer_4
module procedure hdf_read_dataset_integer_5
module procedure hdf_read_dataset_integer_6
module procedure hdf_read_dataset_real_0
module procedure hdf_read_dataset_real_1
module procedure hdf_read_dataset_real_2
module procedure hdf_read_dataset_real_3
module procedure hdf_read_dataset_real_4
module procedure hdf_read_dataset_real_5
module procedure hdf_read_dataset_real_6
module procedure hdf_read_dataset_double_0
module procedure hdf_read_dataset_double_1
module procedure hdf_read_dataset_double_2
module procedure hdf_read_dataset_double_3
module procedure hdf_read_dataset_double_4
module procedure hdf_read_dataset_double_5
module procedure hdf_read_dataset_double_6
end interface hdf_read_dataset
!> \brief Generic interface to read a vector from a dataset
!>
!> The vector is is read in along the fast dimension
!> (column oriented in FORTRAN, row oriented in the HDF5 file).
!> So the vector should have the same length as the first dimension
!> of the dataset, and the offset should agree with dims(2:rank) of
!> of the dataset.
!>
!> Supported types
!> - integers (scalar and 1d-6d arrays)
!> - doubles (scalar and 1d-6d arrays)
!> - reals (scalar and 1d-6d arrays)
! - string (scalar and 1d-6d arrays)
!>
!> \param[in] loc_d local id in file
!> \param[in] dset_name name of dataset
!> \param[in] offset position within the dataset
!> \param[out] vector data array to be written
interface hdf_read_vector_from_dataset
module procedure hdf_read_vector_from_dataset_integer
module procedure hdf_read_vector_from_dataset_real
module procedure hdf_read_vector_from_dataset_double
end interface hdf_read_vector_from_dataset
!> \brief Generic interface to write attribute
!>
!> Supported types
!> - integers (scalar and 1d arrays)
!> - reals (scalar and 1d arrays)
!> - doubles (scalar and 1d arrays)
!> - string (1d array of characters)
!>
!> \param[in] loc_id local id in file
!> \param[in] obj_name name of object to be attached to (if left blank, just use loc_id)
!> \param[in] attr_name name of attribute to be added
!> \param[in] array attribute data to be written
interface hdf_write_attribute
module procedure hdf_write_attr_integer_0
module procedure hdf_write_attr_integer_1
module procedure hdf_write_attr_real_0
module procedure hdf_write_attr_real_1
module procedure hdf_write_attr_double_0
module procedure hdf_write_attr_double_1
module procedure hdf_write_attr_string
end interface hdf_write_attribute
!> \brief Generic interface to read attribute
!>
!> Supported types
!> - integers (scalar and 1d arrays)
!> - reals (scalar and 1d arrays)
!> - doubles (scalar and 1d arrays)
!> - string (1d array of characters)
!>
!> \param[in] loc_id local id in file
!> \param[in] obj_name name of object to be attached to (if left blank, just use loc_id)
!> \param[in] attr_name name of attribute to be added
!> \param[in] array attribute data to be written
interface hdf_read_attribute
module procedure hdf_read_attr_integer_0
module procedure hdf_read_attr_integer_1
module procedure hdf_read_attr_real_0
module procedure hdf_read_attr_real_1
module procedure hdf_read_attr_double_0
module procedure hdf_read_attr_double_1
module procedure hdf_read_attr_string
end interface hdf_read_attribute
! precision for this file
integer, parameter :: sp = kind(1.0) ! single precision
integer, parameter :: dp = kind(1.0d0) ! double precision
!
logical :: hdf_print_messages = .false.
!
character(len=32) :: hdf_default_filter = 'none'
integer :: hdf_gzip_level = 6 ! 0-9
integer :: hdf_szip_pixels_per_block = 8 ! should be even number less than 32 (https://portal.hdfgroup.org/display/HDF5/H5P_SET_SZIP)
character(len=2) :: hdf_szip_options = 'NN' ! H5_SZIP_NN_OM_F or H5_SZIP_EC_OM_F
contains
!> \brief Sets the value of hdf_print_messages
!>
!> By default, hdf_print_messages = .false. By setting it
!> to .true., some messages are printed detailing what hdf_utils
!> is doing.
subroutine hdf_set_print_messages(val_print_messages)
logical, intent(in) :: val_print_messages !< new value for hdf_print_messages
hdf_print_messages = val_print_messages
end subroutine hdf_set_print_messages
!> \brief Sets the value of hdf_print_messages
!>
!>
subroutine hdf_set_default_filter(filter, gzip_level, szip_pixels_per_block, szip_options)
character(len=*), intent(in) :: filter !< new value for hdf_print_messages
integer, optional, intent(in) :: gzip_level
integer, optional, intent(in) :: szip_pixels_per_block
character(len=2), optional, intent(in) :: szip_options
hdf_default_filter = filter
if (present(gzip_level)) then
hdf_gzip_level = gzip_level
endif
if(present(szip_pixels_per_block)) then
hdf_szip_pixels_per_block = szip_pixels_per_block
endif
if (present(szip_options)) then
!hdf_szip_options = szip_options
if (szip_options == 'NN') then
hdf_szip_options = 'NN'
elseif (szip_options == 'EC') then
hdf_szip_options = 'EC'
else
write(*,'(A)') "-->hdf_set_default_filter: warning szip_options "//trim(szip_options)//" not recognized"
stop
endif
endif
!write(*,'(A,I0,A,I0)') ' H5_SZIP_NN_OM_F=', H5_SZIP_NN_OM_F, ', H5_SZIP_EC_OM_F=', H5_SZIP_EC_OM_F
write(*,*) filter, gzip_level, szip_pixels_per_block, szip_options
end subroutine hdf_set_default_filter
!> \brief Check if location exists.
!>
!> Also checks is intemediate paths exists in a safe way.
function hdf_exists(loc_id, obj_name) result(exists)
integer(HID_T), intent(in) :: loc_id !< local id
character(len=*), intent(in) :: obj_name !< relative path to object
logical :: exists !< .TRUE. if everything exists, .FALSE. otherwise
integer :: hdferror, pos, cpos, str_len
if (hdf_print_messages) then
write(*,'(A,A)') "->hdf_exists: " // obj_name
end if
! check intermediate paths (subgroups)
str_len = len_trim(obj_name)
cpos = 0
do
!start = cpos + 1
!write(*,*) start, str_len, obj_name(start:str_len)
pos = index(obj_name(cpos+1:str_len), "/")
! no subgroup found
if (pos == 0) exit
! check subgroup
cpos = cpos + pos
call h5lexists_f(loc_id, obj_name(1:cpos-1), exists, hdferror)
!write(*,*) obj_name(1:cpos-1), exists
! return if intermediate path fails
if (exists .eqv. .false.) then
if (hdf_print_messages) then
write(*,'(A,A,A)') "--->hdf_exists: subpath '", obj_name(1:cpos-1), "' does not exist, return false"
end if
exists = .false.
return
end if
end do
! check object (unless obj_name ended with "/"
if (cpos /= str_len) then
call h5lexists_f(loc_id, obj_name, exists, hdferror)
!write(*,*) obj_name, exists
if (exists .eqv. .false.) then
if (hdf_print_messages) then
write(*,'(A,A,A)') "--->hdf_exists: object '", obj_name, "' does not exist, return false"
end if
exists = .false.
return
end if
end if
exists = .true.
return
end function hdf_exists
!> \brief Opens file and return identifier
!>
!> \todo
!> - case insentive STATUS and ACTION
!> - delete file for REPLACE case
!>
!> | STATUS | ACTION | Description |
!> | :-----: | :-------: | :----------------------------------- |
!> | NEW | na | calls h5fcreate with H5F_ACC_TRUNC_F |
!> | REPLACE | na | calls h5fcreate with H5F_ACC_EXCL_F |
!> | OLD | READ | calls h5fopen with H5F_ACC_RDONLY_F |
!> | OLD | WRITE | calls h5fopen with H5F_ACC_RDWR_F |
!> | OLD | READWRITE | calls h5fopen with H5F_ACC_RDWR_F |
!>
subroutine hdf_open_file(file_id, filename, STATUS, ACTION)
integer(HID_T), intent(out) :: file_id !< HDF5 id of the file
character(len=*), intent(in) :: filename !< the HDF5 filename
character(len=*), optional, intent(in) :: STATUS !< file status (OLD, NEW, REPLACE)
character(len=*), optional, intent(in) :: ACTION !< file action (READ, WRITE, READWRITE)
integer :: hdferror
character(len=16) :: status2, action2
if (hdf_print_messages) then
write(*,'(A)') "->hdf_open_file: " // trim(filename)
end if
! open hdf5 interface
call h5open_f(hdferror)
!write(*,'(A20,I0)') "h5open: ", hdferror
! set defaults
status2 = 'NEW'
if (present(STATUS)) status2 = STATUS
action2 = 'READWRITE'
if (present(STATUS)) action2 = ACTION
! open/create hdf5 file
if (status2 == 'OLD') then
if (action2 == 'READ') then
call h5fopen_f(filename, H5F_ACC_RDONLY_F, file_id, hdferror)
elseif ( (action2 == 'WRITE') .or. (action2 == 'READWRITE') ) then
call h5fopen_f(filename, H5F_ACC_RDWR_F, file_id, hdferror)
else
write(*,*) "hdf_open: action = ", action2, " not supported."
stop
end if
elseif (status2 == 'NEW') then
call h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, hdferror)
elseif (status2 == 'REPLACE') then
call system("rm -f " // filename)
call h5fcreate_f(filename, H5F_ACC_EXCL_F, file_id, hdferror)
else
write(*,*) "hdf_open: status = ", status2, " not supported."
stop
end if
!write(*,'(A20,I0)') "h5fcreate: ", hdferror
end subroutine hdf_open_file
!> \brief Closes a hdf5 file
subroutine hdf_close_file(file_id)
integer(HID_T), intent(in) :: file_id !< file id to be closed
integer :: hdferror
if (hdf_print_messages) then
write(*,'(A)') "->hdf_close_file"
end if
call h5fclose_f(file_id, hdferror)
!write(*,'(A20,I0)') "h5fclose: ", hdferror
call h5close_f(hdferror)
end subroutine hdf_close_file
!> \brief Create a new group
subroutine hdf_create_group(loc_id, group_name)
integer(HID_T), intent(in) :: loc_id !< location id where to put the group
character(len=*), intent(in) :: group_name !< name of the group
integer(HID_T) :: grp_id
integer :: hdferror
if (hdf_print_messages) then
write(*,'(A)') "->hdf_create_group: " // trim(group_name)
end if
call h5gcreate_f(loc_id, group_name, grp_id, hdferror)
!write(*,'(A20,I0)') "h5gcreate: ", hdferror
call h5gclose_f(grp_id, hdferror)
!write(*,'(A20,I0)') "h5gclose: ", hdferror
end subroutine hdf_create_group
!> \brief Opens a group and returns the identifier
subroutine hdf_open_group(loc_id, group_name, group_id)
integer(HID_T), intent(in) :: loc_id !< location id where to put the group
character(len=*), intent(in) :: group_name !< name of the group
integer(HID_T), intent(out) :: group_id !< id for the group
integer :: hdferror
if (hdf_print_messages) then
write(*,'(A,A,A)') "->hdf_open_group: '" // trim(group_name) // "'"
end if
if (hdf_exists(loc_id, group_name)) then
if (hdf_print_messages) then
write(*,'(A,A,A)') "->hdf_open_group: opening group '" // trim(group_name) // "'"
end if
call h5gopen_f(loc_id, group_name, group_id, hdferror)
else
if (hdf_print_messages) then
write(*,'(A,A,A)') "->hdf_open_group: group '" // trim(group_name) // "' does not exist, return with error"
end if
hdferror = -1
end if
end subroutine hdf_open_group
!> \brief Close a group by identifier
subroutine hdf_close_group(group_id)
integer(HID_T), intent(in) :: group_id !< id for the group
integer :: hdferror
if (hdf_print_messages) then
write(*,'(A)') "->hdf_close_group"
end if
call h5gclose_f(group_id, hdferror)
!write(*,'(A20,I0)') "h5gclose: ", hdferror
end subroutine hdf_close_group
!> \brief Get the rank of a dataset
subroutine hdf_get_rank(loc_id, dset_name, rank)
integer(HID_T), intent(in) :: loc_id !< location id
character(len=*), intent(in) :: dset_name !< dataset name
integer, intent(out) :: rank !< rank of the dataset
integer(HID_T) :: dset_id, dspace_id
integer :: hdferror
if (hdf_print_messages) then
write(*,'(A)') "->hdf_get_rank"
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims)
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
! close id's
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
end subroutine hdf_get_rank
!> \brief get the dimensions of a dataset
subroutine hdf_get_dims(loc_id, dset_name, dims)
integer(HID_T), intent(in) :: loc_id !< location id
character(len=*), intent(in) :: dset_name !< name of dataset
integer, intent(out) :: dims(:) !< dimensions of the dataset
integer(HID_T) :: dset_id, dspace_id
integer :: rank
integer(HSIZE_T) :: dset_dims(6), max_dims(6)
integer :: hdferror
if (hdf_print_messages) then
write(*,'(A)') "->hdf_get_dims"
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims)
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
! get dims
call h5sget_simple_extent_dims_f(dspace_id, dset_dims(1:rank), max_dims(1:rank), hdferror)
dims(1:rank) = int(dset_dims(1:rank))
! close id's
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
end subroutine hdf_get_dims
! - hdf_get_kind (H5Dget_type)
!!----------------------------------------------------------------------------------------
!!--------------------------------hdf_write_vector_to_dataset-----------------------------
!!----------------------------------------------------------------------------------------
!> \brief create a dataset 'dset_name' with shape 'dset_dims' of type 'dset_type'
subroutine hdf_create_dataset(loc_id, dset_name, dset_dims, dset_type)
integer(HID_T), intent(in) :: loc_id !< local id in file
character(len=*), intent(in) :: dset_name !< name of dataset
integer, intent(in) :: dset_dims(:) !< dimensions of the dataset
character(len=*), intent(in) :: dset_type !< type of dataset (integer or double)
integer :: rank
integer(SIZE_T) :: dims(8)
integer(HID_T) :: dset_id, dspace_id
integer :: hdferror
if (hdf_print_messages) then
write(*,'(A)') "--->hdf_create_dataset: " // trim(dset_name)
end if
! set rank and dims
rank = size(dset_dims, 1)
dims(1:rank) = int(dset_dims, SIZE_T)
! create dataspace
call h5screate_simple_f(rank, dims, dspace_id, hdferror)
!write(*,'(A20,I0)') "h5screate_simple: ", hdferror
! create dataset
select case (dset_type)
case('integer')
call h5dcreate_f(loc_id, dset_name, H5T_NATIVE_INTEGER, dspace_id, dset_id, hdferror)
call h5dclose_f(dset_id, hdferror)
case('real')
call h5dcreate_f(loc_id, dset_name, H5T_NATIVE_DOUBLE, dspace_id, dset_id, hdferror)
call h5dclose_f(dset_id, hdferror)
case('double')
call h5dcreate_f(loc_id, dset_name, H5T_NATIVE_DOUBLE, dspace_id, dset_id, hdferror)
call h5dclose_f(dset_id, hdferror)
case default
write(*,'(A,A,A)') "---> ERROR: dset_type ", dset_type," not supported"
end select
! close all id's
call h5sclose_f(dspace_id, hdferror)
end subroutine hdf_create_dataset
!
subroutine hdf_write_vector_to_dataset_integer(loc_id, dset_name, offset, vector)
integer(HID_T), intent(in) :: loc_id ! local id in file
character(len=*), intent(in) :: dset_name ! name of dataset
integer, intent(in) :: offset(:) ! position within dataset
integer, intent(out) :: vector(:) ! data to be written
integer(HID_T) :: dset_id, dspace_id, mspace_id
integer :: rank
integer(HSIZE_T) :: dset_dims(6), max_dims(6), mdims(1)
integer(HSIZE_T) :: hs_count(6), hs_offset(6) ! Hyperslab offset
integer :: hdferror
integer :: i
character(len=32) :: format_string
if (hdf_print_messages) then
write(*,'(A)') "--->hdf_write_vector_to_dataset_integer: " // trim(dset_name)
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims), and dims
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
call h5sget_simple_extent_dims_f(dspace_id, dset_dims(1:rank), max_dims(1:rank), hdferror)
! check size and offset
if (size(vector,1) == dset_dims(1)) then
if (all(offset(1:rank-1) <= dset_dims(2:rank)) .and. all(offset(1:rank-1) > 0)) then
! select hyperslab in dataset (note: convert FORTRAN offset to C offset by subtracting 1)
hs_count(1) = dset_dims(1)
hs_count(2:rank) = 1
hs_offset(1) = 0
hs_offset(2:rank) = offset(1:rank-1) - 1
call h5sselect_hyperslab_f(dspace_id, H5S_SELECT_SET_F, hs_offset(1:rank), hs_count(1:rank), hdferror)
! set mspace to a vector
mdims(1) = size(vector,1)
call h5screate_simple_f(1, mdims, mspace_id, hdferror)
! write out vector
call h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, vector, mdims, hdferror, mspace_id, dspace_id)
! close mspace_id
call h5sclose_f(mspace_id, hdferror)
else
write(format_string, '(A,I0,A,I0,A)') '(A,', rank-1, '(I0,A),A,', rank-1, '(I0,A),A)'
write(*,format_string) "--->ERROR: offset=(", (offset(i), ',', i=1,rank-1) , &
"), is not constent with dset_dims(2:rank)=(", (dset_dims(i), ',', i=2,rank),")"
end if
else
write(*,'(A,I0,A,I0)') "--->ERROR: size(vector)=", size(vector), &
", is not constent with dset_dims(1)=", dset_dims(1)
endif
! close id's
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
end subroutine hdf_write_vector_to_dataset_integer
!
subroutine hdf_write_vector_to_dataset_real(loc_id, dset_name, offset, vector)
integer(HID_T), intent(in) :: loc_id ! local id in file
character(len=*), intent(in) :: dset_name ! name of dataset
integer, intent(in) :: offset(:) ! position within dataset
real(sp), intent(in) :: vector(:) ! data to be written
integer(HID_T) :: dset_id, dspace_id, mspace_id
integer :: rank
integer(HSIZE_T) :: dset_dims(6), max_dims(6), mdims(1)
integer(HSIZE_T) :: hs_count(6), hs_offset(6)
integer :: hdferror
integer :: i
character(len=32) :: format_string
if (hdf_print_messages) then
write(*,'(A)') "--->hdf_write_vector_to_dataset_real: " // trim(dset_name)
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims), and dims
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
call h5sget_simple_extent_dims_f(dspace_id, dset_dims(1:rank), max_dims(1:rank), hdferror)
! check size and offset
if (size(vector,1) == dset_dims(1)) then
if (all(offset(1:rank-1) <= dset_dims(2:rank)) .and. all(offset(1:rank-1) > 0)) then
! select hyperslab in dataset (note: convert FORTRAN offset to C offset by subtracting 1)
hs_count(1) = dset_dims(1)
hs_count(2:rank) = 1
hs_offset(1) = 0
hs_offset(2:rank) = offset(1:rank-1) - 1
call h5sselect_hyperslab_f(dspace_id, H5S_SELECT_SET_F, hs_offset(1:rank), hs_count(1:rank), hdferror)
! set mspace to a vector
mdims(1) = size(vector,1)
call h5screate_simple_f(1, mdims, mspace_id, hdferror)
! write out vector
call h5dwrite_f(dset_id, H5T_NATIVE_REAL, vector, mdims, hdferror, mspace_id, dspace_id)
! close mspace_id
call h5sclose_f(mspace_id, hdferror)
else
write(format_string, '(A,I0,A,I0,A)') '(A,', rank-1, '(I0,A),A,', rank-1, '(I0,A),A)'
write(*,format_string) "--->ERROR: offset=(", (offset(i), ',', i=1,rank-1) , &
"), is not constent with dset_dims(2:rank)=(", (dset_dims(i), ',', i=2,rank),")"
end if
else
write(*,'(A,I0,A,I0)') "--->ERROR: size(vector)=", size(vector), &
", is not constent with dset_dims(1)=", dset_dims(1)
endif
! close id's
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
end subroutine hdf_write_vector_to_dataset_real
!
subroutine hdf_write_vector_to_dataset_double(loc_id, dset_name, offset, vector)
integer(HID_T), intent(in) :: loc_id ! local id in file
character(len=*), intent(in) :: dset_name ! name of dataset
integer, intent(in) :: offset(:) ! position within dataset
real(dp), intent(in) :: vector(:) ! data to be written
integer(HID_T) :: dset_id, dspace_id, mspace_id
integer :: rank
integer(HSIZE_T) :: dset_dims(6), max_dims(6), mdims(1)
integer(HSIZE_T) :: hs_count(6), hs_offset(6)
integer :: hdferror
integer :: i
character(len=32) :: format_string
if (hdf_print_messages) then
write(*,'(A)') "--->hdf_write_vector_to_dataset_double: " // trim(dset_name)
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims), and dims
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
call h5sget_simple_extent_dims_f(dspace_id, dset_dims(1:rank), max_dims(1:rank), hdferror)
! check size and offset
if (size(vector,1) == dset_dims(1)) then
if (all(offset(1:rank-1) <= dset_dims(2:rank)) .and. all(offset(1:rank-1) > 0)) then
! select hyperslab in dataset (note: convert FORTRAN offset to C offset by subtracting 1)
hs_count(1) = dset_dims(1)
hs_count(2:rank) = 1
hs_offset(1) = 0
hs_offset(2:rank) = offset(1:rank-1) - 1
call h5sselect_hyperslab_f(dspace_id, H5S_SELECT_SET_F, hs_offset(1:rank), hs_count(1:rank), hdferror)
! set mspace to a vector
mdims(1) = size(vector,1)
call h5screate_simple_f(1, mdims, mspace_id, hdferror)
! write out vector
call h5dwrite_f(dset_id, H5T_NATIVE_DOUBLE, vector, mdims, hdferror, mspace_id, dspace_id)
! close mspace_id
call h5sclose_f(mspace_id, hdferror)
else
write(format_string, '(A,I0,A,I0,A)') '(A,', rank-1, '(I0,A),A,', rank-1, '(I0,A),A)'
write(*,format_string) "--->ERROR: offset=(", (offset(i), ',', i=1,rank-1) , &
"), is not constent with dset_dims(2:rank)=(", (dset_dims(i), ',', i=2,rank),")"
end if
else
write(*,'(A,I0,A,I0)') "--->ERROR: size(vector)=", size(vector), &
", is not constent with dset_dims(1)=", dset_dims(1)
endif
! close id's
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
end subroutine hdf_write_vector_to_dataset_double
!
subroutine hdf_read_vector_from_dataset_integer(loc_id, dset_name, offset, vector)
integer(HID_T), intent(in) :: loc_id ! local id in file
character(len=*), intent(in) :: dset_name ! name of dataset
integer, intent(in) :: offset(:) ! position within dataset
integer, intent(out) :: vector(:) ! data to be written
integer(HID_T) :: dset_id, dspace_id, mspace_id
integer :: rank
integer(HSIZE_T) :: dset_dims(6), max_dims(6), mdims(1)
integer(HSIZE_T) :: hs_count(6), hs_offset(6) ! Hyperslab offset
integer :: hdferror
integer :: i
character(len=32) :: format_string
if (hdf_print_messages) then
write(*,'(A)') "--->hdf_read_vector_from_dataset_integer: " // trim(dset_name)
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims), and dims
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
call h5sget_simple_extent_dims_f(dspace_id, dset_dims(1:rank), max_dims(1:rank), hdferror)
! check size and offset
if (size(vector,1) == dset_dims(1)) then
if (all(offset(1:rank-1) <= dset_dims(2:rank)) .and. all(offset(1:rank-1) > 0)) then
! select hyperslab in dataset (note: convert FORTRAN offset to C offset by subtracting 1)
hs_count(1) = dset_dims(1)
hs_count(2:rank) = 1
hs_offset(1) = 0
hs_offset(2:rank) = offset(1:rank-1) - 1
call h5sselect_hyperslab_f(dspace_id, H5S_SELECT_SET_F, hs_offset(1:rank), hs_count(1:rank), hdferror)
! set mspace to a vector
mdims(1) = size(vector,1)
call h5screate_simple_f(1, mdims, mspace_id, hdferror)
! write out vector
call h5dread_f(dset_id, H5T_NATIVE_INTEGER, vector, mdims, hdferror, mspace_id, dspace_id)
! close mspace_id
call h5sclose_f(mspace_id, hdferror)
else
write(format_string, '(A,I0,A,I0,A)') '(A,', rank-1, '(I0,A),A,', rank-1, '(I0,A),A)'
write(*,format_string) "--->ERROR: offset=(", (offset(i), ',', i=1,rank-1) , &
"), is not constent with dset_dims(2:rank)=(", (dset_dims(i), ',', i=2,rank),")"
end if
else
write(*,'(A,I0,A,I0)') "--->ERROR: size(vector)=", size(vector), &
", is not constent with dset_dims(1)=", dset_dims(1)
endif
! close id's
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
end subroutine hdf_read_vector_from_dataset_integer
!
subroutine hdf_read_vector_from_dataset_real(loc_id, dset_name, offset, vector)
integer(HID_T), intent(in) :: loc_id ! local id in file
character(len=*), intent(in) :: dset_name ! name of dataset
integer, intent(in) :: offset(:) ! position within dataset
real(sp), intent(out) :: vector(:) ! data to be written
integer(HID_T) :: dset_id, dspace_id, mspace_id
integer :: rank
integer(HSIZE_T) :: dset_dims(6), max_dims(6), mdims(1)
integer(HSIZE_T) :: hs_count(6), hs_offset(6)
integer :: hdferror
integer :: i
character(len=32) :: format_string
if (hdf_print_messages) then
write(*,'(A)') "--->hdf_read_vector_from_dataset_real: " // trim(dset_name)
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims), and dims
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
call h5sget_simple_extent_dims_f(dspace_id, dset_dims(1:rank), max_dims(1:rank), hdferror)
! check size and offset
if (size(vector,1) == dset_dims(1)) then
if (all(offset(1:rank-1) <= dset_dims(2:rank)) .and. all(offset(1:rank-1) > 0)) then
! select hyperslab in dataset (note: convert FORTRAN offset to C offset by subtracting 1)
hs_count(1) = dset_dims(1)
hs_count(2:rank) = 1
hs_offset(1) = 0
hs_offset(2:rank) = offset(1:rank-1) - 1
call h5sselect_hyperslab_f(dspace_id, H5S_SELECT_SET_F, hs_offset(1:rank), hs_count(1:rank), hdferror)
! set mspace to a vector
mdims(1) = size(vector,1)
call h5screate_simple_f(1, mdims, mspace_id, hdferror)
! write out vector
call h5dwrite_f(dset_id, H5T_NATIVE_REAL, vector, mdims, hdferror, mspace_id, dspace_id)
! close mspace_id
call h5sclose_f(mspace_id, hdferror)
else
write(format_string, '(A,I0,A,I0,A)') '(A,', rank-1, '(I0,A),A,', rank-1, '(I0,A),A)'
write(*,format_string) "--->ERROR: offset=(", (offset(i), ',', i=1,rank-1) , &
"), is not constent with dset_dims(2:rank)=(", (dset_dims(i), ',', i=2,rank),")"
end if
else
write(*,'(A,I0,A,I0)') "--->ERROR: size(vector)=", size(vector), &
", is not constent with dset_dims(1)=", dset_dims(1)
endif
! close id's
call h5sclose_f(dspace_id, hdferror)
call h5dclose_f(dset_id, hdferror)
end subroutine hdf_read_vector_from_dataset_real
!
subroutine hdf_read_vector_from_dataset_double(loc_id, dset_name, offset, vector)
integer(HID_T), intent(in) :: loc_id ! local id in file
character(len=*), intent(in) :: dset_name ! name of dataset
integer, intent(in) :: offset(:) ! position within dataset
real(dp), intent(out) :: vector(:) ! data to be written
integer(HID_T) :: dset_id, dspace_id, mspace_id
integer :: rank
integer(HSIZE_T) :: dset_dims(6), max_dims(6), mdims(1)
integer(HSIZE_T) :: hs_count(6), hs_offset(6)
integer :: hdferror
integer :: i
character(len=32) :: format_string
if (hdf_print_messages) then
write(*,'(A)') "--->hdf_read_vector_from_dataset_double: " // trim(dset_name)
end if
! open dataset
call h5dopen_f(loc_id, dset_name, dset_id, hdferror)
! get dataspace
call h5dget_space_f(dset_id, dspace_id, hdferror)
! get rank (ndims), and dims
call h5sget_simple_extent_ndims_f(dspace_id, rank, hdferror)
call h5sget_simple_extent_dims_f(dspace_id, dset_dims(1:rank), max_dims(1:rank), hdferror)
! check size and offset
if (size(vector,1) == dset_dims(1)) then
if (all(offset(1:rank-1) <= dset_dims(2:rank)) .and. all(offset(1:rank-1) > 0)) then
! select hyperslab in dataset (note: convert FORTRAN offset to C offset by subtracting 1)
hs_count(1) = dset_dims(1)
hs_count(2:rank) = 1
hs_offset(1) = 0
hs_offset(2:rank) = offset(1:rank-1) - 1
call h5sselect_hyperslab_f(dspace_id, H5S_SELECT_SET_F, hs_offset(1:rank), hs_count(1:rank), hdferror)