Through tutorials and presentations we will demonstrate how free and open-source Python and SunPy library can be used to analyze solar data. Depending on interest, dinner may be ordered after the main presentation (roughly an hour) and a hands-on help session will take place for the remainder of the evening. Installing the following software is recommended but not required: Anaconda Python and SunPy.
Schedule: 19:00 to 22:00
SunPy is built upon foundational libraries which enable scientific computing in Python which includes
Supported observations
Supported data retrieval services
Supported file formats include
Some setup...
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
matplotlib.rc('savefig', dpi=120)
import warnings
warnings.simplefilter("ignore", Warning)
from matplotlib import dates
import sunpy
sunpy.system_info()
SunPy version (stable) 0.5
from sunpy.net import hek
client = hek.HEKClient()
tstart, tend = '2014/01/01 00:00:00', '2014/01/02 00:00:00'
result = client.query(hek.attrs.Time(tstart, tend),
hek.attrs.EventType('FL'),
hek.attrs.FRM.Name=='SSW Latest Events')
len(result)
result[0]
for res in result:
print(res.get('fl_goescls'))
result = client.query(hek.attrs.Time(tstart, tend),
hek.attrs.EventType('FL'),
hek.attrs.FRM.Name=='SSW Latest Events',
hek.attrs.FL.GOESCls>'M')
len(result)
result
We can find out when this event occured
result[0].get('event_peaktime')
and where it occurred
result[0].get('hpc_coord')
from sunpy.time import TimeRange, parse_time
from datetime import timedelta
tmax = parse_time(result[0].get('event_peaktime'))
tmax
tr = TimeRange(tmax - timedelta(minutes=30), tmax + timedelta(minutes=30))
tr
from sunpy.lightcurve import GOESLightCurve
goes = GOESLightCurve.create(tr)
goes.peek()
The data is stored in a standard place
goes.data
This is a pandas dataframe! Provides lots of additional functionality. For example
print('The max flux is {flux:2.5f} at {time}'.format(flux=goes.data['xrsb'].max(), time=goes.data['xrsb'].idxmax()))
Compares well to the official max from the HEK
str(tmax)
goes.peek()
plt.axhline(goes.data['xrsb'].max())
plt.axvline(goes.data['xrsb'].idxmax())
Meta data is also stored in a standard place
goes.meta
This is a dictionary like the hek results so...
goes.meta.get('COMMENT')
goes.data.resample('10s', how='mean')
SunPy has a Map
type that supports 2D images, it makes it simple to read data in from any filetype supported in sunpy.io
which is currently FITS, JPEG2000 and ANA files. You can also create maps from any (data, metadata)
pair.
Let's download an AIA image of this flare from the vso
from sunpy.net import vso
client=vso.VSOClient()
Then do a search.
recs = client.query(vso.attrs.Time(tr), vso.attrs.Instrument('AIA'))
It works, now lets see how many results we have!
recs.num_records()
That's way too many!
So that is every image that SDO/AIA had on that day. Let us reduce that amount.
To do this, we will limit the time search for a specify a wavelength.
recs = client.query(vso.attrs.Time('2014/01/01 18:52:08', '2014/01/01 18:52:15'),
vso.attrs.Instrument('AIA'),
vso.attrs.Wave(171,171))
recs.num_records()
recs.show()
Let's also grab another wavelength for later.
recs = client.query(vso.attrs.Time('2014/01/01 18:52:08', '2014/01/01 18:52:15'),
vso.attrs.Instrument('AIA'),
vso.attrs.Wave(94,171))
recs.num_records()
Let's download this data!
f = client.get(recs, methods = ('URL-FILE_Rice')).wait()
f
For SunPy the top level name-space is kept clean. Importing SunPy does not give you access to much. You need to import specific names. SciPy is the same.
So, the place to start here is with the core SunPy data object. It is called Map.
from sunpy.map import Map
aia = Map(f[1])
aia
Maps contain both the image data and the metadata associated with the image, this metadata currently does not deviate much from the standard FITS WCS keywords, but presented in a instrument-independent manner.
aia.peek()
Maps are the same for each file, regardless of source. It does not matter if the source is SDO or SOHO, for example.
The most used attributes are as follows (some of them will look similar to NumPy's Array):
aia.data
The data (stored in a numpy array)
type(aia.data)
aia.mean(),aia.max(),aia.min()
Because it is just a numpy array you have access to all of those function
The standard deviation
aia.data.std()
aia.data.shape
The original metadata (stored in a dictionary)
aia.meta
aia.meta.keys()
aia.meta.get('rsun_obs')
We also provide quick access to some key metadata values as object variables (these are shortcuts)
print(aia.date, aia.coordinate_system, aia.detector, aia.dsun)
Maps also provide some nice map specific functions such as submaps. Let's zoom in on the flare location which was given to us by the HEK.
result[0].get('hpc_coord')
point = [665.04, -233.4096]
dx = 50
dy = 50
xrange = [point[0] - dx, point[0] + dx]
yrange = [point[1] - dy, point[1] + dy]
aia.submap(xrange,yrange).peek()
plt.plot(point[0], point[1], '+')
plt.xlim(xrange)
plt.ylim(yrange)
The default image scale is definitely not right. Let's fix that so we can see the flare region better.
smap = aia.submap(xrange,yrange)
import matplotlib.colors as colors
norm = colors.Normalize(0, 3000)
smap.plot(norm=norm)
plt.plot(point[0], point[1], '+')
smap.draw_grid(grid_spacing=1)
plt.colorbar()
Let's plot the two channels we downloaded from AIA together! Composite map is the way to do this. Let's check out the other map we got.
aia131 = Map(f[0])
aia131
smap131 = aia131.submap(xrange, yrange)
smap131.peek()
norm = colors.Normalize(0, 4000)
smap131.plot(norm=norm)
plt.colorbar()
smap171 = smap
compmap = Map(smap171, smap131, composite=True)
levels = np.arange(0,100,5)
print(levels)
compmap.set_levels(1, levels, percent=True)
compmap.set_mpl_color_normalizer(0, norm)
compmap.set_colors(1, plt.cm.Reds)
compmap.plot(norm=norm)
plt.show()
Some other topics...
from sunpy.sun import constants as solar_constants
solar_constants.mass
print(solar_constants.mass)
(solar_constants.mass/solar_constants.volume).cgs
solar_constants.volume + solar_constants.density
Not all constants have a short-cut assigned to them (as above). The rest of the constants are stored in a dictionary. The following code grabs the dictionary and gets all of the keys.:
solar_constants.physical_constants.keys()
type(solar_constants.mass)
These are astropy constants which are a subclass of Quantities (numbers with units) which are a great idea.
from astropy import units as u
u.keV
u.keV.decompose()
This has been a simple overview of AstroPy units. IF you want to read more, see http://astropy.readthedocs.org/en/latest/units/.