.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/suvi/plot_suvi_l2_brght.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_brght.py: Plot SUVI L2 Bright Regions =========================== Purpose: Use Python to plot SUVI L2 bright region (brght) product in various coordinate systems. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Overview ------------ .. GENERATED FROM PYTHON SOURCE LINES 14-32 In this example, we will be plotting the bright region boundaries (bnd_loc), center location (center_loc), peak location (peak_loc), and bright region extent (brght_extent) for the bright region report (brght) product. Bright region boundaries outline the bright region, which are akin to coronal active regions. Center location is determined by center-of-mass calculations, whereas peak location is determined by the region with the highest irradiance. The bright region extent is defined by the min/max lat/lon of all vertices of the bright region boundary polygon. `Notes for this product: The extent is only available in the Heliographic Stonyhurst coordinate system. R-Theta is a legacy operational coordinate system that does not contain boundary vertices.` We will plot these variables 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 34-36 Imports ----------- .. GENERATED FROM PYTHON SOURCE LINES 38-39 First, we will import the necessary libraries: .. GENERATED FROM PYTHON SOURCE LINES 39-54 .. 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 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 55-58 Helper Functions ----------------------------------------- We need to define a few helper functions for use throughout plotting: .. GENERATED FROM PYTHON SOURCE LINES 58-74 .. 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 75-83 .. 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 84-109 .. code-block:: Python # Define legend for this product. def legend_handles(colors, coord='hg'): if coord == 'hg': markers = ['o', '*', 'o', '_'] labels = ['boundaries', 'peak', 'center', 'extent'] linestyles = ['-', 'none', 'none', '-'] fillstyles = ['full', 'full', 'none', 'full'] elif coord in ['car', 'pix', 'hpc', 'hcc', 'hcr']: markers = ['o', '*', 'o'] labels = ['boundaries', 'peak', 'center'] linestyles = ['-', 'none', 'none'] fillstyles = ['full', 'full', 'none'] elif coord in ['rt']: markers = ['*', 'o'] labels = ['peak', 'center'] linestyles = ['none', 'none'] fillstyles = ['full', 'none'] else: return None, None f = lambda m, c, ls, fs: plt.plot([], [], marker=m, color=c, ls=ls, fillstyle=fs)[0] handles = [f(markers[i], colors[i], linestyles[i], fillstyles[i]) for i in range(len(colors))] return handles, labels .. GENERATED FROM PYTHON SOURCE LINES 110-112 Retrieving the SUVI SunPy Map ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 114-117 | Let's use some of the helper functions above to retrieve a SUVI image. | We'll grab an image of the sun in the 284Å wavelength for datetime 2023-01-09 18:51. | Example data file: `dr_suvi-l2-brght_g16_s20230109T184800Z_e20230109T185200Z_v1-0-5.nc <../../_static/suvi/dr_suvi-l2-brght_g16_s20230109T184800Z_e20230109T185200Z_v1-0-5.nc>`__ .. GENERATED FROM PYTHON SOURCE LINES 117-124 .. code-block:: Python date = datetime(2023, 1, 9, 18, 51) goes = 16 wavelength = 284 suvi_brght_path = f'../../data/suvi/dr_suvi-l2-brght_g16_s20230109T184800Z_e20230109T185200Z_v1-0-5.nc' brght_nc = nc.Dataset(suvi_brght_path) .. GENERATED FROM PYTHON SOURCE LINES 125-126 Let's look at the variables contained in the bright region files. .. GENERATED FROM PYTHON SOURCE LINES 126-129 .. code-block:: Python for k in brght_nc.variables.keys(): print(k) .. rst-class:: sphx-glr-script-out .. code-block:: none time wavelength degraded_status num_brght_regions brght_area srs_status xrs_status euv_status brght_extent_hg 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 center_loc_pix center_loc_hg center_loc_car center_loc_rtheta center_loc_hpc center_loc_hcc center_loc_hcr bnd_loc_pix bnd_loc_hg bnd_loc_car bnd_loc_hpc bnd_loc_hcc bnd_loc_hcr .. GENERATED FROM PYTHON SOURCE LINES 130-133 For our examples, we will be looking at the bnd_loc, peak_loc, center_loc, and brght_extent 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 133-136 .. code-block:: Python print(brght_nc['bnd_loc_hg']) .. rst-class:: sphx-glr-script-out .. code-block:: none float32 bnd_loc_hg(time, feature_number, vertex, location) long_name: Bright region boundary 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, vertex current shape = (1, 12, 16, 2) filling on .. GENERATED FROM PYTHON SOURCE LINES 137-142 | The structure for bnd_loc in this example is `(1, 7, 16, 2)`: | `1` : The first dimension of the structure is the time dimension, which will always have 1 value for this product. | `7` : The second dimension is the number of separate bright regions within this image. | `16`: The third dimension is each vertex of the bright region boundary polygon. We are limited to 16 vertices for legacy operational reasons. | `2` : The fourth dimension is the coordinate pair, with units as described above. .. GENERATED FROM PYTHON SOURCE LINES 142-147 .. code-block:: Python print(brght_nc['peak_loc_hg']) print('') print(brght_nc['center_loc_hg']) .. rst-class:: sphx-glr-script-out .. code-block:: none 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, 12, 6, 2) filling on float32 center_loc_hg(time, feature_number, wavelength, location) long_name: Centroid 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, 12, 6, 2) filling on .. GENERATED FROM PYTHON SOURCE LINES 148-153 | The structures for peak_loc and center_loc in this example are `(1, 7, 6, 2)`: | `1`: The first dimension of the structure is the time dimension, which will always have 1 value for this product. | `7`: The second dimension is the number of separate bright regions 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 153-156 .. code-block:: Python print(brght_nc['brght_extent_hg']) .. rst-class:: sphx-glr-script-out .. code-block:: none float32 brght_extent_hg(time, feature_number, cardinal_directions) long_name: Maximum extent of bright region in the [North (lat), South (lat), East (lon) and West (lon)] in Stonyhurst/heliographic coordinates units: degrees _FillValue: -9999.0 unlimited dimensions: time, feature_number current shape = (1, 12, 4) filling on .. GENERATED FROM PYTHON SOURCE LINES 157-161 | The structure for brght_extent in this example is `(1, 7, 4)`: | `1`: The first dimension of the structure is the time dimension, which will always have 1 value for this product. | `7`: The second dimension is the number of separate bright regions within this image. | `4`: The third dimension is an array of the N/S Latitudes [0, 1] and the E/W Longitudes [2, 3] of the bright region bounding box, in that order. .. GENERATED FROM PYTHON SOURCE LINES 164-166 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 166-172 .. code-block:: Python # Get file time brght_time = convert_time(brght_nc['time'][:][0]) # Access SUVI SunPy map for that time and defined wavelength suvi_map, suvi_data, suvi_header = create_suvi_sunpymap(brght_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/10 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_suvi_l2_brght.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_suvi_l2_brght.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_