Astro/Common.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 269 CC 13 SCAR 25.3
CC
13
Cov
0%
ephemeris_correction
72.0
EC_C
6.0
EC_X
6.0
CRAP 12.5
1 # $Id: /mirror/datetime/DateTime-Util-Astro/trunk/lib/DateTime/Util/Astro/Common.pm 31665 2007-12-04T03:23:42.352707Z lestrrat $
2 #
3 # Copyright (c) 2004-2007 Daisuke Maki <daisuke@endeworks.jp>
4 #
5 # Please see file "LICENSE" for license information on code from
6 # "Calendrical Calculations".
7  
8 package DateTime::Util::Astro::Common;
9 use strict;
10 use warnings;
11 use vars qw($VERSION @EXPORT_OK);
12 use Exporter;
13 use DateTime::Util::Astro;
14  
15 BEGIN
16 {
17 *import = \&Exporter::import;
18 $VERSION = $DateTime::Util::Astro::VERSION;
19 @EXPORT_OK = qw(
20 aberration
21 dt_from_dynamical
22 dynamical_moment_from_dt
23 ephemeris_correction
24 equation_of_time
25 julian_centuries
26 local_from_apparent
27 nutation
28 obliquity
29 standard_from_local
30 standard_from_universal
31 local_from_standard
32 local_from_universal
33 universal_from_local
34 universal_from_standard
35 SPRING SUMMER AUTUMN WINTER
36 MEAN_TROPICAL_YEAR
37 RD_MOMENT_1900_JAN_1
38 RD_MOMENT_1810_JAN_1
39 RD_MOMENT_J2000
40 );
41 }
42  
43 use DateTime;
44 use DateTime::Util::Calc
45 qw(angle polynomial sin_deg cos_deg tan_deg bigfloat
46 min moment dt_from_moment);
47 use Math::BigInt ('upgrade' => 'Math::BigFloat');
48 use Math::BigFloat ('lib' => 'GMP,Pari');
49 use Math::Trig qw(pi);
50  
51 # I got the following from (DateTime->new(...)->utc_rd_values)[0]
52 use constant RD_MOMENT_1900_JAN_1 =>
53 (DateTime->new(year => 1900, month => 1, day => 1)->utc_rd_values)[0];
54 use constant RD_MOMENT_1810_JAN_1 => Math::BigFloat->new('660724.5');
55 use constant RD_MOMENT_J2000 => Math::BigFloat->new('730120.5');
56 use constant MEAN_TROPICAL_YEAR => Math::BigFloat->new('365.242189');
57 use constant SPRING => 0;
58 use constant SUMMER => 90;
59 use constant AUTUMN => 180;
60 use constant WINTER => 270;
61  
62 # p.170
63 sub standard_from_universal
64 {
65 my($dt, $location) = @_;
66 dt_from_moment(moment($dt) + $location->zone / 24);
67 }
68  
69 # p.170
70 sub universal_from_standard
71 {
72 my($dt, $location) = @_;
73 dt_from_moment(moment($dt) - $location->zone / 24);
74 }
75  
76 # p.170
77 sub standard_from_local
78 {
79 my($dt, $location) = @_;
80 standard_from_universal(dt_from_moment(
81 universal_from_local($dt, $location)), $location);
82 }
83  
84 # p.170
85 sub local_from_standard
86 {
87 my($dt, $location) = @_;
88 local_from_universal(dt_from_moment(
89 universal_from_standard($dt, $location)), $location);
90 }
91  
92 # p.169
93 sub local_from_universal
94 {
95 my($dt, $location) = @_;
96 moment($dt) + $location->longitude / 360;
97 }
98 # p.169
99 sub universal_from_local
100 {
101 my($dt, $location) = @_;
102 moment($dt) - $location->longitude / 360;
103 }
104  
105 # [1] p172
106 sub dynamical_moment_from_dt
107 {
108 my $dt = shift;
109 return moment($dt) + ephemeris_correction($dt);
110 }
111  
112 sub dt_from_dynamical
113 {
114 my $t = shift;
115  
116 return dt_from_moment(
117 $t - ephemeris_correction(dt_from_moment($t)));
118 }
119  
120  
121 # [1] p180
122 sub obliquity
123 {
124 my $dt = shift;
125 my $c = julian_centuries($dt);
126 return polynomial($c,
127 angle(23, 26, 21.448),
128 -1 * angle(0, 0, 46.8150),
129 -1 * angle(0, 0, 0.00059),
130 angle(0, 0, 0.001813)
131 );
132 }
133  
134 # [1] p171 + errata 158
135 my %EC;
136 sub ephemeris_correction
137 {
138 my $dt = shift;
139  
140 # we need a gregorian calendar, so make sure $dt is just 'DateTime'
141 if (ref($dt) ne 'DateTime') {
142 $dt = DateTime->from_object(object => $dt);
143 }
144  
145 my $year = $dt->year;
146 my $correction = $EC{ $year };
147 if (! $correction) {
148 if (1988 <= $year && $year <= 2019) {
149 $correction = EC1($year - 1933);
150 } elsif (1900 <= $year && $year <= 1987) {
151 $correction = EC2( EC_C($year) );
152 } elsif (1800 <= $year && $year <= 1899) {
153 $correction = EC3( EC_C($year) );
154 } elsif (1700 <= $year && $year <= 1799) {
155 $correction = EC4($year - 1700);
156 } elsif (1620 <= $year && $year <= 1699) {
157 $correction = EC5($year - 1600);
158 } else {
159 $correction = EC6( EC_X($year) );
160 }
161 $EC{ $year } = $correction;
162 }
163  
164 return $correction;
165 }
166  
167 my %EC_C;
168 sub EC_C
169 {
170 # This value is constant for a given year
171 my $value = $EC_C{ $_[0] };
172 if (! defined $value) {
173 my $top = (
174 (DateTime->new(
175 year => $_[0],
176 month => 7,
177 day => 1,
178 time_zone => 'UTC'
179 )->utc_rd_values)[0]
180 -
181 RD_MOMENT_1900_JAN_1
182 );
183 $value = $top / 36525;
184 $EC_C{ $_[0] } = $value;
185 }
186 return $value;
187 }
188  
189 my %EC_X;
190 sub EC_X
191 {
192 my $value = $EC_X{ $_[0] };
193 if (! defined $value) {
194 $value = (
195 (DateTime->new(
196 year => $_[0],
197 month => 1,
198 day => 1,
199 time_zone => 'UTC'
200 )->utc_rd_values)[0]
201 -
202 RD_MOMENT_1810_JAN_1
203 );
204 $EC_X{ $_[0] } = $value;
205 }
206 return $value;
207 }
208 sub EC1 { Math::BigFloat->new($_[0]) / (24 * 3600) }
209 sub EC2 {
210 polynomial($_[0], -0.00002, 0.000297, 0.025184,
211 -0.181133, 0.553040, -0.861938, 0.677066, -0.212591);
212 }
213 sub EC3 {
214 polynomial($_[0], qw(
215 -0.000009
216 0.003844
217 0.083563
218 0.865736
219 4.867575
220 15.845535
221 31.332267
222 38.291999
223 28.316289
224 11.636204
225 2.043794
226 ));
227 }
228 sub EC4 {
229 polynomial($_[0], 8.118780842, -0.005092142,
230 0.003336121, -0.0000266484) / (24 * 3600);
231 }
232 sub EC5
233 {
234 polynomial($_[0],
235 Math::BigFloat->new('196.58333'), Math::BigFloat->new('-4.0675'), Math::BigFloat->new('0.0219167')) / ( 24 * 3600 )
236 }
237 sub EC6
238 {
239 ((Math::BigFloat->new($_[0]) ** 2 / Math::BigFloat->new(41048480) ) - 15) / ( 24 * 3600 )
240 }
241  
242 # [1] p.183
243 sub aberration
244 {
245 my $dt = shift;
246 my $c = julian_centuries($dt);
247 return Math::BigFloat->new('0.0000974') * cos_deg(Math::BigFloat->new('177.63') + Math::BigFloat->new('35999.01848') * $c) - Math::BigFloat->new('0.0005575');
248 }
249  
250 # [1] p.172
251 sub julian_centuries
252 {
253 my $dt = shift;
254 return (dynamical_moment_from_dt($dt) - RD_MOMENT_J2000) / 36525;
255 }
256  
257 # [1] p.182
258 sub nutation
259 {
260 my $dt = shift;
261  
262 my $c = julian_centuries($dt);
263 my $A = polynomial($c,
264 Math::BigFloat->new('124.90'), Math::BigFloat->new('-1934.134'), Math::BigFloat->new('0.002063'));
265 my $B = polynomial($c,
266 Math::BigFloat->new('201.11'), Math::BigFloat->new('72001.5377'), Math::BigFloat->new('0.00057'));
267  
268 return Math::BigFloat->new('-0.004778') * sin_deg($A) +
269 Math::BigFloat->new('-0.0003667') * sin_deg($B);
270 }
271  
272 # [1] p.177
273 sub equation_of_time
274 {
275 my $dt = shift;
276 my $c = julian_centuries($dt);
277 my $longitude = polynomial($c,
278 280.46645, 36000.76983, 0.0003032);
279 my $anomaly = polynomial($c,
280 357.52910, 35999.05030, -0.0001559, -0.00000048);
281 my $eccentricity = polynomial($c,
282 0.016708617, -0.000042037, -0.0000001236);
283 my $epsilon = obliquity($dt);
284 my $y = tan_deg($epsilon / 2) ** 2;
285  
286 my $equation =
287 (bigfloat(1) / (2 * pi)) *
288 ($y * sin_deg(2 * $longitude) +
289 (-2 * $eccentricity * sin_deg($anomaly)) +
290 (4 * $eccentricity * $y * sin_deg($anomaly) * cos_deg(2 * $longitude)) +
291 (-0.5 * ($y ** 2) * sin_deg(4 * $longitude)) +
292 (-1.25 * ($eccentricity ** 2) * sin_deg(2 * $anomaly)));
293  
294 my $sign = $equation >= 0 ? 1 : -1;
295 $sign * min(abs($equation), 0.5);
296 }
297  
298 # [1] p.178
299 sub local_from_apparent
300 {
301 my $dt = shift;
302 my $delta = equation_of_time($dt);
303 if ($delta) {
304 $dt->subtract(seconds => bf_downgrade($delta * 24 * 3600));
305 }
306 $dt;
307 }
308  
309 package DateTime::Util::Astro::Location;
310 use strict;
311  
312 sub new
313 {
314 my $class = shift;
315 my %args = @_;
316  
317 bless \%args, $class;
318 }
319  
320 BEGIN
321 {
322 foreach my $f qw(longitude latitude elevation zone) {
323 eval sprintf( <<'EOM', $f, $f, $f);
324 sub %s
325 {
326 my $self = shift;
327 my $ret = $self->{%s};
328 if (@_) {
329 my $val = shift;
330 $self->{%s} = $val;
331 }
332 return $ret;
333 }
334 EOM
335 die if $@;
336 }
337 }
338  
339 1;
340  
341 __END__