Downloading and Compressing a FITS file using SunPy, aiapy, and Astropy#

Written by Matt Wentzel-Long. The purpose of this demo is to demonstrate:

  1. SunPy’s ability to retrieve a Level 1 AIA data, 2) deconvolve the FITS file using aiapy,

  2. convert this to Level 1.5 AIA data using aiapy, and 4) demonstrate the compression ability of Astropy when saving the file.

First import the packages

import astropy.units as u
from astropy.io.fits import CompImageHDU

from sunpy.map import Map
from sunpy.net import Fido, attrs as a

import aiapy.psf as psf_
from aiapy.calibrate import register, update_pointing

import os

Use the SunPy tool Fido to find and download level 1 AIA data.

q = Fido.search(a.Time('2011-06-07T06:52:00', '2011-06-07T06:52:10'),
                a.Instrument('AIA'),
                a.Wavelength(wavemin=171*u.angstrom, wavemax=171*u.angstrom))
aia_map = Map(Fido.fetch(q))

Compute the point-spread function (PSF) and use it to deconvolve the image. Deconvolution should only be performed on level 1 data (see this guide for prepairing AIA data using aiapy). Warning: the PSF computation can take over 16 minutes on a CPU. If you have an NVIDIA GPU and CuPy installed, then PSF will automatically use it. See the PSF documentation for details.

psf = psf_.psf(aia_map.wavelength)
map_deconvolved = psf_.deconvolve(aia_map, psf=psf)

Convert to level 1.5 AIA data. See the registering and aligning level 1 data example in aiapy documentation for more details.

m_updated_pointing = update_pointing(map_deconvolved)
m_registered = register(m_updated_pointing)
m_normalized = Map(m_registered.data/m_registered.exposure_time.to(u.s).value, 
                   m_registered.meta)

Save the deconvolved image as a FITS file without compression using SunPy. Note: this resulted in a 128 MB file while testing.

m_normalized.save('aia_map_deconv.fits')
print(os.path.getsize('aia_map_deconv.fits'))
WARNING: VerifyWarning: Invalid 'BLANK' keyword in header.  The 'BLANK' keyword is only applicable to integer data, and will be ignored in this HDU. [astropy.io.fits.hdu.image]
134104320

This time pass SunPy the CompImagHDU compression routine from Astropy.

m_normalized.save('aia_map_deconv_comp.fits', hdu_type=CompImageHDU)
print(os.path.getsize('aia_map_deconv_comp.fits'))
14221440