1 | import numpy as np |
---|
2 | from matplotlib.backends.backend_pdf import PdfPages |
---|
3 | import matplotlib.pyplot as plt |
---|
4 | import matplotlib as mpl |
---|
5 | import matplotlib.cm as cm |
---|
6 | from mpl_toolkits.basemap import Basemap |
---|
7 | from matplotlib.patches import Polygon |
---|
8 | # |
---|
9 | # Compute layer thickness |
---|
10 | # |
---|
11 | def 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 | # |
---|
27 | def 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 | # |
---|
80 | dpu_max=2.0 |
---|
81 | nslm=11 |
---|
82 | zz=np.zeros(nslm, float) |
---|
83 | dz=np.zeros(nslm+1, float) |
---|
84 | thickness=np.zeros(nslm, float) |
---|
85 | depth=np.zeros(nslm+1, float) |
---|
86 | |
---|
87 | for 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 | |
---|
91 | diaglev=np.zeros(nslm, float) |
---|
92 | for i in range(nslm-1): |
---|
93 | diaglev[i] = dpu_max/(2**(nslm-1) -1) * ( ( 2**(i) -1) + ( 2**(i+1) -1) )/2.0 |
---|
94 | diaglev[nslm-1]=dpu_max |
---|
95 | print "== Old values for reference ==========================================" |
---|
96 | print "zz=", zz |
---|
97 | print "dz=",dz |
---|
98 | print "diagdev=",diaglev |
---|
99 | print "======================================================================" |
---|
100 | print " " |
---|
101 | print " " |
---|
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 |
---|
109 | FirstLayer=9.77517107e-04 |
---|
110 | try : |
---|
111 | FirstLayer=float(raw_input("Depth of top hydrology layer [9.77517107e-04 m] >")) |
---|
112 | except ValueError: |
---|
113 | FirstLayer=9.77517107e-04 |
---|
114 | # |
---|
115 | # Maximum depth of soil moisture |
---|
116 | dpu_max=2.0 |
---|
117 | try : |
---|
118 | dpu_max=float(raw_input("Depth of hydrology [2.0m] >")) |
---|
119 | except ValueError: |
---|
120 | dpu_max=2.0 |
---|
121 | # |
---|
122 | # The Linear layer thickness starts at : |
---|
123 | LinearFromDepth=0.75 |
---|
124 | try : |
---|
125 | LinearFromDepth=float(raw_input("Depth at which linear hydrology layers start [0.75 m] >")) |
---|
126 | except ValueError: |
---|
127 | LinearFromDepth=0.75 |
---|
128 | # |
---|
129 | # Maximum depth of the soil thermodynamics |
---|
130 | tdepth_max = 8.0 |
---|
131 | try : |
---|
132 | tdepth_max=float(raw_input("Depth of thermodynamics [8.0m] >")) |
---|
133 | except 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. |
---|
138 | TFilterDepth=0.05 |
---|
139 | # |
---|
140 | # It is coded FORTRAN style so that translations is easier |
---|
141 | # |
---|
142 | nltmp=500 |
---|
143 | ztmp=np.zeros(nltmp, float) |
---|
144 | dtmp=np.zeros(nltmp+1, float) |
---|
145 | for 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] |
---|
152 | nslm=np.argmin(np.abs(ztmp-dpu_max))+1 |
---|
153 | # |
---|
154 | # Extract now the values we need to reach dpu_max |
---|
155 | # |
---|
156 | zz=ztmp[0:nslm] |
---|
157 | dz=dtmp[0:nslm+1] |
---|
158 | dz[nslm]=0.0 |
---|
159 | diaglev=np.zeros(nslm, float) |
---|
160 | diaglev[0]=dz[1]/2.0 |
---|
161 | for i in range(1,nslm): |
---|
162 | diaglev[i] = diaglev[i-1]+(dz[i]+dz[i+1])/2.0 |
---|
163 | print "== New values ========================================================" |
---|
164 | print "Number of layers in hydrol = ", nslm |
---|
165 | print "zz=", zz |
---|
166 | print "dz=",dz |
---|
167 | print "diagdev=",diaglev |
---|
168 | # |
---|
169 | # Extension for the thermodynamics |
---|
170 | # |
---|
171 | ztmp=np.zeros(nltmp, float) |
---|
172 | for i in range(nslm): |
---|
173 | ztmp[i]=diaglev[i] |
---|
174 | for i in range(nslm,nltmp): |
---|
175 | ztmp[i]=ztmp[i-1]+2.0*(ztmp[i-1]-ztmp[i-2]) |
---|
176 | ngrnd=np.argmin(np.abs(ztmp-tdepth_max))+1 |
---|
177 | # |
---|
178 | tdepth=np.zeros(ngrnd, float) |
---|
179 | tdepth=ztmp[0:ngrnd] |
---|
180 | print "Number of layers in thermosoil = ", ngrnd |
---|
181 | print "tdepth=",tdepth |
---|
182 | print "======================================================================" |
---|
183 | # |
---|
184 | # Do the plot |
---|
185 | # |
---|
186 | pdf = PdfPages('VerticalLayers.pdf') |
---|
187 | discretizationplot(diaglev, tdepth, dpu_max, tdepth_max, "mixing water and temperature") |
---|
188 | pdf.close() |
---|