forked from Bill-Becker/Internal_REopt_Lite_Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults_poller.py
183 lines (142 loc) · 6.84 KB
/
results_poller.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
import requests
import json
import time
#import matplotlib.pyplot as plt
#import matplotlib.gridspec as gridspec
def poller(url, poll_interval=10):
"""
Function for polling the REopt API results URL until status is not "Optimizing..."
:param url: results url to poll
:param poll_interval: seconds
:return: dictionary response (once status is not "Optimizing...")
"""
key_error_count = 0
key_error_threshold = 4
status = "Optimizing..."
print("Polling {} for results with interval of {}s...".format(url, poll_interval))
while True:
resp = requests.get(url=url, verify=False)
resp_dict = json.loads(resp.text)
try:
status = resp_dict['outputs']['Scenario']['status']
except KeyError:
key_error_count += 1
print('KeyError count: {}'.format(key_error_count))
if key_error_count > key_error_threshold:
print('Breaking polling loop due to KeyError count threshold of {} exceeded.'.format(key_error_threshold))
break
if status != "Optimizing...":
time.sleep(10)
resp = requests.get(url=url, verify=False)
resp_dict = json.loads(resp.text)
break
else:
time.sleep(poll_interval)
return resp_dict
def reo_optimize(post):
API_KEY = 'yOODa4jmZy1q3Wd6lkQcne6izi3nq2YSIIlCQkOg'
root_url = 'https://developer.nrel.gov/api/reopt'
post_url = root_url + '/v1/job/?api_key=' + API_KEY
results_url = root_url + '/v1/job/<run_uuid>/results/?api_key=' + API_KEY
resp = requests.post(url=post_url, json=post)
if not resp.ok:
print("Status code {}. {}".format(resp.status_code, resp.content))
else:
print("Response OK from {}.".format(post_url))
run_id_dict = json.loads(resp.text)
try:
run_id = run_id_dict['run_uuid']
except KeyError:
msg = "Response from {} did not contain run_uuid.".format(post_url)
return poller(url=results_url.replace('<run_uuid>', run_id))
def reo_optimize_chpstaging(post):
API_KEY = 'uaz52dr5KTT5Qs5U72rS70hw3IYeHVEyAaDUFQvo'
root_url = 'https://chpbeta-reopt-stage-api.its.nrel.gov'
post_url = root_url + '/v1/job/?format=json&api_key=' + API_KEY
results_url = root_url + '/v1/job/<run_uuid>/results/?api_key=' + API_KEY
resp = requests.post(url=post_url, json=post)
if not resp.ok:
print("Status code {}. {}".format(resp.status_code, resp.content))
else:
print("Response OK from {}.".format(post_url))
run_id_dict = json.loads(resp.text)
try:
run_id = run_id_dict['run_uuid']
except KeyError:
msg = "Response from {} did not contain run_uuid.".format(post_url)
return poller(url=results_url.replace('<run_uuid>', run_id))
def reo_optimize_localhost(post):
API_KEY = 'uaz52dr5KTT5Qs5U72rS70hw3IYeHVEyAaDUFQvo'
root_url = 'http://localhost:8000'
post_url = root_url + '/v1/job/?format=json&api_key=' + API_KEY
results_url = root_url + '/v1/job/<run_uuid>/results/?api_key=' + API_KEY
resp = requests.post(url=post_url, json=post)
if not resp.ok:
print("Status code {}. {}".format(resp.status_code, resp.content))
else:
print("Response OK from {}.".format(post_url))
run_id_dict = json.loads(resp.text)
try:
run_id = run_id_dict['run_uuid']
except KeyError:
msg = "Response from {} did not contain run_uuid.".format(post_url)
return poller(url=results_url.replace('<run_uuid>', run_id))
def reo_optimize_development(post):
API_KEY = 'uaz52dr5KTT5Qs5U72rS70hw3IYeHVEyAaDUFQvo'
root_url = 'https://reopt-dev-api1.nrel.gov'
post_url = root_url + '/v1/job/?format=json&api_key=' + API_KEY
results_url = root_url + '/v1/job/<run_uuid>/results/?api_key=' + API_KEY
resp = requests.post(url=post_url, json=post, verify=False)
if not resp.ok:
print("Status code {}. {}".format(resp.status_code, resp.content))
else:
print("Response OK from {}.".format(post_url))
run_id_dict = json.loads(resp.text)
try:
run_id = run_id_dict['run_uuid']
except KeyError:
msg = "Response from {} did not contain run_uuid.".format(post_url)
return poller(url=results_url.replace('<run_uuid>', run_id))
def results_plots(results_dict):
bar_data = dict()
bar_stor_data = dict()
for name, results in results_dict.items():
# Initilize figure layout and assign axes
fig = plt.figure(figsize=(21,5), constrained_layout=True)
gs = gridspec.GridSpec(ncols=16, nrows=1, figure=fig)
H2_stack = fig.add_subplot(gs[:,1:3])
H2_stack.set_ylim(0, 275)
H2_storage_stack = fig.add_subplot(gs[:,0])
H2_storage_stack.set_ylim(0, 50)
loadAx = fig.add_subplot(gs[0,3:])
loadAx.set_title(name, fontsize=16)
# Plot base and total loads
plotrange = 24*7
bar_data[name] = dict()
bar_stor_data[name] = dict()
utility_load = results['outputs']['Scenario']['Site']['ElectricTariff']['year_one_to_load_series_kw']
PV_load = results['outputs']['Scenario']['Site']['PV']['year_one_to_load_series_kw']
BESS_load = results['outputs']['Scenario']['Site']['Storage']['year_one_to_load_series_kw']
load = results['outputs']['Scenario']['Site']['LoadProfile']['year_one_electric_load_series_kw']
loadAx.stackplot(range(plotrange),[utility_load[:plotrange], BESS_load[:plotrange], PV_load[:plotrange]],
labels = ['Utility', 'BESS', 'Solar PV'],
colors = ['tab:blue', 'maroon', 'tab:orange'])
loadAx.legend(loc='upper left')
pv_size = results['outputs']['Scenario']['Site']['PV']['size_kw']
batt_kw = results['outputs']['Scenario']['Site']['Storage']['size_kw']
batt_kwh = results['outputs']['Scenario']['Site']['Storage']['size_kwh']
bar_names = ['Solar PV', 'Battery']
vals = [pv_size, batt_kw]
for bar_name, val in zip(bar_names, vals):
bar_data[name][bar_name] = val
H2_stack.tick_params(axis='x', labelrotation = 30)
H2_stack.set_ylabel('Power (kW)')
H2_stack.bar(*zip(*bar_data[name].items()), color=['maroon', 'tab:orange'])
bar_names = ['Battery']
vals = [batt_kwh]
for bar_name, val in zip(bar_names, vals):
bar_stor_data[name][bar_name] = val
H2_storage_stack.tick_params(axis='x', labelrotation = 30)
H2_storage_stack.set_ylabel('Energy (kWh)')
H2_storage_stack.bar(*zip(*bar_stor_data[name].items()), color=['black'])
plt.show()