#!/bin/bash

#----- Main path, usually set by $(pwd) so you don't need to change it. -------------------#
here=$(pwd)
#----- Description of this simulation, used to create unique job names. -------------------#
desc=$(basename ${here})
#----- File containing the list of jobs and their settings: -------------------------------#
joborder="${here}/joborder.txt"         # ! File with the job instructions
#----- Sub-directory to copy. -------------------------------------------------------------#
subdir="monthly"
#----- Sub-sub-directory to copy (for all, set all). --------------------------------------#
subsub="compemean compmmean tspft tsdbh theme_mmean"
#----- Prefix of files to bring that are in the main directory. ---------------------------#
subpref="disturb"
#------------------------------------------------------------------------------------------#


#==========================================================================================#
#==========================================================================================#




#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#     Unless you are going to modify the way jobs are submitted, you don't need to change  #
# anything beyond this point.                                                              #
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#





#----- Determine the number of polygons to run. -------------------------------------------#
let npolys=$(wc -l ${joborder} | awk '{print $1 }')-3
if [ ${npolys} -lt 100 ]
then
   ndig=2
elif [ ${npolys} -lt 1000 ]
then
   ndig=3
elif [ ${npolys} -lt 10000 ]
then
   ndig=4
else
   ndig=5
fi
format="%${ndig}.${ndig}i"
#------------------------------------------------------------------------------------------#



#----- Create path with output files. -----------------------------------------------------#
outpath="${here}/${desc}_figures"
mkdir -pv ${outpath}
#------------------------------------------------------------------------------------------#


#------------------------------------------------------------------------------------------#
#      Loop over all polygons.                                                             #
#------------------------------------------------------------------------------------------#
n_submit=0
while [[ ${ff} -lt ${npolys} ]]
do
   let ff=${ff}+1
   let line=${ff}+3
   ffout=$(printf "${format}" ${ff})





   #---------------------------------------------------------------------------------------#
   #      Read the ffth line of the polygon list.  There must be smarter ways of doing     #
   # this, but this works.  Here we obtain the polygon name, and its longitude and         #
   # latitude.                                                                             #
   #---------------------------------------------------------------------------------------#
   oi=$(head -${line} ${joborder} | tail -1)
   polyname=$(echo ${oi}     | awk '{print $1  }')
   echo " + Copy figures from simulation: ${polyname}."
   #---------------------------------------------------------------------------------------#


   #------ Create output path for this simulation. ----------------------------------------#
   inpoly="${here}/${polyname}/${subdir}"
   outpoly="${outpath}/${polyname}"
   mkdir -p ${outpoly}
   #---------------------------------------------------------------------------------------#


   #------ Copy files. --------------------------------------------------------------------#
   for subnow in ${subsub}
   do
      #----- Check whether to copy everything or a specific directory. --------------------#
      case ${subnow} in
      all)
         rsync -Purtv ${inpoly}/*         ${outpoly}
         ;;
      tsdbh)
         mkdir -p ${outpoly}/tsdbh
         rsync -Purtv ${inpoly}/tsdbh/*/*-pft-00-* ${outpoly}/tsdbh
         ;;
      *)
         rsync -Purtv ${inpoly}/${subnow} ${outpoly}
         ;;
      esac
      #------------------------------------------------------------------------------------#
   done
   #---------------------------------------------------------------------------------------#


   #------ Copy files in the main sub-directory, in case any is to be copied. -------------#
   if [[ "${subpref}" != "" ]]
   then
      #------------------------------------------------------------------------------------#
      #      Loop through prefixes.                                                        #
      #------------------------------------------------------------------------------------#
      for prefnow in ${subpref}
      do
         #----- Check whether to copy everything or a specific directory. -----------------#
         rsync -Putv ${inpoly}/${prefnow}*.??? ${outpoly}
         #---------------------------------------------------------------------------------#
      done
      #------------------------------------------------------------------------------------#
   fi
   #---------------------------------------------------------------------------------------#

done
#------------------------------------------------------------------------------------------#
