Example #1
0
        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(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"))
Example #2
0
  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) 
  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
Example #3
0
  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)
  add_var(outdata, 'landmask', ('lat','lon'), values=tmp.filled(0))
  
#   ## dataset feedback and diagnostics
#   # print dataset meta data
Example #4
0
        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,
Example #5
0
 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
Example #6
0
 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)  
 # 2m minimum Temperature
 atts = dict(long_name='Minimum 2m Temperature', units='K')
 add_var(outdata, 'Tmin', dims, values=climdata['Tmin'].filled(fill_value), atts=atts, fill_value=fill_value)  
 # 2m water vapor
Example #7
0
        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)
    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
Example #8
0
            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)
        fngrp.variables[key][:, :, :] = fndynclim[key]
    for (key, value) in hidynset.iteritems():
        copy_vars(higrp, value, [key], namemap=hidynvar, copy_data=False)
        higrp.variables[key][:, :, :] = hidynclim[key]

    ## dataset feedback and diagnostics
    # dataset and groups
    #  print outgrp