## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(nwaa)

## ----catalog, eval = FALSE----------------------------------------------------
# nwaa_catalog()

## ----catalog-vars-wu, eval = FALSE--------------------------------------------
# nwaa_wu_variables("wu-irrigation-wd")

## ----catalog-vars, eval = FALSE-----------------------------------------------
# catalog <- nwaa_catalog()
# catalog$variables[catalog$model_id == "wqn-conus404-ba"][[1]]
# #> [1] "precip"

## ----location, eval = FALSE---------------------------------------------------
# nwaa_location_types()

## ----state-codes, eval = FALSE------------------------------------------------
# head(nwaa_statecd())

## ----wu-example, eval = FALSE-------------------------------------------------
# wu <- nwaa_water_use(
#   model_id      = "wu-irrigation-wd",
#   variable_ids  = c("irrwdtot", "irrwdgw", "irrwdsw"),
#   location_type = "huc12",
#   location_id   = "180300010602",
#   time_res      = "monthly",
#   range         = "custom",
#   start         = "2020-01",
#   end           = "2020-12"
# )
# head(wu)

## ----atmos-example, eval = FALSE----------------------------------------------
# atmos <- nwaa_atmos(
#   variable_ids  = "precip",
#   location_type = "huc12",
#   location_id   = "180300010602",
#   time_res      = "monthly",
#   range         = "custom",
#   start         = "2020-01",
#   end           = "2020-12"
# )
# head(atmos)

## ----hydro-example, eval = FALSE----------------------------------------------
# hydro <- nwaa_hydro(
#   variable_ids  = c("actet", "swe"),
#   location_type = "huc12",
#   location_id   = "180300010602",
#   time_res      = "monthly",
#   range         = "custom",
#   start         = "2020-01",
#   end           = "2020-12"
# )
# head(hydro)

## ----iwa-example, eval = FALSE------------------------------------------------
# iwa <- nwaa_iwa(
#   variable_ids  = c("availab", "consum", "sui"),
#   location_type = "huc12",
#   location_id   = "180300010602",
#   time_res      = "monthly",
#   range         = "custom",
#   start         = "2020-01",
#   end           = "2020-12"
# )
# head(iwa)

## ----aggregate-pattern, eval = FALSE------------------------------------------
# library(dplyr)
# 
# precip_monthly <- nwaa_atmos(
#   variable_ids  = "precip",
#   location_type = "huc12",
#   location_id   = "180300010602",
#   range         = "historical"
# )
# 
# precip_annual <- precip_monthly |>
#   mutate(year = substr(year_month, 1, 4)) |>
#   group_by(huc12_id, year) |>
#   summarise(precip_mm_yr = sum(`precip_mm/mo`), .groups = "drop")

## ----validate-bad, eval = FALSE-----------------------------------------------
# nwaa_water_use(
#   model_id      = "wu-irrigation-wd",
#   variable_ids  = "precip",  # not a valid Water Use variable
#   location_type = "huc12",
#   location_id   = "180300010602",
#   range         = "recent"
# )
# #> Error: Unknown variable_ids for model 'wu-irrigation-wd': 'precip'.
# #> Valid variables: irrwdtot, irrwdgw, irrwdsw.

## ----multi-county, eval = FALSE-----------------------------------------------
# library(purrr)
# library(dplyr)
# 
# county_fips <- c("06029", "06031", "06107", "06019")  # Kern, Kings, Tulare, Fresno
# 
# irrig_sjv <- map_dfr(county_fips, function(fips) {
#   nwaa_water_use(
#     model_id      = "wu-irrigation-wd",
#     variable_ids  = c("irrwdtot", "irrwdgw", "irrwdsw"),
#     location_type = "countycd",
#     location_id   = fips,
#     time_res      = "annualwy",
#     range         = "historical"
#   ) |>
#     mutate(county_fips = fips)
# })
# 
# head(irrig_sjv)

