#!/bin/ksh
######################################################################
# SCRIPT: automated_long_range_verifications.ksh                     #
#                                                                    #
# LANGUAGE: Korn Shell                                               #
#                                                                    #
# MACHINE: Compute Farm
#                                                                    #
# ACCOUNT: cpcfcst.cf
#--------------------------------------------------------------------#
# INITIATION METHOD: crontab                                         #
#
# INITIATION TIME: 10:00 UTC on the 2nd of each month                #
#                                                                    #
# INITIATION SCRIPT: init_long_range_verifications.sh
#                                                                    #
# CALLING SCRIPT: init_long_range_verifications.sh
#--------------------------------------------------------------------#
# PURPOSE: Driver script for CPC gridded monthly and seasonal        #
#          verifications. 
#                                                                    #
# USAGE: ./automated_long_range_verifications.ksh ${1}
#        where ${1} = month after end of verification period [YYYYMM]
#        Note: Current month is used if no command line argument
#        entered
#--------------------------------------------------------------------#
# INPUT FILES:  NONE (input files read in sub-scripts)
#                                                                    #
# OUTPUT FILES: NONE (output files created in subscripts)
#                                                                    #
# SOURCED FILES: ${HOME}/.profile
#--------------------------------------------------------------------#
# SUBROUTINES USED:                                                  #
#    produce_long_range_verifications.ksh - produces verification
#       statistics for a single forecast/observation pair
# EXECUTABLES USED:                                                  #
#    NONE
#--------------------------------------------------------------------#
# COMMAND-LINE VARIABLES:                                            #
#    ${1} - Month after requested verification period [YYYYMM format]
# GLOBAL VARIABLES:                                                  #
#    LLF_GRIDDED_VERIFICATION - process directlry location
# LOCAL VARIABLES:                                                   #
#    all_done - flag indicating whether all necessary input files 
#               have been found for processing
#    count - counter for input search iterations
#    current_month - current month
#    first_observation_month - first observation month (defined as the
#       month after the verification valid period) of the verification
#       record
#    lead - forecast lead being verified [01-seasonal;14-monthly;15-
#           revised monthly]
#    obsmonth - month after verification valid period
#    parameter - forecast parameter [T=temperature;P=precipitation
#    search_time - Maximum number of hours to search for input files
#    time_left - Number of hours left to search for input files
#    transmission_flag - Flag indicating whether transmission 
#       proceeded without errors
#--------------------------------------------------------------------#
# AUTHOR: SCOTT HANDEL
#--------------------------------------------------------------------#
# DATE: Novermber 6, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS:                                                     #
#    20130115(SCH): Added automatic e-mail generation if script
#       attempts to tranmsit forecast data/image files too soon
######################################################################
######################################################################
# QUALITY CONTROL COMMAND-LINE ARGUMENTS AND SET PROCESSING DATE
# (DEFAULT DATE IS THE CURRENT MONTH)
######################################################################
cline="TRUE"
case $# in
    1) case ${1} in
         19[0-9][0-9][0-1][0-9]|2[0-9][0-9][0-9][0-1][0-9])
            obsmonth=${1}
            cline=${cline} ;;
         *) cline="FALSE" ;;
       esac ;;
    0) obsmonth=`date +"%Y%m"`
       cline=${cline} ;;
    *) cline="FALSE" ;;
esac
######################################################################
# IF ANY OF COMMAND LINE ARGUMENT CONDITIONS ARE NOT MET, ALERT USER #
# OF PROPER USAGE                                                    #
######################################################################
if [ ${cline} = "FALSE" ]; then
   echo 'USAGE: automated_long_range_verifications.ksh ${1}'
   echo "--------------------------------"
   echo '${1}= Current Month [YYYYMM format]'
   exit
fi
##################################################################
# EXIT SCRIPT IF USER ENTERS MONTH GREATER THAN CURRENT MONTH.
# THIS IS IMPORTANT AS WE DO NOT WANT OUR FORECASTS TO BE TRANSMITTED
# BEFORE OUR EMBARGO PERIOD ENDS
#######################################################################
current_month=`date +"%Y%m"`
if [ ${obsmonth} -gt ${current_month} ]; then
   echo "Requested observation month cannot be later than the current month"
   echo "Requested observation month is ${obsmonth} while current month is ${current_month}"
   echo "Script terminating"
   exit 3
fi
##################################################################
# EXIT SCRIPT IF THE USER ENTERS A MONTH BEFORE THE FIRST POSSIBLE
# OBSERVATION MONTH IN THE RECORD
##################################################################
first_observation_month='199502'
if [ ${obsmonth} -lt ${first_observation_month} ]; then
   echo "Requested observation month cannot be earlier than the first possible observation month"
   echo "Requested observation month is ${obsmonth} while first possible observation month is ${first_observation_month}"
   echo "Script terminating"
   exit 3
fi
##################################################################
# CHANGE TO PROPER DIRECTORY
##################################################################
#!!!# cd ${LLF_GRIDDED_VERIFICATION}/scripts
cd /cpc/home/sparker/lrf/rerpoc/vf_glbt/scripts

##################################################################
# SEARCH FOR INPUT FILES FOR 6 HOURS
##################################################################
count=0
search_time="6"
all_done="F"
transmission_flag="F"
while [ ${count} -le ${search_time} -a ${all_done} = 'F' ]
do
   all_done="T"
   transmission_flag="T"
   time_left=`expr ${search_time} - ${count}`
##################################################################
# LOOP THROUGH FORECAST PARAMETER AND LEADS
##################################################################
   for parameter in T P
   do
      for lead in 01 14 15
      do
          if [ ${count} -eq 0 -o -f gridded_long_range_verifications.${lead}${parameter}.err ]; then
#!!!#             ./produce_long_range_verifications.ksh ${parameter} ${lead} ${obsmonth}
                  ksh produce_long_range_verifications.ksh ${parameter} ${lead} ${obsmonth}
          fi
          if [ -f gridded_long_range_verifications.${lead}${parameter}.err ]; then
             all_done="F"
          fi
          if [ -f transmission.${lead}${parameter}.err ]; then
             transmission_flag="F"
          fi
      done
   done
##################################################################
# MAIL ERROR MESSAGE IF INPUT FILES MISSING, EMPTY, OR OUT OF DATE
##################################################################
   if [ ${all_done} = 'F' ]; then
      echo "Script: ${LLF_GRIDDED_VERIFICATION}/scripts/automated_long_range_verifications.ksh" > error_message.txt
      echo "Machine: `hostname`" >> error_message.txt
      echo "Account: `whoami`" >> error_message.txt
      echo "==========================================================" >> error_message.txt
      echo "One or more input files are missing or out of date" >> error_message.txt
      echo "Script will continue to search for ${time_left} more hours or" >> error_message.txt
      echo "until necessary input files are found. Please see below for" >> error_message.txt
      echo "details on the missing and/or outdated input files" >> error_message.txt
      echo "==========================================================" >> error_message.txt
      cat gridded_long_range_verifications.*.err >> error_message.txt
#!!!#      cat error_message.txt | mail -s "ERROR IN GRIDDED LONG LEAD VERIFICATION PRODUCTION" scott.handel@noaa.gov,david.unger@noaa.gov
      rm error_message.txt
###################################################################
# WAIT AN HOUR TO SEE IF THE INPUT FILES BECOME AVAILABLE
###################################################################
      sleep 3600
      count=`expr ${count} + 1`
   fi
done
####################################################################
# MAIL FINAL ERROR MESSAGE
####################################################################
if [ ${all_done} = 'F' ]; then
   echo "Script: ${LLF_GRIDDED_VERIFICATION}/scripts/automated_long_range_verifications.ksh" > error_message.txt
   echo "Machine: `hostname`" >> error_message.txt
   echo "Account: `whoami`" >> error_message.txt
   echo "==========================================================================================" >> error_message.txt
   echo "The long lead verification production job has completed searching for correct input data and these data" >> error_message.txt
   echo "still have not been found within the proscribed seach time of ${search_time} hours." >> error_message.txt
   echo "The upstream data problems will have to be further investigated and the following script will have to" >> error_message.txt
   echo "be manually rerun once the upstream problems have been corrected:" >> error_message.txt
   echo "Script: ${LLF_GRIDDED_VERIFICATION}/scripts/automated_long_range_verifications.ksh" >> error_message.txt
   echo "For more information on the source of the processing errors, please see the message below." >> error_message.txt
   echo "==========================================================================================" >> error_message.txt
   cat gridded_long_range_verifications.*.err >> error_message.txt
   if [ ${transmission_flag} = 'F' ]; then
      echo "==========================================================================================" >> error_message.txt
      echo "Additionally, the long lead verification production job attempted to transmit one or more forecast" >> error_message.txt
      echo "files that were modified this month. As a safeguard against accidentally transmitting official forecast" >> error_message.txt
      echo "files in violation of the transmission embargo period, these files were not transmitted. See below for details" >> error_message.txt
      echo "==========================================================================================" >> error_message.txt
      cat transmission.*.err >> error_message.txt
   fi
#!!!#   cat error_message.txt | mail -s "ERROR IN GRIDDED LONG LEAD VERIFICATION PRODUCTION" scott.handel@noaa.gov,david.unger@noaa.gov
   rm error_message.txt gridded_long_range_verifications.*.err
fi
if [ ${transmission_flag} = 'F' ]; then
   echo "Script: ${LLF_GRIDDED_VERIFICATION}/scripts/automated_long_range_verifications.ksh" > error_message.txt
   echo "Machine: `hostname`" >> error_message.txt
   echo "Account: `whoami`" >> error_message.txt
   echo "==========================================================================================" >> error_message.txt
   echo "The long lead verification production job attempted to transmit one or more forecast" >> error_message.txt
   echo "files that were modified this month. As a safeguard against accidentally transmitting official forecast" >> error_message.txt
   echo "files in violation of the transmission embargo period, these files were not transmitted. See below for details" >> error_message.txt
   echo "==========================================================================================" >> error_message.txt
   cat transmission.*.err >> error_message.txt
#!!!#   cat error_message.txt | mail -s "ERROR IN GRIDDED LONG LEAD VERIFICATION TRANSMISSION" scott.handel@noaa.gov,david.unger@noaa.gov
   rm error_message.txt transmission.*.err
fi
