#!/bin/ksh
######################################################
echo "-----------------------------------------------" 
echo "     exhwrf_ocean_init.sh.sms                  "
echo "   Hurricane model control script              "
echo "-----------------------------------------------" 
######################################################
set -x

PS4=' + exhwrf_ocean_init.sh.sms line $LINENO: ' 

#-----------------------------------------------------
# This script will run the ocean initialization script
# hwrf_ocean_init.sh if a coupled model has been requested.
# 
# Be sure that there is a holdvars.txt file, and source 
# it because it contains needed environmental variables 
# from the JHWRF_PRE_MASTER job that need to be passed 
# into this script
#----------------------------------------------------- 
sh ${utilscript}/setup.sh
export pgm=exhwrf_ocean_init.sh.sms
. prep_step
startmsg

if test -f ${COMOUT}/${stormlabel}.holdvars.txt
then
   . ${COMOUT}/${stormlabel}.holdvars.txt
else
   set +x
   echo " "
   echo "!!! ERROR: holdvars.txt file missing"
   echo " "
   set -x
   msg="Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export err=911;err_chk
   err_exit "Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
fi
#-------------------------------------------------------

# remove any ocean_status file; it will be created if 
# hwrf_ocean_init.sh is called 
if [ -f ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH} ]
then
   /bin/rm -f ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH} ]
fi  

echo 'MODEL = ' ${MODEL}
if [[ ${MODEL} = 'COUPLED' ]]
then
   echo 'COUPLED model requested; will submit hwrf_ocean_init.sh'  

   if [[ ${OCEAN} = 'POM' ]]; then

      ${USHhwrf}/hwrf_ocean_init.sh  ${STORM} ${YYYY} ${MM} ${DD} ${CYC} ${STORMID}   \
          "$SPINUP_DATA/$YYYY$MM$DD$CYC" "$STORM_TCVITALS"

   fi

   if [[ ${OCEAN} = 'HYCOM' ]]; then

      #A.1 Acquire environment variables from storm holdvars.txt
      export DATA=$CSTREAM/init
      mkdir -p $DATA
      cd $DATA
      # this should directory needs to be clean
      rm -rf *
      $utilscript/setup.sh

      ${USHhwrf}/hwrf_rtofs_select_domain.sh  ${STORM} ${YYYY} ${MM} ${DD} ${CYC} ${STORMID}

      cp ./select_domain/ocean_status ocean_status

      #A.2.1  create ocean_status
       . ocean_status
      if [ $Application = 'NONE' ]
      then
        echo 'Do not use a coupled run '
        if [ -f ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH} ]
          then
          /bin/rm -f ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH} ]
        fi
        echo 'set flag for an atmosphere model only run'
        echo 'export RUN_COUPLED=NO'  > ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}
        echo 'export OCEAN_STATUS=-2'  >> ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}
      else

        #A.2.2 finish ocean.status
        # collect information for application
        ${USHhwrf}/hwrf_rtofs_read_tables.sh
        if [ -f read_tables/domain_parameters ]
        then
           cat read_tables/domain_parameters >> ocean_status
           cp ocean_status ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}
 
           ${USHhwrf}/hwrf_rtofs_ocean_init.sh  ${STORM} ${YYYY} ${MM} ${DD} ${CYC} ${STORMID}   
           #B. Construct boundary and initial conditions

           # construct initial and boundary conditions
           # from either - previous simulation, or
           #             - rtofs.
           #
 
           . ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}
           ${USHhwrf}/hwrf_rtofs_init.sh  ${STORM} ${YYYY} ${MM} ${DD} ${CYC} ${STORMID}
        else
#
           echo 'COUPLED model will not be run; information for ' $Application ' is missing.'

           if [ -f ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH} ]
           then
              /bin/rm -f ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH} ]
           fi
           echo 'set flag for an atmosphere model only run'
           echo 'export RUN_COUPLED=NO'  > ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}
           echo 'export OCEAN_STATUS=-1'  >> ${COMOUT}/ocean_status.${STORM}${STORMID}.${YYYYMMDDHH}
        fi
     fi
  fi
else
  echo 'COUPLED model not requested; will NOT submit hwrf_ocean_init.sh'  
  if [[ "$PARAFLAG" == NO ]] ; then
  # deal with sms couple forecast model
  $SMSBIN/setev run_noncouple
  rm cdp_cmds
  SMSPASS=FREE
  SMSNAME=/${envir}${cyc}/hwrf${cyc}/hwrf${storm_num}/fcst/jhwrf${storm_num}_couple_forecast_${cyc}
  echo "login -t 60 $SMSNODE $SMSNAME $SMSPASS" > cdp_cmds
  echo "force -r complete $SMSNAME" >> cdp_cmds
  echo "exit" >> cdp_cmds
  $SMSBIN/cdp < cdp_cmds
  fi
fi

# end of script 
