I would like to make a monthly sums of my NetCDF4 file from daily values for precipitation. However, I am quite not sure what I am doing wrong. It seems that something has changed from the code in this post: Sum a daily time series into a monthly time series with a NaN value threshold
I didn’t find anything helpful in the library documentation.
Here is my code:
JavaScript
x
27
27
1
import netCDF4
2
from netCDF4 import Dataset
3
import numpy as np
4
import xarray as xr
5
import pandas as pd
6
7
data = xr.open_dataset('C3S_concat_cropped.nc')
8
# or I can use
9
data2 = Dataset("C3S_concat_cropped.nc", "r", format="NETCDF4")
10
print(data)
11
Out:
12
<xarray.Dataset>
13
Dimensions: (lat: 115, lon: 140, time: 15157)
14
Coordinates:
15
* lat (lat) float64 -7.4 -7.5 -7.6 -7.7 -18.6 -18.7 -18.8
16
* lon (lon) float64 21.1 21.2 21.3 21.4 34.8 34.9 35.0
17
* time (time) datetime64[ns] 1979-01-01 2020-06-30
18
Data variables:
19
Precipitation_Flux (time, lat, lon) float32
20
21
daily_dataset = xr.Dataset({'Precipitation_Flux': (['time', 'lat', 'lon'],
22
data['Precipitation_Flux'][:, :, :])}, coords={'lat': (data['lat'][:]),
23
'lon': (data['lon'][:]), 'time': pd.date_range('1979-01-01', periods=15157)})
24
25
monthly_dataset = daily_dataset['Precipitation_Flux'].resample(indexer='M', time="1D",
26
skipna=False).sum()
27
My ValueError:
JavaScript
1
2
1
ValueError: the first argument to .resample must be a dictionary
2
Advertisement
Answer
I founded that this command works for me!
JavaScript
1
3
1
monthly_dataset = daily_dataset['Precipitation_Flux'].resample(time ='M',
2
skipna=False).sum()
3
However, the documentation of xarray.Dataset.resample can be quite confusing, as the first argument of the function – indexer is not typically written! So be aware of that! :-)