#!/usr/bin/env Rscript

# We test the statistical difference between two experiments for several variables.

library(ncdf)
library(s2dverification)
library(RColorBrewer)

args = commandArgs(trailingOnly=TRUE)

# test if there are the correct number of arguments: if not, return an error
if (length(args)!=6) {
  stop("You should use 6 arguments: the path where is located the data, exp1, exp2, year1, year2", call.=FALSE)
}

path=args[1]
exp1=args[2]
exp2=args[3]
year1=args[4]
year2=args[5]
nmemb=ars[6]

var_name=c('t2m','qnet','SICE')
var_name2=c('tas','str','ci')
#var_name=c('t2m','msl','qnet','tp','ewss','nsss','SST','SSS','SICE','T','U','V','Q')
#var_name2=c('tas','msl','str','toptp','ewss','nsss','sosstsst','sosaline','ci','t','u','v','q')
nb_var=length(var_name)

for (ji in 1:nb_var) {

var=var_name[ji]
var2=var_name2[ji]

print(paste("working for",var))

#memb_name=c('fc0','fc1','fc2','fc3','fc4')
#data=array(NaN,dim=c(length(exp_name),length(memb_name),180,91,240))

exp_name=array(dim=2)
exp_name[1]=exp1
exp_name[2]=exp2

for (jexp in 1:2) {
  for (jmemb in 1:nmemb) {

    fnc <- open.ncdf(paste(path,'/',exp_name[jexp],'/fc',jmemb,'/',var,'_mon_2x2.nc',sep=""))
    lon <- get.var.ncdf(fnc,'longitude')
    lat <- get.var.ncdf(fnc,'latitude')
    time <- get.var.ncdf(fnc,'time')

    if ((jmemb == 1)&& (jexp=1) ) {
      data=array(NaN,dim=c(2,nmemb,length(lon),length(lat),length(time)))
    }
    
    print(paste("reading experiment",exp_name[jexp]," and member ",jmemb,sep=""))
    print(paste("number of longitudes=",length(lon),sep=""))
    print(paste("number of latitudes=",length(lat),sep=""))

    if (dim(lon)==dim(data)[3] && dim(lat)==dim(data)[4]) {print("dimensions OK")} else
       {print(paste("bad dimensions for exp ",exp_name[jexp],' and memb ',memb_name[jmemb],sep=""))} 

    # Get the variable:
    data[jexp,jmemb,,,] <- get.var.ncdf(fnc,var2)
    # Closing netcdf file:
    close.ncdf(fnc)

  }
}

print(paste("computing means for",var))
# Mean and differences:
mean_data <- Mean1Dim(data,posdim=5)
mean_exp1 <- Mean1Dim(data[1,,,,],posdim=4)
mean_exp2 <- Mean1Dim(data[2,,,,],posdim=4)

# as a first step, we only map the differences between exp1 and exp2: 
diff_exp1_exp2 <- Mean1Dim(mean_exp1,posdim=1)-Mean1Dim(mean_exp2,posdim=1)

# KS test:
test_exp1_exp2 <- apply(mean_data,c(3,4),function(x) if (ks.test(x[1,],x[2,])$p.value < 0.05) {1} else {0})
p_value <- apply(mean_data,c(3,4),function(x) ks.test(x[1,],x[2,])$p.value)
# % of grid point where the difference is significant:
percent=round(sum(test_exp1_exp2)/length(test_exp1_exp2)*100)

# We save data before generating maps:
#save(var,diff_exp1_exp2,lon,lat,test_exp1_exp2,percent,file=paste("save_",var,".RData",sep=""))

# Maps
#if (var == 't2m') {brks_def=c(-5,-2.5,-1,-0.5,-0.25,0,0.25,0.5,1,2.5,5)}
if (var == 't2m') {brks_def=c(-1,-0.75,-0.5,-0.25,-0.1,0,0.1,0.25,0.5,0.75,1)}
if (var == 'qnet') {brks_def=c(-50,-25,-10,-5,-1,0,1,5,10,25,50)}
if (var == 'SICE') {brks_def=c(-1,-0.001,0.001,1)}
jBrewColors <- brewer.pal(n=length(brks_def)-1,name='RdBu')
postscript(paste('diff_',exp1,'_',exp2,'_',var,'.eps',sep=""))
PlotEquiMap(diff_exp1_exp2,lon,lat,toptitle=paste(var," difference between 5-members experiments ",exp1," and ",exp2,". Black doted regions indicate where the difference \n is significant according to a Kolmogorov-Smirnov test (",percent,"% of grid points show a significant difference)",sep=""),
            sizetit = 0.5, units = "",
            brks=brks_def, cols=rev(jBrewColors), square = TRUE,
            filled.continents = FALSE, contours = NULL, brks2 = NULL,
            dots = test_exp1_exp2, axelab = TRUE, labW = FALSE, intylat = 20, intxlon = 20,
            drawleg = TRUE, subsampleg = 1, numbfig = 1, colNA = "white")

dev.off()

# Plot the p <- value:

brks_def=c(0,0.0001,0.001,0.01,0.05,0.1,0.5,1)
jBrewColors <- brewer.pal(n=length(brks_def)-1,name='RdBu')
postscript(paste('p_value_diff_',exp1,'_',exp2,'_',var,'.eps',sep=""))
PlotEquiMap(p_value,lon,lat,toptitle=paste(var," p_value when comparing the difference between 5-members experiments ",exp1," and ",exp2," according to a Kolmogorov-Smirnov test",sep=""),
            sizetit = 0.5, units = "",
            brks=brks_def, cols=rev(jBrewColors), square = TRUE,
            filled.continents = FALSE, contours = NULL, brks2 = NULL,
            dots = NULL, axelab = TRUE, labW = FALSE, intylat = 20, intxlon = 20,
            drawleg = TRUE, subsampleg = 1, numbfig = 1, colNA = "white")

dev.off()

}
