source: lmdz_wrf/trunk/tools/interpolate_WRF_vertical_lev.py @ 1292

Last change on this file since 1292 was 181, checked in by lfita, 10 years ago

Adding tool to interpolate WRF vertical levels among the standard initial ones

(17 = 7 + 10) priorityzing lower levels

File size: 3.4 KB
Line 
1# Python script to get vertical levels from original 17 WRF levels
2# From WRFV3.3/WRFV3/dyn_em/module_initialize_real.F#compute_eta
3import numpy as np
4import os
5import re
6from optparse import OptionParser
7
8main = 'interpolate_WRF_vertical_lev'
9errmsg = 'ERROR -- errror -- ERROR -- error'
10warnmsg = 'WARNING -- warning -- WARNING -- warning'
11
12def numVector_String(vec,char):
13    """ Function to transform a vector of numbers to a single string [char] separated
14    numVector_String(vec,char)
15      vec= vector with the numerical values
16      char= single character to split the values
17    >>> print numVector_String(np.arange(10),' ')
18    0 1 2 3 4 5 6 7 8 9
19    """
20    fname = 'numVector_String'
21
22    if vec == 'h':
23        print fname + '_____________________________________________________________'
24        print numVector_String.__doc__
25        quit()
26
27    Nvals = len(vec)
28
29    string=''
30    for i in range(Nvals):
31        if i == 0:
32            string = str(vec[i])
33        else:
34            string = string + char + str(vec[i])
35
36    return string
37
38####### ###### ##### #### #### ## #
39
40parser = OptionParser()
41parser.add_option("-n", "--Level_Numbers", dest="nlevels",
42                  help="number of levels to interpolate", metavar="VALUE")
43(opts, args) = parser.parse_args()
44
45#######    #######
46## MAIN
47    #######
48
49Nwrflev = 17
50znw_prac = np.zeros((Nwrflev), dtype=np.float)
51znw_prac[:] = [ 1.000, 0.993, 0.983, 0.970, 0.954, 0.934, 0.909, 0.88, 0.8, 0.7,     \
52  0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]
53
54print '#Standard eta WRF values_________'
55for iwl in range(Nwrflev):
56    print '#',iwl, znw_prac[iwl]
57
58#Desired levels
59deslev = int(opts.nlevels)
60if deslev < Nwrflev:
61    print errmsg
62    print '  ' + main + ': less desired levels: ',deslev,                            \
63      ' than standards from WRF: ', Nwrflev, '!!!!!'
64    quit(-1)
65
66deslevels = np.zeros((deslev), dtype=np.float)
67desnivs = np.zeros((deslev), dtype=np.float)
68
69il = 0
70deslevels[il] = 1.
71
72# Computing levels
73# Number of levels between original WRF levels
74Nbetween = int((deslev-1)/(Nwrflev-1))
75
76# Number of levels to fullfill desired number of levels (1 more added to each lowest
77#    WRF until is filled)
78fullfilldes = np.mod(deslev-1,Nwrflev-1)
79print 'Nbetween: ', Nbetween, 'N extra levels:',fullfilldes
80
81for iwl in range(1,Nwrflev):
82    if il < deslev - 1:
83        Nbtw = Nbetween
84        if iwl <= fullfilldes: 
85            print '# ',iwl, 'level with an extra value!'
86            Nbtw = Nbetween + 1
87        else:
88            Nbtw = Nbetween
89        for ibetl in range(Nbtw):
90            il = il + 1
91#            print il, iwl, ibetl, znw_prac[iwl-1], (iwl-1.)*1. + (ibetl+1.)/(Nbtw), znw_prac[iwl]
92            if il == deslev - 1 or il == deslev:
93                break
94            else:
95                deslevels[il] = znw_prac[iwl-1] + (ibetl+1)*(znw_prac[iwl] -         \
96                  znw_prac[iwl-1])/(Nbtw)
97                desnivs[il] = (iwl-1.)*1. + (ibetl+1.)/(Nbtw)
98#    if il == deslev - 2:
99#        il = il + 1
100#        deslevels[il] = znw_prac[iwl]
101#        desnivs[il] = iwl*1.
102
103#quit()
104
105deslevels[deslev-1] = 0.
106desnivs[deslev-1] = (Nwrflev-1)*1.
107
108print '#il equiv_WRFlev new_levels diff_levels________________'
109for il in range(0,deslev):
110    if il > 0:
111        print il, desnivs[il], deslevels[il], deslevels[il-1] - deslevels[il]
112    else:
113        print il, desnivs[il], deslevels[il], 0.
114
115print 'eta_levels                          = ',numVector_String(deslevels, ', ')
Note: See TracBrowser for help on using the repository browser.