#/bin/sh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exgdas_vrfyrad.sh.sms
# Script description:  Runs data extract/validation for global radiance diag data
#
# Author:        Ed Safford       Org: NP23         Date: 2012-01-18
#
# Abstract: This script runs the data extract/validation portion of the 
#           RadMon package.  
#
# Script history log:
# 2012-01-18  Ed Safford
#
#   Input script positional parameters:
#     1             Current analysis date in yyyymmddhh format
#                   defaults to PDY; required
#     2             cycle time in cc format
#                   defaults to cyc; required
#
#   Imported Shell Variables:
#     RAD_AREA      flag for global or regional
#     TANKDIR       repository for radmon data files
#     DATA          data working space
#     FIXgfs        fixed file directory
#     USHgfs        scripts directory
#     PDY           processing day; 
#                   overridden by 1
#     cyc	    processing cycle; 
#                   overridden by 2
#     INISCRIPT     Preprocessing script
#                   defaults to none
#     LOGSCRIPT     Log posting script
#                   defaults to none
#     ERRSCRIPT     Error processing script
#                   defaults to 'eval [[ $err = 0 ]]'
#     ENDSCRIPT     Postprocessing script
#                   defaults to none
#
#   Exported Shell Variables:
#     RAD_AREA      flag for global or regional
#     PDATE	    Processing date
#     MAKE_CTL      Signal to make ctl files, set to 1 (on)
#     MAKE_DATA     Signal to make data files, set to 1 (on)
#     USE_ANL       Signal to use analysis input files, set to 0 (off)
#     USE_MAIL      Signal to send error reports by mail, set to 0 (off)
#     SATYPE        list of satellite/instrument sources to process
#     err           last return code
#     DO_DIAG_RPT   Signal to build the diag report, set to 1 (on)
#     DO_DATA_RPT   Signal to build the data report, set to 1 (on)
#     MAIL_TO       Mail recipients list, set to "" (no recipients)
#     MAIL_CC       Mail cc recipients list, set to "" (no recipients)
#
#   Modules and files referenced:
#     scripts    : $INISCRIPT
#                  $LOGSCRIPT
#                  $ERRSCRIPT 
#                  $ENDSCRIPT
#                  ${USHgfs}/radmon_verf_angle.sh
#                  ${USHgfs}/radmon_verf_bcoef.sh
#                  ${USHgfs}/radmon_verf_bcor.sh
#                  ${USHgfs}/radmon_verf_time.sh
#
#     programs   : $NDATE
#
#     fixed data : $SATANGL
#
#     input data : $biascr
#                  $radstat
#                  $biascr
#
#     output data:  
#
#  Remarks:
#
#    Condition codes
#       0 - no problem encountered
#      >0 - some problem encountered
#
#    Control variable resolution priority
#       1 Command line argument.
#       2 Environment variable.
#       3 Inline default.
#
#  Attributes:
#    Language:  POSIX shell
#    Machine:   IBM SP
#
################################################################################
#  Set environment
export VERBOSE=${VERBOSE:-"NO"} 
if [[ "$VERBOSE" = "YES" ]]
then
   echo start exgdas_vrfyrad.sh.sms
   set -x
fi
export RUN_ENVIR=${RUN_ENVIR:-prod}
export NET=${NET:-gfs}
export RUN=${RUN:-gdas}
export envir=${envir:-prod}

#  Command line arguments
export PDY=${1:-${PDY:?}} 
export cyc=${2:-${cyc:?}}

#  Directories
export DATA=${DATA:-$(pwd)}
export COM_OUT=/com/${NET}/${envir}
export COMOUT=${COMOUT:-$COM_OUT/${RUN}.${PDY}}
export HOMEgfs=${HOMEgfs:-/nw${envir}}
export EXECgfs=${EXECgfs:-$HOMEgfs/exec}
export FIXgfs=${FIXgfs:-$HOMEgfs/fix}
export PARMgfs=${PARMgfs:-$HOMEgfs/parm}
export USHgfs=${USHgfs:-$HOMEgfs/ush}
export TANKverf=${TANKverf:-/com/verf/prod}
export TANKverf_rad=${TANKverf}/radmon.${PDY}

#  Filenames
export biascr=${biascr:-$COMOUT/gdas1.t${cyc}z.abias}
export satang=${satang:-$COMOUT/gdas1.t${cyc}z.satang}
export radstat=${radstat:-$COMOUT/gdas1.t${cyc}z.radstat}
export satype_file=${satype_file:-gdas_radmon_satype.txt}
export INISCRIPT=${INISCRIPT}
export ERRSCRIPT=${ERRSCRIPT:-'eval [[ $err = 0 ]]'}
export LOGSCRIPT=${LOGSCRIPT}
export ENDSCRIPT=${ENDSCRIPT}

#  Other variables
export RAD_AREA=${RAD_AREA:-glb}
export MAKE_CTL=${MAKE_CTL:-1}
export MAKE_DATA=${MAKE_DATA:-1}
export USE_ANL=${USE_ANL:-0}
export PDATE=${PDY}${cyc}
export DO_DIAG_RPT=${DO_DIAG_RPT:-1}
export DO_DATA_RPT=${DO_DATA_RPT:-1}
export USE_MAIL=${USE_MAIL:-0}
export MAIL_TO=${MAIL_TO:-" "}
export MAIL_CC=${MAIL_CC:-" "}
export NCP=${NCP:-/bin/cp}
export NDATE=${NDATE:-/nwprod/util/exec/ndate}
export pgm=exgdas_vrfyrad.sh.sms

$LOGSCRIPT

#####################################################################
# Preprocessing
$INISCRIPT

if [[ ! -d ${DATA} ]]; then
   mkdir $DATA
fi
cd $DATA

if [[ ! -d ${TANKverf_rad} ]]; then
   mkdir -p $TANKverf_rad
fi

if [[ "$VERBOSE" = "YES" ]]; then
   if [[ -s ${radstat} ]]; then
      echo ${radstat} is available
   fi
   if [[ -s ${satang} ]]; then
      echo ${satang} is available
   fi
   if [[ -s ${biascr} ]]; then
      echo ${biascr} is available
   fi
fi
#####################################################################

data_available=0
if [[ -s ${radstat} && -s ${satang} && -s ${biascr} ]]; then
   data_available=1                                         

   #------------------------------------------------------------------
   #  Copy data files file to local data directory.  
   #  Untar radstat file.  
   #------------------------------------------------------------------

   cd ${DATA}
   $NCP $biascr  ./biascr.$PDATE
   $NCP $satang  ./satang.$PDATE
   $NCP $radstat ./radstat.$PDATE

   tar -xvf radstat.$PDATE
   rm radstat.$PDATE

   #------------------------------------------------------------------
   #  SATYPE is the list of expected satellite/instrument sources
   #  in the radstat file.  It should be stored in the $TANKverf 
   #  directory.  If it isn't there then use the $FIXgfs copy.  In all 
   #  cases write it back out to the radmon.$PDY directory.  Add any
   #  new sources to the list before writing back out.
   #------------------------------------------------------------------

   radstat_satype=`ls -l d*ges* | sed -e 's/_/ /g;s/\./ /' | awk '{ print $10 "_" $11 }'`
   if [[ "$VERBOSE" = "YES" ]]; then
      echo $radstat_satype
   fi


   qdate=`$NDATE -06 $PDATE`
   pday=`echo $qdate | cut -c1-8`

   if [[ ! -s ${TANKverf}/radmon.${pday}/${satype_file} ]]; then
      if [[ ! -s ${FIXgfs}/${satype_file} ]]; then 
         export SATYPE=$radstat_satype
         if [[ "$VERBOSE" = "YES" ]]; then
            echo " ${satype_file} not found.  Adding it now using radstat file contents."
         fi
      else
         export SATYPE=`cat ${FIXgfs}/${satype_file}`
      fi
   else
      export SATYPE=`cat ${TANKverf}/radmon.${pday}/${satype_file}`
   fi



   #-------------------------------------------------------------
   #  Update the SATYPE if any new sat/instrument was 
   #  found in $radstat_satype.  Write the SATYPE contents back 
   #  to $TANKverf/radmon.$PDY.
   #-------------------------------------------------------------
   satype_changes=0
   new_satype=$SATYPE
   for type in ${radstat_satype}; do
      test=`echo $SATYPE | grep $type | wc -l`

      if [[ $test -eq 0 ]]; then
         if [[ "$VERBOSE" = "YES" ]]; then
            echo "Found $type in radstat file but not in SATYPE list.  Adding it now."
         fi
         new_satype="$new_satype $type"
      fi
   done
    
   if [[ ! -s ${TANKverf}/radmon.${PDY}/${satype_file} ]]; then
      echo $new_satype > ${TANKverf}/radmon.${PDY}/${satype_file}
   fi
    
 
   #------------------------------------------------------------------
   # Rename the diag files and uncompress
   #------------------------------------------------------------------
   for type in ${SATYPE}; do
      mv diag_${type}_ges.${PDATE}.Z  ${type}.Z
      uncompress ./${type}.Z
   done


   #------------------------------------------------------------------
   #   Run the child sccripts.
   #------------------------------------------------------------------
   ${USHgfs}/radmon_verf_angle.sh ${PDATE}
   rc_angle=$?

   ${USHgfs}/radmon_verf_bcoef.sh ${PDATE}
   rc_bcoef=$?

   ${USHgfs}/radmon_verf_bcor.sh ${PDATE}
   rc_bcor=$?

   ${USHgfs}/radmon_verf_time.sh ${PDATE}
   rc_time=$?

fi

#####################################################################
# Postprocessing
$ENDSCRIPT

err=0
if [[ ${data_available} -ne 1 ]]; then
   err=1
elif [[ $rc_angle -ne 0 ]]; then
   err=$rc_angle
elif [[ $rc_bcoef -ne 0 ]]; then
   err=$rc_bcoef
elif [[ $rc_bcor -ne 0 ]]; then
   err=$rc_bcor
elif [[ $rc_time -ne 0 ]]; then
   err=$rc_time
fi

if [[ "$VERBOSE" = "YES" ]]; then
   echo "end exgdas_vrfyrad.sh.sms, exit value = ${err}"
fi

if [[ ! "$RUN_ENVIR" = "prod" ]]; then
   cd $DATA
   cd ..
   rm -rf $DATA
fi

if [[ ${err} -ne 0 ]]; then
   export err
   $ERRSCRIPT||exit ${err} 
fi

set +x
exit ${err}

