IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33161


Ignore:
Timestamp:
Jan 26, 2012, 12:25:36 PM (14 years ago)
Author:
rhenders
Message:

now doesn't re-ingest cpm file if previous attempt was aborted after cpm but before cpt; now checking we are using correct DVO; counting stuff already ingested, stuff we need to ingest (including bytes on disk)

File:
1 edited

Legend:

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

    r33155 r33161  
    4343        self.logger.infoPair("Started", "dvo")
    4444
     45        # decide if we are using the right DVO
     46        self.correctDvo = self.scratchDb.isThisDvoCurrentlyIngested(self.dvoLocation)
     47
    4548    '''
    4649    Destructor
     
    4952        self.logger.debug("Dvo destructor")
    5053
     54
     55    '''
     56    Check we are importing the DVO currenly ingested
     57    '''
     58    def isThisDvoCurrentlyIngested(self):
     59        return self.correctDvo
     60
    5161    '''
    5262    Resets all dvo tables. Be very careful....
     
    5464    def resetAllTables(self):
    5565        self.scratchDb.resetDvoToMysqlTables()
     66        self.correctDvo = True
    5667
    5768    '''
     
    5970    '''
    6071    def loadImages(self):
     72
     73        # go no further if we've already partly ingested a different DVO
     74        if not self.correctDvo: return
    6175
    6276        # check if we have up-to-date version
     
    106120    def loadSkyTable(self):
    107121       
     122        # go no further if we've already partly ingested a different DVO
     123        if not self.correctDvo: return
     124
    108125        path =  self.dvoLocation + "/SkyTable.fits"
    109126        if self.scratchDb.alreadyImportedThisDvoTable(path): return
     
    122139
    123140    '''
    124     Populates dvoDetections and dvoMeta with FITS tables from DVO for the defined region
    125     Defaults to whole sky if limits are missing
    126     '''
    127     def loadRegion(self, minRa=-1., maxRa=361., minDec=-91., maxDec=91):
    128 
     141    Gets a bunch of region files from DVO for this RA/Dec box
     142    '''
     143    def getRegionFiles(self, minRa=-1., maxRa=361., minDec=-91., maxDec=91):
     144   
    129145        if (maxRa - minRa <= 0) or (maxDec - minDec) <= 0:
    130146            self.logger.errorPair("Zero range in either RA or Dec",
     
    134150        self.logger.infoPair("Finding DVO files for range",
    135151                "RA: %.2f -> %.2f, Dec: %.2f -> %.2f" % (minRa, maxRa, minDec, maxDec))
    136         files = self.scratchDb.getDvoFilesCoveringThisRegion(minRa, maxRa, minDec, maxDec)
    137 
    138         count = 0
    139 
    140         self.logger.infoSeparator()
    141         for file in files:
     152
     153        allFiles = self.scratchDb.getDvoFilesCoveringThisRegion(minRa, maxRa, minDec, maxDec)
     154
     155        files = []
     156
     157        total = 0
     158        alreadyIngested = 0
     159        toIngest = 0
     160        for file in allFiles:
    142161
    143162           cpmPath = self.dvoLocation + "/" + file + ".cpm"
    144163           cptPath = self.dvoLocation + "/" + file + ".cpt"
    145164
     165           # check for existence of cpm and cpt files
    146166           if not os.path.isfile(cpmPath): continue
    147            count = count + 1
    148 
    149            # only if we have already imported up-to-date versions of both the cpm and ctp
    150            # will we skip this one
    151            if self.scratchDb.alreadyImportedThisDvoTable(cpmPath) and self.scratchDb.alreadyImportedThisDvoTable(cptPath): continue
    152 
    153            # import cpm table and index
    154            cpmTableName = self.importFits(
    155                    cpmPath,
    156                    "IMAGE_ID DET_ID OBJ_ID CAT_ID EXT_ID DB_FLAGS")
    157            self.scratchDb.createIndex(cpmTableName, "IMAGE_ID")
    158            self.scratchDb.createIndex(cpmTableName, "CAT_ID")
    159            self.scratchDb.createIndex(cpmTableName, "OBJ_ID")
     167           if not os.path.isfile(cptPath): continue
     168
     169           total = total + 1
     170
     171           # if we have already imported up-to-date versions of both the cpm and cpt then we skip this region
     172           if self.scratchDb.alreadyImportedThisDvoTable(cpmPath) and self.scratchDb.alreadyImportedThisDvoTable(cptPath):
     173               alreadyIngested = alreadyIngested + 1
     174               continue
     175
     176           files.append(file)
     177
     178           toIngest = toIngest + 1
     179
     180        self.logger.infoPair("Total DVO regions to ingest", "%d" % total)
     181        self.logger.infoPair("DVO regions already ingested", "%d" % alreadyIngested)
     182        self.logger.infoPair("DVO regions to ingest", "%d" % toIngest)
     183
     184        return files
     185
     186    '''
     187    Formats a nice file size string
     188    Accepts a size in bytes
     189    '''
     190    def getFileSizeStr(self, byteSize):
     191
     192        # format a string for the user -smaller than one MB
     193        if byteSize < 1048576: return "%.1f Kb" % (byteSize/1024.0)
     194        # smaller than one GB
     195        elif byteSize < 1073741824: return "%.1f Mb" % (byteSize/1048576.0)
     196        # more than a GB
     197        else: return "%.1f Gb" % (byteSize/1073741824.0)
     198
     199    '''
     200    Returns the disk size of this region of data in DVO
     201    '''
     202    def getSizeOfDvoRegion(self, files):
     203
     204        size = 0.0
     205        for file in files:
     206
     207            # get combined size of cpm and cpt files
     208            size = size + os.stat(self.dvoLocation + "/" + file + ".cpm").st_size
     209            size = size + os.stat(self.dvoLocation + "/" + file + ".cpt").st_size
     210
     211        return size
     212       
     213    '''
     214    Returns (and reports to log) total size of DVO files ingested to the scratch Db
     215    '''
     216    def getSizeAlreadyIngested(self):
     217
     218        size = self.scratchDb.getTotalSizeOfIngestedDvoFiles()
     219        self.logger.infoPair("Total size of DVO already ingested", self.getFileSizeStr(size))
     220
     221
     222    '''
     223    Populates dvoDetections and dvoMeta with FITS tables from DVO for the defined region
     224    Defaults to whole sky if limits are missing
     225    '''
     226    def loadRegion(self, minRa=-1., maxRa=361., minDec=-91., maxDec=91):
     227
     228        # go no further if we've already partly ingested a different DVO
     229        if not self.correctDvo: return
     230
     231        files = self.getRegionFiles(minRa, maxRa, minDec, maxDec)
     232        totalSize = self.getSizeOfDvoRegion(files)
     233        self.logger.infoPair("Total size of cpm/cpt files to ingest", self.getFileSizeStr(totalSize))
     234        count = 0
     235
     236        self.logger.infoSeparator()
     237
     238        # now loop through all files
     239        for file in files:
     240
     241           cpmPath = self.dvoLocation + "/" + file + ".cpm"
     242           cptPath = self.dvoLocation + "/" + file + ".cpt"
     243
     244           cpmTableName = self.getDatabaseFriendlyTableName(file + ".cpm")
     245           cptTableName = self.getDatabaseFriendlyTableName(file + ".cpt")
     246
     247           # if we have not already ingested the cpm table, then do it now
     248           if not  self.scratchDb.alreadyImportedThisDvoTable(cpmPath):
     249           
     250               # import cpm table and index
     251               self.importFits(
     252                       cpmPath,
     253                       "IMAGE_ID DET_ID OBJ_ID CAT_ID EXT_ID DB_FLAGS",
     254                       cpmTableName)
     255
     256               self.scratchDb.createIndex(cpmTableName, "IMAGE_ID")
     257               self.scratchDb.createIndex(cpmTableName, "CAT_ID")
     258               self.scratchDb.createIndex(cpmTableName, "OBJ_ID")
     259
     260               # shove SOURCE_IDs into measurement table
     261               self.logger.infoPair("Adding", "SOURCE_IDs")
     262               sql = "ALTER TABLE " + cpmTableName + " ADD COLUMN (SOURCE_ID SMALLINT)"
     263               self.scratchDb.execute(sql)
     264               sql = "UPDATE " + cpmTableName + " AS a, " + self.scratchDb.dvoImageTable + " AS b \
     265                      SET a.SOURCE_ID = b.SOURCE_ID \
     266                      WHERE a.IMAGE_ID = b.IMAGE_ID"
     267               self.scratchDb.execute(sql)
     268
     269               # we can report that we have imported this table
     270               self.scratchDb.setImportedThisDvoTable(cpmPath)
    160271
    161272           # import cpt table and index
    162            cptTableName = self.importFits(
     273           self.importFits(
    163274                   cptPath,
    164                    "OBJ_ID CAT_ID EXT_ID")
     275                   "OBJ_ID CAT_ID EXT_ID",
     276                   cptTableName)
     277
    165278           self.scratchDb.createIndex(cptTableName, "IMAGE_ID")
    166279           self.scratchDb.createIndex(cptTableName, "CAT_ID")
    167280           self.scratchDb.createIndex(cptTableName, "OBJ_ID")
    168281
    169            # shove SOURCE_IDs into measurement table
    170            self.logger.infoPair("Adding", "SOURCE_IDs")
    171            sql = "ALTER TABLE " + cpmTableName + " ADD COLUMN (SOURCE_ID SMALLINT)"
    172            self.scratchDb.execute(sql)
    173            sql = "UPDATE " + cpmTableName + " AS a, " + self.scratchDb.dvoImageTable + " AS b \
    174                   SET a.SOURCE_ID = b.SOURCE_ID \
    175                   WHERE a.IMAGE_ID = b.IMAGE_ID"
    176            self.scratchDb.execute(sql)
    177 
    178            # shove PSPS objID in measurement table
     282           # shove PSPS objIDs from cpt table into measurement table
    179283           self.logger.infoPair("Adding","PSPS objIDs")
    180284           sql = "ALTER TABLE "+cpmTableName+" ADD COLUMN (PSPS_OBJ_ID BIGINT)"
    181285           self.scratchDb.execute(sql)
    182            sql = "UPDATE "+cpmTableName+" AS a, "+cptTableName+" AS b \
     286           sql = "UPDATE " + cpmTableName + " AS a, " + cptTableName + " AS b \
    183287                  SET a.PSPS_OBJ_ID = b.EXT_ID \
    184288                  WHERE a.CAT_ID = b.CAT_ID \
     
    221325           self.scratchDb.dropTable(cptTableName)
    222326
    223            self.scratchDb.setImportedThisDvoTable(cpmPath)
    224327           self.scratchDb.setImportedThisDvoTable(cptPath)
    225 
    226         self.logger.infoPair("Found", "%d region files" % count)
     328           count = count + 1
     329
     330        self.logger.infoSeparator()
     331
     332        self.logger.infoPair("Ingested", "%d DVO region files" % count)
    227333
    228334        return True
     335
     336    '''
     337    Create Db-friendly table name from file name
     338    '''
     339    def getDatabaseFriendlyTableName(self, file):
     340
     341        name = file
     342        name = name.replace('.', '_')
     343        name = name.replace('/', '_')
     344
     345        return name
    229346
    230347
     
    233350    An optional final argument lets you name the resulatant database table
    234351    '''
    235     def importFits(self, path, columns, tableName=None):
     352    def importFits(self, path, columns, tableName):
    236353
    237354      self.logger.infoPair("Importing file", path)
    238 
    239       if not tableName:
    240           head,tail = os.path.split(path)
    241           tableName = tail
    242           tableName = tableName.replace('.', '_')
    243           tableName = tableName.replace('/', '_')
    244 
    245355      self.logger.infoPair("Writing to database table", tableName)
    246356
     
    288398
    289399dvo = Dvo(logger, configDoc)
     400dvo.isThisDvoCurrentlyIngested()
    290401#dvo.resetAllTables()
     402dvo.getSizeAlreadyIngested()
    291403dvo.loadImages()
    292404dvo.loadSkyTable()
    293 dvo.loadRegion(330, 330.1, 0.2, 0.7)
     405dvo.loadRegion(330, 331, 0.2, 3)
     406#dvo.loadRegion()
    294407
    295408logger.infoPair("Program...", "complete")
Note: See TracChangeset for help on using the changeset viewer.