IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 10, 2010, 6:28:51 PM (16 years ago)
Author:
watersc1
Message:

Skycell Summary and stack Association stuff should be finished. I'll merge and do final tests on monday.

Location:
branches/czw_branch/20100519
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20100519

  • branches/czw_branch/20100519/ippToPsps/scripts/ippToPsps_run.pl

    r28164 r28304  
    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, $survey, $singleExpId, $no_publish, $force, $initBatch);
    1915
    2016# default values for certain globals
     
    2824$singleExpId = undef;
    2925$datastoreProduct = "PSPS_test";
    30 
     26$force = 0;
     27$initBatch = 0;
    3128
    3229# get user args
     
    3532        'batch|b=s' => \$batchType,
    3633        'dvodb|d=s' => \$dvodb,
    37         'label|l=s' => \$label,
     34        'survey|s=s' => \$survey,
    3835        'expid|e=s' => \$singleExpId,
    3936        'product|p=s' => \$datastoreProduct,
     
    4340        'no-op' => \$no_op,
    4441        'no-update' => \$no_update,
     42        'force|f' => \$force,
    4543        ) or pod2usage( 2 );
    4644
     
    5250        "--batch <init|det|diff|stack>\n".
    5351        "--output <path>\n".
    54         "--label <label> or --expid <expid>\n" .
     52        "--survey <ThreePi|MD01,2 etc|STS|SAS|SweetSpot> or --expid <expid>\n" .
    5553        "--dvodb <path>\n".
    5654        "\n   Optional:\n\n".
     
    6563defined $output and
    6664defined $dvodb and
    67 ( defined $label or defined $singleExpId );
     65(( defined $survey or defined $singleExpId ) or ( $batchType eq "init"));
     66
     67if ($batchType eq "init") {$initBatch = 1;}
    6868
    6969# check we can run programs and get camera config
     
    7373
    7474# make a temporary file for saving results
    75 my ($resultsFile, $resultsFileName) = tempfile( "/tmp/results.XXXX", UNLINK => !$save_temps);
     75my ($resultsFile, $resultsFileName) = tempfile( "/tmp/ippToPsps_results.XXXX", UNLINK => !$save_temps);
    7676
    7777process();
     
    8989    if ($distGroup =~ m/^MD([0-1][0-9])$/i) {return "MD$1";}
    9090    if ($distGroup =~ m/^M31$/i) {return "M31";}
    91     if ($distGroup =~ m/^sts$/i) {return "STS";}
     91    if ($distGroup =~ m/^sts$/i) {return "STS1";}
    9292    if ($distGroup =~ m/^SweetSpot$/i) {return "SSS";}
    9393    if ($distGroup =~ m/^(3PI)|(ThreePi)|(SAS)$/i) {return "3PI";}
     
    9797}
    9898
     99
    99100#######################################################################################
    100101#
    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########################################################################################
     105sub connectToDb {
     106    my ($dbname,$dbserver,$dbuser,$dbpass) = @_;
     107
     108
    115109    my $db = DBI->connect("DBI:mysql:database=${dbname};host=${dbserver};" .
    116110            "mysql_socket=" . DB_SOCKET(),
    117111            ${dbuser},${dbpass},
    118112            { 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#######################################################################################
     126sub 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);
    120133
    121134    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';
    134140SQL
     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
     150SQL
     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;
     164SQL
     165
     166            #AND rawExp.dateobs <= '2010-03-12'
     167            #AND rawExp.exp_id > $lastExpId
     168            #AND camRun.label LIKE '%$label%'
    135169            #AND rawExp.exp_id > 136561
    136170            #AND camRun.dist_group LIKE 'sas'
    137171            #AND rawExp.decl >= '-0.157079633' AND rawExp.decl <= '0.157079633' Jims Dec range
    138172    }
    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 = $singleExpId
    144 SQL
    145     }
    146173
    147174    my $batchId = 0;
    148175    my $batchesPerJob = 1;
    149176    my $anyBatches = 0;
     177    my $published = 0;
    150178
    151179    $query->execute;
    152 
    153     open (LOGFILE, ">> $logFile");
    154180
    155181    my $jobId = -1;
     
    157183    # loop round exposures
    158184    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        }
    164192
    165193        my $surveyType = getSurveyTypeFromDistGroup($distGroup);
    166 
    167194        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
    169199
    170200        # generate batch and job paths from job and batch IDs
     
    173203        my $batchDir = sprintf("B%03d", $batchId);
    174204        my $batchOutputPath = sprintf("$jobOutputPath/%s", $batchDir);
    175 
    176         print "* Preparing exposure $expId as job '$job'...\n";
    177 
    178         # make directories
    179205        mkdir($jobOutputPath, 0777);
    180206        mkdir($batchOutputPath, 0777);
    181207
     208        print "* Preparing exposure $expId as job '$job'...\n";
     209
    182210        # run IppToPsps program
    183         if (runIppToPsps($db, $expId, $batchOutputPath, $batchType)) {
     211        if (runIppToPsps($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType)) {
    184212
    185213            # write batch manifest and tar and zip up
     
    198226            elsif ($anyBatches) {
    199227
     228                $published = 0;
    200229                if (writeJobManifest($output, $job, $surveyType)) {
    201230
    202                     if (!$no_publish && !publishToDatastore($output, $job)) {
    203 
     231                    if (!$no_publish && publishToDatastore($output, $job)) {
     232                        $published = 1;
    204233                    }
    205234                }
    206235
    207                 # write to the log
    208                 print LOGFILE "$jobId $expId\n";
     236                updateDb($ippToPspsDb, $jobId, $expId, 1, $published);
    209237
    210238                $batchId = 0;
     
    214242
    215243                print "* No batches to publish\n";
    216                 print LOGFILE "$jobId $expId ERROR\n";
    217244            }
    218245        }
    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";}
    225248
    226249        print "*\n*******************************************************************************\n\n";
    227250    }
    228251
    229     $db->disconnect();
    230     close (LOGFILE);
     252    $gpc1Db->disconnect();
     253    $ippToPspsDb->disconnect();
    231254
    232255    return 1;
     
    239262########################################################################################
    240263sub 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;
     273SQL
     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
    250287#
    251288########################################################################################
    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;
     289sub 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;
     298SQL
     299
     300    $query->execute; # TODO check response of these
     301}
     302
     303#######################################################################################
     304#
     305# Checks whether we have successfully processed this exposure
     306#
     307########################################################################################
     308sub isExposureAlreadyProcessed {
     309    my ($db, $expId) = @_;
     310
     311    my $query = $db->prepare(<<SQL);
     312
     313SELECT COUNT(*)
     314    FROM batches
     315    WHERE exp_id = $expId
     316    AND processed = 1;
     317
     318SQL
     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########################################################################################
     334sub 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;
     343SQL
     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
     357SQL
     358
     359    $query->execute;
     360
     361    print "* New batch '$batchId' for exposure ID = $expId and survey = '$surveyType'\n";
     362
     363    return $batchId;
    270364}
    271365
     
    276370#######################################################################################
    277371sub runIppToPsps {
    278     my ($db, $expId, $batchOutputPath, $batchType) = @_;
     372    my ($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType) = @_;
    279373
    280374    my $success = 0;
    281375
    282     # before anything else, we can publish the init batch
    283376    if ($batchType =~ /init/) {
    284377        $success = produceInit($batchOutputPath);
    285378    }
    286379    elsif ($batchType =~ /det/) {
    287         $success = produceDetections($db, $expId, $batchOutputPath);
     380        $success = produceDetections($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath);
    288381    }
    289382    elsif ($batchType =~ /diff/) {
    290         $success = produceDiffs($db, $expId, $batchOutputPath);
     383        $success = produceDiffs($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath);
    291384    }
    292385
     
    532625    my $fullJobPath = "$path/$job";
    533626
    534     my ($tempFile, $tempName) = tempfile( "/tmp/dsregList.XXXX", UNLINK => !$save_temps);
     627    my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_dsregList.XXXX", UNLINK => !$save_temps);
    535628
    536629    # loop through all batch files in this job directory
     
    582675    my ($outputPath) = @_;
    583676
    584     my $success = runProgram( "NULL", $outputPath, 0, $batchType);
     677    my $success = runProgram("NULL", $outputPath, 0, "NULL", "NULL", $batchType);
    585678    return $success;
    586679}
     
    591684#######################################################################################
    592685sub produceDetections {
    593     my ($db,$expId,$outputPath) = @_;
     686    my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_;
    594687
    595688    # query to retrieve nebulous key of camera smf file for this exposure
     
    602695        "JOIN chipRun USING(chip_id) ".
    603696        "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
    605699
    606700    if ($verbose) { print"* $query\n";}
     
    608702
    609703    # execute query and check results
    610     my $results = $db->selectall_arrayref( $query );
     704    my $results = $gpc1Db->selectall_arrayref( $query );
    611705    my $size = @$results;
    612706    if ($size < 1) {print "* No smf files found for this exposure\n"; return 0;}
     
    622716
    623717        # now write the path to this file out to temp file
    624         my ($tempFile, $tempName) = tempfile( "/tmp/inputFileList.XXXX", UNLINK => !$save_temps);
     718        my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_inputFileList.XXXX", UNLINK => !$save_temps);
    625719        print $tempFile $realFile . "\n";
    626720        close $tempFile;
    627721
    628         $success = runProgram($tempName, $outputPath, $expId, $batchType);
     722        $success = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType);
    629723        return $success;
    630724    }
     
    637731#######################################################################################
    638732sub produceDiffs {
    639     my ($db,$expId,$outputPath) = @_;
     733    my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_;
    640734
    641735    my $query =
     
    653747
    654748    # execute query and check results
    655     my $results = $db->selectall_arrayref( $query );
     749    my $results = $gpc1Db->selectall_arrayref( $query );
    656750    my $size = @$results;
    657751    if ($size < 1) {print "* No cmf files found for this exposure\n"; return 0;}
     
    675769    }
    676770
    677     my $ret = runProgram($tempName, $outputPath, $expId, $batchType);
     771    my $ret = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType);
    678772
    679773    close $tempFile;
     
    688782#######################################################################################
    689783sub runProgram {
    690     my ($input, $output, $expid, $batchType) = @_;
     784    my ($input, $output, $expid, $expName, $surveyType, $batchType) = @_;
    691785
    692786    # build command
     
    697791    $command .= " -config config"; # TODO
    698792    $command .= " -expid $expid";
     793    $command .= " -expname $expName";
     794    $command .= " -survey $surveyType";
    699795    $command .= " -batch $batchType";
    700796    $command .= " -results $resultsFileName";
Note: See TracChangeset for help on using the changeset viewer.