#!/bin/csh -f
#
# simple SETTE report generator.
#
# This version should be run in the SETTE directory. 
# The machine name will be picked up from the sette.sh script but the location of the
# validation directory needs to be set here (currently assumed to reside in the ../CONFIG directory)
#
  set mach = `grep "COMPILER=" ./sette.sh | sed -e 's/COMPILER=//'`
  set NEMO_VALID = ../CONFIG/NEMO_VALIDATION
#
  if ( ! -d $NEMO_VALID ) then
    echo "$NEMO_VALID validation directory not found"
  endif
#
  set basedir = `pwd`
  cd $NEMO_VALID
#
# The script also needs the date or revision tag. Currently this is taken from the latest sub-directory
# found in the WGYRE_LONG directory
#
  set dorv = `ls -1rtd ./WGYRE_LONG/{$mach}/* | tail -1l `
  set dorv = $dorv:t
#
# Now loop through all the test directories. On the second pass the user can optionally examine any mismatched
# outputs
#
# This is crude but readable and easily extended for new test configurations
#  
foreach pass (0 1 )
  if ( -d ./WGYRE_LONG ) then
    set f1o = ./WGYRE_LONG/{$mach}/{$dorv}/LONG/ocean.output
    set f1s = ./WGYRE_LONG/{$mach}/{$dorv}/LONG/solver.stat
    set f2o = ./WGYRE_LONG/{$mach}/{$dorv}/SHORT/ocean.output
    set f2s = ./WGYRE_LONG/{$mach}/{$dorv}/SHORT/solver.stat
  
    set nl = `wc -l $f2s`
    tail -$nl[1] $f1s > f1.tmp$$
    cmp -s f1.tmp$$ $f2s
    if ( $status == 0 ) then
      echo "GYRE     restartability  passed"
    else
      echo "GYRE     restartability  FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view solver.stat differences"
        set y = $<
        sdiff f1.tmp$$ $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
    rm f1.tmp$$
  endif
  
  if ( -d ./WORCA2LIMPIS_LONG ) then
    set f1o = ./WORCA2LIMPIS_LONG/{$mach}/{$dorv}/LONG/ocean.output
    set f1s = ./WORCA2LIMPIS_LONG/{$mach}/{$dorv}/LONG/solver.stat
    set f2o = ./WORCA2LIMPIS_LONG/{$mach}/{$dorv}/SHORT/ocean.output
    set f2s = ./WORCA2LIMPIS_LONG/{$mach}/{$dorv}/SHORT/solver.stat
  
    set nl = `wc -l $f2s`
    tail -$nl[1] $f1s > f1.tmp$$
    cmp -s f1.tmp$$ $f2s
    if ( $status == 0 ) then
      echo "ORCA2LIMPIS restartability  passed"
    else
      echo "ORCA2LIMPIS restartability  FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view solver.stat differences"
        set y = $<
        sdiff f1.tmp$$ $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
    rm f1.tmp$$
  endif

  if ( -d ./WORCA2OFFPIS_LONG ) then
    set f1o = ./WORCA2OFFPIS_LONG/{$mach}/{$dorv}/LONG/ocean.output
    set f1s = ./WORCA2OFFPIS_LONG/{$mach}/{$dorv}/LONG/tracer.stat
    set f2o = ./WORCA2OFFPIS_LONG/{$mach}/{$dorv}/SHORT/ocean.output
    set f2s = ./WORCA2OFFPIS_LONG/{$mach}/{$dorv}/SHORT/tracer.stat

    set nl = `wc -l $f2s`
    tail -$nl[1] $f1s > f1.tmp$$
    cmp -s f1.tmp$$ $f2s
    if ( $status == 0 ) then
      echo "ORCA2OFFPIS restartability  passed"
    else
      echo "ORCA2OFFPIS restartability  FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view tracer.stat differences"
        set y = $<
        sdiff f1.tmp$$ $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
    rm f1.tmp$$
  endif
  
  if ( -d ./WAMM12_LONG ) then
    set f1o = ./WAMM12_LONG/{$mach}/{$dorv}/LONG/ocean.output
    set f1s = ./WAMM12_LONG/{$mach}/{$dorv}/LONG/solver.stat
    set f2o = ./WAMM12_LONG/{$mach}/{$dorv}/SHORT/ocean.output
    set f2s = ./WAMM12_LONG/{$mach}/{$dorv}/SHORT/solver.stat

    set nl = `wc -l $f2s`
    tail -$nl[1] $f1s > f1.tmp$$
    cmp -s f1.tmp$$ $f2s
    if ( $status == 0 ) then
      echo "AMM12       restartability  passed"
    else
      echo "AMM12       restartability  FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view solver.stat differences"
        set y = $<
        sdiff f1.tmp$$ $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
    rm f1.tmp$$
  endif

echo
  
  if ( -d ./WGYRE_4 ) then
    set f1o = ./WGYRE_4/{$mach}/{$dorv}/REPRO_1_4/ocean.output
    set f1s = ./WGYRE_4/{$mach}/{$dorv}/REPRO_1_4/solver.stat
    set f2o = ./WGYRE_4/{$mach}/{$dorv}/REPRO_2_2/ocean.output
    set f2s = ./WGYRE_4/{$mach}/{$dorv}/REPRO_2_2/solver.stat
  
    cmp -s $f1s $f2s
    if ( $status == 0 ) then
      echo "GYRE     reproducibility passed"
    else
      echo "GYRE     reproducibility FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view solver.stat differences"
        set y = $<
        sdiff $f1s $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
  endif
  
  if ( -d ./WORCA2LIMPIS_16 ) then
    set f1o = ./WORCA2LIMPIS_16/{$mach}/{$dorv}/REPRO_2_8/ocean.output
    set f1s = ./WORCA2LIMPIS_16/{$mach}/{$dorv}/REPRO_2_8/solver.stat
    set f2o = ./WORCA2LIMPIS_16/{$mach}/{$dorv}/REPRO_4_4/ocean.output
    set f2s = ./WORCA2LIMPIS_16/{$mach}/{$dorv}/REPRO_4_4/solver.stat
  
    cmp -s $f1s $f2s
    if ( $status == 0 ) then
      echo "ORCA2LIMPIS reproducibility passed"
    else
      echo "ORCA2LIMPIS reproducibility FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view solver.stat differences"
        set y = $<
        sdiff $f1s $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
  endif

  if ( -d ./WORCA2OFFPIS_16 ) then
    set f1o = ./WORCA2OFFPIS_16/{$mach}/{$dorv}/REPRO_2_8/ocean.output
    set f1s = ./WORCA2OFFPIS_16/{$mach}/{$dorv}/REPRO_2_8/tracer.stat
    set f2o = ./WORCA2OFFPIS_16/{$mach}/{$dorv}/REPRO_4_4/ocean.output
    set f2s = ./WORCA2OFFPIS_16/{$mach}/{$dorv}/REPRO_4_4/tracer.stat

    cmp -s $f1s $f2s
    if ( $status == 0 ) then
      echo "ORCA2OFFPIS reproducibility passed"
    else
      echo "ORCA2OFFPIS reproducibility FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view tracer.stat differences"
        set y = $<
        sdiff $f1s $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
  endif
  
  if ( -d ./WAMM12_32 ) then
    set f1o = ./WAMM12_32/{$mach}/{$dorv}/REPRO_4_8/ocean.output
    set f1s = ./WAMM12_32/{$mach}/{$dorv}/REPRO_4_8/solver.stat
    set f2o = ./WAMM12_32/{$mach}/{$dorv}/REPRO_8_4/ocean.output
    set f2s = ./WAMM12_32/{$mach}/{$dorv}/REPRO_8_4/solver.stat

    cmp -s $f1s $f2s
    if ( $status == 0 ) then
      echo "AMM12       reproducibility passed"
    else
      echo "AMM12       reproducibility FAILED"
      if ( $pass == 1 ) then
        echo "<return> to view solver.stat differences"
        set y = $<
        sdiff $f1s $f2s
        echo "<return> to view ocean.output differences"
        set y = $<
        sdiff $f1o $f2o | grep "|"
        echo "<return> to continue"
        set y = $<
      endif
    endif
  endif

  if ( -d ./WORCA2AGUL_1_2 ) then
    set f1o = ./WORCA2AGUL_1_2/{$mach}/{$dorv}/SHORT/ocean.output
    set f1s = ./WORCA2AGUL_1_2/{$mach}/{$dorv}/SHORT/solver.stat

    if ( -f $f1o && -f  $f1s ) then
      echo "ORCA2_LIM_AGRIF runability passed"
    else
      echo "ORCA2_LIM_AGRIF runability FAILED"
    endif
  endif

  echo "<return> for second (more detailed) pass (y/n)"
  set y = $<
  if ( $y != "y" ) exit
end
cd $basedir
exit
