Skip to content

Commit

Permalink
Make tools/heavens-above-mag update TLE.pm directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
trwyant committed Jan 11, 2025
1 parent 30af7ae commit 1386dee
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/Astro/Coord/ECI/TLE.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8173,12 +8173,14 @@ sub _next_elevation_screen {
};
}

# $$ BEGIN magnitude_table

# The following is all the Celestrak visual list that have magnitudes in
# Heavens Above. These data are generated by the following:
# Heavens Above. These data are generated by the following:
#
# $ tools/heavens-above-mag --celestrak
# $ tools/heavens-above-mag --celestrak --update
#
# Last-Modified: Thu, 09 Jan 2025 01:45:19 GMT
# Last-Modified: Sat, 11 Jan 2025 04:15:40 GMT

%magnitude_table = (
'00694' => 2.7, # ATLAS CENTAUR 2 R/B
Expand Down Expand Up @@ -8341,6 +8343,8 @@ sub _next_elevation_screen {
'59588' => 2.0, # ACS 3
);

# $$ END

1;

__END__
Expand Down
73 changes: 69 additions & 4 deletions tools/heavens-above-mag
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ use YAML 1.25 qw{ Dump Load };
our $VERSION = '0.132';

use constant CACHE_DIR => 'cache';

use constant MARK => '# $$';
use constant MARK_BEGIN => join ' ', MARK, 'BEGIN';
use constant MARK_BEGIN_MAG => join ' ', MARK_BEGIN, 'magnitude_table';
use constant MARK_END => join ' ', MARK, 'END';
use constant MODIFY_FILE => 'lib/Astro/Coord/ECI/TLE.pm';

use constant REF_CODE => sub {};

use constant VISUAL_URL => 'https://celestrak.org/SpaceTrack/query/visual.txt';
use constant VISUAL_YML => 'visual.yml';

Expand All @@ -26,10 +34,12 @@ my %opt = (

unless ( caller ) { # We're a modulino.
GetOptions( \%opt,
qw{ age=i celestrak! open! purge! verbose! },
qw{ age=i celestrak! open! purge! update! verbose! },
help => sub { pod2usage( { -verbose => 2 } ) },
) or pod2usage( { -verbose => 0 } );

$opt{celestrak} //= $opt{update};

if ( $opt{purge} ) {
require File::Glob;
my $glob = join '/', CACHE_DIR, '*';
Expand Down Expand Up @@ -157,6 +167,13 @@ sub heavens_above_url {
return sprintf 'https://www.heavens-above.com/SatInfo.aspx?satid=%05d', $oid;
}

sub open_file_for_output {
my ( $path ) = @_;
open my $fh, '>:encoding(utf-8)', $path
or die "Failed to open $path: $!\n";
return $fh;
}

sub print_perl {
my ( $oid, $name, $mag ) = @_;
if ( defined $mag ) {
Expand All @@ -171,11 +188,42 @@ sub print_perl {
sub process_celestrak {
my $visual = get_cached( VISUAL_YML, VISUAL_URL );
my ( $last_modified ) = $visual->header( 'Last-Modified' );
my ( @preamble, @postamble );
my @options = qw{ --celestrak };
if ( $opt{update} ) {
open my $fh, '<:encoding(utf-8)', MODIFY_FILE
or die "Failed to open @{[ MODIFY_FILE ]}: $!\n";
local $_ = undef;
while ( <$fh> ) {
push @preamble, $_;
$_ eq MARK_BEGIN_MAG . "\n"
and last;
}
defined $_
or die q/Failed to find '/, MARK_BEGIN_MAG, q/' in /,
MODIFY_FILE, "\n";
push @preamble, "\n";
while ( <$fh> ) {
$_ eq MARK_END . "\n"
and last;
}
defined $_
or die q/Failed to find '/, MARK_END, q/' in /, MODIFY_FILE, "\n";
push @postamble, "\n", $_;
while ( <$fh> ) {
push @postamble, $_;
}
close $fh;
push @options, qw{ --update };
}
$opt{update}
and local *STDOUT = open_file_for_output( MODIFY_FILE );
print @preamble;
print <<"EOD";
# The following is all the Celestrak visual list that have magnitudes in
# Heavens Above. These data are generated by the following:
# Heavens Above. These data are generated by the following:
#
# \$ tools/heavens-above-mag --celestrak
# \$ tools/heavens-above-mag @options
#
# Last-Modified: @{[ $last_modified // 'unknown' ]}
Expand All @@ -185,6 +233,7 @@ EOD
my %oid = parse_visual( $visual );
process_perl( sort { $a <=> $b } keys %oid );
say ');';
print @postamble;
return;
}

Expand Down Expand Up @@ -299,7 +348,7 @@ L<Astro::Coord::ECI::TLE|Astro::Coord::ECI::TLE>. If this option is
asserted, no non-option arguments may be specified, and
L<--open|/--open> is ignored.
The default is C<--no-celestrak>.
The default is the value of L<--update|/--update>.
=head2 --help
Expand All @@ -318,6 +367,22 @@ This option causes the cache to be purged before any magnitudes are
retrieved. The script will exit afterwards unless either arguments or
L<--celestrak|/--celestrak> were specified.
=head2 --update
This option causes everything between the lines
# $$ BEGIN magnitude_table
and
# $$ END
in F<lib/Astro/Coord/ECI/TLE.pm> to be replaced by the output
of the L<--celestrak|/--celestrak> option. An error is reported if the
file can not be opened, or if the markers are not found.
The default is C<--no-update>.
=head2 --verbose
If this Boolean option is asserted, whatever information the author
Expand Down

0 comments on commit 1386dee

Please sign in to comment.