# http://pyrocko.org - GPLv3 # # The Pyrocko Developers, 21st Century # ---|P------/S----------~Lg----------
StringPattern, List, Dict, Any
return (a > b) - (a < b)
hashlib.sha1(s.encode('utf8')).digest()).decode('ascii'))
''' Representation of a seismic event.
:param lat: latitude of hypocenter (default 0.0) :param lon: longitude of hypocenter (default 0.0) :param time: origin time system timestamp :param name: event identifier as string (optional) :param depth: source depth (optional) :param magnitude: magnitude of event (optional) :param region: source region (optional) :param catalog: name of catalog that lists this event (optional) :param moment_tensor: moment tensor as :py:class:`moment_tensor.MomentTensor` instance (optional) :param duration: source duration as float (optional) :param tags: list of tags describing event (optional) :param extras: dictionary for user defined event attributes (optional). Keys must be strings, values must be YAML serializable. '''
self, lat=0., lon=0., north_shift=0., east_shift=0., time=0., name='', depth=None, elevation=None, magnitude=None, magnitude_type=None, region=None, load=None, loadf=None, catalog=None, moment_tensor=None, duration=None, tags=None, extras=None):
magnitude_type, region, catalog, moment_tensor, duration, \ tags = vals
self, lat=lat, lon=lon, north_shift=north_shift, east_shift=east_shift, time=time, name=name, depth=depth, elevation=elevation, magnitude=magnitude, magnitude_type=magnitude_type, region=region, catalog=catalog, moment_tensor=moment_tensor, duration=duration, tags=tags, extras=extras)
return util.time_to_str(self.time)
self.name = name
raise EventExtrasDumpError( 'Event user-defined extras attributes cannot be dumped in the ' '"basic" event file format. Use ' 'dump_events(..., format="yaml").')
file.write('north_shift = %.12g\n' % self.north_shift) file.write('east_shift = %.12g\n' % self.east_shift)
moment_tensor.magnitude_to_moment(self.magnitude)) 'mnn = %g\nmee = %g\nmdd = %g\nmne = %g\nmnd = %g\nmed = %g\n' 'strike1 = %g\ndip1 = %g\nrake1 = %g\n' 'strike2 = %g\ndip2 = %g\nrake2 = %g\n') % ( (m[0, 0], m[1, 1], m[2, 2], m[0, 1], m[0, 2], m[1, 2]) + sdr1 + sdr2))
cmp(a.catalog, b.catalog))): groups = Event.grouped(events, deltat)
events = [] for group in groups: if group: group.sort(group_cmp) events.append(group[-1])
return events
events = list(events) groups = [] for ia, a in enumerate(events): groups.append([]) haveit = False for ib, b in enumerate(events[:ia]): if abs(b.time - a.time) < deltat: groups[ib].append(a) haveit = True break
if not haveit: groups[ia].append(a)
groups = [g for g in groups if g] groups.sort(key=lambda g: sum(e.time for e in g) // len(g)) return groups
else: file = stream
finally:
def oldload(filename):
def oldloadf(file): continue
'north_shift east_shift ' 'mnn mee mdd mne mnd med strike1 dip1 rake1 ' 'strike2 dip2 rake2 duration').split()):
except Exception as e: raise FileParseError(e)
raise EOF()
raise EmptyEvent()
mt = moment_tensor.MomentTensor(m=moment_tensor.symmat6(*m6)) else: moment = 1.0 if 'moment' in d: moment = d['moment'] elif 'magnitude' in d: moment = moment_tensor.magnitude_to_moment(d['magnitude'])
mt = moment_tensor.MomentTensor( strike=sdr[0], dip=sdr[1], rake=sdr[2], scalar_moment=moment)
d.get('latitude', 0.0), d.get('longitude', 0.0), d.get('north_shift', 0.0), d.get('east_shift', 0.0), d.get('time', 0.0), d.get('name', ''), d.get('depth', None), d.get('magnitude', None), d.get('magnitude_type', None), d.get('region', None), d.get('catalog', None), mt, d.get('duration', None), d.get('tags', []))
def load_catalog(filename):
pass
else: stime = util.time_to_str(e.time, format='%Y-%m-%d %H:%M:%S.6FRAC')
stime, s(e.lat), s(e.lon), s(e.depth), float_or_none_to_str(e.magnitude, 5), str(e.catalog), str(e.name or ''), str(e.region)))
s = [ 'Latitude [deg]: %g' % self.lat, 'Longitude [deg]: %g' % self.lon, 'Time [UTC]: %s' % util.time_to_str(self.time)]
if self.name: s.append('Name: %s' % self.name)
if self.depth is not None: s.append('Depth [km]: %g' % (self.depth / 1000.))
if self.magnitude is not None: s.append('Magnitude [%s]: %3.1f' % ( self.magnitude_type or 'M?', self.magnitude))
if self.region: s.append('Region: %s' % self.region)
if self.catalog: s.append('Catalog: %s' % self.catalog)
if self.moment_tensor: s.append(str(self.moment_tensor))
return '\n'.join(s)
else:
return 'basic'
''' Read events file.
:param filename: name of file as str :param format: file format: ``'detect'``, ``'basic'``, or ``'yaml'`` :returns: list of :py:class:`Event` objects '''
ev for ev in guts.load_all(filename=filename) if isinstance(ev, Event)]
else: from pyrocko.io.io_common import FileLoadError raise FileLoadError('unknown event file format: %s' % format)
raise OneEventRequired( 'exactly one event is required in "%s"' % filename)
return events[0]
''' Write events file.
:param events: list of :py:class:`Event` objects :param filename: name of file as str :param format: file format: ``'basic'``, or ``'yaml'`` '''
elif format == 'yaml': from pyrocko import guts events = [ev for ev in events if isinstance(ev, Event)] guts.dump_all(object=events, filename=filename, stream=None)
else: from pyrocko.io.io_common import FileSaveError raise FileSaveError('unknown event file format: %s' % format)
elist = [] f = open(filename, 'r') for line in f: toks = line.split() if len(toks) < 7: continue
tim = util.to_time_float(util.ctimegm(toks[0]+' '+toks[1])) lat, lon, depth, magnitude = [float(x) for x in toks[2:6]] duration = float(toks[10]) region = toks[-1] name = util.gmctime_fn(tim) e = Event( lat, lon, tim, name=name, depth=depth, magnitude=magnitude, duration=duration, region=region)
elist.append(e)
f.close() return elist
from pyrocko import catalog cat = catalog.Geofon()
elist = [] f = open(filename, 'r') for line in f: e = cat.get_event(line.strip()) elist.append(e)
f.close() return elist |