Class: HDF5::Group

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_id, name) ⇒ Group

Returns a new instance of Group.



38
39
40
# File 'lib/hdf5/group.rb', line 38

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

Class Method Details

.create(parent_id, name) ⇒ Object



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

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

  begin
    yield group
  ensure
    group.close
  end
end

.open(parent_id, name) ⇒ Object



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

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

  begin
    yield group
  ensure
    group.close
  end
end

Instance Method Details

#[](name) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/hdf5/group.rb', line 78

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

#attrsObject



88
89
90
# File 'lib/hdf5/group.rb', line 88

def attrs
  @attrs ||= AttributeManager.new(@group_id)
end

#closeObject



42
43
44
45
46
47
# File 'lib/hdf5/group.rb', line 42

def close
  return if @group_id.nil?

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

#create_dataset(name, data, &block) ⇒ Object



53
54
55
# File 'lib/hdf5/group.rb', line 53

def create_dataset(name, data, &block)
  Dataset.create(@group_id, name, data, &block)
end

#create_group(name, &block) ⇒ Object



49
50
51
# File 'lib/hdf5/group.rb', line 49

def create_group(name, &block)
  self.class.create(@group_id, name, &block)
end

#list_datasetsObject



74
75
76
# File 'lib/hdf5/group.rb', line 74

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

#list_entriesObject



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

def list_entries
  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