#!/bin/sh

set +x
#####################################################################
echo "----------------------------------------------------------"
echo "gfdl_pre_atmos_mkbogus2.sh - Script To Run Static Initialization for GFDL. "
echo "----------------------------------------------------------"
echo "History: JAN 1998 - Rewritten by David Michaud to standardize"
echo "                  - Script formerly known as runbogus."
#####################################################################
set -x

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

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

cd $DATA/bogus

echo $asym > asetnum

set +x
echo " "
echo "At top of gfdl_pre_mkbogus2.sh, asym = $asym"
echo " "
set -x

cp $DATA/fcst000 fcst000


# Bring in the asymmetries if the previous forecast is available

if [ $asym -eq 1 ]
then

   set +x
   echo
   echo  'link in the 12h forecasted symmetric vortex to compute asymmetries'
   echo
   set -x

   export pgm=gfdl_makeasm
   . prep_step

   ln -s -f mess-$STORMENV-$DATEIN-$HRIN                fort.18
###   ln -s -f makeasm.out                                 fort.72
   ln -s -f disturb                                     fort.77

   startmsg

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

   set +x
   echo
   echo  'Asymmetries have been computed'
   echo
   set -x
else
   set +x
   echo " "
   echo "In gfdl_pre_atmos_mkbogus2.sh, NOT in asym IF, asym = $asym"
   echo " "
   set -x
fi

#--------------------------------------------------------

export pgm=gfdl_rmenvir
. prep_step

ln -s -f fcst000                                     fort.10
ln -s -f rfavg                                       fort.15
ln -s -f mess-$STORMENV-$DATEIN-$HRIN                fort.18
ln -s -f asetnum                                     fort.65
ln -s -f env.datain.fcst000                          fort.87
ln -s -f vortex.struc                                fort.88
ln -s -f bogus.info                                  fort.89

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

cp  rfavg         $COMOUT/$ATCFNAME.rfavg.${PDY}${cyc}
cp  vortex.struc  $COMOUT/$ATCFNAME.vortex.struc.${PDY}${cyc}
rm fort.87 fort.89

cp $FIXhur/gfdl_huranal.data datain

###############################################################
# Now run a program that redefines the LGI (a sort of memory
# capacity) from 20 to 27.  This allows extra room for the
# ocean data, if we are running coupled.  If we are not
# running coupled, we still want to use the same executable,
# so change the LGI to 27 anyway. 
###############################################################

export pgm=gfdl_redef2027
. prep_step

ln -s -f fcst000                 fort.9
ln -s -f fcst000.lgi27           fort.77

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

rm fort.9 fort.20 fort.77

export pgm=gfdl_nwat
. prep_step
startmsg

ln -s -f fcst000.lgi27           fort.9
ln -s -f datahisr                fort.77

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

rm fort.9 fort.20 fort.77

###################################################################
#   SET UP TO RUN THE AXIS-SYMMETRIC MODEL TO CREATE THE SPECIFIC
#   BOGUS VORTEX DETERMINED FROM THE OBSERVED VALUES OF TAN. WIND.
#   THIS NEW VERSION USES RADIATION AND THE LATEST PHYSICS FROM THE 
#   FULL 3-D VERSION OF THE LATEST VERSION OF THE FORECAST MODEL
###################################################################
low=22
hig=45
echo 22 45 > lowhigh
echo $CONV $MOM > cumulus_parm_flags
stat=0
echo $stat > ocean_status

rm fort.51

owave=01
echo $owave > ocean_data_write_hours

#-------------------------------------------------------
# MPI processor configuration data...
# Copy in the processor maps for the X and Y directions,
# based on the number of CPU's we will be using....

XCPUS=1
YCPUS=1

if [ ${PARAFLAG} = 'YES' ]
then
  cp /nfsuser/g01/wx20tm/gfdl/scripts/rerun/limitx.${XCPUS} limit_dat_x
  cp /nfsuser/g01/wx20tm/gfdl/scripts/rerun/limity.${YCPUS} limit_dat_y
else
  cp $FIXhur/gfdl_limit_2nest_dat_x.${XCPUS} limit_dat_x
  cp $FIXhur/gfdl_limit_2nest_dat_y.${YCPUS} limit_dat_y
fi

cat > proc_count  <<eor
$XCPUS $YCPUS
eor

TSTEPS=21600
cat > input_dat << ..eor
21600 1440  001 2220    1    6    3   90    5    0  432
 0000${TSTEPS}${TSTEPS}    1    0    1    0 ${Y4PREFIX}$YEARA   $NMO   $DAY   $HRIN    0
    1 9999    1    0   60
    1    0   30  180 9999
..eor

if [ ${NUMLEVS} -eq 18 ]; then
  cp ${FIXhur}/gfdl_fort.7     gfdl_fort.7
  echo "Setting up 18 level model...."
else
  cp ${FIXhur}/gfdl_fort.7.l42 gfdl_fort.7
  echo "Setting up 42 level model...."
fi
cp  gfdl_fort.7   inrad.datain

cp ${FIXhur}/gfdl_ocean_dat .

echo $BULKINIT > bulkinit_flag

ln -s -f ocean_status                          fort.2
ln -s -f lowhigh                               fort.3
ln -s -f ocean_data_write_hours                fort.4
ln -s -f inrad.datain                          fort.7
ln -s -f datahisr                              fort.9
# ln -s -f datahisr                              fort.20
# ln -s -f datalat                               fort.22
# ln -s -f datatop                               fort.25
ln -s -f bulkinit_flag                         fort.28
ln -s -f datakintw                             fort.32
ln -s -f datakhwnd                             fort.33
ln -s -f stats_6                               fort.34
ln -s -f stats_1                               fort.35
ln -s -f axis_bogus                            fort.51
ln -s -f ihours                                fort.78
ln -s -f vortex_initial_profile                fort.81
ln -s -f proc_count                            fort.87
ln -s -f limit_dat_x                           fort.89
ln -s -f limit_dat_y                           fort.90
ln -s -f input_dat                             fort.91
ln -s -f vortex.struc                          fort.98
ln -s -f cumulus_parm_flags                    fort.99
ln -s -f gfdl_ocean_dat                        ocean_dat

export MP_SHARED_MEMORY=yes

export pgm=gfdl_axisymet
startmsg

$EXEChur/gfdl_axisymet < datain >> $pgmout 2> errfile
export err=$?;err_chk

cp vortex_initial_profile ${COMOUT}/${ATCFNAME}.vortex_initial_profile.${PDY}${cyc}
cp axis_bogus             ${COMOUT}/${ATCFNAME}.axis_bogus.${PDY}${cyc}

rm fort.98 fort.81 fort.51
rm fort.75 fort.76 fort.77 fort.78 fort.79 

export pgm=gfdl_setini
. prep_step

ln -s -f  fcst000                 fort.9
ln -s -f  nest.init               fort.77

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

#########################################################
# The bogus vortex is stored on unit 9.
# The NMC field is stored onto unit 55.
# Now put the bogus vortex onto a 3nested field.
# We also want to find the current and previous 
# surface pressure, because as an upgrade for the 2001
# system, we include this info (as pressure tendency)
# as a criterion for determining how much moisture to
# put into the bogus vortex.
#########################################################

##vitfile=/ptmp/wx20tm/syndat/syndat_tcvitals.${YYYY}
vitfile=/com/arch/prod/syndat/syndat_tcvitals.${YYYY}
ymdh_6h_ago=` /nwprod/util/exec/ndate -6 ${PDY}${HRIN}`
yymmdd_6h_ago=` echo ${ymdh_6h_ago} | cut -c3-8`
hh_6h_ago=`     echo ${ymdh_6h_ago} | cut -c9-10`

tcvit_6h_ago=` grep -i $stormid ${vitfile}           | \
        grep "${yymmdd_6h_ago} ${hh_6h_ago}"         | \
        awk '{ if (length($0) > 100) {print $0} }'   | \
        tail -1`

if [ ${#tcvit_6h_ago} -gt 0 ]
then
  echo "${tcvit_6h_ago}" | awk '{print $10}' >pressure_6h_ago
else
  echo -999 >pressure_6h_ago
fi

cp $COMOUT/$ATCFNAME.vitals.${YYMMDD}$cyc tcvit_now
cat tcvit_now | awk '{print $10}' >pressure_now

export pgm=gfdl_chgvortx
. prep_step

###ln -s -f  vortex.axi                fort.9
ln -s -f  axis_bogus                fort.9
ln -s -f  rfavg                     fort.15
ln -s -f  pressure_6h_ago           fort.21
ln -s -f  pressure_now              fort.22
ln -s -f  nest.init                 fort.55
ln -s -f  asetnum                   fort.65
ln -s -f  vortex.struc              fort.88
ln -s -f  noinit                    fort.77

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

rm fort.77 


export pgm=gfdl_saveini
. prep_step

ln -s -f  fcst000         fort.9
ln -s -f  noinit          fort.10
ln -s -f  new.init        fort.77

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


mv new.init $DATA/fcst000
mv env.datain.fcst000 $DATA/env.datain.fcst000

if test "$SENDCOM" = 'YES'
then
###   cp $DATA/fcst000 $COMOUT/$ATCFNAME.hour0
   cp $DATA/env.datain.fcst000 $COMOUT/$ATCFNAME.env.fcst000
fi

#cat $DATA/bogus/$pgmout >> $DATA/$pgmout

# rm -rf $DATA/bogus/*
