#!/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 saves some of sref com data directory into a
#  tar file for each cycle.
#  The data files are selected as proposed by EMC/MMB.
#
#  Usage: rhist_savesref.sh Directory Date(YYYYMMDDHH format)
#
#  Where: Directory  = Directory to be tarred.
#         Date(YYYYMMDDHH format) = Day that the tar file should be saved under.
#
#  change log:
#  7/14/2005, Jun Du: add 216 and 243 two extra grids besides 212 grid
#  12/08/2005, Jun Du: add WRF_nmm and WRF_em members
#  11/30/2007, J. Cooke: Modified to archive based on subdirectories
#  07/10/2012, Yuqiu Zhu: add ensprod_ndgd, cluster_NCEP, cluster_OU subdirectories
################################################################3
set -x

if [ $# -ne 2 ]
then
  echo "Usage: rhist_savesref.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_savesref.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
                                                                                                   
  hsi mkdir -p -m 755 $hpssdir0
  hsi mkdir -p -m 755 $hpssdir1
  hsi mkdir -p -m 755 $hpssdir2
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}/${rhcyc}/bufr | awk ' BEGIN { FS="/"} { print "./bufr/"$NF } ' > bufr
ls -1 ${dir}/${rhcyc}/ensprod | awk ' BEGIN { FS="/"} { print "./ensprod/"$NF } ' > ensprod
ls -1 ${dir}/${rhcyc}/ensprod_biasc | awk ' BEGIN { FS="/"} { print "./ensprod_biasc/"$NF } ' > ensprod_biasc
ls -1 ${dir}/${rhcyc}/ensprod_ndgd | awk ' BEGIN { FS="/"} { print "./ensprod_ndgd/"$NF } ' > ensprod_ndgd
ls -1 ${dir}/${rhcyc}/gempak | awk ' BEGIN { FS="/"} { print "./gempak/"$NF } ' > gempak
ls -1 ${dir}/${rhcyc}/pgrb | grep -v pgrb132 | awk ' BEGIN { FS="/"} { print "./pgrb/"$NF } ' > pgrb
ls -1 ${dir}/${rhcyc}/pgrb_biasc | awk ' BEGIN { FS="/"} { print "./pgrb_biasc/"$NF } ' > pgrb_biasc
ls -1 ${dir}/${rhcyc}/pgrb_ndgd | awk ' BEGIN { FS="/"} { print "./pgrb_ndgd/"$NF } ' > pgrb_ndgd
ls -1 ${dir}/${rhcyc}/cluster_NCEP | awk ' BEGIN { FS="/"} { print "./cluster_NCEP/"$NF } ' > cluster_NCEP
ls -1 ${dir}/${rhcyc}/cluster_OU | awk ' BEGIN { FS="/"} { print "./cluster_OU/"$NF } ' > cluster_OU
ls -1 ${dir}/${rhcyc}/misc | awk ' BEGIN { FS="/"} { print "./misc/"$NF } ' > misc
ls -1 ${dir}/${rhcyc}/track | awk ' BEGIN { FS="/"} { print "./track/"$NF } ' > track

if [ -s track ]
then 
   archive_list='bufr ensprod ensprod_biasc ensprod_ndgd pgrb pgrb_biasc pgrb_ndgd cluster_NCEP cluster_OU misc track'
else
   archive_list='bufr ensprod ensprod_biasc ensprod_ndgd pgrb pgrb_biasc pgrb_ndgd cluster_NCEP cluster_OU misc'
fi

cd ${dir}/${rhcyc}

#  Now create a tar file for each group of files

for file in $archive_list
do

   #
   #   Pick 1year, 2year, or permanent archive.
   #
   case $file in
      bufr)           hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      ensprod)        hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      ensprod_biasc)  hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      ensprod_ndgd)   hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      gempak)         hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      pgrb)           hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      pgrb_biasc)     hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      pgrb_ndgd)      hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      cluster_NCEP)   hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      cluster_OU)     hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      misc)           hpssdir=$hpssdir0
                      rhistdir=$rhistdir0;;
      track)          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}.${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

   date

#     Send tar file to archive


#  If on Stratus then send to HPSS

   if [ $TSM_FLAG = 'NO' ]
   then  
     htar -P -cvf ${hpssdir}/$tarfile -L ${DATA}/$file
     err=$?
 
   #
   #   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_savesref.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

   elif [ $TSM_FLAG = 'YES' ]
   then
     date
     gtar -cvf ${DATA}/$tarfile -T ${DATA}/$file
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savesref.sh:  File $tarfile was not successfully created."
       exit 3
     fi
     date
     $SCP $SCP_CONFIG ${DATA}/${tarfile} ibmtsm1.ncep.noaa.gov:${rhistdir}/${tarfile}
     date
   fi
 
rm ${DATA}/${tarfile} ${DATA}/$file
   
done

exit 0
