#!/bin/ksh
######################################################################
# SCRIPT: produce_heidke_and_rpss.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: Produces heidke and ranked probability skill scores
# USAGE: produce_heidke_and_rpss.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
# OUTPUT FILES:
#    ${parameter_file_name}${lead}.heidke.txt - ascii text file containing
#        heidke skill scores
#    ${parameter_file_name}${lead}.rpss.txt - ascii text file containing
#        ranked probability skill scores
#    ${heidke_output_file}.err - file containing details on errors 
#        encountered when producing heidke skill scores (if applicable)
#    ${rpss_output_file}.err - file containing details on errors
#        encountered when producing ranked probability skill scores
#        (if applicable)
# SOURCED FILES:
#    ${HOME}/.profile
#--------------------------------------------------------------------#
# SUBROUTINES USED: 
#    getnummo.ksh - Calculates number of months between two input dates
# EXECUTABLES USED: 
#    heidke.Linux.exe - Calculates heidke skill scores
#    rpss.Linux.exe - Calculates ranked probability skill scores
#--------------------------------------------------------------------#
# 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 - Compute farm centralized data input area
#    DATA_OUT - Compute farm centralized data output area
# LOCAL VARIABLES:
#    amon - First month in valid period [MM format]
#    cline - Flag indicating whether user has correctly entered command
#            line arguments
#    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_file_name - Filename of Binary file containing
#       gridded categorical forecast values (full path)
#    forecast_normal_file_name - Filename of Binary file containing
#       gridded probabilistic near normal probability values
#    heidke - Heidke Skill Scores
#    heidke_output_file - output heidke skill score file
#    i - number of months in valid period
#    input_fcst_directory - Directory where input forecast files reside
#    input_obs_directory - Directory where input observation files reside
#    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
#    obsmonth - Month After Valid Period [YYYYMM format]
#    output_directory - Output directory for skill score files (full path)
#    parameter - forecast parameter [T=temperature;P=precipitation]
#    parameter_file_name - portion of input forecast binary file name
#       corresponding to forecast parameter
#    qyear - Year corresponding to first month in valid period [YYYY format]
#    recno - record number corresponding to current valid period being scored
#    rpss - Ranked Probability Skill Scores
#    rpss_output_file - Output ranked probability skill score file
#    ydate - Month After Valid Period [YYYY-MM-DD format] (DD set to 02
#            for gnudate manipulation purposes)
#    yearmo - First month in valid period [YYYYMM format]
#    yyyy - Year corresponding with 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: produce_heidke_and_rpss.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
##################################################################
# SET DATE VARIABLES
##################################################################
yyyy=`echo ${obsmonth} | cut -c1-4`
mm=`echo ${obsmonth} | cut -c5-6`
ydate="${yyyy}-${mm}-02"
##################################################################
# 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" ;;
P) parameter_file_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"
##################################################################
# ESTABLISH OUTPUT DIRECTORY. MAKE OUTPUT DIRECTORY IF IT DOESNT EXIST
##################################################################
#!!!# output_directory="${DATA_OUT}/verification/land_air/long_range/conus/gridded_verifications"
output_directory="/cpc/home/sparker/lrf/reproc/vf_glbt/output"

if [ ! -d ${output_directory} ]; then
   mkdir -p ${output_directory}
   chmod 775 ${output_directory}
fi
##################################################################
# ESTABLISH OUTPUT HEIDKE AND RPSS FILE NAMES
##################################################################
heidke_output_file="${output_directory}/${parameter_file_name}${lead}.heidke.txt"
rpss_output_file="${output_directory}/${parameter_file_name}${lead}.rpss.txt"
##################################################################
# SET DATE VARIABLES
##################################################################
case ${lead} in
01) i="3" ;;
14|15) i="1" ;;
esac
yrmo=`date -d"$ydate -1 month" "+%Y%m"`
qyear=`date -d"$ydate -${i} month" "+%Y"`
amon=`date -d"$ydate -${i} month" "+%m"`
yearmo=`echo ${qyear}${amon}`
##################################################################
# GET RECORD NUMBER
##################################################################
#!!!# recno=`./getnummo.ksh 199412 $yearmo`
recno=`ksh getnummo.ksh 199412 $yearmo`

##################################################################
# EXIT IF REQUESTED VERIFICATION MONTH IS BEFORE THE BEGINNING
# OF THE RECORD
##################################################################
if [ ${recno} -lt 1 ]; then
   echo "Requested verification month is before the beinning of the record"
   echo "Script Terminating"
   exit 1
fi
##################################################################
# PRODUCE HEIDKE SKILL SCORES
##################################################################
if [ -f ${heidke_output_file}.err ]; then
   if [ `stat -c %s ${heidke_output_file}.err` -gt '20000' ]; then
     rm ${heidke_output_file}.err
   fi
fi
heidke=`../build/heidke.Linux.exe $recno ${forecast_file_name} ${observation_file_name} H`
case $? in
   0) if [ -f ${heidke_output_file} ]; then
         sed "/${yearmo}/d" ${heidke_output_file} > ${heidke_output_file}.tmp
      fi
      echo "${yearmo}01${heidke}" >> ${heidke_output_file}.tmp
	echo " "
	echo "******Added*****" $yearmo $heidke "to::" ${parameter_file_name}${lead}.heidke.txt "(fc valid: "$yearmo-$yrmo")"
	echo " "
      sort -n ${heidke_output_file}.tmp > ${heidke_output_file}
      if [ `grep "Target_Month" ${heidke_output_file} | wc -l` -eq 0 ]; then
         sed '1i\ 1st_Target_Month Non-EC All Coverage' ${heidke_output_file} > ${heidke_output_file}.head
         mv ${heidke_output_file}.head ${heidke_output_file}
      fi
      rm ${heidke_output_file}.tmp ;;
   *) echo "${yearmo} Error in heidke executable run at: `date`" >> ${heidke_output_file}.err ;;
esac
##################################################################
# PRODUCE RPSS
##################################################################
rpss=`../build/rpss.Linux.exe $recno ${forecast_below_file_name} ${forecast_above_file_name} ${observation_file_name} H`
case $? in
   0) if [ -f ${rpss_output_file} ]; then
         sed "/${yearmo}/d" ${rpss_output_file} > ${rpss_output_file}.tmp
      fi
      echo "     ${yearmo}      ${rpss}" >> ${rpss_output_file}.tmp
      sort -n ${rpss_output_file}.tmp > ${rpss_output_file}
      if [ `grep "First" ${rpss_output_file} | wc -l` -eq 0 ]; then
         sed '1i\First_Target_Month   RPSS   # Of Valid Grid Points' ${rpss_output_file} > ${rpss_output_file}.head
         mv ${rpss_output_file}.head ${rpss_output_file}
      fi
      rm ${rpss_output_file}.tmp ;;
   *) echo "${yearmo} Error in rpss executable run at: `date`" >> ${rpss_output_file}.err ;;
esac
