-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContributionFigure.py
129 lines (92 loc) · 4.21 KB
/
ContributionFigure.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
"""
@author: nfisher
"""
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from pylab import median
import matplotlib
import psycopg2 as db
from ContributionTests import ChooseSQL
#import time
import Options
class ContributionAnalysis(Options.ScenarioOptions):
def __init__(self, modelRunTitle):
Options.ScenarioOptions.__init__(self, modelRunTitle)
# startTime = time.time()
#-----------inputs begin
fig = Figure( figsize=(7,5), dpi=200)
canvas = FigureCanvas(fig)
fColor = ['r','b','g','k','c']
fMarker = ['o','o','o','o','o']
feedstockList = ['CG','SG','CS','WS','FR']
pollutantList = ['NH3','NOx','VOC','PM25','PM10','CO','SOx']
pollutantLabels = ['$NH_3$','$NO_x$','$VOC$','$PM_{2.5}$','$PM_{10}$','$CO$','$SO_x$']
activityList = ['Non-Harvest','Fertilizer','Chemical','Harvest','Transport']
activityLabels = ['Non-Harvest','N-Fertilizer','Pesticide','Harvest','Transport']
self.f = open(self.path + 'Figures/Contribution_numerical.csv','w')
#-----------inputs end
index = 0
for yLabel, activity in enumerate(activityList):
print activity
for titleLabel, pollutant in enumerate(pollutantList):
ax = fig.add_subplot( 5, 7, index+1 )
ax.set_xlim([-1,5])
ax.set_ylim([-0.1, 1.1])
#show y labels on first column only
if index % 7 == 0 :
matplotlib.rcParams.update({'font.size': 8})
ax.set_ylabel(activityLabels[yLabel])
else:
ax.set_yticklabels([])
#show pollutant labels above first row of plots
if index < 7:
ax.set_title(pollutantLabels[titleLabel])
#show x labels below last row only
if index < 28:
ax.set_xticklabels([])
else:
ax.set_xticklabels(([''] + feedstockList), rotation='vertical')
index+=1
self.f.write(pollutant+','+activity+'\n')
for fNum, feedstock in enumerate(feedstockList):
x = ChooseSQL(activity, pollutant, feedstock, self.schema)
self.makePlots(ax, x, fNum,
fColor[fNum], fMarker[fNum], feedstock)
self.f.write('\n')
# print figure to a .png file (small file size)
# canvas.print_figure('Contribution Analysis.tiff')
fig.savefig(self.path + 'Figures/Contribution_Figure.png', format = 'png')
self.f.close()
# print time.time() - startTime, ' seconds'
"""
This section of code formats the plots
"""
def makePlots(self, ax, x, fNum, fColor, fMarker, feedstock):
x.getQuery()
if x.queryString.startswith('No'):
pass
elif x.queryString.startswith('FR'):
data = [1,1]
ax.plot([fNum]*2,[1,1],fColor,marker=fMarker,markersize=2)
else:
cur = self.conn.cursor()
print x.queryString
cur.execute(x.queryString)
#[all data]
data = cur.fetchall()
cur.close()
medVal = median(data)
maxVal = max(data)
minVal = min(data)
ax.plot([fNum],medVal,fColor,marker='_', markersize=7)
#Plot the max/min values
ax.plot([fNum]*2,[maxVal, minVal],fColor,marker=fMarker, markersize=2)
self.writeResults(feedstock, str(maxVal[0]), str(medVal), str(minVal[0]))
"""
This section writes the results to a file in a readable format.
"""
def writeResults(self, feedstock, maxVal, medVal, minVal):
self.f.write(feedstock+','+maxVal+','+medVal+','+minVal+'\n')
if __name__ == "__main__":
modelRunTitle = "AllFeed"
ContributionAnalysis(modelRunTitle)