#!/bin/sh
###############################################################################
#                                                                             #
# Script Name: estofs_multistart.sh                                           #
#                                                                             #
# Technical Contact(s): Name: Yuji Funakoshi       Org: NOS/OCS/CSDL/MMAP     #
#                       Phone: 301-7132809 ext.113                            #
#                       E-Mail: yuji.funakoshi@noaa.gov                       #
#                                                                             #
# Abstract:                                                                   #
# This program is to find the most recent restart file for ESTOFS and         #
# determines the corresponding initial time                                   #
#                                                                             #
# Usage: ./estofs_multistart.sh                                               #
#                                                                             #
# Input Parameters:                                                           #
#                                                                             #
# Language:   Bourne Shell Script                                             #
#                                                                             #
# Target Computer: CIRRUS/STRATUS at NCEP                                     #
#                                                                             #
# Estimated Execution Time: < 1 minutes                                       #
#                                                                             #
# Suboutines/Functions Called:                                                #
#                                                                             #
# Input Files:                                                                #
# Name               Directory Location            Descriptioni               #
#                                                                             #
# Output Files:                                                               #
# Name               Directory Location            Description                #
# estofs_multistart  /work                         the most recent restart    #
# .out                                             time                       #
#                                                                             #
# Libraries Used: see the makefile                                            #
#                                                                             #
# Author Name: Yuji Funakoshi                      Creation Date: Dec, 2011   #
#                                                                             #
# Revisions:                                                                  #
# Date               Author                        Description                #
#                                                                             #
# Remarks:                                                                    #
# - The ymdh of the restart file is copied to estofs_multistart.out           #
#                                                                             #
###############################################################################
set -x
# Start of estofs_multistart.sh script -------------------------------------- #
# 1.  Set times
# 1.a Loop to find file

  export date=$PDY
  export YMDH=${PDY}${cyc}

  wndh=3
  nowh=6

  ymdh=`$NDATE $nowh $YMDH`
  iback=0

  while [ $iback -le $nback ]
  do
    ymdh=`$NDATE -$nowh $ymdh`
    pdate=`echo $ymdh | cut -c 1-8`
    pcycle=t`echo $ymdh | cut -c 9-10`z

    dir=${COM}/${RUN}.${pdate}
    file=${RUN}.${modID}.${pcycle}.restart

    if [ -d $dir ]
    then
      set +e
      nr=`ls ${dir}/$file 2> /dev/null | wc -l | awk '{print $1}'`
      set -e
      if [ $nr -gt 0 ]
      then
        iback=$nback
      fi
    fi
    iback=`expr $iback + 1`
  done

# --------------------------------------------------------------------------- #
# 2.  Write time

  echo $ymdh > estofs_multistart.out

# End of estofs_multistart.sh script ---------------------------------------- #
