#!/bin/ksh
######################################################################
# SCRIPT: produce_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: automated_long_range_verifications.ksh
#--------------------------------------------------------------------#
# PURPOSE: produces verification statistics for a single 
#          forecast/observation pair 
#                                                                    #
# USAGE: produce_long_range_verifications.ksh ${1} ${2} ${3}'
#       --------------------------------
#       ${1}= Verification Parameter [T=Temperature;P=Precipitation]
#       ${2}= Forecast Lead [01=Lead 01 Seasonal;14=Monthly;15=Revised Monthly]
#       ${3}= Month After Valid Period [YYYYMM format]
#--------------------------------------------------------------------#
# INPUT FILES:  NONE (input files read in subscripts)
#                                                                    #
# OUTPUT FILES: NONE (output files produced in subscripts)
#                                                                    #
# SOURCED FILES: ${HOME}/.profile
#--------------------------------------------------------------------#
# SUBROUTINES USED:                                                  #
#    produce_heidke_and_rpss.ksh - Produces heidke and ranked 
#       probability skill scores
#    quality_control_long_range_verifications.ksh - Quality controls
#       all input files by checking for file existance and modification
#       dates. Sets error exit flags based on type of input file 
#       error encountered. Produces error message files if input file
#       errors are found.
#    transmit_long_range_verifications.ksh - Transmits verification
#       ascii files, forecast binary data files, and gridded forecast
#       graphics to the public web site for use by the public gridded
#       seasonal and monthly web pages
# EXECUTABLES USED:                                                  #
#    NONE
#--------------------------------------------------------------------#
# COMMAND-LINE VARIABLES:                                            #
#    ${1}= Verification Parameter [T=Temperature;P=Precipitation]
#    ${2}= Forecast Lead [01=Lead 01 Seasonal;14=Monthly;15=Revised Monthly]
#    ${3}= Month After Valid Period [YYYYMM format]
# GLOBAL VARIABLES:                                                  #
#    LLF_GRIDDED_VERIFICATION - Process home directory location
# LOCAL VARIABLES:                                                   #
#    cline - Flag indicating whether user has correctly entered 
#            command line arguments
#    current_month - Current month
#    first_observation_month - First possible observation month in
#           record (defined as the month after valid period ends)
#    lead - Forecast lead [01-Seasonal;14-Monthly;15-Revised Monthly]
#    obsmonth - Month After Valid Period [YYYYMM format]
#    parameter - Forecast parameter [T=temperature;P=precipitation]
#    quality_control_flag - Flag indicating whether input files 
#           passed quality control:
#           0 - Everything is OK
#           1 - Missing or out of date input file(s)
#           2 - Out of date input file(s) 
#--------------------------------------------------------------------#
# AUTHOR: Scott Handel
#--------------------------------------------------------------------#
# DATE: Nov 8, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS:                                                     #
######################################################################
######################################################################
# QUALITY CONTROL COMMAND-LINE ARGUMENTS AND SET PROCESSING DATE
# (DEFAULT DATE IS THE CURRENT MONTH)
######################################################################
cline="TRUE"
case $# in
    3) case ${1} in
       T|P)
          parameter="${1}"
          cline=${cline} ;;
       *) cline="FALSE" ;;
       esac
       case ${2} in
       01|14|15)
          lead="${2}"
          cline=${cline} ;;
       *) cline="FALSE" ;;
       esac
       case ${3} in
       19[0-9][0-9][0-1][0-9]|2[0-9][0-9][0-9][0-1][0-9])
          obsmonth="${3}"
          cline=${cline} ;;
       *) cline="FALSE" ;;
       esac ;;
    *) cline="FALSE"
esac
######################################################################
# IF ANY OF COMMAND LINE ARGUMENT CONDITIONS ARE NOT MET, ALERT USER #
# OF PROPER USAGE                                                    #
######################################################################
if [ ${cline} = "FALSE" ]; then
   echo 'USAGE: produce_long_range_verifications.ksh ${1} ${2} ${3}'
   echo "--------------------------------"
   echo '${1}= Verification Parameter [T=Temperature;P=Precipitation]'
   echo '${2}= Forecast Lead [01=Lead 01 Seasonal;14=Monthly;15=Revised Monthly]'
   echo '${3}= Month After Valid Period [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/reproc/vf_glbt/scripts

##################################################################
# QUALITY CONTROL INPUT FILES
##################################################################
#!!!# ./quality_control_long_range_verifications.ksh ${parameter} ${lead} ${obsmonth}
ksh quality_control_long_range_verifications.ksh

quality_control_flag="$?"
if [ -f gridded_long_range_verifications.${lead}${parameter}.err ]; then
   cat gridded_long_range_verifications.${lead}${parameter}.err
fi
##################################################################
# PRODUCE HEIDKE AND RPSS SKILL SCORES
##################################################################
case ${quality_control_flag} in
#!!!# 0|2) ./produce_heidke_and_rpss.ksh ${parameter} ${lead} ${obsmonth} ;;
0|2) ksh produce_heidke_and_rpss.ksh ${parameter} ${lead} ${obsmonth} ;;
*) exit ${quality_control_flag} ;;
esac
##################################################################
# TRANSMIT FILES TO THE WEB
##################################################################
#!!!# ./transmit_long_range_verifications.ksh ${parameter} ${lead} ${obsmonth}
if [ ${quality_control_flag} -ne '0' ]; then
   exit ${quality_control_flag} 
fi
##################################################################
# REMOVE ANY OLD ERROR FLAGS IF EVERYTHING PROCESSED OK
##################################################################
if [ ${quality_control_flag} -eq '0' ]; then
   if [ -f gridded_long_range_verifications.${lead}${parameter}.err ]; then
      rm gridded_long_range_verifications.${lead}${parameter}.err
   fi
fi
