[AstroPy] Writing unsigned int with pyfits
Erik Bray
embray at stsci.edu
Mon Apr 30 11:18:25 EDT 2012
On 04/30/2012 09:45 AM, Martin Raue wrote:
> Dear all,
>
> I am trying to create a FITS file with a bintable extension containing unsigned integers with pyfits but could not get it to work (example below). Does maybe someone know what I am doing wrong here?
>
> Best wishes,
> Martin
>
> import numpy as np
> import pyfits as pf
>
> phdu = pf.PrimaryHDU(uint=True)
>
> a_uint = np.ones(1, dtype=np.uint32)
>
> events = pf.new_table(
> pf.ColDefs([
> pf.Column(name='EVENT_ID', array=a_uint, format='J', bscale=1, bzero=2**31)
> pf.Column(name='OBS_ID', array=a_uint, format='J', bscale=1, bzero=2**31)
> # ... a few more columns
> ])
> )
>
> hdulist = pf.HDUList([phdu, events])
>
> hdulist.writeto('tmp.fits')
>
> hdulist = None
>
> f = pf.open('tmp.fits')
>
> In [2]: f[1].data
> Out[2]:
> FITS_rec([(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)],
> dtype=[('EVENT_ID', '>i4'), ('OBS_ID', '>i4'), ('TIME', '>f4'), ('RA', '>f4'), ('DEC', '>f4'), ('DETX', '>f4'), ('DETY', '>f4'), ('ENERGY', '>f4'), ('HIL_MSW', '>f4'), ('HIL_MSL', '>f4')])
>
> In [3]: f[1].data.field('EVENT_ID')[0].dtype
> Out[3]: dtype('float64')
I believe that you are doing this correctly. However, when a column has
non-trivial BSCALE and/or BZERO values, the values in that column are
converted to floats on the fly. That's because the TSCALn and TZEROn
values themselves are actually treated as floats by the FITS standard.
So while the column format shows up as ints, the actual values given to
the user are floats with the bscale+bzero applied. If you save changes
to the file the values will be converted back to ints.
I think the problem here is that PyFITS supports unsigned ints in image
data by using the `uint=True` argument when opening a FITS file. Using
that argument causes PyFITS to recognize that you want pseudo-unsigned
ints to be returned as actual unsigned ints, and not floats. However,
it seems like it only works for images, and not for table columns.
That, perhaps, should be fixed.
Erik
More information about the AstroPy
mailing list