IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 7, 2009, 4:08:25 PM (17 years ago)
Author:
Paul Price
Message:

Merging trunk (r25026) to get up-to-date on old branch.

Location:
branches/pap
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/ippScripts/scripts/magic_destreak.pl

    r23909 r25027  
    1616use IPC::Cmd 0.36 qw( can_run run );
    1717use File::Temp qw( tempfile );
    18 use File::Basename qw( basename );
     18use File::Basename qw( basename dirname );
    1919use PS::IPP::Metadata::Config;
    2020use PS::IPP::Metadata::List qw( parse_md_list );
    2121
    2222use PS::IPP::Config 1.01 qw( :standard );
     23use Nebulous::Client;
    2324
    2425use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
     
    2930my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
    3031my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
     32my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
     33my $censorObjects = can_run('censorObjects') or (warn "Can't find censorObjects" and $missing_tools = 1);
    3134if ($missing_tools) {
    3235    warn("Can't find required tools.");
     
    3538
    3639# Parse the command-line arguments
    37 my ($magic_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base);
     40my ($magic_ds_id, $camera, $streaks, $inv_streaks, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base);
    3841my ($outroot, $recoveryroot);
    39 my ($replace, $remove, $release);
     42my ($replace, $release);
    4043my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);
    4144
     
    4447           'camera=s'       => \$camera,     # camera for evaluating file rules
    4548           'streaks=s'      => \$streaks,    # file containing the list of streaks
     49           'inv_streaks=s'  => \$inv_streaks,# file containing the list of streaks from the inverse diff
    4650           'stage=s'        => \$stage,      # raw, chip, warp, or diff
    4751           'stage_id=s'     => \$stage_id,   # exp_id, chip_id, warp_id, or diff_id
     
    4953           'uri=s'          => \$uri,        # uri of the input image
    5054           'path_base=s'    => \$path_base,  # path_base of the input
    51            'cam_path_base=s'=> \$cam_path_base,  # path_base of the associated camera run
     55           'cam_path_base=s'=> \$cam_path_base,  # path_base from camera stage (for chip and raw)
    5256           'outroot=s'      => \$outroot,     # "directory" for temporary images (may be nebulous)
    5357           'recoveryroot=s' => \$recoveryroot,# "directory" for saving the images of excised pixels
    5458           'replace=s'      => \$replace,    # replace the input images with the results.
    55            'remove=s'       => \$remove,     # remove the original images when done YIKES!
    5659           'release'        => \$release,    # NAN masked pixels for release
    5760           'save-temps'     => \$save_temps, # Save temporary files?
     
    8992} elsif ($stage eq "diff") {
    9093    $skycell_id = $component;
    91 } else {
     94} elsif ($stage ne "camera") {
    9295    &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
    93 }
     96}   
     97$inv_streaks = undef if defined($inv_streaks) and ($inv_streaks eq "NULL");
    9498
    9599my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    96100
     101my $dirname = dirname($path_base);
    97102my $basename = basename($path_base);
    98 
    99 #
    100 # parse the replace and remove arguments check for errors and set up
     103my $nebulousInput = inNebulous($dirname);
     104my $nebulousOutput = inNebulous($outroot);
     105
     106# parse replace arguments check for errors and set up
    101107# the appropriate paths
     108my $nebulous;
    102109if (defined($replace) and ($replace eq "T")) {
     110    # for camera stage we need a handle to the nebulous server
     111    if ($stage eq 'camera') {
     112        my $nebulousServer = metadataLookupStr( $ipprc->{_siteConfig}, 'NEB_SERVER' );
     113        &my_die("cannot find NEB_SERVER in site configuration",
     114                                        $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR)
     115            if !$ nebulousServer;
     116
     117        $nebulous = eval { Nebulous::Client->new( proxy => $nebulousServer ); };
     118        if ($@ or not defined $nebulous) {
     119            &my_die ("Unable to create a Nebulous::Client object with proxy $nebulousServer",
     120                                    $magic_ds_id, $component , $PS_EXIT_UNKNOWN_ERROR);
     121        }
     122    }
    103123    $replace = 1;
    104124} else {
     
    106126}
    107127
     128
    108129# default value is "NULL" do not use
    109 if (defined($recoveryroot) and ($recoveryroot = "NULL")) {
     130if (defined($recoveryroot) and ($recoveryroot eq "NULL")) {
    110131    $recoveryroot = undef;
    111132}
     
    116137}
    117138
    118 if (defined($remove) and ($remove eq "T")) {
    119     $remove = 1;
    120 } else {
    121     $remove = 0;
    122 }
    123 
    124 if ($remove and !$replace) {
    125     &my_die("cannot remove without replace", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
    126 }
    127 
    128139# create the output directories if it is not a nebulous path and it doesn't exist
    129 if (index($outroot, "neb://") != 0) {
     140if (! $nebulousOutput) {
    130141    if (! -e $outroot ) {
    131142        my $code = system "mkdir -p $outroot";
     
    136147
    137148my $backup_path_base;
    138 if (! $remove) {
    139     $backup_path_base = "$outroot/$basename";
     149my $tmproot;
     150if ($replace) {
     151    # in replace mode, we place the output files in the same "directory" as the inputs
     152    # Nebulous requires this for the two inputs to nebSwap which we use
     153    # We prepend the path with SR_ This causes the filenames for instances of the swapped files to
     154    # have SR in them.
     155    $tmproot = $dirname . "/SR_";
     156    $backup_path_base = $tmproot . "$basename";
     157} else {
     158    # note: trailing / is necessary
     159    $tmproot = "$outroot/";
     160    $backup_path_base = $tmproot . $basename;
    140161}
    141162
    142163my $recovery_path_base;
    143164if ($recoveryroot) {
    144     if (index($recoveryroot, "neb://") != 0) {
     165    if (!inNebulous($recoveryroot)) {
    145166        if (! -e $recoveryroot ) {
    146167            my $code = system "mkdir -p $recoveryroot";
     
    149170        }
    150171    }
    151     $recovery_path_base = "$recoveryroot/$basename";
    152 }
    153 
    154 # get skycell list if needed
    155 my ($sfh, $skycell_list);
    156 if ($skycell_args) {
    157     my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args";
    158     $command .= " -dbname $dbname" if defined $dbname;
    159     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    160         run(command => $command, verbose => $verbose);
    161     unless ($success) {
    162         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    163         &my_die("Unable to perform magicdstool -diffskyfile $skycell_args: $error_code", $magic_ds_id, $component, $error_code);
    164     }
    165 
    166     my $getskycells_output = join "", @$stdout_buf;
    167     if ($getskycells_output) {
    168         my $metadata = $mdcParser->parse($getskycells_output) or
    169             &my_die("Unable to parse metadata config doc", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
    170 
    171         my $skycells = parse_md_list($metadata) or
    172                 &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
    173 
    174         ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);
    175 
    176         foreach my $skycell (@$skycells) {
    177             print $sfh "$skycell->{uri}\n"
    178         }
    179         close $sfh;
    180     }
    181 }
    182 
    183 my $image = $uri;
    184 
    185 my ($mask, $ch_mask, $weight, $astrom);
    186 
    187 if ($stage eq "raw") {
    188     $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
    189     $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id) if $release ;
    190 } elsif ($stage eq "chip") {
    191     # we use the mask output from the camera stage for input and replace
    192     # the output of the chip stage with that mask as well.
    193     $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);
    194     $ch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
    195     $weight = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $path_base, $class_id);
    196     $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
    197 } elsif ($stage eq "warp") {
    198     $mask   = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base);
    199     $weight = $ipprc->filename("PSWARP.OUTPUT.VARIANCE", $path_base);
    200 } elsif ($stage eq "diff") {
    201     $mask   = $ipprc->filename("PPSUB.OUTPUT.MASK", $path_base);
    202     $weight = $ipprc->filename("PPSUB.OUTPUT.VARIANCE", $path_base);
    203 }
    204 
    205 {
    206     my $command = "$streaksremove -stage $stage -tmproot $outroot -streaks $streaks -image $image";
    207 
    208     $command .= " -class_id $class_id" if defined $class_id;
    209     $command .= " -recovery $recoveryroot" if defined $recoveryroot;
    210     $command .= " -astrom $astrom" if defined $astrom;
    211     $command .= " -mask $mask" if defined $mask;
    212     $command .= " -chip_mask $ch_mask" if defined $ch_mask;
    213     $command .= " -weight $weight" if defined $weight;
    214     $command .= " -skycelllist $skycell_list" if defined $skycell_list;
    215     $command .= " -replace" if $replace;
    216     $command .= " -remove" if $remove;
    217     $command .= " -release" if $release;
    218     $command .= " -dbname $dbname" if defined $dbname;
    219     unless (defined $no_op) {
     172    # note: trailing / is necessary
     173    $recovery_path_base = "$recoveryroot/$basename/";
     174}
     175
     176if ($stage ne "camera") {
     177    # get skycell list if needed
     178    my ($sfh, $skycell_list);
     179    if ($skycell_args) {
     180        my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args";
     181        $command .= " -dbname $dbname" if defined $dbname;
    220182        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    221183            run(command => $command, verbose => $verbose);
    222184        unless ($success) {
    223185            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    224             &my_die("Unable to perform streaksremove: $error_code", $magic_ds_id, $component, $error_code);
    225         }
    226     } else {
    227         print "skipping command $command\n";
     186            &my_die("Unable to perform magicdstool -diffskyfile $skycell_args: $error_code", $magic_ds_id, $component, $error_code);
     187        }
     188
     189        my $getskycells_output = join "", @$stdout_buf;
     190        if ($getskycells_output) {
     191            my $metadata = $mdcParser->parse($getskycells_output) or
     192                &my_die("Unable to parse metadata config doc", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
     193
     194            my $skycells = parse_md_list($metadata) or
     195                    &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
     196
     197            ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);
     198
     199            foreach my $skycell (@$skycells) {
     200                my $skycell_uri = $ipprc->filename("PPSUB.OUTPUT", $skycell->{path_base});
     201                print $sfh "$skycell_uri\n";
     202            }
     203            close $sfh;
     204        }
     205    }
     206
     207    my ($image, $mask, $ch_mask, $weight, $astrom, $sources);
     208
     209    if ($stage eq "raw") {
     210        $image = $uri;
     211        $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
     212        $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id) if $release ;
     213    } elsif ($stage eq "chip") {
     214        # we use the mask output from the camera stage for input and replace
     215        # the output of the chip stage with that mask as well.
     216        $image  = $ipprc->filename("PPIMAGE.CHIP", $path_base, $class_id);
     217        $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);
     218        $ch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
     219        $weight = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $path_base, $class_id);
     220        $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
     221        if (!$ipprc->file_exists($mask)) {
     222            carp("camera mask file $mask for $component not found. Continuing using mask from chip stage.");
     223            $mask = $ch_mask;
     224            $ch_mask = undef;
     225        }
     226        # XXX: should we censor the sources as well? For now we're leaving them out of the distribution bundles
     227        # because they confuse people
     228        # XXX: make sure that this is the right file rule
     229        # $sources = $ipprc->filename("PSPHOT.OUT.CMF.SPL",  $path_base);
     230
     231    } elsif ($stage eq "warp") {
     232        $image  = $ipprc->filename("PSWARP.OUTPUT", $path_base);
     233        $mask   = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base);
     234        $weight = $ipprc->filename("PSWARP.OUTPUT.VARIANCE", $path_base);
     235        $sources    = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $path_base);
     236    } elsif ($stage eq "diff") {
     237        $image  = $ipprc->filename("PPSUB.OUTPUT", $path_base);
     238        $mask   = $ipprc->filename("PPSUB.OUTPUT.MASK", $path_base);
     239        $weight = $ipprc->filename("PPSUB.OUTPUT.VARIANCE", $path_base);
     240        $sources    = $ipprc->filename("PPSUB.OUTPUT.SOURCES", $path_base);
     241    }
     242
     243    {
     244        my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $streaks -image $image";
     245
     246        $command .= " -class_id $class_id" if defined $class_id;
     247        $command .= " -recovery $recoveryroot" if defined $recoveryroot;
     248        $command .= " -astrom $astrom" if defined $astrom;
     249        $command .= " -mask $mask" if defined $mask;
     250        $command .= " -chip_mask $ch_mask" if defined $ch_mask;
     251        $command .= " -weight $weight" if defined $weight;
     252        $command .= " -sources $sources" if defined $sources;
     253        $command .= " -skycelllist $skycell_list" if defined $skycell_list;
     254        $command .= " -replace" if $replace;
     255        $command .= " -release" if $release;
     256        $command .= " -dbname $dbname" if defined $dbname;
     257        unless (defined $no_op) {
     258            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     259                run(command => $command, verbose => $verbose);
     260            unless ($success) {
     261                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     262                &my_die("Unable to perform streaksremove: $error_code", $magic_ds_id, $component, $error_code);
     263            }
     264        } else {
     265            print "skipping command $command\n";
     266        }
     267    }
     268    if (($stage eq "diff") and $inv_streaks) {
     269        $image   = $ipprc->filename("PPSUB.INVERSE", $path_base);
     270        $mask    = $ipprc->filename("PPSUB.INVERSE.MASK", $path_base);
     271        $weight  = $ipprc->filename("PPSUB.INVERSE.VARIANCE", $path_base);
     272        $sources = $ipprc->filename("PPSUB.INVERSE.SOURCES", $path_base);
     273
     274        my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $inv_streaks -image $image";
     275
     276        $command .= " -recovery $recoveryroot" if defined $recoveryroot;
     277        $command .= " -mask $mask" if defined $mask;
     278        $command .= " -weight $weight" if defined $weight;
     279        $command .= " -sources $sources" if defined $sources;
     280        $command .= " -replace" if $replace;
     281        $command .= " -release" if $release;
     282        $command .= " -dbname $dbname" if defined $dbname;
     283        unless (defined $no_op) {
     284            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     285                run(command => $command, verbose => $verbose);
     286            unless ($success) {
     287                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     288                &my_die("Unable to perform streaksremove: $error_code", $magic_ds_id, $component, $error_code);
     289            }
     290        } else {
     291            print "skipping command $command\n";
     292        }
     293    }
     294} else {
     295    # camera stage. The only work to do is to censor the detections file
     296
     297    my $tempOutRoot = "/tmp/destreak";
     298    my ($maskListFile,   $maskListName)   = tempfile( "$tempOutRoot.mask.list.XXXX",   UNLINK => !$save_temps);
     299    my $files;
     300    {
     301        my $cam_id = $stage_id;
     302        my $command = "$camtool -pendingimfile -cam_id $cam_id"; # Command to run
     303        $command .= " -dbname $dbname" if defined $dbname;
     304        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     305            run(command => $command, verbose => $verbose);
     306        unless ($success) {
     307            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     308            &my_die("Unable to perform camtool: $error_code", $cam_id, $error_code);
     309        }
     310        my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     311            &my_die("Unable to parse metadata config doc", $cam_id, $PS_EXIT_PROG_ERROR);
     312
     313        # extract the metadata for the files into a hash list
     314        $files = parse_md_list($metadata) or
     315            &my_die("Unable to parse metadata list", $cam_id, $PS_EXIT_PROG_ERROR);
     316    }
     317    foreach my $file (@$files) {
     318        my $class_id = $file->{class_id};
     319        my $quality = $file->{quality};
     320        if (!$quality) {
     321            my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id);
     322            if (! $ipprc->file_exists($mask)) {
     323                # camera mask doesn't exist for this chip. Fall back to the chip mask
     324                $mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $file->{path_base}, $class_id);
     325            }
     326            print $maskListFile "$mask\n";
     327        }
     328    }
     329    close $maskListFile;
     330
     331    my $astrom = $ipprc->filename("PSASTRO.OUTPUT", $path_base);
     332    {
     333        my $command = "$censorObjects -file $astrom -masklist $maskListName $backup_path_base";
     334        # $command .= " -replace" if $replace;
     335        $command .= " -dbname $dbname" if defined $dbname;
     336        unless (defined $no_op) {
     337            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     338                run(command => $command, verbose => $verbose);
     339            unless ($success) {
     340                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     341                &my_die("Unable to perform censorObjects: $error_code", $magic_ds_id, $component, $error_code);
     342            }
     343            my $output = $ipprc->filename("CENSOR.OUTPUT", $backup_path_base);
     344            &my_die("expected output file $output not found ", $magic_ds_id, $component, $PS_EXIT_DATA_ERROR)
     345                unless $ipprc->file_exists($output);
     346
     347            if ($replace) {
     348                $nebulous->swap($astrom, $output) or
     349                    &my_die("nebulous swap failed $astrom $output", $magic_ds_id, $component, $PS_EXIT_UNKNOWN_ERROR);
     350            }
     351        } else {
     352            print "skipping command $command\n";
     353        }
    228354    }
    229355}
     
    267393}
    268394
     395sub inNebulous
     396{
     397    my $path = shift;
     398
     399    my $scheme = file_scheme($path);
     400
     401    return $scheme and ($scheme eq "neb");
     402}
     403
     404
    269405sub my_die
    270406{
Note: See TracChangeset for help on using the changeset viewer.