MetPy
MetPy copied to clipboard
Add distance along cross section to `interpolate_to_slice`
What should we add?
Currently, MetPy's metpy.interpolate.cross_section and metpy.interpolate.interpolate_to_slice return DataArrays/Datasets with an added "index" dimension coordinate that simply contains the range of the slice's length. For many applications (particularly plotting), the distance along the slice is much more useful, and often something I've seen people need to hack onto the DataArray/Dataset after the fact. I'm thinking we could easily add this by replacing
data_sliced.coords['index'] = range(len(points))
with something like
data_sliced.coords['distance'] = xr.DataArray(
np.linalg.norm(points - points[0], axis=1),
dims='index',
coords={'index': range(len(points))},
attrs=(
{'units': x.attrs['units']}
if getattr(x.attrs, 'units', None) == getattr(y.attrs, 'units', None)
else {}
)
)
This is just a mock-up, I think the real version should:
- take
distance_coord_nameas a optional kwarg (just in case a user already has something else called "distance" in their data) - verify proper handling of both units-on-attribute and units-in-quantity
Reference
No response
Seems this hasn't been added to the slices.py, it's really a nice feature.