rm(list=ls())

library(ncdf4)
library(s2dverification)
library(abind)

aa<-nc_open('/esarchive/scratch/pjha/hazelnut_outputs/hazelOutputAnal/soil_Ksat.325.200.inv_lat.nc')

lat<-ncvar_get(aa, "lat")
lon<-ncvar_get(aa, "lon")

path_out<-'/esarchive/scratch/pjha/hazelnut_outputs/hazelOutputAnal/plots/'

models<-c('ECHAM5_R1','ECHAM5_R2','ECHAM5_R3','CSIRO-MK3.0_R1','CSIRO-MK3.0_R2','CSIRO-MK3.0_R3',
             'MIROC3.2_R1','MIROC3.2_R2','MIROC3.2_R3','CCCMA3.1_R1','CCCMA3.1_R2','CCCMA3.1_R3')
        
path1<-paste0('/esarchive/scratch/pjha/hazelnut_outputs/hazelOutputAnal/RDS_files/',models,'/','final','/')   
          
## Other variables than phases
name1<-c('yld','fut_yld')

var1<-array(numeric(), c(325,200,19,2,length(models)))
               
for (n in 1:length(models)){ 
  for (i in 1:2) {
    model<-models[n]
    path_in<-path1[n]

        var1[ , , ,i,n]<-readRDS(paste0(path_in,name1[i],'_',model,'.RDS'))

            }

        }



## Mean and std of other variables
var1_avg<-array(numeric(), c(325,200,2,length(models)))
var1_sd<-array(numeric(), c(325,200,2,length(models)))

## 
for (i in 1:2) { ## pres, fut yield
  for (j in 1:length(models)) {
   var1_avg[ , ,i,j]<-apply(drop(var1[ , , ,i,j]),c(1,2),mean)
   var1_sd[ , ,i,j]<-apply(drop(var1[ , , ,i,j]),c(1,2),sd)
   
      }
   }

## rearange dim to make [model,lon,lat]
var1_avg_fin<-aperm(var1_avg, c(4,1,2,3))
rm(var1_avg)
   
var1_sd_fin<-aperm(var1_sd, c(4,1,2,3))  
rm(var1_sd)

###############################################################################################################
######################################### YIELD #######################################
# Average yield (pres climate)
yld_prs_av<-var1_avg_fin[ , , ,1]

#X11()
mapa <- yld_prs_av[1, 98 : 189, 16 : 200]
names(dim(mapa)) <- c("lon", "lat")
lon <- lon[98 : 189]
lat <- lat[16 : 200]
PlotEquiMap(mapa, lon = lon, lat = lat, intylat = 4, intxlon = 4, filled.continents = FALSE, toptitle = "Map yld_prs_av[1, 98 : 189, 16 : 200]", fileout = "./Map1_testPrakash.png")




file <- "./study_area_final.nc" 
#file <- "./git/Flor/R/study_area_final.nc"
ncfile <- nc_open(file)
description <- ncatt_get(ncfile, "aus_ag_cli_raster.tif")
datos <- ncvar_get(ncfile, "aus_ag_cli_raster.tif")
dim(datos)
names(dim(datos)) <- c("lon", "lat")
lats <- ncvar_get(ncfile, "lat")
lons <- ncvar_get(ncfile, "lon")
nc_close(ncfile)
PlotEquiMap(datos, lon = lons, lat = lats, intylat = 4, intxlon = 4, filled.continents = FALSE, toptitle = "study_area_final.nc", fileout = "./Map2_testPrakash.png")

dim(mapa)
dim(datos)

# Check parameter drawleg = FALSE
# ----------------------------
length(lon)
length(lons)
(lats == min(lat))
length(lats[16 : 200])
length(lat)
X11()
par(mar= c(4,4,4,4))
PlotEquiMap(mapa, lon = lon, lat = lat, intylat = 4, intxlon = 4, filled.continents = FALSE, axelab = TRUE, drawleg = FALSE)
par(xpd = F)
contour(x = lons[97 : 187],y = lats[16 : 200], z = datos[97 : 187, 16 : 200], add = TRUE, col = "black", lwd = 5)
points(148, -32, pch = 15, col = "black", lwd = 5, cex = 10)
