A pure MoonBit HDF5 reader and new-file writer
moon add Zz04321/moon-hdf5@0.1.0
moon updategit clone https://github.com/zzSmall6/moon-hdf5.git
cd moon-hdf5
moon update
moon run src/cmd/main --target native -- testdata/basic_v0.h5///|
let bytes = @fs.read_file_to_bytes("data.h5")
let file = @lib.Hdf5File::parse(bytes)
let metadata = file.read_dataset_metadata("/group/values")
let values = file.read_dataset("/group/values")
let slice = file.read_dataset_selection(
"/matrix",
@lib.Hyperslab([0UL, 0UL], [2UL, 2UL], [1UL, 2UL]),
)
let attributes = file.attributes("/group/values")
let nodes = file.walk()///|
let chunks = file.chunk_iterator("/matrix", [1UL, 2UL])
while chunks.next() is Some(chunk) {
chunk.offset |> ignore
chunk.values |> ignore
}
///|
let resolver = @lib.Hdf5Resolver::new(bytes)
resolver.add_source("other-file.h5", @fs.read_file_to_bytes("other-file.h5"))
let remote = resolver.read_dataset("/experiment/remote_counts")///|
let writer = @lib.Hdf5Writer::new()
writer.set_attribute_sharing(true)
writer.set_fill_sharing(true)
writer.set_filter_sharing(true)
let record = @lib.Datatype::write_compound([
("id", @lib.Datatype::write_integer(true, 4)),
("value", @lib.Datatype::write_float(8)),
])
writer.add_committed_datatype("/types/record", record)
writer.add_dataset(
"/experiment/records",
[2UL],
@lib.StructuredValues(record, [
@lib.Compound([
{ name: "id", value: @lib.SignedInteger(1L) },
{ name: "value", value: @lib.FloatingPoint(1.25) },
]),
@lib.Compound([
{ name: "id", value: @lib.SignedInteger(2L) },
{ name: "value", value: @lib.FloatingPoint(-3.5) },
]),
]),
)
writer.add_attribute(
"/experiment/records",
"unit",
[],
@lib.FixedStringValues(["items"], 8),
)
writer.add_chunked_dataset(
"/experiment/codes",
[6UL],
[3U],
@lib.Int64Values([10L, 11L, 12L, 20L, 21L, 22L]),
fill_value=@lib.Int64Values([-1L]),
filters=[@lib.Shuffle, @lib.Deflate(6), @lib.Fletcher32],
)
writer.add_extensible_dataset(
"/experiment/series",
[0UL, 2UL],
[None, Some(2UL)],
[2U, 2U],
@lib.Int64Values([]),
fill_value=@lib.Int64Values([-1L]),
)
writer.append_dataset(
"/experiment/series",
[2UL, 2UL],
@lib.Int64Values([1L, 2L, 3L, 4L]),
)
writer.add_soft_link("/experiment", "latest", "/experiment/series")
let output = writer.encode()moon run src/cmd/write_demo --target native -- output.h5
python scripts/verify_writer_output.py output.h5moon run src/cmd/main -- inspect testdata/basic_v0.h5
moon run src/cmd/main -- metadata testdata/basic_v0.h5 /matrix
moon run src/cmd/main -- stats testdata/basic_v0.h5 /matrix
moon run src/cmd/main -- read testdata/basic_v0.h5 /matrix 0,0 2,2 1,2
moon run src/cmd/main -- read testdata/basic_v0.h5 /matrix --points "1,2;0,1"moon info
moon fmt
moon check
moon test --target wasm
moon test --target native
H5PY_PYTHON=/path/to/python ./scripts/release_check.shimpl Hdf5Source for Bytespub(all) struct Attribute {
name : String
dataspace : Dataspace
datatype : Datatype
values : DatasetValues?
} derive(Debug)pub(all) struct Dataset {
dataspace : Dataspace
datatype : Datatype
layout : DataLayout
values : DatasetValues
} derive(Debug)pub(all) struct DatasetChunk {
offset : Array[UInt64]
dimensions : Array[UInt64]
values : DatasetValues
} derive(Debug)pub struct DatasetChunkIterator {
// private fields
}pub(all) struct DatasetMetadata {
dataspace : Dataspace
datatype : Datatype
layout : DataLayout
} derive(Debug)pub(all) struct DatasetSelectionResult {
dimensions : Array[UInt64]
datatype : Datatype
values : DatasetValues
} derive(Debug)pub(all) enum DatasetValue {
SignedInteger(Int64)
UnsignedInteger(UInt64)
FloatingPoint(Double)
Text(String)
Enumeration(String)
Bitfield(UInt64)
Opaque(Bytes)
Compound(Array[CompoundFieldValue])
Elements(Array[DatasetValue])
ObjectReference(String?)
RegionReference(String?, Hdf5RegionSelection)
Reference(Bytes)
Sequence(Array[DatasetValue])
Time(UInt64)
Complex(Double, Double)
} derive(Eq, Debug)pub(all) struct Dataspace {
kind : DataspaceKind
dimensions : Array[UInt64]
maximum_dimensions : Array[UInt64]
} derive(Debug)pub(all) struct Datatype {
class : Int
version : Int
size : Int
signed : Bool
little_endian : Bool
vlen_is_string : Bool
enum_members : Array[EnumMember]
compound_members : Array[CompoundMember]
base_types : Array[Datatype]
array_dimensions : Array[UInt64]
bit_offset : Int
bit_precision : Int
float_sign_position : Int
float_exponent_position : Int
float_exponent_size : Int
float_mantissa_position : Int
float_mantissa_size : Int
float_exponent_bias : Int
float_normalization : Int
string_padding : Int
string_charset : Int
opaque_tag : String
reference_kind : Int
} derive(Debug)pub(all) struct Hdf5ExternalSlot {
file_name : String
file_offset : UInt64
data_size : UInt64
}pub struct Hdf5File {
// private fields
}fn Hdf5File::chunk_iterator(self : Hdf5File, path : String, tile_dimensions : Array[UInt64]) -> DatasetChunkIterator raise Hdf5Errorfn Hdf5File::read_dataset_metadata(self : Hdf5File, path : String) -> DatasetMetadata raise Hdf5Errorfn Hdf5File::read_dataset_selection(self : Hdf5File, path : String, selection : DatasetSelection) -> DatasetSelectionResult raise Hdf5Errorfn Hdf5File::read_region_reference(self : Hdf5File, reference : Bytes) -> DatasetSelectionResult? raise Hdf5Errorfn Hdf5File::resolve_object_reference(self : Hdf5File, reference : Bytes) -> Hdf5ReferenceTarget? raise Hdf5Errorfn Hdf5File::resolve_region_reference(self : Hdf5File, reference : Bytes) -> Hdf5RegionReferenceTarget? raise Hdf5Errorpub(all) struct Hdf5ReferenceTarget {
address : UInt64
kind : Hdf5ObjectKind
path : String?
} derive(Eq, Debug)pub(all) struct Hdf5RegionReferenceTarget {
address : UInt64
kind : Hdf5ObjectKind
path : String?
dataspace : Dataspace
selection : Hdf5RegionSelection
} derive(Debug)pub(all) struct Hdf5ResolvedNode {
file : String
path : String
address : UInt64
kind : Hdf5ObjectKind
} derive(Eq, Debug)pub struct Hdf5Resolver {
// private fields
}fn Hdf5Resolver::add_source(self : Hdf5Resolver, name : String, source : &Hdf5Source) -> Unit raise Hdf5Errorfn Hdf5Resolver::chunk_iterator(self : Hdf5Resolver, path : String, tile_dimensions : Array[UInt64]) -> DatasetChunkIterator raise Hdf5Errorfn Hdf5Resolver::read_dataset_metadata(self : Hdf5Resolver, path : String) -> DatasetMetadata raise Hdf5Errorfn Hdf5Resolver::read_dataset_selection(self : Hdf5Resolver, path : String, selection : DatasetSelection) -> DatasetSelectionResult raise Hdf5Errorpub(all) enum Hdf5WriteValues {
Int8Values(Array[Int])
Int16Values(Array[Int])
Int32Values(Array[Int])
Int64Values(Array[Int64])
UInt8Values(Array[UInt])
UInt16Values(Array[UInt])
UInt32Values(Array[UInt])
UInt64Values(Array[UInt64])
Float32Values(Array[Float])
Float64Values(Array[Double])
FixedStringValues(Array[String], Int)
VariableStringValues(Array[String])
ObjectReferenceValues(Array[String?])
Complex64Values(Array[(Float, Float)])
Complex128Values(Array[(Double, Double)])
StructuredValues(Datatype, Array[DatasetValue])
} derive(Debug)pub struct Hdf5Writer {
// private fields
}fn Hdf5Writer::add_attribute(self : Hdf5Writer, object_path : String, name : String, dimensions : Array[UInt64], values : Hdf5WriteValues) -> Unit raise Hdf5Errorfn Hdf5Writer::add_chunked_dataset(self : Hdf5Writer, path : String, dimensions : Array[UInt64], chunk_dimensions : Array[UInt], values : Hdf5WriteValues, fill_value? : Hdf5WriteValues, filters? : Array[Hdf5WriteFilter]) -> Unit raise Hdf5Errorfn Hdf5Writer::add_committed_datatype(self : Hdf5Writer, path : String, datatype : Datatype) -> Unit raise Hdf5Errorfn Hdf5Writer::add_dataset(self : Hdf5Writer, path : String, dimensions : Array[UInt64], values : Hdf5WriteValues) -> Unit raise Hdf5Errorfn Hdf5Writer::add_extensible_dataset(self : Hdf5Writer, path : String, dimensions : Array[UInt64], maximum_dimensions : Array[UInt64?], chunk_dimensions : Array[UInt], values : Hdf5WriteValues, fill_value? : Hdf5WriteValues, filters? : Array[Hdf5WriteFilter]) -> Unit raise Hdf5Errorfn Hdf5Writer::add_external_dataset(self : Hdf5Writer, path : String, dimensions : Array[UInt64], values : Hdf5WriteValues, slots : Array[Hdf5ExternalSlot], fill_value? : Hdf5WriteValues) -> Unit raise Hdf5Errorfn Hdf5Writer::add_external_link(self : Hdf5Writer, parent_path : String, name : String, target_file : String, target_path : String) -> Unit raise Hdf5Errorfn Hdf5Writer::add_soft_link(self : Hdf5Writer, parent_path : String, name : String, target : String) -> Unit raise Hdf5Errorfn Hdf5Writer::append_dataset(self : Hdf5Writer, path : String, dimensions : Array[UInt64], values : Hdf5WriteValues) -> Unit raise Hdf5Errorfn Hdf5Writer::resize_dataset(self : Hdf5Writer, path : String, dimensions : Array[UInt64]) -> Unit raise Hdf5Errorfn Hdf5Writer::set_chunk_filter_mask(self : Hdf5Writer, path : String, chunk_index : Int, filter_mask : UInt) -> Unit raise Hdf5ErrorInstall
Download zipA pure MoonBit HDF5 reader and new-file writer