#!/bin/sh
#
# excdas.sh.sms
#
# This is the main driver script for the CDAS model.
# It runs the file prep, analysis (ssi), forecast (mrf),
# and post processing of the model.
#
# At 00z run a prediction to 384 hours
#

#####################################################################
echo
echo "------------------------------------------------"
echo "excdas.sh.sms - CDAS analysis and forcast"
echo "------------------------------------------------"
echo "History: Jul 20 2000 - Original script."
echo "       : Jul 10 2006 - added fcst to 384 hours"
echo
#####################################################################

set -x
date
cd $DATA

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

cat break > $pgmout

msg="CYCLE TIME FOR CDAS ANALYSIS IS $PDY$cyc"
postmsg "$jlogfile" "$msg"

export CNVGRIB=/nwprod/util/exec/cnvgrib
export WGRIB2=/nwprod/util/exec/wgrib2

if [ -z "$XLFRTEOPTS" ]; then
   export XLFRTEOPTS="nlwidth=132"
else
   export XLFRTEOPTS="${XLFRTEOPTS}:nlwidth=132"
fi


#                                    CYCLE files

# input files
export snow=cdas.t${cyc}z.snogrb
export sst=cdas.t${cyc}z.sstgrb
export ice=cdas.t${cyc}z.engicegrb
cp $COMIN/$snow $snow
cp $COMIN/$sst $sst
cp $COMIN/$ice $ice

# output file
export sfcanl=cdas.t${cyc}z.sfcanl


#                                    SSI files

# input files
export sges=cdas.t${cycm6hr}z.sf06
export bges=cdas.t${cycm6hr}z.bf06
export oldsig=cdas.t${cycm6hr}z.sanl
cp $COMINm6hr/$sges $sges
cp $COMINm6hr/$bges $bges
cp $COMINm6hr/$oldsig $oldsig

export prepqm=cdas.t${cyc}z.prepbufr
cp $COMIN/$prepqm $prepqm

# output files
export sanl=cdas.t${cyc}z.sanl


#                                    MODEL files

# output files
export newsges=cdas.t${cyc}z.sf06
export newbges=cdas.t${cyc}z.bf06
export newflx=cdas.t${cyc}z.sfluxgrbf06
export newdg3=cdas.t${cyc}z.dg3f06
export newznl00=cdas.t${cyc}z.znlf00
export newznl06=cdas.t${cyc}z.znlf06
export pgbf00=cdas.t${cyc}z.pgrbf00
export pgbf06=cdas.t${cyc}z.pgrbf06
if [ $cyc = 00 ] ; then
	fhini=0;fhout=12;fhmax=384
	fhr=$fhini; while [ $((fhr+=$fhout)) -le $fhmax ] ;do 
		eval export  sigf$fhr=sigft.$PDY${cyc}ft$fhr
		eval export  pgbf$fhr=cdas.t${cyc}z.pgrbf$fhr
		eval export  flxf$fhr=flx.$PDY${cyc}ft$fhr
	done
fi


########################## CYCLE ##########################

# check bges for right date code
$USHcdas/cdas_sigmatime.sh $bges $PDY$cyc


# run cycle
$USHcdas/cdas_cycle.sh $bges $sfcanl $ice $snow $sst $PDY$cyc
export err=$?; export pgm=cdas_cycle.sh; err_chk

########################### SSI ###########################

# check files for right date code

$USHcdas/cdas_sigmatime.sh $sges $PDY$cyc

$USHcdas/cdas_sigmatime.sh $oldsig $PDYm6hr$cycm6hr

export pgm=cdas_prepdate; . prep_step
export XLFUNIT_11="$prepqm"
export XLFUNIT_51=prepdate.out
startmsg
timex $EXECcdas/cdas_prepdate >> $pgmout 2> errfile
export err=$?; err_chk
cat errfile >> $pgmout
chkdate=`cat prepdate.out`
if [ "$chkdate" -ne "$PDY$cyc" ] ; then
   echo "bufr file has wrong date"
   err_exit "bufr file has wrong date"
fi


$USHcdas/cdas_ssi.sh

#cp SSI output to COM
cp $sanl $COMOUT/$sanl
cp $sfcanl $COMOUT/$sfcanl

########################### run model ###########################

endhour=6
$USHcdas/cdas_model.sh $sanl $sfcanl $endhour

# save output in COMOUT

cp sigft.$PDY${cyc}ft6 $COMOUT/$newsges
mv sigft.$PDY${cyc}ft6 $newsges
cp bges.$PDY${cyc}ft6 $COMOUT/$newbges
mv bges.$PDY${cyc}ft6 $newbges
cp dg3.$PDY${cyc}ft6 $COMOUT/$newdg3
mv dg3.$PDY${cyc}ft6 $newdg3
cp flx.$PDY${cyc}ft6 $COMOUT/$newflx
mv flx.$PDY${cyc}ft6 $newflx
cp znl.$PDY${cyc}ift0 $COMOUT/$newznl00
mv znl.$PDY${cyc}ift0 $newznl00
cp znl.$PDY${cyc}ft6 $COMOUT/$newznl06
mv znl.$PDY${cyc}ft6 $newznl06

$USHcdas/cdas_pgb.sh $sanl $pgbf00
cp $pgbf00 $COMOUT/$pgbf00
$USHcdas/cdas_pgb.sh $newsges $pgbf06
cp $pgbf06 $COMOUT/$pgbf06

# Convert pgrb and sfluxgrb to grib2
$CNVGRIB -g12 -p40 $newflx ${newflx}.grib2
$WGRIB2 -s ${newflx}.grib2 > ${newflx}.grib2.idx
$CNVGRIB -g12 -p40 $pgbf00 ${pgbf00}.grib2
$WGRIB2 -s ${pgbf00}.grib2 > ${pgbf00}.grib2.idx

# Copy GRIB2 to COMOUT
cp ${newflx}.grib2 $COMOUT/${newflx}.grib2
cp ${pgbf00}.grib2 $COMOUT/${pgbf00}.grib2
cp ${newflx}.grib2.idx $COMOUT/${newflx}.grib2.idx
cp ${pgbf00}.grib2.idx $COMOUT/${pgbf00}.grib2.idx

if [ "$SENDDBN" = "YES" ] ; then
   $DBNROOT/bin/dbn_alert MODEL CDAS_SFLUX $job $COMOUT/$newflx
   $DBNROOT/bin/dbn_alert MODEL CDAS_PGRB $job $COMOUT/$pgbf00
   if [ "$SENDDBN_GB2" = "YES" ] ; then
      $DBNROOT/bin/dbn_alert MODEL CDAS_SFLUX_GB2 $job $COMOUT/${newflx}.grib2
      $DBNROOT/bin/dbn_alert MODEL CDAS_PGRB_GB2 $job $COMOUT/${pgbf00}.grib2
   fi
fi

if [ $cyc = 00 ] ; then

	$USHcdas/cdas_model.sh $sanl $sfcanl $fhmax $fhout
	fhr=$fhini; while [ $((fhr+=$fhout)) -le $fhmax ] ;do 
		eval $USHcdas/cdas_pgb.sh \$sigf$fhr \$pgbf$fhr
	done
fi

################### save data in monthly directory ###############

tdir=tmp$PDY$cyc
[ -d "$tdir" ] && rm -rf "$tdir"
mkdir "$tdir"
[ $? -ne 0 ] && err_exit "Unable to make temp directory for mk_2d_3d.sh"
$USHcdas/cdas_mk_2d_3d.sh $DATA/$newflx $DATA/$newdg3 $DATA/grb2d$PDY$cyc \
  $DATA/grb3d$PDY$cyc $tdir

# fix grb2d
$utilexec/wgrib grb2d$PDY$cyc -s | grep -v ALBDO | \
  $utilexec/wgrib grb2d$PDY$cyc -i -grib \
  -o grb2d$PDY$cyc.tmp -s >/dev/null

mv grb2d$PDY$cyc.tmp $COMARC/grb2d$PDY$cyc
mv grb3d$PDY$cyc $COMARC/grb3d$PDY$cyc

mv $pgbf00 $COMARC/pgb.f00$PDY$cyc
mv $pgbf06 $COMARC/pgb.f06$PDY$cyc

if [ $cyc = 00 ] ; then
    wgrib=/nwprod/util/exec/wgrib
	fhr=$fhini; while [ $((fhr+=$fhout)) -le $fhmax ] ;do 
		# remove 600mb records in the $COMARC pgb copy 
        eval pgb=\$pgbf$fhr
		$wgrib $pgb|grep -v kpds7=600|$wgrib -i -grib -o $COMARC/pgb.f$fhr$PDY$cyc $pgb
		rm $pgb
		eval mv  \$flxf$fhr $COMARC/flx.f$fhr$PDY$cyc
	done
fi

export pgm=cdas_sgb;. prep_step
export XLFUNIT_11="$sanl"
export XLFUNIT_51="grbsanl$PDY$cyc"
startmsg
$EXECcdas/cdas_sgb
export err=$?; err_chk
mv grbsanl$PDY$cyc $COMARC/grbsanl$PDY$cyc

export pgm=cdas_sgb;. prep_step
export XLFUNIT_11="$newsges"
export XLFUNIT_51="grbsf06$PDY$cyc"
startmsg
$EXECcdas/cdas_sgb
export err=$?; err_chk
mv grbsf06$PDY$cyc $COMARC/grbsf06$PDY$cyc

export pgm=cdas_igb;. prep_step
# grib process number
IGEN=180
export XLFUNIT_11="$sanl"
export XLFUNIT_51="ipvanl$PDY$cyc"
startmsg
$EXECcdas/cdas_igb <<EOF
&NAMIGB ICEN=7,ICEN2=1,IGEN=$IGEN,
/
EOF
export err=$?; err_chk
mv ipvanl$PDY$cyc $COMARC/ipvanl$PDY$cyc

# cp $prepqm $COMARC/prepqm$PDY$cyc
mv $sanl $COMARC/sanl$PDY$cyc

mv $sfcanl $COMARC/sfcanl$PDY$cyc

mv $newsges $COMARC/sges$PDYp6hr$cycp6hr
mv $newbges $COMARC/bges$PDYp6hr$cycp6hr
mv $newznl00 $COMARC/znl.f00$PDY$cyc
mv $newznl06 $COMARC/znl.f06$PDY$cyc

# cycle files: save sst, snow and sea-ice and perhaps bges

mv $snow $COMARC/snogrb$PDY$cyc
mv $sst  $COMARC/sstgrb$PDY$cyc
mv $ice  $COMARC/icegrb$PDY$cyc
[ ! -s $COMARC/bges$PDY$cyc ] && mv $bges $COMARC/bges$PDY$cyc

# SSI files, 

[ ! -s $COMARC/prepqm$PDY$cyc ] && cp -p $prepqm $COMARC/prepqm$PDY$cyc
[ ! -s $COMARC/sges$PDY$cyc ] && mv $sges $COMARC/sges$PDY$cyc


########################################################

# GOOD RUN
set +x
echo " "
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " "
set -x


# save standard output
cat break $pgmout break > allout
cat allout
# rm allout

sleep 10

msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################

