###
###     PStamp/Job.pm
###     subroutines and constants related to Postage Stamp Jobs
###

package PStamp::Job;

use strict;
use warnings;

our $VERSION = '1.0';

use base qw( Exporter );

our @EXPORT_OK = qw( 
                    locate_images
                    resolve_project
                    );
our %EXPORT_TAGS = (standard => [@EXPORT_OK]);


use IPC::Cmd 0.36 qw( can_run run );

use PS::IPP::Metadata::List qw( parse_md_list );
use PS::IPP::Config qw( :standard );

### my @images = locate_images($image_db, $req_type, $img_type, $id, $lookup_class_id,
###            $mjd_min, $mjd_max, $filter);

sub locate_images {
    my $ipprc    = shift;   # required
    my $image_db = shift;   # required
    my $req_type = shift;   # required
    my $img_type = shift;   # required
    my $id       = shift;   # required unless req_type eq bycoord
    my $class_id = shift;
    my $x        = shift;
    my $y        = shift;
    my $mjd_min  = shift;
    my $mjd_max  = shift;
    my $filter   = shift;
    my $verbose  = shift;

    # we die in response to bad data in request files
    die "Unknown req_type: $req_type" if ($req_type ne "byid") and ($req_type ne "byexp")
                                        and ($req_type ne "bycoord");
    if ($req_type eq "bycoord") {

        # run
        # regtool -dbname $image_db -processedimfile -time_begin $mjd_min -time_end = $mjd_max -filter $filter
        #
        my $results = lookup_bycoord($ipprc, $image_db, $x, $y, $mjd_min, $mjd_max, $filter, $verbose);

        $req_type = "byexp";
        $id = $results->{exp_name};
    }

    my $results = lookup($ipprc, $image_db, $req_type, $img_type, $id, $class_id, $verbose);

    return $results;
}

sub lookup {
    my $ipprc    = shift;
    my $image_db = shift;
    my $req_type = shift;
    my $img_type = shift;
    my $id       = shift;
    my $class_id = shift;
    my $verbose = shift;

    my $missing_tools;
    my $regtool = can_run('regtool') or (warn "Can't find regtool" and $missing_tools = 1);
    my $chiptool = can_run('chiptool') or (warn "Can't find chiptool" and $missing_tools = 1);
    my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
    my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
    my $stacktool = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
    if ($missing_tools) {
        warn("Can't find required tools.");
        exit ($PS_EXIT_CONFIG_ERROR);
    }
    my $command;
    my $id_opt;     # option for the lookup
    my $mask_name;
    my $weight_name;
    my $base_name;
    my $want_astrom;
    my $use_class_id;

    # special class_id value "null" means all 
    if ($class_id eq "null") {
        $class_id = undef;
    }

    if ($img_type eq "raw") {
        $command = "$regtool -processedimfile -dbname $image_db";
        $id_opt = "-exp_id";
        $command .= " -class_id $class_id" if $class_id;
        $want_astrom = 0;
        $use_class_id = 1;
    } elsif ($img_type eq "chip") {
        $command = "$chiptool -processedimfile -dbname $image_db";
        $command .= " -class_id $class_id" if $class_id;
        $id_opt = "-chip_id";
        $mask_name    = "PPIMAGE.CHIP.MASK";
        $weight_name  = "PPIMAGE.CHIP.WEIGHT";
        $base_name    = "path_base"; # name of the field for the chiptool output
        $want_astrom  = 1;
        $use_class_id = 1;
    } elsif ($img_type eq "warp") {
        $command = "$warptool -warped -dbname $image_db";
        $id_opt = "-warp_id";
        $mask_name    = "PSWARP.OUTPUT.MASK";
        $weight_name  = "PSWARP.OUTPUT.WEIGHT";
        $base_name    = "path_base"; # name of the field for the warptool output
    } elsif ($img_type eq "diff") {
        $command = "$difftool -diffskyfile -dbname $image_db";
        $id_opt = "-diff_id";
        $mask_name   = "PPSUB.OUTPUT.MASK";
        $weight_name = "PPSUB.OUTPUT.WEIGHT";
        $base_name   = "path_base";
    } elsif ($img_type eq "stack") {
        $command = "$stacktool -sumskyfile -dbname $image_db";
        $id_opt = "-stack_id";

        $mask_name   = "PPSTACK.OUTPUT.MASK";
        $weight_name = "PPSTACK.OUTPUT.WEIGHT";
        $base_name   = "path_base";
    } else {
        die "Unknown img_type supplied: $img_type";
    }

    if ($req_type eq "byid") {
        $command .= " $id_opt $id";
    } elsif ($req_type eq "byexp") {
        $command .= " -exp_name $id";
    } else {
        die "Unknown req_type supplied: $req_type";
    }

    # run the tool and parse the output
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
                run(command => $command, verbose => $verbose);
    unless ($success) {
        # not sure if we should die here
        print STDERR @$stderr_buf;
        return undef;
    }

    my $buf = join "", @$stdout_buf;
    if (!$buf) {
        return;
    }

    my $mdcParser = PS::IPP::Metadata::Config->new;
    my $images = parse_md_fast($mdcParser, $buf)
        or die ("Unable to parse metadata config doc");

    my $output = [];

    my $camera;
    foreach my $image (@$images) {
        my $base;
        if ($base_name) {
            $base = $image->{$base_name};
            # if base is undef then the output pruducts for thjis image are incomplete
            # This may only happen for warps.
            next if !$base;
        }
        if (!$camera) {
            # This assumes that all images have the same camera
            $ipprc->define_camera($image->{camera});
            $camera = $image->{camera};
        }
        my $out = {};
        $out->{exp_id} = $image->{exp_id};
        $out->{image}  = $image->{uri};
        $out->{state}  = $image->{state}; # state is undef for rawExp, but that's ok
        $class_id = $image->{class_id} if $use_class_id;


        # find the mask and weight images
        if ($base) {
            $out->{mask}   = $ipprc->filename($mask_name,   $base, $class_id) if $mask_name;
            $out->{weight} = $ipprc->filename($weight_name, $base, $class_id) if $weight_name;
        }
        $out->{camera} = $camera;

#XXX gpc1 and mops data that I've been looking at was made with astrometry in chip processing
#so the filerule doesn't match
if ($camera ne "SIMTEST") {
    $want_astrom = 0;
}
       
        $out->{astrom} = find_astrometry($ipprc, $image_db, $image, $verbose) if $want_astrom;

        push @$output, $out;
    }

    return $output;
}

sub lookup_bycoord {
    my $ipprc    = shift;
    my $image_db = shift;
    my $x        = shift;
    my $y        = shift;
    my $mjd_min  = shift;
    my $mjd_max  = shift;
    my $filter   = shift;
    my $verbose  = shift;

    my $missing_tools;
    my $regtool = can_run('regtool') or (warn "Can't find regtool" and $missing_tools = 1);
    if ($missing_tools) {
        warn("Can't find required tools.");
        exit ($PS_EXIT_CONFIG_ERROR);
    }

    # XXX TODO:
    # Full lookups by coordinate require looking at the DVO database. However, 
    # dateobs and filter which is all that MOPS has asked for.

    my $args;
    if ($mjd_min) {
        my $dateobs_min = mjd_to_dateobs($mjd_min);
        $args .= " -dateobs_begin $dateobs_min";
    }
    if ($mjd_max) {
        my $dateobs_max = mjd_to_dateobs($mjd_max);
        $args .= " -dateobs_end $dateobs_max";
    }
    if ($filter) {
        $args .= " -filter $filter";
    }

    if (!$args) {
        # avoid returning every exposure in the DB
        print STDERR "no query arguments provided for bycoord\n";
        return undef;
    }

    # XXX TODO: This query doesn't work Something appears to be out of
    # sync about the times I'm passing and what regtool and or the DB expect
    # the query I used in the C version of locateimages used
    # WHERE dateobs >= FROM_UNIXTIME(%ld) AND dateobs <= FROM_UNIXTIME(%ld + exp_time)

    my $command = "$regtool -processedexp -dbname $image_db $args";

    # run the tool and parse the output
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
                run(command => $command, verbose => $verbose);
    unless ($success) {
        # not sure if we should die here
        print STDERR @$stderr_buf;
        return undef;
    }
    my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files

    my $output = join "", @$stdout_buf;
    if (!$output) {
        print STDERR "no output returned from $command\n" if $verbose;
        return undef;
    }
    my $images = parse_md_fast($mdcParser, $output) or die ("Unable to parse metadata config doc");

    return $images;
}

# find the astrometry file for a given exposure
# return undef if no completed camRun exists
sub find_astrometry {
    my $ipprc = shift;
    my $image_db = shift;
    my $image = shift;      # hashref to output of the lookup tool
    my $verbose = shift;

    my $missing_tools;
    my $camtool = can_run("camtool") or (warn "Can't find camtool" and $missing_tools = 1);
    my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
    if ($missing_tools) {
        warn("Can't find required tools.");
        exit ($PS_EXIT_CONFIG_ERROR);
    }

    my $command = "$camtool -dbname $image_db -processedexp -exp_id $image->{exp_id}";
    # run the tool and parse the output
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
                run(command => $command, verbose => $verbose);
    unless ($success) {
        print STDERR @$stderr_buf;
        return undef;
    }

    my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files

    my $output = join "", @$stdout_buf;
    if (!$output) {
        print STDERR "no output returned from $command\n" if $verbose;
        return undef;
    }
    my $camruns = parse_md_fast($mdcParser, $output);
    if (!$camruns) {
        return undef;
    }

    # If there are multiple cam runs for this exposure, take the last one
    # assuming that it has the best astrometry 
    my $camdata = pop @$camruns;
    if (!$camdata) {
        # no cam runs for this exposure id therefore best astrometry is whatever is in the header
        return undef;
    }
    my $camRoot = $camdata->{path_base};
    my $camera = $image->{camera};
    my $astromSource;
    {
       my $command = "$ppConfigDump -camera $camera -dump-recipe PSWARP -";
        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
            run(command => $command, verbose => $verbose);
        unless ($success) {
            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
            die("Unable to perform ppConfigDump: $error_code");
        }
        my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
            die("Unable to parse metadata config doc");
        $astromSource = metadataLookupStr($metadata, 'ASTROM.SOURCE');
    }

    my $astromFile = $ipprc->filename($astromSource, $camRoot);

    return $astromFile;
}

# splits meta data config input stream into single units to work around the pathalogically
# slow parser. This is similar to and adapted from code in various ippScripts.
sub parse_md_fast {
    my $mdcParser = shift;
    my $input = shift;
    my $output = ();

    my @whole = split /\n/, $input;
    my @single = ();

    my $n;
    while ( ($n = @whole) > 0) {
        my $value = shift @whole;
        push @single, $value;
        if ($value =~ /^\s*END\s*$/) {
	    push @single, "\n";

            my $list = parse_md_list( $mdcParser->parse( join("\n", @single ) ) ) or
                print STDERR "Unable to parse metdata config doc" and return undef;
#            my $num = @$list;
#            print STDERR "list has $num elments\n";
            push @$output, $list->[0];

            @single = ();
        }
    }
    return $output;
}

sub mjd_to_dateobs {
    my $mjd = shift;

    my $ticks = ($mjd - 40587.0) * 86400;

    my ($sec, $min, $hr, $day, $mon, $year) = gmtime($ticks);

    return sprintf "'%4d-%02d-%02d %02d:%02d:%02d'", $year+1900, $mon+1, $day, $hr, $min, $sec;
}

# resolve_project()
# get project specific information
sub resolve_project {
    my $ipprc = shift;
    my $project_name = shift;
    my $dbname = shift;
    die "project is not defined" if !$project_name;

    my $verbose = 1;

    my $missing_tools;
    my $pstamptool = can_run("pstamptool") or (warn "Can't find pstamptool" and $missing_tools = 1);
    if ($missing_tools) {
        warn("Can't find required tools.");
        exit ($PS_EXIT_CONFIG_ERROR);
    }

    my $command = "$pstamptool -project -name $project_name";
    $command .= " -dbname $dbname" if defined $dbname;
    # run the tool and parse the output
    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
                run(command => $command, verbose => $verbose);
    unless ($success) {
        print STDERR @$stderr_buf;
        return undef;
    }

    my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files

    my $output = join "", @$stdout_buf;
    if (!$output) {
        print STDERR "no output returned from $command\n" if $verbose;
        return undef;
    }
    my $proj_hash = parse_md_fast($mdcParser, $output);


    return $proj_hash->[0];
}
1;
