#!/bin/sh
###################################################################
#
# script: cfs_extvarsput.sh
# purpose: cat extvars files and ftp
#
#  This script assumes that the input extvars files are named as:
#    extvarsf?? where '??' is forecast hour of at least 2 digit
#
# grib file average calculation
#
# $1 is starting forecast hour (at least 2-digit)
# $2 is ending forecast hour (at least 2-digit)
# $3 is forecast hour interval
# $4 is output file name
# $5 ftp directory
# $6 ftp ip address
#
#  This script assumes that the input grib files are named as:
#    extvarsf?? where '??' is forecast hour of at least 2 digit
#
if [ $# -ne 4 -a $# -ne 6 ] ; then
  $DATA/err_exit "$0: argument error" 
fi
fhs=$1
fhe=$2
intvl=$3
outextvars=$4
ftpdir=$5
platform=$6
#
REMOVE_EXTVARS=${REMOVE_EXTVARS:=YES}
REMOVE_FTPED=${REMOVE_FTPED:=YES}
#
rm -f $outextvars
#
fh=$fhs
count=1
while [ $fh -le $fhe ] ; do
  fhr=$fh
  if [ $fh -lt 10 ] ; then fhr=0$fh ; fi
  if [ ! -s $COM_YMDH_DG/extvarsf$fhr ] ; then 
     echo "extvars for fhr=$fhr missing"
     $USHcfs/cfs_extvars.sh $fhr $fhr 24
     ${NCP:-/bin/cp} extvarsf$fhr $COM_YMDH_DG/extvarsf$fhr
  fi
  cat $COM_YMDH_DG/extvarsf$fhr >>$outextvars	
  fh=`expr $fh + $intvl`
done

# delete individual extvars file

if [ $REMOVE_EXTVARS = YES ] ; then
   fh=$fhs
   count=1
   while [ $fh -le $fhe ] ; do
     fhr=$fh
     if [ $fh -lt 10 ] ; then fhr=0$fh ; fi
     rm -f extvarsf$fhr
     fh=`expr $fh + $intvl`
   done
fi
