#!/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: produce_long_range_verifications.ksh
#--------------------------------------------------------------------#
# PURPOSE: 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. The following
#       exit conditions are returned from this script:
#          0 - Everything is OK
#          1 - Missing or out of date input file(s). Verification
#              statistics cannot be computed
#          2 - Out of date input file(s). Verification statistics 
#              can still be computed
# USAGE: quality_control_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:  
#    ${parameter_file_name}${lead}_forecast.bi - Binary file containing
#        gridded categorical forecast values
#    ${parameter_file_name}${lead}_forecast_below.bi - Binary file 
#        containing gridded probabilistic below normal probability values
#    ${parameter_file_name}${lead}_forecast_normal.bi - Binary file
#        containing gridded probabilistic near normal probability values
#    ${parameter_file_name}${lead}_forecast_above.bi - Binary file
#        containing gridded probabilistic above normal probability values
#    ${parameter_file_name}_${period_file_name}_verif.bi - Binary file
#        containing gridded observation categories
#    fcst_${parameter_image_name}.${lead}.gif - latest 2x2 gridded 
#        categorical forecast image
# OUTPUT FILES: 
#    gridded_long_range_verifications.${lead}${parameter}.err - Text
#        file containing details on input file error(s) encountered.
#        Note if no input files contain errors, no error output
#        file is produced
# SOURCED FILES:
#    ${HOME}/.profile
#--------------------------------------------------------------------#
# SUBROUTINES USED: NONE
# 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:
#    DATA_IN - centralized compute farm data area
# LOCAL VARIABLES:
#    binary_month - first month of valid period [YYYYMM format]
#    center_month - forecast center month [MM format]
#    center_year - forecast center year [YYYY format]
#    cline - flag indicating whether user has correctly entered 
#            command line arguments
#    current_month - current month
#    input_fcst_directory - input forecast directory
#    input_obs_directory - input obs directory
#    file - array of input file names
#    forecast_above_file_name - Filename of Binary file containing 
#       gridded probabilistic above normal probability values
#    forecast_below_file_name - Filename of Binary file containing 
#       gridded probabilistic below normal probability values
#    forecast_filedate - Last modification date of input forecast files
#    forecast_file_name - Filename of Binary file containing
#       gridded categorical forecast values (full path)
#    forecast_image_name - Filename of latest 2x2 gridded categorical
#       forecast image
#    forecast_month - forecast issuance month [MM format]
#    forecast_normal_file_name - Filename of Binary file containing 
#       gridded probabilistic near normal probability values
#    forecast_year - forecast issuance year [YYYY format]
#    lead - forecast lead [01-seasonal;14-monthly;15-revised monthly]
#    mm - Month After Valid Period [MM format]
#    observation_file_name - Filename of Binary file containing
#       gridded observation categories
#    obs_filedate - File date of binary file containing gridded observation 
#       categories
#    obsmonth - Month After Valid Period [YYYYMM format]
#    parameter - forecast paramter [T=temperature;P=precipitation]
#    parameter_file_name - portion of input forecast binary file name 
#       corresponding to forecast parameter
#    parameter_image_name - portion of input forecast image file name 
#       corresponding to forecast parameter
#    period_file_name - portion of input forecast binary file name 
#       corresponding to forecast valid period
#    update_month - month when input forecast files should have 
#       been updated [YYYYMM format]
#    yyyy - 4 digit year corresponding to the Month After Valid
#           Period [YYYY format]
#--------------------------------------------------------------------#
# AUTHOR: Scott Handel
#--------------------------------------------------------------------#
# DATE: Nov 8, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS: 
######################################################################
##################################################################
# QUALITY CONTROL COMMAND LINE ARGUMENTS
##################################################################
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: quality_control_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
##################################################################
# REMOVE OLD ERROR FILES
##################################################################
if [ -f gridded_long_range_verifications.${lead}${parameter}.err ]; then
   rm gridded_long_range_verifications.${lead}${parameter}.err
fi
##################################################################
# SET DATE VARIABLES
##################################################################
yyyy=`echo ${obsmonth} | cut -c1-4`
mm=`echo ${obsmonth} | cut -c5-6`
forecast_year=`date -d"${yyyy}-${mm}-01 -1 month" +"%Y"`
forecast_month=`date -d"${yyyy}-${mm}-01 -1 month" +"%m"`
case ${lead} in
01) center_year=`date -d"${yyyy}-${mm}-01 +1 month" +"%Y"`
    center_month=`date -d"${yyyy}-${mm}-01 +1 month" +"%m"`
    binary_month=`date -d"${yyyy}-${mm}-01 -4 month" +"%Y%m"` ;;
14|15) center_year=`date -d"${yyyy}-${mm}-01 +0 month" +"%Y"`
       center_month=`date -d"${yyyy}-${mm}-01 +0 month" +"%m"`
       binary_month=`date -d"${yyyy}-${mm}-01 -2 month" +"%Y%m"` ;;
esac
update_month=`date -d"${yyyy}-${mm}-01 -1 month" +"%Y%m"` 
##################################################################
# ESTABLISH INPUT FORECAST AND OBS DIRECTORY NAMES
##################################################################
#!!!# input_fcst_directory="${DATA_IN}/forecasts/land_air/long_range/conus/gridded_manual/all_ranges"
# input_fcst_directory="/cpc/prod_tst/nick.novella/lrf/fc/output/all_ranges"
### FOR reproc_glbt
input_fcst_directory="${DATA_IN}/forecasts/land_air/long_range/conus/gridded_manual/all_ranges"


#!!!# input_obs_directory="${DATA_IN}/observations/land_air/long_range/conus/gridded_surface/all_ranges"
input_obs_directory="/cpc/home/sparker/lrf/reproc/ob_glbt/output/all_ranges"

##################################################################
# ESTABLISH INPUT FORECAST AND OBS FILE NAMES
##################################################################
case ${parameter} in
T) parameter_file_name="temp"
   parameter_image_name="Temp" ;;
P) parameter_file_name="prec"
   parameter_image_name="Prec" ;;
esac
case ${lead} in
01) period_file_name="seas" ;;
14|15) period_file_name="mons" ;;
esac
forecast_file_name="${input_fcst_directory}/${parameter_file_name}${lead}_forecast.bi"
forecast_below_file_name="${input_fcst_directory}/${parameter_file_name}${lead}_forecast_below.bi"
forecast_normal_file_name="${input_fcst_directory}/${parameter_file_name}${lead}_forecast_normal.bi"
forecast_above_file_name="${input_fcst_directory}/${parameter_file_name}${lead}_forecast_above.bi"
observation_file_name="${input_obs_directory}/${parameter_file_name}_${period_file_name}_verif.bi"

#!!!# forecast_image_name="${DATA_IN}/forecasts/land_air/long_range/conus/gridded_manual/${forecast_year}/${forecast_month}/fcst_${parameter_image_name}.${lead}.gif"
forecast_image_name="/cpc/home/sparker/lrf/reproc/fc/output/${forecast_year}/${forecast_month}/fcst_${parameter_image_name}.${lead}.gif"

############################################################################
# CHECK FOR EXISTANCE OF FILES NECESSARY FOR FORECAST VERIFICATION
###########################################################################
for file in ${forecast_file_name} ${forecast_below_file_name} ${forecast_normal_file_name} ${forecast_above_file_name} ${observation_file_name}
do
   if [ ! -s ${file} ]; then
      echo "${file} does not exist" >> gridded_long_range_verifications.${lead}${parameter}.err
      echo "Cannot produce verification scores" >> gridded_long_range_verifications.${lead}${parameter}.err
      exit 1 
   fi
done
############################################################################
# CHECK FOR UPDATE TIMES FOR INPUT FILES NECESSARY FOR FORECAST VERIFICATION
############################################################################
obs_filedate=`date -r ${observation_file_name} +"%Y%m"`
if [ ${obs_filedate} -lt ${obsmonth} ]; then
   echo "${observation_file_name} last updated on ${obs_filedate}" >> gridded_long_range_verifications.${lead}${parameter}.err
   echo "Cannot produce verification scores" >> gridded_long_range_verifications.${lead}${parameter}.err
   exit 1
fi
for file in ${forecast_file_name} ${forecast_below_file_name} ${forecast_normal_file_name} ${forecast_above_file_name}
do
   forecast_filedate=`date -r ${file} +"%Y%m"`
   if [ ${forecast_filedate} -lt ${binary_month} ]; then
      echo "${file} last updated on ${forecast_filedate}" >> gridded_long_range_verifications.${lead}${parameter}.err
      echo "Cannot produce verification scores" >> gridded_long_range_verifications.${lead}${parameter}.err
      exit 1
   elif [ ${forecast_filedate} -lt ${update_month} ]; then
      echo "${file} last updated on ${forecast_filedate}" >> gridded_long_range_verifications.${lead}${parameter}.err
      echo "${file} that is tranmsitted to the public will not be up to date" >> gridded_long_range_verifications.${lead}${parameter}.err
      exit 2
   fi
done
###########################################################################
# CHECK FOR EXISTANCE OF FORECAST IMAGE FILE
###########################################################################
if [ ! -s ${forecast_image_name} ]; then
   echo "${forecast_image_name} does not exist" >> gridded_long_range_verifications.${lead}${parameter}.err
   echo "Cannot tranmsit ${forecast_image_name} to the public" >> gridded_long_range_verifications.${lead}${parameter}.err
   exit 2 
fi
