From 799c214d5d0fe94adb161f50bcbdc927c5a2500f Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Tue, 29 Nov 2022 20:06:01 -0800 Subject: [PATCH 01/16] forgot to push saline lakes mapping scpt --- mapping/saline_lakes_mapping_script.R | 417 ++++++++++++++++++++++++++ 1 file changed, 417 insertions(+) create mode 100644 mapping/saline_lakes_mapping_script.R diff --git a/mapping/saline_lakes_mapping_script.R b/mapping/saline_lakes_mapping_script.R new file mode 100644 index 0000000..6fe5cb0 --- /dev/null +++ b/mapping/saline_lakes_mapping_script.R @@ -0,0 +1,417 @@ + +# libs -------------------------------------------------------------------- +library(ggplot2) +library(tidyverse) +library(sf) +library(mapview) +library(maps) +library(ggrepel) + +# read in data ----------------------------------------------------------- + +## target load for local pipeline - IGNORE +targets::tar_load(p4_nwis_dv_gw_data_rds) +targets::tar_load(p4_nwis_dv_sw_data_rds) +targets::tar_load(p3_saline_lakes_sf) +targets::tar_load(p2_lake_watersheds_dissolved) +targets::tar_load(p2_saline_lakes_sf) +targets::tar_load(p2_lake_tributaries) +targets::tar_load(p1_nwis_meas_sw_data) +targets::tar_load(p1_nwis_meas_gw_data) +## -- + +saline_lakes <- st_read('saline_lakes.shp') +lake_watersheds <- st_read('watersheds.shp') + +nwis_dv_gw_data <- readRDS("p1_nwis_dv_gw_data.rds") +nwis_dv_sw_data <- readRDS("p1_nwis_dv_sw_data.rds") + +wq_data <- readRDS('harmonized_wqp_data_added_cols.rds') +wq_sites <- readRDS('harmonized_wqp_sites.rds') + +wq_data$MonitoringLocationIdentifier %>% unique() %>% length() +wq_sites$MonitoringLocationIdentifier %>% unique() %>% length() + + +# check dims -------------------------------------------------------------- + +nwis_dv_gw_data %>% dim() +nwis_dv_sw_data %>% dim() + +nwis_dv_sw_data$dateTime %>% max() +nwis_dv_gw_data$dateTime %>% max() + +# general items: ---------------------------------------------------------- + +us_sf <- st_as_sf(maps::map('state', plot=F, fill=T)) %>% st_transform(4326) + +labels_df <- saline_lakes %>% + mutate(label = gsub('Lake: ','',label)) %>% + select(label, X, Y) %>% st_drop_geometry() + +map_bbox <- st_bbox(lake_watersheds) + +# map fun ----------------------------------------------------------------- + +## general map that includes, lakes, labels +general_map <- function(watershed_sf, lake_sf, labels_df, basemap, map_bbox, title = 'Saline Lakes and Watersheds'){ + + map <- ggplot()+ + geom_sf(data = basemap, fill = 'white', color = 'grey', alpha = 0.1)+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ + ggrepel::geom_text_repel(data = labels_df, aes(X, Y, label = label),colour = "black", + size = 3, nudge_y = 0.1, nudge_x = 0.15, + segment.colour="black", min.segment.length = 1.5)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_classic()+ + labs(title = title)+ + theme(plot.title = element_text(size = 10, face= 'bold'), + legend.text = element_text (size = 7), + legend.title = element_text (size = 9), + axis.title.x = element_blank(), + axis.title.y = element_blank() + )+ + guides(fill = guide_colorsteps(direction = 'horizontal')) + + return(map) + +} + +## mapping function for sw and wq - shape aesthetic specified + +map_sites_sw_wq <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, title = '', shape_col, color_col){ + + map <- ggplot()+ + geom_sf(data = basemap, fill = 'white', color = 'grey', alpha = 0.1)+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_sf(data = sites_sf, + aes(geometry = geometry, color = .data[[color_col]], shape = .data[[shape_col]]), size = 1)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_classic()+ + scale_color_steps()+ + labs(title = title)+ + theme(plot.title = element_text(size = 10, face= 'bold'), + legend.text = element_text (size = 7), + legend.title = element_text (size = 9), + axis.title.x = element_blank(), + axis.title.y = element_blank() + ) + + return(map) + +} + +## mapping gw sites - no shape specified +map_dv_sites_gw <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, color_col, title = ''){ + + map <- ggplot()+ + geom_sf(data = basemap, fill = 'white')+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_sf(data = sites_sf, + aes(geometry = geometry, color = .data[[color_col]]), size = 1)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_classic()+ + labs(title = title)+ + scale_color_steps(low = 'orange', high = 'firebrick')+ + theme(plot.title = element_text(size = 10, face= 'bold'), + legend.text = element_text (size = 7), + legend.title = element_text (size = 9), + axis.title.x = element_blank(), + axis.title.y = element_blank())+ + guides(fill = guide_colorsteps(direction = 'horizontal')) + + return(map) + +} + + + +# General map with labels ------------------------------------------------------------ + +gen_map <- general_map(watershed_sf = lake_watersheds, + lake_sf = saline_lakes, + labels_df = labels_df, + basemap = us_sf, + map_bbox = map_bbox, + title = 'Focal Saline Lakes and Watersheds in the Great Basin') + + +ggsave(filename = 'gen_map.png', + device= 'png', + plot =gen_map, + path = 'mapping/') + +# Site maps outputs ------------------------------------------------------------ + + +#### mapping sw sites #### + +sw_sites_sf_00060 <- nwis_dv_sw_data %>% + select(-contains(c('00065','..2..'))) %>% + ## removing where both valid measurements of 00060 are na + filter(!(is.na(X_00060_00003) & is.na(X_00060_00011))) %>% + ## NOTE - filtering out stream order < 3 and recoded for graph purposes + filter(stream_order_category != "not along SO 3+") %>% + mutate(stream_order_category = recode(stream_order_category, + "along SO 3+" = 'Along stream', + "along lake" = 'Along lake')) %>% + mutate(dateTime = as.Date(dateTime)) %>% + group_by(site_no, lake_w_state,lon,lat, stream_order_category) %>% + summarize(nmbr_observations = n(), + min_date = min(dateTime), + max_date = max(dateTime)) %>% + st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes)) %>% arrange(desc(nmbr_observations)) + +sw_data_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, lake_sf = saline_lakes, + sites_sf = sw_sites_sf_00060, basemap = us_sf, + map_bbox = map_bbox, title = 'Active USGS-NWIS SW gages measuring daily stream flow data', + shape_col = 'stream_order_category',color_col = 'nmbr_observations')+ + labs(color = 'Number of observations at gage site', shape = 'Waterbody type') + + +ggsave(filename = 'sw_data_map.png', + device= 'png', + plot =sw_data_map, + path = 'mapping/') + +#### mapping gw sites #### + +gw_sites_sf_72019 <- nwis_dv_gw_data %>% + select(-contains(c('..2..'))) %>% + ## removing where both valid measurements of 00060 are na + filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00003) & is.na(X_72019_00008) & is.na(X_72019_31200))) %>% + # filter(stream_order_category != "not along SO 3+") %>% + mutate(dateTime = as.Date(dateTime)) %>% + group_by(site_no, lake_w_state, lon, lat) %>% + summarize(nmbr_observations = n(), + min_date = min(dateTime), + max_date = max(dateTime)) %>% + # ungroup() %>% + ## adding col for categorizing obser nmbrs + mutate(observations_classified = case_when(nmbr_observations < 100 ~ '<100', + nmbr_observations >= 100 & nmbr_observations < 500 ~ '100 - 500', + nmbr_observations >= 500 & nmbr_observations < 2000 ~ '500 - 2000', + nmbr_observations >= 2000 & nmbr_observations < 5000 ~ '2000 - 5000', + nmbr_observations >= 5000 & nmbr_observations < 8000 ~ '5000 - 8000', + TRUE ~ '> 8000')) %>% + st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes)) + +gw_data_map <- map_dv_sites_gw(watershed_sf = lake_watersheds,lake_sf = saline_lakes, + sites_sf = gw_sites_sf_72019, + basemap = us_sf, + map_bbox = map_bbox, + title = 'Active USGS-NWIS GW gages measuring daily depth to sea level', + color_col = 'nmbr_observations')+ + labs(color = 'Number of observations at gage site') + + +ggsave(filename = 'gw_data_map.png', + device= 'png', + plot =gw_data_map, + path = 'mapping/') + + + +#### mapping qw sites #### + +## transform into sf obj + +wq_sites_sf %>% dim() + +## Creating this because ruby and franklin's tributaries are not directly attaining the wetland and we can keep this date point +ruby_franklin_wq_sites <- c('21NEV1_WQX-NV10-007-T-005','21NEV1_WQX-NV10-007-T-001', + 'USGS-400751115313001','USGS-400450115303401', + 'USGS-400302115294201') + +wq_sites_sf <- wq_sites %>% + st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes))%>% +# mutate(stream_order_category = ifelse(MonitoringLocationIdentifier %in% ruby_franklin_sites,'Save',stream_order_category)) %>% + filter(stream_order_category != "Not along SO 3+ or saline lake") + +wq_sites_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, lake_sf = saline_lakes, sites_sf = wq_sites_sf, + basemap = us_sf,map_bbox = map_bbox, + title = 'Active Water Quality Sites by Provider and Waterbody Type', + shape_col = 'ProviderName', + color_col = 'ResolvedMonitoringLocationTypeName')+ + labs(color = 'Waterbody type', shape = 'Provider name')+ + scale_color_brewer(palette="Accent") ## subbing diff color to supplement + +wq_sites_map + +ggsave(filename = 'wq_sites.png', + device= 'png', + plot =wq_sites_map, + path = 'mapping/') + + + +#### mapping qw data #### +sites_above_data_coverage_threshold <- wq_data %>% + select(MonitoringLocationIdentifier, ActivityStartDate, + CharacteristicName, ResultMeasureValue) %>% + ## Create new Year col and month col to then gather different year and month coverage + mutate(ActivityStartDate = as.Date(ActivityStartDate), + Year = year(ActivityStartDate)) %>% + group_by(MonitoringLocationIdentifier, CharacteristicName, Year) %>% + ## Summarizations related to date range, obr of obs, + summarize(nbr_observations = n()) %>% + filter(nbr_observations > 3) %>% arrange(nbr_observations) %>% pull(MonitoringLocationIdentifier) %>% unique() + +summarized_wqp_data <- wq_data %>% + filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% ## this only removed about 20,000 observations + filter(!grepl('Not',stream_order_category)) %>% + select(MonitoringLocationIdentifier, ActivityStartDate, CharacteristicName, ResultMeasureValue) %>% + ## Create year col and month col to then gather different year and month coverage + mutate(ActivityStartDate = as.Date(ActivityStartDate), + Year = year(ActivityStartDate), + Month = month(ActivityStartDate)) %>% + group_by(MonitoringLocationIdentifier, CharacteristicName) %>% + ## Summarizations related to date range, obr of obs, + summarize(min_date = min(ActivityStartDate), + # mean_date = median(ActivityStartDate), + max_date = max(ActivityStartDate), + nbr_observations = n(), + years_converage = paste0(unique(Year), collapse = ', '), + months_coverage = paste0(unique(Month), collapse = ', '), + mean_value = mean(ResultMeasureValue)) %>% + arrange(desc(nbr_observations)) %>% + ## Create new col with categories of observations + mutate(nbr_obs_classes = ifelse(nbr_observations <= 10,'<10', + ifelse(nbr_observations > 10 & nbr_observations <= 100,'10-100', + ifelse(nbr_observations > 100 & nbr_observations <= 1000,'100-1,000','>1,000')))) %>% + mutate(nbr_obs_classes = factor(nbr_obs_classes, c('<10','10-100','100-1,000','>1,000'))) %>% + ungroup() + +## Join to spatial file +summarized_wqp_data_sf <- summarized_wqp_data %>% + left_join(wq_sites_sf[,c('MonitoringLocationIdentifier','geometry')] %>% + distinct(), + by = 'MonitoringLocationIdentifier') %>% + st_as_sf() + +## Generate Map +map_wq_data_availability <- ggplot()+ + geom_sf(data = us_sf, fill = 'white')+ + geom_sf(data = lake_watersheds, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_sf(data = summarized_wqp_data_sf, + aes(geometry = geometry, color = nbr_obs_classes), size =0.4)+ + lims(x = c(bbox[1],bbox[3]),y = c(bbox[2],bbox[4]))+ + theme_classic()+ + guides(fill = guide_colorsteps(direction = 'horizontal'))+ + labs(title = 'WQP measurement sites scaled by number of observations', + color = 'Number of observations - grouped')+ + scale_colour_brewer(palette = 'PRGn') + + +ggsave(filename = 'wq_data.png', + device= 'png', + plot =map_wq_data_availability, + path = 'mapping/') + + +map_wq_data_availability + +### Field Meas #### + +Lakes_wo_meas_data <- is.na(p1_nwis_meas_sw_data$measurement_dt) + +sw_meas_data <- p1_nwis_meas_sw_data %>% filter(!Lakes_wo_meas_data) + + +meas_sites_along_streams <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), + p2_lake_tributaries, + lake_waterbody = FALSE) + +meas_sites_along_lake <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), + p2_saline_lakes_sf, + lake_waterbody = TRUE) + + +sw_meas_data$measurement_dt %>% max() +sw_meas_data$measurement_dt %>% max() + +sw_meas_per_year <- sw_meas_data %>% + mutate(stream_order = case_when(site_no %in% meas_sites_along_streams ~'along streams SO 3+', + site_no %in% meas_sites_along_lake ~ 'along lake', + ## Saving datapoint around Franklin because tributaries are not very distinguished and it would eliminiate a location unnecessarily + lake_w_state %in% c('Franklin Lake,NV','Ruby Lake,NV') ~ 'in Franklin & Ruby Lake wetland', + TRUE ~ 'Not along tributary or lake')) %>% + select(site_no,lake_w_state,discharge_va, gage_height_va, measurement_dt, stream_order) %>% + filter(!(is.na(discharge_va) & is.na(gage_height_va))) %>% + mutate(year = year(measurement_dt), month = month(measurement_dt)) %>% + group_by(site_no, lake_w_state, stream_order, year) %>% + summarise(nmbr_observations_yr = n()) %>% + filter(nmbr_observations_yr > 3) %>% + ungroup() + +sw_fm <- sw_meas_per_year %>% + group_by(site_no, lake_w_state, stream_order) %>% + summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% + arrange(desc(nmbr_observations)) %>% + left_join(., p2_site_in_watersheds_sf, by = 'site_no') + +sw_meas_map <- ggplot()+ + geom_sf(data = us_sf, fill = 'white')+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_sf(data = sw_fm %>% filter(!grepl('Not',stream_order_col)),aes(geometry = geometry, color = nmbr_observations), size = 1)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_classic()+ + scale_color_steps()+ + labs(title = 'USGS-NWIS SW field measurements at gage sites', + color = 'Number of observations at gage site')+ + theme(plot.title = element_text(size = 10, face= 'bold'), + legend.text = element_text (size = 7), + legend.title = element_text (size = 9)) + + +ggsave(filename = 'sw_meas_map.png', + device= 'png', + plot =sw_meas_map, + path = 'mapping/') + + + +## gw field meas +gw_meas_per_year <- p1_nwis_meas_gw_data %>% + select(site_no,lake_w_state,lev_va, lev_dt) %>% + filter(!is.na(lev_va)) %>% + mutate(lev_dt = as.Date(lev_dt), + year = year(lev_dt), + month = month(lev_dt)) %>% + group_by(site_no, lake_w_state, year) %>% + summarise(nmbr_observations_yr = n()) %>% + arrange(desc(nmbr_observations_yr)) %>% ungroup() + +gw_fm <- meas_per_year %>% + filter(nmbr_observations_yr > 3) %>% + ungroup() %>% + group_by(site_no, lake_w_state) %>% + summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% + arrange(desc(nmbr_observations)) %>% + left_join(., p2_site_in_watersheds_sf, by = 'site_no') + +gw_meas_map <- ggplot()+ + geom_sf(data = us_sf, fill = 'white')+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_sf(data = gw_fm,aes(geometry = geometry, color = nmbr_observations), size = 0.5)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_classic()+ + scale_color_steps()+ + labs(title = 'USGS-NWIS GW field Measurements at gage sites', + color = 'Number of observations at gage site')+ + theme(plot.title = element_text(size = 10, face= 'bold'), + legend.text = element_text (size = 7), + legend.title = element_text (size = 9)) + +ggsave(filename = 'gw_meas_map.png', + device= 'png', + plot =gw_meas_map, + path = 'mapping/') + + From 8f616be63c37f23f7441db5f22ccc4be230d09ad Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Wed, 30 Nov 2022 09:55:09 -0800 Subject: [PATCH 02/16] update to graphics --- .../lake_huc6_huc8_huc10_structure_table.xlsx | Bin 57199 -> 57198 bytes mapping/saline_lakes_mapping_script.R | 76 ++++++++++++------ 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/1_build_raw_feedback_xlsx/out/lake_huc6_huc8_huc10_structure_table.xlsx b/1_build_raw_feedback_xlsx/out/lake_huc6_huc8_huc10_structure_table.xlsx index 0da7c668df66f3a3bda074e37cf5ecf33e3900ac..5d2a1357434d33f3f1de13484e8a53e3eccea262 100644 GIT binary patch delta 734 zcmaFAkNMp`X5IjAW)=|!4h{~6B}ug#c@-F$f%Ikr#?4^HX%_3F5zHirQyMyi5W^qkiO` zZ*9AJzU+@1PA7zpv~aymF5!8s@O^=+#-yA{n>NRF&zQIGpx=q+42H{__8gR26rjXq zbE(Mu@rBOgOLkiYos@iOS7XjN<;aCbHeqHp)`R=+am2OPv#X_+$<;es@BXz<|JTtc z-kltEw{7hFAAY{p(>|pkYn{quIhQT_t}SG)yl|+X=G*C|$#XdhC(pXEb-}x~;>Yn5 z>)UM^9a1%gx|_Xw|7xu7{@wmo>gtvyCh987PwOu15en10fBdfaXVc@;MX$#Pc(Zc| zq=X-oU}Rv>Vq#!`g%~S1$Tn-TYC*y*jI9&Gc*Oow2^@;|v^bc+jF&nSAyTz^{j6XS zev`uxk*lV*5QeDvizOhbNq5XZ)MU#$c3`^djx{Hc9}0HDCFC6zg;#d;OFIRV~`OybP&NFgMG5yeQmZ6w?TpcTmc6N delta 690 zcmaF2kNN#RX5IjAW)=|!4h{~6D=W)4@+vSg1L@5MjGMuX$Y73fyxn}lSr^j|X- zK6Y@ju~^KD1L}f{YFoc4Ms#fyuMW7A9(XPArsVR94*%;(r;j#nZ|Jt!e~^o5x=n%2 z#h~2}E_AxL?zIZ)V$HOVv1eX#;6gK#tc{xF!Tt9b{C?K6r==Fk)z@y{(Uy3kt?1iP z7nc3G^7Eg6$XveMnaja+auXN3jYPQi{bmb|b&__!Qx9F5_j^OH?;EQL<&SrMTz_KU z;du-SlO;OaE*?8pH|6@Le+zFe*ma^?NmWJn)G~!BYhHBi@!zfgEW1r#d)mf1xxdp9EONoF|~y-M9g0-0c-ew+YC%w+_3}Gm3OQ;f$M+Fzf)jN(xBBz>-Fp$wGHE!8+~kN;7&+j=Brc;W_D()h;Cl2I+7{1`(KYAibpV z>g4rzmBFUUKJVkb0Q74*3j+f$ikgd)Ki-uAn<{Wmn(@wLoqG@+Ju~=O16dgu7E3cQ z2&3pIn4EV{32dsWbN7Nyps9zM85sCc)G$q6e@_l<>II;wCnmqSX9VUc-j`% sw_data_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, lake_sf = saline_lakes, sites_sf = sw_sites_sf_00060, basemap = us_sf, - map_bbox = map_bbox, title = 'Active USGS-NWIS SW gages measuring daily stream flow data', + map_bbox = map_bbox, title = 'Active USGS-NWIS surface water sites (2000-2022)', shape_col = 'stream_order_category',color_col = 'nmbr_observations')+ - labs(color = 'Number of observations at gage site', shape = 'Waterbody type') + labs(color = 'Number of observations at gage site', shape = 'Waterbody type')+ + guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), + shape = guide_legend(direction = 'horizontal', title.position = 'top')) + +sw_data_map ggsave(filename = 'sw_data_map.png', device= 'png', @@ -203,7 +209,7 @@ gw_data_map <- map_dv_sites_gw(watershed_sf = lake_watersheds,lake_sf = saline_l sites_sf = gw_sites_sf_72019, basemap = us_sf, map_bbox = map_bbox, - title = 'Active USGS-NWIS GW gages measuring daily depth to sea level', + title = 'Active USGS-NWIS groudwater sites (2000-2022)', color_col = 'nmbr_observations')+ labs(color = 'Number of observations at gage site') @@ -233,11 +239,13 @@ wq_sites_sf <- wq_sites %>% wq_sites_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, lake_sf = saline_lakes, sites_sf = wq_sites_sf, basemap = us_sf,map_bbox = map_bbox, - title = 'Active Water Quality Sites by Provider and Waterbody Type', + title = 'Active water quality sites (2000-2022) by Provider', shape_col = 'ProviderName', color_col = 'ResolvedMonitoringLocationTypeName')+ labs(color = 'Waterbody type', shape = 'Provider name')+ - scale_color_brewer(palette="Accent") ## subbing diff color to supplement + scale_color_brewer(palette="Accent")+ + guides(color = guide_legend(direction = 'horizontal', title.position = 'top'), + shape = guide_legend(direction = 'horizontal', title.position = 'top')) ## subbing diff color to supplement wq_sites_map @@ -284,7 +292,7 @@ summarized_wqp_data <- wq_data %>% ifelse(nbr_observations > 100 & nbr_observations <= 1000,'100-1,000','>1,000')))) %>% mutate(nbr_obs_classes = factor(nbr_obs_classes, c('<10','10-100','100-1,000','>1,000'))) %>% ungroup() - + ## Join to spatial file summarized_wqp_data_sf <- summarized_wqp_data %>% left_join(wq_sites_sf[,c('MonitoringLocationIdentifier','geometry')] %>% @@ -301,11 +309,20 @@ map_wq_data_availability <- ggplot()+ aes(geometry = geometry, color = nbr_obs_classes), size =0.4)+ lims(x = c(bbox[1],bbox[3]),y = c(bbox[2],bbox[4]))+ theme_classic()+ - guides(fill = guide_colorsteps(direction = 'horizontal'))+ - labs(title = 'WQP measurement sites scaled by number of observations', + theme(plot.title = element_text(size = 10, face= 'bold'), + legend.text = element_text (size = 7), + legend.title = element_text (size = 9), + axis.title.x = element_blank(), + axis.title.y = element_blank(), + legend.position = 'bottom' + )+ + labs(title = 'Active water quality sites (2000-2022) by number of observations', color = 'Number of observations - grouped')+ - scale_colour_brewer(palette = 'PRGn') + scale_colour_brewer(palette = 'PRGn')+ + guides(color = guide_legend(direction = 'horizontal', title.position = 'top'), + shape = guide_legend(direction = 'horizontal', title.position = 'top')) +map_wq_data_availability ggsave(filename = 'wq_data.png', device= 'png', @@ -322,13 +339,15 @@ Lakes_wo_meas_data <- is.na(p1_nwis_meas_sw_data$measurement_dt) sw_meas_data <- p1_nwis_meas_sw_data %>% filter(!Lakes_wo_meas_data) -meas_sites_along_streams <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), - p2_lake_tributaries, - lake_waterbody = FALSE) +### Commenting out to avoid rerun -meas_sites_along_lake <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), - p2_saline_lakes_sf, - lake_waterbody = TRUE) +# meas_sites_along_streams <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), +# p2_lake_tributaries, +# lake_waterbody = FALSE) +# +# meas_sites_along_lake <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), +# p2_saline_lakes_sf, +# lake_waterbody = TRUE) sw_meas_data$measurement_dt %>% max() @@ -362,12 +381,14 @@ sw_meas_map <- ggplot()+ lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ theme_classic()+ scale_color_steps()+ - labs(title = 'USGS-NWIS SW field measurements at gage sites', + labs(title = 'USGS-NWIS surface water field measurements conducted between 2000-2022', color = 'Number of observations at gage site')+ theme(plot.title = element_text(size = 10, face= 'bold'), legend.text = element_text (size = 7), - legend.title = element_text (size = 9)) - + legend.title = element_text (size = 9), + legend.position = 'bottom')+ + guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), + shape = guide_legend(direction = 'horizontal', title.position = 'top')) ggsave(filename = 'sw_meas_map.png', device= 'png', @@ -376,7 +397,7 @@ ggsave(filename = 'sw_meas_map.png', -## gw field meas +#### gw field meas #### gw_meas_per_year <- p1_nwis_meas_gw_data %>% select(site_no,lake_w_state,lev_va, lev_dt) %>% filter(!is.na(lev_va)) %>% @@ -403,11 +424,14 @@ gw_meas_map <- ggplot()+ lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ theme_classic()+ scale_color_steps()+ - labs(title = 'USGS-NWIS GW field Measurements at gage sites', + labs(title = 'USGS-NWIS ground water field measurements conducted between 2000-2022', color = 'Number of observations at gage site')+ theme(plot.title = element_text(size = 10, face= 'bold'), legend.text = element_text (size = 7), - legend.title = element_text (size = 9)) + legend.title = element_text (size = 9), + legend.position = 'bottom')+ + guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), + shape = guide_legend(direction = 'horizontal', title.position = 'top')) ggsave(filename = 'gw_meas_map.png', device= 'png', From 2ed8a45b9fb4238d932f021ef47f6afc488e725d Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Wed, 30 Nov 2022 17:24:44 -0800 Subject: [PATCH 03/16] daily update --- mapping/summary_numbers.R | 275 ++++++++++++++++++++++++++++++++++ saline_lakes_mapping_script.R | 167 +++++++++++++++++++++ 2 files changed, 442 insertions(+) create mode 100644 mapping/summary_numbers.R create mode 100644 saline_lakes_mapping_script.R diff --git a/mapping/summary_numbers.R b/mapping/summary_numbers.R new file mode 100644 index 0000000..464fb9b --- /dev/null +++ b/mapping/summary_numbers.R @@ -0,0 +1,275 @@ + +# libs -------------------------------------------------------------------- +library(ggplot2) +library(tidyverse) +library(sf) +library(mapview) +library(lubridate) +library(maps) +library(ggrepel) + +source('2_process/src/sites_along_waterbody.R') + + +# read in data ----------------------------------------------------------- + +targets::tar_load(p4_nwis_dv_gw_data_rds) +targets::tar_load(p4_nwis_dv_sw_data_rds) +targets::tar_load(p2_nwis_dv_gw_data) +targets::tar_load(p2_nwis_dv_sw_data) +targets::tar_load(p3_saline_lakes_sf) +targets::tar_load(p2_lake_watersheds_dissolved) +targets::tar_load(p2_saline_lakes_sf) +targets::tar_load(p2_lake_tributaries) +targets::tar_load(p1_nwis_meas_sw_data) +targets::tar_load(p1_nwis_meas_gw_data) + +#saline_lakes <- p3_saline_lakes_sf +#lake_watersheds <- p2_lake_watersheds_dissolved +saline_lakes <- st_read('mapping/saline_lakes.shp') +lake_watersheds <- st_read('mapping/watersheds.shp') + +nwis_dv_gw_data <- readRDS(p4_nwis_dv_gw_data_rds) +nwis_dv_sw_data <- readRDS(p4_nwis_dv_sw_data_rds) +# nwis_dv_gw_data <- readRDS("mapping/p1_nwis_dv_gw_data") +# nwis_dv_sw_data <- readRDS("mapping/p1_nwis_dv_sw_data.rds") + +wq_data <- readRDS('mapping/harmonized_wqp_data_added_cols.rds') +wq_sites <- readRDS('mapping/harmonized_wqp_sites.rds') + +# summarize sw dv data --------------------------------------------------------- + +p2_nwis_dv_sw_data %>% dim() +p2_nwis_dv_gw_data %>% dim() + +sw_lake_totals <- p2_nwis_dv_sw_data %>% + select(-contains('cd')) %>% + select(lake_w_state, site_no, X_00060_00003) %>% + filter(!is.na(X_00060_00003)) %>% + group_by(lake_w_state, site_no) %>% + summarise(nbr_obs = n()) %>% + arrange(desc(nbr_obs)) + +sw_lake_totals$lake_w_state %>% unique() +# [1] "Great Salt Lake,UT" "Pyramid Lake,NV" "Carson Lake,NV" "Sevier Lake,UT" "Owens Lake,CA" "Walker Lake,NV" +# [7] "Mono Lake,CA" "Malheur Lake,OR" + +sw_lake_totals$nbr_obs %>% sum() +# 1054172 --> This is the total number of sw observations for stream order 3 + and lake + +sw_lake_totals$site_no %>% unique() %>% length # 175 + + +# summarize gw dv data ------------------------------------------------------- + +library(dplyr) + +p2_nwis_dv_gw_data %>% head() + +gw_lake_totals <- nwis_dv_gw_data %>% + select(-contains('cd')) %>% + filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00001) & is.na(X_72019_00008) & is.na(X_72019_31200))) %>% + mutate(rowSums_measurements = rowSums(across(starts_with("X_")), na.rm = T)) %>% + group_by(lake_w_state, site_no) %>% + summarise(nbr_obs = n()) %>% + arrange(desc(nbr_obs)) + +gw_lake_totals$lake_w_state %>% unique() + +gw_lake_totals$nbr_obs %>% sum() +# 181856 + +gw_lake_totals$site_no %>% unique() %>% length + +p2_nwis_dv_sw_data %>% select(lake_w_state, site_no) %>% distinct() %>% group_by(lake_w_state) %>% summarize(n()) %>% arrange(desc(`n()`)) + +p2_nwis_dv_gw_data %>% select(lake_w_state, site_no) %>% distinct() %>% group_by(lake_w_state) %>% summarize(n()) %>% arrange(desc(`n()`)) + + + + + +# field measurement stats - SW ------------------------------------------------- + +p1_nwis_meas_sw_data %>% dim() + +Lakes_wo_meas_data <- is.na(p1_nwis_meas_sw_data$measurement_dt) +sw_meas_data <- p1_nwis_meas_sw_data %>% filter(!Lakes_wo_meas_data) + + +### Commenting out to avoid rerun + +# meas_sites_along_streams <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), +# p2_lake_tributaries, +# lake_waterbody = FALSE) +# +# meas_sites_along_lake <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), +# p2_saline_lakes_sf, +# lake_waterbody = TRUE) + + +sw_meas_data$measurement_dt %>% max() +sw_meas_data$measurement_dt %>% max() + +## wrangling +{ +sw_meas_per_year <- sw_meas_data %>% + mutate(stream_order = case_when(site_no %in% meas_sites_along_streams ~'along streams SO 3+', + site_no %in% meas_sites_along_lake ~ 'along lake', + ## Saving datapoint around Franklin because tributaries are not very distinguished and it would eliminiate a location unnecessarily + lake_w_state %in% c('Franklin Lake,NV','Ruby Lake,NV') ~ 'in Franklin & Ruby Lake wetland', + TRUE ~ 'Not along tributary or lake')) %>% + select(site_no,lake_w_state,discharge_va, gage_height_va, measurement_dt, stream_order) %>% + filter(!(is.na(discharge_va) & is.na(gage_height_va))) %>% + mutate(year = year(measurement_dt), month = month(measurement_dt)) %>% + group_by(site_no, lake_w_state, stream_order, year) %>% + summarise(nmbr_observations_yr = n()) %>% + filter(nmbr_observations_yr > 3) %>% + mutate(year_gp = ifelse(year > 2010, 'After 2010','2010 or before')) %>% + ungroup() + +sw_fm <- sw_meas_per_year %>% + group_by(site_no, lake_w_state, stream_order) %>% + summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% + arrange(desc(nmbr_observations)) +} + +total_sw_fm_per_lake <- sw_meas_per_year %>% + filter(!grepl('Not',stream_order)) %>% + group_by(lake_w_state, year_gp) %>% + summarise(total_number_of_measurements = n()) + +total_sw_fm_per_lake + + +# field measurements stats - gw ------------------------------------------ + + +# These are random gw samples taken +p1_nwis_meas_gw_data %>% dim() +p1_nwis_meas_gw_data$lev_dt %>% max() +Lakes_gw_meas_data <- is.na(p1_nwis_meas_gw_data$lev_dt) +gw_meas_data <- p1_nwis_meas_gw_data %>% filter(!Lakes_gw_meas_data) + +gw_meas_data$lev_dt %>% max() + +{ +gw_meas_per_year <- gw_meas_data %>% + select(site_no,lake_w_state,lev_va, lev_dt) %>% + filter(!is.na(lev_va)) %>% + mutate(lev_dt = as.Date(lev_dt), + year = year(lev_dt), + month = month(lev_dt)) %>% + group_by(site_no, lake_w_state, year) %>% + summarise(nmbr_observations_yr = n()) %>% + filter(nmbr_observations_yr > 3) %>% + mutate(year_gp = ifelse(year > 2010, 'After 2010','2010 or before')) %>% + arrange(nmbr_observations_yr) %>% ungroup() + +gw_fm <- gw_meas_per_year %>% + group_by(site_no, lake_w_state) %>% + summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% + arrange(desc(nmbr_observations)) +} + +total_gw_fm_per_lake <- gw_meas_per_year %>% + group_by(lake_w_state, year_gp) %>% + summarise(total_number_of_measurements = n()) + +total_gw_fm_per_lake + + + + + + + + + + + +# summarize wq data ------------------------------------------------------- + +wq_data %>% filter(!is.na(MonitoringLocationIdentifier)) %>% View() +%>% pull(lake_w_state) %>% unique() + + +## Clean table first +cleaned_wq_data <- wq_data %>% + filter(flag_missing_result == FALSE) %>% + select(MonitoringLocationIdentifier, ActivityStartDate, CharacteristicName, stream_order_category) %>% + mutate(ActivityStartDate = as.Date(ActivityStartDate), + Year = year(ActivityStartDate), + Month = month(ActivityStartDate)) + +cleaned_wq_data %>% + select(MonitoringLocationIdentifier, stream_order_category) %>% + distinct() %>% + group_by(stream_order_category) %>% + summarise(n()) + +cleaned_wq_data %>% + select(MonitoringLocationIdentifier) %>% unique() %>% nrow() + + +sites_above_data_coverage_threshold <- cleaned_wq_data %>% + ## Create new Year col and month col to then gather different year and month coverage + group_by(MonitoringLocationIdentifier, CharacteristicName, Year) %>% + ## Summarizations related to date range, obr of obs, + summarize(nbr_observations = n()) %>% + filter(nbr_observations >= 3) %>% arrange(nbr_observations) %>% + pull(MonitoringLocationIdentifier) %>% unique() + +sites_per_stream_order <- cleaned_wq_data %>% + filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% + select(MonitoringLocationIdentifier, stream_order_category) %>% + distinct() %>% + group_by(stream_order_category) %>% + summarize(n()) + + + + +summarized_wqp_data <- cleaned_wq_data %>% + filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% ## this only removed about 20,000 observations +# filter(!grepl('Not',stream_order_category)) %>% + ## Create year col and month col to then gather different year and month coverage + group_by(MonitoringLocationIdentifier) %>% + ## Summarizations related to date range, obr of obs, + summarize(min_date = min(ActivityStartDate), + # mean_date = median(ActivityStartDate), + max_date = max(ActivityStartDate), + nbr_observations = n(), + wq_data_coverage = paste0(unique(CharacteristicName), collapse = ', '), + years_coverage = paste0(unique(Year), collapse = ', '), + months_coverage = paste0(unique(Month), collapse = ', ')) %>% + arrange(desc(nbr_observations)) %>% + ## Create new col with categories of observations + mutate(nbr_obs_classes = ifelse(nbr_observations <= 10,'<10', + ifelse(nbr_observations > 10 & nbr_observations <= 100,'10-100', + ifelse(nbr_observations > 100 & nbr_observations <= 1000,'100-1,000','>1,000')))) %>% + mutate(nbr_obs_classes = factor(nbr_obs_classes, c('<10','10-100','100-1,000','>1,000'))) %>% + ungroup() + + +summarized_wqp_data %>% group_by(stream_order_category) %>% summarize(n()) + +summarized_wqp_data$nbr_observations %>% sum() ## 411,602 obs + +summarized_wqp_data$MonitoringLocationIdentifier %>% length() # 1212 sites + + +# OLD CODE ---------------------------------------------------------------- + +# obs_completion_00060 <- sw_lake_totals %>% +# st_drop_geometry() %>% +# group_by(nbr_obs, lake_w_state) %>% +# select(lake_w_state, rowSums_measurements) %>% +# summarise(nbr_obs = n()) %>% arrange(desc(`n()`)) %>% +# filter(nbr_obs == 8367) +# +# obs_completion_00060 <- gw_lake_totals %>% +# st_drop_geometry() %>% +# group_by(nmbr_observations, lake_w_state) %>% +# summarise(n()) %>% arrange(desc(`n()`)) %>% +# filter(nbr_obs == 8367) diff --git a/saline_lakes_mapping_script.R b/saline_lakes_mapping_script.R new file mode 100644 index 0000000..040e880 --- /dev/null +++ b/saline_lakes_mapping_script.R @@ -0,0 +1,167 @@ +library(ggplot2) +library(tidyverse) +library(sf) +library(mapview) + +# read in data ----------------------------------------------------------- + +targets::tar_load(p4_nwis_dv_gw_data_rds) +targets::tar_load(p4_nwis_dv_sw_data_rds) +targets::tar_load(p3_saline_lakes_sf) +targets::tar_load(p2_site_in_watersheds_sf) +targets::tar_load(p2_lake_watersheds_dissolved) +targets::tar_load(p2_saline_lakes_sf) +targets::tar_load(p2_lake_tributaries) +targets::tar_load(p1_nwis_meas_sw_data) +targets::tar_load(p1_nwis_meas_gw_data) + + +nwis_dv_gw_data <- readRDS("p1_nwis_dv_gw_data.rds") +nwis_dv_sw_data <- readRDS("p1_nwis_dv_sw_data_rds") + +wq_data <- readRDS('harmonized_wqp_data_added_cols.rds') +wq_sites <- readRDS('harmonized_wqp_sites.rds') + +wq_data$MonitoringLocationIdentifier %>% unique() %>% length() +wq_sites$MonitoringLocationIdentifier %>% unique() %>% length() + + +# check dims -------------------------------------------------------------- + +nwis_dv_gw_data %>% dim() +nwis_dv_sw_data %>% dim() + +nwis_dv_sw_data$dateTime %>% max() +nwis_dv_gw_data$dateTime %>% max() + +# general items: ---------------------------------------------------------- + +us_sf <- st_as_sf(maps::map('state', plot=F, fill=T)) %>% st_transform(4326) + +labels_df <- p3_saline_lakes_sf %>% + mutate(label = gsub('Lake: ','',label)) %>% + select(label, X, Y) %>% st_drop_geometry() + +map_bbox <- st_bbox(p2_lake_watersheds_dissolved) + +# map fun ----------------------------------------------------------------- + +## general map that includes, lakes, labels +general_map <- function(watershed_sf, lake_sf, labels_df, basemap, map_bbox, title = 'Saline Lakes and Watersheds'){ + + map <- ggplot()+ + geom_sf(data = basemap, fill = 'white')+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_text(data = labels_df, aes(X, Y, label = label), colour = "black", nudge_x = 1,size = 2)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_bw()+ + labs(title = title) + + return(map) + +} + +## mapping function for sw and wq - shape aesthetic specified + +map_sites_sw_wq <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, title = '', shape_col, color_col){ + + map <- ggplot()+ + geom_sf(data = basemap, fill = 'white')+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_sf(data = sites_sf, + aes(geometry = geometry, color = .data[[color_col]], shape = .data[[shape_col]]), size = 1)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_bw()+ + labs(title = title) + return(map) + +} + +## mapping gw sites - no shape specified +map_dv_sites_gw <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, color_col, title = ''){ + + map <- ggplot()+ + geom_sf(data = basemap, fill = 'white')+ + geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ + geom_sf(data = sites_sf, + aes(geometry = geometry, color = .data[[color_col]]), size = 1)+ + lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ + theme_bw()+ + labs(title = title)+ + scale_color_gradient(low = 'grey',high = 'black') + + return(map) + +} + + + +# Maps outputs ------------------------------------------------------------ + + +# mapping qw sites + +## transform into sf obj +wq_sites_sf <- wq_sites %>% st_as_sf(coords = c('lon','lat'), crs = st_crs(p3_saline_lakes_sf)) + + +map_sites_sw_wq(watershed_sf = p2_lake_watersheds_dissolved, lake_sf = p3_saline_lakes_sf, sites_sf = wq_sites_sf, + basemap = us_sf,map_bbox = map_bbox, title = '', shape_col = 'ProviderName', + color_col = 'ResolvedMonitoringLocationTypeName') + + + +### mapping sw sites + +sw_sites_sf_00060 <- nwis_dv_sw_data %>% + select(-contains(c('00065','..2..'))) %>% + ## removing where both valid measurements of 00060 are na + filter(!(is.na(X_00060_00003) & is.na(X_00060_00011))) %>% + # filter(stream_order_category != "not along SO 3+") %>% + mutate(dateTime = as.Date(dateTime)) %>% + group_by(site_no, lake_w_state,lon,lat, stream_order_category) %>% + summarize(nmbr_observations = n(), + min_date = min(dateTime), + max_date = max(dateTime)) %>% + st_as_sf(coords = c('lon','lat'), crs = st_crs(p3_saline_lakes_sf)) %>% arrange(desc(nmbr_observations)) + + + +map_sites_sw_wq(watershed_sf = p2_lake_watersheds_dissolved, lake_sf = p3_saline_lakes_sf, + sites_sf = sw_sites_sf_00060, basemap = us_sf, + map_bbox = map_bbox, title = '', shape_col = 'stream_order_category', + color_col = 'nmbr_observations') + + + +### mapping gw sites + +gw_sites_sf_72019 <- nwis_dv_gw_data %>% + select(-contains(c('..2..'))) %>% + ## removing where both valid measurements of 00060 are na + filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00003) & is.na(X_72019_00008) & is.na(X_72019_31200))) %>% + # filter(stream_order_category != "not along SO 3+") %>% + mutate(dateTime = as.Date(dateTime)) %>% + group_by(site_no, lake_w_state, lon, lat) %>% + summarize(nmbr_observations = n(), + min_date = min(dateTime), + max_date = max(dateTime)) %>% + # ungroup() %>% + mutate(observations_classified = case_when(nmbr_observations < 100 ~ '<100', + nmbr_observations >= 100 & nmbr_observations < 500 ~ '100 - 500', + nmbr_observations >= 500 & nmbr_observations < 2000 ~ '500 - 2000', + nmbr_observations >= 2000 & nmbr_observations < 5000 ~ '2000 - 5000', + nmbr_observations >= 5000 & nmbr_observations < 8000 ~ '5000 - 8000', + TRUE ~ '> 8000')) %>% + st_as_sf(coords = c('lon','lat'), crs = st_crs(p3_saline_lakes_sf)) + + +map_dv_sites_gw <- map_dv_sites_gw(watershed_sf = p2_lake_watersheds_dissolved, + lake_sf = p3_saline_lakes_sf, + sites_sf = gw_sites_sf_72019, + basemap = us_sf, + map_bbox = map_bbox, title = '', + color_col = 'nmbr_observations') From c291fefe1aafc15bb1ca9d8366123a4b89e14e0e Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Tue, 20 Dec 2022 10:54:02 -0800 Subject: [PATCH 04/16] adding meas sites to streamorder 3 categorization process --- 2_process_sw_gw_site_data.R | 21 +++- mapping/saline_lakes_mapping_script.R | 142 +++++++++++++++++--------- saline_lakes_mapping_script.R | 1 - 3 files changed, 112 insertions(+), 52 deletions(-) diff --git a/2_process_sw_gw_site_data.R b/2_process_sw_gw_site_data.R index 72ae675..6fb0446 100644 --- a/2_process_sw_gw_site_data.R +++ b/2_process_sw_gw_site_data.R @@ -35,7 +35,7 @@ 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') %>% @@ -51,7 +51,24 @@ p2_sw_gw_site_targets_list <- list( ## quickly re-organizing cols select(!starts_with('X_'),starts_with('X_')) - ) + ), + + ## getting all sites along lake + tar_target(p2_sw_meas_streamorder3_sites, + sites_along_waterbody(p2_site_in_watersheds_sf, + p1_nwis_meas_sw_data, + lake_waterbody = FALSE) + ), + + ## this takes a 5+ minutes due to time for buffer of tributaries to generate + tar_target(p2_sw_meas_in_lake_sites, + sites_along_waterbody(p2_site_in_watersheds_sf, + p2_saline_lakes_sf, + lake_waterbody = TRUE) + + ), + + ) diff --git a/mapping/saline_lakes_mapping_script.R b/mapping/saline_lakes_mapping_script.R index 42e1695..0ffdf29 100644 --- a/mapping/saline_lakes_mapping_script.R +++ b/mapping/saline_lakes_mapping_script.R @@ -18,16 +18,25 @@ targets::tar_load(p2_saline_lakes_sf) targets::tar_load(p2_lake_tributaries) targets::tar_load(p1_nwis_meas_sw_data) targets::tar_load(p1_nwis_meas_gw_data) +targets::tar_load(p2_site_in_watersheds_sf) + +p4_nwis_dv_gw_data_rds ## -- +saline_lakes <- p3_saline_lakes_sf +lake_watersheds <- p2_lake_watersheds_dissolved +saline_lakes <- st_read('mapping/saline_lakes.shp') +lake_watersheds <- st_read('mapping/watersheds.shp') + +nwis_dv_gw_data <- readRDS(p4_nwis_dv_gw_data_rds) +nwis_dv_sw_data <- readRDS(p4_nwis_dv_sw_data_rds) -saline_lakes <- st_read('saline_lakes.shp') -lake_watersheds <- st_read('watersheds.shp') +# nwis_dv_gw_data <- readRDS("mapping/p1_nwis_dv_gw_data.rds") +# nwis_dv_sw_data <- readRDS("mapping/p1_nwis_dv_sw_data.rds") -nwis_dv_gw_data <- readRDS("p1_nwis_dv_gw_data.rds") -nwis_dv_sw_data <- readRDS("p1_nwis_dv_sw_data.rds") +wq_data <- readRDS('mapping/harmonized_wqp_data_added_cols.rds') +wq_sites <- readRDS('mapping/harmonized_wqp_sites.rds') -wq_data <- readRDS('harmonized_wqp_data_added_cols.rds') -wq_sites <- readRDS('harmonized_wqp_sites.rds') +wq_sites wq_data$MonitoringLocationIdentifier %>% unique() %>% length() wq_sites$MonitoringLocationIdentifier %>% unique() %>% length() @@ -134,11 +143,11 @@ map_dv_sites_gw <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, # General map with labels ------------------------------------------------------------ gen_map <- general_map(watershed_sf = lake_watersheds, - lake_sf = saline_lakes, - labels_df = labels_df, - basemap = us_sf, - map_bbox = map_bbox, - title = 'Focal Saline Lakes and Watersheds in the Great Basin') + lake_sf = saline_lakes, + labels_df = labels_df, + basemap = us_sf, + map_bbox = map_bbox, + title = 'Focal Saline Lakes and Watersheds in the Great Basin') ggsave(filename = 'gen_map.png', @@ -151,31 +160,45 @@ ggsave(filename = 'gen_map.png', #### mapping sw sites #### +nwis_dv_sw_data %>% select(-contains(c('00065','..2..'))) %>% + ## removing where both valid measurements of 00060 are na + filter(!(is.na(X_00060_00003))) %>% nrow() + +## 1,553,692 + sw_sites_sf_00060 <- nwis_dv_sw_data %>% select(-contains(c('00065','..2..'))) %>% ## removing where both valid measurements of 00060 are na - filter(!(is.na(X_00060_00003) & is.na(X_00060_00011))) %>% + filter(!(is.na(X_00060_00003))) %>% + drop_na(site_no) %>% ## NOTE - filtering out stream order < 3 and recoded for graph purposes - filter(stream_order_category != "not along SO 3+") %>% - mutate(stream_order_category = recode(stream_order_category, - "along SO 3+" = 'Along stream', - "along lake" = 'Along lake')) %>% + # filter(stream_order_category != "not along SO 3+") %>% + # mutate(stream_order_category = recode(stream_order_category, + # "along SO 3+" = 'Along stream', + # "along lake" = 'Along lake')) %>% mutate(dateTime = as.Date(dateTime)) %>% group_by(site_no, lake_w_state,lon,lat, stream_order_category) %>% summarize(nmbr_observations = n(), min_date = min(dateTime), max_date = max(dateTime)) %>% st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes)) %>% arrange(desc(nmbr_observations)) - -sw_data_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, lake_sf = saline_lakes, - sites_sf = sw_sites_sf_00060, basemap = us_sf, - map_bbox = map_bbox, title = 'Active USGS-NWIS surface water sites (2000-2022)', - shape_col = 'stream_order_category',color_col = 'nmbr_observations')+ - labs(color = 'Number of observations at gage site', shape = 'Waterbody type')+ + +sw_sites_sf_00060 + +sw_data_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, + lake_sf = saline_lakes, + sites_sf = sw_sites_sf_00060, + basemap = us_sf, + map_bbox = map_bbox, + title = 'Active USGS-NWIS surface water sites (2000-2022)', + shape_col = 'stream_order_category', + color_col = 'nmbr_observations')+ + labs(color = 'Number of observations at gage site', + shape = 'Waterbody type')+ guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), shape = guide_legend(direction = 'horizontal', title.position = 'top')) - + sw_data_map ggsave(filename = 'sw_data_map.png', @@ -188,7 +211,7 @@ ggsave(filename = 'sw_data_map.png', gw_sites_sf_72019 <- nwis_dv_gw_data %>% select(-contains(c('..2..'))) %>% ## removing where both valid measurements of 00060 are na - filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00003) & is.na(X_72019_00008) & is.na(X_72019_31200))) %>% + filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00003) & is.na(X_72019_00008))) %>% # filter(stream_order_category != "not along SO 3+") %>% mutate(dateTime = as.Date(dateTime)) %>% group_by(site_no, lake_w_state, lon, lat) %>% @@ -232,17 +255,25 @@ ruby_franklin_wq_sites <- c('21NEV1_WQX-NV10-007-T-005','21NEV1_WQX-NV10-007-T-0 'USGS-400751115313001','USGS-400450115303401', 'USGS-400302115294201') +wq_sites %>% dim() + wq_sites_sf <- wq_sites %>% - st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes))%>% -# mutate(stream_order_category = ifelse(MonitoringLocationIdentifier %in% ruby_franklin_sites,'Save',stream_order_category)) %>% + st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes)) %>% + mutate(stream_order_category = ifelse(MonitoringLocationIdentifier %in% ruby_franklin_sites,'Save',stream_order_category)) %>% filter(stream_order_category != "Not along SO 3+ or saline lake") -wq_sites_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, lake_sf = saline_lakes, sites_sf = wq_sites_sf, +wq_sites_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, + lake_sf = saline_lakes, + sites_sf = wq_sites_sf, basemap = us_sf,map_bbox = map_bbox, - title = 'Active water quality sites (2000-2022) by Provider', + title = 'Active water quality sites (2000-2022) by provider', shape_col = 'ProviderName', color_col = 'ResolvedMonitoringLocationTypeName')+ labs(color = 'Waterbody type', shape = 'Provider name')+ + ## renaming legend items + scale_shape_discrete(labels = c("USGS-NWIS", "EPA-STORET"))+ + scale_color_discrete(labels = c("Lake", "Stream"))+ + ## changing color since discrete scale_color_brewer(palette="Accent")+ guides(color = guide_legend(direction = 'horizontal', title.position = 'top'), shape = guide_legend(direction = 'horizontal', title.position = 'top')) ## subbing diff color to supplement @@ -257,34 +288,35 @@ ggsave(filename = 'wq_sites.png', #### mapping qw data #### -sites_above_data_coverage_threshold <- wq_data %>% - select(MonitoringLocationIdentifier, ActivityStartDate, - CharacteristicName, ResultMeasureValue) %>% - ## Create new Year col and month col to then gather different year and month coverage +## Clean table first +cleaned_wq_data <- wq_data %>% + filter(flag_missing_result == FALSE) %>% + select(MonitoringLocationIdentifier, ActivityStartDate, CharacteristicName, stream_order_category) %>% mutate(ActivityStartDate = as.Date(ActivityStartDate), - Year = year(ActivityStartDate)) %>% + Year = year(ActivityStartDate), + Month = month(ActivityStartDate)) + +sites_above_data_coverage_threshold <- cleaned_wq_data %>% + ## Create new Year col and month col to then gather different year and month coverage group_by(MonitoringLocationIdentifier, CharacteristicName, Year) %>% ## Summarizations related to date range, obr of obs, summarize(nbr_observations = n()) %>% - filter(nbr_observations > 3) %>% arrange(nbr_observations) %>% pull(MonitoringLocationIdentifier) %>% unique() + filter(nbr_observations >= 3) %>% arrange(nbr_observations) %>% + pull(MonitoringLocationIdentifier) %>% unique() -summarized_wqp_data <- wq_data %>% - filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% ## this only removed about 20,000 observations +summarized_wqp_data <- cleaned_wq_data %>% + # filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% ## this only removed about 20,000 observations filter(!grepl('Not',stream_order_category)) %>% - select(MonitoringLocationIdentifier, ActivityStartDate, CharacteristicName, ResultMeasureValue) %>% ## Create year col and month col to then gather different year and month coverage - mutate(ActivityStartDate = as.Date(ActivityStartDate), - Year = year(ActivityStartDate), - Month = month(ActivityStartDate)) %>% - group_by(MonitoringLocationIdentifier, CharacteristicName) %>% + group_by(MonitoringLocationIdentifier) %>% ## Summarizations related to date range, obr of obs, summarize(min_date = min(ActivityStartDate), # mean_date = median(ActivityStartDate), max_date = max(ActivityStartDate), nbr_observations = n(), - years_converage = paste0(unique(Year), collapse = ', '), - months_coverage = paste0(unique(Month), collapse = ', '), - mean_value = mean(ResultMeasureValue)) %>% + wq_data_coverage = paste0(unique(CharacteristicName), collapse = ', '), + years_coverage = paste0(unique(Year), collapse = ', '), + months_coverage = paste0(unique(Month), collapse = ', ')) %>% arrange(desc(nbr_observations)) %>% ## Create new col with categories of observations mutate(nbr_obs_classes = ifelse(nbr_observations <= 10,'<10', @@ -300,6 +332,10 @@ summarized_wqp_data_sf <- summarized_wqp_data %>% by = 'MonitoringLocationIdentifier') %>% st_as_sf() +temp <- st_join(summarized_wqp_data_sf,lake_watersheds) +summarized_wqp_data_sf %>% group_by() +summarized_wqp_data_sf$MonitoringLocationIdentifier %>% length + ## Generate Map map_wq_data_availability <- ggplot()+ geom_sf(data = us_sf, fill = 'white')+ @@ -320,7 +356,8 @@ map_wq_data_availability <- ggplot()+ color = 'Number of observations - grouped')+ scale_colour_brewer(palette = 'PRGn')+ guides(color = guide_legend(direction = 'horizontal', title.position = 'top'), - shape = guide_legend(direction = 'horizontal', title.position = 'top')) + shape = guide_legend(direction = 'horizontal', title.position = 'top') + ) map_wq_data_availability @@ -365,6 +402,7 @@ sw_meas_per_year <- sw_meas_data %>% group_by(site_no, lake_w_state, stream_order, year) %>% summarise(nmbr_observations_yr = n()) %>% filter(nmbr_observations_yr > 3) %>% + mutate(year_gp = ifelse(year > 2010, 'After 2010','2010 or before')) %>% ungroup() sw_fm <- sw_meas_per_year %>% @@ -377,7 +415,9 @@ sw_meas_map <- ggplot()+ geom_sf(data = us_sf, fill = 'white')+ geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = sw_fm %>% filter(!grepl('Not',stream_order_col)),aes(geometry = geometry, color = nmbr_observations), size = 1)+ + geom_sf(data = sw_fm %>% + filter(!grepl('Not',stream_order_col)), + aes(geometry = geometry, color = nmbr_observations), size = 1)+ lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ theme_classic()+ scale_color_steps()+ @@ -408,17 +448,17 @@ gw_meas_per_year <- p1_nwis_meas_gw_data %>% summarise(nmbr_observations_yr = n()) %>% arrange(desc(nmbr_observations_yr)) %>% ungroup() -gw_fm <- meas_per_year %>% +gw_fm <- gw_meas_per_year %>% filter(nmbr_observations_yr > 3) %>% ungroup() %>% group_by(site_no, lake_w_state) %>% summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% arrange(desc(nmbr_observations)) %>% - left_join(., p2_site_in_watersheds_sf, by = 'site_no') + left_join(.,p2_site_in_watersheds_sf , by = 'site_no') gw_meas_map <- ggplot()+ geom_sf(data = us_sf, fill = 'white')+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ + geom_sf(data = lake_watersheds, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ geom_sf(data = gw_fm,aes(geometry = geometry, color = nmbr_observations), size = 0.5)+ lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ @@ -439,3 +479,7 @@ ggsave(filename = 'gw_meas_map.png', path = 'mapping/') + + + +wq_data %>% nrow() diff --git a/saline_lakes_mapping_script.R b/saline_lakes_mapping_script.R index 040e880..9607ff6 100644 --- a/saline_lakes_mapping_script.R +++ b/saline_lakes_mapping_script.R @@ -98,7 +98,6 @@ map_dv_sites_gw <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, } - # Maps outputs ------------------------------------------------------------ From 3153055f3a697e34f87ab868ffd20f7bc164b411 Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Tue, 20 Dec 2022 14:21:56 -0800 Subject: [PATCH 05/16] adding commenting to p2_nwis_dv_sw_data --- 2_process_sw_gw_site_data.R | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/2_process_sw_gw_site_data.R b/2_process_sw_gw_site_data.R index 6fb0446..f1fffaf 100644 --- a/2_process_sw_gw_site_data.R +++ b/2_process_sw_gw_site_data.R @@ -39,38 +39,29 @@ p2_sw_gw_site_targets_list <- list( tar_target(p2_nwis_dv_sw_data, p1_nwis_dv_sw_data %>% left_join(p2_site_in_watersheds_sf, by = 'site_no') %>% + ## 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)) %>% + ## create stream_order_category col depending on site type & match with sites stream order/lake vector targets 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+' )) %>% + ## 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() %>% ## quickly re-organizing cols select(!starts_with('X_'),starts_with('X_')) ), - ## getting all sites along lake - tar_target(p2_sw_meas_streamorder3_sites, - sites_along_waterbody(p2_site_in_watersheds_sf, - p1_nwis_meas_sw_data, - lake_waterbody = FALSE) - ), - - ## this takes a 5+ minutes due to time for buffer of tributaries to generate - tar_target(p2_sw_meas_in_lake_sites, - sites_along_waterbody(p2_site_in_watersheds_sf, - p2_saline_lakes_sf, - lake_waterbody = TRUE) + - ), + ) - - - ) # # SW data ----------------------------------------------------------------- From 7117ee4dd5d0956924477274e89bdcc7c3ebbd31 Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Tue, 20 Dec 2022 15:08:50 -0800 Subject: [PATCH 06/16] adding p2_nwis_meas_sw_data --- 2_process_sw_gw_site_data.R | 19 ++++++++++++++++++- 4_export.R | 16 ++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/2_process_sw_gw_site_data.R b/2_process_sw_gw_site_data.R index f1fffaf..ea0b39e 100644 --- a/2_process_sw_gw_site_data.R +++ b/2_process_sw_gw_site_data.R @@ -53,11 +53,28 @@ p2_sw_gw_site_targets_list <- list( mutate(lon = st_coordinates(.)[,1], lat = st_coordinates(.)[,2]) %>% ## remove geometry col st_drop_geometry() %>% - ## quickly re-organizing cols + ## quickly re-organizing cols so that measurements cols come after non-measurement cols select(!starts_with('X_'),starts_with('X_')) ), + ## this is almost the same process as above - will creat function + tar_target(p2_nwis_meas_sw_data, + p1_nwis_meas_sw_data %>% + left_join(p2_site_in_watersheds_sf, by = 'site_no') %>% + mutate(stream_order_category = case_when( + site_no %in% p2_sw_streamorder3_sites ~ 'along SO 3+', + site_no %in% p2_sw_in_lake_sites ~ '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() + + ) diff --git a/4_export.R b/4_export.R index 506712b..5c620bb 100644 --- a/4_export.R +++ b/4_export.R @@ -4,7 +4,7 @@ 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, @@ -12,7 +12,7 @@ tar_target( 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, @@ -20,18 +20,18 @@ tar_target( 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 @@ -53,11 +53,14 @@ 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, @@ -65,6 +68,7 @@ tar_target(p4_saline_lakes_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'), From 934c67500a565d721089c152172ae551c7f2548a Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Tue, 20 Dec 2022 16:03:31 -0800 Subject: [PATCH 07/16] transformed process of adding stream order cols to sw data into a new function --- 2_process/src/sites_along_waterbody.R | 37 +++++++++++++++++++--- 2_process_sw_gw_site_data.R | 45 +++++++-------------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/2_process/src/sites_along_waterbody.R b/2_process/src/sites_along_waterbody.R index fc55046..ccfdff7 100644 --- a/2_process/src/sites_along_waterbody.R +++ b/2_process/src/sites_along_waterbody.R @@ -1,11 +1,11 @@ -#' @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 %>% + sites_sf <- sites_sf %>% select(site_no, geometry) %>% sf::st_as_sf() @@ -19,10 +19,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)) } - 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)) %>% + ## 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() + +} diff --git a/2_process_sw_gw_site_data.R b/2_process_sw_gw_site_data.R index ea0b39e..5030e9a 100644 --- a/2_process_sw_gw_site_data.R +++ b/2_process_sw_gw_site_data.R @@ -37,22 +37,11 @@ p2_sw_gw_site_targets_list <- list( ## 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') %>% - ## 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)) %>% - ## create stream_order_category col depending on site type & match with sites stream order/lake vector targets - 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+' - )) %>% - ## 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() %>% + add_stream_order(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_')) @@ -60,24 +49,12 @@ p2_sw_gw_site_targets_list <- list( ## this is almost the same process as above - will creat function tar_target(p2_nwis_meas_sw_data, - p1_nwis_meas_sw_data %>% - left_join(p2_site_in_watersheds_sf, by = 'site_no') %>% - mutate(stream_order_category = case_when( - site_no %in% p2_sw_streamorder3_sites ~ 'along SO 3+', - site_no %in% p2_sw_in_lake_sites ~ '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() - - - - - ) + add_stream_order(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) + ) ) From 673495bf5bf55e9d07593387fcfe2b71564f70c5 Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Tue, 20 Dec 2022 16:16:29 -0800 Subject: [PATCH 08/16] rename of add_stream_order param --- 2_process/src/sites_along_waterbody.R | 3 +-- 2_process_sw_gw_site_data.R | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/2_process/src/sites_along_waterbody.R b/2_process/src/sites_along_waterbody.R index ccfdff7..97a99ea 100644 --- a/2_process/src/sites_along_waterbody.R +++ b/2_process/src/sites_along_waterbody.R @@ -6,8 +6,7 @@ sites_along_waterbody <- function(sites_sf, waterbody_sf, lake_waterbody = FALSE sites_sf <- sites_sf %>% - select(site_no, geometry) %>% - sf::st_as_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 diff --git a/2_process_sw_gw_site_data.R b/2_process_sw_gw_site_data.R index 5030e9a..8b6a08c 100644 --- a/2_process_sw_gw_site_data.R +++ b/2_process_sw_gw_site_data.R @@ -37,7 +37,7 @@ p2_sw_gw_site_targets_list <- list( ## get just sw sites with outputed data for 2000-2022 with stream order category column tar_target(p2_nwis_dv_sw_data, - add_stream_order(sw_data = p1_nwis_dv_sw_data, + 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, @@ -49,7 +49,7 @@ p2_sw_gw_site_targets_list <- list( ## this is almost the same process as above - will creat function tar_target(p2_nwis_meas_sw_data, - add_stream_order(sw_data = p1_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, From 1651bd10f76943bd3ac88919ee69ae012f6e9097 Mon Sep 17 00:00:00 2001 From: Margaux Sleckman <36547359+msleckman@users.noreply.github.com> Date: Fri, 30 Dec 2022 15:01:33 -0800 Subject: [PATCH 09/16] Update 2_process_sw_gw_site_data.R Co-authored-by: Cee Nell --- 2_process_sw_gw_site_data.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_process_sw_gw_site_data.R b/2_process_sw_gw_site_data.R index 8b6a08c..0ff13e1 100644 --- a/2_process_sw_gw_site_data.R +++ b/2_process_sw_gw_site_data.R @@ -47,7 +47,7 @@ p2_sw_gw_site_targets_list <- list( ), - ## this is almost the same process as above - will creat function + ## 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, From ee53554508798853c6b666d35167eb7727195c31 Mon Sep 17 00:00:00 2001 From: Margaux Sleckman <36547359+msleckman@users.noreply.github.com> Date: Fri, 30 Dec 2022 15:01:50 -0800 Subject: [PATCH 10/16] Update mapping/summary_numbers.R Co-authored-by: Cee Nell --- mapping/summary_numbers.R | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/mapping/summary_numbers.R b/mapping/summary_numbers.R index 464fb9b..b26f3d8 100644 --- a/mapping/summary_numbers.R +++ b/mapping/summary_numbers.R @@ -178,16 +178,6 @@ total_gw_fm_per_lake <- gw_meas_per_year %>% total_gw_fm_per_lake - - - - - - - - - - # summarize wq data ------------------------------------------------------- wq_data %>% filter(!is.na(MonitoringLocationIdentifier)) %>% View() From c37ed9bdbf77e2acc72e2768687d70e11359d1ff Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Fri, 30 Dec 2022 15:09:09 -0800 Subject: [PATCH 11/16] renamed script to 2_process_site_data.R; --- 2_process_sw_gw_site_data.R => 2_process_site_data.R | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 2_process_sw_gw_site_data.R => 2_process_site_data.R (100%) diff --git a/2_process_sw_gw_site_data.R b/2_process_site_data.R similarity index 100% rename from 2_process_sw_gw_site_data.R rename to 2_process_site_data.R From 3db9a3eb46c6d7f1246844ed2749eb72317c7787 Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Fri, 30 Dec 2022 15:31:13 -0800 Subject: [PATCH 12/16] renamed script to 2_process_site_data.R; --- 2_process_site_data.R | 2 +- _targets.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/2_process_site_data.R b/2_process_site_data.R index 0ff13e1..0a70c4d 100644 --- a/2_process_site_data.R +++ b/2_process_site_data.R @@ -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, diff --git a/_targets.R b/_targets.R index c3b3669..e9f08ed 100644 --- a/_targets.R +++ b/_targets.R @@ -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) \ No newline at end of file From ee1481bc7ef9b3a5fe6a6040bd30cbf11ff4de5d Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Fri, 30 Dec 2022 15:51:09 -0800 Subject: [PATCH 13/16] changing names of lake_tributaries with suffix _so3 --- 2_process_lakes_tribs.R | 4 ++-- 2_process_site_data.R | 2 +- 3_viz_prep.R | 2 +- 4_export.R | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/2_process_lakes_tribs.R b/2_process_lakes_tribs.R index bb3275e..aefa939 100644 --- a/2_process_lakes_tribs.R +++ b/2_process_lakes_tribs.R @@ -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( @@ -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, diff --git a/2_process_site_data.R b/2_process_site_data.R index 0a70c4d..0c44326 100644 --- a/2_process_site_data.R +++ b/2_process_site_data.R @@ -23,7 +23,7 @@ p2_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) ), diff --git a/3_viz_prep.R b/3_viz_prep.R index 573f90b..aec3d72 100644 --- a/3_viz_prep.R +++ b/3_viz_prep.R @@ -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", diff --git a/4_export.R b/4_export.R index 5c620bb..cc803e4 100644 --- a/4_export.R +++ b/4_export.R @@ -63,7 +63,7 @@ tar_target(p4_saline_lakes_shp, ## 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' # ), From ff8f93cd6ca64bd3d807b104cc61195974821934 Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Fri, 30 Dec 2022 15:55:05 -0800 Subject: [PATCH 14/16] rm-ing summary_numbers.R --- mapping/summary_numbers.R | 265 -------------------------------------- 1 file changed, 265 deletions(-) delete mode 100644 mapping/summary_numbers.R diff --git a/mapping/summary_numbers.R b/mapping/summary_numbers.R deleted file mode 100644 index b26f3d8..0000000 --- a/mapping/summary_numbers.R +++ /dev/null @@ -1,265 +0,0 @@ - -# libs -------------------------------------------------------------------- -library(ggplot2) -library(tidyverse) -library(sf) -library(mapview) -library(lubridate) -library(maps) -library(ggrepel) - -source('2_process/src/sites_along_waterbody.R') - - -# read in data ----------------------------------------------------------- - -targets::tar_load(p4_nwis_dv_gw_data_rds) -targets::tar_load(p4_nwis_dv_sw_data_rds) -targets::tar_load(p2_nwis_dv_gw_data) -targets::tar_load(p2_nwis_dv_sw_data) -targets::tar_load(p3_saline_lakes_sf) -targets::tar_load(p2_lake_watersheds_dissolved) -targets::tar_load(p2_saline_lakes_sf) -targets::tar_load(p2_lake_tributaries) -targets::tar_load(p1_nwis_meas_sw_data) -targets::tar_load(p1_nwis_meas_gw_data) - -#saline_lakes <- p3_saline_lakes_sf -#lake_watersheds <- p2_lake_watersheds_dissolved -saline_lakes <- st_read('mapping/saline_lakes.shp') -lake_watersheds <- st_read('mapping/watersheds.shp') - -nwis_dv_gw_data <- readRDS(p4_nwis_dv_gw_data_rds) -nwis_dv_sw_data <- readRDS(p4_nwis_dv_sw_data_rds) -# nwis_dv_gw_data <- readRDS("mapping/p1_nwis_dv_gw_data") -# nwis_dv_sw_data <- readRDS("mapping/p1_nwis_dv_sw_data.rds") - -wq_data <- readRDS('mapping/harmonized_wqp_data_added_cols.rds') -wq_sites <- readRDS('mapping/harmonized_wqp_sites.rds') - -# summarize sw dv data --------------------------------------------------------- - -p2_nwis_dv_sw_data %>% dim() -p2_nwis_dv_gw_data %>% dim() - -sw_lake_totals <- p2_nwis_dv_sw_data %>% - select(-contains('cd')) %>% - select(lake_w_state, site_no, X_00060_00003) %>% - filter(!is.na(X_00060_00003)) %>% - group_by(lake_w_state, site_no) %>% - summarise(nbr_obs = n()) %>% - arrange(desc(nbr_obs)) - -sw_lake_totals$lake_w_state %>% unique() -# [1] "Great Salt Lake,UT" "Pyramid Lake,NV" "Carson Lake,NV" "Sevier Lake,UT" "Owens Lake,CA" "Walker Lake,NV" -# [7] "Mono Lake,CA" "Malheur Lake,OR" - -sw_lake_totals$nbr_obs %>% sum() -# 1054172 --> This is the total number of sw observations for stream order 3 + and lake - -sw_lake_totals$site_no %>% unique() %>% length # 175 - - -# summarize gw dv data ------------------------------------------------------- - -library(dplyr) - -p2_nwis_dv_gw_data %>% head() - -gw_lake_totals <- nwis_dv_gw_data %>% - select(-contains('cd')) %>% - filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00001) & is.na(X_72019_00008) & is.na(X_72019_31200))) %>% - mutate(rowSums_measurements = rowSums(across(starts_with("X_")), na.rm = T)) %>% - group_by(lake_w_state, site_no) %>% - summarise(nbr_obs = n()) %>% - arrange(desc(nbr_obs)) - -gw_lake_totals$lake_w_state %>% unique() - -gw_lake_totals$nbr_obs %>% sum() -# 181856 - -gw_lake_totals$site_no %>% unique() %>% length - -p2_nwis_dv_sw_data %>% select(lake_w_state, site_no) %>% distinct() %>% group_by(lake_w_state) %>% summarize(n()) %>% arrange(desc(`n()`)) - -p2_nwis_dv_gw_data %>% select(lake_w_state, site_no) %>% distinct() %>% group_by(lake_w_state) %>% summarize(n()) %>% arrange(desc(`n()`)) - - - - - -# field measurement stats - SW ------------------------------------------------- - -p1_nwis_meas_sw_data %>% dim() - -Lakes_wo_meas_data <- is.na(p1_nwis_meas_sw_data$measurement_dt) -sw_meas_data <- p1_nwis_meas_sw_data %>% filter(!Lakes_wo_meas_data) - - -### Commenting out to avoid rerun - -# meas_sites_along_streams <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), -# p2_lake_tributaries, -# lake_waterbody = FALSE) -# -# meas_sites_along_lake <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), -# p2_saline_lakes_sf, -# lake_waterbody = TRUE) - - -sw_meas_data$measurement_dt %>% max() -sw_meas_data$measurement_dt %>% max() - -## wrangling -{ -sw_meas_per_year <- sw_meas_data %>% - mutate(stream_order = case_when(site_no %in% meas_sites_along_streams ~'along streams SO 3+', - site_no %in% meas_sites_along_lake ~ 'along lake', - ## Saving datapoint around Franklin because tributaries are not very distinguished and it would eliminiate a location unnecessarily - lake_w_state %in% c('Franklin Lake,NV','Ruby Lake,NV') ~ 'in Franklin & Ruby Lake wetland', - TRUE ~ 'Not along tributary or lake')) %>% - select(site_no,lake_w_state,discharge_va, gage_height_va, measurement_dt, stream_order) %>% - filter(!(is.na(discharge_va) & is.na(gage_height_va))) %>% - mutate(year = year(measurement_dt), month = month(measurement_dt)) %>% - group_by(site_no, lake_w_state, stream_order, year) %>% - summarise(nmbr_observations_yr = n()) %>% - filter(nmbr_observations_yr > 3) %>% - mutate(year_gp = ifelse(year > 2010, 'After 2010','2010 or before')) %>% - ungroup() - -sw_fm <- sw_meas_per_year %>% - group_by(site_no, lake_w_state, stream_order) %>% - summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% - arrange(desc(nmbr_observations)) -} - -total_sw_fm_per_lake <- sw_meas_per_year %>% - filter(!grepl('Not',stream_order)) %>% - group_by(lake_w_state, year_gp) %>% - summarise(total_number_of_measurements = n()) - -total_sw_fm_per_lake - - -# field measurements stats - gw ------------------------------------------ - - -# These are random gw samples taken -p1_nwis_meas_gw_data %>% dim() -p1_nwis_meas_gw_data$lev_dt %>% max() -Lakes_gw_meas_data <- is.na(p1_nwis_meas_gw_data$lev_dt) -gw_meas_data <- p1_nwis_meas_gw_data %>% filter(!Lakes_gw_meas_data) - -gw_meas_data$lev_dt %>% max() - -{ -gw_meas_per_year <- gw_meas_data %>% - select(site_no,lake_w_state,lev_va, lev_dt) %>% - filter(!is.na(lev_va)) %>% - mutate(lev_dt = as.Date(lev_dt), - year = year(lev_dt), - month = month(lev_dt)) %>% - group_by(site_no, lake_w_state, year) %>% - summarise(nmbr_observations_yr = n()) %>% - filter(nmbr_observations_yr > 3) %>% - mutate(year_gp = ifelse(year > 2010, 'After 2010','2010 or before')) %>% - arrange(nmbr_observations_yr) %>% ungroup() - -gw_fm <- gw_meas_per_year %>% - group_by(site_no, lake_w_state) %>% - summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% - arrange(desc(nmbr_observations)) -} - -total_gw_fm_per_lake <- gw_meas_per_year %>% - group_by(lake_w_state, year_gp) %>% - summarise(total_number_of_measurements = n()) - -total_gw_fm_per_lake - -# summarize wq data ------------------------------------------------------- - -wq_data %>% filter(!is.na(MonitoringLocationIdentifier)) %>% View() -%>% pull(lake_w_state) %>% unique() - - -## Clean table first -cleaned_wq_data <- wq_data %>% - filter(flag_missing_result == FALSE) %>% - select(MonitoringLocationIdentifier, ActivityStartDate, CharacteristicName, stream_order_category) %>% - mutate(ActivityStartDate = as.Date(ActivityStartDate), - Year = year(ActivityStartDate), - Month = month(ActivityStartDate)) - -cleaned_wq_data %>% - select(MonitoringLocationIdentifier, stream_order_category) %>% - distinct() %>% - group_by(stream_order_category) %>% - summarise(n()) - -cleaned_wq_data %>% - select(MonitoringLocationIdentifier) %>% unique() %>% nrow() - - -sites_above_data_coverage_threshold <- cleaned_wq_data %>% - ## Create new Year col and month col to then gather different year and month coverage - group_by(MonitoringLocationIdentifier, CharacteristicName, Year) %>% - ## Summarizations related to date range, obr of obs, - summarize(nbr_observations = n()) %>% - filter(nbr_observations >= 3) %>% arrange(nbr_observations) %>% - pull(MonitoringLocationIdentifier) %>% unique() - -sites_per_stream_order <- cleaned_wq_data %>% - filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% - select(MonitoringLocationIdentifier, stream_order_category) %>% - distinct() %>% - group_by(stream_order_category) %>% - summarize(n()) - - - - -summarized_wqp_data <- cleaned_wq_data %>% - filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% ## this only removed about 20,000 observations -# filter(!grepl('Not',stream_order_category)) %>% - ## Create year col and month col to then gather different year and month coverage - group_by(MonitoringLocationIdentifier) %>% - ## Summarizations related to date range, obr of obs, - summarize(min_date = min(ActivityStartDate), - # mean_date = median(ActivityStartDate), - max_date = max(ActivityStartDate), - nbr_observations = n(), - wq_data_coverage = paste0(unique(CharacteristicName), collapse = ', '), - years_coverage = paste0(unique(Year), collapse = ', '), - months_coverage = paste0(unique(Month), collapse = ', ')) %>% - arrange(desc(nbr_observations)) %>% - ## Create new col with categories of observations - mutate(nbr_obs_classes = ifelse(nbr_observations <= 10,'<10', - ifelse(nbr_observations > 10 & nbr_observations <= 100,'10-100', - ifelse(nbr_observations > 100 & nbr_observations <= 1000,'100-1,000','>1,000')))) %>% - mutate(nbr_obs_classes = factor(nbr_obs_classes, c('<10','10-100','100-1,000','>1,000'))) %>% - ungroup() - - -summarized_wqp_data %>% group_by(stream_order_category) %>% summarize(n()) - -summarized_wqp_data$nbr_observations %>% sum() ## 411,602 obs - -summarized_wqp_data$MonitoringLocationIdentifier %>% length() # 1212 sites - - -# OLD CODE ---------------------------------------------------------------- - -# obs_completion_00060 <- sw_lake_totals %>% -# st_drop_geometry() %>% -# group_by(nbr_obs, lake_w_state) %>% -# select(lake_w_state, rowSums_measurements) %>% -# summarise(nbr_obs = n()) %>% arrange(desc(`n()`)) %>% -# filter(nbr_obs == 8367) -# -# obs_completion_00060 <- gw_lake_totals %>% -# st_drop_geometry() %>% -# group_by(nmbr_observations, lake_w_state) %>% -# summarise(n()) %>% arrange(desc(`n()`)) %>% -# filter(nbr_obs == 8367) From 3bab1863a39db14705ffaf28812642a7c98f9cbb Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Fri, 30 Dec 2022 16:02:44 -0800 Subject: [PATCH 15/16] rm-ing saline_lakes_mapping script.R --- mapping/saline_lakes_mapping_script.R | 485 -------------------------- saline_lakes_mapping_script.R | 166 --------- 2 files changed, 651 deletions(-) delete mode 100644 mapping/saline_lakes_mapping_script.R delete mode 100644 saline_lakes_mapping_script.R diff --git a/mapping/saline_lakes_mapping_script.R b/mapping/saline_lakes_mapping_script.R deleted file mode 100644 index 0ffdf29..0000000 --- a/mapping/saline_lakes_mapping_script.R +++ /dev/null @@ -1,485 +0,0 @@ - -# libs -------------------------------------------------------------------- -library(ggplot2) -library(tidyverse) -library(sf) -library(mapview) -library(maps) -library(ggrepel) - -# read in data ----------------------------------------------------------- - -## target load for local pipeline - IGNORE -targets::tar_load(p4_nwis_dv_gw_data_rds) -targets::tar_load(p4_nwis_dv_sw_data_rds) -targets::tar_load(p3_saline_lakes_sf) -targets::tar_load(p2_lake_watersheds_dissolved) -targets::tar_load(p2_saline_lakes_sf) -targets::tar_load(p2_lake_tributaries) -targets::tar_load(p1_nwis_meas_sw_data) -targets::tar_load(p1_nwis_meas_gw_data) -targets::tar_load(p2_site_in_watersheds_sf) - -p4_nwis_dv_gw_data_rds -## -- -saline_lakes <- p3_saline_lakes_sf -lake_watersheds <- p2_lake_watersheds_dissolved -saline_lakes <- st_read('mapping/saline_lakes.shp') -lake_watersheds <- st_read('mapping/watersheds.shp') - -nwis_dv_gw_data <- readRDS(p4_nwis_dv_gw_data_rds) -nwis_dv_sw_data <- readRDS(p4_nwis_dv_sw_data_rds) - -# nwis_dv_gw_data <- readRDS("mapping/p1_nwis_dv_gw_data.rds") -# nwis_dv_sw_data <- readRDS("mapping/p1_nwis_dv_sw_data.rds") - -wq_data <- readRDS('mapping/harmonized_wqp_data_added_cols.rds') -wq_sites <- readRDS('mapping/harmonized_wqp_sites.rds') - -wq_sites - -wq_data$MonitoringLocationIdentifier %>% unique() %>% length() -wq_sites$MonitoringLocationIdentifier %>% unique() %>% length() - - -# check dims -------------------------------------------------------------- - -nwis_dv_gw_data %>% dim() -nwis_dv_sw_data %>% dim() - -nwis_dv_sw_data$dateTime %>% max() -nwis_dv_gw_data$dateTime %>% max() - -# general items: ---------------------------------------------------------- - -us_sf <- st_as_sf(maps::map('state', plot=F, fill=T)) %>% st_transform(4326) - -labels_df <- saline_lakes %>% - mutate(label = gsub('Lake: ','',label)) %>% - select(label, X, Y) %>% st_drop_geometry() - -map_bbox <- st_bbox(lake_watersheds) - -# map fun ----------------------------------------------------------------- - -## general map that includes, lakes, labels -general_map <- function(watershed_sf, lake_sf, labels_df, basemap, map_bbox, title = 'Saline Lakes and Watersheds'){ - - map <- ggplot()+ - geom_sf(data = basemap, fill = 'white', color = 'grey', alpha = 0.1)+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ - ggrepel::geom_text_repel(data = labels_df, aes(X, Y, label = label),colour = "black", - size = 3, nudge_y = 0.1, nudge_x = 0.15, - segment.colour="black", min.segment.length = 1.5)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_classic()+ - labs(title = title)+ - theme(plot.title = element_text(size = 10, face= 'bold'), - legend.text = element_text (size = 7), - legend.title = element_text (size = 9), - axis.title.x = element_blank(), - axis.title.y = element_blank() - )+ - guides(fill = guide_colorsteps(direction = 'horizontal', title.position = 'bottom')) - - return(map) - -} - -## mapping function for sw and wq - shape aesthetic specified - -map_sites_sw_wq <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, title = '', shape_col, color_col){ - - map <- ggplot()+ - geom_sf(data = basemap, fill = 'white', color = 'grey', alpha = 0.1)+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = sites_sf, - aes(geometry = geometry, color = .data[[color_col]], shape = .data[[shape_col]]), size = 1)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_classic()+ - scale_color_steps()+ - labs(title = title)+ - theme(plot.title = element_text(size = 10, face= 'bold'), - legend.text = element_text (size = 7), - legend.title = element_text (size = 9), - axis.title.x = element_blank(), - axis.title.y = element_blank(), - legend.position = 'bottom' - ) - - return(map) - -} - -## mapping gw sites - no shape specified -map_dv_sites_gw <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, color_col, title = ''){ - - map <- ggplot()+ - geom_sf(data = basemap, fill = 'white')+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = sites_sf, - aes(geometry = geometry, color = .data[[color_col]]), size = 1)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_classic()+ - labs(title = title)+ - scale_color_steps(low = 'orange', high = 'firebrick')+ - theme(plot.title = element_text(size = 10, face= 'bold'), - legend.text = element_text (size = 7), - legend.title = element_text (size = 9), - axis.title.x = element_blank(), - axis.title.y = element_blank(), - legend.position = 'bottom')+ - guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), - shape = guide_legend(direction = 'horizontal', title.position = 'top')) - return(map) - -} - - - -# General map with labels ------------------------------------------------------------ - -gen_map <- general_map(watershed_sf = lake_watersheds, - lake_sf = saline_lakes, - labels_df = labels_df, - basemap = us_sf, - map_bbox = map_bbox, - title = 'Focal Saline Lakes and Watersheds in the Great Basin') - - -ggsave(filename = 'gen_map.png', - device= 'png', - plot =gen_map, - path = 'mapping/') - -# Site maps outputs ------------------------------------------------------------ - - -#### mapping sw sites #### - -nwis_dv_sw_data %>% select(-contains(c('00065','..2..'))) %>% - ## removing where both valid measurements of 00060 are na - filter(!(is.na(X_00060_00003))) %>% nrow() - -## 1,553,692 - -sw_sites_sf_00060 <- nwis_dv_sw_data %>% - select(-contains(c('00065','..2..'))) %>% - ## removing where both valid measurements of 00060 are na - filter(!(is.na(X_00060_00003))) %>% - drop_na(site_no) %>% - ## NOTE - filtering out stream order < 3 and recoded for graph purposes - # filter(stream_order_category != "not along SO 3+") %>% - # mutate(stream_order_category = recode(stream_order_category, - # "along SO 3+" = 'Along stream', - # "along lake" = 'Along lake')) %>% - mutate(dateTime = as.Date(dateTime)) %>% - group_by(site_no, lake_w_state,lon,lat, stream_order_category) %>% - summarize(nmbr_observations = n(), - min_date = min(dateTime), - max_date = max(dateTime)) %>% - st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes)) %>% arrange(desc(nmbr_observations)) - -sw_sites_sf_00060 - -sw_data_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, - lake_sf = saline_lakes, - sites_sf = sw_sites_sf_00060, - basemap = us_sf, - map_bbox = map_bbox, - title = 'Active USGS-NWIS surface water sites (2000-2022)', - shape_col = 'stream_order_category', - color_col = 'nmbr_observations')+ - labs(color = 'Number of observations at gage site', - shape = 'Waterbody type')+ - guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), - shape = guide_legend(direction = 'horizontal', title.position = 'top')) - - -sw_data_map - -ggsave(filename = 'sw_data_map.png', - device= 'png', - plot =sw_data_map, - path = 'mapping/') - -#### mapping gw sites #### - -gw_sites_sf_72019 <- nwis_dv_gw_data %>% - select(-contains(c('..2..'))) %>% - ## removing where both valid measurements of 00060 are na - filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00003) & is.na(X_72019_00008))) %>% - # filter(stream_order_category != "not along SO 3+") %>% - mutate(dateTime = as.Date(dateTime)) %>% - group_by(site_no, lake_w_state, lon, lat) %>% - summarize(nmbr_observations = n(), - min_date = min(dateTime), - max_date = max(dateTime)) %>% - # ungroup() %>% - ## adding col for categorizing obser nmbrs - mutate(observations_classified = case_when(nmbr_observations < 100 ~ '<100', - nmbr_observations >= 100 & nmbr_observations < 500 ~ '100 - 500', - nmbr_observations >= 500 & nmbr_observations < 2000 ~ '500 - 2000', - nmbr_observations >= 2000 & nmbr_observations < 5000 ~ '2000 - 5000', - nmbr_observations >= 5000 & nmbr_observations < 8000 ~ '5000 - 8000', - TRUE ~ '> 8000')) %>% - st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes)) - -gw_data_map <- map_dv_sites_gw(watershed_sf = lake_watersheds,lake_sf = saline_lakes, - sites_sf = gw_sites_sf_72019, - basemap = us_sf, - map_bbox = map_bbox, - title = 'Active USGS-NWIS groudwater sites (2000-2022)', - color_col = 'nmbr_observations')+ - labs(color = 'Number of observations at gage site') - - -ggsave(filename = 'gw_data_map.png', - device= 'png', - plot =gw_data_map, - path = 'mapping/') - - - -#### mapping qw sites #### - -## transform into sf obj - -wq_sites_sf %>% dim() - -## Creating this because ruby and franklin's tributaries are not directly attaining the wetland and we can keep this date point -ruby_franklin_wq_sites <- c('21NEV1_WQX-NV10-007-T-005','21NEV1_WQX-NV10-007-T-001', - 'USGS-400751115313001','USGS-400450115303401', - 'USGS-400302115294201') - -wq_sites %>% dim() - -wq_sites_sf <- wq_sites %>% - st_as_sf(coords = c('lon','lat'), crs = st_crs(saline_lakes)) %>% - mutate(stream_order_category = ifelse(MonitoringLocationIdentifier %in% ruby_franklin_sites,'Save',stream_order_category)) %>% - filter(stream_order_category != "Not along SO 3+ or saline lake") - -wq_sites_map <- map_sites_sw_wq(watershed_sf = lake_watersheds, - lake_sf = saline_lakes, - sites_sf = wq_sites_sf, - basemap = us_sf,map_bbox = map_bbox, - title = 'Active water quality sites (2000-2022) by provider', - shape_col = 'ProviderName', - color_col = 'ResolvedMonitoringLocationTypeName')+ - labs(color = 'Waterbody type', shape = 'Provider name')+ - ## renaming legend items - scale_shape_discrete(labels = c("USGS-NWIS", "EPA-STORET"))+ - scale_color_discrete(labels = c("Lake", "Stream"))+ - ## changing color since discrete - scale_color_brewer(palette="Accent")+ - guides(color = guide_legend(direction = 'horizontal', title.position = 'top'), - shape = guide_legend(direction = 'horizontal', title.position = 'top')) ## subbing diff color to supplement - -wq_sites_map - -ggsave(filename = 'wq_sites.png', - device= 'png', - plot =wq_sites_map, - path = 'mapping/') - - - -#### mapping qw data #### -## Clean table first -cleaned_wq_data <- wq_data %>% - filter(flag_missing_result == FALSE) %>% - select(MonitoringLocationIdentifier, ActivityStartDate, CharacteristicName, stream_order_category) %>% - mutate(ActivityStartDate = as.Date(ActivityStartDate), - Year = year(ActivityStartDate), - Month = month(ActivityStartDate)) - -sites_above_data_coverage_threshold <- cleaned_wq_data %>% - ## Create new Year col and month col to then gather different year and month coverage - group_by(MonitoringLocationIdentifier, CharacteristicName, Year) %>% - ## Summarizations related to date range, obr of obs, - summarize(nbr_observations = n()) %>% - filter(nbr_observations >= 3) %>% arrange(nbr_observations) %>% - pull(MonitoringLocationIdentifier) %>% unique() - -summarized_wqp_data <- cleaned_wq_data %>% - # filter(MonitoringLocationIdentifier %in% sites_above_data_coverage_threshold) %>% ## this only removed about 20,000 observations - filter(!grepl('Not',stream_order_category)) %>% - ## Create year col and month col to then gather different year and month coverage - group_by(MonitoringLocationIdentifier) %>% - ## Summarizations related to date range, obr of obs, - summarize(min_date = min(ActivityStartDate), - # mean_date = median(ActivityStartDate), - max_date = max(ActivityStartDate), - nbr_observations = n(), - wq_data_coverage = paste0(unique(CharacteristicName), collapse = ', '), - years_coverage = paste0(unique(Year), collapse = ', '), - months_coverage = paste0(unique(Month), collapse = ', ')) %>% - arrange(desc(nbr_observations)) %>% - ## Create new col with categories of observations - mutate(nbr_obs_classes = ifelse(nbr_observations <= 10,'<10', - ifelse(nbr_observations > 10 & nbr_observations <= 100,'10-100', - ifelse(nbr_observations > 100 & nbr_observations <= 1000,'100-1,000','>1,000')))) %>% - mutate(nbr_obs_classes = factor(nbr_obs_classes, c('<10','10-100','100-1,000','>1,000'))) %>% - ungroup() - -## Join to spatial file -summarized_wqp_data_sf <- summarized_wqp_data %>% - left_join(wq_sites_sf[,c('MonitoringLocationIdentifier','geometry')] %>% - distinct(), - by = 'MonitoringLocationIdentifier') %>% - st_as_sf() - -temp <- st_join(summarized_wqp_data_sf,lake_watersheds) -summarized_wqp_data_sf %>% group_by() -summarized_wqp_data_sf$MonitoringLocationIdentifier %>% length - -## Generate Map -map_wq_data_availability <- ggplot()+ - geom_sf(data = us_sf, fill = 'white')+ - geom_sf(data = lake_watersheds, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = summarized_wqp_data_sf, - aes(geometry = geometry, color = nbr_obs_classes), size =0.4)+ - lims(x = c(bbox[1],bbox[3]),y = c(bbox[2],bbox[4]))+ - theme_classic()+ - theme(plot.title = element_text(size = 10, face= 'bold'), - legend.text = element_text (size = 7), - legend.title = element_text (size = 9), - axis.title.x = element_blank(), - axis.title.y = element_blank(), - legend.position = 'bottom' - )+ - labs(title = 'Active water quality sites (2000-2022) by number of observations', - color = 'Number of observations - grouped')+ - scale_colour_brewer(palette = 'PRGn')+ - guides(color = guide_legend(direction = 'horizontal', title.position = 'top'), - shape = guide_legend(direction = 'horizontal', title.position = 'top') - ) - -map_wq_data_availability - -ggsave(filename = 'wq_data.png', - device= 'png', - plot =map_wq_data_availability, - path = 'mapping/') - - -map_wq_data_availability - -### Field Meas #### - -Lakes_wo_meas_data <- is.na(p1_nwis_meas_sw_data$measurement_dt) - -sw_meas_data <- p1_nwis_meas_sw_data %>% filter(!Lakes_wo_meas_data) - - -### Commenting out to avoid rerun - -# meas_sites_along_streams <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), -# p2_lake_tributaries, -# lake_waterbody = FALSE) -# -# meas_sites_along_lake <- sites_along_waterbody(sw_fm %>% st_as_sf() %>% select(site_no), -# p2_saline_lakes_sf, -# lake_waterbody = TRUE) - - -sw_meas_data$measurement_dt %>% max() -sw_meas_data$measurement_dt %>% max() - -sw_meas_per_year <- sw_meas_data %>% - mutate(stream_order = case_when(site_no %in% meas_sites_along_streams ~'along streams SO 3+', - site_no %in% meas_sites_along_lake ~ 'along lake', - ## Saving datapoint around Franklin because tributaries are not very distinguished and it would eliminiate a location unnecessarily - lake_w_state %in% c('Franklin Lake,NV','Ruby Lake,NV') ~ 'in Franklin & Ruby Lake wetland', - TRUE ~ 'Not along tributary or lake')) %>% - select(site_no,lake_w_state,discharge_va, gage_height_va, measurement_dt, stream_order) %>% - filter(!(is.na(discharge_va) & is.na(gage_height_va))) %>% - mutate(year = year(measurement_dt), month = month(measurement_dt)) %>% - group_by(site_no, lake_w_state, stream_order, year) %>% - summarise(nmbr_observations_yr = n()) %>% - filter(nmbr_observations_yr > 3) %>% - mutate(year_gp = ifelse(year > 2010, 'After 2010','2010 or before')) %>% - ungroup() - -sw_fm <- sw_meas_per_year %>% - group_by(site_no, lake_w_state, stream_order) %>% - summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% - arrange(desc(nmbr_observations)) %>% - left_join(., p2_site_in_watersheds_sf, by = 'site_no') - -sw_meas_map <- ggplot()+ - geom_sf(data = us_sf, fill = 'white')+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = sw_fm %>% - filter(!grepl('Not',stream_order_col)), - aes(geometry = geometry, color = nmbr_observations), size = 1)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_classic()+ - scale_color_steps()+ - labs(title = 'USGS-NWIS surface water field measurements conducted between 2000-2022', - color = 'Number of observations at gage site')+ - theme(plot.title = element_text(size = 10, face= 'bold'), - legend.text = element_text (size = 7), - legend.title = element_text (size = 9), - legend.position = 'bottom')+ - guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), - shape = guide_legend(direction = 'horizontal', title.position = 'top')) - -ggsave(filename = 'sw_meas_map.png', - device= 'png', - plot =sw_meas_map, - path = 'mapping/') - - - -#### gw field meas #### -gw_meas_per_year <- p1_nwis_meas_gw_data %>% - select(site_no,lake_w_state,lev_va, lev_dt) %>% - filter(!is.na(lev_va)) %>% - mutate(lev_dt = as.Date(lev_dt), - year = year(lev_dt), - month = month(lev_dt)) %>% - group_by(site_no, lake_w_state, year) %>% - summarise(nmbr_observations_yr = n()) %>% - arrange(desc(nmbr_observations_yr)) %>% ungroup() - -gw_fm <- gw_meas_per_year %>% - filter(nmbr_observations_yr > 3) %>% - ungroup() %>% - group_by(site_no, lake_w_state) %>% - summarise(nmbr_observations = sum(nmbr_observations_yr)) %>% - arrange(desc(nmbr_observations)) %>% - left_join(.,p2_site_in_watersheds_sf , by = 'site_no') - -gw_meas_map <- ggplot()+ - geom_sf(data = us_sf, fill = 'white')+ - geom_sf(data = lake_watersheds, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = saline_lakes, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = gw_fm,aes(geometry = geometry, color = nmbr_observations), size = 0.5)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_classic()+ - scale_color_steps()+ - labs(title = 'USGS-NWIS ground water field measurements conducted between 2000-2022', - color = 'Number of observations at gage site')+ - theme(plot.title = element_text(size = 10, face= 'bold'), - legend.text = element_text (size = 7), - legend.title = element_text (size = 9), - legend.position = 'bottom')+ - guides(color = guide_colorsteps(direction = 'horizontal', title.position = 'top'), - shape = guide_legend(direction = 'horizontal', title.position = 'top')) - -ggsave(filename = 'gw_meas_map.png', - device= 'png', - plot =gw_meas_map, - path = 'mapping/') - - - - - -wq_data %>% nrow() diff --git a/saline_lakes_mapping_script.R b/saline_lakes_mapping_script.R deleted file mode 100644 index 9607ff6..0000000 --- a/saline_lakes_mapping_script.R +++ /dev/null @@ -1,166 +0,0 @@ -library(ggplot2) -library(tidyverse) -library(sf) -library(mapview) - -# read in data ----------------------------------------------------------- - -targets::tar_load(p4_nwis_dv_gw_data_rds) -targets::tar_load(p4_nwis_dv_sw_data_rds) -targets::tar_load(p3_saline_lakes_sf) -targets::tar_load(p2_site_in_watersheds_sf) -targets::tar_load(p2_lake_watersheds_dissolved) -targets::tar_load(p2_saline_lakes_sf) -targets::tar_load(p2_lake_tributaries) -targets::tar_load(p1_nwis_meas_sw_data) -targets::tar_load(p1_nwis_meas_gw_data) - - -nwis_dv_gw_data <- readRDS("p1_nwis_dv_gw_data.rds") -nwis_dv_sw_data <- readRDS("p1_nwis_dv_sw_data_rds") - -wq_data <- readRDS('harmonized_wqp_data_added_cols.rds') -wq_sites <- readRDS('harmonized_wqp_sites.rds') - -wq_data$MonitoringLocationIdentifier %>% unique() %>% length() -wq_sites$MonitoringLocationIdentifier %>% unique() %>% length() - - -# check dims -------------------------------------------------------------- - -nwis_dv_gw_data %>% dim() -nwis_dv_sw_data %>% dim() - -nwis_dv_sw_data$dateTime %>% max() -nwis_dv_gw_data$dateTime %>% max() - -# general items: ---------------------------------------------------------- - -us_sf <- st_as_sf(maps::map('state', plot=F, fill=T)) %>% st_transform(4326) - -labels_df <- p3_saline_lakes_sf %>% - mutate(label = gsub('Lake: ','',label)) %>% - select(label, X, Y) %>% st_drop_geometry() - -map_bbox <- st_bbox(p2_lake_watersheds_dissolved) - -# map fun ----------------------------------------------------------------- - -## general map that includes, lakes, labels -general_map <- function(watershed_sf, lake_sf, labels_df, basemap, map_bbox, title = 'Saline Lakes and Watersheds'){ - - map <- ggplot()+ - geom_sf(data = basemap, fill = 'white')+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_text(data = labels_df, aes(X, Y, label = label), colour = "black", nudge_x = 1,size = 2)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_bw()+ - labs(title = title) - - return(map) - -} - -## mapping function for sw and wq - shape aesthetic specified - -map_sites_sw_wq <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, title = '', shape_col, color_col){ - - map <- ggplot()+ - geom_sf(data = basemap, fill = 'white')+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = sites_sf, - aes(geometry = geometry, color = .data[[color_col]], shape = .data[[shape_col]]), size = 1)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_bw()+ - labs(title = title) - return(map) - -} - -## mapping gw sites - no shape specified -map_dv_sites_gw <- function(watershed_sf, lake_sf, sites_sf, basemap, map_bbox, color_col, title = ''){ - - map <- ggplot()+ - geom_sf(data = basemap, fill = 'white')+ - geom_sf(data = watershed_sf, fill = 'transparent', color = 'firebrick', size = 0.01, linetype = 'dotted')+ - geom_sf(data = lake_sf, fill = ' light blue', color = 'grey', alpha = 0.5)+ - geom_sf(data = sites_sf, - aes(geometry = geometry, color = .data[[color_col]]), size = 1)+ - lims(x = c(map_bbox[1],map_bbox[3]),y = c(map_bbox[2],map_bbox[4]))+ - theme_bw()+ - labs(title = title)+ - scale_color_gradient(low = 'grey',high = 'black') - - return(map) - -} - - -# Maps outputs ------------------------------------------------------------ - - -# mapping qw sites - -## transform into sf obj -wq_sites_sf <- wq_sites %>% st_as_sf(coords = c('lon','lat'), crs = st_crs(p3_saline_lakes_sf)) - - -map_sites_sw_wq(watershed_sf = p2_lake_watersheds_dissolved, lake_sf = p3_saline_lakes_sf, sites_sf = wq_sites_sf, - basemap = us_sf,map_bbox = map_bbox, title = '', shape_col = 'ProviderName', - color_col = 'ResolvedMonitoringLocationTypeName') - - - -### mapping sw sites - -sw_sites_sf_00060 <- nwis_dv_sw_data %>% - select(-contains(c('00065','..2..'))) %>% - ## removing where both valid measurements of 00060 are na - filter(!(is.na(X_00060_00003) & is.na(X_00060_00011))) %>% - # filter(stream_order_category != "not along SO 3+") %>% - mutate(dateTime = as.Date(dateTime)) %>% - group_by(site_no, lake_w_state,lon,lat, stream_order_category) %>% - summarize(nmbr_observations = n(), - min_date = min(dateTime), - max_date = max(dateTime)) %>% - st_as_sf(coords = c('lon','lat'), crs = st_crs(p3_saline_lakes_sf)) %>% arrange(desc(nmbr_observations)) - - - -map_sites_sw_wq(watershed_sf = p2_lake_watersheds_dissolved, lake_sf = p3_saline_lakes_sf, - sites_sf = sw_sites_sf_00060, basemap = us_sf, - map_bbox = map_bbox, title = '', shape_col = 'stream_order_category', - color_col = 'nmbr_observations') - - - -### mapping gw sites - -gw_sites_sf_72019 <- nwis_dv_gw_data %>% - select(-contains(c('..2..'))) %>% - ## removing where both valid measurements of 00060 are na - filter(!(is.na(X_72019_00003) & is.na(X_72019_00002) & is.na(X_72019_00003) & is.na(X_72019_00008) & is.na(X_72019_31200))) %>% - # filter(stream_order_category != "not along SO 3+") %>% - mutate(dateTime = as.Date(dateTime)) %>% - group_by(site_no, lake_w_state, lon, lat) %>% - summarize(nmbr_observations = n(), - min_date = min(dateTime), - max_date = max(dateTime)) %>% - # ungroup() %>% - mutate(observations_classified = case_when(nmbr_observations < 100 ~ '<100', - nmbr_observations >= 100 & nmbr_observations < 500 ~ '100 - 500', - nmbr_observations >= 500 & nmbr_observations < 2000 ~ '500 - 2000', - nmbr_observations >= 2000 & nmbr_observations < 5000 ~ '2000 - 5000', - nmbr_observations >= 5000 & nmbr_observations < 8000 ~ '5000 - 8000', - TRUE ~ '> 8000')) %>% - st_as_sf(coords = c('lon','lat'), crs = st_crs(p3_saline_lakes_sf)) - - -map_dv_sites_gw <- map_dv_sites_gw(watershed_sf = p2_lake_watersheds_dissolved, - lake_sf = p3_saline_lakes_sf, - sites_sf = gw_sites_sf_72019, - basemap = us_sf, - map_bbox = map_bbox, title = '', - color_col = 'nmbr_observations') From 57f59270c9b18f2bb5e9e29cda025c3863fcbe3d Mon Sep 17 00:00:00 2001 From: Margaux Sleckman Date: Fri, 30 Dec 2022 16:13:42 -0800 Subject: [PATCH 16/16] edit to source after rname of 2_process_site_data.R --- _targets.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_targets.R b/_targets.R index e9f08ed..2bd3a54 100644 --- a/_targets.R +++ b/_targets.R @@ -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")