Get all grid values on a diagonal line from point to point in xarray

968 views Asked by At

I'm looking to select all data from an xarray dataset that is between one point and another. This is of course easy with sel for example with a dataset based on a lat/lon grid to subset from a global dataset to the Atlantic coastline I can do ds_geo_subset = ds.sel( lat=slice(42, 20), lon=slice(-85,-65)) but I want to select all data along the Gulf Stream front, basically from to point (-78 lon, 32 lat) to (-72 lon, 36 lat) but I'm not sure how to do this. Would this be best done by giving a full point dataset and getting data from each point or can these two end points be given to xarray and interpolated data returned?

1

There are 1 answers

1
Light_B On BEST ANSWER

As the comment above suggested, Metpy has a specific function which can be handy for this task.

from metpy.interpolate import cross_section

data = xr.open_dataset('my_data')

# Define start and end points:

start = (-78, 32)
end = (-72, 36)
cross = cross_section(data, start, end).set_coords(('lat', 'lon'))

Here is a nice example to plot a cross-section.