| 1 | package Astro::Catalog::IO::VEX; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Astro::Catalog::IO::VEX - Module to read/write sources in VEX format | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use warnings::register; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Astro::Coords; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Astro::Telescope; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Astro::VEX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Astro::VEX::Block; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Astro::VEX::Def; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Astro::VEX::Param; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Astro::VEX::Param::String; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Astro::Catalog; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Astro::Catalog::Item; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use parent qw/Astro::Catalog::IO::ASCII/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | our $VERSION = '4.38'; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =over 4 | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item B<_read_catalog> | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Parses the catalog lines and returns a new C<Astro::Catalog> object. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $cat = Astro::Catalog::IO::VEX->_read_catalog(\@lines, %options); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Options: | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =over 4 | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item telescope | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Name of telescope to associate with each entry. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =back | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub _read_catalog { | ||||||||||||||||||||||||||
|
Subroutine
_read_catalog: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 53 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 54 | 0 | my $lines = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my %options = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | croak "_read_catalog: catalog lines must be an array reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | unless 'ARRAY' eq ref $lines; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | my $tel = undef; | |||||||||||||||||||||||||||
| 62 | $tel = new Astro::Telescope($options{'telescope'}) | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if defined $options{'telescope'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $vex = new Astro::VEX(text => join "\n", @$lines); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my @items; | |||||||||||||||||||||||||||
| 68 | 0 | foreach my $source ($vex->block('SOURCE')->items) { | |||||||||||||||||||||||||||
| 69 | 0 | my $key_name = $source->name; | |||||||||||||||||||||||||||
| 70 | 0 | my $name = eval {$source->param('source_name')->value;}; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (defined $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | warnings::warnif 'WARNING: source_name ' . $name. ' does not match keyword ' . $key_name . "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | unless $name eq $source->name; | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 0 | $name = $key_name; | |||||||||||||||||||||||||||
| 77 | 0 | warnings::warnif 'WARNING: source_name missing, using keyword ' . $key_name . "\n" | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $ra_str = $source->param('ra')->value; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | die "Did not understand RA '$ra_str' for '$name'" unless $ra_str =~ /^(\d+)h(\d+)m(\d+\.\d+)s$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | my $ra = "$1:$2:$3"; | |||||||||||||||||||||||||||
| 83 | 0 | my $dec_str = $source->param('dec')->value; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | die "Did not understand Dec '$dec_str' for '$name'" unless $dec_str =~ /^(-?\d+)d(\d+)'(\d+.\d+)"$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | my $dec = "$1:$2:$3"; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | die 'Coordinate system not J2000' unless $source->param('ref_coord_frame')->value eq 'J2000'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $coords = new Astro::Coords( | |||||||||||||||||||||||||||
| 89 | name => $name, | ||||||||||||||||||||||||||||
| 90 | ra => $ra, | ||||||||||||||||||||||||||||
| 91 | dec => $dec, | ||||||||||||||||||||||||||||
| 92 | type => 'J2000', | ||||||||||||||||||||||||||||
| 93 | units => 'sexagesimal'); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $coords->telescope($tel) if defined $tel; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | push @items, new Astro::Catalog::Item( | |||||||||||||||||||||||||||
| 98 | id => $name, | ||||||||||||||||||||||||||||
| 99 | coords => $coords, | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | return new Astro::Catalog(Stars => \@items); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =item B<_write_catalog> | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Write the catalog to an array of lines. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | $lines = Astro::Catalog::IO::VEX->_write_catalog($cat); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub _write_catalog { | ||||||||||||||||||||||||||
|
Subroutine
_write_catalog: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 115 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 116 | 0 | my $cat = shift; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my @sources; | |||||||||||||||||||||||||||
| 119 | 0 | for my $item ($cat->stars) { | |||||||||||||||||||||||||||
| 120 | 0 | my $coords = $item->coords; | |||||||||||||||||||||||||||
| 121 | 0 | my $name = $coords->name; | |||||||||||||||||||||||||||
| 122 | 0 | my $type = $coords->type; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Remove problematic characters from name. | ||||||||||||||||||||||||||||
| 125 | 0 | $name =~ s/[^-!#%'()+,\.\/0-9<>?\@A-Z\[\\\]^_`a-z{|}~]/_/g; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if ($type eq 'RADEC') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | my $ra = $coords->ra->components(7); | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | die 'RA array not in expected format' unless $ra->[0] eq '+' && 4 == scalar @$ra; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ra->[0] eq "+" and 4 == scalar @$ra
MC/DC0%0/2
$ra->[0] eq "+" and 4 == scalar @$ra
$ra->[0] eq "+"
4 == scalar @$ra
Truth table0%0/3
A and B
A= $ra->[0] eq "+"B= 4 == scalar @$ra
| |||||||||||||||||||||||||||||
| 130 | 0 | shift @$ra; | |||||||||||||||||||||||||||
| 131 | 0 | my $dec = $coords->dec->components(6); | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | die 'Dec array not in expected format' unless $dec->[0] =~ /^[-+]$/ && 4 == scalar @$dec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dec->[0] =~ /^[-+]$/ and 4 == scalar @$dec
MC/DC0%0/2
$dec->[0] =~ /^[-+]$/ and 4 == scalar @$dec
$dec->[0] =~ /^[-+]$/
4 == scalar @$dec
Truth table0%0/3
A and B
A= $dec->[0] =~ /^[-+]$/B= 4 == scalar @$dec
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $dec->[0] = '' if $dec->[0] eq '+'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | push @sources, new Astro::VEX::Def($name, [ | |||||||||||||||||||||||||||
| 136 | new Astro::VEX::Param('source_name', [new Astro::VEX::Param::String($name)]), | ||||||||||||||||||||||||||||
| 137 | new Astro::VEX::Param('ra', [new Astro::VEX::Param::String(sprintf('%02dh%02dm%010.7fs', @$ra))]), | ||||||||||||||||||||||||||||
| 138 | new Astro::VEX::Param('dec', [new Astro::VEX::Param::String(sprintf('%s%02dd%02d\'%09.6f"', @$dec))]), | ||||||||||||||||||||||||||||
| 139 | new Astro::VEX::Param('ref_coord_frame', [new Astro::VEX::Param::String('J2000')]), | ||||||||||||||||||||||||||||
| 140 | ]); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||
| 143 | 0 | warnings::warnif "Coordinate of type '$type' for target '$name' not currently supported\n"; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $vex = new Astro::VEX(version => '1.5', content => [ | |||||||||||||||||||||||||||
| 148 | new Astro::VEX::Block('SOURCE', \@sources), | ||||||||||||||||||||||||||||
| 149 | ]); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | return [split "\n", "$vex"]; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||