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

	enc := binary.NewEncoder(w)
	err = enc.Encode(axis.nbins)
	if err != nil {
		return err
	}

	err = enc.Encode(axis.low)
	if err != nil {
		return err
	}

	err = enc.Encode(axis.high)
	if err != nil {
		return err
	}

	err = enc.Encode(axis.size)
	if err != nil {
		return err
	}

	return err
}
예제 #2
0
파일: image.go 프로젝트: ramvasanth/fitsio
// Write writes the given image data to the HDU
func (img *imageHDU) Write(data interface{}) error {
	var err error
	rv := reflect.ValueOf(data).Elem()
	if !rv.CanAddr() {
		return fmt.Errorf("fitsio: %T is not addressable", data)
	}

	hdr := img.Header()
	naxes := len(hdr.Axes())
	if naxes == 0 {
		return nil
	}
	nelmts := 1
	for _, dim := range hdr.Axes() {
		nelmts *= dim
	}

	pixsz := hdr.Bitpix() / 8
	if pixsz < 0 {
		pixsz = -pixsz
	}

	img.raw = make([]byte, pixsz*nelmts)
	buf := &sectionWriter{
		buf: img.raw,
		beg: 0,
	}
	enc := binary.NewEncoder(buf)
	enc.Order = binary.BigEndian

	switch data := rv.Interface().(type) {
	case []byte:
		if hdr.Bitpix() != 8 {
			return fmt.Errorf("fitsio: got a %T but bitpix!=%d", data, hdr.Bitpix())
		}
		for _, v := range data {
			err = enc.Encode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
		}

	case []int8:
		if hdr.Bitpix() != 8 {
			return fmt.Errorf("fitsio: got a %T but bitpix!=%d", data, hdr.Bitpix())
		}
		for _, v := range data {
			err = enc.Encode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
		}

	case []int16:
		if hdr.Bitpix() != 16 {
			return fmt.Errorf("fitsio: got a %T but bitpix!=%d", data, hdr.Bitpix())
		}
		for _, v := range data {
			err = enc.Encode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
		}

	case []int32:
		if hdr.Bitpix() != 32 {
			return fmt.Errorf("fitsio: got a %T but bitpix!=%d", data, hdr.Bitpix())
		}
		for _, v := range data {
			err = enc.Encode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
		}

	case []int64:
		if hdr.Bitpix() != 64 {
			return fmt.Errorf("fitsio: got a %T but bitpix!=%d", data, hdr.Bitpix())
		}
		for _, v := range data {
			err = enc.Encode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
		}

	case []float32:
		if hdr.Bitpix() != -32 {
			return fmt.Errorf("fitsio: got a %T but bitpix!=%d", data, hdr.Bitpix())
		}
		for _, v := range data {
			err = enc.Encode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
		}

	case []float64:
		if hdr.Bitpix() != -64 {
			return fmt.Errorf("fitsio: got a %T but bitpix!=%d", data, hdr.Bitpix())
		}
		for _, v := range data {
			err = enc.Encode(&v)
			if err != nil {
				return fmt.Errorf("fitsio: %v", err)
			}
		}

	default:
		return fmt.Errorf("fitsio: invalid image type (%T)", rv.Interface())
	}

	//img.raw = buf.Bytes()
	return err
}
예제 #3
0
파일: column.go 프로젝트: ramvasanth/fitsio
// writeBin writes the value at column number icol and row irow, from ptr.
func (col *Column) writeBin(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

		buf := &sectionWriter{
			buf: table.data[beg:end],
			beg: 0,
		}
		benc := binary.NewEncoder(buf)
		benc.Order = binary.BigEndian

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

		switch col.dtype.dsize {
		case 8:
			data := [2]int32{int32(nmax), int32(len(table.heap))}
			err = benc.Encode(&data)
			if err != nil {
				return fmt.Errorf("fitsio: problem encoding slice 32b-descriptor: %v\n", err)
			}

		case 16:
			data := [2]int64{int64(nmax), int64(len(table.heap))}
			err = benc.Encode(&data)
			if err != nil {
				return fmt.Errorf("fitsio: problem encoding slice 64b-descriptor: %v\n", err)
			}
		}

		{
			buf := new(bytes.Buffer)
			buf.Grow(nmax * col.dtype.hsize)
			benc = binary.NewEncoder(buf)
			benc.Order = binary.BigEndian

			for i := 0; i < nmax; i++ {
				err = benc.Encode(slice.Index(i).Addr().Interface())
				if err != nil {
					return fmt.Errorf("fitsio: problem encoding: %v", err)
				}
			}
			table.heap = append(table.heap, buf.Bytes()...)
		}

	case reflect.Array:

		beg := table.rowsz*int(irow) + col.offset
		end := beg + (col.dtype.dsize * col.dtype.len)

		//buf := new(bytes.Buffer)
		buf := &sectionWriter{
			buf: table.data[beg:end],
			beg: 0,
		}
		benc := binary.NewEncoder(buf)
		benc.Order = binary.BigEndian
		err = benc.Encode(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

		//buf := bytes.NewBuffer(row)
		buf := &sectionWriter{
			buf: table.data[beg:end],
			beg: 0,
		}

		benc := binary.NewEncoder(buf)
		benc.Order = binary.BigEndian
		err = benc.Encode(ptr)

	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

		//buf := bytes.NewBuffer(row)
		buf := &sectionWriter{
			buf: table.data[beg:end],
			beg: 0,
		}

		str := rv.String()
		data := make([]byte, 0, len(str)+1)
		data = append(data, '\x00')
		data = append(data, []byte(str)...)
		n := len(data)
		if n > end-beg {
			n = end - beg
		}
		if n < end-beg {
			data = append(data, bytes.Repeat([]byte("\x00"), end-beg-n)...)
			n = len(data)
		}
		_, err = buf.Write(data[:n])

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

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

	return err
}