IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 6, 2011, 1:07:09 PM (15 years ago)
Author:
rhenders
Message:

added method to create DVO database table and a method to update objIDs for any table with those from DVO table; methiod to get min/max objID from any table; methods to write BatchManifest file in XML

File:
1 edited

Legend:

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

    r31183 r31201  
    55import re
    66import sys
     7import os
     8import md5
    79
    810from gpc1db import Gpc1Db
     11from ipptopspsdb import IppToPspsDb
    912
    1013from java.lang import *
    1114from java.sql import *
    12 from xml.etree.ElementTree import ElementTree
    13 
     15from xml.etree.ElementTree import ElementTree, Element, tostring
     16from xml.dom import minidom
    1417
    1518'''
     
    3033
    3134        # set up class variables
     35        self.batchType = batchType;
    3236        self.pspsVoTableFilePath = "../config/" + batchType + "/tables.vot"
    3337        self.inputFitsPath = inputFitsPath
     
    4650        self.localStmt = self.localCon.createStatement()
    4751
    48         # create Gpc1 object
     52        # create Gpc1Db object
    4953        self.gpc1Db = Gpc1Db()
    50 
    51         # set up JDBC connection to gpc1 Db
    52         dbName = doc.find("gpc1database/name").text
    53         dbHost = doc.find("gpc1database/host").text
    54         dbUser = doc.find("gpc1database/user").text
    55         dbPass = doc.find("gpc1database/password").text
    56         self.gpc1Url = "jdbc:mysql://"+dbHost+"/"+dbName+"?user="+dbUser+"&password="+dbPass
    57         self.gpc1Con = DriverManager.getConnection(self.localUrl)
    58         self.gpc1Stmt = self.localCon.createStatement()
    5954
    6055        # get survey ID from init table
     
    6762            self.log("No survey ID found for this survey: '" + survey + "'")
    6863            self.surveyID = -1;
    69    
     64   
     65        # get dvo info from config
     66        dvoName = doc.find(survey+"dvo/name").text
     67        dvoLocation = doc.find(survey+"dvo/location").text
     68         
     69        # get datastore info from config
     70        datastoreProduct = doc.find("datastore/product").text
     71
     72        # create IppToPspsDb object and create a new batch
     73        self.ippToPspsDb = IppToPspsDb()
     74        self.batchID = self.ippToPspsDb.createNewBatch(66, survey, self.getPspsBatchType(), dvoName, datastoreProduct)
     75
     76        # get local storage location from config
     77        self.batchName = "B%08d" % self.batchID
     78        self.localOutPath = doc.find("localOutPath").text + "/" + self.getPspsBatchType() + "/" + dvoName + "/%s" % self.batchName
     79        if not os.path.exists(self.localOutPath): os.makedirs(self.localOutPath)
     80
     81
    7082        # store today's date
    7183        now = datetime.datetime.now();
    7284        self.dateStr = now.strftime("%Y-%m-%d")
    7385
    74         if self.inputFitsPath != "":
    75             self.parseFitsHeader()
     86        if self.inputFitsPath != "": self.parseFitsHeader()
     87
     88        # create DVO table
     89        self.createDvoTable()
    7690
    7791    '''
     
    8397        self.localStmt.close()
    8498        self.localCon.close()
    85         self.gpc1Stmt.close()
    86         self.gpc1Con.close()
     99
     100    '''
     101    Writes the batch manifest file
     102    '''
     103    def writeBatchManifest(self):
     104
     105        outPath = self.localOutPath + "/BatchManifest.xml"
     106        tmpPath = "./tmp.xml"
     107        self.log("Creating batch manifest file here: " + outPath)
     108        root = Element('manifest')
     109
     110        # batch information
     111        root.attrib['name'] = self.batchName
     112        root.attrib['type'] = self.getPspsBatchType()
     113        root.attrib['survey'] = self.getPspsSurveyType()
     114        root.attrib['timestamp'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
     115        root.attrib['minObjId'] = str(self.minObjID)
     116        root.attrib['maxObjId'] = str(self.maxObjID)
     117
     118        # get md5sum
     119        fits=os.popen("md5sum " + self.outputFitsPath)
     120        for f in fits.readlines(): md5sum = f[0:f.rfind(" ")]
     121
     122        # get file size
     123        fileSize = os.path.getsize(self.outputFitsPath)
     124
     125        # file information
     126        child = Element('file')
     127        root.append(child)
     128        child.attrib['name'] = self.outputFitsFile
     129        child.attrib['bytes'] = str(fileSize)
     130        child.attrib['md5'] = md5sum
     131
     132        # now create doc and write to file
     133        file = open(tmpPath, 'w')
     134        ElementTree(root).write(file)
     135        file.close()
     136
     137        # clunky way to prettify XML
     138        os.popen("xmllint --format " + tmpPath + " > " + outPath)
     139#        os.remove(tmpPath)
     140
     141
     142    '''
     143    Gets PSPS-friendly survey type
     144    '''
     145    def getPspsSurveyType(self):
     146
     147        if self.survey == "ThreePi": return "3PI"
     148        elif self.survey == "MD04": return "MD04"
     149        else: self.log("ERROR: Don't know this survey: " + self.survey)
     150
     151    '''
     152    Gets PSPS friendly batch type
     153    '''
     154    def getPspsBatchType(self):
     155
     156        if self.batchType == "init": return "IN"
     157        elif self.batchType == "detection": return "P2"
     158        elif self.batchType == "stack": return "ST"
     159        else: self.log("ERROR: Don't know this batch type: " + self.survey)
     160
    87161
    88162    '''
     
    92166
    93167        print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " | " + msg
     168
     169    '''
     170    Sets min and max obj ID using the provided table
     171    '''
     172    def setMinMaxObjID(self, table):
     173
     174        sql = "SELECT MIN(objID), MAX(objID) FROM " + table
     175        print
     176        rs = self.localStmt.executeQuery(sql)
     177        rs.first()
     178        self.minObjID = rs.getLong(1)
     179        self.maxObjID = rs.getLong(2)
     180
     181        print "MIN mAX = %d %d" % (self.minObjID,  self.maxObjID)
    94182
    95183    '''
     
    317405        self.log("Not implemented yet")
    318406
     407    '''
     408    Updates provided table with DVO IDs from DVO table
     409    '''
     410    def updateDvoIDs(self, table):
     411
     412        self.log("Updating table '" + table + "' with DVO IDs...")
     413        sql = "UPDATE " + table + " AS a, dvo AS b SET \
     414               a.ippObjID = b.ippObjID, \
     415               a.objID = b.objID \
     416               WHERE a.ippDetectID = b.ippDetectID"
     417        self.localStmt.execute(sql)
     418        self.log("...done")
     419
     420
     421    '''
     422    Creates a table for for ID matching
     423    '''
     424    def createDvoTable(self):
     425
     426        self.log("Creating DVO table for ID matching")
     427        sql = "DROP TABLE dvo"
     428        self.localStmt.execute(sql)
     429        sql = "CREATE TABLE dvo (ippDetectID BIGINT PRIMARY KEY, ippObjID BIGINT, objID BIGINT)"
     430        self.localStmt.execute(sql)
     431
Note: See TracChangeset for help on using the changeset viewer.