#!/bin/ksh
######################################################################
# SCRIPT: quality_control_input_data.ksh
# LANGUAGE: Korn Shell                                               #
# MACHINE: CF
#--------------------------------------------------------------------#
# INITIATION SCRIPT: automated_gridded_2x2deg_surface_obs_production.ksh
# CALLING SCRIPT: produce_gridded_2x2deg_surface_obs.ksh
#--------------------------------------------------------------------#
# PURPOSE: Detects missing or zero length input data. Sends error 
#          message if any missing or zero length input files are 
#          detected
# USAGE: quality_control_input_data.ksh ${1} ${2} ${3}
#--------------------------------------------------------------------#
# INPUT FILES:
#    ${current_year}${current_month}${current_day}.tmax_nohads_ll -
#       Daily 0.5 degree x 0.5 degree maximum temperature observation
#       grids [130W to 60W Longitude; 20N to 55N Latitude]. Units 
#       are degrees Celcius.
#    ${current_year}${current_month}${current_day}.tmin_nohads_ll -
#       Daily 0.5 degree x 0.5 degree minimom temperature observation
#       grids [130W to 60W Longitude; 20N to 55N Latitude]. Units 
#       are degrees Celcius.
#    PRCP_CU_GAUGE_V1.0CONUS_0.25deg.lnx.${current_year}${current_month}${current_day}.RT -
#       Daily 0.25 degree x 0.25 degree precipitation observation
#       grids [129.875W to 55.125W Longitude; 20.125N to 49.875N Latitude].
#       Units are 0.1mm/day. 
# OUTPUT FILES: 
#    input_file_error.txt - Input file error message that is generated
#       in the event that input files are missing or have zero length
# SOURCED FILES:                                                     #
#    ${HOME}/.profile - User's .profile
#--------------------------------------------------------------------#
# SUBROUTINES USED: NONE
# EXECUTABLES USED: NONE
#--------------------------------------------------------------------#
# COMMAND-LINE VARIABLES:
#    ${1}= Observation Parameter [T|P]
#    ${2}= Observation Period [monthly|seasonal]
#    ${3}= Observation Center Month [YYYYMM format]
# GLOBAL VARIABLES:
#    DATA_IN - compute farm data area
# LOCAL VARIABLES:
#    beginning_date - beginning date of observation period
#    center_month - center year and month of observation period
#    cline - Flag indicating whether user has correctly entered 
#            command line arguments
#    current_date - current observation date
#    current_day - day of current observation date
#    current_month - month of current observation date
#    current_year - year of current observation date
#    d - day of beginning observing date
#    file - input file name
#    ending_date - ending date of observation period
#    i - counter for current observation date
#    input_directory - input file directory
#    input_file1 - first input file name
#    input_file2 - second input file name
#    input_files - array containing input file names
#    m - month of beginning observing date
#    mm - center month of observation period
#    next_month - month after end of observation period
#    parameter - observed parameter 
#    period - observation period
#    process_owner - input data process owner e-mail address
#    y - year of beginning observing date
#    yyyy - center year of observation period
#--------------------------------------------------------------------#
# AUTHOR: SCOTT HANDEL
#--------------------------------------------------------------------#
# DATE: April 23, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS:                                                     #
######################################################################
######################################################################
# QUALITY CONTROL COMMAND-LINE ARGUMENTS AND SET PROCESSING DATE
######################################################################
cline="TRUE"
case $# in
   3) case ${1} in
      T|P) cline=${cline}
           parameter=${1} ;;
      *) cline="FALSE" ;;
      esac
      case ${2} in
         monthly|seasonal) cline=${cline}
                           period=${2} ;;
         *) cline="FALSE" ;;
      esac
      case ${3} in
         [0-9][0-9][0-9][0-9][0-9][0-9]) cline=${cline}
                                         yyyy=`echo ${3} | cut -c1-4`
                                         mm=`echo ${3} | cut -c5-6`
                                         center_month=${3} ;;
          *) 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_input_data.ksh ${1} ${2} ${3}'
   echo "--------------------------------"
   echo '${1}= Observation Parameter [T|P]'
   echo '${2}= Observation Period [monthly|seasonal]'
   echo '${3}= Observation Center Month [YYYYMM format]'
   exit
fi
######################################################################
# REMOVE OLD MAIL ERROR MESSAGE IF IT EXISTS
######################################################################
if [ -f 'input_file_error.txt' ]; then
   rm input_file_error.txt
fi
######################################################################
# SET BEGINNING AND ENDING DATE OF INPUT FILE SEARCH
######################################################################
case ${period} in
   monthly) beginning_date="${yyyy}${mm}01"
            next_month=`date -d"${yyyy}-${mm}-01 +1 month" +"%Y-%m-%d"`
            ending_date=`date -d"${next_month} -1 day" +"%Y%m%d"` ;;
   seasonal) beginning_date=`date -d"${yyyy}-${mm}-01 -1 month" +"%Y%m%d"`
             next_month=`date -d"${yyyy}-${mm}-01 +2 month" +"%Y-%m-%d"`
             ending_date=`date -d"${next_month} -1 day" +"%Y%m%d"` ;;
esac
######################################################################
# LOOP THROUGH INPUT DATES
######################################################################
y=`echo ${beginning_date} | cut -c 1-4`
m=`echo ${beginning_date} | cut -c 5-6`
d=`echo ${beginning_date} | cut -c 7-8`
current_date=${beginning_date}
i=0
while [ ${current_date} -lt ${ending_date} ]
do
   current_date=`date -d"${y}-${m}-${d} ${i}day" +"%Y%m%d"`
   current_year=`date -d"${y}-${m}-${d} ${i}day" +"%Y"`
   current_month=`date -d"${y}-${m}-${d} ${i}day" +"%m"`
   current_day=`date -d"${y}-${m}-${d} ${i}day" +"%d"`
######################################################################
# GET INPUT FILE NAMES DEPENDING ON WHETHER REQUESTED PARAMETER
# IS TEMPERATURE OR PRECIPITATION
######################################################################
   case ${parameter} in
#   T) input_directory="${DATA_IN}/cwlinks/temp/US/us_grid_nohads"
#      input_file1="${current_year}${current_month}${current_day}.tmax_nohads_ll"
#      input_file2="${current_year}${current_month}${current_day}.tmin_nohads_ll"
   T) input_directory="/cpc/home/sparker/lrf/reproc/glbt_dlyts"
      input_file1="glbt_conuscut_tmax.${current_year}${current_month}${current_day}"
      input_file2="glbt_conuscut_tmin.${current_year}${current_month}${current_day}"
      process_owner='wei.shi@noaa.gov'
      set -A input_files ${input_directory}/${input_file1} ${input_directory}/${input_file2} ;;
   P) input_directory="${DATA_IN}/cpcsat/cpcUniGaugeRT/PRODUCTS/GRID/${current_year}/gridCONUS/DEG0.250M"
      input_file1="PRCP_CU_GAUGE_V1.0CONUS_0.25deg.lnx.${current_year}${current_month}${current_day}.RT"
      process_owner='pingping.xie@noaa.gov'
      set -A input_files ${input_directory}/${input_file1} ;;
   esac
######################################################################
# LOOP THROUGH INPUT FILE NAMES
######################################################################
   for file in ${input_files[*]}
   do
######################################################################
# CHECK FOR FILE EXISTANCE OR ZERO LENGTH FILE
######################################################################
      if [ ! -s ${file} ]; then
         echo "${file} does not exist or has zero length" >> input_file_error.txt
      fi
   done
   i=`expr ${i} + 1`
done
######################################################################
# MAIL ERROR MESSAGE IF DATA IS MISSING OR HAS ZERO LENGTH
######################################################################
#!!!# if [ -f 'input_file_error.txt' ]; then
#!!!#   cat input_file_error.txt | mail -s "MISSING LONG RANGE GRIDDED OBS" scott.handel@noaa.gov,david.unger@noaa.gov,${process_owner}
#!!!#   rm input_file_error.txt
#!!!#   exit 1
#!!!# fi
