pudl.extract.ferc1

Tools for extracting data from the FERC Form 1 FoxPro database for use in PUDL.

FERC distributes the annual responses to Form 1 as binary FoxPro database files. This format is no longer widely supported, and so our first challenge in accessing the Form 1 data is to convert it into a modern format. In addition, FERC distributes one database for each year, and these databases are not explicitly linked together. Over time the structure has changed as new tables and fields have been added. In order to be able to use the data to do analyses across many years, we need to bring all of it into a unified structure. However it appears that these changes are only entirely additive – the most recent versions of the DB contain all the tables and fields that existed in earlier versions.

PUDL uses the most recently released year of data as a template, and infers the structure of the FERC Form 1 database based on the strings embedded within the binary files, pulling out the names of tables and their constituent columns. The structure of the database is also informed by information we found on the FERC website, including a mapping between the table names, DBF file names, and the pages of the Form 1 (add link to file, which should distributed with the docs) that the data was gathered from, as well as a diagram of the structure of the database as it existed in 2015 (add link/embed image).

Using this inferred structure PUDL creates an SQLite database mirroring the FERC database using sqlalchemy. Then we use a python package called dbfread to extract the data from the DBF tables, and insert it virtually unchanged into the SQLite database. However, we do compile a master table of the all the respondent IDs and respondent names, which all the other tables refer to. Unlike the other tables, this table has no report_year and so it represents a merge of all the years of data. In the event that the name associated with a given respondent ID has changed over time, we retain the most recently reported name.

Ths SQLite based compilation of the original FERC Form 1 databases can accommodate all 116 tables from all the published years of data (beginning in 1994). Including all the data through 2018, the database takes up more than 7GB of disk space. However, almost 90% of that “data” is embeded binary files in two tables. If those tables are excluded, the database is less than 800MB in size.

The process of cloning the FERC Form 1 database(s) is coordinated by a script called ferc1_to_sqlite implemented in pudl.convert.ferc1_to_sqlite which is controlled by a YAML file. See the example file distributed with the package.

Once the cloned SQLite database has been created, we use it as an input into the PUDL ETL pipeline, and we extract a small subset of the available tables for further processing and integration with other data sources like the EIA 860 and EIA 923.

Module Contents

Classes

Ferc1Datastore

Simple datastore wrapper for accessing ferc1 resources.

FERC1FieldParser

A custom DBF parser to deal with bad FERC Form 1 data types.

Functions

missing_respondents(reported, observed, identified)

Fill in missing respondents for the f1_respondent_id table.

observed_respondents(ferc1_engine: sqlalchemy.engine.Engine) → Set[int]

Compile the set of all observed respondent IDs found in the FERC 1 database.

drop_tables(engine)

Drop all FERC Form 1 tables from the SQLite database.

add_sqlite_table(table_name, sqlite_meta, dbc_map, ds, refyear=max(DataSource.from_id('ferc1').working_partitions['years']), bad_cols=())

Adds a new Table to the FERC Form 1 database schema.

get_fields(filedata)

Produce the expected table names and fields from a DBC file.

get_dbc_map(ds, year, min_length=4)

Extract names of all tables and fields from a FERC Form 1 DBC file.

define_sqlite_db(sqlite_engine, sqlite_meta, dbc_map, ds, ferc1_to_sqlite_settings: pudl.settings.Ferc1ToSqliteSettings = Ferc1ToSqliteSettings())

Defines a FERC Form 1 DB structure in a given SQLAlchemy MetaData object.

get_raw_df(ds, table, dbc_map, years=DataSource.from_id('ferc1').working_partitions['years'])

Combine several years of a given FERC Form 1 DBF table into a dataframe.

dbf2sqlite(ferc1_to_sqlite_settings: pudl.settings.Ferc1ToSqliteSettings = Ferc1ToSqliteSettings(), pudl_settings=None, clobber=False, datastore=None)

Clone the FERC Form 1 Databsae to SQLite.

get_ferc1_meta(ferc1_engine)

Grab the FERC Form 1 DB metadata and check that tables exist.

extract(ferc1_settings: pudl.settings.Ferc1Settings = Ferc1Settings(), pudl_settings=None)

Coordinates the extraction of all FERC Form 1 tables into PUDL.

fuel(ferc1_engine, ferc1_settings)

Creates a DataFrame of f1_fuel table records with plant names, >0 fuel.

plants_steam(ferc1_engine, ferc1_settings)

Create a pandas.DataFrame containing valid raw f1_steam records.

plants_small(ferc1_engine, ferc1_settings)

Creates a DataFrame of f1_small for records with minimum data criteria.

plants_hydro(ferc1_engine, ferc1_settings)

Creates a DataFrame of f1_hydro for records that have plant names.

plants_pumped_storage(ferc1_engine, ferc1_settings)

Creates a DataFrame of f1_plants_pumped_storage records with plant names.

plant_in_service(ferc1_engine, ferc1_settings)

Creates a DataFrame of the fields of plant_in_service_ferc1.

purchased_power(ferc1_engine, ferc1_settings)

Creates a DataFrame the fields of purchased_power_ferc1.

accumulated_depreciation(ferc1_engine, ferc1_settings)

Creates a DataFrame of the fields of accumulated_depreciation_ferc1.

Attributes

logger

DBF_TYPES

A mapping of DBF field types to SQLAlchemy Column types.

PUDL_RIDS

Missing FERC 1 Respondent IDs for which we have identified the respondent.

pudl.extract.ferc1.logger[source]
pudl.extract.ferc1.DBF_TYPES[source]

A mapping of DBF field types to SQLAlchemy Column types.

This dictionary maps the strings which are used to denote field types in the DBF objects to the corresponding generic SQLAlchemy Column types: These definitions come from a combination of the dbfread example program dbf2sqlite and this DBF file format documentation page: http://www.dbase.com/KnowledgeBase/int/db7_file_fmt.htm Un-mapped types left as ‘XXX’ which should obviously make an error.

Type

dict

pudl.extract.ferc1.PUDL_RIDS[source]

Missing FERC 1 Respondent IDs for which we have identified the respondent.

pudl.extract.ferc1.missing_respondents(reported, observed, identified)[source]

Fill in missing respondents for the f1_respondent_id table.

Parameters
  • reported (iterable) – Respondent IDs appearing in f1_respondent_id.

  • observed (iterable) – Respondent IDs appearing anywhere in the ferc1 DB.

  • identified (dict) – A {respondent_id: respondent_name} mapping for those observed but not reported respondent IDs which we have been able to identify based on circumstantial evidence. See also: pudl.extract.ferc1.PUDL_RIDS

Returns

A list of dictionaries representing minimal f1_respondent_id table records, of the form {“respondent_id”: ID, “respondent_name”: NAME}. These records are generated only for unreported respondents. Identified respondents get the values passed in through identified and the other observed but unidentified respondents are named “Missing Respondent ID”

Return type

list

pudl.extract.ferc1.observed_respondents(ferc1_engine: sqlalchemy.engine.Engine) Set[int][source]

Compile the set of all observed respondent IDs found in the FERC 1 database.

A significant number of FERC 1 respondent IDs appear in the data tables, but not in the f1_respondent_id table. In order to construct a self-consistent database with we need to find all of those missing respondent IDs and inject them into the table when we clone the database.

Parameters

ferc1_engine – An engine for connecting to the FERC 1 database.

Returns

Every respondent ID reported in any of the FERC 1 DB tables.

class pudl.extract.ferc1.Ferc1Datastore(datastore: pudl.workspace.datastore.Datastore)[source]

Simple datastore wrapper for accessing ferc1 resources.

PACKAGE_PATH = pudl.package_data.ferc1[source]
get_dir(self, year: int) pathlib.Path[source]

Returns the path where individual ferc1 files are stored inside the yearly archive.

get_file(self, year: int, filename: str)[source]

Opens given ferc1 file from the corresponding archive.

pudl.extract.ferc1.drop_tables(engine)[source]

Drop all FERC Form 1 tables from the SQLite database.

Creates an sa.schema.MetaData object reflecting the structure of the database that the passed in engine refers to, and uses that schema to drop all existing tables.

Todo

Treat DB connection as a context manager (with/as).

Parameters

engine (sqlalchemy.engine.Engine) – A DB Engine pointing at an exising SQLite database to be deleted.

Returns

None

pudl.extract.ferc1.add_sqlite_table(table_name, sqlite_meta, dbc_map, ds, refyear=max(DataSource.from_id('ferc1').working_partitions['years']), bad_cols=())[source]

Adds a new Table to the FERC Form 1 database schema.

Creates a new sa.Table object named table_name and add it to the database schema contained in sqlite_meta. Use the information in the dictionary dbc_map to translate between the DBF filenames in the datastore (e.g. F1_31.DBF), and the full name of the table in the FoxPro database (e.g. f1_fuel) and also between truncated column names extracted from that DBF file, and the full column names extracted from the DBC file. Read the column datatypes out of each DBF file and use them to define the columns in the new Table object.

Parameters
  • table_name (str) – The name of the new table to be added to the database schema.

  • sqlite_meta (sqlalchemy.schema.MetaData) – The database schema to which the newly defined sqlalchemy.Table will be added.

  • dbc_map (dict) – A dictionary of dictionaries

  • ds (Ferc1Datastore) – Initialized datastore

  • bad_cols (iterable of 2-tuples) – A list or other iterable containing pairs of strings of the form (table_name, column_name), indicating columns (and their parent tables) which should not be cloned into the SQLite database for some reason.

Returns

None

pudl.extract.ferc1.get_fields(filedata)[source]

Produce the expected table names and fields from a DBC file.

Parameters

filedata – Contents of the DBC file from which to extract.

Returns

[fields]

Return type

dict of table_name

pudl.extract.ferc1.get_dbc_map(ds, year, min_length=4)[source]

Extract names of all tables and fields from a FERC Form 1 DBC file.

Read the DBC file associated with the FERC Form 1 database for the given year, and extract all printable strings longer than min_lengh. Select those strings that appear to be database table names, and their associated field for use in re-naming the truncated column names extracted from the corresponding DBF files (those names are limited to having only 10 characters in their names.)

Parameters
  • ds (Ferc1Datastore) – Initialized datastore

  • year – The year of data from which the database table and column names are to be extracted. Typically this is expected to be the most recently available year of FERC Form 1 data.

Returns

a dictionary whose keys are the long table names extracted from the DBC file, and whose values are lists of pairs of values, the first of which is the full name of each field in the table with the same name as the key, and the second of which is the truncated (<=10 character) long name of that field as found in the DBF file.

Return type

dict

pudl.extract.ferc1.define_sqlite_db(sqlite_engine, sqlite_meta, dbc_map, ds, ferc1_to_sqlite_settings: pudl.settings.Ferc1ToSqliteSettings = Ferc1ToSqliteSettings())[source]

Defines a FERC Form 1 DB structure in a given SQLAlchemy MetaData object.

Given a template from an existing year of FERC data, and a list of target tables to be cloned, convert that information into table and column names, and data types, stored within a SQLAlchemy MetaData object. Use that MetaData object (which is bound to the SQLite database) to create all the tables to be populated later.

Parameters
  • sqlite_meta (sa.MetaData) – A SQLAlchemy MetaData object which is bound to the FERC Form 1 SQLite database.

  • dbc_map (dict of dicts) – A dictionary of dictionaries, of the kind returned by get_dbc_map(), describing the table and column names stored within the FERC Form 1 FoxPro database files.

  • ds (Ferc1Datastore) – Initialized Ferc1Datastore

  • ferc1_to_sqlite_settings – Object containing Ferc1 to SQLite validated settings.

Returns

the effects of the function are stored inside sqlite_meta

Return type

None

class pudl.extract.ferc1.FERC1FieldParser(table, memofile=None)[source]

Bases: dbfread.FieldParser

A custom DBF parser to deal with bad FERC Form 1 data types.

parseN(self, field, data)[source]

Augments the Numeric DBF parser to account for bad FERC data.

There are a small number of bad entries in the backlog of FERC Form 1 data. They take the form of leading/trailing zeroes or null characters in supposedly numeric fields, and occasionally a naked ‘.’

Accordingly, this custom parser strips leading and trailing zeros and null characters, and replaces a bare ‘.’ character with zero, allowing all these fields to be cast to numeric values.

Parameters
  • () (data) –

  • ()

  • ()

pudl.extract.ferc1.get_raw_df(ds, table, dbc_map, years=DataSource.from_id('ferc1').working_partitions['years'])[source]

Combine several years of a given FERC Form 1 DBF table into a dataframe.

Parameters
  • ds (Ferc1Datastore) – Initialized datastore

  • table (string) – The name of the FERC Form 1 table from which data is read.

  • dbc_map (dict of dicts) – A dictionary of dictionaries, of the kind returned by get_dbc_map(), describing the table and column names stored within the FERC Form 1 FoxPro database files.

  • min_length (int) – The minimum number of consecutive printable

  • years (list) – Range of years to be combined into a single DataFrame.

Returns

A DataFrame containing several years of FERC Form 1 data for the given table.

Return type

pandas.DataFrame

pudl.extract.ferc1.dbf2sqlite(ferc1_to_sqlite_settings: pudl.settings.Ferc1ToSqliteSettings = Ferc1ToSqliteSettings(), pudl_settings=None, clobber=False, datastore=None)[source]

Clone the FERC Form 1 Databsae to SQLite.

Parameters
  • ferc1_to_sqlite_settings – Object containing Ferc1 to SQLite validated settings.

  • pudl_settings (dict) – Dictionary containing paths and database URLs used by PUDL.

  • bad_cols (iterable of tuples) – A list of (table, column) pairs indicating columns that should be skipped during the cloning process. Both table and column are strings in this case, the names of their respective entities within the database metadata.

  • datastore (Datastore) – instance of a datastore to access the resources.

Returns

None

pudl.extract.ferc1.get_ferc1_meta(ferc1_engine)[source]

Grab the FERC Form 1 DB metadata and check that tables exist.

Connects to the FERC Form 1 SQLite database and reads in its metadata (table schemas, types, etc.) by reflecting the database. Checks to make sure the DB is not empty, and returns the metadata object.

Parameters

ferc1_engine (sqlalchemy.engine.Engine) – SQL Alchemy database connection engine for the PUDL FERC 1 DB.

Returns

sqlalchemy.MetaData A SQL Alchemy metadata object, containing the definition of the DB structure.

Raises

ValueError – If there are no tables in the SQLite Database.

pudl.extract.ferc1.extract(ferc1_settings: pudl.settings.Ferc1Settings = Ferc1Settings(), pudl_settings=None)[source]

Coordinates the extraction of all FERC Form 1 tables into PUDL.

Parameters

ferc1_settings – Object containing validated settings relevant to FERC Form 1. Contains the tables and years to be loaded into PUDL.

Returns

A dictionary of pandas DataFrames, with the names of PUDL database tables as the keys. These are the raw unprocessed dataframes, reflecting the data as it is in the FERC Form 1 DB, for passing off to the data tidying and cleaning fuctions found in the pudl.transform.ferc1 module.

Return type

dict

Raises
  • ValueError – If the year is not in the list of years for which FERC data is available

  • ValueError – If the year is not in the list of working FERC years

  • ValueError – If the FERC table requested is not integrated into PUDL

pudl.extract.ferc1.fuel(ferc1_engine, ferc1_settings)[source]

Creates a DataFrame of f1_fuel table records with plant names, >0 fuel.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing f1_fuel records that have plant_names and non-zero fuel amounts.

Return type

pandas.DataFrame

pudl.extract.ferc1.plants_steam(ferc1_engine, ferc1_settings)[source]

Create a pandas.DataFrame containing valid raw f1_steam records.

Selected records must indicate a plant capacity greater than 0, and include a non-null plant name.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing f1_steam records that have plant names and non-zero capacities.

Return type

pandas.DataFrame

pudl.extract.ferc1.plants_small(ferc1_engine, ferc1_settings)[source]

Creates a DataFrame of f1_small for records with minimum data criteria.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing f1_small records that have plant names and non zero demand, generation, operations, maintenance, and fuel costs.

Return type

pandas.DataFrame

pudl.extract.ferc1.plants_hydro(ferc1_engine, ferc1_settings)[source]

Creates a DataFrame of f1_hydro for records that have plant names.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing f1_hydro records that have plant names.

Return type

pandas.DataFrame

pudl.extract.ferc1.plants_pumped_storage(ferc1_engine, ferc1_settings)[source]

Creates a DataFrame of f1_plants_pumped_storage records with plant names.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing f1_plants_pumped_storage records that have plant names.

Return type

pandas.DataFrame

pudl.extract.ferc1.plant_in_service(ferc1_engine, ferc1_settings)[source]

Creates a DataFrame of the fields of plant_in_service_ferc1.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing all plant_in_service_ferc1 records.

Return type

pandas.DataFrame

pudl.extract.ferc1.purchased_power(ferc1_engine, ferc1_settings)[source]

Creates a DataFrame the fields of purchased_power_ferc1.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing all purchased_power_ferc1 records.

Return type

pandas.DataFrame

pudl.extract.ferc1.accumulated_depreciation(ferc1_engine, ferc1_settings)[source]

Creates a DataFrame of the fields of accumulated_depreciation_ferc1.

Parameters
  • ferc1_engine (sqlalchemy.engine.Engine) – An SQL Alchemy connection engine for the FERC Form 1 database.

  • ferc1_settings (Ferc1Settings) – Object containing validated settings relevant to FERC Form 1.

Returns

A DataFrame containing all accumulated_depreciation_ferc1 records.

Return type

pandas.DataFrame