Class: HDF5::Group

Inherits:
Object
  • Object
show all
Includes:
Hierarchy
Defined in:
lib/hdf5/group.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hierarchy

#create_hard_link, #create_soft_link, #delete, #each_key, #key?, #keys, #link_info, #move, #open_dataset, #require_group

Constructor Details

#initialize(file_id, name) ⇒ Group

Returns a new instance of Group.



40
41
42
# File 'lib/hdf5/group.rb', line 40

def initialize(file_id, name)
  initialize_from_id(HDF5::FFI.H5Gopen2(file_id, name, HDF5::DEFAULT_PROPERTY_LIST), name, nil)
end

Class Method Details

.create(parent_id, name, context = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hdf5/group.rb', line 6

def create(parent_id, name, context = nil)
  group = from_id(
    HDF5::FFI.H5Gcreate2(parent_id, name, HDF5::DEFAULT_PROPERTY_LIST, HDF5::DEFAULT_PROPERTY_LIST,
                         HDF5::DEFAULT_PROPERTY_LIST), name, context
  )
  return group unless block_given?

  begin
    yield group
  ensure
    Native.close_object(group)
  end
end

.open(parent_id, name, context = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/hdf5/group.rb', line 20

def open(parent_id, name, context = nil)
  group = from_id(HDF5::FFI.H5Gopen2(parent_id, name, HDF5::DEFAULT_PROPERTY_LIST), name, context)
  return group unless block_given?

  begin
    yield group
  ensure
    Native.close_object(group)
  end
end

Instance Method Details

#[](name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hdf5/group.rb', line 109

def [](name)
  ensure_open!
  case object_type(name)
  when :H5O_TYPE_GROUP
    self.class.open(@group_id, name, @context)
  when :H5O_TYPE_DATASET
    Dataset.open(@group_id, name, context: @context)
  else
    raise UnsupportedTypeError, "Object is not a group or dataset: #{name}"
  end
end

#attrsObject



121
122
123
124
# File 'lib/hdf5/group.rb', line 121

def attrs
  ensure_open!
  @attrs ||= AttributeManager.new(@group_id, @context)
end

#closeObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/hdf5/group.rb', line 44

def close
  return if @group_id.nil?

  if @context
    @context.close(@group_id)
  else
    Native.check(HDF5::FFI.H5Gclose(@group_id), 'Failed to close HDF5 group')
  end
  @group_id = nil
end

#closed?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/hdf5/group.rb', line 55

def closed?
  @group_id.nil? || (@context && @context.closed?)
end

#create_dataset(name, data = nil, **options, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hdf5/group.rb', line 73

def create_dataset(name, data = nil, **options, &block)
  dataset = HDF5::FFI::CALL_LOCK.synchronize do
    ensure_open!
    Dataset.create(@group_id, name, data, context: @context, **options)
  end
  return dataset unless block

  begin
    block.call(dataset)
  ensure
    Native.close_object(dataset)
  end
end

#create_group(name, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hdf5/group.rb', line 59

def create_group(name, &block)
  group = HDF5::FFI::CALL_LOCK.synchronize do
    ensure_open!
    self.class.create(@group_id, name, @context)
  end
  return group unless block

  begin
    block.call(group)
  ensure
    Native.close_object(group)
  end
end

#list_datasetsObject



105
106
107
# File 'lib/hdf5/group.rb', line 105

def list_datasets
  list_entries.select { |name| object_type(name) == :H5O_TYPE_DATASET }
end

#list_entriesObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hdf5/group.rb', line 87

def list_entries
  ensure_open!
  entries = []
  callback = ::FFI::Function.new(:int, %i[int64_t string pointer pointer]) do |_, name, _, _|
    entries << name
    0 # continue
  end

  status = if HDF5::FFI::MiV == 10
             HDF5::FFI.H5Literate(@group_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
           else
             HDF5::FFI.H5Literate2(@group_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
           end
  Native.check(status, 'Failed to list entries')

  entries
end