Changeset 1535 in lmdz_wrf


Ignore:
Timestamp:
Apr 21, 2017, 9:14:10 PM (8 years ago)
Author:
lfita
Message:

Improving `copute_opersvarsfiles' by:

Adding `contoperation': which is used for calculations on result of previous operations without using a variable from a file
Adding:

'arctan', arctangent [-pi, pi]
'norm_meanminmax',[NOTnormdims]: normalization of data as: (val-<val>)/(max(val)-min(val)) except along dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)
'norm_minmax',[NOTnormdims]: normalization of data as: [2val-(max(val)-min(val)]/[max(val)+min(val)] except along dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)
'norm_meanstd',[NOTnormdims]: normalization of data as: (val-<val>)/stdev(val) except along dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)

Location:
trunk/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/documentation/ncmanage/compute_opersvarsfiles.html

    r1519 r1535  
    3030      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'add': adding [var] ([prevalues] + [var])<BR>
    3131      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'addc',[modval1]: add [modval1] to [prevalues]<BR>
     32      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'arctan', arctangent [-p1, pi]<BR>
    3233      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'centerderiv',[N],[ord],[dim]: un-scaled center [N]-derivative of order [ord] along dimension [dim] of [var]<BR>
    3334      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'div': dividing by [var] ([prevalues] / [var])<BR>
     
    3940      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mul': multiplying by [var] ([prevvalues] * [var])<BR>
    4041      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mulc',[modval1]: [prevalues] multiplied by [modval1]<BR>
     42      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'norm_meanminmax',[NOTnormdims]: normalization of data as: (val-<val>)/(max(val)-min(val)) except along <BR>
     43      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)<BR>
     44      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'norm_minmax',[NOTnormdims]: normalization of data as: [2val-(max(val)-min(val)]/[max(val)+min(val)]<BR>
     45      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;except along dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)<BR>
     46      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'norm_meanstd',[NOTnormdims]: normalization of data as: (val-<val>)/stdev(val) except along <BR>
     47      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)<BR>
     48       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;dimensions [NOTnormdims] (':' list of dimension names)<BR>
    4149      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'pot': powering with [var] ([prevalues] ** [var])<BR>
    4250      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'potc',[modval1]: [prevalues] ** [modval1]<BR>
     
    6068    </DIV>
    6169    <DIV CLASS="valins">
    62       $ python ${pyHOME}/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'<BR>
    63       $ python ${pyHOME}/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
     70    * Transforming temperature from Kelvin to &deg;C<BR>
     71    &nbsp;&nbsp;  $ python ${pyHOME}/nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@subc,273.15|wrfout_d01_2001-11-11_00:00:00|T2' -v 'tempC,air!temperature,C'<BR>
     72    * Computing the x-derivative of first order<BR>
     73    &nbsp;&nbsp;  $ python ${pyHOME}/nc_var.py -o compute_opersvarsfiles -S 'lon|lon|-1;lat|lat|-1;time_counter|time_counter|-1@forwrdderiv,1,1,2|histday.nc|t2m' -v 'tasderiv,x-derivative|of|air|temperature,K' <BR>
     74    * Normalizing a variable by substracting its mean and weighting by its standard-deviation<BR>
     75    &nbsp;&nbsp; $ python ${pyHOME}/nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;Time|WRFtime|-1@norm_meanstd,Time|wrfout_d01_1995-01-01_00:00:00|T2' -v 'tasnorm,normalized!2m!temperature!substracting!mean!and!weighting!by!standard!deviation,K' <BR>
     76    * Getting height of the frist level from surface from a WRF file<BR>
     77    &nbsp;&nbsp; $ python $pyHOME/nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;bottom_top_stag|ZNW|1@addc,0|wrfout_d01_1995-01-01_00:00:00|PH%west_east|XLONG|-1;south_north|XLAT|-1;bottom_top_stag|ZNW|1@add|wrfout_d01_1995-01-01_00:00:00|PHB%contoperation@divc,9.81%west_east|XLONG|-1;south_north|XLAT|-1;Time|WRFtime|-1@sub|wrfout_d01_1995-01-01_00:00:00|HGT' -v 'height1lev,height!above!surface!of!first!level,m'
     78    * Computing wind-direction from a WRF file<BR>
     79    &nbsp;&nbsp; $ python $pyHOME/nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;Time|WRFtime|-1@addc,0|wrfout_d01_1995-01-01_00:00:00|V10%west_east|XLONG|-1;south_north|XLAT|-1;Time|WRFtime|-1@arctan|wrfout_d01_1995-01-01_00:00:00|U10%contoperation@mulc,57.2957795131' -v 'wsdir,2m!wind!direction,Degrees'
    6480    </DIV>
    6581  </BODY>
  • trunk/tools/generic_tools.py

    r1520 r1535  
    1015510155        'add': adding matB to matA (matA + matB)
    1015610156        'addc',[modval1]: add [modval1]
     10157        'arctan', arctangent of matA (result [-pi, pi])
    1015710158        'centerderiv',[N],[ord],[dim]: un-scaled center [N]-derivative of order [ord] along dimension [dim] of matA
    1015810159        'div': dividing by matB (matA / matB)
     
    1016410165        'mul': multiplying ny matB (matA * matB)
    1016510166        'mulc',[modval1]: multiply by [modval1]
     10167        'norm_meanminmax',[dimns],[normdimns],[idims]: normalization of data of dimensions [dimns] (':' list) as:
     10168          (val - <val>)/(max(val)-min(val)) along dimension-names [normdimns] (':' list of names) and
     10169          dimension-indices [idims] (':' list of indices) of matA
     10170        'norm_minmax',[NOTnormdims]: normalization of data as: [2val-(max(val)-min(val)]/[max(val)+min(val)]
     10171          except along dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)
     10172        'norm_meanstd',[dimns],[normdimns],[idims]: normalization of data of dimensions [dimns] (':' list) as:
     10173          (val - <val>)/stdev(val) along dimension-names [normdimns] (':' list of names) and dimension-indices
     10174          [idims] (':' list of indices) of matA
    1016610175        'pot': powering with matB (matA ** matB)
    1016710176        'potc',[modval1]: [val] ** [modval1]
     
    1029410303
    1029510304    # Dictionary with the description of the operations
    10296     Lopn = {'add': '+', 'addc': '+', 'div': '/', 'divc': '/', 'inv': '^(-1)',        \
    10297       'lowthres': '<', 'mul': '*', 'mulc': '*', 'pot': '^', 'potc': '^',             \
    10298       'repl': 'repl_excpt', 'replbig': 'repl_excpt_bigger',                          \
     10305    Lopn = {'add': '+', 'addc': '+', 'arctan': 'arctangent', 'div': '/', 'divc': '/',\
     10306      'inv': '^(-1)', 'lowthres': '<', 'mul': '*', 'mulc': '*',                      \
     10307      'norm_meanminmax': 'Norm_mean_minmax',                                         \
     10308      'norm_minmax': 'Norm_minmax', 'norm_meanstd': 'Norm_mean_devstd', 'pot': '^',  \
     10309      'potc': '^', 'repl': 'repl_excpt', 'replbig': 'repl_excpt_bigger',             \
    1029910310      'repless': 'repl_excpt_less', 'sub': '-', 'subc': '-', 'upthres': '<'}
    1030010311    # Operations by constant value
     
    1030410315
    1030510316    # Available operations
    10306     opavail = ['add', 'addc', 'centerderiv', 'div', 'divc', 'forwrdderiv', 'inv',    \
    10307       'lowthres', 'lowthres@oper', 'mul', 'mulc', 'pot', 'potc', 'repl', 'replbig',  \
    10308       'repless', 'sub', 'subc', 'upthres', 'upthres@oper']
     10317    opavail = ['add', 'addc', 'arctan', 'centerderiv', 'div', 'divc', 'forwrdderiv', \
     10318      'inv',    \
     10319      'lowthres', 'lowthres@oper', 'mul', 'mulc', 'norm_meanminmax', 'norm_minmax',  \
     10320      'norm_meanstd', 'pot', 'potc', 'repl', 'replbig', 'repless', 'sub', 'subc',    \
     10321      'upthres', 'upthres@oper']
    1030910322    opavailc = ['addc', 'divc', 'inv', 'mulc', 'potc', 'subc']
    1031010323
     
    1031510328        value1 = retype(opN.split(',')[1], valtype)
    1031610329        Lopn['addc'] = '+' + str(value1)
     10330    elif opN[0:6] == 'arctan':
     10331        opn = 'arctan'
     10332        Lopn['arctan'] = 'arctan'
    1031710333    elif opN[0:11] == 'centerderiv':
    1031810334        opn = 'centerderiv'
     
    1039010406        value1 = retype(opN.split(',')[1], valtype)
    1039110407        Lopn['mulc'] = '*' + str(value1)         
     10408    elif opN[0:15] == 'norm_meanminmax':
     10409        opn = 'norm_meanminmax'
     10410        dimns = opN.split(',')[1].split(':')
     10411        normdimns = opN.split(',')[2].split(':')
     10412        idimsS = opN.split(',')[3].split(':')
     10413        idims = tuple(np.array(idimsS, dtype=int))
     10414        Lopn['norm_meanminmax'] = 'Norm_mean_minmax(' + ','.join(normdimns) + ')'
     10415    elif opN[0:11] == 'norm_minmax':
     10416        opn = 'norm_minmax'
     10417        dimns = opN.split(',')[1].split(':')
     10418        normdimns = opN.split(',')[2].split(':')
     10419        idimsS = opN.split(',')[3].split(':')
     10420        idims = tuple(np.array(idimsS, dtype=int))
     10421        Lopn['norm_minmax'] = 'Norm_minmax(' + ','.join(normdimns) + ')'
     10422    elif opN[0:12] == 'norm_meanstd':
     10423        opn = 'norm_meanstd'
     10424        dimns = opN.split(',')[1].split(':')
     10425        normdimns = opN.split(',')[2].split(':')
     10426        idimsS = opN.split(',')[3].split(':')
     10427        idims = tuple(np.array(idimsS, dtype=int))
     10428        Lopn['norm_meanstd'] = 'Norm_mean_std(' + ','.join(normdimns) + ')'
    1039210429    elif opN[0:4] == 'potc':
    1039310430        opn = 'potc'
     
    1043110468   
    1043210469    # Carrying on the operation
     10470    additional = None
    1043310471    if opn == 'add':
    1043410472        newmat = matA + matB
    1043510473    elif opn == 'addc':
    1043610474        newmat = matA + value1
     10475    elif opn == 'arctan':
     10476        newmat = np.arctan2(matA,matB)
    1043710477    elif opn == 'centerderiv':
    1043810478        sdim = matAshape[dim]
     
    1060210642    elif opn == 'mulc':
    1060310643        newmat = matA * value1
     10644    elif opn == 'norm_meanminmax':
     10645        newmat = np.zeros((matA.shape), dtype=matA.dtype)
     10646        meanval = np.mean(matA, axis=idims)
     10647        minval = np.min(matA, axis=idims)
     10648        maxval = np.max(matA, axis=idims)
     10649        slices = provide_slices(dimns, matA.shape, normdimns)
     10650        Nslices = len(slices)
     10651        for islc in range(Nslices):
     10652            listv = list(slices[islc])
     10653            for iind in range(len(idims)):
     10654                indv = idims[iind] - iind
     10655                listv.pop(indv)
     10656            newmat[tuple(slices[islc])] = (matA[tuple(slices[islc])] -               \
     10657              meanval[tuple(listv)]) / (maxval[tuple(listv)]-minval[tuple(listv)])
     10658        additional = [meanval, minval, maxval]
     10659
     10660    elif opn == 'norm_minmax':
     10661        newmat = np.zeros((matA.shape), dtype=matA.dtype)
     10662        minval = np.min(matA, axis=idims)
     10663        maxval = np.max(matA, axis=idims)
     10664        slices = provide_slices(dimns, matA.shape, normdimns)
     10665        Nslices = len(slices)
     10666        for islc in range(Nslices):
     10667            listv = list(slices[islc])
     10668            for iind in range(len(idims)):
     10669                indv = idims[iind] - iind
     10670                listv.pop(indv)
     10671            xnadd = maxval[tuple(listv)]+minval[tuple(listv)]
     10672            xnsub = maxval[tuple(listv)]-minval[tuple(listv)]
     10673            newmat[tuple(slices[islc])] = (2.*matA[tuple(slices[islc])]-xnadd)/xnsub
     10674        additional = [minval, maxval]
     10675    elif opn == 'norm_meanstd':
     10676        newmat = np.zeros((matA.shape), dtype=matA.dtype)
     10677        meanval = np.mean(matA, axis=idims)
     10678        stdval = np.std(matA, axis=idims)
     10679        slices = provide_slices(dimns, matA.shape, normdimns)
     10680        Nslices = len(slices)
     10681        for islc in range(Nslices):
     10682            listv = list(slices[islc])
     10683            for iind in range(len(idims)):
     10684                indv = idims[iind] - iind
     10685                listv.pop(indv)
     10686            newmat[tuple(slices[islc])] = (matA[tuple(slices[islc])] -               \
     10687              meanval[tuple(listv)]) / stdval[tuple(listv)]
     10688        additional = [meanval, stdval]
    1060410689    elif opn == 'pot':
    1060510690        newmat = matA ** matB
     
    1064110726        quit(-1)
    1064210727
    10643     return newmat, Lopn[opn]
     10728    return newmat, Lopn[opn], additional
    1064410729
    1064510730def latex_fig_array(figs, obf, figcaption, figlabel, dist='sqr', refsize=0.9,        \
  • trunk/tools/nc_var_tools.py

    r1533 r1535  
    71787178      (OPER1.FILE1_VAR1 OPER2.FILE2_VAR2), operations are going to be sequentially made
    71797179      compute_opersvarsfiles(values, varinfo).
    7180         NOTE: Values from the first file (with always operation 'add')
    71817180        [prevalues]: values from the previous operations
     7181        NOTE: to start with a multiple operations one can use as operation of the first variable 'addc,0'
    71827182        values= '%' separated list of dimension ranges and file,operation,variable to compute
    71837183          [dimranges1]@[operfile1var]%[dimranges2]@[operfile2var]%[...[dimrangesM]@[operfileMvar]]
     
    71897189                      int, a single value
    71907190            [beg,end,frq], from a beginning to an end with a given frequency
     7191             'WRFtime': as time variable computed from WRF 'Times' variable
    71917192          operfilevar: [oper]|[file]|[var] '|' separated list of triplets of [operation], [file], [variable name to use]
     7193          NOTE: use 'contoperation' to continue an operation with the result of previous operations [prevalues]
    71927194           * [oper]: operations (being [prevalues] the values computed until the operation)
    71937195             'add': adding [var] ([prevalues] + [var])
    71947196             'addc',[modval1]: add [modval1] to [prevalues]
     7197             'arctan', arctangent [-pi, pi]
    71957198             'centerderiv',[N],[ord],[dim]: un-scaled center [N]-derivative of order [ord] along dimension [dim] of [var]
    71967199             'div': dividing by [var] ([prevalues] / [var])
     
    72027205             'mul': multiplying by [var] ([prevvalues] * [var])
    72037206             'mulc',[modval1]: [prevalues] multiplied by [modval1]
     7207             'norm_meanminmax',[NOTnormdims]: normalization of data as: (val-<val>)/(max(val)-min(val)) except along
     7208               dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)
     7209             'norm_minmax',[NOTnormdims]: normalization of data as: [2val-(max(val)-min(val)]/[max(val)+min(val)]
     7210               except along dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)
     7211             'norm_meanstd',[NOTnormdims]: normalization of data as: (val-<val>)/stdev(val) except along
     7212               dimensions [NOTnormdims] (':' list of dimension names, or 'any' for using all dimensions)
    72047213             'pot': powering with [var] ([prevalues] ** [var])
    72057214             'potc',[modval1]: [prevalues] ** [modval1]
     
    72187227              'lowthres@oper', 'mulc', 'potc', 'subc', 'upthres', 'upthres@oper'
    72197228        varinfo= [varname],[Lvarname],[varunits]
    7220           varname: name of the final variable
     7229          varname: name of the final variable (to be used as standard name)
    72217230          Lvarname: long name of the final variable ('!' for spaces)
    72227231          varunits: units of the final variable ('!' for spaces)
     
    72347243    # List of operations which need to be started with a matrix of ones
    72357244    opinione = ['mul', 'div', 'pot']
     7245    # Operations which need to deal with dimension's names and indices
     7246    opdim=['norm_meanminmax', 'norm_meanstd', 'norm_minmax']
    72367247
    72377248    varname = varinfo.split(',')[0]
     
    72507261    varns = []
    72517262    for fileoper in fileopers:
    7252         ranges.append(fileoper.split('@')[0])
     7263        print 'fileoper', fileoper
     7264        arguments = '[dimranges]@[operfilevar]'
     7265        gen.check_arguments(fname, fileoper, arguments, '@')
    72537266        fnop = fileoper.split('@')[1]
    7254         opers.append(fnop.split('|')[0])
    7255         filens.append(fnop.split('|')[1])
    7256         varns.append(fnop.split('|')[2])
     7267        if fileoper.split('@')[0] != 'contoperation':
     7268            ranges.append(fileoper.split('@')[0])
     7269            arguments = '[oper]|[file]|[var]'
     7270            gen.check_arguments(fname, fnop, arguments, '|')
     7271            opers.append(fnop.split('|')[0])
     7272            filens.append(fnop.split('|')[1])
     7273            varns.append(fnop.split('|')[2])
     7274        else:
     7275            ranges.append('continuation')
     7276            opers.append(fnop)
     7277            filens.append('continuation')
     7278            varns.append('continuation')
    72577279
    72587280    print '  ' + fname + 'Files/Operations to perform _______'
    72597281    for ifop in range(Nfileopers):
    7260         print '    ' + filens[ifop] + ' range: ' + ranges[ifop] + ' operation: ' +   \
    7261           opers[ifop] + ' variable: ' + varns[ifop]
     7282        if filens[ifop] != 'continuation':
     7283            print '    ' + filens[ifop] + ' range: ' + ranges[ifop] + ' operation: '+\
     7284              opers[ifop] + ' variable: ' + varns[ifop]
     7285        else:
     7286            print '    continuing computation by: ' + opers[ifop]
    72627287
    72637288    newunits = '-'
     
    72737298    # Dictionary with the attributes dictionay of the dimension-variables of the dimensions of `prevalues'
    72747299    Dadv = {}
     7300    # Dictionary with the additional values for certain operations of `prevalues'
     7301    Dadditionalv = {}
     7302    # Dictionary with the additional dimensions which are not used for certain operations of `prevalues'
     7303    Dadditionaldim = {}
     7304    # Dictionary with the additional units used for certain operations of `prevalues'
     7305    Dadditionalunits = {}
     7306    # Dictionary with the Headers for the names of the additional variables
     7307    DHadditionalvars = {'norm_meanminmax': ['mean','min','max'],                     \
     7308      'norm_meanstd': ['mean','std'], 'norm_minmax': ['min','max']}
    72757309
    72767310    prevalues = None
    72777311    for ifv in range(Nfileopers):
    7278         opern = opers[ifv]
     7312        operN = opers[ifv]
    72797313        filen = filens[ifv]
    72807314        varn = varns[ifv]
    72817315        rng = ranges[ifv].split(';')
    7282         if opern.find(',') != -1:
    7283             opernS = opern.split(',')[0]
     7316        if operN.find(',') != -1:
     7317            opernS = operN.split(',')[0]
    72847318        else:
    7285             opernS = opern
     7319            opernS = operN
    72867320
    72877321        opervars.append(varn)
    72887322
    7289         if not os.path.isfile(filen):
    7290             print errormsg
    7291             print '  ' + fname + ' file: "' + filen + '" does not exist !!'
    7292             quit(-1)
    7293 
    7294         objnc = NetCDFFile(filen, 'r')
    7295 
    7296         # Operation with a variable
    7297         if varn != 'None' or prevalues is None:
    7298             if not objnc.variables.has_key(varn):
     7323        if filen != 'continuation':
     7324            if not os.path.isfile(filen):
    72997325                print errormsg
    7300                 print '  ' + fname + ': netCDF file "' + filen +                     \
     7326                print '  ' + fname + ' file: "' + filen + '" does not exist !!'
     7327                quit(-1)
     7328
     7329            objnc = NetCDFFile(filen, 'r')
     7330
     7331            # Operation with a variable
     7332            if varn != 'WRFtime' and not objnc.variables.has_key(varn):
     7333                print errormsg
     7334                print '  ' + fname + ': netCDF file "' + filen +                 \
    73017335                  '" does not have variable "' + varn + '" !!!!'
    73027336                quit(-1)
     
    73177351                # Dimension-variable
    73187352                if not objnc.variables.has_key(dimv):
    7319                     print warnmsg
    7320                     print '  ' + fname + ": file '" + filen + "' does not have " +   \
    7321                       "dimension-variable '" + dimv + "' !!"
    7322                     print '    keep going on'
    7323                     #quit(-1)
     7353                    if dimv == 'WRFtime':
     7354                        Ddimvs.append([dimn, 'WRFtime', dimr])
     7355                    else:
     7356                        print warnmsg
     7357                        print '  ' +fname+ ": file '" + filen + "' does not have " + \
     7358                          "dimension-variable '" + dimv + "' !!"
     7359                        print '    keep going on'
     7360                        #quit(-1)
    73247361                else:
    73257362                    # Keeping the dimensions
    73267363                    Ddimvs.append([dimn, dimv, dimr])
    7327 
    73287364            # Getting values of the variable
    73297365            varslice, dimvarvals = SliceVarDict(varobj, dictslice)
     
    73327368            varvals = slice_createArray(varslice, varobj.dtype)
    73337369            if len(varvals.shape) > 1:
    7334                 if varvals.shape[0] != 0:
    7335                     for ix in range(varvals.shape[0]):
    7336                         varvals[ix,...] = varobj[ix,...]
    7337                 else:
    7338                     for ix in range(varvals.shape[len(varvals.shape)-1]):
    7339                         varvals[...,ix] = varobj[...,ix]
     7370#               if varvals.shape[0] != 0:
     7371#                   for ix in range(varvals.shape[0]):
     7372#                       varvals[ix,...] = varobj[tuple(varslice[ix,...])]
     7373#               else:
     7374#                   for ix in range(varvals.shape[len(varvals.shape)-1]):
     7375#                       varvals[...,ix] = varobj[tuple(varslice[...,ix])]
     7376                varvals[:] = varobj[tuple(varslice)]
    73407377            else:
    73417378                varvals[:] = varobj[:]
    7342 
     7379           
    73437380            # Getting values of the correspondant dimensions of the variable
    73447381            for iddd in range(len(Ddimvs)):
     
    73477384                dimv = DD[1]
    73487385                dimr = DD[2]
    7349                 if dimv != 'WRFt':
     7386                if dimv != 'WRFtime':
    73507387                    odimv = objnc.variables[dimv]
    73517388                    varslice, Dddv[dimn] = SliceVarDict(odimv, dictslice)
     
    73587395                    Dadv[dimn] = dicattrs
    73597396                else:
     7397                    print infmsg
     7398                    print '  ' + fname + ": computing time-variable from 'WRF'-Time"
    73607399                    datesv = []
    73617400                    wrft = objnc.variables['Times']
     
    73707409                    minref=refdate[10:12]
    73717410                    secref=refdate[12:14]
    7372 
     7411   
    73737412                    refdateS = yrref + '-' + monref + '-' + dayref + ' ' + horref +  \
    73747413                      ':' + minref + ':' + secref
    7375 
     7414   
    73767415                    dt = fdates.shape[0]
    73777416                    cftimes = np.zeros((dt), dtype=np.float)
    73787417                    for it in range(dt):
    7379                         wrfdates = gen.datetimeStr_conversion(timewrfv[it,:],        \
    7380                           'WRFdatetime', 'matYmdHMS')
     7418                        wrfdates = gen.datetimeStr_conversion(wrft[it,:],            \
     7419                         'WRFdatetime', 'matYmdHMS')
    73817420                        cftimes[it] = gen.realdatetime1_CFcompilant(wrfdates,        \
    73827421                          refdate, tunitsval)
    73837422                    tunits = tunitsval + ' since ' + refdateS
    7384                     Dddv[dimn] = 'Time'
     7423                    Dddv[dimn] = ['Time']
    73857424                    Ddv[dimn] = cftimes
    73867425                    Dtdv[dimn] = np.float
     
    73887427                      'units': tunitsval + ' since ' + refdateS}
    73897428
    7390             # Getting sizes of the dimensions
    7391             for dvn in dimvarvals:
    7392                 dimobj = objnc.dimensions[dvn]
    7393                 if dimobj.isunlimited():
    7394                     Dds[dvn] = None
    7395                 else:
    7396                     Dds[dvn] = len(dimobj)
    7397 
    7398             # operation
    7399             if prevalues is None:
    7400                 # Creation of an empty variable to start with
    7401                 if gen.searchInlist(opinizero,opernS):
    7402                     inimat = np.zeros(varvals.shape, dtype=varobj.dtype)
    7403                 elif gen.searchInlist(opinione,opernS):
    7404                     inimat = np.ones(varvals.shape, dtype=varobj.dtype)
    7405 
    7406                 prevalues, operS = gen.matoperations(opern, varvals, inimat)
     7429                # Getting sizes of the dimensions
     7430                for dvn in dimvarvals:
     7431                    dimobj = objnc.dimensions[dvn]
     7432                    if dimobj.isunlimited():
     7433                        Dds[dvn] = None
     7434                    else:
     7435                        Dds[dvn] = len(dimobj)
     7436            # Checking right slice for each dimension in variable
     7437            for dimn in dimvarvals:
     7438                found = False
     7439                if not gen.searchInlist(Dddv.keys(), dimn):
     7440                    print errormsg
     7441                    print '  ' + fname + ": sliced variable has dimension '" + dimn+ \
     7442                      "' but there is no slicing-dimension information"
     7443                    print "    add range for the dimension as '[dim]|[dimv]|[range]'"
     7444                    quit(-1)
     7445
     7446        else:
     7447        # Computing using results of previous computations
     7448            varvals = prevalues.copy()
     7449            dimvarvals = prevdims
     7450            print infmsg
     7451            print '  ' + fname + ': continuing operation with previous values'
     7452            print '    shape:', varvals.shape
     7453            print '    dimensions:', dimvarvals
     7454
     7455        # Conditions for certain operations
     7456        if gen.searchInlist(opdim, opernS):
     7457            opn = operN.split(',')[0]
     7458            normd = gen.str_list(operN.split(',')[1], ':')
     7459            usedims = list(dimvarvals)
     7460            if normd[0] != 'any':
     7461                normi = []
     7462                for dn in normd:
     7463                    normi.append(dimvarvals.index(dn))
     7464                    usedims.remove(dn)
     7465                normi.sort()
    74077466            else:
    7408                 print ' ' + fname + ': Lluis opern:', opern
    7409                 prevalues, operS = gen.matoperations(opern, prevalues, varvals)
    7410             operS = ' ' + operS + '[' + varn + ']'
     7467                normi = range(len(varvals.shape))
     7468                usedims = 'any'
     7469            for iv in range(len(normi)): normi[iv] = str(normi[iv])
     7470            opern = opernS + ',' + ':'.join(dimvarvals) + ',' +                  \
     7471              operN.split(',')[1] + ',' + ':'.join(normi)
     7472            Dadditionaldim[opernS+'@'+varn] = usedims
     7473            if varn != 'continuation':
     7474                Dadditionalunits[opernS+'@'+varn] = get_varunits(varobj)
     7475            else:
     7476                Dadditionalunits[opernS+'@'+varn] = '?'
    74117477        else:
    7412             prevalues, operS = gen.matoperations(opern, prevalues)
    7413             operS = ' ' + operS
    7414 
    7415         if opern == opers[0]:
     7478            opern = operN
     7479
     7480        if varn != 'continuation':
     7481            objnc.close()
     7482
     7483        # operation
     7484        if prevalues is None:
     7485            # Creation of an empty variable to start with
     7486            if gen.searchInlist(opinizero,opernS):
     7487                inimat = np.zeros(varvals.shape, dtype=varobj.dtype)
     7488            elif gen.searchInlist(opinione,opernS):
     7489                inimat = np.ones(varvals.shape, dtype=varobj.dtype)
     7490            else:
     7491                inimat = varvals
     7492
     7493            prevalues, operS, addvs = gen.matoperations(opern, varvals, inimat)
     7494        else:
     7495            prevalues, operS, addvs = gen.matoperations(opern, prevalues, varvals)
     7496        operS = ' ' + operS + '[' + varn + ']'
     7497
     7498#        elif varn == 'None' and prevalues is not None:
     7499#            prevalues, operS, addvs = gen.matoperations(opern, prevalues)
     7500#            operS = ' ' + operS
     7501
     7502        if operN == opers[0]:
    74167503            operationS = operS
    74177504        else:
    74187505            operationS = operationS + operS
    74197506
    7420         objnc.close()
     7507        Dadditionalv[opernS+'@'+varn] = addvs
     7508
     7509        prevdims = list(dimvarvals)
     7510        prevsizedims = varvals.shape
    74217511
    74227512# Creation of the new file
     
    74517541
    74527542    newattr = basicvardef(newvar, varname, Lvarname, varunits)
     7543
     7544    # Additional variables. Each necessary dimension to fill up the information will be created as:
     7545    #   dimension name: dim + '_' + operS + '_' + var
     7546    #   dimension size: value from the shape of the additional values
     7547
     7548    for operSvarn in Dadditionalv.keys():
     7549        addvs = Dadditionalv[operSvarn]
     7550        if addvs is not None:
     7551            operS = operSvarn.split('@')[0]
     7552            vnS = operSvarn.split('@')[1]
     7553            addopdims = Dadditionaldim[operSvarn]
     7554            addunits = Dadditionalunits[operSvarn]
     7555            Hvarns = DHadditionalvars[operS]
     7556            iHv = 0
     7557            if type(addopdims) == type('S') and addopdims == 'any':
     7558                for Hvarn in Hvarns:
     7559                    newvn = Hvarn + '_' + operS + '_' + vnS
     7560                    newvar = objofile.createVariable(newvn, 'f4')
     7561                    newvar[:] = addvs[iHv]
     7562                    Lattr = Hvarn + ' value for operation ' + operS + ' with variable ' + vnS
     7563                    basicvardef(newvar, newvn, Lattr, addunits)
     7564                    iHv = iHv + 1
     7565            else:
     7566                idim = 0
     7567                newvdims = []
     7568                for dimn in addopdims:
     7569                    newdimn = dimn + '_' + operS + '_' + vnS
     7570                    dsize = addvs[0].shape[idim]
     7571                    newdim = objofile.createDimension(newdimn,dsize)
     7572                    newvdims.append(newdimn)
     7573
     7574                for Hvarn in Hvarns:
     7575                    newvn = Hvarn + '_' + operS + '_' + vnS
     7576                    newvar = objofile.createVariable(newvn, 'f4', tuple(newvdims))
     7577                    newvar[:] = addvs[iHv]
     7578                    Lattr = Hvarn + ' value for operation ' + operS + ' with variable ' + vnS
     7579                    basicvardef(newvar, newvn, Lattr, addunits)
     7580                    iHv = iHv + 1
    74537581
    74547582# Global attributes
Note: See TracChangeset for help on using the changeset viewer.