#!/bin/sh
###testb
# remove lines between ###testb and ###teste for production
###teste
echo `date` $0 begin
################################################################################
#   Script:	exgefs_init.sh.sms
#
#   Author:	Mark Iredell
#   Date:	1997 January 08
#
#   Steward:	Richard Wobus
#   Center:	Environmental Modeling Center
#   Phone:	(301) 763-8000 x7712
#
#   Abstract:	Creates initial conditions for the global ensemble
#       	by perturbing the analysis with growing modes
#               extracted from the previous day's ensemble forecasts.
#       	This script is called by jobs JENS0 and JENS2 and calls
#               the utility script enstr_pair.sh to process each pair.
#
#   Modified:   12/11/98	replace hardwired globampl with fixed file
#                               save growth rate output file
#   Modified:   06/08/99	dummy script made to hold over on IBM until real
#                               exenstr.sh.sms script is ready
#   Modified:   09/09/99	modified for IBM SP
#   Modified:   04/17/00	increase to 5 pairs at 12z
#   Modified:   05/19/00	create perturbation at any two resolutions,
#                               currently set to create 5 pairs and a 
#                               low-resolution control run at T126
#   Modified:   04/30/01	generalize to run with create any number of
#                               levels and any number of tracer variables
#   Modified:   07/01/03        separate into outer and inner (single pair)
#                               scripts
#   Modified:   06/24/05        modify for relocation and 6-hour breeding
#   Modified:   09/09/05        use one-sided names with two-sided breeding
#
################################################################################

set +xa
echo " ------------------------------------------------------------"
echo "  "
echo "            GLOBAL ENSEMBLE INITIALIZATION "
echo "  "
echo "                `date`     "
echo "  "
echo "                   JOB  $job  "
echo "  "
echo "  "
echo "               FORECAST cycle TIME is $cycle"
echo "  "
echo " ------------------------------------------------------------"
echo "          processing info for this execution"
echo " Home directory is ............................ $HOMEGLOBAL"
echo " Processing directory for files.. ............. $DATA"
echo "  "
echo " Executable file directory is ................. $EXECGLOBAL"
echo " Fixed field directory is ..................... $FIXGLOBAL"
echo " Parameter directory is ....................... $PARMGLOBAL"
echo " Unix control language file is ................ $USHGLOBAL"
echo "  "
echo " Network id is ................................ $NET"
echo " Run id for $com processing is ................ $RUN"
echo "  "
echo " standard output in file ...................... $pgmout"
echo " unique processing id for run ................. $pid"
echo " YES SENDCOM means save com files ............. $SENDCOM"
echo " ------------------------------------------------------------"
set -xa

cd $DATA
  
################################################################################

echo globampl=$globampl smoothpwr=$smoothpwr relocfact=$relocfact relocflag=$relocflag

(( npairt = npair * 4 ))
nlist=
ipair=0
while (( ipair < npairt ))
do
  (( ipair = ipair + 1 ))
  if (( ipair < 10 )); then
    ipair=0$ipair
  fi
  nlist="$nlist $ipair"
done
echo nlist=$nlist
  
# these variables specify the resolution of low-resolution perturbation output
JCAPLR=62
LEVSLR=28
LONBLR=192
LATBLR=94
NTRACLR=3

# these variables specify the resolution of high-resolution perturbation output
JCAPHR=126
LEVSHR=28
LONBHR=384
LATBHR=190
NTRACHR=3

# cfsuffix identifies long forecast cycle 
# associated with this breeding job

if [[ "$cycsuffix" = "true" ]]; then
  export cfsuffix=".cycfs$cyc_fcst"
  export ensshort=yes
else
  export cfsuffix=""
  export ensshort=no
fi

echo cyc=$cyc cyc_fcst=$cyc_fcst
echo cycle=$cycle cycle_fcst=$cycle_fcst
echo fcstlong=$fcstlong
echo cycsuffix=$cycsuffix
echo cfsuffix=$cfsuffix
echo ensshort=$ensshort

export pdycycp=`/nwprod/util/exec/ndate -$fhrp $PDY$cyc`
export pdyp=`echo $pdycycp|cut -c1-8`
export cycp=`echo $pdycycp|cut -c9-10`

echo pdycycp=$pdycycp pdyp=$pdyp cycp=$cycp

# cfsuffixp identifies long forecast cycle
# associated with the previous forecast cycle
# from which tracking information will be used

if (( cycp != cyc_fcst )); then
  export cfsuffixp=".cycfs$cyc_fcst"
  export ensshortp=yes
else
  export cfsuffixp=""
  export ensshortp=no
fi

echo cfsuffixp=$cfsuffixp
echo ensshortp=$ensshortp


if (( 0 == 1 )); then
#
# begin relocation splitting section for c0
#
echo relocflag=$relocflag

(( relocpertflag = relocflag ))
export relocflag
export relocpertflag

if (( relocpertflag == 1 )); then
  # test for files necessary for relocation
  if [[ ! -e $COM/gefs.$pdyp/$cycp/sfcsig/gec00.t${cycp}z.sf$fhrp ]]; then
    export relocpertflag=0
  fi
  if [[ ! -e $GFSCOMIN/gfs.$cycle.syndata.tcvitals.tm00 ]]; then
    export relocpertflag=0
  fi
fi

if (( relocpertflag == 1 )); then
  echo Separate the storm and environment forecast fields for c0 setup begin

  yy=`echo $PDY|cut -c3-4`
  echo yy=$yy

if [[ -s /com/gfs/prod/gfs.$PDY/gfs.$cycle.pgrbf00 ]]; then
# f00 gfs tracking for relocation
  export cmodel=gfs00
###testb
if   [[ $envir = prod ]]; then
###teste
  sh ${utilscript}/extrkr.sh
###testb
elif [[ $envir = para ]] || \
     [[ $envir = test ]]; then
  sh /nw$envir/util/ush/extrkr.sh
else
  sh $basesource/nw$envir/util/ush/extrkr.sh
fi
###teste
  cat $COMIN/$cyc/init/zgfs.${cycle}.cyclone.trackatcfunix >trackatcfunix.in
else
# replaced by f00 gfs tracking above but retained for retrospective runs
  cat $GFSCOMIN/avn.${cycle}.cyclone.trackatcfunix >trackatcfunix.in
fi

  cp $GFSCOMIN/gfs.$cycle.syndata.tcvitals.tm00 tcvitals.in

  $USHGLOBAL/syndataxtrack.sh $PDY $cyc tcvitals.in trackatcfunix.in tcvitals.as

  cp $COM/gefs.$pdyp/$cycp/sfcsig/gec00.t${cycp}z.sf$fhrp gec00_presep

  for tauprefix in w x y z
  do
    if [[ $tauprefix = w ]]; then
      cyc_fcst_t=00
    elif [[ $tauprefix = x ]]; then
      cyc_fcst_t=06
    elif [[ $tauprefix = y ]]; then
      cyc_fcst_t=12
    elif [[ $tauprefix = z ]]; then
      cyc_fcst_t=18
    else
      cyc_fcst_t=cyc_fcst
    fi
    for file in $COM/gefs.$pdyp/$cycp/track/${tauprefix}[pc]??.t${cycp}z.cyclone.trackatcfunix
    do
      if [[ -s $file ]]; then
	cat $file >>tracks.atcfunix.$cyc_fcst_t
	taufound=yes
      fi
    done
    if [[ "$taufound" = "no" ]]; then
      if (( cyc_fcst == cycp )); then
	for file in $COM/gefs.$pdyp/$cycp/track/a[pc]??.t${cycp}z.cyclone.trackatcfunix
	do
	  if [[ -s $file ]]; then
	    cat $file >>tracks.atcfunix.$cyc_fcst_t
	    taufound=yes
	  fi
	done
      fi
    fi
  done
  if [[ "$taufound" = "no" ]]; then
    export relocpertflag=0
  fi

  echo Separate the storm and environment forecast fields for c0 setup end
fi

###testb
if [[ $envir = prod ]]; then
###teste
execseparate=$EXECGLOBAL/gefs_vortex_separate
###testb
elif [[ $envir = para ]] || [[ $envir = test ]]; then
execseparate=/nw$envir/exec//gefs_vortex_separate
else
execseparate=$basesource/nw$envir/exec//gefs_vortex_separate
fi
###teste
if (( relocpertflag == 1 )); then
  echo Separate the storm and environment forecast fields for c0 begin

  ln -s -f tcvitals.as fort.11

  ln -s -f $FIXGLOBAL/global_slmask.t126.grb    fort.12

  ln -s -f tracks.atcfunix.$cycp      fort.40
  ln -s -f gec00_presep              fort.21

  ln -s -f gec00_env              fort.51
  ln -s -f gec00_strm              fort.71

  export gesfhr=$fhrp
  export ensm=c00

  echo `date` $execseparate before
  echo $gesfhr $ensm | $execseparate
  filtrccc=$?
  echo `date` $execseparate after

  ls -al gec00_presep gec00_env gec00_strm

  if [[ -s gec00_strm ]]; then
    echo c0 storm file was created
  else
    echo c0 storm file was not created
    export relocpertflag=0
  fi

  echo Separate the storm and environment forecast fields for c0 end
fi
echo relocflag=$relocflag relocpertflag=$relocpertflag
#
# end relocation splitting section for c0
#

echo
echo `date` $0 poe part a begin
echo

rm -rf poescript

for ipair in $nlist
do
  echo npair=$npair
  if (( ipair <= npair )); then
    ipairh=ipair
  elif (( ipair <= 2 * npair )); then
    (( ipairh = ipair - npair ))
  elif (( ipair <= 3 * npair )); then
    (( ipairh = ipair - 2 * npair ))
  else
    (( ipairh = ipair - 3 * npair ))
  fi
###testb
if [[ $envir = prod ]]; then
###teste
  if (( ipairh > nhrpair )); then
    echo "$USHGLOBAL/gefs_init_pair_a.sh $ipair $npairt $nhrpair $JCAPLR $LEVSLR $LATBLR $LONBLR $NTRACLR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  else
    echo "$USHGLOBAL/gefs_init_pair_a.sh $ipair $npairt $nhrpair $JCAPHR $LEVSHR $LATBHR $LONBHR $NTRACHR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  fi
###testb
elif [[ $envir = para ]] || [[ $envir = test ]]; then
  if (( ipairh > nhrpair )); then
    echo "/nw$envir/ush/gefs_init_pair_a.sh $ipair $npairt $nhrpair $JCAPLR $LEVSLR $LATBLR $LONBLR $NTRACLR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  else
    echo "/nw$envir/ush/gefs_init_pair_a.sh $ipair $npairt $nhrpair $JCAPHR $LEVSHR $LATBHR $LONBHR $NTRACHR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  fi
else
  if (( ipairh > nhrpair )); then
    echo "$basesource/nw$envir/ush/gefs_init_pair_a.sh $ipair $npairt $nhrpair $JCAPLR $LEVSLR $LATBLR $LONBLR $NTRACLR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  else
    echo "$basesource/nw$envir/ush/gefs_init_pair_a.sh $ipair $npairt $nhrpair $JCAPHR $LEVSHR $LATBHR $LONBHR $NTRACHR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  fi
fi
###teste
done

chmod 755 poescript
ls -al $poescript
cat poescript

echo parallelinit=$parallelinit

if [[ "$parallelinit" = "yes" ]]; then
export MP_HOLDTIME=1000

export MP_PGMMODEL=mpmd
export MP_CMDFILE=poescript
export MP_LABELIO=yes
export MP_INFOLEVEL=3
export MP_STDOUTMODE=unordered
fi

echo before ls before poe

pwd
ls -ablp
echo
echo DATA=$DATA before poe

if [[ "$parallelinit" = "yes" ]]; then
poe
else
parallelinitsave=$parallelinit
export parallelinit=yes
poescript
wait
export parallelinit=$parallelinitsave
fi

echo DATA=$DATA after poe
pwd
ls -ablp

echo
echo after ls after poe

echo
echo `date` $0 poe part a end
echo
fi

if (( 0 == 1 )); then
#
# ----------------------- start  ET,  M. Wei, 2005.11.10
echo
echo `date` $0  ET part b begin
echo
#
globamplm=${globampletm}
globamplr=${globampletr}
globamplg=${globampletg}
echo globamplm=$globamplm
echo globamplr=$globamplr
echo globamplg=$globamplg
cp  sanl.in.pair01   sanl.in
cp  sanl.in  sanl.c0
#
#  Make namelist file
#
cat <<EOF >et_namens
  &NAMENS npair1=$npair,jcap=$JCAPHR,ilat=$LATBHR,ilon=$LONBHR,levs=$LEVSHR,
  ntrac=$NTRACHR,inflag=$inflag,icyc=$cyc,
  globamplm=$globamplm,globamplr=$globamplr,globamplg=$globamplg/
EOF
#
echo  "&DATNAMES" >>  et_namens
#
for ip in $nlist;   do
     (( iip = ip ))
     if (( iip < 10 )); then
        iip=0$iip
     fi
#
#  input ensemble member file names
#
       en1v="en1($ip)='finn.pair${iip}',"
       echo "$en1v" >> et_namens
       en2v="en2($ip)='finp.pair${iip}',"
       echo "$en2v" >> et_namens
#
#  output ensemble member file names
#
     enn1v="enn1($ip)='sanlgm${iip}n',"
     echo "$enn1v" >> et_namens
     enn2v="enn2($ip)='sanlgm${iip}p',"
     echo "$enn2v" >> et_namens
done
echo "/" >> et_namens
cat et_namens
# if there are no initial, use backup perts
#
   ip=1
   iip=0$ip
   for tz in t00z t06z t12z t18z;  do
   for np in 01 02 03 04 05 06 07;  do
      if [[ ! -s finn.pair${iip}  ]]; then
          ln -s -f  gefs.pertback.${tz}.n${np}.pair${iip} finn.pair${iip}
      fi 
      (( ip = ip + 1 ))
       (( iip = ip ))
       if (( iip < 10 )); then
          iip=0$iip
       fi
   done 
   done
   ip=1
   iip=0$ip
   for tz in t00z t06z t12z t18z;  do
   for np in 01 02 03 04 05 06 07;  do
       if [[ ! -s finp.pair${iip}  ]]; then
          ln -s -f  gefs.pertback.${tz}.p${np}.pair${iip} finp.pair${iip}
       fi
       (( ip = ip + 1 ))
       (( iip = ip ))
       if (( iip < 10 )); then
          iip=0$iip
       fi
   done 
   done
#
echo list input files for ET
ls -al | grep -e finn -e finp
#
#
#  run et.exe to read data and solve the matrix
# 
# run et with a mask

###testb
if [[ $envir = prod ]] || [[ $envir = para ]] || [[ $envir = test ]]; then
###teste
export exe_dir=/nw${envir}/exec
###testb
else
  export exe_dir=$basesource/nw${envir}/exec
fi
###teste
execet=$exe_dir/gefs_init_et

cp $FIXGLOBAL/global_geogr.monthly.ens  fort.48
echo using mask of $FIXGLOBAL/global_geogr.monthly.ens

echo `date` $execet before
$execet  < et_namens | tee -a $pgmout
echo `date` $execet after

#
echo change names after ET for gefs_init_pair_c.sh 
echo npair=$npair
echo  nlist=$nlist
for ip in $nlist
do
  echo ip=$ip
  if (( ip <= npair )); then
    (( ipairh=ip ))
  elif (( ip <= 2 * npair )); then
    (( ipairh = ip - npair ))
  elif (( ip <= 3 * npair )); then
    (( ipairh = ip - 2 * npair ))
  else
    (( ipairh = ip - 3 * npair ))
  fi
  if (( ipairh < 10 )); then
     ipairh=0${ipairh}
  fi 
  echo final ipairh=$ipairh
#
  mv  sanlgm${ip}n  sanlgm${ipairh}n.pair${ip}
  mv  sanlgm${ip}p  sanlgm${ipairh}p.pair${ip}
  cp  sanl.c0 sanl.c0.pair${ip}
# cp  sanl.in sanl.in.pair${ip}
done
#
echo
echo `date` $0  ET part b end
echo
echo  ================  list files after the end of  ET
pwd
ls -lt
#
# -------end of testing ET, start removing part b, test ET,  M. Wei, 2005.11.10
#
fi

if (( 0 == 0 )); then
echo
echo `date` $0 poe part c begin
echo

rm -rf poescript

for ipair in $nlist
do
  echo npair=$npair
  if (( ipair <= npair )); then
    ipairh=ipair
  elif (( ipair <= 2 * npair )); then
    (( ipairh = ipair - npair ))
  elif (( ipair <= 3 * npair )); then
    (( ipairh = ipair - 2 * npair ))
  else
    (( ipairh = ipair - 3 * npair ))
  fi
###testb
if [[ $envir = prod ]]; then
###teste
  if (( ipairh > nhrpair )); then
    echo "$USHGLOBAL/gefs_init_pair_c.sh $ipair $npairt $nhrpair $JCAPLR $LEVSLR $LATBLR $LONBLR $NTRACLR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  else
    echo "$USHGLOBAL/gefs_init_pair_c.sh $ipair $npairt $nhrpair $JCAPHR $LEVSHR $LATBHR $LONBHR $NTRACHR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  fi
###testb
elif [[ $envir = para ]] || [[ $envir = test ]]; then
  if (( ipairh > nhrpair )); then
    echo "/nw$envir/ush/gefs_init_pair_c.sh $ipair $npairt $nhrpair $JCAPLR $LEVSLR $LATBLR $LONBLR $NTRACLR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  else
    echo "/nw$envir/ush/gefs_init_pair_c.sh $ipair $npairt $nhrpair $JCAPHR $LEVSHR $LATBHR $LONBHR $NTRACHR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  fi
else
  if (( ipairh > nhrpair )); then
    echo "$basesource/nw$envir/ush/gefs_init_pair_c.sh $ipair $npairt $nhrpair $JCAPLR $LEVSLR $LATBLR $LONBLR $NTRACLR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  else
    echo "$basesource/nw$envir/ush/gefs_init_pair_c.sh $ipair $npairt $nhrpair $JCAPHR $LEVSHR $LATBHR $LONBHR $NTRACHR $globampl $fhrp $inflag $outflag $smoothpwr $relocfact &" >>poescript
  fi
fi
###teste
done

chmod 755 poescript
ls -al poescript
cat poescript

if [[ "$parallelinit" = "yes" ]]; then
export MP_HOLDTIME=1000

export MP_PGMMODEL=mpmd
export MP_CMDFILE=poescript
export MP_LABELIO=yes
export MP_INFOLEVEL=3
export MP_STDOUTMODE=unordered
fi

echo before ls before poe

pwd
ls -ablp
echo
echo DATA=$DATA before poe

if [[ "$parallelinit" = "yes" ]]; then
poe
else
poescript
wait
fi

echo DATA=$DATA after poe
pwd
ls -ablp

echo
echo after ls after poe

echo
echo `date` $0 poe part c end
echo

#
# combine output
#

for ipair in $nlist
do
  cat break >>$pgmout
  echo ipair=$ipair begin >>$pgmout
  cat break >>$pgmout >>$pgmout
  cat pair.$ipair/$pgmout >>$pgmout
  cat break >>$pgmout
  echo ipair=$ipair end >>$pgmout
  cat break >>$pgmout
  cat pair.$ipair/errfile >>$pgmout
  cat break >>$pgmout
done

#
echo
echo "############################# before pgmout ######################"
echo
cat $pgmout
echo
echo "############################# after pgmout ######################"
echo

################################################################################
#  Put output files.

if [[ $SENDCOM = YES ]];then

  cp scale.grid $COMOUT/$cyc/init/$RUN.$cycle.scalegrid$cfsuffix
  cp grwdeg.output $COMOUT/$cyc/init/$RUN.$cycle.grwthrate$cfsuffix

fi

# DBN alerts moved to ush/enstr_pair_c.sh

########################################################
fi

###testb
if [[ $envir = prod ]] || [[ $envir = para ]] || [[ $envir = test ]]; then
###teste
msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################# END OF SCRIPT #######################
$SMSBIN/endt
###testb
else
  echo skip delete for now
fi
###teste
echo `date` $0 end
