IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 20761


Ignore:
Timestamp:
Nov 16, 2008, 1:38:19 PM (17 years ago)
Author:
bills
Message:

move towards this script getting lauched the task

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippScripts/scripts/magic_destreak.pl

    r20732 r20761  
    1616use IPC::Cmd 0.36 qw( can_run run );
    1717use File::Temp qw( tempfile );
     18use File::Basename qw( basename );
    1819use PS::IPP::Metadata::Config;
    1920use PS::IPP::Metadata::List qw( parse_md_list );
     
    2728
    2829# Parse the command-line arguments
    29 my ($magic_id, $camera, $streaks, $stage, $class_id, $skycell_id, $tmpdir, $recoverydir);
     30my ($magic_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base);
     31my ($outroot, $recoveryroot);
    3032my ($replace, $remove);
    3133my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);
    3234
    3335GetOptions(
    34            'magic_id=s'     => \$magic_id,   # Magic identifier
    35            'camera=s'       => \$camera,
     36           'magic_ds_id=s'  => \$magic_ds_id,# Magic destreak run identifier
     37           'camera=s'       => \$camera,     # camera for evaluating file rules
    3638           'streaks=s'      => \$streaks,    # file containing the list of streaks
    3739           'stage=s'        => \$stage,      # raw, chip, warp, or diff
    38            'class_id=s'     => \$class_id,
    39            'skycell_id=s'   => \$skycell_id,
    40            'tmpdir=s'       => \$tmpdir,     # "directory" for temporary images (may be nebulous)
    41            'recoverydir=s'  => \$recoverydir,# "directory" for saving the images of excised pixels
    42            'replace'        => \$replace,    # replace the input images with the results.
    43            'remove'         => \$remove,     # remove the original images when done YIKES!
     40           'stage_id=s'     => \$stage_id,   # exp_id, chip_id, warp_id, or diff_id
     41           'component=s'    => \$component,  # the class_id or skycell_id
     42           'uri=s'          => \$uri,        # uri of the input image
     43           'path_base=s'    => \$path_base,  # path_base of the input
     44           'cam_path_base=s'=> \$cam_path_base,  # path_base of the associated camera run
     45           'outroot=s'      => \$outroot,     # "directory" for temporary images (may be nebulous)
     46           'recoveryroot=s' => \$recoveryroot,# "directory" for saving the images of excised pixels
     47           'replace=s'        => \$replace,    # replace the input images with the results.
     48           'remove=s'         => \$remove,     # remove the original images when done YIKES!
    4449           'save-temps'     => \$save_temps, # Save temporary files?
    4550           'dbname=s'       => \$dbname,     # Database name
     
    5156
    5257pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    53 pod2usage( -msg => "Required options: --magic_id --camera --streaks --stage --tmpdir",
     58pod2usage( -msg => "Required options: --magic_ds_id --camera --streaks --stage --outroot",
    5459           -exitval => 3) unless
    55     defined $magic_id and
     60    defined $magic_ds_id and
    5661    defined $camera and
    5762    defined $streaks and
    5863    defined $stage and
    59     defined $tmpdir;
     64    defined $stage_id and
     65    defined $component and
     66    defined $outroot;
    6067   
    61 if (!defined($class_id) and (($stage eq "raw") or ($stage eq "chip"))) {
    62     carp ("--class_id is required for stage $stage");
    63     exit  $PS_EXIT_PROG_ERROR;
    64 } elsif (!defined($skycell_id) and (($stage eq "warp") or ($stage eq "diff"))) {
    65     carp ("--skycell_id is required for stage $stage");
    66     exit  $PS_EXIT_PROG_ERROR;
     68
     69my ($skycell_args, $class_id, $skycell_id);
     70
     71if (($stage eq "raw") or ($stage eq "chip")) {
     72    $class_id = $component;
     73    $skycell_args = " -class_id $component";
     74} elsif ($stage eq "warp") {
     75    $skycell_id = $component;
     76    $skycell_args = " -skycell_id $component";
     77} elsif ($stage eq "diff") {
     78    $skycell_id = $component;
     79} else {
     80    &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
    6781}
    6882
     
    7488# Look for programs we need
    7589my $missing_tools;
    76 my $magictool      = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
     90my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
    7791my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
     92# todo get rid of this dependence on magic_id
     93my $magictool   = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
    7894if ($missing_tools) {
    7995    warn("Can't find required tools.");
     
    8399my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    84100
    85 my $magictool_args;
    86 my $skycell_args;
    87 my $component;
    88 if ($stage eq "raw") {
    89     $magictool_args = "-rawimfile -class_id $class_id";
    90     $skycell_args = " -class_id $class_id";
    91     $component = $class_id;
    92 } elsif ($stage eq "chip") {
    93     $magictool_args = "-chipprocessedimfile -class_id $class_id";
    94     $skycell_args = " -class_id $class_id";
    95     $component = $class_id;
    96 } elsif ($stage eq "warp") {
    97     $magictool_args = "-warpskyfile -skycell_id $skycell_id";
    98     $skycell_args = " -skycell_id $skycell_id";
    99     $component = $skycell_id;
    100 } elsif ($stage eq "diff") {
    101     $magictool_args = "-diffskyfile -skycell_id $skycell_id";
    102     $component = $skycell_id;
    103 } else {
    104     carp("invalid stage value found: $stage\n");
    105     exit  $PS_EXIT_PROG_ERROR;
    106 }
    107 
    108 ### Get the input information
    109 my $input;         
    110 {
    111     my $command = "$magictool -magic_id $magic_id $magictool_args";
     101my $basename = basename($path_base);
     102
     103#
     104# parse the replace and remove arguments check for errors and set upt
     105# the appropriate paths
     106if (defined($replace) and ($replace eq "F")) {
     107    $replace = 0;
     108}
     109
     110if (($stage eq "raw") and $replace and ! $recoveryroot) {
     111    &my_die("Can not replace raw files without defining recoveryroot.",
     112        $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
     113}
     114
     115if (defined($remove) and ($remove eq "F")) {
     116    $remove = 0;
     117}
     118
     119if ($remove and !$replace) {
     120    &my_die("cannot remove without replace", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
     121}
     122
     123my $backup_uri;
     124if (! $remove) {
     125    $backup_uri = "$outroot/$basename";
     126}
     127
     128my $recovery_uri;
     129if ($recoveryroot) {
     130    $recovery_uri = "$recoveryroot/$basename";
     131}
     132
     133# get skycell list if needed
     134my ($sfh, $skycell_list);         
     135if ($skycell_args) {
     136    my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args";
    112137    $command .= " -dbname $dbname" if defined $dbname;
    113138    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    115140    unless ($success) {
    116141        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    117         &my_die("Unable to perform magictool $magictool_args: $error_code", $magic_id, $component, $error_code);
     142        &my_die("Unable to perform magicdstool -diffskyfile $skycell_args: $error_code", $magic_ds_id, $component, $error_code);
    118143    }
    119144
    120145    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    121         &my_die("Unable to parse metadata config doc", $magic_id, $component, $PS_EXIT_PROG_ERROR);
    122 
    123     my $input_list = parse_md_list($metadata) or
    124         &my_die("Unable to parse metadata list", $magic_id, $component, $PS_EXIT_PROG_ERROR);
    125 
    126     $input = $input_list->[0];
    127 }
    128 
    129 # get skycell list if needed
    130 my ($sfh, $skycell_list);         
    131 if ($skycell_args) {
    132     my $command = "$magictool -magic_id $magic_id -diffskyfile $skycell_args";
    133     $command .= " -class_id $class_id" if $class_id;
    134     $command .= " -dbname $dbname" if defined $dbname;
    135     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    136         run(command => $command, verbose => $verbose);
    137     unless ($success) {
    138         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    139         &my_die("Unable to perform magictool -diffskyfile $skycell_args: $error_code", $magic_id, $component, $error_code);
    140     }
    141 
    142     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    143         &my_die("Unable to parse metadata config doc", $magic_id, $component, $PS_EXIT_PROG_ERROR);
    144 
    145     my $input_list = parse_md_list($metadata) or
    146         &my_die("Unable to parse metadata list", $magic_id, $component, $PS_EXIT_PROG_ERROR);
     146        &my_die("Unable to parse metadata config doc", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
     147
     148    my $skycells = parse_md_list($metadata) or
     149        &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
    147150    ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);
    148151
    149     foreach my $skycell (@$input_list) {
     152    foreach my $skycell (@$skycells) {
    150153        print $sfh "$skycell->{uri}\n"
    151154    }
     
    153156}
    154157
    155 my $image = $input->{uri};
    156 my $path_base = $input->{path_base};
     158my $image = $uri;
    157159
    158160my ($mask, $weight, $astrom);
    159161
    160162if ($stage eq "raw") {
    161     $astrom = $ipprc->filename("PSASTRO.OUTPUT", $input->{cam_path_base});
     163    $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
    162164} elsif ($stage eq "chip") {
    163165    $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id);
    164166    $weight = $ipprc->filename("PPIMAGE.CHIP.WEIGHT", $path_base, $class_id);
    165     $astrom = $ipprc->filename("PSASTRO.OUTPUT", $input->{cam_path_base});
     167    $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
    166168} elsif ($stage eq "warp") {
    167169    $mask   = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base);
     
    173175
    174176{
    175     my $command = "$streaksremove -stage $stage -tmproot $tmpdir -streaks $streaks -image $image";
     177    my $command = "$streaksremove -stage $stage -tmproot $outroot -streaks $streaks -image $image";
    176178    $command .= " -class_id $class_id" if defined $class_id;
    177     $command .= " -recovery $recoverydir" if defined $recoverydir;
     179    $command .= " -recovery $recoveryroot" if defined $recoveryroot;
    178180    $command .= " -astrom $astrom" if defined $astrom;
    179     # skip mask and weight images for now
     181    # XXX: remove this   skip mask and weight images for now
    180182#    $command .= " -mask $mask" if defined $mask;
    181183#    $command .= " -weight $weight" if defined $weight;
     
    187189        unless ($success) {
    188190            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    189             &my_die("Unable to perform streaksremove: $error_code", $magic_id, $component, $error_code);
     191            &my_die("Unable to perform streaksremove: $error_code", $magic_ds_id, $component, $error_code);
    190192        }
    191193    } else {
     
    194196}
    195197
    196 ### Input result into database
    197 #{
    198 #    my $command = "$magictool -addresult";
    199 #    $command   .= " -magic_id $magic_id";
    200 #    $command   .= " -node $node";
    201 #    $command   .= " -uri $outroot";
    202 #    $command   .= " -dbname $dbname" if defined $dbname;
    203 #
    204 #    # Add the processed file to the database
    205 #    unless ($no_update) {
    206 #        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    207 #            run(command => $command, verbose => $verbose);
    208 #        unless ($success) {
    209 #            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    210 #            &my_die("Unable to perform magictool -addresult: $error_code", $magic_id, $node, $error_code);
    211 #        }
    212 #    } else {
    213 #        print "Skipping command: $command\n";
    214 #    }
    215 #}
     198# if recovery and/or backup files were expected make sure they exist
     199
     200# Input result into database
     201{
     202    my $command = "$magicdstool -adddestreakedfile";
     203    $command   .= " -magic_ds_id $magic_ds_id";
     204    $command   .= " -component $component";
     205    $command   .= " -backup_uri $backup_uri" if $backup_uri;
     206    $command   .= " -recovery_uri $recovery_uri" if $recovery_uri;
     207    $command   .= " -dbname $dbname" if defined $dbname;
     208
     209    # Add the processed file to the database
     210    unless ($no_update) {
     211        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     212            run(command => $command, verbose => $verbose);
     213        unless ($success) {
     214            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     215            &my_die("Unable to perform magicdstool -addresult: $error_code", $magic_ds_id, $component,
     216                $error_code);
     217        }
     218    } else {
     219        print "Skipping command: $command\n";
     220    }
     221}
    216222
    217223
     
    223229{
    224230    my $file = shift;           # Name of file
    225     &my_die("Unable to find output file: $file", $magic_id, $component, $PS_EXIT_SYS_ERROR) unless
     231    &my_die("Unable to find output file: $file", $magic_ds_id, $component, $PS_EXIT_SYS_ERROR) unless
    226232        $ipprc->file_exists($file);
    227233}
     
    230236{
    231237    my $msg = shift;            # Warning message on die
    232     my $magic_id = shift;       # Magic identifier
     238    my $magic_ds_id = shift;    # Magic DS identifier
    233239    my $component = shift;      # class_id or skycell_id
    234240    my $exit_code = shift;      # Exit code to add
    235241
     242    my $command = "$magicdstool -adddestreakedfile";
     243    $command   .= " -magic_ds_id $magic_ds_id";
     244    $command   .= " -component $component";
     245    $command   .= " -code $exit_code";
     246    $command   .= " -dbname $dbname" if defined $dbname;
     247
     248    # Add the processed file to the database
     249    unless ($no_update) {
     250        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     251            run(command => $command, verbose => $verbose);
     252        unless ($success) {
     253            carp("failed to update database for $magic_ds_id $component");
     254        }
     255    } else {
     256        print "Skipping command: $command\n";
     257    }
     258
    236259    carp($msg);
    237260    exit $exit_code;
Note: See TracChangeset for help on using the changeset viewer.