DevelopmentActivities/CMIP6/DevelopmentsCMIP6/soil_physic: NewHydroVert.py

File NewHydroVert.py, 5.5 KB (added by jpolcher, 9 years ago)

VerticalLayerConfigurator?

Line 
1import numpy as np
2from matplotlib.backends.backend_pdf import PdfPages
3import matplotlib.pyplot as plt
4import matplotlib as mpl
5import matplotlib.cm as cm
6from mpl_toolkits.basemap import Basemap
7from matplotlib.patches import Polygon
8#
9# Compute layer thickness
10#
11def CompThickness(x):
12    nbx=len(x)
13    dx=0.0
14    for i in range(1,nbx):
15        dx = np.append(dx,(x[i-1]+x[i])/2.0)
16    # Last layer
17    dx=np.append(dx,(x[nbx-1]-dx[nbx-1])*2+dx[nbx-1])
18    #
19    tx=[]
20    for i in range(len(dx)-1):
21        tx = np.append(tx,dx[i+1]-dx[i])
22    #
23    return dx,tx
24#
25# Ploting function
26#
27def discretizationplot(wlev, tlev, wdepth_max, tsol_max, overalltitle):
28    #
29    # Compute thicknesses
30    #
31    wy,wz=CompThickness(wlev)
32    ty,tz=CompThickness(tlev)
33    #
34    x=np.array([0,1])
35
36    f, (ax1, ax2) = plt.subplots(1, 2, sharey=False)
37
38    zmax=max([np.max(wz),np.max(tz)])
39    cNorm = mpl.colors.Normalize(vmin=0, vmax=zmax)
40    cmap = plt.get_cmap('Dark2')   
41
42    ztmp=np.reshape(np.repeat(wz,2), (len(wy)-1, len(x)) )
43    ax1.set_yscale('symlog')
44    ax1.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
45    ax1.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%d'))
46    ax1.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(lambda x, pos: str(int(round(x)))))
47#    ax1.set_yticks([-0.02, -0.2, -2])
48    datamap=ax1.pcolor(x,-wy, ztmp, zorder=0, norm=cNorm, cmap=cmap)
49    # Add tine lines to show the middle of the layer where SM is positioned
50    for i in range(len(wlev)):
51        ax1.plot(x,np.array([-wlev[i],-wlev[i]]), color='k', linewidth=0.4)
52#
53    ax1.text(0.5, -wdepth_max, "dpu_max", fontweight='bold',ha='center',
54             va='center')
55    ax1.set_xlabel("Min thickness="+str(round(np.min(wz),5))+"[m]")
56    ax1.set_title("nslm="+str(len(wlev)))
57#
58    ztmp=np.reshape(np.repeat(tz, 2), (len(ty)-1,len(x)))
59    datamap = ax2.pcolor(x,-ty, ztmp, zorder=0, norm=cNorm, cmap=cmap)
60    # Add tine lines to show the middle of the layer where T is positioned
61    for i in range(len(tlev)):
62        ax2.plot(x,np.array([-tlev[i],-tlev[i]]), color='k', linewidth=0.4)
63
64    ax2.text(0.5, -tsol_max, "tdepth_max", fontweight='bold',ha='center',
65                 va='center')
66    ax2.set_title("ngrnd="+str(len(tlev)))
67    ax2.set_xlabel("Max thickness="+str(round(np.max(tz),5))+"[m]")
68#
69    cb=plt.colorbar(datamap, cmap=cmap, shrink=0.5, norm=cNorm)
70    cb.set_label("Layer Thickness [m]")
71#
72    plt.suptitle(overalltitle)
73#
74#    plt.show()
75    pdf.savefig()
76#
77#
78# As a reminder, the levels of the hydrology as in the code today
79#
80dpu_max=2.0
81nslm=11
82zz=np.zeros(nslm, float)
83dz=np.zeros(nslm+1, float)
84thickness=np.zeros(nslm, float)
85depth=np.zeros(nslm+1, float)
86
87for i in range(1,nslm):
88    zz[i] = dpu_max*((2**i)-1)/((2**(nslm-1))-1)
89    dz[i] = zz[i]-zz[i-1]
90
91diaglev=np.zeros(nslm, float)
92for i in range(nslm-1):
93    diaglev[i] = dpu_max/(2**(nslm-1) -1) * ( ( 2**(i) -1) + ( 2**(i+1) -1) )/2.0
94diaglev[nslm-1]=dpu_max
95print "== Old values for reference =========================================="
96print "zz=", zz
97print "dz=",dz
98print "diagdev=",diaglev
99print "======================================================================"
100print "  "
101print "  "
102#
103#
104# New Methode for computing levels with the new parameters to control.
105#
106# All units in meters
107#
108# Thickness of first Layer
109FirstLayer=9.77517107e-04
110try :
111    FirstLayer=float(raw_input("Depth of top hydrology layer [9.77517107e-04 m] >"))
112except ValueError:
113    FirstLayer=9.77517107e-04
114#
115# Maximum depth of soil moisture
116dpu_max=2.0
117try :
118    dpu_max=float(raw_input("Depth of hydrology [2.0m] >"))
119except ValueError:
120    dpu_max=2.0
121#
122# The Linear layer thickness starts at :
123LinearFromDepth=0.75
124try :
125    LinearFromDepth=float(raw_input("Depth at which linear hydrology layers start [0.75 m] >"))
126except ValueError:
127    LinearFromDepth=0.75
128#
129# Maximum depth of the soil thermodynamics
130tdepth_max = 8.0
131try :
132    tdepth_max=float(raw_input("Depth of thermodynamics [8.0m] >"))
133except ValueError:
134    tdepth_max = 8.0
135# Depth over which the thermal profile needs to be filterd
136# It could be estimate based on the wave velocity of heat in a
137# dry soil and the Courant-Levy criteria.
138TFilterDepth=0.05
139#
140# It is coded FORTRAN style so that translations is easier
141#
142nltmp=500
143ztmp=np.zeros(nltmp, float)
144dtmp=np.zeros(nltmp+1, float)
145for i in range(1,nltmp):
146    if ( ztmp[i-1] < LinearFromDepth ) :
147        ztmp[i] = FirstLayer*2.0*((2**i)-1)
148        dtmp[i] = ztmp[i]-ztmp[i-1]
149    else :
150        ztmp[i] = ztmp[i-1]+dtmp[i-1]
151        dtmp[i] = dtmp[i-1]
152nslm=np.argmin(np.abs(ztmp-dpu_max))+1
153#
154# Extract now the values we need to reach dpu_max
155#
156zz=ztmp[0:nslm]
157dz=dtmp[0:nslm+1]
158dz[nslm]=0.0
159diaglev=np.zeros(nslm, float)
160diaglev[0]=dz[1]/2.0
161for i in range(1,nslm):
162    diaglev[i] = diaglev[i-1]+(dz[i]+dz[i+1])/2.0
163print "== New values ========================================================"
164print "Number of layers in hydrol = ", nslm
165print "zz=", zz
166print "dz=",dz
167print "diagdev=",diaglev
168#
169# Extension for the thermodynamics
170#
171ztmp=np.zeros(nltmp, float)
172for i in range(nslm):
173    ztmp[i]=diaglev[i]
174for i in range(nslm,nltmp):
175    ztmp[i]=ztmp[i-1]+2.0*(ztmp[i-1]-ztmp[i-2])
176ngrnd=np.argmin(np.abs(ztmp-tdepth_max))+1
177#
178tdepth=np.zeros(ngrnd, float)
179tdepth=ztmp[0:ngrnd]
180print "Number of layers in thermosoil = ", ngrnd
181print "tdepth=",tdepth
182print "======================================================================"
183#
184# Do the plot
185#
186pdf = PdfPages('VerticalLayers.pdf')
187discretizationplot(diaglev, tdepth, dpu_max, tdepth_max, "mixing water and temperature")
188pdf.close()