Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get n smallest values of multidimensional xarray.DataArray

I'm currently working with some weather data that I have as netcdf files which I can easily read with pythons xarray library
I would now like to get the n smallest values of my DataArray which has 3 dimensions (longitude, latitude and time)
When I have a DataArray dr, I can just do dr.min(), maybe specify an axis and then I get the minimum, but when I want to get also the second smallest or even a variable amount of smallest values, it seems not to be as simple What I currently do is:

with xr.open_dataset(path) as ds:
    dr = ds[selection]
    dr = dr.values.reshape(dr.values.size)
    dr.sort()
    n_smallest = dr[0:n]

which seems a bit complicated to me compared to the simple .min() I have to type for the smallest value
I actually want to get the times to the respective smallest values which I do for the smallest with:

dr.where(dr[selection] == dr[selection].min(), drop=True)[time].values

so is there a better way of getting the n smallest values? or maybe even a simple way to get the times for the n smallest values?
maybe there is a way to reduce the 3D DataArray along the longitude and latitude axis to the respective smallest values?

like image 653
Marcel H. Avatar asked Jan 23 '26 12:01

Marcel H.


1 Answers

I just figured out there really is a reduce function for DataArray that allows me to reduce along longitude and latitude and as I don't reduce the time dimension, I can just use the sortby function and get the DataArray with min values for each day with their respective times:

with xr.open_dataset(path) as ds:
    dr = ds[selection]
    dr = dr.reduce(np.min,dim=[longitude,latitude])
    dr.sortby(dr)

which is obviously not shorter than my original code, but perfectly satisfies my demands

like image 165
Marcel H. Avatar answered Jan 26 '26 23:01

Marcel H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!