Skip to content

Instantly share code, notes, and snippets.

@markdouthwaite
Last active October 27, 2020 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markdouthwaite/eb3d7778e1d18c7c144462ad44d595ad to your computer and use it in GitHub Desktop.
Save markdouthwaite/eb3d7778e1d18c7c144462ad44d595ad to your computer and use it in GitHub Desktop.
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