#!/bin/ksh
######################################################################
# SCRIPT: write_2x2_ascii_output.obs.ksh
# LANGUAGE: Korn Shell
#--------------------------------------------------------------------#
# INITIATION SCRIPT:                                                 #
#    automated_gridded_2x2deg_surface_obs_production.ksh
# CALLING SCRIPT:                                                    #
#    produce_gridded_2x2deg_surface_obs.ksh
#--------------------------------------------------------------------#
# PURPOSE:                                                           #
#    Generates 2x2 gridded ascii categorical observation files
# USAGE:                                                             #
#    write_2x2_ascii_output.ksh ${1} ${2} ${3}
#--------------------------------------------------------------------#
# INPUT FILES:                                                       #
#    ${input_filename_parameter}_${filename_period}_verif.bi
#       - binary file containing 2 degree x 2 degree categorical obs
# OUTPUT FILES:                                                      #
#    ${output_filename_parameter}_${valid_period}_grid2deg.txt 
#       - ascii file containing 2 degree x 2 degree categorical obs
# SOURCED FILES:                                                     #
#    ${HOME}/.profie - user's .profile
#--------------------------------------------------------------------#
# SUBROUTINES USED:                                                  #
#    getnummo.ksh - Calculates number of months between two input dates
# EXECUTABLES USED:                                                  #
#    write_2x2_ascii_output.obs.Linux.exe - Generates 2x2 gridded ascii files
#--------------------------------------------------------------------#
# 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 output data area
# LOCAL VARIABLES:                                                   #
#    all_ranges_directory - input directory
#    center_month - center month and year of observation valid period
#    cline - flag indicating whether command line variables are correct
#    dd - day of observation (dummy varaible always set to 15 so
#         that gnudate backdate/forward-date routine works correctly)
#    filename_period - filename parameter indicating observation period
#    gdate - observation center month date [YYMMDD]
#    input_filename_parameter - filename parameter indicating observation type
#    ll - number of months in valid period
#    mm - center month of observation valid period
#    obs_file - full path of input observation file
#    observation_period - observation period
#    output_file - full path of output file
#    output_filename_parameter - filename parameter indicating observation type
#    qmon - first month of valid period
#    qyear - first year of valid period
#    recno - record number of input binary observation archive file
#    valid_period - length of valid period (in months)
#    yearmo - first year and month of valid period
#    yy - center year of observation valid period
#    yyyy - center year of observation valid period
#--------------------------------------------------------------------#
# AUTHOR: SCOTT HANDEL
#--------------------------------------------------------------------#
# DATE: APRIL 24, 2012
#--------------------------------------------------------------------#
# MODIFICATIONS:                                                     #
######################################################################
######################################################################
# QUALITY CONTROL COMMAND-LINE ARGUMENTS                             #
######################################################################
cline="TRUE"
case $# in
   3) case ${1} in
      T) output_filename_parameter='obs_temp'
         input_filename_parameter='temp' ;;
      P) output_filename_parameter='obs_precip'
         input_filename_parameter='prec' ;;
      *) cline="FALSE" ;;
      esac
      case ${2} in
         monthly) filename_period='mons'
             observation_period=${2}
             cline=${cline} ;;
         seasonal) filename_period='seas' 
            observation_period=${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])
             center_month=${3}
             yyyy=`echo ${3} | cut -c1-4`
             yy=`echo ${3} | cut -c3-4`
             mm=`echo ${3} | cut -c5-6`
             dd=15
             gdate=${yy}${mm}${dd}
             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: write_2x2_ascii_output.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
#######################################################################
# GET RECORD NUMBER
#######################################################################
case ${observation_period} in
   seasonal) ll=0003
       valid_period="03m"
       qyear=`date -d"${yyyy}-${mm}-${dd} -1 month" "+%Y"`
       qmon=`date -d"${yyyy}-${mm}-${dd} -1 month" "+%m"` ;;
   monthly) ll=0001
       valid_period="01m"
       qyear=`date -d"${yyyy}-${mm}-${dd}" "+%Y"`
       qmon=`date -d"${yyyy}-${mm}-${dd}" "+%m"` ;;
esac
yearmo=`echo ${qyear}${qmon}`
#!!!# recno=`./getnummo.ksh 199412 $yearmo`
recno=`ksh getnummo.ksh 199412 $yearmo`

#######################################################################
# SET DIRECTORY OF INPUT BINARY FILES
#######################################################################
#!!!# 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"

#######################################################################
# SET OUTPUT FILENAME
#######################################################################
#!!!# output_file=${DATA_OUT}/observations/land_air/long_range/conus/gridded_surface/${yyyy}/${mm}/${output_filename_parameter}_${valid_period}_grid2deg.txt
output_file="/cpc/home/sparker/lrf/reproc/ob_glbt/output/${yyyy}/${mm}/${output_filename_parameter}_${valid_period}_grid2deg.txt"

#######################################################################
# SET INPUT FILENAMES
#######################################################################
obs_file=${all_ranges_directory}/${input_filename_parameter}_${filename_period}_verif.bi

#######################################################################
# CREATE OUTPUT FILE
#######################################################################
#!!!# if [ ! -d ${DATA_OUT}/observations/land_air/long_range/conus/gridded_surface/${yyyy}/${mm} ]; then
#!!!#   mkdir -p ${DATA_OUT}/observations/land_air/long_range/conus/gridded_surface/${yyyy}/${mm}
#!!!#   chmod 775 ${DATA_OUT}/observations/land_air/long_range/conus/gridded_surface/${yyyy}/${mm}
#!!!# fi
if [ ! -d /cpc/home/sparker/lrf/reproc/ob_glbt/output/${yyyy}/${mm} ]; then
   mkdir -p /cpc/home/sparker/lrf/reproc/ob_glbt/output/${yyyy}/${mm}
   chmod 775 /cpc/home/sparker/lrf/reproc/ob_glbt/output/${yyyy}/${mm}
fi
../build/write_2x2_ascii_output.obs.Linux.exe ${obs_file} ${output_file}.tmp ${gdate} ${ll} ${recno}

########################################################################
# ORDER BY XXYY COORDINATES
########################################################################
head -2 ${output_file}.tmp > ${output_file}
sed '1,2d' ${output_file}.tmp | sort -n >> ${output_file}
rm ${output_file}.tmp
chmod 775 ${output_file}
