#!/bin/sh
(cat ; echo 'xxx') | tr '()' '  ' | awk '\
	(NF < 6 || substr($0,1,1) != " ") && i > 0 {if (i != 13) \
			print "*** spectrum ",c," has ",i-1," peaks."; \
		g = (sxy*n-sx*sy)/(sx2*n-sx*sx);
		o = (sy-g*sx)/n;
#		print "Spectrum ",c," gain = ",g,", offset = ",o;
		print c,g,o;
		for(j=1; j<i; j++) {
			d = peak[j]-(j*g+o);
			if ((d > 8 || d < -8) && peak[j] != 0)
				print "*** Peak ", j, " at ", peak[j]," of spectrum ",c,\
                                " is  ",d," channels away from fit.";
		}
		sx = 0; sy = 0 ; sx2 = 0; sxy = 0;
		i = 0; n = 0} \
	/^Peak/ {c = $6} \
	 /^No/ {i = 1} \
	i > 0 && NF == 6 && $4 > 19000 && $2 > 500 && $2 < 7000 {
	if (i == 1 && $2 > 600) {peak[1] = 0;
#	print "*** Missing first peak in spectrum ",c; i++
        }
	if (i == 2 && $2 > 1200) {peak[2] = 0;
#	print "*** Missing second peak in spectrum ",c; i++
        }
	peak[i] = $2;\
	sx += i; sy += $2; sxy += i*$2; sx2 += i*i; i++; n++}'

