.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/cross_section.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_cross_section.py: ====================== Cross Section Analysis ====================== The MetPy function `metpy.interpolate.cross_section` can obtain a cross-sectional slice through gridded data. .. GENERATED FROM PYTHON SOURCE LINES 12-23 .. code-block:: Python import cartopy.crs as ccrs import cartopy.feature as cfeature import matplotlib.pyplot as plt import numpy as np import xarray as xr import metpy.calc as mpcalc from metpy.cbook import get_test_data from metpy.interpolate import cross_section .. GENERATED FROM PYTHON SOURCE LINES 24-32 **Getting the data** This example uses [NARR reanalysis data]( https://www.ncei.noaa.gov/products/weather-climate-models/north-american-regional) for 18 UTC 04 April 1987 from NCEI. We use MetPy's CF parsing to get the data ready for use, and squeeze down the size-one time dimension. .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: Python data = xr.open_dataset(get_test_data('narr_example.nc', False)) data = data.metpy.parse_cf().squeeze() print(data) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 21MB Dimensions: (isobaric: 29, y: 118, x: 292) Coordinates: time datetime64[ns] 8B 1987-04-04T18:00:00 * isobaric (isobaric) float64 232B 1e+03 975.0 ... 125.0 100.0 * y (y) float64 944B -3.087e+06 -3.054e+06 ... 7.114e+05 * x (x) float64 2kB -3.977e+06 -3.945e+06 ... 5.47e+06 metpy_crs object 8B Projection: lambert_conformal_conic Data variables: Temperature (isobaric, y, x) float32 4MB ... Lambert_Conformal |S1 1B ... lat (y, x) float64 276kB ... lon (y, x) float64 276kB ... u_wind (isobaric, y, x) float32 4MB ... v_wind (isobaric, y, x) float32 4MB ... Geopotential_height (isobaric, y, x) float32 4MB ... Specific_humidity (isobaric, y, x) float32 4MB ... Attributes: (12/14) Conventions: CF-1.0 Originating_center: US National Weather Service - NCEP(WMC) (7) Originating_subcenter: The North American Regional Reanalysis (NARR) P... Generating_Model: North American Regional Reanalysis (NARR) Product_Type: Forecast/Uninitialized Analysis/Image Product title: US National Weather Service - NCEP(WMC) North A... ... ... history: Direct read of GRIB-1 into NetCDF-Java 4 API CF:feature_type: GRID file_format: GRIB-1 location: /nomads3_data/raid2/noaaport/merged/narr/198704... _CoordinateModelRunDate: 1987-04-04T18:00:00Z History: Translated to CF-1.0 Conventions by Netcdf-Java... .. GENERATED FROM PYTHON SOURCE LINES 38-39 Define start and end points: .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python start = (37.0, -105.0) end = (35.5, -65.0) .. GENERATED FROM PYTHON SOURCE LINES 44-45 Get the cross section, and convert lat/lon to supplementary coordinates: .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: Python cross = cross_section(data, start, end).set_coords(('lat', 'lon')) print(cross) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 120kB Dimensions: (isobaric: 29, index: 100) Coordinates: time datetime64[ns] 8B 1987-04-04T18:00:00 * isobaric (isobaric) float64 232B 1e+03 975.0 ... 125.0 100.0 metpy_crs object 8B Projection: lambert_conformal_conic x (index) float64 800B 1.818e+05 2.18e+05 ... 3.712e+06 y (index) float64 800B -1.454e+06 ... -5.573e+05 * index (index) int64 800B 0 1 2 3 4 5 6 ... 94 95 96 97 98 99 lat (index) float64 800B 37.0 37.05 37.11 ... 35.58 35.5 lon (index) float64 800B -105.0 -104.6 ... -65.39 -65.0 Data variables: Temperature (isobaric, index) float64 23kB 287.7 286.9 ... 211.4 Lambert_Conformal |S1 1B ... u_wind (isobaric, index) float64 23kB -2.729 0.4776 ... 23.68 v_wind (isobaric, index) float64 23kB 8.473 5.723 ... -1.082 Geopotential_height (isobaric, index) float64 23kB 118.6 ... 1.636e+04 Specific_humidity (isobaric, index) float64 23kB 0.006367 ... 4.223e-06 Attributes: (12/14) Conventions: CF-1.0 Originating_center: US National Weather Service - NCEP(WMC) (7) Originating_subcenter: The North American Regional Reanalysis (NARR) P... Generating_Model: North American Regional Reanalysis (NARR) Product_Type: Forecast/Uninitialized Analysis/Image Product title: US National Weather Service - NCEP(WMC) North A... ... ... history: Direct read of GRIB-1 into NetCDF-Java 4 API CF:feature_type: GRID file_format: GRIB-1 location: /nomads3_data/raid2/noaaport/merged/narr/198704... _CoordinateModelRunDate: 1987-04-04T18:00:00Z History: Translated to CF-1.0 Conventions by Netcdf-Java... .. GENERATED FROM PYTHON SOURCE LINES 50-52 For this example, we will be plotting potential temperature, relative humidity, and tangential/normal winds. And so, we need to calculate those, and add them to the dataset: .. GENERATED FROM PYTHON SOURCE LINES 52-71 .. code-block:: Python cross['Potential_temperature'] = mpcalc.potential_temperature( cross['isobaric'], cross['Temperature'] ) cross['Relative_humidity'] = mpcalc.relative_humidity_from_specific_humidity( cross['isobaric'], cross['Temperature'], cross['Specific_humidity'] ) cross['u_wind'] = cross['u_wind'].metpy.convert_units('knots') cross['v_wind'] = cross['v_wind'].metpy.convert_units('knots') cross['t_wind'], cross['n_wind'] = mpcalc.cross_section_components( cross['u_wind'], cross['v_wind'] ) print(cross) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 213kB Dimensions: (isobaric: 29, index: 100) Coordinates: time datetime64[ns] 8B 1987-04-04T18:00:00 * isobaric (isobaric) float64 232B 1e+03 975.0 ... 125.0 100.0 metpy_crs object 8B Projection: lambert_conformal_conic x (index) float64 800B 1.818e+05 2.18e+05 ... 3.712e+06 y (index) float64 800B -1.454e+06 ... -5.573e+05 * index (index) int64 800B 0 1 2 3 4 5 ... 94 95 96 97 98 99 lat (index) float64 800B 37.0 37.05 37.11 ... 35.58 35.5 lon (index) float64 800B -105.0 -104.6 ... -65.39 -65.0 Data variables: Temperature (isobaric, index) float64 23kB 287.7 286.9 ... 211.4 Lambert_Conformal |S1 1B ... u_wind (isobaric, index) float64 23kB ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: cross_section.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_