-
I am using this model data output for salinity and temperature, and it has 2d lons and lats, and I am hoping to take a cross section of the data. Basically to get an output array of the field of interest, "ds_along_section", which has the "nearest neighbors" along the cross section lined up along a new dimension. The new dimension "ds_along_section" later represents distance which can be calculated using the Haversine formula. The data array has a shape of time, depth, y, and x but has coordinates For 1D lon and latitude, xarray's vectorized selections works wonders, but with 2D lat and lon arrays there becomes a limitation. Unfortunately, this is not so reproducible, as the model data is needed. As an additional attempt to take a cross section, I took a cross section using the package region mask:
Is it possible to drop all nans except along the "line" of data? If that makes sense? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
As you noted, having 1D dimension coordinates enables xarray's vectorized indexing functionality (which is all MetPy is doing under the hood). And so, one solution to taking a cross section when you have 2D lon and lat arrays (and probably the most efficient) is to re-derive the "missing" x and y 1D dimension coordinates given your data's projection (if sticking with MetPy, Alternatively, you can index the data with the 2D lon and lat arrays directly using the extension package |
Beta Was this translation helpful? Give feedback.
As you noted, having 1D dimension coordinates enables xarray's vectorized indexing functionality (which is all MetPy is doing under the hood). And so, one solution to taking a cross section when you have 2D lon and lat arrays (and probably the most efficient) is to re-derive the "missing" x and y 1D dimension coordinates given your data's projection (if sticking with MetPy,
assign_y_x
should do the trick if you've added the projection information properly), and then use that built-in vectorized indexing.Alternatively, you can index the data with the 2D lon and lat arrays directly using the extension package
xoak
, as in this example. While this is likely to be less efficient (and may requ…