Re: Programming and Computing with Python Right from the Forum
Posted: Mon Dec 14, 2020 1:25 pm
Test Astropy with the following codes:
- from astropy.io import fits
- fits_image_filename = fits.util.get_testdata_filepath('test0.fits')
- hdul = fits.open(fits_image_filename)
- hdul.info()
- #or
- with fits.open(fits_image_filename) as hdul:
- hdul.info()
- import matplotlib.pyplot as plt
- from astropy.visualization import astropy_mpl_style
- plt.style.use(astropy_mpl_style)
- #import astropy.coordinates as coord
- #import astropy.units as u
- #Download the example FITS files used by this example:
- from astropy.utils.data import get_pkg_data_filename
- from astropy.io import fits
- image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
- #Use astropy.io.fits.info() to display the structure of the file:
- fits.info(image_file)
- #Get image data
- image_data = fits.getdata(image_file, ext=0)
- #The data is now stored as a 2D numpy array. Print the dimensions using the shape attribute:
- print(image_data.shape)
- #Display the image data:
- plt.figure()
- plt.imshow(image_data, cmap='gray')
- plt.colorbar()
- plt.show()