| 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 |
|---|
| 3 | import numpy as np |
|---|
| 4 | import os |
|---|
| 5 | import re |
|---|
| 6 | from optparse import OptionParser |
|---|
| 7 | |
|---|
| 8 | main = 'interpolate_WRF_vertical_lev' |
|---|
| 9 | errmsg = 'ERROR -- errror -- ERROR -- error' |
|---|
| 10 | warnmsg = 'WARNING -- warning -- WARNING -- warning' |
|---|
| 11 | |
|---|
| 12 | def 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 | |
|---|
| 40 | parser = OptionParser() |
|---|
| 41 | parser.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 | |
|---|
| 49 | Nwrflev = 17 |
|---|
| 50 | znw_prac = np.zeros((Nwrflev), dtype=np.float) |
|---|
| 51 | znw_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 | |
|---|
| 54 | print '#Standard eta WRF values_________' |
|---|
| 55 | for iwl in range(Nwrflev): |
|---|
| 56 | print '#',iwl, znw_prac[iwl] |
|---|
| 57 | |
|---|
| 58 | #Desired levels |
|---|
| 59 | deslev = int(opts.nlevels) |
|---|
| 60 | if deslev < Nwrflev: |
|---|
| 61 | print errmsg |
|---|
| 62 | print ' ' + main + ': less desired levels: ',deslev, \ |
|---|
| 63 | ' than standards from WRF: ', Nwrflev, '!!!!!' |
|---|
| 64 | quit(-1) |
|---|
| 65 | |
|---|
| 66 | deslevels = np.zeros((deslev), dtype=np.float) |
|---|
| 67 | desnivs = np.zeros((deslev), dtype=np.float) |
|---|
| 68 | |
|---|
| 69 | il = 0 |
|---|
| 70 | deslevels[il] = 1. |
|---|
| 71 | |
|---|
| 72 | # Computing levels |
|---|
| 73 | # Number of levels between original WRF levels |
|---|
| 74 | Nbetween = 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) |
|---|
| 78 | fullfilldes = np.mod(deslev-1,Nwrflev-1) |
|---|
| 79 | print 'Nbetween: ', Nbetween, 'N extra levels:',fullfilldes |
|---|
| 80 | |
|---|
| 81 | for 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 | |
|---|
| 105 | deslevels[deslev-1] = 0. |
|---|
| 106 | desnivs[deslev-1] = (Nwrflev-1)*1. |
|---|
| 107 | |
|---|
| 108 | print '#il equiv_WRFlev new_levels diff_levels________________' |
|---|
| 109 | for 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 | |
|---|
| 115 | print 'eta_levels = ',numVector_String(deslevels, ', ') |
|---|