Astro/Catalog/Query/SuperCOSMOS.pm

filter: Statementstmt 16.2% 48 / 295 Branchbran 0.0% 0 / 140 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 45.1% 14 / 31 Podpod 100% 3 / 3 totaltotal 13.0% 65 / 498 CC 85 SCAR 64.3
CC
85
Cov
11%
_parse_query
1640.0
_load_config
240.0
configure
72.0
CRAP 617.3
« Astro/Catalog/IO/SExtractor.pm ↑ Summary Astro/Catalog/IO/TST.pm »
1 package Astro::Catalog::Query::SuperCOSMOS;
2  
3 # Depressingly the generic reg expression used the SkyCat.pm doesn't
4 # seem to work for SuperCOSMOS URL's, eventually we're going to have
5 # to make the regexp more generic. In the interim, I've cut and pasted
6 # the entire module into this sub-class so I can do queries.
7 #
8 # Yes Tim, I know this sucks.
9  
10 =head1 NAME
11  
12 Astro::Catalog::Query::SuperCOSMOS - A query request to the SuperCOSMOS catalogue
13  
14 =head1 SYNOPSIS
15  
16 $supercos = new Astro::Catalog::Query::SuperCOSMOS(
17 RA => $ra,
18 Dec => $dec,
19 Radius => $radius,
20 Nout => $number_out,
21 Colour => $band);
22  
23 my $catalog = $supercos->querydb();
24  
25 =head1 WARNING
26  
27 This code totally ignores the epoch of the observations and the associated
28 proper motions, this pretty much means that for astrometric work the catalogs
29 you get back from the query are pretty much bogus. This should be sorted in
30 the next distribution.
31  
32 =head1 DESCRIPTION
33  
34 The module is an object orientated interface to the online SuperCOSMOS
35 catalogue using the generic Astro::Catalog::Query::SkyCat class
36  
37 Stores information about an prospective query and allows the query to
38 be made, returning an Astro::Catalog::Query::SuperCOSMOS object.
39  
40 The object will by default pick up the proxy information from the HTTP_PROXY
41 and NO_PROXY environment variables, see the LWP::UserAgent documentation for
42 details.
43  
44 See L<Astro::Catalog::BaseQuery> for the catalog-independent methods.
45  
46 =cut
47  
48 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use base qw/Astro::Catalog::Transport::REST/;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # generic catalog objects
59 1 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 1 ▶use Astro::Flux;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Astro::FluxColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Astro::Fluxes;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use Number::Uncertainty;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 1 our $VERSION = '4.38';
68 1 our $DEBUG = 0;
69  
70 # Controls whether we follow 'directory' config entries and recursively
71 # expand those. Default to false at the moment.
72 1 our $FOLLOW_DIRS = 0;
73  
74 # This is the name of the config file that was used to generate
75 # the content in %CONFIG. Can be different to the contents ofg_file
76 # if that
77 1 my $CFG_FILE;
78  
79 # This is the content of the config file
80 # organized as a hash indexed by remote server shortname
81 # this has the advantage of removing duplicates
82 my %CONFIG;
83  
84 =head1 METHODS
85  
86 =head2 Constructor
87  
88 =over 4
89  
90 =item B<new>
91  
92 Simple constructor, handles the 'Colour' option, e.g.
93  
94 long_name: SuperCOSMOS catalog - blue (UKJ) southern survey
95 short_name: SSScat_UKJ@WFAU
96  
97 long_name: SuperCOSMOS catalog - red (UKR) southern survey
98 short_name: SSScat_UKR@WFAU
99  
100 long_name: SuperCOSMOS catalog - near IR (UKI) southern survey
101 short_name: SSScat_UKI@WFAU
102  
103 long_name: SuperCOSMOS catalog - red (ESOR) southern survey
104 short_name: SSScat_ESOR@WFAU
105  
106 $q = new Astro::Catalog::Query::SuperCOSMOS(colour => 'UKJ', %options);
107  
108 Allowed options are 'UKJ', 'UKR', 'UKI', and 'ESOR' for the UK Blue, UK Red,
109 UK near-IR and ESO Red catalogues respectively.
110  
111 All other options are passed on to SUPER::new().
112  
113 =cut
114  
115 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
116 0 my $proto = shift;
117 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
118  
119 # Instantiate via base class
120 0 my $block = $class->SUPER::new( @_ );
121  
122 0 return $block;
123 }
124  
125 =back
126  
127 =head2 Accessor methods
128  
129 =over 4
130  
131 =item B<_selected_catalog>
132  
133 Catalog name selected by the user and currently configured for
134 this object. Not to be used outside this class..
135  
136 =cut
137  
138 0 ▶sub _selected_catalog {
Subroutine _selected_catalog: not called CC=2 SCAR=17.9
139 0 my $self = shift;
140 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
141 # The class has to be configured as a hash!!!
142 0 $self->{SKYCAT_CATALOG} = shift;
143 }
144  
145 0 return $self->{SKYCAT_CATALOG};
146 }
147  
148 =back
149  
150 =head2 General methods
151  
152 =over 4
153  
154 =item C<configure>
155  
156 Configure the object. This calls the base class configure , after it has
157 made sure that a sky cat config file has been read (otherwise we will
158 not be able to vet the incoming arguments.
159  
160 =cut
161  
162 0 ▶sub configure {
Subroutine configure: not called CC=8 SCAR=42.8
Pod documented
163 0 my $self = shift;
164  
165 # load a config if we do not have one read yet
166 # Note that this may force a remote URL read via directory
167 # directives even though we do not have a user agent configured...
168 0 ▶ $self->_load_config() unless %CONFIG;
Branch: 1 branch
BranchTrueFalseTotal
unless %CONFIG 0 0 0
169  
170 # Error if we have no config yet
171 0 ▶ croak "Error instantiating SuperCOSMOS object since no config was located"
Branch: 1 branch
BranchTrueFalseTotal
unless %CONFIG 0 0 0
172 unless %CONFIG;
173  
174 # Now we need to configure this object based on the
175 # supplied catalog name. This is not really a public interface
176 # let's call it a protected interface available to subclases
177 # even though we are not technically a subclass...
178 0 my %args = Astro::Catalog::_normalize_hash(@_);
179  
180 croak "A colour must be provided using the 'colour' key"
181 0 ▶ unless exists $args{colour};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"colour"} 0 0 0
182  
183 # case-insensitive
184 0 my $colour = lc($args{colour});
185  
186 0 ▶ if ($colour eq 'ukj') {
Branch: 4 branches
BranchTrueFalseTotal
if ($colour eq 'ukj') { } 0 0 0
elsif ($colour eq 'ukr') { } 0 0 0
elsif ($colour eq 'uki') { } 0 0 0
elsif ($colour eq 'esor') { } 0 0 0
187 0 $self->_selected_catalog('ssscat_ukj@wfau');
188  
189 }
190 elsif ($colour eq 'ukr') {
191 0 $self->_selected_catalog('ssscat_ukr@wfau');
192  
193 }
194 elsif ($colour eq 'uki') {
195 0 $self->_selected_catalog('ssscat_uki@wfau');
196  
197 }
198 elsif ($colour eq 'esor') {
199 0 $self->_selected_catalog('ssscat_esor@wfau');
200  
201 }
202 else {
203 # default to UKR
204 0 $self->_selected_catalog('SSScat_UKR@WFAU');
205 }
206  
207 # Configure
208 0 $self->SUPER::configure(%args);
209 }
210  
211 =item B<_build_query>
212  
213 Construct a query URL based on the options.
214  
215 $url = $q->_build_query();
216  
217 =cut
218  
219 0 ▶sub _build_query {
Subroutine _build_query: not called CC=3 SCAR=24.8
220 0 my $self = shift;
221  
222 0 my $cat = $self->_selected_catalog();
223  
224 # Get the URL
225 0 my $url = $CONFIG{$cat}->{url};
226  
227 # Translate all the options to the internal skycat format
228 0 my %translated = $self->_translate_options();
229  
230 # Now for each token replace it in the URL
231 0 for my $key (keys %translated) {
232 0 my $tok = "%". $key;
233 croak "Token $tok is mandatory but was not specified"
234 0 ▶ unless defined $translated{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $translated{$key} 0 0 0
235 0 $url =~ s/$tok/$translated{$key}/;
236 }
237  
238 0 return $url;
239 }
240  
241  
242 =item B<_parse_query>
243  
244 All the SkyCat servers return data in TST format.
245 Need to make sure that column information is passed
246 into the TST parser.
247  
248 =cut
249  
250 0 ▶sub _parse_query {
Subroutine _parse_query: not called CC=40 SCAR=74.0
251 0 my $self = shift;
252  
253 # Get the catalog info
254 0 my $cat = $self->_selected_catalog();
255  
256 # and extract formatting information needed by the TST parser
257 0 my %params;
258 0 for my $key (keys %{ $CONFIG{$cat} }) {
259 0 ▶ if ($key =~ /_col$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /_col$/) 0 0 0
260 0 $params{$key} = $CONFIG{$cat}->{$key};
261 }
262 }
263  
264 # Time to pad the params with known values, this is yet another un-Godly
265 # hack for which I'm duely ashamed. God help us if they ever change the
266 # catalogs. Why is SuperCOSMOS so much bloody trouble?
267  
268 # Make sure we set origin and field centre if we know it
269 my $query = new Astro::Catalog(
270 Format => 'TST',
271 Data => $self->{BUFFER},
272 ReadOpt => \%params,
273 0 Origin => $CONFIG{$cat}->{long_name});
274  
275 # Grab each star in the catalog and add some value to it
276 0 my $catalog = new Astro::Catalog();
277 0 $catalog->origin($query->origin());
278 0 ▶ $catalog->set_coords($query->get_coords()) if defined $query->get_coords();
Branch: 1 branch
BranchTrueFalseTotal
if defined $query->get_coords 0 0 0
279  
280 0 my @stars = $query->allstars();
281  
282 0 my (@mags, @cols);
283 0 foreach my $i (0 ... $#stars) {
284 0 my ($cval, $err, $mag, $col);
285 0 my @mags = undef;
286 0 my @cols = undef;
287  
288 0 my $star = $stars[$i];
289  
290 # if we have a non-zero quality, set the quality to 1 (this sucks!)
291 0 ▶ $star->quality(1) if( $star->quality() != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $star->quality != 0 0 0 0
292  
293 # calulate the errors
294  
295 0 $err = 0.04;
296 0 ▶ if ( $star->get_magnitude( "BJ" ) != 99.999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($star->get_magnitude('BJ') != 99.999) { } 0 0 0
297 0 ▶ $err = 0.04 if $star->get_magnitude( "BJ" ) > 15.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("BJ") > 15 0 0 0
298 0 ▶ $err = 0.05 if $star->get_magnitude( "BJ" ) > 17.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("BJ") > 17 0 0 0
299 0 ▶ $err = 0.06 if $star->get_magnitude( "BJ" ) > 19.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("BJ") > 19 0 0 0
300 0 ▶ $err = 0.07 if $star->get_magnitude( "BJ" ) > 20.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("BJ") > 20 0 0 0
301 0 ▶ $err = 0.12 if $star->get_magnitude( "BJ" ) > 21.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("BJ") > 21 0 0 0
302 0 ▶ $err = 0.08 if $star->get_magnitude( "BJ" ) > 22.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("BJ") > 22 0 0 0
303 }
304 else {
305 0 $err = 99.999;
306 }
307 0 $mag = new Astro::Flux(new Number::Uncertainty(
308 Value => $star->get_magnitude("BJ"),
309 Error => $err ),
310 'mag', 'BJ' );
311 0 push @mags, $mag;
312  
313 0 $err = 0.06;
314 0 ▶ if ( $star->get_magnitude( "R1" ) != 99.999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($star->get_magnitude('R1') != 99.999) { } 0 0 0
315 0 ▶ $err = 0.06 if $star->get_magnitude( "R1" ) > 11.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R1") > 11 0 0 0
316 0 ▶ $err = 0.03 if $star->get_magnitude( "R1" ) > 12.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R1") > 12 0 0 0
317 0 ▶ $err = 0.09 if $star->get_magnitude( "R1" ) > 13.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R1") > 13 0 0 0
318 0 ▶ $err = 0.10 if $star->get_magnitude( "R1" ) > 14.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R1") > 14 0 0 0
319 0 ▶ $err = 0.12 if $star->get_magnitude( "R1" ) > 18.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R1") > 18 0 0 0
320 0 ▶ $err = 0.18 if $star->get_magnitude( "R1" ) > 19.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R1") > 19 0 0 0
321 }
322 else {
323 0 $err = 99.999;
324 }
325 0 $mag = new Astro::Flux(new Number::Uncertainty(
326 Value => $star->get_magnitude("R1"),
327 Error => $err),
328 'mag', 'R1' );
329 0 push @mags, $mag;
330  
331 0 $err = 0.02;
332 0 ▶ if ( $star->get_magnitude( "R2" ) != 99.999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($star->get_magnitude('R2') != 99.999) { } 0 0 0
333 0 ▶ $err = 0.02 if $star->get_magnitude( "R2" ) > 12.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R2") > 12 0 0 0
334 0 ▶ $err = 0.03 if $star->get_magnitude( "R2" ) > 13.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R2") > 13 0 0 0
335 0 ▶ $err = 0.04 if $star->get_magnitude( "R2" ) > 15.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R2") > 15 0 0 0
336 0 ▶ $err = 0.05 if $star->get_magnitude( "R2" ) > 17.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R2") > 17 0 0 0
337 0 ▶ $err = 0.06 if $star->get_magnitude( "R2" ) > 18.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R2") > 18 0 0 0
338 0 ▶ $err = 0.11 if $star->get_magnitude( "R2" ) > 19.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R2") > 19 0 0 0
339 0 ▶ $err = 0.16 if $star->get_magnitude( "R2" ) > 20.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("R2") > 20 0 0 0
340 }
341 else {
342 0 $err = 99.999;
343 }
344 0 $mag = new Astro::Flux( new Number::Uncertainty(
345 Value => $star->get_magnitude("R2"),
346 Error => $err ),
347 'mag', 'R2' );
348 0 push @mags, $mag;
349  
350 0 $err = 0.05;
351 0 ▶ if ( $star->get_magnitude( "I" ) != 99.999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($star->get_magnitude('I') != 99.999) { } 0 0 0
352 0 ▶ $err = 0.05 if $star->get_magnitude( "I" ) > 15.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("I") > 15 0 0 0
353 0 ▶ $err = 0.06 if $star->get_magnitude( "I" ) > 16.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("I") > 16 0 0 0
354 0 ▶ $err = 0.09 if $star->get_magnitude( "I" ) > 17.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("I") > 17 0 0 0
355 0 ▶ $err = 0.16 if $star->get_magnitude( "I" ) > 18.0;
Branch: 1 branch
BranchTrueFalseTotal
if $star->get_magnitude("I") > 18 0 0 0
356 }
357 else {
358 0 $err = 99.999;
359 }
360 0 $mag = new Astro::Flux( new Number::Uncertainty(
361 Value => $star->get_magnitude("I"),
362 Error => $err ),
363 'mag', 'I' );
364 0 push @mags, $mag;
365  
366 # calculate colours UKST Bj - UKST R, UKST Bj - UKST I
367  
368 0 ▶ if ($star->get_magnitude( "BJ" ) != 99.999 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($star->get_magnitude('BJ') != 99.999 and $star->get_magnitude('R2') != 99.999) { } 0 0 0
Condition0%0/3
$star->get_magnitude("BJ") != "99.999" and $star->get_magnitude("R2") != "99.999"
!ll&&!rl&&r
000
MC/DC0%0/2
$star->get_magnitude("BJ") != "99.999" and $star->get_magnitude("R2") != "99.999"
$star->get_magnitude("BJ") != "99.999" $star->get_magnitude("R2") != "99.999"
Truth table0%0/3
A and B
A= $star->get_magnitude("BJ") != "99.999"B= $star->get_magnitude("R2") != "99.999"
ABresult
0X0
100
111
369 $star->get_magnitude( "R2" ) != 99.999) {
370 0 my $bj_minus_r2 = $star->get_magnitude( "BJ" ) -
371 $star->get_magnitude( "R2" );
372 0 $bj_minus_r2 = sprintf("%.4f", $bj_minus_r2 );
373  
374 0 my $delta_bjmr = ( ( $star->get_errors( "BJ" ) )**2.0 +
375 ( $star->get_errors( "R2" ) )**2.0 )** (1/2);
376 0 $delta_bjmr = sprintf("%.4f", $delta_bjmr );
377  
378 0 $cval = $bj_minus_r2;
379 0 $err = $delta_bjmr;
380 }
381 else {
382 0 $cval = 99.999;
383 0 $err = 99.999;
384 }
385 0 $col = new Astro::FluxColor(
386 upper => 'BJ',
387 lower => "R2",
388 quantity => new Number::Uncertainty(
389 Value => $cval,
390 Error => $err ) );
391 0 push @cols, $col;
392  
393 0 ▶ if ($star->get_magnitude( "BJ" ) != 99.999 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($star->get_magnitude('BJ') != 99.999 and $star->get_magnitude('I') != 99.999) { } 0 0 0
Condition0%0/3
$star->get_magnitude("BJ") != "99.999" and $star->get_magnitude("I") != "99.999"
!ll&&!rl&&r
000
MC/DC0%0/2
$star->get_magnitude("BJ") != "99.999" and $star->get_magnitude("I") != "99.999"
$star->get_magnitude("BJ") != "99.999" $star->get_magnitude("I") != "99.999"
Truth table0%0/3
A and B
A= $star->get_magnitude("BJ") != "99.999"B= $star->get_magnitude("I") != "99.999"
ABresult
0X0
100
111
394 $star->get_magnitude( "I" ) != 99.999) {
395  
396 0 my $bj_minus_i = $star->get_magnitude( "BJ" ) -
397 $star->get_magnitude( "I" );
398 0 $bj_minus_i = sprintf("%.4f", $bj_minus_i );
399  
400 0 my $delta_bjmi = ( ( $star->get_errors( "BJ" ) )**2.0 +
401 ( $star->get_errors( "I" ) )**2.0 )** (1/2);
402 0 $delta_bjmi = sprintf("%.4f", $delta_bjmi );
403  
404 0 $cval = $bj_minus_i;
405 0 $err = $delta_bjmi;
406  
407 }
408 else {
409 0 $cval = 99.999;
410 0 $err = 99.999;
411 }
412 0 $col = new Astro::FluxColor(
413 upper => 'BJ',
414 lower => "I",
415 quantity => new Number::Uncertainty(
416 Value => $cval,
417 Error => $err ) );
418 0 push @cols, $col;
419  
420 # Push the data back into the star object, overwriting ther previous
421 # values we got from the initial Skycat query. This isn't a great
422 # solution, but it wasn't easy in version 3 syntax either, so I guess
423 # your milage may vary.
424  
425 0 my $fluxes = new Astro::Fluxes( @mags, @cols );
426 0 $star->fluxes( $fluxes, 1 ); # the 1 means overwrite the previous values
427  
428 # push it onto the stack
429 0 ▶ $stars[$i] = $star if defined $star;
Branch: 1 branch
BranchTrueFalseTotal
if defined $star 0 0 0
430 }
431  
432 0 $catalog->allstars( @stars );
433  
434 # set the field centre
435 0 my %allow = $self->_get_allowed_options();
436 0 my %field;
437 0 for my $key ("ra","dec","radius") {
438 0 ▶ if (exists $allow{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $allow{$key}) 0 0 0
439 0 $field{$key} = $self->query_options($key);
440 }
441 }
442 0 $catalog->fieldcentre( %field );
443  
444 0 return $catalog;
445 }
446  
447 =item B<_get_allowed_options>
448  
449 This method declares which options can be configured by the user
450 of this service. Generated automatically by the skycat config
451 file and keyed to the requested catalog.
452  
453 =cut
454  
455 0 ▶sub _get_allowed_options {
Subroutine _get_allowed_options: not called CC=1 SCAR=6.9
456 0 my $self = shift;
457 0 my $cat = $self->_selected_catalog();
458  
459 0 return %{ $CONFIG{$cat}->{allow} };
460 }
461  
462 =item B<_get_default_options>
463  
464 Get the default options that are relevant for the selected
465 catalog.
466  
467 %defaults = $q->_get_default_options();
468  
469 =cut
470  
471 0 ▶sub _get_default_options {
Subroutine _get_default_options: not called CC=1 SCAR=6.9
472 0 my $self = shift;
473  
474 # Global skycat defaults
475 0 my %defaults = (
476 # Target information
477 ra => undef,
478 dec => undef,
479 id => undef,
480  
481 # Limits
482 radmin => 0,
483 radmax => 5,
484 width => 10,
485 height => 10,
486  
487 magfaint => 100,
488 magbright => 0,
489  
490 nout => 20000,
491 cond => '',
492 );
493  
494 # Get allowed options
495 0 my %allow = $self->_get_allowed_options();
496  
497 # Trim the defaults (could do with hash slice?)
498 0 my %trim = map {$_ => $defaults{$_}} keys %allow;
499  
500 0 return %trim;
501 }
502  
503 =item B<_get_supported_init>
504  
505 =cut
506  
507 0 ▶sub _get_supported_init {
Subroutine _get_supported_init: not called CC=1 SCAR=6.9
508 0 croak "xxx - get supported init";
509 }
510  
511 =back
512  
513 =head2 Class methods
514  
515 These methods are not associated with any particular object.
516  
517 =over 4
518  
519 =item B<cfg_file>
520  
521 Location of the skycat config file. Default location is
522 C<$PERLPREFIX/etc/sss.cfg>.
523  
524 =cut
525  
526 0 ▶sub cfg_file {
Subroutine cfg_file: not called CC=6 SCAR=37.4
Pod documented
527 0 my $class = shift;
528  
529 0 my $cfg_file;
530 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
531 0 $cfg_file = shift;
532 0 ▶ $class->_load_config() || ($cfg_file = undef);
Branch: 1 branch
BranchTrueFalseTotal
$class->_load_config or $cfg_file = undef 0 0 0
533 }
534 else {
535 # generate the default path to the $PERLPRFIX/etc/sss.cfg file,
536 # this is a horrible hack, there is probably an elegant way to do
537 # this but I can't be bothered looking it up right now.
538 0 my $perlbin = $^X;
539 0 my ($volume, $dir, $file) = File::Spec->splitpath( $perlbin );
540 0 my @dirs = File::Spec->splitdir( $dir );
541 0 my @path;
542 0 foreach my $i ( 0 .. $#dirs-2 ) {
543 0 push @path, $dirs[$i];
544 }
545 0 my $directory = File::Spec->catdir( @path, 'etc' );
546  
547 # reset to the default
548 0 $cfg_file = File::Spec->catfile( $directory, "sss.cfg" );
549  
550 # debugging and testing purposes
551 0 ▶ unless (-f $cfg_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $cfg_file) 0 0 0
552 # use blib version!
553 0 $cfg_file = File::Spec->catfile( '.', 'etc', 'sss.cfg' );
554 }
555 }
556  
557 0 ▶ print "SuperCOSMOS.pm: \$cfg_file in cfg_file() is $cfg_file\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SuperCOSMOS::DEBUG 0 0 0
558 0 return $cfg_file;
559 }
560  
561 =back
562  
563 =begin __PRIVATE_METHODS__
564  
565 =head2 Internal methods
566  
567 =over 4
568  
569 =item B<_load_config>
570  
571 Method to load the skycat config information into
572 the class and configure the modules.
573  
574 $q->_load_config() or die "Error loading config";
575  
576 The config file name is obtained from the C<cfg_file> method.
577 Returns true if the file was read successfully and contained at
578 least one catalog server. Otherwise returns false.
579  
580 Requires an object to attach itself to (mainly for the useragent
581 remote directory follow up). The results of this load are
582 visible to all instances of this class.
583  
584 Usually called automatically from the constructor if a config
585 has not previously been read.
586  
587 =cut
588  
589 0 ▶sub _load_config {
Subroutine _load_config: not called CC=15 SCAR=54.8
590 0 my $self = shift;
591 0 my $cfg = $self->cfg_file;
592  
593 0 ▶ unless (defined $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg) 0 0 0
594 0 warnings::warnif("Config file not specified (undef)");
595 0 return;
596 }
597  
598 0 ▶ unless (-e $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cfg) 0 0 0
599 0 ▶ my $xcfg = (defined $cfg ? $cfg : "<undefined>");
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg ? : 0 0 0
600 0 return;
601 }
602  
603 0 my $fh;
604 0 ▶ unless (open $fh, "<$cfg") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<$cfg") 0 0 0
605 0 warnings::warnif( "Specified config file, $cfg, could not be opened: $!");
606 0 return;
607 }
608  
609 # Need to read the contents into an array
610 0 my @lines = <$fh>;
611  
612 # Process the config file and extract the raw content
613 0 my @configs = $self->_extract_raw_info( \@lines );
614  
615 # Close file
616 0 ▶ close( $fh ) or do {
Branch: 1 branch
BranchTrueFalseTotal
close $fh or do { warnings::warnif("Error closing config file, ${cfg}: $!"); return } 0 0 0
617 0 warnings::warnif("Error closing config file, $cfg: $!");
618 0 return;
619 };
620  
621 # Get the token mapping for validation
622 0 my %map = $self->_token_mapping;
623  
624 # Currently we are only interested in catalog, namesvr and archive
625 # so throw everything else away
626 0 @configs = grep { $_->{serv_type} =~ /(namesvr|catalog|archive)/ } @configs;
627  
628 # Process each entry. Mainly URL processing
629 0 for my $entry ( @configs ) {
630 # Skip if we have already analysed this server
631 0 ▶ if (exists $CONFIG{lc($entry->{short_name})}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CONFIG{lc $entry->{"short_name"}}) 0 0 0
632 0 next;
633 }
634  
635 # Extract info from the 'url'. We need to extract the following info:
636 # - Host name and port
637 # - remaining url path
638 # - all the CGI options including the static options
639 # Note that at the moment we do not do token replacement (the
640 # rest of the REST architecture expects to get the above
641 # information separately). This might well prove to be silly
642 # since we can trivially replace the tokens without having to
643 # reconstruct the url. Of course, this does allow us to provide
644 # mandatory keywords. $url =~ s/\%ra/$ra/;
645 0 ▶ if ( $entry->{url} =~ m|^http://www-wfau.roe.ac.uk/~sss/cgi-bin/gaia_obj.cgi?
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"url"} =~ m[^http://www-wfau.roe.ac.uk/~sss/cgi-bin/gaia_obj.cgi? (.*) # CGI options without trailing space ]x) 0 0 0
646 (.*) # CGI options without trailing space
647 |x) {
648 0 $entry->{remote_host} = "www-wfau.roe.ac.uk";
649 0 $entry->{url_path} = "~sss/cgi-bin/gaia_obj.cgi?";
650 0 my $options = $1;
651  
652 # if first character is & we append that to url_path since it
653 # is an empty argument
654 0 ▶ $entry->{url_path} .= "&" if $options =~ s/^\&//;
Branch: 1 branch
BranchTrueFalseTotal
if $options =~ s/^\&// 0 0 0
655  
656 # In general the options from skycat files are a real pain
657 # Most of them have nice blah=%blah format but there are some cases
658 # that do ?%ra%dec or coords=%ra %dec that just cause more trouble
659 # than they are worth given the standard URL constructor that we
660 # are attempting to inherit from REST
661 # Best idea is not to fight against it. Extract the host, path
662 # and options separately but simply use token replacement when it
663 # comes time to build the URL. This will require that the url
664 # is moved into its own method in REST.pm for subclassing.
665 # We still need to extract the tokens themselves so that we
666 # can generate an allowed options list.
667  
668 # tokens have the form %xxx but we have to make sure we allow
669 # %mime-type. Use the /g modifier to get all the matches
670 0 my @tokens = ( $options =~ /(\%[\w\-]+)/g);
671  
672 # there should always be tokens. No obvious way to reomve the anomaly
673 0 ▶ warnings::warnif( "No tokens found in $options!!!" )
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 0 0 0
674 unless @tokens;
675  
676 # Just need to make sure that these are acceptable tokens
677 # Get the lookup table and store that as the allowed options
678 0 my %allow;
679 0 for my $tok (@tokens) {
680 # only one token. See if we recognize it
681 0 my $strip = $tok;
682 0 $strip =~ s/%//;
683  
684 0 ▶ if (exists $map{$strip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map{$strip}) { } 0 0 0
685 0 ▶ if (!defined $map{$strip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $map{$strip}) { } 0 0 0
686 0 warnings::warnif("Do not know how to process token $tok" );
687 }
688 else {
689 0 $allow{ $map{$strip} } = $strip;
690 }
691 }
692 else {
693 warnings::warnif("Token $tok not currently recognized")
694 0 ▶ unless exists $map{$strip};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map{$strip} 0 0 0
695 }
696 }
697  
698 # Store them
699 0 $entry->{tokens} = \@tokens;
700 0 $entry->{allow} = \%allow;
701  
702 # And store this in the config. Only store it if we have
703 # tokens
704 0 $CONFIG{lc($entry->{short_name})} = $entry;
705 }
706 }
707  
708 0 return;
709 }
710  
711  
712 =item B<_extract_raw_info>
713  
714 Go through a skycat.cfg file and extract the raw unprocessed entries
715 into an array of hashes. The actual content of the file is passed
716 in as a reference to an array of lines.
717  
718 @entries = $q->_extract_raw_info(\@lines);
719  
720 This routine is separate from the main load routine to allow recursive
721 calls to remote directory entries.
722  
723 =cut
724  
725 0 ▶sub _extract_raw_info {
Subroutine _extract_raw_info: not called CC=6 SCAR=37.4
726 0 my $self = shift;
727 0 my $lines = shift;
728  
729 # Now read in the contents
730 0 my $current; # Current server spec
731 my @configs; # Somewhere temporary to store the entries
732  
733 0 for my $line (@$lines) {
734 # Skip comment lines and blank lines
735 0 ▶ next if $line =~ /^\s*\#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\#/ 0 0 0
736 0 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
737  
738 0 ▶ if ($line =~ /^(\w+):\s*(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\w+):\s*(.*?)\s*$/) { } 0 0 0
739 # This is content
740 0 my $key = $1;
741 0 my $value = $2;
742 # Assume that serv_type is always first
743 0 ▶ if ($key eq 'serv_type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'serv_type') { } 0 0 0
744 # Store previous config if it contains something
745 # If it actually contains information on a serv_type of
746 # directory we can follow the URL and recursively expand
747 # the content
748 0 push(@configs, $self->_dir_check( $current ));
749  
750 # Clear the config and store the serv_type
751 0 $current = { $key => $value };
752 }
753 else {
754 # Just store the key value pair
755 0 $current->{$key} = $value;
756 }
757 }
758 else {
759 # do not know what this line signifies since it is
760 # not a comment and not a content line
761 0 warnings::warnif("Unexpected line in config file: $line\n");
762 }
763 }
764  
765 # Last entry will still be in %$current so store it if it contains
766 # something.
767 0 push(@configs, $self->_dir_check( $current ));
768  
769 # Return the entries
770 0 return @configs;
771 }
772  
773 =item B<_dir_check>
774  
775 If the supplied hash reference has content, look at the content
776 and decide whether you simply want to keep that content or
777 follow up directory specifications by doing a remote URL call
778 and expanding that directory specification to many more remote
779 catalog server configs.
780  
781 @configs = $q->_dir_check(\%current);
782  
783 Returns the supplied argument, additional configs derived from
784 that argument or nothing at all.
785  
786 Do not follow a 'directory' link if we have already followed a link with
787 the same short name. This prevents infinite recursion when the catalog
788 pointed to by 'catalogs@eso' itself contains a reference to 'catalogs@eso'.
789  
790 =cut
791  
792 1 my %followed_dirs;
793 0 ▶sub _dir_check {
Subroutine _dir_check: not called CC=8 SCAR=42.8
794 0 my $self = shift;
795 0 my $current = shift;
796  
797 0 ▶ if (defined $current && %$current) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current and %$current) 0 0 0
Condition0%0/3
defined $current and %$current
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current and %$current
defined $current %$current
Truth table0%0/3
A and B
A= defined $currentB= %$current
ABresult
0X0
100
111
798 0 ▶ if ($current->{serv_type} eq 'directory') {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{'serv_type'} eq 'directory') { } 0 0 0
799 # Get the content of the URL unless we are not
800 # reading directories
801 0 ▶ if ($FOLLOW_DIRS && defined $current->{url} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($Astro::Catalog::Query::SuperCOSMOS::FOLLOW_DIRS and defined $current->{"url"} and not exists $followed_dirs{$current->{"short_name"}}) 0 0 0
Condition0%0/6
$Astro::Catalog::Query::SuperCOSMOS::FOLLOW_DIRS and defined $current->{"url"} and not exists $followed_dirs{$current->{"short_name"}}
!ll&&!rl&&r
000
$Astro::Catalog::Query::SuperCOSMOS::FOLLOW_DIRS and defined $current->{"url"}
!ll&&!rl&&r
000
MC/DC0%0/3
$Astro::Catalog::Query::SuperCOSMOS::FOLLOW_DIRS and defined $current->{"url"} and not exists $followed_dirs{$current->{"short_name"}}
$Astro::Catalog::Query::SuperCOSMOS::FOLLOW_DIRS defined $current->{"url"} not exists $followed_dirs{$current->{"short_name"}}
Truth table0%0/4
A and B and not(C)
A= $Astro::Catalog::Query::SuperCOSMOS::FOLLOW_DIRSB= defined $current->{"url"}C= not exists $followed_dirs{$current->{"short_name"}}
ABCresult
0XX0
10X0
1100
1111
802 !exists $followed_dirs{$current->{short_name}}) {
803 print "Following directory link to ". $current->{short_name}.
804 0 ▶ "[".$current->{url}."]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SuperCOSMOS::DEBUG 0 0 0
805 if $DEBUG;
806  
807 # Indicate that we have followed this link
808 0 $followed_dirs{$current->{short_name}} = $current->{url};
809  
810 # Retrieve the url, pass that array to the raw parser and then
811 # return any new configs to our caller
812 # Must force scalar context to get array ref
813 # back rather than a simple list.
814 0 return $self->_extract_raw_info(scalar $self->_get_directory_url( $current->{url} ));
815 }
816 }
817 else {
818 # Not a 'directory' so this is a simple config entry. Simply return it.
819 0 return ($current);
820 }
821 }
822  
823 # return empty list since we have no value
824 0 return ();
825 }
826  
827  
828 =item B<_get_directory_url>
829  
830 Returns the content of the remote directory URL supplied as
831 argument. In scalar context returns reference to array of lines. In
832 list context returns the lines in a list.
833  
834 $lines = $q->_get_directory_url($url);
835 @lines = $q->_get_directory__url($url);
836  
837 If we have an error retrieving the file, just return an empty
838 array (ie skip it).
839  
840 =cut
841  
842 0 ▶sub _get_directory_url {
Subroutine _get_directory_url: not called CC=3 SCAR=24.8
843 0 my $self = shift;
844 0 my $url = shift;
845  
846 # Call the base class to get the actual content
847 0 my $content = '';
848 0 eval {
849 0 $content = $self->_fetch_url( $url );
850 };
851  
852 # Need an array
853 0 my @lines;
854 0 ▶ @lines = split("\n", $content) if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 0 0 0
855  
856 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
857 0 return @lines;
858 }
859 else {
860 0 return \@lines;
861 }
862 }
863  
864 =item B<_token_mapping>
865  
866 Provide a mapping of tokens found in SkyCat config files to the
867 internal values used generically by Astro::Catalog::Query classes.
868  
869 %map = $class->_token_mappings;
870  
871 Keys are skycat tokens.
872  
873 =cut
874  
875 0 ▶sub _token_mapping {
Subroutine _token_mapping: not called CC=1 SCAR=6.9
876 return (
877 0 id => 'id',
878  
879 ra => 'ra',
880 dec => 'dec',
881  
882 # Arcminutes
883 r1 => 'radmin',
884 r2 => 'radmax',
885 w => 'width',
886 h => 'height',
887  
888 n => 'nout',
889  
890 # which filter???
891 m2 => 'magfaint',
892 m1 => 'magbright',
893  
894 # Is this a conditional?
895 cond => 'cond',
896  
897 # Not Yet Supported
898 cols => undef,
899 'mime-type' => undef,
900 ws => undef,
901 );
902 }
903  
904 =back
905  
906 =head2 Translations
907  
908 SkyCat specific translations from the internal format to URL format
909 go here.
910  
911 RA/Dec must match format described in
912 http://vizier.u-strasbg.fr/doc/asu.html
913 (at least for GSC) ie hh:mm:ss.s+/-dd:mm:ss
914 or decimal degrees.
915  
916 =over 4
917  
918 =cut
919  
920 0 ▶sub _from_dec {
Subroutine _from_dec: not called CC=2 SCAR=17.9
921 0 my $self = shift;
922 0 my $dec = $self->query_options("dec");
923 0 my %allow = $self->_get_allowed_options();
924  
925 # Need colons
926 0 $dec =~ s/\s+/:/g;
927  
928 # Need a + preprended
929 0 ▶ $dec = "+" . $dec if $dec !~ /^[\+\-]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dec =~ /^[\+\-]/ 0 0 0
930  
931 0 return ($allow{dec},$dec);
932 }
933  
934 0 ▶sub _from_ra {
Subroutine _from_ra: not called CC=1 SCAR=6.9
935 0 my $self = shift;
936 0 my $ra = $self->query_options("ra");
937 0 my %allow = $self->_get_allowed_options();
938  
939 # need colons
940 0 $ra =~ s/\s+/:/g;
941  
942 0 return ($allow{ra},$ra);
943 }
944  
945 =item B<_translate_one_to_one>
946  
947 Return a list of internal options (as defined in C<_get_allowed_options>)
948 that are known to support a one-to-one mapping of the internal value
949 to the external value.
950  
951 %one = $q->_translate_one_to_one();
952  
953 Returns a hash with keys and no values (this makes it easy to
954 check for the option).
955  
956 This method also returns, the values from the parent class.
957  
958 =cut
959  
960 0 ▶sub _translate_one_to_one {
Subroutine _translate_one_to_one: not called CC=1 SCAR=6.9
961 0 my $self = shift;
962 # convert to a hash-list
963 return ($self->SUPER::_translate_one_to_one,
964 0 map { $_, undef }(qw/
965 cond
966 /)
967 );
968 }
969  
970 1 1;
971  
972 __END__
« Astro/Catalog/IO/SExtractor.pm ↑ Summary Astro/Catalog/IO/TST.pm »