- Timestamp:
- Jan 26, 2012, 12:25:36 PM (14 years ago)
- File:
-
- 1 edited
-
trunk/ippToPsps/jython/dvo.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippToPsps/jython/dvo.py
r33155 r33161 43 43 self.logger.infoPair("Started", "dvo") 44 44 45 # decide if we are using the right DVO 46 self.correctDvo = self.scratchDb.isThisDvoCurrentlyIngested(self.dvoLocation) 47 45 48 ''' 46 49 Destructor … … 49 52 self.logger.debug("Dvo destructor") 50 53 54 55 ''' 56 Check we are importing the DVO currenly ingested 57 ''' 58 def isThisDvoCurrentlyIngested(self): 59 return self.correctDvo 60 51 61 ''' 52 62 Resets all dvo tables. Be very careful.... … … 54 64 def resetAllTables(self): 55 65 self.scratchDb.resetDvoToMysqlTables() 66 self.correctDvo = True 56 67 57 68 ''' … … 59 70 ''' 60 71 def loadImages(self): 72 73 # go no further if we've already partly ingested a different DVO 74 if not self.correctDvo: return 61 75 62 76 # check if we have up-to-date version … … 106 120 def loadSkyTable(self): 107 121 122 # go no further if we've already partly ingested a different DVO 123 if not self.correctDvo: return 124 108 125 path = self.dvoLocation + "/SkyTable.fits" 109 126 if self.scratchDb.alreadyImportedThisDvoTable(path): return … … 122 139 123 140 ''' 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 129 145 if (maxRa - minRa <= 0) or (maxDec - minDec) <= 0: 130 146 self.logger.errorPair("Zero range in either RA or Dec", … … 134 150 self.logger.infoPair("Finding DVO files for range", 135 151 "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: 142 161 143 162 cpmPath = self.dvoLocation + "/" + file + ".cpm" 144 163 cptPath = self.dvoLocation + "/" + file + ".cpt" 145 164 165 # check for existence of cpm and cpt files 146 166 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) 160 271 161 272 # import cpt table and index 162 cptTableName =self.importFits(273 self.importFits( 163 274 cptPath, 164 "OBJ_ID CAT_ID EXT_ID") 275 "OBJ_ID CAT_ID EXT_ID", 276 cptTableName) 277 165 278 self.scratchDb.createIndex(cptTableName, "IMAGE_ID") 166 279 self.scratchDb.createIndex(cptTableName, "CAT_ID") 167 280 self.scratchDb.createIndex(cptTableName, "OBJ_ID") 168 281 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 179 283 self.logger.infoPair("Adding","PSPS objIDs") 180 284 sql = "ALTER TABLE "+cpmTableName+" ADD COLUMN (PSPS_OBJ_ID BIGINT)" 181 285 self.scratchDb.execute(sql) 182 sql = "UPDATE " +cpmTableName+" AS a, "+cptTableName+" AS b \286 sql = "UPDATE " + cpmTableName + " AS a, " + cptTableName + " AS b \ 183 287 SET a.PSPS_OBJ_ID = b.EXT_ID \ 184 288 WHERE a.CAT_ID = b.CAT_ID \ … … 221 325 self.scratchDb.dropTable(cptTableName) 222 326 223 self.scratchDb.setImportedThisDvoTable(cpmPath)224 327 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) 227 333 228 334 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 229 346 230 347 … … 233 350 An optional final argument lets you name the resulatant database table 234 351 ''' 235 def importFits(self, path, columns, tableName =None):352 def importFits(self, path, columns, tableName): 236 353 237 354 self.logger.infoPair("Importing file", path) 238 239 if not tableName:240 head,tail = os.path.split(path)241 tableName = tail242 tableName = tableName.replace('.', '_')243 tableName = tableName.replace('/', '_')244 245 355 self.logger.infoPair("Writing to database table", tableName) 246 356 … … 288 398 289 399 dvo = Dvo(logger, configDoc) 400 dvo.isThisDvoCurrentlyIngested() 290 401 #dvo.resetAllTables() 402 dvo.getSizeAlreadyIngested() 291 403 dvo.loadImages() 292 404 dvo.loadSkyTable() 293 dvo.loadRegion(330, 330.1, 0.2, 0.7) 405 dvo.loadRegion(330, 331, 0.2, 3) 406 #dvo.loadRegion() 294 407 295 408 logger.infoPair("Program...", "complete")
Note:
See TracChangeset
for help on using the changeset viewer.
