How to convert daily to monthly netcdf files

2019-08-03 15:53发布

问题:

I have downloaded climate model output in the form of netcdf files with one variable (pr) for the whole world with a daily time-step. My final goal is to have monthly data for Europe.

I have never used netcdf files before and all the specific software for netcdf I could find doesn't seems to work in windows. Since I programme in R, I tried using the ncdf4 package but run into memory size problems (my files are around 2Gb)... I am now trying the netCDF4 module in python (first time I am using python - so go easy on me).

I have managed to install everything and found some code online to import the dataset:

nc_fid = Dataset(nc_f, 'r')  
# Extract data from NetCDF file
lats = nc_fid.variables['lat'][:]
lons = nc_fid.variables['lon'][:]
time = nc_fid.variables['time'][:]
pp  = nc_fid.variables['pr'][:]  

However all the tutorials I found are on how to make a netcdf file... I have no idea how to aggregate this daily rainfall (variable pr) into monthly. Also, I have different types of calender in different files, but I don't even know how to access that information:

time.calendar
AttributeError: 'numpy.ndarray' object has no attribute 'calendar'

Please help, I don't want to have to learn Linux just so I can sort-out some data :(

回答1:

Why not avoid programming entirely and use NCO which supplies the ncrcat command that aggregates data thusly:

ncrcat day*.nc month.nc

Voilà. See more ncrcat examples here.

Added 20160628: If instead of a month-long timeseries you want a monthly average then use the same command only with ncra instead of ncrcat. The manual explains things like this.



回答2:

If you have a daily timestep and you want to calculate the monthly mean then you can do

cdo monmean input_yyyy.nc output_yyyy.nc

It sounds as if you have several of these files, so you will need to merge them with

cdo mergetime file_*.nc timeseries.nc 

where the * is a wildcard for the years.