set +x
##############################################################################
echo "---------------------------------------------------------------"
echo "hysplit_gfsdata.sh - Script To Convert GFS Grib Data into ARL Format.  "
echo "---------------------------------------------------------------"
echo "History:     1997 - Original Script Submitted by Roland Draxler"
echo "         FEB 1998 - First Implementation of this script   "
echo "                    by David Michaud"
echo "         JUN 1999 - converted to IBM-SP"
##############################################################################
set -x

cd $DATA

#############################################################
# Compute Time Window for GFS Repack Files
#############################################################
let DURHOURS=$run-24-$Icyc
let FCYC=$DURHOURS%24

case $IPDY in
  $PDY) DURSEQ='';;
  $PDYm1) DURSEQ=`$utilscript/finddate.sh $IPDY s+1`;;
  $PDYm2) DURSEQ=`$utilscript/finddate.sh $IPDY s+2`;;
  $PDYm3) DURSEQ=`$utilscript/finddate.sh $IPDY s+3`;;
  $PDYm4) DURSEQ=`$utilscript/finddate.sh $IPDY s+4`;;
  $PDYm5) DURSEQ=`$utilscript/finddate.sh $IPDY s+5`;;
  $PDYm6) DURSEQ=`$utilscript/finddate.sh $IPDY s+6`;;
  $PDYm7) DURSEQ=`$utilscript/finddate.sh $IPDY s+7`;;
esac
#DURSEQ="$PDYm1 $IPDY $DURSEQ"
DURSEQ="$IPDY $DURSEQ"

#############################################################
# Loop through the Sequence of Days Inside of the Time Window
# For the RSMC Run.  If you hit $PDY in the Loop then repack
# the forecasts from 00-72 hours.
#############################################################
flag=0

for FPDY in $DURSEQ
do
  if test $FPDY -ge $PDYm7
  then
    export GFS_DIR="/com/gfs/prod"
  else
    msg='GFS Datasets Out of Time Range: Not Available'
    postmsg "$jlogfile" "$msg"
    err=1; err_exit
  fi

  for LCYC in 00 06 12 18
  do
    #############################################################
    # If Day Equals or Exceeds Current Day Break Out of Loop
    #############################################################
    if test $FPDY$LCYC -ge $PDY$cyc
    then
      flag=1
      break
    else
    #############################################################
    # If Starting Day in Sequence is Less Than the Initial Day and 
    # Cycle repack the first three hours of forecast.
    #############################################################
      for FHR in 03 06
      do
        GFSFILE=$GFS_DIR/gfs.$FPDY/gfs.t${LCYC}z.pgrbf$FHR
        if test ! -f $GFSFILE
        then
          if test $LCYC -eq 6 -o $LCYC -eq 18
          then
            let LCYC=$LCYC-6
            let FHR=$FHR+6
            typeset -Z2 FHR
            GFSFILE=$GFS_DIR/gfs.$FPDY/gfs.t${LCYC}z.pgrbf$FHR
          fi
        fi

        pgm=hysplit_gfs2arlh
        export pgm;prep_step
        msg="hysplit_gfs2arlh for hour $FPDY${LCYC}F$FHR started"
        postmsg "$jlogfile" "$msg"
        $EXEChysplit/hysplit_gfs2arlh $GFSFILE $olat $olon >>$pgmout 2>errfile
        export err=$?;err_chk

        cat DATA.GFS >> GFSA
        #############################################################
        # copy each time to indicate end of forecast processed
        #############################################################
#        rm DATA.GFS
      done
    fi
  done

  if test $flag -eq 1
  then 
    break
  fi
done

if test $flag -eq 1
then
  #############################################################
  # Master Loop Through Potential directories for processing
  #############################################################

  list="00,03,06,09 12,15,18,21 24,27,30,33 36,39,42,45 48,51,54,57 60,63,66,69 72"
  for seq in $list
  do
    cnt=1
    for FHR in `echo $seq | awk -F, '{print $1" "$2" "$3" "$4}'`
    do
      export FHR
      if test $cnt -lt 4
      then
        $USHhysplit/hysplit_gfs2arl.sh > ${FHR}.out 2>&1 &
        let cnt=$cnt+1
      else
        $USHhysplit/hysplit_gfs2arl.sh > ${FHR}.out 2>&1
      fi
    done
#   Wait for all background jobs to finish.
    wait
    for FHR in `echo $seq | awk -F, '{print $1" "$2" "$3" "$4}'`
    do
      sed "s/^/$FHR.out : /g" $FHR.out
      cat $FHR/DATA.GFS >> GFSF
    done
  done
fi

if test $SENDCOM = 'YES'
then
  echo $IPDY$Icyc $PDY$cyc $COMOUT/GFSA.t${cyc}z.$site \
       $COMOUT/GFSF.t${cyc}z.$site $olat $olon > /nwges/$NET/$site.control
  cp GFSA $COMOUT/GFSA.t${cyc}z.$site
  cp GFSF $COMOUT/GFSF.t${cyc}z.$site
fi
