#!/bin/csh -fx
#
# $Version$
# $Path$
# $Date: 2013/09/30 14:28:42 $
#
# This script runs the SMOKE QA processors.  
#
# Script created by : M. Houyoux, North Carolina 
#                     Supercomputing Center
# Last edited : September, 2000
#
#*********************************************************************

# Create directory for output program logs

setenv OUTLOG $LOGS
if ( ! -e $OUTLOG ) then
   mkdir -p $OUTLOG
   chmod ug+w $OUTLOG
endif

# Initialize exit status
set exitstat = 0

# Make sure that debug mode and debug executable are set
if ( $?DEBUGMODE ) then
   set debugmode = $DEBUGMODE
else
   set debugmode = N
endif

if ( $?DEBUG_EXE ) then
   set debug_exe = $DEBUG_EXE
else
   set debug_exe = dbx
endif

# Ensure that SMK_SOURCE is defined
if ( $?SMK_SOURCE ) then

# SMK_SOURCE is not defined   
else
   echo 'SCRIPT ERROR: Environment variable SMK_SOURCE is not set,'
   echo '              but it is needed to use the qa_run.csh script!'
   set exitstat = 1

endif

# Check if QA_TYPE variable is set
if ( $?QA_TYPE ) then
else

   echo 'SCRIPT ERROR: Environment variable QA_TYPE is not set,'
   echo '              but it is needed to use the qa_run.csh script!'
   echo '              Valid values are "inventory" or "monthly".'
   set exitstat = 1
   
endif

# Abort if already had an error
if ( $exitstat == 1 ) then
   exit( $exitstat )
endif

# Check if QA label is set, and initialize file
if ( $?QA_LABEL ) then

   set ilabl = $FYIOP.$QA_LABEL
   set slabl = $ESCEN.$QA_LABEL.$G_STDATE

else

   set ilabl = $FYIOP
   set slabl = $ESCEN.$G_STDATE

endif

switch ( $QA_TYPE ) 
   case custom:
   case none:
      set qa_type = $QA_TYPE
      breaksw

   case part1:
   case PART1:
      if ( $?RUN_PART1 ) then
         if ( $RUN_PART1 == Y ) then
            set qa_type = inventory
         else
            set qa_type = none
         endif
      else
         set qa_type = none
      endif
      breaksw

   case part2:
   case PART2:
      if ( $?RUN_PART2 ) then
         if ( $RUN_PART2 == Y ) then
            set qa_type = temporal
         else
            set qa_type = none
         endif
      else
         set qa_type = none
      endif
      breaksw

   case part3:
   case PART3:
      if ( $?RUN_PART3 ) then
         if ( $RUN_PART3 == Y ) then
            set qa_type = elevpoint
         else
            set qa_type = none
         endif
      else
         set qa_type = none
      endif
      breaksw

   case part4:
   case PART4:
      if ( $?RUN_PART4 ) then
         if ( $RUN_PART4 == Y ) then
            set qa_type = laypoint
         else
            set qa_type = none
         endif
      else
         set qa_type = none
      endif

   case all:
      set qa_type = inventory
      if ( $?RUN_PART1 ) then
         if ( $RUN_PART1 == Y ) then
            set qa_type = inventory
            breaksw
         else
            set qa_type = none
         endif
      else
         set qa_type = none
         breaksw
      endif
      if ( $?RUN_PART2 ) then
         if ( $RUN_PART2 == Y ) then
            set qa_type = temporal
            breaksw
         else
            set qa_type = none
         endif
      endif
      if ( $?RUN_PART3 ) then
         if ( $RUN_PART3 == Y && $SMK_SOURCE == P ) then
            set qa_type = elevpoint
            breaksw
         else
            set qa_type = none
         endif
      endif
#      if ( $?RUN_PART4 ) then
#         if ( $RUN_PART4 == Y && $SMK_SOURCE == P ) then
#            set qa_type = laypiont
#            breaksw
#         else
#            set qa_type = none
#         endif
#      endif
      breaksw

endsw

# Set up input file for Smkreport depending on settings

switch ( $qa_type ) 

case inventory:
   mkdir -p $REPSTAT/${SRCABBR}
   mkdir -p $REPSTAT/${SRCABBR}/${GRID}

   switch ( $SMK_SOURCE )
   case A:

      setenv REPCONFIG $GE_DAT/smkreport/repconfig.ar.inv.txt #repconfig_area_inv_2016beta_07feb2019_v0.txt
      setenv REPORT1 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.state.$ilabl.rpt
      setenv REPORT2 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.county.$ilabl.rpt
      setenv REPORT3 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.scc.$ilabl.rpt
      setenv REPORT4 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.state_scc.$ilabl.rpt
      setenv REPORT5 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.state.$GRID.$ilabl.rpt
      setenv REPORT6 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.scc.$GRID.$ilabl.rpt
      setenv REPORT7 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.state_scc.$GRID.$ilabl.rpt
      setenv REPORT8 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.state_county_scc_srgcode.$GRID.$ilabl.rpt
      setenv REPORT9 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}_${qa_type}.${month}.state_scc_vocprof.$GRID.$ilabl.rpt
      breaksw
      
   case M:
      setenv REPCONFIG $GE_DAT/smkreport/repconfig/repconfig.$SRCABBR.inv.txt
      setenv REPORT1 $REPSTAT/${SRCABBR}/${GRID}/$SRCABBR.${month}.state.$ilabl.rpt
      setenv REPORT2 $REPSTAT/${SRCABBR}/${GRID}/$SRCABBR.${month}.county.$ilabl.rpt
      setenv REPORT3 $REPSTAT/${SRCABBR}/${GRID}/$SRCABBR.${month}.scc.$ilabl.rpt
      setenv REPORT4 $REPSTAT/${SRCABBR}/${GRID}/$SRCABBR.${month}.state_scc.$ilabl.rpt
      setenv REPORT5 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}g.${month}.state.$GRID.$ilabl.rpt
      setenv REPORT6 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}g.${month}.scc.$GRID.$ilabl.rpt
      setenv REPORT7 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}g.${month}.state_county_scc_srg.$GRID.$ilabl.rpt
      setenv REPORT8 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}g.${month}.state_county.$GRID.$ilabl.rpt
      breaksw
      
   case P:
      setenv REPCONFIG $GE_DAT/smkreport/repconfig.pt.inv.txt #repconfig_point_inv_2011platform_11aug2014_v0.txt
      setenv REPORT1 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}.state.$ilabl.rpt
      setenv REPORT2 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}.county.$ilabl.rpt
      setenv REPORT3 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}.scc.$ilabl.rpt
      setenv REPORT4 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}.stackparm.$ilabl.rpt
      setenv REPORT5 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}.state_scc.$ilabl.rpt
      setenv REPORT6 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}g.state.$GRID.$ilabl.rpt
      setenv REPORT7 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}g.scc.$GRID.$ilabl.rpt
      setenv REPORT8 $REPSTAT/${SRCABBR}/${GRID}/${SRCABBR}.county_scc_vocprof.$GRID.$ilabl.rpt
      breaksw
      
   endsw
   
   set logabbr = inv.$ilabl
   
   breaksw
   
case temporal:

   mkdir -p $REPSCEN/${SRCABBR}
   mkdir -p $REPSCEN/${SRCABBR}/${GRID}

   switch ( $SMK_SOURCE )
   case A:
      setenv REPCONFIG $GE_DAT/smkreport/repconfig/repconfig.ar.temporal.txt #repconfig_area_temporal_2016beta_07feb2019_v0.txt
      setenv REPORT1  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}_t.state.$slabl.rpt
      setenv REPORT2  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}_gt.state.$slabl.rpt
      setenv REPORT3  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}_gt.hour_state.$GRID.$slabl.rpt
      setenv REPORT4  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}_gt.hour_scc.$GRID.$slabl.rpt
      setenv REPORT5  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}_gts.scc.$GRID.$slabl.rpt
      setenv REPORT6  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}_gts.state.$GRID.$slabl.rpt
      breaksw
      
   case M:
      setenv REPCONFIG $GE_DAT/smkreport/repconfig/repconfig.$SRCABBR.temporal.txt
      setenv REPORT1  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.state.$slabl.rpt
      setenv REPORT2  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.county.$slabl.rpt
      setenv REPORT3  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.scc.$slabl.rpt
      setenv REPORT4  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.state_scc.$slabl.rpt
      setenv REPORT5  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.hour_scc.$slabl.rpt
      setenv REPORT6  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.state_rclas.$slabl.rpt
      setenv REPORT7  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.county_rclas.$slabl.rpt
      setenv REPORT8  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}gt.state.$GRID.$slabl.rpt
      setenv REPORT9  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}ts.scc.$slabl.rpt
      setenv REPORT10 $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}ts.state_scc.$slabl.rpt
      breaksw
      
   case P:
      setenv REPCONFIG $GE_DAT/smkreport/repconfig/repconfig.pt.temporal.txt
      setenv REPORT1  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.state.$slabl.rpt
      setenv REPORT2  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.county.$slabl.rpt
      setenv REPORT3  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.scc.$slabl.rpt
      setenv REPORT4  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.state_scc.$slabl.rpt
      setenv REPORT5  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.hour_scc.$slabl.rpt
      setenv REPORT6  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}gt.state.$GRID.$slabl.rpt
      setenv REPORT7  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}gt.source.$GRID.$slabl.rpt
      setenv REPORT8  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}ts.scc.$slabl.rpt
      setenv REPORT9  $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}ts.state_scc.$slabl.rpt
      breaksw
      
   endsw
   
   set logabbr = temporal.$slabl
   
   breaksw

case elevpoint:
   setenv REPCONFIG $GE_DAT/smkreport/repconfig/repconfig.pt.elev.txt
   setenv REPORT1  $REPSTAT/${SRCABBR}/${GRID}/$SRCABBR.src_elev.$ilabl.rpt

   set logabbr = elevpoint.$slabl
   
   breaksw

case laypoint:
   setenv REPCONFIG $INVDIR/other/repconfig.$SRCABBR.lfrac.txt
   setenv REPORT1   $REPSCEN/${SRCABBR}/${GRID}/${SRCABBR}t.state_layers.$ilabl.rpt

   set logabbr = laypoint.$slabl
   
   breaksw

case custom:
   set logabbr = custom.$ilabl
   breaksw

case none:
   echo 'SCRIPT NOTE: exiting qa_run.csh without running QA.'
   echo "             QA_TYPE  = "$QA_TYPE
   echo "             RUN_PART1= "$RUN_PART1
   echo "             RUN_PART2= "$RUN_PART2
   echo "             RUN_PART3= "$RUN_PART3
   echo "             RUN_PART4= "$RUN_PART4
   exit( 0 )

default
   echo 'SCRIPT ERROR: Environment variable QA_TYPE is set to'
   echo '              an unknown setting.  Valid values are'
   echo '              "inventory", "temporal", "elevpoint", or "laypoint".'
   set exitstat = 1
   breaksw
      
endsw

# Abort if already had an error
if ( $exitstat == 1 ) then
   exit( $exitstat )
endif
  
#
### Smkreport processing for area, mobile, or point sources
#
set debugexestat = 0
set exestat = 0
setenv TMPLOG   $OUTLOG/smkreport.$SRCABBR.$logabbr.log
if ( $?RUN_SMKREPORT ) then
   if ( $RUN_SMKREPORT == Y ) then

      if( $?MTMP_OUTPUT_YN ) then
      else
          setenv MTMP_OUTPUT_YN N
      endif

      if( $SMK_SOURCE == M && $MTMP_OUTPUT_YN == Y ) then
          unsetenv MTMP
          setenv MTMP $MTMP_INV
      endif

      if ( -e $TMPLOG ) then
	 source $SCRIPTS/run/movelog.csh
      endif

      if ( $exitstat == 0 ) then         # Run program
         setenv LOGFILE $TMPLOG
         if ( $debugmode == Y ) then
            if ( -e $QA_SRC/smkreport.debug ) then
               $debug_exe $QA_SRC/smkreport.debug
            else
                set debugexestat = 1
            endif
         else
            if ( -e $SMK_BIN/smkreport ) then
               time $SMK_BIN/smkreport
            else
               set exestat = 1 
            endif
         endif
      endif

      if ( -e $SCRIPTS/fort.99 ) then
         mv $LOGFILE $LOGFILE.tmp
         cat $LOGFILE.tmp $SCRIPTS/fort.99 > $LOGFILE
         /bin/rm -rf $LOGFILE.tmp
         /bin/rm -rf $SCRIPTS/fort.99
      endif

      if ( $exestat == 1 ) then
	 echo 'SCRIPT ERROR: smkreport program does not exist in:'
	 echo '              '$SMK_BIN
         set exitstat = 1
      endif

      if ( $debugexestat == 1 ) then
	 echo 'SCRIPT ERROR: smkreport.debug program does not exist in:'
	 echo '              '$QA_SRC
         set exitstat = 1
      endif

   endif
endif

#
## Ending of script with exit status
#
exit( $exitstat )

