########################################################################
##
## SCRIPT: sdmtwin_prodhist.sh      NCO/PMB/SDM
##
## CHANGE LOG:
##
##   09/2003   KUMAR       Modified to add switch to check whether the
##                         machine is prod or dev
##   04/2006   CARR        Fixed ftp bug and replaced with scp
##   09/2006   ACHORN      Pointed histories to production versions
##   05/2008   CARR        Removed references to ptmp
##   04/2010   MALONE      Changed working directory from local
##                         /nco/pmb/$LOGNAME/sdmedit to /nco/ops/sdm[2]
##   10/2010   MALONE      Changed output directory from local
##             (KEMPISTY)  /nco/pmb/$LOGNAME to /nco/ops/sdmtwin
##   10/2011   KEMPISTY    Modified to e-mail the output to the SDM
##                         instead of printing the otuput.  Also, modified
##                         to determine if a station has already been added
##                         to the reject list and informs the SDM of
##                         whether or not the station needs to be added.
##   04/2012   KEMPISTY    Modified to address instances in which a twin
##                         report is received for the same station at
##                         multiple times during the same 6-hour cycle.
##                         (For example, upper air data is received for
##                         station 12345 at both 11Z and 12Z during the 12Z
##                         cycle.)
##
########################################################################
set -x

## DETERMINE IF THE MACHINE IS PRODUCTION OR NOT
prod_machine=`cat /etc/prod | cut -c1`
current_machine=`hostname | cut -c1`
node_name=`hostname | cut -c1-4`

## SET THE PRINTER
export SDM_PRNTR=`cat ${HOME}/printer/sdmprint`

## DEFINE THE MACHINE AND ASSOCIATED VARIABLES.
machi=`hostname | cut -c1`
user=`whoami`
## If machi is s, then machine is stratus. If machi is c, then machine is cirrus.

## ERROR CHECK
## If script is running on the development machine, it will not run. Exit.
if [ "$prod_machine" != "$current_machine" ]
then
   echo `date -u` > twin_outfile
   echo "sdmtwin exiting because current machine is not production" >> twin_outfile
   cat twin_outfile >> /nco/ops/sdmtwin/sdmtwin_history
   exit
fi

echo "Job sdmtwin will continue on the production machine"

ORGSRC=/nwprod/exec

WKDIR=/nco/ops/sdmtwin

mkdir -p ${WKDIR}
#rm ${WKDIR}/*
ls ${WKDIR}/* | grep -v sdmtwin_history | xargs rm
cd ${WKDIR}

## ERROR CHECK for working directory.
err=$?
if [ $err -ne 0 ]
then
   echo cannot cd to ${WKDIR} - exit!!
   exit
fi

## Run setup script from nwprod
/nwprod/util/ush/setup.sh

runtime=`date -u`
curdate=`date -u +%Y%m%d`
twindate=`date -u +%y%m%d`
curhr=`date -u +%H`
dumphour=`expr $curhr \/ 6  \* 6 `
if test $dumphour -lt '10'
then
   dumphour=0$dumphour
fi
## reset hour here if testing or rerunning
#dumphour=12

echo "SDM TWIN CODE RUN AT $runtime FOR CYCLE=${dumphour}Z on ${node_name}" > twin_outfile
BUFFILE="PREPDA.${curdate}${dumphour}"
ls -t /nco/ops/sdm/$BUFFILE /nco/ops/sdm2/$BUFFILE > filesel

prepbufr=`head -1 filesel`
test -s filesel
err=$?
if test $err -ne 0
then 
   echo "SDMEDIT HAS NOT BEEN RUN FOR ${dumphour} CYCLE on ${node_name}" >> twin_outfile
   echo "${BUFFILE} NOT FOUND." >> twin_outfile
   awk '{ print $0 "\r" }' twin_outfile | mail -s "SDMTWIN ERROR" sdm@noaa.gov
   exit
fi

cp $ORGSRC/sdm_prtout $WKDIR
ln -sf  "$prepbufr"                    "fort.10"
ln -sf  "ttaanf"                       "fort.61"
ln -sf  "ttbbnf"                       "fort.62"
ln -sf  "ttccnf"                       "fort.63"
ln -sf  "ttddnf"                       "fort.64"
ln -sf  "ppbbnf"                       "fort.65"
ln -sf  "ppddnf"                       "fort.66"

$WKDIR/sdm_prtout 1>prtout.out 2>prtout.err

cp $ORGSRC/sdm_sdmtwin $WKDIR 

COMIN1=/com/gfs/prod/gdas.
THR=6
prevgoodcyc=`/nwprod/util/exec/ndate -$THR $curdate$dumphour`
DATE_HIS=`echo $prevgoodcyc | cut -c1-8`
CYC_HIS=`echo $prevgoodcyc | cut -c9-10`

ln -sf   "$COMIN1$DATE_HIS/upa_his_twinwind.ttaa.t$CYC_HIS"z""    "fort.24"
ln -sf   "$COMIN1$DATE_HIS/upa_his_twinwind.ttbb.t$CYC_HIS"z""    "fort.25"
ln -sf   "$COMIN1$DATE_HIS/upa_his_twinwind.ttcc.t$CYC_HIS"z""    "fort.26"
ln -sf   "$COMIN1$DATE_HIS/upa_his_twinwind.ttdd.t$CYC_HIS"z""    "fort.27"
ln -sf   "$COMIN1$DATE_HIS/upa_his_twinwind.ppbb.t$CYC_HIS"z""    "fort.28"
ln -sf   "$COMIN1$DATE_HIS/upa_his_twinwind.ppdd.t$CYC_HIS"z""    "fort.29"

ln -sf   "ttaanf"                       "fort.34"
ln -sf   "ttbbnf"                       "fort.35"
ln -sf   "ttccnf"                       "fort.36"
ln -sf   "ttddnf"                       "fort.37"
ln -sf   "ppbbnf"                       "fort.38"
ln -sf   "ppddnf"                       "fort.39"
ln -sf   "$WKDIR/twin"                  "fort.75"

$WKDIR/sdm_sdmtwin  1>duptupax.out 2>duptpaex.err

test -s  "$WKDIR/twin" 
err1=$?
if test $err1 -eq 0
then
   cat "$WKDIR/twin" >> twin_outfile
else
   echo "NO TWINS FOUND FOR $curdate$dumphour on $node_name " >> twin_outfile
   echo "NO TWINS FOUND FOR $curdate$dumphour on $node_name " >> sdmtwin_email
fi 

twinstn=`grep "For ${dumphour}Z ${twindate} Type" twin | awk '{ print $2 }' | sort +0 -1 | uniq` 
echo $twinstn

for stn in $twinstn
do
   twinhr=`grep "Station $stn For ${dumphour}Z ${twindate} Type" twin | awk '{ print $9 }' | cut -c1-2 | sort +0 -1 | uniq`
   echo $twinhr
   for hr in $twinhr
   do
      grep $stn /dcom/us007003/sdmedit | egrep "(${curdate}${hr})|( ---------- )|(${curdate}--)|(--------${hr})" > twin_$stn
      if test -s twin_$stn
      then
         exec 5<twin_$stn
         while read -u5 line; do
            echo $line > twin_case_$stn
            if [ `awk '{ print $4 }' twin_case_$stn` = "${curdate}${hr}" ]
            then
               if [ `awk '{ print $6 }' twin_case_$stn` = "PPPPP" -a `awk '{ print $8 }' twin_case_$stn` = "-----------" ]
               then
                  echo "" >> reject_$stn
                  echo "STATION $stn AT ${curdate}${hr} HAS BEEN ACKNOWLEDGED AND ADDED TO THE REJECT LIST.  NO ACTION NEEDED FOR THIS STATION." >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               else
                  datatype=`grep "Station $stn For ${dumphour}Z ${twindate} Type" twin | awk '{ print $7 }' | uniq`
                  echo "" >> reject_$stn
                  echo "STATION ${stn} IS A TWIN OBSERVATION THAT HAS NOT YET BEEN PURGED FOR ALL ELEMENTS AT ${hr}Z ON ${curdate}.  IF NOT ALREADY DONE, PLEASE ADD THE FOLLOWING TWIN OBSERVATION TO THE REJECT LIST." >> reject_$stn 
                  echo "" >> reject_$stn
                  echo "Station ${stn} For ${hr}Z ${twindate} Is a Time Twin For the Following Report Types:\n" >> reject_$stn
                  echo "${datatype}" >> reject_$stn
                  echo "" >> reject_$stn
                  grep "^${stn} " twin | sort +1 -2 >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               fi
            elif [ `awk '{ print $4 }' twin_case_$stn` = "----------" ]
            then
               if [ `awk '{ print $6 }' twin_case_$stn` = "PPPPP" -a `awk '{ print $8 }' twin_case_$stn` = "-----------" ]
               then
                  echo "" >> reject_$stn
                  echo "STATION $stn IS CURRENTLY ON THE PERMANENT REJECT LIST.  NO ACTION NEEDED FOR THIS STATION." >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               else
                  datatype=`grep "Station $stn For ${dumphour}Z ${twindate} Type" twin | awk '{ print $7 }' | uniq`
                  echo "" >> reject_$stn
                  echo "STATION ${stn} IS A TWIN OBSERVATION THAT HAS NOT YET BEEN PURGED FOR ALL ELEMENTS AT ${hr}Z ON ${curdate}.  IF NOT ALREADY DONE, PLEASE ADD THE FOLLOWING TWIN OBSERVATION TO THE REJECT LIST." >> reject_$stn 
                  echo "" >> reject_$stn
                  echo "Station ${stn} For ${hr}Z ${twindate} Is a Time Twin For the Following Report Types:\n" >> reject_$stn
                  echo "${datatype}" >> reject_$stn
                  echo "" >> reject_$stn
                  grep "^${stn} " twin | sort +1 -2 >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               fi
            elif [ `awk '{ print $4 }' twin_case_$stn` = "${curdate}--" ]
            then
               if [ `awk '{ print $6 }' twin_case_$stn` = "PPPPP" -a `awk '{ print $8 }' twin_case_$stn` = "-----------" ]
               then
                  echo "" >> reject_$stn
                  echo "STATION $stn HAS BEEN ACKNOWLEDGED AND ADDED TO THE REJECT LIST FOR ALL HOURS ON ${curdate}.  NO ACTION NEEDED FOR THIS STATION." >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               else
                  datatype=`grep "Station $stn For ${dumphour}Z ${twindate} Type" twin | awk '{ print $7 }' | uniq`
                  echo "" >> reject_$stn
                  echo "STATION ${stn} IS A TWIN OBSERVATION THAT HAS NOT YET BEEN PURGED FOR ALL ELEMENTS AT ${hr}Z ON ${curdate}.  IF NOT ALREADY DONE, PLEASE ADD THE FOLLOWING TWIN OBSERVATION TO THE REJECT LIST." >> reject_$stn 
                  echo "" >> reject_$stn
                  echo "Station ${stn} For ${hr}Z ${twindate} Is a Time Twin For the Following Report Types:\n" >> reject_$stn
                  echo "${datatype}" >> reject_$stn
                  echo "" >> reject_$stn
                  grep "^${stn} " twin | sort +1 -2 >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               fi
            elif [ `awk '{ print $4 }' twin_case_$stn` = "--------${hr}" ]
            then
               if [ `awk '{ print $6 }' twin_case_$stn` = "PPPPP" -a `awk '{ print $8 }' twin_case_$stn` = "-----------" ]
               then
                  echo "" >> reject_$stn
                  echo "STATION $stn HAS BEEN ADDED TO THE REJECT LIST FOR ${hr}Z ON ALL DATES.  NO ACTION NEEDED FOR THIS STATION." >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               else
                  datatype=`grep "Station $stn For ${dumphour}Z ${twindate} Type" twin | awk '{ print $7 }' | uniq`
                  echo "" >> reject_$stn
                  echo "STATION ${stn} IS A TWIN OBSERVATION THAT HAS NOT YET BEEN PURGED FOR ALL ELEMENTS AT ${hr}Z ON ${curdate}.  IF NOT ALREADY DONE, PLEASE ADD THE FOLLOWING TWIN OBSERVATION TO THE REJECT LIST." >> reject_$stn 
                  echo "" >> reject_$stn
                  echo "Station ${stn} For ${hr}Z ${twindate} Is a Time Twin For the Following Report Types:\n" >> reject_$stn
                  echo "${datatype}" >> reject_$stn
                  echo "" >> reject_$stn
                  grep "^${stn} " twin | sort +1 -2 >> reject_$stn
                  echo "" >> reject_$stn
                  echo "" >> reject_$stn
               fi
            fi
         done
      else
         datatype=`grep "Station $stn For ${dumphour}Z ${twindate} Type" twin | awk '{ print $7 }' | uniq`
         echo "" >> reject_$stn
         echo "IF NOT ALREADY DONE, PLEASE ADD THE FOLLOWING TWIN OBSERVATION TO THE REJECT LIST." >> reject_$stn
         echo "" >> reject_$stn
         echo "Station ${stn} For ${hr}Z ${curdate} Is a Time Twin For the Following Report Types:\n" >> reject_$stn
         echo "${datatype}" >> reject_$stn
         echo "" >> reject_$stn
         grep "^${stn} " twin | sort +1 -2 >> reject_$stn
         echo "" >> reject_$stn
         echo "" >> reject_$stn
      fi
      cat reject_${stn} >> reject_${stn}.tmp
      if [ `grep "STATION $stn" reject_${stn}.tmp | wc -l` -gt 1 ]
      then
         if [ `grep ACKNOWLEDGED reject_${stn}.tmp | wc -l` -gt 1 ]
         then
            sort -u reject_${stn}.tmp | pr -d -t > reject_$stn
         else
            line=`grep ACKNOWLEDGED reject_$stn`
            echo "" > reject_$stn
            echo $line >> reject_$stn
            echo "" >> reject_$stn
            echo "" >> reject_$stn
         fi
      fi
      rm reject_*.tmp
   done
done

cat reject_* | uniq >> sdmtwin_email

awk '{ print $0 "\r" }' sdmtwin_email | mail -s "SDMTWIN OUTPUT FOR ${curdate} ${dumphour}Z" sdm@noaa.gov
cat twin_outfile >> /nco/ops/sdmtwin/sdmtwin_history

exit

