@@ -270,18 +270,27 @@ const HDF5Scalar = Union{HDF5BitsKind, HDF5ReferenceObj}
270
270
const ScalarOrString = Union{HDF5Scalar, String}
271
271
272
272
# It's not safe to use particular id codes because these can change, so we use characteristics of the type.
273
- const hdf5_type_map = Dict (
274
- (H5T_INTEGER, H5T_SGN_2, Csize_t (1 )) => Int8,
275
- (H5T_INTEGER, H5T_SGN_2, Csize_t (2 )) => Int16,
276
- (H5T_INTEGER, H5T_SGN_2, Csize_t (4 )) => Int32,
277
- (H5T_INTEGER, H5T_SGN_2, Csize_t (8 )) => Int64,
278
- (H5T_INTEGER, H5T_SGN_NONE, Csize_t (1 )) => UInt8,
279
- (H5T_INTEGER, H5T_SGN_NONE, Csize_t (2 )) => UInt16,
280
- (H5T_INTEGER, H5T_SGN_NONE, Csize_t (4 )) => UInt32,
281
- (H5T_INTEGER, H5T_SGN_NONE, Csize_t (8 )) => UInt64,
282
- (H5T_FLOAT, nothing , Csize_t (4 )) => Float32,
283
- (H5T_FLOAT, nothing , Csize_t (8 )) => Float64,
284
- )
273
+ function _hdf5_type_map (class_id, is_signed, native_size)
274
+ if class_id == H5T_INTEGER
275
+ if is_signed == H5T_SGN_2
276
+ return native_size === Csize_t (1 ) ? Int8 :
277
+ native_size === Csize_t (2 ) ? Int16 :
278
+ native_size === Csize_t (4 ) ? Int32 :
279
+ native_size === Csize_t (8 ) ? Int64 :
280
+ throw (KeyError (class_id, is_signed, native_size))
281
+ else
282
+ return native_size === Csize_t (1 ) ? UInt8 :
283
+ native_size === Csize_t (2 ) ? UInt16 :
284
+ native_size === Csize_t (4 ) ? UInt32 :
285
+ native_size === Csize_t (8 ) ? UInt64 :
286
+ throw (KeyError (class_id, is_signed, native_size))
287
+ end
288
+ else
289
+ return native_size === Csize_t (4 ) ? Float32 :
290
+ native_size === Csize_t (8 ) ? Float64 :
291
+ throw (KeyError (class_id, is_signed, native_size))
292
+ end
293
+ end
285
294
286
295
hdf5_type_id (:: Type{S} ) where {S<: AbstractString } = H5T_C_S1
287
296
@@ -889,16 +898,19 @@ root(obj::Union{HDF5Group, HDF5Dataset}) = g_open(file(obj), "/")
889
898
getindex (dset:: HDF5Dataset , name:: String ) = a_open (dset, name)
890
899
getindex (x:: HDF5Attributes , name:: String ) = a_open (x. parent, name)
891
900
892
- const hdf5_obj_open = Dict (
893
- H5I_DATASET => (d_open, H5P_DATASET_ACCESS, H5P_DATASET_XFER),
894
- H5I_DATATYPE => (t_open, H5P_DATATYPE_ACCESS),
895
- H5I_GROUP => (g_open, H5P_GROUP_ACCESS),
896
- )
897
901
function getindex (parent:: Union{HDF5File, HDF5Group} , path:: String ; pv... )
898
902
objtype = gettype (parent, path)
899
- fun, propids = hdf5_obj_open[objtype]
900
- props = [p_create (prop; pv... ) for prop in propids]
901
- obj = fun (parent, path, props... )
903
+ if objtype == H5I_DATASET
904
+ dapl = p_create (H5P_DATASET_ACCESS; pv... )
905
+ dxpl = p_create (H5P_DATASET_XFER; pv... )
906
+ return d_open (parent, path, dapl, dxpl)
907
+ elseif objtype == H5I_GROUP
908
+ gapl = p_create (H5P_GROUP_ACCESS; pv... )
909
+ return g_open (parent, path, gapl)
910
+ else # if objtype == H5I_DATATYPE # only remaining choice
911
+ tapl = p_create (H5P_DATATYPE_ACCESS; pv... )
912
+ return t_open (parent, path, tapl)
913
+ end
902
914
end
903
915
904
916
# Path manipulation
@@ -971,19 +983,62 @@ end
971
983
t_commit (parent:: Union{HDF5File, HDF5Group} , path:: String , dtype:: HDF5Datatype ) = t_commit (parent, path, dtype, p_create (H5P_LINK_CREATE))
972
984
973
985
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))
986
+
987
+ function _prop_set! (p:: HDF5Properties , name:: Symbol , val, check:: Bool = true )
988
+ class = p. class
989
+
990
+ if class == H5P_FILE_CREATE
991
+ return name === :userblock ? h5p_set_userblock (p, val... ) :
992
+ name === :track_times ? h5p_set_obj_track_times (p, val... ) : # H5P_OBJECT_CREATE
993
+ check ? error (" unknown file create property " , name) : nothing
994
+ end
995
+
996
+ if class == H5P_FILE_ACCESS
997
+ return name === :alignment ? h5p_set_alignment (p, val... ) :
998
+ name === :fclose_degree ? h5p_set_fclose_degree (p, val... ) :
999
+ name === :libver_bounds ? h5p_set_libver_bounds (p, val... ) :
1000
+ check ? error (" unknown file access property " , name) : nothing
1001
+ end
1002
+
1003
+ if class == H5P_GROUP_CREATE
1004
+ return name === :local_heap_size_hint ? h5p_set_local_heap_size_hint (p, val... ) :
1005
+ name === :track_times ? h5p_set_obj_track_times (p, val... ) : # H5P_OBJECT_CREATE
1006
+ check ? error (" unknown group create property " , name) : nothing
1007
+ end
1008
+
1009
+ if class == H5P_LINK_CREATE
1010
+ return name === :char_encoding ? h5p_set_char_encoding (p, val... ) :
1011
+ name === :create_intermediate_group ? h5p_set_create_intermediate_group (p, val... ) :
1012
+ check ? error (" unknown link create property " , name) : nothing
1013
+ end
1014
+
1015
+ if class == H5P_DATASET_CREATE
1016
+ return name === :alloc_time ? h5p_set_alloc_time (p, val... ) :
1017
+ name === :blosc ? h5p_set_blosc (p, val... ) :
1018
+ name === :chunk ? set_chunk (p, val... ) :
1019
+ name === :compress ? h5p_set_deflate (p, val... ) :
1020
+ name === :deflate ? h5p_set_deflate (p, val... ) :
1021
+ name === :external ? h5p_set_external (p, val... ) :
1022
+ name === :layout ? h5p_set_layout (p, val... ) :
1023
+ name === :shuffle ? h5p_set_shuffle (p, val... ) :
1024
+ name === :track_times ? h5p_set_obj_track_times (p, val... ) : # H5P_OBJECT_CREATE
1025
+ check ? error (" unknown dataset create property " , name) : nothing
1026
+ end
1027
+
1028
+ if class == H5P_ATTRIBUTE_CREATE
1029
+ return name === :char_encoding ? h5p_set_char_encoding (p, val... ) :
1030
+ check ? error (" unknown attribute create property " , name) : nothing
1031
+ end
1032
+
1033
+ check ? error (" unknown property class " , class) : return nothing
1034
+ end
1035
+
974
1036
function p_create (class; pv... )
975
- p = HDF5Properties (h5p_create (class), class)
976
- for (k,v) in pairs (pv)
977
- funcget, funcset, classprop = hdf5_prop_get_set[k]
978
- if classprop == H5P_OBJECT_CREATE && (class == H5P_DATASET_CREATE ||
979
- class == H5P_GROUP_CREATE ||
980
- class == H5P_FILE_CREATE)
981
- classprop = class
1037
+ p = HDF5Properties (h5p_create (class), class)
1038
+ for (k, v) in pairs (pv)
1039
+ _prop_set! (p, k, v, false )
982
1040
end
983
- class != classprop && continue
984
- funcset (p, v... )
985
- end
986
- return p
1041
+ return p
987
1042
end
988
1043
989
1044
# Delete objects
@@ -1002,8 +1057,7 @@ setindex!(dset::HDF5Dataset, val, name::String) = a_write(dset, name, val)
1002
1057
setindex! (x:: HDF5Attributes , val, name:: String ) = a_write (x. parent, name, val)
1003
1058
# Getting and setting properties: p[:chunk] = dims, p[:compress] = 6
1004
1059
function setindex! (p:: HDF5Properties , val, name:: Symbol )
1005
- funcget, funcset, _ = hdf5_prop_get_set[name]
1006
- funcset (p, val... )
1060
+ _prop_set! (p, name, val, true )
1007
1061
return p
1008
1062
end
1009
1063
# Create a dataset with properties: obj[path, prop = val, ...] = val
@@ -1850,7 +1904,7 @@ function get_mem_compatible_jl_type(objtype::HDF5Datatype)
1850
1904
else
1851
1905
is_signed = nothing
1852
1906
end
1853
- return hdf5_type_map[ (class_id, is_signed, native_size)]
1907
+ return _hdf5_type_map (class_id, is_signed, native_size)
1854
1908
finally
1855
1909
h5t_close (native_type)
1856
1910
end
@@ -1863,7 +1917,7 @@ function get_mem_compatible_jl_type(objtype::HDF5Datatype)
1863
1917
try
1864
1918
native_size = h5t_get_size (native_type)
1865
1919
is_signed = h5t_get_sign (native_type)
1866
- return hdf5_type_map[ (H5T_INTEGER, is_signed, native_size)]
1920
+ return _hdf5_type_map (H5T_INTEGER, is_signed, native_size)
1867
1921
finally
1868
1922
h5t_close (native_type)
1869
1923
end
@@ -2125,30 +2179,6 @@ function get_alignment(p::HDF5Properties)
2125
2179
return threshold[], alignment[]
2126
2180
end
2127
2181
2128
- # Map property names to function and attribute symbol
2129
- # Property names should follow the naming introduced by HDF5, i.e.
2130
- # keyname => (h5p_get_keyname, h5p_set_keyname, id )
2131
- const hdf5_prop_get_set = Dict (
2132
- :alignment => (get_alignment, h5p_set_alignment, H5P_FILE_ACCESS),
2133
- :alloc_time => (get_alloc_time, h5p_set_alloc_time, H5P_DATASET_CREATE),
2134
- :blosc => (nothing , h5p_set_blosc, H5P_DATASET_CREATE),
2135
- :char_encoding => (nothing , h5p_set_char_encoding, H5P_LINK_CREATE),
2136
- :chunk => (get_chunk, set_chunk, H5P_DATASET_CREATE),
2137
- :compress => (nothing , h5p_set_deflate, H5P_DATASET_CREATE),
2138
- :create_intermediate_group => (nothing , h5p_set_create_intermediate_group, H5P_LINK_CREATE),
2139
- :deflate => (nothing , h5p_set_deflate, H5P_DATASET_CREATE),
2140
- :driver => (h5p_get_driver, nothing , H5P_FILE_ACCESS),
2141
- :driver_info => (h5p_get_driver_info, nothing , H5P_FILE_ACCESS),
2142
- :external => (nothing , h5p_set_external, H5P_DATASET_CREATE),
2143
- :fclose_degree => (get_fclose_degree, h5p_set_fclose_degree, H5P_FILE_ACCESS),
2144
- :layout => (h5p_get_layout, h5p_set_layout, H5P_DATASET_CREATE),
2145
- :libver_bounds => (get_libver_bounds, h5p_set_libver_bounds, H5P_FILE_ACCESS),
2146
- :local_heap_size_hint => (nothing , h5p_set_local_heap_size_hint, H5P_GROUP_CREATE),
2147
- :shuffle => (nothing , h5p_set_shuffle, H5P_DATASET_CREATE),
2148
- :userblock => (get_userblock, h5p_set_userblock, H5P_FILE_CREATE),
2149
- :track_times => (nothing , h5p_set_obj_track_times, H5P_OBJECT_CREATE),
2150
- )
2151
-
2152
2182
# properties that require chunks in order to work (e.g. any filter)
2153
2183
# values do not matter -- just needed to form a NamedTuple with the desired keys
2154
2184
const chunked_props = (; compress= nothing , deflate= nothing , blosc= nothing , shuffle= nothing )
@@ -2217,20 +2247,12 @@ function __init__()
2217
2247
# Turn off automatic error printing
2218
2248
# h5e_set_auto(H5E_DEFAULT, C_NULL, C_NULL)
2219
2249
2220
- ASCII_LINK_PROPERTIES[] = p_create (H5P_LINK_CREATE)
2221
- h5p_set_char_encoding (ASCII_LINK_PROPERTIES[]. id, H5T_CSET_ASCII)
2222
- h5p_set_create_intermediate_group (ASCII_LINK_PROPERTIES[]. id, 1 )
2223
- UTF8_LINK_PROPERTIES[] = p_create (H5P_LINK_CREATE)
2224
- h5p_set_char_encoding (UTF8_LINK_PROPERTIES[]. id, H5T_CSET_UTF8)
2225
- h5p_set_create_intermediate_group (UTF8_LINK_PROPERTIES[]. id, 1 )
2226
- ASCII_ATTRIBUTE_PROPERTIES[] = p_create (H5P_ATTRIBUTE_CREATE)
2227
- h5p_set_char_encoding (ASCII_ATTRIBUTE_PROPERTIES[]. id, H5T_CSET_ASCII)
2228
- UTF8_ATTRIBUTE_PROPERTIES[] = p_create (H5P_ATTRIBUTE_CREATE)
2229
- h5p_set_char_encoding (UTF8_ATTRIBUTE_PROPERTIES[]. id, H5T_CSET_UTF8)
2230
-
2231
- rehash! (hdf5_type_map, length (hdf5_type_map. keys))
2232
- rehash! (hdf5_prop_get_set, length (hdf5_prop_get_set. keys))
2233
- rehash! (hdf5_obj_open, length (hdf5_obj_open. keys))
2250
+ ASCII_LINK_PROPERTIES[] = p_create (H5P_LINK_CREATE; char_encoding = H5T_CSET_ASCII,
2251
+ create_intermediate_group = 1 )
2252
+ UTF8_LINK_PROPERTIES[] = p_create (H5P_LINK_CREATE; char_encoding = H5T_CSET_UTF8,
2253
+ create_intermediate_group = 1 )
2254
+ ASCII_ATTRIBUTE_PROPERTIES[] = p_create (H5P_ATTRIBUTE_CREATE; char_encoding = H5T_CSET_ASCII)
2255
+ UTF8_ATTRIBUTE_PROPERTIES[] = p_create (H5P_ATTRIBUTE_CREATE; char_encoding = H5T_CSET_UTF8)
2234
2256
2235
2257
@require MPI= " da04e1cc-30fd-572f-bb4f-1f8673147195" @eval include (" mpio.jl" )
2236
2258
0 commit comments