Example #1
0
def main():

    #    city = ['-143.44', '61.82']
    #    dist = 1000
    #    closest = []

    for arg in sys.argv:
        filename = arg

    csvfile = open(arg, "rb")
    reader = csv.reader(csvfile, delimiter=" ")
    i = 0
    min_lat = 90
    max_lat = -90
    min_lon = 180
    max_lon = -180

    for row in reader:
        if not i:
            name = row[0]
            mag = row[1]
            origin_lat = row[2]
            origin_lon = row[3]
            i = 1
        else:
            if float(row[0]) > max_lon:
                max_lon = float(row[0])
            if float(row[0]) < min_lon:
                min_lon = float(row[0])
            if float(row[1]) > max_lat:
                max_lat = float(row[1])
            if float(row[1]) < min_lat:
                min_lat = float(row[1])

    csvfile.close()

    fields = ["lat", "lon", "place"]

    db = ds.dbopen("/Seis/databases/places/wellknown_alaskan_cities")
    db = db.lookup("", "places", "", "")
    db = ds.dbsubset(
        db, "lat > %s && lat < %s && lon > %s && lon < %s" % (str(min_lat), str(max_lat), str(min_lon), str(max_lon))
    )

    if db.query("dbRECORD_COUNT") < 1:
        sys.stderr.write("error: No cities are within the Shakemap grid\n")

    else:
        cityInfo = {}
        for i in range(db.query("dbRECORD_COUNT")):
            db[3] = i
            for f in fields:
                cityInfo[f] = db.getv(f)[0]

            dist = 10000
            csvfile = open(arg, "rb")
            reader = csv.reader(csvfile, delimiter=" ")
            reader.next()

            for row in reader:
                dist_point = calc_distance(float(row[1]), float(row[0]), float(cityInfo["lat"]), float(cityInfo["lon"]))
                if dist_point < dist:
                    dist = dist_point
                    closest = row

            epi_dist = calc_distance(
                float(origin_lat), float(origin_lon), float(cityInfo["lat"]), float(cityInfo["lon"])
            )

            # print "Data for %s: %s %s %s %s %s %s %s %s" % (cityInfo['place'], str(int(round(epi_dist*0.6214))), str(mag), closest[2], closest[3], closest[4], closest[5], closest[6], closest[7])

            print "insert into shakemap values('%s','%s', %s, %s, %s, %s, %s, %s, %s, %s);" % (
                cityInfo["place"],
                name,
                str(round(epi_dist, 1)),
                str(mag),
                closest[2],
                closest[3],
                closest[4],
                closest[5],
                closest[6],
                closest[7],
            )

            csvfile.close()

    db.close()
    decom_name = dfile[1]

    if mtype == 'cumulative':
        this_decom_counter = 0

    # Loop over unqiue snets
    for s in usnets:
        stmp = tempfile.mkstemp(suffix='.xy',
                                prefix='deployment_list_%s_' % s)
        file_ptr = stmp[0]
        file_name = stmp[1]
        if verbose:
            print "generate_sta_locations(): Working on network: %s" % s

        try:
            dbptr_snet = antdb.dbsubset(dbptr, 'snet=~/%s/' % s )
        except Exception, e:
            print "Error occurred: %s" % e
        else:
            this_counter = 0
            for i in range( antdb.dbquery( dbptr_snet,
                                          antdb.dbRECORD_COUNT ) ):
                dbptr_snet[3] = i
                if mtype == 'rolling':
                    sta, lat, lon, snet = antdb.dbgetv(dbptr_snet,'sta', 'lat',
                                                          'lon', 'snet')
                    os.write(file_ptr, "%s    %s    # %s %s\n" % (lat, lon,
                                                                  snet, sta))
                    this_counter = this_counter + 1
                elif mtype == 'cumulative':
                    sta, lat, lon, snet, sta_time, sta_endtime = antdb.dbgetv(
Example #3
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