#!/usr/bin/perl -w

use strict;
use Getopt::Long;

my $avpol = 0;
GetOptions('avpol'=>\$avpol);


if (@ARGV != 5) {
  die "Usage antabScale.pl <ANTAB> <S1> <S2> <S3> <S4>\n\nScales ANTAB file columns by S1,S2,S3,S4\n\n";
}

my $filename = shift @ARGV;

open(ANTAB, $filename) || die "Could not open $filename: $!\n";

my ($s1, $s2, $s3, $s4) = @ARGV;

# Read the pre-amble
while (<ANTAB>) {
  print;
  last   if (/^\s*\/\s*$/);
}

# Read the Tsys columns and scale

my ($dayno, $time, $T1, $T2, $T3, $T4);
while (<ANTAB>) {
  if (/^\s*\/\s*$/) {
    print;
    last;
  }
  ($dayno, $time, $T1, $T2, $T3, $T4) = split;

  $T1 *= $s1;
  $T2 *= $s2;
  $T3 *= $s3;
  $T4 *= $s4;

  if ($avpol) {
    $T1 = ($T1+$T2)/2;
    $T2 = $T1;
    $T3 = ($T3+$T4)/2;
    $T4 = $T3;
  }
  
  printf("$dayno $time  %8.2f %8.2f %8.2f %8.2f\n", $T1,  $T2, $T3, $T4);
}


# Print any trailing bits

while (<ANTAB>) {
  print;
  last if (/^\s*TSYS/);
}
