#!/usr/bin/ksh

###################################################
# Run GFDL Pre Ocean script
###################################################
PS4='+ exgfdl_pre_ocean.sh.sms line $LINENO: '

set -xa

label="cat $COMOUT/$stormlabel.$cycle"

if test -f "$label" = "No $stormlabel"
then
   ##############################################
   # Condition if no storms are to be run
   ##############################################
   msg="No hurricane to run."
   postmsg "$jlogfile" "$msg"
   export err=911;err_chk
fi

setvarfile=${COMIN}/${stormlabel}.holdvars.txt
if test -f ${setvarfile}
then
   . ${setvarfile}
else
   msg="GFDL ERROR in pre_ocean: Hurricane to run but no ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export pgm=exgfdl_pre_ocean.sh.sms
   export err=911;err_chk
fi

if [ ${PARAFLAG} = 'YES' ]
then
  export DATA=${DATA}/oceanpre
fi

if [ ${COUPLED} = 'YES' ]
then
  set +x
  echo "################################################################## "
  echo "+++ Input COUPLED flag is YES.  Running ocean pre-processing...."
  echo "################################################################## "
  set -x
else
  set +x
  echo "################################################################## "
  echo "!!! NOT RUNNING COUPLED: INPUT COUPLED FLAG NOT SET TO YES"
  echo "################################################################## "
  set -x
  msg="GFDL COUPLED STATUS: Not running coupled for ${ATCFNAME} ${PDY}${cyc}."
  postmsg "$jlogfile" "$msg"
  if [ ${PARAFLAG} = 'YES' ]
  then
    echo 0 >${COMIN}/ocean_status.${ATCFNAME}.${PDY}${cyc}
  fi
  exit
fi

cd $DATA

###################################################################
# Bring in the current message file from NHC
###################################################################
cp $mesagdir/message$storm_num stormf

testrc=`wc -l stormf | cut -c1-8`

if test $testrc -eq 0
then
   set +x
   echo " "
   echo "################################################################## "
   echo "!!! ERROR in exgfdl_pre_ocean.sh.sms: The storm name is not found in"
   echo "!!!       the message file.  Something is wrong with the vitals file; "
   echo "!!!       it is probably missing...."
   echo "################################################################## "
   set -x
   msg="GFDL ERROR: Problem with vitals file in exgfdl_pre_ocean.sh.sms"
   postmsg "$jlogfile" "$msg"
   msg="GFDL COUPLED STATUS: Not running coupled for ${ATCFNAME} ${PDY}${cyc}."
   postmsg "$jlogfile" "$msg"
   if [ ${PARAFLAG} = 'YES' ]
   then
     echo 0 >${COMIN}/ocean_status.${ATCFNAME}.${PDY}${cyc}
   fi
   exit 
fi

if [ ${PARAFLAG} = 'YES' ]
then
  if [ ${USE_MY_WAKE_VITALS} = 'YES' ]
  then
    export vitarch=${WAKE_VITALS_FILE}
  else
    if [ ${YYYY} -ge 2003 ]
    then
      export vitarch=/com/arch/prod/syndat/syndat_tcvitals.${YYYY}
    else
      export vitarch=/global/save/wx20tm/syndat/syndat_tcvitals.${YYYY}
    fi
  fi
else

  export vitarch=/com/arch/${envir}/syndat/syndat_tcvitals.${YYYY}
fi

###################################################
# Get the hurricane input data
###################################################
$EXhur/gfdl_pre_ocean_trackdata.sh

set +x
echo " "
echo "Back in exgfdl_pre_ocean.sh.sms, pwd = `pwd`"
echo " "
echo "listing of shortstats follows:"
echo " "
set -x

ls -la *short*

set +x
echo " "
echo "cat of shortstats follows: "
echo " "
set -x

cat shortstats

set +x
echo " "
set -x

###################################################
# Determine ocean region.  Run an executable to see 
# which region the hurricane track occupies.  The 
# "shortstats" file is created in the gfdl_pre_trackdata.sh
# script.  This whole thing is done because, as of the
# 2003 and 2004 seasons, we have 2 different ocean 
# integration domains in the Atlantic basin:  1 that covers
# the western Atlantic and Gulf of Mexico, and the other
# that covers the eastern Atlantic.
#
# 2004 UPDATE: We now have an eastern Pacific ocean model.
#      There is only 1 domain for the EastPac, so as long 
#      as we know it's an EastPac storm, don't bother with
#      this "find_region" program.
###################################################
BASIN=`echo $ATCFNAME | cut -c1-2`

if [ ${BASIN} = 'EP' -o ${BASIN} = 'CP' ]; then

  export region=east_pacific
  export regcode=eastpac

else

  export pgm=gfdl_find_region
  . prep_step
  startmsg

  ln -s -f ${DATA}/ocean_region_info.txt  fort.61

  $EXEChur/gfdl_find_region <shortstats >$pgmout
  rcc=$?

  if [ $rcc -ne 0 ]
  then
    set +x
    echo "############################################"
    echo "ERROR: Error = $rcc running gfdl_find_region"
    echo "       We will run uncoupled."
    echo " "
    echo "    Listing of $DATA follows: "
    echo "############################################ "
    set -x
    ls -la $DATA
    msg="GFDL ERROR running gfdl_find_region. "
    postmsg "$jlogfile" "$msg"
    msg="GFDL COUPLED STATUS: Not running coupled for $ATCFNAME ${PDY}${cyc}"
    postmsg "$jlogfile" "$msg"
    if [ ${PARAFLAG} = 'YES' ]
    then
      echo 0 >${COMIN}/ocean_status.${ATCFNAME}.${PDY}${cyc}
    fi
    exit
  fi

  set +x
  echo " "
  echo "After gfdl_find_region in exgfdl_pre_ocean.sh.sms, pwd = `pwd`"
  echo " "
  echo "listing of ocean_region_info.txt follows:"
  echo " "
  set -x

  ls -la ocean_region_info.txt

  set +x
  echo " "
  echo "cat of ocean_region_info.txt follows: "
  echo " "
  set -x

  cat ocean_region_info.txt

  set +x
  echo " "
  set -x

  if [ ! -s ${DATA}/ocean_region_info.txt ]
  then
    set +x
    echo "ERROR: Could not determine the ocean region..."
    set -x
    msg="ERROR in exgfdl_pre_ocean.sh.sms, could not get ocean region" 
    postmsg "$jlogfile" "$msg"
    msg="GFDL COUPLED STATUS: Not running coupled for $ATCFNAME ${PDY}${cyc}"
    postmsg "$jlogfile" "$msg"
    if [ ${PARAFLAG} = 'YES' ]
    then
      echo 0 >${COMIN}/ocean_status.${ATCFNAME}.${PDY}${cyc}
    fi
    exit
  fi

  export region=`tail -1 ${DATA}/ocean_region_info.txt | awk '{print $1}'`
  tail -2 ${DATA}/ocean_region_info.txt | head -1

  case $region in
    east_atlantic) export regcode=eastatl;;
    west_united)   export regcode=united;;
  esac

fi

echo "export region=$region"     >>${setvarfile}
echo "export regcode=$regcode"   >>${setvarfile}

###################################################
# If this is a parallel run, check to see if 
# the rst3 ocean file has already been created.
# If it has not, then call the script that will
# create it....
###################################################
if [ ${PARAFLAG} = 'YES' ]
then
  if [ ${ALREADY_HAVE_PHASE3} = 'NO' ]
  then
    set +x
    echo " "
    echo "  Running the phase3 ocean spinup since the "
    echo "  flag indicates that the phase3 spinup has "
    echo "  not been done yet."
    echo "  TIMING: before phase3 spinup = `date`"
    echo " "
    set -x
    $EXhur/exgfdl_ocean_spinup.sh.sms

    if [ ${regcode} = 'eastpac' ]; then
      $EXhur/gfdl_pre_ocean_eastpac_phase3.sh
    fi

    set +x
    echo " "
    echo "  TIMING: after  phase3 spinup = `date`"
    echo " "
    set -x
  fi
else
###################################################
# Note that for the eastpac region, the phase3 script 
# runs in the pre_ocean job and NOT in the ocean_spinup
# job.  This is because, for the eastpac phase3, we need 
# storm-specific information, and that storm-specific 
# information is not available in the ocean-spinup job,
# which runs operationally only once per day at 00z.
###################################################
  if [ ${regcode} = 'eastpac' ]; then
      $EXhur/gfdl_pre_ocean_eastpac_phase3.sh
  fi

fi    

###################################################
# Note that ocean_phase3 is run once per day, 
# around 00z, to spin up the ocean.  Now we 
# need to run ocean_phase4 in order to use 
# the track data to include a wake in the sst 
# data.  If ocean_phase4 returns with a non-zero
# return code, then it means that something has
# happened which prevents us from using either 
# the output of phase3 or phase4, and so we can't
# run coupled.  But we still need to run a forecast,
# so just exit quietly here so as not to bring the
# whole gfdl system down for this storm (A status
# file has been updated in the phase4 script to 
# indicate that we will run uncoupled).
###################################################
$EXhur/gfdl_pre_ocean_phase4.sh

set +x
echo " "
echo "+++ Ocean pre-processing finished at `date`"
echo " "
set -x
