Skip to content

Commit

Permalink
Merge branch 'main' of github.com:smnorris/bcfishpass
Browse files Browse the repository at this point in the history
* 'main' of github.com:smnorris/bcfishpass:
  Log model runs and results summaries (#439)
  • Loading branch information
smnorris committed Jan 5, 2024
2 parents 37bc624 + 4b2d468 commit 425da58
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,57 @@
-- - accessible (per model)
-- - with spawning rearing habitat upstream (per model)

drop view if exists bcfishpass.crossings_summary_vw;
create view bcfishpass.crossings_summary_vw as
CREATE FUNCTION bcfishpass.wsg_crossings_summary()
RETURNS table (
watershed_group_code text,
crossing_feature_type text,
n_crossings_total integer,
n_passable_total integer,
n_barriers_total integer,
n_potential_total integer,
n_unknown_total integer,
n_barriers_accessible_bt integer,
n_potential_accessible_bt integer,
n_unknown_accessible_bt integer,
n_barriers_accessible_ch_cm_co_pk_sk integer,
n_potential_accessible_ch_cm_co_pk_sk integer,
n_unknown_accessible_ch_cm_co_pk_sk integer,
n_barriers_accessible_st integer,
n_potential_accessible_st integer,
n_unknown_accessible_st integer,
n_barriers_accessible_wct integer,
n_potential_accessible_wct integer,
n_unknown_accessible_wct integer,
n_barriers_habitat_bt integer,
n_potential_habitat_bt integer,
n_unknown_habitat_bt integer,
n_barriers_habitat_ch integer,
n_potential_habitat_ch integer,
n_unknown_habitat_ch integer,
n_barriers_habitat_cm integer,
n_potential_habitat_cm integer,
n_unknown_habitat_cm integer,
n_barriers_habitat_co integer,
n_potential_habitat_co integer,
n_unknown_habitat_co integer,
n_barriers_habitat_pk integer,
n_potential_habitat_pk integer,
n_unknown_habitat_pk integer,
n_barriers_habitat_sk integer,
n_potential_habitat_sk integer,
n_unknown_habitat_sk integer,
n_barriers_habitat_salmon integer,
n_potential_habitat_salmon integer,
n_unknown_habitat_salmon integer,
n_barriers_habitat_st integer,
n_potential_habitat_st integer,
n_unknown_habitat_st integer,
n_barriers_habitat_wct integer,
n_potential_habitat_wct integer,
n_unknown_habitat_wct integer
)
AS $$

select
watershed_group_code,
crossing_feature_type,
Expand Down Expand Up @@ -152,3 +201,4 @@ select
from bcfishpass.crossings_vw
group by watershed_group_code, crossing_feature_type;

$$ LANGUAGE SQL;
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,76 @@
-- - spawning or rearing habitat not isolated by dam or a known pscis barrier (accessible_a)
-- - spawning or rearing habitat not isolated by all anthropogenic barriers (observed and modelled) (accessible_b)


drop view if exists bcfishpass.wsg_linear_summary_vw;

create view bcfishpass.wsg_linear_summary_vw as
CREATE FUNCTION bcfishpass.wsg_linear_summary()
RETURNS table (
watershed_group_code text,
length_total numeric,
length_potentiallyaccessible_bt numeric,
length_potentiallyaccessible_bt_observed numeric,
length_potentiallyaccessible_bt_accessible_a numeric,
length_potentiallyaccessible_bt_accessible_b numeric,
length_obsrvd_spawning_rearing_bt numeric,
length_obsrvd_spawning_rearing_bt_accessible_a numeric,
length_obsrvd_spawning_rearing_bt_accessible_b numeric,
length_spawning_rearing_bt numeric,
length_spawning_rearing_bt_accessible_a numeric,
length_spawning_rearing_bt_accessible_b numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk_observed numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_a numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_b numeric,
length_obsrvd_spawning_rearing_ch numeric,
length_obsrvd_spawning_rearing_ch_accessible_a numeric,
length_obsrvd_spawning_rearing_ch_accessible_b numeric,
length_spawning_rearing_ch numeric,
length_spawning_rearing_ch_accessible_a numeric,
length_spawning_rearing_ch_accessible_b numeric,
length_obsrvd_spawning_rearing_cm numeric,
length_obsrvd_spawning_rearing_cm_accessible_a numeric,
length_obsrvd_spawning_rearing_cm_accessible_b numeric,
length_spawning_rearing_cm numeric,
length_spawning_rearing_cm_accessible_a numeric,
length_spawning_rearing_cm_accessible_b numeric,
length_obsrvd_spawning_rearing_co numeric,
length_obsrvd_spawning_rearing_co_accessible_a numeric,
length_obsrvd_spawning_rearing_co_accessible_b numeric,
length_spawning_rearing_co numeric,
length_spawning_rearing_co_accessible_a numeric,
length_spawning_rearing_co_accessible_b numeric,
length_obsrvd_spawning_rearing_pk numeric,
length_obsrvd_spawning_rearing_pk_accessible_a numeric,
length_obsrvd_spawning_rearing_pk_accessible_b numeric,
length_spawning_rearing_pk numeric,
length_spawning_rearing_pk_accessible_a numeric,
length_spawning_rearing_pk_accessible_b numeric,
length_obsrvd_spawning_rearing_sk numeric,
length_obsrvd_spawning_rearing_sk_accessible_a numeric,
length_obsrvd_spawning_rearing_sk_accessible_b numeric,
length_spawning_rearing_sk numeric,
length_spawning_rearing_sk_accessible_a numeric,
length_spawning_rearing_sk_accessible_b numeric,
length_potentiallyaccessible_st numeric,
length_potentiallyaccessible_st_observed numeric,
length_potentiallyaccessible_st_accessible_a numeric,
length_potentiallyaccessible_st_accessible_b numeric,
length_obsrvd_spawning_rearing_st numeric,
length_obsrvd_spawning_rearing_st_accessible_a numeric,
length_obsrvd_spawning_rearing_st_accessible_b numeric,
length_spawning_rearing_st numeric,
length_spawning_rearing_st_accessible_a numeric,
length_spawning_rearing_st_accessible_b numeric,
length_potentiallyaccessible_wct numeric,
length_potentiallyaccessible_wct_observed numeric,
length_potentiallyaccessible_wct_accessible_a numeric,
length_potentiallyaccessible_wct_accessible_b numeric,
length_obsrvd_spawning_rearing_wct numeric,
length_obsrvd_spawning_rearing_wct_accessible_a numeric,
length_obsrvd_spawning_rearing_wct_accessible_b numeric,
length_spawning_rearing_wct numeric,
length_spawning_rearing_wct_accessible_a numeric,
length_spawning_rearing_wct_accessible_b numeric
)
AS $$

with accessible as
(
Expand Down Expand Up @@ -209,4 +275,6 @@ select

from accessible a
left outer join spawning_rearing_observed o on a.watershed_group_code = o.watershed_group_code
left outer join spawning_rearing_modelled m on a.watershed_group_code = m.watershed_group_code
left outer join spawning_rearing_modelled m on a.watershed_group_code = m.watershed_group_code;

$$ LANGUAGE SQL;
133 changes: 133 additions & 0 deletions db/tables/log.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
--do not generally drop or truncate this table - we want to retain this data
--drop table bcfishpass.log cascade;
--truncate bcfishpass.log cascade;
create table if not exists bcfishpass.log (
model_run_id serial primary key,
model_type text not null,
date_completed timestamp not null default CURRENT_TIMESTAMP,
git_id bytea not null,
check (model_type in ('LINEAR','LATERAL'))
);

-- usage:
-- insert into bcfishpass.log (model_type, git_id) values ('LINEAR', decode('feac3689cef93cc02a4cb4ac6a0fdadebe980f4d', 'hex')) RETURNING model_run_id;

create table if not exists bcfishpass.wsg_linear_summary (
model_run_id integer references bcfishpass.log(model_run_id),
watershed_group_code text,
length_total numeric,
length_potentiallyaccessible_bt numeric,
length_potentiallyaccessible_bt_observed numeric,
length_potentiallyaccessible_bt_accessible_a numeric,
length_potentiallyaccessible_bt_accessible_b numeric,
length_obsrvd_spawning_rearing_bt numeric,
length_obsrvd_spawning_rearing_bt_accessible_a numeric,
length_obsrvd_spawning_rearing_bt_accessible_b numeric,
length_spawning_rearing_bt numeric,
length_spawning_rearing_bt_accessible_a numeric,
length_spawning_rearing_bt_accessible_b numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk_observed numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_a numeric,
length_potentiallyaccessible_ch_cm_co_pk_sk_accessible_b numeric,
length_obsrvd_spawning_rearing_ch numeric,
length_obsrvd_spawning_rearing_ch_accessible_a numeric,
length_obsrvd_spawning_rearing_ch_accessible_b numeric,
length_spawning_rearing_ch numeric,
length_spawning_rearing_ch_accessible_a numeric,
length_spawning_rearing_ch_accessible_b numeric,
length_obsrvd_spawning_rearing_cm numeric,
length_obsrvd_spawning_rearing_cm_accessible_a numeric,
length_obsrvd_spawning_rearing_cm_accessible_b numeric,
length_spawning_rearing_cm numeric,
length_spawning_rearing_cm_accessible_a numeric,
length_spawning_rearing_cm_accessible_b numeric,
length_obsrvd_spawning_rearing_co numeric,
length_obsrvd_spawning_rearing_co_accessible_a numeric,
length_obsrvd_spawning_rearing_co_accessible_b numeric,
length_spawning_rearing_co numeric,
length_spawning_rearing_co_accessible_a numeric,
length_spawning_rearing_co_accessible_b numeric,
length_obsrvd_spawning_rearing_pk numeric,
length_obsrvd_spawning_rearing_pk_accessible_a numeric,
length_obsrvd_spawning_rearing_pk_accessible_b numeric,
length_spawning_rearing_pk numeric,
length_spawning_rearing_pk_accessible_a numeric,
length_spawning_rearing_pk_accessible_b numeric,
length_obsrvd_spawning_rearing_sk numeric,
length_obsrvd_spawning_rearing_sk_accessible_a numeric,
length_obsrvd_spawning_rearing_sk_accessible_b numeric,
length_spawning_rearing_sk numeric,
length_spawning_rearing_sk_accessible_a numeric,
length_spawning_rearing_sk_accessible_b numeric,
length_potentiallyaccessible_st numeric,
length_potentiallyaccessible_st_observed numeric,
length_potentiallyaccessible_st_accessible_a numeric,
length_potentiallyaccessible_st_accessible_b numeric,
length_obsrvd_spawning_rearing_st numeric,
length_obsrvd_spawning_rearing_st_accessible_a numeric,
length_obsrvd_spawning_rearing_st_accessible_b numeric,
length_spawning_rearing_st numeric,
length_spawning_rearing_st_accessible_a numeric,
length_spawning_rearing_st_accessible_b numeric,
length_potentiallyaccessible_wct numeric,
length_potentiallyaccessible_wct_observed numeric,
length_potentiallyaccessible_wct_accessible_a numeric,
length_potentiallyaccessible_wct_accessible_b numeric,
length_obsrvd_spawning_rearing_wct numeric,
length_obsrvd_spawning_rearing_wct_accessible_a numeric,
length_obsrvd_spawning_rearing_wct_accessible_b numeric,
length_spawning_rearing_wct numeric,
length_spawning_rearing_wct_accessible_a numeric,
length_spawning_rearing_wct_accessible_b numeric
);

create table if not exists bcfishpass.wsg_crossing_summary (
model_run_id integer references bcfishpass.log(model_run_id),
watershed_group_code text,
crossing_feature_type text,
n_crossings_total integer,
n_passable_total integer,
n_barriers_total integer,
n_potential_total integer,
n_unknown_total integer,
n_barriers_accessible_bt integer,
n_potential_accessible_bt integer,
n_unknown_accessible_bt integer,
n_barriers_accessible_ch_cm_co_pk_sk integer,
n_potential_accessible_ch_cm_co_pk_sk integer,
n_unknown_accessible_ch_cm_co_pk_sk integer,
n_barriers_accessible_st integer,
n_potential_accessible_st integer,
n_unknown_accessible_st integer,
n_barriers_accessible_wct integer,
n_potential_accessible_wct integer,
n_unknown_accessible_wct integer,
n_barriers_habitat_bt integer,
n_potential_habitat_bt integer,
n_unknown_habitat_bt integer,
n_barriers_habitat_ch integer,
n_potential_habitat_ch integer,
n_unknown_habitat_ch integer,
n_barriers_habitat_cm integer,
n_potential_habitat_cm integer,
n_unknown_habitat_cm integer,
n_barriers_habitat_co integer,
n_potential_habitat_co integer,
n_unknown_habitat_co integer,
n_barriers_habitat_pk integer,
n_potential_habitat_pk integer,
n_unknown_habitat_pk integer,
n_barriers_habitat_sk integer,
n_potential_habitat_sk integer,
n_unknown_habitat_sk integer,
n_barriers_habitat_salmon integer,
n_potential_habitat_salmon integer,
n_unknown_habitat_salmon integer,
n_barriers_habitat_st integer,
n_potential_habitat_st integer,
n_unknown_habitat_st integer,
n_barriers_habitat_wct integer,
n_potential_habitat_wct integer,
n_unknown_habitat_wct integer
);
10 changes: 10 additions & 0 deletions model/02_habitat_linear/habitat_linear.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ done
# with linear model processing complete, refresh materialized views
$PSQL -c "refresh materialized view bcfishpass.crossings_upstr_barriers_per_model_vw"
$PSQL -c "refresh materialized view bcfishpass.crossings_vw"

# Finished processing!
# Now add model run to log, returning the id
git_id=$(git rev-parse HEAD)
model_run_id=$($PSQL -qtAX -c "insert into bcfishpass.log (model_type, git_id) VALUES ('LINEAR', decode('$git_id', 'hex')) returning model_run_id")

# log summaries (todo - call these functions as a trigger on adding row to bcfishpass.log table rather than calling here)
$PSQL -c "insert into bcfishpass.wsg_linear_summary select $model_run_id as model_run_id, * from bcfishpass.wsg_linear_summary()"
$PSQL -c "insert into bcfishpass.wsg_crossing_summary select $model_run_id as model_run_id, * from bcfishpass.wsg_crossing_summary()"

0 comments on commit 425da58

Please sign in to comment.