IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 6, 2011, 11:00:22 AM (15 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ipp-20110710/ippToPsps/jython
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20110710/ippToPsps/jython

    • Property svn:ignore set to
      *.class
  • branches/eam_branches/ipp-20110710/ippToPsps/jython/dvoToMySQL.py

    r31398 r32337  
    1010from subprocess import call, PIPE, Popen
    1111
    12 from gpc1db import Gpc1Db
     12from pslogger import PSLogger
    1313from scratchdb import ScratchDb
    1414
     
    2727
    2828    '''
    29     def __init__(self, logger, pathToDvo):
     29    def __init__(self, logger, doc, RESETTABLES):
    3030
    3131        # set up logging
    3232        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)
    4443
    4544        # create DVO tables
    46         #self.scratchDb.createDvoTables()
     45        if RESETTABLES: self.scratchDb.resetDvoToMysqlTables()
    4746
    4847        # 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,
    5353                "",
    5454                "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")
    5757
    5858        # 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 + " ( \
    6163               sourceID, \
    6264               imageID, \
     
    7173               PHOTCODE \
    7274               FROM " + imagesTableName
    73         self.scratchDb.stmt.execute(sql)
     75        self.scratchDb.execute(sql)
    7476
    7577        subdirs = ['n0000']
     
    7779        for subdir in subdirs:
    7880
    79             files = glob.glob(pathToDvo + "/" + subdir + "/*.cpm")
     81            files = glob.glob(self.dvoLocation + "/" + subdir + "/*.cpm")
    8082
    8183            #files = ['0247.06', '0244.06', '0244.10']
     
    8587                # get just filename, without extension
    8688                file = os.path.basename(os.path.splitext(file)[0])
    87                 self.logger.info("---------------------------------------------: " + file)
     89                self.logger.infoSeparator()
    8890
    8991                if self.scratchDb.alreadyImportedThisDvoTable(file): continue
    9092
    9193                # import cpm table and index
    92                 cpmTableName = self.importFits(self.pathToDvo,
     94                cpmTableName = self.importFits(self.dvoLocation,
    9395                        subdir,
    9496                        file + ".cpm",
    9597                        "IMAGE_ID DET_ID OBJ_ID CAT_ID EXT_ID DB_FLAGS")
     98                self.scratchDb.createIndex(cpmTableName, "IMAGE_ID")
    9699                self.scratchDb.createIndex(cpmTableName, "CAT_ID")
    97100                self.scratchDb.createIndex(cpmTableName, "OBJ_ID")
    98101
    99102                # import cpt table and index
    100                 cptTableName = self.importFits(self.pathToDvo,
     103                cptTableName = self.importFits(self.dvoLocation,
    101104                        subdir,
    102105                        file + ".cpt",
    103106                        "OBJ_ID CAT_ID EXT_ID")
     107                self.scratchDb.createIndex(cptTableName, "IMAGE_ID")
    104108                self.scratchDb.createIndex(cptTableName, "CAT_ID")
    105109                self.scratchDb.createIndex(cptTableName, "OBJ_ID")
    106110     
    107111                # 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 \
    112116                       SET a.SOURCE_ID = b.SOURCE_ID \
    113117                       WHERE a.IMAGE_ID = b.IMAGE_ID"
    114                 self.scratchDb.stmt.execute(sql)
     118                self.scratchDb.execute(sql)
    115119
    116120                # shove PSPS objID in measurement table
    117                 self.logger.info("Adding PSPS objID into measurements table")
     121                self.logger.infoPair("Adding","PSPS objIDs")
    118122                sql = "ALTER TABLE "+cpmTableName+" ADD COLUMN (PSPS_OBJ_ID BIGINT)"
    119                 self.scratchDb.stmt.execute(sql)
     123                self.scratchDb.execute(sql)
    120124                sql = "UPDATE "+cpmTableName+" AS a, "+cptTableName+" AS b \
    121125                       SET a.PSPS_OBJ_ID = b.EXT_ID \
    122126                       WHERE a.CAT_ID = b.CAT_ID \
    123127                       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 + " (\
    128136                       sourceID \
    129137                       ,imageID \
     
    142150                       ,DB_FLAGS \
    143151                       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
    145157
    146158                # now drop what we don't need
    147                 self.logger.info("Dropping tables")
     159                self.logger.infoPair("Dropping table", cpmTableName)
    148160                self.scratchDb.dropTable(cpmTableName)
     161                self.logger.infoPair("Dropping table", cptTableName)
    149162                self.scratchDb.dropTable(cptTableName)
    150163       
     
    173186      tableName = tableName.replace('.', '_')
    174187
    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)
    176190
    177191      tables = stilts.treads(fullPath)
     
    180194      for table in tables:
    181195
    182           self.logger.info("Reading IPP table " + table.name + " from FITS file")
     196          self.logger.infoPair("Reading IPP table", table.name)
    183197          table = stilts.tpipe(table, cmd='explodeall')
    184198     
    185199          # 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")
    187201          table = stilts.tpipe(table, cmd='replaceval -Infinity null *')
    188202          table = stilts.tpipe(table, cmd='replaceval Infinity null *')
    189203
    190204          #try:
    191           self.logger.info("Writing FITS table to database")
     205          self.logger.infoPair("Writing FITS table to", "database")
    192206          table.cmd_keepcols(columns).write(self.scratchDb.url + '#' + tableName)
    193207          #except:
     
    195209          count = count + 1
    196210
    197       self.logger.info("Done. Imported %d tables" % count)
     211      self.logger.infoPair("Finished importing", "%d tables" % count)
    198212
    199213      return tableName
    200214
    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'''
     217Start of program.
     218'''
     219
     220if len(sys.argv) > 1: CONFIG  = sys.argv[1]
     221else:
     222    print "** Usage: " + sys.argv[0] + " <configPath> [reset]"
     223    sys.exit(1)
     224
     225# open config file
     226configDoc = ElementTree(file=CONFIG)
     227
     228logging.setLoggerClass(PSLogger)
     229logger = logging.getLogger("dvoToMySQL")
     230logger.setup(configDoc, "dvoToMySQL", 0, 1)
     231
     232RESETTABLES = 0
     233
     234if 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
     239dvoToMySql = DvoToMySql(logger, configDoc, RESETTABLES)
     240
     241logger.infoPair("Program...", "complete")
     242
Note: See TracChangeset for help on using the changeset viewer.