set -xa
#  November 2001 - Kitzmiller
 
#  Collect and reformat up-to-date RCM's from current
#  collection file in directory /dcom/us007003/yyyymmdd/wtxtbul/rcm*

#  This script is called from the main RCM-mosaic driver

#  Command-line argument: 00 or 30 characters to define
#   which RCMxx.SRVR file to create

#  Input file: /dcom/us007003/{yyyymmdd}/wtxtbul/rcm{hh}
#  Output file: RCM30.SRVR or RCM00.SRVR depending on command-line
#   argument

#  Create RCM00 or RCM30 files

rcmfile="rcm${jhr}"
mc_rcmfile="mc_rcm${jhr}"

  
if [ "$do_radsum" = 'no' ]
then
  if [ -f "$rcmfile" ]
  then
    cp "${DCOM}/${daystring}/wtxtbul/rcm${jhr}" $rcmfile
    cp "${DCOM}/${daystring}/wtxtbul/mc_rcm${jhr}"  $mc_rcmfile
    cat $mc_rcmfile >>  $rcmfile
    echo $rcmfile > extract_RCM.inp
    echo RCM00.SRVR >> extract_RCM.inp
    echo "${daystring}${jhr}" >> extract_RCM.inp
    echo "-1 31 55" >> extract_RCM.inp
    echo radar_list_OSO >> extract_RCM.inp
    igot=0
  else
    msg="ABNORMAL TERMINATION - rcm$jhr not available from OSO" 
    postmsg  "$jlogfile" "$msg"
    export err=1;err_chk
  fi
fi

if [ "$do_radsum" = 'yes' ]
then
  echo "Creating RCM30.SRVR..."
  day=`echo $daystring | cut -c7-8`
  itimes=0
  igot=1
  while [ "$igot" -eq 1 ]
  do
    cp "${DCOM}/${daystring}/wtxtbul/rcm${jhr}" "$rcmfile"
    cp "${DCOM}/${daystring}/wtxtbul/mc_rcm${jhr}"  "$mc_rcmfile"
    strings "${DCOM}/${daystring}/wtxtbul/mc_rcm${jhr}" > mc_rcm
    if [ -f "${rcmfile}"  -o   -f "${mc_rcmfile}" ]
    then
      num_rmcs=`grep "SDUS.. .... ${day}${jhr}[0-2]" $rcmfile | wc -l`
      num_mc_rmcs=`grep "SDUS.. .... ${day}${jhr}[0-2]" mc_rcm | wc -l`
      msg1="${num_rmcs} rcms from TOC available for processing this hour"
      msg2="${num_mc_rmcs} mc_rcms from multicast available for processing this hour"
      postmsg  "$jlogfile" "$msg1"
      postmsg  "$jlogfile" "$msg2"
      cat $mc_rcmfile >>  $rcmfile
      echo $rcmfile > extract_RCM.inp
      echo RCM30.SRVR >> extract_RCM.inp
      echo "${daystring}${jhr}" >> extract_RCM.inp
      echo "00 01 25" >> extract_RCM.inp
      echo radar_list_OSO >> extract_RCM.inp
      if [ "$num_rmcs" -gt 110  -o  $num_mc_rmcs -gt 110 ]
      then
        igot=0
      else
        msg="Insufficient number of Radar Coded Msgs available; try again in 3 mins "
        postmsg  "$jlogfile" "$msg"
        sleep 180
        itimes=`expr $itimes + 1 `
        if [ "$itimes" -gt 6 ]
        then
          msg="ABNORMAL TERMINATION - Insufficient Number of RCMS available"
          postmsg  "$jlogfile" "$msg"
          export err=1;err_chk
        fi
      fi
    else
      msg="rcms not available yet for hour $jhr. Job will try again in 3 mins"
      postmsg  "$jlogfile" "$msg"
      sleep 180
      itimes=`expr $itimes + 1 `
      if [ "$itimes" -gt 6 ]
      then
        msg="ABNORMAL TERMINATION - rcm$jhr not available from OSO"
        postmsg  "$jlogfile" "$msg"
        export err=1;err_chk
      fi
    fi
  done
fi

#  Run program to extract and format RCM's within the
#  needed time range.  Timeliness based on time of reflectivity
#  portion (part A) of RCM, not WMO header
cp $PARMradar/radar_list_OSO .

export pgm=radar_extract_rcm
. prep_step

$EXECradar/radar_extract_rcm < extract_RCM.inp >> $pgmout 2>>errout
err=$? ; export err; err_chk
echo after extract_rcm
exit
