From a55d173527a8fe571bdcdf0ef98f2d49ef1af365 Mon Sep 17 00:00:00 2001 From: Shawn Crawley Date: Tue, 19 Dec 2023 16:03:25 -0700 Subject: [PATCH] Mod to save and reassign status rather than recalculate Fixes a number of issues reported by WPOD --- .../rnr_domain_generator/sql/domain.sql | 75 +++++++++++-------- .../rfc_based_5day_max_streamflow.sql | 36 ++------- 2 files changed, 50 insertions(+), 61 deletions(-) diff --git a/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/domain.sql b/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/domain.sql index 659ed75e..94b8e6a1 100644 --- a/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/domain.sql +++ b/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/domain.sql @@ -105,13 +105,13 @@ SELECT INTO rnr.temporal_domain_flow_forecasts FROM all_flow_forecasts; -------------------------------------------------------- -------------------------------------------------------- -------------- CREATE RNR DOMAIN ROUTLINK ------------- -------------------------------------------------------- -------------------------------------------------------- +----------------------------------------------------------------- +----------------------------------------------------------------- +------------- CREATE DOMAIN LIDS WITH STATUS TABLE ------------- +----------------------------------------------------------------- +----------------------------------------------------------------- -DROP TABLE IF EXISTS rnr.domain_routelink; +DROP TABLE IF EXISTS rnr.domain_lids_with_status; WITH RECURSIVE @@ -215,10 +215,11 @@ max_status_flow AS ( THEN CASE WHEN th.record_flow IS NOT NULL AND mf.max_flow_cfs >= th.record_flow THEN 'record' - ELSE 'thresholds undefined' + ELSE 'all thresholds undefined' END - ELSE 'no_flooding' - END as status + ELSE 'no flooding' + END as status, + rating_source FROM max_flow AS mf LEFT JOIN threshold AS th ON th.nws_station_id = mf.lid @@ -240,39 +241,53 @@ max_status_stage AS ( THEN CASE WHEN th.record_stage IS NOT NULL AND mf.max_stage_ft >= th.record_stage THEN 'record' - ELSE 'thresholds undefined' + ELSE 'all thresholds undefined' END - ELSE 'no_flooding' - END as status + ELSE 'no flooding' + END as status, + rating_source FROM max_stage AS mf LEFT JOIN threshold AS th ON th.nws_station_id = mf.lid ), -flood_flow_lid AS ( - SELECT lid - FROM max_status_flow - WHERE status in ('action', 'minor', 'moderate', 'major', 'record') -), - -flood_stage_lid AS ( - SELECT lid +lid_status AS ( + SELECT + lid, + status, + rating_source FROM max_status_stage - WHERE status in ('action', 'minor', 'moderate', 'major', 'record') -), - -flood_lid AS ( + + UNION + SELECT - lid - FROM flood_flow_lid + lid, + status, + rating_source + FROM max_status_flow + WHERE lid NOT IN (SELECT lid FROM max_status_stage) +) - UNION +SELECT * +INTO rnr.domain_lids_with_status +FROM lid_status +LEFT JOIN derived.ahps_restricted_sites restricted + ON restricted.nws_lid = lid_status.lid +WHERE restricted.nws_lid IS NULL; + +------------------------------------------------------- +------------------------------------------------------- +------------- CREATE RNR DOMAIN ROUTLINK ------------- +------------------------------------------------------- +------------------------------------------------------- + +DROP TABLE IF EXISTS rnr.domain_routelink; +WITH RECURSIVE flood_lid AS ( SELECT lid - FROM flood_stage_lid - WHERE lid NOT IN (SELECT lid FROM flood_flow_lid) - AND lid IN (SELECT lid FROM max_flow) + FROM rnr.domain_lids_with_status + WHERE status IN ('action', 'minor', 'moderate', 'major') ), flood_xwalk AS ( diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/replace_route/rfc_based_5day_max_streamflow.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/replace_route/rfc_based_5day_max_streamflow.sql index 4a15ed50..01a3faf0 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/replace_route/rfc_based_5day_max_streamflow.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/replace_route/rfc_based_5day_max_streamflow.sql @@ -118,39 +118,13 @@ root_status_trace_reaches AS ( stream_length, is_waterbody, fcst_meta.issue_time, - rating_source, - CASE - WHEN rfc_defined_fcst_point AND fcst_meta.issue_time IS NULL - THEN 'No Forecast' - WHEN major_flow IS NOT NULL AND streamflow > major_flow - THEN 'Major' - WHEN major_stage IS NOT NULL AND stage_from_curve > major_stage - THEN 'Major' - WHEN moderate_flow IS NOT NULL AND streamflow > moderate_flow - THEN 'Moderate' - WHEN moderate_stage IS NOT NULL AND stage_from_curve > moderate_stage - THEN 'Moderate' - WHEN minor_flow IS NOT NULL AND streamflow > minor_flow - THEN 'Minor' - WHEN minor_stage IS NOT NULL AND stage_from_curve > minor_stage - THEN 'Minor' - WHEN action_flow IS NOT NULL AND streamflow > action_flow - THEN 'Action' - WHEN action_stage IS NOT NULL AND stage_from_curve > action_stage - THEN 'Action' - WHEN issue_time IS NOT NULL - AND action_flow IS NULL AND minor_flow IS NULL AND moderate_flow IS NULL AND major_flow IS NULL - AND action_stage IS NULL AND minor_stage IS NULL AND moderate_stage IS NULL AND major_stage IS NULL - THEN 'All Thresholds Undefined' - WHEN mf.nws_station_id IS NOT NULL - THEN 'No Flooding' - ELSE '' - END as max_status + status.rating_source, + INITCAP(status.status) as max_status FROM max_flows_station_xwalk_with_rc_stage mf LEFT JOIN fcst_meta ON fcst_meta.lid = mf.nws_station_id - LEFT JOIN threshold - ON threshold.nws_station_id = mf.nws_station_id + LEFT JOIN rnr.domain_lids_with_status status + ON status.lid = mf.nws_station_id WHERE rfc_defined_fcst_point IS TRUE OR issue_time IS NOT NULL ORDER BY feature_id, issue_time, rfc_defined_fcst_point DESC ), @@ -219,7 +193,7 @@ agg_trace AS ( WHEN max_status = 'All Thresholds Undefined' THEN max_status || ' at ' || influential_forecast_point || ' (' || root_feature_id || ' [' || root_stream_order || ']) despite forecast issued ' || issue_time || ' ' || ROUND(CAST(distance_from_forecast_point * 0.000621 as numeric), 1) || ' miles upstream' WHEN issue_time IS NOT NULL - THEN max_status || ' (' || rating_source || ') issued ' || issue_time || ' at ' || influential_forecast_point || ' (' || root_feature_id || ' [' || root_stream_order || ']) ' || ROUND(CAST(distance_from_forecast_point * 0.000621 as numeric), 1) || ' miles upstream' + THEN max_status || ' issued ' || issue_time || ' at ' || influential_forecast_point || ' (' || root_feature_id || ' [' || root_stream_order || ']) ' || ROUND(CAST(distance_from_forecast_point * 0.000621 as numeric), 1) || ' miles upstream' ELSE max_status || ' at ' || influential_forecast_point || ' (' || root_feature_id || ' [' || root_stream_order || ']) ' || ROUND(CAST(distance_from_forecast_point * 0.000621 as numeric), 1) || ' miles upstream' END, '; '