- Timestamp:
- Sep 6, 2011, 11:00:22 AM (15 years ago)
- Location:
- branches/eam_branches/ipp-20110710/ippToPsps/jython
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
dvoToMySQL.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20110710/ippToPsps/jython
-
Property svn:ignore
set to
*.class
-
Property svn:ignore
set to
-
branches/eam_branches/ipp-20110710/ippToPsps/jython/dvoToMySQL.py
r31398 r32337 10 10 from subprocess import call, PIPE, Popen 11 11 12 from gpc1db import Gpc1Db12 from pslogger import PSLogger 13 13 from scratchdb import ScratchDb 14 14 … … 27 27 28 28 ''' 29 def __init__(self, logger, pathToDvo):29 def __init__(self, logger, doc, RESETTABLES): 30 30 31 31 # set up logging 32 32 self.logger = logger 33 self.pathToDvo = pathToDvo 34 self.logger.debug("DvoToMySql class constructor") 35 36 self.logger.debug("Important DVO database at " + self.pathToDvo) 37 38 # open config 39 doc = ElementTree(file="config.xml") 40 41 # create database objects 42 self.scratchDb = ScratchDb(logger) 43 self.gpc1Db = Gpc1Db(self.logger) 33 self.doc = doc 34 self.logger.infoSeparator() 35 self.dvoLocation = self.doc.find("dvo/location").text 36 37 # create database object 38 self.scratchDb = ScratchDb(logger, self.doc, 1) 39 40 # some log stuff 41 self.logger.infoPair("Started", "dvoToMySql") 42 self.logger.infoPair("Importing DVO database at", self.dvoLocation) 44 43 45 44 # create DVO tables 46 #self.scratchDb.createDvoTables()45 if RESETTABLES: self.scratchDb.resetDvoToMysqlTables() 47 46 48 47 # import Images.dat table 49 sql = "DELETE FROM dvoMetaFull" 50 self.scratchDb.stmt.execute(sql) 51 52 imagesTableName = self.importFits(self.pathToDvo, 48 self.logger.infoPair("Deleting from table", self.scratchDb.dvoMetaTable) 49 sql = "DELETE FROM " + self.scratchDb.dvoMetaTable 50 self.scratchDb.execute(sql) 51 52 imagesTableName = self.importFits(self.dvoLocation, 53 53 "", 54 54 "Images.dat", 55 " IMAGE_ID SOURCE_ID CCDNUM EXTERN_ID FLAGS PHOTCODE NSTAR")56 self.scratchDb.createIndex(imagesTableName, " IMAGE_ID")55 "SOURCE_ID IMAGE_ID CCDNUM EXTERN_ID FLAGS PHOTCODE NSTAR") 56 self.scratchDb.createIndex(imagesTableName, "EXTERN_ID") 57 57 58 58 # insert into dvoMetaFull 59 self.logger.info("Inserting all image meta data into database") 60 sql = "INSERT INTO dvoMetaNew ( \ 59 # NB what we and smf files call IMAGE_ID, DVO calls EXTERN_ID. We are sticking 60 # with the smf name 61 self.logger.infoPair("Populating", "all image meta data") 62 sql = "INSERT INTO " + self.scratchDb.dvoMetaTable + " ( \ 61 63 sourceID, \ 62 64 imageID, \ … … 71 73 PHOTCODE \ 72 74 FROM " + imagesTableName 73 self.scratchDb. stmt.execute(sql)75 self.scratchDb.execute(sql) 74 76 75 77 subdirs = ['n0000'] … … 77 79 for subdir in subdirs: 78 80 79 files = glob.glob( pathToDvo+ "/" + subdir + "/*.cpm")81 files = glob.glob(self.dvoLocation + "/" + subdir + "/*.cpm") 80 82 81 83 #files = ['0247.06', '0244.06', '0244.10'] … … 85 87 # get just filename, without extension 86 88 file = os.path.basename(os.path.splitext(file)[0]) 87 self.logger.info ("---------------------------------------------: " + file)89 self.logger.infoSeparator() 88 90 89 91 if self.scratchDb.alreadyImportedThisDvoTable(file): continue 90 92 91 93 # import cpm table and index 92 cpmTableName = self.importFits(self. pathToDvo,94 cpmTableName = self.importFits(self.dvoLocation, 93 95 subdir, 94 96 file + ".cpm", 95 97 "IMAGE_ID DET_ID OBJ_ID CAT_ID EXT_ID DB_FLAGS") 98 self.scratchDb.createIndex(cpmTableName, "IMAGE_ID") 96 99 self.scratchDb.createIndex(cpmTableName, "CAT_ID") 97 100 self.scratchDb.createIndex(cpmTableName, "OBJ_ID") 98 101 99 102 # import cpt table and index 100 cptTableName = self.importFits(self. pathToDvo,103 cptTableName = self.importFits(self.dvoLocation, 101 104 subdir, 102 105 file + ".cpt", 103 106 "OBJ_ID CAT_ID EXT_ID") 107 self.scratchDb.createIndex(cptTableName, "IMAGE_ID") 104 108 self.scratchDb.createIndex(cptTableName, "CAT_ID") 105 109 self.scratchDb.createIndex(cptTableName, "OBJ_ID") 106 110 107 111 # shove SOURCE_IDs into measurement table 108 self.logger.info ("Adding SOURCE_IDs into measurements table")109 sql = "ALTER TABLE " +cpmTableName+" ADD COLUMN (SOURCE_ID SMALLINT)"110 self.scratchDb. stmt.execute(sql)111 sql = "UPDATE " +cpmTableName+" AS a, "+imagesTableName+" AS b \112 self.logger.infoPair("Adding", "SOURCE_IDs") 113 sql = "ALTER TABLE " + cpmTableName + " ADD COLUMN (SOURCE_ID SMALLINT)" 114 self.scratchDb.execute(sql) 115 sql = "UPDATE " + cpmTableName + " AS a, " + imagesTableName + " AS b \ 112 116 SET a.SOURCE_ID = b.SOURCE_ID \ 113 117 WHERE a.IMAGE_ID = b.IMAGE_ID" 114 self.scratchDb. stmt.execute(sql)118 self.scratchDb.execute(sql) 115 119 116 120 # shove PSPS objID in measurement table 117 self.logger.info ("Adding PSPS objID into measurements table")121 self.logger.infoPair("Adding","PSPS objIDs") 118 122 sql = "ALTER TABLE "+cpmTableName+" ADD COLUMN (PSPS_OBJ_ID BIGINT)" 119 self.scratchDb. stmt.execute(sql)123 self.scratchDb.execute(sql) 120 124 sql = "UPDATE "+cpmTableName+" AS a, "+cptTableName+" AS b \ 121 125 SET a.PSPS_OBJ_ID = b.EXT_ID \ 122 126 WHERE a.CAT_ID = b.CAT_ID \ 123 127 AND a.OBJ_ID = b.OBJ_ID" 124 self.scratchDb.stmt.execute(sql) 125 126 self.logger.info("Putting everything into dvoDetectionFull table") 127 sql = "INSERT IGNORE INTO dvoDetectionFull (\ 128 self.scratchDb.execute(sql) 129 130 # NB we use an INSERT IGNORE here. This is because of a known issue where multiple DVO cpm 131 # files can include the same detection, with the same IMAGE_ID/DET_ID pairing, but different 132 # PSPS object IDs assigned in the corresponding cpt file. This is believed to be a chip-boundary 133 # issue within DVO. So, for now, we take the first IMAGE_ID/DET_ID detection we find, and ignore the rest 134 self.logger.infoPair("Populating", self.scratchDb.dvoDetectionTable) 135 sql = "INSERT IGNORE INTO " + self.scratchDb.dvoDetectionTable + " (\ 128 136 sourceID \ 129 137 ,imageID \ … … 142 150 ,DB_FLAGS \ 143 151 FROM " + cpmTableName 144 self.scratchDb.stmt.execute(sql) 152 try: 153 self.scratchDb.execute(sql) 154 except: 155 self.logger.error("FAILED: " + sql) 156 return 145 157 146 158 # now drop what we don't need 147 self.logger.info ("Dropping tables")159 self.logger.infoPair("Dropping table", cpmTableName) 148 160 self.scratchDb.dropTable(cpmTableName) 161 self.logger.infoPair("Dropping table", cptTableName) 149 162 self.scratchDb.dropTable(cptTableName) 150 163 … … 173 186 tableName = tableName.replace('.', '_') 174 187 175 self.logger.info("Attempting to import tables from '" + fullPath + "' to '" + tableName + "'") 188 self.logger.infoPair("Importing tables from file", fullPath) 189 self.logger.infoPair("Writing to database table", tableName) 176 190 177 191 tables = stilts.treads(fullPath) … … 180 194 for table in tables: 181 195 182 self.logger.info ("Reading IPP table " + table.name + " from FITS file")196 self.logger.infoPair("Reading IPP table", table.name) 183 197 table = stilts.tpipe(table, cmd='explodeall') 184 198 185 199 # IPP FITS files are littered with infinities, so remove these 186 self.logger.info ("Removing Infinity values from all columns")200 self.logger.infoPair("Removing", "infinity values") 187 201 table = stilts.tpipe(table, cmd='replaceval -Infinity null *') 188 202 table = stilts.tpipe(table, cmd='replaceval Infinity null *') 189 203 190 204 #try: 191 self.logger.info ("Writing FITS table todatabase")205 self.logger.infoPair("Writing FITS table to", "database") 192 206 table.cmd_keepcols(columns).write(self.scratchDb.url + '#' + tableName) 193 207 #except: … … 195 209 count = count + 1 196 210 197 self.logger.info ("Done. Imported%d tables" % count)211 self.logger.infoPair("Finished importing", "%d tables" % count) 198 212 199 213 return tableName 200 214 201 logging.config.fileConfig("logging.conf") 202 logger = logging.getLogger("dvotomysql") 203 logger.info("Starting") 204 205 dvoToMySql = DvoToMySql(logger, "/data/ipp005.0/gpc1/catdirs/MD04.merges/MD04.merge") 206 #dvoToMySql = DvoToMySql(logger, "/export/ippc00.1/rhenders/MD04.merge") 207 208 logger.info("Program complete") 209 215 216 ''' 217 Start of program. 218 ''' 219 220 if len(sys.argv) > 1: CONFIG = sys.argv[1] 221 else: 222 print "** Usage: " + sys.argv[0] + " <configPath> [reset]" 223 sys.exit(1) 224 225 # open config file 226 configDoc = ElementTree(file=CONFIG) 227 228 logging.setLoggerClass(PSLogger) 229 logger = logging.getLogger("dvoToMySQL") 230 logger.setup(configDoc, "dvoToMySQL", 0, 1) 231 232 RESETTABLES = 0 233 234 if len(sys.argv) > 2 and sys.argv[2] == "reset": 235 response = raw_input("* Are you ABSOLUTELY sure you want to recreate the DVO tables (y/n)? ") 236 if response == "y": RESETTABLES = 1 237 else: sys.exit(1) 238 239 dvoToMySql = DvoToMySql(logger, configDoc, RESETTABLES) 240 241 logger.infoPair("Program...", "complete") 242
Note:
See TracChangeset
for help on using the changeset viewer.
