Class: HDF5::File

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

Constant Summary collapse

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

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(filename, mode = H5F_ACC_RDONLY) ⇒ File

Returns a new instance of File.



67
68
69
# File 'lib/hdf5/file.rb', line 67

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



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hdf5/file.rb', line 11

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
    Native.close_object(file)
  end
end

.open(filename, mode = 'r') ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hdf5/file.rb', line 23

def open(filename, mode = 'r')
  file_id, flags = open_file(filename, mode)
  file = from_id(file_id, filename, flags)
  return file unless block_given?

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

Instance Method Details

#[](name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hdf5/file.rb', line 136

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

#attrsObject



148
149
150
151
# File 'lib/hdf5/file.rb', line 148

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

#closeObject



71
72
73
74
75
76
# File 'lib/hdf5/file.rb', line 71

def close
  return if @file_id.nil? || @context.closed?

  @context.close_file
  @file_id = nil
end

#closed?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/hdf5/file.rb', line 78

def closed?
  @file_id.nil? || @context.closed?
end

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



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

def create_dataset(name, data = nil, **options, &block)
  dataset = @context.synchronize do
    ensure_open!
    Dataset.create(@file_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



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hdf5/file.rb', line 90

def create_group(name, &block)
  group = @context.synchronize do
    ensure_open!
    Group.create(@file_id, name, @context)
  end
  return group unless block

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

#flushObject

Raises:



82
83
84
85
86
87
88
# File 'lib/hdf5/file.rb', line 82

def flush
  ensure_open!
  status = HDF5::FFI.H5Fflush(@file_id, :H5F_SCOPE_GLOBAL)
  raise NativeError, 'Failed to flush file' if status < 0

  self
end

#list_entriesObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hdf5/file.rb', line 118

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

  status = if HDF5::FFI::MiV == 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
  Native.check(status, 'Failed to iterate over file entries')

  list
end