IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28120


Ignore:
Timestamp:
May 26, 2010, 3:53:19 PM (16 years ago)
Author:
rhenders
Message:

Now using a database for logging, rather than flat files; improved queries to get most recent smf files in the event that data has been re-processed

File:
1 edited

Legend:

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

    r28087 r28120  
    1212
    1313# 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";
     14my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $label, $singleExpId, $no_publish, $force);
    1915
    2016# default values for certain globals
     
    2824$singleExpId = undef;
    2925$datastoreProduct = "PSPS_test";
    30 
     26$force = 0;
    3127
    3228# get user args
     
    4339        'no-op' => \$no_op,
    4440        'no-update' => \$no_update,
     41        'force|f' => \$force,
    4542        ) or pod2usage( 2 );
    4643
     
    9794}
    9895
     96
    9997#######################################################################################
    10098#
    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########################################################################################
     102sub connectToDb {
     103    my ($dbname,$dbserver,$dbuser,$dbpass) = @_;
     104
     105
    115106    my $db = DBI->connect("DBI:mysql:database=${dbname};host=${dbserver};" .
    116107            "mysql_socket=" . DB_SOCKET(),
    117108            ${dbuser},${dbpass},
    118109            { 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#######################################################################################
     123sub 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);
    120130
    121131    my $query;
     
    123133
    124134        # query to retrieve all exposures with the provided label
    125         $query = $db->prepare(<<SQL);
     135        $query = $gpc1Db->prepare(<<SQL);
    126136
    127137        SELECT DISTINCT rawExp.exp_id, camRun.dist_group
     
    129139            WHERE camRun.chip_id = chipRun.chip_id
    130140            AND chipRun.exp_id = rawExp.exp_id
    131             AND camRun.label LIKE '%$label%'
     141            AND camRun.dist_group LIKE 'ThreePi'
    132142            AND rawExp.exp_id > $lastExpId
    133143            ORDER BY rawExp.exp_id ASC
    134144SQL
     145            #AND rawExp.exp_id > $lastExpId
     146            #AND camRun.label LIKE '%$label%'
    135147            #AND rawExp.exp_id > 136561
    136148            #AND camRun.dist_group LIKE 'sas'
     
    139151    else {
    140152
    141         $query = $db->prepare(<<SQL);
     153        $query = $gpc1Db->prepare(<<SQL);
    142154
    143155        SELECT DISTINCT rawExp.exp_id, dist_group FROM rawExp, chipRun WHERE chipRun.exp_id = rawExp.exp_id AND rawExp.exp_id = $singleExpId
     
    148160    my $batchesPerJob = 1;
    149161    my $anyBatches = 0;
     162    my $published = 0;
    150163
    151164    $query->execute;
    152 
    153     open (LOGFILE, ">> $logFile");
    154165
    155166    my $jobId = -1;
     
    159170        my ($expId, $distGroup) = @row;
    160171
    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                }
    164177
    165178        my $surveyType = getSurveyTypeFromDistGroup($distGroup);
    166 
    167179        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
    169184
    170185        # generate batch and job paths from job and batch IDs
     
    181196
    182197        # run IppToPsps program
    183         if (runIppToPsps($db, $expId, $batchOutputPath, $batchType)) {
     198        if (runIppToPsps($gpc1Db, $expId, $batchOutputPath, $batchType)) {
    184199
    185200            # write batch manifest and tar and zip up
     
    198213            elsif ($anyBatches) {
    199214
     215                $published = 0;
    200216                if (writeJobManifest($output, $job, $surveyType)) {
    201217
    202                     if (!$no_publish && !publishToDatastore($output, $job)) {
    203 
     218                    if (!$no_publish && publishToDatastore($output, $job)) {
     219                        $published = 1;
    204220                    }
    205221                }
    206222
    207                 # write to the log
    208                 print LOGFILE "$jobId $expId\n";
     223                updateDb($ippToPspsDb, $jobId, $expId, 1, $published);
    209224
    210225                $batchId = 0;
     
    214229
    215230                print "* No batches to publish\n";
    216                 print LOGFILE "$jobId $expId ERROR\n";
    217231            }
    218232        }
    219         else {
    220             print LOGFILE "$jobId $expId ERROR\n";
    221         }
    222233
    223234        if ($batchType eq "init" ) {print "* Wrote initialisation batch\n"; last;}
     
    227238    }
    228239
    229     $db->disconnect();
    230     close (LOGFILE);
     240    $gpc1Db->disconnect();
     241    $ippToPspsDb->disconnect();
    231242
    232243    return 1;
     
    239250########################################################################################
    240251sub 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;
     261SQL
     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
    250275#
    251276########################################################################################
    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;
     277sub 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;
     286SQL
     287
     288    $query->execute; # TODO check response of these
     289}
     290
     291#######################################################################################
     292#
     293# Checks whether we have successfully processed this exposure
     294#
     295########################################################################################
     296sub isExposureAlreadyProcessed {
     297    my ($db, $expId) = @_;
     298
     299    my $query = $db->prepare(<<SQL);
     300
     301SELECT COUNT(*)
     302    FROM batches
     303    WHERE exp_id = $expId
     304    AND processed = 1;
     305
     306SQL
     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########################################################################################
     322sub 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;
     331SQL
     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
     345SQL
     346
     347    $query->execute;
     348
     349    print "* New batch '$batchId' for exposure ID = $expId and survey = '$surveyType'\n";
     350
     351    return $batchId;
    270352}
    271353
     
    276358#######################################################################################
    277359sub runIppToPsps {
    278     my ($db, $expId, $batchOutputPath, $batchType) = @_;
     360    my ($gpc1Db, $expId, $batchOutputPath, $batchType) = @_;
    279361
    280362    my $success = 0;
     
    285367    }
    286368    elsif ($batchType =~ /det/) {
    287         $success = produceDetections($db, $expId, $batchOutputPath);
     369        $success = produceDetections($gpc1Db, $expId, $batchOutputPath);
    288370    }
    289371    elsif ($batchType =~ /diff/) {
    290         $success = produceDiffs($db, $expId, $batchOutputPath);
     372        $success = produceDiffs($gpc1Db, $expId, $batchOutputPath);
    291373    }
    292374
     
    591673#######################################################################################
    592674sub produceDetections {
    593     my ($db,$expId,$outputPath) = @_;
     675    my ($gpc1Db,$expId,$outputPath) = @_;
    594676
    595677    # query to retrieve nebulous key of camera smf file for this exposure
     
    602684        "JOIN chipRun USING(chip_id) ".
    603685        "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
    605688
    606689    if ($verbose) { print"* $query\n";}
     
    608691
    609692    # execute query and check results
    610     my $results = $db->selectall_arrayref( $query );
     693    my $results = $gpc1Db->selectall_arrayref( $query );
    611694    my $size = @$results;
    612695    if ($size < 1) {print "* No smf files found for this exposure\n"; return 0;}
     
    637720#######################################################################################
    638721sub produceDiffs {
    639     my ($db,$expId,$outputPath) = @_;
     722    my ($gpc1Db,$expId,$outputPath) = @_;
    640723
    641724    my $query =
     
    653736
    654737    # execute query and check results
    655     my $results = $db->selectall_arrayref( $query );
     738    my $results = $gpc1Db->selectall_arrayref( $query );
    656739    my $size = @$results;
    657740    if ($size < 1) {print "* No cmf files found for this exposure\n"; return 0;}
Note: See TracChangeset for help on using the changeset viewer.