|
| 1 | +# Copyright 2016-2024 Blue Marble Analytics LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +EIA AEO Fuel Prices |
| 17 | +******************* |
| 18 | +
|
| 19 | +Create GridPath fuel price inputs (fuel_scenario_id) based on the EIA AEO. |
| 20 | +
|
| 21 | +.. warning:: The user is reponsible for ensuring that all prices and costs in |
| 22 | + their model are in a consistent real currency year. |
| 23 | +
|
| 24 | +===== |
| 25 | +Usage |
| 26 | +===== |
| 27 | +
|
| 28 | +>>> gridpath_run_data_toolkit --single_step eiaaeo_fuel_price_input_csvs --settings_csv PATH/TO/SETTINGS/CSV |
| 29 | +
|
| 30 | +=================== |
| 31 | +Input prerequisites |
| 32 | +=================== |
| 33 | +
|
| 34 | +Thios module assumes the following raw input database tables have been |
| 35 | +populated: |
| 36 | + * raw_data_eiaaeo_fuel_prices |
| 37 | + * user_defined_eiaaeo_region_key |
| 38 | +
|
| 39 | +========= |
| 40 | +Settings |
| 41 | +========= |
| 42 | + * database |
| 43 | + * output_directory |
| 44 | + * model_case |
| 45 | + * report_year |
| 46 | + * fuel_price_id |
| 47 | +
|
| 48 | +""" |
| 49 | + |
| 50 | +import csv |
| 51 | +from argparse import ArgumentParser |
| 52 | +import os.path |
| 53 | +import pandas as pd |
| 54 | +import sys |
| 55 | + |
| 56 | +from db.common_functions import connect_to_database |
| 57 | + |
| 58 | + |
| 59 | +def parse_arguments(args): |
| 60 | + """ |
| 61 | + :param args: the script arguments specified by the user |
| 62 | + :return: the parsed known argument values (<class 'argparse.Namespace'> |
| 63 | + Python object) |
| 64 | +
|
| 65 | + Parse the known arguments. |
| 66 | + """ |
| 67 | + parser = ArgumentParser(add_help=True) |
| 68 | + |
| 69 | + parser.add_argument("-db", "--database", default="../../db/open_data_raw.db") |
| 70 | + |
| 71 | + parser.add_argument( |
| 72 | + "-o", |
| 73 | + "--output_directory", |
| 74 | + default="../../db/csvs_open_data/fuels/fuel_prices", |
| 75 | + ) |
| 76 | + parser.add_argument("-fuel_price_id", "--fuel_price_scenario_id", default=1) |
| 77 | + parser.add_argument( |
| 78 | + "-case", |
| 79 | + "--model_case", |
| 80 | + default="aeo2022", |
| 81 | + ) |
| 82 | + parser.add_argument("-r_yr", "--report_year", default=2023) |
| 83 | + |
| 84 | + parser.add_argument("-q", "--quiet", default=False, action="store_true") |
| 85 | + |
| 86 | + parsed_arguments = parser.parse_known_args(args=args)[0] |
| 87 | + |
| 88 | + return parsed_arguments |
| 89 | + |
| 90 | + |
| 91 | +def get_fuel_prices( |
| 92 | + conn, output_directory, subscenario_id, subscenario_name, report_year, model_case |
| 93 | +): |
| 94 | + """ """ |
| 95 | + |
| 96 | + sql = f""" |
| 97 | + SELECT gridpath_generic_fuel || '_' || fuel_region as fuel, projection_year as period, |
| 98 | + fuel_cost_real_per_mmbtu_eiaaeo as fuel_price_per_mmbtu |
| 99 | + FROM raw_data_eiaaeo_fuel_prices |
| 100 | + JOIN (SELECT DISTINCT gridpath_generic_fuel, fuel_type_eiaaeo FROM user_defined_eia_gridpath_key) USING (fuel_type_eiaaeo) |
| 101 | + JOIN user_defined_eiaaeo_region_key using ( |
| 102 | + electricity_market_module_region_eiaaeo) |
| 103 | + WHERE report_year = {report_year} |
| 104 | + AND model_case_eiaaeo = '{model_case}' |
| 105 | + ORDER BY fuel, period |
| 106 | + """ |
| 107 | + |
| 108 | + df = pd.read_sql(sql, conn) |
| 109 | + month_df_list = [] |
| 110 | + for month in range(1, 13): |
| 111 | + month_df = df |
| 112 | + month_df["month"] = month |
| 113 | + cols = month_df.columns.tolist() |
| 114 | + cols = cols[:2] + [cols[3]] + [cols[2]] |
| 115 | + month_df = month_df[cols] |
| 116 | + |
| 117 | + month_df_list.append(month_df) |
| 118 | + |
| 119 | + final_df = pd.concat(month_df_list) |
| 120 | + |
| 121 | + final_df.to_csv( |
| 122 | + os.path.join(output_directory, f"{subscenario_id}_" f"{subscenario_name}.csv"), |
| 123 | + index=False, |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +def main(args=None): |
| 128 | + if args is None: |
| 129 | + args = sys.argv[1:] |
| 130 | + |
| 131 | + parsed_args = parse_arguments(args=args) |
| 132 | + |
| 133 | + if not parsed_args.quiet: |
| 134 | + print("Creating fuel prices...") |
| 135 | + |
| 136 | + os.makedirs(parsed_args.output_directory, exist_ok=True) |
| 137 | + |
| 138 | + conn = connect_to_database(db_path=parsed_args.database) |
| 139 | + |
| 140 | + get_fuel_prices( |
| 141 | + conn=conn, |
| 142 | + output_directory=parsed_args.output_directory, |
| 143 | + subscenario_id=parsed_args.fuel_price_scenario_id, |
| 144 | + subscenario_name=parsed_args.model_case, |
| 145 | + report_year=parsed_args.report_year, |
| 146 | + model_case=parsed_args.model_case, |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments