-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathebmodel.py
491 lines (324 loc) · 14 KB
/
ebmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# -*- coding: utf-8 -*-
"""
ebmodel.py
This module implements the point-based glacier surface energy balance model
originally developed as a spreadsheet application by:
Brock, B. W., and Arnold, N. (2000) Technical Communication: A spreadsheet-
based (Microsoft Excel) point surface energy balance model for glacier and
snow melt studies. Earth Surface Processes and Landforms, 25, p 649-658.
No license was provided with the original model. Please see the accompanying
license for this implementation. Any errors which may have arisen from
porting the model to Python are solely attributable to Andrew Tedstone,
not to the original authors, but without any expression of liability.
Use of this model in a publication should be accompanied by a citation to
the aforementioned reference and to this repository.
For further usage details please see the README and accompanying demo files.
@author: Andrew Tedstone ([email protected])
@date: May 2018
"""
import numpy as np
import math
def calc_dayang(day):
return (day / 365.25) * 360
def calc_eqtim(dayang):
return (-0.128 * np.sin((dayang - 2.8) * (math.pi / 180))) - (0.165 * np.sin(((2 * dayang) + 19.7) * (math.pi / 180)))
def calc_solhour(time, lon, lon_ref, eqtim, summertime):
return (time / 100) + ((lon - lon_ref) /15) + eqtim - summertime
def calc_soldec(day):
i1 = -23.2559 * np.cos(((2 * math.pi * day) / 365) + 0.1582)
i2 = -0.3915 * np.cos(((4 * math.pi * day)/ 365) + 0.0934)
i3 = -0.1764 * np.cos(((6 * math.pi * day) / 365) + 0.4539)
return 0.3948 + (i1 + i2 + i3)
def calc_solhran(solhour):
return 15 * (solhour - 12)
def calc_solaltr(lat, soldec, solhran):
return np.arcsin((
((np.sin(lat*(math.pi/180))) * (np.sin(soldec*(math.pi/180)))) +
((np.cos(lat*(math.pi/180))) * (np.cos(soldec*(math.pi/180))) * (np.cos(solhran*(math.pi/180))))
))
def calc_solaltd(solaltr):
return solaltr * 180/ math.pi
def calc_cossolaz(lat, solaltr, soldec):
return ( ((np.sin(lat*(math.pi/180)))*(np.sin(solaltr))) - ((np.sin(soldec*(math.pi/180)))) ) / ((np.cos((lat)*(math.pi/180)))*((np.cos(solaltr))))
def calc_sinolaz(soldec, solhran, solaltr):
return (np.cos(soldec*(math.pi / 180)))*(np.sin(solhran*(math.pi/180)))/(np.cos(solaltr))
def calc_solaz(sinsolaz, cossolaz):
if sinsolaz < 0:
return -np.arccos(cossolaz)
else:
return np.arccos(cossolaz)
def calc_solaz360(solaz):
return (solaz*180/math.pi)+180
def calc_cloudn(inswrad, elevation, solaltr):
""" Calculate cloud cover
B+A eqn. 3
"""
return np.min([1-(inswrad/((1368*(0.75**(100000/(100000*(1-(elevation*0.0001)))))*np.cos(1.57-solaltr)))), 1])
def calc_diffuser(cloudn):
""" Calculate diffuse fraction of total incoming shortwave radiation
B+A eqn. 2
"""
if cloudn > 0.8:
return 0.8
else:
return 0.65 * (np.max([cloudn,0])) + 0.15
def calc_directr(diffuser):
""" Calculate direct (incident) fraction of total incoming shortwave radiation
Following B+A eqn. 2
"""
return 1 - diffuser
def calc_Qn(directr, inswrad, solaltr):
""" Calculate radiation received at surface normal to sun's rays
B+A eqn. 4
"""
return directr * inswrad / np.sin(solaltr)
def calc_Qi(Qn, solaltr, slope, solaz, aspect):
""" Calculate direct (incident) component of shortwave radiation
B+A eqn. 5
"""
return Qn * (((np.sin(solaltr))*(np.cos((slope)*(math.pi/180))))+((np.cos(solaltr))*(np.sin((slope)*(math.pi/180)))*(np.cos((solaz)-((aspect)*(math.pi/180))))))
def calc_Q_Wm2(albedo, Qi, diffuser, inswrad, slope):
""" Calculate net shortwave radiation (W m-2)
B+A eqn. 7
"""
return ((1-albedo)*Qi)+((1-albedo)*diffuser*((inswrad*((np.cos(((slope)*(math.pi/180))/2))**2))+(albedo*((np.sin(((slope)*(math.pi/180))/2))**2))))
def calc_Q_melt(Q_Wm2):
""" Calculate melting by (net) shortwave radiation (W m-2) """
return Q_Wm2 * 0.0107784
def calc_eo(airtemp, elevation, met_elevation, lapse):
""" Calculate clear sky emissivity
B+A see after eqn. 10
"""
return 8.733 * (0.001 * (((airtemp-(lapse*(elevation-met_elevation))) + 273.16)**0.788))
def calc_e_star(cloudn, eo):
""" Calculate sky effective emissivity
B+A eqn. 10
"""
return (1+(0.26*(np.max([cloudn,0]))))* eo
def calc_lwin(e_star, airtemp, lapse, elevation, met_elevation):
""" Calculate downwelling longwave radiation (W m-2)
B+A eqn. 9
"""
return e_star * (5.7*(10**-8))*(((airtemp-(lapse*(elevation-met_elevation)))+273.16)**4)
def calc_lnet_Wm2(lwin, lwout=316.):
""" Calculate net longwave radiation (W m-2)
B+A eqn. 8
"""
return lwin - lwout
def calc_lnet_melt(lnet_Wm2):
""" Calculate melt due to (net) longwave radiation """
return lnet_Wm2 * 0.0107784
def calc_atmpres(elevation):
""" Calculate atmospheric pressure """
return 100000*(1-(elevation*0.0001))
def calc_de(avp, elevation, met_elevation, lapse):
return (avp-(45*((elevation-met_elevation)*lapse)))-610.8
def calc_spehum(avp, atmpres):
""" Calculate specific humidity of air """
return avp / atmpres
def calc_spehtair(spehum):
""" Calculate specific heat of air at constant pressure (J kg-1 K-1)
B+A para following eqn. 12
"""
return 1004.67*(1+(0.84*spehum))
def calc_Re_star(U_star, roughness):
""" Estimate scaling length for roughness
B+A para following eqn. 12 and 16
"""
return (U_star*roughness)/0.00001461
def calc_ln_zt(roughness, Re_star):
""" Estimate scaling length for temperature
B+A para following eqn. 12 and 16
"""
return np.log(roughness)+0.317-(0.565*(np.log(Re_star)))-(0.183*((np.log(Re_star))**2))
def calc_ln_ze(roughness, Re_star):
""" Estimate scaling length for humidity
B+A para following eqn. 12 and 16
"""
return np.log(roughness)+0.396-(0.512*np.log(Re_star))-(0.18*((np.log(Re_star))**2))
def calc_L(U_star, airtemp, lapse, elevation, met_elevation, spehtair, Qs_Wm2):
""" Estimate Monin-Obukhov length scale
B+A eqn. 13
"""
return (1.225*((U_star)**3)*((((airtemp-(lapse*(elevation-met_elevation)))+273.16)+273.16)/2)*(spehtair))/(0.4*9.81*Qs_Wm2)
def calc_U_star_z_L(windspd, roughness):
""" Initial estimate of friction velocity
B+A para following eqn. 14
"""
return (0.4*windspd)/((np.log(2/roughness)))
def calc_U_star_full(windspd, roughness, L):
""" Full estimate of friction velocity
B+A eqn. 14
"""
return (0.4*windspd)/((np.log(2/roughness))+(5*(2/L)))
def calc_Qsz_L0(spehtair, windspd, airtemp, lapse, elevation, met_elevation, roughness, ln_zt):
""" Initial estimate of sensible heat flux (without L)
B+A para following eqn. 14
"""
return (1.225*spehtair*0.16*windspd*(airtemp-(lapse*(elevation-met_elevation))))/(((np.log(2/roughness)))*(((np.log(2))-ln_zt)))
def calc_Qlz_L0(windspd, de, atmpres, roughness, ln_ze):
""" Initial estimate for latent heat flux (without L)
B+A para following eqn. 14
"""
return (1.225*0.622*2500000*0.16*windspd*(de/atmpres))/((np.log(2/roughness))*((np.log(2))-(ln_ze)))
def calc_Qs_full(spehtair, windspd, airtemp, lapse, elevation, met_elevation, roughness, L, ln_zt):
""" Full estimate of Sensible Heat Flux W m-2
B+A eqn. 11
"""
return (1.225*spehtair*0.16*windspd*(airtemp-(lapse*(elevation-met_elevation))))/(((np.log(2/roughness))+(5*(2/L)))*(((np.log(2))-ln_zt)+(5*(2/L))))
def calc_QI_full(windspd, de, atmpres, roughness, ln_ze, L):
""" Full estimate of Latent Heat Flux W m-2
B+A eqn. 12
"""
return (1.225*0.622*2500000*0.16*windspd*(de/atmpres))/(((np.log(2/roughness))+(5*(2/L)))*(((np.log(2))-(ln_ze))+(5*(2/L))))
def calc_turbulent_fluxes(windspd, roughness, spehtair, airtemp, de, atmpres,
lapse, elevation, met_elevation,
max_n=25, tol=0.001,
verbose=False, return_steps=False):
""" Iteratively solve the turbulent fluxes and the Monin-Obukhov scale
B+A page 653.
Inputs:
n : maximum number of iterations
tol : tolerance threshold for iteration
verbose : if True then print information about iterations
return_steps : if True then return values of intermediary calculations
as per Brock and Arnold (2000)
Returns:
tuple (SHF, LHF) in units W m-2
if return_steps=True then returns:
tuple (U_star, Re_star, ln_zt, ln_ze, Qs_Wm2, QI_Wm2, L)
"""
# Solve initial conditions
U_star = calc_U_star_z_L(windspd, roughness)
Re_star = calc_Re_star(U_star, roughness)
if verbose:
print('Initial conditions: U_star: %s, Re_star: %s' %(U_star, Re_star))
if verbose:
print('Iteration block 1 . . . ')
print('ln_zt ln_ze Qs_Wm2 Ql_Wm2')
n = 0
while n < max_n:
ln_zt = calc_ln_zt(roughness, Re_star)
ln_ze = calc_ln_ze(roughness, Re_star)
Qs_Wm2 = calc_Qsz_L0(spehtair, windspd, airtemp, lapse, elevation,
met_elevation, roughness, ln_zt)
Ql_Wm2 = calc_Qlz_L0(windspd, de, atmpres, roughness, ln_ze)
if verbose:
print (ln_zt, ln_ze, Qs_Wm2, Ql_Wm2)
n += 1
if verbose:
print('Iteration block 2 . . . ')
print('U_star Re_star ln_zt ln_ze Qs_Wm2 QI_Wm2 L')
n = 0
L = 0.
L_old = 1.
while (n < max_n) and (np.abs(L - L_old) > tol):
L_old = L
L = calc_L(U_star, airtemp, lapse, elevation, met_elevation, spehtair, Qs_Wm2)
Re_star = calc_Re_star(U_star, roughness)
ln_zt = calc_ln_zt(roughness, Re_star)
ln_ze = calc_ln_ze(roughness, Re_star)
Qs_Wm2 = calc_Qs_full(spehtair, windspd, airtemp, lapse, elevation, met_elevation, roughness, L, ln_zt)
QI_Wm2 = calc_QI_full(windspd, de, atmpres, roughness, ln_ze, L)
U_star = calc_U_star_full(windspd, roughness, L)
if verbose:
print(U_star, Re_star, ln_zt, ln_ze, Qs_Wm2, QI_Wm2, L)
n += 1
if return_steps:
return (U_star, Re_star, ln_zt, ln_ze, Qs_Wm2, QI_Wm2, L)
else:
return (Qs_Wm2, QI_Wm2)
def calc_shf_melt(windspd, Qs_Wm2, airtemp):
""" Calculate melting (mm w.e.) by SHF """
if windspd > 2:
return Qs_Wm2 * 0.0107784
else:
if windspd/airtemp < 0.3:
return 0
else:
if (airtemp < 1.5) and (airtemp > -1.5) and (windspd < 1.5):
return 0
else:
if (airtemp < 2) and (airtemp > -2) and (windspd < 1):
return 0
else:
return Qs_Wm2 * 0.0107784
def calc_lhf_melt(windspd, QI_Wm2, airtemp):
""" Calculate melting (mm w.e.) by LHF """
if windspd > 2:
return QI_Wm2 * 0.0107784
else:
if (windspd / airtemp) < 0.3:
return 0
else:
if (airtemp < 1.5) and (airtemp > -1.5) and (windspd < 1.5):
return 0
else:
if (airtemp < 2) and (airtemp > -2) and (windspd < 1):
return 0
else:
return QI_Wm2 * 0.0107784
def calc_melt_total(SWR, LWR, SHF, LHF):
""" Sum melt components (mm w.e.) """
melt_total = SWR + LWR + SHF + LHF
if melt_total < 0:
return 0.
else:
return melt_total
def calculate_seb(lat, lon, lon_ref, day, time, summertime,
slope, aspect, elevation, met_elevation, lapse,
inswrad, avp, airtemp, windspd, albedo, roughness):
""" Convenience function to solve energy balance for a single timestep.
Returns:
(SWR, LWR, SHF, LHF)
"""
## Solar characteristics/quantities
dayang = calc_dayang(day)
eqtim = calc_eqtim(dayang)
solhour = calc_solhour(time, lon, lon_ref, eqtim, summertime)
soldec = calc_soldec(day)
solhran = calc_solhran(solhour)
solaltr = calc_solaltr(lat, soldec, solhran)
solaltd = calc_solaltd(solaltr)
cossolaz = calc_cossolaz(lat, solaltr, soldec)
sinolaz = calc_sinolaz(soldec, solhran, solaltr)
solaz = calc_solaz(sinolaz, cossolaz)
solaz360 = calc_solaz360(solaz)
## Calculate shortwave radiation
cloudn = calc_cloudn(inswrad, elevation, solaltr)
diffuser = calc_diffuser(cloudn)
directr = calc_directr(diffuser)
Qn = calc_Qn(directr, inswrad, solaltr)
Qi = calc_Qi(Qn, solaltr, slope, solaz, aspect)
# Net shortwave radiation
Q_Wm2 = calc_Q_Wm2(albedo, Qi, diffuser, inswrad, slope)
## Calculate longwave radiation
eo = calc_eo(airtemp, elevation, met_elevation, lapse)
e_star = calc_e_star(cloudn, eo)
lwin = calc_lwin(e_star, airtemp, lapse, elevation, met_elevation)
# Net longwave radiation
lnet_Wm2 = calc_lnet_Wm2(lwin)
## Calculate turbulent fluxes
atmpres = calc_atmpres(elevation)
de = calc_de(avp, elevation, met_elevation, lapse)
spehum = calc_spehum(avp, atmpres)
spehtair = calc_spehtair(spehum)
# Return individual sensible and latent heat fluxes
Qs_Wm2, QI_Wm2 = calc_turbulent_fluxes(windspd, roughness, spehtair,
airtemp, de, atmpres, lapse, elevation, met_elevation)
return (Q_Wm2, lnet_Wm2, Qs_Wm2, QI_Wm2)
def calculate_melt(swnet, lwnet, shf, lhf, windspd, airtemp):
""" Convert to melt quantities """
swnet_melt = calc_Q_melt(swnet)
lwnet_melt = calc_lnet_melt(lwnet)
SHF_melt = calc_shf_melt(windspd, shf, airtemp)
LHF_melt = calc_lhf_melt(windspd, lhf, airtemp)
melt_total = calc_melt_total(swnet_melt, lwnet_melt, SHF_melt, LHF_melt)
return (swnet_melt, lwnet_melt, SHF_melt, LHF_melt, melt_total)
"""
Not needed - these 'columns' are replicas of Qsfull and QIfull
def calc_Qs_Wm2(spehtair, winspd, airtemp, lapse, elevation, met_elevation, roughness, L, ln_zt):
return (1.225*spehtair*0.16*windspd*(airtemp-(lapse*(elevation-met_elevation))))/(((np.log(2/roughness))+(5*(2/L)))*(((np.log(2))-ln_zt)+(5*(2/L))))
def calc_QI_Wm2(windspd, de, atmpres, roughness, L, ln_ze):
return (1.225*0.622*2500000*0.16*windspd*(de/atmpres))/(((np.log(2/roughness))+(5*(2/L)))*(((np.log(2))-(ln_ze))+(5*(2/L))))
"""