#!/bin/bash

###############################################################################
#
# Author: Alexandre CHATELAIN, AREVA NP
#
#
# Creation:06/08/2010
#
# Version: 05/11/2010
#
# Purpose: Monitoring with xmgrace a Code_Saturne calculation
#          Must be launched in the temporary calculation folder
#
# Usage:
#          Vectors tabvar, tabnum and tableg may be edited by the user following these rules:
#          tabvar: indicate the "unique" pattern to be searched for in the listing file
#                  to retrieve the line containing it
#          tabnum: indicate the position of the wanted value inside the line
#          tableg: legend of the graph inside xmgrace
#
# PART TO BE ADAPTED BY USER
#
listing="listing"
xmgrace='YOUR_XMGRACE_INSTALLATION_DIRECTORY/grace-5.1.21/grace/bin/xmgrace'
declare -a tabvar
declare -a tabnum
declare -a tableg
#
#---------------------------------
# Code_Saturne 1-phase variables:
tabvar[0]='c  VelocityZ  '                  ; tabnum[0]=3 ; tableg[0]='RHS VZ'
tabvar[1]='c  Pressure '                    ; tabnum[1]=3 ; tableg[1]='RHS Press.'
tabvar[2]='v  VelocityZ '                   ; tabnum[2]=4 ; tableg[2]='Max VZ [m/s]'
tabvar[3]='Dimensionless distance   yplus ' ; tabnum[3]=5 ; tableg[3]='Min Y+'
#
###############################################################################

nbItem=${#tabvar[*]}

function reScan()
{
  for ((i=0;i<$nbItem;i++)); do
   grep -h "${tabvar[${i}]}" $listing > temp
   awk  -v n=${tabnum[${i}]} '{print $n}' temp > temp.$i
  done
  rm -f temp
}

function refresh()
{
	    reScan
            for ((i=0;i<$nbItem;i++)); do
	     echo WITH G$i > fifo
	     echo KILL G$i.S0 > fifo
	     echo READ \"temp.$i\" > fifo
	     echo G$i.S0 line linewidth 2.0 > fifo
	     echo G$i.S0 line color 2 > fifo
	     echo UPDATEALL > fifo
	     echo AUTOSCALE XAXES > fifo
	    done
	    sleep 2
	    echo REDRAW > fifo
}


old_IFS=$IFS
rm -f fifo
mkfifo fifo
$xmgrace -npipe fifo &
echo ARRANGE\(1, $nbItem, 0.1, 0.15, 0.2\) > fifo

reScan

for ((i=0;i<$nbItem;i++)); do
 echo -e "* ${tabvar[${i}]}
   Col. number: ${tabnum[${i}]}
   Legend     : ${tableg[${i}]}"

 echo WITH G$i > fifo
 echo "SUBTITLE \"${tableg[${i}]}\"" > fifo
 echo "READ \"temp.$i\"" > fifo
 echo G$i.S0 line linewidth 2.0 > fifo
 echo G$i.S0 line color 2 > fifo
 echo AUTOSCALE > fifo
 echo REDRAW > fifo
 echo AUTOSCALE > fifo
done

echo " ======================================"
echo " *** Automatic (a) or Manual (m) mode:"
echo " ======================================"
read auto

if [ "$auto" = "a" ]
then 
echo " *** Press CTRL+C to Exit Monitoring "
echo " ===================================="
  while :
  do
  refresh
  sleep 3
  done
  
else
  echo "==================================="
  echo " *** Manual mode: u=update | q=quit "
  echo "==================================="
  read choice
  until  [ "$choice" = "q" ]
  do
    case $choice in
        u ) 
	    refresh ;;
        * )
            echo "Impossible choice..." ;;
    esac
  read choice
  done

fi
echo "exit" > fifo
rm -f fifo
