-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
407 lines (285 loc) · 10.8 KB
/
main.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
from dependency import *
from agriculture import *
from water import *
from energy import *
from climate import *
app = Flask(__name__,template_folder='template')
# Homepage EndPoint
@app.route('/')
def index():
return render_template('homepage.html')
# Netlogo Stimulation EndPoint
@app.route('/NetlogoInputV1')
def netlogoinput():
return render_template('NetlogoInputV1.html')
# Agriculture Page EndPoint
@app.route('/AgricultureV1')
def agriculture():
return render_template('index.html')
# Water Page EndPoint
@app.route('/WaterV1')
def water():
return render_template('water.html')
# Energy Page EndPoint
@app.route('/EnergyV1')
def energy():
return render_template('energy.html')
@app.route('/ChartWindow')
def ChartWindow():
return render_template('ChartWindow.html')
@app.route('/ClimateV1')
def climate():
return render_template('climate.html')
@app.route('/AllChartsV1')
def allcharts():
return render_template('allcharts.html')
@app.route('/download-csv')
def download_csv():
# Replace the file path with the actual path to your CSV file
file_path = r'netlogo\What NH3 production pays off 2.0.xlsx'
file_name = r'netlogo\Ammonia.xlsx' # Set the desired file name
# Serve the file for download
return send_file(file_path, as_attachment=True, download_name=file_name)
# Agriculture global variable
global corn_area, wheat_area, soybeans_area, sg_area,num_of_years
# Water global variable
global aquifier_level,min_aquifier_level
# Energy global variable
global energy_value,loan_term,interest,num_wind_turbines,nyear_w,capacity_w,cost_w,degrade_w,wind_factor,num_panel_sets,nyear_s,cost_s,capacity_s,degrade_s,sun_hrs,ptc_w,itc_s,ptc_s
# Climate global variable
global future_processes,climate_model
# Start Year Input
global start_year
# Agriculture Form Value
corn_area=None
wheat_area=None
soybeans_area=None
sg_area=None
# Water Form Value
aquifier_level=None
min_aquifier_level=None
# Energy Form Value
energy_value = None
loan_term = None
interest = None
num_wind_turbines = None
nyear_w = None
capacity_w = None
cost_w = None
degrade_w = None
wind_factor = None
num_panel_sets = None
nyear_s = None
cost_s = None
capacity_s = None
degrade_s = None
sun_hrs = None
ptc_w = None
itc_s = None
ptc_s = None
# Climate Form Value
future_processes = None
climate_model = None
# Get Number of Years
num_of_years=None
# Get Start Year Input
start_year = None
@app.route('/calculate', methods=['GET','POST'])
def calculate():
global corn_area, wheat_area, soybeans_area, sg_area,num_of_years
global energy_value,loan_term,interest,num_wind_turbines,nyear_w,capacity_w,cost_w,degrade_w,wind_factor,num_panel_sets,nyear_s,cost_s,capacity_s,degrade_s,sun_hrs,ptc_w,itc_s,ptc_s
global aquifier_level,min_aquifier_level
global future_processes,climate_model
data = request.get_json()
# Saving the Data to JSON
with open('received_data.json', 'w') as json_file:
json.dump(data, json_file)
print("Data",data)
# Getting Start Year Input
start_year = int(data['StartYear']['input_start_of_years'])
# Getting Agriculture Form Value
corn_area=int(data['agriculture']['corn_area'])
wheat_area=int(data['agriculture']['wheat_area'])
soybeans_area=int(data['agriculture']['soybeans_area'])
sg_area=int(data['agriculture']['sg_area'])
# Getting Energy Form Value
energy_value = int(data['energy']['energy_value'])
loan_term = int(data['energy']['loan_term'])
interest = float(data['energy']['interest'])
num_wind_turbines = int(data['energy']['num_wind_turbines'])
nyear_w = int(data['energy']['nyear_w'])
capacity_w = int(data['energy']['capacity_w'])
cost_w = int(data['energy']['cost_w'])
degrade_w = int(data['energy']['degrade_w'])
wind_factor = int(data['energy']['wind_factor'])
num_panel_sets = int(data['energy']['num_panel_sets'])
nyear_s = int(data['energy']['nyear_s'])
cost_s = int(data['energy']['cost_s'])
capacity_s = int(data['energy']['capacity_s'])
degrade_s = int(data['energy']['degrade_s'])
sun_hrs = int(data['energy']['sun_hrs'])
ptc_w = int(data['energy']['ptc_w'])
itc_s = int(data['energy']['itc_s'])
ptc_s = int(data['energy']['ptc_s'])
# Getting Water Form Value
aquifier_level = int(data['irrigation']['aquifier_level'])
min_aquifier_level = int(data['irrigation']['min_aquifier_level'])
# Getting Climate Form Value
future_processes=str(data['ClimateData']['future_processes'])
climate_model=str(data['ClimateData']['climate_model'])
num_of_years=int(data['numberofyears']['num_of_years'])
print("Start_Year --->",start_year)
print("corn_area ---->", corn_area)
print("wheat_area ---->", wheat_area)
print("soybeans_area ----->", soybeans_area)
print("sg_area----->", sg_area)
print("aquifier_level ---->", aquifier_level)
print("min_aquifier_level ---->", min_aquifier_level)
print("num_of_years ------>", num_of_years)
print("energy_value ---->",energy_value)
print("loan_term ---->",loan_term)
print("interest ---->",interest)
print("num_wind_turbines ---->",num_wind_turbines)
print("nyear_w ---->",nyear_w)
print("capacity_w ----->",capacity_w)
print("cost_w----->",cost_w)
print("degrade_w----->",degrade_w)
print("wind_factor ---->",wind_factor)
print("num_panel_sets ---->",num_panel_sets)
print("nyear_s ---->",nyear_s)
print("cost_s ----->",cost_s)
print("capacity_s----->",capacity_s)
print("degrade_s----->",degrade_s)
print("sun_hrs ---->",sun_hrs)
print("ptc_w ---->",ptc_w)
print("itc_s ---->",itc_s)
print("ptc_s ---->",ptc_s)
print("future_processes ---->",future_processes)
print("climate_model ---->",climate_model)
#Fetching Netlogo Instance
netlogo = get_netlogo_instance()
# Agriculture Netlogo Command
netlogo.command(f"set corn_area {corn_area}")
netlogo.command(f"set wheat_area {wheat_area}")
netlogo.command(f"set soybeans_area {soybeans_area}")
netlogo.command(f"set sg_area {sg_area}")
# Water Netlogo Command
netlogo.command(f"set Aquifer_thickness {aquifier_level}")
netlogo.command(f"set Min_Aq_Thickness {min_aquifier_level}")
# Energy Netlogo Command
netlogo.command(f"set Energy_value {energy_value}")
netlogo.command(f"set Loan_term {loan_term}")
netlogo.command(f"set interest {interest}")
netlogo.command(f"set Nyear_W {nyear_w}")
netlogo.command(f"set #wind_turbines {num_wind_turbines}")
netlogo.command(f"set Cost_W {cost_w}")
netlogo.command(f"set Capacity_W {capacity_w}")
netlogo.command(f"set Degrade_W {degrade_w}")
netlogo.command(f"set wind_factor {wind_factor}")
netlogo.command(f"set #Panel_sets {num_panel_sets}")
netlogo.command(f"set Nyear_S {nyear_s}")
netlogo.command(f"set Cost_S {cost_s}")
netlogo.command(f"set Capacity_S {capacity_s}")
netlogo.command(f"set Degrade_S {degrade_s}")
netlogo.command(f"set Sun_hrs {sun_hrs}")
netlogo.command(f"set PTC_W {ptc_w}")
netlogo.command(f"set ITC_S {itc_s}")
netlogo.command(f"set PTC_S {ptc_s}")
# Climate Netlogo Command
print(f"set Future_Process '{future_processes}'")
netlogo.command(f'set Future_Process "{future_processes}"')
netlogo.command(f'set Climate_Model "{climate_model}"')
# Setup and run the NetLogo model
netlogo.command("setup")
netlogo.command(f'repeat {num_of_years} [go]')
print("Received Data:", data)
restart()
return jsonify(data)
@app.route('/calculateAgriculture', methods=['GET', 'POST'])
def calculate_agriculture():
print("corn_area ---->", corn_area)
print("wheat_area ---->", wheat_area)
print("soybeans_area ----->", soybeans_area)
print("sg_area----->", sg_area)
print("num_of_years ------>", num_of_years)
print("Successfully Hiting the Agriculture Chart API")
crop_production_img = crop_production_calculation()
net_calc_img = net_income_calculation()
result = {
"crop_production_img": crop_production_img,
"net_calc_img": net_calc_img
}
print("Agriculture",result)
return jsonify(result)
@app.route('/calculateIrrigation', methods=['GET', 'POST'])
def calculate_irrigation():
print("aquifier_level ---->", aquifier_level)
print("min_aquifier_level ---->", min_aquifier_level)
crop_groundwater_irrigation_data_for_img = crop_groundwater_irrigation()
groundwater_level_data_img = groundwater_level()
result = {
"crop_groundwater_irrigation_data_for_img": crop_groundwater_irrigation_data_for_img,
"groundwater_level_data_img": groundwater_level_data_img
}
print("Water",result)
return jsonify(result)
@app.route('/calculateEnergy',methods=['GET', 'POST'])
def calculate_energy():
print("energy_value ---->",energy_value)
print("loan_term ---->",loan_term)
print("interest ---->",interest)
print("num_wind_turbines ---->",num_wind_turbines)
print("nyear_w ---->",nyear_w)
print("capacity_w ----->",capacity_w)
print("cost_w----->",cost_w)
print("degrade_w----->",degrade_w)
print("wind_factor ---->",wind_factor)
print("num_panel_sets ---->",num_panel_sets)
print("nyear_s ---->",nyear_s)
print("cost_s ----->",cost_s)
print("capacity_s----->",capacity_s)
print("degrade_s----->",degrade_s)
print("sun_hrs ---->",sun_hrs)
print("ptc_w ---->",ptc_w)
print("itc_s ---->",itc_s)
print("ptc_s ---->",ptc_s)
farm_energy_production_img_data = farm_energy_production_calculation()
energy_net_calc_img_data= energy_net_income_calculation()
result = {
"farm_energy_production_img_data": farm_energy_production_img_data,
"energy_net_calc_img_data": energy_net_calc_img_data
}
print("Energy",result)
return jsonify(result)
@app.route('/calculateClimate',methods=['GET', 'POST'])
def calculate_climate():
print("future_processes ---->",future_processes)
print("climate_model ---->",climate_model)
crop_income_img = crop_income_calculation()
insur_income_calc_img= insurance_income_calculation()
result = {
"crop_income_img": crop_income_img,
"insur_income_img": insur_income_calc_img
}
print("Climate",result)
return jsonify(result)
@app.route('/restart', methods=['GET', 'POST'])
def restart():
some_queue.put("something")
print("Restarted successfully")
def start_flaskapp(queue):
global some_queue
some_queue = queue
app.run(host='0.0.0.0', port=5000)
if __name__ == "__main__":
q = Queue()
p = Process(target=start_flaskapp, args=(q,))
p.start()
while True:
if q.empty():
time.sleep(1)
else:
break
p.terminate()
args = [sys.executable] + sys.argv
subprocess.call(args)