-
Notifications
You must be signed in to change notification settings - Fork 142
/
HDF5.jl
2387 lines (2229 loc) · 94.8 KB
/
HDF5.jl
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
__precompile__()
module HDF5
using Compat
using Compat: unsafe_convert, String
## Add methods to...
import Base: ==, close, convert, done, dump, eltype, endof, flush, getindex,
isempty, isvalid, length, names, ndims, next, parent, read,
setindex!, show, size, sizeof, start, write
include("datafile.jl")
### Load and initialize the HDF library ###
const depsfile = joinpath(dirname(@__DIR__), "deps", "deps.jl")
if isfile(depsfile)
include(depsfile)
else
error("HDF5 not properly installed. Please run Pkg.build(\"HDF5\")")
end
function init_libhdf5()
status = ccall((:H5open, libhdf5), Cint, ())
status < 0 && error("Can't initialize the HDF5 library")
nothing
end
init_libhdf5()
function h5_get_libversion()
majnum = Ref{Cuint}()
minnum = Ref{Cuint}()
relnum = Ref{Cuint}()
status = ccall((:H5get_libversion, libhdf5),
Cint, (Ptr{Cuint}, Ptr{Cuint}, Ptr{Cuint}), majnum, minnum, relnum)
status < 0 && error("Error getting HDF5 library version")
VersionNumber(majnum[], minnum[], relnum[])
end
const libversion = h5_get_libversion()
## C types
const C_time_t = Int
## HDF5 types and constants
if libversion >= v"1.10.0"
const Hid = Int64
else
const Hid = Cint
end
const Herr = Cint
const Hsize = UInt64
const Hssize = Int64
const Htri = Cint # pseudo-boolean (negative if error)
const Haddr = UInt64
# Function to extract exported library constants
# Kudos to the library developers for making these available this way!
const libhdf5handle = Libdl.dlopen(libhdf5)
read_const(sym::Symbol) = unsafe_load(convert(Ptr{Hid}, Libdl.dlsym(libhdf5handle, sym)))
# iteration order constants
const H5_ITER_UNKNOWN = -1
const H5_ITER_INC = 0
const H5_ITER_DEC = 1
const H5_ITER_NATIVE = 2
const H5_ITER_N = 3
# indexing type constants
const H5_INDEX_UNKNOWN = -1
const H5_INDEX_NAME = 0
const H5_INDEX_CRT_ORDER = 1
# dataset constants
const H5D_COMPACT = 0
const H5D_CONTIGUOUS = 1
const H5D_CHUNKED = 2
# error-related constants
const H5E_DEFAULT = 0
# file access modes
const H5F_ACC_RDONLY = 0x00
const H5F_ACC_RDWR = 0x01
const H5F_ACC_TRUNC = 0x02
const H5F_ACC_EXCL = 0x04
const H5F_ACC_DEBUG = 0x08
const H5F_ACC_CREAT = 0x10
# object types
const H5F_OBJ_FILE = 0x0001
const H5F_OBJ_DATASET = 0x0002
const H5F_OBJ_GROUP = 0x0004
const H5F_OBJ_DATATYPE = 0x0008
const H5F_OBJ_ATTR = 0x0010
const H5F_OBJ_ALL = (H5F_OBJ_FILE|H5F_OBJ_DATASET|H5F_OBJ_GROUP|H5F_OBJ_DATATYPE|H5F_OBJ_ATTR)
const H5F_OBJ_LOCAL = 0x0020
# other file constants
const H5F_SCOPE_LOCAL = 0
const H5F_SCOPE_GLOBAL = 1
const H5F_CLOSE_DEFAULT = 0
const H5F_CLOSE_WEAK = 1
const H5F_CLOSE_SEMI = 2
const H5F_CLOSE_STRONG = 3
# object types (C enum H5Itype_t)
const H5I_FILE = 1
const H5I_GROUP = 2
const H5I_DATATYPE = 3
const H5I_DATASPACE = 4
const H5I_DATASET = 5
const H5I_ATTR = 6
const H5I_REFERENCE = 7
# Link constants
const H5L_TYPE_HARD = 0
const H5L_TYPE_SOFT = 1
const H5L_TYPE_EXTERNAL= 2
# Object constants
const H5O_TYPE_GROUP = 0
const H5O_TYPE_DATASET = 1
const H5O_TYPE_NAMED_DATATYPE = 2
# Property constants
const H5P_DEFAULT = 0
const H5P_OBJECT_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_OBJECT_CREATE_ID_g : :H5P_CLS_OBJECT_CREATE_g)
const H5P_FILE_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_FILE_CREATE_ID_g : :H5P_CLS_FILE_CREATE_g)
const H5P_FILE_ACCESS = read_const(libversion >= v"1.8.14" ? :H5P_CLS_FILE_ACCESS_ID_g : :H5P_CLS_FILE_ACCESS_g)
const H5P_DATASET_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_DATASET_CREATE_ID_g : :H5P_CLS_DATASET_CREATE_g)
const H5P_DATASET_ACCESS = read_const(libversion >= v"1.8.14" ? :H5P_CLS_DATASET_ACCESS_ID_g : :H5P_CLS_DATASET_ACCESS_g)
const H5P_DATASET_XFER = read_const(libversion >= v"1.8.14" ? :H5P_CLS_DATASET_XFER_ID_g : :H5P_CLS_DATASET_XFER_g)
const H5P_FILE_MOUNT = read_const(libversion >= v"1.8.14" ? :H5P_CLS_FILE_MOUNT_ID_g : :H5P_CLS_FILE_MOUNT_g)
const H5P_GROUP_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_GROUP_CREATE_ID_g : :H5P_CLS_GROUP_CREATE_g)
const H5P_GROUP_ACCESS = read_const(libversion >= v"1.8.14" ? :H5P_CLS_GROUP_ACCESS_ID_g : :H5P_CLS_GROUP_ACCESS_g)
const H5P_DATATYPE_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_DATATYPE_CREATE_ID_g : :H5P_CLS_DATATYPE_CREATE_g)
const H5P_DATATYPE_ACCESS = read_const(libversion >= v"1.8.14" ? :H5P_CLS_DATATYPE_ACCESS_ID_g : :H5P_CLS_DATATYPE_ACCESS_g)
const H5P_STRING_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_STRING_CREATE_ID_g : :H5P_CLS_STRING_CREATE_g)
const H5P_ATTRIBUTE_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_ATTRIBUTE_CREATE_ID_g : :H5P_CLS_ATTRIBUTE_CREATE_g)
const H5P_OBJECT_COPY = read_const(libversion >= v"1.8.14" ? :H5P_CLS_OBJECT_COPY_ID_g : :H5P_CLS_OBJECT_COPY_g)
const H5P_LINK_CREATE = read_const(libversion >= v"1.8.14" ? :H5P_CLS_LINK_CREATE_ID_g : :H5P_CLS_LINK_CREATE_g)
const H5P_LINK_ACCESS = read_const(libversion >= v"1.8.14" ? :H5P_CLS_LINK_ACCESS_ID_g : :H5P_CLS_LINK_ACCESS_g)
# Reference constants
const H5R_OBJECT = 0
const H5R_DATASET_REGION = 1
const H5R_OBJ_REF_BUF_SIZE = 8
const H5R_DSET_REG_REF_BUF_SIZE = 12
# Dataspace constants
const H5S_ALL = convert(Hid, 0)
const H5S_SCALAR = convert(Hid, 0)
const H5S_SIMPLE = convert(Hid, 1)
const H5S_NULL = convert(Hid, 2)
const H5S_UNLIMITED = typemax(Hsize)
const MAXIMUM_DIM = H5S_UNLIMITED
# Dataspace selection constants
const H5S_SELECT_SET = 0
const H5S_SELECT_OR = 1
const H5S_SELECT_AND = 2
const H5S_SELECT_XOR = 3
const H5S_SELECT_NOTB = 4
const H5S_SELECT_NOTA = 5
const H5S_SELECT_APPEND = 6
const H5S_SELECT_PREPEND = 7
# type classes (C enum H5T_class_t)
const H5T_INTEGER = convert(Hid, 0)
const H5T_FLOAT = convert(Hid, 1)
const H5T_TIME = convert(Hid, 2) # not supported by HDF5 library
const H5T_STRING = convert(Hid, 3)
const H5T_BITFIELD = convert(Hid, 4)
const H5T_OPAQUE = convert(Hid, 5)
const H5T_COMPOUND = convert(Hid, 6)
const H5T_REFERENCE = convert(Hid, 7)
const H5T_ENUM = convert(Hid, 8)
const H5T_VLEN = convert(Hid, 9)
const H5T_ARRAY = convert(Hid, 10)
# Character types
const H5T_CSET_ASCII = 0
const H5T_CSET_UTF8 = 1
# Sign types (C enum H5T_sign_t)
const H5T_SGN_NONE = convert(Cint, 0) # unsigned
const H5T_SGN_2 = convert(Cint, 1) # 2's complement
# Search directions
const H5T_DIR_ASCEND = 1
const H5T_DIR_DESCEND = 2
# String padding modes
const H5T_STR_NULLTERM = 0
const H5T_STR_NULLPAD = 1
const H5T_STR_SPACEPAD = 2
# Other type constants
const H5T_VARIABLE = reinterpret(UInt, -1)
# Type_id constants (LE = little endian, I16 = Int16, etc)
const H5T_STD_I8LE = read_const(:H5T_STD_I8LE_g)
const H5T_STD_I8BE = read_const(:H5T_STD_I8BE_g)
const H5T_STD_U8LE = read_const(:H5T_STD_U8LE_g)
const H5T_STD_U8BE = read_const(:H5T_STD_U8BE_g)
const H5T_STD_I16LE = read_const(:H5T_STD_I16LE_g)
const H5T_STD_I16BE = read_const(:H5T_STD_I16BE_g)
const H5T_STD_U16LE = read_const(:H5T_STD_U16LE_g)
const H5T_STD_U16BE = read_const(:H5T_STD_U16BE_g)
const H5T_STD_I32LE = read_const(:H5T_STD_I32LE_g)
const H5T_STD_I32BE = read_const(:H5T_STD_I32BE_g)
const H5T_STD_U32LE = read_const(:H5T_STD_U32LE_g)
const H5T_STD_U32BE = read_const(:H5T_STD_U32BE_g)
const H5T_STD_I64LE = read_const(:H5T_STD_I64LE_g)
const H5T_STD_I64BE = read_const(:H5T_STD_I64BE_g)
const H5T_STD_U64LE = read_const(:H5T_STD_U64LE_g)
const H5T_STD_U64BE = read_const(:H5T_STD_U64BE_g)
const H5T_IEEE_F32LE = read_const(:H5T_IEEE_F32LE_g)
const H5T_IEEE_F32BE = read_const(:H5T_IEEE_F32BE_g)
const H5T_IEEE_F64LE = read_const(:H5T_IEEE_F64LE_g)
const H5T_IEEE_F64BE = read_const(:H5T_IEEE_F64BE_g)
const H5T_C_S1 = read_const(:H5T_C_S1_g)
const H5T_STD_REF_OBJ = read_const(:H5T_STD_REF_OBJ_g)
const H5T_STD_REF_DSETREG = read_const(:H5T_STD_REF_DSETREG_g)
# Native types
const H5T_NATIVE_INT8 = read_const(:H5T_NATIVE_INT8_g)
const H5T_NATIVE_UINT8 = read_const(:H5T_NATIVE_UINT8_g)
const H5T_NATIVE_INT16 = read_const(:H5T_NATIVE_INT16_g)
const H5T_NATIVE_UINT16 = read_const(:H5T_NATIVE_UINT16_g)
const H5T_NATIVE_INT32 = read_const(:H5T_NATIVE_INT32_g)
const H5T_NATIVE_UINT32 = read_const(:H5T_NATIVE_UINT32_g)
const H5T_NATIVE_INT64 = read_const(:H5T_NATIVE_INT64_g)
const H5T_NATIVE_UINT64 = read_const(:H5T_NATIVE_UINT64_g)
const H5T_NATIVE_FLOAT = read_const(:H5T_NATIVE_FLOAT_g)
const H5T_NATIVE_DOUBLE = read_const(:H5T_NATIVE_DOUBLE_g)
# Library versions
const H5F_LIBVER_EARLIEST = 0
const H5F_LIBVER_LATEST = 1
# Object reference types
immutable HDF5ReferenceObj
r::UInt64 # Size must be H5R_OBJ_REF_BUF_SIZE
end
const HDF5ReferenceObj_NULL = HDF5ReferenceObj(UInt64(0))
## Conversion between Julia types and HDF5 atomic types
hdf5_type_id(::Type{Int8}) = H5T_NATIVE_INT8
hdf5_type_id(::Type{UInt8}) = H5T_NATIVE_UINT8
hdf5_type_id(::Type{Int16}) = H5T_NATIVE_INT16
hdf5_type_id(::Type{UInt16}) = H5T_NATIVE_UINT16
hdf5_type_id(::Type{Int32}) = H5T_NATIVE_INT32
hdf5_type_id(::Type{UInt32}) = H5T_NATIVE_UINT32
hdf5_type_id(::Type{Int64}) = H5T_NATIVE_INT64
hdf5_type_id(::Type{UInt64}) = H5T_NATIVE_UINT64
hdf5_type_id(::Type{Float32}) = H5T_NATIVE_FLOAT
hdf5_type_id(::Type{Float64}) = H5T_NATIVE_DOUBLE
hdf5_type_id(::Type{HDF5ReferenceObj}) = H5T_STD_REF_OBJ
const HDF5BitsKind = Union{Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Float32, Float64}
const HDF5Scalar = Union{HDF5BitsKind, HDF5ReferenceObj}
const ScalarOrString = Union{HDF5Scalar, String}
# It's not safe to use particular id codes because these can change, so we use characteristics of the type.
const hdf5_type_map = Dict(
(H5T_INTEGER, H5T_SGN_2, convert(Csize_t, 1)) => Int8,
(H5T_INTEGER, H5T_SGN_2, convert(Csize_t, 2)) => Int16,
(H5T_INTEGER, H5T_SGN_2, convert(Csize_t, 4)) => Int32,
(H5T_INTEGER, H5T_SGN_2, convert(Csize_t, 8)) => Int64,
(H5T_INTEGER, H5T_SGN_NONE, convert(Csize_t, 1)) => UInt8,
(H5T_INTEGER, H5T_SGN_NONE, convert(Csize_t, 2)) => UInt16,
(H5T_INTEGER, H5T_SGN_NONE, convert(Csize_t, 4)) => UInt32,
(H5T_INTEGER, H5T_SGN_NONE, convert(Csize_t, 8)) => UInt64,
(H5T_FLOAT, nothing, convert(Csize_t, 4)) => Float32,
(H5T_FLOAT, nothing, convert(Csize_t, 8)) => Float64,
)
hdf5_type_id{S<:AbstractString}(::Type{S}) = H5T_C_S1
# Single character types
# These are needed to safely handle VLEN objects
@compat abstract type CharType <: AbstractString end
type ASCIIChar<:CharType
c::UInt8
end
length(c::ASCIIChar) = 1
type UTF8Char<:CharType
c::UInt8
end
length(c::UTF8Char) = 1
chartype(::Type{Compat.ASCIIString}) = ASCIIChar
stringtype(::Type{ASCIIChar}) = Compat.ASCIIString
stringtype(::Type{UTF8Char}) = Compat.UTF8String
cset(::Type{Compat.UTF8String}) = H5T_CSET_UTF8
cset(::Type{UTF8Char}) = H5T_CSET_UTF8
cset(::Type{ASCIIChar}) = H5T_CSET_ASCII
hdf5_type_id{C<:CharType}(::Type{C}) = H5T_C_S1
## HDF5 uses a plain integer to refer to each file, group, or
## dataset. These are wrapped into special types in order to allow
## method dispatch.
# Note re finalizers: we use them to ensure that objects passed back
# to the user will eventually be cleaned up properly. However, since
# finalizers don't run on a predictable schedule, we also call close
# directly on function exit. (This avoids certain problems, like those
# that occur when passing a freshly-created file to some other
# application).
# This defines an "unformatted" HDF5 data file. Formatted files are defined in separate modules.
type HDF5File <: DataFile
id::Hid
filename::Compat.UTF8String
function HDF5File(id, filename, toclose::Bool=true)
f = new(id, filename)
if toclose
finalizer(f, close)
end
f
end
end
convert(::Type{Hid}, f::HDF5File) = f.id
show(io::IO, fid::HDF5File) = isvalid(fid) ? print(io, "HDF5 data file: ", fid.filename) : print(io, "Closed HFD5 data file: ", fid.filename)
type HDF5Group <: DataFile
id::Hid
file::HDF5File # the parent file
function HDF5Group(id, file)
g = new(id, file)
finalizer(g, close)
g
end
end
convert(::Type{Hid}, g::HDF5Group) = g.id
show(io::IO, g::HDF5Group) = isvalid(g) ? print(io, "HDF5 group: ", name(g), " (file: ", g.file.filename, ")") : print(io, "HDF5 group (invalid)")
type HDF5Dataset
id::Hid
file::HDF5File
function HDF5Dataset(id, file)
dset = new(id, file)
finalizer(dset, close)
dset
end
end
convert(::Type{Hid}, dset::HDF5Dataset) = dset.id
show(io::IO, dset::HDF5Dataset) = isvalid(dset) ? print(io, "HDF5 dataset: ", name(dset), " (file: ", dset.file.filename, ")") : print(io, "HDF5 dataset (invalid)")
type HDF5Datatype
id::Hid
toclose::Bool
file::HDF5File
function HDF5Datatype(id, toclose::Bool=true)
nt = new(id, toclose)
if toclose
finalizer(nt, close)
end
nt
end
function HDF5Datatype(id, file::HDF5File, toclose::Bool=true)
nt = new(id, toclose, file)
if toclose
finalizer(nt, close)
end
nt
end
end
convert(::Type{Hid}, dtype::HDF5Datatype) = dtype.id
show(io::IO, dtype::HDF5Datatype) = print(io, "HDF5 datatype ", dtype.id) # TODO: compound datatypes?
hash(dtype::HDF5Datatype, h::UInt) =
(dtype.id % UInt + h) ^ (0xadaf9b66bc962084 % UInt)
==(dt1::HDF5Datatype, dt2::HDF5Datatype) = h5t_equal(dt1, dt2) > 0
# Define an H5O Object type
const HDF5Object = Union{HDF5Group, HDF5Dataset, HDF5Datatype}
type HDF5Dataspace
id::Hid
function HDF5Dataspace(id)
dspace = new(id)
finalizer(dspace, close)
dspace
end
end
convert(::Type{Hid}, dspace::HDF5Dataspace) = dspace.id
type HDF5Attribute
id::Hid
file::HDF5File
function HDF5Attribute(id, file)
dset = new(id, file)
finalizer(dset, close)
dset
end
end
convert(::Type{Hid}, attr::HDF5Attribute) = attr.id
show(io::IO, attr::HDF5Attribute) = isvalid(attr) ? print(io, "HDF5 attribute: ", name(attr)) : print(io, "HDF5 attribute (invalid)")
type HDF5Attributes
parent::Union{HDF5File, HDF5Group, HDF5Dataset}
end
attrs(p::Union{HDF5File, HDF5Group, HDF5Dataset}) = HDF5Attributes(p)
type HDF5Properties
id::Hid
toclose::Bool
function HDF5Properties(id, toclose::Bool=true)
p = new(id, toclose)
if toclose
finalizer(p, close)
end
p
end
end
HDF5Properties() = HDF5Properties(H5P_DEFAULT)
convert(::Type{Hid}, p::HDF5Properties) = p.id
# Methods for reference types
const REF_TEMP_ARRAY = Ref{HDF5ReferenceObj}()
function HDF5ReferenceObj(parent::Union{HDF5File, HDF5Group, HDF5Dataset}, name::String)
h5r_create(REF_TEMP_ARRAY, checkvalid(parent).id, name, H5R_OBJECT, -1)
REF_TEMP_ARRAY[]
end
==(a::HDF5ReferenceObj, b::HDF5ReferenceObj) = a.r == b.r
hash(x::HDF5ReferenceObj, h::UInt) = hash(x.r, h)
# Compound types
immutable HDF5Compound{N}
data::NTuple{N,Any}
membername::NTuple{N,Compat.ASCIIString}
membertype::NTuple{N,Type}
end
# Opaque types
type HDF5Opaque
data
tag::Compat.ASCIIString
end
# An empty array type
type EmptyArray{T}; end
# Stub types to encode fixed-size arrays for H5T_ARRAY
immutable FixedArray{T,D}; end
size{T,D}(::Type{FixedArray{T,D}}) = D
eltype{T,D}(::Type{FixedArray{T,D}}) = T
# VLEN objects
type HDF5Vlen{T}
data
end
HDF5Vlen{S<:String}(strs::Array{S}) = HDF5Vlen{chartype(S)}(strs)
HDF5Vlen{T<:HDF5Scalar}(A::Array{Array{T}}) = HDF5Vlen{T}(A)
HDF5Vlen{T<:HDF5Scalar,N}(A::Array{Array{T,N}}) = HDF5Vlen{T}(A)
## Types that correspond to C structs and get used for ccall
# For VLEN
immutable Hvl_t
len::Csize_t
p::Ptr{Void}
end
const HVL_SIZE = sizeof(Hvl_t) # and determine the size of the buffer needed
function vlenpack{T<:Union{HDF5Scalar,CharType}}(v::HDF5Vlen{T})
len = length(v.data)
Tp = t2p(T) # Ptr{UInt8} or Ptr{T}
h = Vector{Hvl_t}(len)
for i = 1:len
h[i] = Hvl_t(convert(Csize_t, length(v.data[i])), convert(Ptr{Void}, unsafe_convert(Tp, v.data[i])))
end
h
end
# For group information
immutable H5Ginfo
storage_type::Cint
nlinks::Hsize
max_corder::Int64
mounted::Cint
end
# For objects
immutable Hmetainfo
index_size::Hsize
heap_size::Hsize
end
immutable H5Oinfo
fileno::Cuint
addr::Hsize
otype::Cint
rc::Cuint
atime::C_time_t
mtime::C_time_t
ctime::C_time_t
btime::C_time_t
num_attrs::Hsize
version::Cuint
nmesgs::Cuint
nchunks::Cuint
flags::Cuint
total::Hsize
meta::Hsize
mesg::Hsize
free::Hsize
present::UInt64
shared::UInt64
meta_obj::Hmetainfo
meta_attr::Hmetainfo
end
# For links
immutable H5LInfo
linktype::Cint
corder_valid::Cuint
corder::Int64
cset::Cint
u::UInt64
end
# Blosc compression:
include("blosc_filter.jl")
# heuristic chunk layout (return empty array to disable chunking)
function heuristic_chunk(T, shape)
Ts = sizeof(T)
sz = prod(shape)
sz == 0 && return Int[] # never return a zero-size chunk
chunk = [shape...]
nd = length(chunk)
# simplification of ugly heuristic target chunk size from PyTables/h5py:
target = min(1500000, max(12000, floor(Int, 300*cbrt(Ts*sz))))
Ts > target && return ones(chunk)
# divide last non-unit dimension by 2 until we get <= target
# (since Julia default to column-major, favor contiguous first dimension)
while Ts*prod(chunk) > target
i = nd
while chunk[i] == 1
i -= 1
end
chunk[i] >>= 1
end
return chunk
end
heuristic_chunk{T}(A::AbstractArray{T}) = heuristic_chunk(T, size(A))
heuristic_chunk(x) = Int[]
# (strings are saved as scalars, and hence cannot be chunked)
### High-level interface ###
# Open or create an HDF5 file
function h5open(filename::AbstractString, rd::Bool, wr::Bool, cr::Bool, tr::Bool, ff::Bool,
cpl::HDF5Properties=DEFAULT_PROPERTIES, apl::HDF5Properties=DEFAULT_PROPERTIES)
if ff && !wr
error("HDF5 does not support appending without writing")
end
close_apl = false
if apl.id == H5P_DEFAULT
apl = p_create(H5P_FILE_ACCESS, false)
close_apl = true
# With garbage collection, the other modes don't make sense
apl["fclose_degree"] = H5F_CLOSE_STRONG
end
if cr && (tr || !isfile(filename))
fid = h5f_create(filename, H5F_ACC_TRUNC, cpl.id, apl.id)
else
if !h5f_is_hdf5(filename)
error("This does not appear to be an HDF5 file")
end
fid = h5f_open(filename, wr ? H5F_ACC_RDWR : H5F_ACC_RDONLY, apl.id)
end
if close_apl
# Close properties manually to avoid errors when the file is
# closed before the properties are gc'ed
close(apl)
end
HDF5File(fid, filename)
end
function h5open(filename::AbstractString, mode::AbstractString="r", pv...)
p = p_create(H5P_FILE_ACCESS)
# With garbage collection, the other modes don't make sense
# (Set this first, so that the user-passed properties can overwrite this.)
p["fclose_degree"] = H5F_CLOSE_STRONG
for i = 1:2:length(pv)
thisname = pv[i]
if !isa(thisname, Compat.ASCIIString)
error("Argument ", i+2, " should be a String, but it's a ", typeof(thisname))
end
p[thisname] = pv[i+1]
end
modes =
mode == "r" ? (true, false, false, false, false) :
mode == "r+" ? (true, true, false, false, true ) :
mode == "w" ? (false, true, true, true, false) :
# mode == "w+" ? (true, true, true, true, false) :
# mode == "a" ? (true, true, true, true, true ) :
error("invalid open mode: ", mode)
h5open(filename, modes..., DEFAULT_PROPERTIES, p)
end
function h5open(f::Function, args...)
fid = h5open(args...)
try
f(fid)
finally
close(fid)
end
end
function h5rewrite(f::Function, filename::AbstractString, args...)
tmppath,tmpio = mktemp(dirname(filename))
close(tmpio)
try
val = h5open(f, tmppath, "w", args...)
Base.Filesystem.rename(tmppath, filename)
return val
catch
Base.Filesystem.unlink(tmppath)
rethrow()
end
end
function h5write(filename, name::String, data)
fid = h5open(filename, true, true, true, false, true)
try
write(fid, name, data)
finally
close(fid)
end
end
function h5read(filename, name::String)
local dat
fid = h5open(filename, "r")
try
dat = read(fid, name)
finally
close(fid)
end
dat
end
function h5read(filename, name::String, indices::Tuple{Vararg{Union{Range{Int},Int,Colon}}})
local dat
fid = h5open(filename, "r")
try
dset = fid[name]
dat = dset[indices...]
finally
close(fid)
end
dat
end
function h5writeattr(filename, name::String, data::Dict)
fid = h5open(filename, true, true, true, false, true)
try
for x in keys(data)
attrs(fid[name])[x] = data[x]
end
finally
close(fid)
end
end
function h5readattr(filename, name::String)
local dat
fid = h5open(filename,"r")
try
a = attrs(fid[name])
dat = Dict(x => read(a[x]) for x in names(a))
finally
close(fid)
end
dat
end
# Ensure that objects haven't been closed
isvalid(obj::Union{HDF5File, HDF5Properties, HDF5Datatype, HDF5Dataspace}) = obj.id != -1 && h5i_is_valid(obj.id)
isvalid(obj::Union{HDF5Group, HDF5Dataset, HDF5Attribute}) = obj.id != -1 && obj.file.id != -1 && h5i_is_valid(obj.id)
checkvalid(obj) = isvalid(obj) ? obj : error("File or object has been closed")
# Close functions
# Close functions that should try calling close regardless
function close(obj::HDF5File)
if obj.id != -1
h5f_close(obj.id)
obj.id = -1
end
nothing
end
for (h5type, h5func) in
((:(Union{HDF5Group, HDF5Dataset}), :h5o_close),
(:HDF5Attribute, :h5a_close))
# Close functions that should first check that the file is still open. The common case is a
# file that has been closed with CLOSE_STRONG but there are still finalizers that have not run
# for the datasets, etc, in the file.
@eval begin
function close(obj::$h5type)
if obj.id != -1
if obj.file.id != -1 && isvalid(obj)
$h5func(obj.id)
end
obj.id = -1
end
nothing
end
end
end
function close(obj::HDF5Datatype)
if obj.toclose && obj.id != -1
if (!isdefined(obj, :file) || obj.file.id != -1) && isvalid(obj)
h5o_close(obj.id)
end
obj.id = -1
end
nothing
end
function close(obj::HDF5Dataspace)
if obj.id != -1
if isvalid(obj)
h5s_close(obj.id)
end
obj.id = -1
end
nothing
end
function close(obj::HDF5Properties)
if obj.toclose && obj.id != -1
h5p_close(obj.id)
obj.id = -1
end
nothing
end
# Testing file type
ishdf5(name::AbstractString) = h5f_is_hdf5(name)
# Extract the file
file(f::HDF5File) = f
file(g::HDF5Group) = g.file
file(dset::HDF5Dataset) = dset.file
file(dtype::HDF5Datatype) = dtype.file
file(a::HDF5Attribute) = a.file
fd(obj::HDF5Object) = h5i_get_file_id(checkvalid(obj).id)
# Flush buffers
flush(f::Union{HDF5Object, HDF5Attribute, HDF5Datatype, HDF5File}, scope) = h5f_flush(checkvalid(f).id, scope)
flush(f::Union{HDF5Object, HDF5Attribute, HDF5Datatype, HDF5File}) = flush(f, H5F_SCOPE_GLOBAL)
# Open objects
g_open(parent::Union{HDF5File, HDF5Group}, name::String) = HDF5Group(h5g_open(checkvalid(parent).id, name, H5P_DEFAULT), file(parent))
d_open(parent::Union{HDF5File, HDF5Group}, name::String, apl::HDF5Properties) = HDF5Dataset(h5d_open(checkvalid(parent).id, name, apl.id), file(parent))
d_open(parent::Union{HDF5File, HDF5Group}, name::String) = HDF5Dataset(h5d_open(checkvalid(parent).id, name, H5P_DEFAULT), file(parent))
t_open(parent::Union{HDF5File, HDF5Group}, name::String, apl::HDF5Properties) = HDF5Datatype(h5t_open(checkvalid(parent).id, name, apl.id), file(parent))
t_open(parent::Union{HDF5File, HDF5Group}, name::String) = HDF5Datatype(h5t_open(checkvalid(parent).id, name, H5P_DEFAULT), file(parent))
a_open(parent::Union{HDF5File, HDF5Object}, name::String) = HDF5Attribute(h5a_open(checkvalid(parent).id, name, H5P_DEFAULT), file(parent))
# Object (group, named datatype, or dataset) open
function h5object(obj_id::Hid, parent)
obj_type = h5i_get_type(obj_id)
obj_type == H5I_GROUP ? HDF5Group(obj_id, file(parent)) :
obj_type == H5I_DATATYPE ? HDF5Datatype(obj_id, file(parent)) :
obj_type == H5I_DATASET ? HDF5Dataset(obj_id, file(parent)) :
error("Invalid object type for path ", path)
end
o_open(parent, path::String) = h5object(h5o_open(checkvalid(parent).id, path), parent)
# Get the root group
root(h5file::HDF5File) = g_open(h5file, "/")
root(obj::Union{HDF5Group, HDF5Dataset}) = g_open(file(obj), "/")
# getindex syntax: obj2 = obj1[path]
getindex(parent::Union{HDF5File, HDF5Group}, path::String) = o_open(parent, path)
getindex(dset::HDF5Dataset, name::String) = a_open(dset, name)
getindex(x::HDF5Attributes, name::String) = a_open(x.parent, name)
# Path manipulation
function joinpathh5(a::String, b::String)
isempty(a) && return b
isempty(b) && return a
endswith(a, '/') && beginswith(b, '/') && return a * b[2:end]
(endswith(a, '/') || beginswith(b, '/')) && return a * b
return a*"/"*b
end
joinpathh5(a::String, b::String, c::String) = joinpathh5(joinpathh5(a, b), c)
function split1(path::String)
off = search(path, '/')
if off == 0
return path, nothing
else
if off == 1
# Matches the root group
return "/", path[2:end]
else
return path[1:prevind(path, off)], path[nextind(path, off):end]
end
end
end
function g_create(parent::Union{HDF5File, HDF5Group}, path::String,
lcpl::HDF5Properties=_link_properties(path),
dcpl::HDF5Properties=DEFAULT_PROPERTIES)
HDF5Group(h5g_create(checkvalid(parent).id, path, lcpl.id, dcpl.id), file(parent))
end
function g_create(f::Function, parent::Union{HDF5File, HDF5Group}, args...)
g = g_create(parent, args...)
try
f(g)
finally
close(g)
end
end
function d_create(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype,
dspace::HDF5Dataspace, lcpl::HDF5Properties=_link_properties(path),
dcpl::HDF5Properties=DEFAULT_PROPERTIES,
dapl::HDF5Properties=DEFAULT_PROPERTIES)
HDF5Dataset(h5d_create(checkvalid(parent).id, path, dtype.id, dspace.id, lcpl.id,
dcpl.id, dapl.id), file(parent))
end
# Setting dset creation properties with name/value pairs
function d_create(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype, dspace::HDF5Dataspace, prop1::String, val1, pv...)
if !iseven(length(pv))
error("Properties and values must come in pairs")
end
p = p_create(H5P_DATASET_CREATE)
p[prop1] = val1
for i = 1:2:length(pv)
thisname = pv[i]
if !isa(thisname, String)
error("Argument ", i+3, " should be a String, but it's a ", typeof(thisname))
end
p[thisname] = pv[i+1]
end
HDF5Dataset(h5d_create(parent, path, dtype.id, dspace.id, _link_properties(path), p.id, H5P_DEFAULT), file(parent))
end
d_create(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype, dspace_dims::Dims, prop1::String, val1, pv...) = d_create(checkvalid(parent), path, dtype, dataspace(dspace_dims), prop1, val1, pv...)
d_create(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype, dspace_dims::Tuple{Dims,Dims}, prop1::String, val1, pv...) = d_create(checkvalid(parent), path, dtype, dataspace(dspace_dims[1], max_dims=dspace_dims[2]), prop1, val1, pv...)
d_create(parent::Union{HDF5File, HDF5Group}, path::String, dtype::Type, dspace_dims, prop1::String, val1, pv...) = d_create(checkvalid(parent), path, datatype(dtype), dataspace(dspace_dims[1], max_dims=dspace_dims[2]), prop1, val1, pv...)
# Note that H5Tcreate is very different; H5Tcommit is the analog of these others
t_create(class_id, sz) = HDF5Datatype(h5t_create(class_id, sz))
function t_commit(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype, lcpl::HDF5Properties, tcpl::HDF5Properties, tapl::HDF5Properties)
h5p_set_char_encoding(lcpl.id, cset(typeof(path)))
h5t_commit(checkvalid(parent).id, path, dtype.id, lcpl.id, tcpl.id, tapl.id)
dtype.file = file(parent)
dtype
end
function t_commit(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype, lcpl::HDF5Properties, tcpl::HDF5Properties)
h5p_set_char_encoding(lcpl.id, cset(typeof(path)))
h5t_commit(checkvalid(parent).id, path, dtype.id, lcpl.id, tcpl.id, H5P_DEFAULT)
dtype.file = file(parent)
dtype
end
function t_commit(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype, lcpl::HDF5Properties)
h5p_set_char_encoding(lcpl.id, cset(typeof(path)))
h5t_commit(checkvalid(parent).id, path, dtype.id, lcpl.id, H5P_DEFAULT, H5P_DEFAULT)
dtype.file = file(parent)
dtype
end
t_commit(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype) = t_commit(parent, path, dtype, p_create(H5P_LINK_CREATE))
a_create(parent::Union{HDF5File, HDF5Object}, name::String, dtype::HDF5Datatype, dspace::HDF5Dataspace) = HDF5Attribute(h5a_create(checkvalid(parent).id, name, dtype.id, dspace.id), file(parent))
p_create(class, toclose=false) = HDF5Properties(h5p_create(class), toclose)
# Delete objects
a_delete(parent::Union{HDF5File, HDF5Object}, path::String) = h5a_delete(checkvalid(parent).id, path)
o_delete(parent::Union{HDF5File, HDF5Group}, path::String, lapl::HDF5Properties) = h5l_delete(checkvalid(parent).id, path, lapl.id)
o_delete(parent::Union{HDF5File, HDF5Group}, path::String) = h5l_delete(checkvalid(parent).id, path, H5P_DEFAULT)
o_delete(obj::HDF5Object) = o_delete(parent(obj), ascii(split(name(obj),"/")[end]))
# Copy objects
o_copy(src_parent::Union{HDF5File, HDF5Group}, src_path::String, dst_parent::Union{HDF5File, HDF5Group}, dst_path::String) = h5o_copy(checkvalid(src_parent).id, src_path, checkvalid(dst_parent).id, dst_path, H5P_DEFAULT, _link_properties(dst_path))
o_copy(src_obj::HDF5Object, dst_parent::Union{HDF5File, HDF5Group}, dst_path::String) = h5o_copy(checkvalid(src_obj).id, ".", checkvalid(dst_parent).id, dst_path, H5P_DEFAULT, _link_properties(dst_path))
# Assign syntax: obj[path] = value
# Creates a dataset unless obj is a dataset, in which case it creates an attribute
setindex!(parent::Union{HDF5File, HDF5Group}, val, path::String) = write(parent, path, val)
setindex!(dset::HDF5Dataset, val, name::String) = a_write(dset, name, val)
setindex!(x::HDF5Attributes, val, name::String) = a_write(x.parent, name, val)
# Getting and setting properties: p["chunk"] = dims, p["compress"] = 6
function setindex!(p::HDF5Properties, val, name::String)
funcget, funcset = hdf5_prop_get_set[name]
funcset(p, val...)
return p
end
# Create a dataset with properties: obj[path, prop1, set1, ...] = val
function setindex!(parent::Union{HDF5File, HDF5Group}, val, path::String, prop1::String, val1, pv...)
if !iseven(length(pv))
error("Properties and values must come in pairs")
end
p = p_create(H5P_DATASET_CREATE)
need_chunks = prop1 in chunked_props
have_chunks = prop1 == "chunk"
chunk = heuristic_chunk(val)
# ignore chunked_props (== compression) for empty datasets (issue #246):
if !(need_chunks && isempty(chunk))
p[prop1] = val1
end
for i = 1:2:length(pv)
thisname = pv[i]
if !isa(thisname, String)
error("Argument ", i+3, " should be an String, but it's a ", typeof(thisname))
end
thisneeds_chunks = thisname in chunked_props
if !(thisneeds_chunks && isempty(chunk))
p[thisname] = pv[i+1]
end
need_chunks = need_chunks || thisneeds_chunks
have_chunks = have_chunks || (thisname == "chunk")
end
if need_chunks && !have_chunks
if !isempty(chunk)
p["chunk"] = chunk
end
end
write(parent, path, val, p_create(H5P_LINK_CREATE), p)
end
# Check existence
function exists(parent::Union{HDF5File, HDF5Group}, path::String, lapl::HDF5Properties)
first, rest = split1(path)
if first == "/"
parent = root(parent)
elseif !h5l_exists(parent.id, first, lapl.id)
return false
end
ret = true
if !(rest === nothing) && !isempty(rest)
obj = parent[first]
ret = exists(obj, rest, lapl)
close(obj)
end
ret
end
exists(attr::HDF5Attributes, path::String) = h5a_exists(checkvalid(attr.parent).id, path)
exists(dset::Union{HDF5Dataset, HDF5Datatype}, path::String) = h5a_exists(checkvalid(dset).id, path)
exists(parent::Union{HDF5File, HDF5Group}, path::String) = exists(parent, path, HDF5Properties())
has(parent::Union{HDF5File, HDF5Group, HDF5Dataset}, path::String) = exists(parent, path)
# Querying items in the file
const H5GINFO_TEMP_ARRAY = Ref{H5Ginfo}()
function info(obj::Union{HDF5Group,HDF5File})
h5g_get_info(obj, H5GINFO_TEMP_ARRAY)
H5GINFO_TEMP_ARRAY[]
end
const H5OINFO_TEMP_ARRAY = Ref{H5Oinfo}()
function objinfo(obj::Union{HDF5File, HDF5Object})
h5o_get_info(obj.id, H5OINFO_TEMP_ARRAY)
H5OINFO_TEMP_ARRAY[]
end
const LENGTH_TEMP_ARRAY = Ref{UInt64}()
function length(x::Union{HDF5Group,HDF5File})
h5g_get_num_objs(x.id, LENGTH_TEMP_ARRAY)
LENGTH_TEMP_ARRAY[]
end
function length(x::HDF5Attributes)
objinfo(x.parent).num_attrs
end
isempty(x::Union{HDF5Dataset,HDF5Group,HDF5File}) = length(x) == 0
function size(obj::Union{HDF5Dataset, HDF5Attribute})
dspace = dataspace(obj)
dims, maxdims = get_dims(dspace)
close(dspace)
convert(Tuple{Vararg{Int}}, dims)
end
size(dset::Union{HDF5Dataset, HDF5Attribute}, d) = d > ndims(dset) ? 1 : size(dset)[d]
length(dset::Union{HDF5Dataset, HDF5Attribute}) = prod(size(dset))
ndims(dset::Union{HDF5Dataset, HDF5Attribute}) = length(size(dset))
function eltype(dset::Union{HDF5Dataset, HDF5Attribute})
T = Any
dtype = datatype(dset)
try
T = hdf5_to_julia_eltype(dtype)
finally
close(dtype)
end
T
end
function isnull(obj::Union{HDF5Dataset, HDF5Attribute})
dspace = dataspace(obj)
ret = h5s_get_simple_extent_type(dspace.id) == H5S_NULL
close(dspace)
ret
end
# filename and name
filename(obj::Union{HDF5File, HDF5Group, HDF5Dataset, HDF5Attribute, HDF5Datatype}) = h5f_get_name(checkvalid(obj).id)
name(obj::Union{HDF5File, HDF5Group, HDF5Dataset, HDF5Datatype}) = h5i_get_name(checkvalid(obj).id)
name(attr::HDF5Attribute) = h5a_get_name(attr.id)
function names(x::Union{HDF5Group,HDF5File})
checkvalid(x)
n = length(x)
res = Vector{String}(n)
buf = Vector{UInt8}(100)
for i in 1:n
len = h5g_get_objname_by_idx(x.id, i - 1, buf, length(buf))
if len >= length(buf)
resize!(buf, len+10)
len = h5g_get_objname_by_idx(x.id, i - 1, buf, length(buf))
end
res[i] = String(buf[1:len])
end
res