Index: trunk/ippToPsps/scripts/ippToPsps_run.pl
===================================================================
--- trunk/ippToPsps/scripts/ippToPsps_run.pl	(revision 27699)
+++ trunk/ippToPsps/scripts/ippToPsps_run.pl	(revision 27710)
@@ -71,4 +71,33 @@
 #######################################################################################
 # 
+# Generates a PSPS-suitable survey 'type' from the IPP distribution group
+#
+#######################################################################################
+sub getSurveyTypeFromDistGroup {
+    my ($distGroup) = @_;
+
+    if ($distGroup eq "MD01") {return $distGroup;}
+    if ($distGroup eq "MD02") {return $distGroup;}
+    if ($distGroup eq "MD03") {return $distGroup;}
+    if ($distGroup eq "MD04") {return $distGroup;}
+    if ($distGroup eq "MD05") {return $distGroup;}
+    if ($distGroup eq "MD06") {return $distGroup;}
+    if ($distGroup eq "MD07") {return $distGroup;}
+    if ($distGroup eq "MD08") {return $distGroup;}
+    if ($distGroup eq "MD09") {return $distGroup;}
+    if ($distGroup eq "MD10") {return $distGroup;}
+    if ($distGroup eq "M31") {return $distGroup;}
+    if ($distGroup eq "sts") {return "STS";}
+    if ($distGroup eq "SweetSpot") {return "SS";}
+    if ($distGroup eq "3PI") {return $distGroup;}
+    if ($distGroup eq "ThreePi") {return "3PI";}
+    if ($distGroup eq "SAS") {return "3PI";}
+
+    print "* Do not understand distribution group: '$distGroup'\n";
+    return undef;
+}
+
+#######################################################################################
+# 
 # Finds all exposures for the provided label and generates PSPS FITS data for them
 #
@@ -91,20 +120,23 @@
 
         # query to retrieve all exposures with the provided label
-        $query =
-            "SELECT DISTINCT rawExp.exp_id ".
-            "FROM camRun, chipRun, rawExp ".
-            "WHERE camRun.chip_id = chipRun.chip_id ".
-            "AND chipRun.exp_id = rawExp.exp_id ".
-            "AND camRun.label like '%$label%' ".
-            #    "AND rawExp.decl >= '-0.157079633' AND rawExp.decl <= '0.157079633' ". # TODO Jim's decl range
-            "ORDER BY rawExp.exp_id ASC";
+        $query = $db->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.label like '%$label%'
+            ORDER BY rawExp.exp_id ASC
+SQL
     }
     else {
 
-        $query = "SELECT DISTINCT exp_id FROM rawExp WHERE rawExp.exp_id = $singleExpId"
-
-    }
-
-    if ($verbose) { print"$query\n";}
+        $query = $db->prepare(<<SQL);
+
+        SELECT DISTINCT exp_id FROM rawExp WHERE rawExp.exp_id = $singleExpId
+SQL
+    }
+
+    if ($verbose) { print"* $query\n";}
 
     my $batchId = 0;
@@ -114,20 +146,25 @@
     my $jobId = getJobId();
     $jobId++;
-    if ($jobId < 0) { print "Cannot find sensible jobId ($jobId)\n";
+    if ($jobId < 0) { print "* Cannot find sensible jobId ($jobId)\n";
         return 0;
     }
 
-    print "------------------------------------------------------------------------------------------\n\n";
+    print "*******************************************************************************\n*\n";
 
     my $anyBatches = 0;
 
+
+    $query->execute;
+
     # loop round exposures
-    my $expResults = $db->selectall_arrayref( $query );
-
-    for my $expRow (@$expResults) {
-
-        my ($expId) = @$expRow;
-
-        print "      *** Exposure ID = $expId ***\n";
+    while (my @row = $query->fetchrow_array()) {
+        my ($expId, $distGroup) = @row;
+
+        print "* Exposure ID = $expId with dist group = '$distGroup'\n";
+
+        my $surveyType = getSurveyTypeFromDistGroup($distGroup);
+
+        if (!$surveyType) {next;}
+        print "* Using survey type of $surveyType\n";
 
         # generate batch and job paths from job and batch IDs
@@ -137,5 +174,5 @@
         my $batchOutputPath = sprintf("$jobOutputPath/%s", $batchDir);
 
-        print "Preparing job '$job'...\n";
+        print "* Preparing exposure as job '$job'...\n";
 
         # make directories
@@ -161,5 +198,5 @@
             elsif ($anyBatches) {
 
-                if (writeJobManifest($output, $job)) {
+                if (writeJobManifest($output, $job, $surveyType)) {
 
                     if (!$no_publish && !publishToDatastore($output, $job)) { 
@@ -175,12 +212,12 @@
             else {
 
-                print "No batches to publish\n"
+                print "* No batches to publish\n"
             }
         }
 
-        if ($product eq "init" ) {print "Wrote initialisation batch\n"; last;}
-
-
-        print "------------------------------------------------------------------------------------------\n\n";
+        if ($product eq "init" ) {print "* Wrote initialisation batch\n"; last;}
+
+
+        print "*\n*******************************************************************************\n\n";
     }
 
@@ -204,5 +241,5 @@
     close (MYFILE);
 
-    if ($verbose) { print "Current jobId = $jobId\n"; }
+    if ($verbose) { print "* Current jobId = $jobId\n"; }
 
     return $jobId;
@@ -221,5 +258,5 @@
     close (MYFILE);
 
-    if ($verbose) { print "New jobId = $jobId\n"; }
+    if ($verbose) { print "* New jobId = $jobId\n"; }
 }
 
@@ -265,5 +302,5 @@
         run(command => $command, verbose => $verbose);
 
-    if (!$success) { print "Unable to tar up dir: $batch\n" and return 0 };
+    if (!$success) { print "* Unable to tar up dir: $batch\n" and return 0 };
 
     # zip
@@ -272,5 +309,5 @@
         run(command => $command, verbose => $verbose);
 
-    if (!$success) { print "Unable to gzip: $batchTar\n" and return 0 };
+    if (!$success) { print "* Unable to gzip: $batchTar\n" and return 0 };
 
     $command = "rm $batchTar";
@@ -278,5 +315,5 @@
         run(command => $command, verbose => $verbose);
 
-    if (!$success) { print "Unable to remove: $batchTar\n" };
+    if (!$success) { print "* Unable to remove: $batchTar\n" };
 
     $command = "rm -r $batchDir";
@@ -284,5 +321,5 @@
         run(command => $command, verbose => $verbose);
 
-    if (!$success) { print "Unable to remove: $batch\n"};
+    if (!$success) { print "* Unable to remove: $batch\n"};
 
     return 1;
@@ -343,5 +380,5 @@
 
     # open directory to hunt for FITS files
-    opendir(DIR, $path) or print "Cannot open '$path' to write batch manifest\n" and return 0;
+    opendir(DIR, $path) or print "* Cannot open '$path' to write batch manifest\n" and return 0;
     my @thefiles= readdir(DIR);
     closedir(DIR);
@@ -353,5 +390,5 @@
         if ($f =~ /\.FITS/) {
 
-            if ($foundFits) { print "More than one FITS file found for this batch\n"; return 0;}
+            if ($foundFits) { print "* More than one FITS file found for this batch\n"; return 0;}
 
             # get md5sum of file
@@ -404,5 +441,5 @@
 
 
-    if (!$foundFits) { print "Could not find any FITS files for this batch\n"; return 0;}
+    if (!$foundFits) { print "* Could not find any FITS files for this batch\n"; return 0;}
     return 1;
 }
@@ -414,5 +451,5 @@
 #######################################################################################
 sub writeJobManifest {
-    my ($path, $job) = @_;
+    my ($path, $job, $surveyType) = @_;
 
     use XML::Writer;
@@ -433,5 +470,5 @@
     $writer->startTag('manifest',
             "name" => "$job",
-            "type" => "3PI", # TODO survey ID
+            "type" => $surveyType,
             "timestamp" => "$timeStamp");
 
@@ -439,5 +476,5 @@
 
     # open directory to hunt for FITS files
-    opendir(DIR, $jobPath) or print "Cannot open '$path' to write job manifest\n" and return 0;
+    opendir(DIR, $jobPath) or print "* Cannot open '$path' to write job manifest\n" and return 0;
     my @thefiles= readdir(DIR);
     closedir(DIR);
@@ -472,5 +509,5 @@
     $writer->end();
 
-    if (!$foundBatch) { print "Could not find any batches for this job\n"; return 0;}
+    if (!$foundBatch) { print "* Could not find any batches for this job\n"; return 0;}
     return 1;
 }
@@ -489,5 +526,5 @@
 
     # loop through all batch files in this job directory
-    opendir(DIR, $fullJobPath) or print "Cannot open '$fullJobPath' in order to publish to datastore\n" and return 0;
+    opendir(DIR, $fullJobPath) or print "* Cannot open '$fullJobPath' in order to publish to datastore\n" and return 0;
     my @thefiles= readdir(DIR);
     closedir(DIR);
@@ -519,7 +556,7 @@
         run(command => $command, verbose => $verbose);
 
-    if (!$success) { print "Unable to publish $job to datastore\n" and return 0 };
-
-    print "Successfully published $job to datastore\n";
+    if (!$success) { print "* Unable to publish $job to datastore\n" and return 0 };
+
+    print "* Successfully published $job to datastore\n";
 
     close($tempFile);
@@ -558,5 +595,5 @@
         "WHERE rawExp.exp_id = $expId AND camRun.magicked";
 
-    if ($verbose) { print"$query\n";}
+    if ($verbose) { print"* $query\n";}
 
 
@@ -564,5 +601,5 @@
     my $results = $db->selectall_arrayref( $query );
     my $size = @$results;
-    if ($size < 1) {print "No smf files found for this exposure\n"; return 0;}
+    if ($size < 1) {print "* No smf files found for this exposure\n"; return 0;}
 
     # loop round db results TODO should only be one result
@@ -604,10 +641,10 @@
         "WHERE rawExp.exp_id = $expId AND camRun.magicked";
 
-    if ($verbose) { print"$query\n";}
+    if ($verbose) { print"* $query\n";}
 
     # execute query and check results
     my $results = $db->selectall_arrayref( $query );
     my $size = @$results;
-    if ($size < 1) {print "No cmf files found for this exposure\n"; return 0;}
+    if ($size < 1) {print "* No cmf files found for this exposure\n"; return 0;}
 
     my ($tempFile, $tempName) = tempfile( "inputFileList.XXXX", UNLINK => !$save_temps);
