The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/local/bin/perl

# This script uses Astro::SpaceTrack to download Iridium data from
# http://celestrak.com/ and predict flares for the next two days as seen
# from a hard-coded location. It takes about 30 seconds on a
# lightly-loaded 800 MHz PowerPC G4.

use strict;
use warnings;

#	In addition to Astro::Coord::ECI and friends, we need:
#	Astro::SpaceTrack
#	POSIX (which should be standard)

use Astro::Coord::ECI;
use Astro::Coord::ECI::TLE;
use Astro::Coord::ECI::Utils qw(deg2rad rad2deg SECSPERDAY);
use Astro::SpaceTrack;
use POSIX qw{strftime};

our $VERSION = '0.056';

use constant TIMFMT => '%d %b %H:%M:%S';


#	Create an object to represent the observing station. We use
#	data in degrees north and east, and height above sea level in
#	meters. But the object wants radians and kilometers, so we
#	convert.

my $sta = Astro::Coord::ECI->new (
    name => 'Los Pinos, Ciudad Mexico, Mexico',
)->geodetic (
    deg2rad (19.415275),		# Latitude north
    deg2rad (-99.190859),	# Longitude east
    2269/1000,			# Height above sea level
);


#	Fetch Iridium data. The result of the fetch is a
#	HTTP::Response object.

my $st = Astro::SpaceTrack->new (direct => 1);
my $tle = $st->celestrak ('iridium');
$tle->is_success or die <<eod;
Error - Failed to retrieve Iridium data.
        @{[$tle->status_line]}
eod

#	Parse the Iridium data to get Astro::Coord::ECI::TLE
#	objects.

my @bodies = Astro::Coord::ECI::TLE->parse(
    { station => $sta }, $tle->content);

#	For each of the bodies retrieved

my $start = time ();
my $end = $start + 2 * SECSPERDAY;
my @flares;
foreach my $tle (@bodies) {
    next unless $tle->can_flare ();
    push @flares, $tle->flare ( $start, $end )
}

my %mag_limit = (
    am => 0,
    day => -6,
    pm => 0,
);

{	# Begin local symbol block

    print "Iridium flares at\n";
    my $name = $sta->get ('name');
    $name and print "$name\n";
    my ($lat, $lon, $hgt) = $sta->geodetic ();
    printf "Latitude %9.4f, longitude %9.4f, height %5.0f meters\n",
	rad2deg ($lat), rad2deg ($lon), $hgt * 1000;
    print strftime (TIMFMT, localtime $start), " to ",
	strftime (TIMFMT, localtime $end), "\n\n";

}	# End local symbol block

print <<eod;
  Date/Time      Satellite    Elevation    Azimuth Magnitude
eod
foreach my $flare (sort {$a->{time} <=> $b->{time}} @flares) {
    $flare->{magnitude} > $mag_limit{$flare->{type}} and next;
    my $name = $flare->{body}->get ('name');
    $name =~ s/\[.*//;
    $name =~ s/\s+$//;
    printf "%s  %-11s  %9.1f  %9.1f  %5.1f\n",
	strftime (TIMFMT, localtime $flare->{time}),
	ucfirst (lc $name),
	rad2deg ($flare->{elevation}),
	rad2deg ($flare->{azimuth}),
	$flare->{magnitude};
}

# ex: set textwidth=72 :