1 | #!/usr/bin/python3 |
---|
2 | from numpy import * |
---|
3 | import numpy as np |
---|
4 | import matplotlib.pyplot as mpl |
---|
5 | from matplotlib.cm import get_cmap |
---|
6 | import pylab |
---|
7 | from matplotlib import ticker |
---|
8 | import matplotlib.colors as colors |
---|
9 | import datetime |
---|
10 | from matplotlib import pyplot |
---|
11 | |
---|
12 | |
---|
13 | ###################################################################### |
---|
14 | # Input Parameters |
---|
15 | ###################################################################### |
---|
16 | input_file = 'z2sig.def_47' |
---|
17 | ps=1.1 # in Pa |
---|
18 | pa=0.5 |
---|
19 | preff=2 |
---|
20 | |
---|
21 | # first and last level difference in m |
---|
22 | l0=0.0025 |
---|
23 | ln=4.6 |
---|
24 | |
---|
25 | # New levels |
---|
26 | mynbl=201 |
---|
27 | n=float(mynbl) |
---|
28 | |
---|
29 | plot = True |
---|
30 | # plot = False |
---|
31 | |
---|
32 | |
---|
33 | ###################################################################### |
---|
34 | # Fonction read file |
---|
35 | ###################################################################### |
---|
36 | def readfile(name): |
---|
37 | # test reading lire texte txt |
---|
38 | myfile = open(name, 'r') |
---|
39 | mylines=myfile.readlines() |
---|
40 | nbline=size(mylines) |
---|
41 | nbcolumn=len(mylines[0].split()) |
---|
42 | print('nbline, nbcol:',nbline, nbcolumn) |
---|
43 | data=np.zeros(nbline-1,dtype='f') |
---|
44 | i=0 |
---|
45 | nbl = 0 |
---|
46 | for line in mylines: |
---|
47 | s=str.split(line) |
---|
48 | if i==0: |
---|
49 | hh=float(s[0]) |
---|
50 | else: |
---|
51 | data[i-1]=float(s[0]) |
---|
52 | nbl = nbl+1 |
---|
53 | i=i+1 |
---|
54 | return data,hh,nbl |
---|
55 | |
---|
56 | data,hh,nbl = readfile(input_file) |
---|
57 | |
---|
58 | ##################### |
---|
59 | # Get sigma levels |
---|
60 | ##################### |
---|
61 | sig=np.ones(nbl,dtype='f') |
---|
62 | for i in range(nbl-1): |
---|
63 | sig[i+1]=0.5*(exp(-data[i+1]/hh)+exp(-data[i]/hh)) |
---|
64 | |
---|
65 | ##################### |
---|
66 | # Get non-hybrid pressure levels |
---|
67 | ##################### |
---|
68 | ap=0 |
---|
69 | bp=sig |
---|
70 | pp=ap+bp*1.1 |
---|
71 | |
---|
72 | ##################### |
---|
73 | # Get hybrid pressure levels |
---|
74 | ##################### |
---|
75 | def sig_hybrid(sig,pa,preff): |
---|
76 | #c Subroutine utilisee pour calculer des valeurs de sigma modifie |
---|
77 | #c pour conserver les coordonnees verticales decrites dans |
---|
78 | #c esasig.def/z2sig.def lors du passage en coordonnees hybrides |
---|
79 | #c F. Forget 2002 |
---|
80 | #c Connaissant sig (niveaux "sigma" ou on veut mettre les couches) |
---|
81 | #c L'objectif est de calculer newsig telle que |
---|
82 | #c (1 -pa/preff)*exp(1-1./newsig**2)+(pa/preff)*newsig = sig |
---|
83 | #c Cela ne se resoud pas analytiquement: |
---|
84 | #c => on resoud par iterration bourrine |
---|
85 | #c ---------------------------------------------- |
---|
86 | #c Information : where exp(1-1./x**2) become << x |
---|
87 | #c x exp(1-1./x**2) /x |
---|
88 | #c 1 1 |
---|
89 | #c 0.68 0.5 |
---|
90 | #c 0.391 1.E-2 |
---|
91 | #c => on peut utiliser newsig = sig*preff/pa si sig*preff/pa < 0.25 |
---|
92 | |
---|
93 | newsig = sig |
---|
94 | x1=0 |
---|
95 | x2=1 |
---|
96 | if (sig>=1): |
---|
97 | newsig = sig |
---|
98 | elif (sig*preff/pa>=0.25): |
---|
99 | for j in range(9999): # nombre d'iteration max |
---|
100 | F=((1-pa/preff)*exp(1-1./newsig**2)+(pa/preff)*newsig)/sig |
---|
101 | if (F>1): |
---|
102 | x2 = newsig |
---|
103 | newsig=(x1+newsig)*0.5 |
---|
104 | else: |
---|
105 | x1=newsig |
---|
106 | newsig=(x2+newsig)*0.5 |
---|
107 | #Test : on arete lorsque on approxime sig a moins de 0.01 m pres |
---|
108 | #(en pseudo altiude) : |
---|
109 | #if (abs(10.*log(F))<1.e-5): |
---|
110 | # break |
---|
111 | |
---|
112 | else: # if (sig*preff/pa.le.0.25) |
---|
113 | newsig= sig*preff/pa |
---|
114 | |
---|
115 | return newsig |
---|
116 | #---------------------------------- |
---|
117 | |
---|
118 | # hybrid pressure: |
---|
119 | nsig=np.zeros(nbl,dtype='f') |
---|
120 | for i in range(nbl): |
---|
121 | nsig[i]=sig_hybrid(sig[i],pa,preff) |
---|
122 | |
---|
123 | bps=exp(1-1/(nsig)**2) |
---|
124 | aps=pa*(nsig-bps) |
---|
125 | |
---|
126 | pps=aps+bps*ps |
---|
127 | |
---|
128 | ##################### |
---|
129 | # Get Pseudo altitude |
---|
130 | ##################### |
---|
131 | # non hybrid |
---|
132 | ph=-hh*log(pp/ps) |
---|
133 | dzph=-(ph[0:-1]-ph[1:]) |
---|
134 | # hybrid |
---|
135 | phs=-hh*log(pps/ps) |
---|
136 | dzphs=-(phs[0:-1]-phs[1:]) |
---|
137 | |
---|
138 | ##################### |
---|
139 | # New choice delta-zh |
---|
140 | ##################### |
---|
141 | myclev=np.linspace(1,mynbl,mynbl) |
---|
142 | xx=myclev[0:mynbl-1] # xaxis for layer thickness |
---|
143 | xx1=myclev[0:mynbl] |
---|
144 | # Coefficient for the exp function |
---|
145 | c=0.11 |
---|
146 | a=1/n*(np.log(ln/c)-np.log(l0/c)) |
---|
147 | b=-1/a*np.log(l0/c) |
---|
148 | # Dz difference altitude between 2 layers : |
---|
149 | mydz=exp(a*(xx-b))*c |
---|
150 | |
---|
151 | # New pseudo altitude myph |
---|
152 | myph=np.zeros(mynbl,dtype='f') |
---|
153 | myph[0]=mydz[0] |
---|
154 | print(myph[0]) |
---|
155 | for i in range(mynbl-1): |
---|
156 | myph[i+1]=mydz[i]+myph[i] |
---|
157 | print(myph[i+1]) |
---|
158 | |
---|
159 | to_file = np.concatenate([[hh], myph]) |
---|
160 | output = f"z2sig.def_{mynbl}" |
---|
161 | np.savetxt(output,to_file, fmt="%g") |
---|
162 | print(f"Saved to {output}") |
---|
163 | |
---|
164 | # New Ps levels |
---|
165 | mypp=ps*exp(-myph/hh) |
---|
166 | |
---|
167 | |
---|
168 | if plot: |
---|
169 | clev=np.linspace(1,nbl,nbl) |
---|
170 | ### Plot pressure levels |
---|
171 | mpl.figure(figsize=(50/2.54, 30/2.54),facecolor='w') |
---|
172 | ax=mpl.subplot(131) |
---|
173 | mpl.plot(clev,pp,'k-',marker='*',label='P ini') |
---|
174 | mpl.plot(clev,pps,'r-',marker='*',label='P ini hybrid') |
---|
175 | mpl.plot(myclev,mypp,'b-',marker='*',label='P new') |
---|
176 | mpl.gca().invert_yaxis() |
---|
177 | pyplot.yscale('log') |
---|
178 | mpl.grid() |
---|
179 | |
---|
180 | ### Plot difference altitude between 2 layers |
---|
181 | ax=mpl.subplot(132) |
---|
182 | mpl.plot(clev[0:nbl-1],dzph,'k-',marker='*',label='dz ini') |
---|
183 | mpl.plot(clev[0:nbl-1],dzphs,'r-',marker='*',label='dz ini hybrid') |
---|
184 | mpl.plot(xx,mydz,'b-',marker='*',label='dz new') |
---|
185 | pyplot.yscale('log') |
---|
186 | mpl.grid() |
---|
187 | |
---|
188 | ### pseudo altitude |
---|
189 | ax=mpl.subplot(133) |
---|
190 | mpl.plot(clev[0:nbl],ph,'k-',marker='*',label='sig ini') |
---|
191 | mpl.plot(clev[0:nbl],phs,'r-',marker='*',label='sig ini hybrid') |
---|
192 | mpl.plot(xx1,myph,'b-',marker='*',label='sig new') |
---|
193 | #pyplot.yscale('log') |
---|
194 | mpl.grid() |
---|
195 | |
---|
196 | mpl.show() |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|