|
| 1 | +""" |
| 2 | +#################### |
| 3 | +Create a hydro scenario with only half the flow of the baseline |
| 4 | +
|
| 5 | +Date applied: 2021-08-02 |
| 6 | +Description: |
| 7 | +This script adds a scenario to the database that makes all hydro flows half of the expected hydro flow. |
| 8 | +################# |
| 9 | +""" |
| 10 | +import time |
| 11 | + |
| 12 | +from switch_model.utilities import query_yes_no, format_seconds |
| 13 | +from switch_model.wecc.utilities import connect |
| 14 | +import pandas as pd |
| 15 | + |
| 16 | +all_plants_scenario = 23 |
| 17 | + |
| 18 | +new_scenario_id = 26 |
| 19 | +new_scenario_name = "50% of scenario 23" |
| 20 | +new_scenario_description = "All average flows are halved to represent a scenario where hydro generation is low." |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + db_conn = connect() |
| 25 | + db_cursor = db_conn.cursor() |
| 26 | + |
| 27 | + # 1. Get all the hydro plants |
| 28 | + db_cursor.execute( |
| 29 | + f""" |
| 30 | + SELECT DISTINCT generation_plant_id, year, month, hydro_min_flow_mw, hydro_avg_flow_mw FROM hydro_historical_monthly_capacity_factors |
| 31 | + WHERE hydro_simple_scenario_id={all_plants_scenario}; |
| 32 | + """) |
| 33 | + df = pd.DataFrame(db_cursor.fetchall(), |
| 34 | + columns=["generation_plant_id", "year", "month", "hydro_min_flow_mw", "hydro_avg_flow_mw"]) |
| 35 | + |
| 36 | + # 2. Set all the flows to zero and set the scenario id |
| 37 | + df["hydro_avg_flow_mw"] /= 2 |
| 38 | + df["hydro_min_flow_mw"] = df[["hydro_min_flow_mw", "hydro_avg_flow_mw"]].min(axis=1) |
| 39 | + df["hydro_simple_scenario_id"] = new_scenario_id |
| 40 | + |
| 41 | + # 3. Add data to database |
| 42 | + print(f"hydro_simple_scenario: {new_scenario_id}") |
| 43 | + print(f"name: {new_scenario_name}") |
| 44 | + print(f"description: {new_scenario_description}") |
| 45 | + print(f"Num hydro plants: {df.generation_plant_id.nunique()}") |
| 46 | + print(f"Example data:\n{df.head()}") |
| 47 | + |
| 48 | + if not query_yes_no("\nAre you sure you want to add this data to the database?", default="no"): |
| 49 | + raise SystemExit |
| 50 | + |
| 51 | + db_cursor.execute( |
| 52 | + "INSERT INTO hydro_simple_scenario(hydro_simple_scenario_id, name, description) " |
| 53 | + f"VALUES ('{new_scenario_id}','{new_scenario_name}','{new_scenario_description}')" |
| 54 | + ) |
| 55 | + |
| 56 | + n = len(df) |
| 57 | + start_time = time.time() |
| 58 | + for i, r in enumerate(df.itertuples(index=False)): |
| 59 | + if i != 0 and i % 1000 == 0: |
| 60 | + print( |
| 61 | + f"{i}/{n} inserts completed. Estimated time remaining {format_seconds((n - i) * (time.time() - start_time) / i)}") |
| 62 | + db_cursor.execute( |
| 63 | + 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) " |
| 64 | + 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})" |
| 65 | + ) |
| 66 | + |
| 67 | + db_conn.commit() |
| 68 | + db_cursor.close() |
| 69 | + db_conn.close() |
| 70 | + print("Done.") |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments