library(s2dverification)

# The problem is that Ano, duplicates dimensions in 'clim' object to make it match with
# dimensions of 'var' object. Ano doesn't check names dimension. So, if there are two 
# dimensions of the same length, Ano could repeat the wrong dimension.

# To check the problem, we need to create one object 'correct' with two dimension 
# of the same length and
# make Ano duplicate the dimension automatically. And compare.

# Example 1: consecutive values
a <- 1 : (4 * 4 * 5)
dim(a) <- c(mod = 1, member = 4, sdates = 5, ltime = 4)
clim <- apply(a, c(1, 2, 4), mean)
dim(clim) # climatology with member and ltime dimension equal length

clim_rep <- InsertDim(clim[ , 1, ], 1, 1)
identical(clim_rep[1,], clim[ , 1, ])

# The dummy climatology is fine:
    a[1, 1, , 1]
    mean(a[1, 1, , 1])
    clim[1, 1, 1]
    clim_rep[1, 1]
res <- Ano(a, clim)
res_rep <- Ano(a, clim_rep)
identical(res, res_rep)
res[1, 2, , ] 

res_rep[1,2,,]


# Dummy anomaly should be:
    a[1, 1, , 1] - clim[1, 2, 1] # expected
    res[1, 1, , 1] # correct because doesn't need duplication
    res_rep[1, 1, , 1] # Ano duplicates the wrong dimension

# Ano_ debug:
var <- a
dimvar <- dim(var)
length(dim(clim_rep)) <= 2 # TRUE
clim_rep <- InsertDim(clim_rep, 2, dimvar[2]) # The code duplicates for the second dimension
(length(dimvar) > length(dim(clim_rep))) # The length of the variable is still greater than the clim
(dim(clim_rep)[2] != dimvar[2]) # The member dimension are equal
(dim(clim)[3] != dimvar[3]) # But the sdate in var is different from ltime in clim
clim_rep <- InsertDim(clim_rep, 3, dimvar[3]) # Then, it add a new dim in this possition
# Now the arrays are confortable
ano <- var - clim_rep
# This works because clim has length < 2.


# An-chi version debug:
# Fine!


rm(list = ls())

#------------------------
# Example issue 227

t <- array(rnorm(76),c(1,3,4,3,2,2)) # define array as clim expects: nexp,nmemb,nsdates,nltime,nlon,nlat                                                                                   
names(dim(t)) <- c("mod", "memb", "sdates", "ltime", "lon", "lat")
dim(t)
c3 <- Clim(t,t,memb=TRUE)$clim_exp # Clim for each member                                     
dim(c3)
dim(Clim(t,t,memb=FALSE)$clim_exp) # dimensions if memb=FALSE                              
c1 <- InsertDim(c3[,1,,,],1,1) # clim as if memb=FALSE but identical to member 1              
dim(c1)
names(dim(c1)) <- c("mod",  "ltime", "lon", "lat")
identical(c1[,,,],c3[,1,,,]) # results in TRUE
a3 <- Ano(t,c3)  # ano for each member individually                                           
a1 <- Ano(t,c1)  # ano for first member                                                       
identical(a1[,1,,,,],a3[,1,,,,]) 

# Debug Ano:
var <- t
clim <- c1
dimvar <- dim(var)
(length(dim(clim)) <= 2) # clim is length 4
(length(dimvar) > length(dim(clim))) # Yes dim var is (6) greater than clim
(dim(clim)[2] != dimvar[2]) # but ltime is equal 3 to dimension memb is 3 by chance
(dim(clim)[3] != dimvar[3]) # The next one, 2 vs sdates 4
clim <- InsertDim(clim, 3, dimvar[3]) # mod 1, ltim 3, sdates = 4, lon, lat
 (dim(clim)[4] != dimvar[4]) # This is also true
clim <- InsertDim(clim, 4, dimvar[4]) # mod 1, ltim3 3, sdates = 4, ltime 3, lon, lat
# It duplicates ltime instead of member dim

# Anchi version
source("./R/Ano.R")
res1 <- Ano(t, c1)
identical(res1[,1,,,,], a3[,1,,,,]) # First member is the same
identical(res1[,2,,,,], a3[,2,,,,])

# 


rm(list = ls())
