#!/bin/bash -x
#SBATCH -J aste270
#SBATCH -o aste270.%j.out
#SBATCH -e aste270.%j.err
####SBATCH -p sverdrup
#SBATCH -t 300:00:00 
#SBATCH -N 22
#SBATCH -n 580 
#SBATCH --mail-user=
#SBATCH --mail-type=begin
#SBATCH --mail-type=end

#--- 0.load modules ------
#ulimit -s unlimited
ulimit -s hard
ulimit -u hard

#---- 1.set variables ------
#note for bash: can not have any space around =

nprocs=580
snx=18
sny=18
#iter0
#pickupts0="0000000000"
#pickupts1="0000000002"
#extpickup="_2yrjra55" 
#iter62
pickupts0="0000000006"
pickupts1="0000000007"
extpickup=
##extpickup="_it55warm" 
extsmooth="_9x1000mod"

ext4="_diffkr"
ext3="_obcspretanbl_adv7"

use_optim=0
read_xx=1

iter=62
whichexp="_BE2_dthetadr"
jobfile=script${whichexp}.bash

if [ ${iter} -lt 10 ]; then
    ext2=000${iter}
elif [ ${iter} -lt 100 ]; then
    ext2=00${iter}
elif [ ${iter} -lt 1000 ]; then
    ext2=0${iter}
else
    ext2=${iter}
fi

#--- 2.set dir ------------
  basedir=/home/atnguyen/nansen/MITgcm_c65q/mysetups/aste_270x450x180
  scratchdir=/scratch/atnguyen/aste_270x450x180/
  builddir=$basedir/build${whichexp}_sshv4mo_fix_logdkr2_bgkppredi_27Jul2024
  codedir=$basedir/code${whichexp}_sshv4mo_fix_logdkr2
  inputdir=$basedir/input${whichexp}_sshv4mo_fix_logdkr2
  workdir=$scratchdir/run${whichexp}${ext3}${ext4}_it${ext2}_pk${pickupts1}

  if [ ! -d $workdir ]; then
    mkdir -p $workdir;
  fi
  mkdir -p $workdir/tapes;
  mkdir -p $workdir/profiles;
  mkdir -p $workdir/diags;
  cd $workdir;

  rm -f tapes/*
  rm -f profiles/*

  cp -rf ${codedir}/ .
  cp -rf ${basedir}/code_adv7_bypass_tamc .
  cp -rf ${basedir}/code_diagnose_kpp_redi_df .

#--- 3. link forcing -------------
  if [ ! -h jra55 ]; then
    ln -s /scratch/atnguyen/jra55 ./jra55
  fi

#--- 4. linking binary ---------
\rm -f OB*
ln -s ${scratchdir}/run_template/input_binaries/*.bin .
ln -s ${scratchdir}/run_template/input_insitu/+aste/+${snx}x${sny}/*.nc .
ln -s ${scratchdir}/run_template/input* .
ln -s ${scratchdir}/run_template/input_weight/* .
ln -s ${scratchdir}/run_template/input_binaries/tile*.mitgrid .
ln -s ${scratchdir}/run_template/input_climatology/* .
ln -s ${scratchdir}/run_template/input_obcs/input_obcs_r3_precip_bl/*.bin .
###
ln -s ${scratchdir}/run_template/input_smooth/smooth2Dscales001${extsmooth} ./smooth2Dscales001
ln -s ${scratchdir}/run_template/input_smooth/smooth3DscalesH001${extsmooth} ./smooth3DscalesH001
ln -s ${scratchdir}/run_template/input_smooth/smooth3DscalesZ001 ./
ln -s ${scratchdir}/run_template/input_smooth/smooth2Dnorm001${extsmooth}.data ./smooth2Dnorm001.data
ln -s ${scratchdir}/run_template/input_smooth/smooth3Dnorm001${extsmooth}.data ./smooth3Dnorm001.data
ln -s ${scratchdir}/run_template/input_smooth/smooth2Dnorm001.meta ./
ln -s ${scratchdir}/run_template/input_smooth/smooth3Dnorm001.meta ./

#=================================================================================
#--- 6. NAMELISTS ---------
#need to copy namelists before xx
  cp -f ${inputdir}/* .
  cp -f ${basedir}/${jobfile} .

  cp -f data_profiles_${snx}x${sny}x${nprocs} data.profiles
  cp -f data_exch2_${snx}x${sny}x${nprocs} data.exch2

#=================================================================================
#--- 5. linking xx_ fields ------
  xxiter=000000${ext2}
  xxiter1=0062
  xxiter0=0000

#set iter0
#  declare -a arr0=("kapgm" "kapredi" "theta" "salt" "logdiffkr" "atemp" "swdown" "lwdown" "precip" "uwind" "vwind")
#  declare -a arr1=("aqh")
#set it62
  declare -a arr0=("kapgm" "kapredi" "theta" "salt" "logdiffkr")
  declare -a arr1=("aqh" "uwind" "vwind" "lwdown" "swdown" "atemp" "precip")
#
  if [ ${use_optim} -gt 0 ]; then
    cp -f ${optimdir}/ecco_ctrl_MIT_CE_000.opt${ext2} ./
    sed -i -e 's/'"doinitxx = .TRUE."'/'"doinitxx = .FALSE."'/g' data.ctrl
    sed -i -e 's/'"doinitXX = .TRUE."'/'"doinitXX = .FALSE."'/g' data.ctrl
    sed -i -e 's/'"doMainUnpack = .FALSE."'/'"doMainUnpack = .TRUE."'/g' data.ctrl
    sed -i -e 's/'"doMainPack = .FALSE."'/'"doMainPack = .TRUE."'/g' data.ctrl
  else
   if [ ${read_xx} -gt 0 ]; then
#copy pre-estimated control vectors
    dirADXX0=${scratchdir}/run_c65q_jra55_it${xxiter0}_pk0000000002/ADXXfiles/
    for i in "${arr0[@]}"
    do
      cp ${dirADXX0}/xx_$i.000000${xxiter0}.data ./xx_$i.000000${ext2}.data
      echo ${dirADXX0}/xx_$i.000000${xxiter0}.data
    done
    dirADXX1=${scratchdir}/run_BE2_dthetadr_unpack_it${xxiter1}_pk0000000006/ADXXfiles_mod/
    for i in "${arr1[@]}"
    do
      cp ${dirADXX1}/xx_$i.000000${xxiter1}.data    ./xx_$i.000000${ext2}.data
      echo ${dirADXX1}/xx_$i.000000${xxiter1}.data
    done
    sed -i -e 's/'"doinitXX = .TRUE."'/'"doinitXX = .FALSE."'/g' data.ctrl
    sed -i -e 's/'"doinitxx = .TRUE."'/'"doinitxx = .FALSE."'/g' data.ctrl
    sed -i -e 's/'"doMainUnpack = .TRUE."'/'"doMainUnpack = .FALSE."'/g' data.ctrl
    sed -i -e 's/'"doMainPack = .TRUE."'/'"doMainPack = .FALSE."'/g' data.ctrl
   else
    sed -i -e 's/'"doinitXX = .FALSE."'/'"doinitXX = .TRUE."'/g' data.ctrl
    sed -i -e 's/'"doinitxx = .FALSE."'/'"doinitxx = .TRUE."'/g' data.ctrl
    sed -i -e 's/'"doMainUnpack = .TRUE."'/'"doMainUnpack = .FALSE."'/g' data.ctrl
    sed -i -e 's/'"doMainPack = .TRUE."'/'"doMainPack = .FALSE."'/g' data.ctrl
   fi
  fi

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

#--- 7. executable --------
\rm -f mitgcmuv*
cp -f ${builddir}/mitgcmuv_${snx}x${sny}x${nprocs}_ol8 ./mitgcmuv
cp -f ${builddir}/Makefile .

#--- 8. pickups -----------
#NOTE: for pickup: copy instead of link to prevent accidental over-write
\rm -f pickup*
if [[ ${pickupts0} ]]; then
pickupdir=${scratchdir}/run_layerswsurfSPforc_logdiffkr_1_it0062_pk0000000006warm
  cp -f ${pickupdir}/pickup${extpickup}.${pickupts1}.data ./pickup.${pickupts1}.data
  cp -f ${pickupdir}/pickup${extpickup}.${pickupts1}.meta ./pickup.${pickupts1}.meta
  cp -f ${pickupdir}/pickup_seaice${extpickup}.${pickupts1}.data ./pickup_seaice.${pickupts1}.data
  cp -f ${pickupdir}/pickup_seaice${extpickup}.${pickupts1}.meta ./pickup_seaice.${pickupts1}.meta
fi

#--- 9. make a list of all linked files ------

\rm -f command_ln_input
ls -l input_* > command_ln_input
ls -l input*/input_* >> command_ln_input

\rm -f command_ln_binary
ls -l *.nc > command_ln_binary
ls -l *.bin >> command_ln_binary
ls -l tile* >> command_ln_binary
ls -l smooth* >> command_ln_binary
ls -l xx* >> command_ln_binary

#--- 10. (re)set optimcycle --------------------

\rm data.optim
cat > data.optim <<EOF
 &OPTIM
 optimcycle=${iter},
 /
EOF

#--- 11. run ----------------------------------

#module load remora
set -x
date > run.MITGCM.timing
mpiexec -np ${nprocs} ${workdir}/mitgcmuv
date >> run.MITGCM.timing
