-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_app.py
377 lines (329 loc) · 12.6 KB
/
flask_app.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
from flask import Flask, render_template, request, session
from petprocessing import petcalc
from petprocessingprognose import petcalcprognose
import numpy as np
import Solweig_v2015_metdata_noload as metload
import clearnessindex_2013b as ci
import requests
import json
import base64
import pandas as pd
app = Flask(__name__)
app.config["DEBUG"] = True
app.config["SECRET_KEY"] = "klefiedoedfoiefnnoefnveodf"
calcresult = []
result = []
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/petresult', methods=["GET", "POST"])
def petresult():
return render_template("petresult.html", result=result)
@app.route('/prognoseerror', methods=["GET", "POST"])
def prognoseerror():
return render_template("prognoseerror.html", result=result)
@app.route('/prognose', methods=["GET", "POST"])
def prognose():
if request.method == "GET":
return render_template("prognose.html")
if request.method == "POST":
city = request.form["city"]
if city == 'Gothenburg, Sweden':
url = 'https://api.github.com/repos/David-Rayner-GVC/pet_data/contents/json/Gothenburg.json'
lat = 57.7
lon = 12.0
UTC = 1
else:
return render_template("prognoseerror.html", result='Prognose for ' + city + ', not found.')
# download data from github
req = requests.get(url)
if req.status_code == requests.codes.ok:
req = req.json() # the response is a JSON
content = base64.b64decode(req['content'])
else:
return render_template("petprognoseresult.html", result='Content was not found.')
dict_loaded = json.loads(content)
for key, value in dict_loaded['data_vars'].items():
dict_loaded['data_vars'][key]['data'] = [np.nan if isinstance(x,str) else x for x in value['data'] ]
timestamp = dict_loaded['coords']['time']['data']
# putting data in separate vectors
veclen = timestamp.__len__()
year = np.empty(veclen, int)
month = np.empty(veclen, int)
day = np.empty(veclen, int)
hour = np.empty(veclen, int)
minu = np.empty(veclen, int)
year = np.empty(veclen, int)
Ta = np.empty(veclen, float)
RH = np.empty(veclen, float)
radD = np.empty(veclen, float)
radI = np.empty(veclen, float)
radG = np.empty(veclen, float)
Ws = np.empty(veclen, float)
for i in range(0, veclen):
year[i] = int(timestamp[i][0:4])
month[i] = int(timestamp[i][5:7])
day[i] = int(timestamp[i][8:10])
hour[i] = int(timestamp[i][11:13])
minu[i] = int(timestamp[i][14:16])
Ta[i] = float(dict_loaded['data_vars']['air_temperature']['data'][i])
RH[i] = float(dict_loaded['data_vars']['relative_humidity']['data'][i])
radD[i] = float(dict_loaded['data_vars']['downward_diffuse']['data'][i])
radI[i] = float(dict_loaded['data_vars']['downward_direct']['data'][i])
Ws[i] = np.sqrt(float(dict_loaded['data_vars']['eastward_wind']['data'][i])**2 + float(dict_loaded['data_vars']['northward_wind']['data'][i])**2)
with np.errstate(invalid='ignore'):
radI[radI < 0.] = 0.
radD[radD < 0.] = 0.
radG = radD + radI
# re-create xarray Dataset
#x_loaded = Dataset.from_dict(dict_loaded)
# putting data in separate vectors
#year = np.empty((x_loaded.air_temperature.time.shape[0]), int)
#uResponse = requests.get(uri)
#try:
# uResponse = requests.get(uri)
#except requests.ConnectionError:
# return "Connection Error"
#Jresponse = uResponse.text
#result = json.loads(Jresponse)
#with urllib.request.urlopen(uri) as url:
# result = json.loads(url.read().decode())
#return '''
# <html>
# <body>
# <div class="container">
## <p>{result}</p>
# <p><a href="/">Click here to calculate again</a>
# <div>
# </body>
# </html>
# '''.format(result=city)
poi_save = petcalcprognose(Ta, RH, Ws, radG, radD, radI, year, month, day, hour, minu, lat, lon, UTC)
tab = pd.DataFrame(poi_save[1:,[1,2,22,24,26,33]])
tab.columns = ['Day of Year', 'Hour','T_air','RH','Tmrt', 'PET']
tabhtml = tab.to_html(classes='data', header="true")
doy = poi_save[1:, 1]
hour = poi_save[1:, 2]
petres = poi_save[:,26]
#petres = str(round(poi_save[:,26], 1))
return render_template("petprognoseresult.html", result1=doy, result2=hour, result3=tabhtml)
@app.route('/petprognoseresult', methods=["GET", "POST"])
def petprognoseresult():
if request.method == "GET":
return render_template("petprognoseresult.html", result1=result1, result2=result2, result3=result3)
if request.method == "POST":
return render_template("petprognoseresult.html", result1=result1, result2=result2, result3=result3)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return render_template("main_page.html", calcresult=calcresult)
if request.method == "POST":
try:
month = int(request.form["month"])
except:
month = -999
try:
day = int(request.form["day"])
except:
day = -999
try:
hour = int(request.form["hour"])
except:
hour = -999
year = 2019
minu = 30
try:
Ta = float(request.form["Ta"])
except:
Ta = -999
try:
RH = float(request.form["RH"])
except:
RH = -999
try:
Ws = float(request.form["Ws"])
except:
Ws = -999
#try:
# radG = float(request.form["radG"])
#except:
# errors += "<p>{!r} is not a number.</p>\n".format(request.form["radG"])
sky = request.form["sky"]
if month > 12 or month < 0:
return render_template("petresult.html", result="Incorrect month filled in")
if day > 31 or day < 0:
return render_template("petresult.html", result="Incorrect day filled in")
if hour > 23 or hour < 0:
return render_template("petresult.html", result="Incorrect hour filled in")
if Ta > 60 or Ta < -60:
return render_template("petresult.html", result="Unreasonable air temperature filled in")
if RH > 100 or RH < 0:
return render_template("petresult.html", result="Unreasonable relative humidity filled in")
if Ws > 100 or Ws < 0:
return render_template("petresult.html", result="Unreasonable Wind speed filled in")
#day of year
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
leapyear = 1
else:
leapyear = 0
else:
leapyear = 1
else:
leapyear = 0
if leapyear == 1:
dayspermonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
dayspermonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
doy = np.sum(dayspermonth[0:month - 1]) + day
# Currently looked to Gothenburg
location = {'longitude': 12.0, 'latitude': 57.7, 'altitude': 3.}
UTC = 1
# Radiation
P = -999.
radG = 40.
metdata = np.zeros((1, 24)) - 999.
metdata[0, 0] = year
metdata[0, 1] = doy
metdata[0, 2] = hour
metdata[0, 3] = minu
metdata[0, 11] = Ta
metdata[0, 10] = RH
YYYY, altitude, azimuth, zen, jday, leafon, dectime, altmax = metload.Solweig_2015a_metdata_noload(metdata, location, UTC)
if altitude > 0.:
I0, _, Kt, _, _ = ci.clearnessindex_2013b(zen, jday, Ta, RH / 100., radG, location, P)
if sky == "Clear (100%)":
radG = I0
elif sky == "Semi-cloudy (80%)":
radG = I0 * 0.8
elif sky == "Cloudy (60%)":
radG = I0 * 0.6
else:
radG = I0 * 0.4
I0, _, Kt, _, _ = ci.clearnessindex_2013b(zen, jday, Ta, RH / 100., radG, location, P)
else:
radG = 0.
# Main calculation
if Ta is not None and RH is not None and Ws is not None and radG is not None:
Tmrt, resultPET, resultUTCI = petcalc(Ta, RH, Ws, radG, year, month, day, hour, minu)
result = str(round(resultPET, 1))
return render_template("petresult.html", result=result)
#'''
# <html>
# <body>
# <div class="container">
# <p><font size="14">{result}</font></p>
# <p><a href="/"><font size="10">Click here to calculate again</font></a>
# <div>
# </body>
# </html>
#'''.format(result=testout)
#
#
#from flask import Flask, request, session
#
#from processing import calculate_mode
#
#app = Flask(__name__)
#app.config["DEBUG"] = True
#app.config["SECRET_KEY"] = "klefiedoedfoiefnnoefnveodf"
#
##inputs = []
#
#@app.route("/", methods=["GET", "POST"])
#def mode_page():
# if "inputs" not in session:
# session["inputs"] = []
# errors = ""
# if request.method == "POST":
# try:
# #inputs.append(float(request.form["number"]))
# session["inputs"].append(float(request.form["number"]))
# session.modified = True
# except:
# errors += "<p>{!r} is not a number.</p>\n".format(request.form["number"])
#
# if request.form["action"] == "Calculate number":
# #result = calculate_mode(inputs)
# result = calculate_mode(session["inputs"])
# #inputs.clear()
# session["inputs"].clear()
# session.modified = True
# return '''
# <html>
# <body>
# <p>{result}</p>
# <p><a href="/">Click here to calculate again</a>
# </body>
# </html>
# '''.format(result=result)
#
## if len(inputs) == 0:
# if len(session["inputs"]) == 0:
# numbers_so_far = ""
# else:
# numbers_so_far = "<p>Numbers so far:</p>"
# for number in session["inputs"]:
# #for number in inputs:
# numbers_so_far += "<p>{}</p>".format(number)
#
# return '''
# <html>
# <body>
# {numbers_so_far}
# {errors}
# <p>Enter your number:</p>
# <form method="post" action=".">
# <p><input name="number" /></p>
# <p><input type="submit" name="action" value="Add another" /></p>
# <p><input type="submit" name="action" value="Calculate number" /></p>
# </form>
# </body>
# </html>
# '''.format(numbers_so_far=numbers_so_far, errors=errors)
#
#from flask import Flask, request
#from processing import do_calculation
#
#app = Flask(__name__)
#app.config["DEBUG"] = True
#
#@app.route("/", methods=["GET", "POST"])
#def adder_page():
# errors = ""
# if request.method == "POST":
# number1 = None
# number2 = None
# try:
# number1 = float(request.form["number1"])
# except:
# errors += "<p>{!r} is not a number.</p>\n".format(request.form["number1"])
# try:
# number2 = float(request.form["number2"])
# except:
# errors += "<p>{!r} is not a number.</p>\n".format(request.form["number2"])
# if number1 is not None and number2 is not None:
# result = do_calculation(number1, number2)
# return '''
# <html>
# <body>
# <p>The result is {result}</p>
# <p><a href="/">Click here to calculate again</a>
# </body>
# </html>
# '''.format(result=result)
#
# return '''
# <html>
# <body>
# {errors}
# <p>Enter your numbers:</p>
# <form method="post" action=".">
# <p><input name="number1" /></p>
# <p><input name="number2" /></p>
# <p><input type="submit" value="Do calculation" /></p>
# </form>
# </body>
# </html>
# '''.format(errors=errors)