#!/usr/bin/perl
#
#  ut_hxdgsoxblc
#

use strict;
use ut_xrtobj;

# main
{

    # Object Istances
    my $ut = ut_xrtobj->new(
                             name    => "hxdgsoxblc",
                             utname  => "ut_hxdgsoxblc",
                             version => "1.0",
    );

    # Set Parameter
    #$ut->setParam("validate", 0);
    my $input   = $ut->getParam( "input_dir" );
    my $output  = $ut->getParam( "output_dir" ) . "/" . $ut->getParam( "utname" );
    $ut->setParam( "output_dir", $output );
    my $chat    = $ut->getParam( "chatter" );
    my $clobber = $ut->getParam( "clobber" );

    # I/O Directory Check
    $ut->checkIODirectory( ) if $ut->isValid;

    # Environment check
    my @chk_env = ( "LHEASOFT", "HEADAS", "CALDB" );

    $ut->checkEnvironment( @chk_env ) if $ut->isValid;

    my $ArchDir = $ut->getParam( "utname" );

    # Input Files Check
    my @chk_inp = (
                    "$input/$ArchDir/testin_ae101005010hxd_0_gsono_cl.evt.gz",
                    "$input/$ArchDir/testin_ae101005010hxd_0_pse_cl.evt.gz",
                    "$input/$ArchDir/testin_ae101005010_hxd_gsobgd.evt.gz",
    );

    $ut->checkExist( "Input", @chk_inp ) if $ut->isValid;

    # Prototype Check
    my @chk_proto = (
                      "$input/$ArchDir/testchk_ae101005010hxd_gso_sr.lc.gz",
                      "$input/$ArchDir/testchk_ae101005010hxd_gso_bg.lc.gz",
                      "$input/$ArchDir/testchk_ae101005010hxd_gso_net.lc.gz",
                      "$input/$ArchDir/testchk_ae101005010hxd_pse.lc.gz",
    );
    $ut->checkExist( "Prototype", @chk_proto ) if $ut->isValid;

    # Executables Files Check
    my @chk_exe = ( "hxdgsoxblc", "ftdiff", "fverify" );
    $ut->checkExecutable( @chk_exe ) if $ut->isValid;

    # expected output files
    my @outfiles = (
                     "$output/testout_ae101005010hxd_gso_sr.lc",
                     "$output/testout_ae101005010hxd_gso_bg.lc",
                     "$output/testout_ae101005010hxd_gso_net.lc",
                     "$output/testout_ae101005010hxd_pse.lc"
                   );
    unlink @outfiles;

    # run the task
    my %par_task = (
                     input_fname     => "$chk_inp[ 0 ]",
                     pse_event_fname => "$chk_inp[ 1 ]",
                     bkg_event_fname => "$chk_inp[ 2 ]",
                     gti_fname       => 'NONE',
                     outstem         => "$output/testout_ae101005010",
                     bkgsub          => 'yes',
                     nxb_scale       => '1.0',
                     binlc           => '16.0',
                     lcthresh        => '0.5',
                     synch           => 'yes',
                     cleanup         => 'yes',
                     chatter         => "$chat",
                     clobber         => 'yes',
                     history         => 'yes',
                     mode            => 'ql',
    );

    print "task start\n";
    $ut->runTask( %par_task ) if $ut->isValid;
    if ( $ut->isValid ) {

        # fverify
        foreach my $outfile ( @outfiles ) {
            $ut->fverify( "$outfile" );
        }

        # ftdiff
        for ( my $i = 0; $i < @outfiles; $i++ ) {
            $ut->ftdiff( "$outfiles[ $i ]", "$chk_proto[ $i ]" );
        }
    }

    $ut->delete( );
}
