Changeset 1360 in lmdz_wrf


Ignore:
Timestamp:
Dec 2, 2016, 3:32:12 PM (9 years ago)
Author:
lfita
Message:

Adding `getvars_tofile': Function to get variables from a file and pass them to another one

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r1311 r1360  
    3333## e.g. # nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@add|wrfout_d01_2001-11-11_00:00:00|T2%west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@subc,273.15|wrfout_d01_2001-11-11_00:00:00|None' -v 'tempC,air!temperature,C'
    3434## e.g. # nc_var.py -o compute_opersvarsfiles -S 'lon|lon|-1;lat|lat|-1;time_counter|time_counter|-1@forwrdderiv,1,1,2|/ccc/store/cont003/gen7593/fitaborl/etudes/DynamicoESM/aquaplanet/AR40/19800101000000-19810101000000/histday.nc|t2m' -v 'tasderiv,x-derivative|of|air|temperature,K
    35 
     35## e.g. # nc_var.py -o getvars_tofile -S soils_param_Zobler_textXIOS.nc -f soils_param_colorXIOS.nc -v 'soiltext,soiltexttypes'
    3636
    3737from optparse import OptionParser
     
    8383# get_Variables: Function to get a list of variables from an existing file
    8484# getvalues_lonlat: Function to retrieve the values from the closest grid point to a set of longitude, latitude values
     85# getvars_tofile: Function to get variables from a file and pass them to another one
    8586# grattr: Function to read a global atribute
    8687# grmattr: Removing a global attribute
     
    158159  'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_attribute',                    \
    159160  'get_namelist_vars', 'get_Variables',                                              \
    160   'getvalues_lonlat', 'grattr',                                                      \
     161  'getvalues_lonlat', 'getvars_tofile', 'grattr',                                    \
    161162  'grmattr', 'idims', 'igattrs', 'increaseDimvar', 'isgattrs',                       \
    162163  'isvattrs', 'ivars', 'ivattrs', 'LMDZ_toCF', 'maskvar', 'model_characteristics',   \
     
    326327elif oper == 'getvalues_lonlat':
    327328    ncvar.getvalues_lonlat(opts.values, opts.ncfile)
     329elif oper == 'getvars_tofile':
     330    ncvar.getvars_tofile(opts.values, opts.ncfile, opts.varname)
    328331elif oper == 'grattr':
    329332    ncvar.grattr(opts.values, opts.ncfile)
  • trunk/tools/nc_var_tools.py

    r1358 r1360  
    7171# get_Variables: Function to get a list of variables from an existing file
    7272# getvalues_lonlat: Function to retrieve the values from the closest grid point to a set of longitude, latitude values
     73# getvars_tofile: Function to get variables from a file and pass them to another one
    7374# grattr: Function to read a global atribute
    7475# grmattr: Removing a global attribute
     
    1795817959    return
    1795917960
     17961def getvars_tofile(values,ncfile,varname):
     17962    """ Function to get variables from a file and pass them to another one
     17963      values= [getfile]
     17964        getfile: name of the file from which variables have to be taken
     17965      ncfile= name of the file to receive the variables
     17966      varname= ',' list of variables to get ('all', for all variables)
     17967      NOTE: all coincident dimensions, variables will not be taken
     17968    """
     17969    fname = 'getvars_tofile'
     17970   
     17971    if values == 'h':
     17972        print fname + '_____________________________________________________________'
     17973        print getvars_tofile.__doc__
     17974        quit()
     17975
     17976    #arguments = '[getfile]'
     17977    #gen.check_arguments(fname, values, arguments, ',')
     17978
     17979    #getfile = values.split(',')[0]
     17980    getfile = values
     17981
     17982    if not os.path.isfile(getfile):
     17983        print errormsg
     17984        print '  ' + fname + ": file from which get variables '" + getfile +         \
     17985          "' does not exist !!"
     17986        quit(-1)
     17987
     17988    ionc = NetCDFFile(getfile, 'r')
     17989    ronc = NetCDFFile(ncfile, 'a')
     17990
     17991    # Getting list of variables to get
     17992    ivarns = ionc.variables.keys()
     17993    if varname == 'all':
     17994        varns = list(ivarns)
     17995    else:
     17996        varns = varname.split(',')
     17997
     17998    print '  ' + fname + ": getting variables from file '" + getfile + "' to '" +    \
     17999      ncfile + ' _______'
     18000    rvarns = ronc.variables.keys()
     18001    for vn in varns:
     18002       rdimns = list(ronc.dimensions)
     18003       if not gen.searchInlist(ivarns,vn):
     18004           print errormsg
     18005           print '  '  + fname + ": file '" + getfile + "' does not have variable '"+\
     18006             vn + "' !!"
     18007           print '    available variables:', ivarns
     18008           quit(-1)
     18009       if not gen.searchInlist(rvarns,vn):
     18010           print '    '  + fname + ": adding variable '" + vn + "' ..."
     18011           
     18012           iovar = ionc.variables[vn]
     18013           vardims = list(iovar.dimensions)
     18014           for vd in vardims:
     18015               if not gen.searchInlist(rdimns,vd):
     18016                   ovd = ionc.dimensions[vd]
     18017                   if ovd.isunlimited():
     18018                       dsize = None
     18019                   else:
     18020                       dsize = len(ovd)
     18021
     18022                   print '    '  + fname + ": adding dimension '" + vd + "' (",      \
     18023                     dsize, ') ...'
     18024                   ronc.createDimension(vd,dsize)
     18025           varvals = iovar[:]
     18026           vartype = iovar.dtype
     18027           varattrs = iovar.ncattrs()
     18028           if gen.searchInlist(varattrs, '_FillValue'):
     18029               fillval = iovar.getncattr('_FillValue')
     18030               newvar = ronc.createVariable(vn, vartype, vardims, fill_value=fillval)
     18031               varattrs.remove('_FillValue')
     18032           else:
     18033               newvar = ronc.createVariable(vn, vartype, vardims)
     18034           newvar[:] = varvals[:]
     18035           for attrn in varattrs:
     18036               attrv = iovar.getncattr(attrn)
     18037               newattr = set_attribute(newvar,attrn,attrv)
     18038           ronc.sync()
     18039
     18040    ionc.close()
     18041
     18042    # Global values
     18043    newattr = set_attribute(ronc, 'PyNCplot', 'http://www.xn--llusfb-5va.cat/python/PyNCplot/')
     18044    newattr = set_attribute(ronc, 'script', fname)
     18045    newattr = set_attribute(ronc, 'get_file', getfile)
     18046    newattr = set_attribute(ronc, 'get_variables', varname)
     18047
     18048    ronc.sync()
     18049    ronc.close()
     18050
     18051    return
    1796018052
    1796118053#quit()
Note: See TracChangeset for help on using the changeset viewer.