How to create an NDCube from data stored in a FITS file#

This example shows how you load in data from a FITS file to create an NDCube. Here we will use an example of a single image.

import matplotlib.pyplot as plt

from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename
from astropy.wcs import WCS

from ndcube import NDCube

We first download the example file that we will use here to show how to create an NDCube from data stored in a FITS file. Here we are using an example file from astropy.

image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')

Lets extract the image data and the header information from the FITS file. This can be achieved by using the functionality within astropy.io.fits. In this file the image information is located in the Primary HDU (extension 0).

To create an NDCube object, we need both the data array and a WCS object (e.g. an WCS). Here the data WCS information is within the header, which we can pass to WCS to create a WCS object.

example_ndcube = NDCube(image_data, WCS(image_header))
/home/docs/checkouts/readthedocs.org/user_builds/ndcube/conda/latest/lib/python3.11/site-packages/astropy/wcs/wcs.py:805: FITSFixedWarning: 'datfix' made the change 'Set MJD-OBS to 48247.575694 from DATE-OBS'.
  warnings.warn(

Now we have created an NDCube from this data. We can inspect the NDCube, such as the WCS.

WCS Keywords

Number of WCS axes: 2
CTYPE : 'RA---TAN' 'DEC--TAN'
CRVAL : 85.59941666666666 -4.946638888888889
CRPIX : -716.333144294269 -8444.64946698226
PC1_1 PC1_2  : 0.015029018460682027 -9.63735777657198e-06
PC2_1 PC2_2  : 1.0548917307845708e-05 0.015000473845055023
CDELT : -0.018654788242111486 0.018654788242111486
NAXIS : 891  893

and we can also inspect the dimensions.

print(example_ndcube.shape)
(893, 891)

We can also quickly visualize the data using the plot() method.

example_ndcube.plot()

plt.show()
creating ndcube from fitsfile

Total running time of the script: (0 minutes 0.617 seconds)

Gallery generated by Sphinx-Gallery