IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 16, 2009, 6:35:31 PM (17 years ago)
Author:
watersc1
Message:

changes to support new burntool_state

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/ipp_apply_burntool.pl

    r25412 r25425  
    1212use Carp;
    1313
    14 use IPC::Cmd 0.36 qw( can_run );
    15 use IPC::Run 0.36 qw( run );
     14use IPC::Cmd 0.36 qw( can_run run );
     15#use IPC::Run 0.36 qw( run );
    1616use PS::IPP::Metadata::List qw( parse_md_list );
    1717use PS::IPP::Config 1.01 qw( :standard );
     
    2020use Pod::Usage qw( pod2usage );
    2121
    22 my ( $class_id, $dateobs_begin, $dateobs_end, $skip_burned, $rerun_from_first, $dbname, $logfile, $verbose, $save_temps);
     22my ( $class_id, $dateobs_begin, $dateobs_end, $convert, $camera, $dbname, $logfile, $verbose, $save_temps);
    2323GetOptions(
    2424    'class_id=s'        => \$class_id, # chip identifier
    2525    'dateobs_begin=s'   => \$dateobs_begin, # exposure date/time range start
    2626    'dateobs_end=s'     => \$dateobs_end, # exposure date/time range stop
     27    'camera=s'          => \$camera,     # Camera
    2728    'dbname|d=s'        => \$dbname, # Database name
    2829    'logfile=s'         => \$logfile,
    29     'skip_burned'       => \$skip_burned,   # Print to stdout
    30     'rerun_from_first'  => \$rerun_from_first,   # Print to stdout
    3130    'verbose'           => \$verbose,   # Print to stdout
    3231    'save-temps'        => \$save_temps, # Save temporary files?
     32
    3333    ) or pod2usage( 2 );
    3434
     
    4343    defined $dbname;
    4444
    45 if ($skip_burned and $rerun_from_first) { &my_die("-rerun_from_first and -skip_burned are incompatible"); }
     45unless (defined $camera) {
     46    $camera = "GPC1";
     47#    warn("No camera defined. Defaulting to $camera and warning.");
     48}
    4649
    4750my $missing_tools;
     
    4952my $funpack  = can_run('funpack')  or (warn "Can't find funpack" and $missing_tools = 1);
    5053my $burntool = can_run('burntool') or (warn "Can't find burntool" and $missing_tools = 1);
    51 my $fpack    = can_run('fpack')    or (warn "Can't find fpack" and $missing_tools = 1);
     54my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
     55#my $fpack    = can_run('fpack')    or (warn "Can't find fpack" and $missing_tools = 1);
    5256if ($missing_tools) {
    5357    warn("Can't find required tools.");
     
    5761my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    5862
     63# IPP configuration (including nebulous)
     64my $ipprc = PS::IPP::Config->new( $camera ) or my_die("Unable to set up");
     65
     66$ipprc->redirect_output($logfile) if $logfile;
     67
     68# Determine the value of a "good" burntool run.
     69my $config_cmd = "$ppConfigDump -camera $camera -dump-camera - | grep BURNTOOL";
     70my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     71    run ( command => $config_cmd, verbose => $verbose);
     72unless ($success) {
     73    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     74    &my_die("Unable to perform ppConfigDump: $error_code", 0, 0, $class_id, $PS_EXIT_SYS_ERROR);
     75}
     76
     77my $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
     78    &my_die("Unable to parse metadata config doc", 0, 0, $class_id, $PS_EXIT_SYS_ERROR);
     79
     80my $burntoolStateGood = 999;
     81foreach my $cfg (@$recipeData) {
     82    if ($cfg->{name} eq 'BURNTOOL.STATE.GOOD') {
     83        $burntoolStateGood = $cfg->{value};
     84    }
     85}
     86if ($burntoolStateGood == 999) {
     87    &my_die("Failed to determine BURNTOOL.STATE.GOOD", $burntoolStateGood, $class_id, 0, $PS_EXIT_SYS_ERROR);
     88}
     89my $outState = -1 * abs($burntoolStateGood);
     90
     91print ">>$burntoolStateGood<<\n";
     92
     93# Define list of images to examine.
    5994my $command = "$regtool -processedimfile";
    6095$command .= " -class_id $class_id";
     
    6499$command .= " -dbname $dbname" if defined $dbname;
    65100
    66 my @command = split /\s+/, $command;
    67 my ( $stdin, $stdout, $stderr ); # Buffers for running program
    68 print "Running [$command]...\n" if $verbose;
    69 if (not run(\@command, \$stdin, \$stdout, \$stderr)) {
    70     &my_die("Unable to perform regtool -processedimfile");
    71 }
    72 print $stdout . "\n" if $verbose;
    73 
    74 my @files;
    75 my @whole = split /\n/, $stdout;
    76 my @single = ();
    77 while ( scalar @whole > 0 ) {
    78     my $value = shift @whole;
    79     push @single, $value;
    80     if ($value =~ /^\s*END\s*$/) {
    81         push @single, "\n";
    82 
    83         my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) );
    84         &my_die("Unable to parse output from regtool") unless defined $list;
    85         push @files, @$list;
    86         @single = ();
    87     }
    88 }
    89 
    90 # IPP configuration (including nebulous)
    91 my $ipprc = PS::IPP::Config->new() or my_die("Unable to set up");
    92 
    93 $ipprc->redirect_output($logfile) if $logfile;
    94 
    95 my $Nfiles = @files;
    96 print "files: $Nfiles\n";
     101( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     102    run ( command => $command, verbose => $verbose);
     103unless ($success) {
     104    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     105    &my_die("Unable to perform regtool: $error_code", $class_id, $dateobs_begin, $dateobs_end, $PS_EXIT_SYS_ERROR);
     106}
     107
     108my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     109    &my_die("Unable to parse metadata", $class_id, $dateobs_begin, $dateobs_end, $PS_EXIT_SYS_ERROR);
     110
     111my $files = parse_md_list($metadata) or
     112    &my_die("Unable to parse metadat list", $class_id, $dateobs_begin, $dateobs_end, $PS_EXIT_SYS_ERROR);
    97113
    98114my $REALRUN = 1;
    99 my $RAWTABLES = 1;
    100 # XXX if we want to avoid using the raw tables in this script, we need to delay deletion of the tempfile until the next pass.
    101 
    102 # first pass: mark the db entries to catch failures
    103 # these will not be identified as burned by chip processing (user_1 > 0.5)
    104 if (! $skip_burned)  {
    105     foreach my $file (@files) {
     115
     116# Process each file in turn, checking to see if it needs processing, and doing it all in a single pass
     117my $processNext = 0;
     118my $previousTable = '';
     119
     120foreach my $file (@$files) {
     121    my $exp_id = $file->{exp_id};
     122    my $state = $file->{burntool_state};
     123
     124    my $rawImfile = $file->{uri};
     125    my $rawImfileReal = $ipprc->file_resolve($rawImfile, 0);
     126   
     127    my $outTable  = $file->{uri};
     128    $outTable  =~ s/fits$/burn.tbl/;
     129    my $outTableReal = $ipprc->file_resolve($outTable, 1);
     130   
     131    my $process = 0;
     132
     133    if ($state == 0) {
     134        $process = 1;
     135    }
     136    elsif ($state == -1) {
     137        $process = 1;
     138    }
     139    elsif ($state == -3) {
     140        $process = 1;
     141    }
     142    elsif ($state == -2) {
     143        &my_die("Aborting as $rawImfile has modified pixel data!");
     144    }
     145    elsif ($state == $burntoolStateGood) {
     146        $process = 0;
     147        $previousTable = "infits=$rawImfileReal";
     148    }
     149    elsif ($state == -1 * $burntoolStateGood) {
     150        $process = 0;
     151        $previousTable = "in=$outTableReal";
     152    }
     153    else {
     154        $process = 1;
     155    }
     156
     157    if (($process == 1)||($processNext == 1)) {
     158        $processNext = 1;
     159        # Set state to processing
     160        my $status = vsystem ("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -burntool_state -1", $REALRUN);
     161        if ($status) {
     162            &my_die("failed to update imfile");
     163        }
     164        $file->{burntool_state} = -1;
    106165       
    107         # rerun_from_first treats the first image as already burned (if it is already burned)
    108         # the artifact table from the first file is used for the rest of the sequence.
    109         if ($rerun_from_first) {
    110             $skip_burned = 1;
    111             $rerun_from_first = 0;
    112             next;
    113         }
    114         my $exp_id = $file->{exp_id};
    115 
    116         my $status;
    117         $status = vsystem ("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -user_1 0.1", 1);
     166        my $tempfile = new File::Temp ( TEMPLATE => "$file->{exp_name}.XXXX",
     167                                        DIR => '/tmp/',
     168                                        UNLINK => !$save_temps,
     169                                        SUFFIX => '.fits');
     170        my $tempPixels = $tempfile->filename;
     171       
     172        # Run burntool
     173        $status = vsystem ("$funpack -S $rawImfileReal > $tempPixels", $REALRUN);
     174        if ($status) {
     175            &my_die("failed on funpack",$exp_id,$class_id);
     176        }
     177        if ($previousTable ne '') {
     178            $status = vsystem ("$burntool $tempPixels $previousTable out=$outTableReal tableonly=t persist=t", $REALRUN);
     179        }
     180        else {
     181            $status = vsystem ("$burntool $tempPixels out=$outTableReal tableonly=t persist=t", $REALRUN);
     182        }
     183        if ($status) {
     184            &my_die("failed on burntool",$exp_id,$class_id);
     185        }
     186       
     187        $status = vsystem ("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -burntool_state $outState", $REALRUN);
    118188        if ($status) {
    119189            &my_die("failed to update imfile");
    120190        }
    121         $file->{user_1} = 0.1;
    122     }
    123 }
    124 
    125 my $prevFileOpt = "";
    126 foreach my $file (@files) {
    127     my $exp_id = $file->{exp_id};
    128 
    129     my $rawImfile = $file->{uri};
    130     my $rawImfileReal;
    131     if ($REALRUN) {
    132         $rawImfileReal = $ipprc->file_resolve($rawImfile, 0);
    133     } else {
    134         ($rawImfileReal) = $rawImfile =~ m|^neb:/(.*)|;
    135     }
    136     print "rawImfile: $rawImfile -> $rawImfileReal\n";
    137 
    138     # mangle name, create tmp file (always a UNIX file)
    139     my $basename = `basename $rawImfile`; chomp $basename;
    140     my $tempfile = new File::Temp ( TEMPLATE => "$basename.XXXX",
    141                                     DIR => '/tmp',
    142                                     UNLINK => !$save_temps,
    143                                     SUFFIX => '.fits');
    144     my $tmpImfileReal = $tempfile->filename;
    145     # print "tmpImfile: $tmpImfile -> $tmpImfileReal\n";
    146    
    147     # destination for the burntool-applied image file (may be a NEB file)
    148     my $outImfile = $rawImfile;
    149     $outImfile =~ s/fits$/burn.fits/;
    150     my $outImfileReal = $ipprc->file_resolve($outImfile, 1);
    151     # print "outImfile: $outImfile -> $outImfileReal\n";
    152 
    153     # burntool now can write the artifacts to the fits file.
    154     # destination for the burntool artifacts.  use this if RAWTABLES is set.
    155     my $artImfile;
    156     my $artImfileReal;
    157     if ($RAWTABLES) {
    158         $artImfile = $rawImfile;
    159         $artImfile =~ s/fits$/burn.tbl/;
    160         $artImfileReal = $ipprc->file_resolve($artImfile, 1);
    161         # print "artImfile: $artImfile -> $artImfileReal\n";
    162     }
    163 
    164     print "$rawImfile : $skip_burned, $file->{user_1}\n";
    165 
    166     if (! ($skip_burned and ($file->{user_1} > 0.5))) {
    167         print "running on: $rawImfile\n";
    168         # uncompress the image (do we need to check if it is compressed?)
    169         my $status = vsystem ("$funpack -S $rawImfileReal > $tmpImfileReal", $REALRUN);
    170         if ($status) {
    171             &my_die("failed on funpack");
    172         }
    173 
    174         if ($RAWTABLES) {
    175             $status = vsystem ("$burntool $tmpImfileReal out=$artImfileReal $prevFileOpt persist=t", $REALRUN);
    176         } else {
    177             $status = vsystem ("$burntool $tmpImfileReal $prevFileOpt persist=t", $REALRUN);
    178         }
    179         if ($status) {
    180             &my_die("failed on burntool");
    181         }
    182 
    183         # compress the image (do we need to check if it is compressed?)
    184         $status = vsystem ("$fpack -S $tmpImfileReal > $outImfileReal", $REALRUN);
    185         if ($status) {
    186             &my_die("failed on fpack");
    187         }
    188 
    189         # set the user_1 value to a new result value for now
    190         $status = vsystem ("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -user_1 2.0", 1);
    191         if ($status) {
    192             &my_die("failed to update imfile");
    193         }
    194         print "\n";
    195     }
    196     # save the artifact file for the next image
    197     if ($RAWTABLES) {
    198         $prevFileOpt = "in=$artImfileReal";
    199     } else {
    200         $prevFileOpt = "infits=$artImfileReal";
    201     }
    202 }
    203 
     191
     192        $previousTable = "in=$outTableReal";
     193
     194        print "\n";
     195    }
     196}
     197   
    204198exit 0;
    205199
     
    220214sub my_die {
    221215    my $message = shift;
    222 
     216    if ($#_ != -1) {
     217        my $exp_id = shift;
     218        my $class_id = shift;
     219        vsystem("$regtool -dbname $dbname -updateprocessedimfile -exp_id $exp_id -class_id $class_id -burntool_state -3",1);
     220    }
    223221    printf STDERR "$message\n";
    224222    exit 1;
Note: See TracChangeset for help on using the changeset viewer.