## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

## ----load-package-------------------------------------------------------------
library(cropwatMUL)

## ----example-file-------------------------------------------------------------
input_file <- system.file(
  "extdata",
  "cropwat_example.xlsx",
  package = "cropwatMUL"
)

stopifnot(nzchar(input_file))
basename(input_file)

## ----validate-input-----------------------------------------------------------
validate_cropwat_input(
  input_file = input_file,
  crop_name = "Potato"
)

## ----run-model----------------------------------------------------------------
result <- run_cropwat_one_crop_multilocation(
  input_file = input_file,
  crop_name = "Potato",
  irrigation_efficiency = 0.70,
  critical_depletion = 0.35,
  root_initial_m = 0.15,
  u2 = 2,
  krs = 0.16,
  altitude_m = 0,
  initial_depletion_mm = 0
)

## ----result-names-------------------------------------------------------------
names(result)

## ----seasonal-summary---------------------------------------------------------
result$summaries

## ----monthly-output-----------------------------------------------------------
head(result$monthly)

## ----dekadal-output-----------------------------------------------------------
head(result$dekadal)

## ----daily-output-------------------------------------------------------------
head(
  result$daily[
    result$daily$in_crop,
  ]
)

## ----export-results, warning=FALSE, message=FALSE-----------------------------
vignette_output_dir <- tempfile("cropwatMUL-vignette-")
dir.create(
  vignette_output_dir,
  recursive = TRUE,
  showWarnings = FALSE
)

output <- write_cropwat_outputs(
  results = result,
  output_dir = vignette_output_dir,
  prefix = "Potato_cropwatMUL_example"
)

c(
  excel_file = basename(output$excel_file),
  txt_directory = basename(output$txt_dir),
  summary_file = basename(output$all_locations_summary)
)

unlink(
  vignette_output_dir,
  recursive = TRUE,
  force = TRUE
)

## ----effective-rainfall-------------------------------------------------------
usda_scs_effective_rain(
  c(50, 150, 300)
)

## ----eto-example--------------------------------------------------------------
pm_temp_estimated(
  tmax = 25,
  tmin = 15,
  lat = 25.7,
  doy = 100,
  altitude_m = 1000,
  u2 = 2,
  krs = 0.16
)

