## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  warning = FALSE,
  message = FALSE,
  fig.width = 7,
  fig.align = "center"
)

## ----load_package-------------------------------------------------------------
library(clim4health)

## ----vignette_objects, echo = FALSE-------------------------------------------
# c4h_downscale() requires CDO, which may not be available when this vignette
# is built. The code calls are shown but not run; the pre-computed results are
# loaded instead (see dev-utils/vignette_objects.R).
load_vignette_object <- function(name) {
  readRDS(system.file(
    "extdata",
    "vignette_objects",
    paste0("downscaling_", name, ".rds"),
    package = "clim4health",
    mustWork = TRUE
  ))
}

## -----------------------------------------------------------------------------
fcst_path <- system.file("extdata/forecast/", package = "clim4health")
fcst_path <- paste0(fcst_path, "/")
fcst <- c4h_load(
  fcst_path,
  variable = "t2m",
  year = 2025,
  month = 1,
  leadtime_month = "all",
  ext = "nc"
)

hcst_path <- system.file("extdata/hindcast/", package = "clim4health")
hcst_path <- paste0(hcst_path, "/")
hcst <- c4h_load(
  hcst_path,
  variable = "t2m",
  year = 2010:2013,
  month = 1,
  leadtime_month = "all",
  ext = "nc"
)

rean_path <- system.file("extdata/reanalysis/", package = "clim4health")
rean_path <- paste0(rean_path, "/")
rean <- c4h_load(
  rean_path,
  variable = "t2m",
  year = 2010:2013,
  month = 1,
  leadtime_month = 1:3,
  bbox = c(5.5, -78.5, 1.5, -74.5),
  ext = "nc"
)

## ----convert_units------------------------------------------------------------
fcst <- c4h_convert_units(fcst, "celsius")
hcst <- c4h_convert_units(hcst, "celsius")
rean <- c4h_convert_units(rean, "celsius")

## ----explore_data-------------------------------------------------------------
print(rean$dims)
print(hcst$dims)

print(rean$coords$latitude)
print(hcst$coords$latitude)

## ----plot_raw, fig.height=6, fig.width=6--------------------------------------
# Hindcast data
c4h_plot(hcst, ensemble = TRUE, coordgrid = TRUE)

# Observations
c4h_plot(rean, ensemble = TRUE, coordgrid = TRUE)

## ----dwn_interp, eval = FALSE-------------------------------------------------
# dwn <- c4h_downscale(
#   "Interpolation",
#   exp = hcst,
#   method_remap = "bilinear",
#   target_grid = paste0(rean_path, "t2m_201001.nc")
# )

## ----dwn_interp_load, echo = FALSE--------------------------------------------
dwn <- load_vignette_object("interpolation")

## ----fig.height=6, fig.width=6------------------------------------------------
c4h_plot(dwn$exp, ensemble = TRUE)

## ----fig.height=6, fig.width=6------------------------------------------------
c4h_plot(rean)

## ----dwn_intbc, eval = FALSE--------------------------------------------------
# dwn <- c4h_downscale(
#   "Intbc",
#   exp = hcst,
#   obs = rean,
#   method_bc = "evmos",
#   method_remap = "bilinear"
# )

## ----dwn_intbc_load, echo = FALSE---------------------------------------------
dwn <- load_vignette_object("intbc")

## ----fig.height=6, fig.width=6------------------------------------------------
c4h_plot(dwn$exp, ensemble = TRUE)

## ----dwn_intlr, eval = FALSE--------------------------------------------------
# dwn <- c4h_downscale(
#   "Intlr",
#   exp = hcst,
#   obs = rean,
#   exp_cor = fcst,
#   method_lr = "basic",
#   method_remap = "con"
# )

## ----dwn_intlr_load, echo = FALSE---------------------------------------------
dwn <- load_vignette_object("intlr")

## ----fig.height=2.5-----------------------------------------------------------
c4h_plot(dwn$exp, ensemble = TRUE)

## ----dwn_analogs, eval = FALSE------------------------------------------------
# dwn <- c4h_downscale("Analogs", exp = hcst, obs = rean)

## ----dwn_analogs_load, echo = FALSE-------------------------------------------
dwn <- load_vignette_object("analogs")

## ----fig.height=6, fig.width=6------------------------------------------------
c4h_plot(dwn$exp, ensemble = TRUE)

## -----------------------------------------------------------------------------
stat_path <- system.file("extdata/stations/", package = "clim4health")
point_locs <- clim4health::c4h_load(
  stat_path,
  ext = "csv",
  year = 2010:2013,
  month = 1,
  leadtime_month = 1:3,
  variable = "temp_mean"
)

# use c4h_time to obtain monthly data
point_locs <- c4h_time(
  point_locs,
  time_aggregation = "monthly",
  dim_aggregation = "sdate"
)

## ----point_interpolation, eval = FALSE----------------------------------------
# points <- list(
#   latitude = point_locs$attrs$location$latitude,
#   longitude = point_locs$attrs$location$longitude
# )
# 
# dwn <- c4h_downscale(
#   "Interpolation",
#   exp = hcst,
#   points = points,
#   method_point_interp = "bilinear"
# )

## ----point_interpolation_load, echo = FALSE-----------------------------------
dwn <- load_vignette_object("point_interpolation")

## ----plot_point_interpolation, fig.height=6, fig.width=6----------------------
c4h_plot(dwn$exp, ensemble = TRUE, boundaries = "countries")

## ----point_intbc, eval = FALSE------------------------------------------------
# dwn <- c4h_downscale(
#   "Intbc",
#   exp = hcst,
#   obs = rean,
#   points = points,
#   method_point_interp = "bilinear",
#   method_bc = "evmos"
# )

## ----point_intbc_load, echo = FALSE-------------------------------------------
dwn <- load_vignette_object("point_intbc")

## -----------------------------------------------------------------------------
library(sf)

# Region municipalities
munip_path <- system.file(
  "extdata",
  "areas",
  "munip_vallecauca.gpkg",
  package = "clim4health"
)
munip <- read_sf(munip_path)

hcst_aggr <- c4h_space(hcst, munip)
rean_aggr <- c4h_space(rean, munip)

## ----calibration, eval = FALSE------------------------------------------------
# cal <- c4h_downscale(
#   "Intbc",
#   exp = hcst_aggr,
#   obs = rean_aggr,
#   method_bc = "bias"
# )

## ----calibration_load, echo = FALSE-------------------------------------------
cal <- load_vignette_object("calibration")

## ----fig.height=6, fig.width=6------------------------------------------------
c4h_plot(hcst_aggr, ensemble = TRUE, boundaries = "countries")
c4h_plot(cal$exp, ensemble = TRUE, boundaries = "countries")
c4h_plot(rean_aggr, ensemble = TRUE, boundaries = "countries")

