#! /bin/sh
########################################################################################
# Name of Script: exverf_gridtobs_exfits.sh.sms
# Purpose of Script: To generate the verification products for the surface and upper
#                    air (partial sum) for various operational models to be used by the
#                    Forecast Verification System
# Arguments: exverf_gridtobs_exfits.sh.sms $yyyy$mm$dd
# Log history:
########################################################################################
set -x

cd $DATA

echo "$0 STRDATE "`date`

msg="$job HAS BEGUN"
postmsg "$jlogfile" "$msg"

export vday=$1
export model=$2

## Read in the model information
cp $PARMverf_gridtobs/verf_gridtobs.modelinfo gridtobs.modelinfo
cat gridtobs.modelinfo |while read line
do
  model_name=`echo $line |awk -F"|" '{print $1}'`
  if [ $model_name = $model ]
  then
     export regions=`echo $line |awk -F"|" '{print $2}'`
     export vlen=`echo $line |awk -F"|" '{print $3}'`
     export frange=`echo $line |awk -F"|" '{print $4}'`

     break
  fi
done

for vcyc in $cycles
do
  # Remove the vsdb record for the same day and cycle in case this is a rerun
  
  if [ ! -s $COMVSDB/${model} ]; then mkdir -p $COMVSDB/${model}; fi

  if [ -f $COMVSDB/${model}/*_${vday}.vsdb ]
  then
    cd $COMVSDB/${model}
    for vsdbfile in `ls *_${vday}.vsdb`
    do
      sed -e "/$vday$vcyc/d"  $vsdbfile>${vsdbfile}1
      mv ${vsdbfile}1 ${vsdbfile}
    done
  fi

  cd $DATA
  mkdir -p $DATA/$vcyc
  $USHverf_gridtobs/verf_gridtobs_fits.sh $model $vday $vcyc
done

#####################################################################
# GOOD RUN
set +x
echo "**************$job COMPLETED NORMALLY on `date`"
set -x
#####################################################################

msg="HAS COMPLETED NORMALLY!"
echo $msg
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################

