#!/bin/sh
################################################################3
#
#  This script will tar up the data for a given forecast cycle for the
#  directory specified by the first
#  argument ($1) and place the tar file 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 gefs ensemble directory into five separate
#  tar files ( five for each of the four cycles ).
#  
#  ccpa: all files in the ccpa subdirectory  
#  ensstat: all files stored in the ensstat subdirectory
#  init: all files stored in the init subdirectory
#  mos: all files stored in the mos subdirectory (only 00 cycle)
#  ndgd_gb2: all files stored in the ndgd_gb2 subdirectory
#  pgrb2a: all files stored in the pgrb2a subdirectory
#  pgrb2b: all files stored in the pgrb2b subdirectory
#  pgrb2d: all files stored in the pgrb2d subdirectory
#  pgrb2a_an: all files in the pgrb2a_an subdirectory
#  pgrb2a_bc: all files in the pgrb2a_bc subdirectory
#  pgrb2a_wt: all files in the pgrb2a_wt subdirectory
#  sfcsig: all files in the sfcsig subdirectory
#  sflux: all files in the sflux subdirectory
#  track: all files in the track subdirectory  
#
#  Usage: rhist_saveens.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_saveens.sh Directory Date(YYYYMMDD 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_saveens.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 on the CCS.
#

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=$rhcyc

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.
#   Then cd to the directory to be tarred.
# 

cd $DATA
ls -1 ${dir}/${rhcycle}/ccpa | awk ' BEGIN { FS="/"} { print "./ccpa/"$NF } ' > ccpa 
ls -1 ${dir}/${rhcycle}/ensstat | awk ' BEGIN { FS="/"} { print "./ensstat/"$NF } ' > ensstat
ls -1 ${dir}/${rhcycle}/ndgd_gb2 | awk ' BEGIN { FS="/"} { print "./ndgd_gb2/"$NF } ' > ndgd_gb2
ls -1 ${dir}/${rhcycle}/pgrb2a | awk ' BEGIN { FS="/"} { print "./pgrb2a/"$NF } ' > pgrb2a
ls -1 ${dir}/${rhcycle}/pgrb2b | awk ' BEGIN { FS="/"} { print "./pgrb2b/"$NF } ' > pgrb2b
ls -1 ${dir}/${rhcycle}/pgrb2d | awk ' BEGIN { FS="/"} { print "./pgrb2d/"$NF } ' > pgrb2d
ls -1 ${dir}/${rhcycle}/pgrb2a_an | awk ' BEGIN { FS="/"} { print "./pgrb2a_an/"$NF } ' > pgrb2a_an
ls -1 ${dir}/${rhcycle}/pgrb2a_bc | awk ' BEGIN { FS="/"} { print "./pgrb2a_bc/"$NF } ' > pgrb2a_bc 
ls -1 ${dir}/${rhcycle}/pgrb2a_wt | awk ' BEGIN { FS="/"} { print "./pgrb2a_wt/"$NF } ' > pgrb2a_wt
ls -1 ${dir}/${rhcycle}/init | awk ' BEGIN { FS="/"} { print "./init/"$NF } ' > init
ls -1 ${dir}/${rhcycle}/sfcsig | awk ' BEGIN { FS="/"} { print "./sfcsig/"$NF } ' > sfcsig
ls -1 ${dir}/${rhcycle}/sflux | awk ' BEGIN { FS="/"} { print "./sflux/"$NF } ' > sflux 
ls -1 ${dir}/${rhcycle}/track | awk ' BEGIN { FS="/"} { print "./track/"$NF } ' > track


# Only archive MOS subdirectory for the 00Z cycle

if [ ${rhcycle} -eq 00 ]
then
   ls -1 ${dir}/${rhcycle}/mos | awk ' BEGIN { FS="/"} { print "./mos/"$NF } ' > mos
   archive_list='ccpa ensstat pgrb2a pgrb2b pgrb2d pgrb2a_an pgrb2a_bc pgrb2a_wt init mos ndgd_gb2 sfcsig sflux track'
else
   archive_list='ccpa ensstat pgrb2a pgrb2b pgrb2d pgrb2a_an pgrb2a_bc pgrb2a_wt init ndgd_gb2 sfcsig sflux track'
fi

cd ${dir}/${rhcycle}

#  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
      ccpa)         hpssdir=$hpssdir0
                    rhistdir=$rhistdir0;;
      ensstat)      hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      init)         hpssdir=$hpssdir0
                    rhistdir=$rhistdir0;;
      mos)          hpssdir=$hpssdir0
                    rhistdir=$rhistdir0;;
      ndgd_gb2)     hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      pgrb2a)       hpssdir=$hpssdir0
                    rhistdir=$rhistdir0;;
      pgrb2b)       hpssdir=$hpssdir2
                    rhistdir=$rhistdir2;;
      pgrb2d)       hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      pgrb2a_an)    hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      pgrb2a_bc)    hpssdir=$hpssdir2
                    rhistdir=$rhistdir2;;
      pgrb2a_wt)    hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      sfcsig)       hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      sflux)        hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      track)        hpssdir=$hpssdir1
                    rhistdir=$rhistdir1;;
      *)            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

   #   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_saveens.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_saveens.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
     date
     gtar -cvf ${DATA}/$tarfile -T ${DATA}/$file
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_saveens.sh:  File $tarfile was not successfully created."
       exit 3
     fi

     $SCP $SCP_CONFIG ${DATA}/$tarfile ibmtsm1.ncep.noaa.gov:${rhistdir}/$tarfile
     date
   fi 
rm  ${DATA}/$file
   
done
exit 0
