Subs::XrtTdrss (version 0.0)


package Subs::XrtTdrss;
##############################################################################
#
# DESCRIPTION: This subroutine runs the xrttdrss script to process xrt tdrss 
#              data.
#
# HISTORY: 
# HISTORY: $Log: XrtTdrss.pm,v $
# HISTORY: Revision 1.5  2006/03/06 15:06:22  apsop
# HISTORY: Use new filenames for xrt tdrss postage stamp images.
# HISTORY:
# HISTORY: Revision 1.4  2006/01/31 16:49:45  apsop
# HISTORY: Fix bugs in renaming and combining postage stamp images.
# HISTORY:
# HISTORY: Revision 1.3  2006/01/29 19:39:47  apsop
# HISTORY: Code for combining multiple tdrss images into one file.
# HISTORY:
# HISTORY: Revision 1.2  2005/11/08 20:10:42  apsop
# HISTORY: <Previous comment bogus>New module for processing xrt tdrss data.
# HISTORY:
# HISTORY: Revision 1.1  2005/11/08 19:22:28  apsop
# HISTORY: Populate the TIMELIST and DATALIST hashes. Used to be an SWCheckInput.
# HISTORY:
# HISTORY:
#
# VERSION: 0.0
#
##############################################################################


use Subs::Sub;
use Util::SwiftTags;

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

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

    $self->{DESCRIPTION}="Process XRT tdrss messages";

    return $self;
}

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

sub body {
    my $self=shift;

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

    my $tdrss = Util::HEAdas->new('xrttdrss')
                            ->params({spec1file    => 'NONE',
				      spec2file    => 'NONE',
				      posfile      => 'CALDB',
				      imbiasfile   => 'CALDB',
				      pdbiasfile   => 'CALDB'})
			    ->is_script(1);

    my @images = $filename->get('tdrawimage', 'x', 'ps', '*');
    foreach my $rw_image (@images){
      my $index = ( $filename->parse($rw_image, 'tdrawimage') )[2];
      my $sk_image = $rw_image;
      $sk_image =~ s/_rw/_sk/;
      $tdrss->params({imagefile    => $rw_image,
		      outimagefile => $sk_image})
	    ->run();
    }

    ###############################################
    # Check it we need to reformat the image files
    ###############################################
    my $tempim = 'xrttdrss_fits.tmp';
    my $create = Util::Ftool->new('fimgcreate')
	                    ->params({bitpix => 8,
				      naxes => '0',
				      datafile => 'none',
				      outfile => $tempim});

    my $append = Util::HEAdas->new('ftappend');
    my $cphead = Util::Ftool->new('cphead');

    foreach my $type ('tdrawimage', 'tdskyimage'){
      @images = sort( $filename->get($type, 'x', 'ps', '*') );
      if(@images){
	my $image = $images[0];
	my $imfits = Util::FITSfile->new($image);
	if( $imfits->nhdus() <= 1 || 
	    $imfits->keyword('EXTNAME') !~ /(LNG|SHT)\d{9}I\d{2}/ ){

	    $create->run();
	    foreach $image (@images){
	      $append->params({infile => $image .'[0]',
			       outfile => $tempim})
		     ->run();
	    }

	    unlink @images;
	    rename $tempim, $image;
	    $imfits = Util::FITSfile->new($image);

	    my ($tstart, $tstop) = (1E10, 0);
	    for(my $ext=1; $ext < $imfits->nhdus(); $ext++){
	      $imfits->ext($ext);
	      my ($start, $stop) = ($imfits->keyword('TSTART'), $imfits->keyword('TSTOP'));
	      $tstart = $start if $start < $tstart;
	      $tstop = $stop if $stop > $tstop;
	      my $int = int($start);
	      my $frac = int( ($start-$int)/2E-2 );

	      my $mode = $imfits->keyword('DATAMODE');
	      my $pneu = 'UNK';
	      $pneu = 'LNG' if $mode =~ /LONGIMA/;
	      $pneu = 'SHT' if $mode =~ /SHORTIMA/;

	      $imfits->keyword('EXTNAME', $pneu.$int.'I'.$frac);
	    }
	    my $start_date = Util::Date->new($tstart);
	    my $stop_date = Util::Date->new($tstop);

	    $imfits->ext(0);
	    $imfits->begin_many_keywords();
	    $imfits->keyword('INSTRUME', 'XRT     ');
	    $imfits->keyword('TSTART', $tstart);
	    $imfits->keyword('TSTOP', $tstop);
	    $imfits->keyword('DATE-OBS', $start_date->date().'T'.$start_date->time() );
	    $imfits->keyword('DATE-END', $stop_date->date().'T'.$stop_date->time() );
	    $imfits->end_many_keywords();
	}
      }
    }
    
}

1;