File Coverage

blib/lib/Astro/Coords/Angle/Hour.pm
Criterion Covered Total %
statement 64 66 96.9
branch 19 22 86.3
condition 7 9 77.7
subroutine 15 15 100.0
pod 4 4 100.0
total 109 116 93.9


line stmt bran cond sub pod time code
1             package Astro::Coords::Angle::Hour;
2              
3             =head1 NAME
4              
5             Astro::Coords::Angle::Hour - Representation of an angle in units of hours
6              
7             =head1 SYNOPSIS
8              
9             use Astro::Coords::Angle::Hour;
10              
11             $ha = new Astro::Coords::Angle::Hour( "12h30m22.4s", units => 'sex');
12             $ha = new Astro::Coords::Angle::Hour( 12.53, units => 'hour);
13              
14             $ha = new Astro::Coords::Angle::Hour( 12.53 );
15              
16             =head1 DESCRIPTION
17              
18             Class similar to C<Astro::Coords::Angle> but representing the angle
19             as a time. Suitable for use as hour angle or Right Ascension.
20             Inherits from C<Astro::Coords::Angle>.
21              
22             For hour angle a range of "PI" is suitable, for Right Ascension use "2PI".
23             Default range is none at all. If no units are provided, the units will be
24             guessed using the same scheme as for C<Astro::Coords::Angle> except that
25             values greater than 2PI will be assumed to be decimal hours.
26              
27             =cut
28              
29              
30 21     21   16208891 use 5.006;
  21         77  
31 21     21   116 use strict;
  21         63  
  21         588  
32 21     21   122 use warnings;
  21         73  
  21         641  
33 21     21   114 use warnings::register;
  21         45  
  21         2700  
34 21     21   168 use Carp;
  21         58  
  21         1470  
35              
36 21     21   753 use Astro::PAL;
  21         4775  
  21         9059  
37              
38 21     21   163 use base qw/ Astro::Coords::Angle /;
  21         62  
  21         3165  
39              
40             # Package Global variables
41 21     21   167 use vars qw/ $VERSION /;
  21         57  
  21         12824  
42              
43             $VERSION = '0.21';
44              
45             =head1 METHODS
46              
47             =head2 Accessor Methods
48              
49             =over 4
50              
51             =item B<hours>
52              
53             Return the angle in decimal hours.
54              
55             $deg = $ang->hours;
56              
57             =cut
58              
59             sub hours {
60 28     28 1 85 my $self = shift;
61 28         77 my $rad = $self->radians;
62 28         154 return $rad * Astro::PAL::DR2H;
63             }
64              
65             =back
66              
67             =head2 General Methods
68              
69             =over 4
70              
71             =item B<in_format>
72              
73             As for base class implementation, except that 'hour' (and abbreviation) is a supported
74             format. 'sexagesimal' format will result in a stringified form of the object in hours,
75             minutes and seconds.
76              
77             $hr = $hour->in_format( 'hour' );
78              
79             =cut
80              
81             sub in_format {
82 23126     23126 1 37096 my $self = shift;
83 23126         48986 my $format = shift;
84 23126 100       46414 $format = lc($format) if $format;
85 23126 100 100     54049 return $self->hours() if (defined $format && $format =~ /^h/);
86 23116         57226 return $self->SUPER::in_format( $format );
87             }
88              
89             =back
90              
91             =head2 Class Methods
92              
93             The following methods control the default behaviour of the class.
94              
95             =over 4
96              
97             =item B<NDP>
98              
99             As for the base class except that the default number of decimal places
100             is 3.
101              
102             This method has no effect on the base class.
103              
104             =cut
105              
106             {
107             my $DEFAULT_NDP = 3;
108             my $NDP = $DEFAULT_NDP;
109             sub NDP {
110 33     33 1 7292 my $class = shift;
111 33 100       90 if (@_) {
112 3         7 my $arg = shift;
113 3 50       25 if (defined $arg) {
114 3         10 $NDP = $arg;
115             } else {
116 0         0 $NDP = $DEFAULT_NDP;
117             }
118             }
119 33         95 return $NDP;
120             }
121             }
122              
123             =item B<DELIM>
124              
125             As for the base class. The global value in this class does not have
126             any effect on the base class.
127              
128             =cut
129              
130             {
131             my $DEFAULT_DELIM = ":";
132             my $DELIM = $DEFAULT_DELIM;
133             sub DELIM {
134 35     35 1 74 my $class = shift;
135 35 100       101 if (@_) {
136 2         6 my $arg = shift;
137 2 50       7 if (defined $arg) {
138 2         7 $DELIM = $arg;
139             } else {
140 0         0 $DELIM = $DEFAULT_DELIM;
141             }
142             }
143 35         90 return $DELIM;
144             }
145             }
146              
147              
148             =back
149              
150              
151             =begin __PRIVATE_METHODS__
152              
153             =head2 Private Methods
154              
155             These methods are not part of the API and should not be called directly.
156             They are documented for completeness.
157              
158             =over 4
159              
160             =item B<_cvt_torad>
161              
162             Same as the base class, except that if the units are hours
163             or sexagesimal, the resulting number is multiplied by 15 before being
164             passed up to the constructor (since 24 hours is equivalent to 360 degrees).
165              
166             =cut
167              
168             sub _cvt_torad {
169 33238     33238   56146 my $self = shift;
170 33238         50639 my $input = shift;
171 33238         46699 my $units = shift;
172              
173             # If we haven't got any units attempt to guess some
174 33238 100       66866 $units = $self->_guess_units( $input ) unless defined $units;
175              
176             # if units are hours, tell the base class we have degrees
177             # $unt is the unit that will be reported to the base class
178             # $units is the unit known to the subclass
179 33238         48759 my $unt = $units;
180 33238 100 66     117569 if (defined $units && $units =~ /^h/) {
181 6         14 $unt = 'deg';
182             }
183              
184             # Do the conversion
185 33238         91256 my $rad = $self->SUPER::_cvt_torad( $input, $unt );
186              
187             # scale if we had sexagesimal or hour as units
188 33238 100 66     128647 if (defined $rad && $units =~ /^[sh]/) {
189 1027         1987 $rad *= 15;
190             }
191              
192 33238         76024 return $rad;
193             }
194              
195             =item B<_guess_units>
196              
197             Guess the units. Same as base class except that values greater than 2PI
198             radians are assumed to be hours rather than degrees.
199              
200             $guess = $hr->_guess_units( $input );
201              
202             =cut
203              
204             sub _guess_units {
205 19     19   38 my $self = shift;
206 19         31 my $input = shift;
207 19         114 my $guess = $self->SUPER::_guess_units( $input );
208 19 100       90 $guess = 'h' if $guess =~ /^d/;
209 19         46 return $guess;
210             }
211              
212             =item B<_r2f>
213              
214             Routine to convert angle in radians to a formatted array
215             of numbers in order of sign, hour, min, sec, frac.
216              
217             @retval = $ang->_r2f( $ndp );
218              
219             Note that the number of decimal places is an argument.
220              
221             =back
222              
223             =cut
224              
225             sub _r2f {
226 36     36   62 my $self = shift;
227 36         66 my $res = shift;
228              
229 36 50       127 warnings::warnif("More than 9 dp requested ($res), result from palDr2tf likely to overflow in fractional part") if $res > 9;
230              
231 36         91 my ($sign, @dmsf) = Astro::PAL::palDr2tf($res, $self->radians);
232 36         151 return ($sign, @dmsf);
233             }
234              
235             =end __PRIVATE_METHODS__
236              
237             =head1 AUTHOR
238              
239             Tim Jenness E<lt>t.jenness@cpan.orgE<gt>
240              
241             =head1 COPYRIGHT
242              
243             Copyright (C) 2004-2005 Tim Jenness. All Rights Reserved.
244              
245             This program is free software; you can redistribute it and/or modify it under
246             the terms of the GNU General Public License as published by the Free Software
247             Foundation; either version 3 of the License, or (at your option) any later
248             version.
249              
250             This program is distributed in the hope that it will be useful,but WITHOUT ANY
251             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
252             PARTICULAR PURPOSE. See the GNU General Public License for more details.
253              
254             You should have received a copy of the GNU General Public License along with
255             this program; if not, write to the Free Software Foundation, Inc., 59 Temple
256             Place,Suite 330, Boston, MA 02111-1307, USA
257              
258             =cut
259              
260             1;