IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:45:22 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simmosaic_branches
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/simmosaic_branches

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

    r24726 r27839  
    88use Sys::Hostname;
    99my $host = hostname();
     10my $date = `date`;
    1011print "\n\n";
    11 print "Starting script $0 on $host\n\n";
     12print "Starting script $0 on $host at $date\n\n";
    1213
    1314use vars qw( $VERSION );
     
    1516
    1617use IPC::Cmd 0.36 qw( can_run run );
    17 use File::Temp qw( tempfile );
     18use File::Temp qw( tempfile tempdir );
    1819use File::Basename qw( basename dirname );
    1920use PS::IPP::Metadata::Config;
     
    3233my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
    3334my $censorObjects = can_run('censorObjects') or (warn "Can't find censorObjects" and $missing_tools = 1);
     35my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
     36my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
    3437if ($missing_tools) {
    3538    warn("Can't find required tools.");
     
    3841
    3942# Parse the command-line arguments
    40 my ($magic_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base, $inverse, $cam_path_base);
     43my ($magic_ds_id, $camera, $streaks, $inv_streaks, $exp_id, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base, $cam_reduction);
    4144my ($outroot, $recoveryroot);
    4245my ($replace, $release);
     
    4750           'camera=s'       => \$camera,     # camera for evaluating file rules
    4851           'streaks=s'      => \$streaks,    # file containing the list of streaks
     52           'inv_streaks=s'  => \$inv_streaks,# file containing the list of streaks from the inverse diff
     53           'exp_id=s'       => \$exp_id,     # exp_id, chip_id, warp_id, or diff_id
    4954           'stage=s'        => \$stage,      # raw, chip, warp, or diff
    5055           'stage_id=s'     => \$stage_id,   # exp_id, chip_id, warp_id, or diff_id
     
    5257           'uri=s'          => \$uri,        # uri of the input image
    5358           'path_base=s'    => \$path_base,  # path_base of the input
    54            'inverse'        => \$inverse,    # Inverse subtraction?
    5559           'cam_path_base=s'=> \$cam_path_base,  # path_base from camera stage (for chip and raw)
     60           'cam_reduction=s'=> \$cam_reduction,  # reduction class from camera stage (for chip and raw)
    5661           'outroot=s'      => \$outroot,     # "directory" for temporary images (may be nebulous)
    5762           'recoveryroot=s' => \$recoveryroot,# "directory" for saving the images of excised pixels
     
    7479    defined $stage and
    7580    defined $stage_id and
     81    defined $exp_id and
    7682    defined $component and
    7783    defined $uri and
     
    8187my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
    8288$ipprc->redirect_output($logfile) or my_die( "Unable to redirect output", $magic_ds_id, $component, $PS_EXIT_SYS_ERROR ) if $logfile;
     89
     90$cam_reduction = 'DEFAULT' if !$cam_reduction or ($cam_reduction eq 'NULL');
     91
     92my $recipe_psastro = $ipprc->reduction($cam_reduction, 'PSASTRO'); # Recipe to use
     93&my_die("Unrecognised PSASTRO recipe", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_psastro;
    8394
    8495my ($skycell_args, $class_id, $skycell_id);
     
    95106    &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
    96107}
     108$inv_streaks = undef if defined($inv_streaks) and ($inv_streaks eq "NULL");
    97109
    98110my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     
    110122    if ($stage eq 'camera') {
    111123        my $nebulousServer = metadataLookupStr( $ipprc->{_siteConfig}, 'NEB_SERVER' );
    112         &my_die("cannot find NEB_SERVER in site configuration", 
    113                                         $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) 
     124        &my_die("cannot find NEB_SERVER in site configuration",
     125                                        $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR)
    114126            if !$ nebulousServer;
    115127
     
    144156    }
    145157}
     158
     159my $statsFile = "$outroot/$exp_id.mds.$magic_ds_id.$stage_id.$component.stats";
    146160
    147161my $backup_path_base;
     
    165179        if (! -e $recoveryroot ) {
    166180            my $code = system "mkdir -p $recoveryroot";
    167             &my_die("cannot create output directory $outroot", $magic_ds_id, $component,
     181            &my_die("cannot create output directory $recoveryroot", $magic_ds_id, $component,
    168182                    $code >> 8) if $code;
    169183        }
     
    172186    $recovery_path_base = "$recoveryroot/$basename/";
    173187}
     188
     189my $temp_dir;
    174190
    175191if ($stage ne "camera") {
     
    194210                    &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
    195211
     212            $temp_dir = tempdir( CLEANUP => !$save_temps);
    196213            ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps);
    197214
    198215            foreach my $skycell (@$skycells) {
    199                 my $skycell_uri = $ipprc->filename("PPSUB.OUTPUT", $skycell->{path_base});
     216                my $skycell_uri;
     217                if ($skycell->{data_state} eq "full") {
     218                    $skycell_uri = $ipprc->filename("PPSUB.OUTPUT", $skycell->{path_base});
     219                } else {
     220                    # diff run must have been cleaned up, need to create this skycell file on the fly
     221                    my $skycell_id = $skycell->{skycell_id};
     222                    $skycell_uri = "$temp_dir/$skycell_id";
     223                    $ipprc->skycell_file($skycell->{tess_id}, $skycell_id, $skycell_uri, $verbose) or
     224                        &my_die("failed to create skycell file for $skycell_id", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
     225                }
    200226                print $sfh "$skycell_uri\n";
    201227            }
    202             close $sfh;
     228            close $sfh
    203229        }
    204230    }
    205231
    206232    my ($image, $mask, $ch_mask, $weight, $astrom, $sources);
     233
     234    # if we're destreaking a bothways diff need to combine the
     235    # two streaks files
     236    my ($allstreaks_fh, $allstreaks_name);
    207237
    208238    if ($stage eq "raw") {
     
    211241        $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id) if $release ;
    212242    } elsif ($stage eq "chip") {
     243
     244        # Check to see if we're using dynamic masks
     245        my $dynamicMasks;               # Use dynamic masks?
     246        {
     247            # Get the PSASTRO recipe
     248            my $command = "$ppConfigDump -camera $camera -recipe PSASTRO $recipe_psastro -dump-recipe PSASTRO -";
     249            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     250                run(command => $command, verbose => $verbose);
     251            unless ($success) {
     252                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     253                &my_die("Unable to perform ppConfigDump: $error_code", $magic_ds_id, $component,
     254                        $PS_EXIT_CONFIG_ERROR);
     255            }
     256            my $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
     257                &my_die("Unable to parse metadata config doc", $magic_ds_id, $component,
     258                        $PS_EXIT_CONFIG_ERROR);
     259
     260            $dynamicMasks = metadataLookupBool($recipeData, 'REFSTAR_MASK');
     261        }
     262
    213263        # we use the mask output from the camera stage for input and replace
    214264        # the output of the chip stage with that mask as well.
    215265        $image  = $ipprc->filename("PPIMAGE.CHIP", $path_base, $class_id);
    216         $mask   = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);
    217         $ch_mask= $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
    218266        $weight = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $path_base, $class_id);
    219267        $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base);
    220         if (!$ipprc->file_exists($mask)) {
    221             carp("camera mask file $mask for $component not found. Continuing using mask from chip stage.");
    222             $mask = $ch_mask;
    223             $ch_mask = undef;
    224         }
     268
     269        if ($dynamicMasks) {
     270            $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $cam_path_base, $class_id);
     271            $ch_mask = $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
     272        } else {
     273            $mask = $ipprc->filename("PPIMAGE.CHIP.MASK", $path_base, $class_id);
     274        }
     275
    225276        # XXX: should we censor the sources as well? For now we're leaving them out of the distribution bundles
    226277        # because they confuse people
     
    234285        $sources    = $ipprc->filename("PSWARP.OUTPUT.SOURCES", $path_base);
    235286    } elsif ($stage eq "diff") {
    236         my $name = $inverse ? "PPSUB.INVERSE" : "PPSUB.OUTPUT"; # Base name for images
    237         $image  = $ipprc->filename($name, $path_base);
    238         $mask   = $ipprc->filename("$name.MASK", $path_base);
    239         $weight = $ipprc->filename("$name.VARIANCE", $path_base);
     287        $image  = $ipprc->filename("PPSUB.OUTPUT", $path_base);
     288        $mask   = $ipprc->filename("PPSUB.OUTPUT.MASK", $path_base);
     289        $weight = $ipprc->filename("PPSUB.OUTPUT.VARIANCE", $path_base);
    240290        $sources    = $ipprc->filename("PPSUB.OUTPUT.SOURCES", $path_base);
     291
     292        if ($inv_streaks) {
     293            # create a temporary file containing the contents of the
     294            # two streaks files
     295            ($allstreaks_fh, $allstreaks_name) = tempfile ("/tmp/all.streaks.XXXX",
     296                    UNLINK => !$save_temps);
     297
     298            combine_streaks($allstreaks_fh, $streaks, $inv_streaks);
     299
     300            # apply the combined streaks to both the forward and inverse diffs
     301            $streaks = $allstreaks_name;
     302        }
    241303    }
    242304
     
    244306        my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $streaks -image $image";
    245307
     308        $command .= " -stats $statsFile";
    246309        $command .= " -class_id $class_id" if defined $class_id;
    247310        $command .= " -recovery $recoveryroot" if defined $recoveryroot;
     
    266329        }
    267330    }
     331    if (($stage eq "diff") and $inv_streaks) {
     332        $image   = $ipprc->filename("PPSUB.INVERSE", $path_base);
     333        $mask    = $ipprc->filename("PPSUB.INVERSE.MASK", $path_base);
     334        $weight  = $ipprc->filename("PPSUB.INVERSE.VARIANCE", $path_base);
     335        $sources = $ipprc->filename("PPSUB.INVERSE.SOURCES", $path_base);
     336
     337        # Note: we create a stats file for the inverse procesing but we don't look at the results
     338        my $invStatsFile = "$outroot/$exp_id.mds.$magic_ds_id.$stage_id.$component.inv.stats";
     339
     340        my $command = "$streaksremove -stage $stage -tmproot $tmproot -streaks $streaks -image $image";
     341        $command .= " -stats $invStatsFile";
     342
     343        $command .= " -recovery $recoveryroot" if defined $recoveryroot;
     344        $command .= " -mask $mask" if defined $mask;
     345        $command .= " -weight $weight" if defined $weight;
     346        $command .= " -sources $sources" if defined $sources;
     347        $command .= " -replace" if $replace;
     348        $command .= " -release" if $release;
     349        $command .= " -dbname $dbname" if defined $dbname;
     350        unless (defined $no_op) {
     351            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     352                run(command => $command, verbose => $verbose);
     353            unless ($success) {
     354                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     355                &my_die("Unable to perform streaksremove: $error_code", $magic_ds_id, $component, $error_code);
     356            }
     357        } else {
     358            print "skipping command $command\n";
     359        }
     360    }
    268361} else {
    269362    # camera stage. The only work to do is to censor the detections file
     
    293386        my $quality = $file->{quality};
    294387        if (!$quality) {
    295             my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id); 
     388            my $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id);
    296389            if (! $ipprc->file_exists($mask)) {
    297390                # camera mask doesn't exist for this chip. Fall back to the chip mask
     
    315408                &my_die("Unable to perform censorObjects: $error_code", $magic_ds_id, $component, $error_code);
    316409            }
    317             my $output = $ipprc->filename("CENSOR.OUTPUT", $backup_path_base); 
     410            my $output = $ipprc->filename("CENSOR.OUTPUT", $backup_path_base);
    318411            &my_die("expected output file $output not found ", $magic_ds_id, $component, $PS_EXIT_DATA_ERROR)
    319412                unless $ipprc->file_exists($output);
     
    329422}
    330423
    331 # if recovery and/or backup files were expected make sure they exist
     424my $statsFlags;
     425if ($stage ne "camera") {
     426    file_check($statsFile);
     427    {
     428        my $resolvedStatsFile = $ipprc->file_resolve($statsFile);
     429        my $command = "$ppStatsFromMetadata $resolvedStatsFile - STREAKSREMOVE";
     430
     431        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     432            run(command => $command, verbose => $verbose);
     433        unless ($success) {
     434            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     435            &my_die("Unable to perform ppStatsFromMetadata: $error_code", $magic_ds_id, $component, $error_code);
     436        }
     437        foreach my $line (@$stdout_buf) {
     438            $statsFlags .= " $line";
     439        }
     440        chomp $statsFlags;
     441    }
     442}
     443
     444# XXX: if recovery and/or backup files were expected make sure they exist
    332445
    333446# Input result into database
     
    339452    $command   .= " -backup_path_base $backup_path_base" if $backup_path_base;
    340453    $command   .= " -recovery_path_base $recovery_path_base" if $recovery_path_base;
     454    $command   .= " $statsFlags" if $statsFlags;
    341455    $command   .= " -dbname $dbname" if defined $dbname;
    342456
     
    376490}
    377491
     492sub combine_streaks
     493{
     494    my $fout = shift;
     495    my $fn1 = shift;
     496    my $fn2 = shift;
     497
     498    my ($n1, @streaks1) = read_streaks_file($fn1);
     499    my_die("failed to read streaks from $fn1", $magic_ds_id, $component,
     500        $PS_EXIT_UNKNOWN_ERROR) if $n1 < 0;
     501
     502    my ($n2, @streaks2) = read_streaks_file($fn2);
     503    my_die("failed to read streaks from $fn2", $magic_ds_id, $component,
     504        $PS_EXIT_UNKNOWN_ERROR) if $n2 < 0;
     505
     506    print $fout $n1 + $n2 . "\n";
     507
     508    foreach my $line (@streaks1, @streaks2) {
     509        print $fout $line;
     510    }
     511
     512    close $fout
     513        or my_die("failed to close combined streaks file", $magic_ds_id,
     514                     $component, $PS_EXIT_UNKNOWN_ERROR);
     515}
     516
     517sub read_streaks_file
     518{
     519    my $filename = shift;
     520    my $fh;
     521    open $fh, "<$filename" or my_die("failed to open $filename",
     522                    $magic_ds_id, $component, $PS_EXIT_UNKNOWN_ERROR);
     523
     524    # first line is the number of streaks
     525    my $line = <$fh>;
     526    chomp $line;
     527    my $nstreaks = $line;
     528
     529    my @streaks;
     530
     531    foreach $line (<$fh>) {
     532        push @streaks, $line;
     533    }
     534
     535    close $fh;
     536
     537    return ($nstreaks, @streaks);
     538}
     539
    378540
    379541sub my_die
Note: See TracChangeset for help on using the changeset viewer.