#!/bin/sh
################################################################3
#
#  This script will tar up all the data for a given forecast cycle for
#  the directory specified by the first
#  argument ($1) and place the tar files on the HPSS server,
#  under ${HPSSOUT}.  The tar file is put in the directory
#  appropriate for data valid for the day specified as the second 
#  command line argument ($2).
#
#  This script breaks up the gfs data directory into six separate
#  tar files ( that is, six tar files per cycle ).
#  The data files are broken up as proposed by EMC/GMB.
#
#  Usage: rhist_savegfs.sh Directory Date(YYYYMMDDHH format)
#
#  Where: Directory  = Directory to be tarred.
#         Date(YYYYMMDDHH format) = Day that the tar file should be saved under.
#
################################################################3
set -x

if [ $# -ne 2 ]
then
  echo "Usage: rhist_savegfs.sh Directory Date(YYYYMMDDHH format) "
  exit 1
fi 

#
#   Get directory to be tarred from the first command line argument,
#   and check to make sure that the directory exists.
#

dir=$1
if [ ! -d $dir ]
then
  echo "rhist_savegfs.sh:  Directory $dir does not exist."
  exit 2
fi 

#
#   Determine the directory where the tar file will be stored
#   and make sure that it exists in HPSS.
#

year=`echo $2 | cut -c 1-4`
yearmo=`echo $2 | cut -c 1-6`
yrmoday=`echo $2 | cut -c 1-8`
rhcyc=`echo $2 | cut -c 9-10`
rhcycle=t${rhcyc}z

if [ $TSM_FLAG = 'NO' ]
then
  hpssdir0=${HPSSOUT}/rh${year}/${yearmo}/$yrmoday
  hpssdir1=${HPSSOUT}/1year/rh${year}/${yearmo}/$yrmoday
  hpssdir2=${HPSSOUT}/2year/rh${year}/${yearmo}/$yrmoday
                                                                                                   
elif [ $TSM_FLAG = 'YES' ]
then
  rhistdir0=${TSMOUT}/rh${year}/${yearmo}/$yrmoday
  rhistdir1=${TSMOUT}/1year/rh${year}/${yearmo}/$yrmoday
  rhistdir2=${TSMOUT}/2year/rh${year}/${yearmo}/$yrmoday
                                                                                                   
  ssh ibmtsm1.ncep.noaa.gov "mkdir -p -m 755 $rhistdir0; mkdir -p -m 755 $rhistdir1; mkdir -p -m 755 $rhistdir2"
fi

#
#   Get a listing of all files in the directory to be tarred
#   and break the file list up into groups of files.
#   Each list of files names the contents of its associated tar file.
# 

cd $DATA
ls -1 $dir | grep ${rhcycle} | awk '
            /master/ { print "./"$0 > "trash" ; next }
            /sfluxgrb/ { print "./"$0 > "sfluxgrb" ; next }
            /^gfs[.]t..z[.]sf[0-9]*$|^gfs[.]t..z[.]logf[0-9]*$/ { print "./"$0 > "sigma" ; next }
            /^gfs[.]t..z[.]bf[0-9]*$/ { print "./"$0 > "surface" ; next }
            /^gfs[.]t..z[.]pgrbf[0-9][0-9]$|^gfs[.]t..z[.]pgrbf[1][0-8][0-9]$/ { print "./"$0 > "pgrb" ; next }
            /^gfs[.]t..z[.]pgrbif[0-9][0-9]$|^gfs[.]t..z[.]pgrbif[1][0-8][0-9]$/ { print "./"$0 > "pgrb" ; next }
            /^gfs[.]t..z[.]pgrbf[1][9][0-9]$|^gfs[.]t..z[.]pgrbf[2-3][0-9][0-9]/ { print "./"$0 > "pgrb2" ; next }
            /^gfs[.]t..z[.]pgrbf[0-9]*[.]2p5deg/ { print "./"$0 > "pgrb2" ; next }
            /^gfs[.]t..z[.]pgrb2anl$|^gfs[.]t..z[.]pgrb2f[0-9][0-9]$|^gfs[.]t..z[.]pgrb2f[1-3][0-9][0-9]$/ { print "./"$0 > "pgrb0p5" ; next } 
            /^gfs[.]t..z[.]pgrb2bf[2-3][0-9][0-9]$/ { print "./"$0 > "pgrb0p5" ; next }
            /^gfs[.]t..z[.]smartguam[0-9][0-9][.]tm00$|^gfs[.]t..z[.]smartguam[0-9][0-9][.]tm00[.]grib2$/ { print "./"$0 > "sminitguam" ; next }
            /^mdl_|grb65|^gfs_grb211/ { print "./"$0 > "products" ; next }
            /^mosfcsts|^wafgfs/ { print "./"$0 > "products" ; next }
            /pgrb2/ { print "./"$0 > "trash" ; next }
            { print "./"$0 > "anl" ; next } '

# Save MDL etsurge files

ls -1 ${dir} |grep ${rhcyc} | awk '/^mdlsurge/  { print "./"$0 } ' > mdlsurge
ls -1 ${dir} |grep $2   | awk '/^WAFS_blended/  { print "./"$0 } ' > WAFS_blended

cd $dir

#  Now create a tar file for each group of files

for file in anl sigma surface pgrb pgrb2 pgrb0p5 sfluxgrb products mdlsurge sminitguam WAFS_blended
do

   #
   #   Pick 1year, 2year, or permanent archive.
   #
   case $file in
      anl)        hpssdir=$hpssdir0
                  rhistdir=$rhistdir0;;
      sigma)      hpssdir=$hpssdir2
                  rhistdir=$rhistdir2;;
      surface)    hpssdir=$hpssdir2
                  rhistdir=$rhistdir2;;
      pgrb)       hpssdir=$hpssdir1
                  rhistdir=$rhistdir1;;
      pgrb2)      hpssdir=$hpssdir0
                  rhistdir=$rhistdir0;;
      pgrb0p5)    hpssdir=$hpssdir1
                  rhistdir=$rhistdir1;;
      sfluxgrb)   hpssdir=$hpssdir2
                  rhistdir=$rhistdir2;;
      products)   hpssdir=$hpssdir1
                  rhistdir=$rhistdir1;;
      mdlsurge)   hpssdir=$hpssdir2
                  rhistdir=$rhistdir2;;
      sminitguam) hpssdir=$hpssdir2
                  rhistdir=$rhistdir2;;
      WAFS_blended) hpssdir=$hpssdir0
                  rhistdir=$rhistdir0;;
      *)          hpssdir=$hpssdir0
                  rhistdir=$rhistdir0;;
   esac

   #
   #   Generate the name of the tarfile, which should be the same
   #   as the absolute path name of the directory being
   #   tarred, except that "/" are replaced with "_".
   #

   tarfile=`echo $PWD | cut -c 2- | tr "/" "_"`
   tarfile=${tarfile}${rhcyc}.${file}.tar

   #
   #   Check if the tarfile index exists.  If it does, assume that
   #   the data for the corresponding directory has already been
   #   tarred and saved.
   #

   if [ $TSM_FLAG = 'NO' ]
   then
     hsi "ls -l ${hpssdir}/${tarfile}.idx"
     tar_file_exists=$?
     if [ $tar_file_exists -eq 0 ]
     then
       echo "File  $tarfile already saved."
       continue
     fi
   elif [ $TSM_FLAG = 'YES' ]
   then
     size=`ssh ibmtsm1.ncep.noaa.gov ls -l ${rhistdir}/${tarfile} | awk '{print \$5}'`
     if [  -n "$size" ]
     then
       if [ $size -gt 0 ]
       then
          echo "File  $tarfile already saved."
          continue
       fi
     fi
   fi

   #   If on Stratus:
   #   htar is used to create the archive, -P creates
   #   the directory path if it does not already exist,
   #   and an index file is also made.
   #

   if [ $TSM_FLAG = 'NO' ]
   then
     date
     htar -P -cvf ${hpssdir}/$tarfile -L ${DATA}/$file
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savegfs.sh:  File $tarfile was not successfully created."
       exit 3
     fi
     date
 
   #
   #   Read the tarfile and save a list of files that are in the tar file.
   #
 
     htar -tvf $hpssdir/$tarfile
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savegfs.sh:  Tar file $tarfile was not successfully read to"
       echo "             generate a list of the files."
       exit 4
     fi
 
   #
   #  Restrict tar file, if it contains restricted data.
   #
     ${USHrhist}/rhist_restrict.sh ${hpssdir}/$tarfile
 
   #
   #  If on Cirrus send to HSM
   #
   elif [ $TSM_FLAG = 'YES' ]
   then

   #
   #   Tar up the directory and put the tarred file in the 
   #   appropriate directory in ${TSMOUT}.
   #  
   
     date
     gtar -cvf ${DATA}/$tarfile -T ${DATA}/$file
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savegfs.sh:  File $tarfile was not successfully created."
       exit 3
     fi 
     date
     $SCP $SCP_CONFIG ${DATA}/${tarfile} ibmtsm1.ncep.noaa.gov:${rhistdir}/${tarfile}

   fi   
rm ${DATA}/$file
   
done

exit 0
