Changeset 2372 in lmdz_wrf


Ignore:
Timestamp:
Feb 26, 2019, 9:54:13 PM (6 years ago)
Author:
lfita
Message:

Adding new version of 'pinterp' using `range_slicing' !!

Location:
trunk/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2371 r2372  
    1522715227
    1522815228    return loops1D
     15229#print Nloops_1D(['t', 'z', 'y', 'x'], [56, 39, 99, 99], {'y': 99, 't': 56})
     15230#quit()
    1522915231
    1523015232def range_slicing(dimns, dimvs, intdims):
     
    1530915311
    1531015312    # Getting all the combinations of the interval slices
    15311     intcombs = Nloops_1D(intloopvals)
    15312     Tslicesize = intcombs.shape[0]
     15313    if np.all(np.array(intloopvals) == 0):
     15314        print warnmsg
     15315        print '  ' + fname + ': not actual intervals for any dimension !!'
     15316        for dimn in intdims.keys():
     15317            print '  intervals ', dimn, ':', intslices[dimn]
     15318        print '    creation of empty intervals ...'
     15319        Nintdims = len(list(intdims.keys()))
     15320        intcombs = [list(np.zeros((Nintdims), dtype=int))]
     15321        for dimn in dimns:
     15322            if intdims.has_key(dimn):
     15323                intslices[dimn] = [0, dims[dimn]]
     15324        Tslicesize = 1
     15325    else:
     15326        intcombs = Nloops_1D(intloopvals)
     15327        Tslicesize = intcombs.shape[0]
    1531315328
    1531415329    #print '  ' + fname + ': Total number of slices to provide:', Tslicesize
  • trunk/tools/nc_var_tools.py

    r2367 r2372  
    1880918809    print '  ' + fname + ' interpolating at pressure levels _______'
    1881018810    print '     ', interplevs
     18811    usaid = False
     18812    uersaid = False
     18813    vsaid = False
     18814    versaid = False
     18815    tsaid = False
     18816    ysaid = False
     18817    wsaid = False
     18818
     18819    Nplevs = len(interplevs)
     18820    for vn in varns:
     18821        print "      '" + vn + "' ..."
     18822        newvarattr = {}
     18823        varin = None
     18824
     18825        # FORCING the splitting
     18826#        # There is a memroy issue, thus is necessary to split the matrix...
     18827#        if np.prod(list(temp.shape))*8 > 3*gen.oneGB:
     18828#            #dimtfrac = np.min([5,dimt])
     18829#            if not tsaid:
     18830#                print warnmsg
     18831#                print '  ' + fname + ': domain to interpolate:', temp.shape,         \
     18832#                  'too big!!'
     18833#                print '   p-interpolation will be done by time-slices of', dimtfrac, \
     18834#                  'time-steps'
     18835#                print range(0,dimtfrac*int(dimt/dimtfrac),dimtfrac)
     18836#        else:
     18837#            dimtfrac = dimt
     18838#        if np.prod([dimz, dimx, dimy])*8 > gen.oneGB:
     18839#            #dimyfrac = 50
     18840#            if not ysaid:
     18841#                print warnmsg
     18842#                print '  ' + fname + ': variable to interpolate:', temp.shape,       \
     18843#                  'too big!!'
     18844#                print '    p-interpolation will be done by yaxis-slices of',         \
     18845#                  dimyfrac,' grid-points'
     18846#                print range(0,dimyfrac*int(dimy/dimyfrac),dimyfrac)
     18847#        else:
     18848#            dimyfrac = dimy
     18849        if not tsaid:
     18850            print warnmsg
     18851            print '   p-interpolation will be done by time-slices of', dimtfrac,     \
     18852              'time-steps'
     18853            print range(0,dimtfrac*int(dimt/dimtfrac),dimtfrac)
     18854        if not ysaid:
     18855            print warnmsg
     18856            print '    p-interpolation will be done by yaxis-slices of',             \
     18857              dimyfrac,' grid-points'
     18858            print range(0,dimyfrac*int(dimy/dimyfrac),dimyfrac)
     18859
     18860        # Getting slices
     18861        tzslices, tzinds = gen.range_slicing(['t', 'z', 'y', 'x'], list(temp.shape), \
     18862          {'t': dimtfrac, 'y': dimyfrac})
     18863
     18864        varinterp = np.zeros(tuple([dimx,dimyfrac,len(interplevs),dimtfrac]), dtype=np.float)
     18865        TOTslices = len(tzslices)
     18866
     18867        # Splitting
     18868        for islc in range(TOTslices):
     18869            slcv = tzslices[islc]
     18870            slci = tzinds[islc]
     18871            tini = slci[0,0]
     18872            tend = slci[0,1]
     18873            yini = slci[2,0]
     18874            yend = slci[2,1]
     18875
     18876            Ndy = len(range(yini,yend))
     18877            Ndt = len(range(tini,tend))
     18878            #Slice tuple
     18879            tslc = tuple([slice(tini,tend), slice(0,dimz), slice(yini,yend),         \
     18880              slice(0,dimx)])
     18881            tslcx1 = tuple([slice(tini,tend), slice(0,dimz), slice(yini,yend),       \
     18882              slice(1,dimx+1)])
     18883            tslcy1= tuple([slice(tini,tend), slice(0,dimz), slice(yini+1,yend+1),    \
     18884              slice(0,dimx)])
     18885
     18886            if gen.searchInlist(notCHK, vn):
     18887                if vn == 'WRFght':
     18888                    varin = geop[tslc]
     18889                    isgeop = True
     18890                    varattrs = gen.variables_values('WRFght')
     18891                    CFvn = varattrs[0]
     18892                    newvarattr['standard_name'] = varattrs[1]
     18893                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     18894                    newvarattr['units'] = varattrs[5]
     18895                elif vn == 'WRFhus':
     18896                    # specific humidity
     18897                    varin = qv[tslc]/(1.+qv[tslc])
     18898                    isgeop = False
     18899                    varattrs = gen.variables_values('WRFhus')
     18900                    CFvn = varattrs[0]
     18901                    newvarattr['standard_name'] = varattrs[1]
     18902                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     18903                    newvarattr['units'] = varattrs[5]
     18904                elif vn == 'WRFt':
     18905                    varin = temp[tslc]
     18906                    isgeop = False
     18907                    varattrs = gen.variables_values('WRFt')
     18908                    CFvn = varattrs[0]
     18909                    newvarattr['standard_name'] = varattrs[1]
     18910                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     18911                    newvarattr['units'] = varattrs[5]
     18912                elif vn == 'WRFu':
     18913                    ovarin = onc.variables['U']
     18914                    if not usaid:
     18915                        print infmsg
     18916                        print '  ' + fname + ': De-staggering x-wind variable !!'
     18917                        print '    from:',ovarin[tini:tend,:,yini:yend,0:dimx].shape,\
     18918                          'to', (Ndt,dimz,Ndy,dimx)
     18919                        usaid = True
     18920                    if isFR64:
     18921                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     18922                    else:
     18923                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     18924                    varin[:] = 0.5*(ovarin[tslc] + ovarin[tslcx1])
     18925                    isgeop = False
     18926                    varattrs = gen.variables_values('ua')
     18927                    CFvn = varattrs[0]
     18928                    newvarattr['standard_name'] = varattrs[1]
     18929                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     18930                    newvarattr['units'] = varattrs[5]
     18931                elif vn == 'WRFuer':
     18932                    ovarin = onc.variables['U']
     18933                    ovarin2 = onc.variables['V']
     18934                    # Earth-rotating
     18935                    osina =  onc.variables['SINALPHA']
     18936                    ocosa =  onc.variables['COSALPHA']
     18937                    if not uersaid:
     18938                        print infmsg
     18939                        print '  ' + fname + ': De-staggering x-wind variable & ' +  \
     18940                          'Earth rotating!!'
     18941                        print '    from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx)
     18942                        uersaid = True
     18943                    if isFR64:
     18944                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     18945                        varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     18946                        varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     18947                    else:
     18948                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     18949                        varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     18950                        varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     18951                    for it in range(tini,tend):
     18952                        varin0[it-tini,:,:,:]=0.5*(ovarin[it,:,yini:yend,0:dimx] +   \
     18953                          ovarin[it,:,yini:yend,1:dimx+1])
     18954                        varin02[it-tini,:,:,:] = 0.5*(ovarin2[it,:,yini:yend,:] +    \
     18955                          ovarin2[it,:,yini+1:yend+1,:])
     18956                    for iz in range(dimz):
     18957                        varin[:,iz,:,:] = varin0[:,iz,:,:]*                          \
     18958                          ocosa[tini:tend,yini:yend,:] - varin02[:,iz,:,:]*          \
     18959                          osina[tini:tend,yini:yend,:]
     18960                    isgeop = False
     18961                    varattrs = gen.variables_values('uer')
     18962                    CFvn = varattrs[0]
     18963                    newvarattr['standard_name'] = varattrs[1]
     18964                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     18965                    newvarattr['units'] = varattrs[5]
     18966                elif vn == 'WRFv':
     18967                    ovarin = onc.variables['V']
     18968                    if not vsaid:
     18969                        print infmsg
     18970                        print '  ' + fname + ': De-staggering y-wind variable !!'
     18971                        print '    from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx)
     18972                        vsaid = True
     18973                    if isFR64:
     18974                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     18975                    else:
     18976                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     18977                    # Not pro, but less memory problems!
     18978                    for it in range(tini,tend):
     18979                        varin[it-tini,:,:,:] = 0.5*(ovarin[it,:,yini:yend,:] +       \
     18980                          ovarin[it,:,yini+1:yend+1,:])
     18981                    isgeop = False
     18982                    varattrs = gen.variables_values('va')
     18983                    CFvn = varattrs[0]
     18984                    newvarattr['standard_name'] = varattrs[1]
     18985                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     18986                    newvarattr['units'] = varattrs[5]
     18987                elif vn == 'WRFver':
     18988                    ovarin = onc.variables['U']
     18989                    ovarin2 = onc.variables['V']
     18990                    # Earth-rotating
     18991                    osina =  onc.variables['SINALPHA']
     18992                    ocosa =  onc.variables['COSALPHA']
     18993                    if not versaid:
     18994                        print infmsg
     18995                        print '  ' + fname + ': De-staggering y-wind variable & ' +  \
     18996                          'Earth rotating!!'
     18997                        print '    from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx)
     18998                        versaid = True
     18999                    if isFR64:
     19000                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     19001                        varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     19002                        varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     19003                    else:
     19004                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     19005                        varin0 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     19006                        varin02 = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     19007                    # Not pro, but less memory problems!
     19008                    for it in range(tini,tend):
     19009                        varin0[it-tini,:,:,:]=0.5*(ovarin[it,:,yini:yend,0:dimx] +   \
     19010                          ovarin[it,:,yini:yend,1:dimx+1])
     19011                        varin02[it-tini,:,:,:] = 0.5*(ovarin2[it,:,yini:yend,:] +    \
     19012                           ovarin2[it,:,yini+1:yend+1,:])
     19013                    for iz in range(dimz):
     19014                        varin[:,iz,:,:] = varin0[:,iz,:,:]*                          \
     19015                          osina[tini:tend,yini:yend,:] + varin02[:,iz,:,:]*          \
     19016                          ocosa[tini:tend,yini:yend,:]
     19017                    isgeop = False
     19018                    varattrs = gen.variables_values('ver')
     19019                    CFvn = varattrs[0]
     19020                    newvarattr['standard_name'] = varattrs[1]
     19021                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     19022                    newvarattr['units'] = varattrs[5]
     19023                elif vn == 'WRFw':
     19024                    ovarin = onc.variables['W']
     19025                    if not wsaid:
     19026                        print infmsg
     19027                        print '  ' + fname + ': De-staggering z-wind variable !!'
     19028                        print '    from:', ovarin.shape, 'to', (dimt,dimz,dimy,dimx)
     19029                        wsaid = True
     19030                    if isFR64:
     19031                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float64)
     19032                    else:
     19033                        varin = np.zeros((Ndt,dimz,Ndy,dimx), dtype=np.float)
     19034                    # Not pro, but less memory problems!
     19035                    for it in range(tini,tend):
     19036                        varin[it-tini,:,:,:]=0.5*(ovarin[it,0:dimz,yini:yend,:] +    \
     19037                          ovarin[it,1:dimz+1,yini:yend,:])
     19038                    isgeop = False
     19039                    varattrs = gen.variables_values('wa')
     19040                    CFvn = varattrs[0]
     19041                    newvarattr['standard_name'] = varattrs[1]
     19042                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     19043                    newvarattr['units'] = varattrs[5]
     19044                elif vn == 'pres':
     19045                    varin = pres[tslc]
     19046                    isgeop = False
     19047                    varattrs = gen.variables_values('pres')
     19048                    CFvn = varattrs[0]
     19049                    newvarattr['standard_name'] = varattrs[1]
     19050                    newvarattr['long_name'] = varattrs[4].replace('|',' ')
     19051                    newvarattr['units'] = varattrs[5]
     19052#            elif not gen.searchInlist(MODvarrequired, vn) or vn == 'QVAPOR':
     19053            else:
     19054                ovarin = onc.variables[vn]
     19055                varinf = variable_inf(ovarin)
     19056                varin = ovarin[tslc]
     19057                # de-staggering
     19058                #print '  Lluis:', ovarin.dimensions,'unsDIM',  unstaggerDIM, 'CFdim:', CFdimvs
     19059                #if gen.searchInlist(ovarin.dimensions, unstaggerDIM):
     19060                #    varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),MODdimvs)
     19061                #else:
     19062                #    varin = gen.stagger_unstagger(varin,list(ovarin.dimensions),CFdimvs)
     19063                isgeop = False
     19064                if vn == 'z': isgeop = True
     19065                varattrs = gen.variables_values(vn)
     19066                CFvn = varattrs[0]
     19067                for attrn in ovarin.ncattrs():
     19068                    attrv = ovarin.getncattr(attrn)
     19069                    newvarattr[attrn] = attrv
     19070                newvarattr['standard_name'] = varattrs[1]
     19071                newvarattr['long_name'] = varattrs[4].replace('|',' ')
     19072
     19073            if varin is not None:
     19074                if not gen.searchInlist(onewnc.variables,CFvn):
     19075                    newvar = onewnc.createVariable(CFvn, 'f4', tuple(newMODdims),    \
     19076                      fill_value=gen.fillValueF)
     19077                varint = varin.transpose()
     19078                prest = pres[tslc].transpose()
     19079                psfct = psfc[tini:tend,yini:yend,:].transpose()
     19080                hgtt = hgt[yini:yend,:].transpose()
     19081                tempt = temp[tslc].transpose()
     19082                qvt = qv[tslc].transpose()
     19083                varinterp = fin.module_forinterpolate.interp( data_in=varint,        \
     19084                  pres_field=prest, interp_levels=interplevs, psfc=psfct,            \
     19085                  ter=hgtt, tk=tempt, qv=qvt, linlog=linloginterp,                   \
     19086                  extrapolate=extrap, geopt=isgeop, missing=gen.fillValueF,          \
     19087                  ix=dimx, iy=Ndy, iz=dimz, it=Ndt, num_metgrid_levels=Nplevs)
     19088                newvar[tini:tend,:,yini:yend,:] = varinterp.transpose()
     19089                onewnc.sync()
     19090
     19091        for attrn in newvarattr.keys():
     19092            if attrn != '_FillValue':
     19093                attr = set_attribute(newvar, attrn, newvarattr[attrn])
     19094        onewnc.sync()
     19095
     19096    # Global attributes
     19097    add_global_PyNCplot(onewnc, 'nc_var_tools.py', fname, '2.0')
     19098
     19099    onewnc.setncattr('oringal', 'Interpolation using p_interp.F90 subroutines')
     19100    newattr = set_attributek(onewnc, 'original_subroutines_author', unicode('Cindy '+\
     19101      'Bruy' + unichr(232) + 're'), 'U')
     19102    onewnc.setncattr('original_subroutines_date', 'November 2007')
     19103
     19104    for attrn in onc.ncattrs():
     19105        attrv = onc.getncattr(attrn)
     19106        attr = set_attribute(onewnc, attrn, attrv)   
     19107
     19108    onc.close()
     19109
     19110    onewnc.sync()
     19111    onewnc.close()
     19112
     19113    print fname + ": successfull written of '" + ofile + "' !!"
     19114
     19115    return
     19116
     19117
     19118def pinterp_old(values, ncfile, variables):
     19119    """ Function to vertically interpolate using subroutines from the p_interp.F90 NCAR program
     19120      Using fortran codes: module_generic.F90
     19121      values= [interplevs],[linloginterp],[extrap],[dimtfrac],[dimyfrac]
     19122        [interplevs]: ':' separated list of pressure values to interpolate [Pa] (decreassing in Pa)
     19123        [linloginterp]: kind of linear interpolation
     19124          1: linear interp in pressure
     19125          2: linear interp in ln(pressure)
     19126        [extrap]: whether to set to missing value below/above model ground and top (0), or extrapolate (1)
     19127        [dimtfrac]: fractions of time to interpolate to avoid memory issues ('auto' for 5)
     19128        [dimyfrac]: fractions of y-axis to interpolate to avoid memory issues ('auto' for 50)
     19129      ncfile= WRF file to use
     19130      variables = ',' separated list of names of variables to interpolate ('all', fo all 4D-atmospheric variables)
     19131        'WRFght': for WRF geopotential height
     19132        'WRFrh': for WRF relative humidity
     19133        'WRFt': for WRF temperature
     19134        'WRFu': for WRF x-wind de-staggered
     19135        'WRFuer': for WRF x-wind de-staggered Earth-rotated
     19136        'WRFv': for WRF y-wind de-staggered
     19137        'WRFver': for WRF y-wind de-staggered Earth-rotated
     19138        'WRFw': for WRF z-wind de-staggered
     19139    """
     19140    import module_ForInt as fin
     19141    fname = 'pinterp'
     19142
     19143    if values == 'h':
     19144        print fname + '_____________________________________________________________'
     19145        print pinterp.__doc__
     19146        quit()
     19147
     19148    arguments = '[interplevs],[linloginterp],[extrap],[dimtfrac],[dimyfrac]'
     19149    gen.check_arguments(fname, values, arguments, ',')
     19150
     19151    if isFR64:
     19152        interplevs = np.array(values.split(',')[0].split(':'), dtype=np.float64)
     19153    else:
     19154        interplevs = np.array(values.split(',')[0].split(':'), dtype=np.float)
     19155    linloginterp = np.int32(values.split(',')[1])
     19156    extrap = np.int32(values.split(',')[2])
     19157    dimtfrac = gen.auto_val(values.split(',')[3], 5)
     19158    dimyfrac = gen.auto_val(values.split(',')[4], 50)
     19159
     19160    ofile = 'pinterp.nc'
     19161
     19162    CFdims = ['time', 'pres', 'lat', 'lon']
     19163    # Just in case there are weird units, attribute value will be taken from input file
     19164    CFattrs = ['standard_name', 'long_name']
     19165
     19166    onc = NetCDFFile(ncfile, 'r')
     19167
     19168    # Gessing orgin of the file
     19169    dimsinfile = onc.dimensions.keys()
     19170    varsinfile = onc.variables.keys()
     19171 
     19172    if gen.searchInlist(dimsinfile,'bottom_top'):
     19173        modname = 'WRF'
     19174        print warnmsg
     19175        print '  ' + fname + ": gessing that file '" + ncfile + "' comes from '" +   \
     19176          modname + "' !!"
     19177
     19178        # Variables to interpolate
     19179        MODdims = ['Time', 'bottom_top', 'south_north', 'west_east']
     19180        newMODdims = ['Time', 'pres', 'south_north', 'west_east']
     19181        notCHK = ['WRFght', 'WRFhus', 'WRFrh', 'WRFt', 'WRFu', 'WRFuer', 'WRFv',     \
     19182          'WRFver', 'WRFw']
     19183        MODvarrequired = ['P', 'PB', 'PSFC', 'PH', 'PHB', 'HGT', 'T', 'QVAPOR',      \
     19184          'XLONG', 'XLAT', 'Times']
     19185        MODvardims = ['XLONG', 'XLAT', 'Times']
     19186    elif gen.searchInlist(dimsinfile,'presnivs'):
     19187        modname = 'LMDZ'
     19188        print warnmsg
     19189        print '  ' + fname + ": gessing that file '" + ncfile + "' comes from '" +   \
     19190          modname + "' !!"
     19191
     19192        # Variables to interpolate
     19193        MODdims = ['time_counter', 'presnivs', 'lat', 'lon']
     19194        newMODdims = ['time_counter', 'pres', 'lat', 'lon']
     19195        notCHK = []
     19196        MODvarrequired = ['pres', 'psol', 'geop', 'phis', 'temp', 'ovap', 'lon',     \
     19197          'lat', 'time_counter']
     19198        MODvardims = ['lon', 'lat', 'time_counter']
     19199    elif gen.searchInlist(dimsinfile,'pres') and gen.searchInlist(varsinfile,        \
     19200      'psol'):
     19201        modname = 'cfLMDZ'
     19202        print warnmsg
     19203        print '  ' + fname + ": gessing that file '" + ncfile + "' comes from '" +   \
     19204          modname + "' ('pres, time_counter' variables renamed as 'p, time') !!"
     19205
     19206        # Variables to interpolate
     19207        MODdims = ['time', 'pres', 'lat', 'lon']
     19208        newMODdims = ['time', 'pres', 'lat', 'lon']
     19209        notCHK = []
     19210        MODvarrequired = ['p', 'psol', 'geop', 'phis', 'temp', 'ovap', 'lon',     \
     19211          'lat', 'time']
     19212        MODvardims = ['lon', 'lat', 'time']
     19213    else:
     19214        modname = 'CF'
     19215        print warnmsg
     19216        print '  ' + fname + ": gessing that file '" + ncfile + "' comes from '" +   \
     19217          modname + "' !!"
     19218
     19219        # Variables to interpolate
     19220        MODdims = ['time', 'pres', 'lat', 'lon']
     19221        newMODdims = ['time', 'pres', 'lat', 'lon']
     19222        notCHK = []
     19223        MODvarrequired = ['p', 'ps', 'z', 'orog', 'ta', 'hus', 'lon', 'lat', 'time']
     19224        MODvardims = ['lon', 'lat', 'time']
     19225
     19226    varns = gen.str_list(variables, ',')
     19227    if variables == 'all':
     19228        varns = []
     19229        for vn in onc.variables:
     19230            ovn = onc.variables[vn]
     19231            coincdims = set(WRFdims) & set(ovn.dimensions)
     19232           
     19233            if len(MODdims) == len(ovn.dimensions) and len(coincdims) == len(MODdims):
     19234                varns.append(vn)
     19235        varns = varns + notCHK
     19236       
     19237    for vn in varns:
     19238        if not gen.searchInlist(onc.variables, vn) and                               \
     19239          not gen.searchInlist(notCHK, vn):
     19240            print errormsg
     19241            print '  ' + fname + "': file '" + ncfile + "' does not have variable '"+\
     19242              vn + "' !!"
     19243            quit(-1)
     19244
     19245    # looking for model required variables
     19246    for var in MODvarrequired:
     19247        if not gen.searchInlist(onc.variables, var):
     19248            print errormsg
     19249            print '  ' + fname + ": file '" + ncfile + "' does not have required " + \
     19250              "variable '" + var + "' !!"
     19251            quit(-1)
     19252
     19253# Computing necessary variables
     19254    # pressure
     19255    if modname == 'WRF':
     19256        ovar1 = onc.variables['P']
     19257        ovar2 = onc.variables['PB']
     19258
     19259        pres0 = ovar1[:] + ovar2[:]
     19260        if isFR64:
     19261            pres = pres0.astype('float64')
     19262        else:
     19263            pres = pres0.copy()
     19264
     19265        dimx = pres.shape[3]
     19266        dimy = pres.shape[2]
     19267        dimz = pres.shape[1]
     19268        dimt = pres.shape[0]
     19269        MODdimvs = {'Time':dimt, 'bottom_top':dimz, 'south_north':dimy,              \
     19270          'west_east': dimx}
     19271        CFdimvs = {'time': dimt, 'bottom_top': dimz, 'lat': dimy, 'lon': dimx}
     19272        unstaggerDIM = 'west_east'
     19273    elif modname == 'LMDZ':
     19274        ovar1 = onc.variables['pres']
     19275        if isFR64:
     19276            pres = ovar1[:].astype('float64')
     19277        else:
     19278            pres = ovar1[:]
     19279        dimx = pres.shape[3]
     19280        dimy = pres.shape[2]
     19281        dimz = pres.shape[1]
     19282        dimt = pres.shape[0]
     19283        MODdimvs = {'time_counter':dimt, 'presnivs':dimz, 'lat':dimy, 'lon': dimx}
     19284        CFdimvs = {'time': dimt, 'presnivs': dimz, 'lat': dimy, 'lon': dimx}
     19285        unstaggerDIM = 'lon'
     19286    elif modname == 'cfLMDZ':
     19287        ovar1 = onc.variables['p']
     19288        if isFR64:
     19289            pres = ovar1[:].astype('float64')
     19290        else:
     19291            pres = ovar1[:]
     19292        dimx = pres.shape[3]
     19293        dimy = pres.shape[2]
     19294        dimz = pres.shape[1]
     19295        dimt = pres.shape[0]
     19296        MODdimvs = {'time':dimt, 'pres':dimz, 'lat':dimy, 'lon': dimx}
     19297        CFdimvs = {'time': dimt, 'pres': dimz, 'lat': dimy, 'lon': dimx}
     19298        unstaggerDIM = 'lon'
     19299    else:
     19300        ovar1 = onc.variables['p']
     19301        if isFR64:
     19302            pres = ovar1[:].astype('float64')
     19303        else:
     19304            pres = ovar1[:]
     19305        dimx = pres.shape[3]
     19306        dimy = pres.shape[2]
     19307        dimz = pres.shape[1]
     19308        dimt = pres.shape[0]
     19309        MODdimvs = {'time':dimt, 'pres':dimz, 'lat':dimy, 'lon': dimx}
     19310        CFdimvs = {'time': dimt, 'pres': dimz, 'lat': dimy, 'lon': dimx}
     19311        unstaggerDIM = 'lon'
     19312
     19313    print '  ' + fname + ': CF dimension lengths:', CFdimvs
     19314
     19315    # sfc pressure
     19316    if modname == 'WRF':
     19317        ovar1 = onc.variables['PSFC']
     19318        if isFR64:
     19319            psfc = ovar1[:].astype('float64')
     19320        else:
     19321            psfc = ovar1[:]
     19322    elif modname == 'LMDZ' or modname == 'cfLMDZ':
     19323        ovar1 = onc.variables['psol']
     19324        if isFR64:
     19325            psfc = ovar1[:].astype('float64')
     19326        else:
     19327            psfc = ovar1[:]
     19328    else:
     19329        ovar1 = onc.variables['ps']
     19330        if isFR64:
     19331            psfc = ovar1[:].astype('float64')
     19332        else:
     19333            psfc = ovar1[:]
     19334
     19335    # geopotential height
     19336    if modname == 'WRF':
     19337        ovar1 = onc.variables['PH']
     19338        ovar2 = onc.variables['PHB']
     19339        geop0 = ovar1[:] + ovar2[:]
     19340        unstg = list(geop0.shape)
     19341        unstg[1] = unstg[1] - 1
     19342        if isFR64:
     19343            geop = np.zeros(tuple(unstg), dtype=np.float64)
     19344            geop = 0.5*(geop0[:,1:dimz+1,:,:] + geop0[:,0:dimz,:,:]).astype('float64')
     19345        else:
     19346            geop = np.zeros(tuple(unstg), dtype=np.float)
     19347            geop = 0.5*(geop0[:,1:dimz+1,:,:] + geop0[:,0:dimz,:,:])
     19348    elif modname == 'LMDZ' or modname == 'cfLMDZ':
     19349        ovar1 = onc.variables['geop']
     19350        if isFR64:
     19351            geop = ovar1[:].astype('float64')
     19352        else:
     19353            geop = ovar1[:]
     19354    else:
     19355        ovar1 = onc.variables['z']
     19356        if isFR64:
     19357            geop = ovar1[:].astype('float64')
     19358        else:
     19359            geop = ovar1[:]
     19360
     19361    # terrain height
     19362    if modname == 'WRF':
     19363        ovar1 = onc.variables['HGT']
     19364        if isFR64:
     19365            hgt = ovar1[0,:,:].astype('float64')
     19366        else:
     19367            hgt = ovar1[0,:,:]
     19368    elif modname == 'LMDZ' or modname == 'cfLMDZ':
     19369        grav = 9.81
     19370        ovar1 = onc.variables['phis']
     19371        if isFR64:
     19372            hgt = (ovar1[0,:,:]/grav).astype('float64')
     19373        else:
     19374            hgt = (ovar1[0,:,:]/grav)
     19375    else:
     19376        ovar1 = onc.variables['orog']
     19377        if isFR64:
     19378            hgt = ovar1[:].astype('float64')
     19379        else:
     19380            hgt = ovar1[:]
     19381
     19382    # water vapour mixing ratio
     19383    if modname == 'WRF':
     19384        ovar1 = onc.variables['QVAPOR']
     19385        if isFR64:
     19386            qv = ovar1[:].astype('float64')
     19387        else:
     19388            qv = ovar1[:]
     19389    elif modname == 'LMDZ' or modname == 'cfLMDZ':
     19390        ovar1 = onc.variables['ovap']
     19391        if isFR64:
     19392            qv = ovar1[:].astype('float64')
     19393        else:
     19394            qv = ovar1[:]
     19395    else:
     19396        ovar1 = onc.variables['hus']
     19397        if isFR64:
     19398            qv = ovar1[:].astype('float64')
     19399        else:
     19400            qv = ovar1[:]
     19401
     19402    # temperature
     19403    if modname == 'WRF':
     19404        if isFR64:
     19405            Rd = np.float64(287.04)
     19406            Cp = np.float64(7.*Rd/2.)
     19407            RCP = np.float64(Rd/Cp)
     19408            p0 = np.float64(100000.)
     19409        else:
     19410            Rd = 287.04
     19411            Cp = 7.*Rd/2.
     19412            RCP = Rd/Cp
     19413            p0 = 100000.
     19414        ovar10 = onc.variables['T']
     19415        var10 = ovar10[:]
     19416        if isFR64:
     19417            ovar1 = (var10).astype('float64')
     19418        else:
     19419            ovar1 = var10[:]
     19420        temp0 = (ovar1[:]+300.)*(pres[:]/p0)**RCP
     19421        if isFR64:
     19422            temp = temp0.astype('float64')
     19423        else:
     19424            temp = temp0.copy()
     19425    elif modname == 'LMDZ' or modname == 'cfLMDZ':
     19426        ovar1 = onc.variables['temp']
     19427        if isFR64:
     19428            temp = ovar1[:].astype('float64')
     19429        else:
     19430            temp = ovar1[:]
     19431    else:
     19432        ovar1 = onc.variables['ta']
     19433        if isFR64:
     19434            temp = ovar1[:].astype('float64')
     19435        else:
     19436            temp = ovar1[:]
     19437
     19438    print '  ' + fname + ": Opening output file 'pinterp.nc'"
     19439    onewnc = NetCDFFile(ofile, 'w')
     19440# Creation of dimensions
     19441    if modname == 'WRF':
     19442        newdim = onewnc.createDimension('west_east', dimx)
     19443        newdim = onewnc.createDimension('south_north', dimy)
     19444        newdim = onewnc.createDimension('pres', len(interplevs))
     19445        newdim = onewnc.createDimension('Time', None)
     19446    elif modname == 'LMDZ':
     19447        newdim = onewnc.createDimension('lon', dimx)
     19448        newdim = onewnc.createDimension('lat', dimy)
     19449        newdim = onewnc.createDimension('pres', len(interplevs))
     19450        newdim = onewnc.createDimension('time_counter', None)
     19451    else:
     19452        newdim = onewnc.createDimension('lon', dimx)
     19453        newdim = onewnc.createDimension('lat', dimy)
     19454        newdim = onewnc.createDimension('pres', len(interplevs))
     19455        newdim = onewnc.createDimension('time', None)
     19456
     19457# Creation of variable dimensions
     19458    for var in MODvardims:
     19459        ovar = onc.variables[var]
     19460        varinf = variable_inf(ovar)
     19461        newvard = []
     19462        for vd in varinf.dimns:
     19463            if not gen.searchInlist(onewnc.dimensions, vd) and                       \
     19464              not gen.searchInlist(CFdims, vd):
     19465                newdim = onewnc.createDimension(vd, len(onc.dimensions[vd]))
     19466            if gen.searchInlist(CFdims,vd):
     19467                icfdim = CFdims.index(vd)
     19468                newvard.append(MODdims[icfdim])
     19469            else:
     19470                newvard.append(vd)
     19471
     19472        newvar = onewnc.createVariable(var, nctype(varinf.dtype),                    \
     19473          tuple(newvard))
     19474        newvar[:] = ovar[:]
     19475        for attrn in ovar.ncattrs():
     19476            attrv = ovar.getncattr(attrn)
     19477            attr = set_attribute(newvar, attrn, attrv)   
     19478
     19479    # Creation of pressure variable dimension
     19480    newvar = onewnc.createVariable('pres', 'f8', ('pres'))
     19481    newvar[:] = interplevs[:]
     19482    basicvardef(newvar, 'pressure', 'Pressure', 'Pa')
     19483    attr = set_attribute(newvar, 'positive', 'down')
     19484    attr = set_attribute(newvar, 'axis', 'Z')
     19485
     19486    print '  ' + fname + ' interpolating at pressure levels _______'
     19487    print '     ', interplevs
    1881119488    tsaid = False
    1881219489    ysaid = False
  • trunk/tools/variables_values.dat

    r2365 r2372  
    545545Zonal wind, ua, eastward_wind, -30., 30., eastward|wind, ms-1, seismic, $ua$, ua
    546546LVITU, ua, eastward_wind, -30., 30., eastward|wind, ms-1, seismic, $ua$, ua
     547uer, uer, eastward_wind_er, -30., 30., Earth|rotated|eastward|wind, ms-1, seismic, $uer$, uer
    547548uas, uas, eastward_wind, -30., 30., eastward|10m|wind, ms-1, seismic, $uas$, uas
    548549UAS, uas, eastward_wind, -30., 30., eastward|10m|wind, ms-1, seismic, $uas$, uas
     
    558559Meridional wind, va, northward_wind, -30., 30., northward|wind, ms-1, seismic, $va$, va
    559560LVITV, va, northward_wind, -30., 30., northward|wind, ms-1, seismic, $va$, va
     561ver, ver, northward_wind_er, -30., 30., Earth|rotated|northward|wind, ms-1, seismic, $ver$, ver
    560562valley, valley, valley_point, 0., 1., valley|grd|point, 1, Binary, $valley$, valley
    561563vas, vas, northward_wind, -30., 30., northward|10m|wind, ms-1, seismic, $vas$, vas
Note: See TracChangeset for help on using the changeset viewer.