From a580c685b0ae0f4ee01bd38fc42b1d058dd1c024 Mon Sep 17 00:00:00 2001 From: Simon Norris Date: Fri, 5 Jan 2024 15:18:37 -0800 Subject: [PATCH] create cw/mad/map tables up front, add summary diff views, tweak access makefile for minimizing rebuilds --- db/tables/channel_width.sql | 6 + db/tables/discharge.sql | 8 + db/views/wsg_crossing_summary.sql | 113 ++++++++++++ db/views/wsg_linear_summary.sql | 224 ++++++++++++++++++++++++ model/01_access/Makefile | 57 ++++-- model/01_access/sql/load_map_mad_cw.sql | 40 ----- 6 files changed, 398 insertions(+), 50 deletions(-) create mode 100644 db/tables/channel_width.sql create mode 100644 db/tables/discharge.sql create mode 100644 db/views/wsg_crossing_summary.sql create mode 100644 db/views/wsg_linear_summary.sql diff --git a/db/tables/channel_width.sql b/db/tables/channel_width.sql new file mode 100644 index 00000000..4c9314a1 --- /dev/null +++ b/db/tables/channel_width.sql @@ -0,0 +1,6 @@ +drop table if exists bcfishpass.channel_width; +create table if not exists bcfishpass.channel_width ( + linear_feature_id bigint primary key, + channel_width_source text , + channel_width double precision +); \ No newline at end of file diff --git a/db/tables/discharge.sql b/db/tables/discharge.sql new file mode 100644 index 00000000..291d50b6 --- /dev/null +++ b/db/tables/discharge.sql @@ -0,0 +1,8 @@ +drop table if exists bcfishpass.discharge; +create table bcfishpass.discharge +( + linear_feature_id bigint primary key, + watershed_group_code text , + mad_mm double precision, + mad_m3s double precision +); \ No newline at end of file diff --git a/db/views/wsg_crossing_summary.sql b/db/views/wsg_crossing_summary.sql new file mode 100644 index 00000000..5a361c4c --- /dev/null +++ b/db/views/wsg_crossing_summary.sql @@ -0,0 +1,113 @@ +drop view if exists bcfishpass.wsg_crossing_summary_current; + +create view bcfishpass.wsg_crossing_summary_current as +select distinct on (watershed_group_code) + s.model_run_id , + s.watershed_group_code , + s.crossing_feature_type , + s.n_crossings_total , + s.n_passable_total , + s.n_barriers_total , + s.n_potential_total , + s.n_unknown_total , + s.n_barriers_accessible_bt , + s.n_potential_accessible_bt , + s.n_unknown_accessible_bt , + s.n_barriers_accessible_ch_cm_co_pk_sk , + s.n_potential_accessible_ch_cm_co_pk_sk , + s.n_unknown_accessible_ch_cm_co_pk_sk , + s.n_barriers_accessible_st , + s.n_potential_accessible_st , + s.n_unknown_accessible_st , + s.n_barriers_accessible_wct , + s.n_potential_accessible_wct , + s.n_unknown_accessible_wct , + s.n_barriers_habitat_bt , + s.n_potential_habitat_bt , + s.n_unknown_habitat_bt , + s.n_barriers_habitat_ch , + s.n_potential_habitat_ch , + s.n_unknown_habitat_ch , + s.n_barriers_habitat_cm , + s.n_potential_habitat_cm , + s.n_unknown_habitat_cm , + s.n_barriers_habitat_co , + s.n_potential_habitat_co , + s.n_unknown_habitat_co , + s.n_barriers_habitat_pk , + s.n_potential_habitat_pk , + s.n_unknown_habitat_pk , + s.n_barriers_habitat_sk , + s.n_potential_habitat_sk , + s.n_unknown_habitat_sk , + s.n_barriers_habitat_salmon , + s.n_potential_habitat_salmon , + s.n_unknown_habitat_salmon , + s.n_barriers_habitat_st , + s.n_potential_habitat_st , + s.n_unknown_habitat_st , + s.n_barriers_habitat_wct , + s.n_potential_habitat_wct , + s.n_unknown_habitat_wct +from bcfishpass.wsg_crossing_summary s +inner join bcfishpass.log l +on s.model_run_id = l.model_run_id +order by s.watershed_group_code, l.date_completed desc; + + +drop view if exists bcfishpass.wsg_crossing_summary_previous; + +create view bcfishpass.wsg_crossing_summary_previous as +select + s.model_run_id , + s.watershed_group_code , + s.crossing_feature_type , + s.n_crossings_total , + s.n_passable_total , + s.n_barriers_total , + s.n_potential_total , + s.n_unknown_total , + s.n_barriers_accessible_bt , + s.n_potential_accessible_bt , + s.n_unknown_accessible_bt , + s.n_barriers_accessible_ch_cm_co_pk_sk , + s.n_potential_accessible_ch_cm_co_pk_sk , + s.n_unknown_accessible_ch_cm_co_pk_sk , + s.n_barriers_accessible_st , + s.n_potential_accessible_st , + s.n_unknown_accessible_st , + s.n_barriers_accessible_wct , + s.n_potential_accessible_wct , + s.n_unknown_accessible_wct , + s.n_barriers_habitat_bt , + s.n_potential_habitat_bt , + s.n_unknown_habitat_bt , + s.n_barriers_habitat_ch , + s.n_potential_habitat_ch , + s.n_unknown_habitat_ch , + s.n_barriers_habitat_cm , + s.n_potential_habitat_cm , + s.n_unknown_habitat_cm , + s.n_barriers_habitat_co , + s.n_potential_habitat_co , + s.n_unknown_habitat_co , + s.n_barriers_habitat_pk , + s.n_potential_habitat_pk , + s.n_unknown_habitat_pk , + s.n_barriers_habitat_sk , + s.n_potential_habitat_sk , + s.n_unknown_habitat_sk , + s.n_barriers_habitat_salmon , + s.n_potential_habitat_salmon , + s.n_unknown_habitat_salmon , + s.n_barriers_habitat_st , + s.n_potential_habitat_st , + s.n_unknown_habitat_st , + s.n_barriers_habitat_wct , + s.n_potential_habitat_wct , + s.n_unknown_habitat_wct +from bcfishpass.wsg_crossing_summary s +inner join bcfishpass.log l +on s.model_run_id = l.model_run_id +where l.date_completed = (select date_completed from bcfishpass.log order by date_completed desc offset 1 limit 1) +order by watershed_group_code; \ No newline at end of file diff --git a/db/views/wsg_linear_summary.sql b/db/views/wsg_linear_summary.sql new file mode 100644 index 00000000..064cc8c4 --- /dev/null +++ b/db/views/wsg_linear_summary.sql @@ -0,0 +1,224 @@ +drop view if exists bcfishpass.wsg_linear_summary_current; +create view bcfishpass.wsg_linear_summary_current as +select distinct on (watershed_group_code) + s.model_run_id , + s.watershed_group_code , + s.length_total , + s.length_potentiallyaccessible_bt , + s.length_potentiallyaccessible_bt_observed , + s.length_potentiallyaccessible_bt_accessible_a , + s.length_potentiallyaccessible_bt_accessible_b , + s.length_obsrvd_spawning_rearing_bt , + s.length_obsrvd_spawning_rearing_bt_accessible_a , + s.length_obsrvd_spawning_rearing_bt_accessible_b , + s.length_spawning_rearing_bt , + s.length_spawning_rearing_bt_accessible_a , + s.length_spawning_rearing_bt_accessible_b , + s.length_potentiallyaccessible_ch_cm_co_pk_sk , + s.length_potentiallyaccessible_ch_cm_co_pk_sk_observed , + s.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_a , + s.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_b , + s.length_obsrvd_spawning_rearing_ch , + s.length_obsrvd_spawning_rearing_ch_accessible_a , + s.length_obsrvd_spawning_rearing_ch_accessible_b , + s.length_spawning_rearing_ch , + s.length_spawning_rearing_ch_accessible_a , + s.length_spawning_rearing_ch_accessible_b , + s.length_obsrvd_spawning_rearing_cm , + s.length_obsrvd_spawning_rearing_cm_accessible_a , + s.length_obsrvd_spawning_rearing_cm_accessible_b , + s.length_spawning_rearing_cm , + s.length_spawning_rearing_cm_accessible_a , + s.length_spawning_rearing_cm_accessible_b , + s.length_obsrvd_spawning_rearing_co , + s.length_obsrvd_spawning_rearing_co_accessible_a , + s.length_obsrvd_spawning_rearing_co_accessible_b , + s.length_spawning_rearing_co , + s.length_spawning_rearing_co_accessible_a , + s.length_spawning_rearing_co_accessible_b , + s.length_obsrvd_spawning_rearing_pk , + s.length_obsrvd_spawning_rearing_pk_accessible_a , + s.length_obsrvd_spawning_rearing_pk_accessible_b , + s.length_spawning_rearing_pk , + s.length_spawning_rearing_pk_accessible_a , + s.length_spawning_rearing_pk_accessible_b , + s.length_obsrvd_spawning_rearing_sk , + s.length_obsrvd_spawning_rearing_sk_accessible_a , + s.length_obsrvd_spawning_rearing_sk_accessible_b , + s.length_spawning_rearing_sk , + s.length_spawning_rearing_sk_accessible_a , + s.length_spawning_rearing_sk_accessible_b , + s.length_potentiallyaccessible_st , + s.length_potentiallyaccessible_st_observed , + s.length_potentiallyaccessible_st_accessible_a , + s.length_potentiallyaccessible_st_accessible_b , + s.length_obsrvd_spawning_rearing_st , + s.length_obsrvd_spawning_rearing_st_accessible_a , + s.length_obsrvd_spawning_rearing_st_accessible_b , + s.length_spawning_rearing_st , + s.length_spawning_rearing_st_accessible_a , + s.length_spawning_rearing_st_accessible_b , + s.length_potentiallyaccessible_wct , + s.length_potentiallyaccessible_wct_observed , + s.length_potentiallyaccessible_wct_accessible_a , + s.length_potentiallyaccessible_wct_accessible_b , + s.length_obsrvd_spawning_rearing_wct , + s.length_obsrvd_spawning_rearing_wct_accessible_a , + s.length_obsrvd_spawning_rearing_wct_accessible_b , + s.length_spawning_rearing_wct , + s.length_spawning_rearing_wct_accessible_a , + s.length_spawning_rearing_wct_accessible_b +from bcfishpass.wsg_linear_summary s +inner join bcfishpass.log l +on s.model_run_id = l.model_run_id +order by s.watershed_group_code, l.date_completed desc; + + +drop view if exists bcfishpass.wsg_linear_summary_previous; +create view bcfishpass.wsg_linear_summary_previous as +select + s.model_run_id , + s.watershed_group_code , + s.length_total , + s.length_potentiallyaccessible_bt , + s.length_potentiallyaccessible_bt_observed , + s.length_potentiallyaccessible_bt_accessible_a , + s.length_potentiallyaccessible_bt_accessible_b , + s.length_obsrvd_spawning_rearing_bt , + s.length_obsrvd_spawning_rearing_bt_accessible_a , + s.length_obsrvd_spawning_rearing_bt_accessible_b , + s.length_spawning_rearing_bt , + s.length_spawning_rearing_bt_accessible_a , + s.length_spawning_rearing_bt_accessible_b , + s.length_potentiallyaccessible_ch_cm_co_pk_sk , + s.length_potentiallyaccessible_ch_cm_co_pk_sk_observed , + s.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_a , + s.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_b , + s.length_obsrvd_spawning_rearing_ch , + s.length_obsrvd_spawning_rearing_ch_accessible_a , + s.length_obsrvd_spawning_rearing_ch_accessible_b , + s.length_spawning_rearing_ch , + s.length_spawning_rearing_ch_accessible_a , + s.length_spawning_rearing_ch_accessible_b , + s.length_obsrvd_spawning_rearing_cm , + s.length_obsrvd_spawning_rearing_cm_accessible_a , + s.length_obsrvd_spawning_rearing_cm_accessible_b , + s.length_spawning_rearing_cm , + s.length_spawning_rearing_cm_accessible_a , + s.length_spawning_rearing_cm_accessible_b , + s.length_obsrvd_spawning_rearing_co , + s.length_obsrvd_spawning_rearing_co_accessible_a , + s.length_obsrvd_spawning_rearing_co_accessible_b , + s.length_spawning_rearing_co , + s.length_spawning_rearing_co_accessible_a , + s.length_spawning_rearing_co_accessible_b , + s.length_obsrvd_spawning_rearing_pk , + s.length_obsrvd_spawning_rearing_pk_accessible_a , + s.length_obsrvd_spawning_rearing_pk_accessible_b , + s.length_spawning_rearing_pk , + s.length_spawning_rearing_pk_accessible_a , + s.length_spawning_rearing_pk_accessible_b , + s.length_obsrvd_spawning_rearing_sk , + s.length_obsrvd_spawning_rearing_sk_accessible_a , + s.length_obsrvd_spawning_rearing_sk_accessible_b , + s.length_spawning_rearing_sk , + s.length_spawning_rearing_sk_accessible_a , + s.length_spawning_rearing_sk_accessible_b , + s.length_potentiallyaccessible_st , + s.length_potentiallyaccessible_st_observed , + s.length_potentiallyaccessible_st_accessible_a , + s.length_potentiallyaccessible_st_accessible_b , + s.length_obsrvd_spawning_rearing_st , + s.length_obsrvd_spawning_rearing_st_accessible_a , + s.length_obsrvd_spawning_rearing_st_accessible_b , + s.length_spawning_rearing_st , + s.length_spawning_rearing_st_accessible_a , + s.length_spawning_rearing_st_accessible_b , + s.length_potentiallyaccessible_wct , + s.length_potentiallyaccessible_wct_observed , + s.length_potentiallyaccessible_wct_accessible_a , + s.length_potentiallyaccessible_wct_accessible_b , + s.length_obsrvd_spawning_rearing_wct , + s.length_obsrvd_spawning_rearing_wct_accessible_a , + s.length_obsrvd_spawning_rearing_wct_accessible_b , + s.length_spawning_rearing_wct , + s.length_spawning_rearing_wct_accessible_a , + s.length_spawning_rearing_wct_accessible_b +from bcfishpass.wsg_linear_summary s +inner join bcfishpass.log l +on s.model_run_id = l.model_run_id +where l.date_completed = (select date_completed from bcfishpass.log order by date_completed desc offset 1 limit 1) +order by watershed_group_code; + +drop view if exists bcfishpass.wsg_linear_summary_diff; +create view bcfishpass.wsg_linear_summary_diff as +select + a.watershed_group_code , + a.length_total - b.length_total as length_total, + a.length_potentiallyaccessible_bt - b.length_potentiallyaccessible_bt as length_potentiallyaccessible_bt, + a.length_potentiallyaccessible_bt_observed - b.length_potentiallyaccessible_bt_observed as length_potentiallyaccessible_bt_observed, + a.length_potentiallyaccessible_bt_accessible_a - b.length_potentiallyaccessible_bt_accessible_a as length_potentiallyaccessible_bt_accessible_a, + a.length_potentiallyaccessible_bt_accessible_b - b.length_potentiallyaccessible_bt_accessible_b as length_potentiallyaccessible_bt_accessible_b, + a.length_obsrvd_spawning_rearing_bt - b.length_obsrvd_spawning_rearing_bt as length_obsrvd_spawning_rearing_bt, + a.length_obsrvd_spawning_rearing_bt_accessible_a - b.length_obsrvd_spawning_rearing_bt_accessible_a as length_obsrvd_spawning_rearing_bt_accessible_a, + a.length_obsrvd_spawning_rearing_bt_accessible_b - b.length_obsrvd_spawning_rearing_bt_accessible_b as length_obsrvd_spawning_rearing_bt_accessible_b, + a.length_spawning_rearing_bt - b.length_spawning_rearing_bt as length_spawning_rearing_bt, + a.length_spawning_rearing_bt_accessible_a - b.length_spawning_rearing_bt_accessible_a as length_spawning_rearing_bt_accessible_a, + a.length_spawning_rearing_bt_accessible_b - b.length_spawning_rearing_bt_accessible_b as length_spawning_rearing_bt_accessible_b, + a.length_potentiallyaccessible_ch_cm_co_pk_sk - b.length_potentiallyaccessible_ch_cm_co_pk_sk as length_potentiallyaccessible_ch_cm_co_pk_sk, + a.length_potentiallyaccessible_ch_cm_co_pk_sk_observed - b.length_potentiallyaccessible_ch_cm_co_pk_sk_observed as length_potentiallyaccessible_ch_cm_co_pk_sk_observed, + a.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_a - b.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_a as length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_a, + a.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_b - b.length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_b as length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_b, + a.length_obsrvd_spawning_rearing_ch - b.length_obsrvd_spawning_rearing_ch as length_obsrvd_spawning_rearing_ch, + a.length_obsrvd_spawning_rearing_ch_accessible_a - b.length_obsrvd_spawning_rearing_ch_accessible_a as length_obsrvd_spawning_rearing_ch_accessible_a, + a.length_obsrvd_spawning_rearing_ch_accessible_b - b.length_obsrvd_spawning_rearing_ch_accessible_b as length_obsrvd_spawning_rearing_ch_accessible_b, + a.length_spawning_rearing_ch - b.length_spawning_rearing_ch as length_spawning_rearing_ch, + a.length_spawning_rearing_ch_accessible_a - b.length_spawning_rearing_ch_accessible_a as length_spawning_rearing_ch_accessible_a, + a.length_spawning_rearing_ch_accessible_b - b.length_spawning_rearing_ch_accessible_b as length_spawning_rearing_ch_accessible_b, + a.length_obsrvd_spawning_rearing_cm - b.length_obsrvd_spawning_rearing_cm as length_obsrvd_spawning_rearing_cm, + a.length_obsrvd_spawning_rearing_cm_accessible_a - b.length_obsrvd_spawning_rearing_cm_accessible_a as length_obsrvd_spawning_rearing_cm_accessible_a, + a.length_obsrvd_spawning_rearing_cm_accessible_b - b.length_obsrvd_spawning_rearing_cm_accessible_b as length_obsrvd_spawning_rearing_cm_accessible_b, + a.length_spawning_rearing_cm - b.length_spawning_rearing_cm as length_spawning_rearing_cm, + a.length_spawning_rearing_cm_accessible_a - b.length_spawning_rearing_cm_accessible_a as length_spawning_rearing_cm_accessible_a, + a.length_spawning_rearing_cm_accessible_b - b.length_spawning_rearing_cm_accessible_b as length_spawning_rearing_cm_accessible_b, + a.length_obsrvd_spawning_rearing_co - b.length_obsrvd_spawning_rearing_co as length_obsrvd_spawning_rearing_co, + a.length_obsrvd_spawning_rearing_co_accessible_a - b.length_obsrvd_spawning_rearing_co_accessible_a as length_obsrvd_spawning_rearing_co_accessible_a, + a.length_obsrvd_spawning_rearing_co_accessible_b - b.length_obsrvd_spawning_rearing_co_accessible_b as length_obsrvd_spawning_rearing_co_accessible_b, + a.length_spawning_rearing_co - b.length_spawning_rearing_co as length_spawning_rearing_co, + a.length_spawning_rearing_co_accessible_a - b.length_spawning_rearing_co_accessible_a as length_spawning_rearing_co_accessible_a, + a.length_spawning_rearing_co_accessible_b - b.length_spawning_rearing_co_accessible_b as length_spawning_rearing_co_accessible_b, + a.length_obsrvd_spawning_rearing_pk - b.length_obsrvd_spawning_rearing_pk as length_obsrvd_spawning_rearing_pk, + a.length_obsrvd_spawning_rearing_pk_accessible_a - b.length_obsrvd_spawning_rearing_pk_accessible_a as length_obsrvd_spawning_rearing_pk_accessible_a, + a.length_obsrvd_spawning_rearing_pk_accessible_b - b.length_obsrvd_spawning_rearing_pk_accessible_b as length_obsrvd_spawning_rearing_pk_accessible_b, + a.length_spawning_rearing_pk - b.length_spawning_rearing_pk as length_spawning_rearing_pk, + a.length_spawning_rearing_pk_accessible_a - b.length_spawning_rearing_pk_accessible_a as length_spawning_rearing_pk_accessible_a, + a.length_spawning_rearing_pk_accessible_b - b.length_spawning_rearing_pk_accessible_b as length_spawning_rearing_pk_accessible_b, + a.length_obsrvd_spawning_rearing_sk - b.length_obsrvd_spawning_rearing_sk as length_obsrvd_spawning_rearing_sk, + a.length_obsrvd_spawning_rearing_sk_accessible_a - b.length_obsrvd_spawning_rearing_sk_accessible_a as length_obsrvd_spawning_rearing_sk_accessible_a, + a.length_obsrvd_spawning_rearing_sk_accessible_b - b.length_obsrvd_spawning_rearing_sk_accessible_b as length_obsrvd_spawning_rearing_sk_accessible_b, + a.length_spawning_rearing_sk - b.length_spawning_rearing_sk as length_spawning_rearing_sk, + a.length_spawning_rearing_sk_accessible_a - b.length_spawning_rearing_sk_accessible_a as length_spawning_rearing_sk_accessible_a, + a.length_spawning_rearing_sk_accessible_b - b.length_spawning_rearing_sk_accessible_b as length_spawning_rearing_sk_accessible_b, + a.length_potentiallyaccessible_st - b.length_potentiallyaccessible_st as length_potentiallyaccessible_st, + a.length_potentiallyaccessible_st_observed - b.length_potentiallyaccessible_st_observed as length_potentiallyaccessible_st_observed, + a.length_potentiallyaccessible_st_accessible_a - b.length_potentiallyaccessible_st_accessible_a as length_potentiallyaccessible_st_accessible_a, + a.length_potentiallyaccessible_st_accessible_b - b.length_potentiallyaccessible_st_accessible_b as length_potentiallyaccessible_st_accessible_b, + a.length_obsrvd_spawning_rearing_st - b.length_obsrvd_spawning_rearing_st as length_obsrvd_spawning_rearing_st, + a.length_obsrvd_spawning_rearing_st_accessible_a - b.length_obsrvd_spawning_rearing_st_accessible_a as length_obsrvd_spawning_rearing_st_accessible_a, + a.length_obsrvd_spawning_rearing_st_accessible_b - b.length_obsrvd_spawning_rearing_st_accessible_b as length_obsrvd_spawning_rearing_st_accessible_b, + a.length_spawning_rearing_st - b.length_spawning_rearing_st as length_spawning_rearing_st, + a.length_spawning_rearing_st_accessible_a - b.length_spawning_rearing_st_accessible_a as length_spawning_rearing_st_accessible_a, + a.length_spawning_rearing_st_accessible_b - b.length_spawning_rearing_st_accessible_b as length_spawning_rearing_st_accessible_b, + a.length_potentiallyaccessible_wct - b.length_potentiallyaccessible_wct as length_potentiallyaccessible_wct, + a.length_potentiallyaccessible_wct_observed - b.length_potentiallyaccessible_wct_observed as length_potentiallyaccessible_wct_observed, + a.length_potentiallyaccessible_wct_accessible_a - b.length_potentiallyaccessible_wct_accessible_a as length_potentiallyaccessible_wct_accessible_a, + a.length_potentiallyaccessible_wct_accessible_b - b.length_potentiallyaccessible_wct_accessible_b as length_potentiallyaccessible_wct_accessible_b, + a.length_obsrvd_spawning_rearing_wct - b.length_obsrvd_spawning_rearing_wct as length_obsrvd_spawning_rearing_wct, + a.length_obsrvd_spawning_rearing_wct_accessible_a - b.length_obsrvd_spawning_rearing_wct_accessible_a as length_obsrvd_spawning_rearing_wct_accessible_a, + a.length_obsrvd_spawning_rearing_wct_accessible_b - b.length_obsrvd_spawning_rearing_wct_accessible_b as length_obsrvd_spawning_rearing_wct_accessible_b, + a.length_spawning_rearing_wct - b.length_spawning_rearing_wct as length_spawning_rearing_wct, + a.length_spawning_rearing_wct_accessible_a - b.length_spawning_rearing_wct_accessible_a as length_spawning_rearing_wct_accessible_a, + a.length_spawning_rearing_wct_accessible_b - b.length_spawning_rearing_wct_accessible_b as length_spawning_rearing_wct_accessible_b +from bcfishpass.wsg_linear_summary_current a +inner join bcfishpass.wsg_linear_summary_previous b +on a.watershed_group_code = b.watershed_group_code; \ No newline at end of file diff --git a/model/01_access/Makefile b/model/01_access/Makefile index 9305c33e..6dd91bc3 100644 --- a/model/01_access/Makefile +++ b/model/01_access/Makefile @@ -4,12 +4,26 @@ PSQL=psql $(DATABASE_URL) -v ON_ERROR_STOP=1 # point psql to db and stop on errors # process all watershed groups -WSGS = $(shell $(PSQL) -AtX -c "SELECT watershed_group_code FROM whse_basemapping.fwa_watershed_groups_poly") +#WSGS = $(shell $(PSQL) -AtX -c "SELECT watershed_group_code FROM whse_basemapping.fwa_watershed_groups_poly") # test with just watershed groups noted in param (misses cross-wsg barreriers) -#WSGS = $(shell $(PSQL) -AtX -c "SELECT watershed_group_code FROM bcfishpass.parameters_habitat_method") +WSGS = $(shell $(PSQL) -AtX -c "SELECT watershed_group_code FROM bcfishpass.parameters_habitat_method") -# define the make targets that flag when the table has been built -SOURCE_BARRIERS = $(patsubst sql/%.sql, .make/%, $(wildcard sql/barriers_*.sql)) +# define barrier table targets +NATURAL_BARRIERS = .make/barriers_falls \ + .make/barriers_subsurfaceflow \ + .make/barriers_user_definite \ + .make/barriers_gradient_05 \ + .make/barriers_gradient_10 \ + .make/barriers_gradient_15 \ + .make/barriers_gradient_20 \ + .make/barriers_gradient_25 \ + .make/barriers_gradient_30 +ANTH_BARRIERS = .make/barriers_anthropogenic \ + .make/barriers_dams \ + .make/barriers_dams_hydro \ + .make/barriers_pscis + +# SOURCE_BARRIERS = $(patsubst sql/%.sql, .make/%, $(wildcard sql/barriers_*.sql)) # access models - combine barriers into tables of per-species scenarios ACCESS_MODELS = $(patsubst sql/%.sql, .make/%, $(wildcard sql/model_access_*.sql)) @@ -94,10 +108,16 @@ clean: # ----- # LOAD BARRIER TYPE TABLES # ----- -# Create standardized barrier tables, one per type of barrier. -# Process every file that matches the pattern sql/barriers_%.sql -$(SOURCE_BARRIERS): .make/barriers_%: sql/barriers_%.sql \ - .make/crossings \ +# Create standardized barrier tables for natural barriers +$(NATURAL_BARRIERS): sql/barriers_falls.sql \ + sql/barriers_subsurfaceflow.sql \ + sql/barriers_user_definite.sql \ + sql/barriers_gradient_05.sql \ + sql/barriers_gradient_10.sql \ + sql/barriers_gradient_15.sql \ + sql/barriers_gradient_20.sql \ + sql/barriers_gradient_25.sql \ + sql/barriers_gradient_30.sql \ .make/falls \ .make/gradient_barriers $(eval BARRIERTYPE=$(subst .make/barriers_,,$@)) @@ -110,6 +130,22 @@ $(SOURCE_BARRIERS): .make/barriers_%: sql/barriers_%.sql \ parallel $(PSQL) -f sql/barriers_$(BARRIERTYPE).sql -v wsg={1} ::: $(WSGS) touch $@ +# Create standardized barrier tables for anthropogenic barriers +$(ANTH_BARRIERS): .make/crossings \ + sql/barriers_anthropogenic.sql \ + sql/barriers_dams.sql \ + sql/barriers_dams_hydro.sql \ + sql/barriers_pscis.sql + $(eval BARRIERTYPE=$(subst .make/barriers_,,$@)) + # create the table if it does not exist + echo "select bcfishpass.create_barrier_table(:'barriertype')" | \ + $(PSQL) -v barriertype=$(BARRIERTYPE) + # clear barrier table + $(PSQL) -c "truncate bcfishpass.barriers_$(BARRIERTYPE)" + # load data to barrier table in parallel + parallel $(PSQL) -f sql/barriers_$(BARRIERTYPE).sql -v wsg={1} ::: $(WSGS) + touch $@ + # ----- # LOAD PER-SPECIES BARRIER TABLES # ----- @@ -118,7 +154,7 @@ $(SOURCE_BARRIERS): .make/barriers_%: sql/barriers_%.sql \ # the lowest in the system) # Note that observations are a requirement because they can be used to cancel natural barriers $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \ - $(SOURCE_BARRIERS) \ + $(NATURAL_BARRIERS) \ .make/observations $(eval BARRIERTYPE=$(subst .make/model_access_,,$@)) # create table if it does not exist @@ -156,7 +192,7 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \ # INDEX CROSSINGS # ------------ # for crossings table, barriers_anthropogenic, note what barriers are upstream/downstream of each other -.make/index_crossings: .make/barriers_anthropogenic +.make/index_crossings: $(ANTH_BARRIERS) # note all crossings downstream of a crossing $(PSQL) -c "truncate bcfishpass.crossings_dnstr_crossings" parallel --no-run-if-empty \ @@ -234,6 +270,7 @@ $(ACCESS_MODELS): .make/model_access_%: sql/model_access_%.sql \ touch ../02_habitat_linear/channel_width/.make/channel_width mkdir -p ../02_habitat_linear/discharge/.make touch ../02_habitat_linear/discharge/.make/discharge + touch $@ # ------------------------------------------------------------------------------------- # intermediate target for proessing all of above, but not the model_access script diff --git a/model/01_access/sql/load_map_mad_cw.sql b/model/01_access/sql/load_map_mad_cw.sql index f68b4e25..81204a53 100644 --- a/model/01_access/sql/load_map_mad_cw.sql +++ b/model/01_access/sql/load_map_mad_cw.sql @@ -1,44 +1,4 @@ -- load pre-computed precip/discharge/channel width from file - --- drop table if exists bcfishpass.discharge; -- can't drop this, streams_vw requires discharge - -create table if not exists bcfishpass.discharge -( - linear_feature_id bigint primary key, - watershed_group_code text , - mad_mm double precision, - mad_m3s double precision -); -truncate bcfishpass.discharge; \copy bcfishpass.discharge FROM data/discharge.csv delimiter ',' csv header; - - --- drop table if exists bcfishpass.mean_annual_precip; -- as above, can't drop this - -create table if not exists bcfishpass.mean_annual_precip ( - id integer primary key, - wscode_ltree ltree , - localcode_ltree ltree , - watershed_group_code text , - area bigint , - map integer, - map_upstream integer -); -truncate bcfishpass.mean_annual_precip; -create index on bcfishpass.mean_annual_precip using gist (localcode_ltree); -create index on bcfishpass.mean_annual_precip using btree (localcode_ltree); -create index on bcfishpass.mean_annual_precip using gist (wscode_ltree); -create index on bcfishpass.mean_annual_precip using btree (wscode_ltree); - \copy bcfishpass.mean_annual_precip FROM data/mean_annual_precip.csv delimiter ',' csv header; - - --- drop table if exists bcfishpass.channel_width; -- can't drop this - -create table if not exists bcfishpass.channel_width ( - linear_feature_id bigint primary key, - channel_width_source text , - channel_width double precision -); -truncate bcfishpass.channel_width; \copy bcfishpass.channel_width FROM data/channel_width.csv delimiter ',' csv header; \ No newline at end of file