Changeset 1008 in lmdz_wrf


Ignore:
Timestamp:
Aug 11, 2016, 5:17:55 PM (9 years ago)
Author:
lfita
Message:

Adding `cleaning_varsfile': Function to keep a list of varibales from a file

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1005 r1008  
    1515## e.g. # nc_var.py -o model_characteristics -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S WRF
    1616## e.g. # nc_var.py -o WRF_toCF -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S XLONG:XLAT:19491201000000:minutes
     17## e.g. # nc_var.py -o cleaning_varsfile -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S T2,XLONG,XLAT,Times
    1718
    1819from optparse import OptionParser
     
    2728operations=['addvals', 'chdimname', 'changevartype', 'checkallvars',                 \
    2829  'checkAllValues', 'checkNaNs',                                                     \
    29   'chgtimestep', 'chvarname', 'compute_deaccum', 'compute_opersvarsfiles',           \
     30  'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum',                \
     31  'compute_opersvarsfiles',                                                          \
    3032  'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj',          \
    3133  'computevar_model', 'DatesFiles',                                                  \
     
    130132elif oper == 'chvarname':
    131133    ncvar.chvarname(opts.values, opts.ncfile, opts.varname)
     134elif oper == 'cleaning_varsfile':
     135    ncvar.cleaning_varsfile(opts.values, opts.ncfile)
    132136elif oper == 'compute_deaccum':
    133137    ncvar.compute_deaccum(opts.values, opts.ncfile, opts.varname)
  • trunk/tools/nc_var_tools.py

    r1005 r1008  
    2222# chgtimestep: Function to change the values of a given time-step of a variable inside a netCDF for values from a nother file
    2323# chvarname: Changing the name of the variable
     24# cleaning_varsfile: Function to keep a list of varibales from a file
    2425# cls_time_information: Classs to provide information about variable time
    2526# compute_deaccum: Function to compute deaccum: deaccumulation of a variable (VAR[t+1]-VAR[t])
     
    1651416515    return
    1651516516
     16517def cleaning_varsfile(values, filen):
     16518    """ Function to keep a list of varibales from a file
     16519      values= ',' separated list of variables to keep
     16520      filen= name of the file
     16521    """
     16522    fname = 'cleaning_varsfile'
     16523
     16524    if values == 'h':
     16525        print fname + '_____________________________________________________________'
     16526        print WRF_toCF.__doc__
     16527        quit()
     16528
     16529    arguments = '[values]'
     16530    gen.check_arguments(fname, values, arguments, ':')
     16531
     16532    keepvars = values.split(',')
     16533
     16534    ofile = 'cleaning_varsfile.nc'
     16535
     16536    # opening source file
     16537    onc = NetCDFFile(filen, 'r')
     16538
     16539    # opening new file
     16540    newonc = NetCDFFile(ofile, 'w')
     16541
     16542    oldvars = onc.variables.keys()
     16543    for oldv in oldvars:
     16544        newdims = newonc.dimensions
     16545        newvars = newonc.variables.keys()
     16546
     16547        if not gen.searchInlist(newvars, oldv):
     16548            oldov = onc.variables[oldv]
     16549            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)
     16560            newvar[:] = oldov[:]
     16561            for attrn in vinf.attributes:
     16562                attrv = oldov.getncattr(attrn)
     16563                newattr = ncvar.set_attribute(newvar,attrn,attrv)
     16564
     16565        newonc.sync()
     16566
     16567    newonc.sync()
     16568    # Global attributes
     16569    for gattrn in onc.ncattrs():
     16570        gattrval = onc.getncattr(gattr)
     16571        newattr = ncvar.set_attribute(newonc, gattr, gattrval)
     16572
     16573    newonc.sync()
     16574    newonc.close()
     16575    onc.close()
     16576
     16577    sout = sub.call('mv ' + filen + ' ' + ofile, shell=True)
     16578
     16579    return
     16580
     16581
    1651616582#quit()
Note: See TracChangeset for help on using the changeset viewer.