#!/bin/bash

cdir=$(pwd)
prog=$(basename $0)
#-------------------------------------------------------------------------------------------
USAGE="\
Usage: ${prog} <word> <file(s)>
     <word> = e,m (absolute value),E,M (with sign), w/c (Wolff cluster)"
#-------------------------------------------------------------------------------------------
set -u
while getopts :hT: opt_char; do
 case $opt_char in
  T)  T=$OPTARG                                                 ;;
  h)  echo "$USAGE"                                     ;exit 1 ;;
 \?)  echo "$OPTARG is not a valid option";echo "$USAGE";exit 1 ;;
 esac
done
shift $((OPTIND-1))
if(( $# < 1 ));then echo "$USAGE";exit;fi
#-------------------------------------------------------------------------------------------
w=$1
if(( $# > 1 ));then
 files=${@:2:$#}
else
 files="/dev/stdin"
fi
#-------------------------------------------------------------------------------------------
case $w in
 [wc]) awk '/# L/{L=$4;N=L*L;Nl=2*N} $1== "#clu"{                 print $2/N }' $files ;;
    e) awk '/# L/{L=$4;N=L*L;Nl=2*N} $1!= "#"   {                 print $1/Nl}' $files ;;
    m) awk '/# L/{L=$4;N=L*L;Nl=2*N} $1!= "#"   {if($2<0){$2=-$2};print $2/N }' $files ;;
    E) awk '                         $1!= "#"   {                 print $1   }' $files ;;
    M) awk '                         $1!= "#"   {                 print $2   }' $files ;;
    *) echo "${prog}: Sorry, word $w not understood" > /dev/stderr; echo "$USAGE";exit ;;
esac
#  ---------------------------------------------------------------------
#  Copyright by Konstantinos N. Anagnostopoulos (2004-2022)
#  Physics Dept., National Technical University,
#  konstant@mail.ntua.gr, www.physics.ntua.gr/~konstant
#  
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, version 3 of the License.
#  
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#  
#  You should have received a copy of the GNU General Public Liense along
#  with this program.  If not, see <http://www.gnu.org/licenses/>.
#  -----------------------------------------------------------------------
