#!/bin/sh
#
########################################################################
#  This script runs the MOM_3 ocean model  for the number of days      #
#  specified in the namelist "namelist.control".  In the coupled       #
#  this is set to be equal to one day.                                 #
########################################################################
#  Set environment.
export VERBOSE=${VERBOSE:-NO}
if [[ $VERBOSE = YES ]] ; then
  echo $(date) EXECUTING $0 $* >&2
  set -x
fi
#

export CASE=basic_ibm         ;# Define experiment paths

export EXECDIR_OCN=${1:-/nwprod/exec}   ;# MOM3 script/exec directory
export WORKDIR=$2                       ;# forecast job directory
export VDATE=$3                         ;# current forecast (model) time
export mom3rt=$4                        ;# restoring time scale (days)
export COM_YMDH=${5:-$WORKDIR}
#
export ERRSCRIPT=${ERRSCRIPT:-'eval [[ $err = 0 ]]'}

echo VDATE = $VDATE beginning MOM3 `date`

#
export DATAOUTpath=$WORKDIR/data/mom3
#
if [ ! -s $WORKDIR ] ; then
 echo "$WORKDIR does not exist."
 exit 1
fi
if [ ! -s $DATAOUTpath ] ; then
 mkdir -p $DATAOUTpath
fi
cd $WORKDIR


# ======================================================================
#   copy executable and namelists
# ======================================================================

export EXECDIR_OCN=${EXECDIR_OCN:-/nwprod/exec}
export OCEANEXEC=${OCEANEXEC:-${EXECDIR_OCN}/cfs_mom3}
export PGM=$WORKDIR/$(basename $OCEANEXEC)
export pgm=$PGM
${NCP:-/bin/cp} $OCEANEXEC $WORKDIR
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.blmix   namelist.blmix
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.contrl  namelist.contrl
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.diagn   namelist.diagn
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.ictime  namelist.ictime
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.io      namelist.io
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.isopyc  namelist.isopyc
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.kppmix  namelist.kppmix
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.mbcin   namelist.mbcin
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.mixing  namelist.mixing
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.ppmix   namelist.ppmix
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.riglid  namelist.riglid
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.smagnl  namelist.smagnl
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.svd     namelist.svd
${NCP:-/bin/cp} $PARMcfs/cfs_namelist.tsteps  namelist.tsteps

# modify parameters in namelist.mixing
cat >namelist.mixing  << NAMELIST
      &mixing am=2.e7, ah=2.e6, ambi=1.e22, ahbi=1.e21,
              kappa_m=25.0, kappa_h=1.0, aidif=1.0,
              nmix=17, eb=.true.,  ncon=1, cdbot=1.2e-3,
              acor=0.0, dampts(1)=${mom3rt}.0,dampts(2)=${mom3rt}.0,smooth=0.05,
              filter_reflat_s=-81.0,filter_reflat_n=79.0,
      /
NAMELIST

# ======================================================================
# Bring in the data
# ======================================================================

# Get the initial ocean condition (restart)
#
# Note: The model will assume the date stamp in the restart file is
# correct and will begin the integration referencing that date. The
# output files will be set accordingly from the date read in from the
# restart file. T restart file date stamp can be reset using the
# utility "convert_restart".

if [ ! -s restart.dta ] ; then
 echo 'restart.dta of mom3 is not available.'
 exit 1
fi

#
# Retrieve model setup files
#
export ocnfiles='sponge.mom scripps.top ls.dat kmt.dta ht.dta sss.mom sst.mom'
for file in  $ocnfiles
do
  if [ ! -s $file ] ; then
    ${NCP:-/bin/cp} $FIXcfs/cfs_$file $file
  fi
done
#
# Check for the atmospheric  forcing file
#
if [ ! -s flxMOM3.gr ] ; then
   echo flxMOM3.gr does not exist
   exit 1
fi


echo VDATE = $VDATE before MOM3 run `date`

# ======================================================================
# Run the model
# ======================================================================

echo '==>Running MOM3 on IBM SP...'

echo $pwd
rm printout
/usr/bin/poe $PGM >> $mom3$VDATE.out 2> errfile

export err=$?;$DATA/err_chk

# ======================================================================
# Make sure model ran correctly (and email user if it failed)
# ======================================================================

echo VDATE = $VDATE after MOM3 run `date`

# ==============================================================
# Do post-processing and save data to the ARCHIVE.
# (appending date suffix to filenames for identification)
# ==============================================================
#
export diag_suffix=`line < date.suffix.diag`
export diag_suffix=$(echo $diag_suffix | sed s'/^_*//')
#
# the restart_suffix is the date tag for the restart file
#
export restart_suffix=`line < date.suffix.rest`
export restart_suffix=$(echo $restart_suffix | sed s'/^_*//')

#
# gather the daily time_mean data from each processor into one file
# each file in file_list is from an individual processor
#
set -A file_list `ls -1 time_mean.$diag_suffix.dta.nc.????`
if [ ${#file_list[*]} -gt 0 ]; then
  diag_name=time_mean.$diag_suffix.nc
  if [ ${#file_list[*]} -gt 1 ]; then
    $mppnccombine $diag_name ${file_list[*]}
    export err=$?;$DATA/err_chk
  else
    mv ${file_list[0]} $diag_name
  fi
fi
#
#export  file_list= `/bin/ls time_mean.$diag_suffix.dta.nc.????`
#export  diag_name=time_mean.$diag_suffix.nc
#$mppnccombine $diag_name $file_list
#
#
if [[ $SENDCOM = YES ]] ; then
  ${NCP:-/bin/cp} $diag_name  $COM_YMDH
fi

#
# write out the SST from file $diag_name in to  sstMOM3.gr for atmosphere
#
if [ -s sstMOM3.gr ] ; then
   rm  sstMOM3.gr
fi 
echo VDATE = $VDATE after MOM3 daily gathering `date`
$skim_sst $diag_name
export err=$?;$DATA/err_chk
echo VDATE = $VDATE after MOM3 skim_sst `date`

#
# copy the daily time_mean and sstMOM3 files to the archive
#
if [[ $SENDCOM = YES ]] ; then
  ${NCP:-/bin/cp} $diag_name $COM_YMDH
  ${NCP:-/bin/cp} sstMOM3.gr $COM_YMDH/sstMOM3${diag_suffix}.gr
fi
/bin/mv sstMOM3.gr sstMOM3${diag_suffix}.gr

# archive the new restart file and link "restart.dta" to it
# /bin/cp restart.$restart_suffix.dta $DATAOUTpath
/bin/mv restart.$restart_suffix.dta restart.dta

if [ $? -eq 0 ] ; then  /bin/rm time_mean.$diag_suffix.dta.nc.* ; fi
/bin/rm fort.*

# save printout for analysis

if [ -s printout ] ; then
    echo "=> Saving printout file: prnt.$diag_suffix"
    echo "=>         in directory: $DATAOUTpath"
    ${NCP:-/bin/cp} printout $DATAOUTpath/prnt.$diag_suffix
fi

echo " "
echo "==> done"

echo VDATE = $VDATE after ending MOM3 `date`
#
if [[ $VERBOSE = YES ]] ; then
   echo $(date) EXITING $0 with return code $err >&2
fi
