Astro::Catalog::IO::ASCII - base class for ASCII-based catalogues.


Astro-Catalog documentation Contained in the Astro-Catalog distribution.

Index


Code Index:

NAME

Top

Astro::Catalog::IO::ASCII - base class for ASCII-based catalogues.

SYNOPSIS

Top

  $cat = $ioclass->read_catalog( %args );

DESCRIPTION

Top

This class provides a wrapper for reading ASCII-based catalogues into Astro::Catalog objects. The method should, in general, only be called from the Astro::Catalog configure method.

METHODS

Top

read_catalog

Read the catalog.

  $cat = $ioclass->read_catalog( %args );

Takes a hash as argument with the list of keywords. Supported options are:

  Data => Contents of catalogue, either as a scalar variable,
          reference to array of lines or reference to glob (file handle).
          This key is used in preference to 'File' if both are present.

  File => File name for catalog on disk. Not used if 'Data' supplied.
          If a file is specified but is called 'default', the default file 
          for the class is used.

  ReadOpt => Reference to hash of options to be forwarded onto the
             format specific catalogue reader. See the IO documentation
             for details.

The options are case-insensitive.

REVISION

Top

  $Id: ASCII.pm,v 1.2 2005/08/05 03:36:17 timj Exp $

SEE ALSO

Top

Astro::Catalog

COPYRIGHT

Top

AUTHORS

Top

Brad Cavanagh <b.cavanagh@jach.hawaii.edu>


Astro-Catalog documentation Contained in the Astro-Catalog distribution.
package Astro::Catalog::IO::ASCII;

use 5.006;
use warnings;
use warnings::register;
use Carp;
use strict;

use vars qw/ $VERSION $DEBUG /;

$VERSION = '0.02';
$DEBUG = 0;

sub read_catalog {
  my $class = shift;

  my %args = @_;
  %args = Astro::Catalog::_normalize_hash( %args );

  # Lines for the content
  my @lines;

  # Now need to either look for some data or read a file
  if ( defined $args{data}) {

    # Need to extract the data from this and convert to array
    if (not ref($args{data})) {
      # must be a scalar
      @lines = split /\n/, $args{data};
    } else {
      if (ref($args{data}) eq 'GLOB') {
        # A file handle
        local $/ = "\n";
        # For some reason <$args{data}> does not do the right thing
        my $fh = $args{data};
        @lines = <$fh>;
      } elsif (ref($args{data}) eq 'ARRAY') {
        # An array of lines
        @lines = @{ $args{data} };
      } else {
        # Who knows
        croak "Can not extract catalog information from scalar of type " . ref($args{data}) ."\n";
      }
    }

  } else {
    # Look for a filename or the default file
    my $file;
    if ( defined $args{file} && $args{file} ne 'default') {
      $file = $args{file};
    } else {
      # Need to ask for the default file
      $file = $class->_default_file() if $class->can( '_default_file' );
      croak "Unable to read catalogue since no file specified and no default known." unless defined $file;
    }

    # Open the file
    my $CAT;
    croak("Astro::Catalog - Cannot open catalogue file $file: $!")
      unless open( $CAT, "< $file" );

    # read from file
    local $/ = "\n";
    @lines = <$CAT>;
    close($CAT);

  }

  # remove new lines
  chomp @lines;

  # Read Catalog options passed in from caller
  my $readopt = (defined $args{readopt} ? $args{readopt} : {} );

  my $catalog = $class->_read_catalog( \@lines, %$readopt );

  return $catalog;
}

1;