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

## -----------------------------------------------------------------------------
#  library(apsimeval)
#  run_app()                                   # opens on the working directory
#  run_app(out_dir = "~/apsim/sundarbans/outputs")
#  run_app(out_dir = "outputs", project = "sundarbans.apsimproj")
#  run_app(out_dir = "outputs", poll_ms = 1000, launch.browser = FALSE)

## -----------------------------------------------------------------------------
#  sim <- read_out_dir("outputs")              # every .out in the folder
#  apsim_vars("outputs/N120.out")              # variables and units, header only
#  sim <- read_out_dir("outputs", vars = c("wagt", "wrr"))

## -----------------------------------------------------------------------------
#  detect_factors(sim$sim)                     # guesses the separator and fields
#  preview_factors(sim$sim, parts = c("site", "crop", "water", "N"))
#  sim <- set_factors(sim, parts = c("site", "crop", "water", "N"),
#                     numeric_from = "N")

## -----------------------------------------------------------------------------
#  sim <- set_factors(sim, regex = "(?<site>[A-Za-z]+)[-_]N(?<N>\\d+)")
#  map <- factor_template(sim$sim, factors = c("water", "N"))   # fill in and pass back
#  sim <- set_factors(sim, map = map)

## -----------------------------------------------------------------------------
#  obs <- map_obs(read_obs_raw("observed.xlsx"),
#                 date_col = "Sampling_Date", value_cols = c("Grain_yield", "Biomass"),
#                 treatment_col = "Treatment",
#                 # Spreadsheet headings are not APSIM variable names. Without
#                 # this, nothing pairs and nothing plots.
#                 var_map = c(Grain_yield = "wrr", Biomass = "wagt"))
#  
#  check_variables(obs, sim)                   # catches variable name mismatches
#  check_levels(obs, sim)                      # catches treatment label mismatches
#  
#  long <- as_long(sim, vars = "wrr")
#  pr   <- pair_obs(long, obs, tol_days = 3)
#  
#  cut <- suggest_cutoff(pr)                   # first season calibrates
#  pr  <- split_phase(pr, "date", at = cut)
#  
#  # Statistics are never pooled across variables: RMSE would mix units and R2
#  # would be inflated by the gap between the variables rather than model skill.
#  gof("obs", "pred", data = pr, by = "variable")
#  gof("obs", "pred", data = pr, by = c("variable", "phase"))
#  gof("obs", "pred", data = pr, by = c("variable", "phase", "N"))

## -----------------------------------------------------------------------------
#  plot_ts(long, obs = obs, cutoff = cut)
#  # Colouring is a presentation choice and does not affect the statistics; the
#  # corner box follows `stats_by`, which defaults to the faceting column.
#  plot_one2one(pr, group = "N", stats_by = character(0))   # pooled box
#  plot_one2one(pr, group = "N", facet = "phase", sd_band = TRUE)

## -----------------------------------------------------------------------------
#  ag <- aggregate_sim(sim, by = c("sim", "year", "water", "N"),
#                      vars = c(wrr = "max"), crop_only = TRUE)
#  plot_exceedance(ag, "wrr", group = "N", facet = "water", ref = 7000)
#  plot_bar(ag, "wrr", "N", letters = TRUE)
#  
#  sens_anova(ag, "wrr", c("site", "water", "N"), order = 2)
#  sens_oat(ag, "wrr", c("site", "water", "N"))

## -----------------------------------------------------------------------------
#  set_labels(list(wrr = "Grain yield"))
#  set_palette(c(N0 = "#D55E00", N120 = "#0072B2"))
#  set_colour_mode("grey")                     # "colour", "grey", or "mono"
#  
#  fig <- build_figure(list(
#    panel_spec("one2one", group = "N", facet = "phase"),
#    panel_spec("exceedance", var = "wrr", group = "N", ref = 7000),
#    panel_spec("bar", var = "wrr", group = "N", letters = TRUE)
#  ), sim = sim, paired = pr, ncol = 3)
#  
#  save_pub(fig, "figure_2", width = "double", height = 80,
#           dpi = 600, format = c("tiff", "pdf"))

## -----------------------------------------------------------------------------
#  p <- new_project("sundarbans_rice", "outputs")
#  p <- set_project_factors(p, sim)
#  p$labels <- list(wrr = "Grain yield"); p$colour_mode <- "grey"
#  save_project(p, "sundarbans_rice.apsimproj")
#  
#  p <- load_project("sundarbans_rice.apsimproj")
#  sim <- apply_factors(read_out_dir("outputs"), p)   # after a re-run

## -----------------------------------------------------------------------------
#  raw <- read_obs_raw("observed.xlsx")
#  attr(raw, "guess")                # suggested date / treatment / rep / value columns
#  
#  obs <- map_obs(raw, date_col = "Sampling_Date", value_cols = "Biomass",
#                 treatment_col = c("Site", "Treatment"),   # joined to match sim names
#                 rep_col = "Replication")

## -----------------------------------------------------------------------------
#  plot_ts(long, obs = obs, cutoff = cut)          # error bars drawn automatically
#  plot_ts(long, obs = obs, cutoff = cut, obs_sd = NULL)   # suppress them
#  plot_one2one(pr, obs_sd = "obs_sd")             # horizontal bars on the scatter

## -----------------------------------------------------------------------------
#  long <- as_long(sim, vars = c("wagt", "pond"), keep = c("water", "N"))
#  plot_series(long, vars = c("wagt", "pond"), secondary = "pond")

## -----------------------------------------------------------------------------
#  plot_series(long, vars = c("wagt", "wrr", "pond"), layout = "stacked")

## -----------------------------------------------------------------------------
#  plot_series(long, vars = c("wagt", "pond"), secondary = "pond",
#              facet_by = "sim", ncol = 2)             # every simulation
#  plot_series(long, vars = "wagt", facet_by = "water") # or a factor column
#  plot_series(long, vars = "wagt", facet_by = "water",
#              colour_by = "treatment")                # colour instead of panels

## -----------------------------------------------------------------------------
#  plot_series(long, vars = c("wagt", "wrr"), layout = "stacked", facet_by = "sim")
#  plot_series(long, vars = "wagt", facet_by = "sim", obs = obs, cutoff = cut)

## -----------------------------------------------------------------------------
#  plot_series(long, vars = c("wagt", "wrr", "pond"), secondary = "pond", obs = obs)
#  #> No observed data for: pond (simulated only).

## -----------------------------------------------------------------------------
#  plot_series(long, vars = "wagt", facet_by = "water", obs = obs)  # obs has only `sim`

