# SeasonSelect() examples:

dates1 <- seq.Date(as.Date("1903-27-02", format = "%Y-%d-%m"), as.Date("1903-02-03", format = "%Y-%d-%m"),1)
dates2 <- seq.Date(as.Date("1904-27-02", format = "%Y-%d-%m"), as.Date("1904-02-03", format = "%Y-%d-%m"),1)
as.numeric(strftime(dates1, format = "%j"))
as.numeric(strftime(dates2, format = "%j"))
dates1 <- as.POSIXct(dates1, tz = "CET")
dates2 <- as.POSIXct(dates2, 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(dates1, "variables") <- metadata
attr(dates2, "variables") <- metadata
as.numeric(strftime(dates1, format = "%j"))
as.numeric(strftime(dates2, format = "%j"))

pos <- which(substr(dates2, 6, 10) == "02-29")
dates2 <- dates2[-pos]
jdays <- as.numeric(strftime(dates2, format = "%j"))
year <- as.numeric(strftime(dates2,format = "%Y"))
pos <- ((year/100)%%1==0) + ((year/4)%%1==0) + ((year/400)%%1==0)
pos <- which(pos == 1 | pos ==3)
pos <- which(jdays[pos] > 59) 
jdays[pos] <- jdays[pos] - 1

dates2 <- dates2[-pos]
as.numeric(strftime(dates2, format = "%j"))
jdays[(pos+1):(366-pos)]

if (length(which(jdays == 1)) == 0) {
  pos2 <- length(jdays)
}

time <- seq.Date(as.Date("1900-06-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
time
jdays <- as.numeric(strftime(time, format = "%j"))
days <- c(c(335 : 365), c(1 : 59))
index <- which(jdays %in% days)
data <- Subset(data, along = timedim, indices = index, drop = "non-selected")

time <- seq.Date(as.Date("1904-01-01", format = "%Y-%d-%m"), as.Date("1904-31-12", format = "%Y-%d-%m"), 1)
length(time)
attributes(time)
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
jdays <- as.numeric(strftime(time, format = "%j"))
data.frame(jdays = jdays, dates = time)

SeasonSelect(data = time, season = "JJA")
SeasonSelect(data = 1:365, season = "JJA")
SeasonSelect(data = 1:365, season = "JJA", timedim = 1)
SeasonSelect(data = 1:365, season = "JJA", timedim = 1, dates = time)-
  SeasonSelect(data = 1:366, season = "JJA", timedim = 1, dates = time)-
  library(climdex.pcic)
SeasonSelect(data = 1:366, season = "JJA", timedim = 1, dates = time)
library(s2dverification)
SeasonSelect(data = 1:366, season = "JJA", timedim = 1, dates = time)


time <- seq.Date(as.Date("1903-01-01", format = "%Y-%d-%m"), as.Date("1904-31-12", format = "%Y-%d-%m"), 1)
jdays <- as.numeric(strftime(time, format = "%j"))
data.frame(jdays = jdays, dates = time) # jdays no tiene en cuenta el 29 de febrero
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
SeasonSelect(data = 1:(365+366), season = "JJA", timedim = 1, dates = time)

fecha <- "1900-01-01"
fecha <- as.Date(fecha)
fecha
as.POSIXct(fecha)
as.POSIXct(fecha, tz = "UTC")
as.POSIXct(fecha, tz = "CEST")
Sys.time()
fecha2 <- as.POSIXct(fecha, tz = "UTC")
trunc(fecha2, "days")

fecha <- seq(ISOdate(1900,1,1), ISOdate(1901,1,1), "days")

data <- 1:(2 * 3 * 365 * 1)
dim(data) <- c(lon = 2, lat = 3, time = 365, model = 1)
time <- seq(ISOdate(1900,1,1), ISOdate(1900,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
attr(data, 'Variables')$dat1$time <- time
attr(data, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(data, 'Variables')$common[[2]]$dim[[3]]$vals <- time

a <- SeasonSelect(data = data, season = 'JJA')
str(a)
a$data
a$dates

## Example 2 with synthetic data:
data <- 1:(2 * 3 * 365 * 2)
dim(data) <- c(lon = 2, lat = 3, time = 365, model = 2)
time <-  seq(ISOdate(1900,1,1), ISOdate(1900,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
attr(data, 'Variables')$dat1$time <- time
attr(data, 'Variables')$dat2$time <- time
attr(data, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(data, 'Variables')$common[[2]]$dim[[3]]$vals <- time

a <- SeasonSelect(data = data, season = 'JJA')
str(a)
a$dates


## Example 2 with synthetic data:
data <- 1:(2 * 3 * (365 + 366)* 2)
dim(data) <- c(lon = 2, lat = 3, time = 365+366, model = 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
attr(data, 'Variables')$dat1$time <- time
attr(data, 'Variables')$dat2$time <- time
attr(data, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
attr(data, 'Variables')$common[[2]]$dim[[3]]$vals <- time

a <- SeasonSelect(data = data, season = 'JJA')
str(a)
a$dates
a$data
a <- SeasonSelect(data = data, season = 'DJF')
str(a)
a$dates
a$data

