# 
# A utility to get the required files from HPSS to run a standalone GSI job.
# This is loosely based on R. Treadon's hpssget_gdas.sh
# This script is designed to be run interactively but can be incorporated into 
# other scripts to be run in batch if desired.
#
# One argument, the desired analysis date, is required.
# By default the output goes to  /ptmp/${USER}/data_sigmap/globalprod.${adate}
#

if [ $# -eq 1 ]; then 
  adate=$1
else
  echo Incorrect number of arguments.  Usage:
  echo "  Get_Initial_Files <analysis date>"
  exit 1
fi

set -ax

#machine="Zeus"
machine="WCOSS"

if [[ "$machine" = "Zeus" ]]; then
   nwprod=/scratch2/portfolios/NCEPDEV/global/save/Shrinivas.Moorthi/para 
   ptmp=/scratch2/portfolios/NCEPDEV/ptmp
   stmp=/scratch2/portfolios/NCEPDEV/stmp
   ndate=/scratch1/portfolios/NCEPDEV/da/save/Michael.Lueken/nwprod/util/exec/ndate 
elif [[ "$machine" = "WCOSS" ]]; then
   nwprod=/nwprod
   ptmp=/ptmpp1
   stmp=/stmpp1
   ndate=$nwprod/util/exec/ndate 
fi

# We are getting files for a single analysis date.  Some will come from the previous cycle ($gdate).
incr=06
gdate=`$ndate -${incr} $adate`

# Specify temporary directory (tmpdir) in which to run script.
# Specify directory in which to store retrieved files (savdir)
uname=`whoami`
exp=globalprod.$adate
JCAP=382   # We only get 382 for now  (emily: not used)

#tmpdir=/stmp/${uname}/get_initial_files.${adate}
#savdir=/ptmp/${USER}/data_sigmap/${exp}
tmpdir=$stmp/${uname}/get_initial_files.${adate}
savdir=$ptmp/${USER}/data_sigmap/${exp}

# Specify name of files to retrieve from this cycle
DFILESA="$DFILESA sgm3prep sgesprep sgp3prep prepbufr prepbufr.acft_profiles"
DFILESA="$DFILESA 1bamua.tm00.bufr_d 1bamub.tm00.bufr_d"
DFILESA="$DFILESA 1bhrs3.tm00.bufr_d 1bhrs4.tm00.bufr_d 1bmhs.tm00.bufr_d esamua.tm00.bufr_d"
DFILESA="$DFILESA esamub.tm00.bufr_d airsev.tm00.bufr_d mtiasi.tm00.bufr_d"
DFILESA="$DFILESA goesfv.tm00.bufr_d gpsro.tm00.bufr_d osbuv8.tm00.bufr_d  sptrmm.tm00.bufr_d"
#DFILESA="$DFILESA ssmit.tm00.bufr_d  spssmi.tm00.bufr_d ssmis.tm00.bufr_d amsre.tm00.bufr_d"
DFILESA="$DFILESA spssmi.tm00.bufr_d ssmisu.tm00.bufr_d amsre.tm00.bufr_d"
DFILESA="$DFILESA gome.tm00.bufr_d omi.tm00.bufr_d syndata.tcvitals.tm00"
DFILESA="$DFILESA atms.tm00.bufr_d cris.tm00.bufr_d crisf4.tm00.bufr_d"
DFILESA="$DFILESA satwnd.tm00.bufr_d"

# These come from the previous cycle
DFILESG="$DFILESG abias_pc abias satang radstat"
DFILESG="$DFILESG bf03 bf06 bf09"


# Make $tmpdir and $savdir.  Change directory to $tmpdir

rm -rf $tmpdir
mkdir -p $tmpdir

if [[ -s $tmpdir ]]; then
  cd $tmpdir
else
  echo Temporary directory $tmpdir cannot be created 
  echo Check definition of \$stmp variable in this script
  exit 1
fi

mkdir -p $savdir

# SWITCH is the date on which the runhistory tapes moved from 
# hsm to hpss.  The path to the tar files is different for 
# hsm and hpss, hence the need to know when the switch occured.
SWITCH=2003051818

yyyya=`echo $adate | cut -c1-4`
mma=`echo $adate | cut -c5-6`
dda=`echo $adate | cut -c7-8`
hha=`echo $adate | cut -c9-10`

yyyyg=`echo $gdate | cut -c1-4`
mmg=`echo $gdate | cut -c5-6`
ddg=`echo $gdate | cut -c7-8`
hhg=`echo $gdate | cut -c9-10`

#  Based on date in comparison to $SWITCH date, set path to tarfile
if [[ $adate -lt $SWITCH ]]; then
    hpssdira=/hpssuser/g02/wx15gv/s70/hsmprod/runhistory/rh${yyyya}/${yyyya}${mma}/${yyyya}${mma}${dda}
    hpssdirg=/hpssuser/g02/wx15gv/s70/hsmprod/runhistory/rh${yyyyg}/${yyyyg}${mmg}/${yyyyg}${mmg}${ddg}
else
    hpssdira=/NCEPPROD/hpssprod/runhistory/rh${yyyya}/${yyyya}${mma}/${yyyya}${mma}${dda}
    hpssdirg=/NCEPPROD/hpssprod/runhistory/rh${yyyyg}/${yyyyg}${mmg}/${yyyyg}${mmg}${ddg}
fi

tag=gdas
prefixa=./gdas1.t${hha}z
prefixg=./gdas1.t${hhg}z
tarfilea=com2_gfs_prod_gdas.${adate}.tar
tarfileg=com2_gfs_prod_gdas.${gdate}.tar

set +x
for file in $DFILESA; do DFA="$DFA ${prefixa}.$file" ; done
for file in $DFILESG; do DFG="$DFG ${prefixg}.$file" ; done
set -x


#     Loop to pull desired files off tape
#hpsstar=/nwprod/util/ush/hpsstar
#hpsstar=/NCEPPROD/nwprod/util/ush/hpsstar   
if [[ "$machine" = "Zeus" ]]; then
   hpsstar=/home/Emily.Liu/bin/hpsstar #emily:for now
#  hpsstar=$nwprod/util/ush/hpsstar    #emily:eventually     
elif [[ "$machine" = "WCOSS" ]]; then
   hpsstar=$nwprod/util/ush/hpsstar
fi

cpy=""
cpn=""
# Files from analysis cycle:
$hpsstar get $hpssdira/$tarfilea $DFA
for ft in $DFILESA; do
    fto=${prefixa}.$ft
    ftn=$savdir/$fto

    chmod 644 $fto
    cp $fto $ftn
    
    if [[ $? -eq 0 ]]; then
	cpy="$cpy $ft"
    else
	cpn="$cpn $ft"
    fi
done

# Files from guess cycle
$hpsstar get $hpssdirg/$tarfileg $DFG
for ft in $DFILESG; do
    fto=${prefixg}.$ft
    ftn=$savdir/$fto
  
#    $hpsstar get $hpssdirg/$tarfileg $fto
    chmod 644 $fto
    cp $fto $ftn
    
    if [[ $? -eq 0 ]]; then
	cpy="$cpy $ft"
    else
	cpn="$cpn $ft"
    fi
done

echo ${adate}:  File types     copied: $cpy
echo ${adate}:  File types NOT copied: $cpn
echo ${gdate}:  File types     copied: $cpy
echo ${gdate}:  File types NOT copied: $cpn

#Remove temporary directory
cd $tmpdir
rm -rf ./*
cd ../
rm -rf $tmpdir

exit