label="cat $COMOUT/$stormlabel.$cycle"

set +x
########################################################################
echo "----------------------------------------------------------------"
echo "exgfdl_fcst.sh.sms - Script To Run 78 or 126 Hour GFDL Forecast."
echo "----------------------------------------------------------------"
echo "History: JUN 1997 - First implementation of this new script."
echo "         JAN 1998 - Rewritten by David Michaud to standardize"
echo "         AUG 1998 - Modified by Peter Henrichsen to use script"
echo "                    onebul.sh to make and send storm bulletin."
echo "         MAR 2001 - Modified by Tim Marchok for coupled model."
echo "         MAR 2003 - Modified by Tim Marchok to extract wake "
echo "                    generation part of code (now in a separate job)."
########################################################################
set -x

PS4=' + exgfdl_fcst.sh.sms line $LINENO: '

label="cat $COMOUT/$stormlabel.$cycle"

if test -f "$label" = "No $stormlabel"
then
   ##############################################
   # Condition if no storms are to be run
   ##############################################
   msg="No hurricane to run."
   postmsg "$jlogfile" "$msg"

   exit
fi

if test -f ${COMIN}/${stormlabel}.holdvars.txt
then
   . ${COMIN}/${stormlabel}.holdvars.txt
else
   msg="Hurricane to run but no ${stormlabel}.holdvars.txt file found."
   postmsg "$jlogfile" "$msg"
   export pgm=exgfdl_fcst.sh.sms
   export err=911;err_chk
fi

#------------------------------------------------------
# Make sure initial fields are roughly the correct 
# size from the initialization:

par=16000000
if test $nestno = 2
then
   par=12000000
fi

cp ${COMIN}/${ATCFNAME}.hour0         .
cp ${COMIN}/${ATCFNAME}.datatop       datatopr
cp ${COMIN}/${ATCFNAME}.datalat       datalatr

length=`wc -c ${ATCFNAME}.hour0 | awk '{print $1}'`
if test $length -lt $par
then
   echo " !!! ERROR: SIZE OF ${ATCFNAME}.hour0 IS NOT VALID..."
#TM   exit 1
fi

length=`wc -c datalatr | awk '{print $1}'`
if test $length -lt 2462000
then
   echo " !!! ERROR: SIZE OF DATALATR IS NOT VALID..."
#TM   exit 2
fi

if [ $BULK -eq 0 ]
then
  cp ${COMIN}/${ATCFNAME}.hour0       datahisr
else
  cp ${COMIN}/${ATCFNAME}.hour0.nwat  datahisr
fi

if [ ${regcode} = 'eastpac' ]; then
  cp ${COMIN}/temp_salin_levitus.eastpac.${ATCFNAME}.${PDY}${cyc} .
  cp ${COMIN}/eastpac_ocean_model_info.${ATCFNAME}.${PDY}${cyc}   .
fi

#-------------------------------------------------------
# Check the status flag for the coupling to see if
# we will run coupled or uncoupled and to then also 
# get the name of the forecast executable that will
# be used.  The "coupled_status_file" was created 
# in the PRE_WAKE job.

coupled_status_file=coupled_status.${ATCFNAME}.${PDY}${cyc}
cp ${COMIN}/${coupled_status_file} .

coupled_flag=` cat ${coupled_status_file}`
if [ ${#coupled_flag} -eq 0 ]
then
  # This indicates that the coupled_status file was probably
  # missing for some reason; Just run uncoupled....
  set +x
  echo "ERROR: coupled_status file missing.  We will run uncoupled...."
  set -x
  gfdlfcst=gfdl_uncoupled
  regcode=united
else
  if [ ${coupled_flag} -eq 0 ]
  then
    gfdlfcst=gfdl_uncoupled
    regcode=united
  else
    gfdlfcst=gfdl_coupled_${regcode}
  fi
fi

#-------------------------------------------------------
# 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....

if [ ${PARAFLAG} = 'YES' ]
then
#  cp /emcsrc/wx20tm/gfdl/fix/gfdl_limit_2nest_dat_x.${XCPUS} limit_dat_x
#  cp /emcsrc/wx20tm/gfdl/fix/gfdl_limit_2nest_dat_y.${YCPUS} limit_dat_y
  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

#-----------------------------------------------------------
# Make appropriate modifications to the parameters.inp file
# NOTE: The PARAMETERS.inp file name MUST have that specific
#       capitalization in it, since the fortran program
#       opens the file with that format in the name.

cp $PARMhur/gfdl_${regcode}.parm.coup parameters.inp.shell

ph4file=rst.final.${ATCFNAME}.${PDY}${cyc}

sed -e "s/_date_/${YYMMDD}${cyc}/g"   \
    -e "s/_name1_/${stormenv}/g"      \
    -e "s/_name2_/${stormenv}/g"      \
    -e "s/_rstph4file_/${ph4file}/g"  \
    parameters.inp.shell >PARAMETERS.inp

rm parameters.inp.shell

if [ ${PARAFLAG} = 'YES' ]
then
  echo " "
else
# Release the early post job
  $SMSBIN/setev release_hur${storm_num}_earlypost
fi

#-------------------------------------------------------
# Copy over the necessary ocean and other fixed files.

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
#  cp ${FIX_TM_hur}/gfdl_fort.7.l42 gfdl_fort.7
  echo "Setting up 42 level model...."
fi

cp ${COMIN}/${ph4file} .
cp ${FIXhur}/gfdl_ocean_dat .

cp ${RST_PHASE3_DIR}/lonlat.gfs .
cp ${RST_PHASE3_DIR}/sst.gfs.${yyyymmdd}.dat .
cp ${RST_PHASE3_DIR}/mask.gfs.${yyyymmdd}.dat .

if [ ${ocean_clim_flag} = 1 ]; then
    ocn_clim=gdem
elif [ ${ocean_clim_flag} = 2 ]; then
    ocn_clim=gdem3
else
    ocn_clim=levit
fi

if [ ${regcode} = 'eastpac' ]; then
  ln -s -f ${DATA}/temp_salin_levitus.eastpac.${ATCFNAME}.${PDY}${cyc}   fort.13
  ln -s -f ${DATA}/eastpac_ocean_model_info.${ATCFNAME}.${PDY}${cyc}     fort.66
elif [ ${regcode} = 'eastatl' ]; then
  ln -s -f ${FIXhur}/gfdl_initdata.${regcode}.${MM}                      fort.13
  ln -s -f ${FIXhur}/gfdl_Hdeepgsu.$regcode                              fort.66
else
  ln -s -f ${FIXhur}/gfdl_initdata.${ocn_clim}.${regcode}.${MM}          fort.13
  ln -s -f ${FIXhur}/gfdl_ocean_topo_and_mask.${regcode}                 fort.66
fi

ln -s -f ${DATA}/sst.gfs.${yyyymmdd}.dat          fort.41
ln -s -f ${DATA}/mask.gfs.${yyyymmdd}.dat         fort.42
ln -s -f ${DATA}/lonlat.gfs                       fort.43
ln -s -f ${DATA}/${coupled_status_file}           ocean_status
ln -s -f ${DATA}/gfdl_fort.7                      inrad.datain
ln -s -f ${DATA}/gfdl_ocean_dat                   ocean_dat

###############################################################
# Run the GFDL executable....
#
# NOTE ON UNIT NUMBERS & FILE NAMES:
#  - All of the output datahis files are written to unit 31.  They
#    are named, opened and closed in NEST.f.  There is a new datahis 
#    file for each 6h-forecast time. datahisw_1 is the 6h file, 
#    datahisw_2 is the 12h file, etc....
#  - datakintw is the integrals file
#  - datakhwnd is the raw swath file which contains the data 
#    needed for the swathps and precip executables in the post.
###############################################################


# Define the number of hours to write the ocean wave data
# (any number < 0 indicates to NOT write the data)
owave=127
echo $owave > ocean_data_write_hours

# Timestep varies depending on whether we are using the original
# GFDL grid ("poleward") or the grid that is new for 2003 in which 
# the entire grid is shifted 5 degrees towards the equator....

if [ ${GRID_CONFIG} = 'poleward' ]
then
  if [ ${FCST5DAY} = 'YES' ]
  then
    TSTEPS=9450
  else
    TSTEPS=5850
  fi
  cat > input_dat << ..eor
  450  450  001 2220    1    6    3   75    5    0  184
 0000 ${TSTEPS} ${TSTEPS}    1    0    1    0 ${Y4PREFIX}$YEARA   $NMO   $DAY   $HRIN    0
    1 9999    1    0   75
    1    0   30  180 9999
..eor
else
  if [ ${FCST5DAY} = 'YES' ]
  then
    TSTEPS=7560
  else
    TSTEPS=4680
  fi
  cat > input_dat << ..eor
  360  360  001 2220    1    6    3   60    5    0  147
 0000 ${TSTEPS} ${TSTEPS}    1    0    1    0 ${Y4PREFIX}$YEARA   $NMO   $DAY   $HRIN    0
    1 9999    1    0   60
    1    0   30  180 9999
..eor
fi

if [ ${PARAFLAG} = 'YES' -a ${AUTOSUBMIT} = 'YES' ] 
then
# Check to see if there is an hour12 file in the /com 
# directory from a previous run of this forecast.  If 
# so, then delete it, as this autosubmit feature keys
# on the appearance of that hour12 file.
  if [ -s ${COMOUT}/${ATCFNAME}.hour12 ]; then
    rm ${COMOUT}/${ATCFNAME}.hour12
  fi
  ${EXhur}/monitor_hour12.sh ${DATA} ${PDY}${cyc} ${stormid} ${EXhur} ${COMIN} ${ATCFNAME} &
  monitor_id=$!
fi

RBB=100
echo $RBB        > rbb_flag
echo $CONV $MOM  > cumulus_parm_flags
export MIC=2
echo ${micro_flag} > microphysics_flag

echo "MOMENTUM INDICATORS: MOM= $MOM, RBB= $RBB"
echo "MICROPHYSICS INDICATOR: micro_flag= $micro_flag"

set +x
echo "pwd before gfdlfcst is `pwd`"
echo "TIMING: date before gfdlfcst is `date`"
set -x

ln -s -f ocean_data_write_hours                fort.4
ln -s -f inrad.datain                          fort.7
ln -s -f datahisr                              fort.20
ln -s -f datalat                               fort.22
ln -s -f datatop                               fort.25
ln -s -f microphysics_flag                     fort.28
ln -s -f datakintw                             fort.32
ln -s -f datakhwnd                             fort.33
ln -s -f datakhwnc                             fort.36
ln -s -f stats_6                               fort.34
ln -s -f stats_1                               fort.35
ln -s -f ihours                                fort.78
ln -s -f proc_count                            fort.88
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 rbb_flag                              fort.97
ln -s -f cumulus_parm_flags                    fort.98

export MP_SHARED_MEMORY=yes

export pgm=gfdl_fcst
. prep_step
startmsg

$EXEChur/gfdl_coupled_$regcode  >> $pgmout 2>errfile 

export err=$?;err_chk

set +x
echo "pwd after gfdlfcst is `pwd`"
echo "TIMING: date after gfdlfcst is `date`"
set -x

# Make sure the monitor job has been killed off....

if [ ${PARAFLAG} = 'YES' -a ${AUTOSUBMIT} = 'YES' ]
then
  kill -9 ${monitor_id}
fi

# If this is a parallel run, copy the output and error
# files.  If this is a production run, then rename the 
# ocean files (for OMB), tar them up, and put the tar
# file into COMIN.

if [ ${PARAFLAG} = 'YES' ]
then
  cp $pgmout ${COMIN}/${stormenv}.${PDY}${cyc}.fcst.out
  cp errfile ${COMIN}/${stormenv}.${PDY}${cyc}.fcst.err
else
  if [ ${owave} -gt 0 ]
  then
    ict=0
    while [ ${ict} -le ${owave} ]  
    do
      if [ ${ict} -lt 10 ]
      then
        outhour="00${ict}"
      elif [ ${ict} -ge 10 -a ${ict} -lt 100 ]
      then
        outhour="0${ict}"
      else
        outhour=${ict}
      fi
      mv ${DATA}/oceandata_${ict} ${DATA}/${stormenv}.${PDY}${cyc}.gfdl_omb_ocean.f${outhour}
      let ict=ict+1
    done
    cd ${DATA}
    tar -cvf ${stormenv}.${PDY}${cyc}.gfdl_omb_ocean.tar \
           ./${stormenv}.${PDY}${cyc}.gfdl_omb_ocean.f*                     
    mv ${stormenv}.${PDY}${cyc}.gfdl_omb_ocean.tar ${COMIN}/.
  fi
fi


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

if test "$SENDCOM" = 'YES'
then
  mv datakintw                $COMOUT/${ATCFNAME}.integrals
  mv datakhwnd                $COMOUT/${ATCFNAME}.swath
  mv datakhwnc                $COMOUT/${ATCFNAME}.swathc
  mv ihours                   $COMOUT/${ATCFNAME}.ihours
  mv datahisr                 $COMOUT/${ATCFNAME}.hour0
  mv datahisw_1               $COMOUT/${ATCFNAME}.hour6
  mv datahisw_2               $COMOUT/${ATCFNAME}.hour12
  mv datahisw_3               $COMOUT/${ATCFNAME}.hour18
  mv datahisw_4               $COMOUT/${ATCFNAME}.hour24
  mv datahisw_5               $COMOUT/${ATCFNAME}.hour30
  mv datahisw_6               $COMOUT/${ATCFNAME}.hour36
  mv datahisw_7               $COMOUT/${ATCFNAME}.hour42
  mv datahisw_8               $COMOUT/${ATCFNAME}.hour48
  mv datahisw_9               $COMOUT/${ATCFNAME}.hour54
  mv datahisw_10              $COMOUT/${ATCFNAME}.hour60
  mv datahisw_11              $COMOUT/${ATCFNAME}.hour66
  mv datahisw_12              $COMOUT/${ATCFNAME}.hour72
  mv datahisw_13              $COMOUT/${ATCFNAME}.hour78
#  cp GRADS*                   $COMOUT/.
#  cp T.*                      $COMOUT/.
#  cp TXY.*                    $COMOUT/.
#  cp U.*                      $COMOUT/.
#  cp V.*                      $COMOUT/.

  tar -cvf ${COMOUT}/$stormenv.${YMDH}.oceantar ./T.${yy}* ./U.${yy}* \
                                                ./V.${yy}* ./TXY.${yy}*
  if [ ${FCST5DAY} = 'YES' ]
  then
    mv datahisw_14              $COMOUT/${ATCFNAME}.hour84
    mv datahisw_15              $COMOUT/${ATCFNAME}.hour90
    mv datahisw_16              $COMOUT/${ATCFNAME}.hour96
    mv datahisw_17              $COMOUT/${ATCFNAME}.hour102
    mv datahisw_18              $COMOUT/${ATCFNAME}.hour108
    mv datahisw_19              $COMOUT/${ATCFNAME}.hour114
    mv datahisw_20              $COMOUT/${ATCFNAME}.hour120
    mv datahisw_21              $COMOUT/${ATCFNAME}.hour126
  fi
  mv stats_6                  $COMOUT/$stormenv.${YMDH}.stats.short
  mv stats_1                  $COMOUT/$stormenv.${YMDH}.stats.complete
  cp oceandata                $COMOUT/$stormenv.${YMDH}.oceandata
fi
