#!/bin/sh

set +x
#####################################################################
echo "------------------------------------------------------------------"
echo "gfdl_pre_atmos_mkbogus1.sh - Script To make asymmetries from the previous "
echo "forecast.  THE OUTPUT OF THIS SCRIPT WILL BE STORED ON TO UNIT (77)"  
echo " "  
echo "------------------------------------------------------------------"
#####################################################################
set -x

PS4=' + gfdl_pre_atmos_mkbogus1.sh line $LINENO: '

export pgmout=${DATA}/mkbogus1.${pid}.out
echo " "                                >${pgmout}
echo "++++++++++++++++++++++++++++++"  >>${pgmout}
echo " mkbogus1 output starts here  "  >>${pgmout}
echo "++++++++++++++++++++++++++++++"  >>${pgmout}
echo " "                               >>${pgmout}

set +x
echo " "
echo "  *-----------------------------------------*"
echo "  * Beginning of gfdl_pre_atmos_mkbogus1.sh *"
echo "  *-----------------------------------------*"
echo " "
set -x

mkdir -p $DATA/bogus
cd $DATA/bogus
sh $utilscript/setup.sh

cp $DATA/mess-$STORMENV-$DATEIN-$HRIN mess-$STORMENV-$DATEIN-$HRIN

###################################################################
#   DETERMINE IF THE FORECASTED FIELDS FROM THE 12 HOUR OLD FORECAST
#   ARE AVAILABLE. IF NOT, ASSYMETRIES WILL NOT BE ADDED
###################################################################

case $HRIN in
   00)hoo="12";;
   06)hoo="18";;
   12)hoo="00";;
   18)hoo="06";;
esac

if [ $HRIN -eq 6 -o $HRIN -eq 0 ]
then
   datb=`/nwprod/util/exec/ndate -12 ${PDY}${HRIN}`
else
   datb=$PDY$hoo
fi

if [ ${PARAFLAG} = 'YES' ]
then
  pymdh=${datb}
  parastormid=` basename $COMIN`
#  testf=${COMROOT}/${pymdh}/${parastormid}/$ATCFNAME.hour12
#  testf=${COMROOT}/${pymdh}/${STORMID}/$ATCFNAME.hour12
  testf=${COMROOT}/${pymdh}/${stormid}/$ATCFNAME.hour12
else
  testf=/com/hur/${envir}/hur.$datb/$ATCFNAME.hour12
fi

if [ -f $testf -a $DOASYM = "YES" ]
then

   cp $testf datahis
#
   if [ ${BULK} -eq 1 ]
   then

     export pgm=gfdl_fromnwat
     . prep_step
     startmsg

     ln -s -f  datahis           fort.9
     ln -s -f  datahis.fromnwat  fort.77

     $EXEChur/gfdl_fromnwat
     export err=$?;err_chk

     mv datahis.fromnwat  datahis

   fi

   export asym=1
   echo "export asym=$asym"  >>${setvarfile}

   echo "YES ASYMETRIES"

   set +x
   echo
   echo "we will run with asymmetries"
   echo
   set -x
else
   asym=0
   export asym
   echo "export asym=$asym"  >>${setvarfile}

   echo "NO ASYMETRIES"

   set +x
   echo
   echo "we will not run with asymmetries"
   echo
   set -x

   set +x
   echo " "
   echo "TIMING: date before call to mkbogus2 is `date`"
   echo " "
   set -x

   $EXhur/gfdl_pre_atmos_mkbogus2.sh
   err=$?
   if [ $err -eq 8 ]; then
     echo "ERROR: Non-zero return code from gfdl_pre_atmos_mkbogus2.sh, exiting...."
     ls -la
     exit 8
   else
     exit 0
   fi

   set +x
   echo " "
   echo "TIMING: date after  call to mkbogus2 is `date`"
   echo " "
   set -x

fi


###################################################################
#   asym = 0  <<< no asymmetries will be added
#   asym = 1  <<<    asymmetries will be added to the bogus
###################################################################
#
#  PROCEED TO FILTER OUT THE HURRICANE DISTURBANCE..............       
#

export pgm=gfdl_mk1hdeg
. prep_step

ln -s -f datahis       fort.10
ln -s -f onedeggrid    fort.11

startmsg
$EXEChur/gfdl_mk1hdeg  >>  $pgmout 2> errfile
export err=$?;err_chk

export pgm=gfdl_combine
. prep_step

ln -s -f onedeggrid    fort.9
ln -s -f datahis       fort.10
ln -s -f combgrid      fort.77

startmsg
$EXEChur/gfdl_combine  >>  $pgmout 2> errfile
export err=$?;err_chk

/bin/rm onedeggrid datahis

#
#  REMOVE THE FORECASTED VORTEX FROM THE FORECAST FIELD........
# 


aset=0
echo $aset > asetnum

export pgm=gfdl_rmenvir
. prep_step

ln -s -f combgrid                      fort.10
ln -s -f mess-$STORMENV-$DATEIN-$HRIN  fort.18
ln -s -f asetnum                       fort.65
ln -s -f vortex.struc.1                fort.88

startmsg
$EXEChur/gfdl_rmenvir  >>  $pgmout 2> errfile
export err=$?;err_chk

###################################################################
#   SET UP TO RUN THE AXIS-SYMMETRIC MODEL JUST ENOUGH TO CREATE
#   HISTORY TAPE               
###################################################################
touch datain
cat > datain <<eor
   10   10  001 2220    1    6    3   90    5    0  432
    0   10   10    1    0    1    0
    1 9999    1    0   30
eor

cp combgrid datahis
echo 22 45 > lowhig
echo $CONV > cumulus_parm_flag

export pgm=gfdl_axisymet
. prep_step

ln -s -f lowhig             fort.3
ln -s -f vortex.struc.1     fort.88
ln -s -f cumulus_parm_flag  fort.98

startmsg
$EXEChur/gfdl_axisymet.oldinit  < datain  >>  $pgmout 2> err.axisymet.oldinit
export err=$?;err_chk

export pgm=gfdl_setini
. prep_step

ln -s -f datahis        fort.9
ln -s -f setiniout      fort.77

startmsg
$EXEChur/gfdl_setini >> $pgmout 2>errfile
export err=$?;err_chk

#
#  OBTAIN THE ENVIRONMENTAL COMPONENT OF THE FORECASTED FIELD
#
export pgm=gfdl_makeenv
. prep_step

ln -s -f datahis        fort.9
ln -s -f setiniout      fort.55
ln -s -f makeenv.out    fort.77
ln -s -f vortex.struc.1 fort.88

startmsg
$EXEChur/gfdl_makeenv  >>  $pgmout 2> errfile
export err=$?;err_chk

export pgm=gfdl_setasm
. prep_step

mv  fort.77  fort.10

ln -s -f datahis        fort.9
#TM ln -s -f makeenv.out    fort.10
ln -s -f saveini.out    fort.77

startmsg
$EXEChur/gfdl_setasm >> $pgmout 2> errfile
export err=$?;err_chk

/bin/rm  fort.10
cp fort.77 ${COMIN}/${stormenv}.fcst_disturb.${PDY}${cyc}

/bin/rm  datahis

#
#  INTERPOLATE THE DISTURBNCE VORTEX TO A UNIFORM FINE MESH DOMAIN
#
export pgm=gfdl_inter
. prep_step

mv fort.77  fort.10

#TM ln -s -f diffd.out      fort.10
ln -s -f disturb        fort.77

startmsg
$EXEChur/gfdl_inter  >>  $pgmout 2> errfile
export err=$?;err_chk

/bin/rm  fort.*

set +x
echo " "
echo " !!!  END OF gfdl_pre_mkbogus1.sh  !!!"
echo " "
set -x

set +x
echo " "
echo "TIMING: date before call to mkbogus2 is `date`"
echo " "
set -x

$EXhur/gfdl_pre_atmos_mkbogus2.sh

set +x
echo " "
echo "TIMING: date after  call to mkbogus2 is `date`"
echo " "
set -x
