IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.