use module logging instead of warning for messages

This commit is contained in:
Kasper D. Fischer 2021-10-16 19:27:16 +02:00
parent 7fd109b3ea
commit dc1df224cc

View File

@ -13,7 +13,7 @@
curl -s "https://fdsnws.geophysik.ruhr-uni-bochum.de/fdsnws/event/1/query?minlat=50&maxlat=54&minlon=3&maxlon=10&minmag=1" | mkGeolocationTable.py > geolocationTable.js curl -s "https://fdsnws.geophysik.ruhr-uni-bochum.de/fdsnws/event/1/query?minlat=50&maxlat=54&minlon=3&maxlon=10&minmag=1" | mkGeolocationTable.py > geolocationTable.js
License License
Copyright 2020 Kasper D. Fischer <kasper.fischer@rub.de> Copyright 2020-2021 Kasper D. Fischer <kasper.fischer@rub.de>
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free under the terms of the GNU General Public License as published by the Free
@ -27,32 +27,33 @@
You should have received a copy of the GNU General Public License along You should have received a copy of the GNU General Public License along
with this program. If not, see http://www.gnu.org/licenses/. with this program. If not, see http://www.gnu.org/licenses/.
$Id$
''' '''
def mkGeolocationTable(file=''): def mkGeolocationTable(file=''):
# imports ## imports
# XML ElementTree
try: try:
import xml.etree.cElementTree as ET import xml.etree.cElementTree as ET
except ImportError: except ImportError:
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
# json
import json as JSON
# sys stdin
from sys import stdin from sys import stdin
import warnings
# geopy
from geopy.geocoders import Photon from geopy.geocoders import Photon
from geopy.extra.rate_limiter import RateLimiter from geopy.extra.rate_limiter import RateLimiter
from geopy.exc import GeocoderServiceError from geopy.exc import GeocoderServiceError
import json as JSON
# constants ## constants
url = 'https://photon.komoot.io/reverse?lon={lng:.3f}&lat={lat:.3f}' URL = 'https://photon.komoot.io/reverse?lon={lng:.3f}&lat={lat:.3f}&limit=5'
namespaces = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7', NAMESPACES = {'sc3': 'http://geofon.gfz-potsdam.de/ns/seiscomp3-schema/0.7',
'qml': 'http://quakeml.org/xmlns/bed/1.2'} 'qml': 'http://quakeml.org/xmlns/bed/1.2'}
def simple_warning(message, category, filename, lineno, file=None, line=None):
return 'Warning: %s\n' % (message)
warnings.formatwarning = simple_warning
# try loading the file # try loading the file
geolocationTable = {} geolocationTable = {}
if file : if file :
@ -62,7 +63,7 @@ def mkGeolocationTable(file=''):
geolocationTable = JSON.loads(jsonfileContent) geolocationTable = JSON.loads(jsonfileContent)
except: except:
geolocationTable = {} geolocationTable = {}
warnings.warn('Could not parse file %s' %file) logging.warning('Could not parse file %s' %file)
# parse event.xml # parse event.xml
DOM = ET.parse(stdin).getroot() DOM = ET.parse(stdin).getroot()
@ -70,21 +71,24 @@ def mkGeolocationTable(file=''):
reverse_geolocate = RateLimiter(geolocator.reverse, min_delay_seconds=1) reverse_geolocate = RateLimiter(geolocator.reverse, min_delay_seconds=1)
# iterate over all events # iterate over all events
for event in DOM.iterfind('qml:eventParameters/qml:event', namespaces): count = 0
for event in DOM.iterfind('qml:eventParameters/qml:event', NAMESPACES):
count += 1
publicID = event.attrib['publicID'].split('/')[2] publicID = event.attrib['publicID'].split('/')[2]
lat = float(event.find('./qml:origin/qml:latitude/qml:value', namespaces).text) lat = float(event.find('./qml:origin/qml:latitude/qml:value', NAMESPACES).text)
lng = float(event.find('./qml:origin/qml:longitude/qml:value', namespaces).text) lng = float(event.find('./qml:origin/qml:longitude/qml:value', NAMESPACES).text)
evaluationMode = event.find('./qml:origin/qml:evaluationMode', namespaces).text evaluationMode = event.find('./qml:origin/qml:evaluationMode', NAMESPACES).text
if publicID in geolocationTable: if publicID in geolocationTable:
warnings.warn('Skipping cached event %s' %(publicID)) logging.warning('Skipping cached event {id}'.format(id=publicID))
elif evaluationMode == 'automatic': elif evaluationMode == 'automatic':
warnings.warn('Skipping automatic event %s' %(publicID)) logging.warning('Skipping automatic event {id}'.format(id=publicID))
else: else:
logging.info('Processing event {id}'.format(id=publicID))
try: try:
locations = reverse_geolocate("{lat:.3f}, {lng:.3f}".format(lat=lat, lng=lng),exactly_one=False,limit=5) locations = reverse_geolocate("{lat:.3f}, {lng:.3f}".format(lat=lat, lng=lng),exactly_one=False,limit=5)
except GeocoderServiceError: except GeocoderServiceError:
warnings.warn('Reverse Geolocation failed. Skipping event.') logging.warning('Reverse Geolocation failed. Skipping event.')
continue continue
place = [] place = []
for location in locations: for location in locations:
@ -100,27 +104,52 @@ def mkGeolocationTable(file=''):
try: try:
place = location.raw['properties']['county'] place = location.raw['properties']['county']
except KeyError: except KeyError:
pass logging.debug('Could not extract city for event {id} at {lat:.3f} N / {lng:.3f} E (Service: {url}), trying next returned result'
.format(id=publicID, lat=lat, lng=lng, url=URL.format(lat=lat,lng=lng)))
logging.debug(location.raw)
if not place: if not place:
warnings.warn('Could not extract city for event {id} at {lat:.3f} N / {lng:.3f} E (Service: {url})'.format(id=publicID, lat=lat, lng=lng, url=url.format(lat=lat,lng=lng))) logging.critical('Could not extract city for event {id} at {lat:.3f} N / {lng:.3f} E (Service: {url})'
.format(id=publicID, lat=lat, lng=lng, url=URL.format(lat=lat,lng=lng)))
geolocationTable[publicID] = place geolocationTable[publicID] = place
# dump json # dump json
print('var geolocationTable = {0};'.format(JSON.dumps(geolocationTable, sort_keys=True))) print('var geolocationTable = {0};'.format(JSON.dumps(geolocationTable, sort_keys=True)))
logging.info("processed %d events", count)
# __main__ # __main__
if __name__ == "__main__": if __name__ == "__main__":
# use module logging
import logging
# parse arguments # parse arguments
import argparse import argparse
versionText = '$Revision$ ($Date$, $Author$)'.replace('$', '').replace(':','') versionText = 'v0.1 (2021-10-16)'
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Reverse geocoding lookup of events in xml format (stdin).', description='Reverse geocoding lookup of events in xml format (stdin).',
epilog=versionText) epilog=versionText)
parser.add_argument('-v', '-V', '--version', action='version', parser.add_argument('-V', '--version', action='version', version=versionText,
version=versionText) help="show version")
parser.add_argument('-f', '--file', action='store', dest='file', parser.add_argument('-f', '--file', action='store', dest='file',
help='read in JSON file containing old output.') help='read in JSON file containing old output.')
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
group.add_argument("-q", "--quiet", action="store_true",
help="disable output")
group.add_argument("-d", "--debug", action="store_true",
help="show debugging output",
)
cla = parser.parse_args() cla = parser.parse_args()
# set logging level
if cla.quiet:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.ERROR)
elif cla.verbose:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
elif cla.debug:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
else:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARN)
# call mkGeolocationTable(...) # call mkGeolocationTable(...)
mkGeolocationTable(file=cla.file) mkGeolocationTable(file=cla.file)