Util::BATCave (version $)


package Util::BATCave;
##############################################################################
#
# DESCRIPTION: Utilities for BAT pipeline processing
#
#
# HISTORY: $Log: BATCave.pm,v $
# HISTORY: Revision 1.6  2005/06/01 13:38:21  apsop
# HISTORY: Change the burst gti file type from gti to burstgti.
# HISTORY:
# HISTORY: Revision 1.5  2005/02/09 23:06:29  apsop
# HISTORY: Added message identifiers.
# HISTORY:
# HISTORY: Revision 1.4  2004/11/10 17:59:43  apsop
# HISTORY: Comment out get_bayes, as it is not used, and used SimpleFITS.
# HISTORY:
# HISTORY: Revision 1.3  2004/11/09 22:22:29  apsop
# HISTORY: Corrected name of peak GTI.
# HISTORY:
# HISTORY: Revision 1.2  2004/11/09 22:03:53  apsop
# HISTORY: All GTIs now collected in a single file.
# HISTORY:
# HISTORY: Revision 1.1  2004/11/09 19:07:31  apsop
# HISTORY: Adding BAT pipeline support modules.
# HISTORY:
#
# VERSION: $Revision: 1.6 $
#
#
##############################################################################


use strict;

use Util::SwiftTags;


sub chan1
{
	return '15-350';   # keV
}


sub chan4
{
	return '15-25,25-50,50-100,100-350';   # keV
}


sub get_gti
{
	my ($self, $type) = @_;

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

	if ($type eq 'NONE') {
		return 'NONE';
	}

	my $gti = undef;

	my $gtifile = $filename->get('burstgti', 'bat', '', 0);

	if (not $gtifile or not -f $gtifile) {
		$log->error([ 1, BAD_GTI ], "missing BAT GTI file");
	}

	elsif ($type eq 'GTI_TOT' or $type eq 'GTI_PEAK'
			or $type eq 'GTI_T90' or $type eq 'GTI_T50'
			or $type eq 'GTI_BKG1' or $type eq 'GTI_BKG2'
			or $type eq 'GTI_BAYES') {

		$gti = $gtifile . "[$type]";
	}

	else {
		$log->error([ 1, BAD_GTI ], "invalid BAT GTI type requested [$type]");
	}

	if (not $gti) {
		$log->error([ 2, BAD_GTI ], "unable to locate BAT $type GTI");
	}

	return $gti;
}


## sub get_bayes
## {
## 	my ($self, $key) = @_;
## 
## 	my $log      = $self->log;
## 	my $filename = $self->filename;
## 	my $jobpar   = $self->jobpar;
## 
## 	my $gti_bayes = get_gti($self, 'GTI_BAYES');
## 
## 	my $value = undef;
## 
## 	my $status = Util::SimpleFITS->open("<$gti_bayes")
## 			->readkey($key => $value)
## 			->close
## 			->status;
## 
## 	if ($status) {
## 		$log->error(2, "unable to determine BAT Bayes $key [$status]");
## 	}
## 
## 	return $value;
## }


sub get_tdrss_ack
{
	my ($self) = @_;

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

	my $ack = undef;

	my @acks = $filename->get('tdmess', 'bat', 'ce', '*');
	if (@acks) {
		my $root = $filename->sequence_specific;
		my @filtered = grep /$root/, @acks;
		$ack = $filtered[0];
	}

	if (not $ack) {
		$log->error([ 1, BAT_NO_CENTROID ], 'no BAT TDRSS centroid message');
	}

	return $ack;
}


sub get_bscalemap_index
{
	my ($log, $obsid) = @_;
	my $index = undef;
	# 2020-10-14 was using
	# 	$index = substr($obsid, 2, 8)
	# 	$index =~ s/^0+//;
	# as proxy for trigger which is flawed:
	# - it assumes two leading zeros
	# - it requires the first 2 segment digits to be 00
	# ShortTermRepository should be enhanced to support custom
	# binning (by 10000 in this case) instead of hard-coded 1 million.
	# Note that this is only used on OBS_ID keyword from bscalemaps so
	# segment digits are 0s.
	if ($obsid =~ /^\d{11}$/) {
		$index = substr($obsid, 0, 10);
		$index =~ s/^0+//;
		if ($index eq '') {
			$index = '0';
		}
		if (substr($obsid, -3) ne '000') {
			$log->error(1, "get_bscalemap_index: unexpected obsid '$obsid'");
		}
	}
	else {
		$log->error(2, "get_bscalemap_index: invalid obsid '$obsid'");
	}

	return $index;
}


1;