Overplotting SRS active region locations on a magnetograms

How to find and plot the location of an active region on an HMI magnetogram.

import numpy as np
import matplotlib.pyplot as plt

import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.time import TimeDelta

import sunpy.map
import sunpy.coordinates
from sunpy.io.special import srs
from sunpy.time import parse_time
from sunpy.net import Fido, attrs as a

For this example, we will search for and download a single HMI using Fido.

start_time = parse_time("2017-01-25")
end_time = start_time + TimeDelta(23*u.hour + 59*u.minute + 59*u.second)
results = Fido.search(a.Time(start_time, end_time),
                      a.Instrument('HMI') & a.vso.Physobs("LOS_magnetic_field"),
                      a.vso.Sample(60 * u.second))

Let’s select only the first file, download it and create a map.

result = results[0, 0]
file_name = Fido.fetch(result)
smap = sunpy.map.Map(file_name)

Out:

Files Downloaded:   0%|          | 0/1 [00:00<?, ?file/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   0%|          | 0.00/16.9M [00:00<?, ?B/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   0%|          | 49.8k/16.9M [00:00<00:50, 334kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   1%|          | 92.7k/16.9M [00:00<00:47, 357kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   1%|          | 147k/16.9M [00:00<00:42, 398kB/s] 

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   1%|1         | 213k/16.9M [00:00<00:37, 450kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   2%|1         | 296k/16.9M [00:00<00:31, 521kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   2%|2         | 390k/16.9M [00:00<00:27, 602kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   3%|2         | 496k/16.9M [00:00<00:23, 690kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   4%|3         | 616k/16.9M [00:00<00:20, 789kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   4%|4         | 750k/16.9M [00:00<00:18, 898kB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   5%|5         | 896k/16.9M [00:01<00:15, 1.01MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   6%|6         | 1.05M/16.9M [00:01<00:14, 1.13MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   7%|7         | 1.23M/16.9M [00:01<00:12, 1.27MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:   8%|8         | 1.43M/16.9M [00:01<00:10, 1.41MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  10%|9         | 1.64M/16.9M [00:01<00:09, 1.56MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  11%|#1        | 1.87M/16.9M [00:01<00:08, 1.73MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  13%|#2        | 2.12M/16.9M [00:01<00:07, 1.91MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  14%|#4        | 2.41M/16.9M [00:01<00:06, 2.13MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  16%|#6        | 2.73M/16.9M [00:01<00:06, 2.36MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  18%|#8        | 3.09M/16.9M [00:01<00:05, 2.62MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  21%|##        | 3.48M/16.9M [00:02<00:04, 2.91MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  23%|##3       | 3.93M/16.9M [00:02<00:04, 3.24MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  26%|##6       | 4.44M/16.9M [00:02<00:03, 3.65MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  29%|##9       | 5.00M/16.9M [00:02<00:02, 4.06MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  33%|###3      | 5.61M/16.9M [00:02<00:02, 4.51MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  37%|###7      | 6.29M/16.9M [00:02<00:02, 5.02MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  41%|####1     | 7.02M/16.9M [00:02<00:01, 5.53MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  46%|####6     | 7.82M/16.9M [00:02<00:01, 6.07MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  51%|#####1    | 8.68M/16.9M [00:02<00:01, 6.65MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  57%|#####6    | 9.61M/16.9M [00:02<00:01, 7.25MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  63%|######2   | 10.6M/16.9M [00:03<00:00, 7.88MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  69%|######8   | 11.7M/16.9M [00:03<00:00, 8.53MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  76%|#######5  | 12.8M/16.9M [00:03<00:00, 9.26MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  83%|########3 | 14.1M/16.9M [00:03<00:00, 10.0MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  91%|#########1| 15.4M/16.9M [00:03<00:00, 10.9MB/s]

hmi_m_45s_2017_01_25_16_30_45_tai_magnetogram.fits:  99%|#########9| 16.9M/16.9M [00:03<00:00, 11.7MB/s]

                                                                                                        
Files Downloaded: 100%|##########| 1/1 [00:12<00:00, 12.47s/file]
Files Downloaded: 100%|##########| 1/1 [00:12<00:00, 12.47s/file]
/home/docs/checkouts/readthedocs.org/user_builds/sunpy/conda/stable/lib/python3.7/asyncio/base_events.py:623: ResourceWarning: unclosed event loop <_UnixSelectorEventLoop running=False closed=False debug=False>
  source=self)

Download the SRS file.

srs_results = Fido.search(a.Time(start_time, end_time), a.Instrument('SRS_TABLE'))
srs_downloaded_files = Fido.fetch(srs_results)

Out:

Files Downloaded:   0%|          | 0/1 [00:00<?, ?file/s]

2017_SRS.tar.gz:   0%|          | 0.00/33.4k [00:00<?, ?B/s]

                                                            
Files Downloaded: 100%|##########| 1/1 [00:00<00:00,  3.86file/s]
Files Downloaded: 100%|##########| 1/1 [00:00<00:00,  3.86file/s]

Files Downloaded: 0file [00:00, ?file/s]
Files Downloaded: 0file [00:00, ?file/s]

We get one SRS file per day. To read this file, we pass the filename into the SRS reader. So now srs_table contains an astropy table.

srs_table = srs.read_srs(srs_downloaded_files[0])
print(srs_table)

Out:

 ID Number Carrington Longitude  Area ... Mag Type Latitude Longitude
                   deg           uSH  ...            deg       deg
--- ------ -------------------- ----- ... -------- -------- ---------
  I  12626                241.0  10.0 ...    Alpha      8.0      74.0
  I  12627                192.0  30.0 ...     Beta      6.0      25.0
  I  12628                172.0 210.0 ...     Beta     13.0       5.0
  I  12629                109.0  70.0 ...     Beta     15.0     -59.0

We only need the rows which have ‘ID’ = ‘I’ or ‘IA’.

if 'I' in srs_table['ID'] or 'IA' in srs_table['ID']:
    srs_table = srs_table[np.logical_or(srs_table['ID'] == 'I',
                                        srs_table['ID'] == 'IA')]
else:
    print("Warning : No I or IA entries for this date.")
    srs_table = None

Now we extract the latitudes, longitudes and the region numbers. We make an empty list if there are no ARs.

if srs_table is not None:
    lats = srs_table['Latitude']
    lngs = srs_table['Longitude']
    numbers = srs_table['Number']
else:
    lats = lngs = numbers = []

Let’s plot the results by defining coordinates for each location.

ax = plt.subplot(projection=smap)
smap.plot(vmin=-120, vmax=120)
smap.draw_limb()
ax.set_autoscale_on(False)

if len(lats) > 0:
    c = SkyCoord(lngs, lats, frame="heliographic_stonyhurst")
    ax.plot_coord(c, 'o')

    for i, num in enumerate(numbers):
        ax.annotate(num, (lngs[i].value, lats[i].value),
                    xycoords=ax.get_transform('heliographic_stonyhurst'))
plt.show()
../../../_images/sphx_glr_magnetogram_active_regions_001.png

Total running time of the script: ( 0 minutes 24.333 seconds)

Gallery generated by Sphinx-Gallery