#!/usr/bin/ksh

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

set +x
#####################################################################
echo "   ------------------------------------------------------------------"
echo "   exgfdl_pre_lats.sh.sms - Script to process GFS fcst hrs for GFDL lateral bounds"
echo "   ------------------------------------------------------------------"
echo "   History: APR 2002 - Written by Tim Marchok"
#####################################################################
set -x

###################################################################
# NOTE: This script is very similar to exgfdl_pre_atmos.sh.sms, as 
#       chunks were taken directly from it.  The difference is that
#       the "pre_atmos" job was changed in 4/2002 in order to only
#       process the 00h file, while this "pre-lats" job processes 
#       the remainder of the forecast hours from 06-126h in order
#       to generate the GFDL's lateral boundary conditions.  Doing
#       it this way, we save time, as the analysis work is done 
#       immediately after the GFS analysis is available.
###################################################################

####################################################
# First make sure that we've got the stormlabel
# card in the COMOUT directory, then make sure
# that there is a holdvars.txt file in there as
# well.  Source the  holdvars.txt file, as it
# contains needed environmental variables from
# the JGFDL_PRE_MASTER job that have to be passed
# into this script.
####################################################

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

if test -f "$label" = "No $stormlabel"
then
   ##############################################
   # Condition if no storms are to be run
   ##############################################
   msg="GFDL ERROR in exgfdl_pre_lats.sh.sms: No hurricane to run."
   postmsg "$jlogfile" "$msg"
   export pgm=exgfdl_pre_lats.sh.sms
   export err=911;err_chk
fi

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

######################################################
# Now set up the directory from which we will
# pull the global spectral and surface files.
# Then go ahead and copy the surface file.
######################################################
#SEC for testing
#bsm envir=test
#SEC 
if [ $FORAENV = "GDAS" ]
then
   export FORA="gfs/${envir}/gfs.${Y4PREFIX}${YYMMDD}/gdas.t${HRIN}z"
elif [ $FORAENV = "GFS" ]
then
   export FORA="gfs/${envir}/gfs.${Y4PREFIX}${YYMMDD}/gfs.t${HRIN}z"
elif [ $FORAENV = "PARA" ]
then
  export FORA="${PARA_DAT_DIR}/${PARA_FILE_ROOT}"
#TM  export FORA="${PARA_DAT_DIR}/sfcanl.gdas.${PDY}${cyc}"
fi
#SEC
#bsm envir=prod
#SEC
set +x
echo " "
echo "FORA = $FORA"
echo "HRIN = $HRIN"
echo " "
set -x

if [ $FORAENV = "GDAS" -o $FORAENV = "GFS" ]
then
  if test -f /com/${FORA}.sfcanl
  then
     cp /com/${FORA}.sfcanl   for11
  else
     export pgm=exgfdl_pre_lats.sh.sms
     export err=911;err_chk
  fi
else
#  cp ${FORA}.sfcanl  for11.lats
#  cp ${FORA}.${PDY}${cyc}  for11.lats
  cp ${FORA}  for11.lats
  mv for11.lats for11
fi

###################################################################
#
#                     ---  SPC2GRD ---
#
# Copy in the current spectral files and interpolate them onto
# a gaussian grid.
###################################################################

if [ -s spc2grd.cmdfile ]
then
  rm spc2grd.cmdfile
fi

if [ ${PARAFLAG} = 'YES' ]
then

  if [ ${FCST5DAY} = 'YES' ]
  then

    if [ ${ALREADY_HAVE_DATALAT} = 'YES' ]
    then
      set +x
      echo " "
      echo " +++ Skipping spc2grd since we already have datalat...."
      echo " "
      set -x
    else
      for HOUR in 06 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126
      do
        echo "$EXhur/gfdl_pre_atmos_spc2grd.sh $HOUR" >> spc2grd.cmdfile
      done
    fi

  else

    if [ ${ALREADY_HAVE_DATALAT} = 'YES' ]
    then
      set +x
      echo " "
      echo " +++ Skipping spc2grd since we already have datalat...."
      echo " "
      set -x
    else
      for HOUR in 06 12 18 24 30 36 42 48 54 60 66 72 78
      do
        echo "$EXhur/gfdl_pre_atmos_spc2grd.sh $HOUR" >> spc2grd.cmdfile
      done
      for dum in 14 15 16 17 18 19 20 21
      do
        echo "echo Null action (echo) for processor ${dum} since only 3 day forecast" >>spc2grd.cmdfile
      done
    fi

  fi

else

  if [ ${FCST5DAY} = 'YES' ]
  then
    for HOUR in 06 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126
    do
      echo "$EXhur/gfdl_pre_atmos_spc2grd.sh $HOUR" >> spc2grd.cmdfile
    done
  else
    for HOUR in 06 12 18 24 30 36 42 48 54 60 66 72 78
    do
      echo "$EXhur/gfdl_pre_atmos_spc2grd.sh $HOUR" >> spc2grd.cmdfile
    done
    for dum in 14 15 16 17 18 19 20 21
    do
      echo "echo Null action (echo) for processor ${dum} since only 3 day forecast" >>spc2grd.cmdfile
    done
  fi

fi

set +x
echo " "
echo "TIMING: In exgfdl_pre_lats, date before spc2grd poe is `date`"
echo " "
set -x

export MP_PGMMODEL=mpmd
export MP_CMDFILE=spc2grd.cmdfile
export MP_LABELIO=YES
export MP_INFOLEVEL=3
export MP_STDOUTMODE=ordered
#export MP_EUIDEVICE=csss
export MP_EUIDEVICE=sn_all

if [ ${PARAFLAG} = 'YES' ]
then
  if [ ${ALREADY_HAVE_DATALAT} != 'YES' ]
  then
    poe
  fi
else
  poe
fi

set +x
echo " "
echo "TIMING: date after  spc2grd poe is `date`"
echo " "
set -x


###################################################################
#
#                     ---  CNVANL ---
#
# Now take the gaussian files and interpolate them onto the 
# GFDL integration grid.  Before we can call cnvanl, however, 
# we first need to get certain information from the mess file
# and from the output of the setgrid executable.  This same 
# information is obtained in the exgfdl_pre_atmos.sh.sms script,
# but because we are also using the cnvanl code here AND we may
# be running at the same time as the "pre_atmos" script (for a 
# parallel run, anyway), we have no choice but to repeat the 
# script code here.
#
# CAUTION: Since this could be running at the same time as the 
# pre_atmos job (for a parallel run), it is a good idea to create 
# a separate and distinct $DATA subdirectory so as not to stomp
# out files from each job when running the same executable in 
# both jobs.
#
###################################################################

#--------------------------------------------------------
# Determine the position of the inner nests based on the
# current storm position and the western and southern 
# boundary of the outermost nest.
#
# Bring in boundary limits from bogus start up file
#--------------------------------------------------------

LATSDIR=${DATA}/lats
if [ ! -d ${LATSDIR} ]
then
  mkdir -p ${LATSDIR}
fi
cd ${LATSDIR}
sh $utilscript/setup.sh

export STORM="${STORMENV}${DAY}$HRIN"
export FILE="mess-$STORMENV-$DATEIN-$HRIN"

FILE_s=`echo "mess-$STORMENV-$DATEIN-$HRIN" | tr '[A-Z]' '[a-z]'`

if [ -s $COMIN/$FILE_s ] 
then
  cp $COMIN/$FILE_s $FILE
  export WBOUND=`head -1 $FILE`
  export CENLON=`head -2 $FILE | tail -1`
  export CENLAT=`head -3 $FILE | tail -1`
else
  echo "!!! ERROR: The mess file ${FILE_s} does not exist.  Exiting...."
  exit 99
fi

#--------------------------
# Create grid.input file
#--------------------------

export ICDATE="${NMO}${DAY}${Y4PREFIX}${YEARA}${HRIN}"
export NAME=${MI}${DAY}${FORA1}.${STORMENV}${HRIN}
export NAME12="`echo ${NAME}topto | cut -c1-12`"
echo  0000 $NAME12 $ICDATE > header.file

stormlat=` cat $FILE | sed -n 3p | awk '{print $1}' `
if [ ${stormlat} -lt 0 ]; then
  storm_hem=southern
else
  storm_hem=northern
fi

if [ ${storm_hem} = 'northern' ]
then
  if [ ${GRID_CONFIG} = 'poleward' ]
  then
#   more poleward, shorter timestep
    south_bound=-10
    outer_grid_tstep=48
  else
#   more equatorward, longer timestep
    south_bound=-15
    outer_grid_tstep=60
  fi
else
  if [ ${GRID_CONFIG} = 'equatorward' ]
  then
#   more equatorward, longer timestep
    south_bound=-60
    outer_grid_tstep=60
  else
#   more poleward, shorter timestep
    south_bound=-65
    outer_grid_tstep=48
  fi
fi

export TITLE="`echo $STORMENV $DATEIN $HRIN BOGUS FCST $FORAENV`"
cat << _EOF_ > grid.input
3
1
$WBOUND ${south_bound}.   <....outer domain SW corner  (45deg. west of storm rounded to 5deg)
0.5
0.5
${outer_grid_tstep}.      <....time step of outer grid
1
2
75. 75.        <......... outer domain size in degrees
1
0
$CENLON $CENLAT   <............center mesh 2
3
1
0
11. 11.     <......... middle domain size in degrees
1
1
0
$CENLON $CENLAT   <.........center mesh 3
2
1
0
5. 5.     <......... inner domain size in degrees
1
1
$TITLE            <........frame title
$TITLE            <........exp. name
_EOF_

export pgm=gfdl_setgrid
. prep_step
startmsg

ln -s -f grid.input                 fort.33
ln -s -f gridinf.${STORM}           fort.77
ln -s -f domain.${STORM}            fort.78

$EXEChur/gfdl_setgrid >> output-gridbatch.${STORM}.lats
export err=$?;err_chk

cp gridinf.${STORM} ${DATA}/.
cp domain.${STORM}  ${DATA}/.
cp ${FILE}          ${DATA}/.
cp ${FILE_s}        ${DATA}/.

cd ${DATA}

cp ${COMOUT}/$ATCFNAME.datatop $DATA/datatop

if [ -s cnvanl.cmdfile ]
then
  rm cnvanl.cmdfile
fi

if [ ${PARAFLAG} = 'YES' ]
then

  if [ ${FCST5DAY} = 'YES' ]
  then

    if [ ${ALREADY_HAVE_DATALAT} = 'YES' ]
    then
      set +x
      echo " "
      echo " +++ Skipping cnvanl since we already have datalat...."
      echo " "
      set -x
    else
      HOURLIST='006 012 018 024 030 036 042 048 054 060 066 072 078 084 090 096 102 108 114 120 126'
    fi

    export FCSTLEN=126
    for HOUR in ${HOURLIST}
    do
      echo "$EXhur/gfdl_pre_atmos_cnvanl.sh $HOUR" >> cnvanl.cmdfile
    done

  else

    if [ ${ALREADY_HAVE_DATALAT} = 'YES' ]
    then
      set +x
      echo " "
      echo " +++ Skipping cnvanl since we already have datalat...."
      echo " "
      set -x
    else
      HOURLIST='006 012 018 024 030 036 042 048 054 060 066 072 078'
    fi

    export FCSTLEN=78
    for HOUR in ${HOURLIST}
    do
      echo "$EXhur/gfdl_pre_atmos_cnvanl.sh $HOUR" >> cnvanl.cmdfile
    done

    for dum in 14 15 16 17 18 19 20 21
    do
      echo "echo Null action (echo) for processor ${dum} since only 3 day forecast" >>cnvanl.cmdfile
    done

  fi

else

  if [ ${FCST5DAY} = 'YES' ]
  then
    export FCSTLEN=126
    for HOUR in 006 012 018 024 030 036 042 048 054 060 066 072 078 084 090 096 102 108 114 120 126
    do
      echo "$EXhur/gfdl_pre_atmos_cnvanl.sh $HOUR" >> cnvanl.cmdfile
    done
  else
    export FCSTLEN=78
    for HOUR in 000 006 012 018 024 030 036 042 048 054 060 066 072 078
    do
      echo "$EXhur/gfdl_pre_atmos_cnvanl.sh $HOUR" >> cnvanl.cmdfile
    done
    for dum in 15 16 17 18 19 20 21 22
    do
      echo "echo Null action (echo) for processor ${dum} since only 3 day forecast" >>cnvanl.cmdfile
    done
  fi

fi

set +x
echo " "
echo "TIMING: date before cnvanl poe is `date`"
echo " "
set -x

export MP_PGMMODEL=mpmd
export MP_CMDFILE=cnvanl.cmdfile
export MP_LABELIO=YES
export MP_INFOLEVEL=3
export MP_STDOUTMODE=ordered

if [ ${PARAFLAG} = 'YES' ]
then
  if [ ${ALREADY_HAVE_DATALAT} != 'YES' ]
  then
    poe
  fi
else
  poe
fi

set +x
echo " "
echo "TIMING: date after  cnvanl poe is `date`"
echo " "
set -x


###############################################################
# Run mkbound to generate the lateral boundary condition file.
# First, we need to copy the fcst000.nobogus file from COMOUT.
###############################################################

set +x
echo " "
echo " *******************************************************"
echo "  Back in exgfdl_pre_lats.sh.sms, just before mkbound...."
echo " *******************************************************"
echo " "
set -x

cprcc=44
if [ ${PARAFLAG} = 'YES' ]
then

  # Loop & sleep for 14 minutes to see if the fcst000 file 
  # becomes available.  If it doesn't, then we have to quit.

  fcst000_copied=NO
  ict=1
  while [ $ict -lt 99 -a ${fcst000_copied} = 'NO' ]
  do

    if [ -s ${COMOUT}/$ATCFNAME.fcst000.nobogus ]
    then
      cp ${COMOUT}/$ATCFNAME.fcst000.nobogus fcst000
      cprcc=$?
      fcst000_copied=YES
      set +x
      echo "+++ $ATCFNAME.fcst000.nobogus has been copied at `date`"
      set -x
    else
      let iwaittime=ict*20
      set +x
      echo "!!! fcst000 NOT ready yet, sleeping 20 seconds at `date`"
      echo "!!! Total wait time for ocean so far is ${iwaittime} seconds...."
      set -x
      sleep 40
      let ict=ict+1
    fi

  done

else
  cp ${COMOUT}/$ATCFNAME.fcst000.nobogus fcst000
  cprcc=$?
fi

if [ ${cprcc} -ne 0 ]
then
  set +x
  echo " "
  echo "!!! ERROR in exgfdl_pre_lats.sh.sms.  An error was encountered"
  echo "!!! while trying to copy $ATCFNAME.fcst000.nobogus from the "
  echo "!!! COMOUT directory into the working directory."
  echo "!!! If cprcc = 44, it means the file was not found (probably not"
  echo "!!! created yet) and the while loop timed out waiting for it.  If "
  echo "!!! cprcc = something else, then it probably means there was a "
  echo "!!! problem with the copying of the file itself (probably file does"
  echo "!!! not exist; check to make sure directory names are correct. "
  echo "!!! cprcc = $cprcc    Exiting...."
  set -x
  msg="GFDL ERROR in exgfdl_pre_lats.sh.sms: fcst000 not copied from COMIN."
  postmsg "$jlogfile" "$msg"
  export pgm=exgfdl_pre_lats.sh.sms
  export err=911;err_chk
fi

export pgm=gfdl_mkbound
. prep_step

ln -s -f fcst000 fort.8
ln -s -f fcst006 fort.9
ln -s -f fcst012 fort.10
ln -s -f fcst018 fort.11
ln -s -f fcst024 fort.12
ln -s -f fcst030 fort.13
ln -s -f fcst036 fort.14
ln -s -f fcst042 fort.15
ln -s -f fcst048 fort.16
ln -s -f fcst054 fort.17
ln -s -f fcst060 fort.18
ln -s -f fcst066 fort.19
ln -s -f fcst072 fort.20
ln -s -f fcst078 fort.21
ln -s -f fcst084 fort.22  #<-- These files from 84h to 126h will only
ln -s -f fcst090 fort.23  #<-- be available if this script is run
ln -s -f fcst096 fort.24  #<-- with the option of a 5-day forecast.  The
ln -s -f fcst102 fort.25  #<-- forecast length is passed into gfdl_mkbound
ln -s -f fcst108 fort.26  #<-- via a namelist.
ln -s -f fcst114 fort.27  #<--
ln -s -f fcst120 fort.28  #<--
ln -s -f fcst126 fort.29  #<--
ln -s -f datalat fort.51

namelist=${DATA}/mkboundlist
echo "&fcstinfo fcstlen=${FCSTLEN},grid_config='${GRID_CONFIG}'/"  >${namelist}

if [ ${PARAFLAG} = 'YES' ]
then
  if [ ${ALREADY_HAVE_DATALAT} = 'YES' ]; then
    cp ${COMIN}/${ATCFNAME}.datalat datalat
  else
    startmsg
    $EXEChur/gfdl_mkbound <${namelist} >> $pgmout 2> errfile
    export err=$?;err_chk
  fi

  if [ ${STOP_AFTER_DATALAT} = 'YES' ]
  then
    cp datalat $COMOUT/$ATCFNAME.datalat
    set +x
    echo " "
    echo "+++ AT USER REQUEST, STOPPING AFTER CREATING DATALAT...."
    echo " "
    set -x
#TM    ${EXhur}/tar_t254_para_gfdl_to_hsm.sh ${PDY}${cyc} ${ATCFNAME} ${COMIN} datalat
    exit 75
  fi
else
  startmsg
  $EXEChur/gfdl_mkbound <${namelist} >> $pgmout 2> errfile
  export err=$?;err_chk
fi

############################
# Copy datalat into COMOUT
############################

if [ ${SENDCOM} = 'YES' ]
then
   cp datalat $COMOUT/$ATCFNAME.datalat
fi


#####################################################################
#
#     !!!  THE REMAINDER OF THIS SCRIPT IS EXECUTED  !!!
#     !!!  BY PARALLEL RUNS ONLY.  IN PRODUCTION,    !!!
#     !!!  THEY HAVE THEIR OWN SOFTWARE THAT WILL    !!!
#     !!!  MAKE SURE THAT THE THREE PRE-PROCESSING   !!!
#     !!!  JOBS HAVE FINISHED BEFORE THE FORECAST    !!!
#     !!!  IS STARTED.                               !!!
#
#
# ---  OCEAN MODEL & ATMOSPHERIC ANALYSIS PRE-PROCESSING SYNC-UP  ---
#
# In order to cut down on wallclock time, this job (JGFDL_PRE_LATS)
# is run simultaneously with the atmospheric analysis job 
# (JGFDL_PRE_ATMOS) and the ocean spinup job (JGFDL_PRE_OCEAN),
# if we have indicated that we want to run this forecast coupled.
# These jobs will likely not finish at the same time, so we must
# check for the existence of a status file from the PRE_OCEAN job 
# and for a status file from the PRE_ATMOS job to let us know that 
# both jobs have finished.  Once both of these jobs have finished, 
# then we can exit and let the forecast job begin.
#
#####################################################################

if [ ${PARAFLAG} = 'YES' ]
then

  set +x
  echo "TIMING: pre_lats done, starting to wait for ocean and pre_atmos at `date`"
  set -x

  ocean_status_file=ocean_status.${ATCFNAME}.${PDY}${cyc}
  atmos_status_file=atmos_status.${ATCFNAME}.${PDY}${cyc}
  
  # Loop & sleep for 20 minutes, or until the ocean pre-processing
  # and atmospheric analysis jobs finish, whichever comes first....
  
  ocean_done=NO
  atmos_done=NO
  waitflag=WAIT
  ict=1 
  while [ $ict -lt 301 -a ${waitflag} = 'WAIT' ]
  do
    
    if [ -s ${COMOUT}/${ocean_status_file} -a ${ocean_done} = 'NO' ]
    then
      ocean_done=YES
      set +x
      echo " "
      echo "+++ Ocean Pre available at `date`"
      set -x
    fi

    if [ -s ${COMOUT}/${atmos_status_file} -a ${atmos_done} = 'NO' ]
    then
      atmos_done=YES
      set +x
      echo " "
      echo "+++ Pre_Atmos available at `date`"
      set -x
    fi

    if [ ${ocean_done} = 'YES' -a ${atmos_done} = 'YES' ]
    then
      waitflag=DONE
      set +x
      echo "+++ BOTH Pre_Ocean AND Pre_Atmos are done at `date`"
      set -x
    else
      let iwaittime=ict*20
      set +x
      echo " "
      echo "!!! Ocean Pre and/or Pre_Atmos NOT done yet, sleeping 20 seconds "
      echo "!!! at `date`."
      echo "!!! Total wait time for ocean so far is ${iwaittime} seconds...."
      echo "!!! ocean_done flag = ${ocean_done}"
      echo "!!! atmos_done flag = ${atmos_done}"
      set -x
      sleep 20
      let ict=ict+1
    fi
  
  done

  if [ ${waitflag} = 'WAIT' ]
  then

    # Ocean pre-processing or atmospheric analysis must have gotten hung 
    # up somehow.  If it was the atmospheric analysis that got hung up,
    # then we should exit.  But if it was the ocean pre-processing that
    # got hung up, then we can just bag the coupling and run uncoupled.

    if [ ${ocean_done} = 'NO' ]
    then
      set +x
      echo " "
      echo "!!! ERROR: Ocean Pre NOT done yet -- CANNOT WAIT ANY LONGER -- "
      echo "!!! We will run uncoupled.  Time: `date`"
      echo " "
      set -x
      echo 0 > ${ocean_status_file}
      cp ${ocean_status_file} ${COMOUT}/.
    fi

    if [ ${atmos_done} = 'NO' ]
    then
      set +x
      echo " "
      echo "!!! ERROR: Pre_Atmos NOT done yet -- WE MUST EXIT."
      echo "!!! Exiting at time: `date`"
      echo " "
      set -x
      echo 0 > ${atmos_status_file}
      cp ${atmos_status_file} ${COMOUT}/.
      exit 99
    fi

  fi

fi

