-
Notifications
You must be signed in to change notification settings - Fork 1
/
NEIComparison.py
232 lines (175 loc) · 6.28 KB
/
NEIComparison.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
import Options
class NEIComparison(Options.ScenarioOptions):
def __init__(self, modelRunTitle):
Options.ScenarioOptions.__init__(self, modelRunTitle)
self.cellulosicAllocation = 0.34
self.cornGrainAllocation = 0.54
query = """
CREATE TABLE summedEmissions
(
fips char(5) ,
feedstock text ,
prod float ,
harv_Ac float ,
NOX float DEFAULT 0.0,
NH3 float DEFAULT 0.0,
SOX float DEFAULT 0.0,
VOC float DEFAULT 0.0,
PM10 float DEFAULT 0.0,
PM25 float DEFAULT 0.0,
CO float DEFAULT 0.0);"""
self.__executeQuery__(query)
def __setNEIRatioTable__(self, feedstock):
query = """
CREATE TABLE """+feedstock+"""_NEIRatio
(
fips char(5) ,
nox float ,
sox float ,
co float ,
pm10 float ,
pm25 float ,
voc float ,
nh3 float);"""
self.__executeQuery__(query)
def createSummedEmissionsTable(self, feedstock):
if feedstock == 'CG':
f = "Corn Grain"
prod = "dat.total_prod, dat.total_harv_ac"
elif feedstock == 'SG':
f = "Switchgrass"
prod = "dat.prod, dat.harv_ac"
elif feedstock == 'WS':
f = "Wheat Straw"
prod = "dat.prod, dat.harv_ac"
elif feedstock == 'CS':
f = "Corn Stover"
prod = "dat.prod, dat.harv_ac"
elif feedstock == 'FR':
f = "Forest Residue"
prod = "dat.fed_minus_55, 0"
query = """
INSERT INTO summedEmissions
WITH
"""
#populate tables that have fertilizer emissions
# if feedstock == 'CG' or feedstock == 'SG':
if feedstock != 'FR':
query += """
Fert as (SELECT DISTINCT fips,
sum(nox) as nox,
sum(nh3) as nh3
FROM %s_nfert
GROUP BY fips),
---------------------------------------------------------------------------------------------
""" % (feedstock)
if feedstock == 'CG' or feedstock == 'SG':
query += """
Chem as (SELECT DISTINCT fips,
sum(voc) as voc
FROM %s_chem
GROUP BY fips),
---------------------------------------------------------------------------------------------
""" % (feedstock)
query +="""
Raw as (SELECT DISTINCT fips,
sum(nox) AS nox,
sum(nh3) AS nh3,
sum(sox) AS sox,
sum(voc) AS voc,
(sum(pm10) + sum(fug_pm10)) AS pm10,
(sum(pm25) + sum(fug_pm25)) AS pm25,
(sum(co)) AS co
FROM %s_raw
GROUP BY fips)
---------------------------------------------------------------------------------------------
""" % (feedstock)
if feedstock == 'CG' or feedstock == 'SG':
query +="""
(SELECT dat.fips, %s, %s,
(raw.nox + fert.nox) as nox,
(raw.nh3 + fert.nh3) as nh3,
(raw.sox) as sox,
(raw.voc + chem.voc) as voc,
(raw.pm10) as pm10,
(raw.pm25) as pm25,
(raw.co) as co
FROM %s dat
LEFT JOIN Fert ON fert.fips = dat.fips
LEFT JOIN Chem ON chem.fips = dat.fips
LEFT JOIN Raw ON raw.fips = dat.fips
)
;""" % ("'"+f+"'", prod,
self.productionSchema +'.'+ feedstock + "_data")
elif feedstock == 'CS' or feedstock == 'WS':
query +="""
(SELECT dat.fips, %s, %s,
(raw.nox + fert.nox) as nox,
(raw.nh3 + fert.nh3) as nh3,
(raw.sox) as sox,
(raw.voc) as voc,
(raw.pm10) as pm10,
(raw.pm25) as pm25,
(raw.co) as co
FROM %s dat
LEFT JOIN Fert ON fert.fips = dat.fips
LEFT JOIN Raw ON raw.fips = dat.fips
)
;""" % ("'"+f+"'", prod,
self.productionSchema +'.'+ feedstock + "_data")
elif feedstock == 'FR':
query +="""
(SELECT dat.fips, %s, %s,
(raw.nox) as nox,
(raw.nh3) as nh3,
(raw.sox) as sox,
(raw.voc) as voc,
(raw.pm10) as pm10,
(raw.pm25) as pm25,
(raw.co) as co
FROM %s dat
LEFT JOIN Raw ON raw.fips = dat.fips
)
;""" % ("'"+f+"'", prod,
self.productionSchema +'.'+ feedstock + "_data")
self.__executeQuery__(query)
def createNEIComparison(self, feedstock):
if feedstock == 'CG':
allocation = str(self.cornGrainAllocation)
else:
allocation = str(self.cellulosicAllocation)
self.__setNEIRatioTable__(feedstock)
if feedstock == 'CG':
f = 'Corn Grain'
elif feedstock == 'SG':
f = 'Switchgrass'
elif feedstock == 'CS':
f = 'Corn Stover'
elif feedstock == 'WS':
f = 'Wheat Straw'
elif feedstock == 'FR':
f = 'Forest Residue'
query = """
INSERT INTO """+feedstock+"""_NEIRatio
WITH
nrel AS (select distinct fips, sum(nox) as nox,
sum(sox) as sox,
sum(co) as co,
sum(pm10) as pm10,
sum(pm25) as pm25,
sum(voc) as voc,
sum(nh3) as nh3 from summedEmissions where feedstock ilike '%"""+f+"""%' GROUP BY fips),
nei as (select fips, nox, sox, co, pm10, pm25, voc, nh3 from """+self.constantsSchema+""".nei_data_by_county)
select c.fips,
(nrel.nox * """+allocation+""") / nei.nox as nox,
(nrel.sox * """+allocation+""") / nei.sox as sox,
(nrel.co * """+allocation+""") / nei.co as co,
(nrel.pm10 * """+allocation+""") / nei.pm10 as PM10,
(nrel.pm25 * """+allocation+""") / nei.pm25 as PM25,
(nrel.voc * """+allocation+""") / nei.voc as VOC,
(nrel.nh3 * """+allocation+""") / nei.nh3 as NH3
from """+ self.productionSchema +""".cg_data c
LEFT JOIN nrel ON c.fips = nrel.fips
LEFT JOIN nei ON c.fips = nei.fips
"""
self.__executeQuery__(query)