#!/usr/bin/perl
#
# Nov 25, 2002
# I have included a small Perl program, which you can feed with lines of
# "RA Dec id" (RA and Dec in decimal degrees) to search for observations
# within 0.01 degree of the given position in the online database.  It
# then uses Compress::Zlib to decompress the gzip file into a file called
# "<id>.txt" (or if "id" is missing, a file called "<RA><Dec>.txt"). 
# This will save you some time (re)typing a list of coordinates.
# 
# Patrick Wils
# 
# Get Mk IV data

use LWP;
use Compress::Zlib;

$ua = LWP::UserAgent->new;
#$ua->proxy("http","http://...");

$url = "http://sallman.tass-survey.org:8080/markiv/servlet/markiv/action/GetData";

$distance = 0.01; # Degrees

while ($star = <>) 
{
	if (substr($star, 0, 1) eq "#")
	{
		next;
	}
	my ($RA, $DEC, $id) = (split(' ',$star))[0..2];
	next if $DEC > 10;
	next if $DEC < 4;
	$id = sprintf ("%08.4f%s%07.4f", $RA, (($DEC > 0) ? '+' : '-'), abs($DEC)) unless ($id);
	print STDERR "File $id: $star";
	$content = sprintf "ra_from=%.4f&ra_to=%.4f&dec_from=%.4f&dec_to=%.4f&compression=gzip", $RA - $distance, $RA + $distance, $DEC - $distance, $DEC + $distance;
	$request = HTTP::Request->new(GET=>"$url?$content");

	$response = $ua->simple_request($request,"$id.gz");

	# check the outcome
	if ($response->is_success)
	{
		if($gz = gzopen("$id.gz", "rb"))
		{
			$gz->gzread($buffer);
			if (substr($buffer,0,9) ne "<!DOCTYPE")
			{
				open DAT, ">$id.txt";
				print DAT $buffer;
				while ($gz->gzread($buffer))
				{
					print DAT $buffer;
				}
				close DAT;
			}
			$gz->gzclose;
		}
	}
	else
	{
		print "Error: " . $response->status_line . "\n";
	}
}

