<jupyter_start><jupyter_text>Create SMARP and SHARP maps with SunPy This notebook shows how to query SMARP and SHARP data and load these data into a SunPy map.<jupyter_code>import sunpy.map
from sunpy.visualization.colormaps import color_tables as ct
import drms
from astropy.io import fits
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
import pandas as pd
pd.set_option('max_rows', 150) # print more than the default 10 rows of the pandas dataframe
%matplotlib inline
%config InlineBackend.figure_format='retina'<jupyter_output><empty_output><jupyter_text>Set up the data query The first step in querying for SMARP and SHARP data is to establish a connection to JSOC. This can be done with the `Client()` class. To do this, first [register your e-mail address with JSOC](http://jsoc.stanford.edu/ajax/register_email.html).<jupyter_code>email='<EMAIL>' # Put in a registered e-mail address
c = drms.Client(debug=True, verbose=True, email=email)<jupyter_output>http://jsoc.stanford.edu/cgi-bin/ajax/checkAddress.sh?address=mbobra%2540stanford.edu&checkonly=1<jupyter_text>We assign each active region in the SMARP database with a Tracked Active Region Patch (TARP) Number, or `TARPNUM`. Similarly, each active region in the SHARP database has an HMI Active Region Patch (HARP) Number, or `HARPNUM`. Our definition of an active region differs from that of NOAA’s.  We count regions that span a small heliographic area and regions without an associated photometric sunspot as active regions. As a result, both the SMARP and SHARP data sets include more regions than the NOAA active region database. See the keyword `NOAA_ARS`, present in both the SMARP and SHARP database, to map a `HARPNUM` or `TARPNUM` to a NOAA active region number. See [here](http://jsoc.stanford.edu/doc/data/hmi/harpnum_to_noaa/all_harps_with_noaa_ars.txt) for a complete list of all the TARP numbers associated with NOAA active region numbers, and [here](http://jsoc.stanford.edu/doc/data/mdi/all_tarps_with_noaa_ars.txt) for a complete list of all the HARP numbers associated with NOAA active region numbers. Now let's look at one example:<jupyter_code>t_rec = '2010.07.14_11:12:00'
harpnum = '86'
tarpnum = '13520'<jupyter_output><empty_output><jupyter_text>To query the SMARP data, we build a query string. This involves putting the drms series (in this case, mdi.smarp_96m), together with the prime keys, or the keywords that uniquely define a record (in this case, `TARPNUM` and `T_REC`). You can also specify any other constraints. In this case, we specify a constraint on the `QUALITY` keyword, which defines the quality of the data. See [here](http://soi.stanford.edu/production/QUALITY/bits.fd_M.html) for a definition of the `QUALITY` keyword.The SMARP data has two series: `mdi.smarp_96m`, which provides partial-disk magnetic and photospheric activity bitmaps, line-of-sight magnetic field maps, and continuum intensity maps in Helioprojective Cartesian coordinates, and `mdi.smarp_cea_96m`, which provides the same maps in Heliographic Cylindrical Equal-Area coordinates. We will first look at the data in Helioprojective Cartesian coordinates.The series contain a number of keywords, which we can view like this:<jupyter_code>series_info = c.info('mdi.smarp_cea_96m')
series_info.keywords<jupyter_output>http://jsoc.stanford.edu/cgi-bin/ajax/jsoc_info?op=series_struct&ds=mdi.smarp_cea_96m<jupyter_text>For more information on how to build a query string, see the drms documentation [here](https://docs.sunpy.org/projects/drms/en/latest/tutorial.html).<jupyter_code>smarp_query_string = 'mdi.smarp_96m['+tarpnum+']['+t_rec+'][? (QUALITY<262144) ?]'<jupyter_output><empty_output><jupyter_text>Now we export the data and load the URL of the exported FITS file into a SunPy Map. For more information on SunPy Maps, see [here](https://docs.sunpy.org/en/stable/guide/tour.htmlmaps).<jupyter_code>r = c.export(smarp_query_string, method='url', protocol='fits') # Export the data
fits_url_smarp = r.urls['url'][0]                               # Get the URL of the exported FITS file
smarp_map = sunpy.map.Map(fits_url_smarp)                       # Load this URL into a SunPy Map<jupyter_output><empty_output><jupyter_text>We can take a quick look at the SMARP magnetic field map like this:<jupyter_code>smarp_map<jupyter_output><empty_output><jupyter_text>We can also plot it like this:<jupyter_code>smarp_map.plot()<jupyter_output><empty_output><jupyter_text>Now we can look at the data in Heliographic Cylindrical Equal-Area coordinates.We follow the same method to query, download, and load the data.<jupyter_code>smarp_cea_query_string = 'mdi.smarp_cea_96m['+tarpnum+']['+t_rec+'][? (QUALITY<262144) ?]'
r = c.export(smarp_cea_query_string, method='url', protocol='fits') # Export the data
fits_url_smarp_cea = r.urls['url'][0]                               # Get the URL of the exported FITS file
smarp_cea_map = sunpy.map.Map(fits_url_smarp_cea)                   # Load the URL into a SunPy Map
smarp_cea_map.plot()<jupyter_output><empty_output><jupyter_text>This correctly maps the data in the coordinate system defined by the WCS keys `CTYPE1`, which equals and CRLN-CEA, and `CTYPE2` which equals CRLT-CEA. The gridlines are consistent with a Cylindrical Equal-Area mapping. However, we prefer to make the Cylindrical Equal-Area coordinate system a bit more explicit, so we change the labels like this:<jupyter_code>fig = plt.figure()
ax0 = fig.add_subplot(projection=smarp_cea_map)
smarp_cea_map.plot(axes=ax0)
ax0.set_xlabel('CEA Longitude [deg]')
ax0.set_ylabel('CEA Latitude [sine latitude steps]')<jupyter_output><empty_output><jupyter_text>We can do the same thing for the SHARP data:<jupyter_code>sharp_cea_query_string = 'hmi.sharp_cea_720s['+harpnum+']['+t_rec+'][? (QUALITY<65536) ?]'
r = c.export(sharp_cea_query_string, method='url', protocol='fits') # Export the data
fits_url_sharp_cea = r.urls['url'][0]                               # Get the URL of the exported FITS file
sharp_cea_map = sunpy.map.Map(fits_url_sharp_cea)                   # Load the URL into a SunPy Map
fig = plt.figure()
ax0 = fig.add_subplot(projection=sharp_cea_map)
sharp_cea_map.plot(axes=ax0)
ax0.set_xlabel('CEA Longitude [deg]')
ax0.set_ylabel('CEA Latitude [sine latitude steps]')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0920138888888889, 'fraction_numerical': 0.01231060606060606, 'mean_word_length': 5.356068204613842, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 44, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '577366', 'license': 'MIT', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2070, 'n_words': 661, 'path': 'example_gallery/Create_SMARP_and_SHARP_maps_with_SunPy.ipynb', 'repo_name': 'mbobra/SMARPs', 'size': '6347'}