] # list folder and match filelist = [match.group() for match in filelist if match is not None] # assemble valid file list if len(filelist) == 0: 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( prdrgx ) # 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
hidynfile = dict(pmsl=pmslfile) hidynvar = dict(pmsl='PRMSL_L101') # dimensions #tdim = dict(time='time',tstrlen='tstrlen') fndim = dict(lat='lat', lon='lon') hidim = fndim ## start execution if __name__ == '__main__': ## open input datasets fnstatset = dict() fndynset = dict() hidynset = dict() for (key, value) in fnstatfile.iteritems(): fnstatset[key] = Dataset(CFSRdata + value, 'r') for (key, value) in fndynfile.iteritems(): fndynset[key] = Dataset(CFSRdata + value, 'r') for (key, value) in hidynfile.iteritems(): hidynset[key] = Dataset(CFSRdata + value, 'r') fnshape = fndynset['rain'].variables[ fndynvar['rain']].shape # (time, lat, lon) hishape = hidynset['pmsl'].variables[ hidynvar['pmsl']].shape # (time, lat, lon) ## perform actual computation of climatologies ntime = 12 if finyr: fnmax = 12 * (finyr - 1) + finmon himax = fnmax else:
for filename in os.listdir(srcdir)] # list folder and match filelist = [match.group() for match in filelist if match is not None] # assemble valid file list if len(filelist) == 0: print('\nWARNING: no matching files found for %s ' % (cesmname, )) import sys sys.exit(1) # exit if there is no match filelist.sort( ) # sort alphabetically, so that files are in sequence (temporally) datergx = re.compile( prdrgx) # 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 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)
# files (one per variable) varlist = dict(rain='pre', T2='tmp', Tmin='tmn', Tmax='tmx', Q2='vap', PET='pet') # for now... there are more variables to come! filelist = dict() # construct file list from variable list for (key,value) in varlist.iteritems(): filelist[key] = 'cru_ts3.20.1901.2011.%s.dat.nc'%(value,) # dimensions dimlist = dict(lat='lat',lon='lon') # these dimensions will be copied - 'time' is different! ## start execution if __name__ == '__main__': ## open input datasets indata = dict() # loop over all source files and open them for (key,value) in filelist.iteritems(): indata[key] = Dataset(CRUdata+value, 'r') indata[key].variables[varlist[key]].set_auto_maskandscale(False) # get some meta data and create land mask datashape = indata['rain'].variables[varlist['rain']].shape # (time, lat, lon) missing_value = indata['rain'].variables[varlist['rain']].getncattr('_FillValue') dataMask = ( indata['rain'].variables[varlist['rain']][1,:,:] == missing_value ) # # print some meta data # print indata['rain'].variables[varlist['rain']] # print indata['rain'].dimensions[dimlist['lat']] # print indata.variables.keys() # print indata.dimensions.keys() ## perform actual computation of climatologies ntime = 12 # 12 month per year... debmon = max((debyr-1901)*ntime,0) # time begins in 1901 finmon = min((finyr-1901)*ntime,datashape[0]) # time ends in 2010
# pyl.colorbar() # pyl.show(block=True) ## create NetCDF file # import netCDF4-python and added functionality from netcdf import Dataset, add_coord, add_var # settings outfile = 'prism_clim.nc' # prefix = 'test_' # development prefix prefix = 'prismavg/' # production prefix # initialize netcdf dataset structure 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'