Astro/Catalog/IO/Hedwig.pm

filter: Statementstmt 26.8% 32 / 119 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 76.9% 10 / 13 Podpod n/a totaltotal 21.0% 42 / 200 CC 32 SCAR 54.0
CC
32
Cov
18%
_read_catalog
342.0
_write_catalog
240.0
_get_csv_parser
2.0
CRAP 222.0
« Astro/Catalog/IO/Binary.pm ↑ Summary Astro/Catalog.pm »
1 package Astro::Catalog::IO::Hedwig;
2  
3 =head1 NAME
4  
5 Astro::Catalog::IO::Hedwig - Hedwig format catalog parser
6  
7 =cut
8  
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Astro::Telescope;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Astro::Coords;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use parent qw/Astro::Catalog::IO::ASCII/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our $VERSION = '4.38';
23  
24 =head1 METHODS
25  
26 =over 4
27  
28 =item B<_read_catalog>
29  
30 Parses the catalog lines and returns a new C<Astro::Catalog> object.
31  
32 $cat = Astro::Catalog::IO::Hedwig->_read_catalog(\@lines, %options);
33  
34 Options:
35  
36 =over 4
37  
38 =item telescope
39  
40 Name of telescope to associate with each entry.
41  
42 =back
43  
44 =cut
45  
46 0 ▶sub _read_catalog {
Subroutine _read_catalog: not called CC=18 SCAR=58.3
47 0 my $class = shift;
48 0 my $lines = shift;
49  
50 0 my %options = @_;
51  
52 0 ▶ croak "_read_catalog: catalog lines must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $lines 0 0 0
53 unless 'ARRAY' eq ref $lines;
54  
55 0 my $tel = undef;
56 $tel = new Astro::Telescope($options{'telescope'})
57 0 ▶ if defined $options{'telescope'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"telescope"} 0 0 0
58  
59 0 my $pattern_decimal = qr/^[-+]?\d*(?:\.\d*)?$/;
60 0 my $pattern_sexagesimal = qr/^[-+]?[\d.:]+$/;
61  
62 # Ideally would sniff the CSV format from the first line, allowing ' \t,;'
63 # delimiters but for now use default settings.
64 0 my $csv = _get_csv_parser();
65  
66 0 my @items;
67 0 foreach my $line (@$lines) {
68 0 $line =~ s/^\s*//;
69 0 $line =~ s/\s*$//;
70 0 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
71  
72 0 ▶ $csv->parse($line)
Branch: 1 branch
BranchTrueFalseTotal
$csv->parse($line) or croak("Unable to parse CSV line '${line}'") 0 0 0
73 or croak "Unable to parse CSV line '$line'";
74  
75 0 my ($name, $x, $y, $system, $time, $priority, $comment)
76 = $csv->fields();
77  
78 0 my %opt = (
79 name => $name,
80 );
81  
82 0 my %misc = ();
83  
84 # Detect units: decimal or sexagesimal.
85  
86 0 my $x_dec = 0;
87 0 ▶ if ($x =~ $pattern_decimal) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x =~ /$pattern_decimal/) { } 0 0 0
elsif (not $x =~ /$pattern_sexagesimal/) { } 0 0 0
88 0 $x_dec = 1;
89 }
90 elsif ($x !~ $pattern_sexagesimal) {
91 0 die "Did not recognize format of number '$x'";
92 }
93  
94 0 my $y_dec = 0;
95 0 ▶ if ($y =~ $pattern_decimal) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y =~ /$pattern_decimal/) { } 0 0 0
elsif (not $y =~ /$pattern_sexagesimal/) { } 0 0 0
96 0 $y_dec = 1;
97 }
98 elsif ($y !~ $pattern_sexagesimal) {
99 0 die "Did not recognize format of number '$y'";
100 }
101  
102 0 ▶ if ($x_dec xor $y_dec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x_dec xor $y_dec) 0 0 0
Condition0%0/4
$x_dec xor $y_dec
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$x_dec xor $y_dec
$x_dec $y_dec
Truth table0%0/4
A xor B
A= $x_decB= $y_dec
ABresult
110
101
011
000
103 0 die "Coordinates seem to have inconsistent number formats: '$x' '$y'";
104 }
105  
106 # Hedwig assumes decimals are degrees, otherwise (hour angle, degree)
107 # for ICRS, (degree, degree) for Galactic -- Astro::Coords should do
108 # this automatically when specifying 'sexagesimal'.
109 0 ▶ $opt{'units'} = $x_dec ? 'degrees' : 'sexagesimal';
Branch: 1 branch
BranchTrueFalseTotal
$x_dec ? : 0 0 0
110  
111 0 ▶ if ('ICRS' eq uc $system) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ICRS' eq uc $system) { } 0 0 0
elsif ('GALACTIC' eq uc $system) { } 0 0 0
112 0 $opt{'type'} = 'J2000';
113 0 $opt{'ra'} = $x;
114 0 $opt{'dec'} = $y;
115 }
116 elsif ('GALACTIC' eq uc $system) {
117 0 $opt{'type'} = 'galactic';
118 0 $opt{'long'} = $x;
119 0 $opt{'lat'} = $y;
120 }
121 else {
122 0 die "Did not recognize coordinate system '$system'";
123 }
124  
125 0 ▶ if (defined $priority) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $priority) 0 0 0
126 0 $misc{'priority'} = 0 + $priority;
127 }
128 0 ▶ if (defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) 0 0 0
129 0 $misc{'time_hours'} = 0.0 + $time;
130 }
131  
132 0 my $c = new Astro::Coords(%opt);
133  
134 0 ▶ $c->telescope($tel) if defined $tel;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tel 0 0 0
135  
136 0 push @items, new Astro::Catalog::Item(
137 id => $name,
138 coords => $c,
139 misc => \%misc,
140 comment => $comment,
141 );
142 }
143  
144 0 return new Astro::Catalog(Stars => \@items);
145 }
146  
147 =item B<_write_catalog>
148  
149 Write the catalog to an array of lines.
150  
151 $lines = Astro::Catalog::IO::Hedwig->_write_catalog($cat);
152  
153 =cut
154  
155 0 ▶sub _write_catalog {
Subroutine _write_catalog: not called CC=15 SCAR=54.8
156 0 my $class = shift;
157 0 my $cat = shift;
158  
159 0 my $csv = _get_csv_parser();
160  
161 0 my @lines;
162 0 for my $item ($cat->stars) {
163 0 my $coords = $item->coords;
164 0 my $name = $coords->name;
165 0 my $type = $coords->type;
166  
167 0 ▶ if ($type eq 'RADEC') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'RADEC') { } 0 0 0
168 0 my ($x, $y, $type);
169 0 ▶ unless ('glonglat' eq $coords->native) {
Branch: 1 branch
BranchTrueFalseTotal
if ('glonglat' eq $coords->native) { } 0 0 0
170 0 $type = 'ICRS';
171 0 $x = $coords->ra(format => 'sexagesimal');
172 0 $y = $coords->dec(format => 'sexagesimal');
173 }
174 else {
175 0 $type = 'Galactic';
176 0 my ($lon, $lat) = $coords->glonglat();
177 0 $x = sprintf('%.6f', $lon->degrees());
178 0 $y = sprintf('%.6f', $lat->degrees());
179 # After formatting to 6 DP to avoid rounding errors, trim
180 # trailing zeros and decimal place.
181 0 ▶ $x =~ s/0+$// if $x =~ /\./; $x =~ s/\.$//;
Branch: 1 branch
BranchTrueFalseTotal
if $x =~ /\./ 0 0 0
182 0 ▶ $y =~ s/0+$// if $y =~ /\./; $y =~ s/\.$//;
Branch: 1 branch
BranchTrueFalseTotal
if $y =~ /\./ 0 0 0
183 }
184  
185 0 my @extra = ();
186 0 my $comment = $item->comment();
187 0 ▶ if (defined $comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) 0 0 0
188 0 unshift @extra, $comment;
189 }
190  
191 0 my $misc = $item->misc();
192 0 ▶ if (defined $misc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $misc) 0 0 0
193 0 ▶ if (exists $misc->{'priority'} and defined $misc->{'priority'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $misc->{'priority'} and defined $misc->{'priority'}) { } 0 0 0
elsif (@extra) { } 0 0 0
Condition0%0/3
exists $misc->{"priority"} and defined $misc->{"priority"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $misc->{"priority"} and defined $misc->{"priority"}
exists $misc->{"priority"} defined $misc->{"priority"}
Truth table0%0/3
A and B
A= exists $misc->{"priority"}B= defined $misc->{"priority"}
ABresult
0X0
100
111
194 0 unshift @extra, $misc->{'priority'};
195 }
196 elsif (@extra) {
197 0 unshift @extra, undef;
198 }
199 0 ▶ if (exists $misc->{'time_hours'} and defined $misc->{'time_hours'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $misc->{'time_hours'} and defined $misc->{'time_hours'}) { } 0 0 0
elsif (@extra) { } 0 0 0
Condition0%0/3
exists $misc->{"time_hours"} and defined $misc->{"time_hours"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $misc->{"time_hours"} and defined $misc->{"time_hours"}
exists $misc->{"time_hours"} defined $misc->{"time_hours"}
Truth table0%0/3
A and B
A= exists $misc->{"time_hours"}B= defined $misc->{"time_hours"}
ABresult
0X0
100
111
200 0 unshift @extra, $misc->{'time_hours'};
201 }
202 elsif (@extra) {
203 0 unshift @extra, undef;
204 }
205 }
206  
207 # Remove excess spaces because Hedwig format (CSV-based)
208 # forbids duplicated separators.
209 0 $x =~ s/^\s//;
210 0 $y =~ s/^\s//;
211  
212 # Remove problematic characters from name.
213 0 $name =~ s/[^- !#$%&'()*+.\/0-9:;<=>?\@A-Z[\]^_`a-z{|}~]/_/g;
214  
215 0 ▶ $csv->combine($name, $x, $y, $type, @extra)
Branch: 1 branch
BranchTrueFalseTotal
$csv->combine($name, $x, $y, $type, @extra) or croak("Unable to prepare CSV line for '${name}'") 0 0 0
216 or croak "Unable to prepare CSV line for '$name'";
217  
218 0 push @lines, $csv->string();
219 }
220 else {
221 0 warnings::warnif "Coordinate of type '$type' for target '$name' not currently supported\n";
222 }
223 }
224  
225 0 return \@lines;
226 }
227  
228 =item B<_get_csv_parser>
229  
230 Constructs an instance of the Text::CSV class.
231  
232 =cut
233  
234 0 ▶sub _get_csv_parser {
Subroutine _get_csv_parser: not called CC=1 SCAR=6.9
235 0 return new Text::CSV({
236 sep_char => ' ',
237 quote_char => '"',
238 blank_is_undef => 1,
239 empty_is_undef => 1,
240 });
241 }
242  
243 1 1;
244  
245 __END__
« Astro/Catalog/IO/Binary.pm ↑ Summary Astro/Catalog.pm »