
load("metricdata1.RData")
library(ClimProjDiags)
dim(projection_data$data)
dim(thresholds)
unique(jdays <- as.numeric(strftime(projection_data$dates, format = "%j")))
length(unique(jdays <- as.numeric(strftime(projection_data$dates, format = "%j"))))

res <- Climdex(projection_data$data, metric, thresholds, dates = projection_data$dates, 
               calendar = calendar)
str(res)

fechas <- integer(0)
class(fechas) <- "Date"

for(i in 1 : 21) {
  year <- 2019 + i
  dia <- as.Date(paste0(year, "-01-01"), "%Y-%m-%d")
  # pos <- ((year / 100) %% 1 == 0) + ((year / 4) %% 1 == 0) + ((year / 400) %% 1 == 0)
  # num <- ifelse(pos == 1, 92, 91)
  num <- 91
  fechas <- c(fechas, seq(dia, dia + num, "day"))
print(fechas[1])
}
length(fechas)
      # fechas <- fechas[format(fechas, "%m-%d") != "02-29"]
length(fechas)
length(projection_data$dates)

jdays <- as.numeric(strftime(fechas, format = "%j"))

res2 <- Climdex(projection_data$data, metric, thresholds, 
               dates = fechas, 
               calendar = calendar)
matplot(1 : 92, thresholds[, 1,], type = "l")
plot(1:92, projection_data$data[1:92,1,1], type = "h", lwd = 3, col = "darkgrey")
lines(1:92, thresholds[,1,1], type = "l", col ="blue")
sum(projection_data$data[1:92, 1,1] >= thresholds[,1,1])
data = projection_data$data
metric = 't90p'
threshold = thresholds
base.range = NULL
dates = projection_data$dates
timedim = NULL
calendar = '365_day'
ncores = NULL

dim(data)
dim(threshold)

.Climdex(data = data[,1,1], threshold = threshold[,1,1], 
         dates = dates, metric = metric, 
         jdays = rep(1 : 92, 21), date.factor = as.factor(sort(rep(2020: 2040, 92))), 
         base.range = base.range)

