Class: HDF5::File

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

Constant Summary collapse

H5F_ACC_RDONLY =
0x0000
H5F_ACC_RDWR =
0x0001
H5F_ACC_TRUNC =
0x0002

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, mode = H5F_ACC_RDONLY) ⇒ File

Returns a new instance of File.



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

def initialize(filename, mode = H5F_ACC_RDONLY)
  initialize_from_id(HDF5::FFI.H5Fopen(filename, mode, HDF5::DEFAULT_PROPERTY_LIST), filename, mode)
end

Class Method Details

.create(filename, flags = H5F_ACC_TRUNC) ⇒ Object



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

def create(filename, flags = H5F_ACC_TRUNC)
  file = from_id(HDF5::FFI.H5Fcreate(filename, flags, HDF5::DEFAULT_PROPERTY_LIST, HDF5::DEFAULT_PROPERTY_LIST),
                 filename, flags)
  return file unless block_given?

  begin
    yield file
  ensure
    file.close
  end
end

.open(filename, mode = H5F_ACC_RDONLY) ⇒ Object



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

def open(filename, mode = H5F_ACC_RDONLY)
  file = from_id(HDF5::FFI.H5Fopen(filename, mode, HDF5::DEFAULT_PROPERTY_LIST), filename, mode)
  return file unless block_given?

  begin
    yield file
  ensure
    file.close
  end
end

Instance Method Details

#[](name) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/hdf5/file.rb', line 76

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

#attrsObject



86
87
88
# File 'lib/hdf5/file.rb', line 86

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

#closeObject



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

def close
  return if @file_id.nil?

  HDF5::FFI.H5Fclose(@file_id)
  @file_id = nil
end

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



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

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

#create_group(name, &block) ⇒ Object



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

def create_group(name, &block)
  Group.create(@file_id, name, &block)
end

#list_entriesObject



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

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

  case HDF5::FFI::MiV
  when 10
    HDF5::FFI.H5Literate(@file_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
  else
    HDF5::FFI.H5Literate2(@file_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
  end.negative? && raise(HDF5::Error, 'Failed to iterate over file entries')

  list
end