# --- 12/02/99 -------- EXSHF SCRIPT ---------------------------
#
#   Start of script to run the SHEF decoder in operational IBM SP environment
#   This script runs every 3 hours to extract hourly precip reports from
#   the Office of Hydrology's HADS system.   The tasks are:
#
#   1 - Set shell variables to the components of today's date. 
#   2 - Copy the products from the X25 line in today's directory that have
#       arrived since the last run to the decoder's input file.
#   3 - Decode the SHEF products, keeping only valid 1-hour precipitation 
#       reports.
#   4 - During the 1st run after the change to a new GMT day (7-10 PM ET),
#       save all of the original AWIPS products 
#   5 - Create BUFR messages from the modified, decoded SHEF records
#

    set -x

    cd $DATA

    msg="Begin job JISHF"
    postmsg "$jlogfile" "$msg"

 
#   Determine today's date information from NCEP setup script
#   PDY is the GMT day, cyc is the GMT hour
 
    dy=` date "+%h%d" `        # current day Mmmdd, eg Jan06

    if test $cyc -lt 3            
    then 
       dt1=$PDYm1                 # 0Z run, set date to previous GMT day
       export COMOUT=/com/${NET}/${envir}/${DATATYPE}.${PDYm1}
       export COMIN=/com/${NET}/${envir}/${DATATYPE}.${PDYm1}
    else
       dt1=$PDY                   # otherwise, use the current GMT date
       export COMOUT=/com/${NET}/${envir}/${DATATYPE}.${PDY}
       export COMIN=/com/${NET}/${envir}/${DATATYPE}.${PDY}
    fi                            

    mkdir -m 775 -p $COMOUT

    cp $COMIN/hads-status-log $DATA/hads-status-log
#  
#  SHEF DECODER
#

    pgm=ingest_shefdcod
    export pgm; prep_step

    msg="$pgm start"
    postmsg "$jlogfile" "$msg"
 
#   Get only SXUS72 products from today's directory and put in decoder's input
#   file

    if test -f $INDIR/$dt1/wtxtbul/shef_other   # today's AWIPS products
    then
      $USHshef/ingest_getsxus72.pl $INDIR/$dt1/wtxtbul/shef_other >$DATA/sxus72

#     fix bad time increment & hrly precip coding mistakes...6-oct-98

      sed 's:/IN60/:/DIN60/:g' < $DATA/sxus72 | \
      sed 's:/DIN6/:/DIN60/:g' | sed 's:/DIN0/:/DIN60/:g' | sed 's:=: :g' | \
      sed 's:/DN60/:/DIN60/:g' | sed 's:/PHRG/:/PPHRG/:g' | \
      sed 's:/PPRG/:/PPHRG/:g' >  $DATA/SHEFIN1    

      if test $cyc -lt 3                        # new GMT day, copy the whole
      then                                      # day's file to archive copy

        cp       $DATA/SHEFIN1    $DATA/SXUS72-$dy
        compress $DATA/SXUS72-$dy

        if test "$SENDCOM" = "YES"
        then
           cp $DATA/SXUS72-${dy}.Z $COMOUT/SXUS72-${dy}.Z
           if test "$SENDDBN" = "YES"
           then 
              $DBNROOT/bin/dbn_alert DATA HRLY_PRCP $job $COMOUT/SXUS72-$dy.Z
           fi
        fi
      fi

    else
      date "+No file $dt1/wtxtbul/shef_hyd:  %c" >> $DATA/hads-status-log
      msg="No file $INDIR/$dt1/wtxtbul/shef_hyd - ABORTING"
      postmsg "$jlogfile" "$msg"
      err_exit
    fi 

#   Copy products to working directory
    
    cp $PARMshef/ingest_shefparm $DATA/SHEFPARM

#   File assignments within the main pgm of the shef-decoder ...
#   All file names are in uppercase letters and begin with SHEF
     
#   INPUT FILES:
#      fort.11 - Input file with AWIPS bulletins            SHEFIN1 
#      fort.12 - Input file with parameter values           SHEFPARM
#
#   OUTPUT FILES: 
#      fort.52 - Output file with decoded records           SHEFOUT1  
#      fort.53 - Output file with echo of input 
#                and all warning or error messages          SHEFERR1

    $EXECshef/ingest_shefdcod
    err=$?;export err; err_chk

    if test "$SENDCOM" = "YES"
    then
       cp $DATA/SHEFIN1  $COMOUT/SHEFIN1
       cp $DATA/SHEFOUT1 $COMOUT/SHEFOUT1
       cp $DATA/SHEFERR1 $COMOUT/SHEFERR1
       if test "$SENDDBN" = "YES" 
       then
           $DBNROOT/bin/dbn_alert DATA SHEFOUT1 $job $COMOUT/SHEFOUT1
       fi
    fi



#   extract precip reports (only 1 hr type) from output of decoder
#   remove bad reports, **** or values > 5 in amount field .. 20-apr-2000
#   remove dups, and reports with bad ID's    12-Mar-2002

    fgrep 'P P 1001'  $DATA/SHEFOUT1 | fgrep -v '**' |\
    awk ' $14 !~/-9*/ {print $0}' | awk ' $14 < 5  {print $0}' |\
    sort | uniq > xtemp

#   remove reports with incorrect ID's, but keep KNUQ (which is ok)

    awk '$6 ~/KNUQ/ || (length($6) !~/4/ && length($6) !~/7/) {print $0}' \
    xtemp  > rtemp
    date "+End shef-decoder:         %c" >> $DATA/hads-status-log
   
#   Copy SHEF output to a file for transfer to FTP server and BUFR database  
 
    if test -s  rtemp
    then
 
#   Select reports from SHEF output file that appear in this cycle's 
#   run for the program that makes BUFR messages from these reports
 
       if test $cyc -ne 3
       then
          sort $COMOUT/hrly-prcp-current rtemp | uniq -u > rtemp1
       else
          cp rtemp rtemp1
       fi

#   Create specially formatted files of these data for NESDIS & FSL.
#   Transfer the files to NCEP FTP server for them to grab these data.
 
      if test "$SENDCOM" = "YES"
      then
         cp  rtemp                $COMOUT/hrly-prcp-current
         cut -c6-26,48-57 rtemp > $COMOUT/vicente
 
         #
         # Create the daily files 
         # (dbn_alerts inside perl script, will look for SENDDBN var inside)
         #
         $USHshef/ingest_split_hads.pl $COMOUT/hrly-prcp-current

         if test "$SENDDBN" = "YES"
         then
            $DBNROOT/bin/dbn_alert DATA HRLY_PRCP $job $COMOUT/hrly-prcp-current
            $DBNROOT/bin/dbn_alert DATA HRLY_PRCP $job $COMOUT/vicente
         fi
      fi
 
#   move the ID to begining of line, remove excess fields
#   add lat-lons to shef output records
               
      cp $PARMshef/ingest_nwsli.uniq $DATA/nwsli.uniq
      cut -c18-25 rtemp1 | paste -d' ' - rtemp1 | cut -c1-26,36-66,69 > rtemp2
      $USHshef/ingest_anyplot.pl rtemp2 > rtemp3

#  
#   SHEF TO BUFR 
#  
#     now create the BUFR messages from the shef output
 
      pgm=ingest_shef2buf
      export pgm; prep_step

      msg="$pgm start"
      postmsg "$jlogfile" "$msg"

#     replace assign -a with link -sf on IBM sp system   
      ln -sf rtemp3                         fort.20
      ln -sf $TABLEDIR/bufrtab.000          fort.24
      ln -sf $DATA/make-bufr-output         fort.55

      $EXECshef/ingest_shef2buf
      err=$?;export err; err_chk

      date "+End  OK BUFR Prep: %c" >> $DATA/hads-status-log  # A-OK run
   
#  insert the BUFR messages into the IBM BUFR database...
#  assign filename with BUFR msgs to variable so that it fits on tranjb stmt
 
      cword=no
      export cword     
      bufrmsg=$DATA/make-bufr-output

      $USHbufr/tranjb  $TANKDIR $bufrmsg
 
    else
      date "+End BAD BUFR Prep: %c" >> $DATA/hads-status-log  # NOT OK run
      msg="No SHEF precip data"
      postmsg "$jlogfile" "$msg"
    fi


#   keep last 200 lines of the hads-status-log file
  
    if test "$SENDCOM" = "YES"
    then
       tail -200      $DATA/hads-status-log > temp-log
       cp   temp-log  $COMOUT/hads-status-log
       rm   temp-log
    fi

    msg='JOB COMPLETED NORMALLY'
    echo $msg
    postmsg "$jlogfile" "$msg"

    set +x
    echo " ***** PROCESSING COMPLETED NORMALLY ***** "
    echo " ***** PROCESSING COMPLETED NORMALLY ***** "
    echo " ***** PROCESSING COMPLETED NORMALLY ***** "
    set -x
                         
# ---------------END OF EXSHF SCRIPT ----------------------------
