| 1 | package Astro::Catalog::IO::STL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Astro::Catalog::IO::STL - STL catalog 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 catalogs in the CURSA | ||||||||||||||||||||||||||||
| 14 | small text list (STL) catalog format. The methods are not public and | ||||||||||||||||||||||||||||
| 15 | should, in general, only be called from the C<Astro::Catalog> | ||||||||||||||||||||||||||||
| 16 | C<write_catalog> method. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use warnings::register; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
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 | our $VERSION = '4.38'; | |||||||||||||||||||||||||||
| 32 | 2 | our $DEBUG = 0; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =begin __PRIVATE_METHODS__ | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 Private Methods | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | These methods are usually called automatically from the C<Astro::Catalog> | ||||||||||||||||||||||||||||
| 39 | constructor. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =over 4 | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =item B<_read_catalog> | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Parses the catalog lines and returns a new C<Astro::Catalog> | ||||||||||||||||||||||||||||
| 46 | object containing the catalog entries. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | $cat = Astro::Catalog::IO::STL->_read_catalog(\@lines); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | The catalog lines must include column definitions (lines starting | ||||||||||||||||||||||||||||
| 51 | with a C) so that the parser knows in which column values lie. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub _read_catalog { | ||||||||||||||||||||||||||
|
Subroutine
_read_catalog: called CC=23 SCAR=33.1
| |||||||||||||||||||||||||||||
| 56 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 57 | 1 | my $lines = shift; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | if (ref( $lines) ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | croak "Must supply catalog contents as a reference to an array"; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | my @lines = @$lines; # Dereference, make own copy. | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Concatenate all continuation lines (they start with a colon). | ||||||||||||||||||||||||||||
| 66 | 1 | chomp @lines; | |||||||||||||||||||||||||||
| 67 | 1 | my $all_lines = join "\n", @lines; | |||||||||||||||||||||||||||
| 68 | 1 | $all_lines =~ s/\n://g; | |||||||||||||||||||||||||||
| 69 | 1 | @lines = split "\n", $all_lines; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Create an Astro::Catalog object. | ||||||||||||||||||||||||||||
| 72 | 1 | my $catalog = new Astro::Catalog(); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Set a counter for star ID. | ||||||||||||||||||||||||||||
| 75 | 1 | my $id = 0; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Set up columns. | ||||||||||||||||||||||||||||
| 78 | 1 | my $ra_column = -1; | |||||||||||||||||||||||||||
| 79 | 1 | my $dec_column = -1; | |||||||||||||||||||||||||||
| 80 | 1 | my $id_column = -1; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Do we convert from DMS to radians? | ||||||||||||||||||||||||||||
| 83 | 1 | my $ra_convert = 0; | |||||||||||||||||||||||||||
| 84 | 1 | my $dec_convert = 0; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Are we in the main table yet? | ||||||||||||||||||||||||||||
| 87 | 1 | my $intable = 0; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Loop through the lines. | ||||||||||||||||||||||||||||
| 90 | 1 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 91 | # If we're on a column line that starts with a C, check to see | ||||||||||||||||||||||||||||
| 92 | # if it's describing where the position identifier, RA, or Dec. | ||||||||||||||||||||||||||||
| 93 | 18 | ▶ | if ($line =~ /^C/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 3 | my @column = split(/\s+/, $line); | |||||||||||||||||||||||||||
| 95 | 3 | ▶ | if ($column[1] =~ /pident/i) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 96 | 1 | $id_column = $column[3] - 1; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | elsif ($column[1] =~ /ra/i) { | ||||||||||||||||||||||||||||
| 99 | 1 | $ra_column = $column[3] - 1; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | if($line =~ /TBLFMT=HOURS/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | # Convert DMS to radians. | ||||||||||||||||||||||||||||
| 102 | 1 | $ra_convert = 1; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | elsif ($column[1] =~ /dec/i) { | ||||||||||||||||||||||||||||
| 106 | 1 | $dec_column = $column[3] - 1; | |||||||||||||||||||||||||||
| 107 | 1 | ▶ | if($line =~ /TBLFMT=DEGREES/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | # Convert DMS to radians. | ||||||||||||||||||||||||||||
| 109 | 1 | $dec_convert = 1; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | ▶ | elsif (($column[1] =~ /^[a-z]$/i) || | |||||||||||||||||||||||||||
|
Condition0%0/3
$column[1] =~ /^[a-z]$/i or $column[1] =~ /^[a-z]_[a-z]$/i
MC/DC0%0/2
$column[1] =~ /^[a-z]$/i or $column[1] =~ /^[a-z]_[a-z]$/i
$column[1] =~ /^[a-z]$/i
$column[1] =~ /^[a-z]_[a-z]$/i
Truth table0%0/3
A or B
A= $column[1] =~ /^[a-z]$/iB= $column[1] =~ /^[a-z]_[a-z]$/i
| |||||||||||||||||||||||||||||
| 113 | ($column[1] =~ /^[a-z]_[a-z]$/i)) { | ||||||||||||||||||||||||||||
| 114 | 0 | warnings::warnif("Magnitude description found, magnitudes not yet supported"); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 3 | next; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 15 | my $equinox = 0; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # If it's a line starting with a P, then this is a parameter | ||||||||||||||||||||||||||||
| 122 | # for the coordinate system. | ||||||||||||||||||||||||||||
| 123 | 15 | ▶ | if ($line =~ /^P/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | my @column = split(/\s+/, $line); | |||||||||||||||||||||||||||
| 125 | 2 | ▶ | if($column[1] eq 'EQUINOX') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 1 | ($equinox = $column[3]) =~ s/\'//g; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 2 | next; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # We need to wait until the BEGINTABLE line. | ||||||||||||||||||||||||||||
| 132 | 13 | ▶ | next if (! $intable && $line !~ /^BEGINTABLE/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$intable or $line =~ /^BEGINTABLE/
MC/DC100%2/2
$intable or $line =~ /^BEGINTABLE/
$intable
$line =~ /^BEGINTABLE/
Truth table100%3/3
A or B
A= $intableB= $line =~ /^BEGINTABLE/
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | ▶ | if ($line =~ /^BEGINTABLE/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 1 | $intable = 1; | |||||||||||||||||||||||||||
| 136 | 1 | next; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # If we've made it here we're in the table. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Have a winge if we don't have RA/Dec. | ||||||||||||||||||||||||||||
| 142 | 4 | ▶ | if (($ra_column == -1) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ra_column == -1 or $dec_column == -1
MC/DC0%0/2
$ra_column == -1 or $dec_column == -1
$ra_column == -1
$dec_column == -1
Truth table33%1/3
A or B
A= $ra_column == -1B= $dec_column == -1
| |||||||||||||||||||||||||||||
| 143 | ($dec_column == -1)) { | ||||||||||||||||||||||||||||
| 144 | 0 | croak "STL file does not contain RA and Dec information"; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | $line =~ s/^\s+//; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 4 | ▶ | next if length($line) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 4 | my @fields = split /\s+/, $line; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Set the star's ID. | ||||||||||||||||||||||||||||
| 154 | 4 | my $name; | |||||||||||||||||||||||||||
| 155 | 4 | ▶ | if ($id_column != -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 4 | $name = $fields[$id_column]; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | else { | ||||||||||||||||||||||||||||
| 159 | 0 | $name = $id; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Create a temporary Astro::Catalog::Item object. | ||||||||||||||||||||||||||||
| 163 | 4 | my $star = new Astro::Catalog::Item(); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Do RA/Dec conversions to radians, if necessary. | ||||||||||||||||||||||||||||
| 166 | 4 | ▶ | my $ra = Astro::Coords::Angle::Hour->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | $fields[$ra_column], | ||||||||||||||||||||||||||||
| 168 | units => ($ra_convert ? "sex" : "rad")); | ||||||||||||||||||||||||||||
| 169 | 4 | ▶ | my $dec = Astro::Coords::Angle->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | $fields[$dec_column], | ||||||||||||||||||||||||||||
| 171 | units => ($dec_convert ? "sex" : "rad" )); | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # Create an Astro::Coords object, assuming J2000 for RA/Dec. | ||||||||||||||||||||||||||||
| 174 | 4 | ▶ | my $coords = new Astro::Coords( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | type => ($equinox ? $equinox : 'J2000'), | ||||||||||||||||||||||||||||
| 176 | ra => $ra, | ||||||||||||||||||||||||||||
| 177 | dec => $dec, | ||||||||||||||||||||||||||||
| 178 | name => $name, | ||||||||||||||||||||||||||||
| 179 | units => 'radians', | ||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # And push it into the Astro::Catalog::Item object. | ||||||||||||||||||||||||||||
| 183 | 4 | $star->coords($coords); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # Set default "good" quality. | ||||||||||||||||||||||||||||
| 186 | 4 | $star->quality(0); | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # And push the star onto the catalog. | ||||||||||||||||||||||||||||
| 189 | 4 | $catalog->pushstar($star); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 4 | $id++; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | $catalog->origin('IO::STL'); | |||||||||||||||||||||||||||
| 196 | 1 | return $catalog; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =item B<_write_catalog> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Create an output catalog in the STL format and return the lines | ||||||||||||||||||||||||||||
| 203 | in an array. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | $ref = Astro::Catalog::IO::STL->_write_catalog($catalog); | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Argument is an C<Astro::Catalog> object. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub _write_catalog { | ||||||||||||||||||||||||||
|
Subroutine
_write_catalog: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 212 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 213 | 0 | my $catalog = shift; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # An array to hold the output. | ||||||||||||||||||||||||||||
| 216 | 0 | my @return; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # First, the preamble. | ||||||||||||||||||||||||||||
| 219 | 0 | push @return, "!+"; | |||||||||||||||||||||||||||
| 220 | 0 | push @return, "! This catalog is formatted as a CURSA small text list (STL)."; | |||||||||||||||||||||||||||
| 221 | 0 | push @return, "! For a description of this format see Starlink User Note 190."; | |||||||||||||||||||||||||||
| 222 | 0 | push @return, "!-"; | |||||||||||||||||||||||||||
| 223 | 0 | push @return, ""; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # Now the header describing the output columns. | ||||||||||||||||||||||||||||
| 226 | 0 | push @return, "C PIDENT INTEGER 1 EXFMT=I6"; | |||||||||||||||||||||||||||
| 227 | 0 | push @return, ": COMMENTS='Position identifier'"; | |||||||||||||||||||||||||||
| 228 | 0 | push @return, "C RA DOUBLE 2 EXFMT=D19.10"; | |||||||||||||||||||||||||||
| 229 | 0 | push @return, ": UNITS='RADIANS{hms.1}'"; | |||||||||||||||||||||||||||
| 230 | 0 | push @return, ": COMMENTS='Right ascension'"; | |||||||||||||||||||||||||||
| 231 | 0 | push @return, "C Dec DOUBLE 3 EXFMT=D19.10"; | |||||||||||||||||||||||||||
| 232 | 0 | push @return, ": UNITS='RADIANS{dms}'"; | |||||||||||||||||||||||||||
| 233 | 0 | push @return, ": COMMENTS='Declination'"; | |||||||||||||||||||||||||||
| 234 | 0 | push @return, ""; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # Begin the table. | ||||||||||||||||||||||||||||
| 237 | 0 | push @return, "BEGINTABLE"; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # And now the actual data. Loop through the stars in the catalog. | ||||||||||||||||||||||||||||
| 240 | 0 | my $stars = $catalog->stars(); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 0 | foreach my $star (@$stars) { | |||||||||||||||||||||||||||
| 243 | 0 | my $output_string; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | my $id_string = sprintf("%6d", $star->id); | |||||||||||||||||||||||||||
| 246 | 0 | my $ra_string = sprintf("%19.10e", $star->coords->ra->radians); | |||||||||||||||||||||||||||
| 247 | 0 | $ra_string =~ s/e/E/; | |||||||||||||||||||||||||||
| 248 | 0 | my $dec_string = sprintf("%19.10e", $star->coords->dec->radians); | |||||||||||||||||||||||||||
| 249 | 0 | $dec_string =~ s/e/E/; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | $output_string = $id_string . $ra_string . $dec_string; | |||||||||||||||||||||||||||
| 252 | 0 | push @return, $output_string; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | return \@return; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 2 | 1; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | __END__ | ||||||||||||||||||||||||||||