Class: HDF5::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/hdf5/attribute.rb

Instance Method Summary collapse

Constructor Details

#initialize(dataset_id, attr_name) ⇒ Attribute

Returns a new instance of Attribute.

Raises:



3
4
5
6
7
8
# File 'lib/hdf5/attribute.rb', line 3

def initialize(dataset_id, attr_name)
  @dataset_id = dataset_id
  @attr_name = attr_name
  @attr_id = HDF5::FFI.H5Aopen(@dataset_id, @attr_name, HDF5::DEFAULT_PROPERTY_LIST)
  raise HDF5::Error, 'Failed to open attribute' if @attr_id < 0
end

Instance Method Details

#closeObject



46
47
48
49
50
51
# File 'lib/hdf5/attribute.rb', line 46

def close
  return if @attr_id.nil?

  HDF5::FFI.H5Aclose(@attr_id)
  @attr_id = nil
end

#readObject



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
# File 'lib/hdf5/attribute.rb', line 10

def read
  type_id = HDF5::FFI.H5Aget_type(@attr_id)
  space_id = HDF5::FFI.H5Aget_space(@attr_id)

  size = HDF5::FFI.H5Sget_simple_extent_npoints(space_id)

  buffer = \
    case HDF5::FFI.H5Tget_class(type_id)
    when :H5T_INTEGER
      ::FFI::MemoryPointer.new(:int, size)
    when :H5T_FLOAT
      ::FFI::MemoryPointer.new(:double, size)
    when :H5T_STRING
      ::FFI::MemoryPointer.new(:pointer, size)
    else
      raise HDF5::Error, 'Unsupported data type'
    end

  status = HDF5::FFI.H5Aread(@attr_id, type_id, buffer)
  raise HDF5::Error, 'Failed to read attribute' if status < 0

  case HDF5::FFI.H5Tget_class(type_id)
  when :H5T_INTEGER
    buffer.read_array_of_int(size)
  when :H5T_FLOAT
    buffer.read_array_of_double(size)
  when :H5T_STRING
    buffer.read_pointer.read_string
  else
    raise HDF5::Error, 'Unsupported data type'
  end
ensure
  HDF5::FFI.H5Tclose(type_id) if type_id && type_id >= 0
  HDF5::FFI.H5Sclose(space_id) if space_id && space_id >= 0
end