This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hbv_light.jl
342 lines (263 loc) · 8.28 KB
/
hbv_light.jl
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
# water to soil routine
# snow pack
# water content in snow
# meltwater
# refreezing meltwater
# boolean (snow at start of timestep?)
# Q_box0, Q_box1, Q_box2 - Q from box1 och box2
# SUZ, SLZ - storage in box1 och box2
# Q_gen - Q before MAXBAS
# Packages
using Distributions
using PyPlot
using CSV
# Function for computing ordinates of unit hydrograph
function compute_hbv_ord(maxbas, tstep)
maxbas = maxbas / tstep
triang = Distributions.TriangularDist(0, maxbas)
triang_cdf = Distributions.cdf(triang, 0:ceil(Int64, maxbas + 2))
hbv_ord = diff(triang_cdf)
return(hbv_ord)
end
# Compute monthly potential evapotranspiration
function epot_monthly(datevec)
# Monthly values of potential evapotranspiration (mm/day)
epot_month = [0.05, 0.14, 0.46, 1.5, 3.01, 4.15, 3.66, 2.72, 1.42, 0.43, 0.03, 0.0]
# Assign montly values to days
epot = zeros(Float64, length(datevec))
for i in eachindex(datevec)
imonth = Dates.month(datevec[i])
epot[i] = epot_month[imonth]
end
return(epot)
end
# Input data
data = CSV.read("C:\\Work\\Studies\\vann\\hbv_light\\ptq.txt",
types = [DateTime, Float64, Float64, Float64],
delim = "\t", dateformat = "yyyymmdd", header = true)
date = Array(data[:date])
Prec = Array(data[:Prec])
temp = Array(data[:Temp])
Qobs = Array(data[:Qsim])
Prec = transpose(Prec)
temp = transpose(temp)
pET = epot_monthly(date)
# Settings
UseOldSUZ = true
# Elevation and vegetation zones
lake = 0.0
nelev = size(Prec, 1)
nveg = 1
area = ones(nveg, nelev)
area = area / sum(area)
# Parameter values
PERC = 0.7
K0 = 0.2
K1 = 0.08
K2 = 0.03
UZL = 20.0
par_TT = [-1.0]
par_CFMAX = [5.0]
par_SFCF = [0.8]
par_CFR = [0.05]
par_CWH = [0.1]
par_FC = [250.0]
par_LP = [0.7]
par_BETA = [3.0]
par_MAXBAS = 2.5*24.0
tstep = 24.0
maxTS = size(Prec,2)
# Initilize state variables
state_SP = zeros(nveg, nelev)
state_WC = zeros(nveg, nelev)
state_SM = zeros(nveg, nelev)
# Temporary
runoff_sim = zeros(size(Prec,2))
snow_sim = zeros(size(Prec,2))
SM_sim = zeros(size(Prec,2))
SUZ_sim = zeros(size(Prec,2))
SLZ_sim = zeros(size(Prec,2))
aET_sim = zeros(size(Prec,2))
recharge_sim = zeros(size(Prec,2))
Q_com = zeros(size(Prec,2))
# Compute maxbas weights
maxbas_w = compute_hbv_ord(par_MAXBAS, tstep)
# Initilize state variables
SUZ = 0.0
SLZ = min(PERC / K2, Qobs[1] / K2)
Q_box0 = 0.0
for t = 1:size(Prec, 2)
pot_E = pET[t] * (1 - lake)
till_Qsum = 0.0
avg_act_E = 0.0
for elevzone = 1:nelev
P_zone = Prec[elevzone, t]
T_zone = temp[elevzone, t]
for vegzone = 1:nveg
if area[vegzone, elevzone] > 0.0
SP = state_SP[vegzone, elevzone]
WC = state_WC[vegzone, elevzone]
SM = state_SM[vegzone, elevzone]
tt = par_TT[vegzone]
CFMAX = par_CFMAX[vegzone]
SFCF = par_SFCF[vegzone]
CFR = par_CFR[vegzone]
CWH = par_CWH[vegzone]
FC = par_FC[vegzone]
LP = par_LP[vegzone]
BETA = par_BETA[vegzone]
snow = (SP > 0)
#snow routine
insoil = 0.0
if SP > 0.0
if P_zone > 0.0
if T_zone > tt
WC = WC + P_zone
else
SP = SP + P_zone * SFCF
end
end # if P_zone
if T_zone > tt
melt = CFMAX * (T_zone - tt)
if melt > SP
insoil = SP + WC
WC = 0.0
SP = 0.0
else
SP = SP - melt
WC = WC + melt
if WC >= CWH * SP
insoil = WC - CWH * SP
WC = CWH * SP
end
end
else
refrez = CFR * CFMAX * (tt - T_zone)
if refrez > WC
refrez = WC
end
SP = SP + refrez
WC = WC - refrez
end # if T_zone
else
if T_zone > tt
insoil = P_zone
else
SP = P_zone * SFCF
end # if T_zone
end # if SP
#soil routine
till_Q = 0.0
old_SM = SM
if insoil > 0.0
if insoil < 1.0
y = insoil
else
m = floor(insoil) # IS THIS CORRECT
y = insoil - m
for i in 1:m
dQdP = (SM / FC) ^ BETA
if dQdP > 1.0
dQdP = 1.0
end
SM = SM + 1.0 - dQdP
till_Q = till_Q + dQdP
end
end
dQdP = (SM / FC) ^ BETA
if dQdP > 1.0
dQdP = 1.0
end
SM = SM + (1 - dQdP) * y
till_Q = till_Q + dQdP * y
end # if insoil
mean_SM = (SM + old_SM) / 2.0
if mean_SM < (LP * FC)
act_E = pot_E * mean_SM / (LP * FC)
else
act_E = pot_E
end
if snow
act_E = 0.0
end
SM = SM - act_E
if SM < 0.0
SM = 0.0
end
avg_act_E = avg_act_E + act_E * area[vegzone, elevzone]
till_Qsum = till_Qsum + till_Q * area[vegzone, elevzone]
state_SP[vegzone, elevzone] = SP
state_WC[vegzone, elevzone] = WC
state_SM[vegzone, elevzone] = SM
end # if area
end # veg loop
end # elev loop
till_box1 = till_Qsum
# generation of runoff
SUZ = SUZ + till_box1
if ( SUZ - PERC / (1 - lake) ) < 0.0
SLZ = SLZ + SUZ * (1.0 - lake)
SUZ = 0.0
else
SLZ = SLZ + PERC
SUZ = SUZ - PERC / (1.0 - lake)
end
tt = mean(par_TT) # CHECK WITH JAN WHAT THIS MEANS
SFCF = mean(SFCF) # CHECK WITH JAN WHAT THIS MEANS
if mean(temp[:,t]) > tt # CHECK WITH JAN WHAT THIS MEANS
SLZ = max(SLZ - pET[t] * lake, 0.0)
avg_act_E = avg_act_E + min(SLZ, pET[t] * lake)
end
if mean(temp[:,t]) <= tt # CHECK WITH JAN WHAT THIS MEANS
SLZ = SLZ + SFCF * mean(Prec[:,t]) * lake # CHECK WITH JAN WHAT THIS MEANS
else
SLZ = SLZ + mean(Prec[:,t]) * lake # CHECK WITH JAN WHAT THIS MEANS
end
if UseOldSUZ
Q_box1 = K1 * SUZ
if SUZ < UZL
Q_box0 = 0.0
else
Q_box0 = K0 * (SUZ - UZL)
end
else
Q_box1 = min(K1 * SUZ ^ (1 + UZL), SUZ)
end
Q_box2 = K2 * SLZ
SUZ = SUZ - Q_box1 - Q_box0
SLZ = SLZ - Q_box2
Q_gen = Q_box1 + Q_box2 + Q_box0
# transformation of runoff
for i = 0:(length(maxbas_w)-1)
if t + i <= maxTS
Q_com[t + i] = Q_com[t + i] + Q_gen * maxbas_w[i+1]
end
end
avg_SP = 0.0
avg_SM = 0.0
avg_WC = 0.0
for elevzone = 1:nelev
for vegzone = 1:nveg
avg_SP = avg_SP + state_SP[vegzone, elevzone] * area[vegzone, elevzone]
avg_WC = avg_WC + state_WC[vegzone, elevzone] * area[vegzone, elevzone]
avg_SM = avg_SM + state_SM[vegzone, elevzone] * area[vegzone, elevzone]
end
end
runoff_sim[t] = Q_gen
snow_sim[t] = (avg_SP + avg_WC) / (1 - lake)
SM_sim[t] = avg_SM / (1 - lake)
SUZ_sim[t] = SUZ
SLZ_sim[t] = SLZ
aET_sim[t] = avg_act_E
recharge_sim[t] = till_Qsum
end # t loop
plot(date, Q_com, label = "Julia code")
plot(date, Qobs, label = "Original code")
ylabel("Runoff (mm/day)")
@show runoff_sim
#=plot(snow_sim)
plot(SM_sim)
plot(SUZ_sim)
plot(SLZ_sim)
plot(aET_sim)
plot(recharge_sim)=#