#!/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 aqm data directory into two separate
#  tar files ( that is, two tar files per cycle ).
#  The data files are broken up as proposed by EMC/MMB.
#
#  Usage: rhist_saveaqm.sh Directory Date(YYYYMMDDHH format) Domain
#
#  Where: Directory  = Directory to be tarred.
#         Date(YYYYMMDDHH format) = Day that the tar file should be saved under.
#         Domain = Domain that bin file should be saved under
#  
#  Modifed 201107 for sending data to IBMTSM1
#
################################################################3
set -x

if [ $# -ne 3 ]
then
  echo "Usage: rhist_saveaqm.sh Directory Date(YYYYMMDDHH format) Domain "
  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_saveaqm.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

domain=$3

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

if [ $rhcycle = 't06z' ]
then 
  ls -1 $dir | grep ${rhcycle} | awk '
             /awpoz|sigma/ { print "./"$0 > "2yr_res_a" ; next } 
             /log/ { print "./"$0 > "2yr_res_a" ; next } '
  ls -1 $dir | grep ${rhcyc} | awk '
             /\.o/ {print "./"$0 > "2yr_res_b" ; next } '
elif [ $rhcycle = 't12z' ]
then
 ls -1 $dir | grep ${rhcycle} | awk '
             /nmm/ { print "./"$0 > "2yr_con" ; next }
             /aconc|cldiag|dep|chem/ { print "./"$0 > "2yr_res_a" ; next }
             /emission|grd|met/ { print "./"$0 > "2yr_res_a" ; next }
             /soil|awpoz|sigma/ { print "./"$0 > "2yr_res_a" ; next } 
             /log/ { print "./"$0 > "2yr_res_a" ; next } '
 ls -1 $dir | grep ${rhcyc} | awk '
             /\.o/ {print "./"$0 > "2yr_res_b" ; next } '
else
  ls -1 $dir | grep ${rhcycle} | awk '
             /[.]conc|aqfnam/ { print "./"$0 > "2yr_con" ; next }
             /aconc|cldiag|dep|chem/ { print "./"$0 > "2yr_res_a" ; next }
             /emission|grd|met/ { print "./"$0 > "2yr_res_a" ; next }
             /soil|awpoz|sigma/ { print "./"$0 > "2yr_res_a" ; next } 
             /log/ { print "./"$0 > "2yr_res_a" ; next } '
  ls -1 $dir | grep ${rhcyc} | awk '
             /\.o/ {print "./"$0 > "2yr_res_b" ; next } '
fi

# Combine 2year res file lists
cat 2yr_res_a 2yr_res_b > 2yr_res

if [ -f 2yr_con ]
then
  filelist="2yr_con 2yr_res"
else
  filelist="2yr_res"
fi

cd $dir

#  Now create a tar file for each group of files

for file in $filelist
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}${rhcyc}.${file}.tar

   #   Determine where the file should be archived

   if [ $file = "2yr_res" ]
   then
      hpssdir=$hpssdir0
      rhistdir=$rhistdir0
   else
      hpssdir=$hpssdir2
      rhistdir=$rhistdir2
   fi
  
   #
   #   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

   #   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_saveaqm.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_saveaqm.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

   # Submit to TSM

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

if [ $rhcycle = 't12z' ]
then
   if [ $TSM_FLAG = 'NO' ]
   then
     hsi << EOF
       put ${dir}/aqm.${rhcycle}.conc.bin : ${hpssdir2}/${domain}.${rhcycle}.conc.bin.${yrmoday}
       chmod 755 ${hpssdir2}/${domain}.${rhcycle}.conc.bin.${yrmoday}
EOF
   elif [ $TSM_FLAG = 'YES' ]
   then
     $SCP $SCP_CONFIG ${dir}/aqm.${rhcycle}.conc.bin ibmtsm1.ncep.noaa.gov:${rhistdir2}/${domain}.${rhcycle}.conc.bin.${yrmoday}
     ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir2}/${domain}.${rhcycle}.conc.bin.${yrmoday}"
   fi
fi

exit 0
