| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Astro::Catalog::IO::ASCII; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | Astro::Catalog::IO::ASCII - base class for ASCII-based catalogues. | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | $cat = $ioclass->read_catalog( %args ); | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | This class provides a wrapper for reading ASCII-based catalogues | 
| 14 |  |  |  |  |  |  | into C objects. The method should, in general, only | 
| 15 |  |  |  |  |  |  | be called from the C C method. | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =cut | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  | 1 |  | 5003616 | use 5.006; | 
|  | 1 |  |  |  |  | 12 |  | 
|  | 1 |  |  |  |  | 114 |  | 
| 20 | 1 |  |  | 1 |  | 14 | use warnings; | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 244 |  | 
| 21 | 1 |  |  | 1 |  | 63 | use warnings::register; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 522 |  | 
| 22 | 1 |  |  | 1 |  | 8 | use Carp; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 228 |  | 
| 23 | 1 |  |  | 1 |  | 7 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 256 |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 |  |  | 1 |  | 7 | use vars qw/ $VERSION $DEBUG /; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 1025 |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | $VERSION = '4.31'; | 
| 28 |  |  |  |  |  |  | $DEBUG = 0; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =head1 METHODS | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =over 4 | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =item B | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | Read the catalog. | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | $cat = $ioclass->read_catalog( %args ); | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | Takes a hash as argument with the list of keywords. Supported options | 
| 41 |  |  |  |  |  |  | are: | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | Data => Contents of catalogue, either as a scalar variable, | 
| 44 |  |  |  |  |  |  | reference to array of lines or reference to glob (file handle). | 
| 45 |  |  |  |  |  |  | This key is used in preference to 'File' if both are present. | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | File => File name for catalog on disk. Not used if 'Data' supplied. | 
| 48 |  |  |  |  |  |  | If a file is specified but is called 'default', the default file | 
| 49 |  |  |  |  |  |  | for the class is used. | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | ReadOpt => Reference to hash of options to be forwarded onto the | 
| 52 |  |  |  |  |  |  | format specific catalogue reader. See the IO documentation | 
| 53 |  |  |  |  |  |  | for details. | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | The options are case-insensitive. | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =cut | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub read_catalog { | 
| 60 | 0 |  |  | 0 | 1 |  | my $class = shift; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  |  | my %args = @_; | 
| 63 | 0 |  |  |  |  |  | %args = Astro::Catalog::_normalize_hash( %args ); | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # Lines for the content | 
| 66 | 0 |  |  |  |  |  | my @lines; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # Now need to either look for some data or read a file | 
| 69 | 0 | 0 |  |  |  |  | if ( defined $args{data}) { | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # Need to extract the data from this and convert to array | 
| 72 | 0 | 0 |  |  |  |  | if (not ref($args{data})) { | 
| 73 |  |  |  |  |  |  | # must be a scalar | 
| 74 | 0 |  |  |  |  |  | @lines = split /\n/, $args{data}; | 
| 75 |  |  |  |  |  |  | } else { | 
| 76 | 0 | 0 | 0 |  |  |  | if (ref($args{data}) eq 'GLOB' || UNIVERSAL::isa($args{data},"IO::Handle") ) { | 
|  |  | 0 |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | # A file handle | 
| 78 | 0 |  |  |  |  |  | local $/ = "\n"; | 
| 79 |  |  |  |  |  |  | # For some reason <$args{data}> does not do the right thing | 
| 80 | 0 |  |  |  |  |  | my $fh = $args{data}; | 
| 81 | 0 |  |  |  |  |  | @lines = <$fh>; | 
| 82 |  |  |  |  |  |  | } elsif (ref($args{data}) eq 'ARRAY') { | 
| 83 |  |  |  |  |  |  | # An array of lines | 
| 84 | 0 |  |  |  |  |  | @lines = @{ $args{data} }; | 
|  | 0 |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | } else { | 
| 86 |  |  |  |  |  |  | # Who knows | 
| 87 | 0 |  |  |  |  |  | croak "Can not extract catalog information from scalar of type " . ref($args{data}) ."\n"; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | } else { | 
| 92 |  |  |  |  |  |  | # Look for a filename or the default file | 
| 93 | 0 |  |  |  |  |  | my $file; | 
| 94 | 0 | 0 | 0 |  |  |  | if ( defined $args{file} && $args{file} ne 'default') { | 
| 95 | 0 |  |  |  |  |  | $file = $args{file}; | 
| 96 |  |  |  |  |  |  | } else { | 
| 97 |  |  |  |  |  |  | # Need to ask for the default file | 
| 98 | 0 | 0 |  |  |  |  | $file = $class->_default_file() if $class->can( '_default_file' ); | 
| 99 | 0 | 0 |  |  |  |  | croak "Unable to read catalogue since no file specified and no default known." unless defined $file; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | # Open the file | 
| 103 | 0 |  |  |  |  |  | my $CAT; | 
| 104 | 0 | 0 |  |  |  |  | croak("Astro::Catalog - Cannot open catalogue file $file: $!") | 
| 105 |  |  |  |  |  |  | unless open( $CAT, "< $file" ); | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # read from file | 
| 108 | 0 |  |  |  |  |  | local $/ = "\n"; | 
| 109 | 0 |  |  |  |  |  | @lines = <$CAT>; | 
| 110 | 0 |  |  |  |  |  | close($CAT); | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | # remove new lines | 
| 115 | 0 |  |  |  |  |  | chomp @lines; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # Read Catalog options passed in from caller | 
| 118 | 0 | 0 |  |  |  |  | my $readopt = (defined $args{readopt} ? $args{readopt} : {} ); | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 0 |  |  |  |  |  | my $catalog = $class->_read_catalog( \@lines, %$readopt ); | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 |  |  |  |  |  | return $catalog; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =back | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =head1 REVISION | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | $Id: ASCII.pm,v 1.2 2005/08/05 03:36:17 timj Exp $ | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | L | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | Copyright (C) 2005 Particle Physics and Astronomy Research Council. | 
| 138 |  |  |  |  |  |  | All Rights Reserved. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it under | 
| 141 |  |  |  |  |  |  | the terms of the GNU General Public License as published by the Free Software | 
| 142 |  |  |  |  |  |  | Foundation; either version 2 of the License, or (at your option) any later | 
| 143 |  |  |  |  |  |  | version. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | This program is distributed in the hope that it will be useful,but WITHOUT ANY | 
| 146 |  |  |  |  |  |  | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | 
| 147 |  |  |  |  |  |  | PARTICULAR PURPOSE. See the GNU General Public License for more details. | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | You should have received a copy of the GNU General Public License along with | 
| 150 |  |  |  |  |  |  | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
| 151 |  |  |  |  |  |  | Place,Suite 330, Boston, MA  02111-1307, USA | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head1 AUTHORS | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | Brad Cavanagh Eb.cavanagh@jach.hawaii.eduE | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =cut | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | 1; |