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

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

## -----------------------------------------------------------------------------
# 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:2012,
#                  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:2012,
#                  month = 1,
#                  leadtime_month = 1:3,
#                  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-----------------------------------------------------------------
# # Hindcast data
# c4h_plot(hcst, ensemble = TRUE, coordgrid = TRUE)
# 
# # Observations
# c4h_plot(rean, ensemble = TRUE, coordgrid = TRUE)

## ----c4h_downscale_parameters,fig.width=10, fig.height=10, fig.align='center', out.width='100%', echo= FALSE, fig.cap="Figure 1: Structure of the c4h_downscale function, outlining its key arguments and general functionality."----
# knitr::include_graphics("https://gitlab.earth.bsc.es/ghr/clim4health/-/blob/main/inst/figures/clim4health_downscale_parameters.svg?ref_type=heads")

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

## -----------------------------------------------------------------------------
# c4h_plot(dwn$exp, ensemble = TRUE)

## -----------------------------------------------------------------------------
# c4h_plot(rean)

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

## -----------------------------------------------------------------------------
# c4h_plot(dwn$exp, ensemble = TRUE)

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

## -----------------------------------------------------------------------------
# c4h_plot(dwn$exp, ensemble = TRUE)

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

## -----------------------------------------------------------------------------
# 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:2012,
#                                     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------------------------------------------------------
# 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")

## ----plot_point_interpolation-------------------------------------------------
# c4h_plot(dwn$exp, ensemble = TRUE, boundaries = "countries")

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

## -----------------------------------------------------------------------------
# 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)

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

## -----------------------------------------------------------------------------
# c4h_plot(hcst_aggr, ensemble = TRUE, boundaries = "countries")
# c4h_plot(cal$exp,   ensemble = TRUE, boundaries = "countries")
# c4h_plot(rean_aggr, ensemble = TRUE, boundaries = "countries")

## ----c4h_downscale_flowchart,fig.width=10, fig.height=10, fig.align='center', out.width='100%', echo= FALSE, fig.cap="Figure 2: Flowchart for selecting downscaling parameters. Dashed lines indicate optional arguments."----
# knitr::include_graphics("https://gitlab.earth.bsc.es/ghr/clim4health/-/blob/develop-vignettes/inst/figures/c4h_downscale.svg?ref_type=heads")

