-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatUnits.py
406 lines (342 loc) · 12.3 KB
/
HeatUnits.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
import math
cum_ddays = 0
def SiSine(ab, tmin, tmax, thresh):
# Purpose: To calculate Degree-Days accumulated above or below a threshold
# using a sine wave estimation of area under the curve
# Calls: No external routines
# Define data variables
# ab - True = Above, False = Below
# error - Returned error signal
# ddays - Degree-days returned
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrows)
# thresh - Threshold
# arg -
# mn - Minimum temperature
# mx - Maximum temperature
# pi -
# q -
# ta -
# tm -
# xa -
# initializations
ddays = 0
error = 0
halfpi = math.pi/2
#global ddays1
#print(ddays1)
if ab == True:
mn = tmin
mx = tmax
else:
mn = thresh - (tmax - thresh)
mx = thresh + (thresh - tmin)
# Compute Degree-days
if mn < mx:
if mx <= thresh:
return ddays
tm = 0.5 * (mx + mn)
ta = 0.5 * (mx - mn)
arg = (thresh - tm)/ta
if arg > 1.0:
arg = 1.0
if arg < -1.0:
arg = -1.0
# Approximate value of theta at arg
xa = abs(arg)
q = halfpi - math.sqrt(1.0 - xa) * (halfpi + xa * (-0.2121144 + xa * (0.0745610 - xa * 0.0187293)))
q = abs(q)
if arg < 0:
q = -1 * q
theta = q
theta = theta + halfpi
ddays = ((tm - thresh) * (math.pi - theta) + ta * math.sin(theta))/math.pi
elif mn == mx:
if mx > thresh:
ddays = mx - thresh
else:
error = 1
ddays1 = ddays
return ddays
def DoSine(ab, ci, tmin, tmax, thresh, tmin_tomm, tmax_yest):
# Purpose: To calculate Degree-Days accumulated above or below a threshold
# using a sine wave estimation of area under the curve
# Calls: SiSine
# Define data variables
# ab - True = Above, False = Below
# ci - Computation interval
# error - Returned error signal
# ddays - Degree-days returned
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrowa)
# thresh - Threshold
# initializations
ddays = 0
error = 0
# Compute Degree-Days
if ci == 1: # Minimum to Minimum
ddays = SiSine(ab, tmin, tmax, thresh) #, ddays, error)
if error != 0:
return
temp = SiSine(ab, tmin_tomm, tmax, thresh) #, temp, error)
else: # Maximum to Maximum
ddays = SiSine(ab, tmin, tmax_yest, thresh) #, ddays, error)
if error != 0:
return
temp = SiSine(ab, tmin, tmax, thresh) #, temp, error)
if error != 0:
return
print((ab, ci, tmin, tmin_tomm, tmax, tmax_yest, thresh, ddays, temp))
ddays = (ddays + temp) / 2.0
return ddays
def SiTria(ab, tmin, tmax, thresh):
# Purpose: To calculate Degree-Days accumulated above or below a threshold
# using a triangular estimation of area under the curve
# Calls: No external routines
# Define data variables
# ab - True = Above, False = Below
# error - Returned error signal
# ddays - Degree-days returned
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrowa)
# mn - Minimum temperature
# mx - Maximum temperature
# thresh - Threshold
# initializations
ddays = 0
error = 0
if ab == True:
mn = tmin
mx = tmax
else:
mn = thresh - (tmax - thresh)
mx = thresh + (thresh - tmin)
# Compute Degree-days
if mn < mx:
if mn < thresh:
if mx > thresh:
ddays = (0.5 * (mx-thresh) * (mx-thresh))/(mx-mn)
else:
ddays = 0.5 * (mx + mn - (2 * thresh))
elif mn == mx:
if mx > thresh:
ddays = mx - thresh
else:
error = 1
return ddays
def DoTria(ab, ci, tmin, tmax, thresh, tmin_tomm, tmax_yest): #, ddays, error):
# Purpose: To calculate Degree-Days accumulated above or below a threshold
# using a triangular estimation of area under the curve
# Calls: Sitria
# Define data variables
# ab - True = Above, False = Below
# ci - Computation interval
# error - Returned error signal
# ddays - Degree-days returned
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrowa)
# thresh - Threshold
# initializations
ddays = 0
error = 0
# Compute Degree-Days
if ci == 1: # Minimum to Minimum
ddays = SiTria(ab, tmin, tmax, thresh) #, dday, error)
if error != 0:
return
temp = SiTria(ab, tmin_tomm, tmax, thresh) #, temp, error)
else: # Maximum to Maximum
ddays = SiTria(ab, tmin, tmax_yest, thresh) #, dday, error)
if error != 0:
return
temp = SiTria(ab, tmin, tmax, thresh) #, temp, error)
if error != 0:
return
#print((ab, ci, tmin[0], tmin[1], tmax[0], tmax[1], thresh, ddays, temp))
ddays = (ddays + temp) / 2.0
return ddays
def VertCut(ab, method, tmin, tmax, thresh):
# Purpose: Calculate the area to be substracted from Degree-Days computed with
# a horizontal cut off to produce a vertical cut off.
# Calls: No external routines
# Define data variables
# ab - True = Above, False = Below
# method - 1: sine wave 2; triangular
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrowa)
# thresh - Threshold
# area -
# lt - local lower threshold
# mn - Minimum temperature
# mx - Maximum temperature
# q -
# ta =
# thresh - Thresholds (upper and lower threshold as list)
# tm - Average temperature
# ut - local upper threshold
# initializations
area = 0.0
pi = math.pi
lt = thresh[0]
if ab == 1:
mn = tmin
mx = tmax
ut = thresh[1]
else:
mn = lt - (tmax - lt)
mx = lt + (lt - tmin)
ut = lt + (lt - thresh[1])
# Check for appropriate conditions
if mx <= ut:
return area
if ((mn == mx) or (mn >= ut)):
return area
# Compute area for sine wave
if method == 1:
tm = 0.5 * (mx + mn)
ta = 0.5 * (mx - mn)
theta = math.asin((ut-tm)/ta)
q = pi - 2.0 * theta
area = q * (ut-lt)/(pi * 2.0)
else:
q = (mx - ut) / (mx - mn)
area = q * (ut-lt)
#print((ab, lt, mn, mx, ut, tm, ta, theta, q, area))
return area
def DoVrct(ab, method, ci, tmin, tmax, thresh, tmin_tomm, tmax_yest): #, Area):
# Purpose: produce vertical cutoff areas for double sine and double trianglar
# methods
# Calls: VertCut
# Define data variables
# ab - True = Above, False = Below
# method - Computation method
# ci - Computation interval
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrowa)
# thresh - Threshold
# area -
# temp -
# Compute for peak: min, max, min
if ci == 1:
area = VertCut(ab, method, tmin, tmax, thresh) #, area)
temp = VertCut(ab, method, tmin_tomm, tmax, thresh) #, temp)
else: # Compute for trough : max, min, max
area = VertCut(ab, method, tmin, tmax_yest, thresh) #, area)
temp = VertCut(ab, method, tmin, tmax, thresh) #, temp)
area = (area + temp) / 2.0
return area
def Huberm(tmin, tmax, lthres, uthres): #, HU, error):
# Purpose: To Calculate Degree-Days accumulated above a threshold using
# a Sine Wave estimation of area under the curve with reduced
# contributions above the upper threshold.
# Calls: No external routines
# Define data variables
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrowa)
# lthres - Lower threshold
# uthres - Upper threshold
# hu - Heat Units
# error - Returned error signal
# halfpi = a constant
# pi - a constant
# theta1 -
# theta2 -
# w -
pi = math.pi
halfpi = math.pi/2
error = 0
ddays = 0.0
if tmin > tmax:
error = 1
return ddays
mean = (tmax + tmin)/2.0
if tmin < lthres:
if tmax <= lthres:
return ddays
elif tmax <= uthres:
w = tmax - mean
theta1 = math.asin((lthres - mean)/w)
ddays = (w * math.cos(theta1) - (lthres - mean) * (halfpi - theta1))/pi
else:
w = tmax - mean
theta1 = math.asin((lthres - mean)/w)
theta2 = math.asin((uthres - mean)/w)
ddays = w * (math.cos(theta1) - math.cos(theta2)) - (lthres - mean) * (halfpi - theta1) +- (uthres - mean) * (halfpi - theta2)
ddays = ddays / pi
else:
if tmin < uthres:
if tmax <= uthres:
ddays = mean - lthres - 0.3
if ddays < 0.0:
ddays = 0.0
else:
w = tmax - mean
theta2 = math.asin((uthres - mean)/w)
ddays = (mean - lthres) - (w * math.cos(theta2) - (uthres - mean) * (halfpi - theta2))/pi
else:
ddays = uthres - lthres
return ddays
def HeatU(lthres, cthres, cm, coff, ci, tmin, tmax, tmin_tomm = 0, tmax_yest = 0): #, ndays, ddays, accum, error)
# Purpose: Calculate Degree-Days for one day from Max & min
# Calls: DoSine, DoTria, DoVrct, Huberm, SiSine, SiTria, VertCut
# Define data variables
# above - True for heat units
# cm - Computation method
# ci - Computation interval
# error - Returned error signal
# ddays - Degree-days returned
# tmax - Maximum temperature (yesterdays and todays)
# tmin - Minimum temperature (todays and tomorrowa)
# thresh - Thresholds
# temp1 - DD above cutoff threshold
# temp2 - additional DD for vertical cutoff
global cum_ddays
if (coff >= 2):
if (cm <= 2):
j = 1
else: #if (cm > 4):
j = 2
#print ("J Value is %s" % j)
above = 1
temp1 = 0.0
thresh = (lthres, cthres)
if(cm == 1): # Single sine
ddays = SiSine(above, tmin, tmax, lthres)
elif(cm == 2): # Double sine
ddays = DoSine(above, ci, tmin, tmax, lthres, tmin_tomm, tmax_yest)
elif(cm == 3): # Single triangle
ddays = SiTria(above, tmin, tmax, lthres)
elif(cm == 4): # Double triangle
ddays = DoTria(above, ci, tmin, tmax, lthres, tmin_tomm, tmax_yest)
elif(cm == 5): # Huber's method
ddays = Huberm(tmin, tmax, lthres, cthres) #, ddays[i], error)
return ddays
if (coff > 0): # Cutoff Active
# Compute degree-days
if(cm == 1): # Single sine
temp1 = SiSine(above, tmin, tmax, cthres)
elif(cm == 2): # Double sine
temp1 = DoSine(above, ci, tmin, tmax, cthres, tmin_tomm, tmax_yest)
elif(cm == 3): # Single triangle
temp1 = SiTria(above, tmin, tmax, cthres)
elif(cm == 4): # Double triangle
temp1 = DoTria(above, ci, tmin, tmax, cthres, tmin_tomm, tmax_yest)
ddays = ddays - temp1 # remove dd above cutoff
if coff >= 2: # Check for 2 = Int. or 3 = Vert. Cutoff
if((cm == 1) or (cm == 3)): # Single
temp2 = VertCut(above, j, tmin, tmax, thresh)
elif((cm >= 2) or (cm == 4)): # Double
temp2 = DoVrct(above, j, ci, tmin, tmax, thresh, tmin_tomm, tmax_yest)
#elif(cm == 3): # Single triangle
# temp2 = VertCut(above, j, tmin, tmax, thresh)
#elif(cm == 4): # Double triangle
# temp2 = DoVrct(above, j, ci, tmin, tmax, thresh)
if temp1 > temp2:
temp1 = temp2 # IC must be <= VC
if coff == 3: # Vertical cutoff
temp1 = temp2
ddays = ddays - temp1 # remove dd above cutoff
if ddays < 0.0:
ddays = 0.0
#cum_ddays = cum_ddays + ddays
return ddays