Astro/Catalog/IO/RITMatch.pm

filter: Statementstmt 93.3% 84 / 90 Branchbran 50.0% 12 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 79.8% 111 / 139 CC 18 SCAR 23.0
CC
18
Cov
80%
_write_catalog
17.9
_read_catalog
5.4
BEGIN
1.0
CRAP 9.9
« Astro/Catalog/IO/Simple.pm ↑ Summary Astro/Catalog/Query/Sesame.pm »
1 package Astro::Catalog::IO::RITMatch;
2  
3 =head1 NAME
4  
5 Astro::Catalog::IO::RITMatch - Catalog I/O for Astro::Catalog for
6 Michael Richmond's 'match' program
7  
8 =head1 SYNOPSIS
9  
10 $cat = Astro::Catalog::IO::RITMatch->_read_catalog(\@lines);
11 $arrref = Astro::Catalog::IO::RITMatch->_write_catalog($cat, %options);
12  
13 =head1 DESCRIPTION
14  
15 This class provides read and write methods for catalogs in Michael
16 Richmond's 'match' application's input and output file format. The
17 methods are not public and should, in general only be called from the
18 C<Astro::Catalog> C<write_catalog> and C<read_catalog> methods.
19  
20 =cut
21  
22 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use Astro::Flux;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Astro::Fluxes;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use Number::Uncertainty;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use base qw/Astro::Catalog::IO::ASCII/;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 our $VERSION = '4.38';
38 2 our $DEBUG = 0;
39  
40 =head1 METHODS
41  
42 =head2 Private methods
43  
44 =over 4
45  
46 =item B<_read_catalog>
47  
48 Parses the catalog lines and returns a new C<Astro::Catalog> object
49 containing the catalog entries.
50  
51 $cat = Astro::Catalog::IO::RITMatch->_read_catalog(\@lines, %options);
52  
53 Currently supported options are:
54  
55 =over 4
56  
57 =item filter
58  
59 Either an Astro::WaveBand object or a string that can
60 be used by the Filter method of the Astro::WaveBand module when
61 constructing a new object. This option describes the waveband for the
62 magnitudes in the catalog. If this is not defined, then the waveband
63 will default to the near infrared 'J' band.
64  
65 =back
66  
67 =cut
68  
69 2 ▶sub _read_catalog {
Subroutine _read_catalog: called CC=5 SCAR=16.9
70 2 my $class = shift;
71 2 my $lines = shift;
72  
73 2 ▶ if (ref($lines) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lines ne "ARRAY") 0 2 2
74 0 croak "Must supply catalog contents as a reference to an array";
75 }
76  
77 # Retrieve options.
78 2 my %options = @_;
79  
80 # Grab the filter.
81 2 my $filter;
82 2 ▶ if (defined $options{'filter'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'filter'}) { } 0 2 2
83 0 ▶ if (UNIVERSAL::isa($options{'filter'}, "Astro::WaveBand")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($options{'filter'}, 'Astro::WaveBand')) { } 0 0 0
84 0 $filter = $options{'filter'};
85 }
86 else {
87 0 $filter = new Astro::WaveBand(Filter => $options{'filter'});
88 }
89 }
90 else {
91 2 $filter = new Astro::WaveBand(Filter => 'J');
92 }
93  
94 # Create a new Astro::Catalog object.
95 2 my $catalog = new Astro::Catalog();
96  
97 # Go through the lines. The first column is going to be the ID, the
98 # second X, the third Y, and the fourth magnitude. Any columns after
99 # the first will be put into the comments() accessor of the
100 # Astro::Catalog::Item object.
101 2 my @lines = @$lines;
102 2 for (@lines) {
103 10 my $line = $_;
104  
105 10 $line =~ s/^\s+//;
106  
107 10 my ($id, $x, $y, $mag) = split /\s+/, $line, 4;
108  
109 # Create the Astro::Flux object for this magnitude.
110 10 my $flux = new Astro::Flux(
111 new Number::Uncertainty(Value => $mag),
112 'mag',
113 $filter );
114 10 my @mags;
115 10 push @mags, $flux;
116 10 my $fluxes = new Astro::Fluxes(@mags);
117  
118 # Create the Astro::Catalog::Item object.
119 10 my $item = new Astro::Catalog::Item(
120 ID => $id,
121 X => $x,
122 Y => $y,
123 Fluxes => $fluxes,
124 );
125  
126 # Push it into the catalog.
127 10 $catalog->pushstar($item);
128 }
129  
130 # Set the origin.
131 2 $catalog->origin('Astro::Catalog::IO::RITMatch');
132  
133 2 return $catalog;
134 }
135  
136 =item B<_write_catalog>
137  
138 Create an output catalog in the 'match' format and return the lines
139 in an array.
140  
141 $ref = Astro::Catalog::IO::RITMatch->_write_catalog($catalog, %options);
142  
143 The sole mandatory argument is an C<Astro::Catalog> object.
144  
145 Optional arguments are:
146  
147 =over 4
148  
149 =item mag_type
150  
151 the magnitude type to write out to the file. Defaults
152 to 'mag'.
153  
154 =item filter
155  
156 reference filter to select from catalog. See flux()
157 method in Astro::Flux. This allows a specific filter to be selected
158 for output. Argument can be a string or Astro::Waveband object.
159  
160 =back
161  
162 The output format has the ID in column 1, X coordinate in column 2, Y
163 coordinate in column 3, magnitude value in column 4, and any comments
164 in column 5.
165  
166 =cut
167  
168 1 ▶sub _write_catalog {
Subroutine _write_catalog: called CC=14 SCAR=28.9
169 1 my $class = shift;
170 1 my $catalog = shift;
171  
172 1 ▶ if (! UNIVERSAL::isa($catalog, "Astro::Catalog")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($catalog, "Astro::Catalog")) 0 1 1
173 0 croak "Must supply catalog as an Astro::Catalog object";
174 }
175  
176 # Get options.
177 1 my %options = @_;
178 1 my $mag_type = 'mag';
179 1 ▶ if (defined $options{'mag_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"mag_type"}) 0 1 1
180 0 $mag_type = $options{'mag_type'};
181 }
182  
183 1 my $refwb;
184 $refwb = $options{filter}
185 1 ▶ if exists $options{filter};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"filter"} 0 1 1
186  
187 # Set up variables for output.
188 1 my @output;
189 my $output_line;
190  
191 1 my $newid = 1;
192  
193 # Astro::Fluxes can not guarantee that the Astro::Flux
194 # order will be in a particular order so we can not simply
195 # grab the first one. We might get a "J" this time but previously
196 # got "R".
197  
198 # Loop through the items in the catalog.
199 1 foreach my $item ($catalog->stars) {
200 5 my $x = $item->x;
201 5 my $y = $item->y;
202 5 my $fluxes = $item->fluxes;
203  
204 # We need at a bare minimum the X, Y, ID, and a magnitude.
205 5 ▶ next if (! defined($newid) ||
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newid and defined $x and defined $y and defined $fluxes 0 5 5
Condition33%3/9
defined $newid and defined $x and defined $y and defined $fluxes
!ll&&!rl&&r
005
defined $newid and defined $x and defined $y
!ll&&!rl&&r
005
defined $newid and defined $x
!ll&&!rl&&r
005
MC/DC0%0/4
defined $newid and defined $x and defined $y and defined $fluxes
defined $newid defined $x defined $y defined $fluxes
Truth table20%1/5
A and B and C and D
A= defined $newidB= defined $xC= defined $yD= defined $fluxes
ABCDresult
0XXX0
10XX0
110X0
11100
11111
206 ! defined($x) ||
207 ! defined($y) ||
208 ! defined($fluxes));
209  
210 5 my $flux;
211 5 ▶ unless ($refwb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refwb) { } 4 1 5
212 # If no external selection of waveband and this is first time
213 # through loop we select the first waveband as a reference
214 1 my @allflux = $fluxes->allfluxes;
215 1 $flux = $allflux[0];
216 1 $refwb = $flux->waveband->natural;
217 }
218 else {
219 4 $flux = $fluxes->flux(type => $mag_type, waveband => $refwb);
220 }
221  
222 5 ▶ next unless defined $flux;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flux 0 5 5
223 5 ▶ next if uc($flux->type) ne uc($mag_type);
Branch: 1 branch
BranchTrueFalseTotal
if uc $flux->type ne uc $mag_type 0 5 5
224  
225 5 my $magval = $flux->quantity($mag_type);
226  
227 # Sometimes we get a missing value
228 5 ▶ next unless defined $magval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $magval 0 5 5
229 5 ▶ next unless length($magval);
Branch: 1 branch
BranchTrueFalseTotal
unless length $magval 0 5 5
230  
231 # Create the output string.
232 5 $output_line = join ' ',
233 $newid,
234 $x,
235 $y,
236 $magval,
237 $item->id;
238  
239 # And push this string to the output array.
240 5 push @output, $output_line;
241  
242 5 $newid++;
243 }
244  
245 1 return \@output;
246 }
247  
248 2 1;
249  
250 __END__
« Astro/Catalog/IO/Simple.pm ↑ Summary Astro/Catalog/Query/Sesame.pm »