Astro-GuiGeek
Astro-GuiGeek

Reputation: 13

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

Using the Astropy library, I created a FITS image which is made by interpolation from 2 actual FITS 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 FITS 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!

Upvotes: 1

Views: 3047

Answers (1)

jm22b
jm22b

Reputation: 425

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

Upvotes: 1

Related Questions