Index: trunk/ippScripts/scripts/ipp_serial_mops.pl
===================================================================
--- trunk/ippScripts/scripts/ipp_serial_mops.pl	(revision 19717)
+++ trunk/ippScripts/scripts/ipp_serial_mops.pl	(revision 24190)
@@ -71,12 +71,48 @@
     die "Unable to connect to database: $DBI::errstr";
 
-my $sql = "SELECT exp_id, diff_id, skycell_id, warp_id, diffSkyfile.path_base AS diff_path_base, warpSkyfile.path_base AS warp_path_base FROM diffSkyfile JOIN diffRun using(diff_id) JOIN diffInputSkyfile USING(diff_id,tess_id,skycell_id) JOIN warpRun USING(warp_id,tess_id) JOIN warpSkyfile USING(warp_id,skycell_id,tess_id) JOIN fakeRun using(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) WHERE diffSkyfile.fault = 0 AND rawExp.camera = \'$camera\'";
-$sql .= " AND diffRun.label = \'$label\'" if defined $label;
-$sql .= ';';
+my $where_label = defined $label ? "AND label = '$label'" : ""; # WHERE for label
 
-my $rows = $db->selectall_arrayref( $sql, { Slice => {} } ) or die "Unable to execute SQL: $DBI::errstr";
-$db->disconnect;
+my $sql = "
+-- Get a list of exposures on which magic may be performed
+SELECT
+    exp_id,
+    MAX(diffWarps.diff_id) AS diff_id,
+    -- The following trick pulls out the 'inverse' value for the maximum diff_id
+    CONVERT(SUBSTRING_INDEX(GROUP_CONCAT(diffWarps.inverse ORDER BY diffWarps.diff_id), ',', 1), UNSIGNED) AS inverse
+FROM (
+    -- Forward diffs
+    SELECT
+        diffRun.diff_id,
+        warp1 AS warp_id,
+        0 AS inverse
+    FROM diffRun
+    JOIN diffInputSkyfile USING(diff_id)
+    WHERE diffInputSkyfile.warp1 IS NOT NULL
+        AND diffRun.exposure = 1
+        $where_label
+    UNION
+    -- Backward diffs
+    SELECT
+        diffRun.diff_id,
+        warp2 AS warp_id,
+        1 AS inverse
+    FROM diffRun
+    JOIN diffInputSkyfile USING(diff_id)
+    WHERE diffInputSkyfile.warp2 IS NOT NULL
+        AND diffRun.exposure = 1
+        AND diffRun.bothways = 1
+        $where_label
+    ) AS diffWarps
+JOIN warpRun USING(warp_id)
+JOIN fakeRun USING(fake_id)
+JOIN camRun USING(cam_id)
+JOIN chipRun USING(chip_id)
+WHERE diffRun.state = 'full'
+    AND rawExp.camera = '$camera'
+GROUP BY exp_id;";
 
-print "Selected " . scalar @$rows . " rows.\n";
+my $diffs = $db->selectall_arrayref( $sql, { Slice => {} } ) or die "Unable to execute SQL: $DBI::errstr";
+
+print "Selected " . scalar @$diffs . " rows.\n";
 
 $ipprc->outroot_prepare( $outroot );
@@ -84,35 +120,39 @@
 my ($dsFile, $dsName) = tempfile( "$outrootResolved.dslist.XXXX", UNLINK => !$save_temps);
 
-foreach my $row ( @$rows ) {
-    my $exp_id = $row->{exp_id};
-    my $diff_id = $row->{diff_id};
-    my $skycell_id = $row->{skycell_id};
-    my $warp_id = $row->{warp_id};
-    my $diff_base = $row->{diff_path_base};
-    my $warp_base = $row->{warp_path_base};
+foreach my $diff ( @$diffs ) {
+    my $exp_id = $diff->{exp_id};
+    my $diff_id = $diff->{diff_id};
+    my $inverse = $diff->{inverse};
 
-    my $input = $ipprc->filename("PSPHOT.OUT.CMF.MEF", $diff_base);
-    die "Can't find $input\n" unless $ipprc->file_exists($input);
-    $input = $ipprc->file_resolve($input);
+    my $sql = "SELECT * FROM diffSkyfile WHERE diff_id = $diff_id;";
+    my $skycells = $db->selectall_arrayref( $sql, { Slice => {} } ) or die "Unable to execute SQL: $DBI::errstr";
 
-    my $skycell = $ipprc->filename("SKYCELL.TEMPLATE", $warp_base);
-    die "Can't find $skycell\n" unless $ipprc->file_exists($skycell);
-    $skycell = $ipprc->file_resolve($skycell);
+    foreach my $skycell ( @$skycells ) {
+        my $skycell_id = $diff->{skycell_id};
+        my $path_base = $diff->{path_base};
 
-    my $output = caturi( $outroot, "mops.$exp_id.$skycell_id.$diff_id.fits" );
-    $output = $ipprc->file_resolve($output);
+        my $sources = $inverse ? "PPSUB.INVERSE.SOURCES" : "PPSUB.OUTPUT.SOURCES";
 
-    unless ($no_op) {
-        $ipprc->file_prepare($output);
-        my $command = "$mops --input $input --extname SkyChip.psf --skycell $skycell --output $output";
-        my $success = run( command => $command, verbose => $verbose );
-        die "Couldn't translate $input\n" unless $success;
+        my $input = $ipprc->filename($sources, $path_base);
+        die "Can't find $input\n" unless $ipprc->file_exists($input);
+        $input = $ipprc->file_resolve($input);
+
+        my $output = caturi( $outroot, "mops.$exp_id.$skycell_id.$diff_id.fits" );
+        $output = $ipprc->file_resolve($output);
+
+        unless ($no_op) {
+            $ipprc->file_prepare($output);
+            my $command = "$mops --input $input --extname SkyChip.psf --output $output";
+            my $success = run( command => $command, verbose => $verbose );
+            die "Couldn't translate $input\n" unless $success;
+        }
+
+        # format: filename|filesize|md5sum|filetype|
+        # note: since we omit filesize and md5sum, dsreg will calculate them
+        print $dsFile "${output}|||ipp-mops|\n";
     }
-
-    # format: filename|filesize|md5sum|filetype|
-    # note: since we omit filesize and md5sum, dsreg will calculate them
-    print $dsFile "${output}|||ipp-mops|\n";
 }
 close $dsFile;
+$db->disconnect;
 
 # Register new files with the data store
