-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCoCo.pm
259 lines (151 loc) · 4.53 KB
/
CoCo.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package Astro::Montenbruck::CoCo;
use strict;
use warnings;
use Exporter qw/import/;
use POSIX qw /tan atan2 asin acos/;
use Astro::Montenbruck::MathUtils qw/reduce_rad/;
use Math::Trig qw/:pi deg2rad rad2deg/;
use Readonly;
Readonly::Scalar our $ECL => 1;
Readonly::Scalar our $EQU => 2;
our %EXPORT_TAGS = (
all => [ qw/ecl2equ equ2ecl equ2hor hor2equ ecl2equ_rect equ2ecl_rect/ ],
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} }, qw/$ECL $EQU/ );
our $VERSION = 0.01;
# Common routine for coordinate conversion
# $target = $ECL (1) for equator -> ecliptic
# $target = $EQU (2) for ecliptic -> equator
sub _equecl {
my ( $x, $y, $e, $target ) = @_;
my $k = $target == $ECL ? 1
: $target == $EQU ? -1 : 0;
die "Unknown target: '$target'! \n" until $k;
my $sin_a = sin($x);
my $cos_e = cos($e);
my $sin_e = sin($e);
reduce_rad(atan2( $sin_a * $cos_e + $k * ( tan($y) * $sin_e ), cos($x) )),
asin( sin($y) * $cos_e - $k * ( cos($y) * $sin_e * $sin_a ) );
}
sub equ2ecl {
map { rad2deg $_ } _equecl( ( map { deg2rad $_ } @_ ), $ECL );
}
sub ecl2equ {
map { rad2deg $_ } _equecl( ( map { deg2rad $_ } @_ ), $EQU );
}
# Converts between azimuth/altitude and hour-angle/declination.
# The equations are symmetrical in the two pairs of coordinates so that exactly
# the same code may be used to convert in either direction, there is no need
# to specify direction with a swich.
sub _equhor {
my ($x, $y, $phi) = @_;
my ($sx, $sy, $sphi) = map{ sin } ($x, $y, $phi);
my ($cx, $cy, $cphi) = map{ cos } ($x, $y, $phi);
my $sq = ($sy * $sphi) + ($cy * $cphi * $cx);
my $q = asin($sq);
my $cp = ($sy - ($sphi * $sq)) / ($cphi * cos($q));
my $p = acos($cp);
if ($sx > 0) {
$p = pi2 - $p;
}
($p, $q)
}
sub equ2hor {
map {rad2deg $_} _equhor( map { deg2rad $_ } @_ )
}
sub hor2equ {
equ2hor(@_)
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Astro::Montenbruck::CoCo - Coordinates conversions.
=head1 VERSION
Version 0.01
=head1 DESCRIPTION
Celestial sphera related calculations used by Astro::Montenbruck modules.
=head1 EXPORT
=over
=item * L</equ2ecl($alpha, $delta, $epsilon)>
=item * L</ecl2equ($lambda, $beta, $epsilon)>
=back
=head1 FUNCTIONS
=head2 equ2ecl($alpha, $delta, $epsilon)
Conversion of equatorial into ecliptic coordinates
=head3 Arguments
=over
=item * B<$alpha> — right ascension
=item * B<$delta> — declination
=item * B<$epsilon> — ecliptic obliquity
=back
=head3 Returns
Ecliptic coordinates:
=over
=item * B<$lambda>
=item * B<$beta>
=back
All arguments and return values are in degrees.
=head2 ecl2equ($lambda, $beta, $epsilon)
Conversion of ecliptic into equatorial coordinates
=head3 Arguments
=over
=item * B<$lambda> — celestial longitude
=item * B<$beta> — celestial latitude
=item * B<$epsilon> — ecliptic obliquity
=back
=head3 Returns
Equatorial coordinates:
=over
=item * B<$alpha> — right ascension
=item * B<$delta> — declination
=back
All arguments and return values are in degrees.
=head2 equ2hor($h, $delta, $phi)
Conversion of equatorial into horizontal coordinates
=head3 Arguments
=over
=item *
B<$h> — the local hour angle, in degrees, measured westwards from the South.
C<h = Local Sidereal Time - Right Ascension>
=item *
B<$delta> — declination, in arc-degrees
=item *
B<$phi> — the observer's latitude, in arc-degrees, positive in the nothern
hemisphere, negative in the southern hemisphere.
=back
=head3 Returns
Horizontal coordinates:
=over
=item * B<azimuth>, in degrees, measured westward from the South
=item * B<altitude>, in degrees, positive above the horizon
=back
=head2 hor2equ($az, $alt, $phi)
Convert horizontal to equatorial coordinates.
=head3 Arguments
=over
=item *
B<$az> — azimuth, in degrees, measured westward from the South
=item *
B<$alt> — altitude, in degrees, positive above the horizon
=item *
B<$phi> — the observer's latitude, in arc-degrees, positive in the nothern
hemisphere, negative in the southern hemisphere.
=back
=head3 Returns
Horizontal coordinates:
=over
=item * hour angle, in arc-degrees
=item * declination, in arc-degrees
=back
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Astro::Montenbruck::CoCo
=head1 AUTHOR
Sergey Krushinsky, C<< <krushi at cpan.org> >>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2009-2022 by Sergey Krushinsky
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut