IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28661


Ignore:
Timestamp:
Jul 12, 2010, 4:46:07 PM (16 years ago)
Author:
rhenders
Message:

Big changes to support Conrad's new DXLayer, among other things

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/scripts/ippToPsps_run.pl

    r28529 r28661  
    1313
    1414# globals
    15 my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $survey, $singleExpId, $no_publish, $force, $initBatch);
     15my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $survey, $singleExpId, $force, $initBatch, $help);
    1616
    1717# default values for certain globals
     
    2020$no_op = 0;
    2121$no_update = 0;
    22 $no_publish = 0;
    2322$camera = 'GPC1';
    2423$output = undef;
    2524$singleExpId = undef;
    26 $datastoreProduct = "PSPS_test";
     25$datastoreProduct = undef;
    2726$force = 0;
    2827$initBatch = 0;
     28$help = 0;
    2929
    3030# get user args
     
    3636        'expid|e=s' => \$singleExpId,
    3737        'product|p=s' => \$datastoreProduct,
    38         'no_publish' => \$no_publish,
    3938        'verbose|v' => \$verbose,
    4039        'save_temps' => \$save_temps,
     
    4241        'no-update' => \$no_update,
    4342        'force|f' => \$force,
    44         ) or pod2usage( 2 );
    45 
    46 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    47 
    48 # tell off user for not providing proper args
    49 pod2usage(
    50         -msg => "\n   Required options:\n\n".
    51         "--batch <init|det|diff|stack>\n".
    52         "--output <path>\n".
    53         "--survey <ThreePi|MD01,2 etc|STS|SAS|SweetSpot> or --expid <expid>\n" .
    54         "--dvodb <path>\n".
    55         "\n   Optional:\n\n".
    56         "--product <datastoreProduct>\n".
    57         "--no_publish - won't publish to datastore\n".
    58         "--save_temps - will save temporary files\n".
    59         "--no-op - ?\n".
    60         "--no-update - will not update database\n",
    61         -exitval => 3
    62         ) unless
    63 defined $batchType and
    64 defined $output and
    65 defined $dvodb and
    66 (( defined $survey or defined $singleExpId ) or ( $batchType eq "init"));
    67 
    68 if ($batchType eq "init") {$initBatch = 1;}
     43        'help|h' => \$help,
     44        );
     45
     46if ($help) {printUsage(); exit;}
     47if (@ARGV) {print "* Unknown option: @ARGV\n"; printUsage(); exit;}
     48
     49my $quit = 0;
     50print "\n";
     51if (!defined $batchType) {print "* ERROR: need to define a batch type (-b)\n"; $quit=1;}
     52if (!defined $output) {print "* ERROR: need to provide an output path (-o)\n"; $quit=1;}
     53if (!defined $dvodb) {print "* ERROR: need to provide a DVO Db path (-d)\n"; $quit=1;}
     54if (!defined $survey and !defined $singleExpId) {print "* ERROR: need to provide a survey type (-s) or an exposure ID (-e)\n"; $quit=1;}
     55print "\n\n";
     56
     57if ($quit) { exit; }
     58
    6959
    7060# check we can run programs and get camera config
     
    7464
    7565# make a temporary file for saving results
    76 my ($resultsFile, $resultsFilePath) = tempfile( "/tmp/ippToPsps_results.XXXX", UNLINK => !$save_temps);
    77 
     66
     67if ($batchType eq "init") {$initBatch = 1;}
    7868process();
    7969
    80 close $resultsFile;
     70
     71#######################################################################################
     72#
     73# Generates a PSPS-suitable survey 'type' from the IPP distribution group
     74#
     75#######################################################################################
     76sub printUsage {
     77
     78    print "\n" .
     79        "  *** Required options:\n".
     80        "\n" .
     81        " --batch         <init|det|diff|stack>\n".
     82        " --output        <path>\n".
     83        " --survey        <ThreePi|STS|SAS|SweetSpot|MD01|MD02|MD03 etc>\n" .
     84        " --expid         <expid>\n" .
     85        " --dvodb         <path>\n".
     86        "\n" .
     87        "    *** Optional:\n".
     88        "\n" .
     89        " --product       <datastoreProduct> if not provided, data will NOT be published\n".
     90        " --save_temps    will save temporary files\n".
     91        " --no-op         ?\n".
     92        " --no-update     will not update database\n" .
     93        " --force         will force an already processed exposure\n" .
     94        " --help          show this message\n\n";
     95
     96}
    8197
    8298#######################################################################################
     
    128144    print "*******************************************************************************\n*\n";
    129145
     146    my ($resultsFile, $resultsFilePath) = tempfile( "/tmp/ippToPsps_results.XXXX", UNLINK => !$save_temps);
    130147    my $gpc1Db = connectToDb("gpc1", "ippdb01", "ippuser", "ippuser") or return 0;
    131148    my $ippToPspsDb = connectToDb("ippToPsps", "ippdb01", "ipp", "ipp") or return 0;
     
    160177            AND chipRun.exp_id = rawExp.exp_id
    161178            AND camRun.dist_group = '$survey'   
    162             AND rawExp.dateobs >= '2010-02-20'
    163179            GROUP BY  rawExp.exp_id
    164180            ORDER BY rawExp.exp_id ASC;
    165181SQL
    166182
     183            #AND rawExp.exp_id > 133887
     184            #AND filter = 'r.00000'
    167185            #AND rawExp.dateobs <= '2010-03-12'
    168186            #AND rawExp.exp_id > $lastExpId
     
    173191    }
    174192
     193# TODO check if there are no exposures and give a warning
     194
    175195    my $batchId = 0;
    176196    my $batchesPerJob = 1;
    177     my $anyBatches = 0;
    178197    my $published = 0;
    179198
    180199    $query->execute;
    181200
    182     my $jobId = -1;
     201    #my $batchId = -1;
    183202
    184203    # loop round exposures
     
    195214        if (!$surveyType) {next;}
    196215
    197         $jobId = getNewBatchId($ippToPspsDb, $expId, $surveyType);
    198 
    199         # TODO quit here if no sensible job ID
    200 
    201         # generate batch and job paths from job and batch IDs
    202         my $job = sprintf("J%06d", $jobId);
    203         my $jobOutputPath = sprintf("$output/$job");
    204         my $batchDir = sprintf("B%03d", $batchId);
    205         my $batchOutputPath = sprintf("$jobOutputPath/%s", $batchDir);
    206         mkdir($jobOutputPath, 0777);
    207         mkdir($batchOutputPath, 0777);
    208 
    209         print "* Preparing exposure $expId as job '$job'...\n";
     216        $batchId = getNewBatchId($ippToPspsDb, $expId, $surveyType);
     217
     218        # TODO quit here if no sensible batch ID
     219
     220        # generate batch path from batch IDs
     221        my $batch = sprintf("B%08d", $batchId);
     222        my $batchDir = sprintf("$output/$batch");
     223        mkdir($batchDir, 0777);
     224        $published = 0;
     225
     226        print "* Preparing exposure $expId as batch '$batch'...\n";
    210227
    211228        # run IppToPsps program
    212         if (runIppToPsps($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType)) {
     229        if (runIppToPsps($gpc1Db, $expId, $expName, $surveyType, $batchDir, $batchType, $resultsFilePath)) {
     230
     231            my ($filename, $minObjId, $maxObjId, $totalDetections);
     232            parseResults($resultsFilePath, \$filename, \$minObjId, \$maxObjId, \$totalDetections);
    213233
    214234            # write batch manifest and tar and zip up
    215             if (writeBatchManifest($batchOutputPath, $batchDir, $batchType)) {
    216 
    217                 if (tarAndZipDirectory($jobOutputPath, $batchDir)) {
    218 
    219                     $anyBatches = 1
    220                 }
     235            if (writeBatchManifest($batchDir, $batch, $batchType, $surveyType, $filename, $minObjId, $maxObjId)) {
     236
     237                my $tarball = tarAndZipBatch($output, $batch);
     238                if ($tarball && $datastoreProduct && publishToDatastore($batch, $output, $tarball)) {$published = 1;}
    221239            }
    222240
    223             if (($batchId+1) < $batchesPerJob) {
    224                 $batchId++;
    225             }
    226             # job is complete, now publish
    227             elsif ($anyBatches) {
    228 
    229                 $published = 0;
    230                 if (writeJobManifest($output, $job, $surveyType)) {
    231 
    232                     if (!$no_publish && publishToDatastore($output, $job)) {
    233                         $published = 1;
    234                     }
    235                 }
    236 
    237                 updateDb($ippToPspsDb, $jobId, $expId, 1, $published);
    238 
    239                 $batchId = 0;
    240                 $anyBatches = 0;
    241             }
    242             else {
    243 
    244                 print "* No batches to publish\n";
    245             }
     241            updateDb($ippToPspsDb, $batchId, $expId, 1, $published, $totalDetections);
    246242        }
    247243
     
    253249    $gpc1Db->disconnect();
    254250    $ippToPspsDb->disconnect();
     251    close $resultsFile;
    255252
    256253    return 1;
     
    289286########################################################################################
    290287sub updateDb {
    291     my ($db, $batchId, $expId, $processed, $published) = @_;
     288    my ($db, $batchId, $expId, $processed, $published, $totalDetections) = @_;
    292289
    293290    my $query = $db->prepare(<<SQL);
    294291
    295292    UPDATE batches
    296         SET processed = $processed, on_datastore = $published
     293        SET processed = $processed, on_datastore = $published, total_detections = $totalDetections
    297294        WHERE batch_id = $batchId
    298295        AND exp_id = $expId;
     
    330327#######################################################################################
    331328#
    332 # Generates a new record in the database with a new jobId
     329# Generates a new record in the database with a new batch ID
    333330#
    334331########################################################################################
     
    371368#######################################################################################
    372369sub runIppToPsps {
    373     my ($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType) = @_;
     370    my ($gpc1Db, $expId, $expName, $surveyType, $batchDir, $batchType, $resultsFilePath) = @_;
    374371
    375372    my $success = 0;
    376373
    377374    if ($batchType =~ /init/) {
    378         $success = produceInit($batchOutputPath);
     375        $success = produceInit($batchDir, $resultsFilePath);
    379376    }
    380377    elsif ($batchType =~ /det/) {
    381         $success = produceDetections($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath);
     378        $success = produceDetections($gpc1Db, $expId, $expName, $surveyType, $batchDir, $resultsFilePath);
    382379    }
    383380    elsif ($batchType =~ /diff/) {
    384         $success = produceDiffs($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath);
     381        $success = produceDiffs($gpc1Db, $expId, $expName, $surveyType, $batchDir, $resultsFilePath);
    385382    }
    386383
     
    390387#######################################################################################
    391388#
    392 # tar 'n zip up a directory TODO handle errors
    393 #
    394 #######################################################################################
    395 sub tarAndZipDirectory {
    396     my ($jobOutputPath,$batch) = @_;
    397 
    398     my $batchDir = "$jobOutputPath/$batch";
    399     my $batchTar = "$batchDir.tar";
    400     my $batchZip = "$batchTar.gz";
     389# tar 'n zip a batch TODO handle errors
     390#
     391#######################################################################################
     392sub tarAndZipBatch {
     393    my ($path,$batch) = @_;
     394
     395    my $batchDir = "$path/$batch";
     396    my $tar = "$batch.tar";
     397    my $tarball = "$tar.gz";
     398    my $batchTar = "$path/$tar";
     399    my $batchZip = "$path/$tarball";
    401400
    402401    # tar
    403     my $command = "tar -cf $batchTar -C $jobOutputPath $batch";
     402    my $command = "tar -cvf $batchTar -C $path $batch";
    404403    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    405404        run(command => $command, verbose => $verbose);
    406 
    407     if (!$success) { print "* Unable to tar up dir: $batch\n" and return 0 };
     405    if (!$success) { print "* Unable to tar up contents of dir: $batchDir\n" and return undef; }
    408406
    409407    # zip
     
    411409    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    412410        run(command => $command, verbose => $verbose);
    413 
    414     if (!$success) { print "* Unable to gzip: $batchTar\n" and return 0 };
    415 
     411    if (!$success) { print "* Unable to gzip: $batchTar\n" and return undef; }
     412
     413    # remove tar file
    416414    $command = "rm $batchTar";
    417415    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    418416        run(command => $command, verbose => $verbose);
    419 
    420417    if (!$success) { print "* Unable to remove: $batchTar\n" };
    421418
     419    # remove batch dir
    422420    $command = "rm -r $batchDir";
    423421    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    424422        run(command => $command, verbose => $verbose);
    425 
    426423    if (!$success) { print "* Unable to remove: $batch\n"};
    427424
    428     return 1;
    429 }
     425    return $tarball;
     426}
     427
     428#######################################################################################
     429#
     430# zip a file then delete it
     431#
     432#######################################################################################
     433sub zipFile {
     434    my ($path,$origFile) = @_;
     435
     436    my $file;
     437
     438    # zip
     439    my $zippedName =  "$origFile.gz";
     440    my $command = "gzip -c $path/$origFile  > $path/$zippedName";
     441    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
     442        run(command => $command, verbose => $verbose);
     443
     444    if (!$success) {
     445
     446        $file = $origFile;
     447        print "* Unable to gzip: $path/$origFile\n";
     448    }
     449    else {
     450
     451        $file = $zippedName;
     452
     453        # now delete original
     454        $command = "rm -r $path/$origFile";
     455        ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     456            run(command => $command, verbose => $verbose);
     457
     458        if (!$success) { print "* Unable to remove: $path/$origFile\n"};
     459    }
     460
     461    return $file;
     462}
     463
     464
     465#######################################################################################
     466#
     467# parse results XML
     468#
     469#######################################################################################
     470sub parseResults {
     471    my ($resultsFilePath, $filename, $minObjId, $maxObjId, $totalDetections) = @_;
     472
     473    my $parser = XML::LibXML->new;
     474    my $doc = $parser->parse_file($resultsFilePath);
     475    ${$filename} = $doc->findvalue('//filename');
     476    ${$minObjId} = $doc->findvalue('//minObjID');
     477    ${$maxObjId} = $doc->findvalue('//maxObjID');
     478    ${$totalDetections} = $doc->findvalue('//totalDetections');
     479}
     480
    430481
    431482#######################################################################################
     
    435486#######################################################################################
    436487sub writeBatchManifest {
    437 
    438     my ($path,$batch,$batchType) = @_;
     488    my ($path,$batch,$batchType,$surveyType,$filename,$minObjId,$maxObjId) = @_;
    439489
    440490    use XML::Writer;
     
    451501    # determine batch 'type'
    452502    my $type;
    453     if($batchType eq 'init') {$type = "IN";}
    454     elsif($batchType eq 'det') {$type = "P2";}
     503    if ($batchType eq 'init') {$type = "IN";}
     504    elsif ($batchType eq 'det') {$type = "P2";}
    455505    elsif ($batchType eq 'stack') {$type = "ST";}
    456506    elsif ($batchType eq 'diff') {$type = "OB";}
    457507    else {$type = "UNKNOWN";}
    458 
    459     # read results
    460     my $parser = XML::LibXML->new;
    461     my $doc = $parser->parse_file($resultsFilePath);
    462     my $filename = $doc->findvalue('//filename');
    463     my $minObjId = $doc->findvalue('//minObjID');
    464     my $maxObjId = $doc->findvalue('//maxObjID');
    465508
    466509    # create XML file
     
    475518
    476519    my $foundFits = 0;
    477 
    478520    foreach my $f (@thefiles) {
    479521
     
    481523
    482524            if ($foundFits) { print "* More than one FITS file found for this batch\n"; return 0;}
     525
     526            # my $file = zipFile($path, $f);
    483527
    484528            # get md5sum of file
     
    494538                        "name" => "$batch",
    495539                        "type" => $type,
     540                        "survey" => $surveyType,
    496541                        "timestamp" => "$timeStamp",
    497542                        "minObjId" => $minObjId,
     
    511556                    "md5" => $md5sum);
    512557
    513             #$writer->startTag('file' );
    514             #$writer->startTag('name'); $writer->characters("$f"); $writer->endTag();
    515             #$writer->startTag('bytes'); $writer->characters("$filesize"); $writer->endTag();
    516             #$writer->startTag('md5'); $writer->characters("$md5sum"); $writer->endTag();
    517 
    518             #if($batchType eq 'det' && $filename eq $f) {
    519 
    520                 #    $writer->startTag('minObjId'); $writer->characters("$minObjId"); $writer->endTag();
    521                 #    $writer->startTag('maxObjId'); $writer->characters("$maxObjId"); $writer->endTag();
    522                 #}
    523558            $writer->endTag();
    524             # finish up
    525559            $writer->endTag();
    526560            $writer->end();
     
    530564    }
    531565
    532 
    533566    if (!$foundFits) { print "* Could not find any FITS files for this batch\n"; return 0;}
    534567    return 1;
     
    537570#######################################################################################
    538571#
    539 # write job manifest
    540 #
    541 #######################################################################################
    542 sub writeJobManifest {
    543     my ($path, $job, $surveyType) = @_;
    544 
    545     use XML::Writer;
    546     use Digest::MD5::File qw( file_md5_hex );
    547 
    548     # sort out paths
    549     my $outputPath = "$path/$job/JobManifest.xml";
    550     my $output = new IO::File(">$outputPath");
    551 
    552     # create a timestamp
    553     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    554     my $timeStamp = sprintf "%4d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec;
    555 
    556     # create XML file
    557     my $writer = new XML::Writer(OUTPUT => $output, DATA_MODE => 1, DATA_INDENT=>2);
    558     $writer->xmlDecl('UTF-8');
    559     $writer->doctype('manifest', "", "psps-manifest.dtd");
    560     $writer->startTag('manifest',
    561             "name" => "$job",
    562             "type" => $surveyType,
    563             "timestamp" => "$timeStamp");
    564 
    565     my $jobPath = "$path/$job";
    566 
    567     # open directory to hunt for FITS files
    568     opendir(DIR, $jobPath) or print "* Cannot open '$path' to write job manifest\n" and return 0;
    569     my @thefiles= readdir(DIR);
    570     closedir(DIR);
    571 
    572     my $foundBatch = 0;
    573 
    574     foreach my $f (@thefiles) {
    575 
    576         if (($f =~ /\.tar\.gz/)) {
    577 
    578             my $filePath = "$jobPath/$f";
    579 
    580             # get md5sum of file
    581             my $md5sum  = file_md5_hex($filePath);
    582 
    583             # get size of file in bytes
    584             my $filesize = -s $filePath;
    585 
    586             # write the tag for this batch
    587             $writer->startTag('file',
    588                     "name" => $f,
    589                     "bytes" => "$filesize",
    590                     "md5" => "$md5sum",
    591                     );
    592             $writer->endTag();
    593             $foundBatch = 1;
    594         }
    595     }
    596 
    597     # finish up
    598     $writer->endTag();
    599     $writer->end();
    600 
    601     if (!$foundBatch) { print "* Could not find any batches for this job\n"; return 0;}
    602     return 1;
    603 }
    604 
    605 #######################################################################################
    606 #
    607572# register new job with the datastore
    608573#
    609574########################################################################################
    610575sub publishToDatastore {
    611     my ($path, $job) = @_;
    612 
    613     my $fullJobPath = "$path/$job";
     576    my ($batch, $path, $tarball) = @_;
    614577
    615578    my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_dsregList.XXXX", UNLINK => !$save_temps);
    616579
    617     # loop through all batch files in this job directory
    618     opendir(DIR, $fullJobPath) or print "* Cannot open '$fullJobPath' in order to publish to datastore\n" and return 0;
    619     my @thefiles= readdir(DIR);
    620     closedir(DIR);
    621 
    622     my $foundBatch = 0;
    623 
    624     foreach my $f (@thefiles) {
    625 
    626         # get all batch tgz files
    627         if ($f =~ /\.tar.gz/) {print $tempFile $f . '|||tgz' . "\n"; $foundBatch = 1;}
    628 
    629         # get JobManifest XML file
    630         elsif ($f =~ /\.xml/) {print $tempFile $f . '|||xml' . "\n";}
    631     }
    632 
    633     if (!$foundBatch) { print "Could not find any batches to publish\n"; return 0;}
     580    print $tempFile $tarball . '|||tgz' . "\n";
    634581
    635582    # build dsreg command command
    636583    my $command  = "$dsreg";
    637     $command .= " --add $job";
     584    $command .= " --add $batch";
    638585    $command .= " --copy";
    639     $command .= " --datapath $fullJobPath";
     586    $command .= " --datapath $path";
    640587    $command .= " --type IPP_PSPS";
    641588    $command .= " --product $datastoreProduct";
     
    646593        run(command => $command, verbose => $verbose);
    647594
    648     if (!$success) { print "* Unable to publish $job to datastore\n" and return 0 };
    649 
    650     print "* Successfully published $job to datastore\n";
     595    if (!$success) { print "* Unable to publish $tarball to datastore\n" and return 0 };
     596
     597    print "* Successfully published $tarball to datastore\n";
    651598
    652599    close($tempFile);
     
    661608#######################################################################################
    662609sub produceInit {
    663     my ($outputPath) = @_;
    664 
    665     my $success = runProgram("NULL", $outputPath, 0, "NULL", "NULL", $batchType);
     610    my ($outputPath, $resultsFilePath) = @_;
     611
     612    my $success = runProgram("NULL", $outputPath, 0, "NULL", "NULL", $batchType, $resultsFilePath);
    666613    return $success;
    667614}
     615
    668616#######################################################################################
    669617#
     
    672620#######################################################################################
    673621sub produceDetections {
    674     my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_;
     622    my ($gpc1Db, $expId, $expName, $surveyType, $outputPath, $resultsFilePath) = @_;
    675623
    676624    # query to retrieve nebulous key of camera smf file for this exposure
     
    708656        close $tempFile;
    709657
    710         $success = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType);
     658        $success = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType, $resultsFilePath);
    711659        return $success;
    712660    }
     
    719667#######################################################################################
    720668sub produceDiffs {
    721     my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_;
     669    my ($gpc1Db, $expId, $expName, $surveyType, $outputPath, $resultsFilePath) = @_;
    722670
    723671    my $query =
     
    757705    }
    758706
    759     my $ret = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType);
     707    my $ret = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType, $resultsFilePath);
    760708
    761709    close $tempFile;
     
    770718#######################################################################################
    771719sub runProgram {
    772     my ($input, $output, $expid, $expName, $surveyType, $batchType) = @_;
     720    my ($input, $output, $expid, $expName, $surveyType, $batchType, $resultsFilePath) = @_;
    773721
    774722    # build command
     
    790738    return $success;
    791739}
    792 
    793 
    794 
Note: See TracChangeset for help on using the changeset viewer.