#!/usr/bin/ksh

set +x
###########################################################################
echo "  -----------------------------------------------------------------"
echo "   gfdl_pre_master_start.sh - Script To start GFDL analysis system"
echo "   ----------------------------------------------------------------"
echo "   History: MAY 1994 - Script Written by Morris Bender"
echo "                       Phone Number (609-452-6559)"
echo "            DEC 1997 - Script Standardized by David Michaud"
###########################################################################
set -x

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

vtest="18"
ifive="5"
strong="33"
north="N"
south="S"
west="W"
east="E"
n="2"
gal="17.5"
hur="32.9"
str="25.7"

export gal hur str

###################################################################
# Bring in the current message file from NHC
###################################################################
cp $mesagdir/message$storm_num stormf

testrc="`wc -l stormf | cut -c1-8`"

if test $testrc -eq 0
then
   msg="Exiting Script.  The storm name is not found in the message file."
   postmsg "$jlogfile" "$msg"
   export err=2;err_chk
fi

# Use awk logic to format the input date to only have a 2-digit year,
# in case the input date was originally a 4-digit year.  This way, 
# we don't have to change any of the cut statements below here, 
# which otherwise would all have had to have been offset by 2
# characters....

awk '
{
  yycheck = substr($0,20,2)
  if ((yycheck == 20 || yycheck == 19) && (length($4) == 8)) {
    printf ("%s%s\n",substr($0,1,19),substr($0,22))
  }
  else {
    printf ("%s\n",$0)
  }
} ' stormf >${DATA}/message.temp

mv ${DATA}/message.temp stormf

# Get the Storm Environment name from the message file....

STORMENV=`awk '{printf"%s",$3;}' stormf`
STORMID=`awk '{printf"%s",$2}' stormf`
stormid=`echo $STORMID | tr '[A-Z]' '[a-z]'`

###################################################################
# Make the GFDL storm message 
###################################################################
testrc="`cut -c1 stormf | wc -w | cut -c1-8`"
if test $testrc -eq 0
then
   cut -c2-140 stormf > stormm
else
   cut -c1-140 stormf > stormm
fi

###################################################################
# Determine NHC atcf name
###################################################################
snum="`cut -c6-7 stormm`"
bas="`cut -c8   stormm`"

case $bas in
   L) basin="AL";;
   E) basin="EP";;
   C) basin="CP";;
   W) basin="WP";;
   *) :;;
esac

export ATCFID=`awk '{printf"%s",$2}' stormf`
export atcfid=`echo $ATCFID | tr '[A-Z]' '[a-z]'`

if [ ${COUPLED_FLAG} = 'YES' ]
then
  COUPLED=YES
else
  COUPLED=NO
fi

export STORMENV="$STORMENV$snum$bas"

###################################################################
# Determine Initial Hour
###################################################################
export HRIN="`cut -c27-28 stormm`"

##########################################################
YEARA="`cut -c20-21 stormm`"
NMO="`cut  -c22-23  stormf`"
DAY="`cut  -c24-25  stormf`"
datec="`cut  -c20-25  stormf`"
hourc="`cut  -c27-28  stormf`"
cudh="$datec$hourc"
export ATCFNAME="$basin$snum$YEARA"

case $NMO in
  01) DATEIN="JAN"$DAY;;
  02) DATEIN="FEB"$DAY;;
  03) DATEIN="MAR"$DAY;;
  04) DATEIN="APR"$DAY;;
  05) DATEIN="MAY"$DAY;;
  06) DATEIN="JUN"$DAY;;
  07) DATEIN="JUL"$DAY;;
  08) DATEIN="AUG"$DAY;;
  09) DATEIN="SEP"$DAY;;
  10) DATEIN="OCT"$DAY;;
  11) DATEIN="NOV"$DAY;;
  12) DATEIN="DEC"$DAY;;
esac   

YYMMDD=$YEARA$NMO$DAY
stormenv=`echo  $STORMENV | tr '[A-Z]' '[a-z]' `
export NMO DAY YEARA DATEIN MOC YYMMDD stormenv

if   [ $YEARA -gt 0 -a $YEARA -lt 50 ]; then
  export CENT=21
  export Y4PREFIX=20
elif [ $YEARA -gt 50 ]; then
  export CENT=20
  export Y4PREFIX=19
elif [ $YEARA -eq 0 ]; then
  export CENT=20
  export Y4PREFIX=20
fi

export YYYY=` echo $PDY | cut -c1-4`
export YY=`   echo $PDY | cut -c3-4`
export MM=`   echo $PDY | cut -c5-6`
export DD=`   echo $PDY | cut -c7-8`
export HH=${cyc}

if [ -s ${COMIN}/ocean_status.${ATCFNAME}.${PDY}${cyc} ]
then 
  rm ${COMIN}/ocean_status.${ATCFNAME}.${PDY}${cyc}
fi

###################################################################
# Put a copy of current nhc storm message file to tmp space:
###################################################################
cp   stormf  fetchs-$STORMENV-$DATEIN-$HRIN                

if test "$SENDCOM" = 'YES'
then
   cp fetchs-$STORMENV-$DATEIN-$HRIN $COMOUT/$ATCFNAME.vitals.${YYMMDD}$cyc
fi

cat stormf >>${STOREhur}/gfdl.vitals.${YYYY}

###################################################################
# Continue to build the GFDL message file.
# Determine (lat,lon)
###################################################################
lat="`cut -c32-34  stormm`"
nnd="`cut -c35 stormm`"

if test $nnd = $south
then
   lat=-$lat
fi
echo  $lat > ilat

# Do NOT run coupled for any NH storm that is south of 10N
# UPDATE: Leave this out.... in 2006, we'll run southern storms

lat=`expr $lat`
# if [ $nnd = $north -a $lat -lt 100 ]
# then
#   COUPLED=NO
# fi

lon="`cut -c37-40  stormm`"
testrc="`echo $lon | cut -c1`"
if test $testrc = 0
then
   lon="`echo $lon | cut -c2-4`"
fi

wwd="`cut -c41 stormm`"
if test $wwd = $west -a $lon -gt 0
then
   lon=-$lon
fi
if test $wwd = $east -a $lon -gt 0
then
   lon=`expr 3600 - $lon`
   lon=-$lon
fi
echo  $lon > ilon

###################################################################
# Determine other parameters (storm direction and speed and pressure)
###################################################################
ddd="`cut -c43-45  stormm`"
if test $ddd != "000"
then
   while [ `echo $ddd | cut -c1` = "0" ]
   do
      ddd="`echo $ddd | cut -c2-`"
   done
fi
echo  $ddd > dre

sss="`cut -c47-49  stormm`"
if test $sss != "000"
then
   while [ `echo $sss | cut -c1 ` = "0" ]
   do
      sss="`echo $sss | cut -c2-`"
   done
fi
echo  $sss > spd

pp="`cut -c51-54  stormm`"
while [ `echo $pp | cut -c1` = "0" ]
do
   pp="`echo $pp | cut -c2-`"
done
testp=$pp

echo  $pp > pmin
pmxx="`cut -c56-59  stormm`"
echo  $pmxx  > pmax
rb="`cut -c62-64  stormm`"

###################################################################
# Determine radius and magnitude of the maximum wind
###################################################################
vmax="`cut -c65-67  stormm`"
while [ `echo $vmax | cut -c1` = "0" ]
do
   vmax="`echo $vmax | cut -c2-`"
done
testw=$vmax

testv="`cut -c69-71  stormm`"
if [ $testv = '000' ]
then
    
  rmxx=45

else
    
  while [ `echo $testv | cut -c1` = "0" ]
  do
     testv="`echo $testv | cut -c2-3`"
  done
  rmxx=$testv

  if test $testv -lt 26
  then
     rmxx="25"
  fi
    
  if test $testv -gt 150
  then
     rmxx="150"
  fi

fi

###################################################################
# Do some error checks before continuing:
# Check if the winds and minimum pressures are reasonable
# in the current message file...if not we must exit the script
###################################################################
testw="`cut -c65-67  stormm`"

if test $testw -lt 10 -o $testw -gt 90
then
   export err=2;err_chk
fi

if test $testp -lt 850 -o $testp -gt 1040
then
   export err=3;err_chk
fi

###################################################################
# Determine the radius of the gale force winds
###################################################################
maxgale=0

nexd="`cut -c73-76  stormm`"
while [ `echo $nexd | cut -c1` = "0" ]
do
   nexd="`echo $nexd | cut -c2-`"
done

if [ ${nexd} -gt ${maxgale} ]; then
  maxgale=${nexd}
fi

sexd="`cut -c78-81  stormm`"
while [ `echo $sexd | cut -c1` = "0" ]
do
   sexd="`echo $sexd | cut -c2-`"
done

if [ ${sexd} -gt ${maxgale} ]; then
  maxgale=${sexd}
fi

swxd="`cut -c83-86  stormm`"
while [ `echo $swxd | cut -c1` = "0" ]
do
   swxd="`echo $swxd | cut -c2-`"
done

if [ ${swxd} -gt ${maxgale} ]; then
  maxgale=${swxd}
fi

nwxd="`cut -c88-91  stormm`"
while [ `echo $nwxd | cut -c1` = "0" ]
do
   nwxd="`echo $nwxd | cut -c2-`"
done

if [ ${nwxd} -gt ${maxgale} ]; then
  maxgale=${nwxd}
fi

dpf="`cut -c93 stormm`"

###################################################################
# Determine radius of 50 knot winds if available
###################################################################
sne="`cut -c95-98  stormm`"
while [ `echo $sne | cut -c1` = "0" ]
do
   sne="`echo $sne | cut -c2-`"
done

sse="`cut -c100-103  stormm`"
while [ `echo $sse | cut -c1` = "0" ]
do
   sse="`echo $sse | cut -c2-`"
done

ssw="`cut -c105-108  stormm`"
while [ `echo $ssw | cut -c1` = "0" ]
do
   ssw="`echo $ssw | cut -c2-`"
done

snw="`cut -c110-113  stormm`"
while [ `echo $snw | cut -c1` = "0" ]
do
   snw="`echo $snw | cut -c2-`"
done

###################################################################
#  determine (lat,lon) and verification hour of the forecast position
###################################################################
fhou="`cut -c115-116  stormm` "
echo  $fhou  > fhour
testh="`wc -w fhour | cut -c1-9`"
if test $testh = 0
then
   fhou="-9     "
   echo  $fhou  > fhour
fi

flat="`cut -c118-120   stormm`  "
echo  $flat  > soutl

flon="`cut -c123-126  stormm`  "
testrc="`echo $flon | wc -w | cut -c1-10`"
if test $testrc = 0
then
   flon="-10000"
else
   while [ `echo $flon | cut -c1` = "0" ]
   do
      flon="`echo $flon | cut -c2-`"
   done
fi

flon=`expr $flon`

wd="`cut -c127 stormm` "
if test $wd = $west -a $flon -gt 0
then
   flon=-$flon
fi
if test $wd = $east -a $flon -gt 0
then
   let flon=3600-$flon
   flon=-$flon
fi
echo  $flon  > westl

###################################################################
# Radius rb is radius of last closed isobar.  We do a simple 
# doubling of it to get the extent of the storm (rrbn).  Check to 
# make sure that rrbn is greater than any of the gale force radii.
# If not, adjust it to be 50 km greater than the max gale radius.
###################################################################
###  test Morrisr rrbn=`expr $rb + $rb`
# let rrbn=3*rb/2
let rrbn=5*rb/4
#
if test $rrbn -gt 1000
then
   rrbn="1000"
fi
if test $rrbn -lt 500
then
   rrbn="500"
fi

if [ ${rrbn} -le ${maxgale} ]; then
  let rrbn=maxgale+50
fi

echo  $rrbn > rbn

###################################################################
# Determine whether the storm winds exceed 50 knots in any quadrant 
###################################################################
if test $sne -gt "0" -o $sse -gt "0" -o $ssw -gt "0" -o $snw -gt "0"
then
   tests="1"
else
   tests="0"
fi

###################################################################
#   determine whether the storm winds exceed gale force in any quadrant 
###################################################################
if test $nexd -gt "0" -$sexd -gt "0" -o $swxd -gt "0" -o $nwxd -gt "0"
then
   galet="1"
else
   galet="0"
fi

###################################################################
# The program now will estimate the western most boundary
# for the outer nest.  It will either obtain it from the marine 
# advisory or the estimate of the storm motion from the current 
# direction and speed. 
#
# IMPORTANT: 
#   This value WILL be the FIRST number in the storm message file
###################################################################
pgm=gfdl_nhcmsg
export pgm;. prep_step

ln -s -f dre    fort.1
ln -s -f spd    fort.2
ln -s -f westl  fort.3
ln -s -f ilon   fort.4
ln -s -f ilat   fort.7
ln -s -f fhour  fort.9 
ln -s -f westbd fort.10
ln -s -f rlon   fort.11
ln -s -f rlat   fort.12

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

rwest="`cat westbd`"
cat  westbd  > final
cp westbd ${COMIN}/westbd.${ATCFNAME}.${PDY}${cyc}

###################################################################
# Determine the correct storm depth
###################################################################
deep="2"

case $dpf in
   D) deep="3 ";;
   X) deep="2 ";;
   S) deep="1 ";;
   M) deep="2 ";;
   *) :;;
esac

###################################################################
#   modify the storm depth for weak storms
###################################################################
if test $vmax -lt 24
then
   deep="2"
fi
echo  $deep >  deeps
vmax="`cut -c65-67  stormm`"

###################################################################
# Determine whether the storm is a hurricane
###################################################################
if test $vmax -le $strong
then
   ihurr="0"
else
   ihurr="1"
fi
echo $ihurr > hurr

###################################################################
# Find the correct number of observations for the quadrants
###################################################################
if test $galet = 0
then
   n="1"
else
   n="2"
fi

###################################################################
# If the radius of gale force winds is inside the radius of 
# maximum winds we will set the number of obervations to zero.
###################################################################
if test $nexd -lt $rmxx
then
   nne="0"
else
   nne=$n
fi
if test $sexd -lt $rmxx
then
   nse="0"
else
   nse=$n
fi
if test $swxd -lt $rmxx
then
   nsw="0"
else
   nsw=$n
fi
if test $nwxd -lt $rmxx
then
   nnw="0"
else
   nnw=$n
fi

###################################################################
# If the 50 knot radius is available, the number of observations is
# increases by one
###################################################################
if test $sne -gt 0 -a $sne -ge $rmxx
then
   nne=`expr $nne + 1`
fi
if test $sse -gt 0 -a $sse -ge $rmxx
then
   nse=`expr $nse + 1`
fi
if test $ssw -gt 0 -a $ssw -ge $rmxx
then
   nsw=`expr $nsw + 1`
fi
if test $snw -gt 0 -a $snw -ge $rmxx
then
   nnw=`expr $nnw + 1`
fi

###################################################################
# If the radius of 50 knot winds equals the radius of maximum
# winds we must make an adjustment.
###################################################################
if test $sne = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi
if test $sse = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi
if test $ssw = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi
if test $snw = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi


###################################################################
# If the radius of gale force winds equals the radius of maximum 
# winds we must make an adjustment.
###################################################################
if test $nexd = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi
if test $nexd = $rmxx -a $vmax -lt $vtest
then
   nne="0"
fi
if test $sexd = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi
if test $sexd = $rmxx -a $vmax -lt $vtest
then
   nse="0"
fi
if test $swxd = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi
if test $swxd = $rmxx -a $vmax -lt $vtest
then
   nsw="0"
fi
if test $nwxd = $rmxx -a $vmax -ge $vtest
then
   rmxx=`expr $rmxx - $ifive`
fi
if test $nwxd = $rmxx -a $vmax -lt $vtest
then
   nnw="0"
fi

###################################################################
# Check if the radius of 50 knot winds is beyond the radius
# of the gale force winds.  If it is we will ignore those 50kt 
# wind radius.
###################################################################
if test $sne -ge $nexd -a $sne -gt 0
then
   nne=`expr $nne - 1`
fi
if test $sse -ge $sexd -a $sse -gt 0
then
   nse=`expr $nse - 1`
fi
if test $ssw -ge $swxd -a $ssw -gt 0
then
   nsw=`expr $nsw - 1`
fi
if test $snw -ge $nwxd -a $snw -gt 0
then
   nnw=`expr $nnw - 1`
fi

###################################################################
# Now set the numbers of obersvations
###################################################################
if test $nne = 0 -a $nse = 0 -a $nsw = 0 -a $nnw = 0
then
   galet="0"
fi
if test $galet = 0
then
   nne="1"
   nse="1"
   nsw="1"
   nnw="1"
fi

echo  $n > num 
echo  $nne >numne
echo  $nse >numse
echo  $nsw >numsw
echo  $nnw >numnw
echo  $nexd > nerd
echo  $sexd > serd
echo  $swxd > swrd
echo  $nwxd > nwrd 
echo  $rmxx > rmax 
echo  $vmax > vmaxx 
echo  $sne > stne
echo  $sse > stse
echo  $ssw > stsw
echo  $snw > stnw
echo  $str > strm
echo  $gal > gale

###################################################################
# Number of obersvations has been determined.
# Finish putting together the storm message file.
# This is done for each quadrant
###################################################################
cat  rlon  rlat >> final
cat  rbn >> final

###################################################################
# Number of observations for each quadrant added in next
###################################################################
cat numne numse numsw numnw >> final
cat pmin >> final 
cat pmin >> final
cat hurr deeps >> final
cat pmax  pmax   >> final

###################################################################
# Add in quadrant one
###################################################################
if test $nne != 0
then 
   cat rmax  >> final
   if test $tests = 1 -a $sne -lt $nexd -a $sne -gt 0 -a $sne -gt $rmxx
   then
      cat stne >> final
   fi
   if test $galet != 0
   then
      cat nerd >> final
   fi
   cat vmaxx >> final
   if test $tests = 1 -a $sne -lt $nexd -a $sne -gt 0 -a $sne -gt $rmxx
   then
      cat strm >> final
   fi
   if test $galet != 0
   then
      cat gale  >> final
   fi
fi

###################################################################
# Add in quadrant two
###################################################################
if test $nse != 0
then
   cat rmax  >> final
   if test $tests = 1 -a $sse -lt $sexd -a $sse -gt 0 -a $sse -gt $rmxx
   then
      cat stse >> final
   fi
   if test $galet != 0
   then
      cat serd >> final
   fi
   cat vmaxx >> final
   if test $tests = 1 -a $sse -lt $sexd -a $sse -gt 0 -a $sse -gt $rmxx
   then
      cat strm >> final
   fi
   if test $galet != 0
   then
      cat gale  >> final
   fi
fi

###################################################################
# Add in quadrant three
###################################################################
if test $nsw != 0
then
   cat rmax  >> final
   if test $tests = 1 -a $ssw -lt $swxd -a $ssw -gt 0 -a $ssw -gt $rmxx
   then
      cat stsw >> final
   fi
   if test $galet != 0
   then
      cat swrd >> final
   fi
   cat vmaxx >> final
   if test $tests = 1 -a $ssw -lt $swxd -a $ssw -gt 0 -a $ssw -gt $rmxx
   then
      cat strm >> final
   fi
   if test $galet != 0
   then
      cat gale  >> final
   fi
fi

###################################################################
#    add in quadrant four
###################################################################
if test $nnw != 0
then
   cat rmax  >> final
   if test $tests = 1 -a $snw -lt $nwxd -a $snw -gt 0 -a $snw -gt $rmxx
   then
      cat stnw >> final
   fi
   if test $galet != 0
   then
      cat nwrd >> final
   fi
   cat vmaxx >> final
   if test $tests = 1 -a $snw -lt $nwxd -a $snw -gt 0 -a $snw -gt $rmxx
   then
      cat strm >> final
   fi
   if test $galet != 0
   then
      cat gale  >> final
   fi
fi

###################################################################
# Finish up the script
###################################################################
cp final mess-$STORMENV-$DATEIN-$HRIN

if test "$SENDCOM" = YES
then
   messfile=`echo mess-$STORMENV-$DATEIN-$HRIN | tr [A-Z] [a-z]`
   cp  mess-$STORMENV-$DATEIN-$HRIN $COMOUT/$messfile
fi

#---------------------------------------------------------------
# This next bit of stuff is to determine whether or not a 5-day
# forecast will be run.  The first statement follows the current
# rule for production of only doing 5-day forecasts for the 00z
# or 12z cycle.  The second if statement is for our own parallel
# runs, so that we may override this 00z/12z restriction if we
# want to.
#
#  -Michaud - Changed Logic to run FCST5DAY for every cycle 03/06/2002
#---------------------------------------------------------------

if [ ${FCST5DAY} = 'YES' ]
then
  FCST5DAY=YES
else
  FCST5DAY=NO
fi

if [ ${PARAFLAG} = 'YES' -a ${FCST5DAY} = 'YES' ]
then
  FCST5DAY=YES
fi

#--------------------------------------------------------------
# Echo variables out to a temporary file so that the next job
# can read them in.
#--------------------------------------------------------------

setvarfile=${COMOUT}/${stormlabel}.holdvars.txt
if [ -s ${setvarfile} ]; then
  rm ${setvarfile}
fi

echo "export YYMMDD=$YYMMDD"              >${setvarfile}
echo "export YMDH=${PDY}${cyc}"          >>${setvarfile}
echo "export YYYY=${YYYY}"               >>${setvarfile}
echo "export YY=${YY}"                   >>${setvarfile}
echo "export MM=${MM}"                   >>${setvarfile}
echo "export DD=${DD}"                   >>${setvarfile}
echo "export HH=${HH}"                   >>${setvarfile}
echo "export YYYYMMDD=${YYYY}${MM}${DD}" >>${setvarfile}
echo "export yyyymmdd=${YYYY}${MM}${DD}" >>${setvarfile}
echo "export YEARA=$YEARA"               >>${setvarfile}
echo "export CENT=$CENT"                 >>${setvarfile}
echo "export Y4PREFIX=$Y4PREFIX"         >>${setvarfile}
echo "export NMO=$NMO"                   >>${setvarfile}
echo "export DAY=$DAY"                   >>${setvarfile}
echo "export DATEIN=$DATEIN"             >>${setvarfile}
echo "export PDY=$PDY"                   >>${setvarfile}
echo "export HRIN=$HRIN"                 >>${setvarfile}
echo "export ATCFNAME=$ATCFNAME"         >>${setvarfile}
echo "export ATCFID=$ATCFID"             >>${setvarfile}
echo "export atcfid=$atcfid"             >>${setvarfile}
echo "export STORMENV=$STORMENV"         >>${setvarfile}
echo "export stormenv=$stormenv"         >>${setvarfile}
echo "export DATEIN=$DATEIN"             >>${setvarfile}
echo "export HRIN=$HRIN"                 >>${setvarfile}
echo "export nestno=$nestno"             >>${setvarfile}
echo "export FCST5DAY=$FCST5DAY"         >>${setvarfile}
echo "export COUPLED=$COUPLED"           >>${setvarfile}
echo "export STORMID=$STORMID"           >>${setvarfile}
echo "export stormid=$stormid"           >>${setvarfile}
echo "export DOASYM=$DOASYM"             >>${setvarfile}
echo "export FORAENV=${FORAENV}"         >>${setvarfile}
echo "export mesagdir=${mesagdir}"       >>${setvarfile}
echo "export NUMLEVS=${NUMLEVS}"         >>${setvarfile}
echo "export CONV=${CONV}"               >>${setvarfile}
echo "export MOM=${MOM}"                 >>${setvarfile}
echo "export GRID_CONFIG=${GRID_CONFIG}" >>${setvarfile}
echo "export BULK=${BULK}"               >>${setvarfile}
echo "export BULKINIT=${BULKINIT}"       >>${setvarfile}
echo "export RUN_PRE_OCEAN_IF_COUPLED=${RUN_PRE_OCEAN_IF_COUPLED}"  >>${setvarfile}
echo "export WAVEIN=${WAVEIN}"           >>${setvarfile}
echo "export WEXEC=${WEXEC}"             >>${setvarfile}
echo "export winpdir=${winpdir}"         >>${setvarfile}
echo "export LCFLAG=${LCFLAG}"           >>${setvarfile}
echo "export ocean_clim_flag=${ocean_clim_flag}" >>${setvarfile}
echo "export micro_flag=${micro_flag}"           >>${setvarfile}
echo "export lcdir=${lcdir}"                     >>${setvarfile}
echo "export LEVEL_FLAG=${LEVEL_FLAG}"           >>${setvarfile}

if [ ${PARAFLAG} = 'YES' ]
then
  echo "export DATA=${DATA}"                                 >>${setvarfile}
  echo "export TMPDIR=${TMPDIR}"                             >>${setvarfile}
  echo "export HOMEhur=${HOMEhur}"                           >>${setvarfile}
  echo "export EXEChur=${EXEChur}"                           >>${setvarfile}
  echo "export TM_EXEChur=${TM_EXEChur}"                     >>${setvarfile}
  echo "export PARMhur=${PARMhur}"                           >>${setvarfile}
  echo "export EXhur=${EXhur}"                               >>${setvarfile}
  echo "export FIXhur=${FIXhur}"                             >>${setvarfile}
  echo "export FIXfhur=${FIXfhur}"                           >>${setvarfile}
  echo "export FIX_TM_hur=${FIX_TM_hur}"                     >>${setvarfile}
  echo "export PARM_TM_hur=${PARM_TM_hur}"                   >>${setvarfile}
  echo "export STOREhur=${STOREhur}"                         >>${setvarfile}
  echo "export ATCFdir=${ATCFdir}"                           >>${setvarfile}
  echo "export COMROOT=${COMROOT}"                           >>${setvarfile}
  echo "export COMIN=${COMIN}"                               >>${setvarfile}
  echo "export COMOUT=${COMOUT}"                             >>${setvarfile}
  echo "export RST_PHASE3_DIR=${RST_PHASE3_DIR}"             >>${setvarfile}
  echo "export outdir=${outdir}"                             >>${setvarfile}
  echo "export FORAENV=${FORAENV}"                           >>${setvarfile}
  echo "export PARA_DAT_ROOT=${PARA_DAT_ROOT}"               >>${setvarfile}
  echo "export PARA_DAT_DIR=${PARA_DAT_DIR}"                 >>${setvarfile}
  echo "export PARA_FILE_ROOT=${PARA_FILE_ROOT}"             >>${setvarfile}
  echo "export stormlabel=${stormlabel}"                     >>${setvarfile}
  echo "export storm_num=${storm_num}"                       >>${setvarfile}
  echo "export envir=${envir}"                               >>${setvarfile}
  echo "export PARAFLAG=${PARAFLAG}"                         >>${setvarfile}
  echo "export cyc=${cyc}"                                   >>${setvarfile}
  echo "export job=${job}"                                   >>${setvarfile}
  echo "export outid=${outid}"                               >>${setvarfile}
  echo "export pid=${pid}"                                   >>${setvarfile}
  echo "export ALREADY_HAVE_PHASE3=${ALREADY_HAVE_PHASE3}"   >>${setvarfile}
  echo "export RUN_FROM_SPECTRAL=${RUN_FROM_SPECTRAL}"       >>${setvarfile}
  echo "export RUN_BOGUS=${RUN_BOGUS}"                       >>${setvarfile}
  echo "export ALREADY_HAVE_DATALAT=${ALREADY_HAVE_DATALAT}" >>${setvarfile}
  echo "export PARA_SFCANL_FILE=${PARA_SFCANL_FILE}"         >>${setvarfile}
  echo "export PARA_SIGANL_FILE=${PARA_SIGANL_FILE}"         >>${setvarfile}
  echo "export USE_MY_WAKE_VITALS=${USE_MY_WAKE_VITALS}"     >>${setvarfile}
  echo "export WAKE_VITALS_FILE=${WAKE_VITALS_FILE}"         >>${setvarfile}
  echo "export STOP_AFTER_DATALAT=${STOP_AFTER_DATALAT}"     >>${setvarfile}
  echo "export STOP_AFTER_NOBOGUS=${STOP_AFTER_NOBOGUS}"     >>${setvarfile}
  echo "export EXEChuro=${EXEChuro}"                         >>${setvarfile}
  echo "export gridflag=${gridflag}"                         >>${setvarfile}
  echo "export LEV=${LEV}"                                   >>${setvarfile}
  echo "export ASM=${ASM}"                                   >>${setvarfile}
  echo "export BOG=${BOG}"                                   >>${setvarfile}
  echo "export RBB=${RBB}"                                   >>${setvarfile}
  echo "export AUTOSUBMIT=${AUTOSUBMIT}"                     >>${setvarfile}
  echo "export LC_FILE_TYPE=${LC_FILE_TYPE}"                 >>${setvarfile}
  echo "export EXPERIMENT=${EXPERIMENT}"                     >>${setvarfile}
  echo "export ens_pert=${ens_pert}"                         >>${setvarfile}
fi


###################################################################
# Create the guassian grids
# Proceed with the analysis and initialization part of the system:
###################################################################

set +x
echo " "
echo "End of the first GFDL_PRE_MASTER job...."
echo " "
set -x
