#! /usr/bin/env perl

use warnings;
use strict;


my $smf = shift(@ARGV);
my $ra  = shift(@ARGV);
my $dec = shift(@ARGV);

# Test case
#$smf = '/data/ipp019.0/nebulous/7e/aa/3519801261.gpc1:ThreePi.nt:2013:06:13:o6456g0187o.619280:o6456g0187o.619280.cm.821906.smf';
#$ra = 270.7396666667;
#$dec = 7.0036111111;

unless(defined($smf)&&
       defined($ra)&&
       defined($dec)) {
    die "smf2ct.pl SMF RA DEC";
}

# ppCoord requires a file, not command line positions
open(RD,">/tmp/rd.$$");
print RD "$ra $dec\n";
close(RD);

# make call to ppCoord to convert RD to XYchip
open(ppC,"ppCoord -astrom $smf -radec /tmp/rd.$$ |");
while(<ppC>) {
    chomp;
    my ($r,$d,undef,$x,$y,$chip) = split /\s+/;
    
    my (undef,undef,$otaU,$otaV) = split //,$chip; # ;/;
    
    my ($CTchip,$CTx,$CTy) = ("NO_CT",-1,-1);

    {
	# Search SMF
	my @source_match = ();
	my $match_R = 1.0;
#	open(FD,"fdump $smf ${chip}.psf |");
	open(FD,"ftable $smf -n ${chip}.psf |");
	while (<FD>) {
	    chomp;
	    if ($_ =~ /IPP/) { next; }
	    my @row = split /\s+/;
	    # fragile
	    my ($smf_x,$smf_y,$smf_ra,$smf_dec,$smf_flag,$smf_inst_mag,$smf_inst_mag_sig) = 
		($row[1],$row[2],$row[16],$row[17],
		 $row[44],$row[7],$row[8]);
	    if (sqrt(($smf_x - $x)**2 + ($smf_y - $y)**2) < $match_R) {
		$match_R = sqrt(($smf_x - $x)**2 + ($smf_y - $y)**2);
		@source_match = 	($smf_x,$smf_y,$smf_ra,$smf_dec,$smf_flag,$smf_inst_mag,$smf_inst_mag_sig);
	    }
	}
	close(FD);
	printf("%.10f %.10f -> %s %f %f\n",
	       $ra,$dec,$chip,$x,$y);
	printf("            %f %f (%f,%f) %x %f %f\n",
	       @source_match);
	if ($source_match[4] & 1073741824) {
	    printf("            This source flagged as crosstalk.\n");
	}
    }
    
    # Apply known rules
    if ($otaU == 2) {
	if (OTA_cell_col($x) == 3) {
	    ($CTchip,$CTx,$CTy) = ("XY3${otaV}",$x,$y);
	}
	if (OTA_cell_col($x) == 5) {
	    ($CTchip,$CTx,$CTy) = ($chip,$x + 590 + 18,$y);
	}
	if (OTA_cell_col($x) == 6) {
	    ($CTchip,$CTx,$CTy) = ($chip,$x - 590 - 18,$y);
	}
    }
    if ($otaU == 3) {
	if (OTA_cell_col($x) == 3) {
	    ($CTchip,$CTx,$CTy) = ("XY2${otaV}",$x,$y);
	}
    }	
    if ($otaU == 4) {
	if (OTA_cell_col($x) == 3) {
	    ($CTchip,$CTx,$CTy) = ("XY5${otaV}",$x,$y);
	}
    }
    if ($otaU == 5) {
	if (OTA_cell_col($x) == 3) {
	    ($CTchip,$CTx,$CTy) = ("XY4${otaV}",$x,$y);
	}
	if (OTA_cell_col($x) == 5) {
	    ($CTchip,$CTx,$CTy) = ($chip,$x + 590 + 18,$y);
	}
	if (OTA_cell_col($x) == 6) {
	    ($CTchip,$CTx,$CTy) = ($chip,$x - 590 - 18,$y);
	}
    }	

    if ($CTchip eq "NO_CT") {
	printf("            %s %f %f (%f,%f) %x %f %f\n",
	       $CTchip,$CTx,$CTy,-1,-1,0,-1,-1);
    }
    else {
	# Search SMF for CT source
	my @source_match = ();
	my $match_R = 1.0;
#	open(FD,"fdump $smf ${CTchip}.psf |");
	open(FD,"ftable $smf -n ${CTchip}.psf |");
	while (<FD>) {
	    chomp;
	    if ($_ =~ /IPP/) { next; }
	    my @row = split /\s+/;
	    # fragile
	    my ($smf_x,$smf_y,$smf_ra,$smf_dec,$smf_flag,$smf_inst_mag,$smf_inst_mag_sig) = 
		($row[1],$row[2],$row[16],$row[17],
		 $row[44],$row[7],$row[8]);
	    if (sqrt(($smf_x - $CTx)**2 + ($smf_y - $CTy)**2) < $match_R) {
		$match_R = sqrt(($smf_x - $x)**2 + ($smf_y - $y)**2);
		@source_match = 	($smf_x,$smf_y,$smf_ra,$smf_dec,$smf_flag,$smf_inst_mag,$smf_inst_mag_sig);
	    }
	}
	close(FD);
	printf("CT_SOURCE -> %s %f %f\n",
	       $CTchip,$CTx,$CTy);
	printf("            %f %f (%f,%f) %x %f %f\n",
	       @source_match);
    }
}
system("rm /tmp/rd.$$");

sub OTA_cell_col {
    # cells are 590x598, with 18x12 gaps between
    my $inX = shift;
    if ($inX < 590) { return (0); } # col 0
    my $i;
    for ($i = 1; $i <= 7; $i++) {  # col 1-7
	if (($inX > ($i * (590 + 18)))&&
	    ($inX < (($i + 1) * (590 + 18)))) {
	    return($i);
	}
    }
}

	    
    
