Change DXUser->get* to DXUser::get*
[spider.git] / cmd / show / moon.pl
1 #!/usr/bin/perl
2 #
3 # show moonrise and moonset times for each callsign or prefix entered
4 #
5 # 1999/11/9 Steve Franke K9AN
6 # 2000/10/27 fixed bug involving degree to radian conversion.
7 # 2001/09/15 accept prefix/call and number of days from today (+ or -). 
8 #            e.g. sh/moon 2 w0 w9      shows rise/set 2 days hence for w0, w9
9 #                 sh/moon w0 w9 2      same thing
10 #            az and el are shown only when day offset is zero (i.e. today).
11
12 my ($self, $line) = @_;
13 my @f = split /\s+/, $line;
14
15 my @out;
16 my $f;
17 my $l;
18 my $n_offset;
19 my @list;
20 my ($rise, $set, $az, $dec, $loss, $ifrac);
21
22 while ($f = shift @f){
23         if(!$n_offset){
24                 ($n_offset) = $f =~ /^([-+]?\d+)$/;
25                 next if $n_offset;
26         }
27         push @list, $f;
28 }
29 $n_offset = 0 unless defined $n_offset;
30 $n_offset = 0 if $n_offset > 365;  # can request moon rise/set up to 1 year ago or from now... 
31 $n_offset = 0 if $n_offset < -365;
32
33 my ($lat, $lon);              # lats and longs in radians
34 my ($sec, $min, $hr, $day, $month, $yr) = (gmtime($main::systime+$n_offset*24*60*60))[0,1,2,3,4,5];
35
36 $month++;
37 $yr += 1900;
38
39 my @in;
40
41 if (@list) {
42         foreach $l (@list) {
43                 my $user = DXUser::get_current(uc $l);
44                 if ($user && $user->lat && $user->long) {
45                         push @in, [$user->qth, $user->lat, -$user->long, uc $l ];
46                 } else {
47                         # prefixes --->
48                         my @ans = Prefix::extract($l);
49                         next if !@ans;
50                         my $pre = shift @ans;
51                         my $a;
52                         foreach $a (@ans) {
53                                 $lat = $a->{lat};
54                                 $lon = -$a->{long};
55                                 push @in, [ $a->name, $lat, $lon, $pre ];
56                         }
57                 }
58         }
59 } else {
60         if ($self->user->lat && $self->user->long) {
61                 push @in, [$self->user->qth, $self->user->lat, -$self->user->long, $self->call ];
62         } else {
63                 push @in, [$main::myqth, $main::mylatitude, -$main::mylongitude, $main::mycall ];
64         }
65 }
66
67 if( !$n_offset ) {
68         push @out, $self->msg('moon_with_azel');
69 } else {
70         push @out, $self->msg('moon');
71 }
72
73 foreach $l (@in) {
74         ($rise, $set, $az, $dec, $loss, $ifrac)=Sun::rise_set($yr,$month,$day,$hr,$min,$l->[1],$l->[2],1);
75         $l->[3] =~ s{(-\d+|/\w+)$}{};
76         if( !$n_offset ) {      
77         push @out,sprintf("%-6.6s %-30.30s %02d/%02d/%4d %s %s %6.1f %6.1f", $l->[3], $l->[0], $day, $month, $yr, $rise, $set, $az, $dec);
78         } else {
79         push @out,sprintf("%-6.6s %-30.30s %02d/%02d/%4d %s %s", $l->[3], $l->[0], $day, $month, $yr, $rise, $set);
80         }
81 }
82 push @out,sprintf("Illuminated fraction of the Moon's disk is %4.2f",$ifrac);
83 return (1, @out);