IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38837


Ignore:
Timestamp:
Oct 10, 2015, 12:40:44 PM (11 years ago)
Author:
eugene
Message:

make code less forgiving: sql execute aborts on failures; deal with various hidden problems: add IF EXISTS to drop table operations to avoid raising unneeded error, make sure tables exist before trying to add indexes, test for existence of the stack _xsrc, _xfit, etc tables (and skip if missing); fix sqlLine.group to cast inputs to string before performing string ops; getKeyFloat now returns a float value which is safe with sqlLine.group, but can be used in a math expression; use python math. operators as needed (sqrt, fabs); fulltest.sh runs for camera, stack, object stages; update dvoSkyTable to use REGION_ID instead of special word INDEX; add existsClient method (avoid using mysql failure as a test for table existence); no need to test for exceptions to sql.execute since we abort on failure; replace esoteric SQL with dropTable calls; trap errors in stilts.treads in setupScratchdb; drop StackObjecThin and StackDetOffMeta along with other stack scratch tables

Location:
trunk/ippToPsps/jython
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/jython/batch.py

    r38602 r38837  
    234234
    235235    '''
    236     Returns the string keyword value from this header or else "NULL"
     236    Returns the float keyword value from this header or else "NULL"
    237237    '''
    238238    def getKeyFloat(self, header, format, key):
    239239
    240240         if key in header:
    241              value = format % float(header[key])
     241             value = float(format % float(header[key]))
    242242         else:
    243243             self.logger.errorPair("Missing header field", key)
    244              value = format % -999.9
     244             value = float(format % -999.9)
    245245
    246246         return value
     
    414414    '''   
    415415    def createEmptyPspsTables(self):
    416          print "THIS SUCKS"
    417          self.pspsTables = stilts.treads(self.pspsVoTableFilePath)
    418          for table in self.pspsTables:
    419              self.logger.debug("Creating PSPS table: " + table.name)
    420              self.logger.infoPair("creating psps table ",table.name)
    421              table.write(self.scratchDb.url + '#' + table.name)
    422              self.tablesToExport.append(table.name)
    423 
    424          return True
     416        self.pspsTables = stilts.treads(self.pspsVoTableFilePath)
     417        for table in self.pspsTables:
     418            self.logger.debug("Creating PSPS table: " + table.name)
     419            self.logger.infoPair("creating psps table ",table.name)
     420            table.write(self.scratchDb.url + '#' + table.name)
     421            self.tablesToExport.append(table.name)
     422
     423        return True
    425424
    426425    '''
  • trunk/ippToPsps/jython/detectionbatch.py

    r38810 r38837  
    33import os.path
    44import sys
     5import math
    56import glob
    67import time
     
    184185    def populateImageMetaTable(self, ota, header):
    185186
     187        # the supplied 'header' matches this chip
     188        # self.header is the PHU header for this smf
     189
    186190        tableName = "ImageMeta_" + ota
    187191       
    188         # we drop the table before calling this functoin so it is not left behind on failure
     192        # we drop the table before calling this function so it is not left behind on failure
    189193        sql = "CREATE TABLE " + tableName + " LIKE ImageMeta"
    190         try: self.scratchDb.execute(sql)
    191         except: pass
     194        self.scratchDb.execute(sql)
    192195
    193196        if (ota[0:2] == "XY"): ccdID = ota[2:4]
     
    200203        psfFwhmMajor = pltscale * self.getKeyFloat(header, "%.8f", 'FWHM_MAJ')
    201204        psfFwhmMinor = pltscale * self.getKeyFloat(header, "%.8f", 'FWHM_MIN')
    202         psfFwhm = 0.5*(float(psfFwhmMajor) + float(psfFwhmMinor))
     205        psfFWHM = 0.5*(psfFwhmMajor + psfFwhmMinor)
    203206
    204207        psfmodel_name = self.getKeyValue(header, 'PSFMODEL')
     
    207210        ast_cdx = self.getKeyFloat(header, "%.8f",'AST_CDX')
    208211        ast_cdy = self.getKeyFloat(header, "%.8f",'AST_CDY')
    209         astroscat = sqrt(ast_cdx^2 + ast_cdy^2)
     212        astroscat = math.sqrt(ast_cdx**2 + ast_cdy**2)
     213       
     214        # Convert detectionThreshold to appropriate magnitudes
     215        detectionThreshold = self.getKeyFloat(header, "%.8f", 'DETEFF.MAGREF')
     216        expTime = self.getKeyFloat(header, "%.8f", 'EXPTIME')
     217        zp      = self.getKeyFloat(header, "%.8f", 'ZPT_OBS')
     218
     219        # XXX zp correction should come from DVO
     220        detectionThreshold = detectionThreshold + zp - 2.5 * math.log10(expTime)
    210221       
    211222        # insert image metadata into table
    212223        sqlLine = sqlUtility("INSERT INTO " + tableName + "(")
    213224
    214         ## extract ZPT_OBS and define the zpFactor, apply to sky, skyScat?
    215 
    216         sqlLine.group("frameID",          str(self.expID))
    217         sqlLine.group("ccdID",            str(ccdID))
    218         sqlLine.group("bias",             str(self.bias))
    219         sqlLine.group("biasScat",         str(self.biasScat))
    220         sqlLine.group("sky",              self.getKeyFloat(header, "%.8f", 'MSKY_MN'))
    221         sqlLine.group("skyScat",          self.getKeyFloat(header, "%.8f", 'MSKY_SIG'))
    222         sqlLine.group("completMag",       self.getKeyFloat(header, "%.8f", 'FLIMIT'))
    223         sqlLine.group("astroScat",        astroscat)
    224         sqlLine.group("photoScat",        self.getKeyFloat(self.header, "%.8f", 'ZPT_ERR'))
    225         sqlLine.group("numAstroRef",      self.getKeyValue(header, 'NASTRO'))
    226         sqlLine.group("numPhotoRef",      self.getKeyValue(header, 'NASTRO'))
    227         sqlLine.group("nAxis1",           self.getKeyInt(header, 0, 'NAXIS1'))
    228         sqlLine.group("nAxis2",           self.getKeyInt(header, 0, 'NAXIS2'))
    229         sqlLine.group("psfModelID"        psfmodelID)
    230         sqlLine.group("psfFwhm",          str(psfFwhm))
    231         sqlLine.group("psfWidMajor",      psfFwhmMajor)
    232         sqlLine.group("psfWidMinor",      psfFwhmMinor)
    233         sqlLine.group("psfTheta",         self.getKeyFloat(header, "%.8f", 'ANGLE'))
    234         sqlLine.group("momentMajor",      pltscale * self.getKeyFloat(header, "%.8f", 'IQ_FW1'))
    235         sqlLine.group("momentMinor",      pltscale * self.getKeyFloat(header, "%.8f", 'IQ_FW2'))
    236         sqlLine.group("momentM2C",        pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M2C'))
    237         sqlLine.group("momentM2S",        pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M2S'))
    238         sqlLine.group("momentM3",         pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M3'))
    239         sqlLine.group("momentM4",         pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M4'))
    240         sqlLine.group("apResid",          self.getKeyFloat(header, "%.8f", 'APMIFIT'))
    241         sqlLine.group("dapResid",         self.getKeyFloat(header, "%.8f", 'DAPMIFIT'))
    242         sqlLine.group("detectorID",       self.getKeyValue(header, 'DETECTOR'))
    243         sqlLine.group("qaFlags",          str(self.scratchDb.getDvoImageFlags(header['IMAGEID'])))
    244         sqlLine.group("detrend1",         self.getKeyValue(header, 'DETREND.MASK'))
    245         sqlLine.group("detrend2",         self.getKeyValue(header, 'DETREND.DARK'))
    246         sqlLine.group("detrend3",         self.getKeyValue(header, 'DETREND.FLAT'))
    247     #what happens if it can't find it?
    248         # CZW: I think this should properly trap the fringe
    249         if self.getKeyValue(header, 'DETREND.FRINGE') != "NULL":
     225        sqlLine.group("frameID",            self.expID)
     226        sqlLine.group("ccdID",              ccdID)
     227        sqlLine.group("bias",               self.bias)
     228        sqlLine.group("biasScat",           self.biasScat)
     229        sqlLine.group("sky",                self.getKeyFloat(header, "%.8f", 'MSKY_MN'))
     230        sqlLine.group("skyScat",            self.getKeyFloat(header, "%.8f", 'MSKY_SIG'))
     231        sqlLine.group("detectionThreshold", detectionThreshold)
     232        sqlLine.group("astroScat",          astroscat)
     233        sqlLine.group("photoScat",          self.getKeyFloat(header, "%.8f", 'ZPT_ERR'))
     234        sqlLine.group("photoZero",          zp)
     235        sqlLine.group("nAstroRef",          self.getKeyValue(header, 'NASTRO'))
     236        sqlLine.group("nPhotoRef",          self.getKeyValue(header, 'NASTRO'))
     237        sqlLine.group("nAxis1",             self.getKeyInt(header, 0, 'NAXIS1'))
     238        sqlLine.group("nAxis2",             self.getKeyInt(header, 0, 'NAXIS2'))
     239        sqlLine.group("psfModelID",         psfmodelID)
     240        sqlLine.group("psfFWHM",            psfFWHM)
     241        sqlLine.group("psfWidMajor",        psfFwhmMajor)
     242        sqlLine.group("psfWidMinor",        psfFwhmMinor)
     243        sqlLine.group("psfTheta",           self.getKeyFloat(header, "%.8f", 'ANGLE'))
     244        sqlLine.group("momentMajor",        pltscale * self.getKeyFloat(header, "%.8f", 'IQ_FW1'))
     245        sqlLine.group("momentMinor",        pltscale * self.getKeyFloat(header, "%.8f", 'IQ_FW2'))
     246        sqlLine.group("momentM2C",          pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M2C'))
     247        sqlLine.group("momentM2S",          pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M2S'))
     248        sqlLine.group("momentM3",           pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M3'))
     249        sqlLine.group("momentM4",           pltscale2 * self.getKeyFloat(header, "%.8f", 'IQ_M4'))
     250        sqlLine.group("apResid",            self.getKeyFloat(header, "%.8f", 'APMIFIT'))
     251        sqlLine.group("dapResid",           self.getKeyFloat(header, "%.8f", 'DAPMIFIT'))
     252        sqlLine.group("detectorID",         self.getKeyValue(header, 'DETECTOR'))
     253        sqlLine.group("qaFlags",            self.scratchDb.getDvoImageFlags(header['IMAGEID']))
     254        sqlLine.group("detrend1",           self.getKeyValue(header, 'DETREND.MASK'))
     255        sqlLine.group("detrend2",           self.getKeyValue(header, 'DETREND.DARK'))
     256        sqlLine.group("detrend3",           self.getKeyValue(header, 'DETREND.FLAT'))
     257
     258        # DETREND.FRINGE is not required : check if it is in the header before setting
     259        if 'DETREND.FRINGE' in header:
    250260            sqlLine.group("detrend4",         self.getKeyValue(header, 'DETREND.FRINGE'))
    251261
     
    256266        # sqlLine.group("detrend8",         " ")
    257267
    258         sqlLine.group("photoZero",        self.getKeyFloat(self.header, "%.8f", 'ZPT_OBS'))
    259268        sqlLine.group("ctype1",           self.getKeyValue(header, 'CTYPE1'))
    260269        sqlLine.group("ctype2",           self.getKeyValue(header, 'CTYPE2'))
     
    307316
    308317        sql = sqlLine.make(") VALUES ( ", ")")
    309 
    310         try: self.scratchDb.execute(sql)
    311         except:
    312             self.logger.errorPair('failed sql: ', sql)
    313             raise
     318        self.scratchDb.execute(sql)
    314319
    315320        self.scratchDb.updateFilterID(tableName, self.filter)
    316321        self.scratchDb.updateAllRows(tableName, "processingVersion", str(self.skychunk.processingVersion))
     322
    317323        if 'NASTRO' in header: self.totalNumPhotoRef = self.totalNumPhotoRef + int(header['NASTRO'])
    318324        self.scratchDb.replaceNullsInThisColumn(tableName, "polyOrder", "0")
     
    428434        # instrumental fluxes in counts/sec and calibrated sizes (arcsec).
    429435        # below, we convert instrumental fluxes to Jy
     436
     437        # NOTE: math operations below take place in SQL, not JYTHON.  thus we use (e.g.)
     438        # abs() not math.fabs()
    430439
    431440        sqlLine.group("ippDetectID",      "IPP_IDET")                                               
     
    644653                   
    645654                    ota = "XY%d%d" % (x, y)
     655                    if ota not in self.imageIDs: continue
    646656               
    647657                    # I need better control over this..
     
    682692                if x==7 and y==7: continue
    683693
    684                 extension = "XY%d%d_psf" % (x, y)
     694                ota = "XY%d%d" % (x, y)
     695                if ota not in self.imageIDs: continue
     696
     697                extension = ota + "_psf"
    685698                self.scratchDb.createIndex(extension, "IPP_IDET")
     699
    686700        # try the test Chip
    687701        self.scratchDb.createIndex("Chip_psf", "IPP_IDET")
     
    698712
    699713        # XXX : EAM 20150925 : is this IGNORE necessary?
    700         sql = "UPDATE IGNORE " + table + " AS a, " + self.scratchDb.dvoDetectionTable + " AS b SET \
    701                a.objID        = b.objID, \
    702                a.detectID     = b.detectID, \
    703                a.ippObjID     = b.ippObjID, \
    704                a.dvoRegionID  = b.catID, \
    705                a.ra           = b.ra, \
    706                a.dec          = b.dec_, \
    707                a.zp           = b.zp, \
    708                a.telluricExt  = b.telluricExt, \
    709                a.airmass      = b.airmass, \
    710                a.infoFlag3    = b.flags, \
    711                a.expTime      = b.expTime, \
    712                a.psfFlux      = a.psfFlux     * b.zpFactor, \
    713                a.psfFluxErr   = a.psfFluxErr  * b.zpFactor, \
    714                a.apFlux       = a.apFlux      * b.zpFactor, \
    715                a.apFluxErr    = a.apFluxErr   * b.zpFactor, \
    716                a.kronFlux     = a.kronFlux    * b.zpFactor, \
    717                a.kronFluxErr  = a.kronFluxErr * b.zpFactor  \
    718                a.sky          = a.sky         * b.zpFactor, \
    719                a.skyErr       = a.skyErr      * b.zpFactor  \
    720                WHERE a.ippDetectID = b.ippDetectID \
    721                AND b.imageID = " + str(imageID)
    722 
    723                ## a.psfFlux      = a.psfFlux * 3630.78 * POW(10.0, -0.4*b.zp), \
    724                ## a.psfFluxErr   = a.psfFluxErr * 3630.78 * POW(10.0, -0.4*b.zp), \
    725                ## a.apFlux       = a.apFlux * 3630.78 * POW(10.0, -0.4*b.zp), \
    726                ## a.apFluxErr    = a.apFluxErr * 3630.78 * POW(10.0, -0.4*b.zp), \
    727                ## a.kronFlux     = a.kronFlux * 3630.78 * POW(10.0, -0.4*b.zp), \
    728                ## a.kronFluxErr  = a.kronFluxErr * 3630.78 * POW(10.0, -0.4*b.zp) \
     714        sqlLine = sqlUtility("UPDATE IGNORE " + table + " AS a, " + self.scratchDb.dvoDetectionTable + " AS b SET")
     715        sqlLine.group("a.objID",        "b.objID")
     716        sqlLine.group("a.detectID",     "b.detectID")
     717        sqlLine.group("a.ippObjID",     "b.ippObjID")
     718        sqlLine.group("a.dvoRegionID",  "b.catID")
     719        sqlLine.group("a.ra",           "b.ra")
     720        sqlLine.group("a.dec",          "b.dec_")
     721        sqlLine.group("a.zp",           "b.zp")
     722        sqlLine.group("a.telluricExt",  "b.telluricExt")
     723        sqlLine.group("a.airmass",      "b.airmass")
     724        sqlLine.group("a.infoFlag3",    "b.flags")
     725        sqlLine.group("a.expTime",      "b.expTime")
     726        sqlLine.group("a.psfFlux",      "a.psfFlux     * b.zpFactor")
     727        sqlLine.group("a.psfFluxErr",   "a.psfFluxErr  * b.zpFactor")
     728        sqlLine.group("a.apFlux",       "a.apFlux      * b.zpFactor")
     729        sqlLine.group("a.apFluxErr",    "a.apFluxErr   * b.zpFactor")
     730        sqlLine.group("a.kronFlux",     "a.kronFlux    * b.zpFactor")
     731        sqlLine.group("a.kronFluxErr",  "a.kronFluxErr * b.zpFactor")
     732        sqlLine.group("a.sky",          "a.sky         * b.zpFactor")
     733        sqlLine.group("a.skyErr",       "a.skyErr      * b.zpFactor")
     734
     735        sql = sqlLine.makeEquals("WHERE a.ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
     736
     737        ## a.psfFlux      = a.psfFlux * 3630.78 * POW(10.0, -0.4*b.zp), \
    729738
    730739        # instrumental flux vs Janskies:
     
    741750        # NOTE: update dvopsps to populate zpFactor = 3630.78 * ten(-0.4*zp)
    742751
    743         print sql
    744         try: self.scratchDb.execute(sql)
    745         except:
    746             self.logger.infoPair("failed sql",sql)
    747             raise
     752        self.scratchDb.execute(sql)
     753
    748754    '''
    749755    Updates table and generates pspsuniqueids
     
    929935
    930936        # update FrameMeta with count OTAs in this file and total number of photometric reference sources
    931         sql = "UPDATE FrameMeta SET nOTA = %d, numPhotoRef = %d" % (otaCount, self.totalNumPhotoRef)
     937        sql = "UPDATE FrameMeta SET nOTA = %d, nPhotoRef = %d" % (otaCount, self.totalNumPhotoRef)
    932938        self.scratchDb.execute(sql)
    933939        #to make it stop
     
    939945    def removeDuplicateObjects(self):
    940946
    941         sql = "DROP TABLE SkinnyObject_All"
    942         try:
    943             self.scratchDb.execute(sql)
    944         except:
    945             print "SkinnyObject_All table not yet defined"
     947        self.scratchDb.dropTable("SkinnyObject_All")
    946948
    947949        sql = "CREATE TABLE SkinnyObject_All (objID bigint, ippObjID bigint, chipID char(16))"
    948         try:
    949             self.scratchDb.execute(sql)
    950         except:
    951             print "failed to create table SkinnyObject_All"
    952             raise
     950        self.scratchDb.execute(sql)
    953951
    954952        for chipname in self.validChips:
     
    956954            sql = "INSERT INTO SkinnyObject_All (objID, ippObjID, chipID) \
    957955                   SELECT objID, ippObjID, '" + chipname + "' from SkinnyObject_" + chipname
    958             try:
    959                 self.scratchDb.execute(sql)
    960             except:
    961                 print "failed to insert entry SkinnyObject_All: ", chipname
    962                 raise
    963            
     956            self.scratchDb.execute(sql)
    964957
    965958        Nduplicates = 0
  • trunk/ippToPsps/jython/dvo.py

    r38365 r38837  
    167167            return False
    168168
    169         self.logger.infoSeparator()
    170         self.logger.infoPair("DVO SkyTable.fits file", "NOT up-to-date")       
    171 
    172         # drop dvoSkyTable (if it exists)
    173         sql = "drop TABLE dvoSkyTable"
    174         try: self.scratchDb.execute(sql)
    175         except: pass
     169        # self.logger.infoSeparator()
     170        # self.logger.infoPair("DVO SkyTable.fits file", "NOT up-to-date")       
     171        # print "--- go"
     172
     173        self.scratchDb.dropTable("dvoSkyTable")
    176174
    177175        # create dvoSkyTable
    178         sql = "CREATE TABLE dvoSkyTable (R_MIN REAL, R_MAX REAL, D_MIN REAL, D_MAX REAL, INDEX_ INT, NAME CHAR(18))"
     176        sql = "CREATE TABLE dvoSkyTable (R_MIN REAL, R_MAX REAL, D_MIN REAL, D_MAX REAL, REGION_ID INT, NAME CHAR(18))"
    179177        self.scratchDb.execute(sql)
    180178
     
    196194        self.logger.infoPair("Adding index to", self.scratchDb.dvoSkyTable)
    197195
    198         self.scratchDb.createIndex(self.scratchDb.dvoSkyTable, "INDEX")
     196        self.scratchDb.createIndex(self.scratchDb.dvoSkyTable, "REGION_ID")
    199197       
    200198        self.scratchDb.setImportedThisDvoTable(path)
     
    580578        self.ippToPspsDb.clearIngestedBoxes(self.scratchDb.dbHost)
    581579       
    582         if True:
    583             print "*******************************************************************************"
     580        if False:
    584581            self.logger.warning("about to delete detection table and re-ingest; is this correct?")
    585             #response = raw_input("(y/n) ")
    586             #if response != "y":
    587             #    raise
    588             #self.logger.warning("Are you ABSOLUTELY sure you want to do this?")
    589             #response = raw_input("(y/n) ")
    590             #if response != "y":
    591             #    raise
     582            response = raw_input("(y/n) ")
     583            if response != "y":
     584                raise
     585            self.logger.warning("Are you ABSOLUTELY sure you want to do this?")
     586            response = raw_input("(y/n) ")
     587            if response != "y":
     588                raise
    592589
    593590        # blow away existing dvoDetection table & re-crate
  • trunk/ippToPsps/jython/forcedwarpbatch.py

    r38827 r38837  
    404404        imageID = self.scratchDb.getImageIDFromExternID(self.warpSkyFileID[num])
    405405        self.logger.infoPair("updating","forcedWarpMeasurement_"+forcedWarpID)
     406
    406407        sqlLine = sqlUtility("UPDATE ForcedWarpMeasurement_"+forcedWarpID+" as a, " + self.scratchDb.dvoDetectionTable + " as b SET ")
    407408        sqlLine.group("a.objID",      "b.objID")
     
    414415        sqlLine.group("a.FinfoFlag3", "b.flags")
    415416
    416         sqlLine.group("a.FpsfFlux",    "a.FpsfFlux     * b.zpFactor")
    417         sqlLine.group("a.FpsfFluxErr", "a.FpsfFluxErr  * b.zpFactor")
    418         sqlLine.group("a.FapFlux",     "a.FapFlux      * b.zpFactor")
    419         sqlLine.group("a.FapFluxErr",  "a.FapFluxErr   * b.zpFactor")
    420         sqlLine.group("a.FkronFlux",   "a.FkronFlux    * b.zpFactor")
    421         sqlLine.group("a.FkronFluxErr","a.FkronFluxErr * b.zpFactor")
    422         sqlLine.group("a.Fsky",        "a.Fsky         * b.zpFactor")
    423         sqlLine.group("a.FskyErr",     "a.FskyErr      * b.zpFactor")
     417        sqlLine.group("a.FpsfFlux",     "a.FpsfFlux     * b.zpFactor")
     418        sqlLine.group("a.FpsfFluxErr",  "a.FpsfFluxErr  * b.zpFactor")
     419        sqlLine.group("a.FapFlux",      "a.FapFlux      * b.zpFactor")
     420        sqlLine.group("a.FapFluxErr",   "a.FapFluxErr   * b.zpFactor")
     421        sqlLine.group("a.FkronFlux",    "a.FkronFlux    * b.zpFactor")
     422        sqlLine.group("a.FkronFluxErr", "a.FkronFluxErr * b.zpFactor")
     423        sqlLine.group("a.Fsky",         "a.Fsky         * b.zpFactor")
     424        sqlLine.group("a.FskyErr",      "a.FskyErr      * b.zpFactor")
    424425
    425426        sql = sqlLine.makeEquals("WHERE a.ippDetectID = b.ippDetectID AND b.imageID = " +str( imageID))
  • trunk/ippToPsps/jython/ipptopspsdb.py

    r38763 r38837  
    914914
    915915    '''
     916    Returns boolean value for this column in clients table
     917    '''
     918    def existsClient(self, host, pid):
     919
     920        sql = "SELECT id FROM clients WHERE host = '" + host + "' AND pid = " + str(pid) + " LIMIT 1"
     921
     922        try:
     923            rs = self.executeQuery(sql)
     924            if rs.first() is False: return False
     925        except:
     926            self.logger.exception("Unable to check if " + host + "-" + str(pid) + " is in clients table")
     927
     928        return True
     929
     930    '''
    916931    Update clients, or inserts it for the first time
    917932    '''
    918933    def updateClient(self, type, host, pid):
    919934
    920         try:
     935        if self.existsClient(host, pid):
     936            self.execute("UPDATE clients SET timestamp = now() WHERE host = '" + host + "' AND pid = " + str(pid))
     937        else:
    921938            self.insertClient(type, host, pid)
    922         except:
    923             self.execute("UPDATE clients SET timestamp = now() WHERE host = '" + host + "' AND pid = " + str(pid))
    924939
    925940    '''
     
    13371352
    13381353        for id in ids:
    1339             sql = "INSERT IGNORE INTO pending \
     1354            # why is there an IGNORE here? we have deleted old versions already above
     1355            sql = "INSERT INTO pending \
    13401356               (box_id, batch_type, stage_id) \
    13411357               VALUES \
    13421358               (" + str(box_id) + ", '" + batchType + "', " + str(id) + ")"
    13431359
    1344             print sql
    1345             try: self.execute(sql)
    1346             except:
    1347                 print "failed to insert into pending"
    1348                 print "sql: " + sql
    1349                 raise
     1360            self.execute(sql)
    13501361
    13511362    '''
     
    16561667    def storeAllItems(self, rows):
    16571668
    1658         try:
    1659             self.execute("DROP TABLE all_pending")
    1660         except:
    1661             pass
     1669        self.dropTable("all_pending")
    16621670
    16631671        self.execute("CREATE TEMPORARY TABLE all_pending (stage_id bigint(20), ra_bore float, dec_bore float)")
    16641672        for row in rows:
    1665 
    16661673            try:
    16671674                sql = "INSERT INTO all_pending (stage_id, ra_bore, dec_bore) \
     
    16781685    '''
    16791686    def storeAllItemsInDvodb(self, rows, dvo_db, batchType):
    1680 
    1681         #try:
    1682         #    self.execute("DROP TABLE all_pending")
    1683         #except:
    1684         #    pass
    16851687
    16861688        #self.execute("CREATE TEMPORARY TABLE all_pending (stage_id bigint(20), ra_bore float, dec_bore float)")
     
    16991701        #self.logger.infoPair("Items written to Db", "%d" % count)
    17001702
    1701 
    1702 
    17031703    '''
    17041704    Gets all items in the all_pending temporary table within the bounds of this box
  • trunk/ippToPsps/jython/mysql.py

    r37917 r38837  
    230230    def dropTable(self, table):
    231231
    232         sql = "DROP TABLE " + table
     232        sql = "DROP TABLE IF EXISTS " + table
    233233        try: self.execute(sql)
    234234        except: return
     
    334334
    335335        stmt = self.con.createStatement()
    336         stmt.execute(sql)
     336
     337        try: stmt.execute(sql)
     338        except Exception, e:
     339            print "--- Error calling mysql: "
     340            print "--- sql: " + sql
     341            print "--- " + str(e)
     342            os._exit(2)
    337343        stmt.close()
    338344
  • trunk/ippToPsps/jython/scratchdb.py

    r38823 r38837  
    475475        # drop and create Images table
    476476        self.logger.infoPair("Creating DVO table", dvoImagesTable)
    477         sql = "DROP TABLE " + dvoImagesTable
    478 
    479         try: self.execute(sql)
    480         except: pass
    481        
     477        self.dropTable(dvoImagesTable)
     478
    482479        sql = "CREATE TABLE " + dvoImagesTable + " ( \
    483480               SOURCE_ID SMALLINT, \
     
    495492        # now detection table
    496493        self.logger.infoPair("Dropping DVO table", dvoDetectionTable)
    497         sql = "DROP TABLE " + dvoDetectionTable + " IF EXISTS"
    498         try: self.execute(sql)
    499         except:
    500             self.logger.errorPair("problem dropping DVO table", dvoDetectionTable)
    501             pass
     494        self.dropTable(dvoDetectionTable)
    502495
    503496        self.logger.infoPair("Creating DVO table", "dvoDetection")
     
    514507               decErr FLOAT, \
    515508               zp FLOAT, \
     509               zpFactor FLOAT, \
    516510               telluricExt FLOAT, \
    517511               airmass FLOAT, \
     
    554548
    555549               tableName = self.getDbFriendlyTableName(region + "." + fileType)
    556                sql = "DROP TABLE " + tableName
    557                try: self.execute(sql)
    558                except:
    559                     self.logger.errorPair("Unable to drop table", tableName)
    560                     pass
     550               self.dropTable(tableName)
    561551
    562552        # deletedDetectionCount = detectionCount - self.getRowCount(self.dvoDetectionTable)
     
    583573
    584574       sql = "set @@max_heap_table_size = " + str(tableSize)
    585        try: self.execute(sql)
    586        except:
    587            self.logger.errorPair("Unable to set max MEMORY table size for ", self.dvoDetectionTable)
    588            return False
     575       self.execute(sql)
    589576
    590577       # create detections table
    591578       self.logger.infoPair("Creating table", self.dvoDetectionTable)
    592579       sql = "CREATE TABLE " + self.dvoDetectionTable + " LIKE dvoDetection"
    593        try: self.execute(sql)
    594        except:
    595            self.logger.errorPair("Unable to create table", self.dvoDetectionTable)
    596            return False
     580       self.execute(sql)
    597581
    598582       # XXX changing the dvoDetectionFull table to MEMORY
     
    660644
    661645        sql = "SELECT \
    662                INDEX_, \
     646               REGION_ID, \
    663647               R_MIN+ (R_MAX - R_MIN)/2.0, \
    664648               D_MIN + (D_MAX - D_MIN)/2.0, \
     
    689673
    690674        region = ""
    691         sql = "SELECT NAME FROM " + self.dvoSkyTable + " WHERE INDEX_ = " + str(index)
     675        sql = "SELECT NAME FROM " + self.dvoSkyTable + " WHERE REGION_ID = " + str(index)
    692676        try:
    693677            rs = self.executeQuery(sql)
  • trunk/ippToPsps/jython/setupScratchDb.py

    r35178 r38837  
    8888        self.logger.infoPair("Installing", "initialization tables")
    8989        tablepath = self.config.configDir + "tables.IN.vot"
    90         tables = stilts.treads(tablepath)
     90        try:
     91            tables = stilts.treads(tablepath)
     92        except Exception, e:
     93            print "--- problem reading tables.IN.vot"
     94            print "--- " + str(e)
     95            os._exit(3)
    9196
    9297        for table in tables:
     
    9499            self.logger.infoPair("Creating IN table: ", table.name)
    95100            self.logger.debug("Creating IN table: " + table.name)
    96             table.write(self.scratchDb.url + '#' + table.name)
     101            try: table.write(self.scratchDb.url + '#' + table.name)
     102            except Exception, e: print "--- " + str(e)
    97103
    98104        # create basic DVO tables
  • trunk/ippToPsps/jython/sqlUtility.py

    r37246 r38837  
    1212        self.fields = []
    1313        self.values = []
    14         self.startString = startString
     14        self.startString = str(startString)
    1515        return
    1616   
    1717    def group(self,field,value):
    18         self.fields.append(field)
    19         self.values.append(value)
     18        self.fields.append(str(field))
     19        self.values.append(str(value))
     20        print "* " + str(field) + " = " + str(value)
    2021        return
    2122
     
    3031                output += ", "
    3132
    32         output += " " + middleString + "  "
     33        output += " " + str(middleString) + "  "
    3334
    3435        for i in range(len(self.values)):
     
    3738                output += ", "
    3839       
    39         output += " " + endString
     40        output += " " + str(endString)
    4041
    4142        return output
     
    5152                output += ", "
    5253
    53         output += " " + middleString + " "
     54        output += " " + str(middleString) + " "
    5455
    5556        for i in range(len(self.values)):
     
    5859                output += ", "
    5960       
    60         output += " " + endString
     61        output += " " + str(endString)
    6162
    6263        return output
     
    7273                output += ", "
    7374       
    74         output += " " + endString
     75        output += " " + str(endString)
    7576
    7677        return output
  • trunk/ippToPsps/jython/stackbatch.py

    r38826 r38837  
    160160           self.dropTableVerbose(tableName)
    161161
     162       self.dropTableVerbose("StackMeta")
    162163       self.dropTableVerbose("StackObjectThin")
    163164       self.dropTableVerbose("StackObjectAttributes")
     
    176177
    177178       self.dropTableVerbose("StackToImage")
     179       self.dropTableVerbose("StackDetEffMeta")
    178180
    179181       # delete IPP tables
     
    217219
    218220        header = self.headerSet[filter]
    219         tablename = filter + "StackMeta"
     221
    220222        stackID = self.stackIDs[filter]
    221223        filterName = filter + ".00000"
    222224
    223         self.logger.infoPair("Populating table", tablename)
    224 
    225         fwhm_maj    = self.getKeyFloat(header, "%.8f", 'FWHM_MAJ')
    226         fwhm_maj_uq = self.getKeyFloat(header, "%.8f", 'FW_MJ_UQ')
    227         psfmodel_name    = self.getKeyValue(header, 'PSFMODEL')
    228         psfmodelID  = self.scratchDb.getFitModelID(psfmodel_name)
    229 
    230         ast_cdx = self.getKeyFloat(header, "%.8f",'AST_CDX')
    231         ast_cdy = self.getKeyFloat(header, "%.8f",'AST_CDY')
    232         astroscat = sqrt(ast_cdx^2 + ast_cdy^2)
    233        
    234225        # make a table
    235226        filterID = self.scratchDb.getFilterID(filter)
     
    238229        photoCalID = str(self.scratchDb.getPhotoCalID(stackID))
    239230
     231        tablename = filter + "StackMeta"
     232
     233        sql = "CREATE TABLE " + tablename + " LIKE StackMeta"
     234        self.scratchDb.execute(sql)
     235
     236        self.logger.infoPair("Populating table", tablename)
     237
     238        # fwhm_maj    = self.getKeyFloat(header, "%.8f", 'FWHM_MAJ')
     239        # fwhm_maj_uq = self.getKeyFloat(header, "%.8f", 'FW_MJ_UQ')
     240
     241        # XXX hard-wired platescale : 0.25
     242        pltscale = 0.25
     243        pltscale2 = pltscale * pltscale
     244
     245        psfFwhmMajor = pltscale * self.getKeyFloat(header, "%.8f", 'FWHM_MAJ')
     246        psfFwhmMinor = pltscale * self.getKeyFloat(header, "%.8f", 'FWHM_MIN')
     247        psfFWHM = 0.5*(psfFwhmMajor + psfFwhmMinor)
     248
     249        psfmodel_name    = self.getKeyValue(header, 'PSFMODEL')
     250        psfmodelID  = self.scratchDb.getFitModelID(psfmodel_name)
     251
     252        ast_cdx = self.getKeyFloat(header, "%.8f",'AST_CDX')
     253        ast_cdy = self.getKeyFloat(header, "%.8f",'AST_CDY')
     254        astroscat = math.sqrt(ast_cdx**2 + ast_cdy**2)
     255       
    240256        # Convert detectionThreshold to appropriate magnitudes
    241257        detectionThreshold = self.getKeyFloat(header, "%.8f", 'DETEFF.MAGREF')
    242258        expTime = self.getKeyFloat(header, "%.8f", 'EXPTIME')
    243         zp      = self.getKeyFloat(header, "%.8f", 'FPA.ZP')
    244         # CZW check?
     259        zp      = self.getKeyFloat(header, "%.8f", 'ZPT_OBS')
     260
     261        # XXX zp correction should come from DVO
    245262        detectionThreshold = detectionThreshold + zp - 2.5 * math.log10(expTime)
    246263       
    247        
    248         # mysql is sensitive to values which are ambiugously float.  eg
    249         # a warning is raised if we try to insert '25.' into a float field.
    250         # use getKeyFloat(hdr, format, key) to avoid this problem
    251 
    252         sql = "CREATE TABLE " + tablename + " LIKE StackMeta"
    253         try: self.scratchDb.execute(sql)
    254         except: pass
    255 
     264        # insert stack metadata into table
    256265        sqlLine = sqlUtility("INSERT INTO " + tablename + " (")
    257266
    258         sqlLine.group("stackImageID",  str(stackID))       
    259         sqlLine.group("batchID",       str(self.batchID))
    260         sqlLine.group("surveyID",      str(self.surveyID))
    261         sqlLine.group("filterID",      str(filterID))           
    262         sqlLine.group("stackTypeID",   str(self.stackTypeID))
    263         sqlLine.group("tessID",        str(self.tessID))           
    264         sqlLine.group("projectionID",  str(self.projectionID))           
    265         sqlLine.group("skyCellID",     str(self.skycellID))           
    266         sqlLine.group("photoCalID",    photoCalID)
    267         sqlLine.group("analysisVer",   str(self.analysisVer))
    268         sqlLine.group("detectionThreshold",       detectionThreshold)
    269         sqlLine.group("expTime",       self.getKeyFloat(header, "%.5f", 'EXPTIME')) 
    270         sqlLine.group("psfModelID",    psfmodelID)           
    271         sqlLine.group("psfFwhm_mean",  fwhm_maj)     
    272         sqlLine.group("psfFwhm_max",   fwhm_maj_uq)
    273         sqlLine.group("astroScat", astroscat)
    274         sqlLine.group("photoZero",     self.getKeyFloat(header, "%.5f", 'FPA.ZP'))
    275         sqlLine.group("ctype1",        header['CTYPE1']) 
    276         sqlLine.group("ctype2",        header['CTYPE2']) 
    277         sqlLine.group("crval1",        self.getKeyFloat(header, "%.8f", 'CRVAL1'))   
    278         sqlLine.group("crval2",        self.getKeyFloat(header, "%.8f", 'CRVAL2'))   
    279         sqlLine.group("crpix1",        self.getKeyFloat(header, "%.8f", 'CRPIX1'))   
    280         sqlLine.group("crpix2",        self.getKeyFloat(header, "%.8f", 'CRPIX2'))   
    281         sqlLine.group("cdelt1",        self.getKeyFloat(header, "%.8e", 'CDELT1'))   
    282         sqlLine.group("cdelt2",        self.getKeyFloat(header, "%.8e", 'CDELT2'))   
    283         sqlLine.group("pc001001",      self.getKeyFloat(header, "%.8e", 'PC001001'))
    284         sqlLine.group("pc001002",      self.getKeyFloat(header, "%.8e", 'PC001002'))
    285         sqlLine.group("pc002001",      self.getKeyFloat(header, "%.8e", 'PC002001'))
    286         sqlLine.group("pc002002",      self.getKeyFloat(header, "%.8e", 'PC002002'))
    287         sqlLine.group("processingVersion",   str(self.skychunk.processingVersion))
     267        sqlLine.group("stackImageID",       str(stackID))       
     268        sqlLine.group("batchID",            str(self.batchID))
     269        sqlLine.group("surveyID",           str(self.surveyID))
     270        sqlLine.group("filterID",           str(filterID))           
     271        sqlLine.group("stackTypeID",        str(self.stackTypeID))
     272        sqlLine.group("tessID",             str(self.tessID))           
     273        sqlLine.group("projectionID",       str(self.projectionID))           
     274        sqlLine.group("skyCellID",          str(self.skycellID))           
     275        sqlLine.group("photoCalID",         photoCalID)
     276        sqlLine.group("analysisVer",        str(self.analysisVer))
     277        sqlLine.group("detectionThreshold", detectionThreshold)
     278        sqlLine.group("expTime",            self.getKeyFloat(header, "%.5f", 'EXPTIME')) 
     279        sqlLine.group("psfModelID",         psfmodelID)           
     280        sqlLine.group("psfFWHM",            psfFWHM)
     281        sqlLine.group("psfWidMajor",        psfFwhmMajor)
     282        sqlLine.group("psfWidMinor",        psfFwhmMinor)
     283        sqlLine.group("psfTheta",           self.getKeyFloat(header, "%.8f", 'ANGLE'))
     284#       sqlLine.group("psfFwhm_mean",       fwhm_maj)     
     285#       sqlLine.group("psfFwhm_max",        fwhm_maj_uq)
     286        sqlLine.group("astroScat",          astroscat)
     287        sqlLine.group("photoZero",          zp)
     288        sqlLine.group("ctype1",             header['CTYPE1']) 
     289        sqlLine.group("ctype2",             header['CTYPE2']) 
     290        sqlLine.group("crval1",             self.getKeyFloat(header, "%.8f", 'CRVAL1'))   
     291        sqlLine.group("crval2",             self.getKeyFloat(header, "%.8f", 'CRVAL2'))   
     292        sqlLine.group("crpix1",             self.getKeyFloat(header, "%.8f", 'CRPIX1'))   
     293        sqlLine.group("crpix2",             self.getKeyFloat(header, "%.8f", 'CRPIX2'))   
     294        sqlLine.group("cdelt1",             self.getKeyFloat(header, "%.8e", 'CDELT1'))   
     295        sqlLine.group("cdelt2",             self.getKeyFloat(header, "%.8e", 'CDELT2'))   
     296        sqlLine.group("pc001001",           self.getKeyFloat(header, "%.8e", 'PC001001'))
     297        sqlLine.group("pc001002",           self.getKeyFloat(header, "%.8e", 'PC001002'))
     298        sqlLine.group("pc002001",           self.getKeyFloat(header, "%.8e", 'PC002001'))
     299        sqlLine.group("pc002002",           self.getKeyFloat(header, "%.8e", 'PC002002'))
     300        sqlLine.group("processingVersion",  str(self.skychunk.processingVersion))
    288301
    289302        sql = sqlLine.make(") VALUES ( ", ")")
     
    536549                continue
    537550
     551            cmfTable = filter + "SkyChip_xsrc"
     552            if not cmfTable in self.tablesLoaded:
     553                self.logger.infoPair("no xsrc data for filter" , filter)
     554                continue
     555
    538556            # insert all the detections
    539             sqlLine = sqlUtility("UPDATE " + tablename + " AS a , " + filter + "SkyChip_xsrc AS b SET")
     557            sqlLine = sqlUtility("UPDATE " + tablename + " AS a , " + cmfTable + " AS b SET")
    540558
    541559            sqlLine.group("a."+filter+"haveData",       "'1'")
     
    612630            return True
    613631
    614         if self.scratchDb.getRowCount(filter+"SkyChip_xfit") <=0 :
     632        cmfTable = filter + "SkyChip_xfit"
     633        if not cmfTable in self.tablesLoaded:
     634            self.logger.infoPair("no xfit data for filter" , filter)
     635            return True
     636
     637        if self.scratchDb.getRowCount(cmfTable) <=0 :
    615638            self.logger.infoPair("no extended source information, trying to skip",filter)
    616639            return True
     
    621644        magtime = "%.5f" % (2.5*math.log10(float(header['EXPTIME'])))
    622645
    623         sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + filter + "SkyChip_xfit AS b SET")
     646        sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + cmfTable + " AS b SET")
    624647
    625648        sqlLine.group("a."+filter+"haveData",              "'1'")
     
    775798            return True
    776799
    777         if self.scratchDb.getRowCount(filter+"SkyChip_xsrc") <=0 :
    778             self.logger.infoPair("no extended source information, trying to skip",filter)
     800        cmfTable = filter + "SkyChip_xsrc"
     801        if not cmfTable in self.tablesLoaded:
     802            self.logger.infoPair("no xsrc data for filter" , filter)
    779803            return True
    780804
     805        if self.scratchDb.getRowCount(cmfTable) <=0 :
     806            self.logger.infoPair("no extended source information, trying to skip", filter)
     807            return True
    781808
    782809        header  = self.headerSet[filter]
     
    790817
    791818        self.logger.infoPair("Adding", "petrosians for extended sources")
    792         sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + filter + "SkyChip_xsrc AS b SET")
     819        sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + cmfTable + " AS b SET")
    793820
    794821        sqlLine.group("a."+filter+"haveData",         "'1'")
     
    904931
    905932        # properly -999 these to start with.  the default should take
    906         # care of this, but does not
     933        # care of this, but does not?
    907934
    908935        sqlLine = sqlUtility("UPDATE " + tablename + " SET")
     
    925952        # set the flux values from the cmf file
    926953        cmfTable = filter + "SkyChip_xrad"
     954
     955        # skip this table if it was not loaded from the cmf
     956        if not cmfTable in self.tablesLoaded:
     957            self.logger.infoPair("no xrad data for filter" , filter)
     958            return True
    927959
    928960        # we have variable numbers of these columns. find out which are in use
     
    10571089                sql += " AND "
    10581090
    1059         self.logger.infoPair('sql',sql)   
    10601091        self.scratchDb.execute(sql)
    10611092
     
    10841115
    10851116            self.logger.infoPair("Creating indexes on", "IPP tables")
    1086             self.scratchDb.createIndex(filter + "SkyChip_psf",  "IPP_IDET")
    1087             self.scratchDb.createIndex(filter + "SkyChip_xfit", "IPP_IDET")
    1088             self.scratchDb.createIndex(filter + "SkyChip_xrad", "IPP_IDET")
    1089             self.scratchDb.createIndex(filter + "SkyChip_xsrc", "IPP_IDET")
     1117
     1118            cmfTable = filter + "SkyChip_psf"
     1119            if cmfTable in self.tablesLoaded: self.scratchDb.createIndex(cmfTable, "IPP_IDET")
     1120
     1121            cmfTable = filter + "SkyChip_xfit"
     1122            if cmfTable in self.tablesLoaded: self.scratchDb.createIndex(cmfTable, "IPP_IDET")
     1123
     1124            cmfTable = filter + "SkyChip_xrad"
     1125            if cmfTable in self.tablesLoaded: self.scratchDb.createIndex(cmfTable, "IPP_IDET")
     1126
     1127            cmfTable = filter + "SkyChip_xsrc"
     1128            if cmfTable in self.tablesLoaded: self.scratchDb.createIndex(cmfTable, "IPP_IDET")
    10901129
    10911130        return True
     
    13031342        self.logger.infoPair("Importing ST tables with table match expression: ", tableRE)
    13041343
     1344        self.tablesLoaded = []
     1345
    13051346        count = 0
    13061347        for filter in self.filters:
     
    13191360                return False
    13201361           
    1321             # count = 0
    13221362            for table in tables:
    13231363             
     
    13441384                    self.logger.exception("Problem writing table '" + filter + table.name + "' to the database")
    13451385                     
     1386                self.tablesLoaded.append(table.name)
     1387
     1388        # XXX validate required tables (do we need _psf?, _xsrc? etc)
     1389
    13461390        self.logger.infoPair("Done. Imported", "%d tables" % count)
    13471391        self.indexIppTables()
Note: See TracChangeset for help on using the changeset viewer.