- Timestamp:
- Jun 10, 2010, 6:28:51 PM (16 years ago)
- Location:
- branches/czw_branch/20100519
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
ippToPsps/scripts/ippToPsps_run.pl (modified) (26 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/czw_branch/20100519
- Property svn:mergeinfo changed
-
branches/czw_branch/20100519/ippToPsps/scripts/ippToPsps_run.pl
r28164 r28304 12 12 13 13 # globals 14 my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $label, $singleExpId, $no_publish); 15 16 # TODO temporary until we use database to store current jobid 17 my $jobIdFilePath = "./config/currentJobId.txt"; 18 my $logFile = "./config/logfile.txt"; 14 my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $survey, $singleExpId, $no_publish, $force, $initBatch); 19 15 20 16 # default values for certain globals … … 28 24 $singleExpId = undef; 29 25 $datastoreProduct = "PSPS_test"; 30 26 $force = 0; 27 $initBatch = 0; 31 28 32 29 # get user args … … 35 32 'batch|b=s' => \$batchType, 36 33 'dvodb|d=s' => \$dvodb, 37 ' label|l=s' => \$label,34 'survey|s=s' => \$survey, 38 35 'expid|e=s' => \$singleExpId, 39 36 'product|p=s' => \$datastoreProduct, … … 43 40 'no-op' => \$no_op, 44 41 'no-update' => \$no_update, 42 'force|f' => \$force, 45 43 ) or pod2usage( 2 ); 46 44 … … 52 50 "--batch <init|det|diff|stack>\n". 53 51 "--output <path>\n". 54 "-- label <label> or --expid <expid>\n" .52 "--survey <ThreePi|MD01,2 etc|STS|SAS|SweetSpot> or --expid <expid>\n" . 55 53 "--dvodb <path>\n". 56 54 "\n Optional:\n\n". … … 65 63 defined $output and 66 64 defined $dvodb and 67 ( defined $label or defined $singleExpId ); 65 (( defined $survey or defined $singleExpId ) or ( $batchType eq "init")); 66 67 if ($batchType eq "init") {$initBatch = 1;} 68 68 69 69 # check we can run programs and get camera config … … 73 73 74 74 # make a temporary file for saving results 75 my ($resultsFile, $resultsFileName) = tempfile( "/tmp/ results.XXXX", UNLINK => !$save_temps);75 my ($resultsFile, $resultsFileName) = tempfile( "/tmp/ippToPsps_results.XXXX", UNLINK => !$save_temps); 76 76 77 77 process(); … … 89 89 if ($distGroup =~ m/^MD([0-1][0-9])$/i) {return "MD$1";} 90 90 if ($distGroup =~ m/^M31$/i) {return "M31";} 91 if ($distGroup =~ m/^sts$/i) {return "STS ";}91 if ($distGroup =~ m/^sts$/i) {return "STS1";} 92 92 if ($distGroup =~ m/^SweetSpot$/i) {return "SSS";} 93 93 if ($distGroup =~ m/^(3PI)|(ThreePi)|(SAS)$/i) {return "3PI";} … … 97 97 } 98 98 99 99 100 ####################################################################################### 100 101 # 101 # Finds all exposures for the provided label and generates PSPS FITS data for them 102 # 103 ####################################################################################### 104 sub process { 105 my $lastExpId = getLastExpId(); 106 107 print "*******************************************************************************\n*\n"; 108 print "* Last successfully processed exposure ID was $lastExpId\n"; 109 110 # Set up the database 111 my $dbname = 'gpc1'; 112 my $dbserver = 'ippdb01'; 113 my $dbuser = 'ippuser'; 114 my $dbpass = 'ippuser'; 102 # Connects to a db 103 # 104 ######################################################################################## 105 sub connectToDb { 106 my ($dbname,$dbserver,$dbuser,$dbpass) = @_; 107 108 115 109 my $db = DBI->connect("DBI:mysql:database=${dbname};host=${dbserver};" . 116 110 "mysql_socket=" . DB_SOCKET(), 117 111 ${dbuser},${dbpass}, 118 112 { RaiseError => 1, AutoCommit => 1} 119 ) or die "Unable to connect to database $DBI::errstr\n"; 113 ); 114 115 printf("* Connection to '$dbname' on '$dbserver': %s\n", $db ? "success" : "FAILED ($DBI::errstr)"); 116 117 return $db; 118 } 119 120 121 ####################################################################################### 122 # 123 # Finds all exposures for the provided TODO and generates PSPS FITS data for them 124 # 125 ####################################################################################### 126 sub process { 127 128 print "*******************************************************************************\n*\n"; 129 130 my $gpc1Db = connectToDb("gpc1", "ippdb01", "ippuser", "ippuser") or return 0; 131 my $ippToPspsDb = connectToDb("ippToPsps", "ippdb01", "ipp", "ipp") or return 0; 132 my $lastExpId = getLastExpId($ippToPspsDb); 120 133 121 134 my $query; 122 if (!$singleExpId) { 123 124 # query to retrieve all exposures with the provided label 125 $query = $db->prepare(<<SQL); 126 127 SELECT DISTINCT rawExp.exp_id, camRun.dist_group 128 FROM camRun, chipRun, rawExp 129 WHERE camRun.chip_id = chipRun.chip_id 130 AND chipRun.exp_id = rawExp.exp_id 131 AND camRun.label LIKE '%$label%' 132 AND rawExp.exp_id > $lastExpId 133 ORDER BY rawExp.exp_id ASC 135 if ($initBatch) { 136 137 $query = $gpc1Db->prepare(<<SQL); 138 139 SELECT 0, 'NULL', 'ThreePi'; 134 140 SQL 141 } 142 elsif ($singleExpId) { 143 144 $query = $gpc1Db->prepare(<<SQL); 145 146 SELECT DISTINCT rawExp.exp_id, rawExp.exp_name, dist_group 147 FROM rawExp, chipRun 148 WHERE chipRun.exp_id = rawExp.exp_id 149 AND rawExp.exp_id = $singleExpId 150 SQL 151 } 152 else { 153 154 $query = $gpc1Db->prepare(<<SQL); 155 156 SELECT rawExp.exp_id, rawExp.exp_name, camRun.dist_group 157 FROM camRun, chipRun, rawExp 158 WHERE camRun.state = 'full' 159 AND camRun.chip_id = chipRun.chip_id 160 AND chipRun.exp_id = rawExp.exp_id 161 AND camRun.dist_group = '$survey' 162 GROUP BY rawExp.exp_id 163 ORDER BY rawExp.exp_id ASC; 164 SQL 165 166 #AND rawExp.dateobs <= '2010-03-12' 167 #AND rawExp.exp_id > $lastExpId 168 #AND camRun.label LIKE '%$label%' 135 169 #AND rawExp.exp_id > 136561 136 170 #AND camRun.dist_group LIKE 'sas' 137 171 #AND rawExp.decl >= '-0.157079633' AND rawExp.decl <= '0.157079633' Jims Dec range 138 172 } 139 else {140 141 $query = $db->prepare(<<SQL);142 143 SELECT DISTINCT rawExp.exp_id, dist_group FROM rawExp, chipRun WHERE chipRun.exp_id = rawExp.exp_id AND rawExp.exp_id = $singleExpId144 SQL145 }146 173 147 174 my $batchId = 0; 148 175 my $batchesPerJob = 1; 149 176 my $anyBatches = 0; 177 my $published = 0; 150 178 151 179 $query->execute; 152 153 open (LOGFILE, ">> $logFile");154 180 155 181 my $jobId = -1; … … 157 183 # loop round exposures 158 184 while (my @row = $query->fetchrow_array()) { 159 my ($expId, $distGroup) = @row; 160 161 $jobId = getNewJobId(); 162 163 print "* Exposure ID = $expId with dist group = '$distGroup' as job ID $jobId\n"; 185 my ($expId, $expName, $distGroup) = @row; 186 187 if (!$initBatch && isExposureAlreadyProcessed($ippToPspsDb, $expId)) { 188 189 if ($force) {print "* Forcing....\n";} 190 else {next}; 191 } 164 192 165 193 my $surveyType = getSurveyTypeFromDistGroup($distGroup); 166 167 194 if (!$surveyType) {next;} 168 print "* Using survey type of $surveyType\n"; 195 196 $jobId = getNewBatchId($ippToPspsDb, $expId, $surveyType); 197 198 # TODO quit here if no sensible job ID 169 199 170 200 # generate batch and job paths from job and batch IDs … … 173 203 my $batchDir = sprintf("B%03d", $batchId); 174 204 my $batchOutputPath = sprintf("$jobOutputPath/%s", $batchDir); 175 176 print "* Preparing exposure $expId as job '$job'...\n";177 178 # make directories179 205 mkdir($jobOutputPath, 0777); 180 206 mkdir($batchOutputPath, 0777); 181 207 208 print "* Preparing exposure $expId as job '$job'...\n"; 209 182 210 # run IppToPsps program 183 if (runIppToPsps($ db, $expId, $batchOutputPath, $batchType)) {211 if (runIppToPsps($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType)) { 184 212 185 213 # write batch manifest and tar and zip up … … 198 226 elsif ($anyBatches) { 199 227 228 $published = 0; 200 229 if (writeJobManifest($output, $job, $surveyType)) { 201 230 202 if (!$no_publish && !publishToDatastore($output, $job)) {203 231 if (!$no_publish && publishToDatastore($output, $job)) { 232 $published = 1; 204 233 } 205 234 } 206 235 207 # write to the log 208 print LOGFILE "$jobId $expId\n"; 236 updateDb($ippToPspsDb, $jobId, $expId, 1, $published); 209 237 210 238 $batchId = 0; … … 214 242 215 243 print "* No batches to publish\n"; 216 print LOGFILE "$jobId $expId ERROR\n";217 244 } 218 245 } 219 else { 220 print LOGFILE "$jobId $expId ERROR\n"; 221 } 222 223 if ($batchType eq "init" ) {print "* Wrote initialisation batch\n"; last;} 224 246 247 if ($initBatch) {print "* Wrote initialisation batch\n";} 225 248 226 249 print "*\n*******************************************************************************\n\n"; 227 250 } 228 251 229 $ db->disconnect();230 close (LOGFILE);252 $gpc1Db->disconnect(); 253 $ippToPspsDb->disconnect(); 231 254 232 255 return 1; … … 239 262 ######################################################################################## 240 263 sub getLastExpId { 241 242 my $lastExpId = `tail $logFile -n 1 | awk '{print \$2}'`; 243 chomp($lastExpId); 244 return $lastExpId; 245 } 246 247 ####################################################################################### 248 # 249 # Gets the current jobId (ini file for now - will be Db eventually TODO) 264 my ($db) = @_; 265 266 my $query = $db->prepare(<<SQL); 267 268 SELECT exp_id 269 FROM batches 270 WHERE processed = 1 271 ORDER BY exp_id 272 DESC LIMIT 1; 273 SQL 274 275 $query->execute; 276 277 my $expId = $query->fetchrow_array(); 278 279 print "* Last successfully processed exposure ID was $expId\n"; 280 281 return $expId; 282 } 283 284 ####################################################################################### 285 # 286 # Updates an existing database record 250 287 # 251 288 ######################################################################################## 252 sub getNewJobId { 253 254 my $jobId = -1; 255 256 open (MYFILE, $jobIdFilePath); 257 while (<MYFILE>) { 258 chomp; 259 $jobId = $_; 260 } 261 close (MYFILE); 262 263 $jobId++; 264 265 open (MYFILE, "> $jobIdFilePath"); 266 print MYFILE "$jobId"; 267 close (MYFILE); 268 269 return $jobId; 289 sub updateDb { 290 my ($db, $batchId, $expId, $processed, $published) = @_; 291 292 my $query = $db->prepare(<<SQL); 293 294 UPDATE batches 295 SET processed = $processed, on_datastore = $published 296 WHERE batch_id = $batchId 297 AND exp_id = $expId; 298 SQL 299 300 $query->execute; # TODO check response of these 301 } 302 303 ####################################################################################### 304 # 305 # Checks whether we have successfully processed this exposure 306 # 307 ######################################################################################## 308 sub isExposureAlreadyProcessed { 309 my ($db, $expId) = @_; 310 311 my $query = $db->prepare(<<SQL); 312 313 SELECT COUNT(*) 314 FROM batches 315 WHERE exp_id = $expId 316 AND processed = 1; 317 318 SQL 319 320 $query->execute; 321 322 my $processed = $query->fetchrow_array(); 323 324 if ($processed) {print "* Exposure ID '$expId' has already been processed\n";} 325 326 return $processed; 327 } 328 329 ####################################################################################### 330 # 331 # Generates a new record in the database with a new jobId 332 # 333 ######################################################################################## 334 sub getNewBatchId { 335 my ($db, $expId,$surveyType) = @_; 336 337 my $query = $db->prepare(<<SQL); 338 339 SELECT batch_id 340 FROM batches 341 ORDER BY batch_id 342 DESC LIMIT 1; 343 SQL 344 345 $query->execute; 346 347 my $batchId = $query->fetchrow_array(); 348 349 $batchId++; 350 351 $query = $db->prepare(<<SQL); 352 INSERT INTO batches 353 (batch_id, exp_id, survey_id) 354 VALUES 355 ($batchId, $expId, '$surveyType'); 356 357 SQL 358 359 $query->execute; 360 361 print "* New batch '$batchId' for exposure ID = $expId and survey = '$surveyType'\n"; 362 363 return $batchId; 270 364 } 271 365 … … 276 370 ####################################################################################### 277 371 sub runIppToPsps { 278 my ($ db, $expId, $batchOutputPath, $batchType) = @_;372 my ($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType) = @_; 279 373 280 374 my $success = 0; 281 375 282 # before anything else, we can publish the init batch283 376 if ($batchType =~ /init/) { 284 377 $success = produceInit($batchOutputPath); 285 378 } 286 379 elsif ($batchType =~ /det/) { 287 $success = produceDetections($ db, $expId, $batchOutputPath);380 $success = produceDetections($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath); 288 381 } 289 382 elsif ($batchType =~ /diff/) { 290 $success = produceDiffs($ db, $expId, $batchOutputPath);383 $success = produceDiffs($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath); 291 384 } 292 385 … … 532 625 my $fullJobPath = "$path/$job"; 533 626 534 my ($tempFile, $tempName) = tempfile( "/tmp/ dsregList.XXXX", UNLINK => !$save_temps);627 my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_dsregList.XXXX", UNLINK => !$save_temps); 535 628 536 629 # loop through all batch files in this job directory … … 582 675 my ($outputPath) = @_; 583 676 584 my $success = runProgram( "NULL", $outputPath, 0, $batchType);677 my $success = runProgram("NULL", $outputPath, 0, "NULL", "NULL", $batchType); 585 678 return $success; 586 679 } … … 591 684 ####################################################################################### 592 685 sub produceDetections { 593 my ($ db,$expId,$outputPath) = @_;686 my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_; 594 687 595 688 # query to retrieve nebulous key of camera smf file for this exposure … … 602 695 "JOIN chipRun USING(chip_id) ". 603 696 "JOIN rawExp USING (exp_id) ". 604 "WHERE rawExp.exp_id = $expId AND camRun.magicked"; 697 "WHERE rawExp.exp_id = $expId AND camRun.magicked ". 698 "ORDER BY camRun.cam_id DESC LIMIT 1"; # make sure we get most recently processed 605 699 606 700 if ($verbose) { print"* $query\n";} … … 608 702 609 703 # execute query and check results 610 my $results = $ db->selectall_arrayref( $query );704 my $results = $gpc1Db->selectall_arrayref( $query ); 611 705 my $size = @$results; 612 706 if ($size < 1) {print "* No smf files found for this exposure\n"; return 0;} … … 622 716 623 717 # now write the path to this file out to temp file 624 my ($tempFile, $tempName) = tempfile( "/tmp/i nputFileList.XXXX", UNLINK => !$save_temps);718 my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_inputFileList.XXXX", UNLINK => !$save_temps); 625 719 print $tempFile $realFile . "\n"; 626 720 close $tempFile; 627 721 628 $success = runProgram($tempName, $outputPath, $expId, $ batchType);722 $success = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType); 629 723 return $success; 630 724 } … … 637 731 ####################################################################################### 638 732 sub produceDiffs { 639 my ($ db,$expId,$outputPath) = @_;733 my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_; 640 734 641 735 my $query = … … 653 747 654 748 # execute query and check results 655 my $results = $ db->selectall_arrayref( $query );749 my $results = $gpc1Db->selectall_arrayref( $query ); 656 750 my $size = @$results; 657 751 if ($size < 1) {print "* No cmf files found for this exposure\n"; return 0;} … … 675 769 } 676 770 677 my $ret = runProgram($tempName, $outputPath, $expId, $ batchType);771 my $ret = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType); 678 772 679 773 close $tempFile; … … 688 782 ####################################################################################### 689 783 sub runProgram { 690 my ($input, $output, $expid, $ batchType) = @_;784 my ($input, $output, $expid, $expName, $surveyType, $batchType) = @_; 691 785 692 786 # build command … … 697 791 $command .= " -config config"; # TODO 698 792 $command .= " -expid $expid"; 793 $command .= " -expname $expName"; 794 $command .= " -survey $surveyType"; 699 795 $command .= " -batch $batchType"; 700 796 $command .= " -results $resultsFileName";
Note:
See TracChangeset
for help on using the changeset viewer.
