#!/usr/bin/perl 
#Usage: createregions.pl fromobfile.ob 
#
my $infile;
my $radeg;
my $decdeg;
my $ndit;
my $rotangleb;
my $rotangler;
my $deltxrc = (1080-1024)*0.2237/3600;
my $deltyrc = (2892-2304)*0.2237/3600;

$tempfile = "LBC.template";
$infile = @ARGV[0];

open(IN,$infile) || die "cannot open file $infile for reading: $!";

my $rcnt=0;
my $dcnt=0;
my $nditcnt=0;
my $rbangcnt=0;
my $rrangcnt=0;
while (<IN>) {
        chomp($_);
	$rcnt = 2 if /\<TEC_RA\>/;
	$dcnt = 2 if /\<TEC_DEC\>/;
	$nditcnt = 2 if /\<NDIT\>/;
	$rbangcnt = 2 if /\<RotAngle_B\>/;
	$rrangcnt = 2 if /\<RotAngle_R\>/;
	if ($rcnt) 
		{if ($rcnt == 1) 
			{$radeg = $_;}
		$rcnt--;
		 }
	$rcnt = 0 if eof;
	if ($dcnt) 
		{if ($dcnt == 1) 
			{$decdeg = $_;}
		$dcnt--;
		 }
	$dcnt = 0 if eof;
	if ($nditcnt) 
		{if ($nditcnt == 1) 
			{$ndit = $_;}
		$nditcnt--;
		 }
	$nditcnt = 0 if eof;
	if ($rrangcnt) 
		{if ($rrangcnt == 1) 
			{$rotangler = $_;}
		$rrangcnt--;
		 }
	$rrangcnt = 0 if eof;
	if ($rbangcnt) 
		{if ($rbangcnt == 1) 
			{$rotangleb = $_;}
		$rbangcnt--;
		 }
	$rbangcnt = 0 if eof;

	$nditx0 = $ndit+1 if /\<XDIT\>/;
	$ndity0 = $ndit+1 if /\<YDIT\>/;
	if ($nditx0) { if ($nditx0<=$ndit) { $xdit[$ndit-$nditx0] = $_; }
		$nditx0--; }
	$nditx0 = 0 if eof;
	if ($ndity0) { if ($ndity0<=$ndit) { $ydit[$ndit-$ndity0] = $_; }
		$ndity0--; }
	$ndity0 = 0 if eof;
	}

$rahhd = $radeg/15.0;
$rahh = int($rahhd);
$rammd = ($rahhd - $rahh)*60.0;
$ramm = int($rammd);
$rassd = ($rammd - $ramm)*60.0;

$decrad = $decdeg * 3.14159/180.;
$decd = int($decdeg);
$decm = ($decdeg - $decd)*60.0;
$decmd = int($decm);
$decs = ($decm - $decmd)*60.0;
print "$radeg $decdeg $ndit $rotangleb $rotangler\n";
print "$rahh $ramm $rassd \n";
print "$decd $decmd $decs \n";
print "$decrad \n";
print "@xdit\n@ydit\n";

# now create a composite regions file, using the LBC template, for each dither 
# code is based on genlbc.pl
for($i=0;$i<$ndit;$i++) {
	#if ($i==0) $clr = red;
	#if ($i==1) $clr = green;
	#if ($i==2) $clr = blue;
	#if ($i==3) $clr = magenta;
	#if ($i==4) $clr = cyan;
	#if ($i==5) $clr = yellow;
	#create output filename
	$outtemp = "$infile" . "_" . "$i" . "\.reg";
	open(OUT1,">$outtemp") || die "cannot open file $outtemp for writing: $!";
       	printf OUT1 "# Region file format: DS9 version 4.1\n";
	printf OUT1 "# Filename: /tmp/dss225682999.fits.gz\n";
	printf OUT1 "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 OUT1 "fk5\n";
	printf OUT1 "# composite(0,0,0) || composite=1\n";
	open(IN2,$tempfile) || die "cannot open file $tempfile for reading: $!";
	$counter = 0;

	while (<IN2>) {
       		chomp;
       		($x0,$y0,$xp0,$yp0,$angle) = split(/\s{1,}/); 
		$newpa[$i] = $rotangleb-(360-$angle);
       		$newparad = $newpa[$i]*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[$i] = $radeg + -1.*$x2new/cos($decrad) + ($xdit[$i]/3600.0);
       		$y3new[$i] = $decdeg + ($y2new) + ($ydit[$i]/3600.0);
       		$xpnew[$i] = $xp0;
       		$ypnew[$i] = $yp0;
       		if ($counter < 5) {
        		printf OUT1 "box(%10.6f,%10.6f,%10.6f\",%10.6f\",%10.6f) ||\n", $x3new[$i], $y3new[$i], $xpnew[$i], $ypnew[$i], $newpa[$i];
        			}
       		elsif ($counter == 5) {
        		printf OUT1 "box(%10.6f,%10.6f,%10.6f\",%10.6f\",%10.6f)\n", $x3new[$i], $y3new[$i], $xpnew[$i], $ypnew[$i], $newpa[$i];
        			}
        	$counter++;
   		}
close(OUT1);
}

# Compare the old and new regions files to determine the offsets to the dithers.	
#
close(IN2);
close(IN);
close(OUT);
