#!/bin/bash
# Script to generate a plotable table from timestamp data files. Seperate file saved for each 780 pump power.
# Compiled g2.c program is required.
# Output file column format
# Col1 - 776 pump power (in Volts recorded by detector, see README)
# Col2 - Total pairs in measurement time
# Col3&4 - Total singles in measurement time.
# Col5 - Normalized g2 peak

g2_prog="/home/qitlab/programs/g2/g2"

for((i=3;i<=18;i+=3))
do
	rf_780=$(echo "$i" | awk '{print $0/100}')
	for((j=1;j<=19;j+=2))
	do
		rf_776=$(echo "$j" | awk '{print $0/100}')

		power_776=$(awk '{if($1=='"$rf_780"' && $3=='"$rf_776"') print $4}' power.dat)

		# run g2 and put the number of singles in "singles"
		singles=$($g2_prog -t 8 -m 1200 -i "780_""$rf_780""_776_""$rf_776"".dat" -o out.dat 2>&1 | awk '{print $2,$4}')

		pairs=$(awk 'BEGIN{sum=0;} {if($1>=325 && $1<365) sum+=$2;} END{print sum}' out.dat)
		acc=$(awk 'BEGIN{sum=0;} {if($1>=600 && $1<1000) sum+=$2;} END{print sum/400}' out.dat)
		g2_peak=$(awk '{if($1==328) print $2;}' out.dat)
		norm_g2=$(echo "$g2_peak $acc" | awk '{print $1/$2}')

		echo $power_776" "$pairs" "$singles" "$norm_g2" "$acc >> plot_data_"$rf_780".dat
	done
done
