#!/usr/bin/perl 
#
# scale the LBC science and tech chips template to any part of sky
#
# genlbc.pl LBC.template hh mm ss dd mm ss position_angle 
#
# input template: LBC.template
# output file:
# input coordinates: hh mm ss dd mm ss
# convert to degrees & add to x0 y0
# scale RA widths by 1/cos(y0)
# all for PA = 0, so assume angle is fixed.
#
#my @index;
my $x0;
my $y0;
my $xp0;
my $yp0;
my $xnew;
my $ynew;
my $x2new;
my $y2new;
my $x3new;
my $y3new;
my $xpnew;
my $angle;
my $pa = 0;
my $infile;
my $outfile;
my $ra_hh;
my $ra_mm;
my $ra_ss;
my $dec_dd;
my $dec_mm;
my $dec_ss;
my $deltxrc = (1080-1024)*0.2237/3600;
my $deltyrc = (2892-2304)*0.2237/3600;

#chomp(@ARGV) = split(/\s{1,}/);
$nargs = scalar(@ARGV);
print $nargs;
if ($nargs != 9) {
 print "Command syntax: \n./genlbc.pl LBC.template output_file rah ram ras dd dm ds pos_angle\n";
	}
if ($nargs == 9) {
	$infile = @ARGV[0];
	$outfile = @ARGV[1];
	open (IN, $infile) || die "cannot open file $infile for reading: $!";
	open (OUT, ">$outfile") || die "cannot open file $outfile for writing: $!";

	$counter = 0;
	
	$ra_hh = @ARGV[2];
	$ra_mm = @ARGV[3];
	$ra_ss = @ARGV[4];
	$dec_dd = @ARGV[5];
	$dec_mm = @ARGV[6];
	$dec_ss = @ARGV[7];
	$pa= @ARGV[8];
	
	printf "%.2f %.2f %.2f %.2f %.2f %.2f\n", $ra_hh, $ra_mm, $ra_ss, $dec_dd, $dec_mm,$dec_ss;
	
	$radeg = 15.*($ra_hh + (($ra_mm + ($ra_ss/60.))/60.));

	# Edits made by Russell Ryan at LBT to deal with negative declination
	$tmp = reverse($dec_dd);
	$sign = chop $tmp;
        if ($sign eq "-") {
	    $decdeg = $dec_dd + ((-1.0*$dec_mm + (-1.*$dec_ss/60.))/60.);
	}
        else {
	    $decdeg = $dec_dd + (($dec_mm + ($dec_ss/60.))/60.);
	}
	# End of rryan's edits

        # original code
        #$decdeg = $dec_dd + (($dec_mm + ($dec_ss/60.))/60.);
	$decrad = $decdeg*3.14159/180.;
	
	printf "%.2f %.2f\n", $radeg, $decdeg;
	
   		printf OUT "# Region file format: DS9 version 4.1\n";
   		printf OUT "# Filename: /tmp/dss225682999.fits.gz\n";
   		printf OUT "global color=green dashlist=8 3 width=1 font=\"helvetica 10 normal\" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1\n";
		printf OUT "fk5\n";
		printf OUT "# composite(0,0,0) || composite=1\n";
	while (<IN>) {
		chomp;
		($x0,$y0,$xp0,$yp0,$angle) = split(/\s{1,}/); #works but does not store into an array
		printf "%10.6f  %10.6f %10.6f %10.6f %10.6f\n", $x0, $y0, $xp0, $yp0, $angle;
		$newpa = $pa-(360-$angle);
		$newparad = $newpa*3.14159/180.; 
		$x0 = -1*$x0;
		$xnew = $x0 - $deltxrc;
		$ynew = $y0 - $deltyrc;
		$x2new = cos(-1.*$newparad)*$xnew + sin(-1.*$newparad)*$ynew;
		$y2new = -sin(-1.*$newparad)*$xnew + cos(-1.*$newparad)*$ynew;
		#
		$x3new = $radeg + -1.*$x2new/cos($decrad);
		$y3new = $decdeg + ($y2new);
		$xpnew = $xp0;
		if ($counter < 5) {
     	    printf OUT "box(%10.6f,%10.6f,%10.6f\",%10.6f\",%10.6f) ||\n", $x3new, $y3new, $xpnew, $yp0, $newpa;
		}
		elsif ($counter == 5) {
   	      printf OUT "box(%10.6f,%10.6f,%10.6f\",%10.6f\",%10.6f)\n", $x3new, $y3new, $xpnew, $yp0, $newpa;
		}
		$counter++;
	
   	}
	close(IN);
	close(OUT);
    }
