I have many daily NetCDF resulted from a hydrological model and I want to convert them to monthly/yearly level both by summing or averaging them. For this, I use the following code:
import xarray as xr
nc_file = r'J:\RESULTS\WB_PRECIPITATION.nc'
ds = xr.open_dataset(nc_file)
monthly_data=ds.resample(time='Y',skipna=True).sum()
output = r'J:\RESULTS\WB_PRECIPITATION_YEARLY.nc'
monthly_data.to_netcdf(output, engine="netcdf4")
The problem is that my original daily file has several zones with nan (_FillValue=-9999) and that when they pass to the new NetCDF they pass to have the value 0. In this case, that is distorting all the calculations.
I already check "skipna" parameter with True and False values and I got the same result.
In pandas, when I have had the same problem I have used the following code, however, I have not been able to adapt it for this situation.
import numpy as np
import pandas as pd
def very_sum(array_like):
if any(pd.isnull(array_like)):
return np.nan
else:
return array_like.sum()
df = ...
df_yearly = df.resample('Y').apply(very_sum)
How can I resample my data without losing the zones with nan. ?