#
# script to read the output file of each chunk and merge them in one array only
#

output <- commandArgs(TRUE)[1]  # suffix used for the name of the output files with the results of the calculation of each chunk
chunks <- as.integer(commandArgs(TRUE)[2])  # total number of chunks used

workdir <- "/scratch/Earth/ncortesi"        # path of the output files
#save(output,file=paste0(workdir,"/output_file_",output,".RData"))

for(c in 1:chunks){
  load(paste0(workdir,"/",output,"_",c,".RData"))  # load the result of the analysis of a single chunk
  
  if(c==1) RMS <- array(NA, c(chunks,length(my.RMS)))
  RMS[c,] <- my.RMS  # create a matrix the the results of all chunks

  file.remove(paste0(workdir,"/",output,"_",c,".RData")) 
}

save(RMS,file=paste0(workdir,"/RMS.RData"))
