Skip to content

Commit

Permalink
Merge pull request #35 from internetofwater/rebuild-032024
Browse files Browse the repository at this point in the history
Rebuild 032024
  • Loading branch information
dblodgett-usgs authored Mar 21, 2024
2 parents 1bb1ab5 + 87d6683 commit 433849c
Show file tree
Hide file tree
Showing 4 changed files with 1,483 additions and 15 deletions.
27 changes: 17 additions & 10 deletions R/gage_locations.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,21 @@ get_hydrologic_locations <- function(all_gages, hydrologic_locations, nhdpv2_fli
}

add_mainstems <- function(gage_hydrologic_locations, mainstems, vaa) {
mainstems <- mainstems[,c("id", "uri"), drop = TRUE]
mainstems$id <- as.integer(mainstems$id)
vaa <- right_join(vaa, mainstems, by = c("levelpathi" = "id"))

vaa <- vaa[,c("comid", "uri")]

names(vaa) <- c("comid", "mainstem_uri")

left_join(gage_hydrologic_locations, vaa,
by = c("nhdpv2_COMID" = "comid"))
mainstems <- mainstems[,c("head_nhdpv2_COMID", "uri"), drop = TRUE]
mainstems$head_nhdpv2_COMID <- as.integer(gsub("https://geoconnex.us/nhdplusv2/comid/", "",
mainstems$head_nhdpv2_COMID))

mainstem_lookup <- group_by(vaa, levelpathi) |>
filter(hydroseq == max(hydroseq)) |>
ungroup() |>
select(head_nhdpv2_COMID = comid, levelpathi) |>
distinct() |>
left_join(mainstems, by = "head_nhdpv2_COMID") |>
filter(!is.na(uri)) |>
select(-head_nhdpv2_COMID) |>
right_join(select(vaa, comid, levelpathi),
by = "levelpathi") |>
select(-levelpathi, comid, mainstem_uri = uri)

dplyr::left_join(gage_hydrologic_locations, mainstem_lookup, by = c("nhdpv2_COMID" = "comid"))
}
2 changes: 1 addition & 1 deletion R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ get_swim_data <- function() {
}

get_all_mainstems <- function(outdir) {
url <- "https://www.hydroshare.org/resource/4a22e88e689949afa1cf71ae009eaf1b/data/contents/mainstems.gpkg"
url <- "https://www.hydroshare.org/resource/3cc04df349cd45f38e1637305c98529c/data/contents/mainstems.gpkg"

dir.create(outdir, recursive = TRUE, showWarnings = FALSE)

Expand Down
9 changes: 5 additions & 4 deletions _targets.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
library(targets)

tar_option_set(packages = c("nhdplusTools", "sf", "dplyr", "dataRetrieval",
"sbtools", "readr", "knitr", "mapview"),
memory = "transient", garbage_collection = TRUE)
"sbtools", "readr", "knitr", "mapview", "data.table"),
memory = "transient", garbage_collection = TRUE,
debug = "gage_hydrologic_locations_with_mainstems")

reference_file <- "out/ref_gages.gpkg"

Expand All @@ -29,7 +30,7 @@ list(
tar_target("nhdpv2_fline_proc", select(st_transform(nhdpv2_fline, 5070),
COMID, REACHCODE, ToMeas, FromMeas)),
tar_target("mainstems", get_all_mainstems("data/mainstems/")),
tar_target("vaa", get_vaa(atts = c("comid", "levelpathi"),
tar_target("vaa", get_vaa(atts = c("comid", "levelpathi", "hydroseq"),
updated_network = TRUE)),
# This function downloads all NWIS sites from the site file
tar_target("nwis_gage", get_nwis_sites()),
Expand Down Expand Up @@ -74,7 +75,7 @@ list(
locations = nwis_gage_hydro_locatons),
list(provider = "https://cdec.water.ca.gov",
locations = cdec_gage_address)),
nhdpv2_fline = nhdpv2_fline_proc)),
nhdpv2_fline = sf::st_zm(nhdpv2_fline_proc))),

tar_target("gage_hydrologic_locations_with_mainstems", add_mainstems(gage_hydrologic_locations,
mainstems, vaa)),
Expand Down
Loading

0 comments on commit 433849c

Please sign in to comment.