#!/bin/sh
###############################################################
# script:  cfs_getclim.sh
# purpose: This script gets the calibration climatological
#          data from HPSS that have been prepared by EMC
#          and send them out to TOC. It should run in parallel
#          to the Forecast, i.e. same day as the forecast's day
###############################################################
set -x

YMDH=$1
yyyy=$(echo $YMDH | cut -c1-4)
mm=$(echo $YMDH | cut -c5-6)
dd=$(echo $YMDH | cut -c7-8)
hh=$(echo $YMDH | cut -c9-10)

tarfile="cfs_clim.${mm}${dd}.tar" 
hpssdir=$COMIN

# Get the tarfile from HPSS:
cd $COMOUT
if [ -s $COMOUT/clim.$YMDH.stamp ]
then
    status=`cat $COMOUT/clim.$YMDH.stamp |awk '{print $1}'`
    if [ $status != "done" ]
    then
       retrieve=yes
    else
       retrieve=no
    fi
else
    retrieve=yes
fi

if [ $retrieve = "yes" ]
then
    #htar -xf $hpssdir/$tarfile
    tar -xf $COMIN/$tarfile
    export err=$?
    if [ $YMDH -eq $start_date ]
    then
        $DATA/err_chk
    fi
    if [ $err = 0 ]
    then
       echo "done $YMDH" >$COMOUT/clim.$YMDH.stamp
    else
       echo "failed $YMDH" >$COMOUT/clim.$YMDH.stamp
       echo "Retrieval of data for $YMHD is unsuccessful!"
    fi
fi

if [ $SENDDBN = YES -a $YMDH -eq $start_date ]; then
    for datatype in pgb flx ocn
    do
       for file in `ls ${datatype}*`
       do
          season=`echo $file |awk -F"." '{print $2}'`
          dash=`echo $season |cut -c3`
	  if [ $dash = "-" ]; then
              $DBNROOT/bin/dbn_alert MODEL CFS_SEASON $job $COMOUT/$file
	  #    export err=$?; $DATA/err_chk
	      echo "Seasonal Climatology data has been sent to TOC"
          else
              $DBNROOT/bin/dbn_alert MODEL CFS_CLIM $job $COMOUT/$file
	 #     export err=$?; $DATA/err_chk
	      echo "Monthly Climatology data has been sent to TOC"
          fi
       done    
    done
fi

cd $DATA

