Subs::ExtractGTIs (version 0.0)


package Subs::ExtractGTIs;
##############################################################################
#
# DESCRIPTION: 
#
# HISTORY: 
# HISTORY: $Log: ExtractGTIs.pm,v $
# 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 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();
    
    ####################################################
    # initialize fcreate to make a GTI file for images
    ####################################################
    my $column_file = "fcreate_gti_columns.tmp";
    my $data_file = "fcreate_gti_data.tmp";

    my $fcreate = Util::Ftool->new("fcreate")
                             ->params({cdfile   => $column_file,
                                       datafile => $data_file,
                                       headfile => " ",
                                       tbltype  => "binary",
                                       nskip    => 0,
                                       nrows    => "0",
                                       history  => "yes",
                                       morehdr  => 0,
                                       extname  => "GTI",
                                       anull    => " ",
                                       inull    => 0,
                                       clobber  => "yes"});

    open  COLUMNS, ">$column_file";
    print COLUMNS "START 1D\n";
    print COLUMNS "STOP 1D\n";
    close COLUMNS;

    ########################################################
    # 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
        ##############################################
        unlink $data_file;
        open DATA, ">$data_file";

        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{
		  print DATA "$tstart $tstop\n";
		}
	      }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{
		print DATA "$tstart $tstop\n";
	      }
	    }else{
	      $log->error(2, "Could not get TSTART/TSTOP from $file");
	    }
	  }
        } # end of loop over images
        close DATA;

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

            $log->entry("createing $inst image GTIs in $image");

            ######################
            # create the file
            ######################
            $fcreate->params({outfile=>$image})
                    ->run();

            unless($fcreate->had_error() ) {
                ########################
                # sort by time
                ########################
                my $fits = Util::FITSfile->new($image,1);
                $fits->cols("START")
                    ->sort();

                #####################################
                # set the MJDREF keyword
                #####################################
		$fits->keyword('MJDREFI', 51910, 'MJD reference day 01 Jan 2001 00:00:00');
		$fits->keyword('MJDREFF', 7.428703700000000E-04, 
			       'MJD reference (fraction of day) 01 Jan 2001 00:00:00');
		
		#######################
		# Set tstart and tstop
		#######################
		$fits->keyword('TSTART', ($fits->cols('START')->table())[0] );
		$fits->keyword('TSTOP', ($fits->cols('STOP')->table())[-1] );

                #####################################
                # add it to the list of all the GTIs
                #####################################
                $all_gtis->add($image, "GTI");
		$nfi_gtis->add($image, "GTI") if $inst ne 'bat';
            } else {
                #################################
                # couldn;'t create the file so
                # clean up any debris
                ################################
                unlink $image;
            }
        }



    } # end of loop over instruments

    #############################
    # clean up fmerge temp files 
    #############################
    unlink $column_file;
    unlink $data_file;

    ################################################
    # 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