# We test the statistical difference between Reichler-Kim indexes computed for different experiments.


var_name=c('t2m','msl','qnet','tp','ewss','nsss','SST','SSS','SICE','T','U','V','Q')
nb_var=length(var_name)
#ylimites=c(min(index_e00x,index_e00y,index_m04y),max(index_e00x,index_e00y,index_m04y))
ylimites=c(0.8,1.2)
postscript('reichler_kim_scores_stat_m069_m06e.ps', width = 550, height = 300)
plot(NULL,main='Reichler-Kim normalized index for 13 variables of 5-members simulations performed on 3 different platforms\nSimulations statistically different from the others according to a KS test appear in red',xaxt = "n",xlab='var',ylab='Normalized RK index',xlim=c(1,nb_var),ylim=ylimites)
axis(1,at=1:nb_var,labels=var_name)
abline(v=seq(0.5,12.5,1),lty=3)
abline(h=1)
for (ji in 1:nb_var) {
# For tests:
#ji=9
var=var_name[ji]
print(var)

#exp='e00x'
#file_exp=paste(exp,'_',var,'.txt',sep='')
#index_e00x <- unlist(read.table(file_exp),use.names=FALSE)
#mean_index_e00x <- mean(index_e00x)

#exp='e00y'
#file_exp=paste(exp,'_',var,'.txt',sep='')
#index_e00y <- unlist(read.table(file_exp),use.names=FALSE)
#mean_index_e00y <- mean(index_e00y)

#exp='m04y'
#file_exp=paste(exp,'_',var,'.txt',sep='')
#index_m04y <- unlist(read.table(file_exp),use.names=FALSE)
#mean_index_m04y <- mean(index_m04y)

#exp='i06c'
#file_exp=paste(exp,'_',var,'.txt',sep='')
#index_i06c <- unlist(read.table(file_exp),use.names=FALSE)
#mean_index_i06c <- mean(index_i06c)

exp='m06e'
file_exp=paste(exp,'_',var,'.txt',sep='')
index_m06e <- unlist(read.table(file_exp),use.names=FALSE)
mean_index_m06e <- mean(index_m06e)

exp='m069'
file_exp=paste(exp,'_',var,'.txt',sep='')
index_m069 <- unlist(read.table(file_exp),use.names=FALSE)
mean_index_m069 <- mean(index_m069)

# Mean all exp on all machines:
mean_all_index <- mean(c(mean_index_m06e,mean_index_m069))

# Test:
#test_e00x_e00y <- ks.test(index_e00x,index_e00y)
#test_i06c_e00y <- ks.test(index_i06c,index_e00y)
#test_e00x_m04y <- ks.test(index_e00x,index_m04y)
#test_i06c_m04y <- ks.test(index_i06c,index_m04y)
#test_e00y_m04y <- ks.test(index_m04y,index_e00y)
test_m06e_m069 <- ks.test(index_m06e,index_m069)

print('plot')
# Plot:

# Change colors according to the differences of distributions:

if (test_m06e_m069$p.value < 0.05) {color_m06e='blue'; color_m069='red'} else
     {color_m06e='blue'; color_m069='blue'}

#if (test_i06c_e00y$p.value < 0.05) {
#   if (test_e00y_m04y$p.value < 0.05) {
#     if (test_i06c_m04y$p.value < 0.05)
#        {color_i06c='red'; color_e00y='red' ; color_m04y='red'} else
#        {color_i06c='blue'; color_e00y='red' ; color_m04y='blue'}
#    } else {color_i06c='red'; color_e00y='blue' ; color_m04y='blue'}
# } else {
#  if (test_e00y_m04y$p.value < 0.05) {color_i06c='blue'; color_e00y='blue' ; color_m04y='red'} else
#     {color_i06c='blue'; color_e00y='blue' ; color_m04y='blue'}
#}

# Older computation of the color:
#if (test_i06c_e00y$p.value < 0.05 && test_i06c_m04y$p.value < 0.05) {color_i06c='red'} else {color_i06c='blue'}
#if (test_i06c_e00y$p.value < 0.05) {color_e00y='red'} else {color_e00y='blue'}
#if (test_e00y_m04y$p.value < 0.05) {color_m04y='red'} else {color_m04y='blue'}
#if (color_m04y=='red' && color_e00y=='red' && color_i06c=='blue') {color_i06c='red'; color_e00y='blue' ; color_m04y='blue'}

# Currently, we have only one experiment on ithaca:
#color_m06e='blue'

#m06e:
x=array(ji-0.2,dim=length(index_m06e))
# Plot indexed for members:
par(new=TRUE)
#plot(x,index_m06e,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m06e)
#plot(x,index_m06e-mean_index_m06e,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m06e)
plot(x,index_m06e/mean_all_index,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m06e)
# Plot mean:
par(new=TRUE)
#plot(ji,mean_index_m06e,cex=2,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m06e)

#m069:
x=array(ji+0.2,dim=length(index_m069))
# Plot indexed for members:
par(new=TRUE)
#plot(x,index_m069,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m069)
#plot(x,index_m069-mean_index_m069,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m069)
plot(x,index_m069/mean_all_index,cex=1,pch=5,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m069)
# Plot mean:
par(new=TRUE)
#plot(ji,mean_index_m069,cex=2,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m069)


##e00x:
#x=array(ji,dim=length(index_e00x))
## Plot indexed for members:
#par(new=TRUE)
##plot(x,index_e00x,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00x)
##plot(x,index_e00x-mean_index_e00x,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00x)
#plot(x,index_e00x/mean_all_index,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00x)
## Plot mean:
#par(new=TRUE)
##plot(ji,mean_index_e00x,cex=2,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00x)

##i06c:
#x=array(ji-0.2,dim=length(index_i06c))
## Plot indexed for members:
#par(new=TRUE)
##plot(x,index_i06c,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_i06c)
##plot(x,index_i06c-mean_index_i06c,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_i06c)
#plot(x,index_i06c/mean_all_index,cex=1,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_i06c)
## Plot mean:
#par(new=TRUE)
##plot(ji,mean_index_i06c,cex=2,pch=1,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_i06c)
#
## e00y:
#par(new=TRUE)
#x=array(ji,dim=length(index_e00y))
## Plot indexed for members:
##plot(x,index_e00y,cex=1,pch=5,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00y)
##plot(x,index_e00y-mean_index_e00y,cex=1,pch=5,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00y)
#plot(x,index_e00y/mean_all_index,cex=1,pch=5,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00y)
## Plot mean:
#par(new=TRUE)
##plot(ji+0.2,mean_index_e00y,cex=2,pch=5,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_e00y)
#
## m04y:
#par(new=TRUE)
#x=array(ji+0.2,dim=length(index_m04y))
## Plot indexed for members:
##plot(x,index_m04y,cex=1,pch=2,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m04y)
##plot(x,index_m04y-mean_index_m04y,cex=1,pch=2,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m04y)
#plot(x,index_m04y/mean_all_index,cex=1,pch=2,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m04y)
## Plot mean:
#par(new=TRUE)
##plot(ji-0.2,mean_index_m04y,cex=2,pch=2,xlab="",ylab="",axes=FALSE,xlim=c(1,nb_var),ylim=ylimites,col=color_m04y)

}

#legend(x=1.5,y=1.21,legend=c('e00x','e00y','m04y'),pch=c(1,5,2))
#legend(x=1.5,y=1.21,legend=c('i06c','e00y','m04y'),pch=c(1,5,2))
legend(x=1.5,y=1.21,legend=c('m06e','m069'),pch=c(1,5))

dev.off()

