Astropy FITS Image Manipulating

170 Views Asked by At

I have a task for a course and I am working with NASA FITS files. I have two images and their dimensions which are being used in the projection of an image needed to be reshaped. What I mean from reshaping is that

Filename: jw02107-o039_t018_miri_f1130w_i2d.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     349   ()      
  1  SCI           1 ImageHDU        75   (2379, 1178)   float32   
  2  ERR           1 ImageHDU        10   (2379, 1178)   float32   
  3  CON           1 ImageHDU         9   (2379, 1178)   int32   
  4  WHT           1 ImageHDU         9   (2379, 1178)   float32   
  5  VAR_POISSON    1 ImageHDU         9   (2379, 1178)   float32   
  6  VAR_RNOISE    1 ImageHDU         9   (2379, 1178)   float32   
  7  VAR_FLAT      1 ImageHDU         9   (2379, 1178)   float32   
  8  HDRTAB        1 BinTableHDU    816   12R x 403C   [23A, 5A, 3A, 48A, 7A, 13A, 6A, 5A, 7A, 10A, 4A, L, D, D, D, D, 32A, 48A, 70A, 11A, 2A, D, 47A, D, 10A, 12A, 23A, 23A, 26A, 11A, 5A, 3A, 3A, 2A, 1A, 2A, 1A, L, 12A, 6A, 2A, 26A, 20A, 27A, 10A, K, L, L, L, L, 7A, 7A, 5A, D, D, D, D, D, D, 27A, D, D, D, 4A, 8A, D, D, 6A, D, D, D, D, D, D, D, 4A, D, D, D, D, D, 3A, 4A, D, D, D, D, D, D, D, D, D, K, 5A, 9A, D, D, D, D, D, D, D, D, D, 6A, D, D, K, K, D, D, K, K, D, D, K, K, K, K, K, D, D, D, D, D, D, D, D, K, K, L, L, K, K, D, D, D, D, D, D, D, 4A, K, K, K, K, K, K, D, D, D, D, 12A, D, D, K, D, K, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, 7A, 10A, D, D, D, D, D, D, D, D, D, D, D, D, D, 10A, 11A, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, K, 27A, 27A, 10A, D, D, D, D, D, D, D, 9A, 27A, D, D, D, D, D, D, D, 8A, 14A, 31A, D, D, 3A, 3A, D, 31A, 3A, 37A, D, D, 39A, 31A, 3A, 3A, 3A, 3A, 3A, D, 31A, 3A, 3A, 3A, D, D, 36A, 31A, 3A, 3A, D, D, 33A, D, 36A, D, 3A, D, D, 32A, 31A, 37A, D, D, D, 3A, D, D, D, D, D, D, D, D, 3A, D, D, D, D, D, 8A, D, D, D, D, D, 8A, 8A, D, D, D, D, 8A, 8A, D, 7A, 7A, D, D, 7A, 8A, D, 8A, 8A, D, D, D, 8A, D, D, 8A, 8A, 8A, D, 8A, 8A, 8A, 8A, D, D, D, D, D, D, 8A, D, D, D, 5A, D, L, 6A, D, D, D, D, 4A, D, D, D, K, D, D, D, D, D, D, 12A, 12A, D, 3A, 3A, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, 117A, D, D, D, D, D, D, K, D, D, D, D]   
  9  ASDF          1 BinTableHDU     11   1R x 1C   [36428B]   
None 

I have this file from the James Webb Space Telescope from the MIRI instrument which you can see in the name of the file. This file has the images which are in the dimensions of (2379,1178). And

Filename: jw02107-o040_t018_nircam_clear-f335m_i2d.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     369   ()      
  1  SCI           1 ImageHDU        75   (4079, 2190)   float32   
  2  ERR           1 ImageHDU        10   (4079, 2190)   float32   
  3  CON           1 ImageHDU         9   (4079, 2190)   int32   
  4  WHT           1 ImageHDU         9   (4079, 2190)   float32   
  5  VAR_POISSON    1 ImageHDU         9   (4079, 2190)   float32   
  6  VAR_RNOISE    1 ImageHDU         9   (4079, 2190)   float32   
  7  VAR_FLAT      1 ImageHDU         9   (4079, 2190)   float32   
  8  HDRTAB        1 BinTableHDU    816   8R x 403C   [23A, 5A, 3A, 48A, 7A, 13A, 6A, 5A, 7A, 10A, 4A, L, D, D, D, D, 32A, 48A, 70A, 11A, 2A, D, 47A, D, 10A, 12A, 23A, 23A, 26A, 11A, 5A, 3A, 3A, 2A, 1A, 2A, 1A, L, 14A, 13A, 2A, 26A, 20A, 27A, 10A, K, L, L, L, L, 7A, 7A, 5A, D, D, D, D, D, D, 27A, D, D, D, 6A, 8A, 1A, 4A, 5A, 5A, L, D, D, D, D, D, D, D, D, D, D, D, D, 4A, D, D, D, D, D, D, D, D, D, K, 5A, 9A, D, D, D, D, D, D, D, D, D, 7A, D, D, K, K, D, D, K, K, D, D, K, K, K, K, K, D, D, D, D, D, D, D, D, K, K, L, L, K, K, D, D, D, D, D, D, D, 4A, K, K, K, K, K, K, D, D, D, D, 4A, D, D, K, D, K, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, 7A, 10A, D, D, D, D, D, D, D, D, D, D, D, D, D, 10A, 10A, D, D, D, D, D, D, D, D, D, D, D, D, K, K, D, 4A, K, K, K, D, 4A, K, K, K, D, 4A, K, D, D, K, 27A, 27A, 10A, D, D, D, D, D, D, D, 9A, 27A, D, D, D, D, D, D, D, 8A, 14A, 33A, D, D, 3A, 3A, D, 33A, 3A, 39A, D, D, 41A, 33A, 3A, 3A, 3A, 3A, 3A, D, 33A, 3A, 3A, 3A, D, D, 38A, 33A, 3A, 3A, D, 35A, 35A, D, 38A, D, 3A, D, D, D, D, 39A, D, D, D, 3A, D, 38A, D, 40A, 37A, D, D, D, 3A, D, D, D, D, D, 8A, D, D, D, D, D, 8A, 8A, D, D, D, D, D, 8A, D, 7A, 7A, D, D, 7A, 8A, D, D, 8A, D, D, D, 8A, D, 8A, 8A, 8A, 8A, D, D, D, 8A, 8A, D, D, D, D, 8A, D, 8A, D, D, D, 5A, D, L, 6A, D, D, D, D, 4A, D, D, D, K, D, D, D, D, D, D, 12A, 12A, D, 3A, 3A, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, 117A, D, D, D, D, D, D, K, D, D, D, D]   
  9  ASDF          1 BinTableHDU     11   1R x 1C   [36706B]   
None 

I have this from the NIRcam instrument. And this file's images are in the dimensions of (4079, 2190).

When I project them on the Jupyter Notebook, they are projected on a cartesian coordinate system created by the help of numpy and matplotlib.

NIRcam image is projected on the cartesian coordinate system which is x = 4079 and y = 2190. MIRI image is projectd on the cartesian coordinate system which is x = 2379 and y = 1178.

So, my question is that how can make their dimensions equal in the projection step. I mean, for example, how can I project them both on a cartesian coordinate system which is x = 5000 and y = 2000 ?

I tried to use WCS module and FITS_tools module to rehape it but somehow I could not. In the WCS module, I could not find the appropriate method to use and in the FITS_tools, I had an error like,

local variable 'image' referenced before assignment

What is the solution to reshape and reproject the images for this situation? I would be so glad if you can help out. Thank you.

1

There are 1 best solutions below

0
keflavich On

You need to use reproject (https://reproject.readthedocs.io/en/stable/). From the frontpage example:

from reproject import reproject_interp
array, footprint = reproject_interp(hdu2, hdu1.header)

so you'd do:

from astropy.io import fits
hdu1 = fits.open('JWST_File1.fits')['SCI']
hdu2 = fits.open('JWST_File2.fits')['SCI']
array, footprint = reproject_interp(hdu2, hdu1.header)