#!/usr/bin/perl
# convert MPCORB.DAT to 2 .edb files.
# Usage: [-f]
# if -f then use curl to get the script from harvard, 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.
#
# mpcorb.dat is a service of the Minor Planet Center,
# https://www.minorplanetcenter.net/iau/MPCORB
#
# Copyright (c) 2000 Elwood Downey
# 16 Mar 1999: first draft
# 17 Mar 1999: change output filename
# 4 Apr 2000: update arg handling and support new MPC file format.
# 6 Oct 2000: add -f
# 30 Jan 2003: add XCN.
# 24 Sep 2004: only remove files if downloaded fresh
# 1 Nov 2012: change from ftp to curl for better error handling.
# 4 Jul 2014: change download site
# 12 Oct 2021: access to https: (R. J. Mathar)
# grab RCS version
my $ver = '$Revision: 1.3 $';
$ver =~ s/\$//g;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst, $me);
my $XCN=<$brtfn" or die "Can not create $brtfn\n";
$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 is from $MPCSITE/$MPCFTPDIR/$MPCFILE\n";
$what = "# Generated by mpcorb2edb.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 mpcorb.dat entry
while (<$srcfd>) {
chomp();
if (/^-----------/) {
$sawstart = 1;
next;
}
next unless ($sawstart);
# build the name
$name = &s(167, &min(length(),194));
$name =~ s/[\(\)]//g;
$name =~ s/^ *//;
$name =~ s/ *$//;
next if ($name eq "");
# gather the orbital params
$i = &s(60,68) + 0;
$O = &s(49,57) + 0;
$o = &s(38,46) + 0;
$a = &s(93,103) + 0;
$e = &s(71,79) + 0;
$M = &s(27,35) + 0;
$H = &s(9,13) + 0;
$H = 100 if ($H == 0); # beware blank field
$G = &s(15,19) + 0;
$cent = &s(21,21);
$TY = &s(22,23) + 0;
$TY += 1800 if ($cent =~ /I/i);
$TY += 1900 if ($cent =~ /J/i);
$TY += 2000 if ($cent =~ /K/i);
$TM = &mpcdecode (&s(24,24)) + 0;
$TD = &mpcdecode (&s(25,25)) + 0;
# 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 "$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 $MPCFILE;
unlink $MPCZIPFILE;
}
print "Done\n";
exit 0;
# like substr($_,first,last), but one-based.
sub s
{
substr ($_, $_[0]-1, $_[1]-$_[0]+1);
}
# return log base 10
sub log10
{
.43429*log($_[0]);
}
# return absolute value
sub absv
{
$_[0] < 0 ? -$_[0] : $_[0];
}
# return decoded value
sub mpcdecode
{
my $x = $_[0];
$x =~ /\d/ ? $x : sprintf "%d", 10 + ord ($x) - ord ("A");
}
# return min of two values
sub min
{
$_[0] < $_[1] ? $_[0] : $_[1];
}
# print usage message then die
sub usage
{
my $base = $0;
$base =~ s#.*/##;
print "Usage: $base [-f] \n";
print "$ver\n";
print "Purpose: convert $MPCFILE to 2 .edb files.\n";
print "Options:\n";
print " -f: first $MPCFILE from $MPCSITE, 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";
exit 1;
}
# get and unzip the data
sub fetch
{
# transfer
print "Getting $MPCFTPDIR/$MPCZIPFILE from $MPCSITE...\n";
$cmd = "curl -connect-timeout 10 -s -u 'anonymous:xephem\@github.com' $MPCSITE/$MPCFTPDIR/$MPCZIPFILE > $MPCZIPFILE";
print "$cmd\n";
!system "$cmd" or exit(1);
# extract into current dir
print "Decompressing $MPCZIPFILE...\n";
unlink $MPCFILE; # avoid unzip asking OK to overwrite
!system "gunzip $MPCZIPFILE" or die "$MPCZIPFILE: gunzip failed\n";
-s $MPCFILE or die "$MPCFILE: failed to create from unzip $MPCZIPFILE\n";
# flag
$fetchok = 1;
}