@@ -637,27 +637,52 @@ def query_db(full_config, skip_cf):
637637 # zone + watershed. Eventually, we may rethink this derating, but it is a reasonable
638638 # approximation for a large hydro fleet where plant outages are individual random events.
639639 # Negative flows are replaced by 0.
640+ write_csv_from_query (
641+ db_cursor ,
642+ "hydro_timepoints" ,
643+ ["timepoint_id" , "tp_to_hts" ],
644+ f"""
645+ SELECT
646+ tp.raw_timepoint_id AS timepoint_id,
647+ p.label || '_M' || date_part('month', timestamp_utc) AS tp_to_hts
648+ FROM switch.sampled_timepoint AS tp
649+ JOIN switch.period AS p USING(period_id, study_timeframe_id)
650+ WHERE time_sample_id = { time_sample_id }
651+ AND study_timeframe_id = { study_timeframe_id }
652+ ORDER BY 1;
653+ """
654+ )
655+
640656 write_csv_from_query (
641657 db_cursor ,
642658 "hydro_timeseries" ,
643659 ["hydro_project" , "timeseries" , "hydro_min_flow_mw" , "hydro_avg_flow_mw" ],
644660 f"""
645- select generation_plant_id as hydro_project,
646- { timeseries_id_select } ,
661+ SELECT
662+ generation_plant_id AS hydro_project,
663+ hts.hydro_timeseries,
647664 CASE
648665 WHEN hydro_min_flow_mw <= 0 THEN 0
649666 ELSE least(hydro_min_flow_mw, capacity_limit_mw * (1-forced_outage_rate)) END,
650667 CASE
651668 WHEN hydro_avg_flow_mw <= 0 THEN 0
652669 ELSE least(hydro_avg_flow_mw, capacity_limit_mw * (1-forced_outage_rate)) END
653- as hydro_avg_flow_mw
654- from hydro_historical_monthly_capacity_factors
655- join sampled_timeseries on(month = date_part('month', first_timepoint_utc) and year = date_part('year', first_timepoint_utc))
656- join generation_plant using (generation_plant_id)
670+ AS hydro_avg_flow_mw
671+ FROM (
672+ SELECT DISTINCT
673+ date_part('month', tp.timestamp_utc) as month,
674+ date_part('year', tp.timestamp_utc) as year,
675+ p.label || '_M' || date_part('month', timestamp_utc) AS hydro_timeseries
676+ FROM switch.sampled_timepoint AS tp
677+ JOIN switch.period AS p USING(period_id, study_timeframe_id)
678+ WHERE time_sample_id = { time_sample_id }
679+ AND study_timeframe_id = { study_timeframe_id }
680+ ) AS hts
681+ JOIN switch.hydro_historical_monthly_capacity_factors USING(month, year)
682+ JOIN switch.generation_plant USING(generation_plant_id)
657683 JOIN temp_generation_plant_ids USING(generation_plant_id)
658- where hydro_simple_scenario_id={ hydro_simple_scenario_id }
659- and time_sample_id = { time_sample_id }
660- order by 1;
684+ WHERE hydro_simple_scenario_id={ hydro_simple_scenario_id }
685+ ORDER BY 1;
661686 """ ,
662687 )
663688
0 commit comments