#!/bin/ksh
if [ $# -lt 3 ] ; then
     echo "Usage: $0 startdate reanl2/amip rundir ocnic_date"
     exit 8
fi
set -ex

#################################################
# Script to get GFS initial conditions from HPSS#
#################################################

CDATE=$1           ;# yyyymmddhh for the intial state
initype=$2
RUNDIR=$3
ODATE=${4:-$CDATE}



yyyy=`echo $CDATE | cut -c1-4`
mm=`echo $CDATE | cut -c5-6`
dd=`echo $CDATE | cut -c7-8`
#
yyyyo=`echo $ODATE | cut -c1-4`
mmo=`echo $ODATE | cut -c5-6`
ddo=`echo $ODATE | cut -c7-8`

export hpsstar=${hpsstar:-/nw${envir}/util/ush/hpsstar}

#############################################
# specify initial conditions 
#############################################
cd $RUNDIR
export GFS_ICDIR=$RUNDIR
export MOM3_ICDIR=$RUNDIR

if [[ $initype = "reanl2" ]] ; then
  export SIGNAME=siganl.r2.
  export SFCNAME=sfcanl.r2.
fi
if [[ $initype = "amip" ]] ; then
  export SIGNAME=sigf
  export SFCNAME=sfcf
fi
export GFS_ICSIG=$SIGNAME$CDATE
export GFS_ICSFC=$SFCNAME$CDATE

export MOM3_ICNAME=restart.00$yyyyo.${mmo}.${ddo}.dta

sidate=$CDATE
mkdir -p $GFS_ICDIR
cd $GFS_ICDIR
if [[ $initype = "reanl2" ]] ; then
  export HPSS_SIGDIR=${HPSS_SIGDIR:-/hpssuser/g01/wx23ss/wx24ww/reanl2}
  export HPSS_SFCDIR=${HPSS_SFCDIR:-/hpssuser/g01/wx23ss/wx24ww/reanl2}
  export HPSS_SIGTAR=${HPSS_SIGTAR:-$SIGNAME${yyyy}${mm}_00.tar}
  export HPSS_SFCTAR=${HPSS_SFCTAR:-$SFCNAME${yyyy}${mm}_00.tar}
fi
if [[ $initype = "amip" ]] ; then
  export HPSS_SIGDIR=${HPSS_SIGDIR:-/hpssuser/g01/wx23pc/amip_${lm}r1}
  export HPSS_SFCDIR=${HPSS_SFCDIR:-/hpssuser/g01/wx23pc/amip_${lm}r1}
  export HPSS_SIGTAR=${HPSS_SIGTAR:-amip_${lm}r1.${yyyy}${mm}.sig.tar}
  export HPSS_SFCTAR=${HPSS_SFCTAR:-amip_${lm}r1.${yyyy}${mm}.sfc.tar}
fi
hpssfile=$HPSS_SIGDIR/$HPSS_SIGTAR
$hpsstar get $hpssfile $GFS_ICSIG
hpssfile=$HPSS_SFCDIR/$HPSS_SFCTAR
$hpsstar get $hpssfile $GFS_ICSFC
/bin/mv $GFS_ICSFC ${GFS_ICSFC}_org
if [[ $initype = "reanl2" ]] ; then
  /bin/mv $GFS_ICSIG sigtmp
fi
if [[ $initype = "amip" ]] ; then
  /bin/mv $GFS_ICSIG ${GFS_ICSIG}_org
  $pseudoexec ${GFS_ICSIG}_org sigtmp
fi
/nwprod/util/ush/overdate.sigma.sh $CDATE sigtmp $GFS_ICSIG
/nwprod/util/ush/overdate.sigma.sh $CDATE ${GFS_ICSFC}_org $GFS_ICSFC

#
# get Ocean initial condition
#
mkdir -p $MOM3_ICDIR
cd $MOM3_ICDIR
export HPSS_MOM3DIR=${HPSS_MOM3DIR:-/hpssuser/g01/wx24db/GODAS/G3R2TSp/y$yyyyo/Restart}
export HPSS_MOM3TAR=${HPSS_MOM3TAR:-restart.00$yyyyo.${mmo}.${ddo}.dta}
hpssfile=$HPSS_MOM3DIR/$HPSS_MOM3TAR
pftp hpsscore 4021 <<EOF
 cd $HPSS_MOM3DIR
 binary
 dir $HPSS_MOM3TAR
 get $HPSS_MOM3TAR $MOM3_ICDIR/$MOM3_ICNAME
 quit
EOF

export MOM3_ICFILE=$MOM3_ICDIR/$MOM3_ICNAME
ls -l $MOM3_ICFILE
/bin/cp $MOM3_ICDIR/$MOM3_ICNAME $MOM3_ICDIR/restart.dta
#
#
export SIGINP=$GFS_ICDIR/$GFS_ICSIG
ls -l $SIGINP
export SFCINP=$GFS_ICDIR/$GFS_ICSFC
ls -l $SFCINP

export nhourb=`/nwprod/exec/global_sighdr $SIGINP ifhr` ;#nhourb=0 for coldstart
if [[ $nhourb -ne 0 ]] ; then
echo "Forecast hour in initial file does not equal to zero."
exit 8
fi

