#!/bin/ksh
######################################################################
# SCRIPT: plot_gridded_observations.ksh
#--------------------------------------------------------------------#
# INITIATION SCRIPT:  automated_gridded_2x2deg_surface_obs_production.ksh
# CALLING SCRIPT: produce_gridded_2x2deg_surface_obs.ksh
#--------------------------------------------------------------------#
# PURPOSE: generates categorical monthly and seasonal
#          gridded observation graphics
# USAGE: plot_gridded_observations.ksh ${1} ${2} ${3}
#--------------------------------------------------------------------#
# INPUT FILES:
#    plot_categorical_observations_template.ctl - Grads control file
#       template for input binary 2x2 degree categorical observation
#       data
# OUTPUT FILES:
#    obs_${parm}.${lead}.gif - output GIF image of 2x2 degree 
#       categorical observations over the contiguous united states
# SOURCED FILES:                                                     #
#    ${HOME}/.profile - user's .profile
#--------------------------------------------------------------------#
# SUBROUTINES USED:
#    plot_categorical_grids.gs - Produces categorical gridded observation graphics
# 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
# LOCAL VARIABLES
#    all_ranges_directory - input directory
#    amon - 1st month in valid period
#    archive_directory - output directory (full path)
#    ayear - 1st year in valid period
#    bigmo - full month name (used for grads title for monthly plots)
#    c1 - Grads color for below normal category
#    c2 - Grads color for above normal category
#    cline - flag indicating whether user has correctly entered command
#            line arguments
#    controlfile - Grads control file for input 
#       categorical observation data
#    controlfiletemplate - Grads control file template for input 
#       categorical observation data
#    f1 - 1st year of valid period
#    f3 - 3rd year of valid period
#    glead - filename parameter for observation period
#    gparm - filename parameter for observered parameter
#    gmtype - filename parameter for type of data
#    gtit - full name of type of data
#    l1 - line interval for below normal category
#    l2 - line interval for near normal category
#    l3 - line interval for above normal category
#    lead - code for observation period [01-seasonal;14-monthly]
#    m1 - 1st month of valid period
#    m2 - 2nd month of valid period
#    m3 - 3rd month of valid period
#    mm - center month of valid period
#    month - center year and month of valid period
#    mtit - title for month in grads plot
#    mtype - filename parameter for type of data
#    observation_period - observation period
#    parameter - observed parameter
#    parm - filename parameter for observered parameter
#    ptit - full name of observed parameter
#    recno - record number in input binary observation file
#    startdate - date corresponding to first record if input categorical
#       observations binary file
#    ydate - center date of valid period 
#    yearmo - 1st year and month of valid period
#    ytit - title for year in grads plot
#    yyyy - center year of valid period
#--------------------------------------------------------------------#
# AUTHOR: SCOTT HANDEL
#--------------------------------------------------------------------#
# DATE: APRIL 24, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS:                                                     #
#    20120620(SCH): Added convert command to make GIF image fit
#                   verification web page
#    20130212(SCH): Fixed error in monnthly year plot title
######################################################################
######################################################################
# 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) 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`
            ydate="${yyyy}-${mm}-01"
            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: plot_gridded_observations.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
##################################################################
# MAKE OUTPUT DIRECTORIES
##################################################################
#!!!# archive_directory=${DATA_OUT}/observations/land_air/long_range/conus/gridded_surface/${yyyy}/${mm}
archive_directory="/cpc/home/sparker/lrf/reproc/ob_glbt/output/${yyyy}/${mm}"
if [ ! -d ${archive_directory} ]; then
   mkdir -p ${archive_directory}
fi

#######################################################################
# SET VARIABLES FOR INPUT FILE NAME, GRADS PLOT TITLE, AND GRADS PLOT COLORS
#######################################################################
case ${parameter} in
   T) parm="Temp"
      ptit="Temperature"
      gparm="temp"
      c1="4"
      c2="2" ;;
   P) parm="Prec"
      ptit="Precipitation"
      gparm="prec"
      c1="12"
      c2="3" ;;
esac
#######################################################################
# SET VARIABLES FOR MAP TYPE (in input file name and in grads output
# file name)
#######################################################################
mtype="obs"
gmtype="verif"
#######################################################################
# SET VARIABLE FOR LINE INTERVALS
#######################################################################
l1="-1.5"
l2="-0.5"
l3="0.5"
#######################################################################
# SET VARIABLE FOR GRADS PLOT TITLE
#######################################################################
gtit="Observations"
#######################################################################
# SET CONTROL FILE NAMES
#######################################################################
controlfiletemplate="../library/plot_categorical_observations_template.ctl"
controlfile="../library/plot_categorical_observations.ctl"
#######################################################################
# ESTABLISHE BEGINNING DATE OF ALL FORECASTS
#######################################################################
startdate="199412"
#######################################################################
# ESTABLISH OBSERVATION VALID PERIODS
#######################################################################
f1=`date -d"$ydate -1 month" "+%Y"`
f3=`date -d"$ydate +1 month" "+%Y"`
m1=`date -d"$ydate -1 month" "+%b"`
m2=`date -d"$ydate +0 month" "+%b"`
m3=`date -d"$ydate +1 month" "+%b"`
#######################################################################
# ESTABLISH FIRST MONTH IN OBS PERIOD
#######################################################################
case ${observation_period} in
   monthly) ayear=`date -d"$ydate +0 month" "+%Y"`
            amon=`date -d"$ydate +0 month" "+%m"` ;;
   seasonal) ayear=`date -d"$ydate -1 month" "+%Y"`
             amon=`date -d"$ydate -1 month" "+%m"` ;;
esac
#######################################################################
# GET YEAR AND MONTH OF FIRST MONTH OF OBSERVATION PERIOD
#######################################################################
yearmo=${ayear}${amon}
#######################################################################
# GET RECORD NUMBER FOR OUTLOOK
#######################################################################
#!!!# recno=`./getnummo.ksh $startdate $yearmo`
recno=`ksh getnummo.ksh $startdate $yearmo`

#######################################################################
# ESTABLISH TITLES FOR OBSERVATION VALID PERIOD FOR USE IN GRADS PLOT
#######################################################################
case ${observation_period} in
   seasonal) if [ $f1 -ne $f3 ]; then
                ytit="${f1}-`echo ${f3} | cut -c3-4`"
             else
                ytit=${f1}
             fi
             mtit="${m1}-${m2}-${m3}" ;;
   monthly) ytit=`date -d"$ydate" "+%Y"`
            bigmo=`date -d"$ydate" "+%B"`
            mtit="${bigmo}" ;;
esac
##################################################################
# ESTABLISH ALL RANGES DIRECTORY
##################################################################
#!!!# all_ranges_directory="${DATA_OUT}/observations/land_air/long_range/conus/gridded_surface/all_ranges"
all_ranges_directory="/cpc/home/sparker/lrf/reproc/ob_glbt/output/all_ranges"

##################################################################
# CREATE TEMPORARY GRADS CONTROL FILE ON THE FLY USING A STANDARD
# TEMPLATE
##################################################################
sed "s#INPUT_FILE#${all_ranges_directory}/${gparm}${glead}_${gmtype}.bi#g" ${controlfiletemplate} > ${controlfile}
##################################################################
# PRODUCE OUTPUT GRAPHIC
##################################################################
grads -blc "run ./plot_categorical_grids.gs $recno $mtit $ytit $parm $c1 $c2 $l1 $l2 $l3 $mtype $gtit ${lead} $ptit ${controlfile}"
convert -crop 800x515+0+0 ../output/obs_${parm}.${lead}.gif ../output/obs_${parm}.${lead}.gif
mv ../output/obs_${parm}.${lead}.gif ${archive_directory}
##################################################################
# REMOVE TEMPORARY GRADS CONTROL FILE
##################################################################
rm ${controlfile}
