IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 21, 2014, 5:42:34 AM (12 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ps2-tc3-20130727
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ps2-tc3-20130727

  • branches/eam_branches/ps2-tc3-20130727/pstamp/scripts

    • Property svn:mergeinfo deleted
  • branches/eam_branches/ps2-tc3-20130727/pstamp/scripts/pstamp_job_run.pl

    r35609 r36680  
    5757my_die("output_base is required", $job_id, $PS_EXIT_PROG_ERROR) if !$outputBase;
    5858
    59 $options = 1 if !$options;
    60 
    61 # We don't need to muggle anymore. Ignore those options
    62 $options &=  ~($PSTAMP_REQUEST_UNCENSORED | $PSTAMP_REQUIRE_UNCENSORED);
     59# ppstamp requires an input file
     60$options = $PSTAMP_SELECT_IMAGE if !$options;
    6361
    6462my $ipprc = PS::IPP::Config->new(); # IPP Configuration
     
    7876my $ppstamp    = can_run('ppstamp') or (warn "Can't find ppstamp" and $missing_tools = 1);
    7977my $pstamp_get_image_job    = can_run('pstamp_get_image_job.pl') or (warn "Can't find pstamp_get_image_job.pl" and $missing_tools = 1);
     78my $psgetcalibinfo    = can_run('psgetcalibinfo') or (warn "Can't find psgetcalibinfo" and $missing_tools = 1);
    8079my $dquery_job_run = can_run('dquery_job_run.pl') or (warn "Can't find dquery_job_run.pl" and $missing_tools = 1);
    81 my $streaksreplace = can_run('streaksreplace')  or (warn "Can't find streaksreplace"  and $missing_tools = 1);
    82 my $magicdstool = can_run('magicdstool')  or (warn "Can't find magicdstool"  and $missing_tools = 1);
    8380my $whichnode = can_run('whichnode') or (warn "can't find whichnode" and $missing_tools = 1);
    8481
     
    9693    my $argString;
    9794    $argString = $params->{job_args};
     95   
     96    my_die("argument list is empty", $job_id, $PS_EXIT_DATA_ERROR) if !$argString;
     97
    9898    my $stage = $params->{stage};
    99 
    100     # XXX: should we do any other sanity checking?
    101     my_die("argument list is empty", $job_id, $PS_EXIT_DATA_ERROR) if !$argString;
    102 
    103     my $nan_masked = 1;
    104     my $muggle = 0;
    105     if (!$params->{magicked}) {
    106         $nan_masked = 0;
    107     } elsif ($params->{magicked} and ($options & ($PSTAMP_REQUEST_UNCENSORED | $PSTAMP_REQUIRE_UNCENSORED))) {
    108         # Attempt to find or create a muggle image
    109         $nan_masked = 0;
    110         $muggle = 1;
    111     }
     99    my_die("stage is not defined", $job_id, $PS_EXIT_DATA_ERROR) if !$stage;
     100
     101    if ($stage eq 'stack_summary') {
     102
     103        # remove options not supported by stack summary
     104        $options &= ~($PSTAMP_SELECT_SOURCES | $PSTAMP_SELECT_BACKMDL | $PSTAMP_SELECT_INVERSE
     105            | $PSTAMP_RESTORE_BACKGROUND);
     106
     107        # stackSummary outputs do not follow the usual IPP conventions for file names.
     108        # The parser (actually Job.pm) has deferred handling this until here
     109        update_stack_summary_filenames($params);
     110
     111    } elsif ($stage ne 'stack') {
     112        # ignore options only supported by stack and stack_summary
     113        $options &= ~($PSTAMP_SELECT_EXP | $PSTAMP_SELECT_NUM);
     114    }
     115   
    112116
    113117    if ($stage eq "raw") {
     
    122126    my @file_list = ($params->{image});
    123127   
    124     if ($nan_masked or ($options & $PSTAMP_SELECT_MASK)) {
     128    if ($options & $PSTAMP_SELECT_MASK) {
    125129        $mask = $params->{mask};
    126130        $fileArgs .= " -mask $mask";
     
    162166    # find our output directory
    163167    my $outdir = dirname($outputBase);
    164     my ($tmpImage, $tmpMask, $tmpVariance, $tmproot);
    165    
    166     if ($muggle) {
    167         # first see if the original uncensored images are around
    168         if (check_for_backups($params, \$fileArgs)) {
    169             # We're good to go. fileArgs has been edited to contain the paths for the original uncensored images
    170             print "Making stamps from backup images\n";
    171         } elsif (($options & $PSTAMP_REQUIRE_UNCENSORED) and ($stage ne 'chip')) {
    172             # user required uncensored but since stage isn't chip we can't rebuild them
    173             my_die("uncensored inputs not available for job $job_id", $job_id, $PSTAMP_NOT_AVAILABLE, 'stop');
    174         } elsif (($options & $PSTAMP_REQUEST_UNCENSORED) and ($params->{state} ne 'full') and ($stage ne 'chip')) {
    175             # we can only restore pixels for chip stage images if the data has been updated.
    176             # the data will have been updated if the params->{state} the state when the job was queued is not 'full' (
    177             # XXX: we should probably be looking explicitly at the job and checking for a dep_id
    178             print "Run state was $params->{state}: will make stamps from destreaked $stage images.\n";
    179             # make stamps from uncensored images
    180             $muggle = 0;
     168
     169    my ($calib_fd, $calibfile);
     170    if ($stage eq 'chip' or $stage eq 'warp') {
     171        my $cam_id = $params->{cam_id};
     172        if (!$cam_id) {
     173            carp "no cam_id found in job params\n";
     174            exit $PS_EXIT_PROG_ERROR;
     175        }
     176        ($calib_fd, $calibfile) = tempfile ("$outdir/calib.XXXX", UNLINK => !$save_temps);
     177        close $calib_fd;
     178
     179        my $command = "$psgetcalibinfo --cam_id $cam_id --output $calibfile --dbname $params->{imagedb}";
     180        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     181            run(command => $command, verbose => $verbose);
     182
     183        my $exitStatus;
     184        if (WIFEXITED($error_code)) {
     185            $exitStatus = WEXITSTATUS($error_code);
    181186        } else {
    182             # Try and replace the streaks from the recovery images
    183 
    184             my $temp_dir = metadataLookupStr($ipprc->{_siteConfig}, "TEMP.DIR") or &my_die("Unable to find temporary directory in site configuration", $job_id, $PS_EXIT_CONFIG_ERROR);
    185             $tmproot = tempdir("$temp_dir/psjob.$job_id.XXXX", CLEANUP => !$save_temps);
    186             my $muggle_command = "$streaksreplace -stage $stage -tmproot $tmproot";
    187             # find the "directory" of the input path_base
    188             my $inputdir = dirname($image);
    189             my $base = basename($image);
    190             $tmpImage = "$tmproot/$base";
    191 
    192             @file_list = ();
    193 
    194             # XXX: We should get the recovery_path_base from the magicDSFile but that requires a bunch of file rule and
    195             # stage work. Import the rule here.
    196             my $recImage = "$inputdir/REC_$base";
    197             my $newFileArgs = " -file $tmpImage";
    198             $muggle_command .= " -image $image -recimage $recImage";
    199             push @file_list, $recImage;
    200 
    201             if ($mask) {
    202                 $base = basename($mask);
    203                 $tmpMask = "$tmproot/$base";
    204                 $newFileArgs .= " -mask $tmpMask";
    205                 my $recMask = "$inputdir/REC_$base";
    206                 $muggle_command .= " -mask $mask -recmask $recMask";
    207                 push @file_list, $recMask;
    208             }
    209 
    210             if ($variance) {
    211                 $base = basename($variance);
    212                 $tmpVariance = "$tmproot/$base";
    213                 $newFileArgs .= " -variance $tmpVariance";
    214                 my $recVariance = "$inputdir/REC_$base";
    215                 $muggle_command .= " -weight $variance -recweight $recVariance";
    216                 push @file_list, $recVariance;
    217             }
    218             if (check_files(0, @file_list)) {
    219                 # recovery files exist and are accessible restore the excised pixels
    220 
    221                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    222                     run(command => $muggle_command, verbose => $verbose);
    223                 unless ($success) {
    224                     my $exitStatus = WEXITSTATUS($error_code);
    225                     my_die( "streaksreplace failed with error code: $exitStatus", $job_id, $exitStatus);
    226                 }
    227 
    228                 # set the ppstamp file arguments
    229                 $fileArgs = $newFileArgs;
    230             } else {
    231                 if ($options & $PSTAMP_REQUIRE_UNCENSORED) {
    232                     my_die( "unable to restore uncensored images", $job_id, $PSTAMP_NOT_AVAILABLE);
    233                 }
    234                 # just make stamps from the censored images
    235                 print "Unable to restore uncensored images, will extract stamps from censored images\n";
    236                 # these files won't be used so zap them
    237                 ($tmpImage, $tmpMask, $tmpVariance, $tmproot) = (undef, undef, undef, undef);
    238             }
    239         }
    240     }
    241 
    242     # MAGIC IS DEAD
    243     $nan_masked = 0;
    244 
    245     my $command = "$ppstamp $outputBase $argString $fileArgs";
    246     $command .= " -write_jpeg" if ($options & $PSTAMP_SELECT_JPEG);
    247     $command .= " -nocompress" if ($options & $PSTAMP_SELECT_UNCOMPRESSED);
    248     $command .= " -stage $stage";
    249     $command .= " -censor_masked" if $nan_masked;
    250     $command .= " -dbname $dbname" if $dbname;
    251     $command .= " -dbserver $dbserver" if $dbserver;
    252     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    253         run(command => $command, verbose => $verbose);
     187            print STDERR "psgetcalibinfo failed error_code: $error_code\n";
     188            $exitStatus = $PS_EXIT_SYS_ERROR;
     189        }
     190        exit $exitStatus if $exitStatus;
     191
     192        if (-s $calibfile == 0) {
     193            print "no calibration information found for $cam_id\n";
     194            $calibfile = undef;
     195        }
     196    }
     197
     198    # unless the stage is stack_summary we use ppstamp to make postage stamps (including -wholefile)
     199    # otherwise we make copies of the input files to the outputs
     200    my $use_ppstamp = ($stage ne 'stack_summary');
    254201
    255202    my $exitStatus;
    256     if (WIFEXITED($error_code)) {
    257         $exitStatus = WEXITSTATUS($error_code);
    258     } else {
    259         print STDERR "ppstamp failed error_code: $error_code\n";
    260         $exitStatus = $PS_EXIT_SYS_ERROR;
     203    if ($use_ppstamp) {
     204        my $command = "$ppstamp $outputBase $argString $fileArgs";
     205        $command .= " -write_jpeg" if ($options & $PSTAMP_SELECT_JPEG);
     206        $command .= " -nocompress" if ($options & $PSTAMP_SELECT_UNCOMPRESSED);
     207        $command .= " -stage $stage";
     208        $command .= " -forheader $calibfile" if $calibfile;
     209        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     210            run(command => $command, verbose => $verbose);
     211
     212        if (WIFEXITED($error_code)) {
     213            $exitStatus = WEXITSTATUS($error_code);
     214        } else {
     215            print STDERR "ppstamp failed error_code: $error_code\n";
     216            $exitStatus = $PS_EXIT_SYS_ERROR;
     217        }
     218        # XXX: if stage is stack deal with EXP and NUM images if selected
     219    } else {
     220        $exitStatus = justCopyFiles($outputBase, \$options, $params);
    261221    }
    262222
     
    270230
    271231        # Note: we are assuming the contents of the PSTAMP filerules here.
    272         my %extensions = ( $PSTAMP_SELECT_IMAGE    => "fits",
    273                            $PSTAMP_SELECT_MASK     => "mk.fits",
    274                            $PSTAMP_SELECT_VARIANCE => "wt.fits",
    275                            $PSTAMP_SELECT_SOURCES  => "cmf",
    276                            $PSTAMP_SELECT_JPEG     => "jpg");
    277 
    278         my $output_mask = $options & ($PSTAMP_SELECT_IMAGE | $PSTAMP_SELECT_MASK | $PSTAMP_SELECT_VARIANCE | $PSTAMP_SELECT_JPEG | $PSTAMP_SELECT_SOURCES);
     232        my %extensions = ( $PSTAMP_SELECT_IMAGE    => 'fits',
     233                           $PSTAMP_SELECT_MASK     => 'mk.fits',
     234                           $PSTAMP_SELECT_VARIANCE => 'wt.fits',
     235                           $PSTAMP_SELECT_SOURCES  => 'cmf',
     236                           $PSTAMP_SELECT_JPEG     => 'jpg',
     237                           $PSTAMP_SELECT_EXP      => 'exp.fits',
     238                           $PSTAMP_SELECT_NUM      => 'num.fits',
     239                           $PSTAMP_SELECT_EXPJPEG  => 'exp.jpg',
     240                           $PSTAMP_SELECT_NUMJPEG  => 'num.jpg');
     241
     242        my $output_mask = $options & ($PSTAMP_SELECT_IMAGE | $PSTAMP_SELECT_MASK | $PSTAMP_SELECT_VARIANCE
     243            | $PSTAMP_SELECT_JPEG | $PSTAMP_SELECT_SOURCES
     244            | $PSTAMP_SELECT_EXP | $PSTAMP_SELECT_NUM
     245            | $PSTAMP_SELECT_EXPJPEG | $PSTAMP_SELECT_NUMJPEG);
     246
    279247
    280248        foreach my $key (keys (%extensions)) {
     
    298266        close $F;
    299267        $jobStatus = $PS_EXIT_SUCCESS;
    300     } elsif ($exitStatus == $PSTAMP_NO_OVERLAP) {
    301         $jobStatus = $PSTAMP_NO_OVERLAP;
     268    } elsif ($exitStatus == $PSTAMP_NO_OVERLAP || $exitStatus == $PSTAMP_NO_VALID_PIXELS) {
     269        $jobStatus = $exitStatus;
    302270    } else {
    303271        my_die( "ppstamp failed with error code: $exitStatus", $job_id, $exitStatus);
     
    419387        my $backmdl_file = $params->{backmdl} if ($options & $PSTAMP_SELECT_BACKMDL);
    420388        my $pattern_file = $params->{pattern} if ($options & $PSTAMP_SELECT_BACKMDL);
     389        my $filter = $params->{filter};
     390        $filter = ' ' if !$filter;
     391        $filter = substr $filter, 0, 1;
    421392        my $cmf_file;
    422393#        if ($stage ne 'chip') {
     
    431402            if (!$therest or !$rownum or !$jobnum);
    432403
    433         my $prefix = "${rownum}_${jobnum}_";
     404        # XXX: Here we are assuming the form of the output file names
     405        # if we change this in pstampparse we'll need to remember ....
     406        # (the last time I forgot)
     407        my $prefix = "${rownum}_${jobnum}_${filter}_";
    434408
    435409        if ($cmf_file) {
     
    523497}
    524498
    525 sub check_for_backups {
    526     my $params = shift;
    527     my $r_fileArgs = shift;
    528 
    529     my $command = "$magicdstool -destreakedfile -stage $params->{stage} -stage_id $params->{stage_id} -component $params->{component} -dbname $params->{imagedb}";
    530     my $results = runToolAndParse($command, $verbose);
    531     my $dsComponent  = $results->[0];
    532 
    533     if ($dsComponent and ($dsComponent->{data_state} eq 'full')) {
    534 
    535         print "magicDSFile state is full. Backup images should exist\n";
    536 
    537         # replace the file names with the backup paths
    538         # fileArgs has the form:  -file imagename [-mask maskname] [-weight weightname]
    539         my @args = split " ", $$r_fileArgs;
    540 
    541         my $newFileArgs;
    542         while (@args) {
    543             my $a = shift @args;
    544             my $f = shift @args;
    545             my_die( "unexpected fileArg list $$r_fileArgs", $job_id, $PS_EXIT_PROG_ERROR, 'run') if !$a or !$f;
    546 
    547             my $backup_image_name = $ipprc->destreaked_filename($f);
    548             my_die( "failed to extract backup image name from $f", $job_id, $PS_EXIT_PROG_ERROR, 'run') if !$backup_image_name;
    549             $newFileArgs .= " $a $backup_image_name";
    550         }
    551         $$r_fileArgs = $newFileArgs;
    552 
    553         return 1;
    554     }
    555 
    556     return 0;
    557 }
    558 
    559499my $neb;
    560500sub storage_object_exists
     
    630570}
    631571
     572# stack_summary stage does not currently use proper file rules.
     573# The parser defers handlint this to us..
     574
     575sub update_stack_summary_filenames {
     576    my $params  = shift;
     577
     578    my $path_base = $params->{path_base};
     579
     580    $params->{image}  = $path_base . ".image.b1.fits";
     581    $params->{mask}   = $path_base . ".mask.b1.fits";
     582    $params->{weight} = $path_base . ".variance.b1.fits";
     583    $params->{jpeg}   = $path_base . ".image.0.b1.jpeg";
     584    $params->{exp}    = $path_base . ".exp.b1.fits";
     585    $params->{num}    = $path_base . ".num.b1.fits";
     586    $params->{expjpeg} = $path_base . ".exp.0.b1.jpeg";
     587    $params->{numjpeg} = $path_base . ".num.0.b1.jpeg";
     588}
     589
     590sub myCopy {
     591    my ($dest, $src, $type, $dieOnFail) = @_;
     592
     593    my $result;
     594    my $resolved = $ipprc->file_resolve($src);
     595    if ($resolved and $ipprc->file_exists($resolved)) {
     596        print "Copying $src to $dest\n" if $verbose;;
     597        $result = copy($resolved, $dest);
     598    } else {
     599        my $msg = "Specified source $type image $src not found.";
     600        if ($dieOnFail) {
     601            $msg .= "\n";
     602        } else {
     603            $msg .= " ignoring\n";
     604        }
     605        carp $msg;
     606        $result = 0;
     607    }
     608   
     609    if (!$result and $dieOnFail) {
     610        &my_die("Unable to copy $type image", $job_id, $PS_EXIT_SYS_ERROR, 'run');
     611    }
     612    return $result;
     613}
     614
     615sub justCopyFiles {
     616    my ($outputBase, $r_options, $params) = @_;
     617
     618    print "Just copying files for $job_id.\n";
     619
     620    my $options = $$r_options;
     621    if ($options & $PSTAMP_SELECT_IMAGE) {
     622        myCopy("$outputBase.fits", $params->{image}, 'image', 1);
     623    }
     624    if ($options & $PSTAMP_SELECT_MASK) {
     625        if (!myCopy("$outputBase.mk.fits", $params->{mask}, 'mask', 0)) {
     626            $options &= ~$PSTAMP_SELECT_MASK;
     627        }
     628    }
     629    if ($options & $PSTAMP_SELECT_VARIANCE) {
     630        if (!myCopy("$outputBase.wt.fits", $params->{weight}, 'variance', 0)) {
     631            $options =  ~$PSTAMP_SELECT_VARIANCE;
     632        }
     633    }
     634    if ($options & $PSTAMP_SELECT_JPEG) {
     635        if (!myCopy("$outputBase.jpg", $params->{jpeg}, 'jpeg', 0)) {
     636            $options &= ~$PSTAMP_SELECT_JPEG;
     637        }
     638    }
     639    if ($options & $PSTAMP_SELECT_EXP) {
     640        if (!myCopy("$outputBase.exp.fits", $params->{exp}, 'exp', 0)) {
     641            $options &= ~$PSTAMP_SELECT_EXP;
     642        }
     643    }
     644    if ($options & $PSTAMP_SELECT_NUM) {
     645        if (!myCopy("$outputBase.num.fits", $params->{num}, 'num', 0)) {
     646            $options &= ~$PSTAMP_SELECT_NUM;
     647        }
     648    }
     649    if ($options & $PSTAMP_SELECT_EXPJPEG) {
     650        if (!myCopy("$outputBase.exp.jpg", $params->{expjpeg}, 'exp', 0)) {
     651            $options &= ~$PSTAMP_SELECT_EXPJPEG;
     652        }
     653    }
     654    if ($options & $PSTAMP_SELECT_NUMJPEG) {
     655        if (!myCopy("$outputBase.num.jpg", $params->{numjpeg}, 'num', 0)) {
     656            $options &= ~$PSTAMP_SELECT_NUMJPEG;
     657        }
     658    }
     659
     660    $$r_options = $options;
     661
     662    print "Done with copy.\n";
     663
     664    return 0;
     665}
     666
    632667sub my_die
    633668{
Note: See TracChangeset for help on using the changeset viewer.