#! /usr1/local/bin/perl5 
$version ="2.01";
$date  ="2002-12-31";
$author = "kaa";

# This script does CTI correction where appropriate and gain correction
# where CTI correction is not possible. If a third argument is given it is 
# used as a gain file and overrides any other choices.

# Get and parse the command line option 
    if ($ARGV[0]=~/^-(\S+)/){
	$option=$1;
	if($option=~/h/){$help =1;}
	shift(@ARGV);
    }

if($help){
    print "\n acisgaincorr version $version $date $author\n\n";
    print "usage: acisgaincorr [-h] input_event_file output_event_file gain_file\n\n";
    print "This script does CTI correction if the observation was done at a temperature\n";
    print "for which this is available. If CTI correction is not possible then the\n";
    print "script just updates the gain using the most recent gain file available in\n";
    print "the CALDB. This script requires CIAO to be set up and runs the tool \n";
    print "acis_process_events. If a third argument is given this is taken as the \n";
    print "name of a gain file. In this case gain correction is always performed and \n";
    print "CTI correction is not.\n\n";
    exit(0);
}

# Check that CIAO is really set up

if($ENV{'ASCDS_BIN'} !~/\S/) 
{
    print "\n You need to set up CIAO to use this script.\n\n";
    exit(0);
}

# and we also need HEASOFT...

if($ENV{'LHEASOFT'} !~/\S/) 
{
    print "\n You need to set up HEASOFT to use this script.\n\n";
    exit(0);
}

# Check that the user gave the correct number of arguments.

if(@ARGV != 2 && @ARGV != 3)
{
    print "\n usage : acisgaincorr [-h] input_event_file output_event_file gain_file\n";
    print " type acisgaincorr -h to get more information\n\n";
    exit(0);
}

$infile  = $ARGV[0];
$outfile = $ARGV[1];
if(@ARGV == 3) {
    $gainfile = $ARGV[2];
} else {
    $gainfile = 'none';
}

# If the gainfile has not been input then find out what it should be and
# whether CTI or gain correction is required.

$docti = "no";
if ($gainfile eq 'none') {

# First get the detector temperature to see whether we can do CTI correction.
# Just look for an observation on or after 2000-01-29.

   $command = "fkeypar $infile DATE-OBS\n";
   system($command);
   $obsdate = `pget fkeypar value`;
   $year  = substr($obsdate,1,4);
   $month = substr($obsdate,6,2);
   $day   = substr($obsdate,9,2);

   print "\n","Observation performed on ",$month,"/",$day,"/",$year,"\n";

   if ($year > 2000) {
      $docti = "yes";
   } elsif ($year == 2000 && ( ($month == 1 && $day > 29) || $month > 1 )) {
         $docti = "yes";
   }

}     

# Read the event extension to find the gain file actually used in processing

$command = "fkeypar $infile\[EVENTS\] GAINFILE\n";
system($command);
$gainfile_used = `pget fkeypar value`;
$gainfile_used =~ tr/'\n//d;

# Now find out what mode the data were acquired in so we can set the eventdef
# correctly. Also check whether CTI correction can be done - this is not possible
# on GRADED data.

$command = "fkeypar $infile\[EVENTS\] READMODE\n";
system($command);
$readmode = `pget fkeypar value`;
$readmode =~ tr/a-z/A-Z/;
$readmode =~ tr/ '\n//d;
$command = "fkeypar $infile\[EVENTS\] DATAMODE\n";
system($command);
$datamode = `pget fkeypar value`;
$datamode =~ tr/a-z/A-Z/;
$datamode =~ tr/ '\n//d;

if ( $readmode eq "TIMED" ) {

    if ( $datamode eq "FAINT" || $datamode eq "VFAINT" ) {

       $eventdef = ")stdlev1";

    } elsif ( $datamode eq "GRADED" ) {

       $eventdef = ")grdlev1";
       $docti = "yes";

    } else {

       print $datamode," is not a recognized value of TIMED DATAMODE\n";
       exit(0);

    }

} elsif ( $readmode eq "CONTINUOUS" ) {

    if ( $datamode eq "3X3" ) {

	$eventdef = ")cclev1";

    } elsif ( $datamode eq "33GRADED" ) {

	$eventdef = ")ccgrdlev1";
        $docti = "no";

    } else {

       print $datamode," is not a recognized value of CONTINUOUS DATAMODE\n";
       exit(0);

    }

} else {

    print $readmode," is not a recognized value of READMODE\n";
    exit(0);

}

# Now put together the acis_process_events command.

$command = "punlearn acis_process_events";
system($command);

if ($docti eq "yes") {

   $command = "acis_process_events eventdef=\"$eventdef\" stop=none infile=$infile outfile=$outfile apply_cti=yes";

} else {

   if ( $gainfile eq "none" ) {

      $command = "acis_process_events eventdef=\"$eventdef\" stop=none infile=$infile outfile=$outfile apply_cti=no";

   } else {

      $command = "acis_process_events eventdef=\"$eventdef\" stop=none infile=$infile outfile=$outfile apply_cti=no gainfile=$gainfile";

   }

}

# and run it...

print "\n",$command,"\n";
system($command);

if ($docti eq "yes") {
    print "\n Performed CTI correction.\n";
}
print " Old gainfile used : ", $gainfile_used, "\n";
$command = "fkeypar $outfile\[EVENTS\] GAINFILE";
system($command);
($newgainfile_used = `pget fkeypar value`) =~ tr/\n//d;
print " New gainfile used : ", $newgainfile_used, "\n";