#!/bin/bash
#     keyword :: canesm_load_hist
# description :: Load model history files.
set -a
. betapath2  # sets path for new package (required until old package is retired)

#  * ........................... Parmsub Parameters ............................

 runid="runid"; year="yyyy"; mon="mm"; run_start_year="yyy";run_start_month="mm";
 uxxx="mc";
 canesm_load_hist_prefix="mc";
 canesm_load_hist_suffix_list="";
 canesm_dump_hist_short_term="on";

 if [ "$year" = "yyyy" -a "$mon" = "mm" ] ; then
   # if not defined, it is assume a one time job at the beginning of the run.
   # assign previous month
   mon=$(printf "%02d" `expr $run_start_month - 1`)
   if [ $mon -lt 1 ] ; then
     mon=12
     year=`expr $run_start_year - 1`
   else
     year=$run_start_year
   fi
 fi

 # These variables are set when the job string is created
 previous_year=NotSet
 previous_month=NotSet

 current_year=NotSet
 current_month=NotSet

 next_year=NotSet
 next_month=NotSet

 run_start_year=NotSet
 run_start_month=NotSet
 run_stop_year=NotSet
 run_stop_month=NotSet

 crawork="crawork";
 username="username"; user="USER"; nqsprfx=""; nqsext="_${runid}_${year}_m${mon}";
 lopgm="lopgm"; stime="10800"; memory1="4gb";

#  * ............................ Condef Parameters ............................

 noprint=on
 nextjob=on
 debug=off
 shortermdir=$canesm_dump_hist_short_term
 hpcarc_user=unknown

#  * ............................. Deck Definition .............................
jobname=load_${uxxx}; time=$stime ; memory=$memory1
.   comjcl.cdk
cat > Execute_Script <<'end_of_script'
# Loads model history files from HPNLS using hpcarchive
# RSK/RYJ, 03 2019

# These are used by "make_file_name_list" to make the list of files.
[ -z "${canesm_load_hist_suffix_list}" ] && { echo "canesm_load_hist_suffix_list must not be empty when load=on" ; exit 1 ; }
export suffix_list="${canesm_load_hist_suffix_list}"
export prefix_list=$uxxx
export runid

# create list of files to load
touch tmp_load_file_list
make_file_name_list --start=$year:$mon --stop=$year:$mon tmp_load_file_list || { echo "WARNING: could not make tmp_load_file_list" ; exit 0 ; }

# The list has been created. Now source it and parse the files
. tmp_load_file_list

# load using hpcarchive
if [ "$shortermdir" = "on" ] ; then
  hpcarchive_project=crd_short_term
else
  hpcarchive_project=crd_cccma
fi

# find the latest archive
if [ "$hpcarc_user" = "unknown" -o "$hpcarc_user" = "" ] ; then
  hpcarc_user_arg="-U"
else
  hpcarc_user_arg="-u $hpcarc_user"
fi
hpcarchive_pfx=${canesm_load_hist_prefix}_${runid}_${year}_m${mon}_
hpcarchive=`hpcarchive -p $hpcarchive_project $hpcarc_user_arg -L -x -s -c "^$hpcarchive_pfx" | grep $hpcarchive_pfx | head -1`
if [ "$hpcarchive" = "" ] ; then
  echo "ERROR: no HPC archive is found with this prefix: $hpcarchive_pfx!"
  exit 1
fi
hpcarcuser=`echo $hpcarchive | cut -f1 -d' ' | cut -c2-`
hpcarcname=`echo "$hpcarchive" | sed "s/.* \(${hpcarchive_pfx}[0-9]\+\)/\1/"`

# load full archive - try several times
ntrymax=10
ntry=1
while [ $ntry -le $ntrymax ] ; do
  status=""
  if [ $join -eq 1 ] ; then
    # load a single file from the archive (SK: is it possible to specify more than 1 file?)
    hpcarchive -p ${hpcarchive_project} -u ${hpcarcuser} -r -f ${hpcarcname}/${file1} || status=$?
  else
    # load the whole archive
    hpcarchive -p ${hpcarchive_project} -u ${hpcarcuser} -r -c ${hpcarcname} || status=$?
  fi
  echo status=$status
  if [ "$status" = "" ] ; then
    break
  fi
  # unsuccessful retrieval - sleep a bit and try again.
  rm -rf ${hpcarcname}
  sleep 60
  ntry=`expr $ntry + 1`
done
if [ "$status" != "" ] ; then
  exit 1
fi

# save files
cd ${hpcarcname}
for i in `seq $join`; do
  nam=file$i ;
  save ${!nam} ${!nam}
done
end_of_script

. endjcl.cdk
#end_of_job
