Subs::BATQmap (version $)


package Subs::BATQmap;
##############################################################################
#
# DESCRIPTION: Create BAT quality map
#
#		Port of Craig Markwardt's BAT::qmap modules to SDC.
#
#
# HISTORY: $Log: BATQmap.pm,v $
# HISTORY: Revision 1.5  2005/02/09 23:39:01  apsop
# HISTORY: Added message identifiers.
# HISTORY:
# HISTORY: Revision 1.4  2004/11/12 00:01:10  apsop
# HISTORY: Put in test for $enable_map being empty or undefined.
# HISTORY:
# HISTORY: Revision 1.3  2004/11/10 18:02:06  apsop
# HISTORY: Replace SimpleFITS call for getting TRIGTIME with FITSfile call.
# HISTORY:
# HISTORY: Revision 1.2  2004/11/09 22:01:48  apsop
# HISTORY: Corrected retrieval of BAT enable map from repository.
# HISTORY:
# HISTORY: Revision 1.1  2004/11/08 18:37:43  apsop
# HISTORY: Module for creating BAT quality map.
# HISTORY:
# HISTORY: Revision 1.1  2004/09/24 20:51:19  wiegand
# HISTORY: Initial revision
# HISTORY:
#
# VERSION: $Revision: 1.5 $
#
#
##############################################################################


use Subs::Sub;


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

use Util::HEAdas;
use Util::BATCave;
use Util::SwiftTags;


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

	$self->{DESCRIPTION}= 'Creating BAT quality map';

	return $self;
}

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

sub body {

	my $self = shift;

	my $log      = $self->log;
	my $filename = $self->filename;
	my $procpar  = $self->procpar;
	my $jobpar   = $self->jobpar;


	################################################
	# export all the shared files to the repository
	################################################
	$log->entry("Exporting gainoff and dpflags files to the shared repository");
	$filename->export_to_repository('bgaoff', 'bat');
	$filename->export_to_repository('bdetflag', 'bat');


	#################################################
	# look up BAT trigger time
	# TODO: should this be done in LocateBurst and written to jobpar? YES!!
	#################################################
	my $ack = Util::BATCave::get_tdrss_ack($self);
 
	my $enable_map;
	if ($ack) {

		my $ack_fits = Util::FITSfile->new($ack, 0);
		my $trigtime = $ack_fits->keyword('TRIGTIME');
		unless($trigtime) {
		        $trigtime = 0;
			$log->error(1, "unable to get TRIGTIME from $ack");
		}

		# TODO: write TRIGTIME to each event file[EVENTS]

		$enable_map = $filename->fetch_from_repository('bdetflag', 'bat', '', $trigtime);

	}

	my @event_files = $filename->get('unfiltered', 'bat', 'evshto', '*');
	my $inlist = join(',', @event_files);
	if (not $inlist) {
		$log->entry('no BAT event files, will not make quality map');
		return;
	}

	my $dpitmp = 'dpi.tmp';
	unlink($dpitmp);

	my $batbinevt = Util::HEAdas->new('batbinevt')
			->params({
				infile => $inlist,
				outfile => $dpitmp,
				outtype => 'DPI',
				timedel => 0,
				timebinalg => 'uniform',
				energybins => '-',
				weighted => 'NO',
				outunits => 'COUNTS',
				clobber => 'yes',
				})
			->run;

	if ($batbinevt->had_error or not -f $dpitmp) {
		$log->error([ 2, BAT_TASK_ERROR ], "unable to create quality map");
		unlink($dpitmp);
		return;
	}

	my $qmap = $filename->get('qualcal', 'bat', 'cb', 0);
	unless($enable_map && -f $enable_map) {
		$enable_map = 'NONE';
	}

	my $bathotpix = Util::HEAdas->new('bathotpix')
			->params({
				infile => $dpitmp,
				outfile => $qmap,
				detmask => $enable_map,
				clobber => 'yes',
				})
			->run;

	if ($bathotpix->had_error or not -f $qmap) {
		$log->error([ 2, BAT_TASK_ERROR ], "unable to create quality map");
	}


	unlink($dpitmp);

}


1;