3

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?

Marcel H.
  • 197
  • 1
  • 2
  • 14

1 Answers1

1

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

Marcel H.
  • 197
  • 1
  • 2
  • 14
  • 1
    nice example - you might also find it useful to calculate the Xth percentile, so you can then easily identify the lower X% of values, this link might be interesting https://stackoverflow.com/questions/2374640/how-do-i-calculate-percentiles-with-python-numpy – ClimateUnboxed Jun 06 '19 at 08:17