# DTRRef example
DTRRef()
DTRRef(tmax = NULL)
DTRRef(tmin = NULL)
DTRRef(tmax = NULL, tmin = NULL)
DTRRef(tmax = 0, tmin = 0)
DTRRef(tmax = "a", tmin = 0)
DTRRef(tmax = 0, tmin = "b")
DTRRef(tmax = list(0, 0), tmin = 0)
DTRRef(tmax = list(1, 1), tmin = 0)
DTRRef(tmax = c(0, 0), tmin = 0)
tmax <- 1 : 10
dim(tmax) <- c(2, 5)
tmin <- 1 : 10
dim(tmin) <- c(5, 2)
DTRRef(tmax = tmax, tmin = tmin)
tmax <- 1 : 10
dim(tmax) <- c(2, 5)
tmin <- 1 : 10
dim(tmin) <- c(a = 2, b = 5)
DTRRef(tmax = tmax, tmin = tmin)
tmax <- 1 : 10
dim(tmax) <- c(2, 5)
tmin <- 1 : 10
dim(tmin) <- c(a = 2, time = 5)
DTRRef(tmax = tmax, tmin = tmin)
tmax <- 1 : 10
dim(tmax) <- c(a = 2, time = 5)
tmin <- 1 : 10
dim(tmin) <- c(a = 2, time = 5)
DTRRef(tmax = tmax, tmin = tmin)
time <- seq(as.Date("01-01-1900", "%d-%m-%Y"), as.Date("03-01-1900", "%d-%m-%Y"), 1)
DTRRef(tmax = tmax, tmin = tmin, dates = time)
time <- as.POSIXct(time, tz = "CET")
metadata <- list(time = list(standard_name = 'time', long_name = 'time', 
                             calendar = 'noleap',
                             units = 'days since 1970-01-01 00:00:00', prec = 'double', 
                             dim = list(list(name ='time', unlim = FALSE))))
attr(time, "variables") <- metadata
DTRRef(tmax = tmax, tmin = tmin, dates = time)
time <- seq(as.Date("01-01-1900", "%d-%m-%Y"), as.Date("05-01-1900", "%d-%m-%Y"), 1)
time <- as.POSIXct(time, tz = "CET")
metadata <- list(time = list(standard_name = 'time', long_name = 'time', 
                             calendar = 'noleap',
                             units = 'days since 1970-01-01 00:00:00', prec = 'double', 
                             dim = list(list(name ='time', unlim = FALSE))))
attr(time, "variables") <- metadata
a <- DTRRef(tmax = tmax, tmin = tmin, dates = time)
a
dim(a$dtr.ref)

time <- seq(as.Date("01-01-1900", "%d-%m-%Y"), as.Date("05-01-1900", "%d-%m-%Y"), 1)
time <- as.POSIXct(time, tz = "CET")
metadata <- list(time = list(standard_name = 'time', long_name = 'time', 
                             calendar = 'noleap',
                             units = 'days since 1970-01-01 00:00:00', prec = 'double', 
                             dim = list(list(name ='time', unlim = FALSE))))
attr(time, "variables") <- metadata
attr(tmax, 'Variables')$dat1$time <- time
attr(tmax, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(tmax, 'Variables')$common[[2]]$dim[[3]]$vals <- time
DTRRef(tmax = tmax, tmin = tmin)
attr(tmax, 'Variables')$dat1$time <- time
attr(tmax, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(tmax, 'Variables')$common[[2]]$dim[[3]]$vals <- time
attr(tmin, 'Variables')$dat1$time <- time
attr(tmin, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(tmin, 'Variables')$common[[2]]$dim[[3]]$vals <- time
time <- seq(as.Date("01-01-1900", "%d-%m-%Y"), as.Date("03-01-1900", "%d-%m-%Y"), 1)
DTRRef(tmax = tmax, tmin = tmin)

time <- as.POSIXct(time, tz = "CET")
metadata <- list(time = list(standard_name = 'time', long_name = 'time', 
                             calendar = 'noleap',
                             units = 'days since 1970-01-01 00:00:00', prec = 'double', 
                             dim = list(list(name ='time', unlim = FALSE))))
attr(time, "variables") <- metadata
DTRRef(tmax = tmax, tmin = tmin, dates = time, timedim = 2)
library(climdex.pcic)
DTRRef(tmax = tmax, tmin = tmin, dates = time, timedim = 2)
library(multiApply)
DTRRef(tmax = tmax, tmin = tmin, dates = time, timedim = 2)


tmax <- 1:(2 * 3 * 365 * 1)
dim(tmax) <- c(lon = 2, lat = 3, time = 365, model = 1)
tmin <- (1:(2 * 3 * 365 * 1))- 100 * sin(2*pi*1:365/365)
dim(tmin) <- c(lon = 2, lat = 3, time = 365, model = 1)
time <- seq.Date(as.Date("1900-01-01", format = "%Y-%d-%m"), as.Date("1900-31-12", format = "%Y-%d-%m"), 1)
time <- as.POSIXct(time, tz = "CET")
metadata <- list(time = list(standard_name = 'time', long_name = 'time', 
                             calendar = 'noleap',
                             units = 'days since 1970-01-01 00:00:00', prec = 'double', 
                             dim = list(list(name ='time', unlim = FALSE))))
attr(time, "variables") <- metadata
attr(tmax, 'Variables')$dat1$time <- time
attr(tmax, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(tmax, 'Variables')$common[[2]]$dim[[3]]$vals <- time
attr(tmin, 'Variables')$dat1$time <- time
attr(tmin, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(tmin, 'Variables')$common[[2]]$dim[[3]]$vals <- time

a <- DTRRef(tmax, tmin, by.seasons = FALSE, ncores = NULL)
str(a)
dim(a$dtr.ref)
a <- DTRRef(tmax, tmin, by.seasons = TRUE, ncores = NULL)
str(a)
dim(a$dtr.ref)

tmax <- 1:(2 * 3 * 365 * 1)
dim(tmax) <- c(2, 3, 365)
tmin <- (1:(2 * 3 * 365 * 1))-1
dim(tmin) <- c(2, 3, 365)

a <- DTRRef(tmax, tmin, by.seasons = FALSE, dates = time,  timedim = 3, ncores = NULL)
str(a)
dim(a$dtr.ref)
a <- DTRRef(tmax, tmin, by.seasons = TRUE, dates = time,  timedim = 3, na.rm = FALSE, ncores = NULL)
str(a)
dim(a$dtr.ref)
tmax[1,2,10:15] <- NA
a <- DTRRef(tmax, tmin, by.seasons = TRUE, dates = time,  timedim = 3, na.rm = TRUE, ncores = NULL)
str(a)
a$dtr.ref
sum(is.na(a$dtr.ref)); sum(is.na(tmax))
dim(a$dtr.ref)
a <- DTRRef(tmax, tmin, by.seasons = TRUE, dates = time,  timedim = 3, na.rm = FALSE, ncores = NULL)
a
sum(is.na(a$dtr.ref))

