#!/usr/bin/ksh

set +x
#####################################################################
echo "-------------------------------------------------------"
echo "exhwrf_arch.sh.sms - Script to archive GFDL forecast runs"
echo "-------------------------------------------------------"
echo "History: JUNE 2007 - First implementation of this new script."
#####################################################################
set -x

PS4=' + exhwrf_arch.sh.sms line $LINENO: '

label="cat $COMOUT/$stormlabel.$cycle"

if test -f "$label" = "No $stormlabel"
then
   ##############################################
   # Condition if no storms are to be run
   ##############################################
   msg="No hurricane to run."
   postmsg "$jlogfile" "$msg"

   exit
fi

if test -f ${COMIN}/${stormlabel}.holdvars.txt
then
   . ${COMIN}/${stormlabel}.holdvars.txt
else
   msg="Hurricane to Run But No ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export pgm=exhwrf_arch.sh.sms
   export err=911;err_chk
fi

######################################################################
# Do archiving of all relevant GFDL forecast data....
######################################################################

#--------------------------------------------------------------------#
# Determine script variables from ENVIRONMENTAL VARS....
#--------------------------------------------------------------------#

comdir=/com/hur/${envir}/hwrf.${YMDH}

export ymdh=${YMDH}
export yyyy=`     echo $ymdh | cut -c1-4`
export yy=`       echo $ymdh | cut -c3-4`
export yymmddhh=` echo $ymdh | cut -c3-10`

if [ ${PARAFLAG} = 'YES' ]
then
  exit 0
fi

nhcnum=$stormenv
if [ $nhcnum -ge 80 ]; then
# Do not archive 80/90-level storms....
  set +x
  echo " "
  echo "NOT ARCHIVING 80/90-LEVEL STORM $stormenv for $ymdh"
  echo " "
  set -x
  exit 0
fi

mkdir -p $DATA/arch_dir
cd  $DATA/arch_dir

namelen=${#STORM}+${#STORMID}
let cstart=namelen-2
let nameend=namelen-3

stid=` echo $STORM$STORMID | cut -c${cstart}-`
stid=`    echo $stid | tr '[A-Z]' '[a-z]'`
stnum=`   echo $stid | cut -c1-2`
baschar=` echo $stid | cut -c3`

stname=` echo $STORM | tr '[A-Z]' '[a-z]'`

case $baschar in
  l) basin=AL;;
  e) basin=EP;;
  c) basin=CP;;
esac

ATCFNAME=${basin}${stnum}${yy}

cp ${comdir}/*${stid}*                   .    
cp ${COMIN}/${stormlabel}.holdvars.txt   .

#--------------------------------------------------
# Now figure out which storm this is (1,2,3,4 or 5), 
# and copy over the corresponding output files....

for storm in 1 2 3 4 5
do

  txtfile=${comdir}/storm${storm}.holdvars.txt
  filecheck=` grep -i $stid $txtfile | wc -l`

  if [ $filecheck -gt 0 ]; then
    cp /com/output/${envir}/${PDY}/hwrf${storm}_${hh}* .
  fi

done


#------------------------------------------------
# Now tar the files over to the HPSS system....

hpssdir=/NCEPPROD/hpssprod/runhistory/2year/rh${yyyy}/hwrf/${stid}
tsmdir=/hsmprod/runhistory/2year/rh${yyyy}/hwrf/${stid}
errsum=0

if [ $TSM_FLAG = 'NO' ]
then
  #hpssmkdir $hpssdir
  hsi mkdir -p -m 755 $hpssdir
  export err=$?;let errsum=errsum+err;$EXhwrf/rhist_errchk.sh hwrf
  htar -cvf ${hpssdir}/${stname}.${ymdh}.tar ./* 
  export err=$?;let errsum=errsum+err;$EXhwrf/rhist_errchk.sh hwrf
elif [ $TSM_FLAG = 'YES' ]
then
  ssh ibmtsm1.ncep.noaa.gov "mkdir -p -m 755 $tsmdir"
  export err=$?;let errsum=errsum+err
  gtar -cvf ${DATA}/${stname}.${ymdh}.tar ./*
  export err=$?;let errsum=errsum+err
  $SCP $SCP_CONFIG ${DATA}/${stname}.${ymdh}.tar ibmtsm1.ncep.noaa.gov:${tsmdir}/${stname}.${ymdh}.tar
  export err=$?;let errsum=errsum+err  
fi

#
#  check if any the tarfiles were missed, and abort if so.
#
err=$errsum; /nwprod/util/ush/err_chk.sh

#####################################################################

# GOOD RUN
set +x
echo "**************JOB RHIST COMPLETED NORMALLY ON THE IBM"
echo "**************JOB RHIST COMPLETED NORMALLY ON THE IBM"
echo "**************JOB RHIST COMPLETED NORMALLY ON THE IBM"
set -x

msg="JOB $job HAS COMPLETED NORMALLY."
echo $msg
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################


