Changeset 1009 in lmdz_wrf


Ignore:
Timestamp:
Aug 11, 2016, 6:19:51 PM (8 years ago)
Author:
lfita
Message:

Fixing `WRF_toCF', right sort of change dim-names and CF-variables
Fixing `cleaning_varsfile': right variables to keep and only warning when variable is not in original file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1008 r1009  
    66# Importing generic tools file 'generic_tools.py'
    77import generic_tools as gen
     8import subprocess as sub
    89
    910####### Contents:
     
    1646916470    refDateTime = values.split(':')[2]
    1647016471    Tunits = values.split(':')[3]
    16471 
    16472     # CFing time
    16473     WRF_CFtime_creation(refDateTime+','+Tunits, ncfile, 'time')
    16474    
     16472 
    1647516473    # CFing lon,lat
    1647616474    WRF_CFlonlat_creation('lon,lat', ncfile, wrfvl+','+wrfvL)
     
    1648416482    for dimn in CFdims.keys():
    1648516483        if gen.searchInlist(ncdims,dimn):
     16484            print fname + ' Lluis dimn:', dimn, 'CFdims:', CFdims[dimn]
    1648616485            # Making sure CF dimension is not in the file
    1648716486            if not gen.searchInlist(ncdims,CFdims[dimn]):
    1648816487                newname = ncf.renameDimension(dimn,CFdims[dimn])
     16488
     16489    ncf.sync()
     16490    ncf.close()
     16491
     16492    # CFing time
     16493    WRF_CFtime_creation(refDateTime+','+Tunits, ncfile, 'time')
     16494
     16495    ncf = NetCDFFile(ncfile,'a')
    1648916496
    1649016497    # dimension variables which do not need to attach CF attributes, otherwise values
     
    1654116548
    1654216549    oldvars = onc.variables.keys()
    16543     for oldv in oldvars:
     16550
     16551    notinfile = list(set(keepvars).difference(set(oldvars)))
     16552    if len(notinfile) > 0:
     16553        print warnmsg
     16554        print '  ' + fname + ': variables to keep:', notinfile, "are not in file '"+ \
     16555          filen + "' !!"
     16556        print '    variables to keep:', keepvars
     16557        print '    variables in file:', oldvars
     16558
     16559    for oldv in keepvars:
    1654416560        newdims = newonc.dimensions
    1654516561        newvars = newonc.variables.keys()
    1654616562
    16547         if not gen.searchInlist(newvars, oldv):
     16563        if not gen.searchInlist(newvars, oldv) and gen.searchInlist(oldvars, oldv):
     16564            print '  ' + fname + ": keeping variable '" + oldv + "' ..."
    1654816565            oldov = onc.variables[oldv]
    1654916566            oldvdim = oldov.dimensions
    16550             vinf = ncvar.variable_inf(onc,oldv)
    16551 
    16552             if not gen.searchInlist(newdims,olvdim):
    16553                 odim = onc.dimensions[olvdim]
    16554                 if odim.isunlimited:
    16555                     newdim = newonc.createDimension(None)
    16556                 else:
    16557                     newdim = newonc.createDimension(len(odim))
    16558 
    16559             newvar = newonc.createVariable(oldv, ncvar.nctype(vinf.dtype), vind.dimns)
     16567            vinf = variable_inf(oldov)
     16568
     16569            for oldd in oldvdim:
     16570                if not gen.searchInlist(newdims,oldd):
     16571                    odim = onc.dimensions[oldd]
     16572                    if odim.isunlimited():
     16573                        newdim = newonc.createDimension(oldd, None)
     16574                    else:
     16575                        newdim = newonc.createDimension(oldd, len(odim))
     16576
     16577            newvar = newonc.createVariable(oldv, nctype(vinf.dtype), vinf.dimns)
    1656016578            newvar[:] = oldov[:]
    1656116579            for attrn in vinf.attributes:
    1656216580                attrv = oldov.getncattr(attrn)
    16563                 newattr = ncvar.set_attribute(newvar,attrn,attrv)
     16581                newattr = set_attribute(newvar,attrn,attrv)
    1656416582
    1656516583        newonc.sync()
     
    1656816586    # Global attributes
    1656916587    for gattrn in onc.ncattrs():
    16570         gattrval = onc.getncattr(gattr)
    16571         newattr = ncvar.set_attribute(newonc, gattr, gattrval)
     16588        gattrval = onc.getncattr(gattrn)
     16589        newattr = set_attribute(newonc, gattrn, gattrval)
    1657216590
    1657316591    newonc.sync()
     
    1657516593    onc.close()
    1657616594
    16577     sout = sub.call('mv ' + filen + ' ' + ofile, shell=True)
     16595    sout = sub.call('mv ' + ofile + ' ' + filen, shell=True)
     16596    print fname + ": succesfully cleaned '" + filen + "' !!"
    1657816597
    1657916598    return
    1658016599
    16581 
    1658216600#quit()
Note: See TracChangeset for help on using the changeset viewer.