verifiedSolutionGraph.py
4.37 KB
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
import csv, re, sqlite3
fy= open("Maps/GraphInputFileW.txt", "w+")
fs= open("Maps/GraphInputFileS.txt", "w+")
fr= open("Maps/GraphInputFileR.txt", "w+")
con = sqlite3.connect("database_modfire.db")
cur = con.cursor()
Filemame = "VerifiedSolution.csv"
f=open(Filemame,"r")
lines=f.readlines()
result=[]
print("Extracting from database")
for line in lines:
MU = line.split(',')[0]
Pr = line.split(',')[1]
cur.execute("SELECT Year, Species, V_thin, V_harv FROM action_external where Id = "+str(MU)+" and Presc = "+str(Pr)+" and (V_thin or V_harv) > 0.0")
result = cur.fetchall()
for i in range(0, len(result)):
year = result[i][0]
act = ''
woodSum = int(result[i][2]) + int(result[i][3])
toWrite = str(MU)+","+str(year)+","+str(result[i][1])+","+str(woodSum)+"\n"
fy.write(toWrite)
cur.execute("SELECT Year, Soilloss FROM action_external where Id = "+str(MU)+" and Presc = "+str(Pr))
result = cur.fetchall()
for i in range(0, len(result)):
year = result[i][0]
toWrite = str(year)+","+str(result[i][1])+"\n"
fs.write(toWrite)
cur.execute("SELECT Year, Perc_r0 FROM action_external where Id = "+str(MU)+" and Presc = "+str(Pr))
result = cur.fetchall()
for i in range(0, len(result)):
year = result[i][0]
toWrite = str(year)+","+str(result[i][1])+"\n"
fr.write(toWrite)
fy.close()
fs.close()
fr.close()
con.close()
import matplotlib.pyplot as plt
Colors = {'Ec':'darkred', 'Pb':'cornflowerblue',
'Ct':'green', 'Sb':'purple',
'Qr':'darkgoldenrod','Rp':'orange','No':'gray'}
fig = plt.figure(figsize=(8,10))
ax = plt.gca()
ax.set_facecolor('lightgray')
#plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Colors['Ec'], label='Ec')
#plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Colors['Pb'], label='Pb')
#plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Colors['Ct'], label='Ct')
#plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Colors['Sb'], label='Sb')
#plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Colors['Qr'], label='Qr')
#plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Colors['Rp'], label='Rp')
#plt.plot([-25000, -25001], [165000, 165001], lw=17, c='lightgray')
#plt.legend()
Range = range(2020,2070)
fy= open("Maps/GraphInputFileW.txt", "r")
lines=fy.readlines()
ProdMAD = {'Ct':[], 'Ec':[], 'Pb':[], 'Qr':[], 'Sb':[], 'Rp':[]}
print("Plotting Wood Graph")
for year in range(2020, 2070):
sumE = 0
sumC = 0
sumP = 0
sumQ = 0
sumS = 0
sumR = 0
for line in lines:
a = line.split(',')
if(int(a[1]) == year):
if(a[2] == 'Ec'):
sumE += int(a[3][:-1])
elif(a[2] == 'Ct'):
sumC += int(a[3][:-1])
elif(a[2] == 'Pb'):
sumP += int(a[3][:-1])
elif(a[2] == 'Qr'):
sumQ += int(a[3][:-1])
elif(a[2] == 'Sb'):
sumS += int(a[3][:-1])
elif(a[2] == 'Rp'):
sumR += int(a[3][:-1])
ProdMAD['Ec'].append(sumE)
ProdMAD['Ct'].append(sumC)
ProdMAD['Pb'].append(sumP)
ProdMAD['Qr'].append(sumQ)
ProdMAD['Sb'].append(sumS)
ProdMAD['Rp'].append(sumR)
plt.stackplot(Range, ProdMAD['Ct'], ProdMAD['Ec'], ProdMAD['Pb'], ProdMAD['Qr'], ProdMAD['Sb'], ProdMAD['Rp'],
labels = ['Ct', 'Ec', 'Pb', 'Qr', 'Sb','Rp'],
colors= [Colors['Ct'],Colors['Ec'],Colors['Pb'],Colors['Qr'],Colors['Sb'], Colors['Rp']] )
plt.grid()
plt.legend(loc='upper left')
plt.xlabel('Year')
plt.ylabel('WoodYield')
#plt.show()
plt.savefig('Maps/VerifiedSolutionMaps/GraphWood',dpi=100)
fs= open("Maps/GraphInputFileS.txt", "r")
lines=fs.readlines()
print("Plotting Soil Loss Graph")
soils = []
for year in range(2020, 2070):
soilSum = 0
for line in lines:
a = line.split(',')
if(int(a[0]) == year):
soilSum += float(a[1][:-1])
soils.append(soilSum)
fig7 = plt.figure(figsize=(10,4))
plt.plot(Range, soils, '-o', c="darkgreen")
plt.xlabel('Year')
plt.ylabel('Soil Loss')
plt.savefig('Maps/VerifiedSolutionMaps/GraphSoilLoss',dpi=100)
fr= open("Maps/GraphInputFileR.txt", "r")
lines=fr.readlines()
print("Plotting Fire Risk Protection Graph")
rpercentile = []
for year in range(2020, 2070):
riskSum = 0
for line in lines:
a = line.split(',')
if(int(a[0]) == year):
riskSum += float(a[1][:-1])
rpercentile.append(riskSum)
fig8 = plt.figure(figsize=(10,4))
plt.plot(Range, rpercentile, '-o', c="red")
plt.xlabel('Year')
plt.ylabel('Fire Risk Protection')
plt.savefig('Maps/VerifiedSolutionMaps/GraphRiskPercentile',dpi=100)
fy.close()
fs.close()
fr.close()