#!/usr/bin/ksh

set +x
########################################################################
echo "----------------------------------------------------------------"
echo "exgfdl_pre_wake.sh.sms - Script To blend ocean wake into hour0 file."
echo "----------------------------------------------------------------"
echo "History: MAR 2003 - First implementation of this new script."
########################################################################
set -x

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

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"

   exit
fi

if test -f ${COMIN}/${stormlabel}.holdvars.txt
then
   . ${COMIN}/${stormlabel}.holdvars.txt
else
   msg="Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export pgm=exgfdl_pre_wake.sh.sms
   export err=911;err_chk
fi

########################################################################
# Copy in the hour0 file from COMIN.  This file has an LGI of only 20.
# Check the size of this file to make sure it's reasonable before 
# moving on in the script.
########################################################################
par=16000000
if test $nestno = 2
then
   par=12000000
fi

cp ${COMIN}/${ATCFNAME}.hour0.lgi20   .

if [ ${regcode} = 'eastpac' ]; then
  cp ${COMIN}/eastpac_ocean_model_info.${ATCFNAME}.${PDY}${cyc} .
fi

length=`wc -c ${ATCFNAME}.hour0.lgi20 | awk '{print $1}'`
if test $length -lt $par
then
   echo " !!! ERROR: SIZE OF ${ATCFNAME}.hour0.lgi20 IS NOT VALID..."
#TM   exit 1
fi

###############################################################
# Now run a program that redefines the LGI (a sort of memory
# capacity) from 20 to 27.  This allows extra room for the
# ocean data, if we are running coupled.  If we are not
# running coupled, we still want to use the same executable,
# so change the LGI to 27 anyway.  Note that the fcst000 file
# (datahis) which comes out of the makebogus2 script has an
# LGI of 20.
###############################################################

export pgm=gfdl_redef2027
. prep_step

ln -s -f ${DATA}/$ATCFNAME.hour0.lgi20    fort.9
ln -s -f ${DATA}/$ATCFNAME.hour0.lgi27    fort.77

startmsg
$EXEChur/gfdl_redef2027 >>$pgmout 2>errfile
export err=$?;err_chk


#------------------------------------------------------
# Check to see if the rst.final file is in $COMIN for
# the current storm and PDY.  If it is, then that 
# means that the ocean pre-processing ran successfully
# and we will run coupled.  If it is NOT there, then
# something went wrong in the ocean pre-processing 
# and we will run uncoupled.

coupled_status_file=coupled_status.${ATCFNAME}.${PDY}${cyc}

if [ -s ${COMIN}/rst.final.${ATCFNAME}.${PDY}${cyc} ]
then
  set +x
  echo " "
  echo "+++ rst final file exists in $COMIN, so we can"
  echo "+++ run coupled, if requested...."
  set -x
  ocean_flag="SUCCESS"
else 
  set +x
  echo " "
  echo "!!! rst final file does NOT exist in $COMIN"
  echo "!!! We will run uncoupled...."
  echo " "
  set -x
  ocean_flag="FAIL"
fi

if [ ${ocean_flag} = 'SUCCESS' ]
then

  #------------------------------------------------#
  # Ocean pre-processing completed successfully,
  # now run the gfdl_wake program to "blend" the
  # ocean wake data (from the rst.final file) into
  # the hour0 file.
  #------------------------------------------------#

  rst_final=rst.final.${ATCFNAME}.${PDY}${cyc}
  cp ${COMIN}/${rst_final} .

  export pgm=gfdl_wake_${regcode}
  . prep_step
  startmsg

  ln -s -f ${DATA}/$ATCFNAME.hour0.lgi27    fort.15
  ln -s -f ${DATA}/${rst_final}             fort.16
  ln -s -f ${DATA}/hour0.blended            fort.51

  if [ ${regcode} = 'eastpac' ]; then
    ln -s -f ${DATA}/eastpac_ocean_model_info.${ATCFNAME}.${PDY}${cyc}  fort.66
  else
    ln -s -f ${FIXhur}/gfdl_ocean_topo_and_mask.eastatl_extn   fort.66
  fi

  ${EXEChur}/gfdl_wake_${regcode} >>$pgmout 2> errfile
  rcc=$?

  if [ $rcc -ne 0 ]
  then
    set +x
    echo " "
    echo "GFDL ERROR: wake program failed, rcc = $rcc.  Will run uncoupled...."
    echo " "
    set -x
    msg="GFDL ERROR: wake program failed, rcc = $rcc.  Will run uncoupled...."
    postmsg "$jlogfile" "$msg"
    msg="GFDL COUPLED STATUS: Not running coupled for ${ATCFNAME} ${PDY}${cyc}"
    postmsg "$jlogfile" "$msg"
    mv ${DATA}/$ATCFNAME.hour0.lgi27 ${ATCFNAME}.temp1.hour0
    echo 0 > ${coupled_status_file}
  else
    mv ${DATA}/hour0.blended ${ATCFNAME}.temp1.hour0
    echo 1 > ${coupled_status_file}
    msg="GFDL COUPLED STATUS: Running coupled for ${ATCFNAME} ${PDY}${cyc}"
    postmsg "$jlogfile" "$msg"
  fi
else

  # Something went wrong in ocean pre-processing, so we
  # will run uncoupled....

  set +x
  echo " "
  echo "!!! Ocean flag from the ocean_status file indicates that"
  echo "!!! either something went wrong with the ocean spinup,"
  echo "!!! or this run was not requested to be a coupled run,"
  echo "!!! in either case we will run uncoupled...."
  echo " "
  set -x
  mv ${DATA}/$ATCFNAME.hour0.lgi27 ${ATCFNAME}.temp1.hour0
  echo 0 > ${coupled_status_file}
  msg="GFDL COUPLED STATUS: Not running coupled for ${ATCFNAME} ${PDY}${cyc}"
  postmsg "$jlogfile" "$msg"

fi
#


#------------------------------------------------
# Add in extra space for microphysics fields....

rm fort.9
rm fort.77

export pgm=gfdl_nwat2
. prep_step
startmsg

cp ${ATCFNAME}.temp1.hour0 ${COMOUT}/${ATCFNAME}.temp1.hour0.before_nwat2

ln -s -f ${ATCFNAME}.temp1.hour0    fort.9
ln -s -f ${ATCFNAME}.hour0.nwat     fort.77

$EXEChur/gfdl_nwat2
export err=$?;err_chk

mv ${ATCFNAME}.hour0.nwat ${COMOUT}/.
cp ${coupled_status_file} ${COMOUT}/.
