#!/usr/bin/perl -wpi.bak
# Read a vex file and update the antenna positions

use strict;

use Getopt::Long;

use constant NONE => 0;
use constant SITE => 1;

BEGIN {
  use vars qw($ant $mode $atcaref $atca2ref %atcapos %positions);


  $atcaref = undef;
  $atca2ref = undef;
  GetOptions('atca=s'=>\$atcaref, 'a2=s'=>\$atca2ref);

  %positions = ('ASKAP' => [-2556381.172, 5097500.422, -2847998.384]);

  %atcapos = ('W0'  => [-4752447.299, 2790328.78,  -3200491.7],
	      'W2'  => [-4752431.762, 2790355.156, -3200491.7],
	      'W4'  => [-4752416.225, 2790381.533, -3200491.7],
	      'W6'  => [-4752400.688, 2790407.909, -3200491.7],
	      'W8'  => [-4752385.151, 2790434.285, -3200491.7],
	      'W10' => [-4752369.614, 2790460.662, -3200491.7],
	      'W12' => [-4752354.077, 2790487.038, -3200491.7],
	      'W14' => [-4752338.54,  2790513.415, -3200491.7],
	      'W16' => [-4752323.003, 2790539.791, -3200491.7],
	      'W32' => [-4752198.708, 2790750.802, -3200491.7],
	      'W45' => [-4752097.717, 2790922.248, -3200491.7],
	      'W64' => [-4751950.116, 2791172.823, -3200491.7],
	      'W84' => [-4751794.747, 2791436.587, -3200491.7],
	      'W98' => [-4751685.988, 2791621.222, -3200491.7],
	      'W100'=> [-4751670.451, 2791647.598, -3200491.7],
	      'W102'=> [-4751654.914, 2791673.974, -3200491.7],
	      'W104'=> [-4751639.377, 2791700.351, -3200491.7],
	      'W106'=> [-4751623.84,  2791726.727, -3200491.7],
	      'W109'=> [-4751600.535, 2791766.291, -3200491.7],
	      'W110'=> [-4751592.766, 2791779.48,  -3200491.7],
	      'W111'=> [-4751584.998, 2791792.668, -3200491.7],
	      'W112'=> [-4751577.229, 2791805.856, -3200491.7],
	      'W113'=> [-4751569.461, 2791819.044, -3200491.7],
	      'W124'=> [-4751484.008, 2791964.114, -3200491.7],
	      'W125'=> [-4751476.239, 2791977.302, -3200491.7],
	      'W128'=> [-4751452.934, 2792016.867, -3200491.7],
	      'W129'=> [-4751445.165, 2792030.055, -3200491.7],
	      'W140'=> [-4751359.712, 2792175.125, -3200491.7],
	      'W147'=> [-4751305.332, 2792267.442, -3200491.7],
	      'W148'=> [-4751297.564, 2792280.63,  -3200491.7],
	      'W163'=> [-4751181.037, 2792478.453, -3200491.7],
	      'W168'=> [-4751142.194, 2792544.394, -3200491.7],
	      'W172'=> [-4751111.121, 2792597.147, -3200491.7],
	      'W173'=> [-4751103.352, 2792610.335, -3200491.7],
	      'W182'=> [-4751033.436, 2792729.028, -3200491.7],
	      'W189'=> [-4750979.056, 2792821.346, -3200491.7],
	      'W190'=> [-4750971.288, 2792834.534, -3200491.7],
	      'W195'=> [-4750932.445, 2792900.475, -3200491.7],
	      'W196'=> [-4750924.677, 2792913.663, -3200491.7],
	      'W392'=> [-4749402.038, 2795498.531, -3200491.647],
	      'N2'  => [-4751637.131, 2791734.556, -3200465.258],
	      'N5'  => [-4751657.066, 2791746.299, -3200425.595],
	      'N7'  => [-4751670.357, 2791754.128, -3200399.153],
	      'N11' => [-4751696.938, 2791769.785, -3200346.269],
	      'N14' => [-4751716.874, 2791781.528, -3200306.606]
	     );
  
  die "Useage: fixpos.pl <VEXFILE>\n" if (@ARGV!=1);

  $ant = undef;
  $mode = NONE;
}

if ($mode==NONE) {
  if (/^\s*\$SITE\s*\;\s*$/) {
    $mode = SITE;
  }
} else {
  if (/^\s*\$\S+\s*\;\s*$/) {
    $mode = NONE;
  } elsif (/def\s*(\S+)\s*\;/) {
    $ant = $1;
  } elsif (/enddef\s*(\S+)\s*\;/) {
    $ant = undef;
  } elsif (/site_position\s*=\s*(\S+)\s*m\s*:\s*(\S+)\s*m\s*:
	    \s*(\S+)\s*m\s*;/x) {
    if (defined $ant) {
      if ($ant eq 'ATCA' && defined $atcaref) {
	if (exists $atcapos{uc($atcaref)}) {
	  chomp;
	  $_ = "*$_ * Replaced by fixpos.pl\n".
	    sprintf("     site_position =%s m:%s m:%s m;\n",
		    @{$atcapos{uc($atcaref)}});
	} else {
	  die "ATCA reference \"$atcaref\" not recognised\n";
	}
      } elsif ($ant eq 'WXXX' && defined $atca2ref) {
	if (exists $atcapos{uc($atca2ref)}) {
	  chomp;
	  $_ = "*$_ * Replaced by fixpos.pl\n".
	    sprintf("     site_position =%s m:%s m:%s m;\n",
		    @{$atcapos{uc($atca2ref)}});
	} else {
	  die "ATCA reference \"$atca2ref\" not recognised\n";
	}
      } elsif (exists $positions{$ant}) {
	chomp;
	$_ = "*$_ * Replaced by fixpos.pl\n".
	  sprintf("     site_position =%s m:%s m:%s m;\n",
		  @{$positions{$ant}});
      } else {
	warn "Not updating $ant position\n";
      }
    }
  } 
}


