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) # station density atts = dict(long_name='Station Density', units='#') add_var(outdata, 'stns', dims, values=climdata['stns'].filled(fill_value), atts=atts, fill_value=fill_value) # land mask atts = dict(long_name='Land Mask', units='') tmp = ma.masked_array(ma.ones((datashape[1],datashape[2])), mask=dataMask)
filelist.sort() # sort alphabetically, so that files are in sequence (temporally) datergx = re.compile(wrfdate) # compile regular expression, also used to infer month (later) begindate = datergx.search(filelist[0]).group() enddate = datergx.search(filelist[-1]).group() # load first file to copy some meta data wrfout = Dataset(folder + filelist[0], "r", format="NETCDF4") # create monthly mean output file mean = Dataset(folder + meanfile % ndom, "w", format="NETCDF4") 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]
enddate = datergx.search(filelist[-1]).group() # load first file to copy some meta data cesmout = Dataset(srcdir+filelist[0], 'r', format='NETCDF4') # create climatology output file clim = Dataset(dstdir+climfile, '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, cesmout, dimlist=dimlist, namemap=dimmap, copy_coords=True, dtype='f4') # 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, cesmout, prefix='CESM_') # copy all attributes and save with prefix WRF clim.description = 'climatology of CESM monthly means' clim.begin_date = begindate; clim.end_date = enddate clim.experiment = cesmname clim.creator = 'Andre R. Erler' # copy constant variables (no time dimension) copy_vars(clim, cesmout, varlist=statlist, namemap=varmap, dimmap=dimmap, remove_dims=['time'], copy_data=True) # check variable list for var in varlist: if not cesmout.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 copy_vars(clim, cesmout, varlist=varlist, namemap=varmap, dimmap=dimmap, copy_data=False) # length of time, x, and y dimensions nvar = len(varlist)
print('\nWARNING: no matching files found for domain %02i'%(ndom,)) break # skip and go to next domain filelist.sort() # sort alphabetically, so that files are in sequence (temporally) datergx = re.compile(wrfdate) # compile regular expression, also used to infer month (later) begindate = datergx.search(filelist[0]).group() enddate = datergx.search(filelist[-1]).group() # load first file to copy some meta data wrfout = Dataset(folder+filelist[0], 'r', format='NETCDF4') # create monthly mean output file mean = Dataset(folder+meanfile%ndom, 'w', format='NETCDF4') 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
grp.createDimension('tstrlen', 9) # name of month string grp.createVariable('ndays', 'i4', ('time', ))[:] = days # names of months (as char array) coord = grp.createVariable('month', 'S1', ('time', 'tstrlen')) for m in xrange(ntime): for n in xrange(9): coord[m, n] = months[m][n] # global attributes if finmon: description = \ 'Climatology of CFSR monthly means, averaged from January 1979 to %s %04i'%(months[finmon],1979+finyr-1) else: description = 'Climatology of CFSR monthly means, averaged from 1979 to %04i' % ( 1979 + finyr - 1) creator = 'Andre R. Erler' # fine grid fngrp.description = description fngrp.creator = creator copy_ncatts(fngrp, fndynset['rain'], prefix='CFSR_') # for att in fndynset['prt'].ncattrs(): fngrp.setncattr('SRC_'+att,fndynset['prt'].getncattr(att)) higrp.description = description higrp.creator = creator copy_ncatts(fngrp, hidynset['pmsl'], prefix='CFSR_') # create old lat/lon dimensions and coordinate variables copy_dims(fngrp, fndynset['rain'], dimlist=fndim.keys(), namemap=fndim, copy_coords=True) copy_dims(higrp, hidynset['pmsl'], dimlist=hidim.keys(),
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,
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) # 2m mean Temperature atts = dict(long_name='Temperature at 2m', units='K') add_var(outdata, 'T2', dims, values=climdata['T2'].filled(fill_value), atts=atts, fill_value=fill_value) # 2m maximum Temperature atts = dict(long_name='Maximum 2m Temperature', units='K') add_var(outdata, 'Tmax', dims, values=climdata['Tmax'].filled(fill_value), atts=atts, fill_value=fill_value)
copy_dims(clim, cesmout, dimlist=dimlist, namemap=dimmap, copy_coords=True, dtype='f4') # 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, cesmout, prefix='CESM_') # copy all attributes and save with prefix WRF clim.description = 'climatology of CESM monthly means' clim.begin_date = begindate clim.end_date = enddate clim.experiment = cesmname clim.creator = 'Andre R. Erler' # copy constant variables (no time dimension) copy_vars(clim, cesmout, varlist=statlist, namemap=varmap, dimmap=dimmap, remove_dims=['time'], copy_data=True) # check variable list for var in varlist:
print('\nWriting data to disk: %s'%(prefix+outfile,)) # create groups for different resolution outdata = Dataset(PRISMroot+prefix+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',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 monthly PRISM data' outdata.creator = 'Andre R. Erler' # copy_ncatts(outdata,indata['rain'],prefix='GPCC_') # create new lat/lon dimensions and coordinate variables add_coord(outdata, 'lat', values=lat, atts=None) add_coord(outdata, 'lon', values=lon, atts=None) # create climatology variables fill_value = -9999 atts = dict(long_name='Precipitation', units='mm/day') add_var(outdata, 'rain', ('time','lat','lon'), values=ppt.filled(fill_value), atts=atts, fill_value=fill_value) atts = dict(long_name='Minimum Temperature', units='deg. C') add_var(outdata, 'Tmin', ('time','lat','lon'), values=Tmin.filled(fill_value), atts=atts, fill_value=fill_value) atts = dict(long_name='Average Temperature', units='deg. C') add_var(outdata, 'T2', ('time','lat','lon'), values=Tavg.filled(fill_value), atts=atts, fill_value=fill_value) atts = dict(long_name='Maximum Temperature', units='deg. C')
'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',
# names of months (as char array) coord = grp.createVariable("month", "S1", ("time", "tstrlen")) for m in xrange(ntime): for n in xrange(9): coord[m, n] = months[m][n] # global attributes if finmon: description = "Climatology of CFSR monthly means, averaged from January 1979 to %s %04i" % ( months[finmon], 1979 + finyr - 1, ) else: description = "Climatology of CFSR monthly means, averaged from 1979 to %04i" % (1979 + finyr - 1) creator = "Andre R. Erler" # fine grid fngrp.description = description fngrp.creator = creator copy_ncatts(fngrp, fndynset["rain"], prefix="CFSR_") # for att in fndynset['prt'].ncattrs(): fngrp.setncattr('SRC_'+att,fndynset['prt'].getncattr(att)) higrp.description = description higrp.creator = creator copy_ncatts(fngrp, hidynset["pmsl"], prefix="CFSR_") # create old lat/lon dimensions and coordinate variables copy_dims(fngrp, fndynset["rain"], dimlist=fndim.keys(), namemap=fndim, copy_coords=True) copy_dims(higrp, hidynset["pmsl"], dimlist=hidim.keys(), namemap=hidim, copy_coords=True) # copy static variables into new dataset for (key, value) in fnstatset.iteritems(): copy_vars(fngrp, value, [key], namemap=fnstatvar, remove_dims=["time"], incl_=False) # '_' causes problems # create dynamic/time-dependent variables for (key, value) in fndynset.iteritems(): copy_vars(fngrp, value, [key], namemap=fndynvar, copy_data=False)