.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/suvi/plot_suvi_l2_flloc.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_suvi_plot_suvi_l2_flloc.py: Plot SUVI L2 Flare Locations ============================ Purpose: Use Python to plot the SUVI L2 flare location (flloc) product in various coordinate systems. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Overview ------------ .. GENERATED FROM PYTHON SOURCE LINES 14-26 In this example, we will be plotting both the flare location (flloc) and peak location (peak_loc) variables from the flare location product. Flare location is determined by center-of-mass calculations, whereas peak location is determined by the region with the highest irradiance. We will plot both flloc and peak_loc for each of the following coordinate systems: * Heliographic Stonyhurst `(longitude, latitude)` * Heliographic Carrington `(longitude, latitude)` * Pixel `(x pixels, y pixels)` * R-Theta `(solar radii, degrees)` * Helioprojective Cartesian `(x arcsec, y arcsec)` * Heliocentric Cartesian `(x AU, y AU, z AU)` * Heliocentric Radial `(solar radii, degrees, z solar radii)` .. GENERATED FROM PYTHON SOURCE LINES 28-30 Imports ----------- .. GENERATED FROM PYTHON SOURCE LINES 32-33 First, we will import the necessary libraries: .. GENERATED FROM PYTHON SOURCE LINES 33-50 .. code-block:: Python __authors__ = "elucas, ajarvis" import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np from datetime import datetime, timedelta import netCDF4 as nc import astropy from astropy.coordinates import SkyCoord import sunpy import sunpy.map import astropy.units as u from sunpy.coordinates import frames from sunpy.net import Fido, attrs as a from astropy.io import fits .. GENERATED FROM PYTHON SOURCE LINES 51-54 Helper Functions ----------------------------------------- We need to define a few helper functions for use throughout plotting: .. GENERATED FROM PYTHON SOURCE LINES 54-70 .. code-block:: Python # Fetch the SUVI SunPy map to use as a base for plotting. def create_suvi_sunpymap(date, goes=16, wavelength=131, rng=2): ds0 = (date - timedelta(minutes=rng)).strftime("%Y/%m/%d %H:%M:%S") ds1 = (date + timedelta(minutes=rng)).strftime("%Y/%m/%d %H:%M:%S") q = Fido.search(a.Time(ds0, ds1), a.Instrument.suvi, a.Wavelength(wavelength*u.angstrom)) tmp_files = Fido.fetch(q) # Select only files for level 2 composites from G16 for tmp_file in tmp_files: if (f'g{goes}' in tmp_file) and ('l2' in tmp_file): data, header = fits.getdata(tmp_file, ext=1), fits.getheader(tmp_file, ext=1) suvi_map = sunpy.map.Map(data, header) return suvi_map, data, header print('No SUVI map available') return None .. GENERATED FROM PYTHON SOURCE LINES 71-79 .. code-block:: Python # Convert time given in the .nc file to datetime. def convert_time(time_nc): date_2000 = datetime(2000, 1, 1, 12, 0) date = date_2000 + timedelta(seconds=time_nc) return date .. GENERATED FROM PYTHON SOURCE LINES 80-92 .. code-block:: Python # Define legend for this product. def legend_handles(): markers = ['o', '*'] fillstyle = ['none', 'full'] labels = ['flloc', 'peak_loc'] f = lambda m, fill: plt.plot([], [], marker=m, color='k', ls='none', fillstyle=fill)[0] handles = [f(markers[i], fillstyle[i]) for i in range(len(markers))] return handles, labels .. GENERATED FROM PYTHON SOURCE LINES 93-95 Retrieving the SUVI SunPy Map ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 97-100 | Let's use some of the helper functions above to retrieve a SUVI image. | We'll grab an image of the sun in the 131Å wavelength for datetime 2024-01-23 03:44. | Example data file: `dr_suvi-l2-flloc_g16_s20240123T034400Z_e20240123T034800Z_v1-0-6.nc <../../_static/suvi/dr_suvi-l2-flloc_g16_s20240123T034400Z_e20240123T034800Z_v1-0-6.nc>`__ .. GENERATED FROM PYTHON SOURCE LINES 100-107 .. code-block:: Python date = datetime(2024, 1, 23, 3, 44) goes = 16 wavelength = 131 suvi_flloc_path = f'../../data/suvi/dr_suvi-l2-flloc_g16_s20240123T034400Z_e20240123T034800Z_v1-0-6.nc' flloc_nc = nc.Dataset(suvi_flloc_path) .. GENERATED FROM PYTHON SOURCE LINES 108-109 Let's look at the variables contained in the flare location files. .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: Python for k in flloc_nc.variables.keys(): print(k) .. rst-class:: sphx-glr-script-out .. code-block:: none time wavelength degraded_status num_flloc brght_area srs_status xrs_status euv_status flloc_pix flloc_hg flloc_car flloc_rtheta flloc_hpc flloc_hcc flloc_hcr tot_flux peak_flux peak_loc_pix peak_loc_hg peak_loc_car peak_loc_rtheta peak_loc_hpc peak_loc_hcc peak_loc_hcr .. GENERATED FROM PYTHON SOURCE LINES 113-116 For our examples, we will be looking at the flloc and peak_loc variables. We can see the general structure by inspecting the variables in any coordinate system, for example, in Heliographic Stonyhurst. .. GENERATED FROM PYTHON SOURCE LINES 116-121 .. code-block:: Python print(flloc_nc['flloc_hg']) print('') print(flloc_nc['peak_loc_hg']) .. rst-class:: sphx-glr-script-out .. code-block:: none float32 flloc_hg(time, feature_number, wavelength, location) long_name: Flare location in Stonyhurst/heliographic coordinates (lon, lat) comments: Values provided for on-disk flares only. units: degrees, degrees _FillValue: -9999.0 unlimited dimensions: time, feature_number, wavelength current shape = (1, 2, 6, 2) filling on float32 peak_loc_hg(time, feature_number, wavelength, location) long_name: Peak bright region flux location in Stonyhurst/heliographic coordinates (lon, lat) comments: Values provided for on-disk flares only. units: degrees, degrees _FillValue: -9999.0 unlimited dimensions: time, feature_number, wavelength current shape = (1, 2, 6, 2) filling on .. GENERATED FROM PYTHON SOURCE LINES 122-127 | The structure for both in this example is `(1, 2, 6, 2)`: | `1`: The first dimension of the structure is the time dimension, which will always have 1 value for this product. | `2`: The second dimension is the number of separate flares within this image. | `6`: The third dimension is each wavelength, in the order [094, 131, 171, 195, 284, 304]. | `2`: The fourth dimension is the coordinate pair (3 for HCC and HCR), with units as described above. .. GENERATED FROM PYTHON SOURCE LINES 129-131 We need to extract the time (found in the 'time' variable), and convert to python datetime in order to fetch a corresponding SUVI SunPy map at the closest time possible. .. GENERATED FROM PYTHON SOURCE LINES 131-137 .. code-block:: Python # Get file time flloc_time = convert_time(flloc_nc['time'][:][0]) # Access SUVI SunPy map for that time and defined wavelength suvi_map, suvi_data, suvi_header = create_suvi_sunpymap(flloc_time, goes=goes, wavelength=wavelength) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/micromamba/envs/goesr-spwx-examples/lib/python3.12/site-packages/sunpy/net/vso/vso.py:222: SunpyUserWarning: VSO-D400 Bad Request - Invalid wavelength, wavetype of filter specification warn_user(resp["error"]) Files Downloaded: 0%| | 0/8 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_suvi_l2_flloc.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_suvi_l2_flloc.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_