#!/bin/sh

#####################################################################
#
#  script for running breeding cycle from reananlysis data.
#
# History: Jul 2004 - original author: Jeffrey Whitaker
#          Jul 2004 - Modified by Luke Lin for the IBM SP"
#
#####################################################################

cd $DATA

set -x

FCSTDIR=$DATA

nmax='7'
nlist="1 2 3 4 5 6 7"

# calculate initial perturbations.

mkdir -p ${FCSTDIR}/c0

if test -s ${CDASCOMIN}/cdas.t00z.sanl
then
   $CDC_EXECUTIL/convert_sanldp ${CDASCOMIN}/cdas.t00z.sanl ${FCSTDIR}/c0/sanl.ieee
else
   msg="Unable to locate the file ${CDASCOMIN}/cdas.t00z.sanl"
   postmsg "$jlogfile" "$msg"
   err_exit
fi

if test -s ${CDASCOMIN}/cdas.t00z.sfcanl
then
   $CDC_EXECUTIL/convert_sfcanldp ${CDASCOMIN}/cdas.t00z.sfcanl ${FCSTDIR}/c0/sfcanl.ieee
else
   msg="Unable to locate the file ${CDASCOMIN}/cdas.t00z.sfcanl"
   postmsg "$jlogfile" "$msg"
   err_exit
fi

if test $SAVEGES = 'YES'
then
   cp ${FCSTDIR}/c0/sfcanl.ieee $GESdir/ensc0.t${cyc}z.sfcanl.ieee
   cp ${FCSTDIR}/c0/sanl.ieee $GESdir/ensc0.t${cyc}z.sanl.ieee
fi

cat > breeding.parm  << EOF1
 &NAMENS
  IPAIR=7,globampl=1.45
 &END
EOF1


export pgm=cdas_breeding
. prep_step

#  Create perturbed analyses.

for n in $nlist
do
  mkdir -p $FCSTDIR/p$n
  mkdir -p $FCSTDIR/n$n
  unit1=`expr $n + 10`
  unit2=`expr $n + 17`
  ln -s ${COMINm1}/ensn${n}.${cycle}.sf24 fort.$unit1
  ln -s ${COMINm1}/ensp${n}.${cycle}.sf24 fort.$unit2
done 
 
count=0

for n in $nlist
do
  if [ ! -f ${COMINm1}/ensn${n}.${cycle}.sf24 ]
  then
      count=`expr $count + 1`
  fi
  if [ ! -f ${COMINm1}/ensp${n}.${cycle}.sf24 ]
  then
     count=`expr $count + 1`
  fi
done

#if [ "$COLDSTART" = "YES" -o $count -ne 0 ]
if [ $count -ne 0 ]
then
   echo " "
   echo " ************* WARNING *******************************************  "
   echo " CDC ENS INIT job will cold start due to no sig file from prev day. "
   echo " ************* WARNING *******************************************  "
   echo " "
   ln -s $FIXCDCENS/cdas_pertdiff.1bk.ens    fort.39
   ln -s $FIXCDCENS/cdas_pertdiff.2bk.ens    fort.40
   ln -s $FIXCDCENS/cdas_pertdiff.3bk.ens    fort.41
   ln -s $FIXCDCENS/cdas_pertdiff.4bk.ens    fort.42
   ln -s $FIXCDCENS/cdas_pertdiff.5bk.ens    fort.43
   ln -s $FIXCDCENS/cdas_pertdiff.6bk.ens    fort.44
   ln -s $FIXCDCENS/cdas_pertdiff.7bk.ens    fort.45
fi

# analysis (input).

ln -s $FCSTDIR/c0/sanl.ieee fort.46
ln -s $FIXCDCENS/cdas_geogr.win.ens fort.48
 
for n in $nlist
do
  unit1=`expr $n + 50`
  unit2=`expr $n + 57`
  unit3=`expr $n + 70`
  unit4=`expr $n + 77`
  ln -s sanlgm${n}n            fort.$unit1
  ln -s sanlgm${n}p            fort.$unit2
  ln -s fcstdiff${n}.wrk       fort.$unit3
  ln -s scofgmod$n             fort.$unit4
done 
 
# more output files.

 ln -s grwdeg.output           fort.66
 ln -s scale.grid              fort.88
 ln -s grid.wrk                fort.89
 
# do the rescaling.

startmsg
$EXECCDCENS/cdas_breeding < breeding.parm  >> $pgmout 2> errfile
err=$?;export err; err_chk
 
if test $SAVEGES = 'YES'
then
   for n in $nlist
   do
      cp sanlgm${n}n $GESdir/ensn${n}.t${cyc}z.sanl.ieee
      cp sanlgm${n}p $GESdir/ensp${n}.t${cyc}z.sanl.ieee
      cp scofgmod${n} $GESdir/ensp${n}.t${cyc}z.scofgmod
   done 
fi

if test $SENDCOM = 'YES'
then
  cp scale.grid $COMOUT/gdas2.t${cyc}z.scalegrid
fi

if test $SAVEGES = 'YES'
then
  cp scale.grid $GESdir/gdas2.t${cyc}z.scalegrid
fi

#####################################################################
# GOOD RUN
set +x
echo "************** $job COMPLETED NORMALLY ON THE IBM SP"
echo "************** $job COMPLETED NORMALLY ON THE IBM SP"
echo "************** $job COMPLETED NORMALLY ON THE IBM SP"
set -x
#####################################################################

msg="HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"
############## END OF SCRIPT #######################

