Debian Codegraph @

examples/plot_synoptic_mr.py

raw
1 """
2 ============================================
3 Downloading and plotting a HMI synoptic data
4 ============================================
5
6 This example shows how to download HMI synoptic data from JSOC and make a plot.
7 """
8
9 import matplotlib.pyplot as plt
10
11 from astropy.io import fits
12
13 import drms
14
15 ###############################################################################
16 # First we will create a `drms.Client`, using the JSOC baseurl.
17
18 client = drms.Client()
19
20 ###############################################################################
21 # Construct the DRMS query string: "Series[Carrington rotation]"
22
23 qstr = "hmi.synoptic_mr_720s[2150]"
24
25 # Send request to the DRMS server
26 print(f"Querying keyword data...\n -> {qstr}")
27 segname = "synopMr"
28 results, filenames = client.query(qstr, key=drms.JsocInfoConstants.all, seg=segname)
29 print(f" -> {len(results)} lines retrieved.")
30
31 # Use only the first line of the query result
32 results = results.iloc[0]
33 fname = f"http://jsoc.stanford.edu{filenames[segname][0]}"
34
35 # Read the data segment
36 # Note: HTTP downloads get cached in ~/.astropy/cache/downloads
37 print(f"Reading data from {fname}...")
38 a = fits.getdata(fname)
39 ny, nx = a.shape
40
41 ###############################################################################
42 # Now to plot the image.
43
44 # Convert pixel to world coordinates using WCS keywords
45 xmin = (1 - results.CRPIX1) * results.CDELT1 + results.CRVAL1
46 xmax = (nx - results.CRPIX1) * results.CDELT1 + results.CRVAL1
47 ymin = (1 - results.CRPIX2) * results.CDELT2 + results.CRVAL2
48 ymax = (ny - results.CRPIX2) * results.CDELT2 + results.CRVAL2
49
50 # Convert to Carrington longitude
51 xmin = results.LON_LAST - xmin
52 xmax = results.LON_LAST - xmax
53
54 # Compute the plot extent used with imshow
55 extent = (
56 xmin - abs(results.CDELT1) / 2,
57 xmax + abs(results.CDELT1) / 2,
58 ymin - abs(results.CDELT2) / 2,
59 ymax + abs(results.CDELT2) / 2,
60 )
61
62 # Aspect ratio for imshow in respect to the extent computed above
63 aspect = abs((xmax - xmin) / nx * ny / (ymax - ymin))
64
65 # Create plot
66 fig, ax = plt.subplots(1, 1, figsize=(13.5, 6))
67 ax.set_title(f"{qstr}, Time: {results.T_START} ... {results.T_STOP}", fontsize="medium")
68 ax.imshow(
69 a,
70 vmin=-300,
71 vmax=300,
72 origin="lower",
73 interpolation="nearest",
74 cmap="gray",
75 extent=extent,
76 aspect=aspect,
77 )
78 ax.invert_xaxis()
79 ax.set_xlabel("Carrington longitude")
80 ax.set_ylabel("Sine latitude")
81 fig.tight_layout()
82
83 plt.show()