#!/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 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 nam data directory into five separate
#  tar files ( that is, five tar files per cycle ).
#  The data files are broken up as proposed by Eric Rogers, EMC/MMB.
#
#  Nam restart files are also copied from /nwges/prod/nam.YYYMMDD to
#  the appropriate ${TSMOUT} directory.
#
#  Usage: rhist_savenam.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_savenam.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_savenam.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.
#   Then cd to the directory to be tarred.
# 

cd $DATA
ls -1 $dir | grep ${rhcycle} | awk '
            /ieee_d$/ { print "./"$0 > "bufr" ; next }
            /cqc|cyclone|tcvitals|tropcy_relocation/ { print "./"$0 > "bufr" ; next }
            /reject|sgesprep|sdm|syndata|/ { print "./"$0 > "bufr" ; next }
            /satbias|satang|cnvstat|radstat|dump_alert_flag$/ { print "./"$0 > "bufr" ; next }
            /fits[.]tm00$|nampcpnv/ { print "./"$0 > "bufr" ; next }
            /bufr/ && !/nexrad/ && !/bufr_d.listing$/ { print "./"$0 > "bufr" ; next }
            /nexrad|radwnd/ { print "./"$0 > "nexrad" ; next }
            /^mdl_|awphys|awip218/ { print "./"$0 > "awphys" ; next }
            /awak3d/ && !/awak3dtiles/ { print "./"$0 > "awak3d" ; next }
            /awip32/ { print "./"$0 > "awip32" ; next }
            /awp151/ { print "./"$0 > "awp151" ; next }
            /awldas|awip3d|awip20|awip12|awp242|awipak/ { print "./"$0 > "awip" ; next }
            /grbgrd00.tm00$/ { print "./"$0 > "awip" ; print "./"$0 > "grbgrd" ; next }
            /awiphi|awp207|awp211|awp217/ { print "./"$0 > "grbgrd" ; next }
            /awp237|grbgrd/ { print "./"$0 > "grbgrd" ; next }
            /goes151|goes218|goes221|goes243/ { print "./"$0 > "goesll" ; next }
            /z[.]bgrd3d00.tm00|z[.]bgrd3d03.tm00|z[.]bgrd3d06.tm00|z[.]bgrd3d09.tm00/ { print "./"$0 > "bgrd3d" ; next }
            /z[.]bgrd3d12.tm00|z[.]bgrd3d15.tm00|z[.]bgrd3d18.tm00|z[.]bgrd3d21.tm00/ { print "./"$0 > "bgrd3d" ; next }
            /z[.]bgrd3d24.tm00|z[.]bgrd3d27.tm00|z[.]bgrd3d30.tm00|z[.]bgrd3d33.tm00/ { print "./"$0 > "bgrd3d" ; next }
            /z[.]bgrd3d36.tm00|z[.]bgrd3d39.tm00|z[.]bgrd3d42.tm00|z[.]bgrd3d45.tm00/ { print "./"$0 > "bgrd3d" ; next }
            /z[.]bgrd3d48.tm00/ { print "./"$0 > "bgrd3d" ; next }
            /bgrdsf/ { print "./"$0 > "bgrid" ; next }
            /z[.]bgdawp/ { print "./"$0 > "bgdawp" ; next }
            /afwahi/ { print "./"$0 > "afwahi" ; next }
            /afwaca/ { print "./"$0 > "afwaca" ; next }
            /afwacs/ { print "./"$0 > "afwacs" ; next }
            /afwaak/ { print "./"$0 > "afwaak" ; next }
            /smartconus/ { print "./"$0 > "smartconus" ; next }
            /smartak/ { print "./"$0 > "smartak" ; next }
            /smarthi/ { print "./"$0 > "smarthi" ; next }
            /smartpr/ { print "./"$0 > "smartpr" ; next }
            /alaskanest.hiresf/ { print "./"$0 > "alaskanest" ; next }
            /conusnest.hiresf/ { print "./"$0 > "conusnest" ; next }
            /firewxnest.hiresf/ { print "./"$0 > "firewxnest" ; next }
            /hawaiinest.hiresf/ { print "./"$0 > "hawaiinest" ; next }
            /priconest.hiresf/ { print "./"$0 > "priconest" ; next }
            /configure|boco/ { print "./"$0 > "input" ; next }
            /snow/ { print "./"$0 > "input" ; next } 
            { print "./"$0 > "therest" ; next } '
cd $dir

#  Now create a tar file for each group of files

for file in afwaca afwacs afwahi afwaak bufr nexrad awip32 awp151 awphys awak3d awip grbgrd bgrid bgrd3d bgdawp goesll input smartconus smartak smarthi smartpr alaskanest conusnest firewxnest hawaiinest priconest
do

   # 
   #   Pick 1year, 2year, or permanent archive.
   #
   case $file in
      bufr)   hpssdir=$hpssdir0
              rhistdir=$rhistdir0;;
      nexrad) hpssdir=$hpssdir0
              rhistdir=$rhistdir0;;
      awip32) hpssdir=$hpssdir0
              rhistdir=$rhistdir0;;
      awphys) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      awp151) hpssdir=$hpssdir0
              rhistdir=$rhistdir0;;
      awak3d) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      awip)   hpssdir=$hpssdir0
              rhistdir=$rhistdir0;;
      grbgrd) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      afwaca) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      afwacs) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      afwahi) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      afwaak) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      smartconus) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      smartak) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      smarthi) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      smartpr) hpssdir=$hpssdir1
              rhistdir=$rhistdir1;;
      bgrid)  hpssdir=$hpssdir0
              rhistdir=$rhistdir0;;
      bgrd3d) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      bgdawp) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      goesll) hpssdir=$hpssdir0
              rhistdir=$rhistdir0;;
      input)  hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      conusnest) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      firewxnest) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      alaskanest) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      hawaiinest) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      priconest) hpssdir=$hpssdir2
              rhistdir=$rhistdir2;;
      *)      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_savenam.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_savenam.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
 
   #
   #  Send to TSM
   #

   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_savenam.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

#
#   Now copy the restart files from /nwges/prod/nam.YYYYMMDD
# 
if [ $TSM_FLAG = 'NO' ]
then
  hsi << EOF
  put /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.nmm_b_restart_nemsio_anl.tm00 : ${hpssdir0}/nam.${rhcycle}.nmm_b_restart_nemsio_anl.tm00
  put /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_alaskanest : ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_alaskanest
  put /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_conusnest : ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_conusnest
  put /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_firewxnest : ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_firewxnest
  put /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_hawaiinest : ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_hawaiinest
  put /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_priconest : ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_priconest
  put /nwges/prod/ndas.${yrmoday}/ndas.${rhcycle}.input_nemsio.d01.tm12 : ${hpssdir0}/ndas.${rhcycle}.input_nemsio.d01.tm12
  put /nwges/prod/ndas.${yrmoday}/ndas.${rhcycle}.nmm_b_restart_nemsio.tm03 : ${hpssdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio.tm03
  put /nwges/prod/ndas.${yrmoday}/ndas.${rhcycle}.nmm_b_restart_nemsio_anl.tm03 : ${hpssdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio_anl.tm03
  chmod 755 ${hpssdir0}/nam.${rhcycle}.nmm_b_restart_nemsio_anl.tm00
  chmod 755 ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_alaskanest
  chmod 755 ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_conusnest
  chmod 755 ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_firewxnest
  chmod 755 ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_hawaiinest
  chmod 755 ${hpssdir0}/nam.${rhcycle}.input_nemsio_anl_priconest
  chmod 755 ${hpssdir0}/ndas.${rhcycle}.input_nemsio.d01.tm12
  chmod 755 ${hpssdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio.tm03
  chmod 755 ${hpssdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio_anl.tm03
EOF
elif [ $TSM_FLAG = 'YES' ]
then
  $SCP $SCP_CONFIG /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.nmm_b_restart_nemsio_anl.tm00 ibmtsm1.ncep.noaa.gov:${rhistdir0}/nam.${rhcycle}.nmm_b_restart_nemsio_anl.tm00
  $SCP $SCP_CONFIG /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_alaskanest ibmtsm1.ncep.noaa.gov:${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_alaskanest
  $SCP $SCP_CONFIG /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_conusnest ibmtsm1.ncep.noaa.gov:${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_conusnest
  $SCP $SCP_CONFIG /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_firewxnest ibmtsm1.ncep.noaa.gov:${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_firewxnest
  $SCP $SCP_CONFIG /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_hawaiinest ibmtsm1.ncep.noaa.gov:${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_hawaiinest
  $SCP $SCP_CONFIG /nwges/prod/nam.${yrmoday}/nam.${rhcycle}.input_nemsio_anl_priconest ibmtsm1.ncep.noaa.gov:${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_priconest
  $SCP $SCP_CONFIG /nwges/prod/ndas.${yrmoday}/ndas.${rhcycle}.input_nemsio.d01.tm12 ibmtsm1.ncep.noaa.gov:${rhistdir0}/ndas.${rhcycle}.input_nemsio.d01.tm12
  $SCP $SCP_CONFIG /nwges/prod/ndas.${yrmoday}/ndas.${rhcycle}.nmm_b_restart_nemsio.tm03 ibmtsm1.ncep.noaa.gov:${rhistdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio.tm03
  $SCP $SCP_CONFIG /nwges/prod/ndas.${yrmoday}/ndas.${rhcycle}.nmm_b_restart_nemsio_anl.tm03 ibmtsm1.ncep.noaa.gov:${rhistdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio_anl.tm03
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/nam.${rhcycle}.nmm_b_restart_nemsio_anl.tm00"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_alaskanest"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_conusnest"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_firewxnest"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_hawaiinest"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/nam.${rhcycle}.input_nemsio_anl_priconest"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/ndas.${rhcycle}.input_nemsio.d01.tm12"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio.tm03"
  ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir0}/ndas.${rhcycle}.nmm_b_restart_nemsio_anl.tm03"
fi


