source: trunk/UTIL/z2sig/build_sig.py @ 3806

Last change on this file since 3806 was 3677, checked in by afalco, 3 months ago

Pluto: .def example files for newstart.
z2sig: parameters in input section.
AF

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1#!/usr/bin/python3
2from    numpy                 import    *
3import  numpy                 as        np
4import  matplotlib.pyplot     as        mpl
5from matplotlib.cm import get_cmap
6import pylab
7from matplotlib import ticker
8import matplotlib.colors as colors
9import datetime
10from matplotlib import pyplot
11
12
13######################################################################
14# Input Parameters
15######################################################################
16input_file = 'z2sig.def_47'
17ps=1.1 # in Pa
18pa=0.5
19preff=2
20
21# first and last level difference in m
22l0=0.0025
23ln=4.6
24
25# New levels
26mynbl=201
27n=float(mynbl)
28
29plot = True
30# plot = False
31
32
33######################################################################
34# Fonction  read  file
35######################################################################
36def 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
56data,hh,nbl = readfile(input_file)
57
58#####################
59# Get sigma levels
60#####################
61sig=np.ones(nbl,dtype='f')
62for 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#####################
68ap=0
69bp=sig
70pp=ap+bp*1.1
71
72#####################
73# Get hybrid pressure levels
74#####################
75def 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:
119nsig=np.zeros(nbl,dtype='f')
120for i in range(nbl):
121  nsig[i]=sig_hybrid(sig[i],pa,preff)
122
123bps=exp(1-1/(nsig)**2)
124aps=pa*(nsig-bps)
125
126pps=aps+bps*ps
127
128#####################
129# Get Pseudo altitude
130#####################
131# non hybrid
132ph=-hh*log(pp/ps)
133dzph=-(ph[0:-1]-ph[1:])
134# hybrid
135phs=-hh*log(pps/ps)
136dzphs=-(phs[0:-1]-phs[1:])
137
138#####################
139# New choice delta-zh
140#####################
141myclev=np.linspace(1,mynbl,mynbl)
142xx=myclev[0:mynbl-1] # xaxis for layer thickness
143xx1=myclev[0:mynbl]
144# Coefficient for the exp function
145c=0.11
146a=1/n*(np.log(ln/c)-np.log(l0/c))
147b=-1/a*np.log(l0/c)
148# Dz difference altitude between 2 layers :
149mydz=exp(a*(xx-b))*c
150
151# New pseudo altitude myph
152myph=np.zeros(mynbl,dtype='f')
153myph[0]=mydz[0]
154print(myph[0])
155for i in range(mynbl-1):
156 myph[i+1]=mydz[i]+myph[i]
157 print(myph[i+1])
158
159to_file = np.concatenate([[hh], myph])
160output = f"z2sig.def_{mynbl}"
161np.savetxt(output,to_file, fmt="%g")
162print(f"Saved to {output}")
163
164# New Ps levels
165mypp=ps*exp(-myph/hh)
166
167
168if 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
Note: See TracBrowser for help on using the repository browser.