
sname=`basename $0`
echo `date` $sname $member begin
#####################################################################
echo "-----------------------------------------------------"
echo " exgefs_post_intcnv.sh.sms" 
echo " based on exglobal_post.sh.sms and exglobal_post_pgrb2.sh.sms" 
echo " interpolate master post files and convert to GRIB2"
echo " Sep 07 - Wobus - reorganized script"
echo "-----------------------------------------------------"
#####################################################################
export PS4='+ $SECONDS $sname $member $LINENO: '

set -xa
na=`basename $0`

cd $DATA

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

###testb
if [[ $envir != prod ]]; then
echo
echo files at beginning of pgrb1 job
pwd
ls -al
date
fi
###teste
export MP_LABELIO=YES
#poe hostname

if [[ "$cycsuffix" = "true" ]]; then
  export cfsuffix=".cycfs$cyc_fcst"
else
  export cfsuffix=""
fi
echo cyc=$cyc cyc_fcst=$cyc_fcst
echo cycle=$cycle cycle_fcst=$cycle_fcst
echo fcstlong=$fcstlong
echo cycsuffix=$cycsuffix
echo cfsuffix=$cfsuffix

WGRIB=/nwprod/util/exec/wgrib
echo GRBINDEX=$GRBINDEX
export GRBIDX=/nwprod/util/exec/grbindex

############################################################
# clean up missing markers from previous run
############################################################
cd $COMOUT/$cyc/misc
rc=$?
if (( rc == 0 )); then
  for file in $RUN.*.missing
  do
    if [[ -f $file ]]; then
      echo `date` removing $COMOUT/$cyc/misc/$file
      rm -f $COMOUT/$cyc/misc/$file
    fi
  done
fi
cd $DATA

############################################################
#  Define Variables:
#  -----------------
#  SHOUR        is the starting forecast hour. normally 0 except for restarts.
#  FHOUR        is the ending forecast hour.
#  FHINC        is the increment hour for each forecast steps.
#  FH           is the current forecast hour.
#  SLEEP_TIME   is the number of seconds to sleep before exiting with error.
#  SLEEP_INT    is the number of seconds to sleep between restrt file checks.
#  restart_file is the name of the file to key off of to kick off post.
############################################################

SLEEP_LOOP_MAX=`expr $SLEEP_TIME / $SLEEP_INT`

############################################################
# Post Analysis Files before starting the Forecast Post 
############################################################ 
# wait for master post to be finished
############################################################
export ffhr=anl
export fhr=00
export anlflag=yes

ic=1
while [ $ic -le $SLEEP_LOOP_MAX ]
do
   if [[ -f pgbaifile.master.$ffhr$cfsuffix ]] && \
      [[ -f pgbbifile.master.$ffhr$cfsuffix ]] && \
      [[ -f pgbdifile.master.$ffhr$cfsuffix ]]; then
      break
   else
      ic=`expr $ic + 1`
      sleep $SLEEP_INT
   fi
   ###############################
   # If we reach this point assume
   # fcst job never reached restart 
   # period and write file to
   # indicate missing data
   ###############################
   if [ $ic -eq $SLEEP_LOOP_MAX ]
   then
      date >$COMOUT/$cyc/misc/$RUN.$ffhr.missing
      #export err=9
      #err_chk
      echo `date` $sname $member end
      msg="$member" MISSING DATA
      postmsg "$jlogfile" "$msg"
      exit
   fi
done
####################################
# wait for token controller
####################################

set +x
nbw=900
iwaitb=1
echo `date` process and ush limiter begin
pid=$$
dtg=`date +%Y%m%d%H%M%S`
fff=$fhr
if (( fff < 100 )); then
  fff=0$fhr
fi
export token=token.$fff.$dtg.$member.$ffhr.$pid
date >$dirtokenqueue/$token
(( ibw = 0 ))
while (( ibw < nbw ))
do
  (( ibw = ibw + 1 ))
  if [[ -f $dirtokenrun/$token ]]; then
    break
  fi
  sleep $iwaitb
done
if [[ -f $dirtokenqueue/$token ]]; then
  mv $dirtokenqueue/$token $dirtokenrun/$token
fi
echo `date` process and ush limiter after ibw=$ibw
set -xa

####################################
# submit background process for analysis
####################################
echo `date` submit background ush before
(
export PS4='+ $SECONDS $sname $member $ffhr $LINENO: '
echo `date` run background ush begin


#################################### 
# control the inclusion of perturbation identifiers
# in the GRIB1 ensemble PDS header extension
####################################
export addgrb1id=yes
export makegrb1i=yes

if [[ $envir = prod ]]; then
   $USHGLOBAL/gefs_post_intcnv.sh
elif [[ $envir = para ]] || \
     [[ $envir = test ]]; then
   /nw$envir/ush/gefs_post_intcnv.sh
else
   $basesource/nw$envir/ush/gefs_post_intcnv.sh
fi
echo `date` run background ush end

####################################
# remove token
####################################
rm $dirtokenrun/$token
) > $member.$ffhr.out 2>&1 &
echo `date` submit background ush after

############################################################
# Initialize Loop Through the Post Forecast Files 
############################################################

(( fhr=SHOUR))

if test $fhr -lt 10
then
   export fhr="0$fhr"
fi

export anlflag=no

############################################################
# Loop Through the Post Forecast Files 
############################################################
while test $fhr -le $FHOUR
do
    set +x
    echo `date` f$fhr begin
    ffhr=f$fhr

    ###############################
    # Start Looping for the 
    # existence of the restart files
    ###############################
    set -xa
    export pgm="postcheck"
    ic=1

    while [ $ic -le $SLEEP_LOOP_MAX ]
    do
       if [[ -f pgbaifile.master.$ffhr$cfsuffix ]] && \
	  [[ -f pgbbifile.master.$ffhr$cfsuffix ]] && \
	  [[ -f pgbdifile.master.$ffhr$cfsuffix ]]; then
          break
       else
          ic=`expr $ic + 1`
          sleep $SLEEP_INT
       fi
       ###############################
       # If we reach this point assume
       # fcst job never reached restart 
       # period and error exit
       ###############################
       if [ $ic -eq $SLEEP_LOOP_MAX ]
       then
	  date >$COMOUT/$cyc/misc/$RUN.$ffhr.missing
          #export err=9
          #err_chk
	  echo `date` $sname $member end
	  msg="$member" MISSING DATA
	  postmsg "$jlogfile" "$msg"
	  exit
       fi
    done

    ####################################
    # wait for token controller
    ####################################
    set +x

    echo `date` process and ush limiter begin
    pid=$$
    dtg=`date +%Y%m%d%H%M%S`
    fff=$fhr
    if (( fff < 100 )); then
      fff=0$fhr
    fi
    export token=token.$fff.$dtg.$member.$ffhr.$pid
    date >$dirtokenqueue/$token
    (( ibw = 0 ))
    while (( ibw < nbw ))
    do
      (( ibw = ibw + 1 ))
      if [[ -f $dirtokenrun/$token ]]; then
	break
      fi
      sleep $iwaitb
    done
    if [[ -f $dirtokenqueue/$token ]]; then
      mv $dirtokenqueue/$token $dirtokenrun/$token
    fi
    echo `date` process and ush limiter after ibw=$ibw
    set -xa

   msg="Starting post for fhr=$fhr"
   postmsg "$jlogfile" "$msg"

  (
  export PS4='+ $SECONDS $sname $member $ffhr $LINENO: '
  echo `date` run background ush begin

  #################################### 
  # control the inclusion of perturbation identifiers
  # in the GRIB1 ensemble PDS header extension
  ####################################
  export addgrb1id=yes
  export makegrb1i=yes

  if [[ $envir = prod ]]; then
     $USHGLOBAL/gefs_post_intcnv.sh
  elif [[ $envir = para ]] || \
       [[ $envir = test ]]; then
     /nw$envir/ush/gefs_post_intcnv.sh
  else
     $basesource/nw$envir/ush/gefs_post_intcnv.sh
  fi
  echo `date` run background ush end

  ####################################
  # remove token
  ####################################
  rm $dirtokenrun/$token
  ) >$member.$ffhr.out 2>&1 &
  echo `date` submit background ush after
    
  export fhr=`expr $fhr + $FHINC`
  if test $fhr -lt 10
  then
     export fhr="0$fhr"
  fi
done
########################################################


echo `date` before wait within $member
wait
echo `date` after wait within $member

echo
echo files at end of pgrb1 job
pwd
ls -al
echo `date` $sname $member end
msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

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