예제 #1
0
파일: axis1d.go 프로젝트: ebusato/hbook
func (axis *EvenBinAxis) RioUnmarshal(r io.Reader) error {
	var err error

	dec := binary.NewDecoder(r)
	err = dec.Decode(&axis.nbins)
	if err != nil {
		return err
	}

	err = dec.Decode(&axis.low)
	if err != nil {
		return err
	}

	err = dec.Decode(&axis.high)
	if err != nil {
		return err
	}

	err = dec.Decode(&axis.size)
	if err != nil {
		return err
	}

	return err
}
예제 #2
0
파일: image.go 프로젝트: ramvasanth/fitsio
// Read reads the image data into ptr
func (img *imageHDU) Read(ptr interface{}) error {
	var err error
	if img.raw == nil {
		// FIXME(sbinet): load data from file
		panic(fmt.Errorf("image with no raw data"))
	}

	hdr := img.Header()
	nelmts := 1
	for _, dim := range hdr.Axes() {
		nelmts *= dim
	}

	if len(hdr.Axes()) <= 0 {
		nelmts = 0
	}

	//rv := reflect.Indirect(reflect.ValueOf(ptr))
	rv := reflect.ValueOf(ptr).Elem()
	rt := rv.Type()

	if rt.Kind() != reflect.Slice && rt.Kind() != reflect.Array {
		return fmt.Errorf("fitsio: invalid type (%v). expected array or slice", rt.Kind())
	}

	pixsz := hdr.Bitpix() / 8
	if pixsz < 0 {
		pixsz = -pixsz
	}
	otype := rt.Elem()
	if int(otype.Size()) != pixsz {
		return fmt.Errorf(
			"fitsio: element-size do not match. bitpix=%d. elmt-size=%d (conversion not yet supported)",
			hdr.Bitpix(), otype.Size())
	}

	if rt.Kind() == reflect.Slice {
		rv.SetLen(nelmts)
	}

	var cnv = func(v reflect.Value) reflect.Value {
		return v
	}

	bdec := binary.NewDecoder(bytes.NewBuffer(img.raw))
	bdec.Order = binary.BigEndian

	switch hdr.Bitpix() {
	case 8:
		itype := reflect.TypeOf((*byte)(nil)).Elem()
		if !rt.Elem().ConvertibleTo(itype) {
			return fmt.Errorf("fitsio: can not convert []byte to %s", rt.Name())
		}
		if itype != otype {
			cnv = func(v reflect.Value) reflect.Value {
				return v.Convert(otype)
			}
		}
		for i := 0; i < nelmts; i++ {
			var v byte
			err = bdec.Decode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
			rv.Index(i).Set(cnv(reflect.ValueOf(v)))
		}

	case 16:
		itype := reflect.TypeOf((*int16)(nil)).Elem()
		if !rt.Elem().ConvertibleTo(itype) {
			return fmt.Errorf("fitsio: can not convert []int16 to %s", rt.Name())
		}
		if itype != otype {
			cnv = func(v reflect.Value) reflect.Value {
				return v.Convert(otype)
			}
		}
		for i := 0; i < nelmts; i++ {
			var v int16
			err = bdec.Decode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
			rv.Index(i).Set(cnv(reflect.ValueOf(v)))
		}

	case 32:
		itype := reflect.TypeOf((*int32)(nil)).Elem()
		if !rt.Elem().ConvertibleTo(itype) {
			return fmt.Errorf("fitsio: can not convert []int32 to %s", rt.Name())
		}
		if itype != otype {
			cnv = func(v reflect.Value) reflect.Value {
				return v.Convert(otype)
			}
		}
		for i := 0; i < nelmts; i++ {
			var v int32
			err = bdec.Decode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
			rv.Index(i).Set(cnv(reflect.ValueOf(v)))
		}

	case 64:
		itype := reflect.TypeOf((*int64)(nil)).Elem()
		if !rt.Elem().ConvertibleTo(itype) {
			return fmt.Errorf("fitsio: can not convert []int64 to %s", rt.Name())
		}
		if itype != otype {
			cnv = func(v reflect.Value) reflect.Value {
				return v.Convert(otype)
			}
		}
		for i := 0; i < nelmts; i++ {
			var v int64
			err = bdec.Decode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
			rv.Index(i).Set(cnv(reflect.ValueOf(v)))
		}

	case -32:
		itype := reflect.TypeOf((*float32)(nil)).Elem()
		if !rt.Elem().ConvertibleTo(itype) {
			return fmt.Errorf("fitsio: can not convert []float32 to %s", rt.Name())
		}
		if itype != otype {
			cnv = func(v reflect.Value) reflect.Value {
				return v.Convert(otype)
			}
		}
		for i := 0; i < nelmts; i++ {
			var v float32
			err = bdec.Decode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
			rv.Index(i).Set(cnv(reflect.ValueOf(v)))
		}

	case -64:
		itype := reflect.TypeOf((*byte)(nil)).Elem()
		if !rt.Elem().ConvertibleTo(itype) {
			return fmt.Errorf("fitsio: can not convert []float64 to %s", rt.Name())
		}
		if itype != otype {
			cnv = func(v reflect.Value) reflect.Value {
				return v.Convert(otype)
			}
		}
		for i := 0; i < nelmts; i++ {
			var v float64
			err = bdec.Decode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
			rv.Index(i).Set(cnv(reflect.ValueOf(v)))
		}

	default:
		return fmt.Errorf("invalid image type [bitpix=%d]", hdr.Bitpix())
	}

	return err
}
예제 #3
0
파일: column.go 프로젝트: ramvasanth/fitsio
// readBin reads the value at column number icol and row irow, into ptr.
func (col *Column) readBin(table *Table, icol int, irow int64, ptr interface{}) error {
	var err error

	rv := reflect.Indirect(reflect.ValueOf(ptr))
	rt := reflect.TypeOf(rv.Interface())

	switch rt.Kind() {
	case reflect.Slice:

		beg := table.rowsz*int(irow) + col.offset
		end := beg + col.dtype.dsize
		row := table.data[beg:end]
		buf := bytes.NewBuffer(row)
		bdec := binary.NewDecoder(buf)
		bdec.Order = binary.BigEndian

		slice := reflect.ValueOf(ptr).Elem()
		nmax := 0

		switch col.dtype.dsize {
		case 8:
			var n int32
			var offset int32
			err = bdec.Decode(&n)
			if err != nil {
				return fmt.Errorf("fitsio: problem decoding slice 32b-length: %v\n", err)
			}
			err = bdec.Decode(&offset)
			if err != nil {
				return fmt.Errorf("fitsio: problem decoding slice 32b-offset: %v\n", err)
			}
			beg = int(offset)
			end = beg + int(n)*int(col.dtype.gotype.Elem().Size())
			nmax = int(n)

		case 16:
			var n int64
			var offset int64
			err = bdec.Decode(&n)
			if err != nil {
				return fmt.Errorf("fitsio: problem decoding slice 64b-length: %v\n", err)
			}
			err = bdec.Decode(&offset)
			if err != nil {
				return fmt.Errorf("fitsio: problem decoding slice 64b-offset: %v\n", err)
			}
			beg = int(offset)
			end = beg + int(n)*int(col.dtype.gotype.Elem().Size())
			nmax = int(n)
		}

		buf = bytes.NewBuffer(table.heap[beg:end])
		bdec = binary.NewDecoder(buf)
		bdec.Order = binary.BigEndian

		slice.SetLen(0)
		for i := 0; i < nmax; i++ {
			vv := reflect.New(rt.Elem())
			err = bdec.Decode(vv.Interface())
			if err != nil {
				return fmt.Errorf("fitsio: problem encoding: %v", err)
			}
			slice = reflect.Append(slice, vv.Elem())
		}
		if err != nil {
			return fmt.Errorf("fitsio: %v\n", err)
		}
		rv := reflect.ValueOf(ptr)
		rv.Elem().Set(slice)

	case reflect.Array:

		beg := table.rowsz*int(irow) + col.offset
		end := beg + (col.dtype.dsize * col.dtype.len)
		row := table.data[beg:end]
		buf := bytes.NewBuffer(row)
		bdec := binary.NewDecoder(buf)
		bdec.Order = binary.BigEndian

		err = bdec.Decode(ptr)
		if err != nil {
			return fmt.Errorf("fitsio: %v\n", err)
		}

	case reflect.Bool,
		reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Float32, reflect.Float64,
		reflect.Complex64, reflect.Complex128:

		//scalar := true
		//err = col.decode(table, n, rt, rv, scalar)

		beg := table.rowsz*int(irow) + col.offset
		end := beg + col.dtype.dsize
		row := table.data[beg:end]
		buf := bytes.NewBuffer(row)
		bdec := binary.NewDecoder(buf)
		bdec.Order = binary.BigEndian
		err = bdec.Decode(ptr)
		if err != nil {
			return fmt.Errorf("fitsio: %v\n", err)
		}

	case reflect.String:

		//scalar := true
		//err = col.decode(table, n, rt, rv, scalar)

		beg := table.rowsz*int(irow) + col.offset
		end := beg + col.dtype.dsize
		row := table.data[beg:end]
		str := ""
		if row[0] == '\x00' {
			str = string(row[1:])
			str = strings.TrimRight(str, string([]byte("\x00")))
		} else {
			str = string(row)
		}

		rv.SetString(str)

	default:
		return fmt.Errorf("fitsio: binary-table can not read/write %v", rt.Kind())
	}
	return err
}