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
|
# File 'lib/hdf5/attribute.rb', line 70
def write(attr_name, value)
values = normalize_data(value)
datatype_id = datatype_id_for(values)
exists = HDF5::FFI.H5Aexists(@dataset_id, attr_name)
raise HDF5::Error, "Failed to check attribute existence: #{attr_name}" if exists.negative?
if exists.positive?
status = HDF5::FFI.H5Adelete(@dataset_id, attr_name)
raise HDF5::Error, "Failed to replace attribute: #{attr_name}" if status < 0
end
dims = ::FFI::MemoryPointer.new(:ulong_long, 1)
dims.write_array_of_ulong_long([values.length])
dataspace_id = HDF5::FFI.H5Screate_simple(1, dims, nil)
raise HDF5::Error, 'Failed to create attribute dataspace' if dataspace_id < 0
attr_id = HDF5::FFI.H5Acreate2(
@dataset_id,
attr_name,
datatype_id,
dataspace_id,
HDF5::DEFAULT_PROPERTY_LIST,
HDF5::DEFAULT_PROPERTY_LIST
)
raise HDF5::Error, "Failed to create attribute: #{attr_name}" if attr_id < 0
buffer = buffer_for(values)
status = HDF5::FFI.H5Awrite(attr_id, datatype_id, buffer)
raise HDF5::Error, "Failed to write attribute: #{attr_name}" if status < 0
value
ensure
HDF5::FFI.H5Aclose(attr_id) if attr_id && attr_id >= 0
HDF5::FFI.H5Sclose(dataspace_id) if dataspace_id && dataspace_id >= 0
end
|