Changeset 1351 in lmdz_wrf


Ignore:
Timestamp:
Nov 22, 2016, 5:23:21 PM (8 years ago)
Author:
lfita
Message:

Adding computation of 'WRFtime' and the attributes to all complementary variables

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/diagnostics.py

    r1163 r1351  
    867867parser.add_option("-f", "--netCDF_file", dest="ncfile", help="file to use", metavar="FILE")
    868868parser.add_option("-d", "--dimensions", dest="dimns", 
    869   help="[dimtn]@[dtvn],[dimzn]@[dzvn],[...,[dimxn]@[dxvn]], ',' list with the couples [dimDn]@[dDvn], [dimDn], name of the dimension D and name of the variable [dDvn] with the values of the dimension" + comboinf,
     869  help="[dimtn]@[dtvn],[dimzn]@[dzvn],[...,[dimxn]@[dxvn]], ',' list with the couples [dimDn]@[dDvn], [dimDn], name of the dimension D and name of the variable [dDvn] with the values of the dimension ('WRFtime', for WRF time copmutation)" + comboinf,
    870870  metavar="LABELS")
    871871parser.add_option("-v", "--variables", dest="varns",
     
    878878    #######
    879879availdiags = ['ACRAINTOT', 'accum', 'clt', 'cllmh', 'deaccum', 'LMDZrh', 'mslp',     \
    880   'OMEGAw', 'RAINTOT',   \
     880  'OMEGAw', 'RAINTOT',                                                               \
    881881  'rvors', 'td', 'turbulence', 'WRFgeop', 'WRFp', 'WRFrvors', 'ws', 'wds', 'wss',    \
    882882  'WRFheight', 'WRFua', 'WRFva']
     
    890890  'WRFpos', 'WRFprc', 'WRFprls', 'WRFrh', 'LMDZrh', 'LMDZrhs', 'WRFrhs', 'WRFrvors', \
    891891  'WRFt', 'WRFtime', 'WRFua', 'WRFva', 'WRFwds', 'WRFwss', 'WRFheight']
     892
     893NONchkvardims = ['WRFtime']
    892894
    893895ofile = 'diagnostics.nc'
     
    911913
    912914ncobj = NetCDFFile(opts.ncfile, 'r')
    913 
    914 # File creation
    915 newnc = NetCDFFile(ofile,'w')
    916 
    917 # dimensions
    918 dimvalues = dimns.split(',')
    919 dnames = []
    920 dvnames = []
    921 
    922 for dimval in dimvalues:
    923     dnames.append(dimval.split('@')[0])
    924     dvnames.append(dimval.split('@')[1])
    925 
    926 # diagnostics to compute
    927 diags = varns.split(',')
    928 Ndiags = len(diags)
    929915
    930916# Looking for specific variables that might be use in more than one diagnostic
     
    937923WRFpos_compute = False
    938924WRFtime_compute = False
     925
     926# File creation
     927newnc = NetCDFFile(ofile,'w')
     928
     929# dimensions
     930dimvalues = dimns.split(',')
     931dnames = []
     932dvnames = []
     933
     934for dimval in dimvalues:
     935    dn = dimval.split('@')[0]
     936    dnv = dimval.split('@')[1]
     937    dnames.append(dn)
     938    dvnames.append(dnv)
     939    # Is there any dimension-variable which should be computed?
     940    if dnv == 'WRFgeop':WRFgeop_compute = True
     941    if dnv == 'WRFp': WRFp_compute = True
     942    if dnv == 'WRFt': WRFt_compute = True
     943    if dnv == 'WRFrh': WRFrh_compute = True
     944    if dnv == 'WRFght': WRFght_compute = True
     945    if dnv == 'WRFdens': WRFdens_compute = True
     946    if dnv == 'WRFpos': WRFpos_compute = True
     947    if dnv == 'WRFtime': WRFtime_compute = True
     948
     949# diagnostics to compute
     950diags = varns.split(',')
     951Ndiags = len(diags)
    939952
    940953for idiag in range(Ndiags):
     
    960973        if gen.searchInlist(depvars, 'WRFtime'): WRFtime_compute = True
    961974
     975# Dictionary with the new computed variables to be able to add them
     976dictcompvars = {}
    962977if WRFgeop_compute:
    963978    print '  ' + main + ': Retrieving geopotential value from WRF as PH + PHB'
     
    965980    WRFgeop = ncobj.variables['PH'][:] + ncobj.variables['PHB'][:]
    966981
     982    # Attributes of the variable
     983    Vvals = gen.variables_values('WRFgreop')
     984    dictcompvars['WRFgeop'] = {'name': Vvals[0], 'standard_name': Vvals[1],          \
     985      'long_name': Vvals[4].replace('|',' '), 'units': Vvals[5]}
     986
    967987if WRFp_compute:
    968988    print '  ' + main + ': Retrieving pressure value from WRF as P + PB'
     
    970990    WRFp = ncobj.variables['P'][:] + ncobj.variables['PB'][:]
    971991
     992    # Attributes of the variable
     993    Vvals = gen.variables_values('WRFp')
     994    dictcompvars['WRFgeop'] = {'name': Vvals[0], 'standard_name': Vvals[1],          \
     995      'long_name': Vvals[4].replace('|',' '), 'units': Vvals[5]}
     996
    972997if WRFght_compute:
    973998    print '    ' + main + ': computing geopotential height from WRF as PH + PHB ...'
    974999    WRFght = ncobj.variables['PH'][:] + ncobj.variables['PHB'][:]
     1000
     1001    # Attributes of the variable
     1002    Vvals = gen.variables_values('WRFght')
     1003    dictcompvars['WRFgeop'] = {'name': Vvals[0], 'standard_name': Vvals[1],          \
     1004      'long_name': Vvals[4].replace('|',' '), 'units': Vvals[5]}
    9751005
    9761006if WRFrh_compute:
     
    9871017    WRFrh = qv/data2
    9881018
     1019    # Attributes of the variable
     1020    Vvals = gen.variables_values('WRFrh')
     1021    dictcompvars['WRFrh'] = {'name': Vvals[0], 'standard_name': Vvals[1],            \
     1022      'long_name': Vvals[4].replace('|',' '), 'units': Vvals[5]}
     1023
    9891024if WRFt_compute:
    9901025    print '    ' + main + ': computing temperature from WRF as inv_potT(T + 300) ...'
     
    9931028
    9941029    WRFt = (ncobj.variables['T'][:] + 300.)*(p/p0)**(2./7.)
     1030
     1031    # Attributes of the variable
     1032    Vvals = gen.variables_values('WRFt')
     1033    dictcompvars['WRFt'] = {'name': Vvals[0], 'standard_name': Vvals[1],             \
     1034      'long_name': Vvals[4].replace('|',' '), 'units': Vvals[5]}
    9951035
    9961036if WRFdens_compute:
     
    10161056            WRFdens[it,iz,:,:] = levval
    10171057            WRFdens[it,iz,:,:] = mu[it,:,:]*WRFdens[it,iz,:,:]/grav
     1058
     1059    # Attributes of the variable
     1060    Vvals = gen.variables_values('WRFdens')
     1061    dictcompvars['WRFdens'] = {'name': Vvals[0], 'standard_name': Vvals[1],          \
     1062      'long_name': Vvals[4].replace('|',' '), 'units': Vvals[5]}
    10181063
    10191064if WRFpos_compute:
     
    10751120
    10761121    tunits = tunitsval + ' since ' + refdateS
     1122
     1123    # Attributes of the variable
     1124    dictcompvars['WRFtime'] = {'name': 'time', 'standard_name': 'time',              \
     1125      'long_name': 'time', 'units': tunits, 'calendar': 'gregorian'}
    10771126
    10781127### ## #
     
    11631212        diagout, diagoutd, diagoutvd = Forcompute_cllmh(var0,var1,dnames,dvnames)
    11641213
     1214        # Removing the nonChecking variable-dimensions from the initial list
     1215        varsadd = []
     1216        for nonvd in NONchkvardims:
     1217            if gen.searchInlist(diagoutvd,nonvd): diagoutvd.remove(nonvd)
     1218            varsadd.append(nonvd)
     1219
    11651220        ncvar.insert_variable(ncobj, 'cll', diagout[0,:], diagoutd, diagoutvd, newnc)
    11661221        ncvar.insert_variable(ncobj, 'clm', diagout[1,:], diagoutd, diagoutvd, newnc)
     
    11721227        var0 = ncobj.variables[depvars]
    11731228        diagout, diagoutd, diagoutvd = Forcompute_clt(var0,dnames,dvnames)
     1229
     1230        # Removing the nonChecking variable-dimensions from the initial list
     1231        varsadd = []
     1232        for nonvd in NONchkvardims:
     1233            if gen.searchInlist(diagoutvd,nonvd): diagoutvd.remove(nonvd)
     1234            varsadd.append(nonvd)
     1235           
    11741236        ncvar.insert_variable(ncobj, 'clt', diagout, diagoutd, diagoutvd, newnc)
    11751237
     
    15981660
    15991661    newnc.sync()
     1662    # Adding that additional variables required to compute some diagnostics which
     1663    #   where not in the original file
     1664    for vadd in varsadd:
     1665        if not gen.searchInlist(newnc.variables.keys(),vadd):
     1666            attrs = dictcompvars[vadd]
     1667            vvn = attrs['name']
     1668            if not gen.searchInlist(newnc.variables.keys(), vvn):
     1669                iidvn = dvnames.index(vadd)
     1670                dnn = dnames[iidvn]
     1671                if vadd == 'WRFtime':
     1672                    dvarvals = WRFtime[:]
     1673                newvar = newnc.createVariable(vvn, 'f8', (dnn))
     1674                newvar[:] = dvarvals
     1675                for attn in attrs.keys():
     1676                    if attn != 'name':
     1677                        attv = attrs[attn]
     1678                        ncvar.set_attribute(newvar, attn, attv)
    16001679
    16011680#   end of diagnostics
Note: See TracChangeset for help on using the changeset viewer.