Changeset 33155
- Timestamp:
- Jan 25, 2012, 5:10:25 PM (14 years ago)
- Location:
- trunk/ippToPsps/jython
- Files:
-
- 2 edited
-
dvo.py (modified) (8 diffs)
-
scratchdb.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippToPsps/jython/dvo.py
r33154 r33155 47 47 ''' 48 48 def __del__(self): 49 50 49 self.logger.debug("Dvo destructor") 51 52 50 53 51 ''' … … 62 60 def loadImages(self): 63 61 64 # import Images.dat table 62 # check if we have up-to-date version 63 path = self.dvoLocation + "/Images.dat" 64 if self.scratchDb.alreadyImportedThisDvoTable(path): return 65 66 # first, delete old dvoMeta table 65 67 self.logger.infoPair("Deleting from table", self.scratchDb.dvoMetaTable) 66 68 sql = "DELETE FROM " + self.scratchDb.dvoMetaTable 67 69 self.scratchDb.execute(sql) 68 70 69 self.imagesTableName = self.importFits( 70 "Images.dat", 71 "SOURCE_ID IMAGE_ID CCDNUM EXTERN_ID FLAGS PHOTCODE NSTAR") 72 self.scratchDb.createIndex(self.imagesTableName, "EXTERN_ID") 71 self.importFits( 72 path, 73 "SOURCE_ID IMAGE_ID CCDNUM EXTERN_ID FLAGS PHOTCODE NSTAR", 74 self.scratchDb.dvoImageTable) 75 self.scratchDb.createIndex(self.scratchDb.dvoImageTable, "EXTERN_ID") 73 76 74 77 # insert into dvoMetaFull … … 88 91 FLAGS, \ 89 92 PHOTCODE \ 90 FROM " + self.imagesTableName 91 self.scratchDb.execute(sql) 92 93 FROM " + self.scratchDb.dvoImageTable 94 95 try: 96 self.scratchDb.execute(sql) 97 except: 98 self.logger.errorPair("Could not insert into", self.scratchDb.dvoMetaTable) 99 return 100 101 self.scratchDb.setImportedThisDvoTable(path) 93 102 94 103 ''' … … 96 105 ''' 97 106 def loadSkyTable(self): 107 108 path = self.dvoLocation + "/SkyTable.fits" 109 if self.scratchDb.alreadyImportedThisDvoTable(path): return 110 self.importFits( 111 path, 112 "R_MIN R_MAX D_MIN D_MAX INDEX NAME", 113 self.scratchDb.dvoSkyTable) 114 115 self.logger.infoPair("Adding index to", self.scratchDb.dvoSkyTable) 116 117 self.scratchDb.createIndex(self.scratchDb.dvoSkyTable, "INDEX") 98 118 99 self.skyTableName = self.importFits( 100 "SkyTable.fits", 101 "R_MIN R_MAX D_MIN D_MAX INDEX NAME") 102 103 self.logger.infoPair("Adding index to", self.skyTableName) 104 105 self.scratchDb.createIndex(self.skyTableName, "INDEX") 106 119 self.scratchDb.setImportedThisDvoTable(path) 107 120 self.logger.infoPair("Finished importing SkyTable at", self.dvoLocation) 108 121 109 122 110 123 ''' 111 Populates dvoDetections with FITS tables from DVO for the whole sky112 '''113 def loadRegion(self, minRa, maxRa, minDec, maxDec):114 self.loadRegion(-1, 361, -91, 91)115 116 '''117 124 Populates dvoDetections and dvoMeta with FITS tables from DVO for the defined region 118 ''' 119 def loadRegion(self, minRa, maxRa, minDec, maxDec): 120 125 Defaults to whole sky if limits are missing 126 ''' 127 def loadRegion(self, minRa=-1., maxRa=361., minDec=-91., maxDec=91): 128 129 if (maxRa - minRa <= 0) or (maxDec - minDec) <= 0: 130 self.logger.errorPair("Zero range in either RA or Dec", 131 "RA: %.2f -> %.2f, Dec: %.2f -> %.2f" % (minRa, maxRa, minDec, maxDec)) 132 return False 133 134 self.logger.infoPair("Finding DVO files for range", 135 "RA: %.2f -> %.2f, Dec: %.2f -> %.2f" % (minRa, maxRa, minDec, maxDec)) 121 136 files = self.scratchDb.getDvoFilesCoveringThisRegion(minRa, maxRa, minDec, maxDec) 122 137 123 138 count = 0 139 140 self.logger.infoSeparator() 124 141 for file in files: 125 path = self.dvoLocation + "/" + file + ".cpm" 126 if not os.path.isfile(path): continue 142 143 cpmPath = self.dvoLocation + "/" + file + ".cpm" 144 cptPath = self.dvoLocation + "/" + file + ".cpt" 145 146 if not os.path.isfile(cpmPath): continue 127 147 count = count + 1 128 print path 129 130 if self.scratchDb.alreadyImportedThisDvoTable(file): continue 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 131 152 132 153 # import cpm table and index 133 154 cpmTableName = self.importFits( 134 file + ".cpm",155 cpmPath, 135 156 "IMAGE_ID DET_ID OBJ_ID CAT_ID EXT_ID DB_FLAGS") 136 157 self.scratchDb.createIndex(cpmTableName, "IMAGE_ID") … … 140 161 # import cpt table and index 141 162 cptTableName = self.importFits( 142 file + ".cpt",163 cptPath, 143 164 "OBJ_ID CAT_ID EXT_ID") 144 165 self.scratchDb.createIndex(cptTableName, "IMAGE_ID") … … 150 171 sql = "ALTER TABLE " + cpmTableName + " ADD COLUMN (SOURCE_ID SMALLINT)" 151 172 self.scratchDb.execute(sql) 152 sql = "UPDATE " + cpmTableName + " AS a, " + self. imagesTableName + " AS b \173 sql = "UPDATE " + cpmTableName + " AS a, " + self.scratchDb.dvoImageTable + " AS b \ 153 174 SET a.SOURCE_ID = b.SOURCE_ID \ 154 175 WHERE a.IMAGE_ID = b.IMAGE_ID" … … 196 217 # now drop what we don't need 197 218 self.logger.infoPair("Dropping table", cpmTableName) 198 #self.scratchDb.dropTable(cpmTableName)219 self.scratchDb.dropTable(cpmTableName) 199 220 self.logger.infoPair("Dropping table", cptTableName) 200 #self.scratchDb.dropTable(cptTableName) 201 202 self.scratchDb.setImportedThisDvoTable(file) 221 self.scratchDb.dropTable(cptTableName) 222 223 self.scratchDb.setImportedThisDvoTable(cpmPath) 224 self.scratchDb.setImportedThisDvoTable(cptPath) 203 225 204 226 self.logger.infoPair("Found", "%d region files" % count) 205 227 228 return True 229 206 230 207 231 ''' 208 232 Imports a FITS file. A regex filter lets you choose which tables to pull from the file 209 ''' 210 def importFits(self, file, columns): 211 212 fullPath = self.dvoLocation + "/" + file 213 self.logger.infoPair("Importing file", fullPath) 214 215 tableName = file 216 tableName = tableName.replace('.', '_') 217 tableName = tableName.replace('/', '_') 233 An optional final argument lets you name the resulatant database table 234 ''' 235 def importFits(self, path, columns, tableName=None): 236 237 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('/', '_') 218 244 219 245 self.logger.infoPair("Writing to database table", tableName) 220 246 221 tables = stilts.treads( fullPath)247 tables = stilts.treads(path) 222 248 223 249 count = 0 … … 263 289 dvo = Dvo(logger, configDoc) 264 290 #dvo.resetAllTables() 265 #dvo.loadImages()266 #dvo.loadSkyTable()267 dvo.loadRegion(330, 330.1, 0. 3, 0.4)291 dvo.loadImages() 292 dvo.loadSkyTable() 293 dvo.loadRegion(330, 330.1, 0.2, 0.7) 268 294 269 295 logger.infoPair("Program...", "complete") -
trunk/ippToPsps/jython/scratchdb.py
r33147 r33155 26 26 self.dvoDetectionTable = "dvoDetectionFull" 27 27 self.dvoDoneTable = "dvoDone" 28 self.dvoSkyTable = "SkyTable_fits" 28 self.dvoSkyTable = "dvoSkyTable" 29 self.dvoImageTable = "dvoImages" 29 30 else: 30 31 self.dvoMetaTable = "dvoMeta" … … 183 184 184 185 ''' 186 Have we already imported this DVO table? 187 ''' 188 def alreadyImportedThisDvoTable(self, path): 189 190 fileStat = os.stat(path) 191 192 sql = "SELECT COUNT(*) FROM " + self.dvoDoneTable + " WHERE path = '" + path + "' AND modifiedDate = " + str(fileStat.st_mtime) 193 194 try: 195 rs = self.executeQuery(sql) 196 rs.first() 197 if rs.getInt(1) > 0: 198 self.logger.errorPair("Already imported up-to-date version of", path) 199 return True 200 else: 201 return False 202 except: 203 self.logger.exception("Unable to check whether this DVO table has been imported") 204 205 206 ''' 185 207 Updates dvoDone table with this DVO table 186 208 ''' 187 def setImportedThisDvoTable(self, name): 188 189 sql = "INSERT INTO dvoDone (name) VALUES ('" + name + "')" 209 def setImportedThisDvoTable(self, path): 210 211 fileStat = os.stat(path) 212 213 # first delete any old version we have ingested 214 sql = "DELETE FROM " + self.dvoDoneTable + " WHERE path = '" + path + "'" 215 self.execute(sql) 216 217 # now insert new version with up-to-date size and date 218 sql = "INSERT INTO " + self.dvoDoneTable + " (path, modifiedDate, size) VALUES ('" + path + "', " + str(str(fileStat.st_mtime)) + ", " + str(str(fileStat.st_size)) + ")" 190 219 self.execute(sql) 191 220 … … 210 239 211 240 return True 212 213 '''214 Have we already imported this DVO table?215 '''216 def alreadyImportedThisDvoTable(self, name):217 218 sql = "SELECT COUNT(*) FROM dvoDone WHERE name = '" + name + "'"219 220 try:221 rs = self.executeQuery(sql)222 rs.first()223 if rs.getInt(1) > 0:224 self.logger.errorPair("Already imported DVO tables for", name)225 return True226 else:227 return False228 except:229 self.logger.exception("Unable to check whether this DVO table has been imported")230 241 231 242 … … 298 309 299 310 self.logger.infoPair("Creating table", self.dvoDoneTable ) 300 sql = "CREATE TABLE " + self.dvoDoneTable + " ( name VARCHAR(100))"311 sql = "CREATE TABLE " + self.dvoDoneTable + " (path VARCHAR(1000), modifiedDate BIGINT, size BIGINT)" 301 312 try: self.execute(sql) 302 313 except: self.logger.errorPair("Unable to create table", self.dvoDoneTable)
Note:
See TracChangeset
for help on using the changeset viewer.
