Interacting with Data Using SunPy MapsΒΆ

In this example you will be learning how to create and modify SunPy Map objects.

Start by importing the necessary modules.

from __future__ import print_function, division

import numpy as np
import astropy.units as u


SunPy Maps store 2D data in a numpy array and additional data in a metadata dictionary giving information relating to the data and instrument. You can create a Map in a number of ways, including loading a FITS file or URL: mymap ='file1.fits') mymap = Or using creating manually by using tuple with the data/header within:

data = np.random.rand(20,15)
header = {}
manual_map =, header))

The data numpy array and metadata dictionary can easily be accessed:

# In this case notice that the metadata has been populated by default with the
# naxis details that correspond to the array used for the data.


[[  3.26189373e-01   1.53099899e-02   2.44182293e-01   5.98710037e-01
    2.97134935e-01   3.12094603e-01   7.31021142e-01   4.30501274e-01
    2.76895177e-01   6.71429237e-01   4.28394555e-01   2.63874829e-01
    6.52411839e-02   1.97336110e-02   2.15593382e-01]
 [  6.68758671e-02   8.24178013e-01   4.80008295e-01   2.03718270e-01
    4.32644244e-01   3.93839746e-01   4.47514681e-01   4.57641267e-01
    3.64449734e-01   2.71914862e-01   8.25185360e-01   7.59007688e-02
    4.19978009e-01   3.50380685e-02   6.27186945e-02]
 [  9.38125655e-01   8.34884451e-01   4.66292217e-01   5.80677307e-01
    8.65733412e-01   8.97886303e-01   3.38969769e-01   3.91573354e-01
    8.21762668e-01   7.19895960e-01   6.36886284e-01   1.30654491e-02
    5.37618199e-01   4.38578815e-01   2.22485494e-01]
 [  5.17847261e-01   1.10138486e-01   1.35136236e-02   7.01916457e-01
    5.14120537e-01   8.68597308e-01   7.73787921e-01   2.77583322e-01
    4.24633817e-01   1.41749338e-01   6.89478438e-01   9.22980043e-02
    5.70405513e-01   5.71015888e-01   2.00512597e-02]
 [  7.51336421e-01   4.77202231e-01   3.06294644e-01   1.60984839e-01
    5.54093140e-01   1.26357935e-01   4.63466313e-01   4.40064409e-01
    7.05901936e-01   9.66066226e-01   3.75013130e-01   5.22565170e-01
    2.90485879e-01   5.82941263e-01   9.69454190e-01]
 [  9.46955281e-01   8.49587741e-01   4.62870695e-01   2.98810684e-01
    8.17714258e-01   8.01550318e-01   8.13027572e-01   7.78637545e-01
    5.34753183e-01   8.15235432e-01   9.59066028e-02   1.06498272e-01
    5.36525884e-01   9.61197918e-01   9.00908271e-01]
 [  8.86690008e-01   7.33607530e-01   6.22332256e-02   7.34795049e-01
    7.45189859e-01   1.37254086e-01   3.18532381e-01   9.36951767e-01
    4.27600715e-01   1.40778108e-01   8.45727206e-01   1.65590244e-01
    8.56835173e-01   6.98928997e-01   9.17647756e-02]
 [  9.20919554e-01   3.22110721e-01   6.53954238e-01   1.23949685e-01
    5.97953193e-02   6.66869071e-01   4.51810512e-01   9.77234112e-01
    2.98939845e-01   8.43047366e-01   1.39373090e-01   8.44043181e-01
    1.49875898e-01   2.60480088e-01   3.09052937e-01]
 [  5.92631697e-01   2.21996461e-01   9.63637419e-01   6.30365193e-01
    9.14249601e-02   7.35165247e-01   6.98462995e-01   9.46424498e-01
    4.66382306e-01   8.96224110e-01   7.91546144e-01   1.04995000e-01
    6.73704816e-01   5.54489677e-02   3.55036303e-01]
 [  2.38483440e-01   8.80162281e-01   7.74850154e-01   5.51672769e-01
    7.45061705e-01   9.66476618e-02   1.58965376e-01   3.99382065e-01
    9.35115373e-01   1.91232428e-01   4.47600405e-01   6.10940624e-01
    6.98062006e-01   1.60425444e-01   3.13151260e-01]
 [  9.97592891e-01   9.44054175e-01   3.92890056e-01   7.77033656e-01
    6.35232244e-01   9.62614823e-02   6.14323285e-01   7.79661984e-01
    2.91577069e-01   6.73833526e-01   3.19344564e-01   1.19418087e-01
    3.32120133e-01   4.93822982e-01   4.37672716e-01]
 [  9.81790063e-01   3.93256767e-01   1.36475640e-01   9.78406520e-01
    1.90157530e-01   4.50031053e-01   5.72403762e-01   1.13508846e-01
    7.31135788e-01   1.24091551e-01   4.01420127e-01   7.48524755e-01
    1.94769981e-01   5.79389202e-01   3.01657712e-01]
 [  8.69565630e-01   3.39553171e-02   5.89810585e-01   5.01874299e-01
    3.98599285e-01   5.30582765e-01   6.43708026e-01   6.72030026e-01
    7.66930054e-01   5.80009408e-01   5.25562289e-02   1.88959753e-01
    7.34287487e-01   8.23265923e-03   1.05432378e-01]
 [  9.35126602e-01   3.34207978e-01   4.41245632e-01   8.79238057e-04
    9.87682713e-01   7.54207412e-01   3.00459369e-01   3.32351023e-01
    4.22501514e-01   9.56785289e-01   3.82761134e-01   8.21520952e-01
    7.61110768e-01   9.13007760e-02   8.23698359e-01]
 [  1.73166169e-01   9.76746843e-01   4.85859789e-01   7.47491964e-01
    3.31013133e-02   3.23011733e-01   3.46249145e-01   4.50472754e-01
    4.52694918e-01   7.15068259e-01   5.74259567e-01   2.40442625e-01
    8.11204297e-01   9.84675547e-01   9.06206469e-01]
 [  6.15437415e-01   3.31082875e-01   8.99334114e-01   5.69577131e-01
    2.08392742e-01   9.02809580e-01   7.90386904e-01   8.19074184e-01
    7.38959473e-01   5.92534584e-01   9.24447989e-02   7.05593683e-02
    4.45739976e-01   5.68955749e-01   3.65666540e-01]
 [  7.85036100e-01   1.81449192e-01   3.32771108e-01   2.15852760e-01
    6.12086017e-02   1.50232321e-01   8.73131058e-01   1.44707036e-01
    5.27025557e-01   3.92027438e-01   5.30595232e-01   2.79608899e-01
    4.71503045e-01   2.87432153e-02   6.20806917e-01]
 [  8.03462557e-02   3.33468292e-01   6.95284600e-01   9.55927806e-01
    4.92263669e-01   9.27348197e-01   8.32227977e-01   8.39235542e-01
    7.31895577e-01   9.88024491e-01   9.11182056e-01   7.31166744e-01
    9.34049258e-01   3.88606077e-01   8.63103736e-01]
 [  3.91990892e-01   3.23527329e-01   8.25471802e-01   8.45178152e-01
    8.39316652e-01   6.70190906e-01   7.91829074e-01   9.72921279e-01
    7.60916313e-01   8.58193619e-01   6.80694011e-01   1.81594715e-01
    4.70093414e-01   2.62939501e-01   2.11321114e-01]
 [  9.02093808e-01   8.83213500e-01   1.68548531e-01   7.14995494e-01
    4.33419111e-01   4.18124272e-01   6.56637428e-01   3.82413162e-01
    2.87488150e-01   4.41114019e-01   4.62235923e-01   2.76176637e-01
    8.71987454e-01   1.13654207e-01   3.95434296e-01]]
MapMeta([('naxis1', 15), ('naxis2', 20), ('naxis', 2)])

You can quickly plot a map using the peek method:


SunPy Maps have a number of attributes that can be accessed easily, such as the x and y ranges:

# These return astropy Quantity objects.
# In general the attributes are populated using details in the metadata and in
# this case there is no centre pixel or pixel size information given so SunPy
# is defaulting to assuming each pixel is 1 arcsec.
# This is in Helioprojective tangent projection in both longitude and latitude:


[-7.  8.] arcsec
[ -9.5  10.5] arcsec
Pair(x='HPLN-TAN', y='HPLT-TAN')

A real map example is given in the sample data, where the returns the location of the given FITS file.

aia_map =

This has comprehensive metadata:



MapMeta([('simple', True), ('bitpix', 32), ('naxis', 2), ('naxis1', 1024), ('naxis2', 1024), ('extend', True), ('comment', "FITS (Flexible Image Transport System) format is defined in 'Astronomy  and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H"), ('origin', 'SDO/JSOC-SDP'), ('date', '2011-03-19T11:08:25'), ('telescop', 'SDO/AIA'), ('instrume', 'AIA_3'), ('date-obs', '2011-03-19T10:54:00.34'), ('t_obs', '2011-03-19T10:54:01.34Z'), ('tobsstep', 90.0), ('tobsepoc', '1977.01.01_00:00:00_TAI'), ('camera', 3), ('img_type', 'LIGHT'), ('exptime', 1.999601), ('expsdev', 0.00016), ('int_time', 2.273438), ('wavelnth', 171), ('waveunit', 'angstrom'), ('wave_str', '171_THIN'), ('fsn', 22642033), ('fid', 0), ('lvl_num', 1.5), ('quallev0', 0), ('quality', 1073741824), ('totvals', 16777216), ('datavals', 16777216), ('missvals', 0), ('percentd', 100.0), ('datamin', -7), ('datamax', 16228), ('datamedn', 192), ('datamean', 236.57), ('datarms', 294.02), ('dataskew', 4.63), ('datakurt', 49.56), ('oscnmean', 'nan'), ('oscnrms', 'nan'), ('flat_rec', 'aia.flatfield[:#7]'), ('ctype1', 'HPLN-TAN'), ('cunit1', 'arcsec'), ('crval1', 0.0), ('cdelt1', 2.4), ('crpix1', 512.5), ('ctype2', 'HPLT-TAN'), ('cunit2', 'arcsec'), ('crval2', 0.0), ('cdelt2', 2.4), ('crpix2', 512.5), ('crota2', 0.0), ('r_sun', 1608.94397), ('mpo_rec', 'sdo.master_pointing[:#116]'), ('inst_rot', 0.102488), ('imscl_mp', 0.599076), ('x0_mp', 2052.399902), ('y0_mp', 2046.589966), ('rsun_lf', 'nan'), ('x0_lf', 'nan'), ('y0_lf', 'nan'), ('asd_rec', 'sdo.lev0_asd_0004[:#8948067]'), ('sat_y0', -0.365593), ('sat_z0', 14.820671), ('sat_rot', -8.8e-05), ('acs_mode', 'SCIENCE'), ('acs_eclp', 'NO'), ('acs_sunp', 'YES'), ('acs_safe', 'NO'), ('acs_cgt', 'GT3'), ('orb_rec', 'sdo.fds_orbit_vectors[2011.03.19_10:54:00_UTC]'), ('dsun_ref', 149597870691.0), ('dsun_obs', 148940609626.98), ('rsun_ref', 696000000.0), ('rsun_obs', 963.879683), ('gciec_x', 'nan'), ('gciec_y', 'nan'), ('gciec_z', 'nan'), ('hciec_x', 'nan'), ('hciec_y', 'nan'), ('hciec_z', 'nan'), ('obs_vr', -2132.568376), ('obs_vw', 30775.731671), ('obs_vn', 2177.6711), ('crln_obs', 315.285065), ('crlt_obs', -7.064078), ('car_rot', 2108), ('roi_nwin', -2147483648), ('roi_sum', -2147483648), ('roi_nax1', -2147483648), ('roi_nay1', -2147483648), ('roi_llx1', -2147483648), ('roi_lly1', -2147483648), ('roi_nax2', -2147483648), ('roi_nay2', -2147483648), ('roi_llx2', -2147483648), ('roi_lly2', -2147483648), ('ispsname', 'aia.lev0_isp_0011'), ('isppktim', '2011-03-19T10:53:57.50Z'), ('isppktvn', '001.197'), ('aivnmst', 453), ('aimgots', 1679223275), ('asqhdr', 2170125681), ('asqtnum', 2), ('asqfsn', 22642033), ('aiahfsn', 22642025), ('aecdelay', 1540), ('aiaecti', 0), ('aiasen', 0), ('aifdbid', 241), ('aimgotss', 5339), ('aifcps', 10), ('aiftswth', 0), ('aifrmlid', 3024), ('aiftsid', 40960), ('aihismxb', 7), ('aihis192', 8381297), ('aihis348', 8388262), ('aihis604', 8388597), ('aihis860', 8388603), ('aifwen', 204), ('aimgshce', 2000), ('aectype', 0), ('aecmode', 'ON'), ('aistate', 'CLOSED'), ('aiaecenf', 1), ('aifiltyp', 0), ('aimshobc', 41.099998), ('aimshobe', 26.076), ('aimshotc', 55.312), ('aimshote', 69.316002), ('aimshcbc', 2040.791992), ('aimshcbe', 2025.864014), ('aimshctc', 2054.875977), ('aimshcte', 2068.676025), ('aicfgdl1', 0), ('aicfgdl2', 137), ('aicfgdl3', 201), ('aicfgdl4', 236), ('aifoenfl', 1), ('aimgfsn', 5), ('aimgtyp', 0), ('aiawvlen', 7), ('aiagp1', 0), ('aiagp2', 0), ('aiagp3', 0), ('aiagp4', 0), ('aiagp5', 0), ('aiagp6', 0), ('aiagp7', 0), ('aiagp8', 393), ('aiagp9', 457), ('aiagp10', 748), ('agt1svy', 18), ('agt1svz', 10), ('agt2svy', 10), ('agt2svz', 8), ('agt3svy', 2), ('agt3svz', 0), ('agt4svy', 14), ('agt4svz', 0), ('aimgshen', 4), ('recnum', 76202), ('blank', -2147483648), ('bzero', 0.0), ('bscale', 0.0625), ('checksum', 'J7qAL7o6J7oAJ7o5'), ('datasum', '3958014355'), ('history', ''), ('keycomments', {'NAXIS': 'number of data axes', 'NAXIS1': 'length of data axis 1', 'DATASUM': 'data unit checksum updated 2011-03-19T11:08:18', 'EXTEND': 'FITS dataset may contain extensions', 'BITPIX': 'number of bits per data pixel', 'SIMPLE': 'file does conform to FITS standard', 'CHECKSUM': 'HDU checksum updated 2011-03-19T11:08:18', 'NAXIS2': 'length of data axis 2'}), ('detector', 'AIA')])

Which allows it to accurately specify ranges:


# And find out information about the observation device and date:


[-1227.6  1230. ] arcsec
[-1227.6  1230. ] arcsec
2011-03-19 10:54:00.340000
1.999601 s
Pair(x='HPLN-TAN', y='HPLT-TAN')
171.0 Angstrom

To see only a part of the image you create a submap, specifying ranges in AstroPy Quantities:

rangex = u.Quantity([aia_map.xrange[0], 0 * u.arcsec])
rangey = u.Quantity([aia_map.yrange[0], 0 * u.arcsec])
aia_submap = aia_map.submap(rangex, rangey)

Similarly, if you want to reduce the angular resolution of the map you can use the resample method, specifying the dimensions as an Astropy Quantity in pixels:

dimensions = u.Quantity([50, 50], u.pixel)
aia_resampled_map = aia_map.resample(dimensions)
aia_resampled_map.peek(draw_limb=True, draw_grid=True)

Similar to resampling you can use the superpixel method, this will reduce the resolution of the image by combining the number of pixels (in each dimension) in the dimensions argument into one single pixel. This can be used to increase the signal to noise ratio. For this the new dimensions must divide original image size exactly.

dimensions = u.Quantity(aia_map.dimensions) / 16
aia_superpixel_map = aia_map.superpixel(dimensions)

Maps can also be rotated using the rotate method:

aia_rotated_submap = aia_submap.rotate(angle = 10 * u.deg)
aia_rotated_submap.peek(draw_limb=True, draw_grid=True)
# Note: the data array is expanded so that none of the original data is lost
# through clipping.

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

Generated by Sphinx-Gallery