#!/bin/sh
# --- 02/07/97 ---------- EXSURGE SCRIPT --------------------------
#
set +x
echo " ---------------------------------------------------------"
echo "  "
echo "                   STORM SURGE"
echo "  "
echo "  "
echo "                   JOB 677 "
echo "  "
echo "          ANALYSIS CYCLE TIME IS .. $CYCLE"
echo "  "
echo "  "
echo "  "
echo " ---------------------------------------------------------"
echo "        processing info for this execution      "
echo "Processing environment is .............. ${envir}"
echo "Temporary processing file directory is . $DATA"
echo "  "
echo "Executable SURGE directory is .......... $EXECmdl"
echo "Unix Control Language directory is ..... $PARMmdl"
echo "FIX-field directory is ................. $FIXmdl"
echo "  "
echo "Network id is .......................... $NET"
echo "Run id for com files is ................ $RUN"
echo "  "
echo "Unique machine processing id is ........ $pid"
echo "Temporary Output file is ............... $pgmout"
echo "YES SENDDBN means post messages ........ $SENDDBN"
echo "YES SENDCOM means save com files ....... $SENDCOM"
echo " "
echo " ---------------------------------------------------------"
echo " ---------------------------------------------------------"
echo "HISTORY: Oct 03, 1995 - implement Storm Surge"
echo "HISTORY: Feb 07, 1996 - added qsub to ftp the UCL files"
echo "         from the machine in which it ran to the other"
echo "         two Crays."
echo "         Linked the gribex executable in here file exapplywnd,"
echo "         to a more discriptive word to aid in monitoring"
echo "         We no longer save the old cylf10'${bsn}' file to the"
echo "         com files, it is saved only to $PARMmdl dir."
echo "         In here file output_hds, place directions to submit"
echo "         the file to the front end in case of an ftp failure"
echo "         In here file output_com, took off the .new from"
echo "         file ss.${CYCLE}.cylf10'${bsn}' Also, the file"
echo "         ss.gribi${CYL} has '${bsn}' appended on it."
echo "         In here file SENDBULLETIN, added a test for the ftp"
echo "         transfer of the jcl to the HDS."
echo "HISTORY: 03/19/96 - Major script changes."
echo "         01/28/97 - Script changes to use the Cray version"
echo "         of FORMBULL to generate the FQUS23, the FQAC40,"
echo "         and the FQGX40 messages which contain the extra-"
echo "         tropical storm surge forecasts for the Atlantic"
echo "         Coast, the Bering Sea, and the Gulf of Mexico,"
echo "         respectively"
echo "         02/07/97 - Removed as many references to HDS as possible"
echo "                    operator messages can still be disposed to HDS"
echo "         04/30/97 - Added qsub to kick off MDL owned job."
echo "         08/26/98 - Data is no longer stored in /nwprod/mdl/parm."
echo "                    Instead it will be in /com/gfs/prod/gfs.YYMMDD."
echo "         09/02/98 - Modified by Peter Henrichsen to use script"
echo "                    onebul.sh to make and send storm surge"
echo "                    bulletins."
echo "         03/08/00 - Modified by Steve Gilbert to run on IBM SP"
echo "         09/25/00 - Added West Coast and Arctic basins.       "
echo "         11/28/00 - Modified by Peter Henrichsen to replace"
echo "                    onebul.sh with perl script formbul.pl"
echo "         06/12/02 - Added dbnet alerts to get files to TGFTP"
echo "         09/01/01 - Model changes using all 4 cycles of GFS's"
echo "         analyses. Do without rotating the analysis files. J. Chen"
echo "         08/16/02 - Combined J.Chen's version with operational script"
echo " ---------------------------------------------------------"

set -x

######################################################################
#  Prepare data before running program to extract current and
#    forecast (future) global surface pressure, and U and V wind
#    vector fields.
######################################################################

msg="Begin job for $job"
postmsg "$jlogfile" "$msg"

export COMFILES=$COMIN/${RUN}.${cycle}

cp $COMFILES.pgrbif00  pgrbif00
cp $COMFILES.pgrbf00   pgrbf00
cp $COMFILES.pgrbif03  pgrbif03
cp $COMFILES.pgrbf03   pgrbf03
cp $COMFILES.pgrbif06  pgrbif06
cp $COMFILES.pgrbf06   pgrbf06
cp $COMFILES.pgrbif09  pgrbif09
cp $COMFILES.pgrbf09   pgrbf09
cp $COMFILES.pgrbif12  pgrbif12
cp $COMFILES.pgrbf12   pgrbf12
cp $COMFILES.pgrbif15  pgrbif15
cp $COMFILES.pgrbf15   pgrbf15
cp $COMFILES.pgrbif18  pgrbif18
cp $COMFILES.pgrbf18   pgrbf18
cp $COMFILES.pgrbif21  pgrbif21
cp $COMFILES.pgrbf21   pgrbf21
cp $COMFILES.pgrbif24  pgrbif24
cp $COMFILES.pgrbf24   pgrbf24
cp $COMFILES.pgrbif27  pgrbif27
cp $COMFILES.pgrbf27   pgrbf27
cp $COMFILES.pgrbif30  pgrbif30
cp $COMFILES.pgrbf30   pgrbf30
cp $COMFILES.pgrbif33  pgrbif33
cp $COMFILES.pgrbf33   pgrbf33
cp $COMFILES.pgrbif36  pgrbif36
cp $COMFILES.pgrbf36   pgrbf36
cp $COMFILES.pgrbif39  pgrbif39
cp $COMFILES.pgrbf39   pgrbf39
cp $COMFILES.pgrbif42  pgrbif42
cp $COMFILES.pgrbf42   pgrbf42
cp $COMFILES.pgrbif45  pgrbif45
cp $COMFILES.pgrbf45   pgrbf45
cp $COMFILES.pgrbif48  pgrbif48
cp $COMFILES.pgrbf48   pgrbf48

######################################################################
#  Run program to extract current and forecast (future) global
#    surface pressure, and U and V wind vector fields.
######################################################################

pgm="mdl_cy_puv10"
export pgm;. prep_step

export XLFUNIT_31=pgrbif00
export XLFUNIT_11=pgrbf00
export XLFUNIT_32=pgrbif03
export XLFUNIT_12=pgrbf03
export XLFUNIT_33=pgrbif06
export XLFUNIT_13=pgrbf06
export XLFUNIT_34=pgrbif09
export XLFUNIT_14=pgrbf09
export XLFUNIT_35=pgrbif12
export XLFUNIT_15=pgrbf12
export XLFUNIT_36=pgrbif15
export XLFUNIT_16=pgrbf15
export XLFUNIT_37=pgrbif18
export XLFUNIT_17=pgrbf18
export XLFUNIT_38=pgrbif21
export XLFUNIT_18=pgrbf21
export XLFUNIT_39=pgrbif24
export XLFUNIT_19=pgrbf24
export XLFUNIT_40=pgrbif27
export XLFUNIT_20=pgrbf27
export XLFUNIT_41=pgrbif30
export XLFUNIT_21=pgrbf30
export XLFUNIT_42=pgrbif33
export XLFUNIT_22=pgrbf33
export XLFUNIT_43=pgrbif36
export XLFUNIT_23=pgrbf36
export XLFUNIT_44=pgrbif39
export XLFUNIT_24=pgrbf39
export XLFUNIT_45=pgrbif42
export XLFUNIT_25=pgrbf42
export XLFUNIT_46=pgrbif45
export XLFUNIT_26=pgrbf45
export XLFUNIT_47=pgrbif48
export XLFUNIT_27=pgrbf48

export XLFUNIT_30=$PARMmdl/mdl_ft11.egawz
export XLFUNIT_51=gfspuv.${cyc}e
export XLFUNIT_52=gfspuv.${cyc}g
export XLFUNIT_53=gfspuv.${cyc}a
export XLFUNIT_54=gfspuv.${cyc}w
export XLFUNIT_55=gfspuv.${cyc}z
export XLFUNIT_96=sds.${cyc}
export XLFUNIT_81=gfsp.tmp
export XLFUNIT_82=gfsu.tmp
export XLFUNIT_83=gfsv.tmp

startmsg
$EXECmdl/mdl_cy_puv10 >>$pgmout 2>errfile
err=$?;export err; err_chk

######################################################################
#  Save extracted current and forecast (future) global surface
#    pressure, and U and V wind vector fields for each basin.
#  This is so that one can re-run the model in the future.
######################################################################

if test "$SENDCOM" = 'YES'
then
  cp gfspuv.${cyc}e $COMOUT
  cp gfspuv.${cyc}g $COMOUT
  cp gfspuv.${cyc}a $COMOUT
  cp gfspuv.${cyc}w $COMOUT
  cp gfspuv.${cyc}z $COMOUT
fi

######################################################################
#  Prepare data before running program to extract Past (historic)
#    global surface pressure, and U and V wind vector fields.
#  Needs 60 hours back...
#    (ie cycle 0Z day 0 needs back to cycle 12Z day -3)
#  Use PDYm1,PDYm2.... for -1 day, -2 day ...
#  The reason this is so long is to keep it simple.
######################################################################

CYCLE1='t00z'
CYCLE2='t06z'
CYCLE3='t12z'
CYCLE4='t18z'
export COMIN1=/com/gfs/prod/gfs.


# Files needed by all cycles (0,6,12,18)...
#     6Z of -2 day to 18Z of -1 day
cp $COMIN1$PDYm2/${RUN}.$CYCLE2.pgrbif00  pgrbif206
cp $COMIN1$PDYm2/${RUN}.$CYCLE2.pgrbf00   pgrbf206
cp $COMIN1$PDYm2/${RUN}.$CYCLE3.pgrbif00  pgrbif212
cp $COMIN1$PDYm2/${RUN}.$CYCLE3.pgrbf00   pgrbf212
cp $COMIN1$PDYm2/${RUN}.$CYCLE4.pgrbif00  pgrbif218
cp $COMIN1$PDYm2/${RUN}.$CYCLE4.pgrbf00   pgrbf218
cp $COMIN1$PDYm1/${RUN}.$CYCLE1.pgrbif00  pgrbif100
cp $COMIN1$PDYm1/${RUN}.$CYCLE1.pgrbf00   pgrbf100
cp $COMIN1$PDYm1/${RUN}.$CYCLE2.pgrbif00  pgrbif106
cp $COMIN1$PDYm1/${RUN}.$CYCLE2.pgrbf00   pgrbf106
cp $COMIN1$PDYm1/${RUN}.$CYCLE3.pgrbif00  pgrbif112
cp $COMIN1$PDYm1/${RUN}.$CYCLE3.pgrbf00   pgrbf112
cp $COMIN1$PDYm1/${RUN}.$CYCLE4.pgrbif00  pgrbif118
cp $COMIN1$PDYm1/${RUN}.$CYCLE4.pgrbf00   pgrbf118

#  Cycle 0 needs 12Z 18Z of -3 day and 0Z of -2 day
if test $cyc = "00"
then
  cp $COMIN1$PDYm3/${RUN}.$CYCLE3.pgrbif00  pgrbif312
  cp $COMIN1$PDYm3/${RUN}.$CYCLE3.pgrbf00   pgrbf312
  cp $COMIN1$PDYm3/${RUN}.$CYCLE4.pgrbif00  pgrbif318
  cp $COMIN1$PDYm3/${RUN}.$CYCLE4.pgrbf00   pgrbf318
  cp $COMIN1$PDYm2/${RUN}.$CYCLE1.pgrbif00  pgrbif200
  cp $COMIN1$PDYm2/${RUN}.$CYCLE1.pgrbf00   pgrbf200
fi

#  For 06Z cycle
if test $cyc = "06"
then
#  Cycle 6 needs 18Z of -3 day and 0Z of -2 day
  cp $COMIN1$PDYm3/${RUN}.$CYCLE4.pgrbif00  pgrbif318
  cp $COMIN1$PDYm3/${RUN}.$CYCLE4.pgrbf00   pgrbf318
  cp $COMIN1$PDYm2/${RUN}.$CYCLE1.pgrbif00  pgrbif200
  cp $COMIN1$PDYm2/${RUN}.$CYCLE1.pgrbf00   pgrbf200
#  Cycle 6 needs 0Z of current day
  cp $COMIN1$PDY/${RUN}.$CYCLE1.pgrbif00  pgrbif00
  cp $COMIN1$PDY/${RUN}.$CYCLE1.pgrbf00   pgrbf00
fi


#  For the 12Z cycle
if test $cyc = "12"
then
#  Cycle 12 needs 0Z of -2 day
  cp $COMIN1$PDYm2/${RUN}.$CYCLE1.pgrbif00  pgrbif200
  cp $COMIN1$PDYm2/${RUN}.$CYCLE1.pgrbf00   pgrbf200
#  Cycle 12 needs 0Z 6Z of current day
  cp $COMIN1$PDY/${RUN}.$CYCLE1.pgrbif00  pgrbif00
  cp $COMIN1$PDY/${RUN}.$CYCLE1.pgrbf00   pgrbf00
  cp $COMIN1$PDY/${RUN}.$CYCLE2.pgrbif00  pgrbif06
  cp $COMIN1$PDY/${RUN}.$CYCLE2.pgrbf00   pgrbf06
fi

#  Cycle 18 needs 0Z 6Z 12Z of current day
if test $cyc = "18"
then
  cp $COMIN1$PDY/${RUN}.$CYCLE1.pgrbif00  pgrbif00
  cp $COMIN1$PDY/${RUN}.$CYCLE1.pgrbf00   pgrbf00
  cp $COMIN1$PDY/${RUN}.$CYCLE2.pgrbif00  pgrbif06
  cp $COMIN1$PDY/${RUN}.$CYCLE2.pgrbf00   pgrbf06
  cp $COMIN1$PDY/${RUN}.$CYCLE3.pgrbif00  pgrbif12
  cp $COMIN1$PDY/${RUN}.$CYCLE3.pgrbf00   pgrbf12
fi

######################################################################
#  Run program to extract Past (historic) global surface pressure,
#    and U and V wind vector fields.
######################################################################

pgm="mdl_c10_gen"
export pgm;. prep_step

if test $cyc = "00"
then
  export XLFUNIT_31=pgrbif312
  export XLFUNIT_11=pgrbf312
  export XLFUNIT_32=pgrbif318
  export XLFUNIT_12=pgrbf318
  export XLFUNIT_33=pgrbif200
  export XLFUNIT_13=pgrbf200
  export XLFUNIT_34=pgrbif206
  export XLFUNIT_14=pgrbf206
  export XLFUNIT_35=pgrbif212
  export XLFUNIT_15=pgrbf212
  export XLFUNIT_36=pgrbif218
  export XLFUNIT_16=pgrbf218
  export XLFUNIT_37=pgrbif100
  export XLFUNIT_17=pgrbf100
  export XLFUNIT_38=pgrbif106
  export XLFUNIT_18=pgrbf106
  export XLFUNIT_39=pgrbif112
  export XLFUNIT_19=pgrbf112
  export XLFUNIT_40=pgrbif118
  export XLFUNIT_20=pgrbf118
fi

if test $cyc = "06"
then
  export XLFUNIT_31=pgrbif318
  export XLFUNIT_11=pgrbf318
  export XLFUNIT_32=pgrbif200
  export XLFUNIT_12=pgrbf200
  export XLFUNIT_33=pgrbif206
  export XLFUNIT_13=pgrbf206
  export XLFUNIT_34=pgrbif212
  export XLFUNIT_14=pgrbf212
  export XLFUNIT_35=pgrbif118
  export XLFUNIT_15=pgrbf118
  export XLFUNIT_36=pgrbif100
  export XLFUNIT_16=pgrbf100
  export XLFUNIT_37=pgrbif106
  export XLFUNIT_17=pgrbf106
  export XLFUNIT_38=pgrbif112
  export XLFUNIT_18=pgrbf112
  export XLFUNIT_39=pgrbif18
  export XLFUNIT_19=pgrbf18
  export XLFUNIT_40=pgrbif00
  export XLFUNIT_20=pgrbf00
fi

if test $cyc = "12"
then
  export XLFUNIT_31=pgrbif300
  export XLFUNIT_11=pgrbf300
  export XLFUNIT_32=pgrbif206
  export XLFUNIT_12=pgrbf206
  export XLFUNIT_33=pgrbif212
  export XLFUNIT_13=pgrbf212
  export XLFUNIT_34=pgrbif218
  export XLFUNIT_14=pgrbf218
  export XLFUNIT_35=pgrbif200
  export XLFUNIT_15=pgrbf200
  export XLFUNIT_36=pgrbif106
  export XLFUNIT_16=pgrbf106
  export XLFUNIT_37=pgrbif112
  export XLFUNIT_17=pgrbf112
  export XLFUNIT_38=pgrbif118
  export XLFUNIT_18=pgrbf118
  export XLFUNIT_39=pgrbif00
  export XLFUNIT_19=pgrbf00
  export XLFUNIT_40=pgrbif06
  export XLFUNIT_20=pgrbf06
fi

if test $cyc = "18"
then
  export XLFUNIT_31=pgrbif206
  export XLFUNIT_11=pgrbf206
  export XLFUNIT_32=pgrbif212
  export XLFUNIT_12=pgrbf212
  export XLFUNIT_33=pgrbif218
  export XLFUNIT_13=pgrbf218
  export XLFUNIT_34=pgrbif100
  export XLFUNIT_14=pgrbf100
  export XLFUNIT_35=pgrbif106
  export XLFUNIT_15=pgrbf106
  export XLFUNIT_36=pgrbif112
  export XLFUNIT_16=pgrbf112
  export XLFUNIT_37=pgrbif18
  export XLFUNIT_17=pgrbf18
  export XLFUNIT_38=pgrbif00
  export XLFUNIT_18=pgrbf00
  export XLFUNIT_39=pgrbif06
  export XLFUNIT_19=pgrbf06
  export XLFUNIT_40=pgrbif12
  export XLFUNIT_20=pgrbf12
fi

export XLFUNIT_30=$PARMmdl/mdl_ft11.egawz
export XLFUNIT_51=cylf10.${cyc}e
export XLFUNIT_52=cylf10.${cyc}g
export XLFUNIT_53=cylf10.${cyc}a
export XLFUNIT_54=cylf10.${cyc}w
export XLFUNIT_55=cylf10.${cyc}z
export XLFUNIT_96='sds.$cyc'
export XLFUNIT_81='gfsp.tmp'
export XLFUNIT_82='gfsu.tmp'
export XLFUNIT_83='gfsv.tmp'

startmsg
$EXECmdl/mdl_c10_gen >>$pgmout 2>errfile
  err=$?;export err; err_chk

######################################################################
# There are 5 basins for which forecasts are made.
#   1) e = East Coast Basin  (FQUS23 KWNO, MRP SSE)
#   2) g = Gulf Coast Basin  (FQGX23 KWNO, MRP SSG)
#   3) a = Alaska Basin      (FQAK23 KWNO, MRP SSB)
#   4) w = West Coast Basin  (FQPZ23 KWNO, MRP SSP)
#   5) z = Artic Basin       (FQAC23 KWNO, MRP SSA)
######################################################################

for bsn in e g a w z
do

  ####################################################################
  #  Run extratropical storm surge model.
  ####################################################################

  pgm="mdl_ext_6h"
  export pgm;. prep_step

  export XLFUNIT_96=sds.${cyc}
  export XLFUNIT_11=$PARMmdl/mdl_ft11.${bsn}
  export XLFUNIT_14=$PARMmdl/mdl_ettgp.${bsn}
  export XLFUNIT_19=$PARMmdl/mdl_etbsn.${bsn}
  export XLFUNIT_21=$PARMmdl/mdl_ft01.ega
  export XLFUNIT_25=$PARMmdl/mdl_etltlg.${bsn}
  export XLFUNIT_33=cylf10.${cyc}${bsn}
  export XLFUNIT_34=gfspuv.${cyc}${bsn}
  export XLFUNIT_81=fle10.tmp
  export XLFUNIT_82=fle20.tmp
  export XLFUNIT_83=fle30.tmp
  export XLFUNIT_52=sshistory.${cyc}${bsn}

  startmsg
  $EXECmdl/mdl_ext_6h >>$pgmout 2>errfile
  err=$?;export err; err_chk

  ####################################################################
  #  Run program to extract storm surge output.
  ####################################################################

  pgm="mdl_mdlsurge"
  export pgm;. prep_step

  export XLFUNIT_11=$PARMmdl/mdl_ft11.${bsn}
  export XLFUNIT_16=gfspuv.${cyc}${bsn}
  export XLFUNIT_17=sshistory.${cyc}${bsn}
  export XLFUNIT_58=mdlsurge.${cyc}${bsn}
  export XLFUNIT_96=sds.${cyc}

  startmsg
  $EXECmdl/mdl_mdlsurge >>$pgmout 2>errfile
  err=$?;export err; err_chk

  ####################################################################
  #  Save model output files (and some input files),  so that one can
  #    re-run the model in the future.
  ####################################################################

  if test "$SENDCOM" = 'YES'
  then
    cp mdlsurge.${cyc}${bsn} $COMOUT/mdlsurge.${cyc}${bsn}
    cp sshistory.${cyc}${bsn} $COMOUT/sshistory.${cyc}${bsn}
    cp $DATA/sds.${cyc} $COMOUT/sds.${cyc}
    cp $DATA/cylf10.${cyc}${bsn} $COMOUT/cylf10.${cyc}${bsn}
  fi

  ####################################################################
  #  Send test messages to NWS FTP server.
  ####################################################################

  if test "$SENDDBN" = 'YES'
  then
   $DBNROOT/bin/dbn_alert MDLFCST ETSSTXT $job $COMOUT/mdlsurge.${cyc}${bsn}
  fi

  #
  # create the extratropical storm surge bulletins by using NCEP's
  # formbul.pl software to make and send the bulletins.
  #

  case $bsn in

    e)
      name="fqus23"
      $USHutil/formbul.pl  -d $name -f mdlsurge.${cyc}${bsn} \
      -j $job -m $model  -p $pcom -s $SENDDBN -o ${name}.tran.$job ;;
    g)
      name="fqgx23"
      $USHutil/formbul.pl  -d $name -f mdlsurge.${cyc}${bsn} \
      -j $job -m $model  -p $pcom -s $SENDDBN -o ${name}.tran.$job ;;
    a)
      name="fqak23"
      $USHutil/formbul.pl  -d $name -f mdlsurge.${cyc}${bsn} \
      -j $job -m $model  -p $pcom -s $SENDDBN -o ${name}.tran.$job ;;
    w)
      name="fqpz23"
      $USHutil/formbul.pl  -d $name -f mdlsurge.${cyc}${bsn} \
      -j $job -m $model  -p $pcom -s $SENDDBN -o ${name}.tran.$job ;;
    z)
      name="fqac23"
      $USHutil/formbul.pl  -d $name -f mdlsurge.${cyc}${bsn} \
      -j $job -m $model  -p $pcom -s $SENDDBN -o ${name}.tran.$job ;;
    *)
      continue ;;
  esac


done      #  Basin loop

#####################################################################
# GOOD RUN
set +x
echo "**************JOB 677 COMPLETED NORMALLY"
echo "**************JOB 677 COMPLETED NORMALLY"
echo "**************JOB 677 COMPLETED NORMALLY"
set -x
#####################################################################

cat $pgmout

msg="HAS COMPLETED NORMALLY!"
echo $msg
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################
