Skip to content

Commit

Permalink
Update how hydro op chars are selected from the database to only sele…
Browse files Browse the repository at this point in the history
…ct horizons on periods when the project is operational
  • Loading branch information
Janie115 committed Sep 12, 2024
1 parent 23f5d4a commit 857a495
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
32 changes: 32 additions & 0 deletions db/db_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6241,6 +6241,38 @@ FROM inputs_temporal
USING (temporal_scenario_id, stage_id, timepoint)
;

-- This view shows the possible operational horizons for each project based
-- based on its operational periods (see project_operational_periods), its
-- balancing type, and the periods-horizons mapping for that balancing type
-- (see periods_horizons). It also includes the operational type and the
-- hydro_operational_chars_scenario_id, since these are useful to slice out
-- operational types of interest (namely hydro) and join the hydro inputs,
-- which are indexed by project-horizon.
DROP VIEW IF EXISTS project_operational_horizons;
CREATE VIEW project_operational_horizons AS
SELECT project_portfolio_scenario_id,
project_operational_chars_scenario_id,
project_specified_capacity_scenario_id,
project_new_cost_scenario_id,
temporal_scenario_id,
operational_type,
hydro_operational_chars_scenario_id,
stage_id,
project,
horizon
-- Get all projects in the portfolio (with their opchars)
FROM project_portfolio_opchars
-- Add all the periods horizons for the matching balancing type
LEFT OUTER JOIN
periods_horizons
ON (project_portfolio_opchars.balancing_type_project
= periods_horizons.balancing_type_horizon)
-- Only select horizons from the actual operational periods
INNER JOIN
project_operational_periods
USING (temporal_scenario_id, project, period)
;

-- This view shows the possible operational timepoints for each project based
-- based on its operational periods (see project_operational_periods), and
-- the timepoints in the temporal subscenario (see inputs_temporal). It also
Expand Down
55 changes: 21 additions & 34 deletions gridpath/project/operations/operational_types/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,43 +797,30 @@ def get_hydro_inputs_from_database(

# TODO: figure out if this still works after hydro update in ra toolkit
sql = f"""
SELECT project, prj_tbl.horizon, average_power_fraction, min_power_fraction,
max_power_fraction
FROM (
SELECT project, balancing_type_project, horizon, average_power_fraction,
min_power_fraction, max_power_fraction
FROM inputs_project_hydro_operational_chars
-- Portfolio projects only
WHERE project in (
SELECT project
FROM inputs_project_portfolios
SELECT project, horizon, average_power_fraction, min_power_fraction,
max_power_fraction
FROM
(SELECT project, stage_id, horizon
FROM project_operational_horizons
WHERE project_portfolio_scenario_id = {subscenarios.PROJECT_PORTFOLIO_SCENARIO_ID}
)
-- Get the right opchar
AND (project, hydro_operational_chars_scenario_id) in (
AND project_operational_chars_scenario_id = {subscenarios.PROJECT_OPERATIONAL_CHARS_SCENARIO_ID}
AND temporal_scenario_id = {subscenarios.TEMPORAL_SCENARIO_ID}
AND (project_specified_capacity_scenario_id = {subscenarios.PROJECT_SPECIFIED_CAPACITY_SCENARIO_ID}
OR project_new_cost_scenario_id = {subscenarios.PROJECT_NEW_COST_SCENARIO_ID})
AND stage_id = {stage}
) as projects_periods_timepoints_tbl
INNER JOIN (
SELECT project, hydro_operational_chars_scenario_id
FROM inputs_project_operational_chars
WHERE project_operational_chars_scenario_id
= {subscenarios.PROJECT_OPERATIONAL_CHARS_SCENARIO_ID}
)
-- Get the relevant stage
AND stage_id = {stage}
-- Get the right iteration
AND hydro_iteration = {hydro_iteration}
) as prj_tbl
-- Limit to bt-horizons from this temporal scenario ID
JOIN (
SELECT DISTINCT balancing_type_horizon, horizon
FROM inputs_temporal_horizon_timepoints
WHERE temporal_scenario_id = {subscenarios.TEMPORAL_SCENARIO_ID}
AND subproblem_id = {subproblem}
AND stage_id = {stage}
) as hrz_tbl
ON (
balancing_type_project = balancing_type_horizon
AND prj_tbl.horizon = hrz_tbl.horizon
)
;
WHERE project_operational_chars_scenario_id = {subscenarios.PROJECT_OPERATIONAL_CHARS_SCENARIO_ID}
AND operational_type = '{op_type}'
) AS hydro_char
USING (project)
LEFT OUTER JOIN
inputs_project_hydro_operational_chars
USING (hydro_operational_chars_scenario_id, project, stage_id, horizon)
WHERE hydro_iteration = {hydro_iteration}
;
"""

hydro_chars = c.execute(sql)
Expand Down

0 comments on commit 857a495

Please sign in to comment.