Astro/Catalog.pm

filter: Statementstmt 61.6% 241 / 391 Branchbran 46.8% 104 / 222 Conditioncond 12.8% 5 / 39 MC/DCmcdc 4.1% 1 / 24 Subroutinesub 72.5% 37 / 51 Podpod 100% 38 / 38 totaltotal 55.6% 426 / 765 CC 131 SCAR 45.8
CC
131
Cov
54%
sort_catalog
650.0
reftime
42.0
reference
30.0
CRAP 97.6
« Astro/Catalog/IO/Hedwig.pm ↑ Summary Astro/Catalog/Query/USNOA2.pm »
1 package Astro::Catalog;
2  
3 =head1 NAME
4  
5 Astro::Catalog - A generic API for stellar catalogs
6  
7 =head1 SYNOPSIS
8  
9 $catalog = new Astro::Catalog(Stars => \@array);
10 $catalog = new Astro::Catalog(Format => 'Cluster', File => $file_name);
11 $catalog = new Astro::Catalog(Format => 'JCMT', Data => $scalar);
12 $catalog = new Astro::Catalog(Format => 'Simple', Data => \*STDIN);
13 $catalog = new Astro::Catalog(Format => 'VOTable', Data => \@lines);
14  
15 =head1 DESCRIPTION
16  
17 Stores generic meta-data about an astronomical catalog. Takes a hash
18 with an array reference as an argument. The array should contain a list
19 of Astro::Catalog::Item objects. Alternatively it takes a catalog
20 format and either the name of a catalog file or a reference to a
21 scalar, glob or array.
22  
23 =head1 FORMATS
24  
25 For input the C<Astro::Catalog> module understands Cluster, Simple,
26 JCMT, TST, STL, GaiaPick, the UKIRT internal Bright Star catalog
27 format and (a very simple parsing) of VOTable.
28  
29 The module can output all of these formats except TST (which is input only).
30  
31 =cut
32  
33 67 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 56 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 56 ▶use Astro::Coords;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 56 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 56 ▶use Time::Piece qw/:override/;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 56 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 56 our $VERSION = '4.38';
43 56 our $DEBUG = 0;
44  
45 =head1 METHODS
46  
47 =head2 Constructor
48  
49 =over 4
50  
51 =item B<new>
52  
53 Create a new instance from a hash of options
54  
55 $catalog = new Astro::Catalog(Stars => \@array);
56 $catalog = new Astro::Catalog(Format => 'Cluster', File => $file_name);
57 $catalog = new Astro::Catalog(Format => 'JCMT', Data => $scalar);
58  
59 returns a reference to an C<Astro::Catalog> object. See the C<configure> method
60 for a list of allowed arguments.
61  
62 =cut
63  
64 52 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
65 52 my $proto = shift;
66 52 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0520
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
67  
68 # bless the query hash into the class
69 52 my $block = bless {
70 ALLSTARS => [],
71 CURRENT => undef, # undefined until we copy
72 ERRSTR => '',
73 ORIGIN => 'UNKNOWN',
74 COORDS => undef,
75 RADIUS => undef,
76 REFPOS => undef,
77 REFTIME => undef,
78 FIELDDATE => undef,
79 AUTO_OBSERVE => 0,
80 PREFERRED_MAG_TYPE => undef,
81 IDS => {},
82 MISC => undef,
83 }, $class;
84  
85 # If we have arguments configure the object
86 # Note that configuration can result in a new object
87 52 ▶ $block = $block->configure(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 34 18 52
88  
89 51 return $block;
90 }
91  
92 =back
93  
94 =head2 Output Methods
95  
96 =over 4
97  
98 =item B<write_catalog>
99  
100 Will serialise the catalog object in a variety of file formats using
101 pluggable IO, see the C<Astro::Catalog::IO> classes
102  
103 $catalog->write_catalog(
104 File => $file_name, Format => $file_type, [%opts])
105 or die $catalog->errstr;
106  
107 returns true on sucess and false if the write failed (the reason
108 can be obtained using the C<errstr> method). The C<%opts> are optional
109 arguments and are dependent on the output format chosen. Current
110 valid output formats are 'Simple', 'Cluster', 'JCMT' and 'VOTable'.
111  
112 The File argument can refer to a file name on disk (simple scalar),
113 a glob (eg \*STDOUT), an IO::Handle object (for example something
114 returned by the File::Temp constructor) a reference to a scalar
115 (\$content) or reference to an array. For the last two options,
116 the contents of the catalog file are stored in the scalar or in
117 the array (a line per array entry with no new lines).
118  
119 =cut
120  
121 11 ▶sub write_catalog {
Subroutine write_catalog: called CC=4 SCAR=14.5
Pod documented
122 11 my $self = shift;
123  
124 # grab the argument list
125 11 my %args = @_;
126  
127 # Go through hash and downcase all keys
128 11 %args = _normalize_hash(%args);
129  
130 # unless we have a Filename forget it...
131 11 ▶ unless($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"file"}) 0 11 11
132 0 croak('Usage: _write_catalog( File => $catalog, Format => $format');
133 }
134  
135 # default to cluster format if no filenames supplied
136 11 ▶ $args{format} = 'Cluster' unless defined $args{format};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"format"} 0 11 11
137  
138 # Need to read the IO class
139 11 my $ioclass = _load_io_plugin($args{format});
140 11 ▶ return unless defined $ioclass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ioclass 0 11 11
141  
142 # remove the two handled hash options and pass the rest
143 11 delete $args{format};
144  
145 # call the io plugin's write_catalog function
146 11 return $ioclass->write_catalog($self, %args);
147 }
148  
149 =back
150  
151 =head2 Accessor Methods
152  
153 =over 4
154  
155 =item B<origin>
156  
157 Return (or set) the origin of the data. For example, USNOA2, GSC
158 for catalog queries, or 'JCMT' for the JCMT pointing catalog.
159 No constraint is placed on the content of this parameter.
160  
161 $catalog->origin('JCMT');
162 $origin = $catalog->origin();
163  
164 =cut
165  
166 28 ▶sub origin {
Subroutine origin: called CC=2 SCAR=6.9
Pod documented
167 28 my $self = shift;
168 28 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 21 7 28
169 21 $self->{ORIGIN} = shift;
170 }
171 28 return $self->{ORIGIN};
172 }
173  
174 =item B<errstr>
175  
176 Error string associated with any error. Can only be trusted immediately
177 after a call that sets it (eg write_catalog).
178  
179 =cut
180  
181 0 ▶sub errstr {
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
182 0 my $self = shift;
183 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
184 0 $self->{ERRSTR} = shift;
185 }
186 0 return $self->{ERRSTR};
187 }
188  
189 =item B<preferred_magnitude_type>
190  
191 Set or return the preferred magnitude type to be returned from the
192 Astro::Catalog::Item->get_magnitude() method.
193  
194 my $type = $catalog->preferred_magnitude_type;
195 $catalog->preferred_magnitude_type('MAG_ISO');
196  
197 =cut
198  
199 0 ▶sub preferred_magnitude_type {
Subroutine preferred_magnitude_type: not called CC=2 SCAR=17.9
Pod documented
200 0 my $self = shift;
201 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
202 0 my $type = shift;
203 0 $self->{PREFERRED_MAG_TYPE} = $type;
204 }
205 0 return $self->{PREFERRED_MAG_TYPE};
206 }
207  
208 =item B<sizeof>
209  
210 Return the number of stars in the catalog (post filter).
211  
212 $num = $catalog->sizeof();
213  
214 =cut
215  
216 15 ▶sub sizeof {
Subroutine sizeof: called CC=1 SCAR=0.0
Pod documented
217 15 my $self = shift;
218 15 return scalar(@{$self->stars});
219 }
220  
221 =item B<sizeoffull>
222  
223 Returns the total number of stars in the catalog without filtering.
224  
225 =cut
226  
227 0 ▶sub sizeoffull {
Subroutine sizeoffull: not called CC=1 SCAR=6.9
Pod documented
228 0 my $self = shift;
229 0 return scalar(@{$self->allstars});
230 }
231  
232 =item B<pushstar>
233  
234 Push a new star (or stars) onto the end of the C<Astro::Catalog> object
235  
236 $catalog->pushstar(@stars);
237  
238 returns the number of stars now in the Catalog object (even if no
239 arguments were supplied). The method guarantees that the stars are
240 pushed onto the internal original list and the filtered/sorted
241 version.
242  
243 Currently no check is made to make sure that the star is already
244 on one of the two lists.
245  
246 =cut
247  
248 120 ▶sub pushstar {
Subroutine pushstar: called CC=4 SCAR=13.9
Pod documented
249 120 my $self = shift;
250  
251 120 my $allref = $self->allstars;
252  
253 # push onto the original array
254 120 push(@$allref, @_);
255  
256 # Update the IDs hash.
257 120 foreach my $star (@_) {
258 902 ▶ if (defined $star->id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $star->id) 887 15 902
259 887 $self->{IDS}->{$star->id} ++;
260 }
261 }
262  
263 # And push onto the copy ONLY IF WE HAVE A COPY
264 # We do not want to force a copy unnecsarily by using scalar context
265 120 ▶ if ($self->_have_copy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_have_copy) 2 118 120
266 # push the new item onto the stack
267 2 my $ref = $self->stars;
268 2 push(@$ref, @_);
269 }
270 120 return;
271 }
272  
273 =item B<popstar>
274  
275 Pop a star from the end of the C<Astro::Catalog> object. This forces
276 a copy of the array if one has not already been made (ie the original
277 version is unchanged).
278  
279 $star = $catalog->popstar();
280  
281 the method deletes the star and returns the deleted C<Astro::Catalog::Item>
282 object.
283  
284 =cut
285  
286 5 ▶sub popstar {
Subroutine popstar: called CC=2 SCAR=6.9
Pod documented
287 5 my $self = shift;
288  
289 5 my $star = pop(@{$self->stars});
290 5 ▶ if (defined $star->id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $star->id) 4 1 5
291 4 $self->{IDS}->{$star->id} --;
292 }
293  
294 # pop the star out of the stack
295 5 return $star;
296 }
297  
298 =item B<popstarbyid>
299  
300 Return C<Astro::Catalog::Item> objects that have the given ID. This forces
301 a copy of the array if one has not already been made (ie the original
302 version is unchanged).
303  
304 @stars = $catalog->popstarbyid( $id );
305  
306 The method deletes the stars and returns the deleted C<Astro::Catalog::Item>
307 objects. If no star exists with the given ID, the method returns an empty list.
308  
309 If called in scalar context this method returns an array reference, and if
310 called in list context returns an array of C<Astro::Catalog::Item> objects.
311  
312 This is effectively an inverse filter (see C<filter_by_id> for complementary
313 method).
314  
315 =cut
316  
317 3 ▶sub popstarbyid {
Subroutine popstarbyid: called CC=11 SCAR=25.3
Pod documented
318 3 my $self = shift;
319  
320 # Return undef if they didn't pass an ID.
321 3 ▶ return () unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
322  
323 3 my $id = shift;
324  
325 # Return if we know that that star doesn't exist.
326 3 ▶ return () unless defined $self->{IDS};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"IDS"} 0 3 3
327 3 ▶ return () unless defined $self->{IDS}->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"IDS"}{$id} 0 3 3
328 3 ▶ return () unless $self->{IDS}->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"IDS"}{$id} 0 3 3
329  
330 3 my @matched;
331 my @unmatched;
332 3 my $matched;
333 3 my @stars = $self->stars;
334 3 ▶ while (@stars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stars) 218 0 218
335 218 my $item = pop @stars;
336 218 ▶ if (defined($item) && defined($item->id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item and defined $item->id) { } 218 0 218
Condition33%1/3
defined $item and defined $item->id
!ll&&!rl&&r
00218
MC/DC0%0/2
defined $item and defined $item->id
defined $item defined $item->id
Truth table33%1/3
A and B
A= defined $itemB= defined $item->id
ABresult
0X0
100
111
337 218 ▶ if ($item->id eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->id eq $id) { } 3 215 218
338 3 push @matched, $item;
339 3 $self->{IDS}->{$id} --;
340 3 ▶ last if (0 == $self->{IDS}->{$id});
Branch: 1 branch
BranchTrueFalseTotal
if 0 == $self->{"IDS"}{$id} 3 0 3
341 }
342 else {
343 215 push @unmatched, $item;
344 }
345 }
346 else {
347 0 push @unmatched, $item;
348 }
349 }
350  
351 3 push @unmatched, @stars;
352 3 @{$self->stars} = @unmatched;
353  
354 3 ▶ return (wantarray ? @matched : \@matched);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
355 }
356  
357 =item B<allstars>
358  
359 Return all the stars in the catalog in their original ordering and without
360 filtering.
361  
362 @allstars = $catalog->allstars();
363 $ref = $catalog->allstars();
364  
365 In list context returns all the stars, in scalar context returns a reference
366 to the internal array. This allows the primary array to be modified in place
367 so use this with care.
368  
369 Addendum: This is pretty much for internal use only, but if you do this
370  
371 $catalog->allstars(@stars);
372  
373 you repalce the stars array with the array passed. Don't do this, it's bad!
374  
375 =cut
376  
377 160 ▶sub allstars {
Subroutine allstars: called CC=3 SCAR=11.8
Pod documented
378 160 my $self = shift;
379  
380 160 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 160 160
381 0 @{$self->{ALLSTARS}} = @_;
382 }
383  
384 160 ▶ return (wantarray ? @{$self->{ALLSTARS}} : $self->{ALLSTARS});
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 38 122 160
385 }
386  
387 =item B<stars>
388  
389 Return a list of all the C<Astro::Catalog::Item> objects that are currently
390 valid and in the current order. This method may well return different
391 stars to the C<allstars> method depending on the current sort in scope.
392  
393 @stars = $catalog->stars();
394  
395 in list context the copy of the array is returned, while in scalar
396 context a reference to the array is return. In scalar context, the
397 referenced array will always be that of the current list of valid
398 stars. If the current list is empty the primary list will be copied
399 into the current array so that it can be modified independently of the
400 original list. This may cost you a lot of memory. Note that changes to
401 the array ordering or content may be lost in this case whenever the
402 C<reset_list> method is used.
403  
404 =cut
405  
406 70 ▶sub stars {
Subroutine stars: called CC=4 SCAR=13.9
Pod documented
407 70 my $self = shift;
408  
409 # If we have a defined CURRENT array we just do whatever is needed
410 10 return (wantarray ? @{ $self->{CURRENT} } : $self->{CURRENT})
411 70 ▶ if $self->_have_copy;
Branch: 2 branches
BranchTrueFalseTotal
if $self->_have_copy 34 36 70
wantarray ? : 10 24 34
412  
413 # If we are in list context we do not want to force a copy if
414 # we have never copied. Just return the original list.
415 # By this point we know that CURRENT is not defined
416 36 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 15 21 36
417 15 return $self->allstars;
418 }
419 else {
420 # scalar context so we are forced to copy the array from allstars
421 21 @{ $self->{CURRENT} } = $self->allstars;
422 21 return $self->{CURRENT};
423 }
424 }
425  
426 =item B<starbyindex>
427  
428 Return the C<Astro::Catalog::Item> object at index $index
429  
430 $star = $catalog->starbyindex($index);
431  
432 the first star is at index 0 (not 1). Returns undef if no arguments
433 are provided.
434  
435 =cut
436  
437 12 ▶sub starbyindex {
Subroutine starbyindex: called CC=2 SCAR=7.0
Pod documented
438 12 my $self = shift;
439  
440 # return unless we have arguments
441 12 ▶ return () unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 12 12
442  
443 12 my $index = shift;
444  
445 12 return $self->stars->[$index];
446 }
447  
448 =item B<fieldcentre>
449  
450 Set the field centre and radius of the catalog (if appropriate)
451  
452 $catalog->fieldcentre(
453 RA => $ra,
454 Dec => $dec,
455 Radius => $radius,
456 Coords => new Astro::Coords());
457  
458 RA and Dec must be given together or as Coords.
459 Coords (an Astro::Coords object) supercedes RA/Dec.
460  
461 =cut
462  
463 41 ▶sub fieldcentre {
Subroutine fieldcentre: called CC=6 SCAR=17.9
Pod documented
464 41 my $self = shift;
465  
466 # return unless we have arguments
467 41 ▶ return () unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 28 13 41
468  
469 # grab the argument list and normalize hash
470 13 my %args = _normalize_hash( @_ );
471  
472 13 ▶ if (defined $args{coords}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'coords'}) { } 1 12 13
elsif (defined $args{'ra'} and defined $args{'dec'}) { } 5 14 19
473 1 $self->set_coords($args{coords});
474 }
475 ▶ elsif (defined $args{ra} && defined $args{dec}) {
Condition66%2/3
defined $args{"ra"} and defined $args{"dec"}
!ll&&!rl&&r
705
MC/DC50%1/2
defined $args{"ra"} and defined $args{"dec"}
defined $args{"ra"} defined $args{"dec"}
Truth table66%2/3
A and B
A= defined $args{"ra"}B= defined $args{"dec"}
ABresult
0X0
100
111
476 my $c = new Astro::Coords(
477 type => 'J2000',
478 ra => $args{ra},
479 dec => $args{dec},
480 5 );
481 5 $self->set_coords($c);
482 }
483  
484 # set field radius
485 13 ▶ if (defined $args{radius}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"radius"}) 5 8 13
486 5 $self->set_radius($args{radius});
487 }
488 }
489  
490 =item B<set_radius>
491  
492 Set the field centre radius. Must be in arcminutes.
493  
494 $catalog->set_radius($radius);
495  
496 =cut
497  
498 5 ▶sub set_radius {
Subroutine set_radius: called CC=1 SCAR=0.0
Pod documented
499 5 my $self = shift;
500 5 my $r = shift;
501 5 $self->{RADIUS} = $r;
502 5 return;
503 }
504  
505 =item B<set_coords>
506  
507 Set the field centre coordinates with an C<Astro::Coords> object.
508  
509 $catalog->set_coords($c);
510  
511 =cut
512  
513 6 ▶sub set_coords {
Subroutine set_coords: called CC=2 SCAR=7.0
Pod documented
514 6 my $self = shift;
515 6 my $c = shift;
516 6 ▶ croak "Coords must be an Astro::Coords"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($c, "Astro::Coords") 0 6 6
517 unless UNIVERSAL::isa($c, "Astro::Coords");
518 6 $self->{COORDS} = $c;
519 }
520  
521 =item B<get_coords>
522  
523 Return the C<Astro::Coords> object associated with the field centre.
524  
525 $c = $catalog->get_coords();
526  
527 =cut
528  
529 8 ▶sub get_coords {
Subroutine get_coords: called CC=1 SCAR=0.0
Pod documented
530 8 my $self = shift;
531 8 return $self->{COORDS};
532 }
533  
534 =item B<get_ra>
535  
536 Return the RA of the catalog field centre in sexagesimal,
537 space-separated format. Returns undef if no coordinate supplied.
538  
539 $ra = $catalog->get_ra();
540  
541 =cut
542  
543 3 ▶sub get_ra {
Subroutine get_ra: called CC=3 SCAR=11.9
Pod documented
544 3 my $self = shift;
545 3 my $c = $self->get_coords;
546 3 ▶ return unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 1 2 3
547 2 my $ra = $c->ra;
548 2 ▶ if (UNIVERSAL::isa($ra, "Astro::Coords::Angle")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($ra, 'Astro::Coords::Angle')) { } 2 0 2
549 2 $ra->str_delim(' ');
550 2 $ra->str_ndp(2);
551 2 return "$ra";
552 }
553 else {
554 0 $ra = $c->ra(format => 's');
555 0 $ra =~ s/:/ /g;
556 0 $ra =~ s/^\s*//;
557 0 return $ra;
558 }
559 }
560  
561 =item B<get_dec>
562  
563 Return the Dec of the catalog field centre in sexagesimal
564 space-separated format with leading sign.
565  
566 $dec = $catalog->get_dec();
567  
568 =cut
569  
570 2 ▶sub get_dec {
Subroutine get_dec: called CC=5 SCAR=19.5
Pod documented
571 2 my $self = shift;
572 2 my $c = $self->get_coords;
573 2 ▶ return unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 2 2
574 2 my $dec = $c->dec;
575 2 ▶ if (UNIVERSAL::isa($dec, "Astro::Catalog::Angle")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($dec, 'Astro::Catalog::Angle')) { } 0 2 2
576 0 $dec->str_delim(' ');
577 0 $dec->str_ndp(2);
578 0 $dec = "$dec";
579 0 ▶ $dec = (substr($dec, 0, 1) eq '-' ? '' : '+') . $dec;
Branch: 1 branch
BranchTrueFalseTotal
substr($dec, 0, 1) eq '-' ? : 0 0 0
580 0 return $dec;
581 }
582 else {
583 2 $dec = $c->dec(format => 's');
584 2 $dec =~ s/:/ /g;
585 2 $dec =~ s/^\s*//;
586 # prepend sign if there is no sign
587 2 ▶ $dec = (substr($dec, 0, 1) eq '-' ? '' : '+') . $dec;
Branch: 1 branch
BranchTrueFalseTotal
substr($dec, 0, 1) eq '-' ? : 0 2 2
588 2 return $dec;
589 }
590 }
591  
592 =item B<get_radius>
593  
594 Return the radius of the catalog from the field centre
595  
596 $radius = $catalog->get_radius();
597  
598 =cut
599  
600 3 ▶sub get_radius {
Subroutine get_radius: called CC=1 SCAR=0.0
Pod documented
601 3 my $self = shift;
602 3 return $self->{RADIUS};
603 }
604  
605 =item B<reference>
606  
607 If set this must contain an C<Astro::Coords> object that can be
608 used as a reference position. When a reference is supplied
609 distances will be calculated from each catalog target to the
610 reference. It will also be possible to sort by distance.
611  
612 $ref = $catalog->reference;
613 $catalog->reference($c);
614  
615 If a reference position is not specified explicitly the field
616 centre will be used instead (if defined).
617  
618 =cut
619  
620 0 ▶sub reference {
Subroutine reference: not called CC=5 SCAR=34.0
Pod documented
621 0 my $self = shift;
622 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
623 0 my $val = shift;
624 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
625 0 ▶ if (UNIVERSAL::isa($val, "Astro::Coords")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'Astro::Coords')) { } 0 0 0
626 0 $self->{REFPOS} = $val;
627 }
628 else {
629 0 croak "Must supply reference as a Astro::Coords object";
630 }
631 }
632 else {
633 0 $self->{REFPOS} = undef;
634 }
635 }
636  
637 # default to field centre
638 0 ▶ return (defined $self->{REFPOS} ? $self->{REFPOS} : $self->get_coords);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'REFPOS'} ? : 0 0 0
639 }
640  
641 =item B<reftime>
642  
643 The reference time used for coordinate calculations. Extracted
644 from the reference coordinate object if one exists and no override
645 has been specified. If neither a default setting has been made
646 and no reference exists the current time is returned.
647  
648 $reftime = $src->reftime();
649  
650 $src->reftime($newtime);
651  
652 Time must be a C<Time::Piece> object. This is only really important
653 for moving objects such as planets or asteroids or for occasions when
654 you are calcualting azimuth or elevation.
655  
656 =cut
657  
658 0 ▶sub reftime {
Subroutine reftime: not called CC=6 SCAR=37.4
Pod documented
659 0 my $self = shift;
660 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
661 0 my $val = shift;
662 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
663 0 ▶ if (UNIVERSAL::isa($val, "Time::Piece")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'Time::Piece')) { } 0 0 0
664 0 $self->{REFTIME} = $val;
665 }
666 else {
667 0 croak "Must supply start time with a Time::Piece object";
668 }
669 }
670 else {
671 0 $self->{REFTIME} = undef;
672 }
673 }
674  
675 # if we have no default ask for a coordinate object
676 0 my $retval = $self->{REFTIME};
677  
678 0 ▶ unless ($retval) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($retval) 0 0 0
679 0 my $ref = $self->reference;
680 0 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 0 0
681 # retrieve it from the coordinate object
682 0 $retval = $ref->datetime;
683 }
684 else {
685 # else we just say "now"
686 0 $retval = gmtime();
687 }
688 }
689 0 return $retval;
690 }
691  
692 =item B<fielddate>
693  
694 The observation date/time of the field.
695  
696 $fielddate = $src->fielddate;
697  
698 $src->fielddate($date);
699  
700 Date must be a C<Time::Piece> object. This defaults to the current
701 time when the C<Astro::Catalog> object was instantiated.
702  
703 =cut
704  
705 58 ▶sub fielddate {
Subroutine fielddate: called CC=4 SCAR=14.2
Pod documented
706 58 my $self = shift;
707  
708 58 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 25 33 58
709 25 my $val = shift;
710 25 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 25 0 25
711 25 ▶ if (UNIVERSAL::isa($val, "Time::Piece")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'Time::Piece')) { } 25 0 25
712 25 $self->{FIELDDATE} = $val;
713 }
714 else {
715 0 croak "Must supply field date as a Time::Piece object";
716 }
717 }
718 }
719  
720 58 return $self->{FIELDDATE};
721 }
722  
723 =item B<auto_filter_observability>
724  
725 If this flag is true, a reset_list will automatically remove targets
726 that are not observable (as determined by C<filter_by_observability>
727 which will be invoked).
728  
729 Default is false.
730  
731 =cut
732  
733 5 ▶sub auto_filter_observability {
Subroutine auto_filter_observability: called CC=2 SCAR=7.6
Pod documented
734 5 my $self = shift;
735 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5 5
736 0 $self->{AUTO_OBSERVE} = shift;
737 }
738 5 return $self->{AUTO_OBSERVE};
739 }
740  
741 =item B<misc>
742  
743 Method to contain information not handled by other methods.
744 This is analogous to the Astro::Catalog::Item::misc method,
745 and should also typically be used to store a hash reference.
746  
747 =cut
748  
749 0 ▶sub misc {
Subroutine misc: not called CC=2 SCAR=17.9
Pod documented
750 0 my $self = shift;
751 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
752 0 $self->{'MISC'} = shift;
753 }
754 0 return $self->{'MISC'};
755 }
756  
757 =back
758  
759 =head2 General Methods
760  
761 =over 4
762  
763 =item B<configure>
764  
765 Configures the object from multiple pieces of information.
766  
767 $newcat = $catalog->configure(%options);
768  
769 Takes a hash as argument with the list of keywords. Supported options
770 are:
771  
772 Format => Format of supplied catalog
773 File => File name for catalog on disk. Not used if 'Data' supplied.
774 Data => Contents of catalog, either as a scalar variable,
775 reference to array of lines or reference to glob (file handle).
776 This key is used in preference to 'File' if both are present
777  
778 Stars => Array of Astro::Catalog::Item objects. Supercedes all other options.
779 ReadOpt => Reference to hash of options to be forwarded onto the
780 format specific catalog reader. See the IO documentation
781 for details.
782  
783 If Format is supplied without any other options, a default file is requested
784 from the class implementing the formatted read. If no default file is
785 forthcoming the method croaks.
786  
787 If no options are specified the method does nothing, assumes you will
788 be supplying stars at a later time.
789  
790 The options are case-insensitive.
791  
792 Note that in some cases (when reading a catalog) this method will
793 act as a constructor. In any case, always returns a catalog object
794 (either the same one that went in or a modified one).
795  
796 API uncertainty - in principal Data is not needed since File
797 could be overloaded (in a similar way to write_catalog).
798  
799 =cut
800  
801 34 ▶sub configure {
Subroutine configure: called CC=11 SCAR=24.4
Pod documented
802 34 my $self = shift;
803  
804 # return unless we have arguments
805 34 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 34 34
806  
807 # grab the argument list
808 34 my %args = @_;
809  
810 # Go through hash and downcase all keys
811 34 %args = _normalize_hash(%args);
812  
813 # Check for deprecation
814 34 ▶ if (exists $args{cluster}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"cluster"}) 0 34 34
815 0 warnings::warnif("deprecated",
816 "Cluster option now deprecated. Use Format => 'Cluster', File => file instead");
817 0 $args{file} = $args{cluster};
818 0 $args{format} = 'Cluster';
819 }
820  
821 # Define the actual catalog
822  
823 # Stars has priority
824 34 ▶ if (defined $args{stars}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'stars'}) { } 10 24 34
elsif (defined $args{'format'}) { } 23 1 24
825 # grab the array reference and stuff it into the object
826 10 $self->pushstar( @{ $args{stars} } );
827  
828 # Make sure we do not loop over this later
829 10 delete( $args{stars} );
830  
831 }
832 elsif (defined $args{format}) {
833 # Need to read the IO class
834 23 my $ioclass = _load_io_plugin($args{format});
835 23 ▶ return unless defined $ioclass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ioclass 0 23 23
836  
837 # Now read the catalog (overwriting $self)
838 23 ▶ print "# READING CATALOG $ioclass \n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::DEBUG 0 23 23
839 $self = $ioclass->read_catalog(
840 File => $args{file},
841 Data => $args{data},
842 23 ReadOpt => $args{readopt});
843  
844 22 ▶ croak "Error reading catalog of class $ioclass\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 22 22
845 unless defined $self;
846  
847 # Remove used args
848 22 delete $args{format};
849 22 delete $args{file};
850 22 delete $args{data};
851 22 delete $args{readopt};
852 }
853  
854 # Define the field centre if provided
855 33 $self->fieldcentre(%args);
856  
857 # Remove field centre args
858 33 delete $args{ra};
859 33 delete $args{dec};
860 33 delete $args{coords};
861  
862 # Loop over any remaining args
863 33 for my $key (keys %args) {
864 8 my $method = lc($key);
865 8 ▶ $self->$method($args{$key}) if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 7 1 8
866 }
867  
868 33 ▶ unless (defined $self->fielddate) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->fielddate) 25 8 33
869 25 my $date = gmtime;
870 25 $self->fielddate($date);
871 }
872  
873 33 return $self;
874 }
875  
876 =item B<reset_list>
877  
878 Forces the star list to return to the original unsorted, unfiltered catalog
879 list.
880  
881 $catalog->reset_list();
882  
883 If C<auto_filter_observability> is true, the list will be immediately
884 filtered for observability.
885  
886 =cut
887  
888 5 ▶sub reset_list {
Subroutine reset_list: called CC=2 SCAR=7.0
Pod documented
889 5 my $self = shift;
890  
891 # Simply need to clear the CURRENT
892 5 $self->{CURRENT} = undef;
893  
894 # and filter automatically if required
895 5 ▶ $self->filter_by_observability
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_filter_observability 0 5 5
896 if $self->auto_filter_observability;
897  
898 5 return;
899 }
900  
901 =item B<force_ref_time>
902  
903 Force the specified reference time into the coordinate object
904 associated with each star (in the current list). This ensures that
905 calculations on the catalog entries are all calculated for the same
906 time.
907  
908 $catalog->force_ref_time();
909  
910 After this, the times in the coordinate objects will be set and will
911 no longer reflect current time (if they had it originally).
912  
913 =cut
914  
915 0 ▶sub force_ref_time {
Subroutine force_ref_time: not called CC=3 SCAR=24.8
Pod documented
916 0 my $self = shift;
917 0 my $reftime = $self->reftime;
918 0 for my $star (@{$self->stars}) {
919 0 my $c = $star->coords;
920 0 ▶ next unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
921  
922 # Force the time (since we can not tell if the ref time is the
923 # current time then we can not know whether we need to override
924 # the coords objects or not
925 0 $c->datetime($reftime);
926 }
927 }
928  
929 =item B<calc_xy>
930  
931 Calculate the X and Y positions for every item in the catalog, if they
932 have an RA and Dec.
933  
934 $catalog->calc_xy($frameset);
935  
936 The supplied argument must be a Starlink::AST::FrameSet.
937  
938 =cut
939  
940 0 ▶sub calc_xy {
Subroutine calc_xy: not called CC=4 SCAR=30.0
Pod documented
941 0 my $self = shift;
942 0 my $frameset = shift;
943  
944 0 ▶ unless (UNIVERSAL::isa($frameset, "Starlink::AST::FrameSet")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($frameset, "Starlink::AST::FrameSet")) 0 0 0
945 0 croak "Argument to calc_xy() must be a Starlink::AST::FrameSet object";
946 }
947  
948 # Loop through the items, obtaining the RA and Dec in radians for
949 # each item.
950 0 my @ras;
951 my @decs;
952 0 foreach my $item ($self->stars) {
953 0 my ($ra, $dec) = $item->coords->radec();
954 0 push @ras, $ra->radians;
955 0 push @decs, $dec->radians;
956 }
957  
958 # Do the calculations;
959 0 my ($xref, $yref) = $frameset->Tran2(\@ras, \@decs, 0);
960  
961 # Loop through the items, pushing in the X and Y values.
962 0 my $i = 0;
963 0 foreach my $item ($self->stars) {
964 0 $item->x($xref->[$i]);
965 0 $item->y($yref->[$i]);
966 0 $i++;
967 }
968 }
969  
970 =back
971  
972 =head2 Filters
973  
974 All these filters work on a copy of the full star list. The filters are
975 cumulative.
976  
977 =over 4
978  
979 =item B<filter_by_observability>
980  
981 Generate a filtered catalog where only those targets that are
982 observable are present (assumes that the current state of the
983 coordinate objects is correct but will use the reference time returned
984 by C<reftime>). ie the object is returned to its original state and
985 then immediately filtered by observability. Any stars without
986 coordinates are also filtered. Starts from the current star list
987 (which may already have been filtered).
988  
989 @new = $catalog->filter_by_observability();
990  
991 Returns the newly selected stars (as if the C<stars> method was called
992 immediately, unless called in a non-list context.
993  
994 =cut
995  
996 0 ▶sub filter_by_observability {
Subroutine filter_by_observability: not called CC=2 SCAR=17.9
Pod documented
997 0 my $self = shift;
998  
999 0 $self->force_ref_time;
1000 0 my $ref = $self->stars;
1001  
1002 # For each star, extract the coordinate object and, if defined
1003 # check for observability
1004 0 @$ref = grep {$_->coords->isObservable} grep {$_->coords;} @$ref;
1005 0 ▶ return $self->stars if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
1006 }
1007  
1008 =item B<filter_by_id>
1009  
1010 Given a source name filter the source list such that the
1011 supplied ID is a substring of the star ID (case insensitive).
1012  
1013 @stars = $catalog->filter_by_id("IRAS");
1014  
1015 Would result in a catalog with all the stars with "IRAS"
1016 in their name. This is just a convenient alternative to C<filter_by_cb>
1017 and is equivalent to
1018  
1019 @stars = $catalog->filter_by_cb(sub {$_[0]->id =~ /IRAS/i;});
1020  
1021 A regular expression can be supplied explicitly using qr//:
1022  
1023 @stars = $catalog->filter_by_id(qr/^IRAS/i);
1024  
1025 See C<popstarbyid> for a similar method that returns stars
1026 that are an exact match to ID and removes them from the current
1027 list.
1028  
1029 =cut
1030  
1031 3 ▶sub filter_by_id {
Subroutine filter_by_id: called CC=2 SCAR=6.9
Pod documented
1032 3 my $self = shift;
1033 3 my $id = shift;
1034  
1035 # Convert to regex if required
1036 3 ▶ unless (ref $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $id) 2 1 3
1037 2 $id = quotemeta($id);
1038 2 $id = qr/$id/i;
1039 }
1040  
1041 3 ▶ return $self->filter_by_cb(sub {$_[0]->id =~ $id;});
Subroutine __ANON__: called CC=1 SCAR=0.0
1042 }
1043  
1044 =item B<filter_by_distance>
1045  
1046 Retrieve all targets that are within the specified distance of the
1047 reference position.
1048  
1049 @selected = $catalog->filter_by_distance( $radius, $refpos );
1050  
1051 The radius is in radians. The reference position defaults to
1052 the value returned by the C<reference> method if none supplied.
1053  
1054 API uncertainty:
1055  
1056 - Should the radius default to the get_radius() method?
1057 - Should this method take hash arguments?
1058 - Should there be a units argument? (radians, arcmin, arcsec, degrees)
1059  
1060 =cut
1061  
1062 1 ▶sub filter_by_distance {
Subroutine filter_by_distance: called CC=5 SCAR=16.4
Pod documented
1063 1 my $self = shift;
1064 1 ▶ croak "Must be at least one argument"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
1065 unless scalar(@_) > 0;
1066  
1067 # Read the arguments
1068 1 my $radius = shift;
1069 1 my $refpos = shift;
1070 1 ▶ $refpos = $self->reference unless defined $refpos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $refpos 0 1 1
1071  
1072 1 ▶ croak "Reference position not defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $refpos 0 1 1
1073 unless defined $refpos;
1074  
1075 1 ▶ croak "Reference must be an Astro::Coords object"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($refpos, "Astro::Coords") 0 1 1
1076 unless UNIVERSAL::isa($refpos, "Astro::Coords");
1077  
1078 # Calculate distance and throw away outliers
1079 381 ▶ return $self->filter_by_cb(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1080 381 my $star = shift;
1081 381 my $c = $star->coords;
1082 381 ▶ return if not defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 381 381
1083 381 my $dist = $refpos->distance($c);
1084 381 ▶ return if not defined $dist;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dist 132 249 381
1085 249 return $dist < $radius;
1086 1 });
1087 }
1088  
1089 =item B<filter_by_cb>
1090  
1091 Filter the star list using the given the supplied callback (reference
1092 to a subroutine). The callback should expect a star object and should
1093 return a boolean.
1094  
1095 @selected = $catalog->filter_by_cb(sub {$_[0]->id == "HLTau"});
1096 @selected = $catalog->filter_by_cb(sub {$_[0]->id =~ /^IRAS/;});
1097  
1098 =cut
1099  
1100 5 ▶sub filter_by_cb {
Subroutine filter_by_cb: called CC=2 SCAR=7.0
Pod documented
1101 5 my $self = shift;
1102 5 my $cb = shift;
1103  
1104 5 ▶ croak "Callback has to be a reference to a subroutine"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 0 5 5
1105 unless ref($cb) eq "CODE";
1106  
1107 # Get reference to array (force copy)
1108 5 my $ref = $self->stars;
1109  
1110 5 @$ref = grep {$cb->($_);} @$ref;
1111 5 return $self->stars;
1112 }
1113  
1114 =back
1115  
1116 =head2 Sorting
1117  
1118 The following routines are available for sorting the star catalog.
1119 The sort applies to the current source list and not the original source list.
1120 This is the case even if no filters have been applied (ie the original
1121 unsorted catalog is always available).
1122  
1123 =over 4
1124  
1125 =item B<sort_catalog>
1126  
1127 Sort the catalog.
1128  
1129 $catalog->sort_catalog($mode);
1130  
1131 where mode can be one of
1132  
1133 "unsorted"
1134 "id"
1135 "ra"
1136 "dec"
1137 "az"
1138 "el"
1139  
1140 and
1141  
1142 "distance"
1143 "distance_az"
1144  
1145 if a reference position is available. "az" and "el" require that the
1146 star coordinates have an associated telescope and that the reference
1147 time is correct.
1148  
1149 If mode is a code reference, that will be passed to the sort
1150 routine directly. Note that the callback must expect $a and
1151 $b to be set.
1152  
1153 The method C<force_ref_time> is invoked prior to sorting
1154 unless the mode is "id". "name" is a synonym for "id".
1155  
1156 Currently the C<unsorted> option simply forces a C<reset_list>
1157 since there is currently no tracking of the applied filters.
1158 It should be possible to step through the original list and
1159 the current filtered list and end up with a filtered but
1160 unsorted list. This is not implemented.
1161  
1162 Pre-canned sorts are optimized because the values are precalculated
1163 prior to doing the sort rather than calculated each time through
1164 the sort.
1165  
1166 =cut
1167  
1168 0 ▶sub sort_catalog {
Subroutine sort_catalog: not called CC=25 SCAR=64.8
Pod documented
1169 0 my $self = shift;
1170 0 my $mode = shift;
1171  
1172 # unsort is a kluge at the moment
1173 0 ▶ if ($mode =~ /^unsort/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /^unsort/i) 0 0 0
1174 0 $self->reset_list;
1175 0 return;
1176 }
1177  
1178 # For reference time unless we are in id/name mode
1179 $self->force_ref_time
1180 0 ▶ unless ($mode =~ /^(id|name)/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ /^(id|name)/i 0 0 0
1181  
1182 # Get the star list
1183 0 my $stars = $self->stars;
1184  
1185 # If we have a code ref we cannot optimize so just do it
1186 0 ▶ if (ref $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mode) { } 0 0 0
1187 # Just sort it all
1188 0 @$stars = sort $mode, @$stars;
1189 }
1190 else {
1191 # see if we have a reference object
1192 0 my $ref = $self->reference;
1193  
1194 # down case
1195 0 my $sort = lc($mode);
1196  
1197 # to try to speed up all the queries, rather than
1198 # calculating the dynamic values during the sort we should
1199 # do it outside the sort. Create an array of hashes for the
1200 # sorting
1201 my @unsorted = map {
1202 0 my $c = $_->coords;
1203 0 ▶ return () unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
1204 0 my %calc = (
1205 object => $_,
1206 );
1207 0 ▶ $calc{ra} = $c->ra_app if $sort eq 'ra';
Branch: 1 branch
BranchTrueFalseTotal
if $sort eq "ra" 0 0 0
1208 0 ▶ $calc{dec} = $c->dec_app if $sort eq 'dec';
Branch: 1 branch
BranchTrueFalseTotal
if $sort eq "dec" 0 0 0
1209 0 ▶ $calc{az} = $c->az if $sort eq 'az';
Branch: 1 branch
BranchTrueFalseTotal
if $sort eq "az" 0 0 0
1210 0 ▶ $calc{el} = $c->el if $sort eq 'el';
Branch: 1 branch
BranchTrueFalseTotal
if $sort eq "el" 0 0 0
1211 0 ▶ $calc{id} = $_->id if ( $sort eq 'id' || $sort eq 'name' );
Branch: 1 branch
BranchTrueFalseTotal
if $sort eq "id" or $sort eq "name" 0 0 0
Condition0%0/3
$sort eq "id" or $sort eq "name"
l!l&&r!l&&!r
000
MC/DC0%0/2
$sort eq "id" or $sort eq "name"
$sort eq "id" $sort eq "name"
Truth table0%0/3
A or B
A= $sort eq "id"B= $sort eq "name"
ABresult
1X1
011
000
1212  
1213 0 ▶ if ($ref && $sort eq 'distance') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and $sort eq "distance") 0 0 0
Condition0%0/3
$ref and $sort eq "distance"
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and $sort eq "distance"
$ref $sort eq "distance"
Truth table0%0/3
A and B
A= $refB= $sort eq "distance"
ABresult
0X0
100
111
1214 0 $calc{distance} = $ref->distance( $c );
1215 0 ▶ $calc{distance} = "Inf" unless defined $calc{distance};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $calc{"distance"} 0 0 0
1216 }
1217 0 ▶ if ($ref && $sort eq 'distance_az') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and $sort eq "distance_az") 0 0 0
Condition0%0/3
$ref and $sort eq "distance_az"
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and $sort eq "distance_az"
$ref $sort eq "distance_az"
Truth table0%0/3
A and B
A= $refB= $sort eq "distance_az"
ABresult
0X0
100
111
1218 0 my $az = $c->az(format => 'deg');
1219 0 my $ref_az = $ref->az(format => 'deg');
1220 0 ▶ if (defined $az and defined $ref_az) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $az and defined $ref_az) { } 0 0 0
Condition0%0/3
defined $az and defined $ref_az
!ll&&!rl&&r
000
MC/DC0%0/2
defined $az and defined $ref_az
defined $az defined $ref_az
Truth table0%0/3
A and B
A= defined $azB= defined $ref_az
ABresult
0X0
100
111
1221 0 $calc{'distance'} = abs($az - $ref_az);
1222 }
1223 else {
1224 0 $calc{'distance'} = 'Inf';
1225 }
1226 }
1227 0 \%calc;
1228 } @$stars;
1229  
1230 # Array to hold the sorted hashes
1231 0 my @rSources;
1232  
1233 # Now do the sort
1234 0 ▶ if ($sort =~ /(name|id)/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($sort =~ /(name|id)/) { } 0 0 0
elsif ($sort =~ /ra/) { } 0 0 0
elsif ($sort =~ /dec/) { } 0 0 0
elsif ($sort =~ /az/ and not $sort =~ /dist/) { } 0 0 0
elsif ($sort =~ /el/) { } 0 0 0
elsif ($sort =~ /dist/) { } 0 0 0
1235 0 @rSources = sort by_id @unsorted;
1236 }
1237 elsif ($sort =~ /ra/) {
1238 0 @rSources = sort by_ra @unsorted;
1239 }
1240 elsif ($sort =~ /dec/) {
1241 0 @rSources = sort by_dec @unsorted;
1242 }
1243 ▶ elsif ($sort =~ /az/ and $sort !~ /dist/) {
Condition0%0/3
$sort =~ /az/ and not $sort =~ /dist/
!ll&&!rl&&r
000
MC/DC0%0/2
$sort =~ /az/ and not $sort =~ /dist/
$sort =~ /az/ not $sort =~ /dist/
Truth table0%0/3
A and not(B)
A= $sort =~ /az/B= not $sort =~ /dist/
ABresult
0X0
100
111
1244 # Avoid accidentally matching in distance_az
1245 # mode but why are these regexps anyway?
1246 0 @rSources = sort {$a->{az} <=> $b->{az}} @unsorted;
1247 }
1248 elsif ($sort =~ /el/) {
1249 # reverse sort
1250 0 @rSources = sort {$b->{el} <=> $a->{el}} @unsorted;
1251 }
1252 elsif ($sort =~ /dist/) {
1253 0 @rSources = sort by_dist @unsorted;
1254 }
1255 else {
1256 0 croak "Unknown sort type: $sort";
1257 }
1258  
1259 # extract the objects in the right order
1260 0 @$stars = map {$_->{object}} @rSources;
1261 }
1262 }
1263  
1264 =back
1265  
1266 =begin __PRIVATE_METHODS__
1267  
1268 =head3 Internal sort optimizers
1269  
1270 =over 4
1271  
1272 =item by_id
1273  
1274 Internal routine to sort the entries in a source catalog by ID.
1275  
1276 sort by_id @sources;
1277  
1278 Returns -1, 0, 1
1279  
1280 =cut
1281  
1282 0 ▶sub by_id {
Subroutine by_id: not called CC=5 SCAR=34.0
Pod documented
1283 0 my $b2 = $b->{id};
1284 0 my $a2 = $a->{id};
1285  
1286 # only compare if the ID is defined and has length
1287 0 ▶ if (defined $a2 && defined $b2 &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a2 and defined $b2 and length $a2 > 0 and length $b2 > 0) { } 0 0 0
Condition0%0/9
defined $a2 and defined $b2 and length $a2 > 0 and length $b2 > 0
!ll&&!rl&&r
000
defined $a2 and defined $b2 and length $a2 > 0
!ll&&!rl&&r
000
defined $a2 and defined $b2
!ll&&!rl&&r
000
MC/DC0%0/4
defined $a2 and defined $b2 and length $a2 > 0 and length $b2 > 0
defined $a2 defined $b2 length $a2 > 0 length $b2 > 0
Truth table0%0/5
A and B and C and D
A= defined $a2B= defined $b2C= length $a2 > 0D= length $b2 > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1288 length($a2) > 0 && length($b2) > 0) {
1289 0 $a2 = uc($a2);
1290 0 $b2 = uc($b2);
1291 }
1292 else {
1293 0 return -1;
1294 }
1295  
1296 0 ($a2 cmp $b2);
1297 }
1298  
1299 =item by_ra
1300  
1301 Internal routine to sort the entries in a source catalog by RA
1302 (actually sorts by apparent RA).
1303  
1304 sort by_ra @sources;
1305  
1306 Returns -1, 0, 1
1307  
1308 =cut
1309  
1310 0 ▶sub by_ra {
Subroutine by_ra: not called CC=1 SCAR=6.9
Pod documented
1311 0 return $a->{ra} <=> $b->{ra};
1312 }
1313  
1314 =item by_dec
1315  
1316 Internal routine to sort the entries in a source catalog by Dec.
1317 (actually uses apparent Dec)
1318  
1319 sort by_dec @sources;
1320  
1321 Returns -1, 0, 1
1322  
1323 =cut
1324  
1325 0 ▶sub by_dec {
Subroutine by_dec: not called CC=1 SCAR=6.9
Pod documented
1326 0 return $a->{dec} <=> $b->{dec};
1327 }
1328  
1329 =item by_dist
1330  
1331 Sorts by distance from a reference position.
1332  
1333 "Inf" is handled as being a long way off even though it is included
1334 in the search results.
1335  
1336 =cut
1337  
1338 0 ▶sub by_dist {
Subroutine by_dist: not called CC=5 SCAR=34.0
Pod documented
1339 0 my $a2 = $a->{distance};
1340 0 my $b2 = $b->{distance};
1341  
1342 # need to trap for Inf
1343 0 ▶ if ($a2 eq 'Inf' && $b2 eq 'Inf') {
Branch: 3 branches
BranchTrueFalseTotal
if ($a2 eq 'Inf' and $b2 eq 'Inf') { } 0 0 0
elsif ($a2 eq 'Inf') { } 0 0 0
elsif ($b2 eq 'Inf') { } 0 0 0
Condition0%0/3
$a2 eq "Inf" and $b2 eq "Inf"
!ll&&!rl&&r
000
MC/DC0%0/2
$a2 eq "Inf" and $b2 eq "Inf"
$a2 eq "Inf" $b2 eq "Inf"
Truth table0%0/3
A and B
A= $a2 eq "Inf"B= $b2 eq "Inf"
ABresult
0X0
100
111
1344 # they are the same
1345 0 return 0;
1346 }
1347 elsif ($a2 eq 'Inf') {
1348 # A is larger than B
1349 0 return 1;
1350 }
1351 elsif ($b2 eq 'Inf') {
1352 0 return -1;
1353 }
1354  
1355 0 $a2 <=> $b2;
1356 }
1357  
1358 =back
1359  
1360 =head2 Private methods
1361  
1362 These methods and functions are for internal use only.
1363  
1364 =over 4
1365  
1366 =item B<_have_copy>
1367  
1368 Internal method indicating whether we have a copy of the stars array
1369 or whether we are using the original version.
1370  
1371 $havecopy = $catalog->_have_copy;
1372  
1373 =cut
1374  
1375 190 ▶sub _have_copy {
Subroutine _have_copy: called CC=1 SCAR=0.0
1376 190 my $self = shift;
1377 190 return (defined $self->{CURRENT});
1378 }
1379  
1380 =item B<_normalize_hash>
1381  
1382 Given a hash, returns a new hash with each key down cased. If a
1383 key is duplicated after downcasing a warning is issued if the keys
1384 contain differing values.
1385  
1386 %n = _normalize_hash(%args);
1387  
1388 =cut
1389  
1390 98 ▶sub _normalize_hash {
Subroutine _normalize_hash: called CC=4 SCAR=15.5
1391 98 my %args = @_;
1392  
1393 98 my %out;
1394  
1395 98 for my $key (keys %args) {
1396 210 my $outkey = lc($key);
1397 210 ▶ if (exists $out{$outkey} && $out{$outkey} ne $args{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $out{$outkey} and $out{$outkey} ne $args{$key}) 0 210 210
Condition33%1/3
exists $out{$outkey} and $out{$outkey} ne $args{$key}
!ll&&!rl&&r
21000
MC/DC0%0/2
exists $out{$outkey} and $out{$outkey} ne $args{$key}
exists $out{$outkey} $out{$outkey} ne $args{$key}
Truth table33%1/3
A and B
A= exists $out{$outkey}B= $out{$outkey} ne $args{$key}
ABresult
0X0
100
111
1398 0 warnings::warnif("Key '$outkey' supplied more than once with differing values. Ignoring second version");
1399 0 next;
1400 }
1401  
1402 # Store the key in the new hash
1403 210 $out{$outkey} = $args{$key};
1404 }
1405  
1406 98 return %out;
1407 }
1408  
1409 =item B<_load_io_plugin>
1410  
1411 Given a file format, load the corresponding IO class. In general the
1412 IO class is lower case except for the first letter. JCMT and VOTable
1413 are the exception. All plugins are in hierarchy C<Astro::Catalog::IO>.
1414  
1415 Returns the class name on successful load. If the class can not be found
1416 a warning is issued and false is returned.
1417  
1418 =cut
1419  
1420 34 ▶sub _load_io_plugin {
Subroutine _load_io_plugin: called CC=17 SCAR=28.9
1421 34 my $format = shift;
1422  
1423 # Force case
1424 34 $format = ucfirst(lc($format));
1425  
1426 # Horrible kluge since I prefer "JCMT" to "Jcmt".
1427 # Maybe we should not try to fudge case at all?
1428 # Getting out of hand - maybe we should special case Cluster
1429 # and assume uppercase elsewhere.
1430 34 ▶ $format = 'JCMT' if $format eq 'Jcmt';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Jcmt" 6 28 34
1431 34 ▶ $format = 'JCMT_OT_SC' if $format eq 'Jcmt_ot_sc';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Jcmt_ot_sc" 0 34 34
1432 34 ▶ $format = 'TST' if $format eq 'Tst';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Tst" 3 31 34
1433 34 ▶ $format = 'VOTable' if $format eq 'Votable';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Votable" 0 34 34
1434 34 ▶ $format = 'STL' if $format eq 'Stl';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Stl" 1 33 34
1435 34 ▶ $format = 'GaiaPick' if $format eq 'Gaiapick';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Gaiapick" 1 33 34
1436 34 ▶ $format = 'UKIRTBS' if $format eq 'Ukirtbs';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Ukirtbs" 1 33 34
1437 34 ▶ $format = 'SExtractor' if $format eq 'Sextractor';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Sextractor" 3 31 34
1438 34 ▶ $format = 'FINDOFF' if $format eq 'Findoff';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Findoff" 3 31 34
1439 34 ▶ $format = 'FITSTable' if $format eq 'Fitstable';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Fitstable" 0 34 34
1440 34 ▶ $format = 'LCOGTFITSTable' if $format eq 'Lcogtfitstable';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Lcogtfitstable" 0 34 34
1441 34 ▶ $format = 'RITMatch' if $format eq 'Ritmatch';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Ritmatch" 3 31 34
1442 34 ▶ $format = 'VEX' if $format eq 'Vex';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Vex" 0 34 34
1443 34 ▶ $format = 'XY' if $format eq 'Xy';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Xy" 3 31 34
1444 34 ▶ $format = 'ASSM' if $format eq 'Assm';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "Assm" 0 34 34
1445  
1446 34 my $class = "Astro::Catalog::IO::" . $format;
1447  
1448 # For some reason eval require does not work for us. Use string eval
1449 # instead.
1450 34 ▶ eval "use $class;";
Subroutine BEGIN: called CC=1 SCAR=0.0
1451 34 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 34 34
1452 0 warnings::warnif("Error reading IO plugin $class: $@");
1453 0 return;
1454 }
1455 else {
1456 34 return $class;
1457 }
1458 }
1459  
1460 56 1;
1461  
1462 __END__
« Astro/Catalog/IO/Hedwig.pm ↑ Summary Astro/Catalog/Query/USNOA2.pm »