Is there any way to use arithmetic ops on FITS files in Python?

635 Views Asked by At

I'm fairly new to Python, and I have been trying to recreate a working IDL program to Python, but I'm stuck and keep getting errors. I haven't been able to find a solution yet. The program requires 4 FITS files in total (img and correctional images dark, flat1, flat2). The operations are as follows:

flat12 = (flat1 + flat2)/2

img1 = (img - dark)/flat12

The said files have dimensions (1024,1024,1). I have resized them to (1024,1024) to be able to even use im_show() function.

I have also tried using cv2.add(), but I get this:

TypeError: Expected Ptr for argument 'src1'

Is there any workaround for this? Thanks in advance.

1

There are 1 best solutions below

1
saimn On

To read your FITS files use astropy.io.fits: http://docs.astropy.org/en/latest/io/fits/index.html This will give you Numpy arrays (and FITS headers if needed, there are different ways to do this, as explained in the documentation), so you could do something like:

>>> from astropy.io import fits
>>> img = fits.getdata('image.fits', ext=0) # extension number depends on your FITS files
>>> dark = fits.getdata('dark.fits') # by default it reads the first "data" extension
>>> darksub = img - dark
>>> fits.writeto('out.fits', darksub) # save output

If your data has an extra dimension, as shown with the (1024,1024,1) shape, and if you want to remove that axis, you can use the normal Numpy array slicing syntax: darksub = img[0] - dark[0]. Otherwise in the example above it will produce and save a (1024,1024,1) image.