-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqrecord.py
1185 lines (1028 loc) · 33.2 KB
/
eqrecord.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: iso-Latin-1 -*-
"""
Classes corresponding to records in database
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
if sys.version_info[0] == 2:
## Python 2
PY2 = True
else:
## Python 3
PY2 = False
basestring = str
## Import standard python modules
import datetime
import time
import json
from collections import OrderedDict
## Third-party modules
import numpy as np
## Import ROB modules
import mapping.geotools.geodetic as geodetic
## Import package submodules
from . import msc
from . import time as timelib
__all__ = ["LocalEarthquake", "FocMecRecord"]
# TODO: allow nan values instead of zeros
class LocalEarthquake(object):
"""
Class representing a local earthquake retrieved from the
earthquakes database table.
Provides methods to convert magnitudes, and compute distances.
:param ID:
str or int, ID of earthquake in earthquakes table
:param date:
instance of :class:`np.datetime64` or :class:`datetime.date`
or ISO-8601 formatted string, date when the earthquake occurred
Note: if :param:`time` is None, date is interpreted as datetime
:param time:
instance of :class:`datetime.time`, time when earthquake occurred
:param lon:
Float, longitude of epicenter in decimal degrees
:param lat:
Float, latitude of epicenter in decimal degrees
:param depth:
Float, hypocentral depth in km
:param mag:
dict, mapping magnitude types (str) to magnitude values (floats)
:param ML:
Float, local magnitude
(default: np.nan)
:param MS:
Float, surface-wave magnitude
(default: np.nan)
:param MW:
Float, moment magnitude
(default: np.nan)
:param mb:
Float, body-wave magnitude
(default: np.nan)
:param name:
String, name of location
(default: "")
:param intensity_max:
Int, maximum intensity
(default: None)
:param macro_radius:
Float, macroseismic radius
(default: None)
:param errh:
Float, uncertainty on epicentral location, in km
(default: 0)
:param errz:
Float, uncertainty on hypocentral depth, in km
(default: 0)
:param errt:
Float, uncertainty on origin time, in s
(default: 0)
:param errM:
Float, uncertainty on magnitude
(default: 0)
:param zone:
Str, seismotectonic zone the earthquake belongs to
(default: "")
:param agency:
str, agency reporting the event
(default: "")
:param event_type:
str, type of event
(default: "ke" = known earthquake)
"""
def __init__(self,
ID,
date,
time,
lon,
lat,
depth,
mag,
ML=np.nan,
MS=np.nan,
MW=np.nan,
mb=np.nan,
name="",
intensity_max=None,
macro_radius=None,
errh=0.,
errz=0.,
errt=0.,
errM=0.,
zone="",
agency="",
event_type="ke"):
self.ID = ID
if time is None:
try:
self.datetime = timelib.as_np_datetime(date)
except:
raise TypeError("datetime not of correct type")
else:
try:
date = timelib.as_np_date(date)
except:
raise TypeError("date not of correct type")
try:
self.datetime = timelib.combine_np_date_and_py_time(date, time, unit='ms')
except:
raise TypeError("time not of correct type")
self.lon = lon
self.lat = lat
self.depth = depth
#self.mag = mag
if not mag:
self.mag = {}
elif isinstance(mag, dict):
self.mag = mag
else:
raise Exception("mag must be a dictionary!")
if not ML in (None, np.nan):
self.mag['ML'] = ML
if not MS in (None, np.nan):
self.mag['MS'] = MS
if not MW in (None, np.nan):
self.mag['MW'] = MW
if not mb in (None, np.nan):
self.mag['mb'] = mb
"""
try:
self.lon = float(lon)
except TypeError:
self.lon = lon
try:
self.lat = float(lat)
except TypeError:
self.lat = lat
try:
self.depth = float(depth)
except TypeError:
self.depth = depth
try:
self.ML = float(ML)
except TypeError:
self.ML = ML
try:
self.MS = float(MS)
except TypeError:
self.MS = MS
try:
self.MW = float(MW)
except TypeError:
self.MW = MW
"""
self.name = name
self.intensity_max = intensity_max
self.macro_radius = macro_radius
self.errh = errh
self.errz = errz
self.errt = errt
# TODO: errM should be dict as well
self.errM = errM
self.zone = u'' + zone
self.agency = u'' + agency
self.event_type = event_type
def __eq__(self, eq):
if isinstance(eq, self.__class__) and self.ID == eq.ID:
return True
else:
return False
def __repr__(self):
mag_txt = ', '.join(['%s=%.1f' % (Mtype, getattr(self, Mtype))
for Mtype in self.get_Mtypes()])
txt = '<EQ #%s | %s %s | %s | %.3f %.3f %.1f km | %s | %s>'
txt %= (self.ID, self.date, self.time, self.name, self.lon, self.lat,
self.depth, mag_txt, self.event_type)
return txt
@classmethod
def from_json(cls, s):
"""
Generate instance of :class:`LocalEarthquake` from a json string
:param s:
String, json format
"""
dct = json.loads(s)
if len(dct) == 1:
class_name = dct.keys()[0]
#if class_name == LocalEarthquake.__class__.__name__:
if class_name == "__LocalEarthquake__":
return cls.from_dict(dct[class_name])
#return LocalEarthquake.__init__(self, **json.loads(s))
@classmethod
def from_dict(cls, dct):
"""
Generate instance of :class:`LocalEarthquake` from a dictionary
:param dct:
Dictionary
"""
if 'datetime' in dct:
dt = timelib.as_np_datetime(dct['datetime'], unit='ms')
dct['date'] = timelib.as_np_date(dt)
dct['time'] = timelib.to_py_time(dt)
del dct['datetime']
else:
if 'time' in dct:
dct['time'] = datetime.time(*dct['time'])
if 'date' in dct:
#dct['date'] = datetime.date(*dct['date'])
dct['date'] = timelib.time_tuple_to_np_datetime(*dct['date'])
if not 'mag' in dct:
dct['mag'] = {}
for key in dct.keys():
if key[0].upper() == 'M' and len(key) == 2:
dct['mag'][key] = dct[key]
del dct[key]
return cls(**dct)
@classmethod
def from_dict_rec(cls, rec, column_map={}, date_sep='-', time_sep=':',
date_order='YMD', null_value=0):
"""
Construct instance of :class:`LocalEarthquake` from a dict-like
record mapping earthquake property names to values. If keys
do not correspond to standard names, a column map should provide
mapping between standard property names and keys in record.
:param rec:
dict-like, earthquake record
:param column_map:
dict, mapping property names of :class:`LocalEarthquake` to
column names in header or to column numbers (zero-based) if no
header is present.
(default: {})
:param date_sep:
str, character separating date elements
(default: '-')
:param time_sep:
str, character separating time elements
(default: ':')
:param date_order:
str, order of year (Y), month (M), day (D) in date string
(default: 'YMD')
:param null_value:
float, value to use for NULL values (except magnitude)
(default: 0)
:return:
instance of :class:`LocalEarthquake`
"""
## If key is not in column_map, we assume it has the default name
ID_key = column_map.get('ID', 'ID')
ID = rec.get(ID_key)
datetime_key = column_map.get('datetime', 'datetime')
if datetime_key in rec:
dt = timelib.as_np_datetime(rec[datetime_key], unit='ms')
date = timelib.as_np_date(dt)
time = timelib.to_py_time(dt)
else:
date_key = column_map.get('date', 'date')
date = rec.get(date_key)
## Year must always be present
## Silently convert month/day to 1 if it is zero
if date:
if isinstance(date, basestring):
if date_sep:
date_elements = date.split(date_sep)
elif len(date) == 8:
if date_order == 'YMD':
date_elements = date[:4], date[4:6], date[6:]
elif date_order== 'DMY':
date_elements = date[:2], date[2:4], date[4:]
year = int(date_elements[date_order.index('Y')])
try:
month = max(1, int(date_elements[date_order.index('M')]))
except IndexError:
month = 1
try:
day = max(1, int(date_elements[date_order.index('D')]))
except:
day = 1
elif isinstance(date, datetime.date):
year, month, day = date.year, date.month, date.day
else:
year_key = column_map.get('year', 'year')
year = int(rec[year_key])
month_key = column_map.get('month', 'month')
month = max(1, int(rec.get(month_key, 1) or 1))
day_key = column_map.get('day', 'day')
day = max(1, int(rec.get(day_key, 1) or 1))
try:
date = timelib.time_tuple_to_np_datetime(year, month, day)
except:
print("Invalid date in rec %s: %s-%s-%s"
% (ID, year, month, day))
date = None
time_key = column_map.get('time', 'time')
time = rec.get(time_key)
if time:
if isinstance(time, basestring):
if time_sep:
time_elements = time.split(time_sep)
else:
time_elements = time[:2], time[2:4], time[4:]
try:
hour = min(23, int(time_elements[0]))
except (IndexError, ValueError):
hour = 0
try:
minute = min(59, int(time_elements[1]))
except (IndexError, ValueError):
minute = 0
try:
second = min(59, float(time_elements[2]))
except (IndexError, ValueError):
second = 0.
elif isinstance(time, datetime.time):
hour, minute, second = time.hour, time.minute, time.second
second += (time.microsecond * 1E-6)
else:
hour_key = column_map.get('hour', 'hour')
hour = min(23, int(rec.get(hour_key, 0) or 0))
minute_key = column_map.get('minute', 'minute')
minute = min(59, int(rec.get(minute_key, 0) or 0))
second_key = column_map.get('second', 'second')
second = float(rec.get(second_key, 0) or 0)
fraction, second = np.modf(second)
second = min(59, int(second))
microsecs = int(round(fraction * 1E+6))
try:
time = datetime.time(hour, minute, second, microsecs)
except:
print(ID, second)
raise
# TODO: find a better solution than using or
# this should only be used when parameter is None, not if it is zero!
lon_key = column_map.get('lon', 'lon')
lon = float(rec.get(lon_key, null_value) or null_value)
lat_key = column_map.get('lat', 'lat')
lat = float(rec.get(lat_key, null_value) or null_value)
depth_key = column_map.get('depth', 'depth')
depth = float(rec.get(depth_key, null_value) or null_value)
mag = {}
## Magnitude specified as value and magnitude type
Mtype_key = column_map.get('Mtype', 'Mtype')
if Mtype_key in rec:
Mtype = rec.get(Mtype_key)
if Mtype:
if len(Mtype) == 1:
Mtype = 'M' + Mtype
if Mtype[-1] == 'b':
Mtype = Mtype.lower()
else:
Mtype = Mtype.upper()
Mag_key = column_map.get('Mag', 'Mag')
M = float(rec.get(Mag_key, np.nan))
mag[Mtype] = M
## Specific magnitude type column
## takes precedence over value/magnitude type columns
ML_key = column_map.get('ML', 'ML')
ML = float(rec.get(ML_key, mag.get('ML', np.nan)) or np.nan)
mag['ML'] = ML
MS_key = column_map.get('MS', 'MS')
MS = float(rec.get(MS_key, mag.get('MS', np.nan)) or np.nan)
mag['MS'] = MS
MW_key = column_map.get('MW', 'MW')
MW = float(rec.get(MW_key, mag.get('MW', np.nan)) or np.nan)
mag['MW'] = MW
mb_key = column_map.get('mb', 'mb')
mb = float(rec.get(mb_key, mag.get('mb', np.nan)) or np.nan)
mag['mb'] = mb
name_key = column_map.get('name', 'name')
name = rec.get(name_key, "")
intensity_max_key = column_map.get('intensity_max', 'intensity_max')
intensity_max = rec.get(intensity_max_key, null_value)
if intensity_max:
if isinstance(intensity_max, basestring):
## Strip trailing + and - if present
if intensity_max[-1] in ('+', '-'):
intensity_max = intensity_max[:-1]
## Take average if Imax is specified as range
Imax_vals = list(map(float, intensity_max.split('-')))
intensity_max = np.mean(Imax_vals)
else:
intensity_max = 0.
macro_radius_key = column_map.get('macro_radius', 'macro_radius')
macro_radius = float(rec.get(macro_radius_key, 0) or 0)
errh_key = column_map.get('errh', 'errh')
errh = float(rec.get(errh_key, null_value) or null_value)
errz_key = column_map.get('errz', 'errz')
errz = float(rec.get(errz_key, null_value) or null_value)
errt_key = column_map.get('errt', 'errt')
errt = float(rec.get(errt_key, null_value) or null_value)
errM_key = column_map.get('errM', 'errM')
errM = float(rec.get(errM_key, null_value) or null_value)
zone_key = column_map.get('zone', 'zone')
zone = str(rec.get(zone_key, zone_key if not zone_key == 'zone' else ""))
agency_key = column_map.get('agency', 'agency')
agency = str(rec.get(agency_key, agency_key if not agency_key == 'agency' else ""))
event_type_key = column_map.get('event_type', 'event_type')
event_type = str(rec.get(event_type_key, event_type_key if not event_type_key == 'event_type' else 'ke'))
return cls(ID, date, time, lon, lat, depth, mag, name=name,
intensity_max=intensity_max, macro_radius=macro_radius,
errh=errh, errz=errz, errt=errt, errM=errM, zone=zone,
agency=agency, event_type=event_type)
def to_dict(self):
"""
Convert to a dictionary
:return:
instance of :class:`dict`
"""
from copy import deepcopy
return deepcopy(self.__dict__)
def dump_json(self):
"""
Generate json string
"""
from mapping.geotools.json_handler import json_handler
key = '__%s__' % self.__class__.__name__
dct = {key: self.__dict__}
if PY2:
return json.dumps(dct, default=json_handler, encoding="latin1")
else:
return json.dumps(dct, default=json_handler)
@classmethod
def from_HY4(self, hypdat, Mtype='ML', ID=0):
"""
Construct from HYPDAT structure used in HY4 catalog format
used by SeismicEruption
:param hypdat:
instance of :class:`HYPDAT`
:param Mtype:
Str, magnitude type, either 'ML', 'MS' or 'MW' (default: 'ML')
:param ID:
Int, identifier
"""
lat = hypdat.latitude * 0.001 / 3600.0
lon = hypdat.longitude * 0.001 / 3600.0
year = int(hypdat.year)
month = int(hypdat.month)
day = int(hypdat.day)
date = datetime.date(year, month, day)
hour, minutes = divmod(hypdat.minutes, 60)
seconds = hypdat.tseconds * 0.1
time = datetime.time(hour, minutes, seconds)
depth = hypdat.depth / 100000.0
magnitude = hypdat.magnitude / 10.0
ML = {True: magnitude, False: 0.}[Mtype == 'ML']
MS = {True: magnitude, False: 0.}[Mtype == 'MS']
MW = {True: magnitude, False: 0.}[Mtype == 'MW']
return LocalEarthquake(ID, date, time, lon, lat, depth, ML, MS, MW)
def to_HY4(self, Mtype='ML', Mrelation={}):
"""
Convert to HYPDAT structure used by SeismicEruption HY4 catalog format
:param Mtype:
String, magnitude type: "MW", "MS" or "ML" (default: "ML")
:param Mrelation:
{str: str} dict, mapping name of magnitude conversion relation
to magnitude type ("MW", "MS" or "ML")
(default: {})
:return:
instance of :class:`HYPDAT`
"""
from .io.HY4 import HYPDAT
latitude = int(round(self.lat * 3600 / 0.001))
longitude = int(round(self.lon * 3600 / 0.001))
year, month, day, tm_hour, tm_min, tm_sec = timelib.to_time_tuple(self.datetime)
minutes = int(round(tm_hour * 60 + tm_min))
tseconds = int(round(tm_sec / 0.1))
depth = int(round(np.nan_to_num(self.depth) * 100000))
M = self.get_M(Mtype, Mrelation)
magnitude = int(round(M * 10))
return HYPDAT(latitude, longitude, year, month, day, minutes, tseconds,
depth, magnitude, 0, 0, 0)
def to_hypo71(self, Mtype='MW', Mrelation={}):
"""
Convert earthquake record to HYPO71-2000 format.
:param Mtype:
:param Mrelation:
see :meth:`get_mag`
:return:
str
"""
year, month, day = map(int, str(self.date).split('-'))
hr, minute, sec = str(self.time).split(':')
hr, minute, sec = int(hr), int(minute), float(sec)
lonmin, londeg = np.modf(np.abs(self.lon))
lonmin, londeg = lonmin * 60, londeg
ew = 'E' if self.lon > 0 else ' '
latmin, latdeg = np.modf(np.abs(self.lat))
latmin, latdeg = latmin * 60, latdeg
ns = 'S' if self.lat < 0 else ' '
mag = self.get_M(Mtype, Mrelation)
hypo71 = ('%04d%02d%02d ' % (year, month, day),
'%02d%02d%6.2f' % (hr, minute, sec),
'%3.0f%c%5.2f' % (latdeg, ns, latmin),
'%4.0f%c%5.2f' % (londeg, ew, lonmin),
'%7.2f ' % np.nan_to_num(self.depth),
'%c%5.2f' % (Mtype[-1], mag),
' ' * 17,
'%5.1f%5.1f' % (self.errh, self.errz),
' ' * 4,
'%10s' % str(self.ID)[:10],
' ' * 5)
hypo71 = ''.join(hypo71)
return hypo71
def print_info(self):
"""
Print earthquake attributes in pretty table.
"""
try:
from prettytable import PrettyTable
except:
has_prettytable = False
else:
has_prettytable = True
col_names = ["Attribute", "Value"]
if has_prettytable:
tab = PrettyTable(col_names)
else:
tab = []
for attrib in ['ID', 'name', 'date', 'time', 'lon', 'lat', 'depth',
'ML', 'MS', 'MW', 'mb', 'intensity_max', 'macro_radius',
'zone', 'errt', 'errh', 'errz', 'errM', 'zone',
'agency', 'event_type']:
val = getattr(self, attrib)
if val and not val is np.nan:
row = [attrib, val]
if has_prettytable:
tab.add_row(row)
else:
tab.append(row)
if has_prettytable:
print(tab)
else:
print('\t'.join(col_names))
for row in tab:
print('\t'.join(row))
def copy(self):
"""
Copy LocalEarthquake object
"""
return self.from_dict(self.to_dict())
## date-time-related methods
@property
def date(self):
return timelib.as_np_date(self.datetime)
@property
def time(self):
return timelib.to_py_time(self.datetime)
@property
def year(self):
return timelib.to_year(self.datetime)
def get_fractional_year(self):
"""
Compute fractional year of event
:return:
Float, fractional year
"""
#from .time import fractional_year
return timelib.to_fractional_year(self.datetime)
def get_fractional_hour(self):
"""
Compute fractional hour of event
:return:
Float, fractional hour
"""
return timelib.py_time_to_fractional_hours(self.time)
def get_weekday(self):
"""
Determine day of week
:return:
int, day of week (0=Monday)
"""
return timelib.to_py_date(self.datetime).weekday()
## Magnitude-related methods
@property
def ML(self):
"""Local magnitude"""
return self.get_mag('ML')
@property
def MS(self):
"""Surface-wave magnitude"""
return self.get_mag('MS')
@property
def MW(self):
"""Moment magnitude"""
return self.get_mag('MW')
@property
def mb(self):
"""Body-wave magnitude"""
return self.get_mag('mb')
def has_mag(self, Mtype):
"""
Determine whether particular magnitude type is defined
:param Mtype:
str, magnitude type to check
:return:
bool
"""
if Mtype in self.mag and not np.isnan(self.mag[Mtype]):
return True
else:
return False
def get_Mtypes(self):
"""
Get defined magnitude types
:return:
list of strings
"""
return [Mtype for Mtype in self.mag.keys() if not np.isnan(self.mag[Mtype])]
def get_mag(self, Mtype):
"""
Return particular magnitude without conversion.
If magnitude type is not defined, NaN is returned.
:param Mtype:
str, magnitude type to return
:return:
float or np.nan, magnitude
"""
return self.mag.get(Mtype, np.nan)
def set_mag(self, Mtype, mag):
"""
Set magnitude type to given value
:param Mtype:
str, magnitude type to set
:param mag:
float, magnitude value
"""
self.mag[Mtype] = mag
def get_or_convert_mag(self, Mtype, Mrelation={}):
"""
Return particular magnitude. If the magnitude type is not defined,
it will be converted from another magnitude type.
:param Mtype:
str, magnitude type to return
:param Mrelation:
{str: str} dict, mapping magnitude types to names of magnitude
conversion relations
Note: use an ordered dict if more than 1 magnitude type is
specified, as the first magnitude type that is defined will
be used for conversion.
If specified as "default" or None, the default Mrelation
for the given Mtype will be selected.
(default: {})
"""
if self.has_mag(Mtype):
return self.mag[Mtype]
else:
#if Mrelation in (None, "default"):
# Mrelation = default_Mrelations.get(Mtype, {})
return self.convert_mag(Mrelation)
def convert_mag(self, Mrelation):
"""
Return magnitude based on conversion.
:param Mrelation:
{str: str} dict, mapping magnitude types to names of magnitude
conversion relations
Note: use an ordered dict if more than 1 magnitude type is
specified, as the first magnitude type that is defined will
be used for conversion
(default: {})
"""
if len(Mrelation) > 1 and not isinstance(Mrelation, OrderedDict):
print("Warning: Mrelation should be ordered dictionary!")
for Mtype, msce_name in Mrelation.items():
if self.has_mag(Mtype):
if not isinstance(msce_name, msc.MSCE):
msce = getattr(msc, msce_name)()
else:
msce = msce_name
return msce.get_mean(self.get_mag(Mtype))
## If Mrelation is empty or none of the Mtype's match
return np.nan
def get_ML(self, Mrelation={}):
"""
Return ML
"""
#if Mrelation in (None, "default"):
# Mrelation = default_Mrelations['ML']
return self.get_or_convert_mag('ML', Mrelation)
def get_MS(self, Mrelation={}):
"""
Return MS.
If MS is None or zero, calculate it using the specified
magnitude conversion relation
:param Mrelation:
{str: str} dict, mapping name of magnitude conversion relation
to magnitude type ("ML" or "MW").
The following relations are currently supported (see module msc):
- ML -> MS:
- "Ambraseys1985"
- MW -> MS:
None
(default: {})
:return:
Float, surface-wave magnitude
"""
## Set default conversion relation
#if Mrelation in (None, "default"):
# Mrelation = default_Mrelations['MS']
return self.get_or_convert_mag('MS', Mrelation)
def get_MW(self, Mrelation={}):
"""
Return MW.
If MW is None or zero, calculate it using the specified
magnitude conversion relation
:param Mrelation:
{str: str} dict, mapping name of magnitude conversion relation
to magnitude type ("MS" or "ML").
The following relations are supported:
- MS --> MW:
- "AmbraseysFree1997"
- "BungumEtAl2003NCEurope"
- "BungumEtAl2003SEurope"
- "Geller1976"
- "ISC_GEM2013"
- "OkalRomanowicz1994"
- "Scordilis2006"
- "Utsu2002"
- ML --> MW:
- "Ahorner1983"
- "Goutbeek2008"
- "GruenthalWahlstrom2003"
- "GruenthalEtAl2009"
- "ReamerHinzen2004L"
- "ReamerHinzen2004Q"
Note that an ordered dictionary should be used if conversion
from a particular Mtype is preferred over another Mtype
:return:
Float, moment magnitude
"""
## Set default conversion relation
#if Mrelation in (None, "default"):
# Mrelation = default_Mrelations['MW']
return self.get_or_convert_mag('MW', Mrelation)
def get_M(self, Mtype, Mrelation={}):
"""
Wrapper for get_ML, get_MS, and get_MW functions
:param Mtype:
String, magnitude type: "MW", "MS" or "ML"
:param Mrelation:
{str: str} dict, mapping name of magnitude conversion relation
to magnitude type ("MW", "MS" or "ML")
(default: {})
:return:
Float, magnitude
"""
#return getattr(self, "get_"+Mtype)(Mrelation=Mrelation)
return self.get_or_convert_mag(Mtype, Mrelation)
def get_M0(self, Mrelation={}):
"""
Compute seismic moment.
If MW is None or zero, it will be computed using the specified
magntiude conversion relation.
:param Mrelation:
{str: str} dict, mapping name of magnitude conversion relation
to magnitude type.
See :meth:`get_MW`
:return:
Float, scalar seismic moment in N.m
"""
return 10**((self.get_MW(Mrelation=Mrelation) + 6.06) * 3.0 / 2.0)
## Location-related methods
def epicentral_distance(self, pt):
"""
Compute epicentral distance to point in km using haversine formula
:param pt:
(lon, lat) tuple or object having 'lon' and 'lat' attributes
Note that lon, lat may also be arrays
:return:
Float, epicentral distance in km
"""
if hasattr(pt, 'lon') and hasattr(pt, 'lat'):
pt = (pt.lon, pt.lat)
return geodetic.spherical_distance(self.lon, self.lat, pt[0], pt[1]) / 1000.
def hypocentral_distance(self, pt):
"""
Compute hypocentral distance to point in km using haversine formula,
and taking into account depth
:param pt:
(lon, lat, [z]) tuple or other instance of :class:`LocalEarthquake`
Note that lon, lat, z may also be arrays
Note: z should be depth (not altitude) and in km!
:return:
Float, hypocentral distance in km
"""
d_epi = self.epicentral_distance(pt)
if hasattr(pt, 'depth'):
depth2 = pt.depth
elif isinstance(pt, (tuple, list)) and len(pt) == 3:
depth2 = pt[2]
else:
depth2 = 0.
depth1 = np.nan_to_num(self.depth)
delta_depth = np.abs(depth1 - depth2)
d_hypo = np.sqrt(d_epi**2 + delta_depth**2)
return d_hypo
def azimuth_to(self, pt):
"""
Compute bearing or azimuth from epicenter to another point.
:param pt:
(lon, lat) tuple or other instance of :class:`LocalEarthquake`
Note that lon, lat may also be arrays
:return:
Float, azimuth in decimal degrees
"""
if isinstance(pt, LocalEarthquake):
pt = (pt.lon, pt.lat)
return geodetic.spherical_azimuth(self.lon, self.lat, pt[0], pt[1])
def azimuth_from(self, pt):
"""
Compute azimuth from another point to epicenter.
:param pt:
(lon, lat) tuple or other instance of :class:`LocalEarthquake`
Note that lon, lat may also be arrays
:return:
Float, azimuth in decimal degrees
"""
if isinstance(pt, LocalEarthquake):
pt = (pt.lon, pt.lat)
return geodetic.spherical_azimuth(pt[0], pt[1], self.lon, self.lat)
def get_point_at(self, distance, azimuth):
"""
Compute coordinates of point at given distance and azimuth
from epicenter.
:param distance:
Float or array, distance in km
:param azimuth:
Float or array, azimuth in decimal degrees
Note that only one of :param:`distance` and :param:`azimuth` may be
an array
:return:
...
"""
return geodetic.spherical_point_at(self.lon, self.lat, distance*1000, azimuth)
def to_folium_marker(self, marker_shape='circle', marker_size=9,
edge_color='blue', edge_width=1., fill_color=None,
opacity=0.5, add_popup=True):
"""
Create circle marker to plot with folium
Note: if marker_shape = 'circle' and marker_size > 0, a circle marker
will be drawn. The size can be adjusted, and edge_color and fill_color
refer to circle edge and circle interior, respectively
In other cases, a BeautifyIcon marker will be drawn with a star 'icon'
(= internal symbol). The size cannot be modified, and edge_color and
fill_color refer to the 'icon' color and the surrounding marker color,
respectively. border-width is ignored
:param marker_shape:
str, marker shape, e.g. 'circle', 'balloon'
(default: 'circle')
:param marker_size: