#!/bin/sh
#######################################################################
echo "----------------------------------------------------------------"
echo "THIS JOB ARCHIVES GDAS, NDAS and STRATOSPHERIC DATA FOR NCAR" 
echo "----------------------------------------------------------------"
echo "   This script saves ndas,gdas1 and stratospheric data for NCAR "
echo "   On every Sunday, the fields are tarred up and posted on NCEP "
echo "   ftp server."
####################################################################

########################################################################
# START FLOW OF CONTROL
# 1) Archives NDAS and GDAS OBS DATA for NCAR (00Z only)
# 2) Archives GDAS SELECTED GDAS FIELDS for NCAR
# 3) ARCHIVE STRATOSPHERIC FIELDS FOR NCAR (12Z only)  (see below)
# 4) TAR UP THE ARCHIVED DATA FOR NCAR AND POST THEM TO NCEP FTP SERVER
########################################################################

########################################################################
# Dec. 16, 2008 - C. Caruso Magee - Removed archive of stratospheric
#                 fields for NCAR (12Z only).  The products are no longer
#                 produced operationally or supported by CPC.
########################################################################

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

set -x

cd $DATA

######################################
# 1) Save ndas and gdas1 obs data for
#    subsequent archive for NCAR.
######################################

if test "$cycle" = 't00z'
then
  cp $COMINNDAS/ndas.t*.adp*.tm03*.bufr_d $COMOUTOBS/obs.${PDYm2}
  cp $COMINNDAS/ndas.t*.air*.tm03*.bufr_d  $COMOUTOBS/obs.${PDYm2}
  cp $COMINNDAS/ndas.t*.satwnd.tm03*.bufr_d $COMOUTOBS/obs.${PDYm2}
  cp $COMINNDAS/ndas.t*.sfcshp.tm03*.nr $COMOUTOBS/obs.${PDYm2}
  cp $COMINNDAS/ndas.t*.adp*.tm09*.bufr_d $COMOUTOBS/obs.${PDYm2}
  cp $COMINNDAS/ndas.t*.air*.tm09*.bufr_d $COMOUTOBS/obs.${PDYm2}
  cp $COMINNDAS/ndas.t*.satwnd.tm09* $COMOUTOBS/obs.${PDYm2}
  cp $COMINNDAS/ndas.t*.sfcshp.tm09*.nr $COUTOBS/obs.${PDYm2}

  cp $COMINGDAS/gdas1.t*.adp*.tm*.bufr_d $COMOUTOBS/obs.${PDYm2}
  cp $COMINGDAS/gdas1.t*.air*.tm*.bufr_d $COMOUTOBS/obs.${PDYm2}
  cp $COMINGDAS/gdas1.t*.satwnd.tm*.bufr_d $COMOUTOBS/obs.${PDYm2}
  cp $COMINGDAS/gdas1.t*.sfcshp.tm*.bufr_d.nr $COMOUTOBS/obs.${PDYm2}

  rm $COMOUTOBS/obs.${PDYm2}/*.listing
  chmod 664 $COMOUTOBS/obs.${PDYm2}/*
fi

##############################################################
# 2) ARCHIVE SELECTED PREVIOUS DAY's GDAS FIELDS FOR NCAR
##############################################################

cp $COMINGDAS/gdas1.${cycle}.pgrbf00 PGrbF00
cp $FIXutil/ncargdas.dat grib2grib.dat

#########################################################
#  NOTE:  Program grib2grib uses unit 11 as input and   #
#         needs the output file name as the output unit.#
#########################################################
pgm=grib2grib
prep_step

export XLFRTEOPTS="unit_vars=yes"
export XLFUNIT_11="PGrbF00"
export XLFUNIT_51="ncargdas"
$EXECutil/grib2grib

#
#   Interpolate 1-degree grid to 2.5 nh and sh degree grids.
#   Copygb uses the index file to speed the reading.
#
$EXECutil/grbindex ncargdas ncarigdas
$EXECutil/copygb -g29 ncargdas ncarigdas grid29
$EXECutil/copygb -g30 ncargdas ncarigdas grid30

cat grid29 grid30 > gdasgrids
cp gdasgrids $COMOUTGDAS/ncargdas.${PDYm1}$cyc
chmod 664 $COMOUTGDAS/ncargdas.${PDYm1}$cyc

rm grib2grib.dat

############################################################
# 3) TAR UP THE ARCHIVED DATA FOR NCAR
#    This script will tar up the archived data for NCAR and
#    post the tar files on NCEP ftp server. 
############################################################

wkdayname=`date -u +%a`

if test "$cycle" = 't12z'
then
    cd $COMOUTOBS
    tar -cvf /com/arkv/prod/ncartar/obs.${PDYm2}.tar obs.${PDYm2}

    if [ "$SENDDBN" = "YES" ]
    then
        $DBNROOT/bin/dbn_alert MODEL NCAR_ARCH $job /com/arkv/prod/ncartar/obs.${PDYm2}.tar
    fi

    if test $wkdayname = 'Sun'
    then
        sh $USHutil/exncarftp.sh
    fi

#####################################################################
#   Cleanup for old NCAR data files
#####################################################################

    find /com/arkv/prod/ncartar      -mtime +20  -exec rm {} \;
    find /com/arkv/prod/ncarlog      -mtime +20  -exec rm {} \;
    find $COMOUTGDAS                 -mtime +20  -exec rm {} \;
    find $COMOUTOBS                  -mtime +20  -exec rm -rf {} \;

fi 

#####################################################################
# GOOD RUN
set +x
echo "**************JOB $job COMPLETED NORMALLY ON THE IBM"
echo "**************JOB $job COMPLETED NORMALLY ON THE IBM"
echo "**************JOB $job COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################

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

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