rm(list=ls(all=TRUE)) 
cat("\014")

################
####### STATISTICAL TEST MAPS
################

library(dplyr)

setwd("/esarchive/scratch/pdeluca/landmarc/stat_test")

########## read and adjust data
var_hist=readRDS("var_hist_3_1.rds")
var_ssp585=readRDS("var_ssp585_3_1.rds")

lon=read.csv("longitude.csv")
lat=read.csv("latitude.csv")

##### remove extra cols
for (i in 1:length(var_hist)) {
  
  var_hist[[i]]=var_hist[[i]][,-c(3,5)]
  var_hist[[i]]$period='hist'
  
}

for (i in 1:length(var_ssp585)) {
    
  var_ssp585[[i]]=var_ssp585[[i]][,-c(3,5)]
  var_ssp585[[i]]$period='ssp585'
  
}

var_hist=bind_rows(var_hist)
var_ssp585=bind_rows(var_ssp585)

var=bind_rows(var_hist, var_ssp585)

var=merge(var, lon, by = "Var1")
var=merge(var, lat, by = "Var2")
var=var[,-c(1:2)]

var=split(var, list(var$lon, var$lat))

var=var[lapply(var,nrow)>0]

i <- 1
while (i <= length(var)) {
  if (length(unique(var[[i]]$period))==1) {
    var<-var[-i]
  }
  else{
    i <- i+1
  }
}

  
for (i in 1:length(var)) {

   var[[i]]=do.call(zoo::cbind.zoo, split(var[[i]], var[[i]]$period))
   var[[i]]=as.data.frame(var[[i]])
   
   var[[i]]$value.hist=as.numeric(var[[i]]$value.hist)
   var[[i]]$value.ssp585=as.numeric(var[[i]]$value.ssp585)
   
}

##########


######################## check if lat-lon-index are the same
#################
check_ssp585=list()
check_ssp585_2=list()
check_ssp585_3=list()

for (i in 1:length(var)) {
    
    check_ssp585[[i]]=var[[i]]$lon.hist[1] == var[[i]]$lon.ssp585[1]
    check_ssp585_2[[i]]=var[[i]]$lat.hist[1] == var[[i]]$lat.ssp585[1]
    check_ssp585_3[[i]]=var[[i]]$index.hist[1] == var[[i]]$index.ssp585[1]
    
}

check_ssp585=do.call(rbind,check_ssp585)
check_ssp585_2=do.call(rbind,check_ssp585_2)
check_ssp585_3=do.call(rbind,check_ssp585_3)
  
print(unique(check_ssp585))
print(unique(check_ssp585_2))
print(unique(check_ssp585_3))
##########################
##############################################

test_ssp585=list()
p_val_ssp585=list()

for (i in 1:length(var)) {
    
    test_ssp585[[i]]=wilcox.test(var[[i]]$value.hist, 
                                        var[[i]]$value.ssp585, alternative = "two.sided")

    p_val_ssp585[[i]]=data.frame(p=test_ssp585[[i]]$p.value[1])
    
    p_val_ssp585[[i]]$lon=var[[i]]$lon.hist[1]
    p_val_ssp585[[i]]$lat=var[[i]]$lat.hist[1]
    p_val_ssp585[[i]]$index=var[[i]]$index.hist[1]
    
}

p_val_ssp585=do.call(rbind,p_val_ssp585)

## Bonferroni
p_val_ssp585$p_adj=p.adjust(p_val_ssp585$p, method = "bonferroni", n = length(p_val_ssp585$p))

p_val_ssp585=subset(p_val_ssp585, p_adj < 0.05)

setwd("/esarchive/scratch/pdeluca/landmarc/stat_test/out")
write.csv(p_val_ssp585, paste("p_values_maps_", var[[1]]$index.hist[1], "_ssp585.csv", sep = ""), row.names = FALSE)


