<p>I have a 2-D latitude-time zonal wind netCDF dataset (such as <a href="https://drive.google.com/file/d/1MQVvg2qICY5AxVAMrtmW_5aPc9bA_Q-W/view?usp=sharing" rel="nofollow noreferrer">this small sample from ERA-Interim</a>) from which I want to find all local maxima of the zonal wind as a function of latitude, for each time step. The output should be a list of latitudes (possibly empty) for each time step.</p>
<p>I would prefer to use mostly python, cdo and/or nco, if possible. I can use the derivative function in python xarray and specify the latitude dimension, for example, and then find roots of this new function subject to a condition on the second derivate, but I've not come across a function for finding zeroes/roots of a 2-D array along a specific dimension. Is there a more efficient, preferable way of going about this task?</p>
<p>Please let me know if this is not appropriate for the Earth Science Stack Exchange and where I should rather post the question if so.</p>

CC BY-SA 4.0
2022-01-06T04:44:25.397Z
If you're using xarray, you have direct access to the values of the array and you can apply pre-existing local extrema-finding functions, such as https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelextrema.html or https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html (which allows you to set a minimum distance between consecutive minima/maxima). Would that help?
Erik
CC BY-SA 4.0
2022-01-06T08:57:39.213Z
@Erik, thank you, yes, `scipy.signal.find_peaks` seems to be particularly flexible and useful. I was hoping for something that could possibly estimate the location of a maximum/minimum between grid locations, but that's not strictly necessary. Could you suggest an efficient way of applying `scipy.signal.find_peaks` over order 10^5 timesteps? Or is that a separate question?
climatestudent
CC BY-SA 4.0
2022-01-06T14:13:51.233Z
Hmm, no I suspect (but leave this open to other commenters) that these types of statistics about local extrema are computationally expensive; algorithms must check each sample, possibly multiple times. Any speedup can probably only be achieved using your computing resources maximally, e.g.: a multiprocessing approach following https://docs.python.org/3/library/multiprocessing.html can at least make sure you process 8 (or more) NetCDF files in parallel.
Erik
<climatology><netcdf>
Efficient way to find spatial extrema in 2-D space time netcdf field
climatestudent
# Efficient way to find spatial extrema in 2-D space time netcdf field

(Asked by: username_0 on 2022-01-05)

I have a 2-D latitude-time zonal wind netCDF dataset (such as [this small sample from ERA-Interim](https://drive.google.com/file/d/1MQVvg2qICY5AxVAMrtmW_5aPc9bA_Q-W/view?usp=sharing)) from which I want to find all local maxima of the zonal wind as a function of latitude, for each time step. The output should be a list of latitudes (possibly empty) for each time step.

I would prefer to use mostly python, cdo and/or nco, if possible. I can use the derivative function in python xarray and specify the latitude dimension, for example, and then find roots of this new function subject to a condition on the second derivate, but I've not come across a function for finding zeroes/roots of a 2-D array along a specific dimension. Is there a more efficient, preferable way of going about this task?

Please let me know if this is not appropriate for the Earth Science Stack Exchange and where I should rather post the question if so.

**Tags:** climatology, netcdf

> **Comments:**
> **username_1**: If you're using xarray, you have direct access to the values of the array and you can apply pre-existing local extrema-finding functions, such as https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelextrema.html or https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html (which allows you to set a minimum distance between consecutive minima/maxima). Would that help?  
> **username_0**: @username_1, thank you, yes, `scipy.signal.find_peaks` seems to be particularly flexible and useful. I was hoping for something that could possibly estimate the location of a maximum/minimum between grid locations, but that's not strictly necessary. Could you suggest an efficient way of applying `scipy.signal.find_peaks` over order 10^5 timesteps? Or is that a separate question?  
> **username_1**: Hmm, no I suspect (but leave this open to other commenters) that these types of statistics about local extrema are computationally expensive; algorithms must check each sample, possibly multiple times. Any speedup can probably only be achieved using your computing resources maximally, e.g.: a multiprocessing approach following https://docs.python.org/3/library/multiprocessing.html can at least make sure you process 8 (or more) NetCDF files in parallel.  

---

## 0 answers

