#!/bin/sh

data=${1}	 # Name of the data-file
beta=${2}	 # inverse Temperature of the run
symm=${3:-1}	 # Set 1 if Green-Function is symmetric, 0 otherwise
w_max=${4:-20}
h=${5:-0.01}
alpha=${6:-50}
#mePath=/home/bluemer/QMC_BLAS/MaxEnt
#annaPath=$(pwd)

####################
# DATA PREPARATION # 
####################
#Get no. of runs & time slices
eval $(cat $data | awk ' { if(NR==1) { print "NM="$3;} if(NR==2) { print "NT="$2+1;}  }');

#Calculate mean
cat $data | awk -v NM="$NM" -v NT="$NT" ' 
		{ 
		if($1!="#") { g[$1]+=($2+$3)/2; }
		}
		END {
		  printf "# \n # \n"
		  for (x = 0; x < NT; x++)
		    printf "%i %.6f \n", x, g[x]/NM;
		}			
		' >> $data.tmp0
#Calculate Error (temporaly set to a standard value)
paste $data $data.tmp0 | awk -v NM="$NM" -v NT="$NT" '{ 
				if($1!="#") {
					if(NR<(NT+3)) { g[$1]=$5; }
					dg[$1]+=sqrt((($2+$3)/2-g[$1])^2)
				} 
				}
				END {
				    for (x = 0; x < NT; x++)
				    printf "%i %.6f %.6f \n", x, g[x], 0.001;
				    #	printf "%i %.6f %.6f \n", x, g[x], dg[x]/(NM-1);
				}'  >> $data.tmp
rm $data.tmp0

#####################
# START MAXENT TOOL # 
#####################

#${mePath}/./
maxent nt=${NT} w_max=${w_max} h1=${h} h2=${h} beta=${beta} alpha=${alpha} symmetric=${symm} runs=200 output=${data} < ${data}.tmp

rm ${data}.tmp

