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
    group.close
  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
    group.close
  end
end

Instance Method Details

#[](name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/hdf5/group.rb', line 105

def [](name)
  ensure_open!
  if group?(name)
    self.class.open(@group_id, name, @context)
  elsif dataset?(name)
    Dataset.open(@group_id, name, context: @context)
  else
    raise HDF5::Error, "Group or dataset not found: #{name}"
  end
end

#attrs ⇒ Object



116
117
118
119
# File 'lib/hdf5/group.rb', line 116

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

#close ⇒ Object



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

def close
  return if @group_id.nil?

  @context ? @context.close(@group_id) : HDF5::FFI.H5Gclose(@group_id)
  @group_id = nil
end

#closed? ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/hdf5/group.rb', line 51

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

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



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hdf5/group.rb', line 69

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
    dataset.close
  end
end

#create_group(name, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hdf5/group.rb', line 55

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
    group.close
  end
end

#list_datasets ⇒ Object



101
102
103
# File 'lib/hdf5/group.rb', line 101

def list_datasets
  list_entries.select { |name| dataset?(name) }
end

#list_entries ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hdf5/group.rb', line 83

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

  (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).negative? &&
    raise(HDF5::Error, 'Failed to list entries')

  entries
end