Esempio n. 1
1
File: get.go Progetto: postfix/hdf5
// 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
}
Esempio n. 2
0
// ReadSubset reads a subset of raw data from a dataset into a buffer.
func (s *Dataset) ReadSubset(data interface{}, dtype *Datatype, memspace, filespace *Dataspace) error {
	var addr uintptr
	v := reflect.ValueOf(data)

	switch v.Kind() {

	case reflect.Array:
		addr = v.UnsafeAddr()

	case reflect.String:
		str := (*reflect.StringHeader)(unsafe.Pointer(v.UnsafeAddr()))
		addr = str.Data

	case reflect.Ptr:
		addr = v.Pointer()

	default:
		addr = 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.H5Dread(s.id, dtype.id, memspace_id, filespace_id, 0, unsafe.Pointer(addr))
	err := h5err(rc)
	return err
}
Esempio n. 3
0
// Reads raw data from a dataset into a buffer.
// herr_t H5Dread(hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, void * buf )
func (s *DataSet) Read(data interface{}, dtype *DataType) error {
	var addr uintptr
	v := reflect.ValueOf(data)

	//fmt.Printf(":: read[%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.UnsafeAddr()
	}

	rc := C.H5Dread(s.id, dtype.id, 0, 0, 0, unsafe.Pointer(addr))
	err := togo_err(rc)
	return err
}
Esempio n. 4
0
File: h5d.go Progetto: pauh/go-hdf5
// Reads raw data from a dataset into a buffer.
// herr_t H5Dread(hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, void * buf )
func (s *Dataset) Read(data interface{}, dtype *Datatype) error {
	var addr uintptr
	var tmp_slice []byte
	post_process := false
	v := reflect.ValueOf(data)

	//fmt.Printf(":: read[%s]...\n", v.Kind())
	switch v.Kind() {

	case reflect.Array:
		addr = v.UnsafeAddr()

	case reflect.Slice:
		if v.Index(0).Kind() == reflect.String && C.H5Tis_variable_str(dtype.id) == 0 {
			tmp_slice = make([]byte, v.Len()*int(dtype.Size()))
			addr = reflect.ValueOf(tmp_slice).Pointer()
			post_process = true
		} else {
			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.UnsafeAddr()
	}

	rc := C.H5Dread(s.id, dtype.id, 0, 0, 0, unsafe.Pointer(addr))
	err := h5err(rc)

	if err == nil && post_process {
		str_len := int(dtype.Size())
		p := 0
		for i := 0; i < v.Len(); i++ {
			str := tmp_slice[p : p+str_len]
			n := bytes.Index(str, []byte{0})
			if n < 0 {
				n = str_len
			}
			v.Index(i).SetString(string(str[:n]))
			p += str_len
		}
	}

	return err
}
Esempio n. 5
0
// ReadSubset reads a subset of raw data from a dataset into a buffer.
func (s *Dataset) ReadSubset(data interface{}, memspace, filespace *Dataspace) error {
	dtype, err := s.Datatype()
	defer dtype.Close()
	if err != nil {
		return err
	}

	var addr unsafe.Pointer
	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.H5Dread(s.id, dtype.id, memspace_id, filespace_id, 0, addr)
	err = h5err(rc)
	return err
}