Index: trunk/ippToPsps/jython/forcedwarpbatch.py
===================================================================
--- trunk/ippToPsps/jython/forcedwarpbatch.py	(revision 37917)
+++ trunk/ippToPsps/jython/forcedwarpbatch.py	(revision 37935)
@@ -6,4 +6,5 @@
 import time
 import stilts
+import re 
 
 from java.lang import *
@@ -54,24 +55,68 @@
                forcedWarpID,
                batchID,
-               "P2", 
-               gpc1Db.getForcedWarpStageSmf(forcedWarpID)
-
+               "FW",
+               None) 
+     #          gpc1Db.getForcedWarpStageSmf(forcedWarpID)
+       # there is a funny problem with order here: we need to open the file first to read the header to get 
+       # to skycell ID
+       #forcedWarpID = self.forcedWarpID        
+       self.logger.infoPair("getting the fits file for add_id",forcedWarpID)
+       self.fits = gpc1Db.getForcedWarpStageCmf(skychunk.dvoLabel, forcedWarpID)
+       self.forcedWarpID = forcedWarpID
+       self.header = self.fits.getPrimaryHeader()
+       self.warpSkyFileID = self.getKeyValue(self.header,"IMAGEID")
+       self.filterName = self.getKeyValue(self.header,"FPA.FILTERID")
+       self.logger.infoPair("found filterName",self.filterName)
+       self.logger.infoPair("found warp_skyfile_id",self.warpSkyFileID)
        # get camera meta data
-       meta = self.gpc1Db.getForcedWarpStageMeta(self.id)
+
+       meta = self.gpc1Db.getForcedWarpStageMeta(forcedWarpID, self.warpSkyFileID)
+
        if not meta:
            self.logger.errorPair("Could not get", "forced warp metadata")
            raise
 
-       self.expID = meta[0];
-       self.expName = meta[1];
-       self.distGroup = meta[2];
-       self.analysisVer = meta[3];
-       self.bias = meta[4];
-       self.biasScat = meta[5];
+       self.expID = meta[0]
+       self.expTime = meta[1]
+       self.analysisVer = meta[2]
+       self.skycellName = meta[3]
+       self.tessName = meta[4]
+       
+       self.logger.infoPair("found exp_id",str(self.expID))
+       self.logger.infoPair("found exp_time",str(self.expTime))
+       self.logger.infoPair("found skycell_id",self.skycellName)
+       self.logger.infoPair("found tess_id",self.tessName)
+
+
+       #tessName = self.getKeyValue(self.header, 'TESS_ID')
+       #self.tessID = self.scratchDb.getTessID(tessName)
 
        if not self.analysisVer: self.analysisVer = -999
+       if self.analysisVer is None: self.analysisVer = "-999"
+       self.logger.infoPair("found analysisVer",self.analysisVer)
+
+       if self.tessName == 'RINGS.V3':
+
+           # skycell is, eg "skycell.1133.081"
+           #                 0123456789012345
+           self.projectionID = self.skycellName[8:12]
+           #self.skycellID = self.skycellName[13:]
+           self.skycellID = self.skycellName[8:] 
+       else:
+           # skycell is, eg "skycell.081"
+           #                 01234567890
+           self.projectionID = 0
+           self.skycellID = self.skycellName[8:11]
+           self.skycellID=self.skycellName[8:]
+       if self.skycellID == "":
+           self.skycellID = -1
+
+       self.logger.infoPair("projection cell found ",self.projectionID)
+       self.logger.infoPair("skycell found", self.skycellID)
+
+
 
        # create an output filename, which is {expID}.FITS
-       self.outputFitsFile = "%08d.FITS" % self.expID
+       self.outputFitsFile = "%08d.FITS" % forcedWarpID
        self.outputFitsPath = "%s/%s" % (self.localOutPath, self.outputFitsFile)
        self.dropTableVerbose("ForcedWarpMeasurement")
@@ -79,44 +124,35 @@
        self.dropTableVerbose("ForcedWarpMeta")
        self.dropTableVerbose("ForcedWarpToImage")
+       self.dropTableVerbose("ForcedWarpLensing")
        if not self.config.retry:
-       self.scratchDb.dropTable("ForcedWarpMeasurement")
-       self.scratchDb.dropTable("ForcedWarpExtended")
-       self.scratchDb.dropTable("ForcedWarpMeta")
-       self.scratchDb.dropTable("ForcedWarpToImage")
+           self.scratchDb.dropTable("ForcedWarpMeasurement")
+           self.scratchDb.dropTable("ForcedWarpExtended")
+           self.scratchDb.dropTable("ForcedWarpMeta")
+           self.scratchDb.dropTable("ForcedWarpToImage")
+           self.scratchDb.dropTable("ForcedWarpLensing")
        self.historyModNum = "0"
 
        # get a few primary header values. if in test mode, then use defaults
-       if self.safeDictionaryAccessWithDefault(self.header, 'MJD-OBS', "1") == "NULL":
-           self.logger.errorPair("Could not get", "MJD-OBS")
-           raise
-
-
-       if self.safeDictionaryAccessWithDefault(self.header, 'EXPTIME', "1") == "NULL":
-           self.logger.errorPair("Could not get", "EXPTIME")
-           raise
-
-       if self.safeDictionaryAccessWithDefault(self.header, 'FILTERID', "g.0000") == "NULL":
-           self.logger.errorPair("Could not get", "FILTERID")
-           raise
-       
+       self.logger.infoPair("calculating ObsTime","ok")
        # MJD-OBS is the exposure start, EXTIME / 172800 = (EXPTIME sec / 84600 sec/day) / 2
-       self.obsTime = float(self.header['MJD-OBS']) + (float(self.header['EXPTIME']) / 172800.0)
-      
+       self.obsTime = float(self.header['MJD-OBS']) + (float(self.expTime) / 172800.0)
+       self.logger.infoPair("obstime",self.obsTime)
        # set up some defauts
        self.calibModNum = 0
        self.totalNumPhotoRef = 0
 
-
-       self.filter = self.header['FILTERID'][0:1]
-       self.filterID = self.scratchDb.getFilterID(self.filter) 
+       self.filterID = self.scratchDb.getFilterID(self.filterName[0])
+       self.logger.infoPair("filterID is ",self.filterID)
 
        # insert what we know about this stack batch into the stack table
-       self.ippToPspsDb.insertDetectionMeta(self.batchID, self.expID, self.filter)
+       self.logger.infoPair("inserting basics into ","ForcedWarpMeta")
+
+       self.ippToPspsDb.insertForcedWarpMeta(int(self.batchID), int(self.expID), self.filterName[0])
 
        # dump stuff to log
-       self.logger.infoPair("Cam ID", "%d" % self.id)
-       self.logger.infoPair("Exp ID", "%d" % self.expID)
-       self.logger.infoPair("Exp name", "%s" % self.expName)
-       self.logger.infoPair("Distribution group", "%s" % self.distGroup)
+       self.logger.infoPair("add ID", "%d" % self.id)
+       self.logger.infoPair("Exp ID", self.expID)
+
+
 
 
@@ -129,6 +165,6 @@
         self.logger.infoPair("Processing table", "ForcedWarpMeta")
 
-        photoCalID = str(self.scratchDb.getPhotoCalID(forcedWarpID))       
-
+        header   = self.header
+#        photoCalID = str(self.scratchDb.getPhotoCalID(forcedWarpID))       
 
         sqlLine = sqlUtility("INSERT INTO ForcedWarpMeta (")
@@ -136,18 +172,20 @@
         #surveyID (below)
         #filterdID (below)
+        sqlLine.group("forcedWarpID", str(self.forcedWarpID))
         sqlLine.group("skyCellID",     str(self.skycellID)) 
-        sqlLine.group("photoCalID",    photoCalID) 
+#        sqlLine.group("photoCalID",    photoCalID) 
         sqlLine.group("magSat",           self.getKeyFloat(header, "%.8f", 'FSATUR'))
-        sqlLine.group("analVer",           self.analysisVer);
-        sqlLine.group("expTime",               self.getKeyFloat(self.header, "%.8f", 'EXPREQ'));
+        sqlLine.group("analVer",           str(self.analysisVer))
+        sqlLine.group("expTime",self.expTime)
         sqlLine.group("completMag",       self.getKeyFloat(header, "%.8f", 'FLIMIT'))
-        sqlLine.group("astroScat",        self.getKeyFloat(header, "%.8f", 'CERROR'))
-        sqlLine.group("photoScat",             self.getKeyFloat(self.header, "%.8f", 'ZPT_ERR'));
-        sqlLine.group("nAstroRef",      self.getKeyValue(header, 'NASTRO'))
-        sqlLine.group("nPhotoRef",      self.getKeyValue(header, 'NASTRO'))
+#        sqlLine.group("astroScat",        self.getKeyFloat(header, "%.8f", 'CERROR'))
+#        sqlLine.group("photoScat",             self.getKeyFloat(self.header, "%.8f", 'ZPT_ERR'));
+#no nastro
+#        sqlLine.group("nAstroRef",      self.getKeyValue(header, 'NASTRO'))
+#        sqlLine.group("nPhotoRef",      self.getKeyValue(header, 'NASTRO'))
         sqlLine.group("psfModelID",     self.getKeyValue(header, 'PSFMODEL'))
         sqlLine.group("psfFwhm_mean",   self.getKeyFloat(header, "%.8f", 'FWHM_MAJ'))
         sqlLine.group("psfFwhm_max",    self.getKeyFloat(header, "%.8f", 'FW_MJ_UQ'))
-        sqlLine.group("photoZero",        self.getKeyFloat(self.header, "%.8f", 'ZPT_OBS'))
+#        sqlLine.group("photoZero",        self.getKeyFloat(self.header, "%.8f", 'ZPT_OBS'))
         #photoColor -- how do I set this? it's also not set in stack meta
         sqlLine.group("ctype1",                self.getKeyValue(self.header, 'CTYPE1'));
@@ -174,8 +212,9 @@
         self.scratchDb.updateAllRows("ForcedWarpMeta", "batchID", str(self.batchID))
         self.scratchDb.updateAllRows("ForcedWarpMeta", "surveyID", str(self.surveyID))
-        self.scratchDb.updateFilterID("ForcedWarpMeta", self.filter)
+#        self.scratchDb.updateFilterID("ForcedWarpMeta", self.filterID)
+        self.scratchDb.updateFilterID("ForcedWarpMeta",self.filterName[0])
         self.scratchDb.updateAllRows("ForcedWarpMeta", "calibModNum", str(self.calibModNum))
         self.scratchDb.updateAllRows("ForcedWarpMeta", "dataRelease", str(self.skychunk.dataRelease))
-        self.tablesToExport.append("ForcedWarpMeta")
+        #self.tablesToExport.append("ForcedWarpMeta")
        
     '''
@@ -186,8 +225,6 @@
         self.logger.infoPair("Procesing table", "ForcedWarpToImage")
 
-        for filter in self.filters:
-            
-            forcedwarpID = self.forcedwarpID
-            if forcedwarpID > 0:
+        forcedwarpID = self.forcedWarpID
+        if forcedwarpID > 0:
     
                 imageIDs = self.gpc1Db.getImageIDsForThisForcedWarpID(forcedwarpID)
@@ -198,9 +235,11 @@
                    VALUES (\
                    " + str(forcedwarpID) + ", " + imageID + ")"
-                    self.scratchDb.execute(sql)
-
+                    try: self.scratchDb.execute(sql)
+                    except:
+                       self.logger.infoPair("can't execute sql",sql)
+                       raise
                     # now update StackMeta with correct number of inputs
 
-        self.tablesToExport.append("ForcedWarpToImage")
+        #self.tablesToExport.append("ForcedWarpToImage")
 
 
@@ -211,16 +250,91 @@
     Populates the ForcedWarpMeasurement table
     '''
-    def populateForcedWarpMeasurementTable(self, results):
+    def populateForcedWarpMeasurement(self):
 
         pspsTableName = "ForcedWarpMeasurement"
-        ippTableName = "ForcedWarpMeasurement_psf"
+        ippTableName = "SkyChip_psf"
 
 
         self.logger.infoPair("Procesing table", "ForcedWarpMeasurement")
 
+        BEFORE = self.scratchDb.getRowCount(ippTableName)
+        extTimeString = str(self.expTime)
+
+#missing are zp, telluricExt, airmass (not easy to get ?)
+
+        sqlLine = sqlUtility("INSERT INTO " + pspsTableName + " (")
+        sqlLine.group("ippDetectID",     "IPP_IDET")
+        sqlLine.group("forcedWarpID",            str(self.forcedWarpID));
+        sqlLine.group("randomWarpID",     "FLOOR(RAND("+str(self.batchID)+")*9223372036854775807)")   
+        sqlLine.group("filterID",        str(self.filterID))
+        sqlLine.group("surveyID",        str(self.surveyID))
+        sqlLine.group("skycellID",       str(self.skycellID))
+        sqlLine.group("expTime",         str(self.expTime))
+        sqlLine.group("obsTime", str(self.obsTime))
+        sqlLine.group("psfFlux", "PSF_INST_FLUX / "+extTimeString)
+        sqlLine.group("psfFluxErr", "PSF_INST_FLUX_SIG / "+extTimeString)
+        sqlLine.group("psfMajorFWHM",    "PSF_FWHM_MAJ")
+        sqlLine.group("psfMinorFWHM",    "PSF_FWHM_MIN")
+        sqlLine.group("psfTheta",        "PSF_THETA")
+        sqlLine.group("psfCore",         "PSF_CORE")
+        sqlLine.group("psfQf",           "PSF_QF")
+        sqlLine.group("psfQfPerfect",    "PSF_QF_PERFECT")
+        sqlLine.group("psfChiSq",        "PSF_CHISQ")
+        sqlLine.group("psfLikelihood",   "psfLikelihood(EXT_NSIGMA)")
+        sqlLine.group("momentXX",        "MOMENTS_XX")
+        sqlLine.group("momentXY",        "MOMENTS_XY")
+        sqlLine.group("momentYY",        "MOMENTS_YY")
+        sqlLine.group("momentR1",        "MOMENTS_R1")
+        sqlLine.group("momentRH",        "MOMENTS_RH")
+        sqlLine.group("momentM3C",       "MOMENTS_M3C")
+        sqlLine.group("momentM3S",       "MOMENTS_M3S")
+        sqlLine.group("momentM4C",       "MOMENTS_M4C")
+        sqlLine.group("momentM4S",       "MOMENTS_M4S")
+        sqlLine.group("apFlux","AP_FLUX / " + extTimeString)
+        sqlLine.group("apFluxErr","AP_FLUX_SIG / " + extTimeString)
+        sqlLine.group("apFillF","AP_NPIX / (3.14159265359 * POW(AP_MAG_RADIUS - 0.5, 2))")
+        sqlLine.group("kronFlux","KRON_FLUX / " + extTimeString)
+        sqlLine.group("kronFluxErr","KRON_FLUX_ERR / " + extTimeString )
+        sqlLine.group("kronRad","MOMENTS_R1 * 2.5")
+        sqlLine.group("sky","SKY /" +extTimeString)
+        sqlLine.group("skyErr","SKY_SIGMA / "+extTimeString)
+        sqlLine.group("infoFlag","FLAGS")
+        sqlLine.group("infoFlag2","FLAGS2")
+        sqlLine.group("dataRelease",str(self.skychunk.dataRelease))
+
+        sql = sqlLine.makeRaw(") SELECT ", " FROM " + ippTableName)
+
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("failed sql: ", sql)
+            raise
+
+
+
+
         # insert the per-object data (IDs, random stack ID, dataRelease, etc)
-        self.selectDvoObjIDs()
-
-        self.generateRandomIDs()
+        #self.selectDvoObjIDs()
+        self.logger.infoPair("inserting dvo info: find imageID","from externID")
+        imageID = self.scratchDb.getImageIDFromExternID(self.warpSkyFileID)
+        self.logger.infoPair("updating","forcedWarpMeasurement")
+        sqlLine = sqlUtility("UPDATE ForcedWarpMeasurement as a, " + self.scratchDb.dvoDetectionTable + " as b SET ")
+        sqlLine.group("a.objID","b.objID")
+        sqlLine.group("a.detectID","b.detectID")
+        sqlLine.group("a.ippObjID","b.ippObjID")
+        sqlLine.group("a.dvoRegionID","b.catID")
+        sqlLine.group("a.zp","b.zp")
+        sqlLine.group("a.telluricExt","b.telluricExt")
+        sqlLine.group("a.airmass","b.airmass")
+        sql = sqlLine.makeEquals("WHERE a.ippDetectID = b.ippDetectID AND b.imageID = " +str( imageID))
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("failed sql:",sql)
+            raise
+        self.logger.infoPair("need to cull"," nulls objid")
+        results= self.scratchDb.reportAndDeleteRowsWithNULLS("ForcedWarpMeasurement", "objID")
+
+
+
+#        self.generateRandomIDs()
 
         self.logger.infoPair("Adding 'row' columns to", "ForcedWarpMeasurement")
@@ -233,29 +347,135 @@
         # add indexes ForcedWarpMeasurement
         self.scratchDb.createIndex("ForcedWarpMeasurement", "objID")
-
+        self.scratchDb.createIndex("ForcedWarpMeasurement", "ippDetectID")
+    '''
+    Populates the ForcedWarpExtended table
+    '''
+    def populateForcedWarpExtended(self):
+
+        pspsTableName = "ForcedWarpExtended"
+        ippTableName = "SkyChip_xrad"
+
+        #self.tablesToExport.append("ForcedWarpExtended")
+
+        #isert stuff from SkyChip_psf
+        sqlLine = sqlUtility("INSERT INTO " + pspsTableName + " (")
+        sqlLine.group("ippDetectID",     "IPP_IDET")
+        sqlLine.group("flxR5", "APER_FLUX_5")
+        sqlLine.group("flxR5Err", "APER_FLUX_ERR_5" )
+        sqlLine.group("flxR5Std", "APER_FLUX_STDEV_5")
+        sqlLine.group("flxR5Fill", "APER_FILL_5")
+        sqlLine.group("flxR6", "APER_FLUX_6")
+        sqlLine.group("flxR6Err", "APER_FLUX_ERR_6" )
+        sqlLine.group("flxR6Std", "APER_FLUX_STDEV_6")
+        sqlLine.group("flxR6Fill", "APER_FILL_6")
+
+        sql = sqlLine.makeRaw(") SELECT ", " FROM " + ippTableName)
+
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("failed sql: ", sql)
+            raise
+
+        #insert the things from FOrcedWarpMeasurement
+        self.logger.infoPair("inserting objID,etc from","ForcedWarpMeasurement")
+        sqlLine = sqlUtility("UPDATE " + pspsTableName + " as a, ForcedWarpMeasurement as b SET ")
+        sqlLine.group("a.objID","b.objID")
+        sqlLine.group("a.uniquePspsFWid","b.uniquePspsFWid")
+        sqlLine.group("a.detectID","b.detectID")
+        sqlLine.group("a.ippObjID","b.ippObjID")
+        sqlLine.group("a.filterID","b.filterID")
+        sqlLine.group("a.surveyID","b.surveyID")
+        sqlLine.group("a.forcedWarpID","b.forcedWarpID")
+        sqlLine.group("a.randomWarpID","b.randomWarpID")
+        sqlLine.group("a.skycellID","b.skycellID")
+        sqlLine.group("a.dvoRegionID","b.dvoRegionID")
+        sqlLine.group("a.obsTime","b.obsTime")
+        sql = sqlLine.makeEquals("Where a.ippDetectID = b.ippDetectID")
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("failed sql: ", sql)
+            raise
+        
+       # add in the psps unique ids from stuff
+        self.logger.infoPair("adding information from Skychip_xrad into","ForcedWarpExtended")
+        
+        # cull bad d
+        self.logger.infoPair("need to cull"," nulls objid")
+        results= self.scratchDb.reportAndDeleteRowsWithNULLS("ForcedWarpExtended", "objID")
+        
+        self.scratchDb.createIndex("ForcedWarpExtended", "objID")
                
 
 
-
-
-
-
-
-        self.tablesToExport.append("ForcedWarpMeasurement")
-
-
-    '''
-    Populates the ForcedWarpExtended table
-    '''
-    def populateForcedWarpExtendedTable(self, results):
-
-        pspsTableName = "ForcedWarpExtended"
-        ippTableName = "ForcedWarpExtended_psf"
-
-    
-        self.tablesToExport.append("ForcedWarpExtended")
-
-           # get dvo ids, 
-        # add in the psps unique ids from stuff
+    '''                                                                                                                                                                  
+    Populates the ForcedWarpExtended table                                                                                                                               
+    '''
+    def populateForcedWarpLensing(self):
+
+        pspsTableName = "ForcedWarpLensing"
+        ippTableName = "SkyChip_psf"
+
+        #self.tablesToExport.append("ForcedWarpExtended")                                                                                                                
+
+        #isert stuff from SkyChip_psf                                                                                                                                    
+        sqlLine = sqlUtility("INSERT INTO " + pspsTableName + " (")
+        sqlLine.group("ippDetectID",     "IPP_IDET")
+        sqlLine.group("lensObjSmearX11", "LENS_X11_SM_OBJ")
+        sqlLine.group("lensObjSmearX12", "LENS_X12_SM_OBJ")
+        sqlLine.group("lensObjSmearX22", "LENS_X22_SM_OBJ")
+        sqlLine.group("lensObjSmearE1", "LENS_E1_SM_OBJ")
+        sqlLine.group("lensObjSmearE2", "LENS_E2_SM_OBJ")
+        sqlLine.group("lensObjShearX11", "LENS_X11_SH_OBJ")
+        sqlLine.group("lensObjShearX12", "LENS_X12_SH_OBJ")
+        sqlLine.group("lensObjShearX22", "LENS_X22_SH_OBJ")
+        sqlLine.group("lensObjShearE1", "LENS_E1_SH_OBJ")
+        sqlLine.group("lensObjShearE2", "LENS_E2_SH_OBJ")
+        sqlLine.group("lensPSFSmearX11", "LENS_X11_SM_PSF")
+        sqlLine.group("lensPSFSmearX12", "LENS_X12_SM_PSF")
+        sqlLine.group("lensPSFSmearX22", "LENS_X22_SM_PSF")
+        sqlLine.group("lensPSFSmearE1", "LENS_E1_SM_PSF")
+        sqlLine.group("lensPSFSmearE2", "LENS_E2_SM_PSF")
+        sqlLine.group("lensPSFShearX11", "LENS_X11_SH_PSF")
+        sqlLine.group("lensPSFShearX12", "LENS_X12_SH_PSF")
+        sqlLine.group("lensPSFShearX22", "LENS_X22_SH_PSF")
+        sqlLine.group("lensPSFShearE1", "LENS_E1_SH_PSF")
+        sqlLine.group("lensPSFShearE2", "LENS_E2_SH_PSF")
+        sql = sqlLine.makeRaw(") SELECT ", " FROM " + ippTableName)
+
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("failed sql: ", sql)
+            raise
+
+        #insert the things from FOrcedWarpMeasurement                                                                                                                    
+        self.logger.infoPair("inserting objID,etc from","ForcedWarpMeasurement")
+        sqlLine = sqlUtility("UPDATE " + pspsTableName + " as a, ForcedWarpMeasurement as b SET ")
+        sqlLine.group("a.objID","b.objID")
+        sqlLine.group("a.uniquePspsFWid","b.uniquePspsFWid")
+        sqlLine.group("a.detectID","b.detectID")
+        sqlLine.group("a.ippObjID","b.ippObjID")
+        sqlLine.group("a.filterID","b.filterID")
+        sqlLine.group("a.surveyID","b.surveyID")
+        sqlLine.group("a.forcedWarpID","b.forcedWarpID")
+        sqlLine.group("a.randomWarpID","b.randomWarpID")
+        sqlLine.group("a.skycellID","b.skycellID")
+        sqlLine.group("a.dvoRegionID","b.dvoRegionID")
+        sqlLine.group("a.obsTime","b.obsTime")
+        sql = sqlLine.makeEquals("Where a.ippDetectID = b.ippDetectID")
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("failed sql: ", sql)
+            raise
+
+       # add in the psps unique ids from stuff                                                                                                                           
+        self.logger.infoPair("adding information from Skychip_psf into","ForcedWarpLensing")
+
+        # cull bad d                                                                                                                                                     
+        self.logger.infoPair("need to cull"," nulls objid")
+        results= self.scratchDb.reportAndDeleteRowsWithNULLS("ForcedWarpLensing", "objID")
+
+        self.scratchDb.createIndex("ForcedWarpLensing", "objID")
+
+
 
     '''
@@ -269,10 +489,27 @@
     
         #this should be done
+        self.logger.infoPair("Populating","ForcedWarpMeta")
         self.populateForcedWarpMeta()          
-
-        self.populateForcedWarpMeasurement()           
+        self.tablesToExport.append("ForcedWarpMeta")
+
+        self.logger.infoPair("Populating","ForcedWarpMeasurement")
+        self.populateForcedWarpMeasurement()          
+        self.tablesToExport.append("ForcedWarpMeasurement")
+
+        self.logger.infoPair("Populating","ForcedWarpExtended")
         self.populateForcedWarpExtended()
+        self.tablesToExport.append("ForcedWarpExtended")
+
+        self.logger.infoPair("Populating","ForcedWarpLensing")
+        self.populateForcedWarpLensing()
+        self.tablesToExport.append("ForcedWarpLensing")
+
+
+        self.logger.infoPair("Populating","ForcedWarpToImage")
         self.populateForcedWarpToImage()
-             
+        self.tablesToExport.append("ForcedWarpToImage")             
+
+
+        self.logger.infoPair("setting min/max objid from","ForcedWarpMeasurement")
         self.setMinMaxObjID(["ForcedWarpMeasurement"])
 
@@ -286,5 +523,5 @@
     '''
     def importIppTables(self, columns="*", tableRE=""):
-        
+        count = 0 
         if self.config.retry: return True
         self.logger.infoPair("Importing FW tables with table match expression: ", tableRE)
@@ -307,4 +544,5 @@
            
            # drop any previous tables before import
+           
            self.scratchDb.dropTable(table.name)
                   
@@ -323,21 +561,14 @@
         self.logger.infoPair("Done. Imported", "%d tables" % count)
         self.indexIppTables()
-                      
         return True
 
 
-               
-
-
-
-
-
-
     def generateRandomIDs(self):
-           sql = "UPDATE ForcedWarpMeasurement set randomWarpID = FLOOR(RAND("+str(self.batchID)+")*9223372036854775807)"
-        try: self.scratchDb.execute(sql)
+        sql = "UPDATE ForcedWarpMeasurement set randomWarpID = FLOOR(RAND("+str(self.batchID)+")*9223372036854775807)"
+        try: 
+            self.scratchDb.execute(sql)
         except:
             self.logger.errorPair('failed sql',sql)
-            return
+            raise
 
 
@@ -346,21 +577,58 @@
     '''
 
-    def generatePspsUniqueIDs(self):
-#        sql = "UPDATE ForcedWarpMeasurement join (select @r:=@r+1 rownum, objID from \
-#        (select @r:=0) r, ForcedWarpMeasurement t) as foo using (objID) set \
-#        uniquePspsFWid = ((" +str(self.batchID)+ "*1000000000 ) + rownum)"
+    def generateUniquePspsIDs(self):
         sql = "UPDATE ForcedWarpMeasurement set uniquePspsFWid = (("+str(self.batchID)+"*1000000000 ) + row)"
-
         try: self.scratchDb.execute(sql)
         except:
            self.logger.errorPair('failed sql',sql)
-           return
-            
-
-
-
-
-
+           raise
+
+    '''
+    Defines the tables to be exported to psps            
+    '''
 
     def exportPspsTablesToFits(self, regex="(.*)"):
-       return super(DetectionBatch, self).exportPspsTablesToFits("(ForcedWarp.*)")
+       return super(ForcedWarpBatch, self).exportPspsTablesToFits("(ForcedWarp.*)")
+
+
+    '''
+    Drop a table and report the drop
+    '''
+    def dropTableVerbose(self, table):
+        self.logger.infoPair("dropping table:",table)
+        self.scratchDb.dropTable(table)
+
+
+    '''
+    Applies indexes to the PSPS tables
+    '''
+    def alterPspsTables(self):
+
+        self.logger.debug("Altering PSPS tables (currently does nothing in stackbatch)")
+
+        return True
+
+    '''
+    Applies indexes to the IPP tables
+    '''
+    def indexIppTables(self):
+
+        self.logger.infoPair("Creating indexes on", "IPP tables")
+        self.scratchDb.createIndex("SkyChip_psf",  "IPP_IDET")
+        self.scratchDb.createIndex("SkyChip_xfit", "IPP_IDET")
+        self.scratchDb.createIndex("SkyChip_xrad", "IPP_IDET")
+        self.scratchDb.createIndex("SkyChip_xsrc", "IPP_IDET")
+        self.scratchDb.createIndex("SkyChip_xgal", "IPP_IDET")
+
+        return True
+
+    '''
+    Updates table and generates pspsuniqueids
+    '''
+
+    def updatePspsUniqueIDs(self,table):
+        sql = "UPDATE ForcedWarpMeasurement set uniquePspsFWid = (("+str(self.batchID)+"*1000000000 ) + row)"
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair('failed sql',sql)
+            raise
