Index: trunk/ippToPsps/jython/diffbatch.py
===================================================================
--- trunk/ippToPsps/jython/diffbatch.py	(revision 38956)
+++ trunk/ippToPsps/jython/diffbatch.py	(revision 38958)
@@ -98,6 +98,4 @@
            self.number[num]= diffSkyFileID
 
-
-
            self.logger.infoPair("getting diff" ," metadata")
            self.logger.infoPair("for diff skyfile id ", str(diffSkyFileID))
@@ -106,4 +104,6 @@
            self.logger.infoPair("ok, I guess that worked","now doing this")
 
+           # this code is very fragile: it depends on the right order in the output 
+           # of the sql call in another file (see forcedwarpbatch.py for a better method)
            if not meta:
                self.logger.errorPair("Could not get", "diff metadata")
@@ -132,5 +132,5 @@
            self.logger.infoPair("TessName ",tessName)
            self.tessID[num] = self.scratchDb.getTessID(tessName)
-        
+
            self.logger.infoPair("TessName ",tessName)
        # XXX EAM 20140812 : I am hardwiring the parsing logic for RINGS vs LOCAL
@@ -325,6 +325,7 @@
             sqlLine.group("DapFlux",          "AP_FLUX / " + expTimeString)
             sqlLine.group("DapFluxErr",       "AP_FLUX_SIG / " + expTimeString)
-#           sqlLine.group("DapFillF",         "AP_NPIX / (3.14159265359 * AP_MAG_RADIUS * AP_MAG_RADIUS)")
-#       sqlLine.group("DapRadius",        "AP_MAG_RADIUS")
+
+        if self.haveApNpix[num]:
+            sqlLine.group("DapFillF", "AP_NPIX / (3.14159265359 * AP_MAG_RADIUS * AP_MAG_RADIUS)")
         sqlLine.group("DkronFlux",        "KRON_FLUX / " + expTimeString)
         sqlLine.group("DkronFluxErr",     "KRON_FLUX_ERR / " + expTimeString)
@@ -568,4 +569,7 @@
         self.logger.infoPair("Importing DF tables with table match expression: ", tableRE)
 
+        self.tablesLoaded = []
+        self.haveApNpix = {}
+
         count = 0
         for num in self.number:
@@ -581,4 +585,7 @@
                 return False
         
+            # this is an optional field, check for it in the table below
+            self.haveApNpix[num] = 0
+
             # count = 0
             for table in tables:
@@ -587,13 +594,20 @@
                 if not match: continue
 
-                self.logger.infoPair("Reading IPP table", table.name+"_"+str(num))
+                # check for AP_NPIX columns: missing for SAS39
+                if table.name == "SkyChip_psf":
+                    columns = table.columns()
+                    if 'AP_NPIX' in columns:
+                        self.haveApNpix[num] = 1
+
+                myTable = table.name + "_" + str(num)
+
+                self.logger.infoPair("Reading IPP table", myTable)
                 table = stilts.tpipe(table, cmd='addcol table_index $0')
                 table = stilts.tpipe(table, cmd='explodeall')
                   
-            # drop any previous tables before import
-                self.scratchDb.dropTable(table.name+"_"+str(num))
+                # drop any previous tables before import
+                self.scratchDb.dropTable(myTable)
                   
-                  
-            # IPP FITS files are littered with infinities, so remove these
+                # IPP FITS files are littered with infinities, so remove these
                 self.logger.info("Removing Infinity values from all columns")
                 table = stilts.tpipe(table, cmd='keepcols "' + columns + '"')
@@ -602,12 +616,14 @@
                   
                 try:
-                    table.write(self.scratchDb.url + '#' +  table.name+"_"+str(num))
+                    table.write(self.scratchDb.url + '#' +  myTable)
                     count = count + 1
                 except:
-                    self.logger.exception("Problem writing table '" +  table.name+"_"+str(num) + "' to the database")
+                    self.logger.exception("Problem writing table '" +  myTable + "' to the database")
                       
                 self.logger.infoPair("Done. Imported", "%d tables" % count)
                 self.indexIppTables(num)
                       
+                self.tablesLoaded.append(myTable)
+
         return True
 
