#!/bin/ksh
######################################################################
# SCRIPT: transmit_long_range_gridded_obs.ksh
# LANGUAGE: Korn Shell                                               #
# MACHINE: CF
#--------------------------------------------------------------------#
# INITIATION SCRIPT: automated_gridded_2x2deg_surface_obs_production.ksh
# CALLING SCRIPT: produce_gridded_2x2deg_surface_obs.ksh
#--------------------------------------------------------------------
# PURPOSE: Transmits 2 degree x 2 degree categorical one month and
#          three month tmperature and precipitation categorical 
#          observation data files and images to the web
# USAGE: transmit_long_range_gridded_obs.ksh ${1} ${2} ${3}
#--------------------------------------------------------------------#
# INPUT FILES: 
#    ${data_parm}${glead}_verif.bi - 2 degree x 2 degree gridded 
#       categorical observations binary file
#    obs_${graphic_parm}.${lead}.gif - 2 degree x 2 degree gridded
#       categorical observations GIF image file
# OUTPUT FILES:
#    ${data_parm}${glead}_verif.bi - 2 degree x 2 degree gridded
#       categorical observations binary file
#    obs_${graphic_parm}.${lead}.${yyyy}${mm}.gif - 2 degree x 2 degree
#       gridded categorical observations GIF image file
# 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_OUT - compute farm data output area
#    WEB_OUT - compute farm web output area
# LOCAL VARIABLES:
#    cline - flag indicating whether command line arguments were 
#            correctly entered by user
#    data_parm - filename parameter for obverved parameter
#    glead - filename parameter for observation period
#    graphic_parm - filename parameter for obverved parameter
#    input_data_directory - input data directory (full path)
#    lead - filename parameter for observation period [01 - seasonal;
#           14 - monthly]
#    mm - center month of valid period
#    month - center year and month of valid period
#    observation_period - observation period 
#    output_web_directory - output web directory (full path)
#    parameter - observed parameter
#    yyyy - center year of valid period
#--------------------------------------------------------------------#
# AUTHOR: SCOTT HANDEL
#--------------------------------------------------------------------#
# DATE: April 23, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS:                                                     #
#    20120905(SCH): Fixed erroneous output file location. Script
#       previously had erroneously created a month subdirectory in
#       the web mirroring output area. Output files should only be
#       placed in an output directory in terms of year only and NOT
#       year and month.
######################################################################
######################################################################
# QUALITY CONTROL COMMAND-LINE ARGUMENTS                             #
######################################################################
cline="TRUE"
case $# in
   3) case ${1} in
      T) cline=${cline}
         data_parm="temp"
         graphic_parm="Temp"
         parameter=${1} ;;
      P) cline=${cline}
         data_parm="prec"
         graphic_parm="Prec"
         parameter=${1} ;;
      *) cline="FALSE" ;;
      esac
      case ${2} in
         monthly) cline=${cline}
                  observation_period=${2}
                  glead="_mons"
                  lead=14 ;;
         seasonal) cline=${cline}
                  observation_period=${2}
                  glead="_seas"
                  lead=01 ;;
         *) 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`
            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: transmit_long_range_gridded_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
#######################################################################
# ESTABLISH INPUT DATA DIRECTORY AND OUTPUT WEB DIRECTORY
#######################################################################
input_data_directory="${DATA_OUT}/observations/land_air/long_range/conus/gridded_surface"
output_web_directory="${WEB_OUT}/products/predictions/long_range/tools/briefing/verification"
#######################################################################
# MAKE OUTPUT WEB DIRECTORIES IF THEY DONT EXIST
#######################################################################
if [ ! -d ${output_web_directory}/${yyyy} ]; then
   mkdir -p ${output_web_directory}/${yyyy}
fi
#######################################################################
# TRANSMIT OBS BINARY FILE
#######################################################################
echo "Transmitting obs binary file ..."
if [ -f ${input_data_directory}/all_ranges/${data_parm}${glead}_verif.bi ]; then
   cp -p ${input_data_directory}/all_ranges/${data_parm}${glead}_verif.bi ${output_web_directory}
else
   echo "${input_data_directory}/all_ranges/${data_parm}${glead}_verif.bi does not exist"
fi
#######################################################################
# TRANSMIT OBS GRAPHIC
#######################################################################
echo "Transmitting obs graphic ..."
if [ -f ${input_data_directory}/${yyyy}/${mm}/obs_${graphic_parm}.${lead}.gif ]; then
   cp -p ${input_data_directory}/${yyyy}/${mm}/obs_${graphic_parm}.${lead}.gif ${output_web_directory}/${yyyy}/obs_${graphic_parm}.${lead}.${yyyy}${mm}.gif
else
   echo "${input_data_directory}/${yyyy}/${mm}/obs_${graphic_parm}.${lead}.gif does not exist"
fi
