# Climdex examples
Climdex()
Climdex(data = NULL)
Climdex(data = NULL, metric = NULL)
Climdex(data = "a", metric = 1)
Climdex(data = 1, metric = 1)
Climdex(data = 1, metric = 1, dates = ISOdate(1900, 1, 1))
time = ISOdate(1900, 1, 1)
metadata <- list(time = list(standard_name = 'time', long_name = 'time', 
                             calendar = 'standard',
                             units = 'days since 1970-01-01 00:00:00', prec = 'double', 
                             dim = list(list(name ='time', unlim = FALSE))))
attr(time, "variables") <- metadata
a <- Climdex(data = 1, metric = 1, dates = time)
attributes(a)
str(a)
a
Climdex(data = 1, metric = "t")
Climdex(data = 1, metric = "t", dates = time)
Climdex(data = 1, metric = "t90p", dates = time)
Climdex(data = 1, metric = "t90p", threshold = 1, dates = time)
Climdex(data = 1:10, metric = "t90p", threshold = 1, dates = time)

time = seq(ISOdate(1900, 1, 1), ISOdate(1900, 1, 10), "day")
attr(time, "variables") <- metadata
Climdex(data = 1:10, metric = "t90p", threshold = 1, dates = time)
Climdex(data = 1:10, metric = "t90p", threshold = 1 : 10, dates = time)
Climdex(data = 1:10, metric = "t90p", threshold = 0 : 9, dates = time)
Climdex(data = 1:10, metric = "t90p", threshold = 5, dates = time)
Climdex(data = 1:10, metric = "Wx", threshold = 1, dates = time)
Climdex(data = 1:10, metric = "t10p", threshold = 5, dates = time)
Climdex(data = 1:10, metric = "Wx", threshold = 1 : 10, dates = time)
Climdex(data = 1:10, metric = "Wx", threshold = 1, dates = time, timedim = 2)
Climdex(data = 1:10, metric = "Wx", threshold = 1, dates = time, timedim = 2 : 3)
Climdex(data = 1:10, metric = "cdd", threshold = 1, dates = time, timedim = 2 : 3)
Climdex(data = 1:10, metric = "cdd", dates = time, timedim = 2 : 3)
Climdex(data = 1:10, metric = "cdd", dates = time, timedim = 1)

Climdex(data = 1, metric = "t90p")


# No funciona Apply ... corregido reutilizando threshold 
data <- 1 : 36
dim(data) <- c(9, 4)
thres <- 1 : 3
time = c(seq(ISOdate(1900, 1, 1), ISOdate(1900, 1, 3), "day"),
         seq(ISOdate(1901, 1, 1), ISOdate(1901, 1, 3), "day"),
         seq(ISOdate(1902, 1, 1), ISOdate(1902, 1, 3), "day"))
attr(time, "variables") <- metadata
Climdex(data = data, metric = "t90p", dates = time, threshold = thres)
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 2)
a <- Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1)
class(a)
class(a$result)
class(a$years)

data <- 1 : ((365 * 3 + 366) * 2)
dim(data) <- c((365 * 3 + 366), 2)
thres <- 1 : 366
time = seq(ISOdate(1901, 1, 1), ISOdate(1904, 12, 31), "day")
attr(time, "variables") <- metadata
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1)

thres = cbind(thres, thres)
dim(thres) = c(jdays = 366, a = 2)
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1)

data <- 1 : 10
thres <- 1
time = seq(ISOdate(1901, 1, 1), ISOdate(1910, 1, 1), "year")
attr(time, "variables") <- metadata
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1) 

data <- 1 : 10
thres <- 1 :10
time = seq(ISOdate(1901, 1, 1), ISOdate(1910, 1, 1), "year")
attr(time, "variables") <- metadata
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1) # Error correcto

data <- 1 : 10
dim(data) <- c(5, 2)
thres <- 1 
time = seq(ISOdate(1901, 1, 1), ISOdate(1905, 1, 1), "year")
attr(time, "variables") <- metadata
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1) # Warning correcto

data <- 1 : 20
dim(data) <- c(10, 2)
thres <- 1:2 
time = sort(c(seq(ISOdate(1901, 1, 1), ISOdate(1905, 1, 1), "year"),
              seq(ISOdate(1901, 1, 2), ISOdate(1905, 1, 2), "year")))
attr(time, "variables") <- metadata
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1) # Warning correcto

data <- 1 : 20
dim(data) <- c(10, 2)
thres <- 1:5 
time = sort(c(seq(ISOdate(1901, 1, 1), ISOdate(1905, 1, 1), "year"),
              seq(ISOdate(1901, 1, 2), ISOdate(1905, 1, 2), "year")))
attr(time, "variables") <- metadata
Climdex(data = data, metric = "t90p", dates = time, threshold = thres, timedim = 1) # Error correcto

#'Example
data <- 1:(2 * 3 * 372 * 1)
dim(data) <- c(lon = 2, lat = 3, time = 372, model = 1)
time <- as.POSIXct(paste(sort(rep(1900:1911, 31)), 1, 1:31, sep = "-"), 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(data, 'Variables')$dat1$time <- time

thres <- Threshold(data, dates = NULL, base.range = c(1900, 1903), qtiles = 0.9, ncores = NULL)
str(thres)
dim(thres)
clim <- Climdex(data, metric = "t90p", threshold = thres)
str(clim)
