Index: trunk/ippToPsps/jython/stackbatch.py
===================================================================
--- trunk/ippToPsps/jython/stackbatch.py	(revision 38938)
+++ trunk/ippToPsps/jython/stackbatch.py	(revision 38939)
@@ -382,5 +382,5 @@
           
             sqlLine.group("a."+filter+"infoFlag3",     "b.flags")
-            sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND b.imageID = " + str(imageID) + " and a." + filter + "ippDetectID=b.ippDetectID")
+            sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
 
             try: self.scratchDb.execute(sql)
@@ -445,4 +445,7 @@
                 self.logger.infoPair("no stack data for filter" , filter) 
                 continue
+
+            imageID = self.imageIDs[filter]
+            self.logger.infoPair("selecting imageID", imageID)
 
             header = self.headerSet[filter]
@@ -511,5 +514,6 @@
             # sqlLine.group("a."+filter+"airmass",       "b.airmass")
 
-            sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID")
+            # match to the detections from the specific image
+            sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
 
             try:
@@ -580,6 +584,4 @@
         self.deleteRowsWithNoStackData(tablename)
         self.tablesToExport.append(tablename)
-
-
 
         # gippdetectic
@@ -593,11 +595,4 @@
         # ghalfLightRad
 
-
-
-
-
-
-
-
         # currently, none of the fields defined by the table are generated
         
@@ -647,4 +642,7 @@
         header  = self.headerSet[filter]
 
+        imageID = self.imageIDs[filter]
+        self.logger.infoPair("selecting imageID", imageID)
+
         # model calibrated magnitude = instrumental magnitude + 2.5*log10(exptime) + ZP (added below from dvoDetectionTable)
         magtime = "%.5f" % (2.5*math.log10(float(header['EXPTIME'])))
@@ -704,5 +702,6 @@
         sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + self.scratchDb.dvoDetectionTable + " as b SET")
         sqlLine.group("a." + filter + model + "Mag",       "a." + filter + model + "Mag + b.zp")
-        sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID" + " AND b.imageID = " + str(self.imageIDs[filter]))
+        sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
+
         try:
             self.scratchDb.execute(sql)
@@ -815,4 +814,7 @@
         header  = self.headerSet[filter]
 
+        imageID = self.imageIDs[filter]
+        self.logger.infoPair("selecting imageID", imageID)
+
         # PETRO_MAG is instrumental + 2.5log(exptime) + FPA.ZP
         # we want to apply the DVO zero point, so remove FPA.ZP:
@@ -847,5 +849,5 @@
         sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + self.scratchDb.dvoDetectionTable + " as b SET")
         sqlLine.group("a." + filter + "petMag",       "a." + filter + "petMag + b.zp")
-        sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID")
+        sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
 
         try: self.scratchDb.execute(sql)
@@ -933,4 +935,7 @@
             self.logger.infoPair("no stack data for filter" , filter) 
             return True
+
+        imageID = self.imageIDs[filter]
+        self.logger.infoPair("selecting imageID", imageID)
 
         minRadius = 1
@@ -1032,5 +1037,7 @@
             sqlLine.group(field + "Err",       field + "err" + " * b.zpFactor")
             sqlLine.group(field + "Std",       field + "Std" + " * b.zpFactor")
-        sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(self.imageIDs[filter]) )
+
+        sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
+
         try: self.scratchDb.execute(sql)
         except:
@@ -1222,5 +1229,5 @@
             raise
 
- # ok, now fill g/r/i/z/yippDetectID using objID and detectID and imge ID
+ # ok, now fill g/r/i/z/yippDetectID using objID and detectID and image ID
 
         for filter in self.filters:
Index: trunk/ippToPsps/jython/testCode.py
===================================================================
--- trunk/ippToPsps/jython/testCode.py	(revision 38938)
+++ trunk/ippToPsps/jython/testCode.py	(revision 38939)
@@ -20,4 +20,6 @@
     def run(self):
 
+        # tables = stilts.treads(fileName)
+
         for x in range(0, 8):
             for y in range(0, 8):
Index: trunk/ippToPsps/test/mkgpc1data.dvo
===================================================================
--- trunk/ippToPsps/test/mkgpc1data.dvo	(revision 38938)
+++ trunk/ippToPsps/test/mkgpc1data.dvo	(revision 38939)
@@ -19,31 +19,46 @@
 # imagedata describes the full fake exposure set.  the fields are
 list imagedata
-# exp_id stk_id filebase         M_off date       time      warpfile
-   1 	 1      $OUTDIR/test.01  0.000 2010/01/01 01:00:00  $OUTDIR/warp.01 
-   2 	 1      $OUTDIR/test.02 -0.025 2010/01/01 02:00:00  $OUTDIR/warp.02 
-   3 	 1      $OUTDIR/test.03  0.025 2010/01/01 03:00:00  $OUTDIR/warp.03 
-   4 	 1      $OUTDIR/test.04  0.010 2010/01/01 04:00:00  $OUTDIR/warp.04 
-   5 	 2      $OUTDIR/test.05  0.000 2010/01/01 05:00:00  $OUTDIR/warp.05 
-   6 	 2      $OUTDIR/test.06 -0.025 2010/01/01 06:00:00  $OUTDIR/warp.06 
-   7 	 2      $OUTDIR/test.07  0.025 2010/01/01 07:00:00  $OUTDIR/warp.07 
-   8 	 2      $OUTDIR/test.08  0.010 2010/01/01 08:00:00  $OUTDIR/warp.08 
-   9 	 3      $OUTDIR/test.09  0.000 2010/01/01 09:00:00  $OUTDIR/warp.09 
-  10 	 3      $OUTDIR/test.10 -0.025 2010/01/01 10:00:00  $OUTDIR/warp.10 
-  11 	 3      $OUTDIR/test.11  0.025 2010/01/01 11:00:00  $OUTDIR/warp.11 
-  12 	 3      $OUTDIR/test.12  0.010 2010/01/01 12:00:00  $OUTDIR/warp.12 
+# exp_id stk_id filebase         M_off date       time      warpfile         filter
+   1 	 1      $OUTDIR/test.01  0.000 2010/01/01 01:00:00  $OUTDIR/warp.01  g
+   2 	 1      $OUTDIR/test.02 -0.025 2010/01/01 02:00:00  $OUTDIR/warp.02  g
+   3 	 1      $OUTDIR/test.03  0.025 2010/01/01 03:00:00  $OUTDIR/warp.03  g
+   4 	 1      $OUTDIR/test.04  0.010 2010/01/01 04:00:00  $OUTDIR/warp.04  g
+#  5 	 2      $OUTDIR/test.05  0.000 2010/01/01 05:00:00  $OUTDIR/warp.05  g
+#  6 	 2      $OUTDIR/test.06 -0.025 2010/01/01 06:00:00  $OUTDIR/warp.06  g
+#  7 	 2      $OUTDIR/test.07  0.025 2010/01/01 07:00:00  $OUTDIR/warp.07  g
+#  8 	 2      $OUTDIR/test.08  0.010 2010/01/01 08:00:00  $OUTDIR/warp.08  g
+#  9 	 3      $OUTDIR/test.09  0.000 2010/01/01 09:00:00  $OUTDIR/warp.09  g
+# 10 	 3      $OUTDIR/test.10 -0.025 2010/01/01 10:00:00  $OUTDIR/warp.10  g
+# 11 	 3      $OUTDIR/test.11  0.025 2010/01/01 11:00:00  $OUTDIR/warp.11  g
+# 12 	 3      $OUTDIR/test.12  0.010 2010/01/01 12:00:00  $OUTDIR/warp.12  g
+  13 	 4      $OUTDIR/test.13 -0.025 2010/01/02 01:00:00  $OUTDIR/warp.13  r
+  14 	 4      $OUTDIR/test.14  0.025 2010/01/02 02:00:00  $OUTDIR/warp.14  r
+  15 	 4      $OUTDIR/test.15  0.010 2010/01/02 03:00:00  $OUTDIR/warp.15  r
+  16 	 4      $OUTDIR/test.16  0.000 2010/01/02 04:00:00  $OUTDIR/warp.16  r
+# 17 	 5      $OUTDIR/test.17 -0.025 2010/01/02 05:00:00  $OUTDIR/warp.17  r
+# 18 	 5      $OUTDIR/test.18  0.025 2010/01/02 06:00:00  $OUTDIR/warp.18  r
+# 19 	 5      $OUTDIR/test.19  0.010 2010/01/02 07:00:00  $OUTDIR/warp.19  r
+# 20 	 5      $OUTDIR/test.20  0.000 2010/01/02 08:00:00  $OUTDIR/warp.20  r
+# 21 	 6      $OUTDIR/test.21 -0.025 2010/01/02 09:00:00  $OUTDIR/warp.21  r
+# 22 	 6      $OUTDIR/test.22  0.025 2010/01/02 10:00:00  $OUTDIR/warp.22  r
+# 23 	 6      $OUTDIR/test.23  0.010 2010/01/02 11:00:00  $OUTDIR/warp.23  r
+# 24 	 6      $OUTDIR/test.24 -0.020 2010/01/02 12:00:00  $OUTDIR/warp.24  r
 end
 
 list stackdata
-# stk_id filename         M_off
-  1      $OUTDIR/stack.01 0.010
-  2      $OUTDIR/stack.02 0.020
-  3      $OUTDIR/stack.03 0.030
+# stk_id sky_id filename         M_off filter
+  1      1 	$OUTDIR/stack.01 0.010 g
+# 2      2 	$OUTDIR/stack.02 0.020 g
+# 3      3 	$OUTDIR/stack.03 0.030 g
+  4      1 	$OUTDIR/stack.04 0.025 r
+# 5      2 	$OUTDIR/stack.05 0.035 r
+# 6      3 	$OUTDIR/stack.06 0.045 r
 end
  
 macro mkfull
   init.db
-  mkcatdir.cam PS1_V5 PS1_V5
+# mkcatdir.cam PS1_V5 PS1_V5
   mkcatdir.stk PS1_V5 PS1_V5
-  mkcatdir.wrp PS1_V5_Lensing PS1_V5
+# mkcatdir.wrp PS1_V5_Lensing PS1_V5
   insert.stack.set
 end
@@ -96,7 +111,8 @@
     $cmffile = $word:2\.smf
     $offset = $word:3
-
     sprintf myDATE "%s" $word:4
     sprintf myTIME "%s" $word:5
+    $filter = $word:7
+
     echo ctimes -abs $myDATE,$myTIME -var mjd
     ctimes -abs $myDATE,$myTIME -var mjd
@@ -114,5 +130,5 @@
     mcreate dummy 0 0
     keyword dummy MJD-OBS  -wf $mjd
-    keyword dummy FILTERID -w  r.00000
+    keyword dummy FILTERID -w  $filter.00000
     keyword dummy EXPTIME  -wf 1.0
 
@@ -152,6 +168,6 @@
 
     mkinput $offset $rawfile
-    echo mkcmf -photcode SIMTEST.r.Chip -no-noise -append $rawfile $cmffile -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 0
-    exec mkcmf -photcode SIMTEST.r.Chip -no-noise -append $rawfile $cmffile -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 0
+    echo mkcmf -photcode SIMTEST.$filter.Chip -no-noise -append $rawfile $cmffile -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 0
+    exec mkcmf -photcode SIMTEST.$filter.Chip -no-noise -append $rawfile $cmffile -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 0
 
     if ($i == 3)
@@ -210,5 +226,5 @@
   mkdir $OUTDIR
 
-  local i catdir RA DEC ID rawfile cmffile offset
+  local i catdir RA DEC stkID skyID rawfile cmffile offset
   $catdir = $OUTDIR/catdir.stk
   exec rm -rf $catdir
@@ -219,25 +235,30 @@
   for i 0 $stackdata:n
     list word -split $stackdata:$i
-    $ID = $word:0
-    $rawfile = $word:1\.txt
-    $cmffile = $word:1\.cmf
-    $offset = $word:2
+    $stkID = $word:0
+    $skyID = $word:1
+    $rawfile = $word:2\.txt
+    $cmffile = $word:2\.cmf
+    $offset = $word:3
+    $filter = $word:4
 
     ## this is not generating photcodes recognized as stack by relphot
 
     mkinput $offset $rawfile
-    echo mkcmf -stack -photcode SIMTEST.r.SkyChip $rawfile $cmffile -extroot SkyChip -date 2008/1/1 -time $i\:00:00 -mjd 54466.0 -radec $RA $DEC -type $1 -imageID $ID -sourceID 1 -tess_id $TESS_ID -skycell $SKYCELL
-    exec mkcmf -stack -photcode SIMTEST.r.SkyChip $rawfile $cmffile -extroot SkyChip -date 2008/1/1 -time $i\:00:00 -mjd 54466.0 -radec $RA $DEC -type $1 -imageID $ID -sourceID 1 -tess_id $TESS_ID -skycell $SKYCELL
+    echo mkcmf -stack -photcode SIMTEST.$filter.SkyChip $rawfile $cmffile -extroot SkyChip -date 2008/1/1 -time $i\:00:00 -mjd 54466.0 -radec $RA $DEC -type $1 -imageID $stkID -sourceID 1 -tess_id $TESS_ID -skycell $SKYCELL
+    exec mkcmf -stack -photcode SIMTEST.$filter.SkyChip $rawfile $cmffile -extroot SkyChip -date 2008/1/1 -time $i\:00:00 -mjd 54466.0 -radec $RA $DEC -type $1 -imageID $stkID -sourceID 1 -tess_id $TESS_ID -skycell $SKYCELL
 
     # add some required header keywords:
-    exec echo "HIERARCH FPA.ZP =          25. / Magnitude zero point" > tmp.hdr
-    exec fits_insert $cmffile tmp.hdr
-
-    echo addstar -D SKY_DEPTH 4 -D CATDIR $catdir -D CAMERA simtest $cmffile -D CATFORMAT $2 -quick-airmass
-    exec addstar -D SKY_DEPTH 4 -D CATDIR $catdir -D CAMERA simtest $cmffile -D CATFORMAT $2 -quick-airmass
-  end
-
-  echo relphot -D CATDIR $catdir -region 8 12 18 22 -images g,r,i,z,y -update
-  exec relphot -D CATDIR $catdir -region 8 12 18 22 -images g,r,i,z,y -update
+    # exec echo "HIERARCH FPA.ZP =          {25.+$offset} / Magnitude zero point" > tmp.hdr
+    # exec fits_insert $cmffile tmp.hdr
+
+    echo addstar -D SKY_DEPTH 4 -D CATDIR $catdir -D CAMERA simtest $cmffile -D CATFORMAT $2 -quick-airmass -D ZERO_POINT_OPTION CHIP_HEADER
+    exec addstar -D SKY_DEPTH 4 -D CATDIR $catdir -D CAMERA simtest $cmffile -D CATFORMAT $2 -quick-airmass -D ZERO_POINT_OPTION CHIP_HEADER
+  end
+
+# echo relphot -D CATDIR $catdir -region 8 12 18 22 -images g,r,i,z,y -update
+# exec relphot -D CATDIR $catdir -region 8 12 18 22 -images g,r,i,z,y -update
+
+  echo relphot -D CATDIR $catdir -region 8 12 18 22 -averages -update
+  exec relphot -D CATDIR $catdir -region 8 12 18 22 -averages -update
 
   if ($PARALLEL)
@@ -287,7 +308,8 @@
     $cmffile = $word:6\.cmf
     $offset = $word:3
-
     sprintf myDATE "%s" $word:4
     sprintf myTIME "%s" $word:5
+    $filter = $word:7
+
     echo ctimes -abs $myDATE,$myTIME -var mjd
     ctimes -abs $myDATE,$myTIME -var mjd
@@ -296,6 +318,6 @@
 
     mkinput $offset $rawfile
-    echo mkcmf -forcedwarp -photcode SIMTEST.r.ForcedWarp $rawfile $cmffile -extroot SkyChip -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 2 -tess_id $TESS_ID -skycell $SKYCELL -bad-psfqf-frac $BAD_PSFQF_FRAC
-    exec mkcmf -forcedwarp -photcode SIMTEST.r.ForcedWarp $rawfile $cmffile -extroot SkyChip -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 2 -tess_id $TESS_ID -skycell $SKYCELL -bad-psfqf-frac $BAD_PSFQF_FRAC
+    echo mkcmf -forcedwarp -photcode SIMTEST.$filter.ForcedWarp $rawfile $cmffile -extroot SkyChip -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 2 -tess_id $TESS_ID -skycell $SKYCELL -bad-psfqf-frac $BAD_PSFQF_FRAC
+    exec mkcmf -forcedwarp -photcode SIMTEST.$filter.ForcedWarp $rawfile $cmffile -extroot SkyChip -date $myDATE -time $myTIME -mjd $mjd -radec $RA $DEC -type $1 -imageID $ID -sourceID 2 -tess_id $TESS_ID -skycell $SKYCELL -bad-psfqf-frac $BAD_PSFQF_FRAC
 
     echo addstar -D SKY_DEPTH 4 -D CATDIR $catdir -D CAMERA simtest $cmffile -D CATFORMAT $2 -quick-airmass -xrad
@@ -383,18 +405,22 @@
 # this inserts a single stack, adding the exposures associated with this stack
 macro insert.stack
-  if ($0 != 3)
-    echo "USAGE: insert.stack (ID) (stkfile)"
-    break
-  end
-
-  local i stkID expID expfile wrpfile stkfile
+  if ($0 != 5)
+    echo "USAGE: insert.stack (stkID) (skyID) (stkfile) (filter)"
+    break
+  end
+
+  local i stkID skyID expID expfile wrpfile stkfile filter
   
-  $stkID = $1
-  $stkfile = $2
-
-  dbinsert stackRun (stack_id, skycell_id, filter, software_ver) values ($stkID, $SKYCELL, "g.00000", "37500")
+  $stkID   = $1
+  $skyID   = $2
+  $stkfile = $3
+  $filter  = $4
+
+  $skycalID = $stkID + 20
+
+  dbinsert stackRun (stack_id, skycell_id, filter, software_ver) values ($stkID, $SKYCELL, "$filter.00000", "37500")
 
   # we have a single fullForceRun for each stack, so we can match fullForceRun ids to stackRun ids
-  dbinsert fullForceRun (ff_id, skycal_id) values ($stkID, $stkID)
+  dbinsert fullForceRun (ff_id, skycal_id) values ($stkID, $skycalID)
 
   $TIMEFORMAT = mjd
@@ -429,8 +455,8 @@
 
   dbinsert stackSumSkyfile (stack_id, mjd_obs) values ($stkID, $mjd)
-  dbinsert skycalRun (sky_id, skycal_id, stack_id) values ($stkID, $stkID, $stkID)
-  dbinsert skycalResult (skycal_id, path_base) values ($stkID, "$stkfile")
+  dbinsert skycalRun (sky_id, skycal_id, stack_id) values ($skyID, $skycalID, $stkID)
+  dbinsert skycalResult (skycal_id, path_base) values ($skycalID, "$stkfile")
   
-  dbinsert addRun (stage, state, stage_id, minidvodb_name) values ("skycal", "full", $stkID, "catdir.stk")
+  dbinsert addRun (stage, state, stage_id, minidvodb_name) values ("skycal", "full", $skycalID, "catdir.stk")
 end
 
@@ -438,11 +464,13 @@
 macro insert.stack.set
 
-  local i
+  local i stkID skyID cmffile filter
 
   for i 0 $stackdata:n
     list word -split $stackdata:$i
-    $ID = $word:0
-    $cmffile = $word:1
-    insert.stack $ID $cmffile
+    $stkID = $word:0
+    $skyID = $word:1
+    $cmffile = $word:2
+    $filter = $word:4
+    insert.stack $stkID $skyID $cmffile $filter
   end
 
