|
| 1 | +""" |
| 2 | +#################### |
| 3 | +Create a low hydro scenario |
| 4 | +
|
| 5 | +Date applied: 2021-07-29 |
| 6 | +Description: |
| 7 | +This script adds a scenario to the database for low hydro power. |
| 8 | +The worst year for hydro is 2015. As such we use those values for every year unless a plant is missing |
| 9 | +in 2015 in which case we use the lowest value in the other years for that plant. |
| 10 | +################# |
| 11 | +""" |
| 12 | +import time |
| 13 | + |
| 14 | +from switch_model.utilities import query_yes_no, format_seconds |
| 15 | +from switch_model.wecc.utilities import connect |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | +raw_data_scenario = 21 |
| 19 | +all_plants_scenario = 23 |
| 20 | +worst_year = 2015 |
| 21 | + |
| 22 | +new_start_year = 2020 |
| 23 | +new_end_year = 2050 |
| 24 | +new_scenario_id = 24 |
| 25 | +new_scenario_name = "Lowest year (2015) repeated. Using EIA and AMPL Canada and Mex data." |
| 26 | +new_scenario_description = "Lowest year (2015) repeated from 2020 to 2050, based on data from id 21 (EIA + AMPL Canada & Mex)." |
| 27 | + |
| 28 | + |
| 29 | +def main(): |
| 30 | + db_conn = connect() |
| 31 | + db_cursor = db_conn.cursor() |
| 32 | + |
| 33 | + # 1. Get all the hydro plants |
| 34 | + db_cursor.execute( |
| 35 | + f""" |
| 36 | + SELECT DISTINCT generation_plant_id FROM hydro_historical_monthly_capacity_factors |
| 37 | + WHERE hydro_simple_scenario_id={all_plants_scenario}; |
| 38 | + """) |
| 39 | + hydro_plants = pd.DataFrame(db_cursor.fetchall(), columns=["generation_plant_id"])["generation_plant_id"] |
| 40 | + |
| 41 | + # 2. Get all the hydro flow data for the worst year |
| 42 | + db_cursor.execute( |
| 43 | + f""" |
| 44 | + SELECT generation_plant_id, month, hydro_min_flow_mw, hydro_avg_flow_mw FROM hydro_historical_monthly_capacity_factors |
| 45 | + WHERE hydro_simple_scenario_id={raw_data_scenario} and year={worst_year}; |
| 46 | + """) |
| 47 | + worst_year_data = pd.DataFrame(db_cursor.fetchall(), |
| 48 | + columns=["generation_plant_id", "month", "hydro_min_flow_mw", "hydro_avg_flow_mw"]) |
| 49 | + |
| 50 | + # 3. Identify plants where data is missing |
| 51 | + missing_hydro_plants = hydro_plants[~hydro_plants.isin(worst_year_data["generation_plant_id"])].values |
| 52 | + |
| 53 | + # 4. For each missing plant get the data for all the years |
| 54 | + db_cursor.execute( |
| 55 | + f""" |
| 56 | + SELECT generation_plant_id, year, month, hydro_min_flow_mw, hydro_avg_flow_mw FROM hydro_historical_monthly_capacity_factors |
| 57 | + WHERE hydro_simple_scenario_id={raw_data_scenario} and generation_plant_id in ({",".join(missing_hydro_plants.astype(str))}); |
| 58 | + """) |
| 59 | + missing_plants_data = pd.DataFrame(db_cursor.fetchall(), |
| 60 | + columns=["generation_plant_id", "year", "month", "hydro_min_flow_mw", |
| 61 | + "hydro_avg_flow_mw"]) |
| 62 | + |
| 63 | + # 5. Pick the year with the least flow |
| 64 | + # Aggregate by year |
| 65 | + missing_data_by_year = missing_plants_data.groupby(["generation_plant_id", "year"], as_index=False)[ |
| 66 | + "hydro_avg_flow_mw"].mean() |
| 67 | + # Select years where the flow is at its lowest |
| 68 | + year_to_use = \ |
| 69 | + missing_data_by_year.loc[missing_data_by_year.groupby("generation_plant_id")["hydro_avg_flow_mw"].idxmin()][ |
| 70 | + ["generation_plant_id", "year"]] |
| 71 | + # Essentially filter missing_plants_data to only include keys from the right table, aka plants and years that are lowest |
| 72 | + missing_plants_data = missing_plants_data.merge( |
| 73 | + year_to_use, |
| 74 | + on=["generation_plant_id", "year"], |
| 75 | + how="right" |
| 76 | + ).drop("year", axis=1) |
| 77 | + |
| 78 | + # 6. Add the missing data to our worst year data and verify we have data for all the plants |
| 79 | + worst_year_data = pd.concat([worst_year_data, missing_plants_data]) |
| 80 | + assert all(hydro_plants.isin(worst_year_data["generation_plant_id"])) |
| 81 | + |
| 82 | + # 7. Cross join the series with all the years from 2020 to 2050 |
| 83 | + years = pd.Series(range(new_start_year, new_end_year + 1), name="year") |
| 84 | + worst_year_data = worst_year_data.merge( |
| 85 | + years, |
| 86 | + how="cross" |
| 87 | + ) |
| 88 | + worst_year_data["hydro_simple_scenario_id"] = new_scenario_id |
| 89 | + |
| 90 | + # 8. Complete some data checks |
| 91 | + assert len(worst_year_data) == 12 * (new_end_year - new_start_year + 1) * len(hydro_plants) |
| 92 | + |
| 93 | + # 9. Add data to database |
| 94 | + print(f"hydro_simple_scenario: {new_scenario_id}") |
| 95 | + print(f"name: {new_scenario_name}") |
| 96 | + print(f"description: {new_scenario_description}") |
| 97 | + print(f"Num hydro plants: {worst_year_data.generation_plant_id.nunique()}") |
| 98 | + print(f"From year: {new_start_year}") |
| 99 | + print(f"To year: {new_end_year}") |
| 100 | + print(f"Example data:\n{worst_year_data.head()}") |
| 101 | + |
| 102 | + if not query_yes_no("\nAre you sure you want to add this data to the database?", default="no"): |
| 103 | + raise SystemExit |
| 104 | + |
| 105 | + db_cursor.execute( |
| 106 | + "INSERT INTO hydro_simple_scenario(hydro_simple_scenario_id, name, description) " |
| 107 | + f"VALUES ('{new_scenario_id}','{new_scenario_name}','{new_scenario_description}')" |
| 108 | + ) |
| 109 | + |
| 110 | + n = len(worst_year_data) |
| 111 | + start_time = time.time() |
| 112 | + for i, r in enumerate(worst_year_data.itertuples(index=False)): |
| 113 | + if i !=0 and i % 1000 == 0: |
| 114 | + print( |
| 115 | + f"{i}/{n} inserts completed. Estimated time remaining {format_seconds((n - i) * (time.time() - start_time) / i)}") |
| 116 | + db_cursor.execute( |
| 117 | + f"INSERT INTO hydro_historical_monthly_capacity_factors(hydro_simple_scenario_id, generation_plant_id, year, month, hydro_min_flow_mw, hydro_avg_flow_mw) " |
| 118 | + f"VALUES ({r.hydro_simple_scenario_id},{r.generation_plant_id},{r.year},{r.month},{r.hydro_min_flow_mw},{r.hydro_avg_flow_mw})" |
| 119 | + ) |
| 120 | + |
| 121 | + db_conn.commit() |
| 122 | + db_cursor.close() |
| 123 | + db_conn.close() |
| 124 | + print("Done.") |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments