Note
Click here to download the full example code or to run this example in your browser via Binder
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) convert this to Level 1.5 AIA data using aiapy, 3) deconvolve the FITS file using aiapy, and 4) demonstrate the compression ability of Astropy when saving the file.
First import the packages
import astropy
import astropy.units as u
from astropy.io.fits import CompImageHDU
from sunpy.net import Fido, attrs as a
import sunpy.map
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 = sunpy.map.Map(Fido.fetch(q))
Out:
Files Downloaded: 0%| | 0/1 [00:00<?, ?file/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 0%| | 0.00/12.3M [00:00<?, ?B/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 0%| | 100/12.3M [00:02<90:54:50, 37.5B/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 0%| | 34.1k/12.3M [00:02<12:16, 16.6kB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 1%| | 104k/12.3M [00:02<03:20, 60.8kB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 2%|1 | 218k/12.3M [00:03<01:18, 153kB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 2%|2 | 287k/12.3M [00:03<01:02, 191kB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 7%|6 | 836k/12.3M [00:03<00:13, 843kB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 9%|8 | 1.06M/12.3M [00:03<00:10, 1.05MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 10%|# | 1.28M/12.3M [00:03<00:08, 1.25MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 12%|#2 | 1.50M/12.3M [00:03<00:07, 1.44MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 14%|#4 | 1.73M/12.3M [00:03<00:06, 1.62MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 16%|#5 | 1.95M/12.3M [00:03<00:05, 1.77MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 18%|#7 | 2.18M/12.3M [00:03<00:05, 1.90MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 20%|#9 | 2.42M/12.3M [00:04<00:04, 2.01MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 22%|##1 | 2.67M/12.3M [00:04<00:04, 2.14MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 24%|##3 | 2.90M/12.3M [00:04<00:04, 2.19MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 26%|##5 | 3.16M/12.3M [00:04<00:03, 2.29MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 28%|##7 | 3.41M/12.3M [00:04<00:03, 2.36MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 30%|##9 | 3.66M/12.3M [00:04<00:03, 2.40MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 32%|###1 | 3.91M/12.3M [00:04<00:03, 2.42MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 34%|###3 | 4.16M/12.3M [00:04<00:03, 2.46MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 36%|###6 | 4.43M/12.3M [00:04<00:03, 2.52MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 38%|###8 | 4.68M/12.3M [00:04<00:03, 2.51MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 40%|#### | 4.94M/12.3M [00:05<00:02, 2.54MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 42%|####2 | 5.21M/12.3M [00:05<00:02, 2.56MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 44%|####4 | 5.46M/12.3M [00:05<00:02, 2.56MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 47%|####6 | 5.75M/12.3M [00:05<00:02, 2.64MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 49%|####8 | 6.01M/12.3M [00:05<00:03, 1.98MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 52%|#####2 | 6.44M/12.3M [00:05<00:02, 2.42MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 55%|#####4 | 6.71M/12.3M [00:05<00:02, 2.08MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 56%|#####6 | 6.94M/12.3M [00:05<00:02, 2.00MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 58%|#####8 | 7.15M/12.3M [00:06<00:02, 1.96MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 60%|#####9 | 7.35M/12.3M [00:06<00:02, 1.65MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 61%|######1 | 7.53M/12.3M [00:06<00:02, 1.66MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 63%|######2 | 7.71M/12.3M [00:06<00:02, 1.66MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 64%|######4 | 7.88M/12.3M [00:06<00:02, 1.51MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 65%|######5 | 8.03M/12.3M [00:06<00:02, 1.52MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 67%|######6 | 8.19M/12.3M [00:06<00:02, 1.53MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 68%|######7 | 8.35M/12.3M [00:06<00:02, 1.54MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 69%|######9 | 8.51M/12.3M [00:07<00:02, 1.56MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 71%|####### | 8.67M/12.3M [00:07<00:02, 1.57MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 72%|#######1 | 8.83M/12.3M [00:07<00:02, 1.57MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 73%|#######3 | 8.99M/12.3M [00:07<00:02, 1.57MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 75%|#######4 | 9.15M/12.3M [00:07<00:01, 1.61MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 76%|#######5 | 9.32M/12.3M [00:07<00:01, 1.61MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 77%|#######7 | 9.48M/12.3M [00:07<00:01, 1.62MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 79%|#######8 | 9.64M/12.3M [00:07<00:01, 1.62MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 80%|#######9 | 9.81M/12.3M [00:07<00:01, 1.64MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 81%|########1 | 9.98M/12.3M [00:07<00:01, 1.61MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 83%|########2 | 10.2M/12.3M [00:08<00:01, 1.68MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 84%|########4 | 10.3M/12.3M [00:08<00:01, 1.68MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 85%|########5 | 10.5M/12.3M [00:08<00:01, 1.68MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 87%|########6 | 10.7M/12.3M [00:08<00:00, 1.64MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 88%|########8 | 10.8M/12.3M [00:08<00:00, 1.67MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 90%|########9 | 11.0M/12.3M [00:08<00:00, 1.56MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 91%|#########1| 11.2M/12.3M [00:08<00:00, 1.59MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 92%|#########2| 11.3M/12.3M [00:08<00:00, 1.61MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 94%|#########3| 11.5M/12.3M [00:08<00:00, 1.61MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 95%|#########5| 11.7M/12.3M [00:08<00:00, 1.64MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 96%|#########6| 11.8M/12.3M [00:09<00:00, 1.64MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 98%|#########7| 12.0M/12.3M [00:09<00:00, 1.65MB/s]
aia_lev1_171a_2011_06_07t06_52_00_35z_image_lev1.fits: 99%|#########9| 12.2M/12.3M [00:09<00:00, 1.65MB/s]
Files Downloaded: 100%|##########| 1/1 [00:21<00:00, 21.31s/file]
Files Downloaded: 100%|##########| 1/1 [00:21<00:00, 21.31s/file]
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(aia_map)
m_registered = register(m_updated_pointing)
m_normalized = sunpy.map.Map(
m_registered.data/m_registered.exposure_time.to(u.s).value,
m_registered.meta)
Compute the point-spread function (PSF) and use it to deconvolve the image. 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(m_normalized.wavelength)
map_deconvolved = psf_.deconvolve(m_normalized, psf=psf)
Save the deconvolved image as a FITS file without compression using SunPy. Note: this resulted in a 128 MB file while testing.
map_deconvolved.save('aia_map_deconv.fits')
print(os.path.getsize('aia_map_deconv.fits'))
# This time pass SunPy the `CompImagHDU <https://docs.astropy.org/en/stable/io/fits/api/images.html#compimagehdu>`_ compression routine from Astropy.
sunpy.io.fits.write('aia_map_deconv_comp.fits', map_deconvolved.data,
map_deconvolved.fits_header, hdu_type=CompImageHDU)
print(os.path.getsize('aia_map_deconv_comp.fits'))
Out:
134236800
14181120
Total running time of the script: ( 6 minutes 55.505 seconds)