#! /bin/ksh
#
# Script name:         exhysplit_hiresw_prep.sh.sms
#

set -x

export PS4='$SECONDS + '
export tmmark=tm00

#######################################################
# Wait for all fcst hours to finish 
#######################################################
icnt=1
while [ $icnt -lt 1000 ]
do
  for fhr in $postjobs
  do
    if [ -s $FCST_DIR/postdone${fhr}.${tmmark} ]
    then
      HIRESWFILE=$HIRESW_DIR/${NEST}.t${cyc}z.awpreg${fhr}.tm00

      ######################################
      # Convert HIRESW Data to ARL Format
      ######################################
      export pgm=hysplit_hiresw4arl_hls
      . prep_step

      msg="hysplit_hiresw4arl_hls for hour $PDY${cyc}F$fhr started"
      postmsg "$jlogfile" "$msg"

      $EXEChysplit/hysplit_hiresw4arl_hls $HIRESWFILE >> $pgmout 2>errfile
      export err=$?;err_chk

      cat $DATA/DATA.HIRESW >> HIRESWF

      $SMSBIN/setev converted_fhr${fhr}

      ######################################
      # Remove current fhr from list
      ######################################
      postjobs=`echo $postjobs | sed s/${fhr}//g`
    else
       break
    fi
  done
  
  result_check=`echo $postjobs | wc -w`
  if [ $result_check -eq 0 ]
  then
     break
  fi

  sleep 10
  icnt=$((icnt + 1))
  if [ $icnt -ge 540 ]
  then
    msg="ABORTING after 1.5 hours of waiting for HIRESW FCST hours $postjobs."
    err_exit $msg
  fi

done

if test $SENDCOM = 'YES'
then
  cp HIRESWF $COMOUT/hysplit.t${cyc}z.hireswf.${NEST}
fi

echo Exiting $0

exit
