.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/suvi/plot_suvi_l2_chbnd.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_chbnd.py: Plot SUVI L2 Coronal Hole Boundaries ==================================== Purpose: Use Python to plot the SUVI L2 coronal hole boundary (chbnd) product in various coordinate systems. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Overview ------------ .. GENERATED FROM PYTHON SOURCE LINES 14-27 In this example, we will be plotting the coronal hole boundaries (chbnd_loc) from the coronal hole boundary product. The boundaries are determined by outlining the coronal hole classifications from the SUVI Thematic Map algorithm. We will plot chbnd_loc for each of the following coordinate systems: * Heliographic Stonyhurst `(longitude, latitude)` * Heliographic Carrington `(longitude, latitude)` * Pixel `(x pixels, y pixels)` * Helioprojective Cartesian `(x arcsec, y arcsec)` * Heliocentric Cartesian `(x AU, y AU, z AU)` * Heliocentric Radial `(solar radii, degrees, z solar radii)` `Note for this product: The extent is only available in the Heliographic Stonyhurst coordinate system.` .. GENERATED FROM PYTHON SOURCE LINES 30-32 Imports ----------- .. GENERATED FROM PYTHON SOURCE LINES 34-35 First, we will import the necessary libraries: .. GENERATED FROM PYTHON SOURCE LINES 35-51 .. 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 52-55 Helper Functions ----------------------------------------- We need to define a few helper functions for use throughout plotting: .. GENERATED FROM PYTHON SOURCE LINES 55-71 .. 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 72-80 .. 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 81-96 .. code-block:: Python # Define legend for this product. def legend_handles(coord=''): markers = ['o'] fillstyle = ['full'] labels = ['chbnd_loc'] if coord == 'hg': markers += ['_'] fillstyle += ['full'] labels += ['chbnd_extent'] f = lambda m, fill: plt.plot([], [], marker=m, color='k', ls='-', fillstyle=fill)[0] handles = [f(markers[i], fillstyle[i]) for i in range(len(markers))] return handles, labels .. GENERATED FROM PYTHON SOURCE LINES 97-99 Retrieving the SUVI SunPy Map ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 101-104 | Let's use some of the helper functions above to retrieve a SUVI image. | We'll grab an image of the sun in the 195Å wavelength for datetime 2024-09-12 23:28. | Example data file: `dr_suvi-l2-chbnd_g16_s20240912T232800Z_e20240912T233200Z_v1-0-6.nc <../../_static/suvi/dr_suvi-l2-chbnd_g16_s20240912T232800Z_e20240912T233200Z_v1-0-6.nc>`__ .. GENERATED FROM PYTHON SOURCE LINES 104-111 .. code-block:: Python date = datetime(2024, 9, 12, 23, 28) goes = 16 wavelength = 195 suvi_chbnd_path = f'../../data/suvi/dr_suvi-l2-chbnd_g16_s20240912T232800Z_e20240912T233200Z_v1-0-6.nc' chbnd_nc = nc.Dataset(suvi_chbnd_path) .. GENERATED FROM PYTHON SOURCE LINES 112-113 Let's look at the variables contained in the coronal hole boundary files. .. GENERATED FROM PYTHON SOURCE LINES 113-116 .. code-block:: Python for k in chbnd_nc.variables.keys(): print(k) .. rst-class:: sphx-glr-script-out .. code-block:: none time degraded_status num_chbnd chbnd_area chbnd_extent_hg chbnd_loc_pix chbnd_loc_hg chbnd_loc_car chbnd_loc_hpc chbnd_loc_hcc chbnd_loc_hcr .. GENERATED FROM PYTHON SOURCE LINES 117-120 For our examples, we will be looking at the chbnd_loc variable. We can see the general structure by inspecting this variable in any coordinate system, for example, in Heliographic Stonyhurst. .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: Python print(chbnd_nc['chbnd_loc_hg']) .. rst-class:: sphx-glr-script-out .. code-block:: none float32 chbnd_loc_hg(time, feature_number, vertex, location) long_name: Coronal hole 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, 4, 16, 2) filling on .. GENERATED FROM PYTHON SOURCE LINES 124-129 | The structure in this example is `(1, 4, 16, 2)`: | `1` : The first dimension of the structure is the time dimension, which will always have 1 value for this product. | `4` : The second dimension is the number of separate coronal holes within this image. | `16`: The third dimension is each vertex of the coronal hole boundary polygon. We are limited to 16 vertices for legacy operational reasons. | `2` : The fourth dimension is the coordinate pair (3 for HCC and HCR) of each vertex, with units as described above. .. GENERATED FROM PYTHON SOURCE LINES 131-133 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 133-139 .. code-block:: Python # Get file time chbnd_time = convert_time(chbnd_nc['time'][:][0]) # Access SUVI SunPy map for that time and defined wavelength suvi_map, suvi_data, suvi_header = create_suvi_sunpymap(chbnd_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/16 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_suvi_l2_chbnd.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_suvi_l2_chbnd.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_