Astro::Catalog::IO::Binary - base class for binary catalogues.


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

Index


Code Index:

NAME

Top

Astro::Catalog::IO::Binary - base class for binary catalogues.

SYNOPSIS

Top

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

DESCRIPTION

Top

This class provides a wrapper for reading binary 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, as a reference to glob (file handle)
          or a scalar containing data to be turned into a catalog.
          This key is used in preference to 'File' if both are present.
  File => File name for catalog on disk. Not used if 'Data' supplied.
  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: Binary.pm,v 1.1 2005/03/31 01:26:07 cavanagh 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::Binary;

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

use vars qw/ $VERSION $DEBUG /;

$VERSION = '0.01';
$DEBUG = 0;

sub read_catalog {
  my $class = shift;

  my $catalog;

  # Retrieve and normalize arguments.
  my %args = @_;
  %args = Astro::Catalog::_normalize_hash( %args );

  my $readopt = (defined $args{readopt} ? $args{readopt} : {} );

  # Find out if the class would rather have a file handle or
  # a file name.
  my $input_format = $class->input_format;

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

    if (ref($args{data}) eq 'GLOB') {
      # A file handle. If the requested input format is a file handle,
      # then we're good. If the requested input format is a file name,
      # then copy the file pointed to by the file handle to a temporary
      # file, then pass that file name to the IO class.
      if( $input_format eq 'handle' ) {

        $catalog = $class->_read_catalog( filehandle => $args{data},
                                          %$readopt );

      } elsif( $input_format eq 'name' ) {

        ( my $fh, my $filename ) = tempfile( UNLINK => 1 );
        binmode $args{data};
        while( read $args{data}, my $buffer, 1024 ) {
          print $fh $buffer;
        }
        close $fh;
        $catalog = $class->_read_catalog( filename => $filename,
                                          %$readopt );
      } else {

        # We got back something we can't use.
        croak "Unknown input format $input_format";

      }
    } elsif( not ref( $args{data} ) ) {

      ( my $fh, my $filename ) = tempfile( UNLINK => 1 );
      print $fh $args{data};
      close $fh;

      if( $input_format eq 'handle' ) {
        open( $fh, $filename ) or croak "Could not open file $filename for reading: $!";
        $catalog = $class->_read_catalog( filehandle => $fh,
                                          %$readopt );
        close $fh;
      } elsif( $input_format eq 'name' ) {

        $catalog = $class->_read_catalog( filename => $filename,
                                            %$readopt );
      }
    } 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} ) {
      $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;
    }

    # Pass along the desired input format.
    if( $input_format eq 'handle' ) {

      open( my $fh, $file ) or croak "Could not open file $file: $!";
      $catalog = $class->_read_catalog( filehandle => $fh,
                                        %$readopt );

    } elsif( $input_format eq 'name' ) {

      $catalog = $class->_read_catalog( filename => $file,
                                        %$readopt );

    } else {

      croak "Unknown input format $input_format";
    }

  }

  return $catalog;
}

1;