Subs::Swift2FITS (version 0.0)


package Subs::Swift2FITS;
##############################################################################
#
# DESCRIPTION: 
# DESCRIPTION: 
#
# HISTORY: 
# HISTORY: $Log: Swift2FITS.pm,v $
# HISTORY: Revision 1.14  2006/04/27 15:54:32  apsop
# HISTORY: Split off combining of engineering files to a new combined_fits method.
# HISTORY:
# HISTORY: Revision 1.13  2006/01/29 19:38:36  apsop
# HISTORY: Sort hkfiles before combining so extensions have coherent ordering.
# HISTORY:
# HISTORY: Revision 1.12  2004/12/24 02:02:36  apsop
# HISTORY: Add 776, 782 to apids to be processed by uvot2fits.
# HISTORY:
# HISTORY: Revision 1.11  2004/11/03 15:46:40  apsop
# HISTORY: Remove empty TUNIT keywords from hk files.
# HISTORY:
# HISTORY: Revision 1.10  2004/10/20 18:49:45  apsop
# HISTORY: Add in additonal apids which are handled by uvot2fits.
# HISTORY:
# HISTORY: Revision 1.9  2004/08/22 18:42:25  apsop
# HISTORY: Initial changes for new file classes and repository
# HISTORY:
# HISTORY: Revision 1.8  2004/07/19 16:02:05  apsop
# HISTORY: Add 491 to list of BAT apids processed by bat2fits.
# HISTORY:
# HISTORY: Revision 1.7  2004/06/11 13:15:47  apsop
# HISTORY: Add 605 as apid to be handled by uvot2fits.
# HISTORY:
# HISTORY: Revision 1.6  2004/06/08 00:04:26  apsop
# HISTORY: Fix problem with deleting bad hk files.
# HISTORY:
# HISTORY: Revision 1.5  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.4  2004/04/30 16:20:30  dah
# HISTORY: Add in additional FITS info to hk files if available.  Fix code for selecting APIDs for hk processing
# HISTORY:
# HISTORY: Revision 1.3  2004/04/28 13:47:35  dah
# HISTORY: Make one method for extracting hk, and put it in Swift2FTIS superclass.
# HISTORY:
# HISTORY: Revision 1.2  2004/04/16 20:21:18  dah
# HISTORY: Begin using embedded history records
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


use Subs::SwiftSub;

@ISA = ('Subs::SwiftSub');
use strict;

my %sci_ids;

sub BEGIN {
  ######################################################
  # List of apid's NOT to be converter using 'unpacket'
  ######################################################
  my @bat_ids = (299, 334, 335, 337, 338, 339, 341, 342, 351, 384, 385, 386, 387, 400, 401, 402, 
		 403, 404, 416, 417, 481, 484, 485, 488, 489, 490, 491, 494, 496, 497, 526, 527, 528, 
		 529, 530, 533, 567, 568, 570, 571);


  my @uvot_ids = (605, 688, 704, 776, 782, 843, 848, 850, 853, 854, 855, 856, 857, 858, 860, 861, 1104);
  
  my @xrt_ids = (1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162, 1163, 1164, 
		 1165, 1166, 1167, 1248, 1249, 1250, 1253, 1264, 1280, 1328, 1329, 1330, 1331, 
		 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341, 1342, 1343, 1344,
		 1345, 1346, 1351);

  #######################################################
  # Convert the lists into hashes and store in %sci_ids
  #######################################################

  my (%bat_hash, %xrt_hash, %uvot_hash, %swift_hash);

  @bat_hash{@bat_ids} = (1) x @bat_ids;
  $sci_ids{'bat'} = \%bat_hash;

  @xrt_hash{@xrt_ids} = (1) x @xrt_ids;
  $sci_ids{'xrt'} = \%xrt_hash;

  @uvot_hash{@uvot_ids} = (1) x @uvot_ids;
  $sci_ids{'uvot'} = \%uvot_hash;

  $sci_ids{'swift'} = \%swift_hash;
}

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

    return $self;
}

##############################################################################
# Extract the HK data
##############################################################################
sub hk_extract {
    my $self=shift;
    my $inst=shift;

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

    $log->entry("Converting $inst HK to FITS");

    my $veto = $sci_ids{$inst};

    my $fthedit = Util::HEAdas->new('fthedit');
    my $ftcopy = Util::HEAdas->new('ftcopy');

    ##################################
    # set up to run the unpacket tool
    ##################################
    my $base = $filename->mission() . $filename->sequence();
    my $bin = $procpar->read('fitspackets');
    my $unpacket = Util::Tool->new($bin,'unpacket');

    my $config = $procpar->read('packets_config');

    my $telem;
    foreach $telem ($filename->get('telemetry', $inst, '*', '*') ) {

        ###############################################
        # skip APIDs which are converted by swift2fits
        ###############################################
        my ($inst, $mode, $apid) = $filename->parse($telem, 'telemetry');

        next if $veto->{$apid};

        ################################
        # do the conversion
        ################################
        $log->entry("converting $telem to FITS");

        $unpacket->command_line("-infile $telem",
                                '-apid_list /aps/lists/swift_apids.list',
                                "-config_dir $config",
                                '-prefix SwiftAPID',
                                '-digits 4',
                                '-suffix .db',
                                "-base $base",
                                "-instrument $inst",
                                '-ext hk');

        $unpacket->run();

        if( $unpacket->had_error() ){
	  foreach my $hk ($filename->get('hk', $inst, sprintf('%x',$apid).'*', '*') ) {
	    $log->entry("Deleting $hk because there was an error in unpacket");
	    unlink $hk;
	  }
	  next;
        }

	##########################################################
	# Convert some of the hk data into a more meaningful form
	##########################################################
	my $apid_string = sprintf('%04d', $apid);
	my $headmod = "$config/SwiftAPID${apid_string}.head";
	my $calcmod = "$config/SwiftAPID${apid_string}.calc";
	foreach my $hk ($filename->get('hk', $inst, sprintf('%03x',$apid).'*', '*') ){
	  if(-f $headmod){
	    $fthedit->params({infile => $hk,
			      keyword => "\@$headmod"})
	            ->run();
	  }
	  if(-f $calcmod){
	    $ftcopy->params({infile => "${hk}\[col \@'$calcmod'\]",
			     outfile => 'convert.tmp'});
	    rename 'convert.tmp', $hk;
	  }
	}
    }

} # end of extract_hk method


sub hk_combine {
  my $self=shift;
  my $inst=shift;

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

  my $eng_file;
  if($inst eq 'swift'){
    $eng_file = $filename->get('scenhk', $inst, '', 0);
  }else{
    $eng_file = $filename->get('enhk', $inst, '', 0);
  }
  my @hkfiles = $filename->get('hk', $inst, '[a-f0-9][a-f0-9][a-f0-9]x[0-9][0-9][0-9]', 0);
  
  $self->fits_combine($eng_file, @hkfiles) if @hkfiles;
}

sub fits_combine {
  my $self=shift;
  my $outfile=shift;
  my @infiles=@_;

  my $log     =$self->log();
  my $filename=$self->filename();
  my $procpar =$self->procpar();
  my $jobpar  =$self->jobpar();
  
  Util::Ftool->new('fextract')
	->params({infile => $infiles[0] . '[0]',
		  outfile => $outfile . '[0]'})
	->run();  
	
  my $append = Util::Ftool->new('fappend');

  foreach my $infile (sort @infiles){
    my @parse = $filename->parse($infile, 'hk');
    my $hkfits = Util::FITSfile->new($infile);

    ###############################
    # Delete empty TUNIT keywords
    ###############################
    my %keywords = $hkfits->keywords();
    $hkfits->begin_many_keywords();
    $hkfits->keyword('EXTNAME', 'hk' . $parse[1]);
    foreach my $key (grep /^TUNIT/, keys %keywords){
      if( $keywords{$key} =~ /^'?\s*'?$/ ){
	$hkfits->keyword("-$key", ' ');
      }
    }
    $hkfits->end_many_keywords();

    $append->params({infile => $infile . '[1]',
		     outfile => $outfile})
      ->run();
  }

  unlink @infiles;
}

1;