#!/usr/bin/ksh

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

PS4=' + exgfdl_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=exgfdl_post.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/prod/hur.${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=` echo $stormenv | awk '{print substr($0,length($0)-2,2)}'`
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=${#stormenv}
let cstart=namelen-2
let nameend=namelen-3

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

stname=` echo $stormenv | cut -c1-${nameend}`

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

lcdir=/tpc/save/guidance/storm-data/ncep

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

cp ${comdir}/${ATCFNAME}.datalat                      .
cp ${comdir}/${ATCFNAME}.datatop                      .
cp ${comdir}/${ATCFNAME}.hour0                        .
cp ${comdir}/${ATCFNAME}.hour12                       .
cp ${comdir}/${ATCFNAME}.fcst000.nobogus              .
cp ${comdir}/${ATCFNAME}.vitals.${yymmddhh}  .
cp ${comdir}/${ATCFNAME}.integrals                    .
cp ${comdir}/*${stid}*                                .
cp ${comdir}/*swath*ps                                .
cp ${comdir}/*precip*ps                               .
cp ${comdir}/coupled_status.*                         .
cp ${comdir}/rst.final.${ATCFNAME}.*                  .
cp ${comdir}/${stormenv}.${ymdh}.cdchfile             .
cp ${RST_PHASE3_DIR}/sst.gfs.${PDY}.dat          .
cp ${RST_PHASE3_DIR}/mask.gfs.${PDY}.dat         .
cp ${RST_PHASE3_DIR}/lonlat.gfs                       .
cp ${lcdir}/*loop_current*.dat                        .

rm *${stid}*.oceandata*
rm *${stid}*.*omb_ocean.tar*
rm *${stid}*.*fcst_disturb*

#--------------------------------------------------
# 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/prod/${PDY}/hur${storm}_${hh}* .
  fi

done


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

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

if [ $TSM_FLAG = 'NO' ]
then
  #hpssmkdir $hpssdir
  hsi mkdir -p -m 755 $hpssdir
  export err=$?;let errsum=errsum+err;$EXhur/rhist_errchk.sh hur
  htar -cvf ${hpssdir}/${stname}.${ymdh}.tar ./* 
  export err=$?;let errsum=errsum+err;$EXhur/rhist_errchk.sh hur
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 #######################


