Util::GTIfile (version 0.0)


package Util::GTIfile;
##############################################################################
#
# DESCRIPTION: 
#
# HISTORY: 
# HISTORY: $Log: GTIfile.pm,v $
# HISTORY: Revision 1.1  2008/05/16 17:55:15  apsop
# HISTORY: New FITSfile subclass with specialized GTI methods.
# HISTORY:
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################

use Util::FITSfile;
@ISA=("Util::FITSfile");
use Util::Ftool;
use Util::HEAdas;
use strict;

sub new { 
    my $self = shift;
    my $log = Util::Ftool->log();
    my $file = shift;

    ########################################
    # inherit the generic Tool constructor
    ########################################
    $self=$self->SUPER::new($file);

    if( -f $self->{NAME} ){
      $self->find_ext();
    }else{
      if( @_ > 1 ){
	$self->create_file(@_);
      }else{
	$log->error(3, "Invalid Arguments for new GTIfile method");
      }
    }
    return $self;
}


########################################################
# Find and set the first GTI extension, and complain if
# there isn't one
########################################################
sub find_ext {
    my $self = shift;
    my $log=Util::Ftool->log();

    my $nhdus = $self->nhdus();
    my $gti_found = 0;
    for(my $hdu_num=1; $hdu_num <= $nhdus; $hdu_num++){
      $self->ext($hdu_num);

      my $class_key = $self->keyword('HDUCLASS');
      next unless ($class_key && $class_key eq 'OGIP');

      my $class_key1 = $self->keyword('HDUCLAS1');
      next unless ($class_key1 && $class_key1 eq 'GTI');

      $gti_found = 1;
      last;
    }

    unless($gti_found){
      $log->error(2, "File $self->{NAME} does not have a valid GTI extension.");      
    }

    return;
}


######################################################
# Create a gti file from a hash of start=>stop times.
######################################################
sub create_file {
  my $self = shift;
  my @intervals = @_;

  my $datafile = 'fits_data.tmp';
  my $cdfile = 'fits_cols.tmp';
  my $hdfile = 'fits_head.tmp';
   
  open COLS, ">$cdfile";
  print COLS "START 1D s\n";
  print COLS "STOP 1D s\n";
  close COLS;

  open HEAD, ">$hdfile";
  print HEAD "EXTNAME = 'GTI     '  / name of this binary table extension\n";
  print HEAD "HDUCLASS= 'OGIP    '  / format conforms to OGIP/GSFC conventions\n";
  print HEAD "HDUCLAS1= 'GTI     '  / Extension contains Good Time Intervals\n";
  print HEAD "HDUCLAS2= 'STANDARD'  / Extension contains Good Time Intervals\n";
  print HEAD "TIMESYS = 'TT      '  / time measured from\n";
  print HEAD "MJDREFI =       51910 / MJD reference day\n";
  print HEAD "MJDREFF = 7.428703700000000E-04 / MJD reference (fraction of day)\n";
  print HEAD "CLOCKAPP=           F / default\n";
  print HEAD "TIMEUNIT= 's       '  / unit for time keywords\n";
  close HEAD;

  open DATA, ">$datafile";
  print DATA join(' ', @intervals), "\n";
  close DATA;

  my $table = Util::Ftool->new('fcreate')
                         ->params({cdfile => $cdfile,
				   datafile => $datafile,
				   headfile => $hdfile,
				   outfile => $self->{NAME}})
			 ->run();

  unlink $cdfile, $datafile, $hdfile;

  $self->ext(1);
  $self->cols('START')->sort();
  
  my @sorted_times = sort {$a <=> $b} @intervals;
  $self->keyword('TSTART', $intervals[0]);
  $self->keyword('TSTOP', $intervals[-1]);

  return;
}


################################################
# Sum up and return all the good time intervals
################################################
sub sum {
  my $self = shift;

  $self->cols('START', 'STOP');
  my %intervals = $self->table();

  my $sum = 0;
  foreach my $start (keys %intervals){
    $sum += $intervals{$start} - $start;
  }

  return $sum;
}

1;