#!/bin/csh -x
#PBS -S /bin/csh
##-------------------
##### an example for 242 cpus, based on 24cpu/node resource
#####PBS -l select=1:ncpus=2+10:ncpus=24
#####PBS -l walltime=48:00:00
##-------------------
#PBS -j oe
#PBS -m abe

#--- 0.load modules ------
#check your .cshrc
limit stacksize unlimited

#---- 1.set variables ------

#set nprocs    = 580
#set snx       = 18
#set sny       = 18
set nprocs    = 242
set snx       = 30
set sny       = 30
#set nprocs    = 117
#set snx       = 45
#set sny       = 45
#set nprocs    = 36
#set snx       = 90
#set sny       = 90
##-----------
set pickupts0 = 0000000006
set pickupts1 = 0000000007
set extpickup = _it55warm
set extsmooth = _9x1000mod

set ext0      = 00

set iter      = 62
set forwadj   = 
set whichexp  = _ASTE_R1
set jobfile   = script${whichexp}${forwadj}.csh

if ( ${iter} < 10 ) then
    set ext2 = 000${iter}
else if ( ${iter} < 100 ) then
    set ext2 = 00${iter}
else if ( ${iter} < 1000 ) then
    set ext2 = 0${iter}
endif

#--- 2.set dir ------------
set basedir  = ./
set builddir  = $basedir/build${whichexp}${forwadj}
set codedir   = $basedir/code${whichexp}
set inputdir  = $basedir/NAMELISTS${whichexp}
set workdir   = $basedir/run${whichexp}_pk${pickupts1}

if (! -e $workdir) then
  mkdir -p $workdir ;
  #mkdir -p $workdir/tapes ;
  #mkdir -p $workdir/profiles ;
  mkdir -p $workdir/diags
endif
cd $workdir;

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

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

#--- 3. link forcing -------------
if (! -e jra55) then
  ln -s $basedir/jra55 ./jra55
endif

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

#=================================================================================
#--- 6. NAMELISTS ---------
#need NAMELISTS before xx block in order to have data.ctrl for later
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 ------
set dirADXX=${basedir}/input_binaries/

#3D fields have already been mapped into pickups and input files
# so we're reading in a set of zeros:
set arr3d=(kapredi kapgm salt theta logdiffkr)
foreach i ($arr3d)
  cp ${dirADXX}/xx_$i.000000${ext2}.data ./xx_$i.000000${ext2}.data
  echo xx_$i.000000${ext2}.data
end

#2D time-varying fields were not mapped into jra55 because it cannot
#be done accurately in the Arctic.  As a result, need iter62 xx files:
set arr2d=(aqh atemp uwind vwind precip lwdown swdown)
foreach i ($arr2d)
  cp ${dirADXX}/xx_$i.000000${ext2}.data ./xx_$i.000000${ext2}.data
  echo xx_$i.000000${ext2}.data
end

#setting data.ctrl flags in case they're not compatible
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 = .FALSE."'/'"doMainPack = .TRUE."'/g' data.ctrl

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

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

#--- 8. pickups -----------
#NOTE: for pickup: copy instead of link to prevent accidental over-write
\rm -f pickup*
set pickupdir = ${basedir}/input_binaries

if($?pickupts0) then
  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
endif

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

#\rm -f command_ln_input
#ls -l 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 ----------------------------------
#
## for 242 cpus:
date +%Y/%m/%d_%H:%M:%S.%3N > run_mitgcm_timing.txt
mpiexec ./mitgcmuv${forwadj}
date +%Y/%m/%d_%H:%M:%S.%3N >> run_mitgcm_timing.txt
