Debian Codegraph @

examples/plot_polarfield.py

raw
1 """
2 ================================================
3 Downloading and plotting a HMI polar field data
4 ================================================
5
6 This example shows how to download HMI polar field data from JSOC and make a plot.
7 """
8
9 import matplotlib.pyplot as plt
10 import numpy as np
11
12 import drms
13
14 ###############################################################################
15 # First we will create a `drms.Client`, using the JSOC baseurl.
16
17 client = drms.Client()
18
19 ###############################################################################
20 # Construct the DRMS query string: "Series[timespan][wavelength]"
21
22 qstr = "hmi.meanpf_720s[2010.05.01_TAI-2016.04.01_TAI@12h]"
23
24 # Send request to the DRMS server
25 print(f"Querying keyword data...\n -> {qstr}")
26 result = client.query(qstr, key=["T_REC", "CAPN2", "CAPS2"])
27 print(f" -> {len(result)} lines retrieved.")
28
29 # Convert T_REC strings to datetime and use it as index for the series
30 result.index = drms.to_datetime(result.pop("T_REC"))
31
32 # Determine smallest timestep
33 dt = np.diff(result.index.to_pydatetime()).min()
34
35 # Make sure the time series contains all time steps (fills gaps with NaNs)
36 a = result.asfreq(dt)
37
38 ###############################################################################
39 # Plot the magnetic field values.
40
41 # Compute 30d moving average and standard deviation using a boxcar window
42 win_size = int(30 * 24 * 3600 / dt.total_seconds())
43 a_avg = a.rolling(win_size, min_periods=1, center=True).mean()
44 a_std = a.rolling(win_size, min_periods=1, center=True).std()
45
46 # Plot results
47 t = a.index.to_pydatetime()
48 n, mn, sn = a.CAPN2, a_avg.CAPN2, a_std.CAPN2
49 s, ms, ss = a.CAPS2, a_avg.CAPS2, a_std.CAPS2
50
51 # Plot smoothed data
52 fig, ax = plt.subplots(1, 1, figsize=(15, 7))
53 ax.set_title(qstr, fontsize="medium")
54 ax.plot(t, n, "b", alpha=0.5, label="North pole")
55 ax.plot(t, s, "g", alpha=0.5, label="South pole")
56 ax.plot(t, mn, "r", label="Moving average")
57 ax.plot(t, ms, "r", label="")
58 ax.set_xlabel("Time")
59 ax.set_ylabel("Mean radial field strength [G]")
60 ax.legend()
61 fig.tight_layout()
62
63 # Plot raw data
64 fig, ax = plt.subplots(1, 1, figsize=(15, 7))
65 ax.set_title(qstr, fontsize="medium")
66 ax.fill_between(t, mn - sn, mn + sn, edgecolor="none", facecolor="b", alpha=0.3, interpolate=True)
67 ax.fill_between(t, ms - ss, ms + ss, edgecolor="none", facecolor="g", alpha=0.3, interpolate=True)
68 ax.plot(t, mn, "b", label="North pole")
69 ax.plot(t, ms, "g", label="South pole")
70 ax.set_xlabel("Time")
71 ax.set_ylabel("Mean radial field strength [G]")
72 ax.legend()
73 fig.tight_layout()
74
75 plt.show()