source("/esarchive/scratch/nperez/git/cstools/R/CST_QuantileMapping.R")
library(multiApply)
library(qmap)
library(abind)
exp <- 1:(2*3*4)
obs <- 1:(2*3*4)
dim(exp) <- c(time= 2, member = 3, sdates = 4)
dim(obs) <- c(time= 2, member = 3, sdates = 4)
QuantileMapping(exp, obs, sample_dims = c('time', 'member', 'sdates'))
QuantileMapping(exp, obs, sample_dims = c('time', 'member'))
exp <- 1:(2*3*4)
obs <- 1:(2*1*4)
dim(exp) <- c(time= 2, member = 3, sdates = 4)
dim(obs) <- c(time= 2, member = 1, sdates = 4)
QuantileMapping(exp, obs, sample_dims = c('time', 'member', 'sdates'))
QuantileMapping(exp, obs, sample_dims = c('time', 'member'))
QuantileMapping(exp, obs, sample_dims = c('time'))
QuantileMapping(exp, obs, sample_dims = c('time', 'sdates'))
# check if
obs <- 1:(2*3*1*4)
dim(obs) <- c(time = 2, sdate = 1, member = 3, var = 4)
obsdims <- c('time', 'sdate', 'member', 'var')
expdims <- c('sdate', 'member', 'time')
obsdims %in% expdims
obsdims[obsdims %in% expdims]
commondims <- obsdims[obsdims %in% expdims]
sample_dims = c('time', 'member')
commondims[commondims %in% sample_dims]
if (any(!(commondims %in% sample_dims))) {
   todrop <- commondims[!(commondims %in% sample_dims)]
   todrop <- match(todrop, obsdims)
   obs <- adrop(obs, drop = todrop)
}






pdf("../CST_QuantileMapping_plots.pdf")
# Testing basic function qmapcor:
## The input are two arrays with the same number of dimension, one should be time or ftime.
exp <-  1:20
obs <- 1:10
dim(exp) <- c(time= 10, member= 2)
dim(obs) <- c(time= 10, member = 1)
res <- qmapcor(exp, obs, sample_length = NULL, method = 'QUANT', qstep = 0.1)
# Ex1:
exp <- 1:20
dim(exp) <- c(time = 20)
obs <- 101:120
dim(obs) <- c(time = 20)
exp_cor <- 51:70
dim(exp_cor) <- c(time = 20)
res <- qmapcor(exp, obs, sample_length = NULL, method = 'QUANT', qstep = 0.1)
res <- qmapcor(exp, obs, sample_length = 5, method = 'QUANT', qstep = 0.1)
res <- qmapcor(exp, obs, sample_length = 3, method = 'QUANT', qstep = 0.1)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = NULL, method = 'QUAN', qstep = 0.1)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = 5, method = 'QUAN', qstep = 0.1)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = 3, method = 'QUAN', qstep = 0.1)
exp_cor <- 51:60
dim(exp_cor) <- c(time = 10)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = NULL, method = 'QUAN', qstep = 0.1)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = 5, method = 'QUAN', qstep = 0.1)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = 3, method = 'QUAN', qstep = 0.1)
exp_cor <- 51:80
dim(exp_cor) <- c(time = 30)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = NULL, method = 'QUAN', qstep = 0.1)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = 5, method = 'QUAN', qstep = 0.1)
res <- qmapcor(exp, obs, exp_cor = exp_cor, sample_length = 4, method = 'QUAN', qstep = 0.1)
res <- qmapcor(exp, obs, sample_length = 5, method = 'RQUAN', qstep = 0.1)
res <- qmapcor(exp, obs, sample_length = 5, method = 'SSPLIN')
res <- qmapcor(exp, obs, sample_length = 5, method = 'PTF')
res <- qmapcor(exp, obs, sample_length = 5, method = 'DIST')  # Not work.

exp <- 1:40
dim(exp) <- c(time = 20, sdate = 2)
obs <- 1: 20
dim(obs) <- c(time = 20, sdate = 1)
res <- qmapcor(exp, obs, sample_length = 5, method = 'QUAN', qstep = 0.1)

exp <- 1:400
dim(exp) <- c(time = 20, sdate = 2, member = 10)
obs <- 1: 20
dim(obs) <- c(time = 20, sdate = 1, member = 1)
res <- qmapcor(exp, obs, sample_length = 5, method = 'QUAN', qstep = 0.1)

exp <- 1:20
dim(exp) <- c(time = 20)
obs <- 101:120
dim(obs) <- c(time = 20)
res <- qmapcor(exp, obs, sample_length = 3, method = 'QUAN', qstep = 0.1)


dim(res) # the same as exp
length(res)
res
plot(exp, obs, pch = 16, main = "Test1 QUANT in qmapcor()",
     ylim = c(min(obs, res), max(obs, res)))
lines(exp, res, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp-res'), pch = 16, col= c(1, 2), bty = 'n')
# Ex2:
exp <- 1:10
dim(exp) <- c(time =10)
obs <- 101:120
res <- qmapcor(exp, obs)
dim(res)
res
plot(exp, obs[1:10], pch = 16, main = "Test2 QUANT in qmapcor()",
     ylim = c(min(obs, res), max(obs, res)))
lines(exp, res, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp-res'), pch = 16, col= c(1, 2), bty = 'n')

# Ex3:
exp <- 1:10
dim(exp) <- c(time = 2, lat = 5)
obs <- 101:120
dim(obs) <- c(time = 20)
res <- qmapcor(exp, obs)
dim(res) #same dimesnions as exp 
res
plot(exp, obs[1:10], pch = 16, main = "Test3 QUANT in qmapcor()",
     ylim = c(min(obs, res), max(obs, res)))
lines(exp, res, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp-res'), pch = 16, col= c(1, 2), bty = 'n')

# Ex4:
exp <- 1:20
dim(exp) <- c(time = 2, lat = 10)
obs <- 101:120
dim(obs) <- c(lon = 2, time = 10)
res <- qmapcor(exp, obs)
dim(res) #same dimesnions as exp 
res
plot(exp, obs, pch = 16, main = "Test4 QUANT in qmapcor()",
     ylim = c(min(obs, res), max(obs, res)))
lines(exp, res, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp-res'), pch = 16, col= c(1, 2), bty = 'n')

# Ex5:
exp_cor <- 5:12
res <- qmapcor(exp, obs, exp_cor = exp_cor)
dim(res) # no dims provided with exp_cor
res
plot(exp, obs, pch = 16, main = "Test5 QUANT in qmapcor()",
     ylim = c(min(obs, res), max(obs, res)))
lines(exp_cor, res, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp_cor-res'), pch = 16, col= c(1, 2), bty = 'n')

# Ex6:
dim(exp_cor) <- c(time = 8)
  if (length(wetday) > 1) {
t(exp$data, obs$data, pch = 16, main = "Test1 QUANT in CST_QuantileMapping()",
     ylim = c(min(obs$data, res$data), max(obs$data, res$data)))

res <- qmapcor(exp, obs, exp_cor = exp_cor)
dim(res)
res
plot(exp, obs, pch = 16, main = "Test6 exp_cor QUANT in qmapcor()",
     ylim = c(min(obs, res), max(obs, res)))
lines(exp_cor, res, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp_cor-res'), pch = 16, col= c(1, 2), bty = 'n')

# Ex7:
dim(exp_cor) <- c(time = 8)
exp_cor[2] <- NA
res <- qmapcor(exp, obs, exp_cor = exp_cor) # NA not accepted
 




# Testing function QuantileMapping
# Ex1:
exp_cor <- NULL
exp <- 1:60
dim(exp) <- c(lat = 2, lon = 3, time = 10)
obs <- 101:160
dim(obs) <- c(lat = 2, lon = 3, time = 10)
res <- QuantileMapping(exp, obs, sample_dims = 'time')
dim(res)
head(res)
summary(res)
res <- QuantileMapping(exp, obs, sample_dims = 'time', method = 'RQUAN')
res <- QuantileMapping(exp, obs, sample_dims = 'time', method = 'RQUAN', qstep = 0.01)
res <- QuantileMapping(exp, obs, sample_dims = 'time', method = 'RQUAN', type = 'linear2')
res <- QuantileMapping(exp, obs, sample_dims = 'time', method = 'tri') #expected error
res <- QuantileMapping(exp, obs, sample_dims = 'time', method = 'PTF')

exp <- 1 : 120
dim(exp) <- c(member = 2, lat = 2, lon = 3, time = 10)
res <- QuantileMapping(exp, obs, sample_dims = 'time', sample_length = 5)
res <- QuantileMapping(exp, obs, sample_dims = 'time', sample_length = 7)
res <- QuantileMapping(exp, obs, sample_dims = c('time', 'member'), sample_length = NULL)
res <- QuantileMapping(exp, obs, sample_dims = c('time', 'member'), sample_length = 7)




# Ex2:
res <- QuantileMapping(exp, obs, sampledims = c('lon', 'time'))
dim(res)
head(res)
summary(res) #no me gusta
# Ex3:
exp <- 1:60
dim(exp) <- c(lat = 2, lon = 3, member = 2, time = 5)
obs <- 101:130
dim(obs) <- c(lat = 2, lon = 3, time = 5)
res <- QuantileMapping(exp, obs) # expected error
dim(res)
head(res)
summary(res)
# Ex4:
res <- QuantileMapping(exp, obs, sample_dims = c('time', 'member'))
dim(res)
head(res)
summary(res)
# Ex5:
obs <- 101:160
dim(obs) <- c(lat = 2, lon = 3, time = 5, member = 2)
res <- QuantileMapping(exp, obs, sample_dims = c('time', 'member'))
dim(res)
head(res)
summary(res)
# Ex6:
res <- QuantileMapping(exp, obs, method = 'PTF', sample_dims = c('time', 'member'))
dim(res)
head(res)
summary(res)




exp$data <- runif(n = 1 * 10 * 20 * 60 * 6 * 7, min = 0, max = 5)
dim(exp$data) <- c(dataset = 1, member = 10, sdate = 20, ftime = 60 , 
                   lat = 6, lon = 7)
class(exp) <- 's2dv_cube'
obs$data <- runif(n = 1 * 1 * 20 * 60 * 6 * 7, min = 0, max = 5)
dim(obs$data) <- c(dataset = 1, member = 1, sdate = 20, ftime = 60 ,
                   lat = 6, lon = 7)
class(obs) <- 's2dv_cube'
res <- CST_QuantileMapping(exp, obs)
plot(exp$data[1,1,,,,], obs$data, pch = 16, main = "Test1 QUANT in qmapcor()",
ylim = c(min(obs$data, res$data), max(obs$data, res$data)))
lines(exp$data, res$data, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp-res'), pch = 16, col= c(1, 2), bty = 'n')

exp <- lonlat_data$exp
obs <- lonlat_data$obs
res <- CST_QuantileMapping(exp, obs)

plot(exp$data[1,1,,,,], obs$data, pch = 16, main = "Test1 QUANT in qmapcor()",
     ylim = c(min(obs$data, res$data), max(obs$data, res$data)))
lines(exp$data, res$data, col = "red", type = "p", pch = 16)
legend("bottomright", c('exp-obs', 'exp-res'), pch = 16, col= c(1, 2), bty = 'n')
time = 10)
)ev.of(f)


exp <- lonlat_data$exp$data
obs <- lonlat_data$obs$data
res <- Apply(list(exp, obs, exp), target_dims = sample_dims, fun = qmapcor, ...,
                  #exp_cor = exp_cor, sample_length = sample_length,
                  method = method, ncores = ncores)$output1


library(qmap)
data(obsprecip)
data(modprecip)
exp <- modprecip$MOSS[1:10000]
dim(exp) <- c(time = length(exp))
exp <- list(data = exp)
class(exp) <- 's2dv_cube'
obs <- obsprecip$MOSS[1:10000]
dim(obs) <- c(time = length(obs))
obs <- list(data = obs)
class(obs) <- 's2dv_cube'
#wet.day.log <- FALSE
wet.day.log <- TRUE
for (i in 100:length(obs$data)){
print( c("Sample_length =" , i))
res <- try(CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time', method = 'QUANT', wet.day = T, sample_length = i))
next
}

dim(exp$data)
res <- CST_QuantileMapping(exp = exp, obs = obs, sample_dims = 'time',
                           method = 'QUANT', wet.day = TRUE,
                           sample_length = 10)


