#!/bin/sh
###############################################################################
####  UNIX Script Documentation Block                                         #
#                                                                             #
# Script name:         exofs_atl_update.sh.sms                                #
# Script description:                                                         #
#                                                                             #
# Author:        Dan Iredell       Org: NP23         Date: 2005-06-14         #
#                                                                             #
# Abstract: This script combines ssh and ctd analyses with restrat fields     #
#                                                                             #
# Sub-scripts called:                                                         #
#    ofs_edit_restart.sh : edit restart file with ssh and ctd assims          #
#                                                                             #
# Script history log:                                                         #
# 2005-07-27  Dan Iredell                                                     #
#                                                                             #
###############################################################################

set -x

# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation


export PS4='$SECONDS + '

# send extra output for monitoring purposes.

export monitor=1

cd $DATA

msg="${modID} update of restart file  has begun on `hostname`"
postmsg "$jlogfile" "$msg"

# 0.c set restart file identifier and verification
export restart_previous=$COMINm1

if [ -f $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.a -a -f $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.b ]
then
      msg1="Restart File available."
      postmsg "$jlogfile" "$msg1" 
else
      msg1="Missing Restart File"
      msg2="ABNORMAL EXIT ASSIMILATION: FATAL ERROR--NO RESTART FILE"
      postmsg "$jlogfile" "$msg1" "$msg2"
      err=911;
      export err;err_chk
fi
##########################
# 1. check existence of input files
##########################

if [ $runssh -eq 1 ] 
then
  # Check input files for SSH assimilation
  if [ ! -s $COMIN/${RUN}_${modID}.t${cyc}z.ssh.${PDY}.spread.bin ] || \
     [ ! -s $COMIN/${RUN}_${modID}.t${cyc}z.ssh.${PDY}.adh.bin ]
  then
    runssh=0
    echo "Now runssh=$runssh"
  fi
fi
if [ $runctd -eq 1 ]
then
  # Check input files for CTD assimilation
  typeset -Z2 klev
  klev=1
  # for update, only need 5 levels
  while [ $klev -le 5 ]
  do
    for fd in T Z D 
    do
      filein=$COMIN/${RUN}_${modID}.t${cyc}z.ctd.${fd}${klev}.${PDY}.spread.bin
      if [ ! -s $filein ]
      then
        runctd=0
	echo "The file $filein is missing. "
        echo "Now runctd=$runctd"
        break
      fi
    done
    if  [ $runctd -eq 0 ] ; then
      break
    fi
    klev=`expr $klev + 1 `
  done
fi

##########################
# 2. place restart files in output directory after changes if any.
##########################

if [[ $runssh -eq 1 || $runctd -eq 1 ]] ; then

#2.a edit restart file to include changes due to sea surface heigth or CTD information.

   cp $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.a $DATA/next_restart.a
   cp $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.b $DATA/next_restart.b

   ${USHofs}/ofs_edit_restart.sh $PDY
   msg="restart file  from   $restart_previous   was  modified."
   if [ $SENDCOM = YES ]
   then
      cp  $DATA/updated_restart.a $COMOUT/${RUN}_${modID}.t${cyc}z.updated_restart.a
      cp  $DATA/updated_restart.b $COMOUT/${RUN}_${modID}.t${cyc}z.updated_restart.b
      echo $msg > $COMOUT/${RUN}_${modID}.restart_status
   fi

 else
 if [ $minor_edit_restart -eq 1 ]; then
#2.b place restart file in the output directory WITH range  modification.

   cp $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.a $DATA/next_restart.a
   cp $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.b $DATA/next_restart.b

   ${USHofs}/ofs_minor_edit_restart.sh $PDY
   msg="restart file  from   $restart_previous   was  modified for correctness."
   if [ $SENDCOM = YES ]
   then
      cp  $DATA/updated_restart.a $COMOUT/${RUN}_${modID}.t${cyc}z.updated_restart.a
      cp  $DATA/updated_restart.b $COMOUT/${RUN}_${modID}.t${cyc}z.updated_restart.b
      echo $msg > $COMOUT/${RUN}_${modID}.restart_status
   fi



 else
 
#2.c place restart file in the output directory WITHOUT  modification.
#
    msg="WARNING: restart file  from $restart_previous was not modified."
    if [ $SENDCOM = YES ]
    then
       cp  $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.a $COMOUT/${RUN}_${modID}.t${cyc}z.updated_restart.a 
       cp  $restart_previous/${RUN}_${modID}.t${cyc}z.next_restart.b $COMOUT/${RUN}_${modID}.t${cyc}z.updated_restart.b
       echo $msg 
       echo $msg > $COMOUT/${RUN}_${modID}.restart_status
    fi
  fi
fi


postmsg "$jlogfile" "$msg"

msg='THE OFS_UPDATE JOB HAS ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################
