copy_dims(mean, wrfout, dimlist=dimlist, namemap=dimmap, copy_coords=False) # don't have coordinate variables # global attributes copy_ncatts(mean, wrfout, prefix="WRF_") # copy all attributes and save with prefix WRF mean.description = "WRF monthly means" mean.begin_date = begindate mean.end_date = enddate mean.experiment = exp mean.creator = "Andre R. Erler" # create climatology output file clim = Dataset(folder + climfile % ndom, "w", format="NETCDF4") add_coord(clim, "time", values=mons, dtype="i4", atts=dict(units="month of the year")) # month of the year copy_dims(clim, wrfout, dimlist=dimlist, namemap=dimmap, copy_coords=False) # don't have coordinate variables # variable with proper names of the months clim.createDimension("tstrlen", size=9) coord = clim.createVariable("month", "S1", ("time", "tstrlen")) for m in xrange(nmons): for n in xrange(9): coord[m, n] = months[m][n] # global attributes copy_ncatts(clim, wrfout, prefix="WRF_") # copy all attributes and save with prefix WRF clim.description = "climatology of WRF monthly means" clim.begin_date = begindate clim.end_date = enddate clim.experiment = exp clim.creator = "Andre R. Erler" # check variable list for var in varlist: if not wrfout.variables.has_key(varmap.get(var, var)): print("\nWARNING: variable %s not found in source file!\n" % (var,))
## convert values to mm/day days_per_month = np.array([31,28.25,31,30,31,30,31,31,30,31,30,31]) climdata['rain'] /= days_per_month.reshape((len(days_per_month),1,1)) # convert to mm/day ## initialize netcdf dataset structure print('\nWriting data to disk: %s'%(test+outfile,)) # create groups for different resolution outdata = Dataset(GPCCroot+test+outfile, 'w', format='NETCDF4') # outgrp.createGroup('fineres') # new time dimensions months = ['January ', 'February ', 'March ', 'April ', 'May ', 'June ', # 'July ', 'August ', 'September', 'October ', 'November ', 'December '] # create time dimensions and coordinate variables add_coord(outdata,'time',np.arange(1,ntime+1),dtype='i4') outdata.createDimension('tstrlen', 9) # name of month string outdata.createVariable('ndays','i4',('time',))[:] = days_per_month # names of months (as char array) coord = outdata.createVariable('month','S1',('time','tstrlen')) for m in xrange(ntime): for n in xrange(9): coord[m,n] = months[m][n] # global attributes outdata.description = 'Climatology of GPCC monthly precipitation, averaged from %04i to %04i'%(debyr,finyr) outdata.creator = 'Andre R. Erler' copy_ncatts(outdata,indata['rain'],prefix='GPCC_') # create old lat/lon dimensions and coordinate variables copy_dims(outdata, indata['rain'], dimlist=dimlist.keys(), namemap=dimlist, copy_coords=True) # create climatology variables dims = ('time','lat','lon'); fill_value = -9999 # precipitation atts = dict(long_name='Precipitation', units='mm/day') add_var(outdata, 'rain', dims, values=climdata['rain'].filled(fill_value), atts=atts, fill_value=fill_value)
# create climatology output file clim = Dataset(folder + climfile % ndom, 'w', format='NETCDF4') add_coord(clim, 'time', values=mons, dtype='i4', atts=dict(units='month of the year')) # month of the year copy_dims(clim, wrfout, dimlist=dimlist, namemap=dimmap, copy_coords=False) # don't have coordinate variables # variable with proper names of the months clim.createDimension('tstrlen', size=9) coord = clim.createVariable('month', 'S1', ('time', 'tstrlen')) for m in xrange(nmons): for n in xrange(9): coord[m, n] = months[m][n] # global attributes copy_ncatts( clim, wrfout, prefix='WRF_') # copy all attributes and save with prefix WRF clim.description = 'climatology of WRF monthly means' clim.begin_date = begindate clim.end_date = enddate clim.experiment = exp clim.creator = 'Andre R. Erler' # check variable list for var in varlist:
add_coord(mean, 'time', values=None, dtype='i4', atts=dict(units='month since '+begindate)) # unlimited time dimension copy_dims(mean, wrfout, dimlist=dimlist, namemap=dimmap, copy_coords=False) # don't have coordinate variables # global attributes copy_ncatts(mean, wrfout, prefix='WRF_') # copy all attributes and save with prefix WRF mean.description = 'WRF monthly means' mean.begin_date = begindate; mean.end_date = enddate mean.experiment = exp mean.creator = 'Andre R. Erler' # create climatology output file clim = Dataset(folder+climfile%ndom, 'w', format='NETCDF4') add_coord(clim, 'time', values=mons, dtype='i4', atts=dict(units='month of the year')) # month of the year copy_dims(clim, wrfout, dimlist=dimlist, namemap=dimmap, copy_coords=False) # don't have coordinate variables # variable with proper names of the months clim.createDimension('tstrlen', size=9) coord = clim.createVariable('month','S1',('time','tstrlen')) for m in xrange(nmons): for n in xrange(9): coord[m,n] = months[m][n] # global attributes copy_ncatts(clim, wrfout, prefix='WRF_') # copy all attributes and save with prefix WRF clim.description = 'climatology of WRF monthly means' clim.begin_date = begindate; clim.end_date = enddate clim.experiment = exp clim.creator = 'Andre R. Erler' # check variable list for var in varlist: if not wrfout.variables.has_key(varmap.get(var,var)): print('\nWARNING: variable %s not found in source file!\n'%(var,)) del var # remove variable if not present in soruce file # copy variables to new datasets
climdata['rain'] /= days_per_month.reshape((len(days_per_month),1,1)) # convert to mm/day climdata['Tmin'] += 273.15 # convert to Kelvin climdata['T2'] += 273.15 # convert to Kelvin climdata['Tmax'] += 273.15 # convert to Kelvin ## initialize netcdf dataset structure print('\nWriting data to disk:') # create groups for different resolution outdata = Dataset(CRUroot+test+outfile, 'w', format='NETCDF4') # outgrp.createGroup('fineres') # new time dimensions months = ['January ', 'February ', 'March ', 'April ', 'May ', 'June ', # 'July ', 'August ', 'September', 'October ', 'November ', 'December '] # create time dimensions and coordinate variables add_coord(outdata,'time',np.arange(1,ntime+1),dtype='i4') outdata.createDimension('tstrlen', 9) # name of month string outdata.createVariable('ndays','i4',('time',))[:] = days_per_month # names of months (as char array) coord = outdata.createVariable('month','S1',('time','tstrlen')) for m in xrange(ntime): for n in xrange(9): coord[m,n] = months[m][n] # global attributes outdata.description = 'Climatology of CRU monthly climate data, averaged from %04i to %04i'%(debyr,finyr) outdata.creator = 'Andre R. Erler' copy_ncatts(outdata,indata['rain'],prefix='CRU_') # create old lat/lon dimensions and coordinate variables copy_dims(outdata, indata['rain'], dimlist=dimlist.keys(), namemap=dimlist, copy_coords=True) # create climatology variables dims = ('time','lat','lon'); fill_value = -9999 # precipitation atts = dict(long_name='Precipitation', units='mm/day') add_var(outdata, 'rain', dims, values=climdata['rain'].filled(fill_value), atts=atts, fill_value=fill_value)