#!/usr/bin/perl

$[ = 0;			# set array base to 1
$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator


do 'frames.pl';

print ">Usage: pelli <file> [color] [clean] [coltext] # $ARGV[0] $ARGV[1] $ARGV[2]";


$color=$ARGV[1];
if ($color eq '')
	{
	$color="red";
	}
$col=$ARGV[3];
if ($col eq '')
	{
	$col=1;
	}

if ($ARGV[2] eq "clean")
{
system("xpaset -p ds9 regions deleteall");
}

open(ds9,">e.reg");

#----------------------------------------------

$n=0;

$pixelx=$pixel;

if ($type eq 'fk5')
{
print "type $type";
($hh,$mm,$ss)=split(':',$ra);
($dd,$nn,$tt)=split(':',$dec);
$sign=1;
if (substr($dec,0,1) eq '-')
        {
        $sign=-1;
        $dd=abs($dd);
        }

$pixel=3600;
$ycpix=$sign*($dd+$nn/60+$tt/3600);
$xcpix=($hh+$mm/60+$ss/3600)*15;
$pixelx=-3600*cos($ycpix/180*3.1415926);
}

if ($type eq 'fk5deg')
{
print "type $type";
$pixel=3600;
$xcpix=$ra;
$ycpix=$dec;
$pixelx=-3600*cos($ycpix/180*3.1415926);
$type='fk5';
}



open(in,"$ARGV[0]");
while (<in>) {
    chop;	# strip record separator
    ($id,$xc,$yc,$a,$b,$theta,$z,$mag) = split;
    @fld=split;
	$xx=$xc/$pixelx+$xcpix; $yy=$yc/$pixel+$ycpix;

	
	if ($a != 0.000 && $b != 0.000)
	{
	$a /=$pixel;
	$b /=$pixel;
#	$theta *=-1.;
	printf ds9 "$type;ellipse(%f,%f,%f,%f,%f) # color=$color text={%s}\n",
	$xx,$yy,$a,$b,$theta,$fld[$col-1];
	printf ds9 "$type;ellipse(%f,%f,%f,%f,%f) # color=$color \n",
	$xx,$yy,$a,0,$theta;
	printf ds9 "$type;ellipse(%f,%f,%f,%f,%f) # color=$color \n",
	$xx,$yy,0,$b,$theta;
	}
	else
	{
	$aa=$a+$b;
	if ($b == 0) {$theta +=90;}
	$tr=$theta/180*3.1415926;
	#  print $aa,$tr,$aa*cos($tr),$aa*sin($tr);
	$xbeg=($xc+$aa*sin($tr))/$pixelx+$xcpix;
	$xfin=($xc-$aa*sin($tr))/$pixelx+$xcpix;
	$ybeg=($yc-$aa*cos($tr))/$pixel+$ycpix;
	$yfin=($yc+$aa*cos($tr))/$pixel+$ycpix;

	printf ds9 "$type;line(%f,%f,%f,%f) # color=$color  text={%s}\n",
	$xbeg,$ybeg,$xfin,$yfin,$fld[$col-1];
	}
	}

close(ds9);

system("cat e.reg | xpaset ds9 regions");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
