Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter sites streamorder meas #56

Merged
merged 16 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
40 changes: 34 additions & 6 deletions 2_process/src/sites_along_waterbody.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#' @param sw_sites_sf sf object of sf sites and measurements
#' @param sites_sf sf object of sf sites and measurements
#' @param waterbody_sf sf object of water body that will be buffered
#' @param lake_waterbody whether water body is lake or not

sites_along_waterbody <- function(sw_sites_sf, waterbody_sf, lake_waterbody = FALSE){
sites_along_waterbody <- function(sites_sf, waterbody_sf, lake_waterbody = FALSE){


sw_sites <- sw_sites_sf %>%
select(site_no, geometry) %>%
sf::st_as_sf()
sites_sf <- sites_sf %>%
select(site_no)

## running st_union for the tributary shp because it smooths the buffer and polygons are overlap less.
## Not feasible for lakes due to specific selection of columns
Expand All @@ -19,10 +18,39 @@ sites_along_waterbody <- function(sw_sites_sf, waterbody_sf, lake_waterbody = FA
summarize(geometry = sf::st_union(geometry)) %>%
sf::st_buffer(dist = units::set_units(250, m))
msleckman marked this conversation as resolved.
Show resolved Hide resolved
}
filtered_sites <- st_join(sw_sites, waterbody_buffered, left = FALSE) %>%
filtered_sites <- st_join(sites_sf, waterbody_buffered, left = FALSE) %>%
pull(site_no) %>%
unique()

return(filtered_sites)

}

#' @title add_stream_order
#' @description function that joins nwis site info to nwis sw data and adds stream order col
#' @param nwis_sw_data nwis sw data pulled from dataRetrieval::readNWISdata() or similar dataRetrieval functions
#' @param sites_sf sf object of sf sites and measurements
#' @param join_site_col col to site col to join sites_sf with nwis_sw_data. defaults is site_no as that is expected to be common between both dfs
#' @param sites_along_streamorder3 vector of site_no that are along stream order 3 streams. Output of sites_along_waterbody()
#' @param sites_along_lake vector of sites that are adjacent to saline lakes. Output of sites_along_waterbody

add_stream_order <- function(nwis_sw_data, sites_sf, join_site_col = 'site_no', sites_along_streamorder3, sites_along_lake){

nwis_sw_data %>%
left_join(sites_sf, by = join_site_col) %>%
## filtering sites that are surface level stream (all ST), lake (LK), or wetland (WE) sites
filter(site_tp_cd %in% c('LK','WE') | grepl('ST',site_tp_cd)) %>%
msleckman marked this conversation as resolved.
Show resolved Hide resolved
## create stream_order_category col depending on site type & match with sites stream order/lake vector targets
mutate(
stream_order_category = case_when(
grepl('^ST',site_tp_cd) & site_no %in% sites_along_streamorder3 ~ 'along SO 3+',
site_tp_cd == 'LK' | site_no %in% sites_along_lake ~ 'along lake',
TRUE ~ 'not along SO 3+')) %>%
## used geometry from sites_in_watersheds_sf to get spatial info
st_as_sf() %>%
## grab coords
mutate(lon = st_coordinates(.)[,1], lat = st_coordinates(.)[,2]) %>%
## remove geometry col
st_drop_geometry()

msleckman marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 2 additions & 2 deletions 2_process_lakes_tribs.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source('2_process/src/scope_lake_tributaries.R')
## 1. p2_saline_lakes_sf : final sf object representing our focal saline lakes. Fun process_saline_lakes_sf() is
## not generalized, and you will find input-specific commands and processing steps ther
##
## 2. p2_lake_tributaries : polylines object representing all upstream tributaries of the lakes - creating using nhdplusTools Get_UT() function (UT:upstream)
## 2. p2_lake_tributaries_so3 : polylines object representing all upstream tributaries of the lakes - creating using nhdplusTools Get_UT() function (UT:upstream). Filtered to so3
## 3. p2_lake_tributaries_cat: polygon object representing all upstream nhd catchments. Current target is not used downstream

p2_lakes_tribs_targets_list <- list(
Expand All @@ -30,7 +30,7 @@ p2_lakes_tribs_targets_list <- list(
# Basin Flowlines Processing #
## Get only tributaries of the Lakes using get_UT function
tar_target(
p2_lake_tributaries,
p2_lake_tributaries_so3,
scope_lake_tributaries(fline_network = p1_lake_flowlines_huc8_sf,
lakes_sf = p2_saline_lakes_sf,
buffer_dist = 10000,
Expand Down
36 changes: 19 additions & 17 deletions 2_process_sw_gw_site_data.R → 2_process_site_data.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source('2_process/src/sites_along_waterbody.R')

p2_sw_gw_site_targets_list <- list(
p2_site_targets_list <- list(

## simplified vrn of all site in watersheds(regardless of whether there is relevant data at that site) + all site types
tar_target(p2_site_in_watersheds_sf,
Expand All @@ -23,7 +23,7 @@ p2_sw_gw_site_targets_list <- list(
## getting all sites along lake
tar_target(p2_sw_streamorder3_sites,
sites_along_waterbody(p2_site_in_watersheds_sf,
p2_lake_tributaries,
p2_lake_tributaries_so3,
lake_waterbody = FALSE)
),

Expand All @@ -35,25 +35,27 @@ p2_sw_gw_site_targets_list <- list(

),

## get just sw sites with outputed data for 2000-2020 (timeframe to change) with stream order category column
## get just sw sites with outputed data for 2000-2022 with stream order category column
tar_target(p2_nwis_dv_sw_data,
p1_nwis_dv_sw_data %>%
left_join(p2_site_in_watersheds_sf, by = 'site_no') %>%
filter(site_tp_cd %in% c('LK','WE') | grepl('ST',site_tp_cd)) %>%
mutate(stream_order_category = case_when(
grepl(site_tp_cd,'^ST') & site_no %in% p2_sw_streamorder3_sites ~ 'along SO 3+',
site_tp_cd == 'LK' | site_no %in% p2_sw_in_lake_sites ~ 'along lake',
TRUE ~ 'not along SO 3+'
)) %>%
st_as_sf() %>%
mutate(lon = st_coordinates(.)[,1], lat = st_coordinates(.)[,2]) %>%
st_drop_geometry() %>%
## quickly re-organizing cols
add_stream_order(nwis_sw_data = p1_nwis_dv_sw_data,
sites_sf = p2_site_in_watersheds_sf,
join_site_col = 'site_no',
sites_along_streamorder3 = p2_sw_streamorder3_sites,
sites_along_lake = p2_sw_in_lake_sites) %>%
## quickly re-organizing cols so that measurements cols come after non-measurement cols
select(!starts_with('X_'),starts_with('X_'))

)
),

## add stream order to sw sites
tar_target(p2_nwis_meas_sw_data,
add_stream_order(nwis_sw_data = p1_nwis_meas_sw_data,
sites_sf = p2_site_in_watersheds_sf,
join_site_col = 'site_no',
sites_along_streamorder3 = p2_sw_streamorder3_sites,
sites_along_lake = p2_sw_in_lake_sites)
)


)

# # SW data -----------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion 3_viz_prep.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ p3_prep_viz_targets_list <- list(
## This takes a long time because rmapshaper::ms_simplify() is very slow for me when simplifying flowlines
tar_target(
p3_lake_tributaries,
p2_lake_tributaries %>%
p2_lake_tributaries_so3 %>%
rmapshaper::ms_simplify() %>%
mutate(label = paste0("Stream: ",
ifelse(gnis_name == " ", "No GNIS name/ID",
Expand Down
18 changes: 11 additions & 7 deletions 4_export.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@ p4_export_csv_targets_list <- list(

# exports dfs -------------------------------------------------------------------

## writing daily sw values to a single csv
## writing daily sw values to a single rds
tar_target(
p4_nwis_dv_sw_data_rds,
write_rds(p2_nwis_dv_sw_data,
"4_reports/out/p1_nwis_dv_sw_data.rds"),
format = "file"
),

## writing daily gw values to a single csv
## writing daily gw values to a single rds
tar_target(
p4_nwis_dv_gw_data_rds,
write_rds(p2_nwis_dv_gw_data,
"4_reports/out/p1_nwis_dv_gw_data.rds"),
format = "file"
),

## writing field measurement sw values to a single csv
## writing field measurement sw values to a single rds
tar_target(
p4_nwis_meas_sw_data_rds,
readr::write_csv(p1_nwis_meas_sw_data,
'4_reports/out/p1_nwis_meas_sw_data.csv')
'4_reports/out/p1_nwis_meas_sw_data.rds')
),

## writing field measurement gw values to a single csv
## writing field measurement gw values to a single rds
tar_target(
p4_nwis_meas_gw_data_rds,
readr::write_csv(p1_nwis_meas_gw_data,
'4_reports/out/p1_nwis_meas_gw_data.csv')
'4_reports/out/p1_nwis_meas_gw_data.rds')
),

## writing iv data to lake specific csvs
Expand All @@ -53,18 +53,22 @@ tar_target(

# export shapefiles -------------------------------------------------------

# exporting saline lakes shp
tar_target(p4_saline_lakes_shp,
write_shp(p2_saline_lakes_sf,
'out_shp/saline_lakes.shp'),
format = 'file'
),

## exporting flines shp
## Commenting out because taking too long
# tar_target(p4_lake_tributaries_shp,
# write_shp(p2_lake_tributaries,
# write_shp(p2_lake_tributaries_so3,
# 'out_shp/p2_lake_tributaries.shp'),
# format = 'file'
# ),

# exporting saline lakes shp
tar_target(p4_lake_watersheds_shp,
write_shp(p2_lake_watersheds_dissolved,
'out_shp/p2_lake_watersheds.shp'),
Expand Down
4 changes: 2 additions & 2 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source("1_fetch_nwis.R")

source("2_process_lakes_tribs.R")
source("2_process_watershed_boundary.R")
source('2_process_sw_gw_site_data.R')
source('2_process_site_data.R')

source("3_viz_prep.R")
source("3_visualize.R")
Expand All @@ -28,6 +28,6 @@ dir.create('1_fetch/in/states_shp', showWarnings = FALSE)
# Return the complete list of targets
c(p0_targets_list,
p1_sp_targets_list, p1_feeback_xlsx_targets_list, p1_nw_targets_list,
p2_watershed_boundary_targets_list, p2_lakes_tribs_targets_list, p2_sw_gw_site_targets_list,
p2_watershed_boundary_targets_list, p2_lakes_tribs_targets_list, p2_site_targets_list,
p3_prep_viz_targets_list, p3_viz_targets_list,
p4_export_csv_targets_list, p4_reports_targets_list)