, linestring)[source]#

Obtain an interpolated cross-sectional slice through gridded data. Utilizing the interpolation functionality in xarray, this function takes a vertical cross-sectional slice along a linestring through the given data on a regular grid, which is given as an xarray.DataArray so that we can utilize its coordinate data.

Adapted from Metpy: Unidata/MetPy

  • data (xarray.DataArray or xarray.Dataset) –

    Three- (or higher) dimensional field(s) to interpolate. The DataArray (or each DataArray in the Dataset) must have been parsed by MetPy and include both an x and y coordinate dimension and the added crs


  • linestring (shapely.geometry.LineString) –

    Shapely geometry designating the linestring along which to sample the cross section.

    Note that a LineString can easily be taken from a geopandas.GeoDataFrame using the .geometry attribute. Please refer to the examples.


The interpolated cross section, with new index dimension along the cross-section.

Return type:

xarray.DataArray or xarray.Dataset


Load a shapefile (that you might have drawn before using a GIS program), take a linestring from it, and use it to extract the data for a cross section.

>>> geodataframe = gpd.read_file("cross_section.shp")
>>> linestring = geodataframe.geometry[0]
>>> section = cross_section_linestring(data, linestring)

Or, construct the linestring directly in Python:

>>> import shapely.geometry as sg
>>> linestring = sg.LineString([(0.0, 1.0), (5.0, 5.0), (7.5, 5.0)])
>>> section = cross_section_linestring(data, linestring)

If you have drawn multiple cross sections within a shapefile, simply loop over the linestrings:

>>> sections = [cross_section_linestring(data, ls) for ls in geodataframe.geometry]