#!/usr/bin/perl
# convert astorb.txt to 2 .edb files.
# Usage: [-f]
# if -f then use curl to get the script from lowell, else read it from stdin.
# is a prefix used in naming the generated .edb files.
# Two files are created:
# .edb contains only those asteroids which might ever be brighter
# than $dimmag (set below);
# _dim.edb contains the remaining asteroids.
#
# astorb is a set of elements for 30k+ asteroids maintained by Lowell
# Observatory. See https://ftp.lowell.edu/pub/elgb/astorb.html. From the
# Acknowledgments section of same:
# The research and computing needed to generate astorb.dat were funded
# principally by NASA grant NAGW-1470, and in part by the Lowell Observatory
# endowment. astorb.dat may be freely used, copied, and transmitted provided
# attribution to Dr. Edward Bowell and the aforementioned funding sources is
# made.
#
# grab RCS version
my $ver = '$Revision: 1.2 $';
$ver =~ s/\$//g;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, $me);
my $XCN=<$brtfn" or die "Can not create $brtfn\n";
my $dimfn = "$fnbase"."_dim.edb"; # name of file for dim asteroids
open DIM, ">$dimfn" or die "Can not create $dimfn\n";
print "Creating $brtfn and $dimfn..\n";
# build some common boilerplate
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;
$year += 1900;
$mon += 1;
$from = "# Data From https://ftp.lowell.edu/pub/elgb/astorb.dat.gz\n";
$what = "# Generated by astorb2edb.pl $ver, (c) 2000 Elwood Downey\n";
$when = "# Processed $year-$mon-$mday $hour:$min:$sec UTC\n";
# add boilerplate to each file
print BRT "# Asteroids ever brighter than $dimmag.\n";
print BRT $from;
print BRT $what;
print BRT $when;
print BRT $XCN;
print DIM "# Asteroids never brighter than $dimmag.\n";
print DIM $from;
print DIM $what;
print DIM $when;
print DIM $XCN;
# process each astorb.dat entry. see astorb.txt from same site for format.
while (<$srcfd>) {
# build the name
my $nmbr = &s(1,6);
$nmbr =~ s/^s+//g;
$nmbr =~ s/\s+$//g;
$nmbr =~ s/\s+//g;
$nmbr = $nmbr+0;
$name = &s(8,25);
$name =~ s/^ *//;
$name =~ s/ *$//;
# gather the orbital params
$i = &s(149,157) + 0;
$O = &s(138,147) + 0;
$o = &s(127,136) + 0;
$a = &s(170,181) + 0;
$e = &s(159,168) + 0;
$M = &s(116,125) + 0;
$H = &s(43,47) + 0;
$G = &s(49,53) + 0;
$TM = &s(111,112) + 0;
$TD = &s(113,114) + 0;
$TY = &s(107,110) + 0;
# skip obviously bogus entries
if ($a == 0) {
next;
}
# decide whether it's ever bright
$per = $a*(1 - $e);
$aph = $a*(1 + $e);
if ($per < 1.1 && $aph > .9) {
$fd = BRT; # might be in the back yard some day :-)
} else {
$maxmag = $H + 5*&log10($per*&absv($per-1));
$fd = $maxmag > $dimmag ? DIM : BRT;
}
# print
print $fd "$nmbr " if ($nmbr != 0);
print $fd "$name";
print $fd ",e,$i,$O,$o,$a,0,$e,$M,$TM/$TD/$TY,2000.0,$H,$G\n";
}
# remove fetched files, if new
if (defined($fetchok)) {
unlink $ORBFILE;
unlink $ORBGZFILE;
}
print "Done\n";
exit 0;
# like substr($_,first,last), but one-based.
sub s {
my $str=();
$str = $_;
# just to reduce the noise from -w.
substr ($str, $_[0]-1, $_[1] - $_[0]+1);
}
# return log base 10
sub log10 {
.43429*log($_[0]);
}
# return absolute value
sub absv {
$_[0] < 0 ? -$_[0] : $_[0];
}
# print usage message then die
sub usage
{
my $base = $0;
$base =~ s#.*/##;
print "Usage: $base [-f] \n";
print "$ver\n";
print "Purpose: convert $ORBFILE to 2 .edb files.\n";
print "Options:\n";
print " -f: first ftp $ORBFILE from $ORBSITE, else read from stdin\n";
print "Creates two files:\n";
print " .edb: all asteroids ever brighter than $dimmag\n";
print " _dim.edb: all asteroids never brighter than $dimmag\n";
print "Note: If you downloaded the file astorb.dat or astorb.dat.gz,\n";
print " run this script with the file name as the only command.\n";
print " line option.\n";
exit 1;
}
# get and explode the data
sub fetch
{
# transfer
print "Getting $ORBFTPDIR/$ORBGZFILE from $ORBSITE...\n";
$cmd_v1 = "curl -s --connect-timeout 10 -u 'anonymous:xephem\@github.com' $ORBSITE/$ORBFTPDIR/$ORBGZFILE > $ORBGZFILE";
$cmd_v2 = "curl -s --disable-epsv --connect-timeout 10 -u 'anonymous:xephem\@github.com' $ORBSITE/$ORBFTPDIR/$ORBGZFILE > $ORBGZFILE";
print "trying $cmd_v1\n";
$curl_return = system "$cmd_v1";
$curl_return = $curl_return >> 8;
if ($curl_return != 0) {
print "trying $cmd_v2\n";
$curl_return = system "$cmd_v2";
$curl_return = $curl_return >> 8;
}
if ($curl_return != 0) {
die "curl failed\n";
}
# explode
print "Decompressing $ORBGZFILE ...\n";
$gunzip = "gunzip";
!system "$gunzip < $ORBGZFILE > $ORBFILE" or die "$gunzip failed\n";
-s $ORBFILE or die "$ORBFILE: failed to create from $gunzip $ORBGZFILE\n";
# flag
$fetchok = 1;
}