#!/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 "    Zhu     - 4/25/05 - Changes for the GFS T382 upgrades"
echo "                        used for MRF, GDAS and GFS_ENS only"
echo "    Cooke   - 6/06/05 - Removal of MRF alerts"
echo "    Cooke   - 1/30/07 - Turned MRF alerts back on"
echo "-----------------------------------------------------"
#####################################################################
set -xa
 
export CNVGRIB=/nwprod/util/exec/cnvgrib
export WGRIB2=/nwprod/util/exec/wgrib2

if test "$SAVEGES" = "YES" -a $fhr -le 15 -a `expr $fhr % 3` -eq 0
then
  cp $COMOUT/${RUN}.t${cyc}z.sf$fhr $GESdir/${RUN}.${cycle}.sf$fhr
  cp $COMOUT/${RUN}.t${cyc}z.bf$fhr $GESdir/${RUN}.${cycle}.bf$fhr

  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
   cp pgbfile $COMOUT/${RUN}.${cycle}.pgrbf$fhr
   cp pgifile $COMOUT/${RUN}.${cycle}.pgrbif$fhr
 
   ##########################
   # Convert to grib2 format
   ##########################
   if [ $RUN = drfmr -a $fhr -le 168 ]
   then
      $CNVGRIB -g12 -p40 pgbfile.2 pgbfile.2.grib2
      $WGRIB2 pgbfile.2.grib2 -s >pgbfile.2.grib2.idx
    
      cp pgbfile.2.grib2 $ENS_COM/${RUN}.${cycle}.pgrbf${fhr}.2.grib2
      cp pgbfile.2.grib2.idx $ENS_COM/${RUN}.${cycle}.pgrbf${fhr}.2.grib2.idx

      if [ $SENDDBN_GB2 = YES ]
      then
         $DBNROOT/bin/dbn_alert MODEL ENS_PGB_MRF_GB2 $job $ENS_COM/${RUN}.${cycle}.pgrbf${fhr}.2.grib2
         $DBNROOT/bin/dbn_alert MODEL ENS_PGB_MRF_GB2_WIDX $job $ENS_COM/${RUN}.${cycle}.pgrbf${fhr}.2.grib2.idx
      fi
   elif [ $RUN = gdas1 ]
   then
      $CNVGRIB -g12 -p40 pgbfile pgbfile.grib2
      $WGRIB2 pgbfile.grib2 -s >pgbfile.grib2.idx
      $CNVGRIB -g12 -p40 flxfile flxfile.grib2
      $WGRIB2 flxfile.grib2 -s >flxfile.grib2.idx
    
      cp pgbfile.grib2 $COMOUT/${RUN}.${cycle}.pgrbf${fhr}.grib2
      cp pgbfile.grib2.idx $COMOUT/${RUN}.${cycle}.pgrbf${fhr}.grib2.idx
      cp flxfile.grib2 $COMOUT/${RUN}.${cycle}.sfluxgrbf${fhr}.grib2
      cp flxfile.grib2.idx $COMOUT/${RUN}.${cycle}.sfluxgrbf${fhr}.grib2.idx
   fi

   #
   # Thin the output file directory for the GFS_ENS 
   # Keep only up to 90 hours of the sf, bf and sflux files for GFS_ENS
   # Keep only up to 84 hours of the bf files for other models
   #
   if [[ $RUN = ens?? ]]
   then
     if [[ $fhr -gt 90 ]]
     then
       rm $COMOUT/${RUN}.${cycle}.sf$fhr
       rm $COMOUT/${RUN}.${cycle}.bf$fhr
       #
       # Save COM flux files
       #
       rm $COMOUT/${RUN}.${cycle}.sfluxgrbf$fhr
       rm $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhr
       # 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
       rm $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhrm3
     fi
   else 
     if [[ $fhr -gt 84 && $fhr -ne 120 ]]
     then
       rm $COMOUT/${RUN}.${cycle}.bf$fhr
     fi
   fi
 
   #
   # Save lowres files if we are supposed to
   #
   if [[ $RUN = ens?? || $RUN = "gfs" ]]
   then
     if test "$DO_LOW_RES" = 'YES' -a `expr $fhr % 6` -eq 0
     then
       cp pgbfile.2 $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2
       cp pgifile.2 $ENS_COM/${RUN}.${cycle}.pgrbif$fhr.2
     fi
   else
     if test "$DO_LOW_RES" = 'YES' -a `expr $fhr % 12` -eq 0
     then
       cp pgbfile.2 $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2
       cp pgifile.2 $ENS_COM/${RUN}.${cycle}.pgrbif$fhr.2
     fi
   fi
  
fi

#
# DBNet Alerts for gfs suite
#
if test "$SENDDBN" = 'YES' -a  \( "$RUN" = 'gdas1' \) -a `expr $fhr % 3` -eq 0
then
  run=`echo $RUN | tr '[a-z]' '[A-Z]'`
  $DBNROOT/bin/dbn_alert MODEL ${run}_PGB $job $COMOUT/${RUN}.${cycle}.pgrbf$fhr
  $DBNROOT/bin/dbn_alert MODEL ${run}_PGBI $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr
  $DBNROOT/bin/dbn_alert MODEL ${run}_SGB $job $COMOUT/${RUN}.${cycle}.sfluxgrbf$fhr
  $DBNROOT/bin/dbn_alert MODEL ${run}_SGBI $job $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhr
  $DBNROOT/bin/dbn_alert MODEL ${run}_SF $job $COMOUT/${RUN}.${cycle}.sf$fhr
  $DBNROOT/bin/dbn_alert MODEL ${run}_BF $job $COMOUT/${RUN}.${cycle}.bf$fhr

  if [ $SENDDBN_GB2 = YES ]
  then
    $DBNROOT/bin/dbn_alert MODEL ${run}_PGB_GB2 $job $COMOUT/${RUN}.${cycle}.pgrbf${fhr}.grib2
    $DBNROOT/bin/dbn_alert MODEL ${run}_SGB_GB2 $job $COMOUT/${RUN}.${cycle}.sfluxgrbf${fhr}.grib2
    $DBNROOT/bin/dbn_alert MODEL ${run}_PGB_GB2_WIDX $job $COMOUT/${RUN}.${cycle}.pgrbf${fhr}.grib2.idx
    $DBNROOT/bin/dbn_alert MODEL ${run}_SGB_GB2_WIDX $job $COMOUT/${RUN}.${cycle}.sfluxgrbf${fhr}.grib2.idx
  fi
fi

if test "$SENDDBN" = 'YES' -a "$NET" = 'mrf' -a ` expr $cyc % 12 ` -eq 0
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
      $DBNROOT/bin/dbn_alert MODEL ENS_PGBI_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr
      $DBNROOT/bin/dbn_alert MODEL ENS_SF_$MEMBER $job $ENS_COM/${RUN}.${cycle}.sf$fhr
      $DBNROOT/bin/dbn_alert MODEL ENS_BF_$MEMBER $job $ENS_COM/${RUN}.${cycle}.bf$fhr
    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
      $DBNROOT/bin/dbn_alert MODEL ENS_PGBI2_$MEMBER $job $ENS_COM/${RUN}.${cycle}.pgrbif$fhr.2
    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
      $DBNROOT/bin/dbn_alert MODEL ENS_PGBI_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr
    fi

  fi

  if test "$RUN" = drfmr
  then
#    $DBNROOT/bin/dbn_alert MODEL MRF_SGB_MR $job $COMOUT/${RUN}.${cycle}.sfluxgrbf$fhr
#    $DBNROOT/bin/dbn_alert MODEL MRF_SGBI_MR $job $COMOUT/${RUN}.${cycle}.sfluxgrbif$fhr
#    $DBNROOT/bin/dbn_alert MODEL MRF_SF_MR $job $COMOUT/${RUN}.${cycle}.sf$fhr
    
    if [[ $fhr -lt 192 ]]
    then 
       $DBNROOT/bin/dbn_alert MODEL MRF_PGB_MR $job $COMOUT/${RUN}.${cycle}.pgrbf$fhr
#       $DBNROOT/bin/dbn_alert MODEL MRF_PGBI_MR $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr
   else
       $DBNROOT/bin/dbn_alert MODEL MRF_PGB2P5_MR $job $COMOUT/${RUN}.${cycle}.pgrbf$fhr
#       $DBNROOT/bin/dbn_alert MODEL MRF_PGBI2P5_MR $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr
    fi

#    if [[ $fhr -gt 0 && $fhr -le 84 ]]
#    then
#      $DBNROOT/bin/dbn_alert MODEL MRF_BF_MR $job $COM/${RUN}.${cycle}.bf$fhr
#    fi

    if test "$DO_LOW_RES" = 'YES' -a ` expr $fhr % 12 ` -eq 0
    then
      $DBNROOT/bin/dbn_alert MODEL ENS_PGB_MRF $job $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2
      $DBNROOT/bin/dbn_alert MODEL ENS_PGBI_MRF $job $ENS_COM/${RUN}.${cycle}.pgrbif$fhr.2
    fi
  fi
fi

exit 

