Skip to content

Commit 8b9cb31

Browse files
committed
read in Dataset
1 parent f92aca4 commit 8b9cb31

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

lib/hdf5/dataset.rb

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,39 @@ def shape
3232
HDF5::FFI.H5Sclose(dataspace_id) if dataspace_id && dataspace_id >= 0
3333
end
3434

35-
# def read
36-
# dtype = dtype()
37-
# shape = shape()
38-
39-
# total_elements = shape.inject(:*)
40-
# case dtype
41-
# when :H5T_INTEGER
42-
# read_integer_data(total_elements)
43-
# when :H5T_FLOAT
44-
# read_float_data(total_elements)
45-
# when :H5T_STRING
46-
# read_string_data(total_elements)
47-
# else
48-
# raise 'Unsupported datatype'
49-
# end
50-
# end
35+
def read
36+
dtype = dtype()
37+
shape = shape()
38+
39+
total_elements = shape.inject(:*)
40+
case dtype
41+
when :H5T_INTEGER
42+
read_integer_data(total_elements)
43+
when :H5T_FLOAT
44+
read_float_data(total_elements)
45+
when :H5T_STRING
46+
read_string_data(total_elements)
47+
else
48+
raise 'Unsupported datatype'
49+
end
50+
end
51+
52+
def read_integer_data(total_elements)
53+
buffer = ::FFI::MemoryPointer.new(:int, total_elements)
54+
mem_type_id = HDF5::FFI.H5T_NATIVE_INT
55+
HDF5::FFI.H5Dread(@dataset_id, mem_type_id, 0, 0, 0, buffer)
56+
buffer.read_array_of_int(total_elements)
57+
end
58+
59+
def read_float_data(total_elements)
60+
buffer = ::FFI::MemoryPointer.new(:float, total_elements)
61+
mem_type_id = HDF5::FFI.H5T_NATIVE_FLOAT
62+
HDF5::FFI.H5Dread(@dataset_id, mem_type_id, 0, 0, 0, buffer)
63+
buffer.read_array_of_float(total_elements)
64+
end
65+
66+
def read_string_data(total_elements)
67+
raise NotImplementedError
68+
end
5169
end
5270
end

lib/hdf5/ffi3.rb

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,29 +231,29 @@ class Anon_Type_1 < ::FFI::Struct
231231

232232
typedef :int, :register_t
233233

234-
attach_function '__bswap_16', [
235-
:__uint16_t
236-
], :__uint16_t
234+
# attach_function '__bswap_16', [
235+
# :__uint16_t
236+
# ], :__uint16_t
237237

238-
attach_function '__bswap_32', [
239-
:__uint32_t
240-
], :__uint32_t
238+
# attach_function '__bswap_32', [
239+
# :__uint32_t
240+
# ], :__uint32_t
241241

242-
attach_function '__bswap_64', [
243-
:__uint64_t
244-
], :__uint64_t
242+
# attach_function '__bswap_64', [
243+
# :__uint64_t
244+
# ], :__uint64_t
245245

246-
attach_function '__uint16_identity', [
247-
:__uint16_t
248-
], :__uint16_t
246+
# attach_function '__uint16_identity', [
247+
# :__uint16_t
248+
# ], :__uint16_t
249249

250-
attach_function '__uint32_identity', [
251-
:__uint32_t
252-
], :__uint32_t
250+
# attach_function '__uint32_identity', [
251+
# :__uint32_t
252+
# ], :__uint32_t
253253

254-
attach_function '__uint64_identity', [
255-
:__uint64_t
256-
], :__uint64_t
254+
# attach_function '__uint64_identity', [
255+
# :__uint64_t
256+
# ], :__uint64_t
257257

258258
class Anon_Type_2 < ::FFI::Struct
259259
layout \
@@ -6348,5 +6348,30 @@ class H5FDSplitterVfdConfigT < ::FFI::Struct
63486348
attach_function 'H5Pset_fapl_stdio', [
63496349
:hid_t
63506350
], :herr_t
6351+
6352+
# These variables are defined manually because c2ffi can't parse them
6353+
6354+
attach_variable :H5T_NATIVE_SCHAR, :H5T_NATIVE_SCHAR_g, :hid_t
6355+
attach_variable :H5T_NATIVE_UCHAR, :H5T_NATIVE_UCHAR_g, :hid_t
6356+
attach_variable :H5T_NATIVE_SHORT, :H5T_NATIVE_SHORT_g, :hid_t
6357+
attach_variable :H5T_NATIVE_USHORT, :H5T_NATIVE_USHORT_g, :hid_t
6358+
attach_variable :H5T_NATIVE_INT, :H5T_NATIVE_INT_g, :hid_t
6359+
attach_variable :H5T_NATIVE_UINT, :H5T_NATIVE_UINT_g, :hid_t
6360+
attach_variable :H5T_NATIVE_LONG, :H5T_NATIVE_LONG_g, :hid_t
6361+
attach_variable :H5T_NATIVE_ULONG, :H5T_NATIVE_ULONG_g, :hid_t
6362+
attach_variable :H5T_NATIVE_LLONG, :H5T_NATIVE_LLONG_g, :hid_t
6363+
attach_variable :H5T_NATIVE_ULLONG, :H5T_NATIVE_ULLONG_g, :hid_t
6364+
attach_variable :H5T_NATIVE_FLOAT, :H5T_NATIVE_FLOAT_g, :hid_t
6365+
attach_variable :H5T_NATIVE_DOUBLE, :H5T_NATIVE_DOUBLE_g, :hid_t
6366+
attach_variable :H5T_NATIVE_LDOUBLE, :H5T_NATIVE_LDOUBLE_g, :hid_t
6367+
attach_variable :H5T_NATIVE_HADDR, :H5T_NATIVE_HADDR_g, :hid_t
6368+
attach_variable :H5T_NATIVE_HSIZE, :H5T_NATIVE_HSIZE_g, :hid_t
6369+
attach_variable :H5T_NATIVE_HSSIZE, :H5T_NATIVE_HSSIZE_g, :hid_t
6370+
attach_variable :H5T_NATIVE_HERR, :H5T_NATIVE_HERR_g, :hid_t
6371+
attach_variable :H5T_NATIVE_HBOOL, :H5T_NATIVE_HBOOL_g, :hid_t
6372+
attach_variable :H5T_C_S1, :H5T_C_S1_g, :hid_t
6373+
6374+
SIZE_MAX = (1 << (::FFI::MemoryPointer.new(:size_t).size * 8)) - 1
6375+
H5T_VARIABLE = SIZE_MAX
63516376
end
63526377
end

0 commit comments

Comments
 (0)