Xarray find lat/lon coordinates for maximum/minimum values for each timestep


As the title says, supposing I have a ds with coords: [time lat lon], how can I obtain for each timestep in time the pair of ['lat','lon'] in which the maximum(or minimum) value for a given variable is located.

Asked By: Henrique



Use xr.Dataset.idxmax to find the index label of the maximum along a dimension (one at a time). Same for xr.Dataset.idxmin.

max_lons = ds.max(dim="lat").idxmax(dim="lon")
max_lats = ds.max(dim="lon").idxmax(dim="lat")

The results will be datasets, with each variable giving the lon or lat coresponding to the maximum in each time step for that variable.

Answered By: Michael Delgado
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.