File Coverage

blib/lib/Astro/Catalog/IO/STL.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Astro::Catalog::IO::STL;
2              
3             =head1 NAME
4              
5             Astro::Catalog::IO::STL - STL catalogue I/O for Astro::Catalog
6              
7             =head1 SYNOPSIS
8              
9             $cat = Astro::Catalog::IO::STL->_read_catalog( \@lines );
10              
11             =head1 DESCRIPTION
12              
13             This class provides read and write methods for catalogues in the CURSA
14             small text list (STL) catalogue format. The methods are not public and
15             should, in general, only be called from the C
16             C method.
17              
18             =cut
19              
20 1     1   6532843 use 5.006;
  1         5  
  1         76  
21 1     1   21 use warnings;
  1         3  
  1         124  
22 1     1   266 use warnings::register;
  1         3  
  1         382  
23 1     1   7 use Carp;
  1         1  
  1         286  
24 1     1   19 use strict;
  1         2  
  1         54  
25              
26 1     1   4 use Carp;
  1         2  
  1         251  
27              
28 1     1   1161 use Astro::Catalog;
  0            
  0            
29             use Astro::Catalog::Star;
30             use Astro::Coords;
31              
32             use base qw/ Astro::Catalog::IO::ASCII /;
33              
34             use vars qw/$VERSION $DEBUG/;
35              
36             $VERSION = '4.31';
37             $DEBUG = 0;
38              
39             =begin __PRIVATE_METHODS__
40              
41             =head1 Private Methods
42              
43             These methods are usually called automatically from the C
44             constructor.
45              
46             =over 4
47              
48             =item B<_read_catalog>
49              
50             Parses the catalogue lines and returns a new C
51             object containing the catalogue entries.
52              
53             $cat = Astro::Catalog::IO::STL->_read_catalog( \@lines );
54              
55             The catalogue lines must include column definitions (lines starting
56             with a C) so that the parser knows in which column values lie.
57              
58             =cut
59              
60             sub _read_catalog {
61             my $class = shift;
62             my $lines = shift;
63              
64             if( ref( $lines) ne 'ARRAY' ) {
65             croak "Must supply catalogue contents as a reference to an array";
66             }
67              
68             my @lines = @$lines; # Dereference, make own copy.
69              
70             # Concatenate all continuation lines (they start with a colon).
71             chomp @lines;
72             my $all_lines = join( "\n", @lines );
73             $all_lines =~ s/\n://g;
74             @lines = split( "\n", $all_lines );
75              
76             # Create an Astro::Catalog object.
77             my $catalog = new Astro::Catalog();
78              
79             # Set a counter for star ID.
80             my $id = 0;
81              
82             # Set up columns.
83             my $ra_column = -1;
84             my $dec_column = -1;
85             my $id_column = -1;
86              
87             # Do we convert from DMS to radians?
88             my $ra_convert = 0;
89             my $dec_convert = 0;
90              
91             # Are we in the main table yet?
92             my $intable = 0;
93              
94             # Loop through the lines.
95             for( @lines ) {
96              
97             my $line = $_;
98              
99             # If we're on a column line that starts with a C, check to see
100             # if it's describing where the position identifier, RA, or Dec.
101             if( $line =~ /^C/ ) {
102             my @column = split( /\s+/, $line );
103             if( $column[1] =~ /pident/i ) {
104             $id_column = $column[3] - 1;
105             } elsif( $column[1] =~ /ra/i ) {
106             $ra_column = $column[3] - 1;
107             if( $line =~ /TBLFMT=HOURS/ ) {
108             # Convert DMS to radians.
109             $ra_convert = 1;
110             }
111             } elsif( $column[1] =~ /dec/i ) {
112             $dec_column = $column[3] - 1;
113             if( $line =~ /TBLFMT=DEGREES/ ) {
114             # Convert DMS to radians.
115             $dec_convert = 1;
116             }
117             } elsif( ( $column[1] =~ /^[a-z]$/i ) ||
118             ( $column[1] =~ /^[a-z]_[a-z]$/i ) ) {
119             warnings::warnif("Magnitude description found, magnitudes not yet supported");
120             }
121             next;
122             }
123              
124             my $equinox = 0;
125              
126             # If it's a line starting with a P, then this is a parameter
127             # for the coordinate system.
128             if( $line =~ /^P/ ) {
129             my @column = split( /\s+/, $line );
130             if( $column[1] eq 'EQUINOX' ) {
131             ( $equinox = $column[3] ) =~ s/\'//g;
132             }
133             next;
134             }
135              
136             # We need to wait until the BEGINTABLE line.
137             next if( ! $intable && $line !~ /^BEGINTABLE/ );
138              
139             if( $line =~ /^BEGINTABLE/ ) {
140             $intable = 1;
141             next;
142             }
143              
144             # If we've made it here we're in the table.
145              
146             # Have a winge if we don't have RA/Dec.
147             if( ( $ra_column == -1 ) ||
148             ( $dec_column == -1 ) ) {
149             croak "STL file does not contain RA and Dec information";
150             }
151              
152             $line =~ s/^\s+//;
153              
154             next if length( $line ) == 0;
155              
156             my @fields = split( /\s+/, $line );
157              
158             # Set the star's ID.
159             my $name;
160             if( $id_column != -1 ) {
161             $name = $fields[$id_column];
162             } else {
163             $name = $id;
164             }
165              
166             # Create a temporary Astro::Catalog::Star object.
167             my $star = new Astro::Catalog::Star();
168              
169             # Do RA/Dec conversions to radians, if necessary.
170             my $ra = Astro::Coords::Angle::Hour->new( $fields[$ra_column],
171             units => ($ra_convert ? "sex" : "rad")
172             );
173             my $dec = Astro::Coords::Angle->new( $fields[$dec_column],
174             units => ($dec_convert ? "sex" : "rad" )
175             );
176              
177             # Create an Astro::Coords object, assuming J2000 for RA/Dec.
178             my $coords = new Astro::Coords( type => ( $equinox ? $equinox : 'J2000' ),
179             ra => $ra,
180             dec => $dec,
181             name => $name,
182             units => 'radians',
183             );
184              
185             # And push it into the Astro::Catalog::Star object.
186             $star->coords( $coords );
187              
188             # Set default "good" quality.
189             $star->quality( 0 );
190              
191             # And push the star onto the catalog.
192             $catalog->pushstar( $star );
193              
194             $id++;
195              
196             }
197              
198             $catalog->origin( 'IO::STL' );
199             return $catalog;
200              
201             }
202              
203             =item B<_write_catalog>
204              
205             Create an output catalogue in the STL format and return the lines
206             in an array.
207              
208             $ref = Astro::Catalog::IO::STL->_write_catalog( $catalog );
209              
210             Argument is an C object.
211              
212             =cut
213              
214             sub _write_catalog {
215             my $class = shift;
216             my $catalog = shift;
217              
218             # An array to hold the output.
219             my @return;
220              
221             # First, the preamble.
222             push( @return, "!+" );
223             push( @return, "! This catalogue is formatted as a CURSA small text list (STL)." );
224             push( @return, "! For a description of this format see Starlink User Note 190." );
225             push( @return, "!-" );
226             push( @return, "" );
227              
228             # Now the header describing the output columns.
229             push( @return, "C PIDENT INTEGER 1 EXFMT=I6" );
230             push( @return, ": COMMENTS='Position identifier'" );
231             push( @return, "C RA DOUBLE 2 EXFMT=D19.10" );
232             push( @return, ": UNITS='RADIANS{hms.1}'" );
233             push( @return, ": COMMENTS='Right ascension'" );
234             push( @return, "C Dec DOUBLE 3 EXFMT=D19.10" );
235             push( @return, ": UNITS='RADIANS{dms}'" );
236             push( @return, ": COMMENTS='Declination'" );
237             push( @return, "" );
238              
239             # Begin the table.
240             push( @return, "BEGINTABLE" );
241              
242             # And now the actual data. Loop through the stars in the catalogue.
243             my $stars = $catalog->stars();
244              
245             foreach my $star ( @$stars ) {
246             my $output_string;
247              
248             my $id_string = sprintf( "%6d", $star->id );
249             my $ra_string = sprintf( "%19.10e", $star->coords->ra->radians );
250             $ra_string =~ s/e/E/;
251             my $dec_string = sprintf( "%19.10e", $star->coords->dec->radians );
252             $dec_string =~ s/e/E/;
253              
254             $output_string = $id_string . $ra_string . $dec_string;
255             push( @return, $output_string );
256             }
257              
258             # And return.
259             return \@return;
260             }
261              
262             =back
263              
264             =head1 REVISION
265              
266             $Id: STL.pm,v 1.3 2005/09/13 02:12:50 cavanagh Exp $
267              
268             =head1 FORMAT
269              
270             The STL format is specified in SUN/190
271             [http://www.starlink.rl.ac.uk/star/docs/sun190.htx//sun190.html] and SSN/75
272             [http://www.starlink.rl.ac.uk/star/docs/ssn75.htx//ssn75.html], both by
273             Clive Davenhall.
274              
275             =head1 SEE ALSO
276              
277             L, L.
278              
279             =head1 COPYRIGHT
280              
281             Copyright (C) 2004-2005 Particle Physics and Astronomy Research Council.
282             All Rights Reserved.
283              
284             This module is free software; you can redistribute it and/or modify it
285             under the terms of the GNU Public License.
286              
287             =head1 AUTHORS
288              
289             Brad Cavanagh Eb.cavanagh@jach.hawaii.eduE
290              
291             =cut
292              
293             1;