#Threshold.R () Examples
devtools::install_git('https://earth.bsc.es/gitlab/es/ClimProjDiags', 
                      branch = 'dev-NAinThreshold')
library(ClimProjDiags)
library(as.PCICt)
Threshold()
Threshold(data <- NULL)
Threshold(data = 1)
Threshold(data = 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
Threshold(data = 1, dates = time)
Threshold(data = 1, dates = time, qtiles = 100)

tmax <- rep(1 : 365, 2)
time <-  seq(ISOdate(1903,1,1), ISOdate(1904,12,31), "days")
#time <- as.POSIXct(time, tz = "CET")
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
calendar <- attributes(time)$variables$time$calendar
dates <- try({
  if (is.character(time)) {
    as.POSIXct(, format = "%Y%m%d")
  } else {
    as.POSIXct(dates)
  }
})
dates <- as.PCICt(time, cal = calendar)
jdays <- as.numeric(strftime(dates, format = "%j"))

if (calendar == "gregorian" | calendar == "standard") {
  if (length(which(substr(dates, 6, 10) == "02-29")) > 0) {
    #dates <- dates[-which(substr(dates, 6, 10) == "02-29")]
    jdays <- as.numeric(strftime(dates, format = "%j"))
    year <- as.numeric(strftime(dates, format = "%Y"))
    pos <- ((year / 100) %% 1 == 0) + ((year / 4) %% 1 == 0) + ((year / 400) %% 1 == 0)
    pos <- which(pos == 0 | pos == 2 | pos == 4)
    pos <- pos[which(jdays[pos] > 59)] 
    jdays[pos] <- jdays[pos] + 1
    #warning("Parameter 'dates' has attributes of 'gregorian' or 'standard' calendar. The 29th of february is being excluded of the computation.")
  }
}


time <-  seq(ISOdate(1901,1,1), ISOdate(1904,12,31), "days")
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
data <- as.numeric(strftime(time, "%j"))
a <- Threshold(data = data, dates = time, base.range = c(1902, 1903), qtiles = 0.9, ncores = NULL)
a
is.array(a)
is.list(a)
length(data)
Threshold(data = data, dates = time)

time <-  seq(ISOdate(1901,6,1), ISOdate(1904,12,31), "days")
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
data <- as.numeric(strftime(time, "%j"))
a <- Threshold(data = data, dates = time, base.range = NULL, qtiles = 0.9, ncores = NULL)
a
summer <-  SeasonSelect(data = data, season = "JJA", dates = time)
str(summer)
summer$dates
summer$data
attr(summer$dates, "variables") <- metadata
Threshold(data = summer$data, dates = summer$dates)


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

a <- Threshold(data, dates = NULL, base.range = c(1900, 1903), qtiles = 0.9, ncores = NULL)
str(a)
dim(a)

