Astro-GuiGeek Astro-GuiGeek - 3 years ago 101
Python Question

How to convert (or scale) a FIT image with Astropy

Using the Astropy library, I created a FIT image which is made by interpolation from 2 actual FIT images (they are scaled as "int16", the right format for the software I use : Maxim DL).

But the scale of this image is float64 and not int16. And any astronomical processing software can't read it (except FITS Liberator)

Do you have an idea how to proceed ? Can we convert a FIT image just by changing the "BITPIX" in the header ?

I tried: (following this method : Why is an image containing integer data being converted unexpectedly to floats?



from astropy.io import fits

hdu1=fits.open('mypicture.fit')
image=hdu1[0]
print(image.header['BITPIX']) # it gives : -64

image.scale('int16')
data=image.data
data.dtype
print(image.header['BITPIX']) # it gives : 16
hdu1.close()





However, when I check the newly-modified scale of "mypicture.fit", it still displays -64 !
No change was saved and applied!

Answer Source

If I understand your problem correctly, this should work.

from astropy.io import fits
import numpy as np

# create dummy fits file
a = np.array([[1,2,3],
              [4,5,6],
              [7,8,9]],dtype=np.float64)

hdu = fits.PrimaryHDU()
hdu.data = a

# looking at the header object confirms BITPIX = -64
hdu.header

# change data type
hdu.data = np.int16(hdu.data)

# look again to confirm BITPIX = 16
hdu.header
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download