/
vpvs.py
523 lines (453 loc) · 22.9 KB
/
vpvs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# -*- coding: utf-8 -*-
import handler
import tornado.ioloop
import tornado.web
import tornado.escape
from request_manager_vpvs import RequestManagerVPVS, RequestManagerVPVSStations
import _mysql
import json
from ConfigParser import ConfigParser, NoSectionError
import os
import logging
logging.basicConfig(level=logging.INFO)
class MainHandler(handler.APIBaseHandler):
def initialize(self, config):
self.config = config
def do_get(self):
manager = RequestManagerVPVS()
user_request = manager.bind(self).validate()
if user_request.is_valid:
args = user_request.getArgs()
# Uncomment lines 17, 18 in order to just show the user request args and stop
# self.send_success_response(args)
# return
db = _mysql.connect(self.config.get('db', 'host'),
self.config.get('db', 'user'),
self.config.get('db', 'pass'),
self.config.get('db', 'db'))
if args['stacode'] == '0000':
query = '''
select distinct
( 1 + ((TIMESTAMPDIFF(microsecond,c.origintime,g.pick)/{DIV}) - (TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV})) / (TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV})) as vpvs_value,
( ( (pow(f.wei,2)*0.02 + pow(g.wei,2)*0.02) / ((TIMESTAMPDIFF(microsecond,c.origintime,g.pick)/{DIV}) - (TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV})) ) + ((pow(f.wei,2)*0.02)/(TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV}))) as vpvs_error,
c.origintime as event_origin_time,
h.lat as station_latitude,
h.lon as station_longitude,
h.elev as station_elevation,
h.network as network_code,
h.stacode as station_code,
h.loco as station_location,
f.pick as p_arrival_time,
f.wei as p_quality,
g.pick as s_arrival_time,
g.wei s_quality,
c.eqkID as event_id,
c.id as event_loc_id,
c.lat as event_latitude,
c.lon as event_longitude,
c.elev as event_elevation
from
(select c.eqkID as ID,count(*) as counterPS from phases f, phases g, eqlocations c, stations h where f.eqkID=g.eqkID and g.eqkID=c.eqkID and f.type='P' and g.type='S' and f.wei<={maxpw} and g.wei<={maxpw} and f.stacode=g.stacode and f.stacode=h.stacode and g.stacode=h.stacode and f.loco=h.loco and g.loco=h.loco and f.loco=g.loco and (h.lat between {minlat} and {maxlat}) and (h.lon between {minlon} and {maxlon}) and (h.elev between {mineqdep} and {maxeqdep}) and c.model={modtype} and c.lcode={codetype} and c.method={mettype} and (c.origintime between '{mintime}' and '{maxtime}') group by f.eqkID) as tblPS,
(select f.eqkID as ID,count(*) as counterP from phases f, eqlocations c where f.eqkID=c.eqkID and f.type='P' and f.wei<={maxpw} and c.model={modtype} and c.lcode={codetype} and c.method={mettype} and (c.origintime between '{mintime}' and '{maxtime}') group by f.eqkID) as tblP,
(select g.eqkID as ID,count(*) as counterS from phases g, eqlocations c where g.eqkID=c.eqkID and g.type='S' and g.wei<={maxsw} and c.model={modtype} and c.lcode={codetype} and c.method={mettype} and (c.origintime between '{mintime}' and '{maxtime}') group by g.eqkID) as tblS,
eqlocations c, eqstatistics d, magnitudes e, phases f, phases g, stations h
where
tblPS.counterPS >= {minps}
and tblP.counterP >= {minnp}
and tblS.counterS >= {minns}
and f.wei<= {maxvpvspw}
and g.wei<= {maxvpvssw}
and d.gap <= {maxgap}
and d.mindist <= {midi}
and (d.errh <= {maxherr} or sqrt(pow(d.errx,2)+pow(d.erry,2)) <= {maxherr})
and d.errv <= {maxverr}
and c.eqkID=tblPS.ID
and c.eqkID=tblP.ID
and c.eqkID=tblS.ID
and c.model=1
and c.lcode={codetype}
and c.method={mettype}
and (c.lat between {minlat} and {maxlat})
and (c.lon between {minlon} and {maxlon})
and (c.elev between {mineqdep} and {maxeqdep})
and c.id=d.idloc
and c.eqkID=e.eqkID
and e.idloc=c.id
and (c.origintime between '{mintime}' and '{maxtime}')
and f.eqkID=g.eqkID
and c.eqkID=f.eqkID
and f.eqkID=g.eqkID
and g.eqkID=c.eqkID
and f.type='P'
and g.type='S'
and f.stacode=h.stacode
and h.stacode=g.stacode
and f.stacode=g.stacode
and f.loco=h.loco
and h.loco=g.loco
and f.loco=g.loco
having (vpvs_value > {vpvsmin}) and vpvs_error <= {maxvpvserr}
order by c.origintime asc
limit 1000000;
'''.format(**args)
else:
query = '''
select distinct
( 1 + ((TIMESTAMPDIFF(microsecond,c.origintime,g.pick)/{DIV}) - (TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV})) / (TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV})) as vpvs_value,
( ( (pow(f.wei,2)*0.02 + pow(g.wei,2)*0.02) / ((TIMESTAMPDIFF(microsecond,c.origintime,g.pick)/{DIV}) - (TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV})) ) + ((pow(f.wei,2)*0.02)/(TIMESTAMPDIFF(microsecond,c.origintime,f.pick)/{DIV}))) as vpvs_error,
c.origintime as event_origin_time,
h.lat as station_latitude,
h.lon as station_longitude,
h.elev as station_elevation,
h.network as network_code,
h.stacode as station_code,
h.loco as station_location,
f.pick as p_arrival_time,
f.wei as p_quality,
g.pick as s_arrival_time,
g.wei s_quality,
c.eqkID as event_id,
c.id as event_loc_id,
c.lat as event_latitude,
c.lon as event_longitude,
c.elev as event_elevation
from
(select c.eqkID as ID,count(*) as counterPS from phases f, phases g, eqlocations c, stations h where f.eqkID=g.eqkID and g.eqkID=c.eqkID and f.type='P' and g.type='S' and f.wei<={maxpw} and g.wei<={maxpw} and f.stacode=g.stacode and f.stacode=h.stacode and g.stacode=h.stacode and f.loco=h.loco and g.loco=h.loco and f.loco=g.loco and (h.lat between {minlat} and {maxlat}) and (h.lon between {minlon} and {maxlon}) and (h.elev between {mineqdep} and {maxeqdep}) and c.model={modtype} and c.lcode={codetype} and c.method={mettype} and (c.origintime between '{mintime}' and '{maxtime}') group by f.eqkID) as tblPS,
(select f.eqkID as ID,count(*) as counterP from phases f, eqlocations c where f.eqkID=c.eqkID and f.type='P' and f.wei<={maxpw} and c.model={modtype} and c.lcode={codetype} and c.method={mettype} and (c.origintime between '{mintime}' and '{maxtime}') group by f.eqkID) as tblP,
(select g.eqkID as ID,count(*) as counterS from phases g, eqlocations c where g.eqkID=c.eqkID and g.type='S' and g.wei<={maxsw} and c.model={modtype} and c.lcode={codetype} and c.method={mettype} and (c.origintime between '{mintime}' and '{maxtime}') group by g.eqkID) as tblS,
eqlocations c, eqstatistics d, magnitudes e, phases f, phases g, stations h
where
h.stacode='{stacode}'
and tblPS.counterPS >= {minps}
and tblP.counterP >= {minnp}
and tblS.counterS >= {minns}
and f.wei<= {maxvpvspw}
and g.wei<= {maxvpvssw}
and d.gap <= {maxgap}
and d.mindist <= {midi}
and (d.errh <= {maxherr} or sqrt(pow(d.errx,2)+pow(d.erry,2)) <= {maxherr})
and d.errv <= {maxverr}
and c.eqkID=tblPS.ID
and c.eqkID=tblP.ID
and c.eqkID=tblS.ID
and c.model=1
and c.lcode={codetype}
and c.method={mettype}
and (c.lat between {minlat} and {maxlat})
and (c.lon between {minlon} and {maxlon})
and (c.elev between {mineqdep} and {maxeqdep})
and c.id=d.idloc
and c.eqkID=e.eqkID
and e.idloc=c.id
and (c.origintime between '{mintime}' and '{maxtime}')
and f.eqkID=g.eqkID
and c.eqkID=f.eqkID
and f.eqkID=g.eqkID
and g.eqkID=c.eqkID
and f.type='P'
and g.type='S'
and f.stacode=h.stacode
and h.stacode=g.stacode
and f.stacode=g.stacode
and f.loco=h.loco
and h.loco=g.loco
and f.loco=g.loco
having (vpvs_value > {vpvsmin}) and vpvs_error <= {maxvpvserr}
order by c.origintime asc
limit 1000000;
'''.format(**args)
# uncomment lines 75, 76 in order to echo the query to the screen and stop
# self.send_success_response(query)
# return
db.query(query)
rs = db.store_result()
# self.send_success_response(json.dumps(dict(result=rs.fetch_row(maxrows=0, how=1))))
# db.close()
resp = self.render_string(
'response.json', result=json.dumps(rs.fetch_row(maxrows=0, how=1)))
self.write(resp)
self.set_header('Content-Type', 'application/json')
return
else:
errors = [e.message for e in user_request.global_errors]
errors.extend(["{0}: {1}".format(param.varname, error.message)
for param, error in user_request.errors])
return self.send_error_response(errors)
class StationMinMaxHandler(handler.APIBaseHandler):
def initialize(self, config):
self.config = config
def get(self):
manager = RequestManagerVPVSStations()
user_request = manager.bind(self).validate()
if user_request.is_valid:
args = user_request.getArgs()
db = _mysql.connect(self.config.get('db', 'host'),
self.config.get('db', 'user'),
self.config.get('db', 'pass'),
self.config.get('db', 'db'))
query = 'select stacode as station_code, min(pick) as mintime, max(pick) as maxtime from phases GROUP BY stacode %s'
station_where = ''
if args['stacode'] != '0000':
station_where = "where stacode = '{stacode}'"
query = query % station_where
query = query.format(**args)
logging.info(query)
db.query(query)
rs = db.store_result()
self.write('''
{
"@context": {},
"header": {},
"result": [
''')
is_first = True
for row in rs.fetch_row(maxrows=0, how=1):
if is_first is True:
is_first = False
else:
self.write(',')
row['mintime'] = 'T'.join(row['mintime'].split(' '))
row['maxtime'] = 'T'.join(row['maxtime'].split(' '))
self.write(json.dumps(row))
self.write(']}')
self.set_header('Content-Type', 'application/json')
else:
errors = [e.message for e in user_request.global_errors] + [e.message for (p, e) in user_request.errors]
return self.send_error_response(errors)
class StationHandler(handler.APIBaseHandler):
def initialize(self, config):
self.config = config
def get(self):
manager = RequestManagerVPVSStations()
user_request = manager.bind(self).validate()
if user_request.is_valid:
args = user_request.getArgs()
db = _mysql.connect(self.config.get('db', 'host'),
self.config.get('db', 'user'),
self.config.get('db', 'pass'),
self.config.get('db', 'db'))
query = 'select DISTINCT(stacode) as station_code, birth as start_time, death as end_time, lat as station_latitude, lon as station_longitude, elev as station_elevation, place from stations s %s'
station_where = ''
if args['stacode'] != '0000':
station_where = "where stacode = '{stacode}'"
query = query % station_where
query = query.format(**args)
logging.info(query)
db.query(query)
rs = db.store_result()
self.write('''
{
"@context": {},
"header": {},
"result": [
''')
is_first = True
for row in rs.fetch_row(maxrows=0, how=1):
if is_first is True:
is_first = False
else:
self.write(',')
row['start_time'] = row['start_time'] + 'T00:00:00'
row['end_time'] = row['end_time'] + 'T00:00:00'
self.write(json.dumps(row))
self.write(']}')
self.set_header('Content-Type', 'application/json')
else:
errors = [e.message for e in user_request.global_errors] + [e.message for (p, e) in user_request.errors]
return self.send_error_response(errors)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
queries = []
# add some example queries
queries.append(dict(
mintime='2001-01-01T00:00:00.000',
maxtime='2000-01-01T00:00:00.000',
minlat=30, # -90, 90 and smaller than maxlat
maxlat=50, # -90, 90
minlon=40, # -180, 180 and smaller than maxlon
maxlon=60, # -180, 180
mineqdep=-70, # -6378, -9 and smaller than maxeqdep
maxeqdep=-30, # -6378, -9
minnp=2, # number of p waves, int, min: 0
minns=3, # number of s waves, int, min: 0
maxpw=3, # int, 0, 4
maxsw=3, # int, 0, 4
minps=4, # 0, +
maxvpvspw=3,
maxvpvssw=3,
maxgap=150, # azim gap 0, 360
midi=70, # horiz dist of closest sta
maxherr=300, # 0, 4000
maxverr=200, # 0, 6378
maxvpvserr=1000000,
DIV=1000000,
vpvsmin=1.41,
modtype=1,
codetype=2,
mettype=2
))
# this works on the TABOO database
# queries.append(dict(
# mintime='2015-01-01T00:00:00.000',
# maxtime='2015-05-01T00:00:00.000',
# minlat=43.10, # -90, 90 and smaller than maxlat
# maxlat=43.65, # -90, 90
# minlon=12.10, # -180, 180 and smaller than maxlon
# maxlon=12.65, # -180, 180
# mineqdep=0.4, # -9, 6378 and smaller than maxeqdep
# maxeqdep=1.2, # -9, 6378
# minnp=2, # number of p waves, int, min: 0
# minns=3, # number of s waves, int, min: 0
# maxpw=3, # int, 0, 4
# maxsw=3, # int, 0, 4
# minps=4, # 0, +
# maxvpvspw=3,
# maxvpvssw=3,
# maxgap=150, # azim gap 0, 360
# midi=70, # horiz dist of closest sta
# maxherr=300, # 0, 4000
# maxverr=200, # 0, 6378
# maxvpvserr=1000000,
# DIV=1000000,
# vpvsmin=1.41,
# modtype=1,
# codetype=2,
# mettype=2
# ))
# this works on the NIEP database
queries.append(dict(
minnp=2,
vpvsmin=1.41,
codetype=2,
minlon=19,
maxgap=150,
mintime='2017-01-01T00:00:00.000',
maxverr=200,
modtype=1,
minlat=42,
maxlon=30,
midi=70,
maxvpvserr=1000000,
mineqdep=0.0,
DIV=1000000,
maxsw=4,
minns=3,
maxvpvssw=4,
maxlat=48,
minps=4,
maxeqdep=200,
mettype=2,
maxtime='2017-05-01T00:00:00.000',
maxherr=300,
maxvpvspw=4,
maxpw=4
))
for idx, q in enumerate(queries):
queries[idx] = '&'.join(['{}={}'.format(k, v)
for k, v in q.iteritems()])
# transform the queries into http query strings
queries = ['/query?%s' % q for q in queries]
manager = RequestManagerVPVS()
self.render('index.html', queries=queries, manager=manager)
class DcatHandler(handler.APIBaseHandler):
def initialize(self, config):
self.config = config
def get(self):
def recurse(validator, vartype=None, type_tags=None, descriptions=None):
if type_tags is None:
type_tags = []
if descriptions is None:
descriptions = []
# print validator
try:
if isinstance(validator, list):
results = [None, None, [], []]
for crt_validator in validator:
_, crt_vartype, crt_type_tags, crt_descriptions = recurse(
crt_validator, vartype, type_tags, descriptions)
if results[1] is None:
results[1] = crt_vartype
results[2].extend(crt_type_tags)
results[3].append(crt_validator.describe())
results[2] = set(results[2])
results[3] = set(results[3])
return results
except TypeError:
# print "not a list of validators"
pass
try:
vartype = validator.type
except AttributeError:
# print "does not have validator.type"
pass
try:
type_tags.extend(validator.type_tags)
# print type_tags
except AttributeError:
# print "does not have validator.type_tags"
pass
try:
# print "recursing to internal_validators"
return recurse(validator.internal_validators, vartype, type_tags)
except AttributeError:
# print e
# print "does not have internal_validators"
return (validator, vartype, type_tags, [validator.describe()])
manager = RequestManagerVPVS()
descriptions = []
for param in manager.rq.parameters:
for validator in param.validators:
validator_description = recurse(validator)
# parameter name, parameter primitive type, validation tags,
# validator descriptions
validation_hints = list(validator_description[2])
if param.unit:
validation_hints.append('unit:{}'.format(param.unit))
descriptions = list(validator_description[3])
description = param.describe()
if description:
description.strip()
descriptions.append(description)
descriptions.append([param.varname, validator_description[
1], validation_hints, descriptions])
self.set_header("Content-Type", 'application/xml; charset="utf-8"')
# self.set_header("Content-Disposition", "attachment; filename=dcat.xml")
try:
self.render('dcat.xml', descriptions=descriptions)
except IOError:
self.send_error_response(
'Please move dcat.sample.xml to dcat.xml and customise.')
if __name__ == "__main__":
cfg = ConfigParser()
cfg.read('config.ini')
# if environment configuration is present then override whatever there is
# usefull for overriding configuration in Docker mode
# environment variables should look like this EP_SECTIONNAME_VARNAME_HERE='value'
# EP_DB_HOST=<ip or hostname of database>
# EP_DB_USER=<username of db>
# EP_DB_PASS=<password to the database>
# EP_DB_DB=<name of database>
# EP_SERVICE_PORT=<port on which the app is running>
env_config = {key[3:].lower(): val for key, val in os.environ.items() if key.startswith('EP_')}
for k, v in env_config.items():
keyparts = k.split('_')
sectionname = keyparts[0]
varname = '_'.join(keyparts[1:])
try:
cfg.set(sectionname, varname, v)
except NoSectionError:
cfg.add_section(sectionname)
cfg.set(sectionname, varname, v)
settings = dict(
debug=True,
template_path='templates/'
)
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/query", MainHandler, dict(config=cfg)),
(r"/dcat", DcatHandler, dict(config=cfg)),
(r"/stationsminmax", StationMinMaxHandler, dict(config=cfg)),
(r"/stations", StationHandler, dict(config=cfg)),
], **settings)
application.listen(cfg.get('service', 'port'))
tornado.ioloop.IOLoop.current().start()