Esempio n. 1
0
        ## setup files and folders
        wrffiles = wrfpfx % ndom + wrfdate + wrfext
        # N.B.: wrfpfx must contain something like %02i to accommodate the domain number
        # assemble input filelist
        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"
 ## setup files and folders
 wrffiles = wrfpfx%ndom + wrfdate + wrfext
 # N.B.: wrfpfx must contain something like %02i to accommodate the domain number  
 # assemble input filelist
 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
Esempio n. 3
0
      print('  %04i'%(cnt/12 +1901,))    
      cnt += ntime      
      sumtmp += indata[key].variables[value][cnt-ntime:cnt,:,:]
#       climdata[key] = ma.array( sumtmp / (finyr-debyr+1), mask=dataMask.repeat(ntime,axis=0))
#                       keep_mask=True, hard_mask=True)
    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_')
Esempio n. 4
0
                    ]  # 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
Esempio n. 5
0
  # assemble input filelist
  #print cesmfiles
  cesmrgx = re.compile(cesmfiles) # compile regular expression
  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'
Esempio n. 6
0
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:
Esempio n. 7
0
# 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
Esempio n. 8
0
                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)
Esempio n. 9
0
#     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' 
    
Esempio n. 10
0
            cnt += ntime
            tmp += fndynset[key].variables[value][cnt - ntime : cnt, :, :]
        #    if key =='rain': print(cnt/ntime)
        fndynclim[key] = tmp / (cnt / ntime)
    for (key, value) in hidynvar.iteritems():
        tmp = zeros((ntime, hishape[1], hishape[2]))
        cnt = 0
        while cnt <= himax:
            cnt += ntime
            tmp += hidynset[key].variables[value][cnt - ntime : cnt, :, :]
        hidynclim[key] = tmp / (cnt / ntime)

    ## initialize netcdf dataset structure
    print("\nWriting data to disk:")
    # create groups for different resolution
    fngrp = Dataset(CFSRroot + test + fnoutfile, "w", format="NETCDF4")  # outgrp.createGroup('fineres')
    higrp = Dataset(CFSRroot + test + hioutfile, "w", format="NETCDF4")  # outgrp.createGroup('highres')
    # new time dimensions
    months = [
        "January  ",
        "February ",
        "March    ",
        "April    ",
        "May      ",
        "June     ",  #
        "July     ",
        "August   ",
        "September",
        "October  ",
        "November ",
        "December ",