Index: /trunk/ippToPsps/jython/gpc1db.py
===================================================================
--- /trunk/ippToPsps/jython/gpc1db.py	(revision 31830)
+++ /trunk/ippToPsps/jython/gpc1db.py	(revision 31831)
@@ -8,4 +8,5 @@
 
 from mysql import MySql
+from fits import Fits
 from java.sql import *
 
@@ -54,5 +55,5 @@
         rs.close()
 
-        self.logger.info("Found %d items in DVO database '" % (len(ids)))
+        self.logger.debug("Found %d items in DVO database '" % (len(ids)))
 
         return ids
@@ -61,10 +62,22 @@
     Gets a list of ids in this DVO database for this stage, could be cam or staticsky (so far)
     '''
-    def getIDsInThisDVODbForThisStage(self, dvoDb, stage):
-
-        sql = "SELECT DISTINCT stage_id \
-               FROM addRun \
-               WHERE stage = '" + stage + "' \
-               AND dvodb = '" + dvoDb + "'"
+    def getIDsInThisDVODbForThisStage(self, dvoDb, batchType):
+
+        if batchType == "P2": # TODO define these someplace 
+
+            stage = "cam"
+            sql = "SELECT DISTINCT stage_id \
+                   FROM addRun \
+                   WHERE stage = '" + stage + "' \
+                   AND dvodb = '" + dvoDb + "'"
+
+        elif batchType == "ST":       
+
+            stage = "staticsky"
+            sql = "SELECT DISTINCT stack_id \
+                   FROM gpc1.staticskyInput \
+                   JOIN gpc1.addRun ON(gpc1.staticskyInput.sky_id = gpc1.addRun.stage_id) \
+                   WHERE stage = '" + stage + "' \
+                   AND dvodb = '" + dvoDb + "'"
 
         try:
@@ -79,5 +92,5 @@
         rs.close()
 
-        self.logger.info("Found %d items in DVO database '%s' for stage='%s'" % (len(ids), dvoDb, stage))
+        self.logger.debug("Found %d items in DVO database '%s' for stage='%s'" % (len(ids), dvoDb, stage))
 
         return ids
@@ -116,5 +129,5 @@
     Gets some stack-stage meta data for this sky_id # TODO this SQL could surely be improved
     '''
-    def getStackStageMeta(self, skyID, filter):
+    def getStackStageMeta(self, stackID):
 
         self.logger.debug("Querying GPC1 for stack meta data")
@@ -122,20 +135,14 @@
         meta = []
         sql = "SELECT \
-               stackRun.stack_id,\
-               stackRun.skycell_id, \
-               stackRun.software_ver \
-               FROM \
-               staticskyInput, staticskyRun, stackRun, staticskyResult \
-               WHERE staticskyRun.sky_id = staticskyInput.sky_id \
-               AND staticskyInput.stack_id = stackRun.stack_id \
-               AND staticskyInput.sky_id = staticskyResult.sky_id \
-               and staticskyInput.sky_id = %d \
-               and filter = '%s'" % (skyID, filter)
-
-
-        try:
-            rs = self.executeQuery(sql)
-            rs.first()
-            meta.append(rs.getInt(1))
+               filter,\
+               skycell_id, \
+               software_ver \
+               FROM stackRun \
+               WHERE stack_id = %d" % stackID
+               
+        try:
+            rs = self.executeQuery(sql)
+            rs.first()
+            meta.append(rs.getString(1))
             meta.append(rs.getString(2))
             meta.append(rs.getString(3))
@@ -212,38 +219,37 @@
 
     '''
-    Gets all cmf files for this sky_id. handles both absolute paths and neb paths
-    '''
-    def getStackStageCmfs(self, skyID):
-
-        self.logger.debug("Querying GPC1 for stack cmf files")
-
-        sql = "SELECT path_base, num_inputs \
-               FROM staticskyResult \
-               WHERE sky_id = %d" % skyID
-
-        try:
-            rs = self.executeQuery(sql)
-            rs.first()
+    Gets the path to the cmf file for this stack_id
+    '''
+    def getStackStageCmf(self, stackID):
+      
+        sql = "SELECT staticskyResult.path_base \
+               FROM staticskyRun \
+               JOIN staticskyInput USING(sky_id) \
+               JOIN staticskyResult USING(sky_id) \
+               JOIN stackRun USING(stack_id) \
+               JOIN stackSumSkyfile USING(stack_id) \
+               WHERE stack_id = %s" % stackID
+
+        try:
+            rs = self.executeQuery(sql)
+            rs.first()
+            pathBase = rs.getString(1)
+
+            # now find ALL cmf files at this path_base as there can be more than one
+            f=os.popen("neb-ls -p " + pathBase + "%cmf")
+            for i in f.readlines():
+
+                path = i.rstrip()
+                fits = Fits(self.logger, path)
+
+                # we need to check if this is the correct cmf file, and if so then break and return
+                if fits.getPrimaryHeaderValue("STK_ID") == str(stackID):
+                    return path
+
         except:
             self.logger.exception("Can't query for stack cmfs")
 
-        # get path to base dir of cmf files
-        path = rs.getString(1)
-        #path = path[0:path.rfind("/")]
-       
-        # list all cmf files if a neb path
-        files = []
-        if path.startswith("neb"):
-
-            f=os.popen("neb-ls -p "+path+"%cmf")
-            for i in f.readlines():
-                files.append(i.rstrip())
-
-        # or not a neb path
-        else:
-            files = glob.glob(path + "*.cmf")
-
-        return files
-
+        self.logger.error("Could not find stack cmf")
+        return "NULL"
 
     '''
