#!/bin/sh

#####################################################################
echo "-----------------------------------------------------"
echo " Script: global_transfer.sh" 
echo " "
echo " Purpose - Copy Global Posts to /nwges and /com"
echo "           Alert posted files to DBNet"
echo " "
echo " History - "
echo "    Facey   - 9/10/96 - Implemented on C90"
echo "    Michaud - 6/03/99 - Converted to IBM SP"
echo "    Wobus   - 8/25/03 - Changes for Ens 4cyc x 180hr "
echo "    Wobus   - 6/08/05 - Changes for 6-hour breeding "
echo "-----------------------------------------------------"
#####################################################################
set -xa
 
if test "$SAVEGES" = "YES" -a $fhr -le 15
then
  cp $COMOUT/${RUN}.t${cyc}z.sf$fhr$cfsuffix $GESdir/${RUN}.${cycle}.sf$fhr$cfsuffix
  cp $COMOUT/${RUN}.t${cyc}z.bf$fhr$cfsuffix $GESdir/${RUN}.${cycle}.bf$fhr$cfsuffix

  msg="Guess files for fcst hour $fhr copied to $GESdir"
  postmsg "$jlogfile" "$msg"
fi
 
if test "$SENDCOM" = "YES"
then
   #
   # Save Pressure and SFLUX GRIB/GRIB Index files
   #
   cp flxifile $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhr$cfsuffix
   cp pgbfile $COMOUT/${RUN}.${cycle}.pgrbf$fhr$cfsuffix
   cp pgifile $COMOUT/${RUN}.${cycle}.pgrbif$fhr$cfsuffix
 
   # Save Sig and sfc fcst files

   if [[ $RUN = ens?? ]]
   then
     if [[ $fhr -gt 90 ]]
     then
       if [[ $fhr -gt 126 ]]
       then
          rm $COMOUT/${RUN}.${cycle}.sf$fhr$cfsuffix
       fi
       rm $COMOUT/${RUN}.${cycle}.bf$fhr$cfsuffix
       #
       # Save COM flux files
       #
       rm $COMOUT/${RUN}.${cycle}.sfluxgrbf$fhr$cfsuffix
       rm $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhr$cfsuffix
       # remove the 3-hourly files as well
       fhrm3=$((fhr-3))
       if [ $fhrm3 -lt 10 ]; then fhrm3=0$fhrm3; fi
       rm $COMOUT/${RUN}.${cycle}.sfluxgrbf$fhrm3$cfsuffix
       rm $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhrm3
     else
       if [[ $fhr -le 90 && $cycsuffix = true ]]
       then
          rm $COMOUT/${RUN}.${cycle}.sfluxgrbf$fhr$cfsuffix
          rm $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhr$cfsuffix
          # remove the 3-hourly files as well
          fhrm3=$((fhr-3))
          if [ $fhrm3 -lt 10 ]; then fhrm3=0$fhrm3; fi
          rm $COMOUT/${RUN}.${cycle}.sfluxgrbf$fhrm3$cfsuffix
          rm $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhrm3$cfsuffix
       fi
     fi
   else 
     if [[ $fhr -gt 84 && $fhr -ne 120 ]]
     then
       rm $COMOUT/${RUN}.${cycle}.bf$fhr$cfsuffix
     fi
   fi
 
   # Save lowres files if we are supposed to
   #
   if test "$DO_LOW_RES" = 'YES' -a `expr $fhr % 6` -eq 0
   then
     cp pgbfile.2 $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2$cfsuffix
     cp pgifile.2 $ENS_COM/${RUN}.${cycle}.pgrbif$fhr.2$cfsuffix
   fi
fi

#
# DBNet Alerts for gfs ensemble
#

if test "$SENDDBN" = 'YES' -a "$NET" = 'mrf' -a ` expr $cyc % 12 ` -eq 0 -a "$cfsuffix" = ""
then
  if test `echo $RUN | cut -c1-3` = "ens"
  then

    MEMBER=`echo $RUN | cut -c4-5 | tr '[a-z]' '[A-Z]'`
    if [[ $fhr -ge 0 && $fhr -le 84 && ` expr $fhr % 6 ` -eq 0 ]]
    then
      $DBNROOT/bin/dbn_alert MODEL ENS_PGB_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbf$fhr$cfsuffix
      $DBNROOT/bin/dbn_alert MODEL ENS_PGBI_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr$cfsuffix
      $DBNROOT/bin/dbn_alert MODEL ENS_SF_$MEMBER $job $ENS_COM/${RUN}.${cycle}.sf$fhr$cfsuffix
      $DBNROOT/bin/dbn_alert MODEL ENS_BF_$MEMBER $job $ENS_COM/${RUN}.${cycle}.bf$fhr$cfsuffix
    fi
    if test "$DO_LOW_RES" = 'YES' -a ` expr $fhr % 12 ` -eq 0 -a $fhr -ge 96
    then
      $DBNROOT/bin/dbn_alert MODEL ENS_PGB2_$MEMBER $job $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2$cfsuffix
      $DBNROOT/bin/dbn_alert MODEL ENS_PGBI2_$MEMBER $job $ENS_COM/${RUN}.${cycle}.pgrbif$fhr.2$cfsuffix
    fi
    if [[ $fhr -ge 192 && ` expr $fhr % 12 ` -eq 0 ]]
    then
      $DBNROOT/bin/dbn_alert MODEL ENS_PGB_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbf$fhr$cfsuffix
      $DBNROOT/bin/dbn_alert MODEL ENS_PGBI_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr$cfsuffix
    fi

  fi

fi

exit 

