Changeset 882 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 18, 2016, 7:16:25 PM (9 years ago)
Author:
lfita
Message:

Adding `SliceVarDict?': Function to slice a given variable throughout a dictionary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r877 r882  
    9191# setvar_asciivalues: Function to set de values of a variable with an ASCII file (common Fortran-like format)
    9292# SliceVar: Function to slice a given variable throughout a given list of dimensions
     93# SliceVarDict: Function to slice a given variable throughout a dictionary
    9394# slice_variable: Function to return a slice of a given variable according to values to its dimensions
    9495# sorttimesmat: Function to sort the time values of a given file
     
    88918892            * -1: all along the dimension
    88928893            * [beg]:[end] slice from [beg] to [end]
     8894            NOTE: that dimension of the variables which not appear al values will be used
     8895        [dimsoper]; [dimname1]:[dimname2]:[...[dimnameN]] names of the dimensions along
     8896          which operation has to be done
     8897        [opkind]; operation to perform along the dimensions with a range: max, mean,
     8898          mean2, min, sum
     8899        [dimvn]; [varname1]:[varname2]:[...[varnameM]] variables with the values of the
     8900          dimensions
     8901      ncfile= netCDF file
     8902      varn= ',' list of variable names ('all' for all)
     8903    """
     8904    fname = 'file_oper_alongdims'
     8905
     8906    if values == 'h':
     8907        print fname + '_____________________________________________________________'
     8908        print file_oper_alongdims.__doc__
     8909        quit()
     8910
     8911    dimvals = values.split(',')[0]
     8912    dimsoper = values.split(',')[1]
     8913    operkind = values.split(',')[2]
     8914    dimvn = values.split(',')[3]
     8915
     8916    dimsoperS = gen.numVector_String(dimsoper.split(':'),', ')
     8917    objnc = NetCDFFile(ncfile, 'r')
     8918
     8919    if varn.find(',') != -1:
     8920        varns = varn.split(',')
     8921    else:
     8922        if varn == 'all':
     8923            varns = objnc.variables
     8924        else:
     8925            varns = [varn]
     8926   
     8927    ofile = 'file_oper_alongdims_' + operkind + '.nc'
     8928
     8929    if dimsoper.find(':') != -1:
     8930        dimstouse = dimsoper.split(':')
     8931    else:
     8932        dimstouse = [dimsoper]
     8933
     8934# Getting slice dimensions
     8935    dimslice = []
     8936    dimvalslice = []
     8937    for dv in dimvals:
     8938        dnv = dv.slice('|')[0]
     8939        dvv = dv.slice('|')[1]
     8940        dimslice.append(dnv)
     8941        dimvalslice.append(dvv)
     8942
     8943
     8944# Creation of output file
     8945##
     8946    objnewnc = NetCDFFile(ofile, 'w')
     8947
     8948    print '  ' + fname + ": '" + operkind + "' with:",dimsoper,'_______'
     8949    for vn in varns:
     8950        print "'" + vn + "' ... .. ."
     8951        if not objnc.variables.has_key(vn):
     8952            print errormsg
     8953            print '  ' + fname + ': netCDF file "' + ncfile +                        \
     8954              '" does not have variable "' + vn + '" !!'
     8955            quit(-1)
     8956
     8957        # Do we have to compute this variable?
     8958        ov = objnc.variables[vn]
     8959        tocompute = False
     8960        for vd in ov.dimensions:
     8961            if searchInlist(dimsoper, vd): tocompute = True
     8962
     8963        if tocopmute:
     8964       
     8965
     8966
     8967
     8968    return
     8969
     8970def file_oper_alongdims_old(values, ncfile, varn):
     8971    """ Function to operate a file along different dimensions of a variable
     8972    file_oper_alongdims(values, ncfile, varn)
     8973      values= [dimvals],[dimsoper],[opkind],[dimvn]
     8974        [dimvals]; [dimname1]:[val1]|[dimdname2]:[val2]|[...[dimnameN]:[valN]]
     8975          [value];
     8976            * [integer]: which value of the dimension
     8977            * -1: all along the dimension
     8978            * [beg]:[end] slice from [beg] to [end]
    88938979        [dimsoper]; [dimname1]:[dimname2]:[...[dimnameN]] names of the dimensions along
    88948980          which operation has to be done
     
    1276112847        if gen.searchInlist(dloop,dim):
    1276212848            slicevals.append(dloopindex[idim])
     12849        else:
     12850            slicevals.append(slice(0,varshape[idim]))
     12851        idim = idim + 1
     12852
     12853    return slicevals
     12854
     12855def SliceVarDict(ovar,slicedcit):
     12856    """ Function to slice a given variable throughout a dictionary
     12857      ovar= variable object to slice
     12858      slicedict= dictionary with the values to slice
     12859        slicedict[dimn] = [value]
     12860          [value] = -1, all the values
     12861          [value] = int, a single value
     12862          [value] = [beg, end, frq], from a beginning to an end with a given frequency
     12863    """
     12864    fname = 'SliceVarDict'
     12865
     12866    varshape = ovar.shape
     12867
     12868    slicevals = []
     12869    idim = 0
     12870    for dim in ovar.dimensions:
     12871        if gen.searchInlist(slicedict.keys(),dim):
     12872            dictv = slicedict[dim]
     12873            if type(dictv) == type([1]):
     12874                slicevals.append( slice(dictv[0], dictv[1], dictv[2]) )
     12875            elif type(dictv) == type(int(1)):
     12876                if dictv == -1:
     12877                    slicevals.append(slice(0,varshape[idim]))
     12878                else
     12879                    slicevals.append(dictv)
    1276312880        else:
    1276412881            slicevals.append(slice(0,varshape[idim]))
Note: See TracChangeset for help on using the changeset viewer.