[AstroPy] BSCALE/BZERO

Matthew Craig mcraig at mnstate.edu
Sat Oct 28 16:52:58 EDT 2017


Hi,

That's why I don't understand the problem because I have been told that
the PIXEL VALUES should remain the same it's just that the dynamic range with the BITPIX=16,BZERO=32767 should be from 0 to 65535.

I think this is true as long as the data is already in the range 0 to 65535; in that case the data is actually stored in the FITS files as signed 16 bit integers which are transparently changed to unsigned int when you read it with astropy.io.fits or other FITS tools.

In your case you will need to transform the pixel values because you need to change the range of your data.

If I understand your use case, you need to squeeze the data into an int16 range because another tool handles that.

Two options are to manually calculate a BZERO/BSCALE and scale the data with that or to convert the data int16 or uint16 and let astropy.io.fits set BZERO/BSCALE for you.

I would recommend the latter…it will not affect the dynamic range of the data and saves you the hassle of calculating those things yourself. The other reason is that numpy has a way of representing uint16 or int16.

I’ll address how to do want you want by converting the data to uint16.

In your case you need to scale the values so that they fit in the range 0 to 65535.

I would do something like (using the same example data as before)(have not tested this :)):

from __future__ import division  # if you are running Python 2
my_data = np.array([[90000, 2.0], [-1.0, -4000]])
my_hdu = fits.PrimaryHDU(my_data)
data_min = my_hdu.data.min()
data_max = my_hdu.data.max()

# Yes, the maximum value should be 65535 not 65534
new_data = 65535 * (my_hdu.data - data_min) / (data_max - data_min)

# The line below will automatically revise BITPIX/BZERO/BSCALE
my_hdu.data = new_data.astype(np.uint16)

my_hdu.writeto('ui16_image.fits’)

mh_hdu.data
array([[65535,   2790],
          [ 2788,           0]], dtype=uint16)

# Note that the largest and smallest pixel values in the original array are still the largest and smallest
# pixels in the result.

Matt Craig

schedule:  http://physics.mnstate.edu/craig
——
Professor
Department of Physics and Astronomy
Minnesota State University Moorhead
1104 7th Ave S, Moorhead MN 56563

office: Hagen 307F
phone: (218) 477-2439
fax: (218) 477-2290

On Oct 28, 2017, at 1:10 PM, Diego Farias <dnfarias at uc.cl<mailto:dnfarias at uc.cl>> wrote:

Hi Matt and everyone:

I did the same 'experiments'! and I realized that I 'loose' information if I scale to 'uint16' . That's why I don't understand the problem because I have been told that
the PIXEL VALUES should remain the same it's just that the dynamic range with the BITPIX=16,BZERO=32767 should be from 0 to 65535. As I'm not clever enough I'm stuck in this step.. I'm guessing that later we need to use another astronomical tool that requires the images with BITPIX = 16. In my despair, I found this:

https://github.com/varenius/salsa/blob/master/Control_program/spectrum.py

where the important step is

#Since using int16 as datatype we use bscale and bzero to keep dynamic range. #SalsaJ cannot read bitpix correctly except 16 bit. If SalsaJ could read bitpix #we could just have BITPIX -64 and skip Bscale, Bzero, i.e. just remove #astype above.


datamin = np.min(self.data)
datamax = np.max(self.data) bscale = (datamax-datamin)/65534.0 #shouldn't be 65535.0? bzero = datamin+bscale*32767.0 scaledata = (self.data - bzero)/bscale hdu.data = scaledata.reshape(1, 1, self.nchans).astype(np.int16)




Shouldn't this also changes the values? (in this case, spectrum values). Also,
bscale ~ 1., bzero ~ 326767.

Thanks a lot,

Diego










_______________________________________________
AstroPy mailing list
AstroPy at python.org<mailto:AstroPy at python.org>
https://mail.python.org/mailman/listinfo/astropy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20171028/faf7d9df/attachment-0001.html>


More information about the AstroPy mailing list