Astro/Catalog/Query.pm

filter: Statementstmt 70.3% 114 / 162 Branchbran 44.2% 23 / 52 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 64.7% 22 / 34 Podpod 25.0% 3 / 12 totaltotal 61.1% 162 / 265 CC 31 SCAR 16.5
CC
31
Cov
63%
_from_sort
21.5
new
6.0
faint
6.0
CRAP 5.2
« Astro/Catalog/IO/XY.pm ↑ Summary Astro/Catalog/Item/Morphology.pm »
1 package Astro::Catalog::Query;
2  
3 =head1 NAME
4  
5 Astro::Catalog::Query - Base class for Astro::Catalog query objects
6  
7 =head1 SYNOPSIS
8  
9 use base qw/Astro::Catalog::Query/;
10  
11 =head1 DESCRIPTION
12  
13 This class forms a base class for all the query classes provided
14 in the C<Astro::Catalog> distribution (eg C<Astro::Catalog::GSC::Query>).
15  
16 =cut
17  
18 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 19 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 19 ▶use Astro::Coords;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 19 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 19 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 19 our $VERSION = '4.38';
30  
31 =head1 METHODS
32  
33 =head2 Constructor
34  
35 =over 4
36  
37 =item B<new>
38  
39 Create a new instance from a hash of options
40  
41 $q = new Astro::Catalog::Query(
42 Coords => new Astro::Coords(),
43 Radius => $radius,
44 Bright => $magbright,
45 Faint => $magfaint,
46 Sort => $sort_type,
47 Number => $number_out);
48  
49 returns a reference to an query object. Must only called from
50 sub-classed constructors.
51  
52 RA and Dec are also allowed but are deprecated (since with only
53 RA/Dec the coordinates must always be supplied as J2000 space-separated
54 sexagesimal format).
55  
56 =cut
57  
58 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
59 0 my $proto = shift;
60 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
61  
62 # bless the query hash into the class
63 0 my $block = bless {
64 OPTIONS => {},
65 BUFFER => undef,
66 }, $class;
67  
68 # Configure the object [even if there are no args]
69 0 $block->configure(@_);
70  
71 0 return $block;
72 }
73  
74 =back
75  
76 =head2 Accessor Methods
77  
78 =over 4
79  
80 =item B<query_options>
81  
82 Hash representing the query options to be used to query the catalog
83 server. This keys in this hash are restricted by the subclass. Some
84 keys are not usable by all catalogs.
85  
86 Returns a copy of the options hash when.
87  
88 %options = $q->query_options();
89  
90 Note that the hash keys included here are not necessarily the keys
91 used to form a remote query.
92  
93 If an argument is supplied, the value for that option is returned
94 I<if> the option is supported.
95  
96 $ra = $q->query_options("ra");
97  
98 Values can not be set directly. Please use the provided accessor methods.
99  
100 =cut
101  
102 51 ▶sub query_options {
Subroutine query_options: called CC=3 SCAR=13.1
Pod documented
103 51 my $self = shift;
104 51 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 51 0 51
105 51 my $opt = lc(shift);
106 51 my %allow = $self->_get_allowed_options;
107  
108 51 ▶ unless (exists $allow{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $allow{$opt}) 0 51 51
109 0 warnings::warnif("Option $opt not supported by this cataloge");
110 0 return;
111 }
112 51 return $self->{OPTIONS}->{$opt};
113 }
114 0 return %{$self->{OPTIONS}};
115 }
116  
117 =item B<RA>
118  
119 Return (or set) the current target R.A. defined for the query
120  
121 $ra = $usno->ra();
122 $usno->ra($ra);
123  
124 where $ra should be a string of the form "HH MM SS.SS", e.g. 21 42 42.66
125  
126 =cut
127  
128 2 ▶sub ra {
Subroutine ra: called CC=2 SCAR=7.0
Pod undocumented
129 2 my $self = shift;
130  
131 # SETTING R.A.
132 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
133 # grab the new R.A.
134 2 my $ra = shift;
135 2 $self->_set_query_options(ra => $ra);
136 }
137 # Return it
138 2 return $self->query_options("ra");
139 }
140  
141 =item B<Dec>
142  
143 Return (or set) the current target Declination defined for the query
144  
145 $dec = $q->dec();
146 $q->dec($dec);
147  
148 where $dec should be a string of the form "+-HH MM SS.SS", e.g. +43 35 09.5
149 or -40 25 67.89
150  
151 =cut
152  
153 2 ▶sub dec {
Subroutine dec: called CC=2 SCAR=7.0
Pod undocumented
154 2 my $self = shift;
155  
156 # SETTING DEC
157 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
158 # grab the new Dec
159 2 my $dec = shift;
160 2 $self->_set_query_options(dec => $dec);
161 }
162  
163 2 return $self->query_options("dec");
164 }
165  
166  
167 =item B<Target>
168  
169 Instead of querying by R.A. and Dec., you may also query it
170 by object name. Return (or set) the current target object defined for
171 the USNO-A2.0 query, will query SIMBAD for object name resolution.
172  
173 $ident = $usno->target();
174 $usno->target("HT Cas");
175  
176 using an object name will override the current R.A. and Dec settings for the
177 Query object (if currently set) and the next querydb() method call will query
178 using this identifier rather than any currently set co-ordinates.
179  
180 =cut
181  
182 3 ▶sub target {
Subroutine target: called CC=4 SCAR=13.9
Pod undocumented
183 3 my $self = shift;
184  
185 # SETTING IDENTIFIER
186 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
187 # grab the new object name
188 3 my $ident = shift;
189  
190 # Need to clear RA and Dec iff they are allowed options
191 3 my %allow = $self->_get_allowed_options();
192  
193 3 my %clear;
194 3 ▶ $clear{ra} = undef if exists $allow{ra};
Branch: 1 branch
BranchTrueFalseTotal
if exists $allow{"ra"} 1 2 3
195 3 ▶ $clear{dec} = undef if exists $allow{dec};
Branch: 1 branch
BranchTrueFalseTotal
if exists $allow{"dec"} 1 2 3
196  
197 # Store it in the options table
198 3 $self->_set_query_options(
199 object => $ident,
200 %clear);
201 }
202 3 return $self->query_options("object");
203 }
204  
205 =item B<Radius>
206  
207 The radius to be searched for objects around the target R.A. and Dec in
208 arc minutes, the radius defaults to 5 arc minutes.
209  
210 $radius = $query->radius();
211 $query->radius(20);
212  
213 =cut
214  
215 2 ▶sub radius {
Subroutine radius: called CC=2 SCAR=7.0
Pod undocumented
216 2 my $self = shift;
217  
218 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
219 2 $self->_set_query_options(radmax => shift);
220 }
221  
222 2 return $self->query_options("radmax");
223 }
224  
225 =item B<Faint>
226  
227 Set (or query) the faint magnitude limit for inclusion on the results
228  
229 $faint = $query->faint();
230 $query->faint(50);
231  
232 =cut
233  
234 0 ▶sub faint {
Subroutine faint: not called CC=2 SCAR=17.9
Pod undocumented
235 0 my $self = shift;
236  
237 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
238 0 $self->_set_query_options(magfaint => shift);
239 }
240  
241 0 return $self->query_options("magfaint");
242 }
243  
244 =item B<Bright>
245  
246 Set (or query) the bright magnitude limit for inclusion on the results
247  
248 $faint = $query->bright();
249 $query->bright(2);
250  
251 =cut
252  
253 0 ▶sub bright {
Subroutine bright: not called CC=2 SCAR=17.9
Pod undocumented
254 0 my $self = shift;
255  
256 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
257 0 $self->_set_query_options(magbright => shift);
258 }
259  
260 0 return $self->query_options("magbright");
261 }
262  
263 =item B<Sort>
264  
265 Set or query the order in which the stars are listed in the catalog
266  
267 $sort = $query->sort();
268 $query->sort('RA');
269  
270 valid options are RA, DEC, RMAG, BMAG, DIST (distance to centre of the
271 requested field) and POS (the position angle to the centre of the field).
272  
273 =cut
274  
275 0 ▶sub sort {
Subroutine sort: not called CC=2 SCAR=17.9
Pod undocumented
276 0 my $self = shift;
277  
278 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
279 0 my $sort = shift;
280 0 $self->_set_query_options(sort => $sort);
281 }
282  
283 # return the sort option
284 0 return $self->query_options("sort");
285 }
286  
287 =item B<Number>
288  
289 The number of objects to return, defaults to 2000 which should hopefully
290 be sufficent to return all objects of interest. This value should be increased
291 if a (very) large sample radius is requested.
292  
293 $num = $query->number();
294 $query->nout(100);
295  
296 =cut
297  
298 0 ▶sub number {
Subroutine number: not called CC=2 SCAR=17.9
Pod undocumented
299 0 my $self = shift;
300  
301 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
302 0 $self->_set_query_options(nout => shift);
303 }
304  
305 0 return $self->query_options("nout");
306 }
307  
308 0 ▶sub nout {
Subroutine nout: not called CC=1 SCAR=6.9
Pod undocumented
309 0 my $self = shift;
310 0 warnings::warnif("deprecated","The nout() method is deprecated. Please use number()");
311 0 return $self->number(@_);
312 }
313  
314 =back
315  
316 =head2 General Methods
317  
318 =over 4
319  
320 =item B<configure>
321  
322 Configures the object, takes an options hash as an argument
323  
324 $dss->configure(%options);
325  
326 Does nothing if the array is not supplied.
327  
328 =cut
329  
330 5 ▶sub configure {
Subroutine configure: called CC=5 SCAR=16.2
Pod documented
331 5 my $self = shift;
332  
333 # CONFIGURE DEFAULTS
334  
335 5 $self->_set_default_options();
336  
337 # CONFIGURE FROM ARGUMENTS
338  
339 # return unless we have arguments
340 5 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 5 5
341  
342 # grab the argument list
343 5 my %args = Astro::Catalog::_normalize_hash(@_);
344  
345 # Grab the allowed options
346 5 my %allow = $self->_get_allowed_options();
347  
348 # Loop over the supplied arguments. If they correspond to
349 # a method, run it, if they correspond to an option, set it
350 5 for my $key (keys %args) {
351 13 my $lckey = lc($key);
352 13 ▶ if ($self->can($lckey)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can($lckey)) { } 9 4 13
elsif (exists $allow{$lckey}) { } 4 0 4
353 9 $self->$lckey( $args{$key} );
354 }
355 elsif (exists $allow{$lckey}) {
356 # set the option explcitly
357 4 $self->_set_query_options( $lckey => $args{$key} );
358 }
359 else {
360 #warnings::warnif("Unrecognized option: $key. Ignoring it.");
361 }
362 }
363 }
364  
365 =back
366  
367 =begin __PRIVATE_METHODS__
368  
369 =head2 Private methods
370  
371 These methods are for internal use only.
372  
373 =over 4
374  
375 =item B<_set_query_options>
376  
377 Set the query options.
378  
379 $q->_set_query_options(%newopt);
380  
381 Keys are standardised and are not necessarily those used
382 in the query. A warning is issued if an attempt is made to
383 set an option for an option that is not used by the particular
384 subclass.
385  
386 =cut
387  
388 16 ▶sub _set_query_options {
Subroutine _set_query_options: called CC=3 SCAR=11.6
389 16 my $self = shift;
390 16 my %newopt = @_;
391  
392 16 my %allow = $self->_get_allowed_options();
393  
394 16 for my $newkey (keys %newopt) {
395 53 ▶ unless (exists $allow{$newkey}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $allow{$newkey}) 0 53 53
396 0 warnings::warnif("Option $newkey not supported by catalog ".
397 ref($self)."\n");
398 0 next;
399 }
400 # set the option
401 53 $self->{OPTIONS}->{$newkey} = $newopt{$newkey};
402 }
403 16 return;
404 }
405  
406 =item B<_get_allowed_options>
407  
408 Return a hash with keys corresponding to the internal options
409 supported by the query, and values corresponding to the names
410 used by the specific query sub-system. Can use the keys
411 to work out whether an option is supported.
412  
413 %allow = $q->_get_allowed_options();
414  
415 Generally, must be over-ridden in subclass. By default returns all
416 the internal options, with 1-1 mapping.
417  
418 =cut
419  
420 0 ▶sub _get_allowed_options {
Subroutine _get_allowed_options: not called CC=1 SCAR=6.9
421 return (
422 0 ra => 'ra',
423 dec => 'dec',
424 object => 'object',
425 radmax => 'radmax',
426 radmin => 'radmin',
427 width => 'width',
428 height => 'height',
429 magbright => 'magbright',
430 magfaint => 'magfaint',
431 sort => 'sort',
432 nout => 'nout',
433 );
434 }
435  
436 =item B<_get_supported_accessor_options>
437  
438 Returns a hash with keys corresponding to accessor methods
439 and values corresponding to the internal option.
440  
441 %opt = $q->_get_supported_accessor_options();
442  
443 This method should be superfluous if the methods had been named
444 correctly!
445  
446 Should support object init either via options or methods. This does not cover all
447 options. In configure, if there is an option available but no corresponding mapping
448 then we will just set the option directly.
449  
450 =cut
451  
452 0 ▶sub _get_supported_accessor_options {
Subroutine _get_supported_accessor_options: not called CC=1 SCAR=6.9
453 return (
454 0 ra => 'ra',
455 dec => 'dec',
456 faint => 'magfaint',
457 bright => 'magbright',
458 radius => 'radmax',
459 target => 'object',
460 sort => 'sort',
461 number => 'nout',
462 format => 'format',
463 );
464 }
465  
466 =item B<_get_default_options>
467  
468 Retrieve the defaults options for this particular catalog query.
469 Usually called by C<_set_default_options> during object configure.
470  
471 %defs = $q->_get_default_options();
472  
473 =cut
474  
475 0 ▶sub _get_default_options {
Subroutine _get_default_options: not called CC=1 SCAR=6.9
476 0 croak "get_default_options must be subclassed";
477 }
478  
479 =item B<_set_default_options>
480  
481 Each catalog requires different default settings for the
482 URL parameters. They should be specified in a subclass.
483  
484 =cut
485  
486 3 ▶sub _set_default_options {
Subroutine _set_default_options: called CC=1 SCAR=0.0
487 3 my $self = shift;
488  
489 # get the defaults
490 3 my %defaults = $self->_get_default_options();
491  
492 # set them
493 3 $self->_set_query_options( %defaults );
494 3 return;
495 }
496  
497 =item B<_dump_raw>
498  
499 Private function for debugging and other testing purposes. It will return
500 the raw output of the last query made using querydb().
501  
502 @lines = $q->_dump_raw();
503  
504 =cut
505  
506 0 ▶sub _dump_raw {
Subroutine _dump_raw: not called CC=1 SCAR=6.9
507 0 my $self = shift;
508  
509 # split the BUFFER into an array
510 0 my @portable = split(/\n/, $self->{BUFFER});
511 0 chomp @portable;
512  
513 0 return @portable;
514 }
515  
516 =item B<_set_raw>
517  
518 Private function to fill the current buffer with a string. This is used
519 when deealing with the buffer cannot be encapsulated inside a Transport
520 class and must be deal with by child classese.
521  
522 $q->_set_raw($buffer);
523  
524 =cut
525  
526 2 ▶sub _set_raw {
Subroutine _set_raw: called CC=1 SCAR=0.0
527 2 my $self = shift;
528 2 $self->{BUFFER} = shift;
529 }
530  
531 =item B<_dump_options>
532  
533 Private function for debugging and other testing purposes. It will return
534 the current query options as a hash.
535  
536 =cut
537  
538 0 ▶sub _dump_options {
Subroutine _dump_options: not called CC=1 SCAR=6.9
539 0 my $self = shift;
540  
541 0 return $self->query_options;
542 }
543  
544 =item B<_parse_query>
545  
546 Stub. Needs to be subclassed.
547  
548 =cut
549  
550 0 ▶sub _parse_query {
Subroutine _parse_query: not called CC=1 SCAR=6.9
551 0 croak "Query parsing is not generic. Please write one\n";
552 }
553  
554 =back
555  
556 =head2 Translation Methods
557  
558 The query options stored internally in the object are not necessarily
559 the form required for a query to a remote server. Methods for converting
560 from the internal representation to the external query format are
561 provided in the form of _from_$opt. ie:
562  
563 ($outkey, $outvalue) = $q->_from_ra();
564 ($outkey, $outvalue) = $q->_from_object();
565  
566 Items that have a one-to-one mapping can be declared by the query
567 subclass using the C<_translate_one_to_one> method which returns
568 a list of options that support the simplest mapping. If an explicit
569 method exists it is always used.
570  
571 If an option has no translation method and is not declared as
572 a one-to-one mapping, the translator will assume one-to-one but
573 issue a warning.
574  
575 =item B<_translate_options>
576  
577 Translates the options from the default interface into the internal
578 options specific for the sub-class
579  
580 %options = _translate_options( );
581  
582 The keys and values therefore are no longer general.
583  
584 =cut
585  
586 5 ▶sub _translate_options {
Subroutine _translate_options: called CC=4 SCAR=13.9
587 5 my $self = shift;
588  
589 5 my %outhash;
590 5 my %allow = $self->_get_allowed_options();
591 5 my %one_one = $self->_translate_one_to_one();
592  
593 5 foreach my $key ( keys %allow ) {
594 # Need to translate them...
595 40 my $cvtmethod = "_from_" . $key;
596 40 my ($outkey, $outvalue);
597 40 ▶ if ($self->can($cvtmethod)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($cvtmethod)) { } 9 31 40
598 9 ($outkey, $outvalue) = $self->$cvtmethod();
599 }
600 else {
601 # This is the one-to-one mapping section
602 # issue a warning if the method has not been declared
603 # as supporting that simply mapping
604 warnings::warnif("Unable to find translation for key $key. Assuming 1 to 1 mapping.\n")
605 31 ▶ unless exists $one_one{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $one_one{$key} 0 31 31
606  
607 # Translate the key and copy the value
608 31 $outkey = $allow{$key};
609 31 $outvalue = $self->query_options($key);
610 }
611 40 $outhash{$outkey} = $outvalue;
612 }
613 5 return %outhash;
614 }
615  
616 =item B<_translate_one_to_one>
617  
618 Returns (hash) indicating which of the standard options support
619 a one-to-one mapping when forming a URL (etc).
620  
621 =cut
622  
623 5 ▶sub _translate_one_to_one {
Subroutine _translate_one_to_one: called CC=1 SCAR=0.0
624 # convert to a hash-list
625 5 return map {$_, undef} (qw/
626 object radmax radmin magfaint magbright
627 nout format
628 /);
629 }
630  
631 # RA and Dec replace spaces with pluses and + sign with special code
632  
633 3 ▶sub _from_ra {
Subroutine _from_ra: called CC=2 SCAR=6.9
634 3 my $self = shift;
635 3 my $ra = $self->query_options("ra");
636 3 my %allow = $self->_get_allowed_options();
637  
638 # Must replace spaces with +
639 3 ▶ $ra =~ s/\s/\+/g if defined $ra;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ra 2 1 3
640  
641 3 return ($allow{ra},$ra);
642 }
643  
644 3 ▶sub _from_dec {
Subroutine _from_dec: called CC=2 SCAR=6.9
645 3 my $self = shift;
646 3 my $dec = $self->query_options("dec");
647 3 my %allow = $self->_get_allowed_options();
648  
649 3 ▶ if (defined $dec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dec) 2 1 3
650 # Must replace + with %2B
651 2 $dec =~ s/\+/%2B/g;
652  
653 # Must replace spaces with +
654 2 $dec =~ s/\s/\+/g;
655 }
656  
657 3 return ($allow{dec},$dec);
658 }
659  
660 2 ▶sub _from_sort {
Subroutine _from_sort: called CC=7 SCAR=30.7
661 2 my $self = shift;
662 2 my $key = "sort";
663 # case insensitive conversion
664 2 my $value = uc($self->query_options($key));
665  
666 2 my $sort;
667 # pick an option
668 2 ▶ if ($value eq "RA") {
Branch: 6 branches
BranchTrueFalseTotal
if ($value eq 'RA') { } 2 0 2
elsif ($value eq 'DEC') { } 0 0 0
elsif ($value eq 'RMAG') { } 0 0 0
elsif ($value eq 'BMAG') { } 0 0 0
elsif ($value eq 'DIST') { } 0 0 0
elsif ($value eq 'POS') { } 0 0 0
669 # sort by RA
670 2 $sort = "ra";
671 }
672 elsif ($value eq "DEC") {
673 # sort by Dec
674 0 $sort = "dec";
675 }
676 elsif ($value eq "RMAG") {
677 # sort by R magnitude
678 0 $sort = "mr";
679 }
680 elsif ($value eq "BMAG") {
681 # sort by B magnitude
682 0 $sort = "mb";
683 }
684 elsif ($value eq "DIST") {
685 # sort by distance from field centre
686 0 $sort = "d";
687 }
688 elsif ($value eq "POS") {
689 # sort by position angle to field centre
690 0 $sort = "pos";
691 }
692 else {
693 # in case there are no valid options sort by RA
694 0 warnings::warnif("Unknown sort type [$value]: using ra");
695 0 $sort = "ra";
696 }
697 2 my %allow = $self->_get_allowed_options();
698 2 return ($allow{$key}, $sort);
699 }
700  
701 # This is a template methdo that can be extended. This one
702 # implements a one to one mapping
703  
704 #sub _from_XXX {
705 # my $self = shift;
706 # my $key = "XXX";
707 # my $value = $self->query_options($key);
708 # my %allow = $self->_get_allowed_options();
709 # return ($allow{$key}, $value);
710 #}
711  
712 19 1;
713  
714 __END__
« Astro/Catalog/IO/XY.pm ↑ Summary Astro/Catalog/Item/Morphology.pm »