Changeset 28661 for trunk/ippToPsps/scripts/ippToPsps_run.pl
- Timestamp:
- Jul 12, 2010, 4:46:07 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/ippToPsps/scripts/ippToPsps_run.pl (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippToPsps/scripts/ippToPsps_run.pl
r28529 r28661 13 13 14 14 # globals 15 my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $survey, $singleExpId, $ no_publish, $force, $initBatch);15 my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $survey, $singleExpId, $force, $initBatch, $help); 16 16 17 17 # default values for certain globals … … 20 20 $no_op = 0; 21 21 $no_update = 0; 22 $no_publish = 0;23 22 $camera = 'GPC1'; 24 23 $output = undef; 25 24 $singleExpId = undef; 26 $datastoreProduct = "PSPS_test";25 $datastoreProduct = undef; 27 26 $force = 0; 28 27 $initBatch = 0; 28 $help = 0; 29 29 30 30 # get user args … … 36 36 'expid|e=s' => \$singleExpId, 37 37 'product|p=s' => \$datastoreProduct, 38 'no_publish' => \$no_publish,39 38 'verbose|v' => \$verbose, 40 39 'save_temps' => \$save_temps, … … 42 41 'no-update' => \$no_update, 43 42 '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 46 if ($help) {printUsage(); exit;} 47 if (@ARGV) {print "* Unknown option: @ARGV\n"; printUsage(); exit;} 48 49 my $quit = 0; 50 print "\n"; 51 if (!defined $batchType) {print "* ERROR: need to define a batch type (-b)\n"; $quit=1;} 52 if (!defined $output) {print "* ERROR: need to provide an output path (-o)\n"; $quit=1;} 53 if (!defined $dvodb) {print "* ERROR: need to provide a DVO Db path (-d)\n"; $quit=1;} 54 if (!defined $survey and !defined $singleExpId) {print "* ERROR: need to provide a survey type (-s) or an exposure ID (-e)\n"; $quit=1;} 55 print "\n\n"; 56 57 if ($quit) { exit; } 58 69 59 70 60 # check we can run programs and get camera config … … 74 64 75 65 # make a temporary file for saving results 76 my ($resultsFile, $resultsFilePath) = tempfile( "/tmp/ippToPsps_results.XXXX", UNLINK => !$save_temps); 77 66 67 if ($batchType eq "init") {$initBatch = 1;} 78 68 process(); 79 69 80 close $resultsFile; 70 71 ####################################################################################### 72 # 73 # Generates a PSPS-suitable survey 'type' from the IPP distribution group 74 # 75 ####################################################################################### 76 sub 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 } 81 97 82 98 ####################################################################################### … … 128 144 print "*******************************************************************************\n*\n"; 129 145 146 my ($resultsFile, $resultsFilePath) = tempfile( "/tmp/ippToPsps_results.XXXX", UNLINK => !$save_temps); 130 147 my $gpc1Db = connectToDb("gpc1", "ippdb01", "ippuser", "ippuser") or return 0; 131 148 my $ippToPspsDb = connectToDb("ippToPsps", "ippdb01", "ipp", "ipp") or return 0; … … 160 177 AND chipRun.exp_id = rawExp.exp_id 161 178 AND camRun.dist_group = '$survey' 162 AND rawExp.dateobs >= '2010-02-20'163 179 GROUP BY rawExp.exp_id 164 180 ORDER BY rawExp.exp_id ASC; 165 181 SQL 166 182 183 #AND rawExp.exp_id > 133887 184 #AND filter = 'r.00000' 167 185 #AND rawExp.dateobs <= '2010-03-12' 168 186 #AND rawExp.exp_id > $lastExpId … … 173 191 } 174 192 193 # TODO check if there are no exposures and give a warning 194 175 195 my $batchId = 0; 176 196 my $batchesPerJob = 1; 177 my $anyBatches = 0;178 197 my $published = 0; 179 198 180 199 $query->execute; 181 200 182 my $jobId = -1;201 #my $batchId = -1; 183 202 184 203 # loop round exposures … … 195 214 if (!$surveyType) {next;} 196 215 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"; 210 227 211 228 # 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); 213 233 214 234 # 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;} 221 239 } 222 240 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); 246 242 } 247 243 … … 253 249 $gpc1Db->disconnect(); 254 250 $ippToPspsDb->disconnect(); 251 close $resultsFile; 255 252 256 253 return 1; … … 289 286 ######################################################################################## 290 287 sub updateDb { 291 my ($db, $batchId, $expId, $processed, $published ) = @_;288 my ($db, $batchId, $expId, $processed, $published, $totalDetections) = @_; 292 289 293 290 my $query = $db->prepare(<<SQL); 294 291 295 292 UPDATE batches 296 SET processed = $processed, on_datastore = $published 293 SET processed = $processed, on_datastore = $published, total_detections = $totalDetections 297 294 WHERE batch_id = $batchId 298 295 AND exp_id = $expId; … … 330 327 ####################################################################################### 331 328 # 332 # Generates a new record in the database with a new jobId329 # Generates a new record in the database with a new batch ID 333 330 # 334 331 ######################################################################################## … … 371 368 ####################################################################################### 372 369 sub runIppToPsps { 373 my ($gpc1Db, $expId, $expName, $surveyType, $batch OutputPath, $batchType) = @_;370 my ($gpc1Db, $expId, $expName, $surveyType, $batchDir, $batchType, $resultsFilePath) = @_; 374 371 375 372 my $success = 0; 376 373 377 374 if ($batchType =~ /init/) { 378 $success = produceInit($batch OutputPath);375 $success = produceInit($batchDir, $resultsFilePath); 379 376 } 380 377 elsif ($batchType =~ /det/) { 381 $success = produceDetections($gpc1Db, $expId, $expName, $surveyType, $batch OutputPath);378 $success = produceDetections($gpc1Db, $expId, $expName, $surveyType, $batchDir, $resultsFilePath); 382 379 } 383 380 elsif ($batchType =~ /diff/) { 384 $success = produceDiffs($gpc1Db, $expId, $expName, $surveyType, $batch OutputPath);381 $success = produceDiffs($gpc1Db, $expId, $expName, $surveyType, $batchDir, $resultsFilePath); 385 382 } 386 383 … … 390 387 ####################################################################################### 391 388 # 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 ####################################################################################### 392 sub 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"; 401 400 402 401 # tar 403 my $command = "tar -c f $batchTar -C $jobOutputPath $batch";402 my $command = "tar -cvf $batchTar -C $path $batch"; 404 403 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 405 404 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; } 408 406 409 407 # zip … … 411 409 ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 412 410 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 416 414 $command = "rm $batchTar"; 417 415 ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 418 416 run(command => $command, verbose => $verbose); 419 420 417 if (!$success) { print "* Unable to remove: $batchTar\n" }; 421 418 419 # remove batch dir 422 420 $command = "rm -r $batchDir"; 423 421 ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 424 422 run(command => $command, verbose => $verbose); 425 426 423 if (!$success) { print "* Unable to remove: $batch\n"}; 427 424 428 return 1; 429 } 425 return $tarball; 426 } 427 428 ####################################################################################### 429 # 430 # zip a file then delete it 431 # 432 ####################################################################################### 433 sub 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 ####################################################################################### 470 sub 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 430 481 431 482 ####################################################################################### … … 435 486 ####################################################################################### 436 487 sub writeBatchManifest { 437 438 my ($path,$batch,$batchType) = @_; 488 my ($path,$batch,$batchType,$surveyType,$filename,$minObjId,$maxObjId) = @_; 439 489 440 490 use XML::Writer; … … 451 501 # determine batch 'type' 452 502 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";} 455 505 elsif ($batchType eq 'stack') {$type = "ST";} 456 506 elsif ($batchType eq 'diff') {$type = "OB";} 457 507 else {$type = "UNKNOWN";} 458 459 # read results460 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');465 508 466 509 # create XML file … … 475 518 476 519 my $foundFits = 0; 477 478 520 foreach my $f (@thefiles) { 479 521 … … 481 523 482 524 if ($foundFits) { print "* More than one FITS file found for this batch\n"; return 0;} 525 526 # my $file = zipFile($path, $f); 483 527 484 528 # get md5sum of file … … 494 538 "name" => "$batch", 495 539 "type" => $type, 540 "survey" => $surveyType, 496 541 "timestamp" => "$timeStamp", 497 542 "minObjId" => $minObjId, … … 511 556 "md5" => $md5sum); 512 557 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 #}523 558 $writer->endTag(); 524 # finish up525 559 $writer->endTag(); 526 560 $writer->end(); … … 530 564 } 531 565 532 533 566 if (!$foundFits) { print "* Could not find any FITS files for this batch\n"; return 0;} 534 567 return 1; … … 537 570 ####################################################################################### 538 571 # 539 # write job manifest540 #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 paths549 my $outputPath = "$path/$job/JobManifest.xml";550 my $output = new IO::File(">$outputPath");551 552 # create a timestamp553 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 file557 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 files568 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 file581 my $md5sum = file_md5_hex($filePath);582 583 # get size of file in bytes584 my $filesize = -s $filePath;585 586 # write the tag for this batch587 $writer->startTag('file',588 "name" => $f,589 "bytes" => "$filesize",590 "md5" => "$md5sum",591 );592 $writer->endTag();593 $foundBatch = 1;594 }595 }596 597 # finish up598 $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 #607 572 # register new job with the datastore 608 573 # 609 574 ######################################################################################## 610 575 sub publishToDatastore { 611 my ($path, $job) = @_; 612 613 my $fullJobPath = "$path/$job"; 576 my ($batch, $path, $tarball) = @_; 614 577 615 578 my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_dsregList.XXXX", UNLINK => !$save_temps); 616 579 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"; 634 581 635 582 # build dsreg command command 636 583 my $command = "$dsreg"; 637 $command .= " --add $ job";584 $command .= " --add $batch"; 638 585 $command .= " --copy"; 639 $command .= " --datapath $ fullJobPath";586 $command .= " --datapath $path"; 640 587 $command .= " --type IPP_PSPS"; 641 588 $command .= " --product $datastoreProduct"; … … 646 593 run(command => $command, verbose => $verbose); 647 594 648 if (!$success) { print "* Unable to publish $ jobto datastore\n" and return 0 };649 650 print "* Successfully published $ jobto 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"; 651 598 652 599 close($tempFile); … … 661 608 ####################################################################################### 662 609 sub 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); 666 613 return $success; 667 614 } 615 668 616 ####################################################################################### 669 617 # … … 672 620 ####################################################################################### 673 621 sub produceDetections { 674 my ($gpc1Db, $expId, $expName, $surveyType, $outputPath ) = @_;622 my ($gpc1Db, $expId, $expName, $surveyType, $outputPath, $resultsFilePath) = @_; 675 623 676 624 # query to retrieve nebulous key of camera smf file for this exposure … … 708 656 close $tempFile; 709 657 710 $success = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType );658 $success = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType, $resultsFilePath); 711 659 return $success; 712 660 } … … 719 667 ####################################################################################### 720 668 sub produceDiffs { 721 my ($gpc1Db, $expId, $expName, $surveyType, $outputPath ) = @_;669 my ($gpc1Db, $expId, $expName, $surveyType, $outputPath, $resultsFilePath) = @_; 722 670 723 671 my $query = … … 757 705 } 758 706 759 my $ret = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType );707 my $ret = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType, $resultsFilePath); 760 708 761 709 close $tempFile; … … 770 718 ####################################################################################### 771 719 sub runProgram { 772 my ($input, $output, $expid, $expName, $surveyType, $batchType ) = @_;720 my ($input, $output, $expid, $expName, $surveyType, $batchType, $resultsFilePath) = @_; 773 721 774 722 # build command … … 790 738 return $success; 791 739 } 792 793 794
Note:
See TracChangeset
for help on using the changeset viewer.
