File Coverage

blib/lib/Astro/Catalog/IO/RITMatch.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Astro::Catalog::IO::RITMatch;
2              
3             =head1 NAME
4              
5             Astro::Catalog::IO::RITMatch - Catalogue 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              
16             This class provides read and write methods for catalogues in Michael
17             Richmond's 'match' application's input and output file format. The
18             methods are not public and should, in general only be called from the
19             C C and C methods.
20              
21             =cut
22              
23 1     1   2474798 use 5.006;
  1         11  
  1         76  
24 1     1   13 use warnings;
  1         2  
  1         99  
25 1     1   60 use warnings::register;
  1         2  
  1         616  
26 1     1   7 use strict;
  1         2  
  1         64  
27              
28 1     1   6 use Carp;
  1         1  
  1         197  
29              
30 1     1   1183 use Astro::Catalog;
  0            
  0            
31             use Astro::Catalog::Item;
32              
33             use Astro::Flux;
34             use Astro::Fluxes;
35             use Number::Uncertainty;
36              
37             use base qw/ Astro::Catalog::IO::ASCII /;
38              
39             use vars qw/ $VERSION $DEBUG /;
40              
41             $VERSION = '4.31';
42             $DEBUG = 0;
43              
44             =head1 METHODS
45              
46             =head2 Private methods
47              
48             =over 4
49              
50             =item B<_read_catalog>
51              
52             Parses the catalogue lines and returns a new C object
53             containing the catalogue entries.
54              
55             $cat = Astro::Catalog::IO::RITMatch->_read_catalog( \@lines, %options );
56              
57             Currently supported options are:
58              
59             =item filter - 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 catalogue. If this is not defined, then the waveband
63             will default to the near infrared 'J' band.
64              
65             =cut
66              
67             sub _read_catalog {
68             my $class = shift;
69             my $lines = shift;
70              
71             if( ref( $lines ) ne 'ARRAY' ) {
72             croak "Must supply catalogue contents as a reference to an array";
73             }
74              
75             # Retrieve options.
76             my %options = @_;
77              
78             # Grab the filter.
79             my $filter;
80             if( defined( $options{'filter'} ) ) {
81             if( UNIVERSAL::isa( $options{'filter'}, "Astro::WaveBand" ) ) {
82             $filter = $options{'filter'};
83             } else {
84             $filter = new Astro::WaveBand( Filter => $options{'filter'} )
85             }
86             } else {
87             $filter = new Astro::WaveBand( Filter => 'J' );
88             }
89              
90             # Create a new Astro::Catalog object.
91             my $catalog = new Astro::Catalog();
92              
93             # Go through the lines. The first column is going to be the ID, the
94             # second X, the third Y, and the fourth magnitude. Any columns after
95             # the first will be put into the comments() accessor of the
96             # Astro::Catalog::Item object.
97             my @lines = @$lines;
98             for ( @lines ) {
99             my $line = $_;
100              
101             $line =~ s/^\s+//;
102              
103             my ( $id, $x, $y, $mag ) = split /\s+/, $line, 4;
104              
105             # Create the Astro::Flux object for this magnitude.
106             my $flux = new Astro::Flux( new Number::Uncertainty( Value => $mag ),
107             'mag',
108             $filter );
109             my @mags;
110             push @mags, $flux;
111             my $fluxes = new Astro::Fluxes( @mags );
112              
113             # Create the Astro::Catalog::Item object.
114             my $item = new Astro::Catalog::Item( ID => $id,
115             X => $x,
116             Y => $y,
117             Fluxes => $fluxes,
118             );
119              
120             # Push it into the catalogue.
121             $catalog->pushstar( $item );
122              
123             }
124              
125             # Set the origin.
126             $catalog->origin( 'Astro::Catalog::IO::RITMatch' );
127              
128             # And return!
129             return $catalog;
130             }
131              
132             =item B<_write_catalog>
133              
134             Create an output catalogue in the 'match' format and return the lines
135             in an array.
136              
137             $ref = Astro::Catalog::IO::RITMatch->_write_catalog( $catalog, %options );
138              
139             The sole mandatory argument is an C object.
140              
141             Optional arguments are:
142              
143             =item mag_type - the magnitude type to write out to the file. Defaults
144             to 'mag'.
145              
146             The output format has the ID in column 1, X coordinate in column 2, Y
147             coordinate in column 3, magnitude value in column 4, and any comments
148             in column 5.
149              
150             =cut
151              
152             sub _write_catalog {
153             my $class = shift;
154             my $catalog = shift;
155              
156             if( ! UNIVERSAL::isa( $catalog, "Astro::Catalog" ) ) {
157             croak "Must supply catalogue as an Astro::Catalog object";
158             }
159              
160             # Get options.
161             my %options = @_;
162             my $mag_type = 'mag';
163             if( defined( $options{'mag_type'} ) ) {
164             $mag_type = $options{'mag_type'};
165             }
166              
167             # Set up variables for output.
168             my @output;
169             my $output_line;
170              
171             my $newid = 1;
172              
173             # Loop through the items in the catalogue.
174             foreach my $item ( $catalog->stars ) {
175              
176             my $x = $item->x;
177             my $y = $item->y;
178             my $fluxes = $item->fluxes;
179              
180             # We need at a bare minimum the X, Y, ID, and a magnitude.
181             next if ( ! defined( $newid ) ||
182             ! defined( $x ) ||
183             ! defined( $y ) ||
184             ! defined( $fluxes ) );
185              
186             # Grab the Astro::Flux objects. We'll use the first one.
187             my @flux = $fluxes->allfluxes;
188             my $flux = $flux[0];
189              
190             next if ( uc( $flux->type ) ne uc( $mag_type ) );
191              
192             # Create the output string.
193             $output_line = join ' ', $newid,
194             $x,
195             $y,
196             $flux->quantity($mag_type);
197              
198             # And push this string to the output array.
199             push @output, $output_line;
200              
201             $newid++;
202             }
203              
204             # All done looping through the items, so return the array ref.
205             return \@output;
206              
207             }
208              
209             =back
210              
211             =head1 REVISION
212              
213             $Id: RITMatch.pm,v 1.2 2007/05/31 01:45:07 cavanagh Exp $
214              
215             =head1 SEE ALSO
216              
217             L, L
218              
219             http://spiff.rit.edu/match/
220              
221             =head1 COPYRIGHT
222              
223             Copyright (C) 2006 Particle Physics and Astronomy Research
224             Council. All Rights Reserved.
225              
226             This program is free software; you can redistribute it and/or modify it
227             under the terms of the GNU Public License.
228              
229             =head1 AUTHORS
230              
231             Brad Cavanagh Eb.cavanagh@jach.hawaii.eduE
232              
233             =cut
234              
235             1;