From 1386deee5f7e1735e642b9beea9214944ee0624f Mon Sep 17 00:00:00 2001 From: Tom Wyant Date: Sat, 11 Jan 2025 10:18:37 -0500 Subject: [PATCH] Make tools/heavens-above-mag update TLE.pm directly. --- lib/Astro/Coord/ECI/TLE.pm | 10 ++++-- tools/heavens-above-mag | 73 +++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/lib/Astro/Coord/ECI/TLE.pm b/lib/Astro/Coord/ECI/TLE.pm index c44a1743..fc33fb76 100644 --- a/lib/Astro/Coord/ECI/TLE.pm +++ b/lib/Astro/Coord/ECI/TLE.pm @@ -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 @@ -8341,6 +8343,8 @@ sub _next_elevation_screen { '59588' => 2.0, # ACS 3 ); +# $$ END + 1; __END__ diff --git a/tools/heavens-above-mag b/tools/heavens-above-mag index 4af93750..20b80e28 100755 --- a/tools/heavens-above-mag +++ b/tools/heavens-above-mag @@ -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'; @@ -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, '*'; @@ -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 ) { @@ -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' ]} @@ -185,6 +233,7 @@ EOD my %oid = parse_visual( $visual ); process_perl( sort { $a <=> $b } keys %oid ); say ');'; + print @postamble; return; } @@ -299,7 +348,7 @@ L. 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 @@ -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 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