##################################################################################
##
## SCRIPT:  sdmedit_start.sh            NCO/PMB/SDM
##
## Syntax:  sdmedit_start.sh $1 $2 $3
##          $1 = date (yyyymmdd)
##          $2 = model hour (00 06 12 18)
##          $3 = data type (1 - adpupa
##                          2 - adpupa aircft
##                          3 - adpupa aircft acars
##                          4 - adpupa aircft acars adpsfc)
##
## CHANGE LOG:
##
##   10/2003   KUMAR       		Added listq code.  Creates asci output with
##		           		detailed diagnostics (listq.out) and 3 asci
## 			   		files pwater.$date$hh, rhlow.$date$hh,
## 			   		rhhigh.$date$hh
##   04/2004   STOUDT	   		Added bigq.$date$hour printed output file
##   02/2005   SHIREY      		Changed hardwired printer to variable SDM_PRNTR
##			   		which is set in sdm account .profile for easy
## 			   		switching during COOP situations
##   08/2007   STOUDT      		Modified listq.x to listqnew.x which produces
##			   		streamlined version of bigq output.  Also added
##			   		acftqc aircraft QC tool including printouts and
##			   		listac file
##   05/2008   STOUDT	   		Added listsuper QC tool that computes and lists
##			   		lapse rates with emphasis on supers
##   04/2010   MALONE      		Changed working directory from local
##             (KEMPISTY)  		/nco/pmb/$LOGNAME/sdmedit to /nco/ops/sdm[2]
##   10/2011   KEMPISTY		V1.2.0	Modified sdmedit to only print output files
##			   		for which diagnostic output is produced.
##			   		Also, added error checking to provide
##			   		troubleshooting assistance in the event of a
##                         		failure/hang of sdmedit.  Integrated sdmtwin
##					into sdmedit driver script.
##   12/2011   KEMPISTY		V1.2.1  Bug fix for sdmtwin to correct for processing
##					of multiple twin reports.
##   01/2012   KEMPISTY		V1.2.2  Bug fix for sdmtwin to correct for multiple
##					twins from the same station within the same
##					six hour cycle.
##   04/2012   KEMPISTY         V1.2.3  Increased sdm_acar array size from 65,000 to 
##					85,000 to handle higher counts of ACARS reports.
##   06/2012   KEMPISTY    	V1.2.4	Replaced all 'lpr' print commands with 
##                         		'enscript' print commands due to an lpr 
##                         		incompatibility with the color Xerox printer
##                         		at NCWCP.
##   06/2012   KEMPISTY    	V1.2.5	Modified for handling of 2012 PREPOBS_PREPACQC
##                         		V2.0.0 upgrade.  
##
####################################################################################

set -xua

##  Override ncepdate
##  -----------------
DATE=$1
hour=$2
type=$3
export CDATE10=$DATE$hour

##  Required!!!
##  -----------

envir=prod   #   default is prod

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

###
NET=gfs
run=gdas1
###

## DEFINE THE WORKING DIRECTORY.
if [ $LOGNAME = 'wx12sd' ]; then
   export DATA=/nco/ops/sdm  # operational wx12sd
else
   export DATA=/nco/ops/sdm2   # operational wx12ss
fi

mkdir -p $DATA
rm -rf $DATA/[a-z]* [A-Z]*
cd $DATA

### Error check for working directory.

err=$?
if [ $err -ne 0 ]
then
   echo "cannot cd to ${DATA} - exit!!"
   exit
fi

## Before running the QC codes, clean up previous prepbufr files.  If 
## any prepbufr files greater than 20 days old remain in the working
## directory delete them.  Only run the cleanup, if the date of this
## sdmedit run matches the current date.

pdy=`date "+%Y%m%d"`
echo $pdy

if [ $pdy = $DATE ]; then
   cleandate=`/nwprod/util/ush/finddate.sh $pdy d-20`
   cleanlist=`ls -ltr $DATA | grep "PREPDA\." | awk '{ print $9 }'`
   for file in $cleanlist
   do
      filedate=`echo $file | cut -c8-15`
      filehour=`echo $file | cut -c16-17`
      if [ $filedate -lt $cleandate ]; then
         rm -f PREPDA.${filedate}${filehour}
      fi
   done
fi

export jlogfile=$DATA/jlog
/nwprod/util/ush/setup.sh

cycle=`echo $CDATE10|cut -c9-10`z
export COMSP=$DATA/$run.t${cycle}.

################################################################
# dumpjb section
################################################################

# Override defaults

pgmout=$DATA/allout   #   default is /dev/null
errPREPDATA_limit=4
echo "type=$type"

if test $type = "2"
then
   export PREPACQC=YES
   export BUFRLIST='adpupa aircft'
   export ACARSQC=NO
   acars=no
   aircft=yes

   ( 
   mkdir $DATA/dumpjb1
   cd $DATA/dumpjb1
   sh /nwprod/ush/dumpjb $CDATE10 3 adpupa
   cd $DATA
   mv adpupa.ibm $run.t${cycle}.adpupa.tm00.bufr_d ) &

   ( 
   mkdir $DATA/dumpjb2
   cd $DATA/dumpjb2
   sh /nwprod/ush/dumpjb $CDATE10 3 aircft
   cd $DATA
   mv aircft.ibm $run.t${cycle}.aircft.tm00.bufr_d ) &
   
   wait

elif test $type = "3"
then
   export PREPACQC=YES
   export BUFRLIST='adpupa aircft aircar'
   export ACARSQC=YES
   acars=yes
   aircft=yes

   ( 
   mkdir $DATA/dumpjb1
   cd $DATA/dumpjb1
   sh /nwprod/ush/dumpjb $CDATE10 3 adpupa
   cd $DATA
   mv adpupa.ibm $run.t${cycle}.adpupa.tm00.bufr_d ) &

   ( 
   mkdir $DATA/dumpjb2
   cd $DATA/dumpjb2
   sh /nwprod/ush/dumpjb $CDATE10 3 aircft
   cd $DATA
   mv aircft.ibm $run.t${cycle}.aircft.tm00.bufr_d ) &

   ( 
   mkdir $DATA/dumpjb3
   cd $DATA/dumpjb3
   sh /nwprod/ush/dumpjb $CDATE10 3 aircar
   cd $DATA
   mv aircar.ibm $run.t${cycle}.aircar.tm00.bufr_d ) &

   wait

elif test $type = "4"
then
   export PREPACQC=YES
   export BUFRLIST='adpupa aircft aircar adpsfc'
   export ACARSQC=YES
   acars=yes
   aircft=yes

   ( 
   mkdir $DATA/dumpjb1
   cd $DATA/dumpjb1
   sh /nwprod/ush/dumpjb $CDATE10 3 adpupa
   cd $DATA
   mv adpupa.ibm $run.t${cycle}.adpupa.tm00.bufr_d ) &

   ( 
   mkdir $DATA/dumpjb2
   cd $DATA/dumpjb2
   sh /nwprod/ush/dumpjb $CDATE10 3 aircft
   cd $DATA
   mv aircft.ibm $run.t${cycle}.aircft.tm00.bufr_d ) &

   ( 
   mkdir $DATA/dumpjb3
   cd $DATA/dumpjb3
   #sh /nwprod/ush/dumpjb $CDATE10 3 aircar aircft
   sh /nwprod/ush/dumpjb $CDATE10 3 aircar 
   cd $DATA
   mv aircar.ibm $run.t${cycle}.aircar.tm00.bufr_d ) &

   ( 
   mkdir $DATA/dumpjb4
   cd $DATA/dumpjb4
   sh /nwprod/ush/dumpjb $CDATE10 3 adpsfc
   cd $DATA
   mv adpsfc.ibm $run.t${cycle}.adpsfc.tm00.bufr_d ) &

   wait

else
   export PREPACQC=NO
   export BUFRLIST='adpupa'
   export ACARSQC=NO
   acars=no
   aircft=no

   ( 
   mkdir $DATA/dumpjb1
   cd $DATA/dumpjb1
   sh /nwprod/ush/dumpjb $CDATE10 3 adpupa
   cd $DATA
   mv adpupa.ibm $run.t${cycle}.adpupa.tm00.bufr_d ) &

   wait

fi
export acars aircft
echo dumpjb complete

################################################################
# prepobs_makeprepbufr.sh section 
################################################################

# --> These all have default YES
SYNDATA=NO
DO_QC=YES
PREPDATA=YES
CQCBUFR=YES
PROFCQC=NO
CQCVAD=NO
OIQCBUFR=NO
SDMACQC=NO

# makeprepbufer is faster with poe if there are aircraft.
# if's faster without poe for just upaair

export POE=NO
export POE_OPTS="-llfile $DATA/mpbllfile"

echo "#@resources=ConsumableMemory(3000 MB)" > $DATA/mpbllfile
echo "#@task_affinity=cpu(1)" >> $DATA/mpbllfile
echo "#@node=1" >> $DATA/mpbllfile
echo "#@node_usage=shared" >> $DATA/mpbllfile
echo "#@class=prodser" >> $DATA/mpbllfile
echo "#@account_no=SDM-OPS" >> $DATA/mpbllfile
echo "#@job_type=parallel" >> $DATA/mpbllfile
echo "#@wall_clock_limit=00:10:00" >> $DATA/mpbllfile
echo "#@group=nwprod" >> $DATA/mpbllfile
echo "#@total_tasks=4" >> $DATA/mpbllfile
echo "#@network.MPI=sn_all,not_shared,IP" >> $DATA/mpbllfile
echo "#@queue" >> $DATA/mpbllfile

export envir=prod
SCRMAKE=/nw${envir}/ush

export LOADL_INTERACTIVE_CLASS=dev
export MP_RETRY=10
export MP_RETRYCOUNT=1000
export XLSMPOPTS=parthds=4:usrthds=4:stack=20000000
export MP_ADAPTER_USE=dedicated 
export MP_SHARED_MEMORY=yes
export MP_LABELIO=YES
export MP_STDOUTMODE=ordered
export MP_INFOLEVEL=2
export MP_SHARED_MEMORY=yes

## run makeprepburf mpmd - many codes across numerous processors
export MP_PGMMODEL=mpmd

export OIQCBUFR=NO

timex sh /nwprod/ush/prepobs_makeprepbufr.sh $CDATE10  > outfile 2> makeprep_err

## Error check for the completion of makeprepbufr

err=$?
if [ $err -ne 0 ]
then
   echo "A problem occurred in makeprepbufr.  Aborting sdmedit.  Exit!!"
   exit
fi

################################################################
## prepobs_oiqcbufr.sh section
################################################################

## run oiqc spmd - one code across numerous processors
export MP_PGMMODEL=spmd

## export vars needed by prepobs_oiqcbufr.sh
export XLFRTEOPTS="unit_vars=yes"
export OIQCT=${OIQCT:-/nw${envir}/fix/prepobs_oiqc.oberrs}
export OIQCX=/nw${envir}/exec/prepobs_oiqcbufr

timex poe $SCRMAKE/prepobs_oiqcbufr.sh $DATA/prepda.t${cycle} $CDATE10 -llfile $DATA/mpbllfile -infolevel 5  > outout 2> oiqc_poe_err

## Error check for the completion of oiqcbufr

err=$?
if [ $err -ne 0 ]
then
   echo "A problem occurred in oiqcbufr.  Aborting sdmedit.  Exit!!"
   exit
fi

cat errfile >> outout
cat outout >> oiqcbufr.out

set -uax
prepbufr=$DATA/prepda.t${cycle}
envir=prod

cp $prepbufr PREPDA.$CDATE10

## RUN SDMTWIN CODE
SDMTWIN=/nw${envir}/ush/sdmtwin_prodhist.sh

sh $SDMTWIN

## Error check for the successful completion of sdmtwin

err=$?
if [ $err -ne 0 ]
then
   echo "A problem occurred in sdmtwin.  Aborting sdmedit.  Exit!!"
   exit
fi

## RUN LISTQNEW CODE FOR MOISTURE QC
LISTQEXEC=/nw${envir}/exec/sdm_listqnew

runtime=`date -u`
echo "$runtime  " > run.time
cat run.time

export XLFRTEOPTS="unit_vars=yes"
export pgm=sdm_listqnew
#. prep_step

BUFFILE=$prepbufr
export XLFUNIT_10="$BUFFILE"
export XLFUNIT_13="file13"
export XLFUNIT_15="file15"
export XLFUNIT_16="file16"
export XLFUNIT_17="file17"
export XLFUNIT_50="pwater"
export XLFUNIT_51="rhlow"
export XLFUNIT_52="rhhigh"

$LISTQEXEC < run.time 1>listq.out 2>listq.err

## Error check for the successful completion of listq

err=$?
if [ $err -ne 0 ]
then
   echo "A problem occurred in listq.  Aborting sdmedit.  Exit!!"
   exit
fi

cat pwater  >  pwater.$date$hour
cat rhlow   >  rhlow.$date$hour
cat rhhigh  >  rhhigh.$date$hour
cat file17  >  listq.$date$hour
cat file13  >  bigq.$date$hour
cat file15  >  bigqdif.$date$hour

envir=prod

## RUN LISTSUPER CODE FOR LAPSE RATE QC
SUPEREXEC=/nw${envir}/exec/sdm_listsuper # operational

runtime=`date -u`
echo "$runtime  " > run.time
cat run.time

export pgm=sdm_listsuper
#. prep_step

BUFFILE=$prepbufr
export XLFUNIT_10="$BUFFILE"
export XLFUNIT_13="superhead"
export XLFUNIT_15="super"
export XLFUNIT_17="listlapse"

$SUPEREXEC < run.time 1>listsuper.out 2>listsuper.err

## Error check for the successful completion of listsuper

err=$?
if [ $err -ne 0 ]
then
   echo "A problem occurred in listsuper.  Aborting sdmedit.  Exit!!"
   exit
fi

cat superhead > super$date$hour
sort -r super | cut -c7-86 | sed 's/```/   /g' >> super$date$hour
cat listlapse > listlapse$date$hour

## IF AIRCRAFT DATA ARE DUMPED FOR QC, RUN ACQC CODE
if test $aircft = 'yes'
then
   time=`date -u`
   echo $time > run.time

   GETAIR=/nw${envir}/exec/sdm_acqc

   export pgm=sdm_acqc
#  . prep_step

   echo $time > run.time
   export XLFUNIT_10=$prepbufr
   export XLFUNIT_55=Airdiag1$hour
   export XLFUNIT_60=Listair1$hour

   $GETAIR < run.time

## Error check for the successful completion of sdm_acqc

   err=$?
   if [ $err -ne 0 ]
   then
      echo "A problem occurred in sdm_acqc.  Aborting sdmedit.  Exit!!"
      exit
   fi

####
## this is printing duplicates...remove them
####
   for ID_pnt in `grep -n ID $DATA/Airdiag1$hour | awk -F: '{print $1}'`
   do
      echo $ID_pnt+1 | /usr/bin/bs >>$DATA/nums
      echo $ID_pnt-3 | /usr/bin/bs >>$DATA/nums
   done
   cat $DATA/nums | sort -un > nums2
   csplit_opts=""
   for line in `cat $DATA/nums2`
   do
      echo $line
      if [ $line -gt 5 ]
      then
         csplit_opts=`echo "$csplit_opts $line"`
      fi
   done
   echo $csplit_opts
   csplit $DATA/Airdiag1$hour $csplit_opts

## now sort the odd ones and put it all back together.
   for part in $DATA/xx01 $DATA/xx03 $DATA/xx05
   do
      cat $part | sort -u > ${part}s
   done

   cat $DATA/xx00 $DATA/xx01s $DATA/xx02 $DATA/xx03s $DATA/xx04 $DATA/xx05s > $DATA/Airdiag1${hour}
#  rm $DATA/nums $DATA/nums2 $DATA/xx*
## End of sort code

   echo "SDM AIRCRAFT DIAGNOSTICS FOR RUN $date$hour" > Airinfo$hour
   echo "SDM AIRCRAFT DIAGNOSTICS FOR RUN $date$hour" > Listair$hour
   cat  Airdiag1$hour >> Airinfo$hour
   grep " 1.[0-9][0-9]W" Listair1$hour > tahiti1
   grep -v " [2-9][0-9].[0-9][0-9]N" tahiti1 | grep -v " [2-9][0-9].[0-9][0-9]S" > tahiti2
   if test -s tahiti2
   then
      if [ `cat tahiti2 | wc -l` -ge 1 ]; then
         echo "tahiti: Diagnostic output for Tahiti position errors produced and appended to Airinfo$hour" > sdmedit_diagnostic_output
         echo " " >> Airinfo$hour
         echo "**** Suspect Tahiti Position Errors Follow *****" >> Airinfo$hour
         echo "                                      Temperature     Direct   Speeds" >> Airinfo$hour
         echo "  ID      Time   Lat    Lon     Press  OB     Dif QM  OB/GES  OB GES QM  Dif" >> Airinfo$hour
         cat tahiti2 >> Airinfo$hour
      else
         echo "tahiti: No diagnostic output produced for Tahiti position errors.  Will not append tahiti data to Airinfo$hour file." > sdmedit_diagnostic_output
      fi
   else
      echo "Could not find tahiti2 file.  Continue."
   fi
   cat  Listair1$hour >> Listair$hour

   if test $LOGNAME = wx12sd -o $LOGNAME = wx12ss 
   then
      if test -s Airinfo$hour
      then
         if [ `cat Airinfo$hour | wc -l` -gt 7 ];
         then
            echo "sdm_acqc: Printing diagnostic output produced for Aircraft in Airinfo$hour file." >> sdmedit_diagnostic_output
            echo "Printing Airinfo"
            awk '{ print $0 "\r" }' Airinfo$hour | enscript -d$SDM_PRNTR -hB -f Courier11
         else
            echo "sdm_acqc:  No diagnostic output produced for aircraft.  Will not print Airinfo$hour file." >> sdmedit_diagnostic_output
         fi
      else
         echo "Could not find Airinfo$hour.  File does not exist.  Exit sdmedit."
         exit
      fi
   fi
fi

## IF ACARS DATA ARE DUMPED FOR QC, RUN SDMACAR CODE AND ACFTQC CODE

if test $acars = 'yes'
then

   GETACARS=/nw${envir}/exec/sdm_sdmacar

   export pgm=sdm_sdmacar
#  . prep_step

   export XLFUNIT_10=$prepbufr
   export XLFUNIT_55=BIGDIF_ACARS$hour
   export XLFUNIT_56=BIGDIF_EQUAT$hour
   export XLFUNIT_60=LISTACARS$hour

   $GETACARS < run.time 

## Error check for the successful completion of sdm_acars

   err=$?
   if [ $err -ne 0 ]
   then
      echo "A problem occurred in sdm_acars.  Aborting sdmedit.  Exit!!"
      exit
   fi

   echo "SDM ACARS DIAGNOSTICS FOR RUN $date$hour" > bigdif_acars$hour
   cat BIGDIF_ACARS$hour >> bigdif_acars$hour

   if test -s BIGDIF_EQUAT$hour 
   then
      if [ `cat BIGDIF_EQUAT$hour | wc -l` -ge 5 ]; then
         cat BIGDIF_EQUAT$hour >> bigdif_acars$hour
         echo "acar_equator: Diagnostic output for Equator data produced and appended to bigdif_acars$hour" >> sdmedit_diagnostic_output
      else
         echo "acar_equator: No diagnostic output produced for equator data.  Will not append equator data to bigdif_acars$hour file." >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find BIGDIF_EQUAT$hour.  Continue."
   fi

   echo "SDM ACARS DIAGNOSTICS FOR RUN $date$hour" > listacars_$hour
   cat LISTACARS$hour >> listacars_$hour

   ACFTQCEXEC=/nw${envir}/exec/sdm_acftqc 

   export XLFUNIT_10=$prepbufr
   export XLFUNIT_30=acftrejt$hour
   export XLFUNIT_31=acftrejn$hour
   export XLFUNIT_55=acftsort$hour
   export XLFUNIT_65=acfthigh$hour
   export XLFUNIT_69=acftpats$hour
   export XLFUNIT_70=acfttrks$hour
   export XLFUNIT_75=acfttrkt$hour
   export XLFUNIT_76=acfttrkw$hour
   export XLFUNIT_80=acfttrksprint
   export XLFUNIT_81=acfttrktprint
   export XLFUNIT_82=acfttrkwprint

   $ACFTQCEXEC < run.time 1>acftqc.out 2>acftqc.err

## Error check for the successful completion of sdm_acftqc

   err=$?

   if [ $err -ne 0 ]
   then
      echo "A problem occurred in sdm_acftqc.  Aborting sdmedit.  Exit!!"
      exit
   fi

   cat /nw${envir}/fix/listac.head > listac.$date$hour
   cat acftsort$hour >> listac.$date$hour
   cat acfttrks$hour > listacfttrks.$date$hour
   cat acfttrkt$hour > listacfttrkt.$date$hour
   cat acfttrkw$hour > listacfttrkw.$date$hour
   cat listacfttrk*.$date$hour > printacfttrkx$hour

   if test $LOGNAME = wx12sd -o $LOGNAME = wx12ss 
   then
      if test -s bigdif_acars$hour
      then
         if [ `cat bigdif_acars$hour | wc -l` -ge 7 ]; then
            echo "sdm_sdmacar:  Printing diagnostic output produced for ACARS big differences in bigdif_acars$hour file." >> sdmedit_diagnostic_output
            echo "Printing bigdif_acars"
            awk '{ print $0 "\r" }' bigdif_acars$hour | enscript -d$SDM_PRNTR -hB -f Courier11
         else 
            echo "sdm_acars: No diagnostic output produced for ACARS big differences.  Will not print bigdif_acars$hour."
         fi
      else
         echo "Could not find bigdif_acars$hour.  File does not exist. Exit sdmedit."
         exit
      fi
   fi

   acfttrks_printstatus=`cat acfttrksprint`
   acfttrkt_printstatus=`cat acfttrktprint`
   acfttrkw_printstatus=`cat acfttrkwprint`

   if test -s listacfttrks.$date$hour 
   then
      if [ $acfttrks_printstatus -eq 1 ]; then
         echo "sdm_acftqc: Printing diagnostic output produced for ACARS speeds in listacfttrks.$date$hour file." >> sdmedit_diagnostic_output
         awk '{ print $0 "\r" }' listacfttrks.$date$hour | enscript -P$SDM_PRNTR -rhB -f Courier11
      elif [ $acfttrks_printstatus -eq 2]; then
         echo "sdm_acftqc: No diagnostic output produced for ACARS speeds.  Will not print listacfttrks.$date$hour." >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find listacfttrks.$date$hour.  File does not exist.  Exit sdmedit."
      exit 
   fi

   if test -s listacfttrkt.$date$hour
   then
      if [ $acfttrkt_printstatus -eq 1 ]; then
         echo "sdm_acftqc: Printing diagnostic output produced for ACARS temperatures in listacfttrkt.$date$hour file." >> sdmedit_diagnostic_output
         awk '{ print $0 "\r" }' listacfttrkt.$date$hour | enscript -P$SDM_PRNTR -rhB -f Courier11
      elif [ $acfttrkt_printstatus -eq 2 ]; then
         echo "sdm_acftqc: No diagnostic output produced for ACARS temperatures.   Will not print listacfttrkt.$date$hour." >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find listacfttrkt.$date$hour.  File does not exist.  Exit sdmedit."
      exit
   fi

   if test -s listacfttrkw.$date$hour 
   then
      if [ $acfttrkw_printstatus -eq 1 ]; then
         echo "sdm_acftqc: Printing diagnostic output produced for ACARS winds in listacfttrkw.$date$hour file." >> sdmedit_diagnostic_output
         awk '{ print $0 "\r" }' listacfttrkw.$date$hour | enscript -P$SDM_PRNTR -rhB -f Courier11
      elif [ $acfttrkw_printstatus -eq 2 ]; then
         echo "sdm_acftqc: No diagnostic output produced for ACARS winds.   Will not print listacfttrkw.$date$hour." >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find listacfttrkw.$date$hour.  File does not exist.  Exit sdmedit."
      exit
   fi
fi


rm BIGDIF$hour BIGDIF.tempfile.$hour lowtemp1

## RUN LISTADP_NEW CODE FOR RAOB STATION QC
GETADP=/nw${envir}/exec/sdm_listadp_new

runtime=`date -u`
echo "$runtime  " > run.time

export pgm=sdm_listadp_new
#. prep_step

export XLFUNIT_10=$prepbufr
export XLFUNIT_13=BIGDIF$hour       
export XLFUNIT_15=BIGDIF.tempfile.$hour
export XLFUNIT_16=GETADP$hour   
export XLFUNIT_17=lowtemp1     

$GETADP < run.time

## Error check for the successful completion of sdm_listadp

err=$?
if [ $err -ne 0 ]
then
   echo "A problem occurred in sdm_listadp.  Aborting sdmedit.  Exit!!"
   exit
fi

sort +0n -1 +1 -3 BIGDIF.tempfile.$hour | cut -c 23-144 > SORTEDFILE
cat SORTEDFILE >> BIGDIF$hour
# sort -rn bigqdif.$date$hour | cut -c12- > nsdmqdif.$date$hour
sort -rn bigqdif.$date$hour | cut -c14- > nsdmqdif.$date$hour
cat nsdmqdif.$date$hour >> bigq.$date$hour

echo "LOWTEMP OUTPUT FOR RUN $DATE$hour" > Lowtemp$hour
head -1  BIGDIF$hour >> Lowtemp$hour

if test -s lowtemp1
then
   sort +0n -1 -r +1 -2 +3n -4 -r lowtemp1 | cut -c6-130  >> Lowtemp$hour
   lowtemp_print=1
else
   echo "NO DIAGNOSTIC OUTPUT GENERATED FOR THIS RUN." >> Lowtemp$hour
   lowtemp_print=2
fi

if test $LOGNAME = wx12sd -o $LOGNAME = wx12ss 
then
   if test -s BIGDIF$hour
   then
      if [ `cat BIGDIF$hour | wc -l` -ge 8 ]; then
         echo "sdm_listadp: Printing diagnostic output produced for Wind Differences in BIGDIF$hour." >> sdmedit_diagnostic_output 
         echo "Printing BigDif"
         awk '{ print $0 "\r" }' BIGDIF$hour | enscript -d$SDM_PRNTR -hB -f Courier11
      else 
         echo "sdm_listadp: No diagnostic output produced for Upper Air Wind Differences.  Will not print BIGDIF$hour." >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find BIGDIF$hour.  File does not exist.  Exit sdmedit."
      exit
   fi
   if test -s Lowtemp$hour
   then
      if [ $lowtemp_print -eq 1 ]; then
         echo "sdm_listadp: Printing diagnostic output produced for Lowtemp in Lowtemp$hour." >> sdmedit_diagnostic_output
         echo "Printing Lowtemp"
         awk '{ print $0 "\r" }' Lowtemp$hour | enscript -d$SDM_PRNTR -hB -f Courier11
      else 
         echo "sdm_listadp:  No diagnostic output produced for Upper Air Low Temperatures.  Will not print Lowtemp$hour." >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find Lowtemp$hour.  File does not exist.  Exit sdmedit."
      exit
   fi
   if test -s bigq.$date$hour
   then
      bigq_print=`cat bigq.$date$hour | wc -l`
      if [ $bigq_print -gt 5 ]; then
         echo "sdm_listq: Printing diagnotic output produced for Moisture Differeces in bigq.$date$hour" >> sdmedit_diagnostic_output
         echo "Printing Bigq"
         awk '{ print $0 "\r" }' bigq.$date$hour | enscript -d$SDM_PRNTR -hB -f Courier11
      else
         echo "sdm_listq: No diagnostic output prodcued for Moisture Differences in bigq.$date$hour.  Will not print bigq.$date$hour" >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find bigq.$date$hour.  File does not exist.  Exit sdmedit."
      exit
   fi
   if test -s super$date$hour
   then
      superprint=`cat super$date$hour | wc -l`
      if [ $superprint -gt 6 ]; then
         echo "sdm_listsuper:  Printing diagnostic output produced for Super Adiabatic Lapse Rates in super$date$hour" >> sdmedit_diagnostic_output
         echo "Printing Super"
         awk '{ print $0 "\r" }' super$date$hour | enscript -d$SDM_PRNTR -hB -f Courier11
      else
         echo "sdm_listsuper: No diagnostic output produced for Super Adiabatic Lapse Rates.  Will not print super$date$hour." >> sdmedit_diagnostic_output
      fi
   else
      echo "Could not find super$date$hour.  File does not exist.  Exit sdmedit."
      exit
   fi
fi

exec=/nw${envir}/exec/sdm_readqc

export pgm=sdm_readqc
#. prep_step

rm qcfile pointer_file
export XLFUNIT_10=cqc_sdm
export XLFUNIT_51=cqcbufrdata
export XLFUNIT_52=pointer_file
$exec  > outfile

## RUN SDMEDIT_IBM8 CODE FOR SDMEDIT INTERACTIVE QC
SDMparm=/nw${envir}/parm
SDMexec=/nw${envir}/exec/sdm_sdmedit_ibm8 

#cp $prepbufr PREPDA.$CDATE10

export pgm=sdm_sdmedit_ibm8
#. prep_step

export XLFUNIT_3=sdmcom.cur
export XLFUNIT_6=cqcs.out  
export XLFUNIT_8=sgesprep
export XLFUNIT_14=adpupa
export XLFUNIT_15=$prepbufr
export XLFUNIT_20=pointer_file
export XLFUNIT_30=cqcbufrdata
export XLFUNIT_48=$SDMparm/sdmedit.man 
export XLFUNIT_49=$SDMparm/makfmt.data   
export XLFUNIT_70=fort.70
export XLFUNIT_60=subsetfile

## run interactive sdmedit
## for testing... no interactive
$SDMexec | tee -a sdmedit.out