0

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.

Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
apoorva_9
  • 13
  • 4
  • Turn them into numpy arrays. https://stackoverflow.com/questions/7762948/how-to-convert-an-rgb-image-to-numpy-array – shanecandoit Jan 23 '20 at 17:26
  • @shanecandoit Thank you for the suggestion, but it doesn't deal with FITS files. I tried using the methods given in the thread, but converting FITS to ndarray gives me an empty tuple or class 'NoneType'. – apoorva_9 Jan 24 '20 at 04:34
  • Just a conceptual note I like to harp on: one doesn't do arithmetic operations on FITS files (though I know what you mean by asking; I'm not nitpicking). It's just useful I think to consider the conceptually difference between data, and methods of data storage. FITS is just one way of storing array data in a file on disk (along with associated metadata). So what you really want to think about is how to read the data stored in FITS files into some more general format, and perform arithmetic operations on that (in Python, that "general format" being Numpy arrays, per @saimn's answer) – Iguananaut Jan 26 '20 at 00:07
  • (and even Numpy arrays are a higher-level abstraction) – Iguananaut Jan 26 '20 at 00:07

1 Answers1

2

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.

saimn
  • 443
  • 2
  • 10
  • Note that this approach extracts the data from the FITS file and discards the header information. The distinction between the data (an array) and the header (a set of key-value pairs) is part of the reason you cannot directly perform operations on FITS files. – keflavich Jan 27 '20 at 19:08