1

Hi I am trying to extract a layer of my fits cube that as NAXIS 4 and write the data from one layer to a new file. Below is what I have tried.

'''

import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits

fname = ( '/home/natasha/Documents/J1601.7-7544_aFix_pol_I_15arcsec_5pln_cor.fits')  #"MAR24/meerkat-hydra-selfcal-1-MFS-image.fits"  # <---- Change me
with fits.open(fname) as hdul:
    data = hdul[0].data
    hdr = hdul[0].header
wcs = WCS(hdr)
fig = plt.figure(figsize=(12,12))

newdata = data[:,:,0,0]
hdu = fits.TableHDU(data=newdata)
hdu.writeto('/home/natasha/Documents/J1601test.fits')

ax = fig.add_subplot(projection=wcs.celestial)
ax.imshow(data[0,0,:,:], vmin=-1e-4, vmax=1e-3)
ax.set_xlabel("RA")
ax.set_ylabel("Dec")

'''

But I get : '''

Traceback (most recent call last):
  File "new 1.py", line 27, in <module>
    hdu = fits.TableHDU(data=newdata)
  File "/usr/lib/python3/dist-packages/astropy/io/fits/hdu/table.py", line 738, in __init__
    super().__init__(data, header, name=name, ver=ver, character_as_bytes=character_as_bytes)
  File "/usr/lib/python3/dist-packages/astropy/io/fits/hdu/table.py", line 368, in __init__
    raise TypeError('Table data has incorrect type.')
TypeError: Table data has incorrect type.

'''

Natasha
  • 11
  • 1
  • That message appears when the data doesn't pass this check `isinstance(data, np.ndarray) and data.dtype.fields is not None`. I don't have your data file so I can't reproduce the issue. – Zev Aug 18 '21 at 13:46
  • Is there a specific reason you want to use a TableHDU (which is for ASCII tables). If this is just a slice of an image cube you can use `PrimaryHDU` instead. Or you can even use the [`writeto()`](https://docs.astropy.org/en/stable/io/fits/api/files.html#astropy.io.fits.writeto) convenience function. – Iguananaut Aug 19 '21 at 10:05
  • See also https://stackoverflow.com/questions/31887478/write-3d-numpy-array-to-fits-file-with-astropy/31923640#31923640 – Iguananaut Aug 19 '21 at 10:05

0 Answers0