#!/bin/sh
###############################################################################
#                                                                             #
# This script is the preprocessor for the global multi_grid wave model. It    #
# sets some shell script variables for export to child scripts and copies     #
# some generally used files to the work directory. After this the actual      #
# preprocessing is performed by the following child scripts :                 #
#                                                                             #
#  multiwaveice.sh     : preprocess ice fields.                               #
#  multiwavesst.sh     : preprocess sst fields.                               #
#  multiwavesig.sh     : find and copy sigma files.                           #
#                                                                             #
# Also used is the utililty script                                            #
#                                                                             #
#  multiwavestart.sh   : get initial time of most recent restart file(s)      #
#                                                                             #
# Remarks :                                                                   #
# - The sst script generates files that are needed in the wind script.        #
# - For non-fatal errors output is witten to the wave.log file.               #
# - NOTE TO NCO: this script is I/O limited. To get the script to run in      #
#                approximately 1:30, 2 nodes and 12 processors are optimal    #
#                (not_shared, mist or dew).                                   #
#                                                                             #
#  Update record :                                                            #
#                                                                             #
# - Origination:                                               01-Mar-2007    #
# - Added NCO note on resources on mist/dew.                   05-Mar-2007    #
# - Renmaing mod_def files in $FIX_wave.                       26-Apr-2007    #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

  seton='+xa'
  setoff='+xa'
  set $seton

  msg="HAS BEGUN on `hostname`"
  postmsg "$jlogfile" "$msg"
  msg="Starting MWW3 PREPROCESSOR SCRIPT for $modID"
  postmsg "$jlogfile" "$msg"

  set $setoff
  echo ' '
  echo '                      ********************************'
  echo '                      *** MWW3 PREPROCESSOR SCRIPT ***'
  echo '                      ********************************'
  echo '                          Global multi-grid model with GFS forcing'
  echo "                          Model identifier : $modID"
  echo ' '
  echo "Starting at : `date`"
  set $seton

# definition of the multi-scale wave model inputs

  export iceID=icean_5m

  export wndID=gfs_30m ; export IDIML=720 ; export JDIML=361 
                         export JCLIP=0

  export MP_PGMMODEL=mpmd
  export MP_CMDFILE=cmdfile

  lsth=180

# 0.b Date and time stuff
#     The ending time of the run always is the $lsth hour forecast. The starting
#     time depends on availablility of restart files, and is obtained with
#     multiwavestart.sh
#
#     Make sure nback is set identically in the forecast script !!!
#     nback is the number of cycles to look back.

  export date=$PDY
  export YMDH=${PDY}${cyc}

  export nback=28

  $USHwave/multiwavestart.sh

  ymdh_beg=`head multiwavestart.out | awk '{ print $1 }'`
  rm -f multiwavestart.out
  time_beg="`echo $ymdh_beg | cut -c1-8` `echo $ymdh_beg | cut -c9-10`0000"

  ymdh_end=`$utilexec/ndate $lsth $YMDH`
  time_end="`echo $ymdh_end | cut -c1-8` `echo $ymdh_end | cut -c9-10`0000"

  set $setoff
  echo ' '
  echo 'Times in wave model format :'
  echo '----------------------------'
  echo "   date / cycle  : $date $cycle"
  echo "   starting time : $time_beg"
  echo "   ending time   : $time_end"
  echo ' '

# --------------------------------------------------------------------------- #
# 1.  Get files that are used by most child scripts

  echo 'Preparing input files :'
  echo '-----------------------'
  set $seton

# 1.a Model definition files

  for grdID in $iceID $wndID
  do

    if [ -f $FIXwave/multiwave_$grdID.moddef ]
    then
      cp $FIXwave/multiwave_$grdID.moddef mod_def.$grdID
    fi

    if [ -f mod_def.$grdID ]
    then
      echo "   mod_def.$grdID copied ($FIXwave/multiwave_$grdID.moddef)."
    else
      msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************** '
      echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
      echo '********************************************** '
      echo "                                grdID = $grdID"
      echo ' '
      echo $msg
      echo "$modID prep $date $cycle : mod_def.$grdID missing." >> $wavelog
      set $seton
      err=1;export err;err_chk
    fi

  done

# 1.b Preprocessor template files

  for grdID in $iceID $wndID
  do

    if [ -f $FIXwave/multiwaveprep.$grdID.tmpl ]
    then
      cp $FIXwave/multiwaveprep.$grdID.tmpl .
    fi

    if [ -f multiwaveprep.$grdID.tmpl ]
    then
      echo "   multiwaveprep.$grdID.tmpl copied ($FIXwave)."
    else
      msg="ABNORMAL EXIT: NO FILE $file"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************** '
      echo '*** FATAL ERROR : NO TEMPLATE FILE *** '
      echo '************************************** '
      echo "             multiwaveprep.$grdID.tmpl"
      echo ' '
      echo $msg
      echo "$modID prep $date $cycle : multiwaveprep.$grdID.tmpl missing." >> $wavelog
      set $seton
      err=2;export err;err_chk
    fi
  done

# 1.c Data assimilation buoy file
#     *** NOT YET PORTED TO NEW SYSTEM ***


# --------------------------------------------------------------------------- #
# 2.  Make command file(s) (ice, SST, coefficient, data files)
# 2.a Command file set-up
#     The command file points to $nfile files named cmdfile.$ifile.
#     The actual work is distributed over these files.

  ifile=1
  nfile=`echo $LOADL_PROCESSOR_LIST | wc -w | awk '{ print $1}'`
  iskip=1

  if [ "$nfile" -gt '1' ]
  then
    cmdtype='poe'
  else
    cmdtype='sh'
    nskip='-'
  fi

  set $setoff
  echo '   Making command file(s)'
  echo "   Set up command file structure (type = $cmdtype)."
  echo "      Number of command files  : $nfile"
  set $seton

  while [ "$ifile" -le "$nfile" ]
  do
    touch cmdfile.$ifile
    chmod 700 cmdfile.$ifile
    echo "cmdfile.$ifile" >> cmdfile
    ifile=`expr $ifile + 1`
  done

  ifile=1

# 2.b Ice preprocessing

  echo "$USHwave/multiwaveice.sh > ice.out 2>&1"            >> cmdfile.$ifile

  ifile=`expr $ifile + 1`
  if [ "$ifile" -gt "$nfile" ]
  then
    ifile=1
  fi

# 2.c SST preprocessing

  echo "$USHwave/multiwavesst.sh > sst.out 2>&1"            >> cmdfile.$ifile

  ifile=`expr $ifile + 1`
  if [ "$ifile" -gt "$nfile" ]
  then
    ifile=1
  fi

# 2.d Data file
#     *** NOT YET PORTED TO NEW SYSTEM ***

# 2.e Sigma files processing

  ymdh=$ymdh_beg

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    echo "$USHwave/multiwavesig.sh $ymdh > sig_$ymdh.out 2>&1" >> cmdfile.$ifile

    ymdh=`$utilexec/ndate 3 $ymdh`

    ifile=`expr $ifile + 1`
    if [ "$ifile" -gt "$nfile" ]
    then
      ifile=1
    fi

  done

# --------------------------------------------------------------------------- #
# 3   Execute command file
# 3.a Execution

  set $setoff
  echo "   Executing command file."
  set $seton

  if [ "$nfile" -gt '1' ]
  then
    poe
    exit=$?
  else
    cmdfile.1
    exit=$?
  fi

# 3.b Error trap on poe or shell

#  if [ "$exit" != '0' ]
#  then
#    msg="ABNORMAL EXIT: ERROR IN $cmdtype"
#    postmsg "$jlogfile" "$msg"
#    set $setoff
#    echo ' '
#    echo '**************************************** '
#    echo '*** FATAL ERROR : ERROR IN poe OR sh *** '
#    echo '**************************************** '
#    echo ' '
#    echo "$modID prep $date $cycle : error in $cmdtype." >> $wavelog
#    echo $msg
#    set $seton
#    err=3;export err;err_chk
#  fi

# 3.c Timing info on steps

  set $setoff
  echo ' '
  echo "   Ending times for sepate processors :"

  for file in `cat cmdfile`
  do
    if [ "`wc $file | awk '{ print $1 }'`" = '0' ]
    then
      echo "      $file : no commands in this file."
    else
      words="`tail -1 $file | wc | awk '{ print $2 }'`"
      wrdnr=`expr $words - 1`
      echo "{ print "'$'"$wrdnr }" > awkfile
      outfile="`tail -1 $file | awk -f awkfile`"
      rm -f awkfile
      echo "      $file : `tail -1 $outfile`"
    fi
  done
  set $seton

# --------------------------------------------------------------------------- #
# 4.  Check for errors

  set $setoff
  echo ' '
  echo '   Checking for errors.'
  set $seton

# 4.a Ice field

  if [ -d ice ]
  then
    set $setoff
    postmsg "$jlogfile" "NON-FATAL ERROR in ice field (will be trapped by fcst)."
    echo '      Error in ice field (not fatal, will be trapped by fcst).'
    echo ' '
    sed "s/^/ice.out : /g" ice.out
    echo ' '
    set $seton
  else
    rm -f ice.out multiwaveprep.$iceID.tmpl mod_def.$iceID
    set $setoff
    echo '      Ice field unpacking successful.'
    set $seton
  fi

# 4.b SST field

  if [ -d sst ]
  then
    msg="ABNORMAL EXIT: ERROR IN SSTs"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** FATAL ERROR : ERROR IN SST UNPACKING *** '
    echo '******************************************** '
    echo ' '
    sed "s/^/sst.out : /g" sst.out
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : error in SST unpacking." >> $wavelog
    set $seton
    err=4;export err;err_chk
  else
    rm -f sst.out
    set $setoff
    echo '      SST field unpacking successful.'
    set $seton
  fi

# 4.c Assimilation data
#     *** NOT YET PORTED TO NEW SYSTEM ***

# 4.d Sigma files
#     We will go on if the number of errors in files is less
#     than err_max

  err_max=1

  echo '      Sources of sigma files :'
  set $seton

  ymdh=$ymdh_beg
  nr_err=0

  while [ "$ymdh" -le "$ymdh_end" ]
  do
    if [ -d sig_${ymdh} ]
    then
      echo "         File for $ymdh : error in wavesig2.sh"
      postmsg "$jlogfile" "    File for $ymdh : error in wavesig2.sh"
      nr_err=`expr $nr_err + 1`
      rm -f gwnd.$ymdh
    else
      sigfile=`grep 'File for' sig_${ymdh}.out`
      if [ -z "$sigfile" ]
      then
        echo "         File for $ymdh : cannot identify source"
        nr_err=`expr $nr_err + 1`
        rm -f gwnd.$ymdh
      else
        if [ ! -f gwnd.$ymdh ]
        then
          echo "         File for $ymdh : file not found"
          nr_err=`expr $nr_err + 1`
        else
          echo "      $sigfile"
          rm -f sig_${ymdh}.out
        fi
      fi
    fi
    ymdh=`$utilexec/ndate 3 $ymdh`
  done

  if [ -f sig_*.out ]
  then
    set $setoff
    echo ' '
    echo '*******************************'
    echo '*** ERROR OUTPUT wavesig2.sh ***'
    echo '*******************************'
    echo '            Possibly in multiple calls'
    echo "$modID prep $date $cycle : error in sigma files." >> $wavelog
    for file in sig_*.out
    do
      echo ' '
      sed "s/^/$file : /g" $file
    done
    rm -f sig_*.out
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR in wavesig2.sh, possibly in multiple calls."
  fi

  if [ "$nr_err" -gt "$err_max" ]
  then
    msg="ABNORMAL EXIT: ERROR(S) IN SIGMA FILES"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '********************************************* '
    echo '*** FATAL ERROR : ERROR(S) IN SIGMA FILES *** '
    echo '********************************************* '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : fatal error in sigma files." >> $wavelog
    set $seton
    err=5;export err;err_chk
  fi

  rm -f cmdfile*

# --------------------------------------------------------------------------- #
# 5.  Process extracted wind fields
# 5.a Get into single file 

  set $setoff
  echo ' '
  echo '   Concatenate extracted wind fields ...'
  set $seton

  files=`ls gwnd.* 2> /dev/null`

  if [ -z "$files" ]
  then
    msg="ABNORMAL EXIT: NO gwnd.* FILES FOUND"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** FATAL ERROR : CANNOT FIND WIND FILES *** '
    echo '******************************************** '
    echo ' '
    echo "$modID prep $date $cycle : no wind files found." >> $wavelog
    set $seton
    err=6;export err;err_chk
  fi

  rm -f gfs.wind

  for file in $files
  do
    cat $file >> gfs.wind
    rm -f $file
  done

  rm -f sst.ww3

# 5.b Run waveprep

  set $setoff
  echo ' '
  echo '   Running wind fields through preprocessor.'
  set $seton

  sed -e "s/HDRFL/T/g" multiwaveprep.$wndID.tmpl > multiwaveprep.inp
  mv mod_def.$wndID            mod_def.ww3

  echo "Executing $EXECwave/multiwaveprep"

  $EXECwave/multiwaveprep
  err=$?

  if [ "$err" != '0' ]
  then
    msg="ABNORMAL EXIT: ERROR IN waveprep"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN waveprep *** '
    echo '*************************************** '
    echo ' '
    echo "$modID prep $date $cycle : error in waveprep." >> $wavelog
    set $seton
    err=7;export err;err_chk
  fi

  if [ ! -f wind.ww3 ]
  then
    msg="ABNORMAL EXIT: FILE wind.ww3 MISSING"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    cat waveprep.out
    echo ' '
    echo '****************************************'
    echo '*** FATAL ERROR : wind.ww3 NOT FOUND ***'
    echo '****************************************'
    echo ' '
    echo "$modID prep $date $cycle : wind.ww3 missing." >> $wavelog
    set $seton
    err=8;export err;err_chk
  fi

  rm -f gfs.wind
  rm -f mod_def.ww3
  rm -f multiwaveprep.inp

# --------------------------------------------------------------------------- #
# 6.  Output to /com

  if [ "$SENDCOM" = 'YES' ]
  then
    set $setoff
    echo ' '
    echo "   Saving wind.ww3 as $COMOUT/$wndID.$cycle.wind"
    echo "   Saving times file as $COMOUT/times.$cycle.$wndID"
    set $seton
    cp wind.ww3 $COMOUT/$wndID.$cycle.wind
    cp times.WNS $COMOUT/times.$cycle.$wndID
  fi 

  rm -f wind.ww3
  rm -f $iceID.*
  rm -f times.WNS

# --------------------------------------------------------------------------- #
# 7.  Ending output

  set $setoff
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                     *** End of MWW3 preprocessor ***'
  echo ' '

  msg="$job completed normally"
  postmsg "$jlogfile" "$msg"

# End of MWW3 preprocessor script ------------------------------------------- #
