File Coverage

lib/Astro/Montenbruck/RiseSet.pm
Criterion Covered Total %
statement 74 77 96.1
branch 1 2 50.0
condition n/a
subroutine 22 23 95.6
pod 2 2 100.0
total 99 104 95.1


line stmt bran cond sub pod time code
1             package Astro::Montenbruck::RiseSet;
2              
3 3     3   2511 use strict;
  3         22  
  3         99  
4 3     3   15 use warnings;
  3         6  
  3         82  
5 3     3   17 no warnings qw/experimental/;
  3         6  
  3         102  
6 3     3   16 use feature qw/switch/;
  3         6  
  3         255  
7              
8 3     3   20 use Exporter qw/import/;
  3         5  
  3         98  
9 3     3   16 use Readonly;
  3         5  
  3         141  
10 3     3   1862 use Memoize;
  3         7759  
  3         232  
11             memoize qw/_get_obliquity/;
12              
13 3     3   23 use Math::Trig qw/:pi deg2rad/;
  3         6  
  3         353  
14 3     3   22 use Astro::Montenbruck::MathUtils qw/frac/;
  3         6  
  3         126  
15 3     3   1270 use Astro::Montenbruck::Time qw/jd_cent/;
  3         10  
  3         246  
16 3     3   1266 use Astro::Montenbruck::CoCo qw/ecl2equ/;
  3         7  
  3         175  
17 3     3   1214 use Astro::Montenbruck::NutEqu qw/obliquity/;
  3         7  
  3         167  
18 3     3   1242 use Astro::Montenbruck::Ephemeris qw/iterator/;
  3         12  
  3         189  
19 3     3   23 use Astro::Montenbruck::Ephemeris::Planet qw/:ids/;
  3         6  
  3         395  
20 3     3   849 use Astro::Montenbruck::RiseSet::Constants qw/:altitudes :twilight :states/;
  3         7  
  3         518  
21 3     3   1375 use Astro::Montenbruck::RiseSet::RST qw/rst_function/;
  3         7  
  3         183  
22 3     3   1305 use Astro::Montenbruck::RiseSet::Sunset qw/riseset/;
  3         9  
  3         1700  
23              
24             our %EXPORT_TAGS = (
25             all => [ qw/rst_event twilight/ ],
26             );
27             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
28             our $VERSION = 0.01;
29              
30             Readonly::Array our @TWILIGHT_TYPES =>
31             ($TWILIGHT_ASTRO, $TWILIGHT_NAUTICAL, $TWILIGHT_CIVIL);
32              
33              
34             sub _get_obliquity { obliquity( $_[0] ) }
35              
36             sub _get_equatorial {
37 643     643   3652 my ( $id, $jd ) = @_;
38 643         3999 my $t = jd_cent($jd);
39 643         2406 my $iter = iterator( $t, [$id] );
40 643         1774 my $res = $iter->();
41 643         1937 my @ecl = @{ $res->[1] }[ 0 .. 1 ];
  643         1977  
42 643         15622 map { deg2rad($_) } ecl2equ( @ecl, _get_obliquity($t) );
  1286         9418  
43             }
44              
45              
46             sub twilight {
47 11     11 1 10143 my %arg = (type => $TWILIGHT_NAUTICAL, @_);
48 11         113 my $type = delete $arg{type};
49 11 50       78 die "Unknown twilight type: \"$type\"" unless exists $H0_TWL{$type};
50              
51             riseset(
52             %arg,
53 235     235   462 get_position => sub { _get_equatorial( $SU, $_[0] ) },
54 11         167 sin_h0 => sin( deg2rad($H0_TWL{$type}) ),
55             );
56             }
57              
58             # Return the standard altitude of the Moon.
59             #
60             # Arguments:
61             # - $r : Distance between the centers of the Earth and Moon, in km.
62             # Returns:
63             # - Standard altitude in radians.
64             sub _moon_rs_alt {
65 0     0   0 my ($y, $m, $d) = @_;
66 0         0 $H0_MOO
67             }
68              
69             sub rst_event {
70 9     9 1 3347 my %arg = @_;
71 9         30 my $pla = delete $arg{planet};
72              
73             rst_function(
74             h => do {
75 9         21 given( $pla ) {
76 9         42 $H0_SUN when $SU;
77 8         67 _moon_rs_alt(@{$arg{date}}) when $MO;
  0         0  
78 8         43 default { $H0_PLA }
  8         25  
79             }
80             },
81             get_position => sub {
82 27     27   49 my $jd = shift;
83 27         59 _get_equatorial( $pla, $jd )
84             },
85 9         23 %arg
86             )
87             }
88              
89             1;
90             __END__
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             Astro::Montenbruck::RiseSet — rise, set, transit.
99              
100             =head1 SYNOPSIS
101              
102             use Astro::Montenbruck::Ephemeris::Planet qw/:ids/;
103             use Astro::Montenbruck::MathUtils qw/frac/;
104             use Astro::Montenbruck::RiseSet::Constants qw/:all/;
105             use Astro::Montenbruck::RiseSet' qw/:all/;
106              
107             # create function for calculating Moon events for Munich, Germany, on March 23, 1989.
108             my $func = rst_event(
109             planet => $MO,
110             date => [1989, 3, 23],
111             phi => 48.1,
112             lambda => -11.6
113             );
114              
115             # calculate Moon rise. Alternatively, use $EVT_SET for set, $EVT_TRANSIT for
116             # transit as the first argument
117             $func->(
118             $EVT_RISE,
119             on_event => sub {
120             my $jd = shift; # Standard Julian date
121             my $ut = frac(jd - 0.5) * 24; # UTC, 18.95 = 18h57m
122             }
123             );
124              
125             # calculate civil twilight
126             twilight(
127             date => [1989, 3, 23],
128             phi => 48.1,
129             lambda => -11.6,
130             on_event => sub {
131             my ($evt, $ut) = @_;
132             say "$evt: $ut";
133             },
134             on_noevent => sub {
135             my $state = shift;
136             say $state;
137             }
138             );
139              
140             =head1 VERSION
141              
142             Version 0.01
143              
144              
145             =head1 DESCRIPTION
146              
147             High level interface for calculating rise, set and transit times of celestial
148             bodies, as well as twilight of different types.
149              
150             To take into account I<parallax>, I<refraction> and I<apparent radius> of the
151             bodies, we use average corrections to geometric altitudes:
152              
153             =over
154              
155             =item * sunrise, sunset : B<-0°50'>
156              
157             =item * moonrise, moonset : B<0°8'>
158              
159             =item * stars and planets : B<-0°34'>
160              
161             =back
162              
163             =head2 TWILIGHT
164              
165             The library also calculates the times of the beginning of the morning twilight
166             (I<dawn>) and end of the evening twilight (I<dusk>).
167              
168             Twilight occurs when Earth's upper atmosphere scatters and reflects sunlight
169             which illuminates the lower atmosphere. Astronomers define the three stages of
170             twilight – I<civil>, I<nautical>, and I<astronomical> – on the basis of the Sun's
171             elevation which is the angle that the geometric center of the Sun makes with the
172             horizon.
173              
174             =over
175              
176             =item * I<astronomical>
177              
178             Sun altitude is B<-18°> In the morning, the sky is completely dark before the
179             onset of astronomical twilight, and in the evening, the sky becomes completely
180             dark at the end of astronomical twilight. Any celestial bodies that can be
181             viewed by the naked eye can be observed in the sky after the end of this phase.
182              
183             =item * I<nautical>
184              
185             Sun altitude is B<-12°>. This twilight period is less bright than civil twilight
186             and artificial light is generally required for outdoor activities.
187              
188             =item * I<civil>
189              
190             Sun altitude is B<-6°>. Civil twilight is the brightest form of twilight.
191             There is enough natural sunlight during this period that artificial light may
192             not be required to carry out outdoor activities. Only the brightest celestial
193             objects can be observed by the naked eye during this time.
194              
195             =back
196              
197             =head1 EXPORT
198              
199             =head2 FUNCTIONS
200              
201             =over
202              
203             =item * L</rst_event( %args )>
204              
205             =item * L</twilight( %args )>
206              
207             =back
208              
209             =head1 FUNCTIONS
210              
211             =head2 rst_event( %args )
212              
213             Returns function for calculating time of event. See
214             L<Astro::Montenbruck::RiseSet::RST/EVENT FUNCTION> .
215              
216             =head3 Named Arguments
217              
218             =over
219              
220             =item * B<planet> — celestial body identifier, one of constants defined in
221             L<Astro::Montenbruck::Ephemeris::Planet>.
222              
223             =item * B<date> — array of B<year> (astronomical, zero-based), B<month> [1..12]
224             and B<day>, [1..31].
225              
226             =item * B<phi> — geographical latitude, degrees, positive northward
227              
228             =item * B<lambda> — geographical longitude, degrees, positive westward
229              
230             =back
231              
232              
233             =head2 twilight( %args )
234              
235             Function for calculating twilight. See L</TWILIGHT EVENT FUNCTION> below.
236              
237             =head3 Named Arguments
238              
239             =over
240              
241             =item * B<type> — type of twilight, C<$TWILIGHT_NAUTICAL>, C<$TWILIGHT_ASTRO>
242             or C<$TWILIGHT_CIVIL>, see L<Astro::Montenbruck::RiseSet::Constants/TYPES OF TWILIGHT>.
243              
244             =item * B<date> — array of B<year> (astronomical, zero-based), B<month> [1..12] and B<day>, [1..31].
245              
246             =item * B<phi> — geographical latitude, degrees, positive northward
247              
248             =item * B<lambda> — geographical longitude, degrees, positive westward
249              
250             =item * B<on_event> — callback called when the event time is determined. The arguments are:
251              
252             =over
253              
254             =item * Event type, one of C<$EVT_RISE> or C<$EVT_SET>,
255             L<Astro::Montenbruck::RiseSet::Constants/EVENTS>. The first indicates I<dawn>,
256             the second — I<dusk>.
257              
258             =item * Time of the event, UTC.
259              
260             =back
261              
262             =item * B<on_noevent> is called when the event never happens, either because the body
263             never rises, or is circumpolar. The argument is respectively
264             C<$STATE_NEVER_RISES> or C<$STATE_CIRCUMPOLAR>, see
265             L<Astro::Montenbruck::RiseSet::Constants/STATES>.
266              
267             =back
268              
269             =head1 AUTHOR
270              
271             Sergey Krushinsky, C<< <krushi at cpan.org> >>
272              
273             =head1 COPYRIGHT AND LICENSE
274              
275             Copyright (C) 2010-2019 by Sergey Krushinsky
276              
277             This library is free software; you can redistribute it and/or modify
278             it under the same terms as Perl itself.
279              
280             =cut