#!/bin/bash
#############################################################################
#                                                                           #
# mww3_case_02 : Test case for Alaskan coastal modeling.                    #
#                                                                           #
# Model is compiled inline using provided switch options (serial and MPI)   #
#                                                                           #
#   All other model setting as desired.                                     #
#                                                                           #
# Grids generated in this test :                                            #
#                                                                           #
#  grd1   : Pacific 1degree resolution.                                     #
#  grd2   : Alaskan 0.25x0.50 degree grid.                                  #
#  grd3   : Coastal 1/8x1/16 degree grid.                                   #
#                                                                           #
# Remarks:                                                                  #
#                                                                           #
# - In order to run this case, the following directories need to be         #
#   unpacked in the test directory                                          #
#     mww3_data_00 : General input data for all realistic test cases.       #
#     mww3_data_02 : Grid setup for this test case.                         #
#   After installation of WAVEWATCH III these directories are provided      #
#   as compressed tar files, but are not unpacked to save disk space.       #
# - Restart files are saved in the mww3_data_02 directory for use in other  #
#   runs. CLeaned up by arc_wwatch3 only.                                   #
# - Script set up for parallel running :                                    #
#    a) For cluster with mpirun activate mpirun command lines and set       #
#       environment with valiables MPI and proc in section 0.               #
#    b) For IBM with poe, activale poe command lines and set environment    #
#       variables in poe jobcards at the top of the scripts.                #
#                                                                           #
#                                 Origination: Hendrik Tolman, Aug 2006     #
#                                 Update to v4.18: J-H. ALves, Mar 2014     #
#                                 Update to v5.16: J. Meixner, Sep 2016     #
#                                                   Last Mod : Sep 2016     #
#                                                                           #
#    Copyright 2009-2014 National Weather Service (NWS),                    #
#       National Oceanic and Atmospheric Administration.  All rights        #
#       reserved.  WAVEWATCH III is a trademark of the NWS.                 #
#       No unauthorized use without permission.                             #
#                                                                           #
#    Copyright does not cover fifth grid.                                   #
#                                                                           #
#############################################################################

# Sample PBS queue options for NCEP's Zeus R&D machine
# Runs using command line sentence:
# >> qsub mww3_case_02 

#PBS -l procs=24
#PBS -q batch
#PBS -l walltime=01:00:00
#PBS -A omd
#PBS -N mww3_case_02
#PBS -j oe
#PBS -o mww3_case_02.out

# For batch run in mpi environment, set this to the location of your
# cases WW3 directory
cd /scratch1/portfolios/NCEPDEV/ocean/noscrub/Henrique.Alves/WW3_CASES/cases

# Load modules if system requires
# Options for NCEP Zeus R&D machine
module load intel mpt grads

# 0. Preparations -----------------------------------------------------------

  set -e

# User must specify compiler string (as per in ww3_dir/bin options) here:
  compstr="Intel"

  FIELDS='ICE DPT WND HS FP DP'

  case_dir=`pwd`
  ww3_dir=`echo $case_dir | sed 's/\/cases\/*//g'`
  temp_dir="$case_dir/tmp_case_02"

  mkdir -p ${temp_dir}

# 0.a Define model input

   wind='gfs_1d'           # 1 degree GFS wind data to be used by all grids
#  wind='no'               # run models without wind
    ice='icean_30m'         # 5 min ice analyses to be used by all grids
#   ice='no'               # run models without ice

# 0.b Define model grids

   mods='grd1 grd2 grd3'
#  mods='grd1 grd2'
     NR=`echo $mods | wc -w | awk '{print $1}'`

# 0.c Set initial conditions

# itype=1 ; Hini='10.1'    # Set initial swell
# itype=3                  # Initialize by wind
  itype=5                  # Start from sea at rest

# 0.d Set output

#   dtp='3600' ; PT=' '
    dtp='   0' ; PT='$'

# 0.e Set run times
#     undefined t_rst will give no restart file

  t_beg='20060101 000000' ; t_end='20060102 000000' ; t_rst=
     dt='3600' ;   tn='25'

# t_beg='20060101 000000' ; t_end='20060116 000000' ; t_rst=$t_end
#    dt='3600' ;   tn='361'

# t_beg='20060116 000000' ; t_end='20060201 000000' ; t_rst=
#    dt='3600' ;   tn='385'

# t_beg='20060101 000000' ; t_end='20060201 000000' ; t_rst=
#    dt='10800' ;   tn='249'

  if [ -z "$t_rst" ]
  then
    dte='   0'
    t_rst=$t_end
  else
    dte='   1'
  fi

   fstID=`echo $t_beg | sed 's/ /\./g'`
   rstID=`echo $t_rst | sed 's/ /\./g'`

# 0.f Parallel environment

    MPI='yes'              # run ww3_multi in MPI mode
    proc=24

# 0.g Set-up variables

# WAVEWATCH directories
  export WWATCH3_ENV=${ww3_dir}/wwatch3.env

# Code compilation options
  case_switch_ser="F90 NOGRB NOPA LRB4 SHRD PR3 UQ FLX2 LN1 ST2 STAB2 NL1 BT1 DB1 MLIM TR0 BS0 IC0 IS0 REF0 XX0 WNT1 WNX1 CRT1 CRX1 O0 O1 O2 O3 O4 O5 O6 O7 O11 O14"
  case_switch_mpi="F90 NOGRB NOPA LRB4 MPI DIST PR3 UQ FLX2 LN1 ST2 STAB2 NL1 BT1 DB1 MLIM TR0 BS0 IC0 IS0 REF0 XX0 WNT1 WNX1 CRT1 CRX1 O0 O1 O2 O3 O4 O5 O6 O7 O11 O14"

 # Compiler headings
  cp ${ww3_dir}/bin/comp.${compstr} ${ww3_dir}/bin/comp
  cp ${ww3_dir}/bin/link.${compstr} ${ww3_dir}/bin/link

  path_c="$case_dir"
  path_w="$temp_dir"              # work directory
  path_e="$ww3_dir/exe"          # path for executables
  path_b="$ww3_dir/bin"          # path for binaries
  path_a="$ww3_dir/aux"          # path for aux files and scripts
  path_d="$path_c/mww3_data_02"   # path for data directory
  path_i="$path_c/mww3_data_00"   # path for data directory
  path_o="$path_c/work_case_02" # path for output files


# 0.h Clean-up

  rm -rf $path_w
  rm -rf $path_o
 
  mkdir -p $path_w
  mkdir -p $path_o
  cd $path_w

  echo ' ' ; echo ' '
  echo '                  ======> TEST RUN WAVEWATCH III <====== '
  echo '                    ==================================   '
  echo '                              Alaskan Coastline test case'
  echo ' '

# 1. Grid pre-processor -----------------------------------------------------

# Compile appropriate code 
  ${path_b}/w3_clean all
  ${path_b}/w3_new
  echo $case_switch_ser > ${path_b}/switch
  ${path_b}/w3_make ww3_grid

  echo ' '
  echo '+--------------------+'
  echo '|  Grid preprocessor |'
  echo '+--------------------+'
  echo ' '

  rm -f mod_def.*

  cp $path_d/ww3_grid.inp.* .
  cp $path_d/*.bot .
  cp $path_d/*.obs .
  cp $path_d/*.mask .
  cp $path_d/*.mask2 .
  cp $path_i/ww3_grid.inp.* .
  cp $path_i/*.mask .

  for mod in $mods $wind $ice
  do
    if [ "$mod" != 'no' ]
    then
      mv ww3_grid.inp.$mod ww3_grid.inp

      echo "   Screen ouput routed to $path_o/ww3_grid.$mod.out"
      $path_e/ww3_grid > $path_o/ww3_grid.$mod.out

      rm -f ww3_grid.inp mapsta.ww3 mask.ww3
      mv mod_def.ww3 mod_def.$mod
    fi
  done

  rm -f *.bot *.obs *.mask* ww3_grid.inp.*

# 2. Initial conditions -----------------------------------------------------

  echo ' '
  echo '+--------------------+'
  echo '| Initial conditions |'
  echo '+--------------------+'
  echo ' '

# Compile appropriate code 
  echo $case_switch_ser > ${path_b}/switch
  ${path_b}/w3_make ww3_strt

cat > ww3_strt.inp << EOF
$ WAVEWATCH III Initial conditions input file
$ -------------------------------------------
  $itype
$  0.07 0.0001  225. 25  165.  20.  50.  10.  $Hini
   0.07 0.01  245. 5  180.  20.  50.  10.  $Hini
EOF

  for grid in $mods
  do
    if [ -f $path_d/restart.$grid.$fstID ]
    then
      echo "   initial conditions from $path_d/restart.$grid.$fstID"
      ln -sf $path_d/restart.$grid.$fstID restart.$grid
    else
      rm -f mod_def.ww3
      ln -s mod_def.$grid mod_def.ww3
      echo "   Running ww3_strt for initial conditions"
      echo "   Screen ouput routed to $path_o/ww3_strt.$grid.out"
      $path_e/ww3_strt > $path_o/ww3_strt.$grid.out
      mv restart.ww3 restart.$grid
    fi
  done

  rm -f ww3_strt.inp mod_def.ww3

# 3. Input fields -----------------------------------------------------------

  echo ' '
  echo '+--------------------+'
  echo '| Input data         |'
  echo '+--------------------+'
  echo ' '

# Compile appropriate code 
  echo $case_switch_ser > ${path_b}/switch
  ${path_b}/w3_make ww3_prep

  NRI='0'

  if [ "$wind" != 'no' ]
  then
    NRI=`expr $NRI + 1`
    if [ -f $path_i/wind.$wind ]
    then
      echo "   Wind file $path_i/wind.$wind"
      ln -sf $path_i/wind.$wind .
    else
      echo '   Copy raw wind file ...'
      cp $path_i/$wind.wind.gz wind.raw.gz
      gunzip wind.raw.gz

cat > ww3_prep.inp.$wind << EOF
$ WAVEWATCH III Field preprocessor input file
$ -------------------------------------------
  'WND' 'LL' T
    0. 359.  360  -90. 90. 181
  'NAME' 4 2 '(I8,I7)' '(16F5.2)'
    20 'wind.raw'
$
$ End of input file
EOF

      rm -f mod_def.ww3
      ln -s mod_def.$wind mod_def.ww3
      cp ww3_prep.inp.$wind ww3_prep.inp
      echo "   Screen ouput routed to $path_o/ww3_prep.$wind.out"
      $path_e/ww3_prep > $path_o/ww3_prep.$wind.out
      mv wind.ww3 wind.$wind
      echo "   Copy wind.$wind to $path_i"
      cp wind.$wind $path_i/.

      rm -f wind.raw ww3_prep.inp.$wind ww3_prep.inp mod_def.ww3
      echo ' '
    fi
  fi

  if [ "$ice" != 'no' ]
  then
    NRI=`expr $NRI + 1`
    if [ -f $path_i/ice.$ice ]
    then
      echo "   Ice file $path_i/ice.$ice"
      ln -sf $path_i/ice.$ice .
    else
      echo '   Copy raw ice file ...'
      cp $path_i/$ice.ice.gz ice.raw.gz
      gunzip ice.raw.gz

cat > ww3_prep.inp.$ice << EOF
$ WAVEWATCH III ice preprocessor input file
$ -----------------------------------------
  'ICE' 'LL' T T
    0. 359.5 720  -90. 90. 361
  'NAME'  1  1  '(...)'  '(...)'
    51   'ice.raw'
$
$ end of input fil
EOF

      rm -f mod_def.ww3
      ln -s mod_def.$ice mod_def.ww3
      cp ww3_prep.inp.$ice ww3_prep.inp
      echo "   Screen ouput routed to $path_o/ww3_prep.$ice.out"
      $path_e/ww3_prep > $path_o/ww3_prep.$ice.out
      mv ice.ww3 ice.$ice
      echo "   Copy ice.$ice to $path_i"
      cp ice.$ice $path_i/.

      rm -f ice.raw ww3_prep.inp.$ice ww3_prep.inp mod_def.ww3
      echo ' '

    fi
  fi

  rm -f wind.raw ice.raw ww3_prep.inp* mod_def.ww3

# 4. Main program -----------------------------------------------------------

  echo ' '
  echo '+--------------------+'
  echo '|    Main program    |'
  echo '+--------------------+'
  echo ' '

# Compile appropriate code 
  if [ "$MPI" = "yes" ]
  then
    echo $case_switch_mpi > ${path_b}/switch
    ${path_b}/w3_make ww3_multi
  else
    echo $case_switch_ser > ${path_b}/switch
    ${path_b}/w3_make ww3_multi
  fi

cat > ww3_multi.inp << EOF
$ WAVEWATCH III multi-grid input file
$ ------------------------------------
  $NR $NRI F 1 T T
$
EOF
  if [ "$wind" != 'no' ] ; then
     echo " '$wind'  F F T F F F F"   >> ww3_multi.inp ; fi
  if [ "$ice" != 'no' ] ; then
     echo " '$ice'  F F F T F F F"    >> ww3_multi.inp ; fi

  flags="'no' 'no' '$wind' '$ice' 'no' 'no' 'no'"

  for grid in $mods
  do
    case $grid in
     'grd1') echo " 'grd1' $flags  1 1  0.00 1.00  F" >> ww3_multi.inp ;;
     'grd2') echo " 'grd2' $flags  2 1  0.00 1.00  F" >> ww3_multi.inp ;;
     'grd3') echo " 'grd3' $flags  3 1  0.00 1.00  F" >> ww3_multi.inp ;;
       *   ) echo " *** HELP *** " ; exit 99 ;;
    esac
  done

cat >> ww3_multi.inp << EOF
$
   $t_beg  $t_end
$
   T  T
$
   $t_beg  $dt  $t_end
   N
   $FIELDS
   $t_beg      0  $t_end
   $t_beg      0  $t_end
   $t_rst   $dte  $t_rst
   $t_beg      0  $t_end
   $t_beg   $dtp  $t_end
$PT    0 999 1 0 999 1 T
$
  'the_end'  0
$
  'STP'
$
$ End of input file
EOF

  echo "   Running multi-grid model ..."
# echo "   Screen output routed to $path_o/ww3_multi.out"

  if [ "$MPI" = 'yes' ]
  then
    mpirun -np $proc $path_e/ww3_multi # > $path_o/ww3_multi.out
#   poe hpmcount $path_e/ww3_multi # > $path_o/ww3_multi.out
#   poe $path_e/ww3_multi # > $path_o/ww3_multi.out
  else
    $path_e/ww3_multi # > $path_o/ww3_multi.out
  fi

  for grid in $mods
  do
    if [ -f restart1.$grid ]
    then
      echo "   Output file restart1.$grid routed to"
      echo "               $path_d/restart.$grid.$rstID"
      mv restart1.$grid $path_d/restart.$grid.$rstID
    fi
  done

  echo "   Log files routed to $path_o"
  mv log.* $path_o/.

  rm -f ww3_multi.inp
  rm -f restart.*
  rm -f wind.*

  set +e

  nr_test=`ls test* 2> /dev/null | wc -w | awk '{ print $1}'`
  if [ "$nr_test" != '0' ]
  then
    for file in `ls test* 2> /dev/null`
    do
      size=`wc -w $file | awk '{print $1}'`
      if [ "$size" = 0 ] ; then
        rm -f $file ; fi
    done
  fi

  nr_test=`ls test* 2> /dev/null | wc -w | awk '{ print $1}'`
  if [ "$nr_test" != '0' ]
  then
    echo "   Output file test[nnn].[m]ww3 routed to $path_o"
    mv test* $path_o/.
  fi

  nr_prof=`ls prf*.mww3 2> /dev/null | wc -w | awk '{ print $1}'`
  if [ "$nr_prof" != '0' ]
  then
    echo "   Profiling file prf.*.mww3 routed to $path_o"
    mv prf*.mww3 $path_o/. 2> /dev/null
  fi

  nr_part=`ls partition.* 2> /dev/null | wc -w | awk '{ print $1}'`
  if [ "$nr_part" != '0' ]
  then
    echo "   Partition file(s) partition.* routed to $path_o"
    mv partition.* $path_o/. 2> /dev/null
  fi

  rm -f prf*.mww3
  echo ' '

  set -e

# exit 99

# 4. Gridded output ---------------------------------------------------------

  echo ' '
  echo '+--------------------+'
  echo '|   Gridded output   |'
  echo '+--------------------+'
  echo ' '

# Compile appropriate code 
  echo $case_switch_ser > ${path_b}/switch
  ${path_b}/w3_make gx_outf

cat > gx_outf.inp << EOF
$ WAVEWATCH III Grid output post-processing
$ -----------------------------------------
  $t_beg  $dt  $tn
$
  N 
  $FIELDS
$
  0 999 0 999  T F
$
$ End of input file
EOF

  for mod in $mods
  do
    echo "   GrADS data for $mod ..."
    echo "      Screen ouput routed to $path_o/gx_outf.$mod.out"
    ln -s mod_def.$mod mod_def.ww3
    ln -s out_grd.$mod out_grd.ww3

    $path_e/gx_outf > $path_o/gx_outf.$mod.out

    echo "      ww3.ctl routed to $path_o/$mod.ctl"
    sed "s/ww3\.grads/ww3\.$mod/g" ww3.ctl > $path_o/$mod.ctl
    rm -f ww3.ctl
    echo "      ww3.grads routed to $path_o/ww3.$mod"
    mv ww3.grads $path_o/ww3.$mod

    rm -f mod_def.ww3 out_grd.ww3

  done

# rm -f gx_outf.inp out_grd.*

# 5. Point output -----------------------------------------------------------

  echo ' '
  echo '+--------------------+'
  echo '|    Point output    |'
  echo '+--------------------+'
  echo ' '
  echo '   Not wanted here'


# 6. End, cleaning up -------------------------------------------------------

  echo ' ' ; echo "Cleaning-up `pwd`"
# rm -f mod_def.* log.*

 rm -rf $temp_dir

  echo ' ' ; echo ' '
  echo '                  ======>  END OF WAVEWATCH III  <====== '
  echo '                    ==================================   '
  echo ' '

# End of mww3_case_02 -------------------------------------------------------
