library(tidyverse)
library(here)
library(dataRetrieval)

Put two data frames together by site. This will create a column of usable site names.

ex_df <-data.frame(Site = rep(c(1:5),each=6))

name_df <- data.frame(site_name = c('joe_creek', 'bean_trail', 'orange_outlet', 'sane_mountain', 'fuzzy_lake'),
            Site = c(1:5))

ex_name_df <- merge(ex_df, name_df, by = 'Site', all.x =TRUE)

Here we can download some GW data. The raw data are instantaneous values (15 min) and the gw are daily values. I’m commenting out the NWIS download so that this will knit.

# NOT RUN {
site_id <- '373930118491602'
parameterCd <- '72019'
startDate <- "1992-01-19"
endDate <- "2022-04-06"
# }
# NOT RUN {
# rawData <- readNWISuv(site_id,parameterCd,startDate,endDate)
# 
# gw <- readNWISdv(site_id,parameterCd,startDate,endDate) %>% 
#   select(-agency_cd, -X_72019_00003_cd) %>% 
#   rename(water_depth = X_72019_00003, site = site_no)

Next we need to take daily values for the rawData. Because I have commented out the download this is also commented out - again so it will knit.

# dv <- rawData %>% 
#   mutate(day = day(dateTime), month = month(dateTime), year = year(dateTime)) %>% 
#   group_by(year, month, day) %>% 
#   summarize(water_depth = mean(X_72019_00000)) 
# 
# dv$Date <- as.Date(with(dv,paste(year, month, day,sep="-")),"%Y-%m-%d") 
# 
# dv$site <- "373930118491602"
# 
# dv <- dv %>% 
#   ungroup(year, month, day) %>% 
#   select(-year, - month, -day) %>% 
#   filter(Date > "2010-12-13")

Last we can rbind the two data frames together and write it to csv to save. Again - commented out. In the write_csv bit I am writing the file to a folder on my computer so you will need to update that path if you want to do this part. It is pretty helpful, this way you just have it all saved in a csv in a tidy format so moving forward you can just read_csv and do your work. You don’t have to keep going through the download and clean process.

#full <- rbind(gw, dv)

#write_csv(full, here('hydrology/src_classes_spring_2022/data', 'full.csv'))