Chapter 10 Demo on calculating flow totals in mm.

library(dataRetrieval)
library(tidyverse)
site <- "06187915"
parm_cd <- "00060"
service_cd <- "dv"
start <- as.Date("2022-05-01")
end <- as.Date("2022-10-01")
gauge_info <- readNWISsite(site) %>% 
  select(site_no, station_nm, drain_area_va)
flow <- readNWISdata(sites = site, parameterCd = parm_cd, service = service_cd, startDate = start, endDate = end) %>% 
  renameNWISColumns() %>% 
  select(site_no, date = dateTime, flow_cfs = Flow)
flow <- left_join(flow, gauge_info, by = "site_no")
flow_mm <- flow %>% 
  mutate(cfd = flow_cfs * 86400) %>% 
  mutate(cmd = cfd * 0.0283168) %>% 
  mutate(drain_sq_me = drain_area_va * 2.59e+6) %>% 
  mutate(m_d = cmd/drain_sq_me) %>% 
  mutate(mm_d = m_d * 1000) %>% 
  select(site_no, station_nm, date, mm_d)
flow_mm %>% 
  ggplot(aes(x = date, y = mm_d)) +
  geom_point()

library(lubridate)

flow_tot_an <- flow_mm %>%
  mutate(year = year(date)) %>% 
  group_by(year, site_no, station_nm) %>% 
  summarize(tot_an = sum(mm_d, na.rm = TRUE))