#!/bin/sh 
#
# <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>
# <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>
# << FSL_aircraft.run                                                       >>
# <<                                                                        >>
# << Dump acars and and other select aircraft obs                           >>
# <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>
# <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>
#
# This script is called with arguments
#   Arg 1: the MADIS Run directory
#   Arg 2: the 9-character DB time (YYDDDHHMM)
#   Arg 3: the current minute (MM)
#   Arg 4: bufr dump output directory
#
# Imported variables
#   CDY   - Current day (like PDY but not based on com/date)
#   
###############################################################################
#   Data flow
#     1) BUFR tank dump
#          - Format: BUFR  
#          - Prog: dumpjb                 
#          - OutDir: ${BUFRDUMPDIR}
###############################################################################

set -xa

###############################################################################
# ASSIGN THE ARGUMENTS
###############################################################################

RUNDIR=$1
ATIME=$2
MIN=$3
BUFRDUMPDIR=$4

echo "Incoming ATIME=$ATIME" 

############################################################################### 
# The timeing will change from this for NCEP runs
############################################################################### 
#
# The ACARS processing schedule:
#
# Each HH is processed 12 times, in the following order.
#
# Every 10 minutes from HH:22 through HH+1:22 (7 times straight); then
# at HH+1:37, HH+2:22 (one hour later), HH+3:22 (two hours later), 
# HH+9:32 (8 hours later), and HH+13:32 (12 hours later).  Note that the 
# 8h & 12h late runs are pushed back to minute 32.  Also note that the
# HH+1:37 run is squeezed in between the minute 22 and 32 runs in order
# to match the upstream processing schedule.
#
# Note that when we process multiple hours they're processed oldest to
# newest (because the previous hour's data are used in the QC).
#
# First we'll process the regular files, then after all of those have
# been done we'll process the same files for profiles.
###############################################################################

cd ${RUNDIR}/aircraft

    HH=`echo $ATIME | cut -c6-7`           #current hour
    echo "CURRENT HH=$HH" 
    
    
###############################################################################
# BUFR dump
###############################################################################
# Note - probably need to use DTIM_earliest_TTTSSS and
# DTIM_latest_TTTSSS in order to get most current hour, previous hour, etc.,
# also note that this can be a floating point number, e.g., 1.5 hours
#
# DGRP = data group(s) - from mnemonics or type/subtype numbers found in
#        /nwprod/fix/bufr_dumplist; if using multiple groups, enclose them
#        with " " since this is a single argument; note that you don't use
#        the underscore at the beginning of the mnemonic, e.g., use "sflnd"
#        not "_sflnd".
###############################################################################
    
#    Skip PIREP, AIREP, and RECCO 
     export SKIP_004001=1 SKIP_004002=1 SKIP_004005=1
#    export SKIP_004001=1 SKIP_004002=1 SKIP_004005=1 SKIP_004008=1 SKIP_004009=1 SKIP_004010=1 SKIP_004011=1
#    export SKIP_004001=1 SKIP_004002=1 SKIP_004005=1 SKIP_004009=1 SKIP_004011=1
    

# We need 30 minutes on either side of the hour for sounding reports     
    RAD=1.0
    
    DGRP=004	
    

# Prior hours to run loop
# TBD -- SET time schedule
# Once per hour run 8 and 12 hours back
    HOURSTODO=( 0 1 )
    if [ $MIN -gt 35 -a $MIN -lt 41 ]
    then
        HOURSTODO=( 0 1 8 12 )
    fi
    if [ $MIN -gt 12 -a $MIN -lt 18 ]
    then
        HOURSTODO=( 0 1 2 3 )
    fi
    if [ $MIN -gt 47 -a $MIN -lt 53 ]
    then
        HOURSTODO=( 0 1 2 3 )
    fi

## Current time
#   export ADTM=`perl -e 'print time;'`
# Cycle time
#   export ADTM=`date --date="$CDY $HH:$MIN:00 UTC" +%s`
    export CTIME=${CDY}${HH}${MIN}
    export ADTM=`perl -e 'use Time::Piece; my $time = Time::Piece->strptime($ENV{CTIME},"%Y%m%d%H%M"); print $time->epoch;'`
    echo "PERL TIME ADTM=$ADTM"
    for hr in "${HOURSTODO[@]}"
      do
      export ADTMOFF=$hr
      
      echo "Running $ADTM $ADTMOFF"
      echo "INSIDE DO HOURSTODO = ${HOURSTODO[@]} hr=$hr" 

# Running directory
      cd ${RUNDIR}/aircraft
      
# Start hour of time period in format YMHD
      SYMDH=`perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime($ENV{"ADTM"}-($ENV{"ADTMOFF"}*3600));printf "%4d%02d%02d%02d\n",$year+1900,$mon+1,$mday,$hour;'`
      SYJH=`perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime($ENV{"ADTM"}-($ENV{"ADTMOFF"}*3600));printf "%02d%03d%02d\n",(($year+1900)%100),$yday+1,$hour;'`
      SYJHM=${SYJH}00
      EYJHM=${SYJH}59
      
      echo "Checking for: ${SYMDH}00 to ${SYMDH}59 -- $EYJHM"
# Prior run cleanup
      rm -f ac.tmp.1 ac.tmp.2 ${RUNDIR}/004.ibm 004.ibm 004.out
      
# OLD BUFR FILES DUMP LOCATION
# Dump the tanks BUFR data
# Set time to middle of hour
      ${DUMPJB:?} ${SYMDH}.5 $RAD $DGRP
      #clp - change error checking
      export err=$?
      if [[ $err -ne 11 && $err -ne 0  && $err -ne 22 ]]
        then  err_chk
      fi
      FILE=004.ibm
      cp ${RUNDIR}/${FILE} .
      export err=$?;err_chk
      FILEPREV=${BUFRDUMPDIR}/${SYJHM}.bufr_d
# Check for new files
      if [ -r ${FILE} ]
	  then
	  C=`cat ${FILE} | wc -l`
	  if [ $C -ne 0 ] ; then
	      
	      if [ -r ${FILEPREV} ]
		  then
		  ls -l ${FILE} | awk '{print $5}' > ac.tmp.1
		  ls -l ${FILEPREV} | awk '{print $5}' > ac.tmp.2
		  x=`diff ac.tmp.1 ac.tmp.2 | wc -l`
	      else 
		  echo "FIRST TIME"
# First time here.  Make sure we have a prev file for next time.
		  x=1
		  cp $FILE ${FILEPREV}.prev
          
	      fi

# For redo times greater than 11 hours, always redo everything. We want to pick up any old EDR files
	      if [ $hr -gt 11 ]
		  then
		  x=1
	      fi

	      
# New data check
	      if [ $x -gt 0 ]
		  then
		  cp $FILE ${FILEPREV}
      		  export err=$?;err_chk
		  echo "New file: ${FILEPREV}"

          if [ "$SENDDBN" = 'YES' ] ; then
            export DBNALERT_TYPE=${DBNALERT_TYPE:-DATA}
            $SIPHONROOT/bin/dbn_alert $DBNALERT_TYPE MADIS_DUMP $job ${FILEPREV}
          fi
		  
	      fi
	  else
	      echo "File is zero length"
	  fi
      else
	  echo "No 004.ibm file"
      fi
    done