Changeset 1863 in lmdz_wrf for trunk


Ignore:
Timestamp:
Mar 26, 2018, 4:46:27 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `CFvars': Function to adapt a given variable and file following CF-standards
Location:
trunk/tools
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1862 r1863  
    3131# CDO_toCF: Function to pass a CDO output file to CF-conventions
    3232# CFmorzization: Function to provide a CF-compilation version of a variable within a file
     33# CFvars: Function to adapt a given variable and file following CF-standards
    3334# changevartype: Function to change the type of a variable (when possible)
    3435# chdimname: Changing the name of the dimension
     
    2309023091    return newdim, newvar, newvarv, nonCFattr
    2309123092
     23093def CFvars(cfvarn, vardims, varvalues, outnc):
     23094    """ Function to adapt a given variable and file following CF-standards
     23095      NOTE: reading information from 'CFvariables.dat'
     23096        cfvarn= provided CF name of the variabale
     23097        vardims= dimensions of the variable
     23098        varvalues= values of the variable
     23099        outnc= netCDF object for the output
     23100    """
     23101    fname = 'CFvars'
     23102    inCFvarfile = 'CFvariables.dat'
     23103    availactions = ['new_z_dim']
     23104
     23105    oCFf = open(inCFvarfile, 'r')
     23106    CFvars = {}
     23107    for line in oCFf:
     23108        if line[0:1] != '#' and len(line) > 1:
     23109            linevals = line.replace('\n','').replace('\t','').split(' ')
     23110            CFvars[linevals[0]] = linevals[1:len(linevals)]
     23111
     23112    if not CFvars.has_key(cfvarn):
     23113        print errormsg
     23114        print '  ' + fname + ": file '" + inCFvarfile + "' does not have CF " +      \
     23115          "variable: '" + cfvarn + "' !!"
     23116        print '    available ones:', CFvars.keys()
     23117        print '    Nothing will be changed'
     23118        return None, None
     23119    else:
     23120        # Processing variable and take actions
     23121        print infmsg
     23122        print '    ' + fname + ": processing variale '" + cfvarn + "' with values:", \
     23123          CFvars[cfvarn]
     23124        CFactions = CFvars[cfvarn]
     23125        action = CFactions[0]
     23126
     23127        # Adding z-dimension
     23128        if action == 'new_z_dim':
     23129            newdim = CFactions[1]
     23130            newvdim = CFactions[2]
     23131            newvvar = np.float(CFactions[3])
     23132            outnc.createDimension(newdim, 1)
     23133            dimvals = gen.CFcorValues(newdim)
     23134            if not gen.searchInlist(outnc.dimensions, newdim):
     23135                newdim = outnc.createDimension(newdim, 1)
     23136            if not outnc.variables.has_key(newvdim):
     23137                newvar = outnc.createVariable(newvdim, 'f8', (newdim))
     23138                basicvardef(newvar, dimvals['stdn'], dimvals['longname'],            \
     23139                  dimvals['units'])
     23140                newvar[:] = newvvar
     23141                for ivn in dimvals.keys():
     23142                    if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \
     23143                      ivn != 'units' and ivn != 'maxrank' and ivn != 'length':
     23144                        set_attribute(newvar,ivn,dimvals[ivn])
     23145            newvdn = []
     23146            newvshape = []
     23147            idim = 0
     23148            for vdn in vardims:
     23149                if vdn == 'time':
     23150                    newvdn.append(vdn)
     23151                    newvshape.append(varvalues.shape[idim])
     23152                    newvdn.append(newdim)
     23153                    newvshape.append(1)
     23154                else:
     23155                    newvdn.append(vdn)
     23156                    newvshape.append(varvalues.shape[idim])
     23157                idim = idim + 1
     23158            newvarvalues = np.zeros(tuple(newvshape), dtype=np.float)
     23159            newvarvalues[:,0,:,:] = varvalues
     23160        else:
     23161            print errormsg
     23162            print '  ' + fname + ": action '" + action + "' not ready !!"
     23163            print '    available onees:', availactions
     23164            quit(-1)
     23165
     23166    return newvdn, newvarvalues
     23167
    2309223168def CFmorzization(values, ncfile, variable):
    2309323169    """ Function to provide a CF-compilation version of a variable within a file
     
    2356223638            print '    ' + fname + ": creation of variable '" + cfvarn + "(" +       \
    2356323639              ', '.join(CFvardims) + ")' ..."
    23564             newvar=onewnc.createVariable(cfvarn, 'f4', tuple(CFvardims),             \
    23565               fill_value=gen.fillValueF)
    23566             basicvardef(newvar, stdvarn, longvarn, utsvarn)
    23567 
    2356823640            # Providing the right coordinates
    2356923641            coorv = []
     
    2357323645                elif cdn == 'time': print ' '
    2357423646                else: coorv.append(cdn)
     23647            newVdims, newVvalues = CFvars(cfvarn, CFvardims, ovar[:], onewnc)
     23648            if newVdims is None:
     23649                newvar=onewnc.createVariable(cfvarn, 'f4', tuple(CFvardims),         \
     23650                  fill_value=gen.fillValueF)
     23651                basicvardef(newvar, stdvarn, longvarn, utsvarn)
     23652                # Setting values, but taking into account pre-existing masked values
     23653                varv = ovar[:]
     23654                if type(varv) == type(gen.mamat):
     23655                    imaskv = varv.fill_value
     23656                    varv = np.where(varv == fill_value, gen.fillValueF, varv)
     23657            else:
     23658                newvar=onewnc.createVariable(cfvarn, 'f4', tuple(newVdims),          \
     23659                  fill_value=gen.fillValueF)
     23660                basicvardef(newvar, stdvarn, longvarn, utsvarn)
     23661                # Setting values, but taking into account pre-existing masked values
     23662                if type(newVvalues) == type(gen.mamat):
     23663                    imaskv = varv.fill_value
     23664                    varv = np.where(varv == fill_value, gen.fillValueF, newVvalues)
     23665                else:
     23666                    varv = newVvalues
     23667
     23668            newvar[:] = varv[:]
    2357523669            set_attribute(newvar, 'coordinates', ' '.join(coorv))
    2357623670
    23577             # Setting values, but taking into account pre-existing masked values
    23578             varv = ovar[:]
    23579             if type(varv) == type(gen.mamat):
    23580                 imaskv = varv.fill_value
    23581                 varv = np.where(varv == fill_value, gen.fillValueF, varv)
    23582  
    23583             newvar[:] = varv[:]
    2358423671            set_attribute(newvar, 'grid_mapping', gattr['grid'])
    2358523672            onewnc.sync()
Note: See TracChangeset for help on using the changeset viewer.