<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# showasteroids.pl
# Jure Skvarc, March 1999
# Using asteroid_server and fitsjd program list asteroids present in the given FITS
# images.  Assumes correct time and WCS coordinates
use IO::Socket;
use Getopt::Std;

$remote  = shift || die 'Host name missing!';
$user = shift || die 'User id missing!';
$mag = shift || die 'Magnitude limit missing!';
$port = 58972;
$iaddr = inet_aton($remote)               || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);

while ($name = shift) {
    $line = `fitsjd -j -w $name` || die "Error in $name";
    @args = split ' ', $line;
    shift @args;
    $newline = join(' ', @args);
    #  Establish connection
    $server = IO::Socket::INET-&gt;new(Proto =&gt; "tcp", 
				    PeerAddr =&gt; $remote, 
				    PeerPort =&gt; $port) 
	or die "Can't connect to asteroid server $remote";
    print $server $user . "\0";
    $hello = get_answer($server);
    printf $server "fits $newline %f%c", $mag, '\0';
    $line = get_answer($server);
    close $server;

    @lines = split '\n', $line;
    $f = (split ' ', $lines[0])[0];
    if ($f eq "WARNING" || $f eq "ERROR") {
	printf "%s:: %s\n", $name, $lines[0];
	next;
    }
    open FP, "&gt;temp.inp";
    printf FP "%s 0 0\n", $name;
    $ast = 0;
    printf "%s\n", $name;
    for $a (@lines) {
	print "   $a\n";
	#  Cut off the file name
	@y = split ' ', $a;
	$desig = shift(@y);
	$args = join ' ', @y;
	if ($desig + 0 == 0) {
	    $s = substr($a, 6, 19);
	    $s =~ s/^\s*(.*?)\s*$/$1/;
	    $desig = join('_', split(' ', $s, 2));
	}
	@x = split(' ', $args);
	printf FP "%.2f %.2f 5 %s\n", $x[-3], $x[-2], $desig;
	$ast = 1;
    }
    close FP;
    if ($ast) {
	`fitsblink -i temp.inp`;
    }
}
unlink 'temp.inp';
exit;


#  Receive an answer until the '\0' is received
sub get_answer

{
    my ($server) = @_;
    local $msk, $timeout = 30, $in = 1;
    local $a, $ans, $ans1;

    $ans = '';
    #  Prepare a bit mask for the select statement
    $msk = '';
    vec($msk, fileno($server), 1) = 1;
    while ($in) {
	#  Wait for data
	$a = select($msk, undef, undef, $timeout);
	if ($a &gt; 0) {
	    sysread $server, $ans1, 1;
	    if ($ans1 eq "\0") {
		$in = 0;
	    }
	    else {
		$ans .= $ans1;
	    }
	}
	else {
	    $in = 0;
	}
    }
    return $ans;
}
</pre></body></html>