#!/bin/ksh -x
####################################################################################
# Name of script:    verf_precip_getpptndas.sh.sms
# Purpose of script: This script extracts the precip data from NDAS and outputs it
#                    in the format of $model_$yyyy$mm$dd$hh_$hr1_$hr2 where hr1 and 
#                    hr2 (3-digit) are the beginning and ending time of
#                    the accumulation period.
# Files used: ndas.t[cyc]z.egrdsf
# History:
#
# Usage:  verf_precip_getpptndas.sh.sms $input_card
# Format of the input card:
#  cat > input_card.ndas << EOF
#  ndas
#  ndas_${daym1}12_000_003 /com/nam/prod/ndas.$day/ndas.t00z.egrdsf03.tm12
#  ndas_${daym1}12_003_006 /com/nam/prod/ndas.$day/ndas.t00z.egrdsf03.tm09
#  ndas_${daym1}12_006_009 /com/nam/prod/ndas.$day/ndas.t06z.egrdsf03.tm12
#  ndas_${daym1}12_009_012 /com/nam/prod/ndas.$day/ndas.t06z.egrdsf03.tm09
#  ndas_${daym1}12_012_015 /com/nam/prod/ndas.$day/ndas.t12z.egrdsf03.tm12
#  ndas_${daym1}12_015_018 /com/nam/prod/ndas.$day/ndas.t12z.egrdsf03.tm09
#  ndas_${daym1}12_018_021 /com/nam/prod/ndas.$day/ndas.t18z.egrdsf03.tm12
#  ndas_${daym1}12_021_024 /com/nam/prod/ndas.$day/ndas.t18z.egrdsf03.tm09
#  EOF
######################################################################################
set -x

if [ $# -lt 1 ]
then 
   echo "Invalid Argument"
   echo "Usage: verf_precip_getpptfcst.sh $input_card"
   err_exit
fi
   
INPUT=$1

# Read in model name from the input card:
model=`sed -n 1p $INPUT`

# Read in the two arguments for each EDAS segment: 1 - output suffix;
#                                                  2 - input file

typeset -R3 -Z parm tbl

# if the 'model name' contains the word 'soil' then it is assumed that
# it is 'edasxsoil', 'namzsoil' etc. and will be processed as the 
# land-surface precipitation accumulation (LSPA, table 130, parm 154).

inqsoil=`echo $model | grep soil | wc -l`

if [ $inqsoil -eq 1 ]; then
  convert=yes
  mkdir $model
  tbl=130
  parm=154
else 
  convert=no
  tbl=2
  parm=061
fi

nline=`wc -l $INPUT`
let "linecnt = 2"

rm -f cntrlfile

while [ $linecnt -le $nline ]; do

   INFILE=`sed -n ${linecnt}p $INPUT | nawk '{print $2}'`
  OUTFILE=`sed -n ${linecnt}p $INPUT | nawk '{print $1}'`

  if [ $convert = yes ]; then
    OTPUTF=$model/$OUTFILE
    CNVTDF=$COMOUT${daym1}/$OUTFILE
  else
    OTPUTF=$COMOUT${daym1}/$OUTFILE
  fi

# if $INFILE does not exist, or if the size is zero, note in 
# $LOG, skip remaining commands in the loop:

  if ! [ -s $INFILE ]; then
    echo $INFILE n/a >> $LOG
    let "linecnt = $linecnt + 1"
    continue
  fi

  cat >> cntrlfile << EOF
INPUTF  A120 :($INFILE
KGRID   I3   :(000)
OTPUTF  A120 :($OTPUTF
TABLE I3:($tbl)  PARM I3:($parm)  TFLAG I2:(-1)  P1 I3:(  0)  P2 I3:(  3)
EOF

  export pgm=verf_precip_brkout_ndas
  . prep_step

  $EXECverf_precip/verf_precip_brkout_ndas < cntrlfile >>$pgmout 
  export err=$?; err_chk

  if [ $convert = yes ]; then
    
    export pgm=verf_precip_pcpconform
    . prep_step

    $EXECverf_precip/verf_precip_pcpconform $model $OTPUTF $CNVTDF >>$pgmout
    export err=$?; err_chk
  fi

  let "linecnt = $linecnt + 1"

done

exit

