Subs::UVOTDB (version 0.0)


package Subs::UVOTDB;
##############################################################################
#
# DESCRIPTION: 
#
# HISTORY: 
# HISTORY: $Log: UVOTDB.pm,v $
# HISTORY: Revision 1.3  2004/12/22 18:17:06  apsop
# HISTORY: Bug fixes to acquistion of UVOT db values.
# HISTORY:
# HISTORY: Revision 1.2  2004/12/14 02:45:37  apsop
# HISTORY: Test for existence of exposure catalog before making db file.
# HISTORY:
# HISTORY: Revision 1.1  2004/12/10 02:12:44  apsop
# HISTORY: Class for making uvot database files.
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


use Subs::Sub;

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

use Astro::FITS::CFITSIO qw(:constants);

use Util::HEAdas;
use Util::Date;
use Util::FITStable;

use Subs::UvotNames;

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

    $self->{DESCRIPTION}="Sort and organize UVOT data";

    return $self;
}

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

sub body
{
    my $self=shift;

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


	# set up the database columns
	my @db = (
		# alert keys
		{ name => 'name',
			type => '80A',
			# $jobpar->read('object')
		},
		{ name => 'orig_target_id',
			type => '1J',
			null => -1,
		        # $jobpar->read('target')
		},
		{ name => 'target_id',
			type => '1J',
			null => -1,
			# substr(0, 8, $jobpar->read('sequence'))
		},
		{ name => 'ra',
			type => '1D',
			unit => 'degree',
			disp => 'F10.5',
			# $jobpar->read('ra')
		},
		{ name => 'dec',
			type => '1D',
			unit => 'degree',
			disp => 'F10.5',
			# $jobpar->read('dec')
		},
		{ name => 'roll_angle',
			type => '1D',
			unit => 'degree',
			disp => 'F10.5',
			# $jobpar->read('roll')
		},
		{ name => 'start_time',
			type => '24A',
			# ecat column ESTART
		},
		{ name => 'stop_time',
			type => '24A',
			# ecat column ESTOP
		},
		{ name => 'orig_obs_segment',
			type => '1J',
			disp => 'I3',
			null => -1,
		        # $jobpar->read('obs')
		},
		{ name => 'obs_segment',
			type => '1J',
			disp => 'I3',
			null => -1,
		},
		{ name => 'orig_obsid',
			type => '11A',
		},
		{ name => 'obsid',
			type => '11A',
		},
		{ name => 'integration_time',
			type => '1E',
			unit => 's',
		},
		{ name => 'window_size',
			type => '80A',
		},
		{ name => 'filter',
			type => '80A',
			key => 'FILTER', # to string
		},
		{ name => 'operation_mode',
			type => '80A',
			# value IMAGE|EVENT|IMAGE&EVENT
		},
		{ name => 'pointing_mode',
			type => '80A',
			# SLEW|SETTLING|...
		},
		{ name => 'filename',
			type => '80A',
			# either IFILEREF or EFILEREF
		},
	);

	my $db = Util::FITStable->new(\@db,
			log => $log,
			which => 'uvot',
			);


	my $path = $filename->get('hk', 'uvot', 'ct');
	unless( $path && -f $path ) {
		$log->entry("Unable to locate uvot exposure catalog");
		return;
	}

	my $uecat = Util::SimpleFITS->open("<$path");
	my $status = $uecat->status;
	if (not $uecat or $status) {
		$log->error(2, "unable to open uvot exposure catalog [$status]");
		return;
	}

	$uecat->move('EXPCATALOG');
	if ($uecat->status) {
		$log->error(2, 'unable to move to EXPCATALOG extension');
		return;
	}

	################################################
	# Compile the data needed for the UVOT db file
	################################################
	my $rows;
	$uecat->handle->get_num_rows($rows, $status);
	my @indef = ('INDEF') x $rows;

	# observation
	$db->set(name => [ ("'".$jobpar->read('object')."'") x $rows ]);
	$db->set(orig_target_id => [ ($jobpar->read('target')) x $rows ]);
	$db->set(target_id => [ (substr $jobpar->read('sequence'), 0, 8) x $rows ]);

        ####################################################################
        # Early observations (before PPSTs) had multiple pointings, so set
        # pointing to indef
        ####################################################################
        if( $jobpar->read('tstart') > 124383600 ){
	  $db->set(ra => [ ($jobpar->read('ra')) x $rows ]);
	  $db->set(dec => [ ($jobpar->read('dec')) x $rows ]);
	  $db->set(roll_angle => [ ($jobpar->read('roll')) x $rows ]);
	}else{
	  $db->set(ra => \@indef );
	  $db->set(dec => \@indef );
	  $db->set(roll_angle => \@indef );	  
	}

	# per exposure data
	my @estart;
	my @estop;
	my @segment;
	my @enet;
	my @windowDX;
	my @windowDY;
	my @filterID;
	my @efile;
	my @ifile;
        my @mode;

	$status = $uecat
			->readcol('ESTART'   => TDOUBLE, [ ], \@estart)
			->readcol('ESTOP'    => TDOUBLE, [ ], \@estop)
			->readcol('SEGMENT'  => TLONG,   [ ], \@segment)
			->readcol('ENET'     => TDOUBLE, [ ], \@enet)
			->readcol('WINDOWDX' => TLONG,   [ ], \@windowDX)
			->readcol('WINDOWDY' => TLONG,   [ ], \@windowDY)
			->readcol('FILTERID' => TSTRING, [ ], \@filterID)
			->readcol('EFILEREF' => TSTRING, [ ], \@efile)
			->readcol('IFILEREF' => TSTRING, [ ], \@ifile)
			->readcol('MODEID'   => TINT,    [ ], \@mode)
			->status;

	if ($status) {
		$log->error(2, 'unable to read EXPCATALOG data');
		return;
	}

	my @start = map { $db->timeString($_) } @estart;
	my @stop = map { $db->timeString($_) } @estop;

	$db->set(start_time => \@start);
	$db->set(stop_time => \@stop);

	$db->set(orig_obs_segment => [ ($jobpar->read('obs')) x $rows ]);
	$db->set(obs_segment => [ (substr $jobpar->read('sequence'), 8, 3) x $rows ]);

	$db->set(orig_obsid => [ ($jobpar->read('target') . $jobpar->read('obs')) x $rows ]);
	$db->set(obsid => [ ($jobpar->read('sequence')) x $rows ]);

	$db->set(integration_time => \@enet);

	my @window;
	for (my $i = 0; $i < $rows; ++$i) {
		push(@window, "'$windowDX[$i] x $windowDY[$i]'");
	}
	$db->set(window_size => \@window);
	$db->set(filter => \@filterID);
	my @mode_name = map { $Subs::UvotNames::modeNames->[$_] } @mode;
	$db->set(operation_mode => \@mode_name);

	my @filename;
        my @pointing_mode;
	for (my $i = 0; $i < $rows; ++$i) {
	        if( $efile[$i] ne 'NONE' ){
		     push(@filename, $efile[$i]);
	             push(@pointing_mode, $efile[$i] =~ /po_uf.evt/ ? 'POINTING' : 'SLEW');
		}else{
		     push(@pointing_mode, 'INDEF');
                     push(@filename, $ifile[$i]);
                }
	}
	$db->set(filename => \@filename);
	$db->set(pointing_mode => \@pointing_mode);

	my $outfile = $filename->get('uvdb', 'proc', '', 0);

	$db->write($outfile);

} # end of body method