#!/usr/bin/env perl
#
# P. Toale 6/14/2005
#
# Script to create an amacalib file with ADC calibration constants
# based on an ama.elec simulation input. It simple grabs the caladc
# field and calculates beta_a=1/caladc. To create a calibration file,
# say for icecube 2010,
#
#  $ make_amacalib.pl prodtree/ama/detector/icecube/2010/geo/ama.elec \
#        > amacalib.icecube.2010
# 
# To use the amacalib file:
#
#  $ amacalib -G -L -T -D amacalib.icecube.2010 -o <calibrated f2k> \
#        <uncalibrated f2k>
#
$file = shift;
#
## open file and read first line (version header)
#
open (IN, "$file") or die "Can't open file $file\n";
<IN>;
#
## Get number of OMs
#
$line = `tail -n 1 $file`;
split /\s+/, $line;
$nOM = $_[1];
#
## Print header info
#
print "X 8.10\n";
print "Y 2099 -90.0 0.0 1730 0 0 0\n";
print "D 1 1 $nOM 0 0\n";
#
## loop over all lines and discard comments
#
while ($line = <IN>) {
    if (!($line =~ /^c/)) {

#       Get channel and caladc values
	split /\s+/, $line;
	$om = $_[1];
	$caladc = $_[4];
	$beta_a = 1.0/$caladc;
	printf "A %d 0 %8f 0 1.04667\n", $om, $beta_a;
    }
}
close IN;
#
exit 0;
