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

## -----------------------------------------------------------------------------
library(xyt)

## -----------------------------------------------------------------------------
read <- synthetic_reader()
head(read(returnfiles = TRUE))

## -----------------------------------------------------------------------------
xyt <- data.frame(
  x = c(0.5, 2.5),
  y = c(0.5, 3.5),
  t = as.POSIXct(c("2000-01-01", "2000-01-05"), tz = "UTC")
)

extract_xyt(read, xyt)

## -----------------------------------------------------------------------------
mid <- data.frame(x = 0.5, y = 0.5,
                  t = as.POSIXct("2000-01-03 12:00:00", tz = "UTC"))

extract_xyt(read, mid)                   # nearest slice
extract_xyt(read, mid, ctstime = TRUE)   # interpolated in time

## -----------------------------------------------------------------------------
extract_xyt(read, mid, method = "bilinear")
extract_xyt(read, mid, ctstime = TRUE, method = "bilinear")

## -----------------------------------------------------------------------------
off <- data.frame(x = 0.5, y = 0.5,
                  t = as.POSIXct("2001-06-01", tz = "UTC"))
extract_xyt(read, off)

## -----------------------------------------------------------------------------
invisible(xyt_reader_check(synthetic_reader()))

## -----------------------------------------------------------------------------
r <- lapply(1:2, function(i) {
  x <- terra::rast(terra::ext(0, 4, 0, 4), resolution = 1, crs = "EPSG:4326")
  terra::values(x) <- i
  x
})

src <- xyt_source(
  slice = function(date, files, ...) r[[which(files$date == date)]],
  catalogue = data.frame(
    date = as.POSIXct(c("2000-01-01", "2000-01-02"), tz = "UTC")
  )
)

extract_xyt(src, data.frame(x = 1, y = 1,
                            t = as.POSIXct("2000-01-02", tz = "UTC")))

## ----eval = FALSE-------------------------------------------------------------
# library(geodata)
# 
# ## twelve monthly layers: wc2.1_10m_tavg_01 ... _12
# tavg <- worldclim_global(var = "tavg", res = 10, path = tempdir())
# 
# ## a catalogue of one row per month, dated to a representative year
# catalogue <- data.frame(
#   date = as.POSIXct(sprintf("2020-%02i-15", 1:12), tz = "UTC")
# )
# 
# worldclim_src <- xyt_source(
#   slice = function(date, files, ...) {
#     i <- which(files$date == date)
#     tavg[[i]]
#   },
#   catalogue = catalogue
# )
# 
# ## a couple of points, each in a different month
# pts <- data.frame(
#   lon  = c(147.3, 2.35),
#   lat  = c(-42.9, 48.85),
#   time = as.POSIXct(c("2020-01-15", "2020-07-15"), tz = "UTC")
# )
# 
# ## check the reader against the contract, then extract
# xyt_reader_check(worldclim_src)
# extract_xyt(worldclim_src, pts)
# 
# ## interpolate between monthly slices for points mid-month
# extract_xyt(worldclim_src, pts, ctstime = TRUE, method = "bilinear")

## ----eval = FALSE-------------------------------------------------------------
# mirai::daemons(6)
# mirai::everywhere({ library(terra) })   # load whatever the reader needs
# 
# extract_xyt(worldclim_src, pts)
# 
# mirai::daemons(0)

