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 matplotlib.pyplot as plt

import astropy.units as u
from astropy.coordinates import SkyCoord


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.


[[7.97564154e-01 2.18391001e-01 6.01741214e-01 4.52286905e-02
  1.92788922e-01 4.39316875e-01 7.48770804e-01 5.96318327e-01
  6.93049034e-01 1.23154129e-01 7.55713583e-01 5.88948639e-01
  6.61602649e-02 2.22564919e-01 9.16232879e-01]
 [8.72303589e-01 1.30164560e-01 5.64540240e-02 5.37451344e-01
  4.49621098e-01 5.63237183e-01 2.52741356e-01 2.71392294e-01
  1.48903096e-01 7.83068301e-01 1.28481006e-01 9.91651666e-01
  5.21480061e-01 8.80697886e-01 4.43488207e-01]
 [5.22016386e-01 7.50312365e-01 9.37453035e-01 1.37997633e-02
  6.04089848e-03 1.50034672e-01 9.82578386e-02 5.38446707e-01
  6.28759381e-01 1.53146796e-01 5.07110781e-01 9.90259827e-01
  5.22084637e-01 1.82625007e-01 7.61422458e-01]
 [3.43057631e-01 1.44554732e-01 5.10680993e-01 5.38869162e-01
  8.54850966e-01 2.96901273e-01 1.99200338e-01 6.49890645e-01
  2.11181426e-01 4.38007182e-01 6.36975257e-01 7.15974293e-01
  3.60952562e-01 4.04446962e-01 4.38051380e-01]
 [9.92485578e-01 8.23315408e-01 7.11924464e-01 3.42581879e-02
  1.78099410e-01 1.18369525e-01 1.51698843e-01 7.27345039e-01
  2.54862448e-01 2.27743557e-03 1.31595492e-02 8.39121318e-01
  4.46628339e-01 5.55876975e-01 5.95364720e-01]
 [1.00070768e-01 2.59223517e-01 8.14668327e-01 2.80126618e-01
  5.73739597e-01 7.36635311e-01 7.20350767e-01 5.70890317e-01
  3.80811474e-01 3.47427326e-01 7.09759186e-01 4.37939040e-01
  4.61346302e-01 2.69888604e-01 7.64411984e-01]
 [7.53412812e-02 4.94785515e-01 5.12654933e-02 3.09421147e-01
  7.11121935e-01 1.75710330e-01 2.08949683e-01 6.10831981e-01
  4.38029508e-01 9.62952265e-02 9.63930655e-01 9.51501176e-01
  9.81972407e-01 4.23265526e-01 9.74130185e-01]
 [5.17171848e-01 5.56324105e-01 3.24634316e-02 1.27566558e-01
  6.59623190e-01 5.98160060e-01 6.48769268e-01 7.39266816e-01
  9.02616280e-01 8.02989954e-01 2.01747299e-01 8.73941709e-01
  8.59165229e-01 4.77437136e-01 8.94201670e-02]
 [1.74273627e-01 5.51929395e-01 1.18652997e-01 4.07271562e-01
  3.02117039e-01 6.04877510e-01 2.46262082e-01 2.37068111e-02
  1.40020756e-01 4.96477641e-02 3.39662419e-01 7.05260092e-01
  2.07629236e-01 5.34842376e-01 3.56118178e-01]
 [3.50212875e-01 1.40321113e-01 2.45274986e-01 2.92533151e-01
  5.78614363e-01 9.53790925e-02 8.16042235e-01 7.35180648e-01
  8.41997568e-01 6.20759275e-01 6.85598157e-04 8.22220962e-01
  7.07929273e-01 4.96280848e-01 2.07586405e-01]
 [9.94859054e-02 2.22403647e-01 9.78042228e-01 3.67016320e-01
  5.37513664e-01 8.85355195e-01 7.68745549e-01 8.31879490e-01
  8.27208603e-01 2.99436484e-01 6.17145432e-01 7.15569770e-02
  2.25997217e-02 3.37955341e-02 5.63527955e-01]
 [9.59890644e-01 3.85136910e-01 2.61584337e-01 2.70659660e-01
  9.11744913e-01 4.74772762e-01 2.12108445e-01 5.18813324e-01
  6.56158000e-01 2.23178074e-01 3.03148494e-02 9.92858699e-02
  1.85938389e-02 4.20533351e-01 3.01632112e-01]
 [8.66874239e-02 6.18767984e-01 1.43104505e-01 4.33190061e-01
  9.51192042e-01 8.47988818e-01 1.13529283e-01 9.46027377e-01
  7.97505550e-01 2.41708580e-01 8.53521506e-01 8.39694192e-01
  5.50210843e-01 2.12736325e-01 9.65814853e-01]
 [6.90170797e-01 5.05684606e-01 7.38237449e-02 4.89086223e-01
  6.89021598e-02 9.09974189e-01 8.58121051e-01 6.15535709e-01
  7.63074465e-01 7.67711080e-01 8.30852421e-01 5.20459775e-01
  9.01414255e-02 6.23927502e-01 4.02630861e-01]
 [6.88249849e-01 2.42116692e-01 4.10489973e-01 6.01025210e-01
  7.71945042e-01 1.69244212e-01 4.60877535e-01 9.18711357e-01
  4.78606252e-01 2.79560635e-01 9.18147155e-01 2.94691860e-01
  8.24155435e-01 6.40331008e-01 6.23796103e-01]
 [6.68149257e-01 8.88036486e-01 1.90301443e-01 5.40779638e-01
  4.45811586e-01 3.32934978e-01 4.46988230e-01 6.02235216e-01
  5.74153512e-01 8.66878424e-01 3.55502465e-01 2.89415723e-01
  5.85068901e-01 8.41153844e-01 4.73097236e-01]
 [7.58759631e-01 5.43446818e-01 4.55471050e-01 3.89412334e-01
  2.16118274e-01 2.09260469e-01 8.42494306e-01 4.65773722e-01
  9.00110302e-01 9.03349034e-01 4.92125415e-02 5.05465737e-01
  8.16639695e-01 5.47402961e-01 9.62927116e-01]
 [1.94054125e-01 8.91713941e-01 3.88054059e-01 5.22386774e-01
  7.85775138e-02 2.73939927e-01 2.14747198e-01 4.81931824e-01
  4.55354872e-01 7.03081256e-01 8.20960496e-01 1.94673372e-01
  9.46896081e-01 6.78587164e-01 3.09601538e-01]
 [2.15111854e-01 6.26388075e-01 7.70577628e-01 6.03901738e-01
  1.40536401e-01 1.50488624e-01 1.10343228e-01 4.65814741e-01
  7.66987479e-01 7.62953875e-01 8.08516040e-01 4.06694429e-01
  6.18290614e-01 4.23028412e-01 5.86375709e-01]
 [6.82390201e-01 6.47461187e-01 2.41205275e-01 2.78999740e-01
  3.73842910e-04 3.64724492e-01 2.56420896e-01 1.78483479e-01
  7.51782836e-01 9.60839229e-01 1.71811432e-01 3.41640091e-01
  9.44114505e-01 5.94962552e-01 2.15520351e-01]]
MetaDict([('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
SpatialPair(axis1='HPLN-   ', axis2='HPLT-   ')

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:



MetaDict([('simple', True), ('bitpix', -32), ('naxis', 2), ('naxis1', 1024), ('naxis2', 1024), ('bld_vers', 'V5R12X'), ('lvl_num', 1.5), ('t_rec', '2011-06-07T06:33:03Z'), ('trecstep', 1.0), ('trecepoc', '1977.01.01_00:00:00_TAI'), ('trecroun', 1), ('origin', 'SDO'), ('date', '2012-10-16T19:45:34'), ('telescop', 'SDO'), ('instrume', 'AIA_3'), ('date-obs', '2011-06-07T06:33:02.77'), ('t_obs', '2011-06-07T06:33:02.88Z'), ('camera', 3), ('img_type', 'LIGHT'), ('exptime', 0.234256), ('expsdev', 0.000135), ('int_time', 0.507812), ('wavelnth', 171), ('waveunit', 'angstrom'), ('wave_str', '171_THIN'), ('fsn', 27194331), ('fid', 0), ('quallev0', 0), ('quality', 0), ('totvals', 16777216), ('datavals', 16777216), ('missvals', 0), ('percentd', 100.0), ('datamin', -8), ('datamax', 17722), ('datamedn', 18), ('datamean', 26.6888), ('datarms', 9429260.0), ('dataskew', 81.433), ('datakurt', 17076.7421875), ('datacent', 30.76), ('datap01', -1.0), ('datap10', 1.0), ('datap25', 3.0), ('datap75', 39.0), ('datap90', 64.0), ('datap95', 87.0), ('datap98', 127.0), ('datap99', 168.0), ('nsatpix', 29), ('oscnmean', 'nan'), ('oscnrms', 'nan'), ('flat_rec', 'aia.flatfield[:#30]'), ('nspikes', 977), ('ctype1', 'HPLN-TAN'), ('cunit1', 'arcsec'), ('crval1', 3.223099507700556), ('cdelt1', 2.402792), ('crpix1', 512.5), ('ctype2', 'HPLT-TAN'), ('cunit2', 'arcsec'), ('crval2', 1.385781353025793), ('cdelt2', 2.402792), ('crpix2', 512.5), ('crota2', -0.138829), ('r_sun', 1573.89688496), ('mpo_rec', 'sdo.master_pointing[:#394]'), ('inst_rot', 0.019327), ('imscl_mp', 0.599489), ('x0_mp', 2049.459961), ('y0_mp', 2049.030029), ('asd_rec', 'sdo.lev0_asd_0004[:#10672125]'), ('sat_y0', -4.519384), ('sat_z0', 13.793308), ('sat_rot', -3.4e-05), ('acs_mode', 'SCIENCE'), ('acs_eclp', 'NO'), ('acs_sunp', 'YES'), ('acs_safe', 'NO'), ('acs_cgt', 'GT3'), ('orb_rec', 'sdo.fds_orbit_vectors[2011.06.07_06:33:00_UTC]'), ('dsun_ref', 149597870691.0), ('dsun_obs', 151846026489.0), ('rsun_ref', 696000000.0), ('rsun_obs', 945.436711), ('gaex_obs', -13315441.72), ('gaey_obs', -25080995.82), ('gaez_obs', 31173320.91), ('haex_obs', -36634444009.2), ('haey_obs', -147360551017.0), ('haez_obs', 35731768.47), ('obs_vr', 86.972467), ('obs_vw', 31968.269521), ('obs_vn', 4842.822869), ('crln_obs', 340.698273), ('crlt_obs', 0.048591), ('car_rot', 2111), ('hgln_obs', 0.0), ('hglt_obs', 0.048591), ('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), ('pixlunit', 'DN'), ('dn_gain', 17.7), ('eff_area', 2.419), ('eff_ar_v', 3.0), ('tempccd', -71.0), ('tempgt', 14.327), ('tempsmir', 34.029), ('tempfpad', 16.29), ('ispsname', 'aia.lev0_isp_0011'), ('isppktim', '2011-06-07T06:32:57.50Z'), ('isppktvn', '001.197'), ('aivnmst', 453), ('aimgots', 1686119616), ('asqhdr', 2174677979), ('asqtnum', 2), ('asqfsn', 27194331), ('aiahfsn', 27194323), ('aecdelay', 1537), ('aiaecti', 0), ('aiasen', 0), ('aifdbid', 241), ('aimgotss', 55109), ('aifcps', 10), ('aiftswth', 0), ('aifrmlid', 3057), ('aiftsid', 41729), ('aihismxb', 7), ('aihis192', 8377773), ('aihis348', 8384772), ('aihis604', 8386035), ('aihis860', 8386516), ('aifwen', 204), ('aimgshce', 237), ('aectype', 2), ('aecmode', 'ON'), ('aistate', 'CLOSED'), ('aiaecenf', 1), ('aifiltyp', 0), ('aimshobc', 41.104), ('aimshobe', 26.068001), ('aimshotc', 55.327999), ('aimshote', 69.344002), ('aimshcbc', 275.432007), ('aimshcbe', 260.484009), ('aimshctc', 289.556), ('aimshcte', 303.395996), ('aicfgdl1', 0), ('aicfgdl2', 107), ('aicfgdl3', 171), ('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', 619), ('aiagp9', 683), ('aiagp10', 748), ('agt1svy', 2), ('agt1svz', -6), ('agt2svy', 0), ('agt2svz', -1), ('agt3svy', -2), ('agt3svz', 3), ('agt4svy', -5), ('agt4svz', 5), ('aimgshen', 4), ('keywddoc', 'https:'), ('recnum', 76618218), ('blank', -32768), ('date_obs', '2011-06-07T06:33:02.77'), ('xcen', 2.91938326953), ('ycen', 1.09323792231), ('detector', 'AIA'), ('comment', "FITS (Flexible Image Transport System) format is defined in 'Astronomyand Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359HFITSHEAD2STRUCT"), ('history', 'FITSHEAD2STRUCT run at: Fri May 26 11:33:15 2017mreadfits_shm VERSION:1.20read_sdo VERSION: VERSION:  5.10aia2wcsminaia2wcsmin  MPO_date: 2012-09-04T00:00:00Zaia2wcsmin  MPO_t_start: 2011-06-05T00:00:00Zaia2wcsmin  MPO_t_stop: 2011-06-12T00:00:00Zaia2wcsmin  MPO_version: VERSION:  1.30ssw_regssw_reg  ROT called with cubic interpolation: cubic = -0.500000ssw_reg  Image registered to SDO image with FSN = 27194330ssw_reg  Image registered to SDO image with T_OBS = VERSION: VERSION: VERSION:  1.20'), ('keycomments', {'BITPIX': 'array data type', 'NAXIS': 'number of array dimensions', 'SIMPLE': 'conforms to FITS standard'})])

And find out information about the observation device and date:



2011-06-07 06:33:02.770000
0.234256 s
SpatialPair(axis1='HPLN-TAN', axis2='HPLT-TAN')
171.0 Angstrom

Maps also hold coordinate objects for the coordinate system they are in.



<Helioprojective Frame (obstime=2011-06-07 06:33:02.770000, rsun=696000000.0 m, observer=<HeliographicStonyhurst Coordinate (obstime=2011-06-07 06:33:02.770000): (lon, lat, radius) in (deg, deg, m)
    (0., 0.048591, 1.51846026e+11)>)>

To see only a part of the image you create a submap, by specifying the top left and bottom right corners of the rectangle as either SkyCoord or Quantity objects.

bottom_left = aia_map.bottom_left_coord
top_right = SkyCoord(0 * u.arcsec, 0 * u.arcsec, frame=aia_map.coordinate_frame)
aia_submap = aia_map.submap(bottom_left, top_right)

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.020 seconds)

Gallery generated by Sphinx-Gallery