IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 3, 2014, 2:41:20 PM (12 years ago)
Author:
heather
Message:

added diff / ff stages to addstar, partially to ipptopsps

File:
1 edited

Legend:

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

    r36697 r37551  
     1#!/usr/bin/env jython
     2
     3import os.path
     4import sys
     5
     6import stilts
     7from java.lang import *
     8from java.sql import *
     9
     10from xml.etree.ElementTree import ElementTree, Element, tostring
     11
     12from batch import Batch
     13from gpc1db import Gpc1Db
     14from ipptopspsdb import IppToPspsDb
     15from scratchdb import ScratchDb
     16from dvoforcedobjects import DvoForcedObjects # need one specific for cpy ?
     17from sqlUtility import sqlUtility
     18
     19import logging.config
     20
     21'''
     22ForcedObjectBatch class
     23
     24This class, a sub-class of Batch, processes individual DVO region file pairs (cpt, cpy, and cps files). The cpt file contains a list of all objects in that region, the cps file contains the magnitudes and so has a row per filter per object (so, generally, has rows = 5 x number of objects). The Photcode.dat table us used to figure out the number and order of filters as they appear in the cps file.
     25
     26'''
     27class ForcedMeanObjectBatch(Batch):
     28
     29    '''
     30    Constructor
     31    '''
     32    def __init__(self,
     33                 logger,
     34                 config,
     35                 skychunk,
     36                 gpc1Db,
     37                 ippToPspsDb,
     38                 scratchDb,
     39                 dvoID,
     40                 batchID):
     41
     42       super(ForcedMeanObjectBatch, self).__init__(
     43               logger,
     44               config,
     45               skychunk,
     46               gpc1Db,
     47               ippToPspsDb,
     48               scratchDb,
     49               dvoID,
     50               batchID,
     51               "FO",
     52               None)
     53
     54       try:
     55           self.dvoForcedObjects = DvoForcedObjects(self.logger, self.config, self.skychunk, self.ippToPspsDb, self.scratchDb)
     56       except:
     57           self.logger.errorPair("Unable to create instance of", "DvoForcedObjects")
     58           raise
     59
     60       # create an output filename, which is {dvoINDEX}.FITS
     61       self.outputFitsFile = "%08d.FITS" % self.id
     62       self.outputFitsPath = "%s/%s" % (self.localOutPath, self.outputFitsFile)
     63
     64       # dump stuff to log
     65       self.logger.infoPair("DVO INDEX", "%d" % self.id)
     66
     67    '''
     68    Overriden from batch base-class. We import from DVO directly for forced mean objects
     69    '''
     70    def importIppTables(self, columns="*", filter=""):
     71
     72        self.region = self.scratchDb.getRegionNameFromThisDvoIndex(self.id)
     73        self.ippToPspsDb.insertForcedMeanObjectMeta(self.batchID, self.region)
     74        self.dvoForcedObjects.nativeIngestRegion(self.region)
     75
     76        cptTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpt")
     77
     78        if not self.scratchDb.tableExists(cptTableName):
     79            self.logger.infoPair("can't find in scratch db:","cpt table")
     80            return False
     81
     82        cpyTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpy")
     83
     84        if not self.scratchDb.tableExists(cpyTableName):
     85            self.logger.infoPair("can't find in scratch db:","cpy table")
     86            return False
     87
     88        return True
     89
     90    '''
     91    Applies indexes to the PSPS tables
     92    '''
     93    def alterPspsTables(self):
     94
     95        return True
     96
     97    '''
     98    Applies indexes to the IPP tables
     99    '''
     100    def indexIppTables(self):
     101
     102        # since dvopsps is now used, no action is needed here
     103        # self.logger.infoPair("Creating indexes on", "IPP tables")
     104
     105        return True
     106
     107    '''
     108    Inserts stuff for all mags
     109    '''
     110    def updateForcedMeanObjectFromCps(self, cpsTable):
     111
     112        # list of all filters PSPS is interested in
     113        # XXX EAM : 2014.07.24 : this list should probably be in a config file somewhere
     114        interestedFilters = ['g', 'r', 'i', 'z', 'y']
     115       
     116        filters = self.scratchDb.getOrderedListOfFiltersFromPhotcodesTable(interestedFilters)
     117   
     118        # get a count of the available filters
     119        filterCount = self.scratchDb.getCountOfFiltersFromPhotcodesTable()
     120
     121        self.logger.infoPair("Available filters in Photcodes", filters)
     122
     123        # the 'code' now defines the order in the cps file that the mags are listed for a given filter
     124        self.logger.infoPair("Adding magnitudes from", "cps table")
     125        for filter in filters:
     126
     127            filterID = self.scratchDb.getFilterID(filter[1])
     128
     129            # NOTE: Manipulation of FLAGS from cpsTable is to move ID_SECF_OBJ_EXT flag (0x01000000)
     130            # from bit 24 to bit 13 so that it fits into the SMALLINT Object.Flags
     131            # XXX EAM : 20140724 this manipulation is no longer needed : [grizy]Flags is now 4 byte (was 8 byte!)
     132
     133            # set the MeanObject fields based largely on dvopsps cps fields:
     134
     135            # XXX EAM 20140724 : filterCount is meant to match
     136            # Nsecfilt, but is potentially not determined correctly.
     137            # use a call to a dvo-native command which knows how to
     138            # find Nsecfilt (or save in the db with dvopsps)
     139
     140            # the math below depends on filterCount = Nsecfilt and MeanObject.row being 1 counting but cps being 0 counting?
     141            # cps.row has a count of MeanObject.row * Nsecfilt + Nfilter
     142            #  " + cpsTable + " AS cps ON (cps.row = (MeanObject.row* " + str(filterCount) + ")-(" + str(filterCount) + " - " + str(filter[0]) + ")) \
     143
     144            sql = "UPDATE ForcedMeanObject JOIN \
     145                   " + cpsTable + " AS cps ON (cps.row = (ForcedMeanObject.row* " + str(filterCount) + ")-(" + str(filterCount) + " - " + str(filter[0]) + ")) \
     146                   SET \
     147                    ForcedMeanObject." + filter[1] + "nTotal         = NWARP \
     148                   ,ForcedMeanObject." + filter[1] + "nIncPSFMag     = NUSED_WRP \
     149                   ,ForcedMeanObject." + filter[1] + "nIncKronMag    = NUSED_KRON_WRP \
     150                   ,ForcedMeanObject." + filter[1] + "nIncApMag      = NUSED_AP_WRP \
     151                   ,ForcedMeanObject." + filter[1] + "PSFMag         = MAG_PSF_WRP \
     152                   ,ForcedMeanObject." + filter[1] + "PSFMagErr      = MAG_PSF_WRP_ERR \
     153                   ,ForcedMeanObject." + filter[1] + "PSFMagStd      = MAG_PSF_WRP_STD \
     154                   ,ForcedMeanObject." + filter[1] + "KronMag        = MAG_KRON_WRP \
     155                   ,ForcedMeanObject." + filter[1] + "KronMagErr     = MAG_KRON_WRP_ERR \
     156                   ,ForcedMeanObject." + filter[1] + "KronMagStd     = MAG_KRON_WRP_STD \
     157                   ,ForcedMeanObject." + filter[1] + "ApMag          = MAG_AP_WRP \
     158                   ,ForcedMeanObject." + filter[1] + "ApMagErr       = MAG_AP_WRP_ERR \
     159                   ,ForcedMeanObject." + filter[1] + "ApMagStd       = MAG_AP_WRP_STD \
     160                   ,ForcedMeanObject." + filter[1] + "Flags          = FLAGS "
     161
     162
     163            try: self.scratchDb.execute(sql)
     164            except:
     165                self.logger.errorPair("failed update MeanObject", sql)
     166                raise
     167
     168        # now set to null all MeanMagErr values > 0.5 (cut set by Gene, 2012-04-12)
     169        # XXX EAM 20140724 : keep this cut?
     170        cut = 0.5
     171        self.logger.infoPair("Setting to NULL all MeanMagErr value >", "%f" % cut)
     172        for filter in filters:
     173
     174            sql = "UPDATE ForcedMeanObject \
     175                   SET " + filter[1] + "PSFMagErr = null \
     176                   WHERE " + filter[1] + "PSFMagErr > " + str(cut)
     177            self.scratchDb.execute(sql)
     178
     179        self.logger.infoPair("Calculating nDetections from", "n[filters]")
     180        for filter in filters:
     181            # now do a sum of n[filters], but do not include the ones with -999
     182            sql  = "UPDATE ObjectThin "
     183            sql += "SET nDetections = nDetections + n" + filter[1]
     184            sql += " WHERE n" + filter[1] + " != -999"
     185            self.scratchDb.execute(sql)
     186
     187    '''
     188    Inserts stuff for all mags from the cpy files
     189    '''
     190    def updateForcedMeanObjectFromCpy(self, cpyTable):
     191
     192        # list of all filters PSPS is interested in
     193        # XXX EAM : 2014.07.24 : this list should probably be in a config file somewhere
     194        interestedFilters = ['g', 'r', 'i', 'z', 'y']
     195       
     196        filters = self.scratchDb.getOrderedListOfFiltersFromPhotcodesTable(interestedFilters)
     197   
     198        # get a count of the available filters
     199        filterCount = self.scratchDb.getCountOfFiltersFromPhotcodesTable()
     200
     201        self.logger.infoPair("Available filters in Photcodes", filters)
     202
     203        # the 'code' now defines the order in the cps file that the mags are listed for a given filter
     204        self.logger.infoPair("Adding magnitudes from", "cpy table")
     205        for filter in filters:
     206
     207            filterID = self.scratchDb.getFilterID(filter[1])
     208
     209            # NOTE: Manipulation of FLAGS from cpsTable is to move ID_SECF_OBJ_EXT flag (0x01000000)
     210            # from bit 24 to bit 13 so that it fits into the SMALLINT Object.Flags
     211            # XXX EAM : 20140724 this manipulation is no longer needed : [grizy]Flags is now 4 byte (was 8 byte!)
     212
     213            # set the MeanObject fields based largely on dvopsps cps fields:
     214
     215            # XXX EAM 20140724 : filterCount is meant to match
     216            # Nsecfilt, but is potentially not determined correctly.
     217            # use a call to a dvo-native command which knows how to
     218            # find Nsecfilt (or save in the db with dvopsps)
     219
     220            # the math below depends on filterCount = Nsecfilt and MeanObject.row being 1 counting but cps being 0 counting?
     221            # cps.row has a count of MeanObject.row * Nsecfilt + Nfilter
     222            #  " + cpsTable + " AS cps ON (cps.row = (MeanObject.row* " + str(filterCount) + ")-(" + str(filterCount) + " - " + str(filter[0]) + ")) \
     223
     224            sql = "UPDATE ForcedMeanObject JOIN \
     225                   " + cpyTable + " AS cpy ON (cpy.row = (ForcedMeanObject.row* " + str(filterCount) + ")-(" + str(filterCount) + " - " + str(filter[0]) + ")) \
     226                   SET \
     227                    ForcedMeanObject." + filter[1] + "nIncR5          =  NMEAS \
     228                   ,ForcedMeanObject." + filter[1] + "nIncR6          =  NMEAS \
     229                   ,ForcedMeanObject." + filter[1] + "FmeanflxR5      =  FLUX_AP_R5 \
     230                   ,ForcedMeanObject." + filter[1] + "FmeanflxR5Err   =  FLUX_ERR_AP_R5 \
     231                   ,ForcedMeanObject." + filter[1] + "FmeanflxR5Std   =  FLUX_STD_AP_R5 \
     232                   ,ForcedMeanObject." + filter[1] + "FmeanflxR5Fill  =  FLUX_FIL_AP_R5 \
     233                   ,ForcedMeanObject." + filter[1] + "FmeanflxR6      =  FLUX_AP_R6 \
     234                   ,ForcedMeanObject." + filter[1] + "FmeanflxR6Err   =  FLUX_ERR_AP_R6 \
     235                   ,ForcedMeanObject." + filter[1] + "FmeanflxR6Std   =  FLUX_STD_AP_R6 \
     236                   ,ForcedMeanObject." + filter[1] + "FmeanflxR6Fill  =  FLUX_FIL_AP_R6 \
     237                   ,ForcedMeanObject." + filter[1] + "LensObjSmearX11 =  X11_SM_OBJ \
     238                   ,ForcedMeanObject." + filter[1] + "LensObjSmearX12 =  X12_SM_OBJ \
     239                   ,ForcedMeanObject." + filter[1] + "LensObjSmearX22 =  X22_SM_OBJ \
     240                   ,ForcedMeanObject." + filter[1] + "LensObjSmearE1  =  E1_SM_OBJ \
     241                   ,ForcedMeanObject." + filter[1] + "LensObjSmearE2  =  E2_SM_OBJ \
     242                   ,ForcedMeanObject." + filter[1] + "LensObjShearX11 =  X11_SH_OBJ \
     243                   ,ForcedMeanObject." + filter[1] + "LensObjShearX12 =  X12_SH_OBJ \
     244                   ,ForcedMeanObject." + filter[1] + "LensObjShearX22 =  X22_SH_OBJ \
     245                   ,ForcedMeanObject." + filter[1] + "LensObjShearE1  =  E1_SH_OBJ \
     246                   ,ForcedMeanObject." + filter[1] + "LensObjShearE2  =  E2_SH_OBJ \
     247                   ,ForcedMeanObject." + filter[1] + "LensPSFSmearX11 =  X11_SM_PSF \
     248                   ,ForcedMeanObject." + filter[1] + "LensPSFSmearX12 =  X12_SM_PSF \
     249                   ,ForcedMeanObject." + filter[1] + "LensPSFSmearX22 =  X22_SM_PSF \
     250                   ,ForcedMeanObject." + filter[1] + "LensPSFSmearE1  =  E1_SM_PSF \
     251                   ,ForcedMeanObject." + filter[1] + "LensPSFSmearE2  =  E2_SM_PSF \
     252                   ,ForcedMeanObject." + filter[1] + "LensPSFShearX11 =  X11_SH_PSF \
     253                   ,ForcedMeanObject." + filter[1] + "LensPSFShearX12 =  X12_SH_PSF \
     254                   ,ForcedMeanObject." + filter[1] + "LensPSFShearX22 =  X22_SH_PSF \
     255                   ,ForcedMeanObject." + filter[1] + "LensPSFShearE1  =  E1_SH_PSF \
     256                   ,ForcedMeanObject." + filter[1] + "LensPSFShearE2  =  E2_SH_PSF \
     257                   ,ForcedMeanObject." + filter[1] + "Gamma           =  GAMMA \
     258                   ,ForcedMeanObject." + filter[1] + "E1              =  E1 \
     259                   ,ForcedMeanObject." + filter[1] + "E2              =  E2 "
     260
     261
     262            try: self.scratchDb.execute(sql)
     263            except:
     264                self.logger.errorPair("failed update on ForcedMeanObject using cpy table", sql)
     265                raise
     266
     267
     268    '''
     269    Populates the ForcedMeanObject table
     270    '''
     271    def populateForcedMeanObjectTable(self):
     272
     273        cptTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpt")
     274        cpsTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cps")
     275        cpyTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpy")
     276
     277               
     278        self.logger.info("Populating ForcedMeanObject")
     279        self.logger.info("Inserting objects from cpt file")
     280
     281        # note "dec" is a reserved word in MySQL
     282        # XXX EAM 20140724 : do not use INGORE unless we discover unavoidable problems...
     283        # INSERT IGNORE INTO ObjectThin
     284
     285        sqlLine = sqlUtility("INSERT INTO ForcedMeanObject (")
     286
     287        sqlLine.group("objID",           "EXT_ID")
     288        sqlLine.group("ippObjID",        "OBJ_ID + (CAT_ID << 32)") # NOTE: shift by 32 bits exactly
     289        sqlLine.group("batchID",         "'" + str(self.batchID) + "'")
     290        sqlLine.group("nDetections",      "'0'")
     291        sql = sqlLine.makeRaw(") SELECT ", " FROM " + cptTableName)
     292
     293        try:
     294            self.scratchDb.execute(sql)
     295        except:
     296            self.logger.errorPair("Couldn't populate Object table from cpt", sql)
     297            return False
     298 
     299        # add row count columns so we can perform joins to get colors
     300        self.logger.infoPair("Adding 'row' columns to", "Object and cps tables")
     301        self.scratchDb.addRowCountColumn("ForcedMeanObject", "row")
     302        self.scratchDb.addRowCountColumn(cpsTableName, "row")
     303
     304
     305        self.logger.infoPair("update ForcedMeanObject from ","cps table")
     306
     307        self.updateForcedMeanObjectFromCps(cpsTableName)
     308
     309        self.logger.infoPair("update ForcedMeanObject from ","cpy table")
     310
     311        self.updateForcedMeanObjectFromCpy(cpyTableName)
     312
     313
     314        self.logger.infoPair("Dropping row column from", "ForcedMeanObject table")
     315        self.scratchDb.dropColumn("ForcedMeanObject", "row")
     316
     317        ##self.logger.infoPair("Purging from scratch Db", self.region + " region")
     318
     319        ##Don't do this till after MeanObject
     320        ##self.dvoForcedObjects.purgeRegion(self.region)
     321
     322        self.setMinMaxObjID(["ForcedMeanObject"])
     323
     324        return True
     325
     326 
     327    '''
     328    Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
     329    '''
     330    def populatePspsTables(self):
     331
     332        if not self.populateForcedMeanObjectTable(): return False
     333
     334        # now remove the objID duplicates. We could not do this before as cpt/cps tables relate by row number
     335        self.logger.infoPair("Forcing uniqueness on", "objID in ForcedMeanObject table")
     336        rowCountBefore = self.scratchDb.getRowCount("ForcedMeanObject")
     337
     338        # XXX EAM : note that in mysql versions later than 5.1, this fails
     339        # unless the following is called first:
     340        # set session old_alter_table=1
     341        # follow the command with
     342        # set session old_alter_table=0
     343        # OF COURSE, this fails for mysql version < 5.5...
     344        if self.scratchDb.version > 5.1:
     345            self.scratchDb.execute("set session old_alter_table=1")
     346           
     347        self.scratchDb.execute("ALTER IGNORE TABLE ForcedMeanObject ADD UNIQUE INDEX(objID)")
     348        if self.scratchDb.version > 5.1:
     349            self.scratchDb.execute("set session old_alter_table=0")
     350
     351        rowCountAfter = self.scratchDb.getRowCount("ForcedMeanObject")
     352        self.logger.infoPair("Number of duplicated objIDs removed", "%d out of %d" % ((rowCountBefore - rowCountAfter), rowCountBefore))
     353
     354        self.dvoForcedObjects.purgeRegion(self.region)
     355
     356        #this is abuse of something but this is how I get the object batches to crash to further investigate them
     357       
     358#        rowCountAfter = self.scratchDb.getRowCount("Object")
     359        return True
     360#        return False
Note: See TracChangeset for help on using the changeset viewer.