// Writes raw data from a buffer to a dataset. // herr_t H5Dwrite(hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, const void * buf ) func (s *DataSet) Write(data interface{}, dtype *DataType) error { var addr uintptr v := reflect.ValueOf(data) //fmt.Printf(":: write[%s]...\n", v.Kind()) switch v.Kind() { case reflect.Array: addr = v.UnsafeAddr() case reflect.Slice: addr = v.Pointer() case reflect.String: str := (*reflect.StringHeader)(unsafe.Pointer(v.UnsafeAddr())) addr = str.Data case reflect.Ptr: addr = v.Pointer() default: addr = v.Pointer() } rc := C.H5Dwrite(s.id, dtype.id, 0, 0, 0, unsafe.Pointer(addr)) err := togo_err(rc) return err }
// WriteSubset writes a subset of raw data from a buffer to a dataset. func (s *Dataset) WriteSubset(data interface{}, memspace, filespace *Dataspace) error { dtype, err := s.Datatype() defer dtype.Close() if err != nil { return err } addr := unsafe.Pointer(nil) v := reflect.Indirect(reflect.ValueOf(data)) switch v.Kind() { case reflect.Array: addr = unsafe.Pointer(v.UnsafeAddr()) case reflect.Slice: slice := (*reflect.SliceHeader)(unsafe.Pointer(v.UnsafeAddr())) addr = unsafe.Pointer(slice.Data) case reflect.String: str := (*reflect.StringHeader)(unsafe.Pointer(v.UnsafeAddr())) addr = unsafe.Pointer(str.Data) case reflect.Ptr: addr = unsafe.Pointer(v.Pointer()) default: addr = unsafe.Pointer(v.UnsafeAddr()) } var filespace_id, memspace_id C.hid_t = 0, 0 if memspace != nil { memspace_id = memspace.id } if filespace != nil { filespace_id = filespace.id } rc := C.H5Dwrite(s.id, dtype.id, memspace_id, filespace_id, 0, addr) err = h5err(rc) return err }
// 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 }