I am using xarray in pyhton (Spyder) to read large NetCDF-files and process them.
import xarray as xr
ds = xr.open_dataset('my_file.nc')
ds has the following dimensions and variables:
<xarray.Dataset>
Dimensions: (time: 62215, points: 2195)
Coordinates:
* time (time) datetime64[ns] 1980-04-01 ... 2021-09-30T21:00:00
Dimensions without coordinates: points
Data variables:
longitude (time, points) float32 ...
latitude (time, points) float32 ...
hs (time, points) float32 ...
I want to calculate the 95th percentile of the variable hs for each specific point, and generate a new variable to the dataset:
hs_95 (points) float32
I do this with one line of code:
ds['hs_95'] = ds.hs.quantile(0.95, dim='time')
Where ds.hs is a xr.DataArray.
But it takes a very long time to run. Is there anything I can do to make it run faster? Is xarray the most convenient to use for this application?