Subs::XrtImages (version 0.0)


package Subs::XrtImages;
##############################################################################
#
# DESCRIPTION: Extract transform and plot images
#
# HISTORY: 
# HISTORY: $Log: XrtImages.pm,v $
# HISTORY: Revision 1.7  2004/08/27 18:54:58  apsop
# HISTORY: Just call Images::convert_raw_to_sky in body.
# HISTORY:
# HISTORY: Revision 1.6  2004/05/06 20:02:34  dah
# HISTORY: Add version number back into the header comments.
# HISTORY:
# HISTORY: Revision 1.5  2004/04/16 20:21:18  dah
# HISTORY: Begin using embedded history records
# HISTORY:
#
# VERSION: 0.0
#
#
##############################################################################


use Subs::Sub;
use Subs::Images;
use Util::Xanadu;

@ISA = ("Subs::Images");
use strict;
sub new {
    my $proto=shift;
    my $self=$proto->SUPER::new();

    $self->{DESCRIPTION}="Extracting, merging and plotting images for XRT";

    return $self;
}

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

sub body {
    my $self=shift;

    # $self->process_img_mode;
    #####################################
    # extract images from the event data
    #####################################

###########  $self->extract_images();
    $self->image_raw_to_sky('xrt');
###########  $self->plot_images();

    # $self->make_products;
} # end of body method

sub process_img_mode {
    my $self=shift;

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

    # my $gtifile = $filename->get('gti', 'xrt', 'im', '*');
    my $gtifile = 'tmp.gti';
    my $mkffile = $filename->get('filter', 'x');
    if (not -f "$mkffile") {
       $log->error(1, "missing XRT filter file, unable to create images");
    }

    ##################################
    # run xrtscreen
    ##################################

    my $xrtscreen = Util::HEAdas->new('xrtscreen');
    # parameter values taken from XRT Build 8 Appendix E
    $xrtscreen->params({
            outdir => '.',
            createattgti => 'no',
            createinstrgti => 'yes',
            gtiscreen => 'no',
            evtscreen => 'no',
            hkrangefile => $filename->fetch_cal('rangefile', 'xrt'),
            evtrangefile => $filename->fetch_cal('graderange', 'xrt'),
            mkffile => $mkffile,
            gtifile => $gtifile,
            # usrgtifile => 'NONE',
            # gtiexpr => 'NONE',
            # gtiext => 'GTI',
            outfile => 'NONE',
            # timecol => 'TIME',
            expr => 'none',
            exprgrade => 'none',
            })->is_script(1);

    my $bptable = $filename->get('onboardbp', 'xrt', 'im', '*');
    $bptable = 'NONE' unless $bptable;

    my $xrtimage = Util::HEAdas->new('xrtimage')->params({
                       biasfile => $filename->fetch_cal('imagebias', 'xrt'),
                       bpfile => $filename->fetch_cal('badpix', 'xrt'),
                       bptable => $bptable,
                       gtifile => $gtifile,
                      # ->is_script(1);
                      });

    foreach my $rawFile ($filename->get('rawimage', 'xrt', 'im', '*')) {

		  unlink($gtifile);

        $xrtscreen->params({
             infile => $rawFile,
             })->run;

        my $corrFile = $filename->corresponding('rawimage', 'corrimage', $rawFile);
print "$rawFile => $corrFile\n";
        $xrtimage->params({'infile' => $rawFile,
                           'outfile' => $corrFile,
                   })
        ->run();
    }
}

#############################################################################
# Extract images from filtered event data
#############################################################################
sub extract_images {
    my $self=shift;

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

    $log->entry("Extracting xrt images from event data");

    ###########################
    # fire up the extractor
    ###########################
    my $extractor = Util::Extractor->new();
    $extractor->verbose(2);

    $log->entry("Mode photon counting");
    $extractor->instrument('xrt', 'phot');

    #################################
    # get a list of event files
    ##################################
    my $evt;
    foreach $evt ($filename->get('event', 'xrt', 'phot*', '*')) {

      $extractor->infiles($evt);

      my $img = $filename->corresponding("event",
                                         "skyimage",
                                         $evt);
        
      $log->entry("Extracting $img from $evt");
      $extractor->coord_type('sky')
                ->outfile($img, "image")
                ->run();

    } # end of loop over event files

} # end if extract images method

################################################################################
#
################################################################################
sub plot_images {
    my $self = shift;

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

    my $max_image_dimen=1024;

    $log->entry("Plotting xrt images");
    my $ximage = Util::Xanadu->new("ximage");

    my $type;
    foreach $type ("detimage", "skyimage") {

        my $plot_type = $type;
        $plot_type =~ s/image/plot/;

        ###############################
        # loop over files of this type
        ###############################
        my $file;
        foreach $file ($filename->get($type, 'xrt', "*", "*")) {

            my $plot = $filename->corresponding($type, $plot_type, $file);


            ########################################
            # determine if we need to bin the image
            ########################################
            my $dimen = Util::FITSfile->new($file,0)->keyword("NAXIS1");
            my $bin=1;
            for($bin=1; $max_image_dimen*$bin<$dimen; $bin++) {}


            $log->entry("Plotting $file in $plot binned by $bin");

            my $grid="";
            if($type =~ /^sky/ ) { $grid = "grid" }
            $ximage->script(
                            "read/fits/rebin=$bin $file",
                            "smooth",
                            "cpd /vcps",
                            "cey 2000",
                            "disp",
                            $grid,
                            "scale",
                            "exit" );

            $ximage->run();


            rename "pgplot.ps", $plot;


        }
    }

} # end of plot_images method



sub make_products
{
    my $self = shift;

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

    $log->entry("making products");

    my $producer = Util::HEAdas->new('xrtproducts')
           ->params({
               outdir => '.',
               display => 'no',
               plotdevice => 'gif',
               mode => 'im',
               })
			->is_script(1);


    foreach my $skyFile ($filename->get('skyimage', 'xrt', 'im', '*')) {

        $producer->params({
               infile => $skyFile,
               })
			->run;

    }

} # end of make_products method