Changeset 28120
- Timestamp:
- May 26, 2010, 3:53:19 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/ippToPsps/scripts/ippToPsps_run.pl (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippToPsps/scripts/ippToPsps_run.pl
r28087 r28120 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, $label, $singleExpId, $no_publish, $force); 19 15 20 16 # default values for certain globals … … 28 24 $singleExpId = undef; 29 25 $datastoreProduct = "PSPS_test"; 30 26 $force = 0; 31 27 32 28 # get user args … … 43 39 'no-op' => \$no_op, 44 40 'no-update' => \$no_update, 41 'force|f' => \$force, 45 42 ) or pod2usage( 2 ); 46 43 … … 97 94 } 98 95 96 99 97 ####################################################################################### 100 98 # 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'; 99 # Connects to a db 100 # 101 ######################################################################################## 102 sub connectToDb { 103 my ($dbname,$dbserver,$dbuser,$dbpass) = @_; 104 105 115 106 my $db = DBI->connect("DBI:mysql:database=${dbname};host=${dbserver};" . 116 107 "mysql_socket=" . DB_SOCKET(), 117 108 ${dbuser},${dbpass}, 118 109 { RaiseError => 1, AutoCommit => 1} 119 ) or die "Unable to connect to database $DBI::errstr\n"; 110 ); 111 112 printf("* Connection to '$dbname' on '$dbserver': %s\n", $db ? "success" : "FAILED ($DBI::errstr)"); 113 114 return $db; 115 } 116 117 118 ####################################################################################### 119 # 120 # Finds all exposures for the provided label and generates PSPS FITS data for them 121 # 122 ####################################################################################### 123 sub process { 124 125 print "*******************************************************************************\n*\n"; 126 127 my $gpc1Db = connectToDb("gpc1", "ippdb01", "ippuser", "ippuser") or return 0; 128 my $ippToPspsDb = connectToDb("ippToPsps", "ippdb01", "ipp", "ipp") or return 0; 129 my $lastExpId = getLastExpId($ippToPspsDb); 120 130 121 131 my $query; … … 123 133 124 134 # query to retrieve all exposures with the provided label 125 $query = $ db->prepare(<<SQL);135 $query = $gpc1Db->prepare(<<SQL); 126 136 127 137 SELECT DISTINCT rawExp.exp_id, camRun.dist_group … … 129 139 WHERE camRun.chip_id = chipRun.chip_id 130 140 AND chipRun.exp_id = rawExp.exp_id 131 AND camRun. label LIKE '%$label%'141 AND camRun.dist_group LIKE 'ThreePi' 132 142 AND rawExp.exp_id > $lastExpId 133 143 ORDER BY rawExp.exp_id ASC 134 144 SQL 145 #AND rawExp.exp_id > $lastExpId 146 #AND camRun.label LIKE '%$label%' 135 147 #AND rawExp.exp_id > 136561 136 148 #AND camRun.dist_group LIKE 'sas' … … 139 151 else { 140 152 141 $query = $ db->prepare(<<SQL);153 $query = $gpc1Db->prepare(<<SQL); 142 154 143 155 SELECT DISTINCT rawExp.exp_id, dist_group FROM rawExp, chipRun WHERE chipRun.exp_id = rawExp.exp_id AND rawExp.exp_id = $singleExpId … … 148 160 my $batchesPerJob = 1; 149 161 my $anyBatches = 0; 162 my $published = 0; 150 163 151 164 $query->execute; 152 153 open (LOGFILE, ">> $logFile");154 165 155 166 my $jobId = -1; … … 159 170 my ($expId, $distGroup) = @row; 160 171 161 $jobId = getNewJobId(); 162 163 print "* Exposure ID = $expId with dist group = '$distGroup' as job ID $jobId\n"; 172 if (isExposureAlreadyProcessed($ippToPspsDb, $expId)) { 173 174 if ($force) {print "* Forcing....\n";} 175 else {next}; 176 } 164 177 165 178 my $surveyType = getSurveyTypeFromDistGroup($distGroup); 166 167 179 if (!$surveyType) {next;} 168 print "* Using survey type of $surveyType\n"; 180 181 $jobId = getNewBatchId($ippToPspsDb, $expId, $surveyType); 182 183 # TODO quit here if no sensible job ID 169 184 170 185 # generate batch and job paths from job and batch IDs … … 181 196 182 197 # run IppToPsps program 183 if (runIppToPsps($ db, $expId, $batchOutputPath, $batchType)) {198 if (runIppToPsps($gpc1Db, $expId, $batchOutputPath, $batchType)) { 184 199 185 200 # write batch manifest and tar and zip up … … 198 213 elsif ($anyBatches) { 199 214 215 $published = 0; 200 216 if (writeJobManifest($output, $job, $surveyType)) { 201 217 202 if (!$no_publish && !publishToDatastore($output, $job)) {203 218 if (!$no_publish && publishToDatastore($output, $job)) { 219 $published = 1; 204 220 } 205 221 } 206 222 207 # write to the log 208 print LOGFILE "$jobId $expId\n"; 223 updateDb($ippToPspsDb, $jobId, $expId, 1, $published); 209 224 210 225 $batchId = 0; … … 214 229 215 230 print "* No batches to publish\n"; 216 print LOGFILE "$jobId $expId ERROR\n";217 231 } 218 232 } 219 else {220 print LOGFILE "$jobId $expId ERROR\n";221 }222 233 223 234 if ($batchType eq "init" ) {print "* Wrote initialisation batch\n"; last;} … … 227 238 } 228 239 229 $ db->disconnect();230 close (LOGFILE);240 $gpc1Db->disconnect(); 241 $ippToPspsDb->disconnect(); 231 242 232 243 return 1; … … 239 250 ######################################################################################## 240 251 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) 252 my ($db) = @_; 253 254 my $query = $db->prepare(<<SQL); 255 256 SELECT exp_id 257 FROM batches 258 WHERE processed = 1 259 ORDER BY exp_id 260 DESC LIMIT 1; 261 SQL 262 263 $query->execute; 264 265 my $expId = $query->fetchrow_array(); 266 267 print "* Last successfully processed exposure ID was $expId\n"; 268 269 return $expId; 270 } 271 272 ####################################################################################### 273 # 274 # Updates an existing database record 250 275 # 251 276 ######################################################################################## 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; 277 sub updateDb { 278 my ($db, $batchId, $expId, $processed, $published) = @_; 279 280 my $query = $db->prepare(<<SQL); 281 282 UPDATE batches 283 SET processed = $processed, on_datastore = $published 284 WHERE batch_id = $batchId 285 AND exp_id = $expId; 286 SQL 287 288 $query->execute; # TODO check response of these 289 } 290 291 ####################################################################################### 292 # 293 # Checks whether we have successfully processed this exposure 294 # 295 ######################################################################################## 296 sub isExposureAlreadyProcessed { 297 my ($db, $expId) = @_; 298 299 my $query = $db->prepare(<<SQL); 300 301 SELECT COUNT(*) 302 FROM batches 303 WHERE exp_id = $expId 304 AND processed = 1; 305 306 SQL 307 308 $query->execute; 309 310 my $processed = $query->fetchrow_array(); 311 312 if ($processed) {print "* Exposure ID '$expId' has already been processed\n";} 313 314 return $processed; 315 } 316 317 ####################################################################################### 318 # 319 # Generates a new record in the database with a new jobId 320 # 321 ######################################################################################## 322 sub getNewBatchId { 323 my ($db, $expId,$surveyType) = @_; 324 325 my $query = $db->prepare(<<SQL); 326 327 SELECT batch_id 328 FROM batches 329 ORDER BY batch_id 330 DESC LIMIT 1; 331 SQL 332 333 $query->execute; 334 335 my $batchId = $query->fetchrow_array(); 336 337 $batchId++; 338 339 $query = $db->prepare(<<SQL); 340 INSERT INTO batches 341 (batch_id, exp_id, survey_id) 342 VALUES 343 ($batchId, $expId, '$surveyType'); 344 345 SQL 346 347 $query->execute; 348 349 print "* New batch '$batchId' for exposure ID = $expId and survey = '$surveyType'\n"; 350 351 return $batchId; 270 352 } 271 353 … … 276 358 ####################################################################################### 277 359 sub runIppToPsps { 278 my ($ db, $expId, $batchOutputPath, $batchType) = @_;360 my ($gpc1Db, $expId, $batchOutputPath, $batchType) = @_; 279 361 280 362 my $success = 0; … … 285 367 } 286 368 elsif ($batchType =~ /det/) { 287 $success = produceDetections($ db, $expId, $batchOutputPath);369 $success = produceDetections($gpc1Db, $expId, $batchOutputPath); 288 370 } 289 371 elsif ($batchType =~ /diff/) { 290 $success = produceDiffs($ db, $expId, $batchOutputPath);372 $success = produceDiffs($gpc1Db, $expId, $batchOutputPath); 291 373 } 292 374 … … 591 673 ####################################################################################### 592 674 sub produceDetections { 593 my ($ db,$expId,$outputPath) = @_;675 my ($gpc1Db,$expId,$outputPath) = @_; 594 676 595 677 # query to retrieve nebulous key of camera smf file for this exposure … … 602 684 "JOIN chipRun USING(chip_id) ". 603 685 "JOIN rawExp USING (exp_id) ". 604 "WHERE rawExp.exp_id = $expId AND camRun.magicked"; 686 "WHERE rawExp.exp_id = $expId AND camRun.magicked ". 687 "ORDER BY camRun.cam_id DESC LIMIT 1"; # make sure we get most recently processed 605 688 606 689 if ($verbose) { print"* $query\n";} … … 608 691 609 692 # execute query and check results 610 my $results = $ db->selectall_arrayref( $query );693 my $results = $gpc1Db->selectall_arrayref( $query ); 611 694 my $size = @$results; 612 695 if ($size < 1) {print "* No smf files found for this exposure\n"; return 0;} … … 637 720 ####################################################################################### 638 721 sub produceDiffs { 639 my ($ db,$expId,$outputPath) = @_;722 my ($gpc1Db,$expId,$outputPath) = @_; 640 723 641 724 my $query = … … 653 736 654 737 # execute query and check results 655 my $results = $ db->selectall_arrayref( $query );738 my $results = $gpc1Db->selectall_arrayref( $query ); 656 739 my $size = @$results; 657 740 if ($size < 1) {print "* No cmf files found for this exposure\n"; return 0;}
Note:
See TracChangeset
for help on using the changeset viewer.
