Index: /trunk/tools/generic_tools.py
===================================================================
--- /trunk/tools/generic_tools.py	(revision 2097)
+++ /trunk/tools/generic_tools.py	(revision 2098)
@@ -173,4 +173,5 @@
 # vals_around: Function to provide the 3x3 values around a given j,i point
 # xtrm_nx: Function to provide the extreme (a percentage above min,max) of a series of values
+# WRF_percenlevels: Function to compute eta-levels for WRF following a percentage distribution
 # WRFsetup: Function to check the set-up of a WRF model namelist or from any other model
 
@@ -13750,6 +13751,111 @@
     return       
 
+def WRF_percenlevels(Nlevs=38, ps=1000., pt=20.):
+    """ Function to compute eta-levels for WRF following a percentage distribution
+      Nlevs: number of levels
+      pt: p_top pressure [hPa]
+      ps: p_sfc pressure [hPa]
+        p_sfc - 975 hPa:  4 %
+          975 - 900 hPa: 28 %
+          900 - 850 hPa: 10 %
+          850 - 600 hPa: 25 %
+          600 - 450 hPa: 12 %
+          450 - 250 hPa: 12 %
+          250 - p_top:    9 % 
+    """
+    fname = 'WRF_percenlevels'
+
+    # 72 numbers
+    #dps = {900.:5., 850.: 10., 700.: 15., 600.: 20., 450.: 10., 250.: 20., 50.: 30.}
+    #Nlevp: {450.0: 15, 900.0: 20, 50.0: 6, 850.0: 5, 600.0: 5, 250.0: 10, 700.0: 10} %
+    #900.0 : 27.7777777778
+    #850.0 : 6.94444444444
+    #700.0 : 13.8888888889
+    #600.0 : 6.94444444444
+    #450.0 : 20.8333333333
+    #250.0 : 13.8888888889
+    #50.0 : 8.33333333333
+
+    # Assigning percentage of levels per interval
+    # Computing dps
+    #dppsperce = {900.: 29., 850.: 6., 700.: 15., 600.: 7., 450.:21., 250.:14., 50.: 8.}
+    dppsperce = {990: 4., 900.: 28., 850.: 10., 600.: 25., 450.:12., 250.:12., pt: 9.}
+    dpps0 = dppsperce.keys()
+    dpps0.sort(reverse=True)
+
+    dps = {}
+
+    # Assigning number of levels
+    nplevs = {}
+    percen = 0
+    Ntotlev = 0
+    # Removing 1st and last levels
+    Nlevs1 = Nlevs - 2
+    for dpp in dpps0:
+        nlev = int(Nlevs1*dppsperce[dpp]/100.)
+        nplevs[dpp] = nlev
+        Ntotlev = Ntotlev + nlev
+
+    print '  ' + fname + ' final total number of levels:', Ntotlev,                  \
+      'of initial Nlevs:', Nlevs, 'TOT %:', np.sum(dppsperce.values())
+
+    # Re-distributing rest of levels on first intervals
+    if Ntotlev < Nlevs1:
+        Nextralevs = Nlevs1 - Ntotlev
+        print '  ' + fname + ': Ntotlev:', Ntotlev, 're-distributing:', Nextralevs,  \
+          'extra levels !!'
+        for i in range(Nextralevs): nplevs[dpps0[i]] = nplevs[dpps0[i]] + 1
+    else:
+        Nextralevs = 0
+
+    print '  ' + fname + ' final number of levels:', nplevs, 'sum:',                 \
+      np.sum(nplevs.values()) + 2
+
+    # Assigning pressure-differenctials
+    pp0 = ps
+    print '  uppper_limit_section [hPa] dp [hPa] _______'
+    for dpp in dpps0:
+        nlev = nplevs[dpp]
+        # Adding an extra level to meet final pt
+        if dpp == dpps0[len(dpps0)-1]: nlev = nlev + 1
+        dp = pp0 - dpp
+        ddp = dp / nlev
+        dps[dpp] = ddp
+        percen = percen + dppsperce[dpp]
+        print '      ', '{:.2f}'.format(dpp), '                ', ddp
+        pp0 = dpp 
+
+    p = [ps]
+    dpps = list(dps.keys())
+    dpps.sort(reverse=True)
+    iiz = 1
+    Ntotlevs = 0
+    for dpp in dpps:
+        dp = dps[dpp]
+        Ntotlevs = Ntotlevs + nplevs[dpp]
+        for iz in range(nplevs[dpp]):
+            p.append(p[iiz-1] - dp)
+            iiz = iiz + 1
+            #print 'iz:', iz, p[iz], dp, pt
+    p.append(pt)
+    p = np.array(p)
+    eta = (p-pt)/(ps-pt)
+    dz=p.shape[0]
+
+    ofilen = 'WRFpercen_p' + str(Nlevs) + 'lev.dat'
+    of = open(ofilen, 'w')
+    of.write('# ' + str(Nlevs) + ' i-lev[1] pressure[2] eta[3] dp[4]__________\n')
+    for i in range(dz-1):
+       of.write(str(i) + ' ' + str(p[i]) + ' ' + str(eta[i]) + ' ' + str(p[i]-p[i+1]) + '\n')
+    i = dz-1
+    of.write(str(i) + ' ' + str(pt) + ' ' + str(eta[i]) + ' 0.\n')
+
+    of.close()
+    print fname + ": succesfull written of file '" + ofilen + "' !!"
+
+    return
+
+print WRF_percenlevels()
+
 #quit()
 
-
-
