-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproto-DEPRECATED-073124.py
1623 lines (1374 loc) · 72.8 KB
/
proto-DEPRECATED-073124.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, jsonify, Response
from flask_cors import CORS
from threading import Thread
from time import sleep
import time
import datetime
import json
import pandas as pd
import argparse
import asyncio
import calendar
import hashlib
import shutil
import os
import requests
import urllib
import glob
import calendar
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('agg')
from dw_tap.data_fetching import getData
from v2 import validated_params_v2_w_year
from v2 import validated_params_v2
from v2 import validated_latlon
from hsds_helpers import connected_hsds_file
from bc import bc_for_point
from infomap import get_infomap_script
from windrose import WindroseAxes
from powercurve import PowerCurve
from shapely.geometry import Point
import geopandas as gpd
import heapq
import scipy.interpolate
import numpy as np
from html_maker import *
import flask
# Start of WTK-LED fucntions
# Code below is copied from the uncertainty notebook by Caleb Phillips
# This function fetches the grid point index and returns as an indexed geodataframe
def get_index():
# For sample:
#base_url = "https://wtk-csv-testing-dav-sandbox.s3.us-west-2.amazonaws.com"
base_url = "https://wtk-led.s3.us-west-2.amazonaws.com"
file_path = f"/location_index.csv.gz"
index = gpd.GeoDataFrame(pd.read_csv(base_url+file_path))
index['location'] = gpd.GeoSeries(gpd.points_from_xy(index.longitude, index.latitude))
return index
wtkled_index = get_index()
# This function finds the closest grid point to the given lat/lon
# Note that a faster method might use index.location.sindex, the STRTree optimized
# spatial index, but support is not universal and depends on specific versions of Geos and PyGeos:
# https://pygeos.readthedocs.io/en/latest/strtree.html
# Hence, we're doing the more universally supported, but less optimized thing here
def closest_grid_point(index,lat,lon):
p = Point(lon,lat)
r = index.iloc[index.location.distance(p).argmin()].copy()
r['distance_degrees'] = Point(r['longitude'],r['latitude']).distance(p)
return r
def parse_mohr(mohr):
s = str(mohr)
m, h = s[:-2], s[-2:]
return int(m), int(h)
def get_1224(idx, year=2020, add_year_col=False):
# For sample:
#base_url = "https://wtk-csv-testing-dav-sandbox.s3.us-west-2.amazonaws.com/1224"
base_url = "https://wtk-led.s3.us-west-2.amazonaws.com/1224"
file_path = f"/year={year}/varset=all/index={idx}/{idx}_{year}_all.csv.gz"
res = pd.read_csv(base_url + file_path)
res["m"], res["h"] = zip(*res["mohr"].apply(parse_mohr))
if add_year_col:
res["year"] = year
return res
def get_1224_20yrs(idx, ws_col_for_estimating_power, selected_powercurve, relevant_columns_only=True):
# Example of the dataframe returned by this func (wtih relevant_columns_only=True):
#
# year mohr month h windspeed_40m windspeed_40m_kw winddirection_40m
# 0 2001 101 Jan 1 4.31 10.765848 334.29
# 1 2001 102 Jan 2 5.07 17.058348 323.40
# 2 2001 103 Jan 3 5.38 20.721432 302.85
df = pd.concat([get_1224(idx, year=yr, add_year_col=True) for yr in range(2001, 2021)])
df.rename(columns={"m": "month"}, inplace=True)
# df.month = df.month.apply(lambda x: calendar.month_abbr[x]) -- converting months to Jan, Feb, etc. should happen last to avoid messing up the other of months
if ws_col_for_estimating_power in df.columns:
df[ws_col_for_estimating_power + "_kw"] = selected_powercurve.windspeed_to_kw(df, ws_col_for_estimating_power)
if relevant_columns_only:
wd_col = ws_col_for_estimating_power.replace("speed", "direction")
relevant_columns = ["year", "mohr", "month", "h", ws_col_for_estimating_power, ws_col_for_estimating_power + "_kw", wd_col]
return df[relevant_columns]
else:
return df
def df2yearly_avg(df, ws_column, kw_column):
tmp = df.drop(columns=["mohr", "month", "h"] + [x for x in df.columns if "winddirection" in x])
#res = tmp.groupby("year").agg("mean")
res = tmp.groupby("year").agg(avg_ws=(ws_column, "mean"), kwh_total=(kw_column, "sum")) # kwh_total will sum for all months and all hours
res["kwh_total"] = res["kwh_total"] * 30 # Coarse estimation: 30 days in every month; no need to /20.0 for individual years
res.rename(columns={"avg_ws": "Average wind speed, m/s", "kwh_total": "kWh produced"}, inplace=True)
# res["kWh produced"] = res["kWh produced"].astype(float).map('{:,.0f}'.format)
# res["Average wind speed, m/s"] = res["Average wind speed, m/s"].astype(float).map('{:,.2f}'.format)
# Return entired dataframe, with all years
# return res
# Create df with low, avg, high year
res = res.sort_values("Average wind speed, m/s")
#return res
res_avg = pd.DataFrame(res.mean()).T
res_avg.index=["Average year"]
#return res_avg
res_3years = pd.concat([res.iloc[[0]], res_avg, res.iloc[[-1]]])
res_3years["kWh produced"] = res_3years["kWh produced"].astype(float).map('{:,.0f}'.format)
res_3years["Average wind speed, m/s"] = res_3years["Average wind speed, m/s"].astype(float).map('{:,.2f}'.format)
res_3years.index = ["Lowest year (%s)" % str(res_3years.index[0]), \
res_3years.index[1], \
"Highest year (%s)" % str(res_3years.index[2])]
return res_3years
def df2monthly_avg(df, ws_column, kw_column):
tmp = df.drop(columns=["year", "mohr", "h"] + [x for x in df.columns if "winddirection" in x])
#res = tmp.groupby("month").agg("mean")
res = tmp.groupby("month").agg(avg_ws=(ws_column, "mean"), kwh_total=(kw_column, "sum")) # kwh_total will sum for all hours and all years
res["kwh_total"] = res["kwh_total"] * 30 / 20.0 # Coarse estimation: 30 days in every month & 20 years
res.rename(columns={"avg_ws": "Average wind speed, m/s", "kwh_total": "kWh produced"}, inplace=True)
res.index = pd.Series(res.index).apply(lambda x: calendar.month_abbr[x])
res["kWh produced"] = res["kWh produced"].astype(float).map('{:,.0f}'.format)
res["Average wind speed, m/s"] = res["Average wind speed, m/s"].astype(float).map('{:,.2f}'.format)
return res
def yearly_avg_df_to_closest_heights(yearly_avg, selected_height, heights_count=1):
# Use columns that are of format: windspeed_XYZm where XYZ are integers
all_cols = yearly_avg.columns
heights = [int(c.split("_")[1].rstrip("m")) for c in yearly_avg.columns if "windspeed" in c]
#return heights
closest_heights = heapq.nsmallest(heights_count, heights, key=lambda x: abs(x-selected_height))
closest_heights_columns = []
for h in closest_heights:
c = "windspeed_%dm" % h
if c in all_cols:
closest_heights_columns.append(c)
# Leave out winddirection columns for now
# c = "winddirection_%dm" % h
# if c in all_cols:
# closest_heights_columns.append(c)
return closest_heights, closest_heights_columns
def get_uncertainty_dataframe(location,height=40):
# For sample:
#base_url = "https://wtk-csv-testing-dav-sandbox.s3.us-west-2.amazonaws.com/uncertainty"
base_url = "https://wtk-led.s3.us-west-2.amazonaws.com/uncertainty"
file_path = f"/height={height}/index={location}/{location}_{height}m.csv.gz"
return pd.read_csv(base_url+file_path)
# End of WTK-LED fucntions
def feasibility_thresh_by_height(h):
"""
From literature (shared by Heidi):
For a small wind turbine hub height of 30 m, 4.0 m/s (9 mph)
is typically cited as the minimum annual average wind speed
required for a feasible project.
For a large wind turbine hub height of 80 m,
a minimum annual average wind speed of 6.5 m/s (14.5 mph) is typically needed.
"""
y_interp = scipy.interpolate.interp1d([30, 80], [4.0, 6.5], fill_value="extrapolate")
return y_interp(h)
server_started_at = datetime.datetime.now()
if "GOOGLE_MAPS_API_KEY" in os.environ:
given_google_maps_api_key = os.environ.get('GOOGLE_MAPS_API_KEY')
else:
given_google_maps_api_key = ""
# Necessary directories: create if not there
outputs_dir = "outputs"
if not os.path.exists(outputs_dir):
os.mkdir(outputs_dir)
# pending_dir = "static/pending/"
# if not os.path.exists(pending_dir):
# os.mkdir(pending_dir)
completed_dir = "static/completed/"
if not os.path.exists(completed_dir):
os.mkdir(completed_dir)
templates_dir = "templates"
if not os.path.exists("%s/served" % templates_dir):
os.mkdir("%s/served" % templates_dir)
# Csv and png outputs will be saved in the following dirs;
# Having them live inside static is important becuase it makes them accessible to the outside
csv_dir = "static/raw"
if not os.path.exists(csv_dir):
os.mkdir(csv_dir)
plot_dir = "static/plots/"
if not os.path.exists(plot_dir):
os.mkdir(plot_dir)
powercurves_dir = "powercurves"
# Create dict with keys being names of all available power curves (without extensions)
# and values being the corresponding PowerCurve objects
powercurves = {}
powercurve_default = ""
for fname in glob.glob(powercurves_dir + '/*.csv'):
powercurve_name = os.path.basename(fname).replace(".csv", "")
powercurves[powercurve_name] = PowerCurve(fname)
if powercurve_name == "nrel-reference-100kW":
powercurve_default = powercurve_name
if not powercurve_default:
powercurve_default = powercurves.keys()[0]
# def instantiate_from_template(src, dest, old_text, new_text):
# """ Copy src file to dest with replacement of old_text with new_text """
# # This version performs single substring replacement: old_text -> new_text
#
# fin = open(src)
# fout = open(dest, "wt")
# for line in fin:
# fout.write(line.replace(old_text, new_text))
# fin.close()
# fout.close()
def instantiate_from_template(src, dest, replacements):
""" Copy src file to dest with replacement of old_text with new_text """
# This version performs multiple substring replacement, using each tuple provided in the replacements list
# Each element there is: old_text -> new_text
fin = open(src)
fout = open(dest, "wt")
for line in fin:
updated_line = line
for r in replacements:
old_text, new_text = r[0], r[1]
updated_line = updated_line.replace(old_text, new_text)
fout.write(updated_line)
fin.close()
fout.close()
def plot_monthly_avg(atmospheric_df, ws_column="ws", datetime_column="datetime",
title="Windspeed monthly averages",
show_avg_across_years=True,
label_avg_across_years=True,
save_to_file=True,
show_overall_avg=True,
show=True):
if ws_column not in atmospheric_df.columns:
raise ValueError("Can't find %s column in dataframe. Skipping plotting" % ws_column)
if datetime_column not in atmospheric_df.columns:
raise ValueError("Can't find %s column in dataframe. Skipping plotting" % datetime_column)
df = atmospheric_df[[datetime_column, ws_column]].copy()
year_month = pd.Series(pd.PeriodIndex(df[datetime_column], freq="M"))
df["month"] = year_month.apply(lambda x: x.month)
df["year"] = year_month.apply(lambda x: x.year)
#display(df)
fig, ax = plt.subplots(figsize=(10, 3))
xvals = list(range(1, 13)) # for showing monthly data
nyears = df["year"].nunique()
if nyears > 1:
for year, grp in df.groupby("year"):
monthly_avg = grp[[ws_column, "month"]].groupby("month").agg("mean")
ax.plot(monthly_avg, label=str(year), linestyle="--", alpha=0.4)
if show_avg_across_years:
monthly_avg_across_years = df.groupby("month")[ws_column].agg("mean")
ax.plot(monthly_avg_across_years, label="Avg across years (labeled)", marker="o")
if label_avg_across_years:
ylim0 = ax.get_ylim()[0]
ylim1 = ax.get_ylim()[1]
yoffset = ylim1 / 20 # express offest as a fraction of height
yvals = pd.Series(monthly_avg_across_years.tolist())
a = pd.concat({'x': pd.Series(xvals),
'y': yvals,
'val': yvals}, axis=1)
for i, point in a.iterrows():
t = ax.text(point['x'], point['y'] + yoffset, "%.2f" % point['val'], fontsize=7)
t.set_bbox(dict(facecolor='lightgray', alpha=0.75, edgecolor='red'))
ax.set_ylim([ylim0, ylim1*1.25])
else:
single_year = df["year"].tolist()[0]
monthly_avg_across_years = df.groupby("month")[ws_column].agg("mean")
ax.plot(monthly_avg_across_years, linestyle="--", label="Year: " + str(single_year), marker="o")
if label_avg_across_years:
ylim0 = ax.get_ylim()[0]
ylim1 = ax.get_ylim()[1]
yoffset = ylim1 / 20 # express offest as a fraction of height
yvals = pd.Series(monthly_avg_across_years.tolist())
a = pd.concat({'x': pd.Series(xvals),
'y': yvals,
'val': yvals}, axis=1)
for i, point in a.iterrows():
t = ax.text(point['x'], point['y'] + yoffset, "%.2f" % point['val'], fontsize=7)
t.set_bbox(dict(facecolor='lightgray', alpha=0.75, edgecolor='red'))
ax.set_ylim([ylim0, ylim1*1.25])
ax.set_xticks(xvals)
ax.set_xticklabels(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
ax.set_ylabel("Monthly avg windspeed, m/s")
ax.set_title(title)
# plt.figtext(0.1, -0.05,
# "Code used to produce this figure is developed under NREL's TAP project "
# "(https://www.nrel.gov/wind/tools-assessing-performance.html)",
# ha="left", fontsize=6)
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5));
if show_overall_avg:
ax.set_xlim([0, 16])
overall_avg = df[ws_column].mean()
ax.axhline(y=overall_avg,linestyle="dotted", color="orange", linewidth=2.0)
t = ax.text(13.0, overall_avg + yoffset, "Overall avg=%.2f" % overall_avg, fontsize=8)
t.set_bbox(dict(facecolor='orange', alpha=0.3, edgecolor='black'))
if save_to_file == True:
plt.savefig('%s.png' % title, dpi=300)
elif type(save_to_file) == str:
plt.savefig(save_to_file, dpi=300)
if show:
plt.show()
def plot_windrose(df, ws_column="ws", wd_column="wd", save_to_file=True):
ax = WindroseAxes.from_ax()
ax.bar(df[wd_column], df[ws_column], normed=True, opening=0.8, edgecolor='white')
ax.set_legend()
if save_to_file == True:
plt.savefig('%s.png' % title, dpi=300)
elif type(save_to_file) == str:
plt.savefig(save_to_file, dpi=300)
def timeseries_to_12_by_24(df, styling=True, format="html"):
res = df
res["datetime"] = pd.to_datetime(res["datetime"])
res["hour"] = res["datetime"].apply(lambda x: x.hour)
res["month"] = res["datetime"].apply(lambda x: x.month)
res = res.groupby(["hour", "month"]).agg(ws_avg=("ws", "mean"))
res = res.stack().unstack(level=1)
res.index = ["Hour " + str(el[0]) for el in res.index] # range(len(res))
res.columns.name = ""
res.rename(columns={c:calendar.month_name[c][:3] for c in res.columns}, inplace=True)
min_ws = res.min().min()
max_ws = res.max().max()
overall_mean = res.mean().mean()
if styling:
res = res.style.background_gradient("BuPu", axis=1).format(precision=3).set_caption("Overall average: <strong>%.3f</strong>" % overall_mean)
if format=="dataframe":
return res
elif format=="html":
#html = res.to_html(classes='12_by_24')
# Add colormap image
#html += "<div id=\"colormap_wrapper\"><p id=\"colormap_min\">Min=%.3f</p><img id=\"colormap\" src=\"static/colormap.png\"/></div>" % (min_ws)
html = "<div class=\"centered\">%s</div>" % (res.to_html(table_id='12_by_24'))
# <tr>
# <td id="colormap_min_cell">Min=%.3f</td>
# <td id="colormap_max_cell">Max=%.3f</td>
# </tr>
# <tr>
# <td colspan=2><img id=\"colormap\" src=\"static/colormap.png\"/></td>
# </tr>
# """ % (min_ws, max_ws)
return html
else:
return res
with open('config.json', 'r') as f:
config = json.load(f)
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--production', action='store_true')
parser.add_argument('-d', '--development', action='store_true')
args = parser.parse_args()
# Make development the default mode
development_mode = True
# Switch it if: "-p" and no "-d"
if (not args.development) and (args.production):
development_mode = False
# Use parameters that are appropriate for the selected mode
if development_mode:
host = config["development"]["host"]
port = config["development"]["port"]
else:
host = config["production"]["host"]
port = config["production"]["port"]
# # Identify the current environment
# # URL_prefix should NOT include "/" at the end, otherwise there will be errors
# if os.environ.get('AWS_EXECUTION_ENV') == "AWS_ECS_EC2":
# running_in_aws = True
# if port == 80 or port == "80":
# URL_prefix = "https://dw-tap.nrel.gov"
# else:
# URL_prefix = "https://dw-tap.nrel.gov:%s" % str(port)
# else:
# running_in_aws = False
# # This case is for running locally (container should be accessed via port 8080 even though inside it the server runs on part 80)
# URL_prefix = "http://localhost:8080"
# Identify the current environment
# URL_prefix should NOT include "/" at the end, otherwise there will be errors
if os.environ.get('ENV') == "prod":
running_in_aws = True
if port == 80 or port == "80":
URL_prefix = "https://dw-tap.nrel.gov"
else:
URL_prefix = "https://dw-tap.nrel.gov:%s" % str(port)
elif os.environ.get('ENV') == "stage":
running_in_aws = True
if port == 80 or port == "80":
URL_prefix = "https://dw-tap-stage.stratus.nrel.gov"
else:
URL_prefix = "https://dw-tap-stage.stratus.nrel.gov:%s" % str(port)
elif os.environ.get('ENV') == "dev":
running_in_aws = True
if port == 80 or port == "80":
URL_prefix = "https://dw-tap-dev.stratus.nrel.gov"
else:
URL_prefix = "https://dw-tap-dev.stratus.nrel.gov:%s" % str(port)
else:
running_in_aws = False
# This case is for running locally (container should be accessed via port 8080 even though inside it the server runs on part 80)
URL_prefix = "http://localhost:8080"
# Now that URL_prefix is determined for the current env, prepare templates from universal ones
# Universal here means that those template can be used for AWS and non-AWS envs
src_dest_names = [("universal_monthly_index.html", "monthly_index.html"),\
("universal_windrose_index.html", "windrose_index.html"),\
("universal_12x24_index.html", "12x24_index.html"),\
("universal_ts_index.html", "ts_index.html"),\
("universal_bc_index.html", "bc_index.html"),\
("universal_info.html", "info.html"),\
("universal_on_map.html", "on_map.html"),\
("universal_on_map3.html", "on_map3.html"),\
("universal_by_address.html", "by_address.html"),\
("universal_kwh_index.html", "kwh_index.html"),\
("universal_energy.html", "energy.html")]
for src_dest in src_dest_names:
t_src, t_dest = src_dest[0], src_dest[1]
t_src = os.path.join(templates_dir, t_src)
t_dest = os.path.join(templates_dir, t_dest)
if os.path.exists(t_src):
instantiate_from_template(t_src,\
t_dest, \
[("URL_PREFIX", URL_prefix)])
app = Flask(__name__)
app.config["DEBUG"] = False
# Note: may consider limiting CORS for production deployment
# this opens up to AJAX calls from any domain
cors = CORS(app)
output = ""
req_args = {}
def serve_12x24(req_id, req_args):
output_dest = os.path.join(outputs_dir, req_id)
try:
height, lat, lon, year_list = validated_params_v2_w_year(req_args)
f = connected_hsds_file(req_args, config)
# Time index can be obtained from f but reading it from a previously saved file is faster
dt = pd.read_csv("wtk-dt.csv")
dt["datetime"] = pd.to_datetime(dt["datetime"])
dt["year"] = dt["datetime"].apply(lambda x: x.year)
subsets=[]
for yr in year_list:
idx = dt[dt["year"] == yr].index
subsets.append(getData(f, lat, lon, height,
"IDW",
power_estimate=False,
inverse_monin_obukhov_length=False,
start_time_idx=idx[0], end_time_idx=idx[-1], time_stride=1,
saved_dt=dt))
atmospheric_df = pd.concat(subsets)
atmospheric_df.index = range(len(atmospheric_df))
# output = timeseries_to_12_by_24(atmospheric_df)
#
# output_dest = os.path.join(outputs_dir, req_id)
# with open(output_dest, "w") as text_file:
# text_file.write(output)
# return
# except Exception as e:
# output = "The following error has occurred:<br>" + str(e)
# with open(output_dest, "w") as text_file:
# text_file.write(output)
# return
output_table = timeseries_to_12_by_24(atmospheric_df)
output = "Selected location:<br><div id=\"infomap\"></div><br><br>" + \
"""
<div classes="centered">
<div>
<table>
<tr>
<td>
%s
</td>
</tr>
</table>
</div>
</div>
""" % output_table
info = "Source of data: <a href=\"https://www.nrel.gov/grid/wind-toolkit.html\" target=\"_blank\" rel=\"noopener noreferrer\">NREL's WTK dataset</a>, covering 2007-2013."
info += "<br><br>The shown subset of the model data includes %d timesteps between %s and %s." % \
(len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
info += """<br><br>To get the shown point estimates, TAP API performed horizontal and vertical interpolation based on the TAP team's
previous research published at: <a href=\"https://www.nrel.gov/docs/fy21osti/78412.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.nrel.gov/docs/fy21osti/78412.pdf</a>.
Specifically, the Inverse-Distance Weighting was used for horizontal interpolation and linear interpolation between the two adjacent heights in the model data was used for vertical interpolation.
"""
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
except Exception as e:
output = "The following error has occurred:<br>" + str(e)
info = ""
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
def serve_monthly(req_id, req_args):
output_dest = os.path.join(outputs_dir, req_id)
try:
height, lat, lon, year_list = validated_params_v2_w_year(req_args)
f = connected_hsds_file(req_args, config)
dt = pd.read_csv("wtk-dt.csv")
dt["datetime"] = pd.to_datetime(dt["datetime"])
dt["year"] = dt["datetime"].apply(lambda x: x.year)
subsets=[]
for yr in year_list:
idx = dt[dt["year"] == yr].index
subsets.append(getData(f, lat, lon, height,
"IDW",
power_estimate=False,
inverse_monin_obukhov_length=False,
start_time_idx=idx[0], end_time_idx=idx[-1], time_stride=1,
saved_dt=dt))
atmospheric_df = pd.concat(subsets)
atmospheric_df.index = range(len(atmospheric_df))
plot_monthly_avg(atmospheric_df, \
title="Location: (%f, %f), %.0fm hub height" % (lat, lon, height),\
save_to_file='static/saved.png',\
show_avg_across_years=True,
show_overall_avg=True,
show=False)
#return flask.send_file('saved.png')
output = "Selected location:<br><div id=\"infomap\"></div><br><br>" + \
"""
<div classes="centered">
<div>
<table>
<tr>
<td><img id=\"monthly_plot\" src=\"static/saved.png\"/></td>
</tr>
</table>
</div>
</div>
"""
# Adding info map after output
#output += "<br><br>Selected location:<br><div id=\"infomap\"></div>"
#info = "The shown dataset includes %d timesteps between %s and %s." % \
# (len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
info = "Source of data: <a href=\"https://www.nrel.gov/grid/wind-toolkit.html\" target=\"_blank\" rel=\"noopener noreferrer\">NREL's WTK dataset</a>, covering 2007-2013."
info += "<br><br>The shown subset of model data includes %d timesteps between %s and %s." % \
(len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
info += """<br><br>To get the shown point estimates, TAP API performed horizontal and vertical interpolation based on the TAP team's
previous research published at: <a href=\"https://www.nrel.gov/docs/fy21osti/78412.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.nrel.gov/docs/fy21osti/78412.pdf</a>.
Specifically, the Inverse-Distance Weighting was used for horizontal interpolation and linear interpolation between the two adjacent heights in the model data was used for vertical interpolation.
"""
# Adding info map inside the info collapsable box doesn't quite work; there is a strage display problem where map shows up partially
#info += "<br><br>Selected location:<br><div id=\"infomap\"></div>"
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
except Exception as e:
output = "The following error has occurred:<br>" + str(e)
info = ""
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
def serve_windrose(req_id, req_args):
output_dest = os.path.join(outputs_dir, req_id)
try:
height, lat, lon, year_list = validated_params_v2_w_year(req_args)
f = connected_hsds_file(req_args, config)
dt = pd.read_csv("wtk-dt.csv")
dt["datetime"] = pd.to_datetime(dt["datetime"])
dt["year"] = dt["datetime"].apply(lambda x: x.year)
subsets=[]
for yr in year_list:
idx = dt[dt["year"] == yr].index
subsets.append(getData(f, lat, lon, height,
"IDW",
power_estimate=False,
inverse_monin_obukhov_length=False,
start_time_idx=idx[0], end_time_idx=idx[-1], time_stride=1,
saved_dt=dt))
atmospheric_df = pd.concat(subsets)
atmospheric_df.index = range(len(atmospheric_df))
plot_name = "%s/windrose_%s.png" % (plot_dir, req_id)
plot_windrose(atmospheric_df, save_to_file=plot_name)
output = "Selected location:<br><div id=\"infomap\"></div><br><br>" + \
"""
<div classes="centered">
<div>
<table>
<tr>
<td><img id=\"windrose_plot\" src=\"%s\"/></td>
</tr>
</table>
</div>
</div>
""" % plot_name
info = "Source of data: <a href=\"https://www.nrel.gov/grid/wind-toolkit.html\" target=\"_blank\" rel=\"noopener noreferrer\">NREL's WTK dataset</a>, covering 2007-2013."
info += "<br><br>The shown subset of model data includes %d timesteps between %s and %s." % \
(len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
info += """<br><br>To get the shown point estimates, TAP API performed horizontal and vertical interpolation based on the TAP team's
previous research published at: <a href=\"https://www.nrel.gov/docs/fy21osti/78412.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.nrel.gov/docs/fy21osti/78412.pdf</a>.
Specifically, the Inverse-Distance Weighting was used for horizontal interpolation and linear interpolation between the two adjacent heights in the model data was used for vertical interpolation.
"""
# Adding info map inside the info collapsable box doesn't quite work; there is a strage display problem where map shows up partially
#info += "<br><br>Selected location:<br><div id=\"infomap\"></div>"
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
except Exception as e:
output = "The following error has occurred:<br>" + str(e)
info = ""
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
def serve_ts(req_id, req_args):
output_dest = os.path.join(outputs_dir, req_id)
try:
height, lat, lon, year_list = validated_params_v2_w_year(req_args)
f = connected_hsds_file(req_args, config)
# Time index can be obtained from f but reading it from a previously saved file is faster
dt = pd.read_csv("wtk-dt.csv")
dt["datetime"] = pd.to_datetime(dt["datetime"])
dt["year"] = dt["datetime"].apply(lambda x: x.year)
subsets=[]
for yr in year_list:
idx = dt[dt["year"] == yr].index
subsets.append(getData(f, lat, lon, height,
"IDW",
power_estimate=False,
inverse_monin_obukhov_length=False,
start_time_idx=idx[0], end_time_idx=idx[-1], time_stride=1,
saved_dt=dt))
atmospheric_df = pd.concat(subsets)
atmospheric_df.index = range(len(atmospheric_df))
atmospheric_df = atmospheric_df.round(3)
if "Unnamed: 0" in atmospheric_df.columns:
atmospheric_df.drop(columns=["Unnamed: 0"], inplace=True)
if "year" in atmospheric_df.columns:
# Year is redundant if datetime is there
atmospheric_df.drop(columns=["year"], inplace=True)
# Saving to file
csv_dest = "%s/ts-%s.csv" % (csv_dir, req_id)
atmospheric_df.to_csv(csv_dest, index=False)
output = atmospheric_df.to_csv(index=False).replace("\n", "<br>")
#info = ""
info = "Source of data: <a href=\"https://www.nrel.gov/grid/wind-toolkit.html\" target=\"_blank\" rel=\"noopener noreferrer\">NREL's WTK dataset</a>, covering 2007-2013."
info += "<br><br>The shown subset of model data includes %d timesteps between %s and %s." % \
(len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
info += """<br><br>To get the shown point estimates, TAP API performed horizontal and vertical interpolation based on the TAP team's
previous research published at: <a href=\"https://www.nrel.gov/docs/fy21osti/78412.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.nrel.gov/docs/fy21osti/78412.pdf</a>.
Specifically, the Inverse-Distance Weighting was used for horizontal interpolation and linear interpolation between the two adjacent heights in the model data was used for vertical interpolation.
"""
#save = "Download: static/raw/ts-%s.csv" % req_id
proposed_fname="%.6f_%.6f_%.1f.csv" % (lat, lon, height)
save = "href=\"%s\" download=\"%s\"" % (csv_dest, proposed_fname)
# Example: href="static/raw/ts-cd5e6247a3b935d7770bb1657df34715.csv" download="39.7430_-105.1470_65.000000.csv"
# it will be added inside the <a> tag
json_output = {'output': output, "info": info, "save": save}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
except Exception as e:
output = "The following error has occurred:<br>" + str(e)
info = ""
save = ""
json_output = {'output': output, "info": info, "save": save}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
def serve_bc(req_id, req_args):
output_dest = os.path.join(outputs_dir, req_id)
try:
height, lat, lon, year_list = validated_params_v2_w_year(req_args)
f = connected_hsds_file(req_args, config)
dt = pd.read_csv("wtk-dt.csv")
dt["datetime"] = pd.to_datetime(dt["datetime"])
dt["year"] = dt["datetime"].apply(lambda x: x.year)
subsets=[]
for yr in year_list:
idx = dt[dt["year"] == yr].index
subsets.append(getData(f, lat, lon, height,
"IDW",
power_estimate=False,
inverse_monin_obukhov_length=False,
start_time_idx=idx[0], end_time_idx=idx[-1], time_stride=1,
saved_dt=dt))
atmospheric_df = pd.concat(subsets)
atmospheric_df.index = range(len(atmospheric_df))
# plot_monthly_avg(atmospheric_df, \
# title="Location: (%f, %f), %.0fm hub height" % (lat, lon, height),\
# save_to_file='static/saved.png',\
# show_avg_across_years=True,
# show_overall_avg=True,
# show=False)
# #return flask.send_file('saved.png')
# output = """
# <div classes="centered">
# <div>
# <table>
# <tr>
# <td><img id=\"monthly_plot\" src=\"static/saved.png\"/></td>
# </tr>
# </table>
# </div>
# </div>
# """
# Ordered list of BC data locations; supports running inside ECS container and locally
# Code below will find first existing and will proceed to using it
bc_locs = ["/bc/bc_v4/", "~/OneDrive - NREL/dw-tap-data/bc_development/bc_v4/"]
selected_bc_loc = None
for bc_loc in bc_locs:
d = os.path.expanduser(bc_loc)
if os.path.isdir(d):
selected_bc_loc = d
if not (selected_bc_loc):
output = """
<div classes="centered">
Unable to locate directory with BC data. Checked locations: %s.
</div>
""" % str(bc_locs)
info = ""
else:
# Todo: check to make sure that atmospheric_df is not empty
output, bc_info = bc_for_point(lon=lon, lat=lat, height=height, \
model_data=atmospheric_df, \
bc_dir=selected_bc_loc,\
plot_dest = 'static/bc.png') # plot_dest="outputs/fig-%s.png" % req_id)
basic_info = "Source of data: <a href=\"https://www.nrel.gov/grid/wind-toolkit.html\" target=\"_blank\" rel=\"noopener noreferrer\">NREL's WTK dataset</a>, covering 2007-2013."
basic_info += "<br><br>The shown subset of model data includes %d timesteps between %s and %s." % \
(len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
basic_info += """<br><br>To get the shown point estimates, TAP API performed horizontal and vertical interpolation based on the TAP team's
previous research published at: <a href=\"https://www.nrel.gov/docs/fy21osti/78412.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.nrel.gov/docs/fy21osti/78412.pdf</a>.
Specifically, the Inverse-Distance Weighting was used for horizontal interpolation and linear interpolation between the two adjacent heights in the model data was used for vertical interpolation.
"""
info = basic_info + "<br><br> Additionally, <strong>bias correction (BC)</strong> was applied to the point estimates. Details:<br><br>" + bc_info
#info = "The shown dataset includes %d timesteps between %s and %s." % \
# (len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
except Exception as e:
output = "The following error has occurred:<br>" + str(e)
info = ""
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
def serve_kwh(req_id, req_args):
output_dest = os.path.join(outputs_dir, req_id)
try:
height, lat, lon, year_list = validated_params_v2_w_year(req_args)
if 'pc' in req_args:
pc = req_args['pc']
if not (pc in powercurves.keys()):
pc = powercurve_default
else:
pc = powercurve_default
f = connected_hsds_file(req_args, config)
# Time index can be obtained from f but reading it from a previously saved file is faster
dt = pd.read_csv("wtk-dt.csv")
dt["datetime"] = pd.to_datetime(dt["datetime"])
dt["year"] = dt["datetime"].apply(lambda x: x.year)
subsets=[]
for yr in year_list:
idx = dt[dt["year"] == yr].index
subsets.append(getData(f, lat, lon, height,
"IDW",
power_estimate=False,
inverse_monin_obukhov_length=False,
start_time_idx=idx[0], end_time_idx=idx[-1], time_stride=1,
saved_dt=dt))
atmospheric_df = pd.concat(subsets)
atmospheric_df.index = range(len(atmospheric_df))
if "Unnamed: 0" in atmospheric_df.columns:
atmospheric_df.drop(columns=["Unnamed: 0"], inplace=True)
atmospheric_df['year'] = atmospheric_df['datetime'].dt.year
atmospheric_df['month'] = atmospheric_df['datetime'].dt.month
atmospheric_df["Month-Year"] = atmospheric_df['month'].astype(str).apply(lambda x: x.zfill(2)) + "-" + atmospheric_df['year'].astype(str)
# Add power colum
atmospheric_df["kw"] = powercurves[pc].windspeed_to_kw(atmospheric_df, 'ws')
# Calculate the time difference between consecutive rows
atmospheric_df['interval_hrs'] = atmospheric_df['datetime'].diff().fillna(pd.Timedelta(seconds=0)).dt.components.hours
# Energy as the power * time product
atmospheric_df["kwh"] = atmospheric_df["kw"] * atmospheric_df['interval_hrs']
atmospheric_df_agg = atmospheric_df.groupby("Month-Year").agg(avg_ws=("ws", "mean"), \
median_ws=("ws", "median"), \
energy_total=("kwh", "sum"))
atmospheric_df_agg.rename(columns={"avg_ws": "Avg. wind speed, m/s", "median_ws": "Median wind speed, m/s", "energy_total": "Energy produced, kWh"}, \
inplace=True)
atmospheric_df_agg = atmospheric_df_agg.round(3)
# Add summary row
atmospheric_df_agg = pd.concat([atmospheric_df_agg,\
pd.DataFrame([{"Avg. wind speed, m/s": "Overall avg.: %.3f" % (atmospheric_df_agg["Avg. wind speed, m/s"].mean()),\
"Median wind speed, m/s": "Overall median: %.3f" % (atmospheric_df["ws"].median()),\
"Energy produced, kWh": "Total: %.3f" % (atmospheric_df_agg["Energy produced, kWh"].sum())}], index=["Summary"])])
output_table = atmospheric_df_agg.to_html(classes='energy_table')
output = "Selected location:<br><div id=\"infomap\"></div><br><br>" + \
"""
<div classes="centered">
<div>
<table>
<tr>
<td>
%s
</td>
</tr>
</table>
</div>
</div>
""" % output_table
#output = str(atmospheric_df.head()).replace("\n", "<br>")
info = "Source of data: <a href=\"https://www.nrel.gov/grid/wind-toolkit.html\" target=\"_blank\" rel=\"noopener noreferrer\">NREL's WTK dataset</a>, covering 2007-2013."
info += "<br><br>The shown subset of the model data includes %d timesteps between %s and %s." % \
(len(atmospheric_df), atmospheric_df.datetime.tolist()[0], atmospheric_df.datetime.tolist()[-1])
info += """<br><br>To get the shown point estimates, TAP API performed horizontal and vertical interpolation based on the TAP team's
previous research published at: <a href=\"https://www.nrel.gov/docs/fy21osti/78412.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.nrel.gov/docs/fy21osti/78412.pdf</a>.
Specifically, the Inverse-Distance Weighting was used for horizontal interpolation and linear interpolation between the two adjacent heights in the model data was used for vertical interpolation.
"""
info += "<br><br>Selected power curve: %s" % pc
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
except Exception as e:
output = "The following error has occurred:<br>" + str(e)
info = ""
json_output = {'output': output, "info": info}
with open(output_dest, 'w') as f:
json.dump(json_output, f)
return
@app.route('/output', methods=['GET'])
def get_output():
""" Check if output file for requested req_id has been cretead and, if so, return its contents as part of a json in the form expected by the js in html page """
req_args = request.args
if 'req_id' in req_args:
req_id = request.args['req_id']
output_path = os.path.join(outputs_dir, req_id)
if os.path.exists(output_path):
output = open(output_path, 'r').read()
else:
output = {'output': "", "info": ""}
else:
output = {'output': "", "info": ""}
return output
@app.route('/infomap', methods=['GET'])
def get_infomap():
req_args = request.args
if not ('lat' in req_args):
return ""
if not ('lon' in req_args):
return ""
try:
lat = float(req_args["lat"])
lon = float(req_args["lon"])
#return get_infomap_script(lat, lon)
# The following should solve the issue "Refused to execite script because "X-Content-Type-Options: nosniff" was given and its Content-Type is not a script MIME type"
return Response(get_infomap_script(lat, lon), mimetype='text/javascript')
except Exception as e:
return ""
@app.route('/addr_to_latlon', methods=['GET'])
def addr_to_latlon():
""" Translate a request with an address to a lat/lon"""
req_args = request.args
if 'addr' in req_args:
addr = request.args['addr']
try:
url = 'https://nominatim.openstreetmap.org/search?q=' + urllib.parse.quote(addr) +'&format=json'
response = requests.get(url).json()
return json.dumps({"latlon": "lat=%s&lon=%s" % (response[0]["lat"], response[0]["lon"])})
except Exception as e:
# "Error" at the beginning is important; js will be looking for it in the returned string to handle the error cases