#!/usr/bin/perl

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

if (@ARGV<1) {
	print "Usage: gianti <file> [color] [clean] [notext]";
	print "Plot the gianti.dat file";
	exit;
}

do 'frames.pl';

print ">Usage: gianti <file> [color] [clean] [notext] # $ARGV[0] $ARGV[1] $ARGV[2] $ARGV[3]";


$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");
}

$notext = 0;
if( $ARGV[3] eq "notext" )
{
	$notext=1;
}

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

$n=0;
$pixelx=$pixely=1;
# type, ra and dec are defined in frame.pl
if ($type eq '') {$type='fk5';}
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);
	}

	$pixely=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";
	$xcpix=$ra;
	$ycpix=$dec;
	$pixely=3600.;
	$pixelx=-3600.*cos($ycpix/180*3.1415926);
	$type='fk5';
}

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

open(in,"$ARGV[0]");
open(ds9,">e.reg");
$i=1;
while (<in>) 
{
	chop;	# strip record separator
	($xc,$yc)=split;
	if(($xc!=0)&&($yc!=0)) 
	{
		$xc=$xc/$pixelx+$xcpix;
		$yc=$yc/$pixely+$ycpix;
		printf ds9 "fk5;ellipse(%f,%f,0.1\",0.1\",0) # color=$color",$xc,$yc;
		printf ds9 " text={%d}",$i if( !$notext );
		printf ds9 "\n";
		$i++;
	}
}
close(ds9);

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