Index: branches/pap/ippToPsps/scripts/ippToPsps_run.pl
===================================================================
--- branches/pap/ippToPsps/scripts/ippToPsps_run.pl	(revision 28179)
+++ branches/pap/ippToPsps/scripts/ippToPsps_run.pl	(revision 28484)
@@ -12,5 +12,5 @@
 
 # globals
-my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $label, $singleExpId, $no_publish, $force);
+my ($verbose, $save_temps, $no_op, $no_update, $camera, $batchType, $output, $dvodb, $datastoreProduct, $survey, $singleExpId, $no_publish, $force, $initBatch);
 
 # default values for certain globals
@@ -25,4 +25,5 @@
 $datastoreProduct = "PSPS_test";
 $force = 0;
+$initBatch = 0;
 
 # get user args
@@ -31,5 +32,5 @@
         'batch|b=s' => \$batchType,
         'dvodb|d=s' => \$dvodb,
-        'label|l=s' => \$label,
+        'survey|s=s' => \$survey,
         'expid|e=s' => \$singleExpId,
         'product|p=s' => \$datastoreProduct,
@@ -49,5 +50,5 @@
         "--batch <init|det|diff|stack>\n".
         "--output <path>\n".
-        "--label <label> or --expid <expid>\n" .
+        "--survey <ThreePi|MD01,2 etc|STS|SAS|SweetSpot> or --expid <expid>\n" .
         "--dvodb <path>\n".
         "\n   Optional:\n\n".
@@ -62,5 +63,7 @@
 defined $output and
 defined $dvodb and
-( defined $label or defined $singleExpId );
+(( defined $survey or defined $singleExpId ) or ( $batchType eq "init"));
+
+if ($batchType eq "init") {$initBatch = 1;}
 
 # check we can run programs and get camera config
@@ -70,5 +73,5 @@
 
 # make a temporary file for saving results
-my ($resultsFile, $resultsFileName) = tempfile( "/tmp/results.XXXX", UNLINK => !$save_temps);
+my ($resultsFile, $resultsFileName) = tempfile( "/tmp/ippToPsps_results.XXXX", UNLINK => !$save_temps);
 
 process();
@@ -86,5 +89,5 @@
     if ($distGroup =~ m/^MD([0-1][0-9])$/i) {return "MD$1";}
     if ($distGroup =~ m/^M31$/i) {return "M31";}
-    if ($distGroup =~ m/^sts$/i) {return "STS";}
+    if ($distGroup =~ m/^sts$/i) {return "STS1";}
     if ($distGroup =~ m/^SweetSpot$/i) {return "SSS";}
     if ($distGroup =~ m/^(3PI)|(ThreePi)|(SAS)$/i) {return "3PI";}
@@ -118,5 +121,5 @@
 #######################################################################################
 # 
-# Finds all exposures for the provided label and generates PSPS FITS data for them
+# Finds all exposures for the provided TODO and generates PSPS FITS data for them
 #
 #######################################################################################
@@ -130,17 +133,36 @@
 
     my $query;
-    if (!$singleExpId) {
-
-        # query to retrieve all exposures with the provided label
+    if ($initBatch) {
+    
         $query = $gpc1Db->prepare(<<SQL);
 
-        SELECT DISTINCT rawExp.exp_id, camRun.dist_group
-            FROM camRun, chipRun, rawExp
-            WHERE camRun.chip_id = chipRun.chip_id
-            AND chipRun.exp_id = rawExp.exp_id
-            AND camRun.dist_group LIKE 'ThreePi'
-            AND rawExp.exp_id > $lastExpId
-            ORDER BY rawExp.exp_id ASC
+        SELECT 0, 'NULL', 'ThreePi';
 SQL
+    }
+    elsif ($singleExpId) {
+
+        $query = $gpc1Db->prepare(<<SQL);
+
+        SELECT DISTINCT rawExp.exp_id,  rawExp.exp_name, dist_group 
+            FROM rawExp, chipRun 
+            WHERE chipRun.exp_id = rawExp.exp_id 
+            AND rawExp.exp_id = $singleExpId
+SQL
+    }
+    else {
+
+        $query = $gpc1Db->prepare(<<SQL);
+
+        SELECT rawExp.exp_id, rawExp.exp_name, camRun.dist_group 
+            FROM camRun, chipRun, rawExp 
+            WHERE camRun.state = 'full' 
+            AND camRun.chip_id = chipRun.chip_id 
+            AND chipRun.exp_id = rawExp.exp_id 
+            AND camRun.dist_group = '$survey'   
+            GROUP BY  rawExp.exp_id 
+            ORDER BY rawExp.exp_id ASC;
+SQL
+
+            #AND rawExp.dateobs <= '2010-03-12'
             #AND rawExp.exp_id > $lastExpId
             #AND camRun.label LIKE '%$label%'
@@ -149,11 +171,4 @@
             #AND rawExp.decl >= '-0.157079633' AND rawExp.decl <= '0.157079633' Jims Dec range
     }
-    else {
-
-        $query = $gpc1Db->prepare(<<SQL);
-
-        SELECT DISTINCT rawExp.exp_id, dist_group FROM rawExp, chipRun WHERE chipRun.exp_id = rawExp.exp_id AND rawExp.exp_id = $singleExpId
-SQL
-    }
 
     my $batchId = 0;
@@ -168,11 +183,11 @@
     # loop round exposures
     while (my @row = $query->fetchrow_array()) {
-        my ($expId, $distGroup) = @row;
-
-        if (isExposureAlreadyProcessed($ippToPspsDb, $expId)) {
+        my ($expId, $expName, $distGroup) = @row;
+
+        if (!$initBatch && isExposureAlreadyProcessed($ippToPspsDb, $expId)) {
                 
                 if ($force) {print "* Forcing....\n";} 
                 else {next};
-                }
+        }
 
         my $surveyType = getSurveyTypeFromDistGroup($distGroup);
@@ -181,5 +196,5 @@
         $jobId = getNewBatchId($ippToPspsDb, $expId, $surveyType);
 
-# TODO quit here if no sensible job ID
+        # TODO quit here if no sensible job ID
 
         # generate batch and job paths from job and batch IDs
@@ -188,13 +203,11 @@
         my $batchDir = sprintf("B%03d", $batchId);
         my $batchOutputPath = sprintf("$jobOutputPath/%s", $batchDir);
-
-        print "* Preparing exposure $expId as job '$job'...\n";
-
-        # make directories
         mkdir($jobOutputPath, 0777);
         mkdir($batchOutputPath, 0777);
 
+        print "* Preparing exposure $expId as job '$job'...\n";
+
         # run IppToPsps program
-        if (runIppToPsps($gpc1Db, $expId, $batchOutputPath, $batchType)) {
+        if (runIppToPsps($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType)) {
 
             # write batch manifest and tar and zip up
@@ -232,6 +245,5 @@
         }
 
-        if ($batchType eq "init" ) {print "* Wrote initialisation batch\n"; last;}
-
+        if ($initBatch) {print "* Wrote initialisation batch\n";}
 
         print "*\n*******************************************************************************\n\n";
@@ -358,17 +370,16 @@
 #######################################################################################
 sub runIppToPsps {
-    my ($gpc1Db, $expId, $batchOutputPath, $batchType) = @_;
+    my ($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath, $batchType) = @_;
 
     my $success = 0;
 
-    # before anything else, we can publish the init batch
     if ($batchType =~ /init/) {
         $success = produceInit($batchOutputPath);
     }
     elsif ($batchType =~ /det/) {
-        $success = produceDetections($gpc1Db, $expId, $batchOutputPath);
+        $success = produceDetections($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath);
     }
     elsif ($batchType =~ /diff/) {
-        $success = produceDiffs($gpc1Db, $expId, $batchOutputPath);
+        $success = produceDiffs($gpc1Db, $expId, $expName, $surveyType, $batchOutputPath);
     }
 
@@ -614,5 +625,5 @@
     my $fullJobPath = "$path/$job";
 
-    my ($tempFile, $tempName) = tempfile( "/tmp/dsregList.XXXX", UNLINK => !$save_temps);
+    my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_dsregList.XXXX", UNLINK => !$save_temps);
 
     # loop through all batch files in this job directory
@@ -664,5 +675,5 @@
     my ($outputPath) = @_;
 
-    my $success = runProgram( "NULL", $outputPath, 0, $batchType);
+    my $success = runProgram("NULL", $outputPath, 0, "NULL", "NULL", $batchType);
     return $success;
 }
@@ -673,5 +684,5 @@
 #######################################################################################
 sub produceDetections {
-    my ($gpc1Db,$expId,$outputPath) = @_;
+    my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_;
 
     # query to retrieve nebulous key of camera smf file for this exposure
@@ -705,9 +716,9 @@
 
         # now write the path to this file out to temp file
-        my ($tempFile, $tempName) = tempfile( "/tmp/inputFileList.XXXX", UNLINK => !$save_temps);
+        my ($tempFile, $tempName) = tempfile( "/tmp/ippToPsps_inputFileList.XXXX", UNLINK => !$save_temps);
         print $tempFile $realFile . "\n";
         close $tempFile;
 
-        $success = runProgram($tempName, $outputPath, $expId, $batchType);
+        $success = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType);
         return $success;
     }
@@ -720,5 +731,5 @@
 #######################################################################################
 sub produceDiffs {
-    my ($gpc1Db,$expId,$outputPath) = @_;
+    my ($gpc1Db, $expId, $expName, $surveyType, $outputPath) = @_;
 
     my $query = 
@@ -758,5 +769,5 @@
     }
 
-    my $ret = runProgram($tempName, $outputPath, $expId, $batchType);
+    my $ret = runProgram($tempName, $outputPath, $expId, $expName, $surveyType, $batchType);
 
     close $tempFile;
@@ -771,5 +782,5 @@
 #######################################################################################
 sub runProgram {
-    my ($input, $output, $expid, $batchType) = @_;
+    my ($input, $output, $expid, $expName, $surveyType, $batchType) = @_;
 
     # build command
@@ -780,4 +791,6 @@
     $command .= " -config config"; # TODO
     $command .= " -expid $expid";
+    $command .= " -expname $expName";
+    $command .= " -survey $surveyType";
     $command .= " -batch $batchType";
     $command .= " -results $resultsFileName";
