#!/usr/bin/perl
# Perl script performing search for new GRBs in pi database in the
# specified dates range

# Usage: grb_search.pl how_many_nights_befor_to_search [mail]
# where mail shouldbe entered if a mail is to be sent
# example: coinc_search! -50 mail
# searches 50 nights before today and sends mail

$ra = 9.02867043;
$dec = -0.5206112;
$radius = 1;
$min_points=80;


if(index($ARGV[0],"-help")>-1 || index($ARGV[0],"-h")>-1){
	print "usage: list_stars.pl -ra=$ra -dec=$dec -radius=$radius -min_points=$min_points"; 
	exit;
}


for($i = 0; $i < @ARGV; ++$i){
        if( index($ARGV[$i],"-radius")>-1){
                $radius = substr($ARGV[$i],8);
                print "radius = $radius\n";
        }

        if( index($ARGV[$i],"-ra")>-1){
				if( index($ARGV[$i],"-radius")<=-1){
                $ra = substr($ARGV[$i],4);
                print "ra = $ra\n";	
				}
        }

        if( index($ARGV[$i],"-dec")>-1){
                $dec = substr($ARGV[$i],5);
                print "dec = $dec\n";
        }

        if( index($ARGV[$i],"-min_points")>-1){
                $min_points = substr($ARGV[$i],12);
                print "min_points = $min_points\n";
        }

        print($ARGV[$i]."\n");
}

$radius_h = $radius / 15.00;

print "PARAMETERS : \n";
print "$ra $dec $radius $radius_h $min_points\n";

use DBI;

#Connect to db

$dbh = DBI->connect ( "dbi:Pg:dbname=20050203_2;host=heplx43", "pidb_user", "");
if ( !defined $dbh ) {
die "Cannot connect to database!\n";
} 


# Preapre query

# Search for events coincidencing with external alerts in DB (by CheckExternalAlerts
# - check this function for coincydence radius, etc.), by position and time.
# Look only for events between $night_start and $night_end, type "flash",
# from camera k2a and only from classical online analysis (evt_runtype=0)

# $stmt = "SELECT * FROM Stars WHERE id=1 AND id>=( $ra - 12 )";
# $sth = $dbh->prepare( $stmt );
# $sth = $dbh->prepare( "SELECT * FROM Stars WHERE id=1" );
$sth = $dbh->prepare( "SELECT * FROM Stars WHERE ra>=($ra-$radius_h) AND ra<=($ra+$radius_h) AND dec>=($dec-$radius) AND dec<=($dec+$radius) AND no_measurements>=$min_points" );
if ( !defined $sth ) {
	die "Cannot prepare statement: $DBI::errstr\n";
}

# Execute query
$sth->execute;

$ext_alert_flag=0;

if($sth->rows > 0)
{

	# Browse and print results
	while ( ( $id,$ra,$dec,$magnitude,$sigma_mag)  = $sth->fetchrow())
	{
		print "----------------------------------------\n";
		print "STAR: $id $ra $dec $magnitude $sigma_mag\n";
		print "    HJD  MAGNITUDE\n";		
		
		# now list measurements of star :
		$sth2 = $dbh->prepare( "SELECT * FROM Measurements WHERE star=$id" );
		if ( !defined $sth2 ) {
		   die "Cannot prepare statement: $DBI::errstr\n";
		}
		# Execute query
		$sth2->execute;

		$ext_alert_flag=0;

		if($sth2->rows > 0)
		{
			while ( ( $star, $time_hjd, $magnitude )  = $sth2->fetchrow())
			{
				print "     $time_hjd $magnitude\n";
			}
		}		
		print "----------------------------------------\n";
	}
}
else { print "No new results found\n";}

# Close db

$sth->finish;
