// Get reads data from the file. func (f *File) Get(name string, something interface{}) error { value := reflect.ValueOf(something) if value.Kind() != reflect.Ptr { return errors.New("expected a pointer") } value = reflect.Indirect(value) cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) did := C.H5Dopen2(f.fid, cname, C.H5P_DEFAULT) if did < 0 { return errors.New("cannot find the dataset") } defer C.H5Dclose(did) object := newObject() defer object.free() object.tid = C.H5Dget_type(did) if object.tid < 0 { return errors.New("cannot get the datatype of the dataset") } if err := initializeToGet(object, value); err != nil { return err } if C.H5Dread(did, object.tid, C.H5S_ALL, C.H5S_ALL, C.H5P_DEFAULT, object.data) < 0 { return errors.New("cannot read the dataset from the file") } if err := finalizeToGet(object, value); err != nil { return err } one := C.hsize_t(1) sid := C.H5Screate_simple(1, (*C.hsize_t)(unsafe.Pointer(&one)), nil) if sid < 0 { return errors.New("cannot create a data space") } defer C.H5Sclose(sid) if C.H5Dvlen_reclaim(object.tid, sid, C.H5P_DEFAULT, object.data) < 0 { return errors.New("cannot reclaim memory") } return nil }
// Creates a new simple dataspace and opens it for access. // hid_t H5Screate_simple( int rank, const hsize_t * current_dims, const hsize_t * maximum_dims ) func CreateSimpleDataSpace(dims, maximum_dims []int) (*DataSpace, error) { var c_dims *C.hsize_t = nil var c_maxdims *C.hsize_t = nil rank := C.int(0) if dims != nil { rank = C.int(len(dims)) // FIXME: size of C.hsize_t and go.int !! c_dims = (*C.hsize_t)(unsafe.Pointer(&dims[0])) } if maximum_dims != nil { rank = C.int(len(maximum_dims)) // FIXME: size of C.hsize_t and go.int !! c_maxdims = (*C.hsize_t)(unsafe.Pointer(&maximum_dims[0])) } if len(dims) != len(maximum_dims) && (dims != nil && maximum_dims != nil) { return nil, errors.New("sizes of 'dims' and 'maximum_dims' dont match") } hid := C.H5Screate_simple(rank, c_dims, c_maxdims) err := togo_err(C.herr_t(int(hid))) if err != nil { return nil, err } ds := new_dataspace(hid) return ds, err }
// CreateSimpleDataspace creates a new simple dataspace and opens it for access. func CreateSimpleDataspace(dims, maxDims []uint) (*Dataspace, error) { var c_dims, c_maxdims *C.hsize_t rank := C.int(0) if dims != nil { rank = C.int(len(dims)) c_dims = (*C.hsize_t)(unsafe.Pointer(&dims[0])) } if maxDims != nil { rank = C.int(len(maxDims)) c_maxdims = (*C.hsize_t)(unsafe.Pointer(&maxDims[0])) } if len(dims) != len(maxDims) && (dims != nil && maxDims != nil) { return nil, errors.New("lengths of dims and maxDims do not match") } hid := C.H5Screate_simple(rank, c_dims, c_maxdims) if hid < 0 { return nil, fmt.Errorf("failed to create dataspace") } return newDataspace(hid), nil }
// Put writes data into the file. func (f *File) Put(name string, something interface{}, dimensions ...uint) error { object := newObject() defer object.free() if err := initializeToPut(object, reflect.ValueOf(something), dimensions...); err != nil { return err } cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) one := C.hsize_t(1) sid := C.H5Screate_simple(1, (*C.hsize_t)(unsafe.Pointer(&one)), nil) if sid < 0 { return errors.New("cannot create a data space") } defer C.H5Sclose(sid) if result := C.H5Lexists(f.fid, cname, C.H5P_DEFAULT); result < 0 { return errors.New("cannot check if the name already exists") } else if result > 0 && C.H5Ldelete(f.fid, cname, C.H5P_DEFAULT) < 0 { return errors.New("cannot overwrite an existing dataset") } did := C.H5Dcreate2(f.fid, cname, object.tid, sid, C.H5P_DEFAULT, C.H5P_DEFAULT, C.H5P_DEFAULT) if did < 0 { return errors.New("cannot create a dataset") } defer C.H5Dclose(did) if C.H5Dwrite(did, object.tid, C.H5S_ALL, C.H5S_ALL, C.H5P_DEFAULT, object.data) < 0 { return errors.New("cannot write the dataset into the file") } return nil }