#!/usr/bin/perl
#
# Extract Gain Curve data from ELINT output and
# write CLCOR script file, as well as data file
#
# Original version - 07sep00 stm
# Revised          - 11sep00 stm   output data file
# Revised          - 12sep00 stm   optional renormalization
# Revised          - 13sep00 stm   extract old elint format also
# Revised          - 20oct00 stm   normalize at EL=0,90 for extremal curves
# Revised          - 08feb02 stm   version for BButlers coefficient files
#
if ( $#ARGV >= 0 ) {
  $target = $ARGV[0];
  if ( $#ARGV == 1 ) {
      $donorm = $ARGV[1];
      print "Normalizing gain curves to unity at maximum\n"
  } else {
      $donorm = 0;
  }
} else {
  die "usage: makegains.pl FILE <donorm> \n";
}
#
# Now open the input file and read line by line
# Sample input line:
#
open(DATF,"<$target") || die "Error: cannot open $target ";
print "Reading coefficient file $target\n";

$clcf = $target . '.clcor';
$outf = $target . '.dat';
open(CLCF,">$clcf") || die "Error: cannot create $clcf\n";
print "Creating CLCOR file $clcf\n";
open(OUTF,">$outf") || die "Error: cannot create $outf\n";
print "Creating data file $outf\n";

while ( <DATF> ) {
    if ( /^;
    if ( /ANT=\s*(\S+) STOK=\s*(\S) IF=\s*(\S): \s*(\S+) \+ \s*(\S+)\*ZA \+ \s*(\S+)\*ZA\*\*2/ ) {
	$ant = $1;
	$stokes = $2;
	$iff = $3;
	$f0 = $4;
	$f1 = $5;
	$f2 = $6;
	if ( $donorm ) {
	    $zn = -0.5*$f1/$f2;
	    if ( $zn < 0 ) {
		$zn = 0.0;
	    } elsif ( $zn > 90 ) {
		$zn = 90.0;
	    }
	    $fn = $f0 + $f1*$zn + $f2*$zn**2;
	    $f0 /= $fn;
	    $f1 /= $fn;
	    $f2 /= $fn;
	}
	print CLCF " ANTEN $ant 0; STOKES = '$stokes'; BIF=$iff; EIF=$iff; \n";
	printf CLCF " CLCOR %.4e, %.4e, %.4e, 0; \n", $f0, $f1, $f2;
	print CLCF " GO clcor; wait clcor \n";
	printf OUTF " %1.d %1.1s %1.d %.4e %.4e %.4e \n", $ant, $stokes, $iff, $f0, $f1, $f2;
    } elsif ( /ANT=\s*(\S+) STOK=\s*(\S) IF=\s*(\S): \s*(\S+) \+ \s*(\S+)\* \(ZA - \s*(\S+)\)\^2/ ) {
	print "Processing old ELINT format - convering to new\n";
	$ant = $1;
	$stokes = $2;
	$iff = $3;
	$g0 = $4;
	$g1 = $5;
	$zn = $6;
        $f0 = $g0 + $g1*$zn**2;
	$f1 = -2.0*$g1*$zn;
	$f2 = $g1;
	if ( $donorm ) {
	    if ( $zn < 0 ) {
		$zn = 0.0;
		$fn = $f0;
		$f0 /= $fn;
		$f1 /= $fn;
		$f2 /= $fn;
	    } elsif ( $zn > 90 ) {
		$zn = 90.0;
		$fn = $f0 + $f1*$zn + $f2*$zn**2;
		$f0 /= $fn;
		$f1 /= $fn;
		$f2 /= $fn;
	    } else {
		$f0 /= $g0;
		$f1 /= $g0;
		$f2 /= $g0;
	    }
	}
	print CLCF " ANTEN $ant 0; STOKES = '$stokes'; BIF=$iff; EIF=$iff; \n";
	printf CLCF " CLCOR %.4e, %.4e, %.4e, 0; \n", $f0, $f1, $f2;
	print CLCF " GO clcor; wait clcor \n";
	printf OUTF " %1.d %1.1s %1.d %.4e %.4e %.4e \n", $ant, $stokes, $iff, $f0, $f1, $f2;
    } elsif ( /ANT=\s*(\S+) STOK=\s*(\S) IF=\s*(\S): \s*(\S+) \+ \s*(\S+)\* \(EL - \s*(\S+)\)\^2/ ) {
	print "Processing ancient ELINT format - convering to new\n";
	$ant = $1;
	$stokes = $2;
	$iff = $3;
	$g0 = $4;
	$g1 = $5;
	$zn = 90.0 - $6;
        $f0 = $g0 + $g1*$zn**2;
	$f1 = -2.0*$g1*$zn;
	$f2 = $g1;
	if ( $donorm ) {
	    if ( $zn < 0 ) {
		$zn = 0.0;
		$fn = $f0;
		$f0 /= $fn;
		$f1 /= $fn;
		$f2 /= $fn;
	    } elsif ( $zn > 90 ) {
		$zn = 90.0;
		$fn = $f0 + $f1*$zn + $f2*$zn**2;
		$f0 /= $fn;
		$f1 /= $fn;
		$f2 /= $fn;
	    } else {
		$f0 /= $g0;
		$f1 /= $g0;
		$f2 /= $g0;
	    }
	}
	print CLCF " ANTEN $ant 0; STOKES = '$stokes'; BIF=$iff; EIF=$iff; \n";
	printf CLCF " CLCOR %.4e, %.4e, %.4e, 0; \n", $f0, $f1, $f2;
	print CLCF " GO clcor; wait clcor \n";
	printf OUTF " %1.d %1.1s %1.d %.4e %.4e %.4e \n", $ant, $stokes, $iff, $f0, $f1, $f2;
    }
}
# Done with that file
close(DATF);
close(CLCF);
close(OUTF);
print "Created output files $clcf $outf\n";
