示例#1
0
def main():
    input = configure()
    
    #################################
    # Get the database origin and error information
    #################################
    
    fields = ['lat', 'lon', 'depth', 'time', 'lddate', 'ndef', 'review', 'ml', 'sdobs', 'smajax', 'sminax', 'strike', 'sdepth', 'stime']
    originInfo = {}
    
    db = ds.dbopen(input.dbname, "r")
    dbor = db.lookup("", "origin", "", "" )
    dbor = ds.dbsubset(dbor, "evid==%s" % int(input.evid))
    dbev = db.lookup("", "event", "", "" )
    dbor = ds.dbjoin(dbor, dbev)
    dbor = ds.dbsubset(dbor, "orid==prefor")
    dberr = db.lookup("", "origerr", "", "")
    dbor = ds.dbjoin(dbor, dberr)

    if dbor.query('dbRECORD_COUNT') < 1:
        sys.stderr.write('error: Database view contains no records\n')

    else:
        for i in range(dbor.query('dbRECORD_COUNT')):
            dbor[3] = i
            for f in fields:
                originInfo[f] = dbor.getv(f)[0]

    # Set if the origin has been reviewed or not
    
    if originInfo['review']=='y':
        originInfo['review']="manual"
    else:
        originInfo['review']="automatic"
    
    #################################
    # Begin writing the QuakeML document using the ElementTree package
    #################################
    
    # Populate the quakeml tag
    qml = ET.Element("q:quakeml")
    qml.set('xmlns', 'http://quakeml.org/xmlns/bed/1.2')
    qml.set('xmlns:catalog', 'http://anss.org/xmlns/catalog/0.1')
    qml.set('xmlns:q', 'http://quakeml.org/xmlns/quakeml/1.2')
    qml.set('xmlns:anss', 'http://anss.org/xmlns/event/0.1')
    
    # Populate the eventParameters tag
    eventParams = ET.SubElement(qml, "eventParameters")
    eventParams.set('publicID', 'quakeml:ak.anss.org/eventParameters/%s' % input.evid)
    
    # Populate the event tag
    event = ET.SubElement(eventParams, "event")
    event.set('publicID', 'quakeml:ak.anss.org/eventParameters/%s' % input.evid)
    event.set('catalog:datasource', 'ak')
    event.set('catalog:eventsource', 'ak')
    event.set('catalog:eventid', '%s' % input.evid)
    
    # Check if this is a delete QuakeML
    if input.print_delete:
        eventComment = ET.SubElement(event, "comment")
        eventText = ET.SubElement(eventComment, "text")
        eventText.text = "EVENT CANCELLED: "
        eventType = ET.SubElement(event, "type")
        eventType.text = "not existing"
        
        originInfo['lddate'] = stock.now()
    
    # Populate the magnitude tag
    if input.print_mag:
        
        magnitude = ET.SubElement(event, "magnitude")
        magnitude.set('publicID', 'quakeml:ak.anss.org/eventParameters/%s' % input.evid)
    
        mag = ET.SubElement(magnitude, "mag")
        magVal = ET.SubElement(mag, "value")
        magVal.text = "%s" % str(round(originInfo['ml'], 1))
    
        magType = ET.SubElement(magnitude, "type")
        magType.text = "Ml"
        
        magOrid = ET.SubElement(magnitude, "originID")
        magOrid.text = "quakeml:ak.anss.org/origin/%s" % input.evid
        
        magMethId = ET.SubElement(magnitude, "methodID")
        magMethId.text = "quakeml:anss.org/cube/magnitudeType/L"
        
        prefMagId = ET.SubElement(event, "preferredMagnitudeID")
        prefMagId.text = "quakeml:ak.anss.org/magnitude/%s/Ml" % input.evid
        
    # Populate the origin tag
    if input.print_origin:
        
        origin = ET.SubElement(event, "origin")
        origin.set('publicID', 'quakeml:ak.anss.org/eventParameters/%s' % input.evid)
        
        if input.print_arrival:
            dbassoc = db.lookup("","assoc","","")
            dbarr = db.lookup("","arrival","","")
            dbpicks = ds.dbjoin(dbor,dbassoc)
            dbpicks = ds.dbjoin(dbpicks, dbarr)
            
            arrFields = ['arid', 'sta', 'phase', 'delta', 'esaz', 'timeres', 'wgt', 'arrival.time', 'chan', 'arrival.lddate']
            arrInfo = {}
            
            for i in range(dbpicks.query('dbRECORD_COUNT')):
                dbpicks[3] = i
                for f in arrFields:
                    arrInfo[f] = dbpicks.getv(f)[0]
                
                # Write the arrival tag    
                arrival = ET.SubElement(origin, "arrival")
                arrival.set('publicID', 'quakeml:ak.anss.org/arrival/%s' % arrInfo['arid'])
                pickId = ET.SubElement(arrival, "pickID")
                pickId.text = "quakeml:ak.anss.org/arrival/%s" % arrInfo['arid']
                phase = ET.SubElement(arrival, "phase")
                phase.text = "%s" % arrInfo['phase']
                azimuth = ET.SubElement(arrival, "azimuth")
                azimuth.text = "%s" % arrInfo['esaz']
                distance = ET.SubElement(arrival, "distance")
                distance.text = "%s" % arrInfo['delta']
                timeRes = ET.SubElement(arrival, "timeResidual")
                timeRes.text = "%s" % arrInfo['timeres']
                timeWeight = ET.SubElement(arrival, "timeWeight")
                timeWeight.text = "%s" % arrInfo['wgt']
                arrCreationInfo = ET.SubElement(arrival, "creationInfo")
                arrAgencyId = ET.SubElement(arrCreationInfo, "agencyID")
                arrAgencyId.text = "ak"
                arrCreationTime = ET.SubElement(arrCreationInfo, "creationTime")
                arrCreationTime.text = "%s" % parse_time(arrInfo['arrival.lddate'])
                
                # Write the pick tag
                pick = ET.SubElement(event, "pick")
                pick.set('publicID', 'quakeml:ak.anss.org/arrival/%s' % arrInfo['arid'])
                pickTime = ET.SubElement(pick, "time")
                pickTimeVal = ET.SubElement(pickTime, "value")
                pickTimeVal.text = "%s" % parse_time(arrInfo['arrival.time'])
                pickWaveId = ET.SubElement(pick, "waveformID")
                pickWaveId.set('networkCode', "ak")
                pickWaveId.set('stationCode', "%s" % arrInfo['sta'])
                pickWaveId.set('channelCode', "%s" % arrInfo['chan'])
                pickWaveId.set('locationCode', "--")
                
                pickEvalMode = ET.SubElement(pick, "evaluationMode")
                pickEvalMode.text = "%s" % originInfo['review']
                
                pickCreationInfo = ET.SubElement(pick, "creationInfo")
                pickAgencyId = ET.SubElement(pickCreationInfo, "agencyID")
                pickAgencyId.text = "ak"
                pickCreationTime = ET.SubElement(pickCreationInfo, "creationTime")
                pickCreationTime.text = "%s" % parse_time(arrInfo['arrival.lddate'])
                
        
        origUncert = ET.SubElement(origin, "originUncertainty")
        minHorizUncert = ET.SubElement(origUncert, "minHorizontalUncertainty")
        minHorizUncert.text = "%s" % str(int(round((float(originInfo['sminax'])*1000), -2)))
        maxHorizUncert = ET.SubElement(origUncert, "maxHorizontalUncertainty")
        maxHorizUncert.text = "%s" % str(int(round((float(originInfo['smajax'])*1000), -2)))
        aziMaxHorizUncert = ET.SubElement(origUncert, "azimuthMaxHorizontalUncertainty")
        aziMaxHorizUncert.text = "%s" % originInfo['strike']
        prefDesc = ET.SubElement(origUncert, "preferredDescription")
        prefDesc.text = "uncertainty ellipse"
        
        origTime = ET.SubElement(origin, "time")
        origTimeVal = ET.SubElement(origTime, "value")
        origTimeVal.text = "%s" % parse_time(originInfo['time'])
        
        origLon = ET.SubElement(origin, "longitude")
        origLonVal = ET.SubElement(origLon, "value")
        origLonVal.text = "%s" % originInfo['lon']
        
        origLat = ET.SubElement(origin, "latitude")
        origLatVal = ET.SubElement(origLat, "value")
        origLatVal.text = "%s" % originInfo['lat']
        
        origDepth = ET.SubElement(origin, "depth")
        origDepthVal = ET.SubElement(origDepth, "value")
        origDepthVal.text = "%s" % str(int(round(float(originInfo['depth'])*1000, -2)))
        origDepthUncert = ET.SubElement(origDepth, "uncertainty")
        origDepthUncert.text = "%s" % str(int(round(float(originInfo['sdepth'])*1000, -2)))
        
        origMethId = ET.SubElement(origin, "methodID")
        origMethId.text = "quakeml:anss.org/cube/locationMethod/a"
        
        origQuality = ET.SubElement(origin, "quality")
        origNumPhase = ET.SubElement(origQuality, "usedPhaseCount")
        origNumPhase.text = "%s" % originInfo['ndef']
        origStdErr = ET.SubElement(origQuality, "standardError")
        origStdErr.text = "%s" % str(round(originInfo['sdobs'], 2))
        
        origEvalMode = ET.SubElement(origin, "evaluationMode")
        origEvalMode.text = "%s" % originInfo['review']
        
        prefOrigId = ET.SubElement(event, "preferredOriginID")
        prefOrigId.text = "quakeml:ak.anss.org/origin/%s" % input.evid
    
    
    # Populate various other QuakeML event tags
    eventCreationInfo = ET.SubElement(event, "creationInfo")
    agencyId = ET.SubElement(eventCreationInfo, "agencyID")
    agencyId.text = "ak"
    eventCreationTime = ET.SubElement(eventCreationInfo, "creationTime")
    eventCreationTime.text = "%s" % parse_time(originInfo['lddate'])
    eventVersion = ET.SubElement(eventCreationInfo, "version")
    eventVersion.text = "%s" % input.version
    
    # Creation Info tag
    evParamCreationInfo = ET.SubElement(eventParams, "creationInfo")
    evParamCreationTime = ET.SubElement(evParamCreationInfo, "creationTime")
    evParamCreationTime.text = "%s" % parse_time(originInfo['lddate'])
    
    #quakeml = ET.ElementTree(qml)
    pretty_quakeml = prettify(qml)
    
    #quakeml = ET.ElementTree(pretty_quakeml)
        
    #quakeml.write("quakeml.xml")
    
    db.close()
    print pretty_quakeml