67. Solar Physics with SunPy

Author: Stuart Mumford and Andrew Leonard
at The University of Sheffield (on behalf of the SunPy Project).

<< previous nuggetnext nugget >>

SunPy – Introduction

SunPy is an international project which aims to “facilitate and promote the use and development of a community-led, free and open-source solar data-analysis software based on the scientific Python environment.” SunPy is a project run by solar physicists to make it easier to do solar physics using the scientific Python ecosystem. The main component of the SunPy project is the development of the sunpy Python library, which provides many of the core tools needed to process solar data from Python.

Why Python

Python is a powerful, open source, general purpose programming language which has a very large and active scientific community. This community has developed libraries to perform a dazzling array of tasks using Python. Python makes an excellent language for scientific work for many reasons; some of the most prominent are:

  • Python has very human readable syntax and is very fast to read and write.
  • Python is an excellent ‘glue’ language allowing performance sensitive code or
    existing code to be ‘wrapped’ in Python.
  • There is a large existing ecosystem of scientific tools.

Python also has the advantage that it is widely used in industry as well as in academia, meaning that undergraduates and postgraduates learning Python pick up a skill that is more likely to be applicable once they graduate than other languages might be.

Finally, all of the data reduction code for the DKIST will be written in Python and will be using SunPy, so now is an excellent time to learn the skills you will need.

Working with SunPy

The sunpy package provides the core tools for handling common solar physics data – if you are familiar with SSW it’s kind of similar in scope to the ‘gen’ package. It provides a few key features:

  • Downloading of data from sources such as the VSO, HEK and JSOC.
  • Objects to read in and provide access to images (Map), timeseries
    (lightcurve) and spectral (spectra) data.
  • Conversion of coordinates between pixel values and several different physical coordinate systems.
  • Simple coordinate aware plotting.
  • Physical units and constants built in.

The following code snippet opens some sample AIA data, creates a Map object creates a ‘quick-look’ plot with some default settings using the Map.peek() method:

>>> from sunpy.data.sample import AIA_171_ROLL_IMAGE 
>>> import sunpy.map
>>> aia = sunpy.map.Map(AIA_171_ROLL_IMAGE)
>>> aia.peek()
Fig. 1: Map of SDO/AIA data made with SunPy.

Notice that the grid lines plotted on the image by default are plotted in line with the data coordinate system rather than the image grid. The Map object is designed to work with any kind of solar image data, so the same code will work with data from any other instrument. Similarly, one can create a LightCurve object from GOES satellite data and plot this, again using peek(). This example shows an X ray flare observed by GOES:

>>> from sunpy.lightcurve import GOESLightcurve
>>> goes = GOESLightCurve.create("2011/2/15", "2011/2/16") 
>>> goes.peek()
Fig. 2: GOES X-ray lightcurve made with SunPy.

SunPy also contains facilities for working with physical constants, tracking the units of quantities, working with times, amongst other useful things. For instance, the solar radius is easily imported:

>>> from sunpy.sun import constants as con 
>>> print(con.radius)
     Name =Solarradius
     Value =695508000.0
     Uncertainty =26000.0
     Unit = m
     Reference = Allen's Astrophysical Quantities 4th Ed.

and can be converted to other units using the astropy.units package:

>>> import astropy.units as u
>>> print(con.radius.to(u.imperial.furlong))  # Print solar radius in furlongs. 
     3457349.08136 fur
>>> print(con.radius.to(u.Mm)) # Or, more usefully, in Mm.
     695.508 Mm

And times are easily manipulated:

>>> import sunpy.time
>>> t = sunpy.time.parse_time('2014-01-11 13:00') 
>>> print(t)
     2014-01-11 13:00:00
>>> sunpy.time.julian_day(t)
     2456669.0416666665

Getting started

To get started with SunPy, see the installation instructions here, which will walk you through installing SunPy and the scientific Python environment it uses. For a more in-depth explanation of what can be achieved with SunPy and how to do it, see the SunPy documentation.

Sheffield University hosts regular OpenAstronomy workshops, which teach researchers the basics of programming in Python and how to use SunPy and Astropy for research. If you’re interested in attending one of these workshops, or would like us to run one at your institution, contact Stuart Mumford, Andrew Leonard or Samuel Bennett.

Get Involved

SunPy is a collaborative project which is always in need of different kinds of contributions. The first and most important way to get involved with SunPy is to start using Python and SunPy to do your science, and to send an email to the mailing list or dropping in on our chat, questions on either of these channels are normally answered in a quick and friendly manner. If you want to contribute back, the most obvious way is by writing code for the package, which is managed on GitHub. There are however, many different ways to contribute, from website design, to documentation or teaching SunPy to people in your group.