-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update data export notebook and small bug fixes #391
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# import packages\n", | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"import os\n", | ||
"\n", | ||
"%reload_ext autoreload\n", | ||
"%autoreload 2\n", | ||
"\n", | ||
"# # Tell python where to look for modules.\n", | ||
"import sys\n", | ||
"\n", | ||
"sys.path.append(\"../../../open-grid-emissions/src/oge\")\n", | ||
"\n", | ||
"\n", | ||
"import load_data\n", | ||
"import helpers\n", | ||
"from filepaths import *\n", | ||
"\n", | ||
"year = 2023\n", | ||
"path_prefix = f\"{year}/\"\n", | ||
"\n", | ||
"os.environ[\"PUDL_BUILD\"] = \"nightly\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# load data for each generator\n", | ||
"gens = load_data.load_pudl_table(\n", | ||
" \"core_eia860__scd_generators\",\n", | ||
" year,\n", | ||
" columns=[\n", | ||
" \"report_date\",\n", | ||
" \"plant_id_eia\",\n", | ||
" \"generator_id\",\n", | ||
" \"prime_mover_code\",\n", | ||
" \"capacity_mw\",\n", | ||
" \"generator_retirement_date\",\n", | ||
" \"operational_status\",\n", | ||
" \"operational_status_code\",\n", | ||
" \"current_planned_generator_operating_date\",\n", | ||
" \"planned_generator_retirement_date\",\n", | ||
" ],\n", | ||
")\n", | ||
"\n", | ||
"gens_entity = load_data.load_pudl_table(\n", | ||
" \"core_eia__entity_generators\",\n", | ||
" columns=[\n", | ||
" \"plant_id_eia\",\n", | ||
" \"generator_id\",\n", | ||
" \"generator_operating_date\",\n", | ||
" \"original_planned_generator_operating_date\",\n", | ||
" ],\n", | ||
")\n", | ||
"\n", | ||
"plants_entity = load_data.load_pudl_table(\n", | ||
" \"core_eia__entity_plants\",\n", | ||
" columns=[\"plant_id_eia\", \"latitude\", \"longitude\", \"city\", \"county\", \"state\"],\n", | ||
")\n", | ||
"\n", | ||
"plant_ba = load_data.load_pudl_table(\n", | ||
" \"core_eia860__scd_plants\",\n", | ||
" year,\n", | ||
" columns=[\"plant_id_eia\", \"balancing_authority_code_eia\"],\n", | ||
").rename(columns={\"balancing_authority_code_eia\": \"ba_code\"})\n", | ||
"\n", | ||
"# merge the ba code into the gens data\n", | ||
"gens = (\n", | ||
" gens.merge(\n", | ||
" gens_entity, how=\"left\", on=[\"plant_id_eia\", \"generator_id\"], validate=\"m:1\"\n", | ||
" )\n", | ||
" .merge(plant_ba, how=\"left\", on=\"plant_id_eia\", validate=\"m:1\")\n", | ||
" .merge(plants_entity, how=\"left\", on=\"plant_id_eia\", validate=\"m:1\")\n", | ||
")\n", | ||
"\n", | ||
"# drop data for generators outside of continental US\n", | ||
"gens = gens[~gens[\"state\"].isin([\"AK\", \"HI\"])]\n", | ||
"\n", | ||
"# if there are longitudes in the wrong hemisphere, try reversing the sign\n", | ||
"gens.loc[gens[\"longitude\"] > 0, \"longitude\"] = (\n", | ||
" -1 * gens.loc[gens[\"longitude\"] > 0, \"longitude\"]\n", | ||
")\n", | ||
"\n", | ||
"# fix coordinates\n", | ||
"# NOTE: This is the bounding box for the continental US and the southern canadian provinces\n", | ||
"LONGITUDE_MIN = -125.25\n", | ||
"LONGITUDE_MAX = -67\n", | ||
"LATITUDE_MIN = 24.5\n", | ||
"LATITUDE_MAX = 49.25\n", | ||
"\n", | ||
"# are there any old station locations that are outside of North America?\n", | ||
"gens.loc[\n", | ||
" (gens[\"longitude\"] < LONGITUDE_MIN)\n", | ||
" | (gens[\"longitude\"] > LONGITUDE_MAX)\n", | ||
" | (gens[\"latitude\"] < LATITUDE_MIN)\n", | ||
" | (gens[\"latitude\"] > LATITUDE_MAX),\n", | ||
" [\"latitude\", \"longitude\"],\n", | ||
"] = np.NaN\n", | ||
"\n", | ||
"# fill in missing locations\n", | ||
"# get lat/lon\n", | ||
"# loop this process twice to try and address any geocoder errors\n", | ||
"loop_count = 1\n", | ||
"missing_coord = gens[gens[\"longitude\"].isna() | gens[\"latitude\"].isna()]\n", | ||
"while loop_count <= 2 and len(missing_coord) > 0:\n", | ||
" if len(missing_coord) > 0:\n", | ||
" print(f\"Finding coordinates for {len(missing_coord)} missing locations\")\n", | ||
" # only get coordinates when state, county and city are available\n", | ||
" for i in missing_coord.index:\n", | ||
" state = gens.loc[i, \"state\"]\n", | ||
" county = gens.loc[i, \"county\"]\n", | ||
" city = gens.loc[i, \"city\"]\n", | ||
"\n", | ||
" lat, lon = helpers.get_coordinates_of_location(state, county, city)\n", | ||
" gens.loc[i, \"latitude\"] = lat\n", | ||
" gens.loc[i, \"longitude\"] = lon\n", | ||
" missing_coord = gens[gens[\"longitude\"].isna() | gens[\"latitude\"].isna()]\n", | ||
" loop_count += 1\n", | ||
"\n", | ||
"# load wind data\n", | ||
"wind = pd.read_excel(\n", | ||
" downloads_folder(f\"eia860/eia860{year}/3_2_Wind_Y{year}.xlsx\"),\n", | ||
" sheet_name=\"Operable\",\n", | ||
" header=1,\n", | ||
").rename(\n", | ||
" columns={\n", | ||
" \"Plant Code\": \"plant_id_eia\",\n", | ||
" \"Generator ID\": \"generator_id\",\n", | ||
" \"Design Wind Speed (mph)\": \"rated_speed_mph\",\n", | ||
" \"Turbine Hub Height (Feet)\": \"hub_height_ft\",\n", | ||
" }\n", | ||
")\n", | ||
"\n", | ||
"# convert to metric units\n", | ||
"wind[\"rated_speed_m_per_s\"] = (wind[\"rated_speed_mph\"] * 0.44704).round(1)\n", | ||
"wind[\"hub_height_m\"] = (wind[\"hub_height_ft\"] * 0.3048).round(0)\n", | ||
"\n", | ||
"wind = wind[[\"plant_id_eia\", \"generator_id\", \"rated_speed_m_per_s\", \"hub_height_m\"]]\n", | ||
"\n", | ||
"# merge the wind data into the gens data\n", | ||
"gens = gens.merge(wind, how=\"left\", on=[\"plant_id_eia\", \"generator_id\"])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gens.to_csv(outputs_folder(f\"gens_{year}.csv\"), index=False)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "open-grid-emissions-QkuIZ37I", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.4" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing that.