Saving and loading sunpy Maps with FITS#

In this example we are going to look at how to save and load a GenericMap as FITS files.

FITS is a modern file format designed to meet the needs of the astronomy community. It has deep integration with Python, SunPy and Astropy as well as IDL and many other languages.

Here, even though we will be working with AIAMap specifically, the process can be extended to any GenericMap.

import astropy.units as u

import sunpy.data.sample
import sunpy.map

We begin by creating an AIAMap object using the sample data.

aia_map = sunpy.map.Map(sunpy.data.sample.AIA_171_IMAGE)
aia_map.peek(clip_interval=(1, 99.99)*u.percent)
AIA $171 \; \mathrm{\mathring{A}}$ 2011-06-07 06:33:02

We can now save this object to a FITS file to use later. Saving it like this allows us to preserve all of the metadata of the object along with the actual array data. When we load the FITS file again, we get an identical AIAMap object.

All changes to GenericMap are saved within the FITS file. For example, if we rotate the image by 45 degrees and then save it to a FITS file, these changes are reflected in both the image data and metadata when we load the data back in.

aia_map = aia_map.rotate(45*u.deg)
# Please be aware that if you try to save this twice,
# it will throw an exception rather than overwriting the file.
aia_map.save('aia_map.fits')
/home/docs/checkouts/readthedocs.org/user_builds/sunpy/conda/latest/lib/python3.10/site-packages/astropy/io/fits/hdu/image.py:627: VerifyWarning: Invalid 'BLANK' keyword in header.  The 'BLANK' keyword is only applicable to integer data, and will be ignored in this HDU.
  warnings.warn(msg, VerifyWarning)

This FITS file is portable and can be safely loaded by any FITS reader. Using Map, we can easily read our rotated image back in:

aia_map_from_fits = sunpy.map.Map('aia_map.fits')
aia_map_from_fits.peek(clip_interval=(1, 99.99)*u.percent)
AIA $171 \; \mathrm{\mathring{A}}$ 2011-06-07 06:33:02

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

Gallery generated by Sphinx-Gallery