[AstroPy] AstroPy Digest, Vol 68, Issue 13

Martin Raue martin.raue at desy.de
Tue May 1 05:41:30 EDT 2012


> 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')
> 
> 
> -----------------------------------
> 
> Martin Raue
> 
> Group Leader - LEXI Young Investigator Group
> "Cosmic radiation fields and search for dark matter in the early universe"
> http://wwwiexp.desy.de/groups/astroparticle/crf/
> 
> University of Hamburg
> Institute for Experimental Physics
> Luruper Chaussee 149
> D-22761 Hamburg
> Germany
> 
> Phone  : +49-40-8998-2993
> Fax    : +49-40-8998-2170
> E-Mail : martin.raue at desy.de
> 
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Mon, 30 Apr 2012 11:18:25 -0400
> From: Erik Bray <embray at stsci.edu>
> Subject: Re: [AstroPy] Writing unsigned int with pyfits
> To: <astropy at scipy.org>
> Message-ID: <4F9EAD41.2050204 at stsci.edu>
> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed
> 
> 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.

For table creation (i.e. in Column) maybe one could also support for the pseudo data types S, U and V, as done in cfitsio, which then get automatically converted to the correct TZERO/TSCALE values.
http://heasarc.gsfc.nasa.gov/docs/software/fitsio/quick/node10.html

m.

> 
> Erik
> 
> 
> ------------------------------
> 
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
> 
> 
> End of AstroPy Digest, Vol 68, Issue 13
> ***************************************




More information about the AstroPy mailing list