#!/bin/sh
###############################################################################
#                                                                             #
# Script Name: estofs_adcprep.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 used to prepare ADCIRC running preparation                  #
#                                                                             #
# Usage: ./estofs_adcprep.sh ${time_spi} $spin $ncpu or                       #
# Usage: ./estofs_adcprep.sh ${time_now} $ncst $ncpu or                       #
# Usage: ./estofs_adcprep.sh ${time_now} $fcst $ncpu                          #
#                                                                             #
# Input Parameters:                                                           #
#  time_spi:  start time for the tidal spin-up run , e.g., YMDH               #
#  time_now:  start time for the nowcast and forecast run , e.g., YMDH        #
#  run_type:  Run type , e.g., spin, ncst, or fcst                            #
#                                                                             #
# Language:   Bourne Shell Script                                             #
#                                                                             #
# Target Computer: CIRRUS/STRATUS at NCEP                                     #
#                                                                             #
# Estimated Execution Time: < 2 minutes                                       #
#                                                                             #
# Suboutines/Functions Called:                                                #
# Name               Directory Location            Description                #
# estofs_adcprep     /exec                         ADCIRC preparation         #
#                                                                             #
# Input Files:                                                                #
# Name               Directory Location            Description                #
# adcprep.ctl        /work                         adcprep control file       #
#                                                                             #
# Output Files:                                                               #
# Name               Directory Location            Description                #
#                                                                             #
# Libraries Used: see the makefile                                            #
#                                                                             #
# Author Name: Yuji Funakoshi                      Creation Date: Dec, 2011   #
#                                                                             #
# Revisions:                                                                  #
# Date               Author                        Description                #
#                                                                             #
# Remarks:                                                                    #
#                                                                             #
###############################################################################
set -x
# Start of estofs_adcprep.sh script ----------------------------------------- #
# 1.  Set Times, and run types

  time=$1
  run_type=$2

# --------------------------------------------------------------------------- #
# 2.  Execute adcprep 
# 2.a Prepare adcpprep variables 

  rm -f adcprep.ctl adcprep.log 
  if [ -d PE0000 ]
  then
    rm -rf PE*
  fi

  ncpu=$3
  prepspec3=3
  grid_file=${RUN}.${modID}.fort.14
  input_file=${time}.${modID}.${run_type}.fort.15
  surface_file=${time}.${modID}.${run_type}.fort.22

# 2.b Execute adcpprep

  if [ $run_type = spin ]
  then
    rm -f adcprep.ctl
    echo $ncpu >> adcprep.ctl
    echo $prepspec3 >> adcprep.ctl
    echo $grid_file >> adcprep.ctl
    echo $input_file >> adcprep.ctl

    export pgm="estofs_adcprep"
    prep_step
    startmsg
    $EXECestofs/estofs_adcprep < adcprep.ctl >> adcprep.log 2>errfile
    export err=$?; err_chk
  else
    rm -f adcprep.ctl
    echo $ncpu >> adcprep.ctl
    echo $prepspec3 >> adcprep.ctl
    echo $grid_file >> adcprep.ctl
    echo $input_file >> adcprep.ctl
    echo $surface_file >> adcprep.ctl
    
    export pgm="estofs_adcprep"
    prep_step
    startmsg
    $EXECestofs/estofs_adcprep < adcprep.ctl >> adcprep.log 2>errfile
    export err=$?; err_chk
  fi

# End of estofs_adcprep.sh script ------------------------------------------- #
