Example #1
0
        filelist = [wrfrgx.match(filename) for filename in os.listdir(folder)]  # 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(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
 wrfrgx = re.compile(wrffiles) # compile regular expression
 filelist = [wrfrgx.match(filename) for filename in os.listdir(folder)] # 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(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 #3
0
    climdata[key] = sumtmp / (finyr-debyr+1)
#     climdata[key] = ma.masked_less(sumtmp, 0) / (finyr-debyr+1)
      
  ## 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
Example #4
0
            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
        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
Example #5
0
  filelist = [cesmrgx.match(filename) 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) 
  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)
  
Example #6
0
        'February ',
        'March    ',
        'April    ',
        'May      ',
        'June     ',  #
        'July     ',
        'August   ',
        'September',
        'October  ',
        'November ',
        'December '
    ]
    days = array([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
    # create time dimensions and coordinate variables
    for grp in [fngrp, higrp]:
        add_coord(grp, 'time', arange(1, ntime + 1), dtype='i4')
        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
Example #7
0
 ## convert values if necessary
 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
 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
Example #8
0
        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
Example #9
0
    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' 
    
#     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  
Example #10
0
     "February ",
     "March    ",
     "April    ",
     "May      ",
     "June     ",  #
     "July     ",
     "August   ",
     "September",
     "October  ",
     "November ",
     "December ",
 ]
 days = array([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
 # create time dimensions and coordinate variables
 for grp in [fngrp, higrp]:
     add_coord(grp, "time", arange(1, ntime + 1), dtype="i4")
     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)