Note
Go to the end to download the full example code.
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.12/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.
print(example_ndcube.wcs)
WCS Keywords
Number of WCS axes: 2
CTYPE : 'RA---TAN' 'DEC--TAN'
CRVAL : np.float64(85.59941666666666) np.float64(-4.946638888888889)
CRPIX : np.float64(-716.333144294269) np.float64(-8444.64946698226)
PC1_1 PC1_2 : np.float64(0.015029018460682027) np.float64(-9.63735777657198e-06)
PC2_1 PC2_2 : np.float64(1.0548917307845708e-05) np.float64(0.015000473845055023)
CDELT : np.float64(-0.018654788242111486) np.float64(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()
Total running time of the script: (0 minutes 0.605 seconds)