PlotRD<-function(rel_diag,nbins=10,consbars=F,tit=NULL,colLine=NULL,colBar=NULL,marHist=T,hist_ylim=NULL,Lg=NULL) {
  
print("Plot")
#   rel_diag<-rd
#   nbins=10
#   consbars=T
#   colLine=col_line
#   colBar=col_bar
#   tit=tit1
#   marHist=T
#   hist_ylim=c(0,100)
#   x11(width=12,height=10)
  #   x11()
  #  PLOT OF THE RELIABILITY DIAGRAM 
  #
  ######################################################################################
  # rd: a list with the reliability diagrams that will be represented in the same plot
  # cons.bars : if the consistency bar must be represented or not.
  # nbins : number of equidistant points used to compute the reliability diagram (optional)
  # tit: the title of the plot (optional)
  # brierScores: The brier score linked to the reliability diagram (optional)
  # marHist: Whether to plot the small refinement histogram is showed
  #####################################################################################
  
  # Some parameters are defined
  nrd<-length(rel_diag)   # nrd = 5, 4 models + mme
  rg<-list()
  # Check the dimensions of the rank histogram
  for (i in 1:nrd){
    if (dim(rel_diag[[i]])[1]!=nbins){
      stop ('The bins of the reliability diagram must be the same that nbins')
    }
    rg[[i]]<-range(rel_diag[[i]]$hist.counts)# check the range of the histograms
  }
  if (is.null(hist_ylim)){
    rgH<-range(rg)
#print(rgH)
  }else{
    rgH<-hist_ylim
  }
  
  
  ##########################################
  # reliability plot
  # par(mar=c(5,3,2,2)+0.1)
  ##########################################
  
  layout(matrix(c(rep(1,nrd),seq(2,(nrd+1))),nrd,2,byrow=F),width=c(5,2))
  par(oma=c(2.5,4,5,1))
  #layout.show(a)
  
  # The axis are defined
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #x11(width=12,height=10)
#  old.par <- par(no.readonly = TRUE)
#print(old.par)
#  on.exit(par(old.par))
#  par(mar=c(5,5,5,0))
  old.par <- par(mar=c(5,5,5,0))
  on.exit(par(old.par))

  plot(NULL, xlim = c(0,1), ylim = c(0,1),axes=F, xlab='', ylab='')

  axis(1, at=seq(0,1,by=0.1),labels=seq(0,1,by=0.1),cex.axis=2.0)
  title(xlab= "Forecast probability",line=3.9,cex.lab=2.0)

  axis(2, at=seq(0,1,by=0.1), labels=seq(0,1,by=0.1), las=2,cex.axis=2.0)
  #axis(2, at=seq(0,1,by=0.1), labels=seq(0,1,by=0.1), cex.axis=2.0)
  box()
  title(ylab= "Observed relative frequency", line=0.2,cex.lab=2.0,outer=T)
  if(is.null(tit)==F){
#    title(tit,cex.main=4,outer=T,line=-1)
#    title(tit,cex.main=2.0,outer=T,line=-4)
    title(tit,cex.main=2.0,outer=T,line=-3)
  }
  
  # Legend
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  yloc <- c(1.0, 0.95, 0.90, 0.85, 0.80)
  legend(0.,yloc[1], legend=Lg[[1]], fill=colLine[[1]], bty="n", cex=1.2)
  legend(0.,yloc[2], legend=Lg[[2]], fill=colLine[[2]], bty="n", cex=1.2)
  legend(0.,yloc[3], legend=Lg[[3]], fill=colLine[[3]], bty="n", cex=1.2)
  legend(0.,yloc[4], legend=Lg[[4]], fill=colLine[[4]], bty="n", cex=1.2)
  legend(0.,yloc[5], legend=Lg[[5]], fill=colLine[[5]], bty="n", cex=1.2)
#  legend("topleft", "(x,y)", pch = 1, title = "topleft, inset = .05", inset = .05)

  # No resolution and No skill lines
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  no_res <- sum(rel_diag[[1]]$obs.counts)/sum(rel_diag[[1]]$hist.counts)
#  vt_res <- sum(rel_diag[[1]]$for.prob)/sum(rel_diag[[1]]$hist.counts)
#print(paste("no_res = ",no_res))
  numb <- c(seq(0,1,by=0.1))
#print(numb)
  no_skill <- (numb+no_res)/2.
#print(no_skill)

# diagonal line
  lines(c(0,1), c(0,1), lty=1)
# no_resolution line
  lines(c(0,1), c(no_res,no_res), col="gray", lty=3)
  lines(c(1/3,1/3), c(0,1), col="gray", lty=3)
#  lines(c(vt_res,vt_res), c(0,1), col="gray", lty=3)
#  lines(c(no_res,no_res), c(0,1), col="gray", lty=3)
# no_skill line
  lines(c(0,1), c(no_skill[1],no_skill[11]), col="black", lty=3)


  # Consistency bars
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  HI<-matrix(NA,nrow=nrd,ncol=length(rel_diag[[1]]$hist.counts))
  
  for (j in 1:nrd){                    # nrd = 5: 4 models + mme
    HI[j,]<-rel_diag[[j]]$hist.counts
    if (consbars==T){
      # The lower limit of consistency bar i and the upper limit are combined in one list
      consBars<-list()
      consBars[[j]]<-abind(InsertDim(rel_diag[[j]]$cbar.lo,1,1),InsertDim(rel_diag[[j]]$cbar.hi,1,1),along=1)

      # plot consistency bars
      for (i in 1:nbins){              # nbins = 10 bins
#        lines(rep(rel_diag[[j]]$p.avgs[i], 2), consBars[[j]][, i], col=colBar[j], lwd=3)
        lines(rep(rel_diag[[j]]$p.avgs[i], 2), consBars[[j]][, i], col=colBar[j], lwd=2)  # lwd: line width
      }
    }
    
# see plot: "p" for points, "l" for lines, "b" for both points and lines, "c" for empty points joined by lines, "o" for overplotted points and lines, "s" and "S" for stair steps and "h" for histogram-like vertical lines. Finally, "n" does not produce any points or lines.
    points(rel_diag[[j]]$p.avgs, rel_diag[[j]]$cond.probs,  type="b", pch=1 , col =colLine[[j]], cex=2.0 , lwd=3)
    
  }
  
  
  # Number of forecasts
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  if (marHist==TRUE){
    
    for (i in 1:nrd){
#      par(mar=c(5,0,5,12))
#      par(mar=c(1.5,1.5,6,7))  # in case num of sharpness diagram is 3 or 4
      par(mar=c(0.5,1.5,5,7))  # in case num of sharpness diagram is 5
      barplot(HI[i,]/10000, beside=T,space=c(0,1.2),axes = F, axis.lty=F, axisnames = F, col = colLine[[i]], ylim=rgH/10000)
#      axis(1, at=seq(0,1,by=0.1),labels=seq(0,1,by=0.1),cex.axis=1.5)
      title(main = "# of forecasts (x10⁴)", font.main = 1.0, line=0.5)
#      grid(1,5,col='#525252')
      axis(4,cex.axis=1.0)
      box(bg='grey') 
    }
    #pp<- par("plt")
    #par("plt" = c(pp[2] - 0.14 , pp[2], pp[3], pp[3]+ 0.15) )
    #par(new = TRUE)
  }
  
}

