/
eia923.py
499 lines (424 loc) · 20.3 KB
/
eia923.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
"""Functions for pulling EIA 923 data out of the PUDl DB."""
import pandas as pd
import sqlalchemy as sa
import pudl
def generation_fuel_eia923(pudl_engine, pt, freq=None,
start_date=None, end_date=None):
"""
Pull records from the generation_fuel_eia923 table in given date range.
Optionally, aggregate the records over some timescale -- monthly, yearly,
quarterly, etc. as well as by fuel type within a plant.
If the records are not being aggregated, all of the database fields are
available. If they're being aggregated, then we preserve the following
fields. Per-unit values are re-calculated based on the aggregated totals.
Totals are summed across whatever time range is being used, within a
given plant and fuel type.
* ``plant_id_eia``
* ``report_date``
* ``fuel_type_code_pudl``
* ``fuel_consumed_units``
* ``fuel_consumed_for_electricity_units``
* ``fuel_mmbtu_per_unit``
* ``fuel_consumed_mmbtu``
* ``fuel_consumed_for_electricity_mmbtu``
* ``net_generation_mwh``
In addition, plant and utility names and IDs are pulled in from the EIA
860 tables.
Args:
pudl_engine (sqlalchemy.engine.Engine): SQLAlchemy connection engine
for the PUDL DB.
pt (immutabledict): a sqlalchemy metadata dictionary of pudl tables
freq (str): a pandas timeseries offset alias. The original data is
reported monthly, so the best time frequencies to use here are
probably month start (freq='MS') and year start (freq='YS').
start_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
end_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
Returns:
pandas.DataFrame: A DataFrame containing all records from the EIA 923
Generation Fuel table.
"""
gf_tbl = pt['generation_fuel_eia923']
gf_select = sa.sql.select([gf_tbl, ])
if start_date is not None:
gf_select = gf_select.where(
gf_tbl.c.report_date >= start_date)
if end_date is not None:
gf_select = gf_select.where(
gf_tbl.c.report_date <= end_date)
gf_df = pd.read_sql(gf_select, pudl_engine)
cols_to_drop = ['id']
gf_df = gf_df.drop(cols_to_drop, axis=1)
# fuel_type_code_pudl was formerly aer_fuel_category
by = ['plant_id_eia', 'fuel_type_code_pudl']
if freq is not None:
# Create a date index for temporal resampling:
gf_df = gf_df.set_index(pd.DatetimeIndex(gf_df.report_date))
by = by + [pd.Grouper(freq=freq)]
# Sum up these values so we can calculate quantity weighted averages
gf_gb = gf_df.groupby(by=by)
gf_df = gf_gb.agg({
'fuel_consumed_units': pudl.helpers.sum_na,
'fuel_consumed_for_electricity_units': pudl.helpers.sum_na,
'fuel_consumed_mmbtu': pudl.helpers.sum_na,
'fuel_consumed_for_electricity_mmbtu': pudl.helpers.sum_na,
'net_generation_mwh': pudl.helpers.sum_na,
})
gf_df['fuel_mmbtu_per_unit'] = \
gf_df['fuel_consumed_mmbtu'] / gf_df['fuel_consumed_units']
gf_df = gf_df.reset_index()
# Bring in some generic plant & utility information:
pu_eia = pudl.output.eia860.plants_utils_eia860(pudl_engine, pt,
start_date=start_date,
end_date=end_date)
out_df = pudl.helpers.merge_on_date_year(
gf_df, pu_eia, on=['plant_id_eia'])
# Drop any records where we've failed to get the 860 data merged in...
out_df = out_df.dropna(subset=[
'plant_id_eia',
'plant_id_pudl',
'plant_name_eia',
'utility_id_eia',
'utility_id_pudl',
'utility_name_eia',
])
first_cols = ['report_date',
'plant_id_eia',
'plant_id_pudl',
'plant_name_eia',
'utility_id_eia',
'utility_id_pudl',
'utility_name_eia', ]
out_df = pudl.helpers.organize_cols(out_df, first_cols)
# Clean up the types of a few columns...
out_df['plant_id_eia'] = out_df.plant_id_eia.astype(int)
out_df['plant_id_pudl'] = out_df.plant_id_pudl.astype(int)
out_df['utility_id_eia'] = out_df.utility_id_eia.astype(int)
out_df['utility_id_pudl'] = out_df.utility_id_pudl.astype(int)
return out_df
def fuel_receipts_costs_eia923(pudl_engine, pt, freq=None,
start_date=None, end_date=None):
"""
Pull records from ``fuel_receipts_costs_eia923`` table in given date range.
Optionally, aggregate the records at a monthly or longer timescale, as well
as by fuel type within a plant, by setting freq to something other than
the default None value.
If the records are not being aggregated, then all of the fields found in
the PUDL database are available. If they are being aggregated, then the
following fields are preserved, and appropriately summed or re-calculated
based on the specified aggregation. In both cases, new total values are
calculated, for total fuel heat content and total fuel cost.
* ``plant_id_eia``
* ``report_date``
* ``fuel_type_code_pudl`` (formerly energy_source_simple)
* ``fuel_qty_units`` (sum)
* ``fuel_cost_per_mmbtu`` (weighted average)
* ``total_fuel_cost`` (sum)
* ``total_heat_content_mmbtu`` (sum)
* ``heat_content_mmbtu_per_unit`` (weighted average)
* ``sulfur_content_pct`` (weighted average)
* ``ash_content_pct`` (weighted average)
* ``moisture_content_pct`` (weighted average)
* ``mercury_content_ppm`` (weighted average)
* ``chlorine_content_ppm`` (weighted average)
In addition, plant and utility names and IDs are pulled in from the EIA
860 tables.
Args:
pudl_engine (sqlalchemy.engine.Engine): SQLAlchemy connection engine
for the PUDL DB.
pt (immutabledict): a sqlalchemy metadata dictionary of pudl tables
freq (str): a pandas timeseries offset alias. The original data is
reported monthly, so the best time frequencies to use here are
probably month start (freq='MS') and year start (freq='YS').
start_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
end_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
Returns:
pandas.DataFrame: A DataFrame containing all records from the EIA 923
Fuel Receipts and Costs table.
"""
# Most of the fields we want come direclty from Fuel Receipts & Costs
frc_tbl = pt['fuel_receipts_costs_eia923']
frc_select = sa.sql.select([frc_tbl, ])
# Need to re-integrate the MSHA coalmine info:
cmi_tbl = pt['coalmine_eia923']
cmi_select = sa.sql.select([cmi_tbl, ])
cmi_df = pd.read_sql(cmi_select, pudl_engine)
if start_date is not None:
frc_select = frc_select.where(
frc_tbl.c.report_date >= start_date)
if end_date is not None:
frc_select = frc_select.where(
frc_tbl.c.report_date <= end_date)
frc_df = pd.read_sql(frc_select, pudl_engine)
frc_df = pd.merge(frc_df, cmi_df,
how='left',
on='mine_id_pudl')
cols_to_drop = ['id', 'mine_id_pudl']
frc_df = frc_df.drop(cols_to_drop, axis=1)
# Calculate a few totals that are commonly needed:
frc_df['total_heat_content_mmbtu'] = \
frc_df['heat_content_mmbtu_per_unit'] * frc_df['fuel_qty_units']
frc_df['total_fuel_cost'] = \
frc_df['total_heat_content_mmbtu'] * frc_df['fuel_cost_per_mmbtu']
if freq is not None:
by = ['plant_id_eia', 'fuel_type_code_pudl', pd.Grouper(freq=freq)]
# Create a date index for temporal resampling:
frc_df = frc_df.set_index(pd.DatetimeIndex(frc_df.report_date))
# Sum up these values so we can calculate quantity weighted averages
frc_df['total_ash_content'] = \
frc_df['ash_content_pct'] * frc_df['fuel_qty_units']
frc_df['total_sulfur_content'] = \
frc_df['sulfur_content_pct'] * frc_df['fuel_qty_units']
frc_df['total_mercury_content'] = \
frc_df['mercury_content_ppm'] * frc_df['fuel_qty_units']
frc_df['total_moisture_content'] = \
frc_df['moisture_content_pct'] * frc_df['fuel_qty_units']
frc_df['total_chlorine_content'] = \
frc_df['chlorine_content_ppm'] * frc_df['fuel_qty_units']
frc_gb = frc_df.groupby(by=by)
frc_df = frc_gb.agg({
'fuel_qty_units': pudl.helpers.sum_na,
'total_heat_content_mmbtu': pudl.helpers.sum_na,
'total_fuel_cost': pudl.helpers.sum_na,
'total_sulfur_content': pudl.helpers.sum_na,
'total_ash_content': pudl.helpers.sum_na,
'total_mercury_content': pudl.helpers.sum_na,
'total_moisture_content': pudl.helpers.sum_na,
'total_chlorine_content': pudl.helpers.sum_na,
})
frc_df['fuel_cost_per_mmbtu'] = \
frc_df['total_fuel_cost'] / frc_df['total_heat_content_mmbtu']
frc_df['heat_content_mmbtu_per_unit'] = \
frc_df['total_heat_content_mmbtu'] / frc_df['fuel_qty_units']
frc_df['sulfur_content_pct'] = \
frc_df['total_sulfur_content'] / frc_df['fuel_qty_units']
frc_df['ash_content_pct'] = \
frc_df['total_ash_content'] / frc_df['fuel_qty_units']
frc_df['mercury_content_ppm'] = \
frc_df['total_mercury_content'] / frc_df['fuel_qty_units']
frc_df['chlorine_content_ppm'] = \
frc_df['total_chlorine_content'] / frc_df['fuel_qty_units']
frc_df['moisture_content_pct'] = \
frc_df['total_moisture_content'] / frc_df['fuel_qty_units']
frc_df = frc_df.reset_index()
frc_df = frc_df.drop(['total_ash_content',
'total_sulfur_content',
'total_moisture_content',
'total_chlorine_content',
'total_mercury_content'], axis=1)
# Bring in some generic plant & utility information:
pu_eia = pudl.output.eia860.plants_utils_eia860(pudl_engine, pt,
start_date=start_date,
end_date=end_date)
out_df = pudl.helpers.merge_on_date_year(
frc_df, pu_eia, on=['plant_id_eia'])
# Drop any records where we've failed to get the 860 data merged in...
out_df = out_df.dropna(subset=['utility_id_eia', 'utility_name_eia'])
if freq is None:
# There are a couple of invalid records with no specified fuel.
out_df = out_df.dropna(subset=['fuel_group_code'])
first_cols = ['report_date',
'plant_id_eia',
'plant_id_pudl',
'plant_name_eia',
'utility_id_eia',
'utility_id_pudl',
'utility_name_eia', ]
# Re-arrange the columns for easier readability:
out_df = pudl.helpers.organize_cols(out_df, first_cols)
# Clean up the types of a few columns...
out_df['plant_id_eia'] = out_df.plant_id_eia.astype(int)
out_df['plant_id_pudl'] = out_df.plant_id_pudl.astype(int)
out_df['utility_id_eia'] = out_df.utility_id_eia.astype(int)
out_df['utility_id_pudl'] = out_df.utility_id_pudl.astype(int)
return out_df
def boiler_fuel_eia923(pudl_engine, pt, freq=None,
start_date=None, end_date=None):
"""
Pull records from the boiler_fuel_eia923 table in a given data range.
Optionally, aggregate the records over some timescale -- monthly, yearly,
quarterly, etc. as well as by fuel type within a plant.
If the records are not being aggregated, all of the database fields are
available. If they're being aggregated, then we preserve the following
fields. Per-unit values are re-calculated based on the aggregated totals.
Totals are summed across whatever time range is being used, within a
given plant and fuel type.
* ``fuel_consumed_units`` (sum)
* ``fuel_mmbtu_per_unit`` (weighted average)
* ``total_heat_content_mmbtu`` (sum)
* ``sulfur_content_pct`` (weighted average)
* ``ash_content_pct`` (weighted average)
In addition, plant and utility names and IDs are pulled in from the EIA
860 tables.
Args:
pudl_engine (sqlalchemy.engine.Engine): SQLAlchemy connection engine
for the PUDL DB.
pt (immutabledict): a sqlalchemy metadata dictionary of pudl tables
freq (str): a pandas timeseries offset alias. The original data is
reported monthly, so the best time frequencies to use here are
probably month start (freq='MS') and year start (freq='YS').
start_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
end_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
Returns:
pandas.DataFrame: A DataFrame containing all records from the EIA 923
Boiler Fuel table.
"""
bf_eia923_tbl = pt['boiler_fuel_eia923']
bf_eia923_select = sa.sql.select([bf_eia923_tbl, ])
if start_date is not None:
bf_eia923_select = bf_eia923_select.where(
bf_eia923_tbl.c.report_date >= start_date
)
if end_date is not None:
bf_eia923_select = bf_eia923_select.where(
bf_eia923_tbl.c.report_date <= end_date
)
bf_df = pd.read_sql(bf_eia923_select, pudl_engine)
# The total heat content is also useful in its own right, and we'll keep it
# around. Also needed to calculate average heat content per unit of fuel.
bf_df['total_heat_content_mmbtu'] = bf_df['fuel_consumed_units'] * \
bf_df['fuel_mmbtu_per_unit']
# Create a date index for grouping based on freq
by = ['plant_id_eia', 'boiler_id', 'fuel_type_code_pudl']
if freq is not None:
# In order to calculate the weighted average sulfur
# content and ash content we need to calculate these totals.
bf_df['total_sulfur_content'] = bf_df['fuel_consumed_units'] * \
bf_df['sulfur_content_pct']
bf_df['total_ash_content'] = bf_df['fuel_consumed_units'] * \
bf_df['ash_content_pct']
bf_df = bf_df.set_index(pd.DatetimeIndex(bf_df.report_date))
by = by + [pd.Grouper(freq=freq)]
bf_gb = bf_df.groupby(by=by)
# Sum up these totals within each group, and recalculate the per-unit
# values (weighted in this case by fuel_consumed_units)
bf_df = bf_gb.agg({
'total_heat_content_mmbtu': pudl.helpers.sum_na,
'fuel_consumed_units': pudl.helpers.sum_na,
'total_sulfur_content': pudl.helpers.sum_na,
'total_ash_content': pudl.helpers.sum_na,
})
bf_df['fuel_mmbtu_per_unit'] = \
bf_df['total_heat_content_mmbtu'] / bf_df['fuel_consumed_units']
bf_df['sulfur_content_pct'] = \
bf_df['total_sulfur_content'] / bf_df['fuel_consumed_units']
bf_df['ash_content_pct'] = \
bf_df['total_ash_content'] / bf_df['fuel_consumed_units']
bf_df = bf_df.reset_index()
bf_df = bf_df.drop(['total_ash_content', 'total_sulfur_content'],
axis=1)
# Grab some basic plant & utility information to add.
pu_eia = pudl.output.eia860.plants_utils_eia860(pudl_engine, pt,
start_date=start_date,
end_date=end_date)
out_df = pudl.helpers.merge_on_date_year(
bf_df, pu_eia, on=['plant_id_eia'])
if freq is None:
out_df = out_df.drop(['id'], axis=1)
out_df = out_df.dropna(subset=[
'plant_id_eia',
'plant_id_pudl',
'utility_id_eia',
'utility_id_pudl',
'boiler_id',
])
first_cols = [
'report_date',
'plant_id_eia',
'plant_id_pudl',
'plant_name_eia',
'utility_id_eia',
'utility_id_pudl',
'utility_name_eia',
'boiler_id',
]
# Re-arrange the columns for easier readability:
out_df = pudl.helpers.organize_cols(out_df, first_cols)
out_df['utility_id_eia'] = out_df.utility_id_eia.astype(int)
out_df['utility_id_pudl'] = out_df.utility_id_pudl.astype(int)
out_df['plant_id_pudl'] = out_df.plant_id_pudl.astype(int)
return out_df
def generation_eia923(pudl_engine, pt, freq=None,
start_date=None, end_date=None):
"""
Pull records from the boiler_fuel_eia923 table in a given data range.
Args:
pudl_engine (sqlalchemy.engine.Engine): SQLAlchemy connection engine
for the PUDL DB.
pt (immutabledict): a sqlalchemy metadata dictionary of pudl tables
freq (str): a pandas timeseries offset alias. The original data is
reported monthly, so the best time frequencies to use here are
probably month start (freq='MS') and year start (freq='YS').
start_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
end_date (date-like): date-like object, including a string of the
form 'YYYY-MM-DD' which will be used to specify the date range of
records to be pulled. Dates are inclusive.
Returns:
pandas.DataFrame: A DataFrame containing all records from the EIA 923
Generation table.
"""
g_eia923_tbl = pt['generation_eia923']
g_eia923_select = sa.sql.select([g_eia923_tbl, ])
if start_date is not None:
g_eia923_select = g_eia923_select.where(
g_eia923_tbl.c.report_date >= start_date
)
if end_date is not None:
g_eia923_select = g_eia923_select.where(
g_eia923_tbl.c.report_date <= end_date
)
g_df = pd.read_sql(g_eia923_select, pudl_engine)
# Index by date and aggregate net generation.
# Create a date index for grouping based on freq
by = ['plant_id_eia', 'generator_id']
if freq is not None:
g_df = g_df.set_index(pd.DatetimeIndex(g_df.report_date))
by = by + [pd.Grouper(freq=freq)]
g_gb = g_df.groupby(by=by)
g_df = g_gb.agg(
{'net_generation_mwh': pudl.helpers.sum_na}).reset_index()
# Grab EIA 860 plant and utility specific information:
pu_eia = pudl.output.eia860.plants_utils_eia860(pudl_engine, pt,
start_date=start_date,
end_date=end_date)
# Merge annual plant/utility data in with the more granular dataframe
out_df = pudl.helpers.merge_on_date_year(g_df, pu_eia, on=['plant_id_eia'])
if freq is None:
out_df = out_df.drop(['id'], axis=1)
# These ID fields are vital -- without them we don't have a complete record
out_df = out_df.dropna(subset=[
'plant_id_eia',
'plant_id_pudl',
'utility_id_eia',
'utility_id_pudl',
'generator_id',
])
first_cols = [
'report_date',
'plant_id_eia',
'plant_id_pudl',
'plant_name_eia',
'utility_id_eia',
'utility_id_pudl',
'utility_name_eia',
'generator_id',
]
# Re-arrange the columns for easier readability:
out_df = pudl.helpers.organize_cols(out_df, first_cols)
out_df['utility_id_eia'] = out_df.utility_id_eia.astype(int)
out_df['utility_id_pudl'] = out_df.utility_id_pudl.astype(int)
out_df['plant_id_pudl'] = out_df.plant_id_pudl.astype(int)
return out_df