Skip to content
This repository was archived by the owner on Aug 24, 2023. It is now read-only.

Commit f824750

Browse files
authored
Merge pull request #267 from dstansby/hmi-example
Hmi example
2 parents 3fabea7 + d2adcea commit f824750

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

examples/using_pfsspy/plot_hmi.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
HMI PFSS solutions
3+
------------------
4+
Calculating a PFSS solution from a HMI synoptic map.
5+
6+
This example shows how to calcualte a PFSS solution from a HMI synoptic map.
7+
There are a couple of important things that this example shows:
8+
9+
- HMI maps have non-standard metadata, so this needs to be fixed
10+
- HMI synoptic maps are very big (1440 x 3600), so need to be downsampled
11+
in order to calculate the PFSS solution in a reasonable time.
12+
"""
13+
14+
###############################################################################
15+
# First import the required modules
16+
import astropy.units as u
17+
import matplotlib.pyplot as plt
18+
from sunpy.net import Fido, attrs as a
19+
import sunpy.map
20+
import pfsspy
21+
import pfsspy.utils
22+
23+
###############################################################################
24+
# Set up the search.
25+
#
26+
# Note that for SunPy versions earlier than 2.0, a time attribute is needed to
27+
# do the search, even if (in this case) it isn't used, as the synoptic maps are
28+
# labelled by Carrington rotation number instead of time
29+
time = a.Time('2010/01/01', '2010/01/01')
30+
series = a.jsoc.Series('hmi.synoptic_mr_polfil_720s')
31+
crot = a.jsoc.PrimeKey('CAR_ROT', 2210)
32+
33+
###############################################################################
34+
# Do the search.
35+
#
36+
# If you use this code, please replace this email address
37+
# with your own one, registered here:
38+
# http://jsoc.stanford.edu/ajax/register_email.html
39+
result = Fido.search(time, series, crot, a.jsoc.Notify("[email protected]"))
40+
files = Fido.fetch(result)
41+
42+
###############################################################################
43+
# Read in a file. This will read in the first file downloaded to a sunpy Map
44+
# object. Note that HMI maps have several bits of metadata that do not comply
45+
# to the FITS standard, so we need to fix them first.
46+
hmi_map = sunpy.map.Map(files[0])
47+
pfsspy.utils.fix_hmi_meta(hmi_map)
48+
print('Data shape: ', hmi_map.data.shape)
49+
50+
###############################################################################
51+
# Since this map is far to big to calculate a PFSS solution quickly, lets
52+
# resample it down to a smaller size.
53+
hmi_map = hmi_map.resample([360, 180] * u.pix)
54+
print('New shape: ', hmi_map.data.shape)
55+
56+
###############################################################################
57+
# Now calculate the PFSS solution
58+
nrho = 35
59+
rss = 2.5
60+
input = pfsspy.Input(hmi_map, nrho, rss)
61+
output = pfsspy.pfss(input)
62+
63+
###############################################################################
64+
# Using the Output object we can plot the source surface field, and the
65+
# polarity inversion line.
66+
ss_br = output.source_surface_br
67+
# Create the figure and axes
68+
fig = plt.figure()
69+
ax = plt.subplot(projection=ss_br)
70+
71+
# Plot the source surface map
72+
ss_br.plot()
73+
# Plot the polarity inversion line
74+
ax.plot_coord(output.source_surface_pils[0])
75+
# Plot formatting
76+
plt.colorbar()
77+
ax.set_title('Source surface magnetic field')
78+
79+
plt.show()

pfsspy/utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88

99
import sunpy.io
1010
import sunpy.map
11+
import sunpy.time
12+
13+
import pfsspy.map
1114

1215

1316
def fix_hmi_meta(hmi_map):
1417
"""
1518
Fix non-compliant FITS metadata in HMI maps.
1619
1720
This function:
18-
- Corrects CUNIT2 from 'Sine Latitude' to 'deg'
19-
- Corrects CDELT1 and CDELT2 for a CEA projection
20-
- Populates the DATE-OBS keyword from the T_OBS keyword
21+
- Corrects CUNIT2 from 'Sine Latitude' to 'deg'
22+
- Corrects CDELT1 and CDELT2 for a CEA projection
23+
- Populates the DATE-OBS keyword from the T_OBS keyword
24+
- Sets the observer coordinate to the Earth
2125
2226
Notes
2327
-----
@@ -43,7 +47,11 @@ def fix_hmi_meta(hmi_map):
4347
hmi_map.meta['cdelt1'] = np.abs(hmi_map.meta['cdelt1'])
4448

4549
if 'date-obs' not in hmi_map.meta and 't_obs' in hmi_map.meta:
46-
hmi_map.meta['date-obs'] = hmi_map.meta['t_obs']
50+
hmi_map.meta['date-obs'] = sunpy.time.parse_time(hmi_map.meta['t_obs']).isot
51+
52+
# Fix observer coordinate
53+
if 'hglt_obs' not in hmi_map.meta:
54+
hmi_map.meta.update(pfsspy.map._earth_obs_coord_meta(hmi_map.meta['date-obs']))
4755

4856

4957
def load_adapt(adapt_path):

0 commit comments

Comments
 (0)