IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 11, 2012, 11:19:21 PM (15 years ago)
Author:
mhuber
Message:

merged latest r33093 trunk changes to meh_branches/ppsub_test

Location:
branches/meh_branches/ppsub_test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppsub_test

  • branches/meh_branches/ppsub_test/ippScripts/scripts/videophot_process.pl

    r32125 r33098  
    88my $host = hostname();
    99my $date = `date`;
    10 print "\n\n";
    11 print "Starting script $0 on $host at $date\n\n";
    12 
    1310use DateTime;
    1411my $mjd_start = DateTime->now->mjd;   # MJD of starting script
     
    2522use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
    2623use Pod::Usage qw( pod2usage );
     24use File::Temp qw( tempfile );
     25use File::Basename qw( basename );
    2726
    2827# Look for programs we need
    2928my $missing_tools;
    3029my $vptool = can_run('vptool') or (warn "Can't find vptool" and $missing_tools = 1);
    31 my $regtool = can_run('regtool') or (warn "Can't find regtool" and $missing_tools = 1);
    3230my $psvideophot = can_run('psvideophot') or (warn "Can't find psvideophot" and $missing_tools = 1);
     31my $dumpvideo = can_run('dumpvideo') or (warn "Can't find dumpvideo" and $missing_tools = 1);
    3332my $listvideocells = can_run('listvideocells.pl') or (warn "Can't find listvideocells.pl" and $missing_tools = 1);
     33my $dsreg = can_run('dsreg') or (warn "Can't find dsreg" and $missing_tools = 1);
     34my $nebrepair = can_run('neb-repair') or (warn "Can't find neb-repair" and $missing_tools = 1);
    3435if ($missing_tools) {
    3536    warn("Can't find required tools.");
    3637    exit($PS_EXIT_CONFIG_ERROR);
    3738}
    38 my ($vp_id, $exp_id, $camera, $outroot, $dbname, $verbose, $no_update, $no_op, $redirect);
     39my ($vp_id, $camera, $outroot, $dest_id, $dbname, $verbose, $no_update, $no_op, $redirect, $save_temps);
     40my ($product, $save_video_cube, $ds_dbname, $ds_dbhost);
    3941
    4042GetOptions(
    4143    'vp_id=s'           => \$vp_id,
    42     'exp_id=s'          => \$exp_id,
    4344    'camera=s'          => \$camera,
    4445    'outroot=s'         => \$outroot,
     46    'dest_id=s'         => \$dest_id,
     47    'product=s'         => \$product,
     48    'save-video-cube'   => \$save_video_cube,
     49    'ds_dbname=s'       => \$ds_dbname,
     50    'ds_dbhost=s'       => \$ds_dbhost,
    4551    'dbname|d=s'        => \$dbname,
    4652    'verbose'           => \$verbose,
     53    'save-temps'        => \$save_temps,
    4754    'no-update'         => \$no_update, # Don't update the database?
    4855    'no-op'             => \$no_op,     # Don't do any operations?
     
    5259pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    5360pod2usage(
    54     -msg => "Required options: --vp_id --exp_id --outroot --camera",
     61    -msg => "Required options: --vp_id --outroot --camera",
    5562    -exitval => 3,
    5663          ) unless defined $vp_id
    57             and defined $exp_id
    5864            and defined $camera
    5965            and defined $outroot;
    6066
     67pod2usage(
     68    -msg => " --product -ds_dbname and --dsdbhost are required if --dest_id",
     69    -exitval => 3,
     70          ) if ($dest_id and !( defined $product and defined $ds_dbname and defined $ds_dbhost));
     71
    6172
    6273$no_update = 1 if $no_op;
    63 
    64 my $ipprc = PS::IPP::Config->new($camera) or my_die( "Unable to set up", $vp_id, 'nochip', 'nocell', $PS_EXIT_CONFIG_ERROR ); # IPP configuration
     74$vptool .= " -dbname $dbname" if $dbname;
     75
     76my $ipprc = PS::IPP::Config->new($camera) or my_die( "Unable to set up", $vp_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
    6577
    6678my $logDest = $ipprc->filename("LOG.EXP", $outroot);
    67 $ipprc->redirect_output($logDest) or my_die( "Unable to redirect output", $vp_id, 'nochip', 'nocell', $PS_EXIT_SYS_ERROR ) if $redirect;
    68 
    69 my $source_id = $ipprc->source_id($dbname, $PS_TABLE_ID_DIFF);
    70 
    71 # Get list of components for subtraction
     79if ($redirect) {
     80    $ipprc->redirect_to_logfile($logDest) or my_die( "Unable to redirect output", $vp_id, $PS_EXIT_SYS_ERROR );
     81
     82    print "\n\n";
     83    print "Starting script $0 on $host at $date\n\n";
     84}
     85
     86# Get list of chips with pending video cells
    7287my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    7388my $files;
    7489{
    75 #    my $command = "$vptool -pendingimfile -vp_id $vp_id";
    76 #    $command .= " -dbname $dbname" if defined $dbname;
    77     my $command = "$regtool -processedimfile -video_cells -exp_id $exp_id";
    78     $command .= " -dbname $dbname" if defined $dbname;
     90    my $command = "$vptool -pendingimfile -vp_id $vp_id";
    7991    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    8092        run(command => $command, verbose => $verbose);
    8193    unless ($success) {
    8294        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    83         &my_die("Unable to perform vptool -pendingimfile: $error_code", $vp_id, 'nochip', 'nocell', $error_code);
    84     }
    85 
    86     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    87         &my_die("Unable to parse metadata config doc", $vp_id, 'nochip', 'nocell', $PS_EXIT_PROG_ERROR);
    88     $files = parse_md_list($metadata) or
    89         &my_die("Unable to parse metadata list", $vp_id, 'nochip', 'nocell', $PS_EXIT_PROG_ERROR);
    90 }
     95        &my_die("Unable to perform vptool -pendingimfile: $error_code", $vp_id, $error_code);
     96    }
     97
     98    my $output = join "", @$stdout_buf;
     99    if ($output) {
     100        my $metadata = $mdcParser->parse($output) or
     101            &my_die("Unable to parse metadata config doc", $vp_id, $PS_EXIT_PROG_ERROR);
     102        $files = parse_md_list($metadata) or
     103        &my_die("Unable to parse metadata list", $vp_id, $PS_EXIT_PROG_ERROR);
     104    }
     105}
     106
    91107
    92108foreach my $file (@$files) {
     
    95111
    96112    my $resolved = $ipprc->file_resolve($uri);
    97     &my_die("Unable to resolve $uri", $vp_id, $class_id, 'nocell', $PS_EXIT_UNKNOWN_ERROR) unless $resolved;
     113    &my_die("Unable to resolve $uri", $vp_id, $PS_EXIT_UNKNOWN_ERROR) unless $resolved;
    98114
    99115    my $command = "$listvideocells --file $resolved";
     
    101117        run(command => $command, verbose => $verbose);
    102118    unless ($success) {
     119        check_input_file($uri, $vp_id, $class_id, 'nocell');
    103120        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    104         &my_die("Unable to perform $command: $error_code", $vp_id, $class_id,
    105             'nocell', $error_code);
     121        &my_die("Unable to perform $command: $error_code", $vp_id, $error_code);
    106122    }
    107123
    108124    my @video_cells = split "\n", (join "", @$stdout_buf);
    109     &my_die("No video cells found in $uri", $vp_id, $class_id,
    110             'nocell', $PS_EXIT_UNKNOWN_ERROR) unless scalar @video_cells;
     125    &my_die("No video cells found in $uri", $vp_id, $PS_EXIT_UNKNOWN_ERROR) unless scalar @video_cells;
    111126
    112127    foreach my $cell_id (@video_cells) {
    113         my $command = "$psvideophot $outroot";
     128        my $path_base =  "$outroot.$class_id.$cell_id";
     129        my $error;
     130        my $output = $ipprc->prepare_output("PSVIDEOPHOT.OUTPUT", $path_base, undef, 1, \$error)
     131            or &my_die("failed to prepare output file for PSVIDEOPHOT.OUTPUT", $vp_id, $error);
     132        my $command = "$psvideophot $output";
    114133        $command .= " -file $uri";
     134        $command .= " -class_id $class_id";
    115135        $command .= " -cell_id $cell_id";
    116         $command .= " -dbname $dbname" if defined $dbname;
     136        my $vpstart = DateTime->now->mjd;
    117137        unless ($no_op) {
    118138            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    120140            unless ($success) {
    121141                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    122                 &my_die("Unable to perform ppSub: $error_code", $vp_id, $class_id, $cell_id, $error_code);
     142                &my_die("Unable to perform psvideophot: $error_code", $vp_id, $error_code);
    123143            }
    124 
    125             # check_output($outputStats, 1);
    126144        } else {
    127145            print "Not executing: $command\n";
    128146        }
     147        # dump the video cell and the video table to a file
     148        if ($save_video_cube) {
     149            my $output = $path_base . ".fits";
     150            my $extname = "$cell_id";
     151            my $command = "$dumpvideo $uri $extname $output -includetable";
     152            unless ($no_op) {
     153                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     154                    run(command => $command, verbose => $verbose);
     155                unless ($success) {
     156                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     157                    &my_die("Unable to perform dumpvideo: $error_code", $vp_id, $error_code);
     158                }
     159            } else {
     160                print "Not executing: $command\n";
     161            }
     162        }
     163
    129164        unless ($no_update) {
    130             my $command = "$vptool -addvpcell -vp_id $vp_id -class_id $class_id -cell_id $cell_id";
    131             $command .= " -path_base $outroot";
    132 
    133             $command .= " -dbname $dbname" if defined $dbname;
     165            my $command = "$vptool -addprocessedcell -vp_id $vp_id -class_id $class_id -cell_id $cell_id";
     166            $command .= " -path_base $path_base";
     167            $command .= " -hostname $host";
     168            $command .= " -dtime_photom " . ((DateTime->now->mjd - $vpstart) * 86400);
    134169
    135170            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    138173                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    139174                my $err_message = "Unable to perform vptool -addvpcell";
    140                 &my_die("$err_message: $error_code", $vp_id, $class_id, $cell_id, $error_code);
     175                &my_die("$err_message: $error_code", $vp_id, $error_code);
    141176            }
    142177        }
    143178    }
    144179}
     180if ($dest_id) {
     181    my $command = "$vptool -processedcell -vp_id $vp_id";
     182    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     183        run(command => $command, verbose => $verbose);
     184    unless ($success) {
     185        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     186        &my_die("Unable to perform vptool -pendingimfile: $error_code", $vp_id, $error_code);
     187    }
     188
     189    my $output = join "", @$stdout_buf;
     190    my $cells;
     191    if ($output) {
     192        my $metadata = $mdcParser->parse($output) or
     193            &my_die("Unable to parse metadata config doc", $vp_id, $PS_EXIT_PROG_ERROR);
     194        $cells = parse_md_list($metadata) or
     195            &my_die("Unable to parse metadata list", $vp_id, $PS_EXIT_PROG_ERROR);
     196    }
     197    my ($reglist, $reglistName) = tempfile("/tmp/filelist.$vp_id.XXXX", UNLINK => !$save_temps);
     198
     199    if ($redirect) {
     200        print $reglist "$logDest|||text|\n";
     201    }
     202    # list of extensions and types for files to distribute
     203    my @exts = qw( vpt );
     204    my @types = qw( table );
     205    if ($save_video_cube) {
     206        push @exts, 'fits';
     207        push @types, 'fits';
     208    }
     209    foreach my $cell (@$cells) {
     210        my $path_base = $cell->{path_base};
     211        for (my $i = 0; $i < scalar @exts; $i++) {
     212            my $file = "$path_base.$exts[$i]";
     213            my $type = $types[$i];
     214
     215            print $reglist "$file|||$type|\n";
     216        }
     217    }
     218    close $reglist or &my_die("failed to close $reglistName", $vp_id, $PS_EXIT_UNKNOWN_ERROR);
     219    unless ($no_update or $no_op) {
     220        my $fileset = basename($outroot);
     221        my $command = "$dsreg --add $fileset --product $product --type dump --list $reglistName";
     222        $command .= " --abspath --link";
     223        $command .= " --dbname $ds_dbname --dbhost $ds_dbhost";
     224        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     225            run(command => $command, verbose => $verbose);
     226        unless ($success) {
     227            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     228             &my_die("Unable to perform $command error_code: $error_code", $vp_id, $error_code);
     229        }
     230    }
     231}
     232
     233unless ($no_update) {
     234    my $command = "$vptool -updaterun -vp_id $vp_id -set_state full";
     235    $command .= " -set_outroot $outroot";
     236    $command .= " -set_hostname $host";
     237    $command .= " -set_dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400);
     238
     239    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     240        run(command => $command, verbose => $verbose);
     241    unless ($success) {
     242        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     243        my $err_message = "Unable to perform vptool -updaterun";
     244        &my_die("$err_message: $error_code", $vp_id, $error_code);
     245    }
     246}
    145247
    146248exit 0;
     249
     250sub check_input_file {
     251    my $uri = shift;
     252    my $vp_id = shift;
     253    my $class_id = shift;
     254    my $cell_id = shift;
     255    my $resolved = $ipprc->file_resolve($uri);
     256
     257    my $tryrepair = 0;
     258    if (!-e $resolved) {
     259        printf STDERR "instance $resolved for $uri does not exist\n";
     260        $tryrepair = 1;
     261    } elsif (-s $resolved == 0) {
     262        printf STDERR "instance $resolved for $uri is empty\n";
     263        $tryrepair = 1;
     264    }
     265    if ($tryrepair) {
     266        my $scheme = file_scheme($uri);
     267        if ($scheme and ($scheme = 'neb')) {
     268            my $command = "$nebrepair $uri";
     269            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     270                run(command => $command, verbose => $verbose);
     271            unless ($success) {
     272                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     273                my $err_message = "Unable to perform nebrepair";
     274                &my_die("$err_message: $error_code", $vp_id, $error_code);
     275            }
     276       }
     277    }
     278}
    147279
    148280sub my_die
    149281{
    150282    my $msg = shift;            # Warning message on die
    151     my $vp_id = shift;
    152     my $class_id = shift;       # chip identifier
    153     my $cell_id = shift;       # chip identifier
     283    my $vp_id = shift;          # vpRun id
    154284    my $exit_code = shift;      # Exit code to add
    155285
     
    157287
    158288    warn($msg);
    159     if (defined $vp_id and defined $class_id and not $no_update) {
    160         my $command = "$vptool -vp_id $vp_id -class_id $class_id -cell_id -fault $exit_code";
    161         $command .= " -addvpcell";
    162         $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
    163         $command .= " -hostname $host" if defined $host;
    164         $command .= " -path_base $outroot" if defined $outroot;
    165         $command .= " -dbname $dbname" if defined $dbname;
     289    if (defined $vp_id and not $no_update) {
     290        my $command = "$vptool -vp_id $vp_id";
     291        $command .= " -updaterun";
     292        $command .= " -set_fault $exit_code";
     293        $command .= " -set_hostname $host" if defined $host;
     294        $command .= " -set_outroot $outroot" if defined $outroot;
     295        $command .= " -set_dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400);
     296
    166297        run(command => $command, verbose => $verbose);
    167298    }
     
    169300}
    170301
    171 END {
    172     my $exit = $?;
    173     system("sync") == 0 or die "failed to execute sync: $!";
    174     $? = $exit;
    175 }
    176 
    177302__END__
Note: See TracChangeset for help on using the changeset viewer.