Astro/Catalog/IO/JCMT.pm

filter: Statementstmt 76.7% 211 / 275 Branchbran 52.4% 64 / 122 Conditioncond 57.8% 22 / 38 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 93.7% 15 / 16 Podpod 100% 1 / 1 totaltotal 67.5% 320 / 474 CC 82 SCAR 40.6
CC
82
Cov
68%
_write_catalog
104.5
_parse_line
55.5
_default_file
20.0
CRAP 57.9
« Astro/Catalog/Query/USNOA2.pm ↑ Summary Astro/Catalog/IO/VEX.pm »
1 package Astro::Catalog::IO::JCMT;
2  
3 =head1 NAME
4  
5 Astro::Catalog::IO::JCMT - JCMT catalog I/O for Astro::Catalog
6  
7 =head1 SYNOPSIS
8  
9 $cat = Astro::Catalog::IO::JCMT->_read_catalog(\@lines);
10 $arrref = Astro::Catalog::IO::JCMT->_write_catalog($cat, %options);
11 $filename = Astro::Catalog::IO::JCMT->_default_file();
12  
13 =head1 DESCRIPTION
14  
15 This class provides read and write methods for catalogs in the JCMT
16 pointing catalog format. The methods are not public and should, in general,
17 only be called from the C<Astro::Catalog> C<write_catalog> and C<read_catalog>
18 methods.
19  
20 =cut
21  
22 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 3 ▶use Astro::Telescope;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Astro::Coords;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 ▶use base qw/Astro::Catalog::IO::ASCII/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 3 our $VERSION = '4.38';
35 3 our $DEBUG = 0;
36  
37 # Name must be limited to 15 characters on write
38 3 ▶use constant MAX_SRC_LENGTH => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # Default location for a JCMT catalog
41 3 my $defaultCatalog = "/local/progs/etc/poi.dat";
42  
43 # Planets appended to the catalog
44 3 my @PLANETS = qw/mercury mars uranus saturn jupiter venus neptune/;
45  
46 =over 4
47  
48 =item B<clean_target_name>
49  
50 Method to take a general target name and clean it up
51 so that it is suitable for writing in a JCMT source catalog.
52 This routine is used by the catalog writing code but can also
53 be used publically in order to make sure that a target name
54 to be written to the catalog is guaranteed to match that used
55 in another location (e.g. when writing an a document to accompany
56 the catalog which refers to targets within it).
57  
58 The source name can be truncated.
59  
60 $cleaned = Astro::Catalog::IO::JCMT->clean_target_name($dirty);
61  
62 Will return undef if the argument is not defined.
63  
64 Punctuation such as "," and ";" are replaced with underscores.
65 ".", "()" and "+-" are allowed.
66  
67 =cut
68  
69 782 ▶sub clean_target_name {
Subroutine clean_target_name: called CC=2 SCAR=7.0
Pod documented
70 782 my $class = shift;
71 782 my $dirty = shift;
72 782 ▶ return unless defined $dirty;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dirty 0 782 782
73  
74 # Remove spaces [compress]
75 782 $dirty =~ s/\s+//g;
76  
77 # Remove disallowed characters
78 # and replace with dashes
79 782 $dirty =~ s/[,;:'"`]/-/g;
80  
81 # Truncate it to the allowed length
82 # Name must be limited to MAX_SRC_LENGTH characters
83 782 $dirty = substr($dirty, 0, MAX_SRC_LENGTH);
84  
85 # Return the cleaned name
86 782 return $dirty;
87 }
88  
89  
90 =item B<_default_file>
91  
92 Returns the location of the default JCMT pointing catalog at the
93 JCMT itself. This is purely for convenience of the caller when they
94 are at the JCMT and wish to use the default catalog without having
95 to know explicitly where it is.
96  
97 $filename = Astro::Catalog::IO::JCMT->_default_file();
98  
99 Returns empty list/undef if the file is not available.
100  
101 If the environment variable ASTRO_CATALOG_JCMT is defined (and exists)
102 this will be used as the default.
103  
104 =cut
105  
106 0 ▶sub _default_file {
Subroutine _default_file: not called CC=4 SCAR=30.0
107 0 my $class = shift;
108 return $ENV{ASTRO_CATALOG_JCMT}
109 0 ▶ if (exists $ENV{ASTRO_CATALOG_JCMT} && -e $ENV{ASTRO_CATALOG_JCMT});
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"ASTRO_CATALOG_JCMT"} and -e $ENV{"ASTRO_CATALOG_JCMT"} 0 0 0
Condition0%0/3
exists $ENV{"ASTRO_CATALOG_JCMT"} and -e $ENV{"ASTRO_CATALOG_JCMT"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"ASTRO_CATALOG_JCMT"} and -e $ENV{"ASTRO_CATALOG_JCMT"}
exists $ENV{"ASTRO_CATALOG_JCMT"} -e $ENV{"ASTRO_CATALOG_JCMT"}
Truth table0%0/3
A and B
A= exists $ENV{"ASTRO_CATALOG_JCMT"}B= -e $ENV{"ASTRO_CATALOG_JCMT"}
ABresult
0X0
100
111
110 0 ▶ return (-e $defaultCatalog ? $defaultCatalog : ());
Branch: 1 branch
BranchTrueFalseTotal
-e $defaultCatalog ? : 0 0 0
111 }
112  
113 =item B<_read_catalog>
114  
115 Parses the catalog lines and returns a new C<Astro::Catalog>
116 object containing the catalog entries.
117  
118 $cat = Astro::Catalog::IO::JCMT->_read_catalog(\@lines, %options);
119  
120 Supported options (with defaults) are:
121  
122 telescope => Name of telescope to associate with each coordinate entry
123 (defaults to JCMT). If the telescope option is specified
124 but is undef or empty string, no telescope is used.
125  
126 incplanets => Append planets to catalog entries (default is true)
127  
128 inccomments => Read comment lines into misc entries (default: false)
129  
130 =cut
131  
132 3 ▶sub _read_catalog {
Subroutine _read_catalog: called CC=10 SCAR=24.4
133 3 my $class = shift;
134 3 my $lines = shift;
135  
136 # Default options
137 3 my %defaults = (
138 telescope => 'JCMT',
139 incplanets => 1,
140 inccomments => 0,
141 respacecomments => 1,
142 );
143  
144 3 my %options = (%defaults, @_);
145  
146 3 ▶ croak "Must supply catalog contents as a reference to an array"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $lines eq "ARRAY" 0 3 3
147 unless ref($lines) eq 'ARRAY';
148  
149 # Create a new telescope to associate with this
150 3 my $tel;
151 $tel = new Astro::Telescope($options{telescope})
152 3 ▶ if $options{telescope};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"telescope"} 3 0 3
153  
154 # Go through each line and parse it
155 # and store in the array if we had a successful read
156 3 ▶ my $parse_buff = $options{'inccomments'} ? {} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$options{'inccomments'} ? : 0 3 3
157 3 my @stars = map {$class->_parse_line($_, $tel, $parse_buff, \%options);} @$lines;
158  
159 $stars[-1]->misc->{'_jcmt_com_after'} = delete $parse_buff->{'comments'}
160 if ((defined $parse_buff)
161 and (scalar @stars)
162 3 ▶ and (exists $parse_buff->{'comments'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $parse_buff and scalar @stars and exists $parse_buff->{"comments"} 0 3 3
Condition40%2/5
defined $parse_buff and scalar @stars and exists $parse_buff->{"comments"}
!ll&&!rl&&r
300
defined $parse_buff and scalar @stars
!ll
30
MC/DC0%0/2
defined $parse_buff and scalar @stars and exists $parse_buff->{"comments"}
defined $parse_buff exists $parse_buff->{"comments"}
Truth table33%1/3
A and B
A= defined $parse_buffB= exists $parse_buff->{"comments"}
ABresult
0X0
100
111
163  
164 # Add planets if required
165 3 ▶ if ($options{incplanets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"incplanets"}) 3 0 3
166 # create coordinate objects for the planets
167 3 my @planets = map {new Astro::Coords(planet => $_)} @PLANETS;
168  
169 # And associate a telescope
170 3 ▶ if ($tel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tel) 3 0 3
171 3 for (@planets) {
172 21 $_->telescope($tel);
173 }
174 }
175  
176 # And create the star objects
177 3 push(@stars, map {new Astro::Catalog::Item(
178 field => 'JCMT',
179 id => $_->name,
180 coords => $_,
181 comment => 'Added automatically',
182 )} @planets);
183 }
184  
185 # Create the catalog object
186 3 return new Astro::Catalog(
187 Stars => \@stars,
188 Origin => 'JCMT',
189 );
190 }
191  
192 =item B<_write_catalog>
193  
194 Write the catalog to an array and return it. Returning a reference to
195 an array provides more flexibility to the caller.
196  
197 $ref = Astro::Catalog::IO::JCMT->_write_catalog($cat, %options);
198  
199 Spaces are removed from source names. The contents of the catalog
200 are sanity checked.
201  
202 Supported options (with defaults) are:
203  
204 =over 4
205  
206 =item incheader
207  
208 Add a comment header to the start of the catalog. [default: true]
209  
210 =item removeduplicates
211  
212 Check for duplicates. Remove if the coordinates match. Add suffix
213 to disambiguate otherwise. [default: true]
214  
215 =back
216  
217 =cut
218  
219 2 ▶sub _write_catalog {
Subroutine _write_catalog: called CC=35 SCAR=46.5
220 2 my $class = shift;
221 2 my $cat = shift;
222  
223 # Default options
224 2 my %defaults = (
225 incheader => 1,
226 removeduplicates => 1,
227 );
228  
229 2 my %options = (%defaults, @_);
230  
231 # Would make more sense to use the array ref here
232 2 my @sources = $cat->stars;
233  
234 # Counter for unknown targets
235 2 my $unk = 1;
236  
237 # Hash for storing target information
238 # so that we can search for duplicates
239 2 my %targets;
240  
241 # Create hash of all unique target names present
242 # after cleaning. We need this so that we can make sure
243 # a generated name derived from a duplication (with target mismatch)
244 # does not generate a name that already existed explicitly.
245 2 my %allnames = map {$class->clean_target_name($_->coords->name), undef}
246 @sources;
247  
248 # Loop over each source and extract catalog information
249 # Make sure that we remove unique entries
250 # BUT THAT WE RETAIN THE ORDER OF THE SOURCES IN THE CATALOG
251 # Hence an array for the information
252 2 my @processed;
253 2 for my $star (@sources) {
254 # Extract the coordinate object
255 391 my $src = $star->coords;
256  
257 # Get the name but do not deal with undef yet
258 # in case the type is not valid
259 391 my $name = $src->name;
260  
261 # Somewhere to store the extracted information
262 391 my %srcdata;
263  
264 # Store the name (stripped of spaces) and
265 # treat srcdata{name} as the primary name from here on
266 391 $srcdata{name} = $class->clean_target_name($name);
267  
268 # Store a comment
269 391 $srcdata{comment} = $star->comment;
270  
271 # prepopulate the default velocity settings
272 391 $srcdata{rv} = 'n/a';
273 391 $srcdata{vdefn} = 'RADIO';
274 391 $srcdata{vframe} = 'LSR';
275  
276 # Default proper motion and parallax.
277 391 $srcdata{'pm1'} = 'n/a';
278 391 $srcdata{'pm2'} = 'n/a';
279 391 $srcdata{'parallax'} = 'n/a';
280  
281 # Get the miscellaneous data.
282 391 my $misc = $star->misc;
283 391 ▶ if (defined $misc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $misc) { } 377 14 391
284 $srcdata{vrange} = ((defined $misc->{'velocity_range'})
285 377 ▶ ? sprintf("%s", $misc->{'velocity_range'})
Branch: 1 branch
BranchTrueFalseTotal
defined $misc->{'velocity_range'} ? : 129 248 377
286 : "n/a");
287 $srcdata{flux850} = ((defined $misc->{'flux850'})
288 377 ▶ ? sprintf("%s", $misc->{'flux850'})
Branch: 1 branch
BranchTrueFalseTotal
defined $misc->{'flux850'} ? : 373 4 377
289 : "n/a" );
290 }
291 else {
292 14 $srcdata{vrange} = "n/a";
293 14 $srcdata{flux850} = "n/a";
294 }
295  
296 391 foreach (qw/_jcmt_com_before _jcmt_com_after/) {
297 782 ▶ $srcdata{$_} = $misc->{$_} if exists $misc->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $misc->{$_} 0 782 782
298 }
299  
300 # Get the type of source
301 391 my $type = $src->type;
302 391 ▶ if ($type eq 'RADEC') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'RADEC') { } 376 15 391
elsif ($type eq 'PLANET') { } 14 1 15
elsif ($type eq 'FIXED') { } 1 0 1
303 376 $srcdata{system} = "RJ";
304  
305 # Need to get the space separated RA/Dec and the sign
306 376 $srcdata{long} = $src->ra2000(format => 'array');
307 376 $srcdata{lat} = $src->dec2000(format => 'array');
308  
309 # Get the velocity information
310 376 my $rv = $src->rv;
311 376 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 154 222 376
312 154 $srcdata{rv} = $rv;
313 154 $srcdata{vdefn} = $src->vdefn;
314 154 $srcdata{vframe} = $src->vframe;
315  
316 # JCMT compatibility
317 154 ▶ $srcdata{vframe} = "LSR" if $srcdata{vframe} eq 'LSRK';
Branch: 1 branch
BranchTrueFalseTotal
if $srcdata{"vframe"} eq "LSRK" 154 0 154
318  
319 }
320  
321 376 my $parallax = $src->parallax;
322 376 my @pm = $src->pm;
323 376 ▶ if (scalar @pm) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pm) 0 376 376
324 0 ▶ if (not $parallax) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parallax) 0 0 0
325 0 ▶ my $errname = (defined $srcdata{name} ? $srcdata{name} : "<undefined>");
Branch: 1 branch
BranchTrueFalseTotal
defined $srcdata{'name'} ? : 0 0 0
326 0 warnings::warnif "Proper motion for target $errname specified without parallax";
327 }
328 0 $srcdata{'pm1'} = $pm[0] * 1000.0;
329 0 $srcdata{'pm2'} = $pm[1] * 1000.0;
330 }
331 376 ▶ if ($parallax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parallax) 0 376 376
332 0 $srcdata{'parallax'} = $parallax * 1000.0;
333 }
334  
335 }
336 elsif ($type eq 'PLANET') {
337 # Planets are not supported in catalog form. Skip them
338 14 next;
339  
340 }
341 elsif ($type eq 'FIXED') {
342 1 $srcdata{system} = "AZ";
343  
344 1 $srcdata{long} = $src->az(format => 'array');
345 1 $srcdata{lat} = $src->el(format => 'array');
346  
347 # Need to remove + sign from long/AZ since we are not expecting
348 # it in RA/DEC. This is probably a bug in Astro::Coords
349 1 ▶ shift(@{$srcdata{long}}) if $srcdata{long}->[0] eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $srcdata{"long"}[0] eq "+" 1 0 1
350  
351 }
352 else {
353 0 ▶ my $errname = (defined $srcdata{name} ? $srcdata{name} : "<undefined>");
Branch: 1 branch
BranchTrueFalseTotal
defined $srcdata{'name'} ? : 0 0 0
354 0 warnings::warnif "Coordinate of type $type for target $errname not supported in JCMT catalog files\n";
355 0 next;
356 }
357  
358 # Generate a name if not defined
359 377 ▶ if (!defined $srcdata{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $srcdata{"name"}) 0 377 377
360 0 $srcdata{name} = "UNKNOWN$unk";
361 0 $unk++;
362 }
363  
364 # See if we already have this source and that it is really the
365 # same source Note that we do not see whether this name is the
366 # same as one of the derived names. Eg if CRL618 is in the
367 # pointing catalog 3 times with identical coords and we add a
368 # new CRL618 with different coords then we trigger 3 warning
369 # messages rather than 1 because we do not check that CRL618_2 is
370 # the same as CRL618_1
371  
372 # Note that velocity specification is included in this comparison
373  
374 377 ▶ if ($options{'removeduplicates'} and exists $targets{$srcdata{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'removeduplicates'} and exists $targets{$srcdata{'name'}}) { } 0 377 377
Condition66%2/3
$options{"removeduplicates"} and exists $targets{$srcdata{"name"}}
!ll&&!rl&&r
37430
MC/DC0%0/2
$options{"removeduplicates"} and exists $targets{$srcdata{"name"}}
$options{"removeduplicates"} exists $targets{$srcdata{"name"}}
Truth table66%2/3
A and B
A= $options{"removeduplicates"}B= exists $targets{$srcdata{"name"}}
ABresult
0X0
100
111
375 0 my $previous = $targets{$srcdata{name}};
376  
377 # Create stringified form of previous coordinate with same name
378 # and current coordinate
379 0 my $prevcoords = join(" ",@{$previous->{long}},@{$previous->{lat}},
380 0 $previous->{rv}, $previous->{vdefn}, $previous->{vframe});
381 0 my $curcoords = join(" ",@{$srcdata{long}},@{$srcdata{lat}},
382 0 $srcdata{rv}, $srcdata{vdefn}, $srcdata{vframe});
383  
384 0 ▶ if ($prevcoords eq $curcoords) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevcoords eq $curcoords) { } 0 0 0
385 # This is the same target so we can ignore it
386 }
387 else {
388 # Make up a new name. Use a counter for this.
389 0 my $oldname = $srcdata{name};
390  
391 # loop for 100 times
392 0 my $count;
393 0 while (1) {
394 # protection loop
395 0 $count++;
396  
397 # Try to construct a new name based on the counter.
398 0 my $suffix = "_$count";
399  
400 # Abort if we have gone round too many times
401 0 ▶ if ($count > 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 100) 0 0 0
402 0 $srcdata{name} = substr($oldname, 0, int(MAX_SRC_LENGTH / 2)) .
403 int(rand(10000) + 1000);
404 0 warn "Uncontrollable looping (or unfeasibly large number of duplicate sources with different coordinates). Panicked and generated random source name of $srcdata{name}\n";
405 0 last;
406 }
407  
408 # Assume the old name will do fine
409 0 my $root = $oldname;
410  
411 # Do not want to truncate the _XX off the end later on
412 0 ▶ if (length($oldname) > MAX_SRC_LENGTH - length($suffix)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $oldname > 15 - length($suffix)) 0 0 0
413 # This may well be confusing but we have no choice. Since
414 # _XX is unique the only time we will get a name clash by
415 # simply chopping the string is if we have a duplicate
416 # that is too long along with a target name that includes
417 # _XX amd matches the truncated source name!
418 0 $root = substr($oldname, 0, (MAX_SRC_LENGTH - length($suffix)) );
419 }
420  
421 # Form the new name
422 0 my $newname = $root . $suffix;
423  
424 # check to see if this name is in the existing target list
425 0 ▶ unless ((exists $allnames{$newname}) or (exists $targets{$newname})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $allnames{$newname} or exists $targets{$newname}) 0 0 0
Condition0%0/3
exists $allnames{$newname} or exists $targets{$newname}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $allnames{$newname} or exists $targets{$newname}
exists $allnames{$newname} exists $targets{$newname}
Truth table0%0/3
A or B
A= exists $allnames{$newname}B= exists $targets{$newname}
ABresult
1X1
011
000
426 # Store it in the targets array and exit loop
427 0 $srcdata{name} = $newname;
428 0 last;
429 }
430 }
431  
432 # different target
433 0 warn "Found target with the same name [$oldname] but with different coordinates, renaming it to $srcdata{name}\n";
434  
435 0 $targets{$srcdata{name}} = \%srcdata;
436  
437 # Store it in the array
438 0 push @processed, \%srcdata;
439 }
440 }
441 else {
442 # Store in hash for easy lookup for duplicates
443 377 $targets{$srcdata{name}} = \%srcdata;
444  
445 # Store it in the array
446 377 push @processed, \%srcdata;
447 }
448 }
449  
450 # Output array for new catalog lines
451 2 my @lines;
452  
453 2 ▶ if ($options{'incheader'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"incheader"}) 2 0 2
454 # Write a header
455 2 push @lines, "*\n";
456 2 push @lines, "* Catalog written automatically by class ". __PACKAGE__ ."\n";
457 2 push @lines, "* on date " . gmtime . "UT\n";
458 2 push @lines, "* Origin of catalog: ". $cat->origin ."\n";
459 2 push @lines, "*\n";
460 }
461  
462 # Now need to go through the targets and write them to disk
463 2 for my $src (@processed) {
464 377 ▶ if (exists $src->{'_jcmt_com_before'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $src->{"_jcmt_com_before"}) 0 377 377
465 0 push @lines, '*' . $_ foreach @{$src->{'_jcmt_com_before'}};
466 }
467  
468 377 my $name = $src->{name};
469 377 my $long = $src->{long};
470 377 my $lat = $src->{lat};
471 377 my $system = $src->{system};
472 377 my $comment = $src->{comment};
473 377 my $rv = $src->{rv};
474 377 my $vdefn = $src->{vdefn};
475 377 my $vframe = $src->{vframe};
476 377 my $vrange = $src->{vrange};
477 377 my $flux850 = $src->{flux850};
478 377 my $pm1 = $src->{'pm1'};
479 377 my $pm2 = $src->{'pm2'};
480 377 my $px = $src->{'parallax'};
481  
482 377 ▶ $comment = '' unless defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $comment 0 377 377
483  
484 # Velocity can not easily be done with a sprintf since it can be either
485 # a string or a 2 column number
486 377 $rv = _format_value($rv, '%6.1f', ' n/a ', 1);
487  
488 # Similarly format proper motion and parallax.
489 377 $pm1 = _format_value($pm1, '%8.3f', ' n/a ', 1);
490 377 $pm2 = _format_value($pm2, '%8.3f', ' n/a ', 1);
491 377 $px = _format_value($px, '%8.4f', ' n/a ', 0);
492  
493 # Name must be limited to MAX_SRC_LENGTH characters
494 # [this should be taken care of by clean_target_name but
495 # if we have appended _X....
496 377 $name = substr($name,0,MAX_SRC_LENGTH);
497  
498 # Maybe shift flux by 1 space to align decimal point in
499 # 1dp values with that in 2dp values and also middle of n/a.
500 377 ▶ $flux850 .= ' ' if $flux850 =~ /(?:\.\d|n\/a)$/ and 5 > length $flux850;
Branch: 1 branch
BranchTrueFalseTotal
if $flux850 =~ m[(?:\.\d|n/a)$] and 5 > length $flux850 164 213 377
Condition66%2/3
$flux850 =~ m[(?:\.\d|n/a)$] and 5 > length $flux850
!ll&&!rl&&r
2130164
MC/DC50%1/2
$flux850 =~ m[(?:\.\d|n/a)$] and 5 > length $flux850
$flux850 =~ m[(?:\.\d|n/a)$] 5 > length $flux850
Truth table66%2/3
A and B
A= $flux850 =~ m[(?:\.\d|n/a)$]B= 5 > length $flux850
ABresult
0X0
100
111
501  
502 377 push @lines, sprintf(
503 "%-" . MAX_SRC_LENGTH . "s %02d %02d %06.3f %1s %02d %02d %05.2f %2s %s %5s %5s %-4s %s %s %s %s %s\n",
504 $name, @$long, @$lat, $system,
505 $rv, $flux850, $vrange, $vframe, $vdefn,
506 $pm1, $pm2, $px,
507 $comment);
508  
509 377 ▶ if (exists $src->{'_jcmt_com_after'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $src->{"_jcmt_com_after"}) 0 377 377
510 0 push @lines, '*' . $_ foreach @{$src->{'_jcmt_com_after'}};
511 }
512 }
513  
514 2 return \@lines;
515 }
516  
517 =item B<_format_value>
518  
519 Format a value for inclusion in a JCMT format catalog.
520  
521 =cut
522  
523 1508 ▶sub _format_value {
Subroutine _format_value: called CC=5 SCAR=17.1
524 1508 my ($val, $fmt, $na, $signed) = @_;
525  
526 1508 ▶ if (lc($val) eq 'n/a') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $val eq "n/a") 1354 154 1508
527 1354 return $na;
528 }
529  
530 154 ▶ my $sign = ($val >= 0 ? '+' : '-');
Branch: 1 branch
BranchTrueFalseTotal
$val >= 0 ? : 81 73 154
531 154 $val =~ s/^\s*[+-]\s*//;
532 154 $val =~ s/\s*$//;
533 154 ▶ unless ($signed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($signed) 0 154 154
534 0 ▶ warnings::warnif "Unsigned value is negative" unless $sign eq '+';
Branch: 1 branch
BranchTrueFalseTotal
unless $sign eq "+" 0 0 0
535 0 return sprintf($fmt, $val);
536 }
537 154 return $sign . ' ' . sprintf($fmt, $val);
538 }
539  
540 =item B<_parse_line>
541  
542 Parse a line from a JCMT format catalog and return a corresponding
543 C<Astro::Catalog::Item> object. Returns empty list if the line can not
544 be parsed or refers to a comment line (so that map can be used in the
545 caller).
546  
547 $star = Astro::Catalog::IO::JCMT->_parse_line($line, $tel, \%status_buffer, \%options);
548  
549 where C<$line> is the line to be parsed and (optional) C<$tel>
550 is an C<Astro::Telescope> object to be associated with the
551 coordinate objects.
552  
553 A reference to a hash can be provided to track parsing status between lines.
554 This is currently used to store comments read from the catalog.
555  
556 The line is parsed using a pattern match.
557  
558 =cut
559  
560 1070 ▶sub _parse_line {
Subroutine _parse_line: called CC=31 SCAR=40.2
561 1070 my $class = shift;
562 1070 my $line = shift;
563 1070 my $tel = shift;
564 1070 my $parse_buff = shift;
565 1070 my $options = shift;
566 1070 chomp $line;
567  
568 # Skip commented and blank lines
569 1070 ▶ return if ($line =~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 1070 1070
570 1070 ▶ if ($line =~ s/^\s*[\*\%]//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^\s*[\*\%]//) 319 751 1070
571 0 ▶ push @{$parse_buff->{'comments'}}, $line if $parse_buff;
Branch: 1 branch
BranchTrueFalseTotal
if $parse_buff 0 319 319
572 319 return;
573 }
574  
575 # Use a pattern match parser
576 751 my @match = ($line =~ m/
577 ^(.*?) # Target name (non greedy)
578 \s* # optional trailing space
579 (\d{1,2}) # 1 or 2 digits [RA:h] [greedy]
580 \s+ # separator
581 (\d{1,2}) # 1 or 2 digits [RA:m]
582 \s+ # separator
583 (\d{1,2}(?:\.\d*)?) # 1|2 digits opt .fraction [RA:s]
584 # no capture on fraction
585 \s+
586 ([+-]?\s*\d{1,2}) # 1|2 digit [dec:d] inc sign
587 \s+
588 (\d{1,2}) # 1|2 digit [dec:m]
589 \s+
590 (\d{1,2}(?:\.\d*)?) # arcsecond (optional fraction)
591 # no capture on fraction
592 \s+
593 (RJ|RB|GA|AZ) # coordinate type
594 # most everything else is optional
595 # [sign]velocity, flux,vrange,vel_def,frame,comments
596 \s*
597 (n\/a|[+-]\s*\d+(?:\.\d*)?)? # velocity [8]
598 \s*
599 (n\/a|\d+(?:\.\d*)?)? # flux [9]
600 \s*
601 (n\/a|\d+(?:\.\d*)?)? # vel range [10]
602 \s*
603 ([\w\/]+)? # vel frame [11]
604 \s*
605 ([\w\/]+)? # vel defn [12]
606 \s*
607 (n\/a|[+-]\s*\d+(?:\.\d*)?)? # pm1 [13]
608 \s*
609 (n\/a|[+-]\s*\d+(?:\.\d*)?)? # pm2 [14]
610 \s*
611 (n\/a|\d+(?:\.\d*)?)? # parallax [15]
612 \s*
613 (.*)$ # comment [16]
614 /xi);
615  
616 # Abort if we do not have matches for the first 8 fields
617 751 for (0..7) {
618 6008 ▶ return unless defined $match[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match[$_] 0 6008 6008
619 }
620  
621 # Read the values
622 751 my $target = $match[0];
623 751 my $ra = join ":", @match[1..3];
624 751 my $dec = join ":", @match[4..6];
625 751 $dec =~ s/\s//g; # remove space between the sign and number
626 751 my $epoc = $match[7];
627  
628 751 ▶ print "Creating a new source in _parse_line: $target\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::IO::JCMT::DEBUG 0 751 751
629  
630 # need to translate JCMT epoch to normal epoch
631 751 my %coords;
632 751 $epoc = uc($epoc);
633 751 $coords{name} = $target;
634 751 ▶ if ($epoc eq 'RJ') {
Branch: 4 branches
BranchTrueFalseTotal
if ($epoc eq 'RJ') { } 749 2 751
elsif ($epoc eq 'RB') { } 0 2 2
elsif ($epoc eq 'GA') { } 0 2 2
elsif ($epoc eq 'AZ') { } 2 0 2
635 749 $coords{ra} = $ra;
636 749 $coords{dec} = $dec;
637 749 $coords{type} = "j2000"
638 }
639 elsif ($epoc eq 'RB') {
640 0 $coords{ra} = $ra;
641 0 $coords{dec} = $dec;
642 0 $coords{type} = "b1950";
643 }
644 elsif ($epoc eq 'GA') {
645 0 $coords{long} = $ra;
646 0 $coords{lat} = $dec;
647 0 $coords{type} = "galactic";
648 }
649 elsif ($epoc eq 'AZ') {
650 2 $coords{az} = $ra;
651 2 $coords{el} = $dec;
652 2 $coords{units} = 'sexagesimal';
653 }
654 else {
655 0 warnings::warnif "Unknown coordinate type: '$epoc' for target $target. Ignoring line.";
656 0 return;
657 }
658  
659 # catalog comments are space delimited
660 751 my $ccol = 16;
661 751 ▶ my $cat_comm = (defined $match[$ccol] ? $match[$ccol] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $match[$ccol] ? : 751 0 751
662  
663 # Replace multiple spaces in comment with single space
664 751 ▶ $cat_comm =~ s/\s+/ /g if $options->{'respacecomments'};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"respacecomments"} 751 0 751
665  
666 # velocity
667 751 $coords{vdefn} = "RADIO";
668 751 $coords{vframe} = "LSR";
669 751 ▶ if (defined $match[8] && $match[8] !~ /n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match[8] and not $match[8] =~ /n/) 308 443 751
Condition100%3/3
defined $match[8] and not $match[8] =~ /n/
!ll&&!rl&&r
3440308
MC/DC100%2/2
defined $match[8] and not $match[8] =~ /n/
defined $match[8] not $match[8] =~ /n/
Truth table100%3/3
A and not(B)
A= defined $match[8]B= not $match[8] =~ /n/
ABresult
0X0
100
111
670 308 $match[8] =~ s/\s//g; # remove spaces
671 308 $coords{rv} = $match[8];
672 308 $coords{vdefn} = $match[12];
673 308 $coords{vframe} = $match[11];
674 }
675  
676 751 ▶ if ((defined $match[13]) and (defined $match[14])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match[13] and defined $match[14] and not $match[13] =~ /n/ and not $match[14] =~ /n/) 0 751 751
Condition55%5/9
defined $match[13] and defined $match[14] and not $match[13] =~ /n/ and not $match[14] =~ /n/
!ll&&!rl&&r
75100
defined $match[13] and defined $match[14] and not $match[13] =~ /n/
!ll&&!rl&&r
3773740
defined $match[13] and defined $match[14]
!ll&&!rl&&r
3770374
MC/DC0%0/4
defined $match[13] and defined $match[14] and not $match[13] =~ /n/ and not $match[14] =~ /n/
defined $match[13] defined $match[14] not $match[13] =~ /n/ not $match[14] =~ /n/
Truth table40%2/5
A and B and not(C) and not(D)
A= defined $match[13]B= defined $match[14]C= not $match[13] =~ /n/D= not $match[14] =~ /n/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
677 and ($match[13] !~ /n/) and ($match[14] !~ /n/)) {
678 0 $match[13] =~ s/\s//g; # remove spaces
679 0 $match[14] =~ s/\s//g; # remove spaces
680 # Convert components of proper motion from mas/year to arcsec/year.
681 0 $coords{'pm'} = [$match[13] / 1000.0, $match[14] / 1000.0];
682 }
683  
684 751 ▶ if ((defined $match[15]) and ($match[15] !~ /n/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match[15] and not $match[15] =~ /n/) 0 751 751
Condition66%2/3
defined $match[15] and not $match[15] =~ /n/
!ll&&!rl&&r
3773740
MC/DC0%0/2
defined $match[15] and not $match[15] =~ /n/
defined $match[15] not $match[15] =~ /n/
Truth table66%2/3
A and not(B)
A= defined $match[15]B= not $match[15] =~ /n/
ABresult
0X0
100
111
685 # Convert parallax from mas to arcsec.
686 0 $coords{'parallax'} = $match[15] / 1000.0;
687 }
688  
689 # create the source object
690 751 my $source = new Astro::Coords(%coords);
691  
692 751 ▶ unless (defined $source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source) 0 751 751
693 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Astro::Catalog::IO::JCMT::DEBUG) { } 0 0 0
694 0 print "failed to create source for '$target' and $ra and $dec and $epoc\n";
695 0 return;
696 }
697 else {
698 0 croak "Error parsing line. Unable to create source date for target '$target' at RA '$ra' Dec '$dec' and Epoch '$epoc'\n";
699 }
700 }
701  
702 751 ▶ $source->telescope($tel) if $tel;
Branch: 1 branch
BranchTrueFalseTotal
if $tel 751 0 751
703 751 $source->comment($cat_comm);
704  
705 # Field name should simply be linked to the telescope
706 751 ▶ my $field = (defined $tel ? $tel->name : '<UNKNOWN>');
Branch: 1 branch
BranchTrueFalseTotal
defined $tel ? : 751 0 751
707  
708 751 my %misc;
709 # Grab the line's velocity range, if it isn't "n/a".
710 751 ▶ if (defined $match[10] && $match[10] !~ /n\/a/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match[10] and not $match[10] =~ m[n/a]) 258 493 751
Condition100%3/3
defined $match[10] and not $match[10] =~ m[n/a]
!ll&&!rl&&r
3490258
MC/DC100%2/2
defined $match[10] and not $match[10] =~ m[n/a]
defined $match[10] not $match[10] =~ m[n/a]
Truth table100%3/3
A and not(B)
A= defined $match[10]B= not $match[10] =~ m[n/a]
ABresult
0X0
100
111
711 258 $misc{'velocity_range'} = $match[10];
712 }
713  
714 # Grab the 850-micron flux, if it isn't "n/a".
715 751 ▶ if (defined $match[9] && $match[9] !~ /n\/a/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match[9] and not $match[9] =~ m[n/a]) 746 5 751
Condition100%3/3
defined $match[9] and not $match[9] =~ m[n/a]
!ll&&!rl&&r
32746
MC/DC100%2/2
defined $match[9] and not $match[9] =~ m[n/a]
defined $match[9] not $match[9] =~ m[n/a]
Truth table100%3/3
A and not(B)
A= defined $match[9]B= not $match[9] =~ m[n/a]
ABresult
0X0
100
111
716 746 $misc{'flux850'} = $match[9];
717 }
718  
719 $misc{'_jcmt_com_before'} = delete $parse_buff->{'comments'}
720 751 ▶ if exists $parse_buff->{'comments'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parse_buff->{"comments"} 0 751 751
721  
722 751 ▶ print "Created a new source in _parse_line: $target in field $field\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::IO::JCMT::DEBUG 0 751 751
723  
724 # Now create the star object
725 751 return new Astro::Catalog::Item(
726 id => $target,
727 coords => $source,
728 field => $field,
729 comment => $cat_comm,
730 misc => \%misc,
731 );
732 }
733  
734 3 1;
735  
736 __END__
« Astro/Catalog/Query/USNOA2.pm ↑ Summary Astro/Catalog/IO/VEX.pm »