forked from roberthansen/py_cet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables_match_sql.py
412 lines (384 loc) · 18.4 KB
/
tables_match_sql.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
import numpy as np
import types
from models import EDCS_Connection, EDCS_Table, EDCS_Query_Results, Local_CSV
from tables import setup_input_programs, setup_settings, \
setup_emissions, \
setup_combustion_types, \
setup_output_measures, setup_output_programs, setup_output_portfolio,\
setup_rate_schedule_electric, setup_rate_schedule_gas
from login import user
def setup_input_measures(source, source_name, first_year, market_effects_benefits, market_effects_costs, user):
if source == 'csv':
if source_name == 'Measure.csv':
InputMeasures = Local_CSV(source_name, delimiter='|')
else:
InputMeasures = Local_CSV(source_name, delimiter=',')
elif source == 'database':
InputMeasures = EDCS_Table(source_name,user['id'],user['passwd'])
else:
InputMeasures = EDCS_Table('InputMeasureCEDARS',user['id'],user['passwd'])
# fix input measure column name and type issues:
column_name_map = [
['BldgType','BuildingType'],
['CEInputID','CET_ID'],
['ClaimYearQuarter','InstallationQuarter'],
['ClimateZone','ClimateZone'],
['DILaborCost','UnitLaborCost'],
['DIMaterialCost','UnitMaterialsCost'],
['E3ClimateZone','ClimateZone'],
['E3GasSavProfile','GasSavingsProfile'],
['E3GasSector','GasTargetSector'],
['E3MeaElecEndUseShape','ElectricEndUse'],
['E3TargetSector','ElectricTargetSector'],
['EUL_Yrs','EUL'],
['ElecEndUseShape','ElectricEndUse'],
['ElecRateSchedule','ElectricRateSchedule'],
['ElecTargetSector','ElectricTargetSector'],
['EndUserRebate','UnitEndUserRebate'],
['GasSector','GasTargetSector'],
['IRThm','IRTherm'],
['IncentiveToOthers','UnitIncentiveToOthers'],
['InstallationRateTherm','IRTherm'],
['InstallationRatekW','IRkW'],
['InstallationRatekWh','IRkWh'],
['MarketEffectBens','MarketEffectsBenefits'],
['MarketEffectCost','MarketEffectsCosts'],
['MeasAppType','MeasureApplicationType'],
['MeasCode','MeasureCode'],
['MeasDescription','MeasureDescription'],
['MeasImpactType','ImpactType'],
['MeasInflation','AnnualInflationRate'],
['NTGRThm','NTGRTherm'],
['NormUnit','NormalizingUnits'],
['NumUnits','Quantity'],
['PA','ProgramAdministrator'],
['PreDesc','PreInterventionDescription'],
['PrgID','ProgramID'],
['Qty','Quantity'],
['RRThm','RRTherm'],
['RUL_Yrs','RUL'],
['RateScheduleElec','ElectricRateSchedule'],
['RateScheduleGas','GasRateSchedule'],
['RealizationRateTherm','RRTherm'],
['RealizationRatekW','RRkW'],
['RealizationRatekWh','RRkWh'],
['Residential_Flag','ResidentialFlag'],
['SourceDesc','ExAnteSourceDescription'],
['StdDesc','StandardDescription'],
['TechGroup','TechnologyGroup'],
['TechType','TechnologyType'],
['UESThm','Therm1'],
['UESThm_ER','Therm2'],
['UESkW','kW1'],
['UESkW_ER','kW2'],
['UESkWh','kWh1'],
['UESkWh_ER','kWh2'],
['UnitDirectInstallLab','UnitLaborCost'],
['UnitDirectInstallMat','UnitMaterialsCost'],
['UnitMeaCost1stBaseline','UnitGrossCost1'],
['UnitMeaCost2ndBaseline','UnitGrossCost2'],
['UnitMeasureGrossCost','UnitGrossCost1'],
['UnitMeasureGrossCost_ER','UnitGrossCost2'],
['UnitTherm1stBaseline','Therm1'],
['UnitTherm2ndBaseline','Therm2'],
['UnitkW1stBaseline','kW1'],
['UnitkW2ndBaseline','kW2'],
['UnitkWh1stBaseline','kWh1'],
['UnitkWh2ndBaseline','kWh2'],
['Upstream_Flag','UpstreamFlag'],
['Version','DEERVersion'],
]
for old_name,new_name in column_name_map:
InputMeasures.rename_column(old_name,new_name)
# Set values for missing columns:
if 'ImpactType' not in InputMeasures.data.columns:
new_column = {'ImpactType':[''] * InputMeasures.data.CET_ID.count()}
InputMeasures.append_columns(new_column)
if 'AnnualInflationRate' not in InputMeasures.data.columns:
new_column = {'AnnualInflationRate':[0] * InputMeasures.data.CET_ID.count()}
InputMeasures.append_columns(new_column)
InputMeasures.column_map('ClimateZone',lambda s: str(s).upper())
InputMeasures.column_map('ElectricEndUse',lambda s: s.upper())
InputMeasures.column_map('ElectricTargetSector',lambda s: s.upper())
InputMeasures.column_map('GasSavingsProfile',lambda s: s or '')
InputMeasures.column_map('GasSavingsProfile',lambda s: s.upper())
InputMeasures.column_map('GasTargetSector',lambda s: s or '')
InputMeasures.column_map('GasTargetSector',lambda s: s.upper())
#INCORRECT OVERWRITE MEASURE-LEVEL MARKET EFFECTS BENEFITS TO MATCH SQL:
InputMeasures.data.MarketEffectsBenefits = market_effects_benefits
f = lambda x: market_effects_costs if x is None else x
InputMeasures.column_map('MarketEffectsCosts', f)
# helper function to calculate additional columns for input measure table:
def input_measure_calculated_columns(data_frame_row):
year,quarter = list(map(int,data_frame_row.InstallationQuarter.split('Q')))
quarter_index = 4 * year + quarter - 1
rul_quarters = 4 * data_frame_row.RUL
eul_quarters = 4 * data_frame_row.EUL
if rul_quarters == 0:
EUL = [data_frame_row.EUL,0]
EULq = [eul_quarters,0]
else:
EUL = [data_frame_row.RUL,data_frame_row.EUL]
EULq = [rul_quarters,eul_quarters]
calculated_columns = {
'Qi' : quarter_index,
'EUL1' : EUL[0],
'EUL2' : EUL[1],
'EULq1' : EULq[0],
'EULq2' : EULq[1],
'RULq' : rul_quarters,
'EULq' : eul_quarters,
}
return calculated_columns
# append input measures with calculated columns:
InputMeasures.append_columns(InputMeasures.data.apply(input_measure_calculated_columns, axis='columns', result_type='expand'))
nan_to_num_columns = ['Quantity','EUL','RUL','NTGRkW','NTGRkWh','NTGRTherm',
'NTGRCost','IRkW','IRkWh','IRTherm','AnnualInflationRate','RRkW',
'RRkWh','RRTherm','MarketEffectsBenefits','MarketEffectsCosts','kW1',
'kW2','kWh1','kWh2','Therm1','Therm2','UnitGrossCost1','UnitGrossCost2',
'UnitLaborCost','UnitMaterialsCost','UnitEndUserRebate',
'UnitIncentiveToOthers','EULq1','EULq2','RULq','EULq']
for column in nan_to_num_columns:
InputMeasures.column_map(column,np.nan_to_num)
nan_to_str_columns = [
'ElectricTargetSector',
'GasSavingsProfile',
'GasTargetSector',
'ElectricEndUse',
'CombustionType',
]
for column in nan_to_str_columns:
InputMeasures.column_map(column,lambda x: '' if x is np.nan else x)
return InputMeasures
def setup_avoided_cost_electric(acc_source, source_name, InputMeasures, user):
### parameters:
### acc_source : a string, either 'csv' or 'database', indicating whether
### the avoided cost electric table should be retrieved from a
### comma separated value text file or a Microsoft SQL Server
### database
### source_name : a string containing the file path if source is 'csv'
### or the database object name if source is 'database'
### InputMeasures : an instance of an 'InputMeasures' object of class
### 'EDCS_Table' or 'EDCS_Query_Results'
### user : a dictionary containing items labelled 'id' and 'passwd'
### which provide login credentials for the database if needed
if acc_source == 'csv':
if InputMeasures.source == 'database':
if InputMeasures.table_name == 'InputMeasure':
sql_str = '\n\tSELECT DISTINCT\n\t\tPA + \'|\' + ' \
'UPPER(ElecTargetSector) + \'|\' + ' \
'UPPER(ElecEndUseShape) + \'|\' + ' \
'UPPER(ClimateZone) AS LookupKey' \
'\n\tFROM InputMeasure\n'
elif InputMeasures.table_name == 'InputMeasureCEDARS':
sql_str = '\n\tSELECT DISTINCT\n\t\tPA + \'|\' + ' \
'UPPER(E3TargetSector) + \'|\' + ' \
'UPPER(E3MeaElecEndUseShape) + \'|\' + ' \
'UPPER(E3ClimateZone) AS LookupKey' \
'\n\tFROM InputMeasureCEDARS\n'
lookup_keys = list(
EDCS_Query_Results(
sql_str,
user['id'],
user['passwd']
).data.LookupKey
)
else:
lookup_keys = list(dict.fromkeys([
'|'.join(r[1][[
'ProgramAdministrator',
'ElectricTargetSector',
'ElectricEndUse',
'ClimateZone'
]]) for r in InputMeasures.data.iterrows()
]))
def filter_function(dataframe_chunk):
f = lambda r: r['PA'] + '|' + r['TS'].upper() + '|' + r['EU'].upper() + '|' + str(r['CZ']) in lookup_keys
return dataframe_chunk.apply(f,axis='columns')
AvoidedCostElectric = Local_CSV(
source_name,
delimiter=',',
filter_csv=True,
filter_function=filter_function
)
else:
if InputMeasures.source == 'database':
# use the following query string when input measures are loaded into database:
if InputMeasures.table_name == 'InputMeasure':
sql_str = '\n\tSELECT *\n\tFROM {}\n\tWHERE PA + \'|\' + ' \
'UPPER(TS) + \'|\' + UPPER(EU) + \'|\' + UPPER(CZ)\n\t' \
'IN (\n\t\tSELECT PA + \'|\' + UPPER(ElecTargetSector) + ' \
'\'|\' + UPPER(ElecEndUseShape) + \'|\' + ' \
'UPPER(ClimateZone)\n\t\t' \
'FROM InputMeasure\n\t)\n'.format(source_name)
elif InputMeasures.table_name == 'InputMeasureCEDARS':
sql_str = '\n\tSELECT *\n\tFROM {}\n\tWHERE PA + \'|\' + ' \
'UPPER(TS) + \'|\' + UPPER(EU) + \'|\' + UPPER(CZ)\n\t' \
'IN (\n\t\tSELECT' \
'\n\t\t\tPA + \'|\' + UPPER(E3TargetSector) + \'|\' + ' \
'UPPER(E3MeaElecEndUseShape) + \'|\' + ' \
'UPPER(E3ClimateZone) AS LookupKey\n\t\t' \
'FROM InputMeasureCEDARS\n\t)\n'.format(source_name)
else:
# use the following query string when input measures are from a file:
lookup_keys = ',\n\t\t'.join(
list(
dict.fromkeys([
'\''+'|'.join(
r[1][[
'ProgramAdministrator',
'ElectricTargetSector',
'ElectricEndUse',
'ClimateZone'
]]
)+'\'' for r in InputMeasures.data.iterrows()
])
)
)
sql_str = '\n\tSELECT *\n\tFROM {}\n\tWHERE PA + \'|\' + '\
'UPPER(TS) + \'|\' + UPPER(EU) + \'|\' + UPPER(CZ)' \
'\n\tIN (\n\t\t{}\n\t)\n'.format(source_name,lookup_keys)
AvoidedCostElectric = EDCS_Query_Results(sql_str,user['id'],user['passwd'])
rename_columns = [
['PA','ProgramAdministrator'],
['TS','ElectricTargetSector'],
['EU','ElectricEndUse'],
['CZ','ClimateZone'],
['Qtr','UsageQuarter'],
]
for old_name,new_name in rename_columns:
AvoidedCostElectric.rename_column(old_name,new_name)
# fix column formatting:
AvoidedCostElectric.column_map('ElectricTargetSector',lambda s: s.upper())
AvoidedCostElectric.column_map('ElectricEndUse',lambda s: s.upper())
AvoidedCostElectric.column_map('ClimateZone',lambda s: str(s).upper())
# apply universal quarter indices:
def quarter_index(r):
year,quarter = list(map(int,r.UsageQuarter.split('Q')))
return {'Qi' : year * 4 + quarter - 1}
AvoidedCostElectric.append_columns(AvoidedCostElectric.data.apply(quarter_index,axis='columns',result_type='expand'))
# add method to get avoided cost electric data filtered by a single input measure:
def filter_by_measure(self, measure):
filtered_avoided_costs_electric = self.data.get(
(self.data.ProgramAdministrator == measure.ProgramAdministrator) & \
(self.data.ElectricTargetSector == measure.ElectricTargetSector) & \
(self.data.ElectricEndUse == measure.ElectricEndUse) & \
(self.data.ClimateZone == measure.ClimateZone) & \
(self.data.Qi >= measure.Qi) & \
#INCLUDE EXTRA QUARTER TO MATCH SQL:
(self.data.Qi < measure.Qi + measure.EULq + 1)
)
return filtered_avoided_costs_electric
AvoidedCostElectric.filter_by_measure = \
types.MethodType(filter_by_measure,AvoidedCostElectric)
return AvoidedCostElectric
def setup_avoided_cost_gas(acc_source, source_name, InputMeasures, user):
### parameters:
### acc_source : a string, either 'csv' or 'database', indicating whether
### the avoided cost electric table should be retrieved from a
### comma separated value text file or a Microsoft SQL Server
### database
### source_name : a string containing the file path if source is 'csv'
### or the database object name if source is 'database'
### InputMeasures : an instance of an 'InputMeasures' object of class
### 'EDCS_Table' or 'EDCS_Query_Results'
### user : a dictionary containing items labelled 'id' and 'passwd'
### which provide login credentials for the database if needed
if acc_source == 'csv':
if InputMeasures.source == 'database':
if InputMeasures.table_name == 'InputMeasure':
sql_str = '\n\tSELECT DISTINCT\n\t\tPA + \'|\' + ' \
'UPPER(GasSector) + \'|\' + UPPER(GasSavingsProfile) ' \
'AS LookupKey\n\tFROM InputMeasure\n'
elif InputMeasures.table_name == 'InputMeasureCEDARS':
sql_str = '\n\tSELECT DISTINCT\n\t\tPA + \'|\' + ' \
'UPPER(E3GasSector) + \'|\' + UPPER(GasSavProfile) AS ' \
'LookupKey\n\tFROM InputMeasureCEDARS\n'
lookup_keys = list(
EDCS_Query_Results(
sql_str,
user['id'],
user['passwd']
).data.LookupKey
)
else:
lookup_keys = list(dict.fromkeys([
'|'.join(r[1][[
'ProgramAdministrator',
'GasTargetSector',
'GasSavingsProfile',
]]) for r in InputMeasures.data.iterrows()
]))
def filter_function(dataframe_chunk):
f = lambda r: r['PA'] + '|' + r['GS'].upper() + '|' + r['GP'].upper() in lookup_keys
return dataframe_chunk.apply(f, axis='columns')
AvoidedCostGas = Local_CSV(
source_name,
delimiter=',',
filter_csv=True,
filter_function=filter_function
)
else:
if InputMeasures.source == 'database':
# use the following query string when input measures are loaded into database:
if InputMeasures.table_name == 'InputMeasure':
sql_str = 'SELECT *\n\tFROM {}\n\tWHERE PA + \'|\' + ' \
'UPPER(GS) + \'|\' + UPPER(GP)\n\t' \
'IN (\n\t\tSELECT PA + UPPER(GasSector) + \'|\' + ' \
'UPPER(GasSavingsProfile)\n\t\t' \
'FROM InputMeasure\n\t)\n'.format(source_name)
elif InputMeasures.table_name == 'InputMeasureCEDARS':
sql_str = 'SELECT *\n\tFROM {}\n\tWHERE PA + \'|\' + ' \
'UPPER(GS) + \'|\' + UPPER(GP)\n\t' \
'IN (\n\t\tSELECT PA + \'|\' + UPPER(E3GasSector) + ' \
'\'|\' + UPPER(E3GasSavProfile)\n\t\t' \
'FROM InputMeasureCEDARS\n\t)\n'.format(source_name)
else:
lookup_keys = ',\n\t\t'.join(
list(
dict.fromkeys(
['\''+'|'.join(
r[1][[
'ProgramAdministrator',
'GasTargetSector',
'GasSavingsProfile',
]]
)+'\'' for r in InputMeasures.data.iterrows()]
)
)
)
sql_str = '\n\tSELECT *\n\tFROM {}\n\tWHERE PA + \'|\' + ' \
'UPPER(GS) + \'|\' + UPPER(GP)' \
'\n\tIN (\n\t\t{}\n\t)\n'.format(source_name,lookup_keys)
AvoidedCostGas = EDCS_Query_Results(sql_str,user['id'],user['passwd'])
rename_columns = [
['PA','ProgramAdministrator'],
['GS','GasTargetSector'],
['GP','GasSavingsProfile'],
['Qtr','UsageQuarter'],
['Total','Cost'],
]
for old_name,new_name in rename_columns:
AvoidedCostGas.rename_column(old_name,new_name)
# fix column formatting:
AvoidedCostGas.column_map('GasTargetSector',lambda s: s.upper())
AvoidedCostGas.column_map('GasSavingsProfile',lambda s: s.upper())
# apply universal quarter indices:
def quarter_index(r):
year,quarter = list(map(int,r.UsageQuarter.split('Q')))
return {'Qi' : year * 4 + quarter - 1}
AvoidedCostGas.append_columns(AvoidedCostGas.data.apply(quarter_index,axis='columns',result_type='expand'))
# add method to get avoided cost gas data filtered by a single input measure:
def filter_by_measure(self, measure):
filtered_avoided_costs_gas = self.data.get(
(self.data.ProgramAdministrator == measure.ProgramAdministrator) & \
(self.data.GasTargetSector == measure.GasTargetSector) & \
(self.data.GasSavingsProfile == measure.GasSavingsProfile) & \
(self.data.Qi >= measure.Qi) & \
#INCLUDE EXTRA QUARTER TO MATCH SQL:
(self.data.Qi < measure.Qi + measure.EULq + 1)
)
return filtered_avoided_costs_gas
AvoidedCostGas.filter_by_measure = \
types.MethodType(filter_by_measure,AvoidedCostGas)
return AvoidedCostGas