Subs::ExtractGTIs (version 0.0)


package Subs::ExtractGTIs;
##############################################################################
#
# DESCRIPTION: 
#
# HISTORY: 
# HISTORY: $Log: ExtractGTIs.pm,v $
# HISTORY: Revision 1.21  2012/08/15 08:01:12  apsop
# HISTORY: Add use Util::GTIlist. [Bob W.]
# HISTORY:
# HISTORY: Revision 1.20  2008/05/16 14:14:16  apsop
# HISTORY: Make GTI files using the new GTIfile class.
# HISTORY:
# HISTORY: Revision 1.19  2007/01/31 17:22:07  apsop
# HISTORY: Bug fix for setting of MJDREF in image data GTIs.
# HISTORY:
# HISTORY: Revision 1.18  2005/03/28 19:18:49  apsop
# HISTORY: Set TSTART and TSTOP for image GTI extensions
# HISTORY:
# HISTORY: Revision 1.17  2004/08/16 15:23:09  apsop
# HISTORY: Turn on writing of HISTORY keywords.
# HISTORY:
# HISTORY: Revision 1.16  2004/07/06 20:01:18  apsop
# HISTORY: Add test for existence of nfi GTIs.
# HISTORY:
# HISTORY: Revision 1.15  2004/06/29 14:28:19  apsop
# HISTORY: Do not make a GTI for files where TSTART=TSTOP. Create a gti file just for the nfi instruments.
# HISTORY:
# HISTORY: Revision 1.14  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.13  2004/04/16 20:21:18  dah
# HISTORY: Begin using embedded history records
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


use Subs::Sub;
@ISA = ("Subs::Sub");

use Util::GTIfile;
use Util::GTIlist;
use strict;

sub new {
    my $proto=shift;
    my $self=$proto->SUPER::new();

    $self->{DESCRIPTION}="Extracting GTIs from unfiltered data";

    return $self;
}

##################
# METHODS:
##################

sub body {
    my $self=shift;

    my $log     =$self->log();
    my $filename=$self->filename();
    my $procpar =$self->procpar();
    my $jobpar  =$self->jobpar();
    
    ########################################################
    # accumulate a list of all event file GTIs
    ########################################################
    my $all_gtis = Util::GTIlist->new();
    my $nfi_gtis = Util::GTIlist->new();
 
    ############################
    # loop over XRT and BAT
    ############################
    foreach my $inst ("bat", "xrt", "uvot") {

        #################################
        # make a list of event file GTIs
        #################################
        my $list;
        if($inst eq "uvot" ) {
            #####################################################
            # UVOT event files have more than one GTI extension
            # so we have to loop over all extensions of all files
            # looking for them
            #####################################################
            $list = Util::GTIlist->new();
            foreach my $file ($filename->get("unfiltered", $inst, "*", "*")) {

                my $fits = Util::FITSfile->new($file);
                my $nhdus = $fits->nhdus();
                for(my $hdu=1; $hdu<$nhdus; $hdu++) {
                    $fits->ext($hdu);
                    my $extname = $fits->keyword("EXTNAME");
                    if($extname =~ /^GTI/) {
                        ##########################
                        # this is a GTI extension
                        ##########################
                        $list->add($file, $extname);
                    }

                }
            }

        } else {
            ##########################################
            # XRT and BAT have one GTI per event file
            # so its easy to make a list of them.
            ##########################################
            $list = Util::GTIlist->new( $filename->get("unfiltered", $inst, "*", "*") );
            $list->extension("GTI");
        }

        ##############################
        # make the merged GTI file
        ##############################
        if($list->count() > 0) {
            my $gti = $filename->get("gti", $inst, "ev", "00");

            $log->entry("Merging ".join(' ', $list->files_with_ext()).
                        " to make $gti");

            $list->merge_or_extract($gti);
            $all_gtis->add($gti, "GTI");
	    $nfi_gtis->add($gti, "GTI") if $inst ne 'bat';
        }

        ##############################################
        # now construct a GTI file for the image data
        ##############################################

        my @gti_intervals;

        my $file;
        foreach $file ($filename->get("rawimage", $inst, "*", "*") ) {
	  my $fits = Util::FITSfile->new($file, 0);

	  if( $inst eq 'uvot' ){
	    my $nhdus = $fits->nhdus();
	    for(my $hdu=1; $hdu<$nhdus; $hdu++) {
	      $fits->ext($hdu);
	      my $tstart = $fits->keyword("TSTART");
	      my $tstop  = $fits->keyword("TSTOP" );
	      if($tstart && $tstop) {
		$log->entry("$file has TSTART=$tstart TSTOP=$tstop");
		if( $tstart==$tstop ){
		  $log->entry("Do not make GTI because TSTART=TSTOP.");
		}else{
		  push @gti_intervals, $tstart, $tstop;
		}
	      }else{
		$log->error(2, 
		      "Could not get TSTART/TSTOP from $file, ext $hdu");
	      }
	    }
	  }else{
	    my $tstart = $fits->keyword("TSTART");
	    my $tstop  = $fits->keyword("TSTOP" );
	    if($tstart && $tstop) {
	      $log->entry("$file has TSTART=$tstart TSTOP=$tstop");
	      if( $tstart==$tstop ){
		$log->entry("Do not make GTI because TSTART=TSTOP.");
	      }else{
		push @gti_intervals, $tstart, $tstop;
	      }
	    }else{
	      $log->error(2, "Could not get TSTART/TSTOP from $file");
	    }
	  }
        } # end of loop over images

        ###################################
        # create the image GTI file
        # and sort it by start time
        ###################################
        if( @gti_intervals ) {
            my $image = $filename->get('gti', $inst, 'im', 0);
            unlink $image;

            $log->entry("creating $inst image GTIs in $image");
	    Util::GTIfile->new($image, @gti_intervals);

	    #####################################
	    # add it to the list of all the GTIs
	    #####################################
	    $all_gtis->add($image, "GTI");
	    $nfi_gtis->add($image, "GTI") if $inst ne 'bat';
        }
    } # end of loop over instruments

    ################################################
    # now merge all the GTI files into a single one
    ################################################
    if($all_gtis->count()) {

        my $gti = $filename->get('gti', 's', 'to', 0);        
	my $nfi_gti = $filename->get('gti', 's', 'nf', 0);

        $log->entry("Merging ".join(' ', $all_gtis->files_with_ext()) ." to make $gti");
        $all_gtis->merge_or_extract($gti);

        if($nfi_gtis->count()) {
	  $log->entry("Merging ".join(' ', $nfi_gtis->files_with_ext()) ." to make $nfi_gti");
	  $nfi_gtis->merge_or_extract($nfi_gti);
	}
    }

} # end if body method