source("/esarchive/scratch/nperez/git/cstools/R/CST_QuantileMapping.R")
library(multiApply)
library(qmap)
AAAA
pdf("../CST_QuantileMapping_plots.pdf")
# Testing basic function qmapcor:
# Ex1:
exp <- 1:20
dim(exp) <- c(time = 20)
obs <- 1:20
dim(obs) <- c(time = 20)
res <- qmapcor(exp, obs, sample_length = 5)
dim(res) #no dimensions
length(res)
res
plot(exp, obs[1:10], 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)
dim(res)
head(res)
summary(res)
# 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)
dim(res)
head(res)
summary(res)
# Ex4:
res <- QuantileMapping(exp, obs, sampledims = 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, sampledims = c('time', 'member'))
dim(res)
head(res)
summary(res)
# Ex6:
res <- QuantileMapping(exp, obs, method = 'PTF', sampledims = 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')

dev.off()




