Beispiel #1
0
 outdata.creator = 'Andre R. Erler' 
 
 # create ncatts dictionary from varatts
 ncatts = dict()
 for var,atts in varatts.iteritems():
   newatts = dict() # new atts dictionary
   # and iterate over atts dict contents
   for key1,val1 in atts.iteritems():
     if isinstance(val1,dict): # flatten nested dicts
       for key2,val2 in val1.iteritems():
         newatts[key2] = val2 
     else: newatts[key1] = val1
   ncatts[var] = newatts      
 
 # create time dimensions and coordinate variables
 add_coord(outdata, 'time', data=np.arange(1,len(days_per_month)+1), dtype='i4', atts=ncatts['time'])
 add_var(outdata, 'length_of_month', ['time'], data=days_per_month, 
         atts=dict(name='length_of_month',units='days',long_name='Length of Month'))
 # names of months (as char array)
 add_strvar(outdata, 'name_of_month', name_of_month, 'time', 
            atts=dict(name='name_of_month', units='', long_name='Name of the Month'))
 
 # create new lat/lon dimensions and coordinate variables
 add_coord(outdata, 'lat', data=lat, atts=ncatts['lon'])
 add_coord(outdata, 'lon', data=lon, atts=ncatts['lat'])
 # create climatology variables  
 fillValue = -9999; axes = ('time','lat','lon')
 for name,field in data.iteritems():
   add_var(outdata, name, axes, data=field.filled(fillValue), atts=ncatts[name], fillValue=fillValue)
 # create land/sea/no-data mask
 mask = ma.getmaskarray(data['precip'])[0,:,:]
Beispiel #2
0
def creatNetCDF(varname,
                varatts=None,
                ncatts=None,
                data_folder=daily_folder,
                fillValue=missing_value):
    ''' create a NetCDF-4 file for the given variable, create dimensions and variable, and allocate data;
        also set options for chunking and compression in a sensible manner '''
    if varatts is None: varatts = netcdf_varatts
    if ncatts is None: ncatts = netcdf_settings
    # create Dataset/file
    filename = netcdf_filename.format(varname.lower())
    filepath = data_folder + filename
    ds = nc.Dataset(filepath, mode='w', format='NETCDF4', clobber=True)
    # add coordinate variables
    # time is the outer-most (record) dimension
    axes = []  # build axes order (need to add in order
    atts = varatts['time']
    axes.append(atts['name'])
    add_coord(
        ds,
        atts['name'],
        data=None,
        length=None,
        atts=atts,
        dtype=np.dtype('i4'),
        zlib=True,
        fillValue=fillValue,
    )  # daily
    # also add a time-stamp variable
    atts = varatts['time_stamp']
    add_var(ds,
            atts['name'],
            dims=axes,
            data=None,
            shape=(None, ),
            atts=atts,
            dtype=np.dtype('S'),
            zlib=True,
            fillValue=None,
            lusestr=True)  # daily time-stamp
    # latitude (intermediate/regular dimension)
    atts = varatts['lat']
    axes.append(atts['name'])
    add_coord(
        ds,
        atts['name'],
        data=SnoDAS_grid.ylat[:],
        length=SnoDAS_grid.size[1],
        atts=atts,
        dtype=netcdf_dtype,
        zlib=True,
        fillValue=fillValue,
    )
    # longitude is the inner-most dimension (continuous)
    atts = varatts['lon']
    axes.append(atts['name'])
    add_coord(
        ds,
        atts['name'],
        data=SnoDAS_grid.xlon[:],
        length=SnoDAS_grid.size[0],
        atts=atts,
        dtype=netcdf_dtype,
        zlib=True,
        fillValue=fillValue,
    )
    # create NC variable
    atts = varatts[varname].copy()
    if 'scalefactor' in atts: del atts['scalefactor']
    add_var(ds,
            atts['name'],
            dims=axes,
            data=None,
            shape=(None, ) + snodas_shape2d,
            atts=atts,
            dtype=netcdf_dtype,
            zlib=True,
            fillValue=fillValue,
            lusestr=True,
            **ncatts)
    # return dataset object
    return ds
Beispiel #3
0
        ncatts = dict()
        for var, atts in varatts.iteritems():
            newatts = dict()  # new atts dictionary
            # and iterate over atts dict contents
            for key1, val1 in atts.iteritems():
                if isinstance(val1, dict):  # flatten nested dicts
                    for key2, val2 in val1.iteritems():
                        newatts[key2] = val2
                else:
                    newatts[key1] = val1
            ncatts[var] = newatts

        # create time dimensions and coordinate variables
        add_coord(outdata,
                  'time',
                  data=np.arange(1,
                                 len(days_per_month) + 1),
                  dtype='i4',
                  atts=ncatts['time'])
        add_var(outdata,
                'length_of_month', ['time'],
                data=days_per_month,
                atts=dict(name='length_of_month',
                          units='days',
                          long_name='Length of Month'))
        # names of months (as char array)
        add_strvar(outdata,
                   'name_of_month',
                   name_of_month,
                   'time',
                   atts=dict(name='name_of_month',
                             units='',
Beispiel #4
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  
  if os.path.exists(dstdir+climfile): 
    print(' removing old climatology file \'%s\''%(dstdir+climfile,))
    os.remove(dstdir+climfile)
  clim = Dataset(dstdir+climfile, 'w', format='NETCDF4')
  add_coord(clim, 'time', data=mons, length=len(mons), dtype='i4', atts=dict(units='month of the year')) # month of the year
  if dimlist is None:
    dimlist = [dim for dim in cesmout.dimensions if dim not in ignorelist]
  copy_dims(clim, cesmout, dimlist=dimlist, namemap=dimmap, copy_coords=True, dtype='f4')
  # 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'
  
Beispiel #5
0
    )  # 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
    if os.path.exists(dstdir + climfile):
        print(' removing old climatology file \'%s\'' % (dstdir + climfile, ))
        os.remove(dstdir + climfile)
    clim = Dataset(dstdir + climfile, 'w', format='NETCDF4')
    add_coord(clim,
              'time',
              data=mons,
              length=len(mons),
              dtype='i4',
              atts=dict(units='month of the year'))  # month of the year
    if dimlist is None:
        dimlist = [dim for dim in cesmout.dimensions if dim not in ignorelist]
    copy_dims(clim,
              cesmout,
              dimlist=dimlist,
              namemap=dimmap,
              copy_coords=True,
              dtype='f4')
    # 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):