Astro/Catalog/IO/Simple.pm

filter: Statementstmt 89.8% 88 / 98 Branchbran 63.8% 23 / 36 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 83.8% 125 / 149 CC 23 SCAR 23.4
CC
23
Cov
83%
_read_catalog
17.1
_write_catalog
9.7
_default_file
2.0
CRAP 10.4
« Astro/Catalog/IO/Astrom.pm ↑ Summary Astro/Catalog/IO/RITMatch.pm »
1 package Astro::Catalog::IO::Simple;
2  
3 =head1 NAME
4  
5 Astro::Catalog::IO::Simple - Simple Input/Output format
6  
7 =head1 SYNOPSIS
8  
9 $catalog = Astro::Catalog::IO::Simple->_read_catalog(\@lines);
10 $lines = Astro::Catalog::IO::Simple->_write_catalog($catalog);
11 Astro::Catalog::IO::Simple->_default_file();
12  
13 =head1 DESCRIPTION
14  
15 Performs simple IO, reading or writing "id_string hh mm ss.s +dd mm ss.s"
16 formated strings for each Astro::Catalog::Item object in the catalog.
17  
18 =cut
19  
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Astro::Coords;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 ▶use base qw/Astro::Catalog::IO::ASCII/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 our $VERSION = '4.38';
34  
35 =begin __PRIVATE_METHODS__
36  
37 =head1 Private methods
38  
39 These methods are for internal use only and are called from the
40 Astro::Catalog module. It is not expected that anyone would want to
41 call them from outside that module.
42  
43 =over 4
44  
45 =item B<_read_catalog>
46  
47 Parses a reference to an array containing a simply formatted catalog
48  
49 $catalog = Astro::Catalog::IO::Simple->_read_catalog(\@lines);
50  
51 =cut
52  
53 3 ▶sub _read_catalog {
Subroutine _read_catalog: called CC=15 SCAR=28.4
54 3 ▶ croak('Usage: _read_catalog(\@lines)') unless scalar(@_) >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ >= 1 0 3 3
55 3 my $class = shift;
56 3 my $arg = shift;
57 3 my @lines = @{$arg};
58  
59 # create an Astro::Catalog object;
60 3 my $catalog = new Astro::Catalog();
61  
62 # loop through lines
63 MAINLOOP:
64 3 foreach my $i ( 0 .. $#lines ) {
65  
66 # Skip commented and blank lines
67 24 ▶ next if ($lines[$i] =~ /^\s*[\#\*\%]/);
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$i] =~ /^\s*[\#\*\%]/ 9 15 24
68 15 ▶ next if ($lines[$i] =~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$i] =~ /^\s*$/ 0 15 15
69  
70 # temporary star object
71 15 my $star = new Astro::Catalog::Item();
72  
73 # Use a pattern match parser
74 15 my @match = ( $lines[$i] =~ m/
75 ^(.*?) # Target name (non greedy)
76 \s* # optional trailing space
77 (\d{1,2}) # 1 or 2 digits [RA:h] [greedy]
78 [:\s]+ # separator
79 (\d{1,2}) # 1 or 2 digits [RA:m]
80 [:\s]+ # separator
81 (\d{1,2}(?:\.\d*)?) # 1|2 digits opt .fraction [RA:s]
82 # no capture on fraction
83 [:\s]+
84 ([+-]?\s*\d{1,2}) # 1|2 digit [dec:d] inc sign
85 [:\s]+
86 (\d{1,2}) # 1|2 digit [dec:m]
87 [:\s]+
88 (\d{1,2}(?:\.\d*)?) # arcsecond (optional fraction)
89 # no capture on fraction
90 \s*
91 (RB|RJ|AZ|GA|AZEL|J2000|B1950|Galactic)? # coordinate type
92  
93 # most everything else is optional
94 \s*
95 (?:\#\s*(.*))?$ # comment [8]
96 /xi);
97  
98 # Abort if we do not have matches for the first 8 fields
99 # type is optional
100 15 for (0 ... 6) {
101 105 ▶ next MAINLOOP unless defined $match[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match[$_] 0 105 105
102 }
103  
104 # Read the values
105 15 my $target = $match[0];
106 15 my $ra = join(":", @match[1..3]);
107 15 my $dec = join(":", @match[4..6]);
108 15 $dec =~ s/\s//g; # remove space between the sign and number
109  
110 # Type defaults to J2000
111 15 ▶ my $type = $match[7] || "J2000";
Condition100%2/2
$match[7] || 'J2000'
l!l
132
MC/DC100%1/1
$match[7] || 'J2000'
$match[7]
Truth table100%2/2
A
A= $match[7]
Aresult
11
00
112 15 $type = uc($type);
113  
114 # Comment can be undefined
115 15 my $comment = $match[8];
116  
117 # push the target id
118 15 $star->id($target);
119  
120 # push the comment
121 15 ▶ $star->comment($comment) if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 9 6 15
122  
123 # Allow simple mapping of TYPE from JCMT abbreviations.
124 # This does not hurt things or break simplicity of the format.
125 # The form of the hash depends on the type
126 15 my %c;
127 15 ▶ if ($type =~ /(RB|RJ|J2000|B1950)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /(RB|RJ|J2000|B1950)/) { } 12 3 15
elsif ($type =~ /^(GA|SUPERGAL)/) { } 0 3 3
elsif ($type =~ /^AZ/) { } 3 0 3
128 # Standard RADEC
129 12 $c{ra} = $ra;
130 12 $c{dec} = $dec;
131  
132 12 ▶ if ($type =~ /B/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /B/) { } 1 11 12
133 1 $c{type} = "B1950";
134 }
135 else {
136 11 $c{type} = "J2000";
137 }
138 }
139 elsif ($type =~ /^(GA|SUPERGAL)/) {
140 0 $c{long} = $ra;
141 0 $c{lat} = $dec;
142 0 ▶ if ($type =~ /S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /S/) { } 0 0 0
143 0 $c{type} = "SUPERGALACTIC";
144 }
145 else {
146 0 $c{type} = "GALACTIC";
147 }
148 }
149 elsif ($type =~ /^AZ/) {
150 3 $c{az} = $ra;
151 3 $c{el} = $dec;
152 }
153 else {
154 0 croak "Unexpected coordinate type: $type\n";
155 }
156  
157 # Assume J2000 and create an Astro::Coords object
158 15 my $coords = new Astro::Coords(
159 units => 'sex',
160 name => $star->id(),
161 %c);
162  
163 15 ▶ croak "Error creating coordinate object from $ra / $dec "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $coords 0 15 15
164 unless defined $coords;
165  
166 # and push it into the Astro::Catalog::Item object
167 15 $star->coords($coords);
168  
169 # push it onto the stack
170 15 $catalog->pushstar($star);
171 }
172  
173 3 $catalog->origin('IO::Simple');
174 3 return $catalog;
175 }
176  
177 =item B<_write_catalog>
178  
179 Will write the catalog object to an simple output format
180  
181 $lines = Astro::Catalog::IO::Simple->_write_catalog($catalog);
182  
183 where $catalog is an Astro::Catalog object.
184  
185 =cut
186  
187 1 ▶sub _write_catalog {
Subroutine _write_catalog: called CC=9 SCAR=22.7
188 1 ▶ croak ( 'Usage: _write_catalog( $catalog )') unless scalar(@_) >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ >= 1 0 1 1
189 1 my $class = shift;
190 1 my $catalog = shift;
191  
192 # write header
193 1 my @output;
194 my $output_line;
195  
196 1 push (@output, "# Catalog written automatically by class ". __PACKAGE__ ."\n");
197 1 push (@output, "# on date " . gmtime . "UT\n" );
198 1 push (@output, "# Origin of catalog: ". $catalog->origin ."\n");
199  
200 # write body
201  
202 # Keep track of star count for unnamed stars
203 1 my $starcnt = 0;
204  
205 # loop through all the stars in the catalog
206 1 foreach my $star ($catalog->stars) {
207 # Extract the information into an array for later formatting
208 5 my @chunks;
209 my $comment; # in case we need to create a whole comment line
210  
211 # Star ID
212 5 ▶ push(@chunks, (defined $star->id() ? $star->id : $starcnt));
Branch: 1 branch
BranchTrueFalseTotal
defined $star->id ? : 5 0 5
213  
214 # Get the coordinate information
215 5 my $c = $star->coords;
216  
217 5 ▶ if (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c) { } 5 0 5
218 5 my $type = $c->type;
219  
220 5 ▶ if ($type eq 'RADEC') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'RADEC') { } 4 1 5
elsif ($type eq 'FIXED') { } 1 0 1
221 # Standard J2000
222 4 push(@chunks, $c->ra(format => 's'), $c->dec(format => 's'), "J2000");
223 }
224 elsif ($type eq 'FIXED') {
225 1 push(@chunks, $c->az(format => 's'), $c->el(format => 's'), "AZEL");
226 }
227 else {
228 0 $comment = "Can not represent star $chunks[0] in catalog since it is of type $type";
229 }
230  
231 }
232 else {
233 0 $comment = "Star $chunks[0] has no coordinates.";
234 }
235  
236 # now the line. If we have comment set this is just a comment line
237 5 ▶ if (defined $comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) { } 0 5 5
238 0 push(@output, "# $comment");
239 }
240 else {
241 5 my $cmt = '';
242 5 ▶ $cmt = " # " .$star->comment()
Branch: 1 branch
BranchTrueFalseTotal
if $star->comment 3 2 5
243 if $star->comment();
244  
245 5 push(@output, sprintf("%-15s %s %s %s", @chunks) . $cmt);
246 }
247 }
248  
249 1 return \@output;
250 }
251  
252 =item B<_default_file>
253  
254 If Astro::Catalog is created with a Format but no Filename or other data
255 source it checked this routine to see whether there is a default file
256 that should be read. This is mainly for Astro::Catalo::IO::JCMT and the
257 JAC, but might prive useful elsewhere.
258  
259 =cut
260  
261 0 ▶sub _default_file {
Subroutine _default_file: not called CC=1 SCAR=6.9
262 # returns an empty list
263 0 return;
264 }
265  
266 2 1;
267  
268 __END__
« Astro/Catalog/IO/Astrom.pm ↑ Summary Astro/Catalog/IO/RITMatch.pm »