#!/bin/sh
################################################################3
#
#  This script archives specific hires data to the two-year archive.
#
#  Usage: rhist_savehires.sh Directory Date(YYYYMMDDHH format)
#
#  Where: Directory  = Directory to be tarred.
#         Date(YYYYMMDDHH format) = Day that the tar file should be saved under.
#
#  *All error checking disabled because hires data is not always
#   available since the model is preempted by the GFDL and HWRF*
#
################################################################3
set -x

if [ $# -ne 2 ]
then
  echo "Usage: rhist_savehires.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_savehires.sh:  Directory $dir does not exist."
  exit 0
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`

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 | awk '
           /(00|03|06|09|12|15|18|21|24|27|30|33|36|39|42|45|48).tm00.grib2$/ { print "./"$0 > "save" ; next }
           /wrfinput_d01|wrfbdy_d01|namelist.input/ { print "./"$0 > "save" ; next }'


cd $dir

#  Now create a tar file for each group of files

for file in save
do

   #
   #   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}.tar

   #   Determine where the file should be archived

   hpssdir=$hpssdir2
   rhistdir=$rhistdir2
  
   #
   #   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_savehiresw.sh:  File $tarfile was not successfully created."
        exit 0
     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_savehiresw.sh:  Tar file $tarfile was not successfully read to"
       echo "             generate a list of the files."
       exit 0
     fi 

   #
   #  Restrict tar file, if it contains restricted data.
   #
     ${USHrhist}/rhist_restrict.sh ${hpssdir}/$tarfile

   # If on Cirrus submit to HSM

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

done

exit 0
