Changeset 1892 in lmdz_wrf for trunk


Ignore:
Timestamp:
Apr 4, 2018, 10:14:04 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `genericNCvariable_Dict': Class to fake a netCDF varibale using dictionary values for
  • rlon', rlat' to 'CFvars'

Fixing various issues related to `CFmorzization'

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1887 r1892  
    1272312723     'vdimn': 'time', 'axis': 'T', 'length': -1}
    1272412724    """
    12725     fname = 'CFDIMvalues'
     12725    fname = 'CFcorValues'
    1272612726
    1272712727    # Dictionary for each dimension
     
    1274812748
    1274912749    # names to search for
    12750     lonn = ['lon', 'X', 'Longitude', 'longitude', 'x', 'west_east']
    12751     latn = ['lat', 'Y', 'Latitude', 'latitude', 'y', 'south_north']
     12750    lonn = ['lon', 'X', 'Longitude', 'longitude', 'x', 'west_east', 'rlon']
     12751    latn = ['lat', 'Y', 'Latitude', 'latitude', 'y', 'south_north', 'rlat']
    1275212752    pressn = ['pres', 'press', 'P', 'Press', 'Pressure', 'pressure', 'p']
    1275312753    heightn = ['height', 'Z', 'Height', 'z']
  • trunk/tools/nc_var_tools.py

    r1891 r1892  
    7575# gaddattr: Add a global attribute to a netCDF. Removes previous attribute if it exist
    7676# gaddattrk: Add a global attribute to a netCDF caring about the type. Removes previous attribute if it exist
     77# genericNCvariable_Dict: Class to fake a netCDF varibale using dictionary values for
     78#   dimensions
    7779# genericNCvariable: Class to fake a netCDF varibale
    7880# get_1str_nc: Function to get 1 string value in a netCDF variable as a chain of 1char values
     
    195197# Radius of the Earth (m)
    196198EarthR = 6378000.
     199
     200class genericNCvariable_Dict(object):
     201    """ Class to fake a netCDF varibale using dictionary values for dimensions
     202      dicdim: dictionary with name of the dimension as key and its length as valu
     203      dimlist: list of dimensions to give the shape of the variable
     204      name: name of the variable
     205      stdname: standard_name of the variable
     206      longname: longname of the variable
     207      units: units of the variable
     208      values: values for the variable
     209    >>> variable = genericNCvariable_Dict({'lon':25, 'lat':30, 'time': 2}, ['time', 'lat', 'lon'], 'tas', 'air_temperature', 'air temperature', 'K', np.ones((2,30,25),dtype=np.float))
     210    """
     211    def __init__(self, dicdim, dimlist, name, stdname, longname, units, values):
     212        from collections import OrderedDict
     213        totshape = np.prod(dicdim.values())
     214        dv = []
     215        for idd in dimlist: dv.append(dicdim[idd])
     216        self.values = values[:]
     217        self.dimensions = tuple(list(dicdim.keys()))
     218        self.shape = tuple(dv)
     219        self.name = name
     220        self.standard_name = stdname
     221        self.long_name = longname
     222        self.units = units
     223
     224    def __getitem__(self, val):
     225        return self.values[val]
     226#variable = genericNCvariable_Dict({'lon':25, 'lat':30, 'time': 2}, ['time', 'lat', 'lon'], 'tas', 'air_temperature', 'air temperature', 'K', np.ones((2,30,25), dtype=np.float))
     227#print variable
     228#print variable.dimensions
     229#print variable.shape
     230#print variable.units
     231#print variable[:].shape
    197232
    198233class genericNCvariable(object):
     
    2106321098      ncfile + ' _______'
    2106421099    rvarns = ronc.variables.keys()
    21065     print 'Lluis varns:', varns
    2106621100    for vn in varns:
    2106721101       rdimns = list(ronc.dimensions)
     
    2328723321
    2328823322    if not CFvars.has_key(cfvarn):
    23289         print errormsg
     23323        print infmsg
    2329023324        print '  ' + fname + ": file '" + inCFvarfile + "' does not have CF " +      \
    2329123325          "variable: '" + cfvarn + "' !!"
     
    2364523679            tvals, urefvals = compute_WRFtime(timewrfv, refdate=refT,                \
    2364623680              tunitsval='minutes')
    23647             oaxisv = tvals
     23681            #oaxisv = tvals
     23682            oaxisv = genericNCvariable_Dict({'time':len(tvals)}, ['time'], 'time',   \
     23683              'time', 'Time', urefvals, tvals[:])
    2364823684
    2364923685        # CF values of axis
     
    2366623702        if len(oaxisv.shape) == CFdimvalues['maxrank']:
    2366723703            axisv = oaxisv[:]
    23668             if CFdimvalues['length'] != -1: CFdimvalues['length'] = oaxisv.shape[0]
    23669             CFvardimvalues.append(dimn)
     23704            vardimdimn = []
     23705            vardimdimL = []
     23706            for dn in oaxisv.dimensions:
     23707                vardimdimn.append(dn)
     23708            if CFdimvalues['length'] != -1:
     23709                CFdimvalues['length'] = len(getdim_listonc(dimn, afNs, allonc))
     23710            CFvardimvalues = vardimdimn
    2367023711        elif len(oaxisv.shape) == CFdimvalues['maxrank'] + 1:
    2367123712            print warnmsg
     
    2392623967                    varv = np.where(varv == fill_value, gen.fillValueF, varv)
    2392723968            else:
    23928                 print 'Lluis: ', newVdims
    2392923969                newvar=onewnc.createVariable(cfvarn, 'f4', tuple(newVdims),          \
    2393023970                  fill_value=gen.fillValueF)
     
    2397224012                    # Re-setting time values to the center of the bounds
    2397324013                    ovtime[:] = tvals[:]
    23974                 elif vtb[0:10] == 'Ctime_bnds':
     24014                elif vtb[0:11] == 'CFtime_bnds':
    2397524015                    vtbvals = vtb.split('|')
    2397624016                    if len(vtbvals) != 4:
     
    2399224032                    print infmsg
    2399324033                    print '      ' + fname + ": creation of variable 'time_bnds' " + \
    23994                       "from variable '" + tvtbvals[1] + "' using period '" + period +\
     24034                      "from variable '" + vtbvals[1] + "' using period '" + period +\
    2399524035                      "' !!"
    23996                     odimvar = onc.variables[tvtbvals[1]]
     24036                    odimvar = onc.variables[vtbvals[1]]
    2399724037                    timev = odimvar[:]
    2399824038                    ttu = odimvar.getncattr('units')
     
    2400124041                      'Y-m-dTH:M:SZ', 'YmdHMS')
    2400224042                    tbvals, tvals, urefvals = compute_time_bnds(timev, ttu, period,  \
    24003                       kindWRFt='begperiod', refdate=refT, tunitsval='minutes')
     24043                      kindt='begperiod', refdate=refT, tunitsval='minutes')
    2400424044                    # Re-setting time values to the center of the bounds
    2400524045                    ovtime[:] = tvals[:]
     
    2427224312        tfmt = tkind.split(',')[1]
    2427324313        tvals = []
     24314        utime = 'minutes since 1949-12-01 00:00:00'
    2427424315        mattvals = np.zeros((itvals.shape[0],6), dtype=int)
    2427524316        for it in range(itvals.shape[0]):
    2427624317            mattvals[it,:] = dt.datetime.string(''.join(itvals[it,:]),tfmt)
    2427724318            tvals[it] = gen.datetimeStr_conversion(mattvals[it,:].strftime("%Y%m%d"+ \
    24278               "%H%M%S"), 'YmdHMS', 'cfTime,minutes since 1949-12-01 00:00:00')
     24319              "%H%M%S"), 'YmdHMS', 'cfTime,'+utime)
     24320       
    2427924321    else:
    2428024322        print errormsg
Note: See TracChangeset for help on using the changeset viewer.