#!/bin/ksh
######################################################################
# SCRIPT: produce_gridded_2x2deg_surface_obs.ksh
# LANGUAGE: Korn Shell                                               #
# MACHINE: CF
#--------------------------------------------------------------------#
# INITIATION SCRIPT: automated_gridded_2x2deg_surface_obs_production.ksh
# CALLING SCRIPT: automated_gridded_2x2deg_surface_obs_production.ksh
#--------------------------------------------------------------------#
# PURPOSE: Produces individual one month or three month gridded
#          2 degree x 2 degree categorical surface temperature or
#          precipitation data and images based on the command line
#          arguments that are entered by the user.
# USAGE: produce_gridded_2x2deg_surface_obs.ksh ${1} ${2} ${3}
#--------------------------------------------------------------------#
# INPUT FILES: NONE
# OUTPUT FILES: NONE
# SOURCED FILES:                                                     #
#    ${HOME}/.profile - User's .profile
#--------------------------------------------------------------------#
# SUBROUTINES USED:                                                  #
#    create_observation_archive_files.ksh - 1) Converts input 
#       observations to 2 degree x 2 degree grid. 2) Produces binary
#       archive files of all observations from 1995 to the present
#    plot_gridded_observations.ksh - Generates GIF images of 
#       one and three month gridded surface temperature and 
#       precipitation observations
#    quality_control_input_data.ksh - Detects missing or zero length
#       input data
#    transmit_long_range_gridded_obs.ksh - Transmits 2x2 gridded 
#       observations (binary files and GIF images) to the web
#    write_2x2_ascii_output.obs.ksh - Generates 2x2 gridded ascii 
#       files of one and three month gridded observations
# EXECUTABLES USED:                                                  #
#    NONE
#--------------------------------------------------------------------#
# COMMAND-LINE VARIABLES:                                            #
#    ${1}= Observation Parameter [T|P]
#    ${2}= Observation Period [monthly|seasonal]
#    ${3}= Observation Center Month [YYYYMM format]
# GLOBAL VARIABLES:                                                  #
#    GRIDDED_LONG_LEAD_SURFACE_OBS - driver script directory
# LOCAL VARIABLES:                                                   #
#    center_month - center month of valid period
#    cline - Flag indicating whether user has correctly entered 
#            command line arguments
#    parameter - observed parameter 
#    period - observation period
#--------------------------------------------------------------------#
# AUTHOR: SCOTT HANDEL
#--------------------------------------------------------------------#
# DATE: April 23, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS:                                                     #
######################################################################
######################################################################
# QUALITY CONTROL COMMAND-LINE ARGUMENTS
######################################################################
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}
                                         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: produce_gridded_2x2deg_surface_obs.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
##################################################################
# CHANGE TO PROPER DIRECTORY
##################################################################
#!!!# cd ${GRIDDED_LONG_LEAD_SURFACE_OBS}/scripts
cd /cpc/home/sparker/lrf/reproc/ob_glbt/scripts

##################################################################
# QUALITY CONTROL INPUT DATA
##################################################################
#!!!# ./quality_control_input_data.ksh ${parameter} ${period} ${center_month}
ksh quality_control_input_data.ksh ${parameter} ${period} ${center_month}
if [ $? -ne 0 ]; then
   echo "Input data was missing or had zero length. See mail message for more details."
   exit
fi

##################################################################
# GENERATE ALL OBSERVATION GRID FILES (BINARY AND ASCII) AND
# CHECK FOR ERROR CONDITIONS
##################################################################
#!!!# ./create_observation_archive_files.ksh ${parameter} ${period} ${center_month}
ksh create_observation_archive_files.ksh ${parameter} ${period} ${center_month}
if [ $? -ne 0 ]; then
   echo "There was a problem generating the observation archive file"
   exit
fi

##################################################################
# GENERATE ASCII OBSERVATION OUTPUT
##################################################################
#!!!# ./write_2x2_ascii_output.obs.ksh ${parameter} ${period} ${center_month}
ksh write_2x2_ascii_output.obs.ksh ${parameter} ${period} ${center_month}

##################################################################
# PLOT LONG RANGE VERIFICATIONS 
##################################################################
#!!!# ./plot_gridded_observations.ksh  ${parameter} ${period} ${center_month}
ksh plot_gridded_observations.ksh  ${parameter} ${period} ${center_month}

##################################################################
# TRANSMIT FILES TO THE WEB
##################################################################
#!!!# ./transmit_long_range_gridded_obs.ksh ${parameter} ${period} ${center_month}
