Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple Go module for reading a single array of 64 bit floating point numbers from a HDF5 formatted file.
package main
import (
"gonum.org/v1/hdf5"
)
// load a scalar-valued variable of type int from an attribute of the given name on the given dataset.
func getIntScalarAttr(dataset *hdf5.Dataset, attrName string) int {
var value int
attr, err := dataset.OpenAttribute(attrName)
if err != nil {
panic(err)
}
attr.Read(&value, hdf5.T_NATIVE_UINT32)
return value
}
// load the image data from the indicated HDF5 file.
func getImageDataset(fileName string) *hdf5.Dataset {
file, err := hdf5.OpenFile(fileName, hdf5.F_ACC_RDONLY)
if err != nil {
panic(err)
}
dataset, err := file.OpenDataset("data")
if err != nil {
panic(err)
}
return dataset
}
func main(){
// load dataset & dataset attributes
dataset := getImageDataset("image.h5")
height := getIntScalarAttr(dataset, "height")
width := getIntScalarAttr(dataset, "width")
// create image data slice using height/width attributes
image := make([]float64, height*width)
dataset.Read(&image)
// reshape and process here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.