Changeset 672 in lmdz_wrf for trunk/tools/nc_var_tools.py


Ignore:
Timestamp:
Jan 15, 2016, 4:02:25 PM (9 years ago)
Author:
lfita
Message:

Fixing some errors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r671 r672  
    1717217172    fname = 'varDimension'
    1717317173
    17174     if not oncf.dimensions._has_key(dname):
     17174    if not oncf.dimensions.has_key(dname):
    1717517175        print errormsg
    1717617176        print '  ' + fname + ": NetCDF object does not have dimension '" + dname + "' !!"
     
    1718817188    else:
    1718917189        print warnmsg
    17190         print '  ' + fname ": Any  1D variables with dimension '" + dname + "' !!"
     17190        print '  ' + fname + ": Any  1D variables with dimension '" + dname + "' !!"
    1719117191
    1719217192# Dpouble precision
     
    1719617196    if len(d1var) > 1:
    1719717197        d1vardouble = []
    17198         for vn in d1var
     17198        for vn in d1var:
    1719917199            ovn = oncf.variables[vn]
    17200             if ovn.dtype == np.float64(1.): d1vardouble.append(vn)
    17201         if len(d1vardouble) != 1:
     17200            if ovn.dtype == type(np.float64(1.)): d1vardouble.append(vn)
     17201        if len(d1vardouble) > 1:
    1720217202            print errormsg
    17203             print '  ' + fname + ": More than 1D double variable with dimension '" + dname + "' !!"
     17203            print '  ' + fname + ": Found more than 1D double variable with " +      \
     17204              "dimension '" + dname + "' !!"
     17205            print '    found:', d1vardouble
    1720417206            quit(-1)
     17207        elif len(d1vardouble) == 0:
     17208            print warnmsg
     17209            print '  ' + fname + ": Found any 1D double variable with dimension '" + \
     17210              dname + "' !!"
     17211            dimvarname = None
    1720517212        else:
    1720617213            dimvarname = d1vardouble[0]
     
    1721017217    return dimvarname
    1721117218
    17212 def ovar_onc(ovar, oncf):
     17219def ovar_onc(incf, ovar, oncf):
    1721317220    """ Function to copy an object variable to a nother netcdf object
     17221      incf: input netCDF object
    1721417222      ovar= variable object
    1721517223      oncf= netCDF object
     
    1721717225    fname = 'ovar_onc'
    1721817226
    17219     varname = ovar.name
    17220     varadims = ovar.dimensions
     17227    varname = ovar._name
     17228
     17229    if oncf.variables.has_key(varname):
     17230        print warnmsg
     17231        print '  ' + fname + ": netCDF object already has variable '" + ovar.name +  \
     17232          "' !!"
     17233        print '    doing noting'
     17234        return
     17235
     17236    vardims = ovar.dimensions
    1722117237    vartype = ovar.dtype
     17238
     17239    for dn in vardims:
     17240        if not searchInlist(oncf.dimensions, dn):
     17241            if incf.dimensions[dn].isunlimited():
     17242                newdim = oncf.createDimension(dn, None)
     17243            else:
     17244                newdim = oncf.createDimension(dn, len(incf.dimensions[dn]))
    1722217245 
    1722317246    newvar = oncf.createVariable(varname, vartype, vardims)
    17224     newvar = ovar[:]
    17225 
    17226     for ncatr in ovar.ncattrs():
    17227         ncatrv = ovar.get_ncattribute(ncattr)
    17228         set_ncatrribute(newvar, ncatr, ncatrv)
     17247    newvar[:] = ovar[:]
     17248
     17249    for attrn in ovar.ncattrs():
     17250        attrv = ovar.getncattr(attrn)
     17251        set_attribute(newvar, attrn, attrv)
    1722917252
    1723017253    oncf.sync()
     
    1723417257def SpatialWeightedMean(values, filen, varn):
    1723517258    """ Function to compute the spatial mean using weights from a netCDF file
    17236       values= [weightskind],[xdimname],[ydimname]
     17259      values= [weightskind],[xdimname],[ydimname],[addvars]
    1723717260        [weightskind]: type of weights:
    1723817261          * 'varnames',[varname],[oper]: using a variable [varname] with an operation [oper] as area size
     
    1724517268        [xdimname]: name of the dimension for the x-axis
    1724617269        [ydimname]: name of the dimension for the y-axis
     17270        [addvars]: ':', separetd list of name of variables to add to the output file
    1724717271      filen= name of the netCDF file
    1724817272      varn= name of the variable
     
    1726017284    xdimname = values.split(',')[3]
    1726117285    ydimname = values.split(',')[4]
     17286    addvars = values.split(',')[5]
    1726217287
    1726317288    ofile = 'SpatialWeightedMean_' + weightk + '.nc'
    1726417289
    1726517290    if weightk == 'varnames':
    17266         arguments = '[weightk],[varname],[oper],[xdimname],[ydimname]'
     17291        arguments = '[weightk],[varname],[oper],[xdimname],[ydimname],[addvars]'
    1726717292        check_arguments(fname, len(values.split(',')), arguments, ',',               \
    1726817293          len(arguments.split(',')))
     
    1749817523        vardim = varDimension(onc, dim)
    1749917524        if vardim is not None:
    17500             ovar_nc(onc.variables[vardim], onewnc)
     17525            ovar_onc(onc, onc.variables[vardim], onewnc)
    1750117526           
    17502 
    1750317527# Output variable
    1750417528##
     
    1750817532      longvarname, iovar.getncattr('units'))
    1750917533    newvar[:] = newvals
     17534
     17535    onewnc.sync()
    1751017536
    1751117537# Spatial weight
     
    1751717543    basicvardef(newvar, 'spatialweight', 'space weight ' + longvarname, '-')
    1751817544    newvar[:] = outweightvals
     17545
     17546    onewnc.sync()
     17547
     17548# Additional variables
     17549##
     17550    for vn in addvars.split(':'):
     17551        if not onc.variables.has_key(vn):
     17552            print errormsg
     17553            print '  ' + fname + ": file '" + filen + "' does not have variable '" + \
     17554              vn + "' !!"
     17555            quit(-1)
     17556        ovar_onc(onc, onc.variables[vn], onewnc)
     17557    onewnc.sync()
    1751917558
    1752017559# Global attributes
     
    1753117570
    1753217571    onc.close()
     17572    onewnc.sync()
     17573    onewnc.close()
     17574
    1753317575
    1753417576    print fname + ": Successfull written of file: '" + ofile + "' !!"
Note: See TracChangeset for help on using the changeset viewer.