#!/usr/local/bin/perl

# A script to apply the following equation to the x and y columns in a
# FITS event list:
#
#			x = ax + by +c
#               	y = dx + ey +f
#
# Since fcalc isn't very elegant, I have to call it several times, storing
# the calculated values in columns newx and newy, then copying these values
# into the columns x and y, and finally deleting columns newx and newy.
#

sub Usage {
	printf "Usage:  coordshift.pl infile outfile a b c d e f\n";
}

$infile  = $ARGV[0] or die Usage();
$outfile = $ARGV[1] or die Usage();
$a       = $ARGV[2] or die Usage();
$b       = $ARGV[3] or die Usage();
$c       = $ARGV[4] or die Usage();
$d       = $ARGV[5] or die Usage();
$e       = $ARGV[6] or die Usage();
$f       = $ARGV[7] or die Usage();

$expression_x = "$a*x+$b*y+$c";
$expression_y = "$d*x+$e*y+$f";

printf "Applying the following equation to the x column:  $expression_x\n\n";
$command = "fcalc clobber=yes $infile+1 $outfile newx $expression_x";
system($command);

printf "Applying the following equation to the y column:  \n\n$expression_y\n\n";
$command = "fcalc clobber=yes $outfile+1 $outfile newy $expression_y";
system($command);

printf "Copying column 'newx' to column 'x'\n\n";
$command = "fcalc clobber=yes $outfile+1 $outfile x '1.0*newx'";
system($command);

printf "Copying column 'newy' to column 'y'\n\n";
$command = "fcalc clobber=yes $outfile+1 $outfile y '1.0*newy'";
system($command);

printf "Deleting column 'newx'\n\n";
$command = "fdelcol $outfile+1 newx N Y";
system($command);

printf "Deleting column 'newy'\n\n";
$command = "fdelcol $outfile+1 newy N Y";
system($command);

