#! /bin/bash
. setupdk
. id.cdk
( set -x ; hostname ; pwd ; ls -al || : )

# ---------------------------------- Perform code checks

event_year=`echo $year | awk '{printf "%04d",$1}'`
event_mon=`echo $mon | awk '{printf "%02d",$1}'`
[ $production -eq 1 ] && flgs="-tx" || flgs="-tdx"   # use 'development' mode if production is off
strict_check $flgs $runid model-run-${event_year}m${event_mon}

#-----------------------------------

pakrs=${pakrs:='pakrs2'}
HOSTIDf6=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-6` ; if [ "$HOSTIDf6" = 'xc1mom' -o "$HOSTIDf6" = 'xc2mom' -o "$HOSTIDf6" = 'xc3mom' -o "$HOSTIDf6" = 'xc4mom' ] ; then XCPRFX='aprun -n 1 -cc none ' ; fi
# Possibly, setup to use "float1" executables...
if [ "$float1" = 'on' ] ; then FLTEXTNSN='_float1' ; else FLTEXTNSN=' ' ; fi

#  --------------------------------- Access executables,etc

# access AN   ${start}an  nocp=off
access GCM  ${start}ab  nocp=off
access PAR  ${start}par nocp=off

# The new physical namelist
access PHYS_PARM ${start}PHYS_PARM nocp=off

# The inline diagnostics namelist
access INLINE_DIAG_NL ${start}INLINE_DIAG_NL nocp=off

#  --------------------------------- Access restart files

access OLDRS ${start}rs_nmf na
if [ ! -s OLDRS ] ; then
  #  ------------------------------- unpack, if needed
  access RS ${start}rs
  $XCPRFX unpakrs${FLTEXTNSN} RS OLDRS
fi

access OLDTS ${start}ts_nmf na
if [ ! -s OLDTS ] ; then
  #  ------------------------------- unpack, if needed
  access TS ${start}ts na
  if [ -s TS ] ; then
    $XCPRFX unpakrs${FLTEXTNSN} TS OLDTS
  fi
fi

#  ----------------------------------- create namlist input cards for tracers
#                                      in tracnl.dat file
if [ -n "$nlcjcl" ] ; then
  . $nlcjcl.cdk
else
  . nlcjcl.cdk
fi

#  ----------------------------------- AGCM parameter file

cat > modl0.dat <<endcat
  gcm     $ksteps\$kfinal    0$levs$incd
          $issp$isgg$israd$isen$iepr$icsw$iclw$isbeg$jlatpr$istr$ishf$isst
          $iocean$icemod$itrvar
 $delt$ifdiff$coord$moist$iyear
endcat
if [ -s tracnl.dat ] ; then
  cat tracnl.dat >> modl0.dat
fi

if [ -n "$RF_SCENARIO" ] ; then
  # write radiative forcing run time parameters to a file
  cat > RF_RTP <<endcat
    &rf_rtp
    rf_scenario = "$RF_SCENARIO"
    trop_idx = ${trop_idx:=-1}
    start_year_r = ${start_year_r:=1850}
    /
endcat
fi

#  ----------------------------------- Access multi-year boundary condition
#                                      files through the coupler.

# individual netcdf files for GT/SICN/SIC
if [ "${coupler_specified_bc_file_gt}" != "" ] ; then
  access coupler_specified_bc_file_gt.nc ${coupler_specified_bc_file_gt}.nc
elif [ "${coupler_specified_bc_file}" != "" ] ; then
  access coupler_specified_bc_file_gt.nc ${coupler_specified_bc_file}_gt.nc
fi
if [ "${coupler_specified_bc_file_sicn}" != "" ] ; then
  access coupler_specified_bc_file_sicn.nc ${coupler_specified_bc_file_sicn}.nc
elif [ "${coupler_specified_bc_file}" != "" ] ; then
  access coupler_specified_bc_file_sicn.nc ${coupler_specified_bc_file}_sicn.nc
fi
if [ "${coupler_specified_bc_file_sic}" != "" ] ; then
  access coupler_specified_bc_file_sic.nc ${coupler_specified_bc_file_sic}.nc
elif [ "${coupler_specified_bc_file}" != "" ] ; then
  access coupler_specified_bc_file_sic.nc ${coupler_specified_bc_file}_sic.nc
fi
# GT/SICN/SIC climatologies in one file
if [ "${coupler_specified_bc_file_clim}" != "" ] ; then
  access coupler_specified_bc_file.nc ${coupler_specified_bc_file_clim}.nc
fi
# additional GT anomaly
if [ "${coupler_specified_bc_file_gtano}" != "" ] ; then
  access coupler_specified_bc_file_gtano.nc ${coupler_specified_bc_file_gtano}.nc
fi

if [ ! -s LAKES  ] ; then
  access LAKES  $lakes  na
fi
if [ ! -s TARGET ] ; then
  access TARGET $target na
fi

#  ----------------------------------- Access extra GCM input files.

. $gcm_inpfls_extra.cdk
. gcm_access_extra.cdk
. cgcm_access_extra.cdk

if [ -n "$spfilt" -a ! -f "SPFILT" ] ; then
  access SPFILT $spfilt
fi

if [ "$relax" = on ] ; then
   if [ -n "${target_ss_pfx}" -a ! -s SPCFILE ] ; then
     access SPCFILE ${target_ss_pfx}_${year}_ss
   else
     .  reanal_sp2ss.cdk
   fi
   if [ -n "${target_soil_tg1}" -a ! -s TG1FILE ] ; then
     access TG1FILE ${target_soil_tg1} # top soil layer temperature
#      ggstat TG1FILE | head
#      ggstat TG1FILE | tail
   fi
   if [ -n "${target_soil_wg1}" -a ! -s WG1FILE ] ; then
     access WG1FILE ${target_soil_wg1} # top soil layer volumetric moisture
#      ggstat WG1FILE | head
#      ggstat WG1FILE | tail
   fi

# access daily SST
   if [ -n "${target_ocn_sst}" -a ! -s SSTFILE ] ; then
     access SSTFILE  ${target_ocn_sst}
#      ggstat SSTFILE | head
#      ggstat SSTFILE | tail
   fi

# access daily SICN
   if [ -n "${target_ocn_sicn}" -a ! -s SICNFILE ] ; then
     access SICNFILE ${target_ocn_sicn}
#      ggstat SICNFILE | head
#      ggstat SICNFILE | tail
   fi

# access daily SIC
   if [ -n "${target_ocn_sic}" -a ! -s SICFILE ] ; then
     access SICFILE ${target_ocn_sic}
#      ggstat SICFILE | head
#      ggstat SICFILE | tail
   fi

# spectral climatological bias-correcting tendencies
   if [ -n "$tspcclim" -a "$tspcclim" != " " -a ! -s "TSPCCLIM" ] ; then
     access TSPCCLIM $tspcclim
#      ggstat TSPCCLIM | head
#      ggstat TSPCCLIM | tail
   fi
# gridded climatological bias-correcting tendencies
   if [ -n "$tgrdclim" -a "$tgrdclim" != " " -a ! -s "TGRDCLIM" ] ; then
     access TGRDCLIM $tgrdclim
#      ggstat TGRDCLIM | head
#      ggstat TGRDCLIM | tail
   fi
fi

#  ----------------------------------- Setup to run in parallel mode.

. gcm_parallel_setup.cdk
if [ -z "$cmd_runx" ] ; then
  echo $cmd_runx > prunsc
fi

crnt_year=`echo $year | awk '{printf "%04d",$1}'`
crnt_monthd=`echo $mon | awk '{printf "%02d",$1}'`
run_prfx=`echo ${model} | sed -n -e 's/\(.*_\)[0-9]*_m[0-9][0-9][ _]*$/\1/p'`
tail_suffix=`echo ${model} | sed -n -e 's/^.*_$/_/p'`

#  ----------------------------------- Loop over number of months.

months_run=${months_run:-1}
nruns=`expr \( $months - 1 \) / $months_run + 1`
months_sofar=$months_run

for month_counter in `seq $nruns -1 1` ; do

  previous_year=$crnt_year
  previous_month=$crnt_monthd
  current_year=$crnt_year
  current_month=`echo $crnt_monthd $months_run | awk '{printf "%02d",$1+$2-1}'`
  if [ $current_month -gt 12 ] ; then
    current_month=`echo $current_month | awk '{printf "%02d",$1-12}'`
    current_year=`echo $current_year | awk '{printf "%04d",$1+1}'`
  fi
  echo current_year=$current_year current_month=$current_month

  # model/restart names
  model="${run_prfx}${crnt_year}_m${crnt_monthd}${tail_suffix}"
  model_rs="${run_prfx}${current_year}_m${current_month}${tail_suffix}"
  echo model=$model
  echo model_rs=$model_rs

  # replace kfinal value in model parameter file modl0.dat
  rm -f modl.dat
  echo "kfinal=$kfinal"
  sed -e "s/\$kfinal/$kfinal/" modl0.dat > modl.dat
  cat modl.dat

  #  ----------------------------------- CanCPL setup

  if [ "$use_cancpl" = "on" ]; then
    . $CCRNSRC/CanESM/CanCPL/lib/cpl_prelude
  fi

  #  ----------------------------------- NEMO setup

  if [ "$use_nemo" = "on" ]; then
    . $CCRNSRC/CanESM/CanNEMO/lib/nemo_prelude
  fi

  # We want to exit here to capture the configuration before running
  exit 0

  #  ------------------------------- Run the model.
  [ -n "$oeList" ] && touch $oeList && chmod a+r $oeList || :
  cat prunsc
  ./prunsc && model_status=$? || model_status=$?
  echo model_status=$model_status

  #  ------------------------------- Collect output
  #
  oelist=`echo oe0*`
  if [ "$oelist" != 'oe0*' ] ; then
    for oefil in $oelist ; do
      cat $oefil | sed -e "s/^/$oefil - /"
    done
    if [ "$debug" != "on" ] ; then
      rm $oelist
    fi
  fi

  #  ------------------------------- Exit if model is aborted.

  if [ $model_status -ne 0 ] ; then
    exit 1
  fi

  #  ------------------------------- Save model files

  . gcmpak.cdk

  #  ------------------------------- Rename and save rs files

  release  OLDRS
  mv NEWRS OLDRS
  save OLDRS ${model_rs}rs_nmf

  if [ -s NEWTS ] ; then
    release  OLDTS
    mv NEWTS OLDTS
    save OLDTS ${model_rs}ts_nmf
  fi

  #  ------------------------------- Save static files

  # ln AN   AN_SAVE
  # save    AN_SAVE ${model_rs}an
  # release AN_SAVE

  cp PAR  PARSAVE
  save    PARSAVE ${model_rs}par
  release PARSAVE

  cp      PHYS_PARM PHYS_PARM_SAVE
  save    PHYS_PARM_SAVE ${model_rs}phys_parm
  release PHYS_PARM_SAVE

  cp      INLINE_DIAG_NL INLINE_DIAG_NL_SAVE
  save    INLINE_DIAG_NL_SAVE ${model_rs}inline_diag_nl
  release INLINE_DIAG_NL_SAVE

  #  ----------------------------------- CanCPL postlude

  if [ "$use_cancpl" = "on" ]; then
    . $CCRNSRC/CanESM/CanCPL/lib/cpl_postlude
  fi

  #  ----------------------------------- CanNEMO postlude

  if [ "$use_nemo" = "on" ]; then
    . $CCRNSRC/CanESM/CanNEMO/lib/nemo_postlude
  fi

  # setup for next month and adjust if necessary the "year" field:

  if [ $month_counter -gt 1 ] ; then
    crnt_monthd=`expr $crnt_monthd + $months_run`
    if [ "$crnt_monthd" -gt 12 ] ; then
      crnt_monthd=`expr $crnt_monthd - 12`
      crnt_year=`expr $crnt_year + 1`
    fi
    crnt_monthd=` printf '%02d' $crnt_monthd`
    crnt_year=` printf "%04d" $crnt_year`

    # adjust kfinal value for the next period. Do not exceed $months in total
    months_sofar=`expr $months_sofar + $months_run`
    echo months_sofar=$months_sofar
    if [ $months_sofar -le $months ] ; then
      crnt_monthe=`expr $crnt_monthd + $months_run`
      echo crnt_monthe=$crnt_monthe
    else
      crnt_monthe=`expr $crnt_monthd + $months_sofar - $months`
      echo crnt_monthe=$crnt_monthe
      crnt_monthe=`expr $mon + $months`
      echo crnt_monthe=$crnt_monthe
    fi
    cday1=`echo "0 31 59 90 120 151 181 212 243 273 304 334 365 396 424 455 485 516 546 577 608 638 669 699 730" | cut -f$crnt_monthd -d' '`
    cday2=`echo "0 31 59 90 120 151 181 212 243 273 304 334 365 396 424 455 485 516 546 577 608 638 669 699 730" | cut -f$crnt_monthe -d' '`
    echo cday1=$cday1
    echo cday2=$cday2
    ndays_run=`expr $cday2 - $cday1`
    echo ndays_run=$ndays_run
    kfinal=`echo $kfinal $ndays_run $delt | awk '{printf "%10d",$1+$2*86400/$3}'`
    echo kfinal=$kfinal
  fi

  #  --------------------------------- End of loop.
done

# save executables files for the next chunk

touch GCM
save  GCM ${model_rs}ab
if [ "$use_cancpl" = "on" ]; then
  touch cpl_main
  save  cpl_main ${model_rs}cpl.exe
fi

# save environment and jobstring
cp $HOME/.queue/.crawork/${crawork}_string ${model_rs}jobstring
env > ${model_rs}env
save ${model_rs}jobstring ${model_rs}jobstring
save ${model_rs}env ${model_rs}env

##############################################################
# create and submit diagnostic job to frontend every 12 months,
# or at the end of the run

if [ $with_post_processing -eq 1 ] ; then

if [ $run_start_month -eq 1 ] ; then
  diag_end_month=12
else
  diag_end_month=`echo $run_start_month | awk '{printf "%02d",$1-1}'`
fi

echo current_month=$current_month diag_end_month=$diag_end_month
echo current_YYYYMM=$current_year$current_month run_stop_YYYYMM=$run_stop_year$run_stop_month

if [ $current_month -eq $diag_end_month -o $current_year$current_month -eq $run_stop_year$run_stop_month ] ; then

diag_start_year=$current_year
if [ $diag_start_year$run_start_month -gt $current_year$current_month ] ; then
  diag_start_year=`expr $current_year - 1`
fi

echo "make_diag_job_${runid}_${diag_start_year}m${run_start_month}_${current_year}m${current_month}"

cd $WRK_DIR

finp=make_diag_job_${runid}
fout=make_diag_job_${runid}_${diag_start_year}m${run_start_month}_${current_year}m${current_month}
diagjob_sfx=diagjob_`echo ${CMCFEDEST} | cut -f2 -d'-'`_${DIAGJOB_SFX}

# modify some parameters in make_diag_job_${runid}
rm -f $fout
cat $finp | sed \
 -e "s/^start\=[0-9]\+/start\=$diag_start_year:$run_start_month/" \
 -e "s/^stop\=[0-9]\+/stop\=$current_year:$current_month/" \
 -e "s/crawork\=.\+/crawork\=${runid}_${diagjob_sfx}/" \
> $fout

# create diag string to be run on front end
chmod +x $fout
./$fout

# the resulting diag job is saved in
fdiag="${runid}_${diag_start_year}m${run_start_month}_${current_year}m${current_month}_diag_job"

flist="$fout"
if [ -s $fdiag ] ; then
  # diagnostic string is not empty - submit or append to the existing string in .crawork

  # checks if the job is being updated
  ntrymax=10
  for n in `seq 1 1 $ntrymax` ; do
    if [ -f "$HOME/.queue/.crawork/.${runid}_${diagjob_sfx}_string_updtng" ] ; then
      sleep 5
    else
      break
    fi
    if [ $n -eq $ntrymax ] ; then
      abort='abort'
      exit 1
    fi
  done
  if [ -s "$HOME/.queue/.crawork/${runid}_${diagjob_sfx}_string" ] ; then
    # append job to the existing string
    cat $fdiag >> $HOME/.queue/.crawork/${runid}_${diagjob_sfx}_string
  else
    # submit diag job to FE
    dest=`echo $CMCFEDEST | cut -f2 -d'-'`
    ssh $dest "cd $WRK_DIR ; . env_setup_file ; rsub mdest=$dest $fdiag" && status=$? || status=$?
    echo status=$status
    if [ $status -ne 0 ] ; then
      # failed to submit. Copy to crawork
      cp $fdiag $HOME/.queue/.crawork/${runid}_${diagjob_sfx}_string
    fi
  fi
  flist="$flist $fdiag"
fi # -s $fdiag

# move jobs to post_jobs subdirectory
mv $flist $CCRNSRC/post_jobs/. && status=$? || status=$?
echo status=$status
if [ $status -ne 0 ] ; then
  # failed to move
  echo "Warning: hostname=`hostname`. Failed to move $fout $fdiag to $CCRNSRC/post_jobs/." > $HOME/.queue/${fout}_warning_failed_to_move
  ls -ld $CCRNSRC/post_jobs/. >> $HOME/.queue/${fout}_warning_failed_to_move 2>> $HOME/.queue/${fout}_warning_failed_to_move || :
fi

fi # $current_month -eq $diag_end_month ...

fi # $with_post_processing -eq 1

