-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXLR_Framework_setup1.py
1029 lines (757 loc) · 43.8 KB
/
XLR_Framework_setup1.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
import os
import os.path
import random
from operator import add
from datetime import datetime, date, timedelta
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
import shutil
import ema_workbench
import time
## Step 2: Function for initiating the main dictionary of climate stations
def create_dic(a):
'''Function: creating a dictionary for each climate station'''
a = {}
keys = ['fM', 'iPot', 'rSnow', 'dSnow', 'cPrec', 'dP', 'elev', 'lat', 'long', 'fileName']
a = {key: None for key in keys}
return a
def initialize_input_dict (mainFolderSki):
''' This function returns a dictionary , and addresses of 4 folders'''
'''Step 1'''
rootFolder = mainFolderSki
inputFolder = os.path.join(rootFolder,'input')
ablationFolder = os.path.join(inputFolder, 'Ablation')
accumulationFolder = os.path.join(inputFolder, 'Accumulation')
climate_ref_Folder = os.path.join(inputFolder, 'Climate_ref')
climate_Ref_Folder_org = os.path.join(inputFolder, 'Climate_ref_no_randomness_0')
climate_ref_Folder_rand_1 = os.path.join(inputFolder, 'Climate_ref_randomness_1')
climate_ref_Folder_rand_2 = os.path.join(inputFolder, 'Climate_ref_randomness_2')
'''Step 2: Reading all files names inside the Ablation, Accumulation, and Climate folders'''
ablationFiles = []
for filename in os.walk(ablationFolder):
ablationFiles = filename[2]
accumulationFiles = list()
for filename in os.walk(accumulationFolder):
accumulationFiles = filename[2]
climate_ref_Files = list()
for filename in os.walk(climate_ref_Folder):
climate_ref_Files = filename[2]
'''Step 3: Reading files inside ablation folder '''
os.chdir(ablationFolder)
with open(ablationFiles[0], 'r') as file:
FM1 = file.read()
with open(ablationFiles[1], 'r') as file:
Ipot1 = file.read()
with open(ablationFiles[2], 'r') as file:
Rsnow1 = file.read()
'''Step 4: Reading the lines of files inside ablation folder'''
FM1 = FM1.replace('\n', '\t')
FM1 = FM1.split('\t')
Ipot1 = Ipot1.replace('\n', '\t').split('\t')
Rsnow1 = Rsnow1.replace('\n', '\t').split('\t')
'''Step 5: Reading the lines of files inside accumulation folder'''
os.chdir(accumulationFolder)
with open(accumulationFiles[0], 'r') as file:
cPrec = file.read()
with open(accumulationFiles[1], 'r') as file:
dSnow1 = file.read()
cPrec = cPrec.replace('\n', '\t')
cPrec = cPrec.split('\t')
dSnow1 = dSnow1.replace('\n', '\t').split('\t')
'''Step 6: Reading the lines of files inside climate folder'''
os.chdir(climate_ref_Folder)
with open('pcp.txt', 'r') as file:
pcpData = file.read()
with open('tmp.txt', 'r') as file:
tmpData = file.read()
pcpData = pcpData.split('\n')
for i in range(len(pcpData)):
pcpData[i] = pcpData[i].split(',')
'''Step 7: Initialazing the input dictionary of climate stations which holds the information of accumulation
and ablation, and etc of the stations'''
nameStn = []
for file in climate_ref_Files:
if 'p.csv' in file:
#nameStn.append('n_' + file[-25: -5])
nameStn.append(file[-25: -5])
stnDicts = []
for i in range(len(nameStn)):
stnDicts.append(create_dic(nameStn[i]))
'''Step 8: Assigning the file names to the dictionary'''
for i in range (len(nameStn)):
stnDicts[i]['fileName'] = nameStn[i]
'''Step 9: Assigning the accumulation and ablation values'''
for stnDict in stnDicts:
for i, element in enumerate(FM1):
if element == stnDict['fileName'][:]:
#if element == stnDict['fileName'][2:]:
stnDict['fM'] = FM1[i+1]
for i, element in enumerate(Ipot1):
if element == stnDict['fileName'][:]:
#if element == stnDict['fileName'][2:]:
stnDict['iPot'] = Ipot1[i+1]
for i, element in enumerate(Rsnow1):
if element == stnDict['fileName'][:]:
#if element == stnDict['fileName'][2:]:
stnDict['rSnow'] = Rsnow1[i+1]
for i, element in enumerate(dSnow1):
if element == stnDict['fileName'][:]:
#if element == stnDict['fileName'][2:]:
stnDict['dSnow'] = dSnow1[i+1]
for i, element in enumerate(cPrec):
stnDict['cPrec'] = cPrec[1]
stnDict['dP'] = cPrec[3]
'''Step 10: Assigning the elevation, Lat and long to the dictionaries'''
for i in range(len(stnDicts)):
for j in range(1, len(pcpData)):
#if pcpData[j][1][2:-1] == stnDicts[i]['fileName'][2:]:
if pcpData[j][1][:-1] == stnDicts[i]['fileName'][:]:
stnDicts[i]['lat']= pcpData[j][2]
stnDicts[i]['long']= pcpData[j][3]
stnDicts[i]['elev']= pcpData[j][4]
return stnDicts, inputFolder, ablationFolder, accumulationFolder, climate_ref_Folder, climate_Ref_Folder_org, \
climate_ref_Folder_rand_1, climate_ref_Folder_rand_2
# Step 3 Snow Model
## S3.1 Initializiing the main dictionary for a case study
caseStudyStns = {}
inputFolder = ''
ablationFolder = ''
accumulationFolder = ''
climateFolder = ''
climateFolder_org = ''
climateFolder1 = ''
climateFolder2 = ''
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case1_sattel-hochstuckli'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case2_Atzmaening'
root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case3_hoch-ybrig\setup1'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case4_villars-diablerets_elevations_b1339'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case4_villars-diablerets_elevations_b1822'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case4_villars-diablerets_elevations_b2000'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case4_villars-diablerets_elevations_b2500'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case5_champex'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case6_davos_elevations_b1564'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case6_davos_elevations_b2141'
#root = r'C:\Saeid\Prj100\SA_2\snowModelUZH\case6_davos_elevations_b2584'
## calling the function with multiple return values
caseStudyStns, inputFolder, ablationFolder, accumulationFolder, climateFolder, climateFolder_org, \
climateFolder1, climateFolder2 = initialize_input_dict(root)
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
## 1st column as index: makaing date from 01 01 1981 to 2099 12 31
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date ).days + 1)):
yield start_date + timedelta(n)
### OR Let's make this function in a more OOP way:
class Policy_Ski:
def __init__(self, x1SnowThershold):
self.x1SnowThershold = x1SnowThershold
def policy_release2(self):
return(self.x1SnowThershold)
def policy_release3(self):
''' this function should make a matrix of evaluation fot the condition of 100 day ay minimum condition'''
pass
class Economic_Model_Ski:
def __init__(self, xCostDay, xRevenueDay):
self.costDayFixed = xCostDay
self.revenueDayFixed = xRevenueDay
def economic_costDay(self):
return(self.costDayFixed)
def economic_revenueDay(self):
return(self.revenueDayFixed)
class RCP_Model:
def __init__(self, xRCP, xClimateModel):
self.input1 = round(xRCP)
#self.input1 = xRCP
self.input2 = xClimateModel
def rcpGenerator(self):
if self.input1 == 1:
RCP = str(2.6)
rcpInt = 1
if self.input1 == 2:
RCP = str(4.5)
rcpInt = 2
if self.input1 == 3:
RCP = str(8.5)
rcpInt = 3
return(RCP, rcpInt)
def climateModel(self):
a, b = RCP_Model.rcpGenerator(self)
if b == 1:
climateModel = round(self.input2*11)
elif b == 2:
climateModel = 11 + max(1,round(self.input2*25))
else:
climateModel = 36 + max(1, round(self.input2*31))
return (int(climateModel))
def tipping_points_freq(df, xGoodDays):
"""
This function, calculates the frequency of tipping points for each individual resort
"""
dfColumns= df.columns
scenarios_length= len(dfColumns)
simulations_Length = len(df[dfColumns[1]])
tipping_freq = np.zeros(scenarios_length)
for i in range (1, scenarios_length, 1):
m = 0
for j in range (1 , simulations_Length, 1):
if float(df[dfColumns[i]].iloc[j]) < xGoodDays:
m += 1
if m == 3:
tipping_freq[i] += 1
m = 0
else:
m = 0
continue
#break
return tipping_freq
# XLR Framework
def snow_Model (xRCP=None, xClimateModel=None, Xfactor1 = None, X2fM = None, X3iPot = None, X4rSnow = None,
X5temp = None, X6tempArt = None, xCostDay = None, xRevenueDay = None, x1SnowThershold = None,
xGoodDays = None):
'''' This function controls the Ski resort model in an XLR framework'''
''' VERY IMPORTANT --- Controling the randomness --- VERY IMPORTANT'''
xClimateRandomness = round(Xfactor1)
if (xClimateRandomness == 1):
os.chdir(climateFolder_org)
src = os.getcwd()
os.chdir(climateFolder)
dst = os.getcwd()
#copytree(src, dst)
print('Original CH2018 is being used')
elif (xClimateRandomness == 2) :
os.chdir(climateFolder1)
src = os.getcwd()
os.chdir(climateFolder)
dst = os.getcwd()
#copytree(src, dst)
print('Random Climate realization version 1 is being used')
else:
os.chdir(climateFolder2)
src = os.getcwd()
os.chdir(climateFolder)
dst = os.getcwd()
#copytree(src, dst)
print('Random Climate realization version 2 is being used')
os.chdir(climateFolder)
fnames = os.listdir()
#randomness_pcp_tmp(fnames, Xfactor1)
print('Snow_Model: Matching the station names values with CSV files!')
'''Matching the station names values in the dictionary of stations with CSV files in Climate folder of the case Study'''
pcpCaseStudy = []
tmpCaseStudy = []
if (xClimateRandomness == 1):
for i in range(len(caseStudyStns)):
pcpCaseStudy.append(os.path.join(climateFolder, caseStudyStns[i]['fileName'] + 'p.csv'))
tmpCaseStudy.append(os.path.join(climateFolder, caseStudyStns[i]['fileName'] + 't.csv'))
elif (xClimateRandomness == 2) :
for i in range(len(caseStudyStns)):
pcpCaseStudy.append(os.path.join(climateFolder1, caseStudyStns[i]['fileName'] + 'p.csv'))
tmpCaseStudy.append(os.path.join(climateFolder1, caseStudyStns[i]['fileName'] + 't.csv'))
else:
for i in range(len(caseStudyStns)):
pcpCaseStudy.append(os.path.join(climateFolder2, caseStudyStns[i]['fileName'] + 'p.csv'))
tmpCaseStudy.append(os.path.join(climateFolder2, caseStudyStns[i]['fileName'] + 't.csv'))
print('Snow_Model: Building a database for each csv file (tmp and pcp)!')
'''Step 6: building a database for each precipitation and temperature file in Climate folder and saving them in a list'''
'''6.1 reading the csv files as databases'''
dfpcp = [None for _ in range(len(pcpCaseStudy))]
dftmp = [None for _ in range(len(tmpCaseStudy))]
for i in range(len(pcpCaseStudy)):
dfpcp[i] = pd.read_csv(pcpCaseStudy[i])
dftmp[i] = pd.read_csv(tmpCaseStudy[i])
'''6.2 making a header for output files'''
dfpcpCol = dfpcp[0].columns
dftmpCol = dftmp[0].columns
'''6.3 defining the length of simulations and scenarios'''
scenariosLength = len(dfpcpCol)
simulationLength = len(dftmp[0][dftmpCol[0]]) - 1
'''Reading the beginning and end of the simulation'''
start_date = date(1981, 1, 1)
end_date = date(2099, 12, 31)
dateList = []
for single_date in daterange(start_date, end_date):
dateList.append(single_date.strftime("%m/%d/%Y"))
seasonList = []
for n in range (1981, 2100, 1):
seasonList.append(str(n))
print('Snow_Model: Part 1 Running the model, daily output!')
'''################################ PART1 ################################'''
'''Running the model for each climate station:'''
for k in range(len(caseStudyStns)):
'''making a header for output files'''
dfpcpCol = dfpcp[k].columns
dftmpCol = dftmp[k].columns
#X2fM = caseStudyStns[k].get("fM") # change 0 to i for all stations
#X3iPot = caseStudyStns[k].get("iPot")
#X4rSnow = caseStudyStns[k].get("rSnow")
'''defining the length of simulations and scenarios'''
#scenariosLength = len(dfpcpCol)
scenariosLength = 1
simulationLength = len(dftmp[0][dftmpCol[0]]) - 1
'''declaring the initial arrays'''
accumulation = [0 for _ in range(simulationLength)]
ablation = [0 for _ in range(simulationLength)]
snowDeposite = [0 for _ in range(simulationLength)]
total = np.zeros([simulationLength, 3*scenariosLength])
'''declaring the new variables for financial analyses and temrature Index for artificial snow making'''
artSnowCheck = [0 for _ in range(simulationLength)]
revenue = [0 for _ in range(simulationLength)]
cost = [0 for _ in range(simulationLength)]
profit = [0 for _ in range(simulationLength)]
totalMoney = np.zeros([simulationLength, 4*scenariosLength])
'''RCP and Climate Model Controler'''
rcp_Model = RCP_Model(xRCP, xClimateModel)
RCP, intRCP = rcp_Model.rcpGenerator()
climateModel = rcp_Model.climateModel()
'''Running the model for each climate scenario:'''
for j in range(climateModel, climateModel + 1, 1):
#for j in range(len(dfpcpCol)):
## Reading the information and inputs of the first day of simulation
todayPCP = dfpcp[k][dfpcpCol[j]].iloc[1] if (dfpcp[k][dfpcpCol[j]].iloc[1] != -99) else 0
todayTMPMAX = round(dftmp[k][dftmpCol[2*j]].iloc[1],2) if(dftmp[k][dftmpCol[2*j]].iloc[1] != -99) else 0
todayTMPMIN = round(dftmp[k][dftmpCol[2*j+1]].iloc[1],2) if(dftmp[k][dftmpCol[2*j+1]].iloc[1] != -99) else 0
todayTMPAVE = round((todayTMPMAX+todayTMPMIN)/2,2) if((todayTMPMAX+todayTMPMIN)/2 != -99) else 0
'''Thershold 300 mm
EMA_workbench_controler for the thershold of good snow condition'''
#A = policy_release1(x1SnowThershold)
policySkiResort = Policy_Ski(x1SnowThershold) ## 300 mm
snowThershold = policySkiResort.policy_release2()
'''EMA_workbench_controler for the thershold daily fixed revenue and cost expenses'''
economyDaySki = Economic_Model_Ski(xCostDay, xRevenueDay)
revenueDayFixed = economyDaySki.economic_revenueDay() # self.revenueDayFixed
costDayFixed = economyDaySki.economic_costDay() # self.costDayFixed
'''Accumulation for the first day:'''
if (todayTMPAVE) <= X5temp:
accumulation[0] = todayPCP *(1 + float(caseStudyStns[k]['cPrec']))*float(caseStudyStns[k]['dSnow'])*(1)
elif X5temp -1 < (todayTMPAVE) <= X5temp + 1:
accumulation[0] = todayPCP *(1 + float(caseStudyStns[k]['cPrec']))*float(caseStudyStns[k]['dSnow'])*float((X5temp + 1 -todayTMPAVE)/2)
else: accumulation[0] = 0
'''Ablation for the first day:'''
if todayTMPAVE <= X5temp:
ablation[0] = 0
else:
#ablation[0] = (float(caseStudyStns[k]['fM']) + float(caseStudyStns[k]['rSnow'])*float(caseStudyStns[k]['iPot'])*0.001)*float(todayTMPAVE)*(1+0)
ablation[0] = (float(X2fM) + float(X4rSnow)*float(X3iPot)*0.001)*float(todayTMPAVE)*(1+0)
'''Main mass balance equation for the first day:'''
snowDeposite[0] = 0 if (0 + accumulation[0] - ablation[0]) < 0 else (0 + accumulation[0] - ablation[0])
'''storing three values in a list for the first day'''
#total[0,3*j+0] = round((accumulation[0] - ablation[0]), 2)
#total[0,3*j+1] = round(snowDeposite[0], 2)
#total[0,3*j+2] = 1 if (total[0,3*j+1] > snowThershold) else total[0,3*j+1] / snowThershold
total[0,0] = round((accumulation[0] - ablation[0]), 2)
total[0,1] = round(snowDeposite[0], 2)
total[0,2] = 1 if (total[0,1] > snowThershold) else total[0,1] / snowThershold
'''Check the posiibility of Snow Making'''
if (todayTMPAVE) <= X6tempArt:
artSnowCheck[0] = 1
elif X6tempArt < (todayTMPAVE) <= X6tempArt + 2:
artSnowCheck[0] = 1 * float((X6tempArt + 2 -todayTMPAVE)/2)
else:
artSnowCheck[0] = 0
'''Revenue and financial status'''
#revenue[0] = round((total[0,3*j+2] * 10), 2)
revenue[0] = float(round(revenueDayFixed,2)) if (total[0,2] > 0.6 * snowThershold ) else float(round(revenueDayFixed,2))*float((total[0,2] / snowThershold))
'''Cost'''
#cost[0] = round((revenue[0] * 0.4) , 2)
#cost[0] = float(round(costDayFixed, 2)) if (total[0,3*j+2] > snowThershold ) else float(round(costDayFixed,2))*float((total[0,3*j+2] / snowThershold))
#cost[0] = float(round(costDayFixed, 2)) if (total[0,3*j+2] > snowThershold else float(round(costDayFixed,2))*float((total[0,3*j+2] / snowThershold))
cost[0] = float(round(costDayFixed, 2)) if (total[0,2] > snowThershold ) else float(round(costDayFixed,2))*float((total[0,2] / snowThershold))
'''Profit'''
profit[0] = revenue[0] - cost[0]
'''Storing the artificial snow possibility check and financial situation'''
#totalMoney[0,1*j+0] = round(100.345, 2)
totalMoney[0,0] = round(artSnowCheck[0], 2)
totalMoney[0,1] = round(revenue[0], 2)
totalMoney[0,2] = round(cost[0], 2)
totalMoney[0,3] = round(profit[0], 2)
'''For the SECOND DAY to the End of Simulation:'''
i = 0
for i in range(2, simulationLength + 1, 1):
'''# precipitation and temperature missing values were handled'''
todayPCP = dfpcp[k][dfpcpCol[j]].iloc[i] if (dfpcp[k][dfpcpCol[j]].iloc[i] != -99) else 0
todayTMPMAX = round(dftmp[k][dftmpCol[2*j]].iloc[i],2) if(dftmp[k][dftmpCol[2*j]].iloc[i] != -99) else 0
todayTMPMIN = round(dftmp[k][dftmpCol[2*j+1]].iloc[i],2) if(dftmp[k][dftmpCol[2*j+1]].iloc[i] != -99) else 0
todayTMPAVE = round((todayTMPMAX+todayTMPMIN)/2,2) if((todayTMPMAX+todayTMPMIN)/2 != -99) else 0
'''### Accumulation :'''
if(todayTMPAVE) <= X5temp:
##
accumulation[i-1] = todayPCP *(1 + float(caseStudyStns[k]['cPrec']))*float(caseStudyStns[k]['dSnow'])*(1)
elif X5temp -1 < (todayTMPAVE) <= X5temp + 1:
accumulation[i-1] = todayPCP *(1 + float(caseStudyStns[k]['cPrec']))*float(caseStudyStns[k]['dSnow'])*float((X5temp + 1 -todayTMPAVE)/2)
else: accumulation[i-1] = 0
'''### Ablation :'''
if todayTMPAVE <= X5temp:
ablation[i-1] = 0
else:
#ablation[i-1] = (float(caseStudyStns[k]['fM']) + float(caseStudyStns[k]['rSnow'])*float(caseStudyStns[k]['iPot'])*0.001)*float(todayTMPAVE)*(1+0)
ablation[i-1] = (float(X2fM) + float(X4rSnow)*float(X3iPot)*0.001)*float(todayTMPAVE)*(1+0)
'''### Main mass balance equation for second day to the end of simulation:'''
snowDeposite[i-1] = 0 if (snowDeposite[i-2] + accumulation[i-1] - ablation[i-1]) < 0 else (snowDeposite[i-2] + accumulation[i-1] - ablation[i-1])
'''### storing three values in a list'''
total[i-1,0] = round((accumulation[i-1] - ablation[i-1]) , 2)
total[i-1,1] = round(snowDeposite[i-1], 2)
#total[i-1,3*j+2] = 1 if (total[i-1,3*j+1] > A) else 0
total[i-1,2] = 1 if (total[i-1,1] > snowThershold) else total[i-1,1] / snowThershold
## 2020/06/22
'''Check the posiibility of Snow Making'''
if (todayTMPAVE) <= X6tempArt:
artSnowCheck[i-1] = 1
elif X6tempArt < (todayTMPAVE) <= X6tempArt + 2:
artSnowCheck[i-1] = 1 * float((X6tempArt + 2 -todayTMPAVE)/2)
else:
artSnowCheck[i-1] = 0
'''Revenue'''
#revenue[i-1] = round((total[i-1,3*j+2] * 10), 2)
revenue[i-1] = float(round(revenueDayFixed,2)) if (total[i-1,2] > 0.6 * snowThershold ) else float(round(revenueDayFixed,2))*float(total[i-1,2] / (0.6 *snowThershold))
'''Cost''' #cost[i-1] = round((revenue[i-1] * 0.4) , 2)
#cost[i-1] = float(round(costDayFixed, 2)) if (total[i-1,3*j+2] > snowThershold ) else float(round(costDayFixed,2))*float((total[i-1,3*j+2] / snowThershold))
#cost[i-1] = float(round(costDayFixed, 2))
cost[i-1] = float(round(costDayFixed, 2)) if (total[i-1,2] > snowThershold ) else float(round(costDayFixed,2))*float((total[i-1,2] / snowThershold))
'''Profit'''
profit[i-1] = revenue[i-1] - cost[i-1]
'''Storing the artificial snow possibility and financial situation'''
totalMoney[i-1,0] = round(artSnowCheck[i-1], 2)
totalMoney[i-1,1] = round(revenue[i-1], 2)
totalMoney[i-1,2] = round(cost[i-1], 2)
totalMoney[i-1,3] = round(profit[i-1], 2)
'''Saving the Outputs of total list in a CSV file in a specific path'''
## 1st row as the column names:
columnsDF = []
columnsDF_aerSnowCheck = []
#for col in dfpcpCol[j]:
# columnsDF.append('SnowAmount_' + col)
# columnsDF.append('TotalSnowAmount_' + col)
# columnsDF.append('isOverSnow_' + col)
# columnsDF_aerSnowCheck.append('ArtSnowPossibility_' + col)
# columnsDF_aerSnowCheck.append('Revenue_' + col)
# columnsDF_aerSnowCheck.append('Cost_' + col)
# columnsDF_aerSnowCheck.append('Money_' + col)
#nameHeader = dfpcpCol[int(xClimateModel)]
nameHeader = dfpcpCol[climateModel]
columnsDF.append('SnowAmount_' + nameHeader)
columnsDF.append('TotalSnowAmount_' + nameHeader)
columnsDF.append('isOverSnow_' + nameHeader)
columnsDF_aerSnowCheck.append('ArtSnowPossibility_' + nameHeader)
columnsDF_aerSnowCheck.append('Revenue_' + nameHeader)
columnsDF_aerSnowCheck.append('Cost_' + nameHeader)
columnsDF_aerSnowCheck.append('Money_' + nameHeader)
'''Snow daily'''
columnsDF0 = ['DATE']
dfnew0 = pd.DataFrame(dateList, columns = columnsDF0)
dfnew1 = pd.DataFrame(total, columns = columnsDF)
df1 = pd.concat([dfnew0, dfnew1], axis=1, sort=False)
'''Money and Artifical Snow'''
dfnew2 = pd.DataFrame(totalMoney, columns = columnsDF_aerSnowCheck)
df2 = pd.concat([dfnew0, dfnew2], axis=1, sort=False)
if os.path.isdir(os.path.join(root, 'Outputs_py')):
pass
else: os.mkdir(os.path.join(root, 'Outputs_py'))
'''Make CSvs for daily Snow Outputs'''
outfolder =os.path.join(root, 'Outputs_py')
outfileName = 'Total_daily_' + caseStudyStns[k]['fileName'] + '.csv'
outputFile = os.path.join(outfolder, outfileName )
df1.to_csv(outputFile, index = False)
'''Artificial Snow and Financial Outputs'''
outfileName2 = 'Total_Moneydaily_' + caseStudyStns[k]['fileName'] + '.csv'
outputFile2 = os.path.join(outfolder, outfileName2)
df2.to_csv(outputFile2, index = False)
#return df1, df2
print('End of Part 1 Calculations!')
'''################################ PART2 ################################'''
'''##### PART 2 seasonal outputs Tipping points and Liklihood of Survival#####'''
print('Snow_Model: Starting Part 2, Running the model, seasonal outputs, reading files!')
#### 2020/06/10 ####
total_Daily_FilesAll = list()
total_Daily_Files = []
#### 2020/06/22 ####
total_Money_Files = []
for filename in os.walk(outfolder):
total_Daily_FilesAll = filename[2]
for bIndex in range (len(total_Daily_FilesAll)):
if 'Moneydaily_' in total_Daily_FilesAll[bIndex]:
total_Money_Files.append(total_Daily_FilesAll[bIndex])
elif 'Total_daily_' in total_Daily_FilesAll[bIndex]:
total_Daily_Files.append(total_Daily_FilesAll[bIndex])
else: continue
'''##Adding the whole address of directory to the name of total daily snow files'''
totalFiles = []
for i in range(len(total_Daily_Files)):
totalFiles.append(os.path.join(outfolder, total_Daily_Files[i]))
'''##Adding the whole address of directory to the name of total daily money files'''
totalMoneyFiles = []
for i in range(len(total_Money_Files)):
totalMoneyFiles.append(os.path.join(outfolder, total_Money_Files[i]))
print('Snow Model: Continuing of Part 2, Seasonal Outputs, Performing Tipping Points Analyses!')
## databases are read here:
dfSeason = [ None for _ in range(len(totalFiles))]
##2020/06/22
dfSeasonMoney = [ None for _ in range(len(totalMoneyFiles))]
##Here we calcluate seasonal tipping points here
for i in range(len(totalFiles)):
dfSeason[i] = pd.read_csv(totalFiles[i], low_memory=False)
start_date = date(1981, 1, 1)
end_date = date(2099, 12, 31)
dateList = []
for single_date in daterange(start_date, end_date):
dateList.append(single_date.strftime("%m/%d/%Y"))
start_season = []
end_season = []
for pp in range (1981, 2099, 1):
start_season.append(date(pp, 11, 1))
end_season.append(date(pp+1, 4, 30))
df2 = dfSeason[i]
df2.set_index('DATE', inplace = True)
df2Col = df2.columns
df2ColCal = []
for m in range(1):
#for m in range(68):
df2ColCal.append(df2Col[3*m+2])
sumGoodCondition = np.zeros([len(start_season), len(df2ColCal)])
#sumRows = np.zeros(len(df2ColCal)) ### Saeed 2020/06/11
sumRows = np.zeros(len(start_season)) ### Saeed 2020/08/17
for j in range(len(df2ColCal)):
for k in range(len(start_season)):
#for i in range(3):
start_date = start_season[k]
end_date = end_season[k]
#start_date = date(1981, 1, 2)
#end_date = date(1981, 1, 10)
for single_date in daterange(start_date, end_date):
sumGoodCondition[k,j] += df2[df2ColCal[j]].loc[single_date.strftime("%m/%d/%Y")]
#sumRows[j] += sumGoodCondition[k,j] ### Saeed 2020/06/11
sumRows[k] += sumGoodCondition[k,j] ### Saeed 2020/08/17
#AveragesumRows = np.average(sumRows/len(df2ColCal))
#AveragesumRows: the averange nmber of days in a season with good snow condition(season 1981-1982 to 2098-2099)
#AveragesumRows = np.average(sumRows/118) ## Saeed 2020/07/31
AveragesumRows = np.average(sumRows) ## Saeed 2020/08/17
df3 = pd.DataFrame(sumGoodCondition, columns = df2ColCal)
firstCol = []
for o in range (len(seasonList)-1):
firstCol.append(seasonList[o] +'-' + seasonList[o+1])
columnsDF1 = ['Season']
dfnew3 = pd.DataFrame(firstCol, columns = columnsDF1)
dfFinalSeason = pd.concat([dfnew3, df3], axis=1, sort=False)
if os.path.isdir(os.path.join(root, 'outSeason')):
pass
else:
os.mkdir(os.path.join(root, 'outSeason'))
outfileNameSeason = 'season_' + total_Daily_Files[i]
outFolderSeason = os.path.join(root, 'outSeason')
outputFileSeason = os.path.join(outFolderSeason, outfileNameSeason)
outFilesFinal = []
for filename in os.walk(outFolderSeason):
outFilesFinal = filename[2]
iii = len(outFilesFinal)
if os.path.isfile(outputFileSeason):
newOutFileNameSeason = outputFileSeason[0 : -4] + '_' + str(iii) + '.csv'
dfFinalSeason.to_csv(newOutFileNameSeason, index = False)
else:
dfFinalSeason.to_csv(outputFileSeason, index = False)
'''Calculating the tipping points'''
tipping_points = tipping_points_freq(dfFinalSeason, xGoodDays)
tipping_points_Report = float(tipping_points[1:])
print('Snow Model: Continuing of Part 2, Seasonal Outputs, Likelihood Analyses!')
df_sum_ch2018 = dfFinalSeason
### We transfer the data to a Matrix dfFinalSeason
df_sum_ch2018_M = df_sum_ch2018.iloc[0: , 1:]
df_sum_ch2018_Matrix = df_sum_ch2018_M.to_numpy()
df_sum_ch2018_M_Columns= df_sum_ch2018_M.columns
## We initialize the Matrix of Survival
#reportMatrix = np.zeros((118, 68))
reportMatrix = np.zeros((118, 1))
## We Calculate the Chance of Survival
xGoodDays_Condiion = xGoodDays
for j in range (len(df_sum_ch2018_M_Columns)):
for iii in range(len(df_sum_ch2018_M[df_sum_ch2018_M_Columns[0]])):
if df_sum_ch2018_M[df_sum_ch2018_M_Columns[j]][iii] < xGoodDays:
reportMatrix[iii,j] = (df_sum_ch2018_M[df_sum_ch2018_M_Columns[j]][iii] / xGoodDays) * 100
else:
reportMatrix[iii,j] = 100
AveragereportMatrix = np.average(reportMatrix/118)
#we sevae the results in a database
dfFinalSeasonLikelihood_noFirstCol = pd.DataFrame(reportMatrix, columns = df_sum_ch2018_M_Columns)
dfFinalSeasonLikelihood = pd.concat([dfnew3, dfFinalSeasonLikelihood_noFirstCol], axis=1, sort=False)
#make a directory for outputs of part 4
if os.path.isdir(os.path.join(root, 'outSeason_Likelihood_survival')):
pass
else:
os.mkdir(os.path.join(root, 'outSeason_Likelihood_survival'))
outfileNameSeasonLikelihood = 'season_Likelihood_' + total_Daily_Files[i]
outFolderSeasonLikelihood = os.path.join(root, 'outSeason_Likelihood_survival')
outputFileSeasonLikelihood = os.path.join(outFolderSeasonLikelihood, outfileNameSeasonLikelihood)
####
outFilesFinalLikelihood = []
for fname in os.walk(outFolderSeasonLikelihood):
outFilesFinalLikelihood = fname[2]
qq = len(outFilesFinalLikelihood)
if os.path.isfile(outputFileSeasonLikelihood):
newOutFileNameSeasonLikelihood = outputFileSeasonLikelihood[0 : -4] + '_' + str(qq) + '.csv'
dfFinalSeasonLikelihood.to_csv(newOutFileNameSeasonLikelihood, index = False)
else:
dfFinalSeasonLikelihood.to_csv(outputFileSeasonLikelihood, index = False)
print('End of Part 2 Calculations')
'''################################ PART3 ################################'''
'''##### PART 3 seasonal outputs for Artificial snow possibility and Economic Model#####'''
print('PART3: Running the Artificial snow possibility and Economic Model, seasonal outputs analyses!')
dfSeasonMoney = [ None for _ in range(len(totalMoneyFiles))]
for i in range(len(totalMoneyFiles)):
dfSeasonMoney[i] = pd.read_csv(totalMoneyFiles[i], low_memory=False)
start_date = date(1981, 1, 1)
end_date = date(2099, 12, 31)
dateList = []
for single_date in daterange(start_date, end_date):
dateList.append(single_date.strftime("%m/%d/%Y"))
start_season = []
end_season = []
for pp in range (1981, 2099, 1):
start_season.append(date(pp, 11, 1))
end_season.append(date(pp+1, 4, 30))
df4 = dfSeasonMoney[i]
df4.set_index('DATE', inplace = True)
df4Col = df4.columns
df4ColCal = [] # list columns
df4ColCalMoney = []
#### Here is the syntax that controls the columns that should be taken to account for cal
for m in range(1):
df4ColCal.append(df4Col[4*m+0])
df4ColCalMoney.append(df4Col[4*m+3])
sumGoodArtSnow = np.zeros([len(start_season), len(df4ColCal)])
sumRowsArtSnow = np.zeros(len(df4ColCal)) ### Saeed 2020/06/11
sumProfit = np.zeros([len(start_season), len(df4ColCalMoney)])
sumRowsProfit = np.zeros(len(df4ColCalMoney))
'''Artificial Snow'''
for j in range(len(df4ColCal)):
for k in range(len(start_season)):
start_date = start_season[k]
end_date = end_season[k]
for single_date in daterange(start_date, end_date):
sumGoodArtSnow[k,j] += df4[df4ColCal[j]].loc[single_date.strftime("%m/%d/%Y")]
sumProfit[k,j] += df4[df4ColCalMoney[j]].loc[single_date.strftime("%m/%d/%Y")]
sumRowsArtSnow[j] += sumGoodArtSnow[k,j] ### Saeed 2020/06/22
sumRowsProfit[j] += sumProfit[k,j]
#AveragesumRowsArtSnow = np.average(sumRowsArtSnow/len(df4ColCal))
AveragesumRowsArtSnow = np.average(sumRowsArtSnow/118)
#AveragesumRowsProfit = np.average(sumRowsProfit/len(df4ColCalMoney))
AveragesumRowsProfit = np.average(sumRowsProfit/118)
df5 = pd.DataFrame(sumGoodArtSnow, columns = df4ColCal)
df6 = pd.DataFrame(sumProfit, columns = df4ColCalMoney)
firstCol = []
for o in range (len(seasonList)-1):
firstCol.append(seasonList[o] +'-' + seasonList[o+1])
columnsDF2 = ['Season']
dfnew4 = pd.DataFrame(firstCol, columns = columnsDF2)
dfFinalSeasonArtSnow = pd.concat([dfnew4, df5], axis=1, sort=False)
dfFinalSeasonFinancial = pd.concat([dfnew4, df6], axis=1, sort=False)
if os.path.isdir(os.path.join(root, 'outSeasonArt')):
pass
else:
os.mkdir(os.path.join(root, 'outSeasonArt'))
if os.path.isdir(os.path.join(root, 'outSeasonFinancial')):
pass
else:
os.mkdir(os.path.join(root, 'outSeasonFinancial'))
outfileNameSeasonArt = 'season_Art_' + total_Money_Files[i]
outFolderSeasonArt = os.path.join(root, 'outSeasonArt')
outputFileSeasonArt = os.path.join(outFolderSeasonArt, outfileNameSeasonArt)
outfileNameSeasonMoney = 'season_Financial_' + total_Money_Files[i]
outFolderSeasonMoney = os.path.join(root, 'outSeasonFinancial')
outputFileSeasonMoney = os.path.join(outFolderSeasonMoney, outfileNameSeasonMoney)
##### Moshkel injast
outFilesFinalArt = []
for filename in os.walk(outFolderSeasonArt):
outFilesFinalArt = filename[2]
jjj = len(outFilesFinalArt)
if os.path.isfile(outputFileSeasonArt):
newOutFileNameSeasonArt = outputFileSeasonArt[0 : -4] + '_' + str(jjj) + '.csv'
dfFinalSeasonArtSnow.to_csv(newOutFileNameSeasonArt, index = False)
else:
dfFinalSeasonArtSnow.to_csv(outputFileSeasonArt, index = False)
####
outFilesFinalMoney = []
for fname in os.walk(outFolderSeasonMoney):
outFilesFinalMoney = fname[2]
q = len(outFilesFinalMoney)
if os.path.isfile(outputFileSeasonMoney):
newOutFileNameSeasonMoney = outputFileSeasonMoney[0 : -4] + '_' + str(q) + '.csv'
dfFinalSeasonFinancial.to_csv(newOutFileNameSeasonMoney, index = False)
else:
dfFinalSeasonFinancial.to_csv(outputFileSeasonMoney, index = False)
print('End of all calculations')
#return df1, outfolder, dfFinalSeason
#return {'y' : x1 * Xfactor1 * X2}
return {'y' : AveragesumRows, 'y1' : climateModel, 'y2' : dfpcpCol[climateModel], 'y3' : sumRows,
'y4' : AveragesumRowsArtSnow, 'y5' : AveragesumRowsProfit, 'y6' : AveragereportMatrix, 'y7' : tipping_points_Report}
# Step 4: EMA_Workbench connector
'''
Created on 20 dec. 2010
This file illustrated the use the EMA classes for a contrived example
It's main purpose has been to test the parallel processing functionality
.. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl>
'''
#(absolute_import, print_function, division,
# unicode_literals)
from ema_workbench import (Model, RealParameter, Constant, ScalarOutcome, ema_logging, IntegerParameter,
perform_experiments, TimeSeriesOutcome, ArrayOutcome)
from ema_workbench import (MultiprocessingEvaluator)
### import time
start_time = time.time()
if __name__ == '__main__':
ema_logging.LOG_FORMAT = '[%(name)s/%(levelname)s/%(processName)s] %(message)s'
ema_logging.log_to_stderr(ema_logging.INFO)
model = Model('UZHModel', function = snow_Model) # instantiate the model
# specify process model parameters xRCP=None, xClimateModel=None
model.uncertainties = [RealParameter("Xfactor1", 0.51, 3.49),
IntegerParameter ("xRCP", 1,3),
#RealParameter("xRCP", 0.51, 3.49),
RealParameter("xClimateModel", 0, 1),
RealParameter("X2fM", 1.01, 1.61),
RealParameter("X3iPot", 900, 1100),
RealParameter("X5temp", 3.0, 6.0),
RealParameter("X6tempArt", -2.0, -1.0)]
# specify polices IntegerParameter
model.levers = [RealParameter("x1SnowThershold", 200.0, 300.0),
RealParameter("xGoodDays", 70.0 , 100.0)]
# specify outcomes
model.outcomes = [ScalarOutcome('y'),
ScalarOutcome('y1'),
ArrayOutcome('y3'),
ScalarOutcome('y4'),
ScalarOutcome('y5'),
ScalarOutcome('y6'),
ScalarOutcome('y7')]
# override some of the defaults of the model
model.constants = [Constant("X4rSnow", 0.7),
Constant("xCostDay", 6),
Constant("xRevenueDay", 10)]