Changeset 964 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 28, 2016, 5:30:05 PM (8 years ago)
Author:
lfita
Message:

Adding `get_Variables': Function to get a list of variables from an existing file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r961 r964  
    5454# get_namelist_vars: Function to get namelist-like  values ([varname] = [value])
    5555# get_str_nc: Function to get string values in a netCDF variable as a chains of 1char values
     56# get_Variables: Function to get a list of variables from an existing file
    5657# getvalues_lonlat: Function to retrieve the values from the closest grid point to a set of longitude, latitude values
    5758# grattr: Function to read a global atribute
     
    1634116342    return
    1634216343
     16344def get_Variables(values, ncfile):
     16345    """ Function to get a list of variables from an existing file
     16346      values = ',' separated list of names of variables to get
     16347      ncfile= WRF file to use
     16348    """
     16349    fname = 'get_Variables'
     16350
     16351    if values == 'h':
     16352        print fname + '_____________________________________________________________'
     16353        print get_Variables.__doc__
     16354        quit()
     16355
     16356    variables = values.split(',')
     16357
     16358    ofile = 'get_Variables.nc'
     16359
     16360    onewnc = NetCDFFile(ofile, 'w')
     16361
     16362    onc = NetCDFFile(ncfile, 'r')
     16363    filevars = list(onc.varibles.keys)
     16364
     16365    for var in variables:
     16366        if not gen.searchInlist(filevars, var):
     16367            print errormsg
     16368            print '  ' + fname + ": file '" + ncfile + "' does not have variable '" +\
     16369              var + "' !!"
     16370            print '    variables in file:', filevars
     16371            quit(-1)
     16372
     16373        print '  ' + fname + ": getting variable '" + var + "'..."
     16374        ovar = onc.variables[var]
     16375        varinf = variable_inf(ovar)
     16376        vardims = varind.dmns
     16377
     16378        newfiledims = list(onewnc.dimensions.keys)
     16379
     16380        for vdim in vardims:
     16381            if not gen.searchInlist(newfiledims, vdim):
     16382                print '  ' + fname + ": including dimension '" + vdim + "'..."
     16383                if onc.dimensions[vdim].isunlimted:
     16384                    newdim = onewnc.createDimension(vdim, None)
     16385                else:
     16386                    newdim = onewnc.createDimension(vdim, len(onc.dimensions[vdim]))
     16387
     16388       if varinf.FillValue is not None:
     16389           newvar = onewnc.creatrVariable(var, nctype(varinf.dtype), tuple(vardims), \
     16390             fillvalue = varinf.FillValue)
     16391       else:
     16392           newvar = onewnc.creatrVariable(var, nctype(varinf.dtype), tuple(vardims))
     16393
     16394       for attrn in varinf.atttributes:
     16395           attrv = ovar.getattribute(attrn)
     16396           newattr = set_attribute(newvar, attrn, attrv)
     16397
     16398       onewnc.sync()
     16399
     16400    # Global attributes
     16401    for attrn in onc.ncattrs:
     16402       attrv = ovar.getattribute(attrn)
     16403       newattr = set_attribute(newvar, attrn, attrv)
     16404   
     16405    onewnc.sync()
     16406    onc.close()
     16407
     16408    onewnc.close()
     16409
     16410    print fname + ": successful written of '" + ncfile + "' !!"
     16411
    1634316412#quit()
Note: See TracChangeset for help on using the changeset viewer.