/
core.py
1500 lines (1403 loc) · 60.2 KB
/
core.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: utf-8 -*-
"""
Nordic file format support for ObsPy
:copyright:
The ObsPy Development Team (devs@obspy.org)
:license:
GNU Lesser General Public License, Version 3
(https://www.gnu.org/copyleft/lesser.html)
.. note::
Pick time-residuals are handled in event.origins[0].arrivals, with
the arrival.pick_id linking the arrival (which contain calculated
information) with the pick.resource_id (where the pick contains only
physical measured information).
.. versionchanged:: 1.2.0
The number of stations used to calculate the origin was previously
incorrectly stored in a comment. From version 1.2.0 this is now stored
in `origin.quality.used_station_count`
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.builtins import * # NOQA @UnusedWildImport
import warnings
import datetime
import os
import io
from obspy import UTCDateTime, read
from obspy.geodetics import kilometers2degrees, degrees2kilometers
from obspy.core.event import (
Event, Origin, Magnitude, Catalog, EventDescription, CreationInfo,
OriginQuality, OriginUncertainty, Pick, WaveformStreamID, Arrival,
Amplitude, FocalMechanism, MomentTensor, NodalPlane, NodalPlanes,
QuantityError, Tensor, ResourceIdentifier)
from obspy.io.nordic import NordicParsingError
from obspy.io.nordic.utils import (
_int_conv, _str_conv, _float_conv, _evmagtonor, _nortoevmag,
_get_line_tags, _km_to_deg_lat, _km_to_deg_lon)
from obspy.io.nordic.ellipse import Ellipse
POLARITY_MAPPING = {"": "undecidable", "C": "positive", "D": "negative"}
INV_POLARITY_MAPPING = {item: key for key, item in POLARITY_MAPPING.items()}
ONSET_MAPPING = {'I': 'impulsive', 'E': 'emergent'}
INV_ONSET_MAPPING = {item: key for key, item in ONSET_MAPPING.items()}
EVALUATION_MAPPING = {'A': 'automatic', ' ': 'manual'}
INV_EVALUTATION_MAPPING = {
item: key for key, item in EVALUATION_MAPPING.items()}
def _is_sfile(sfile, encoding='latin-1'):
"""
Basic test of whether the file is nordic format or not.
Not exhaustive, but checks some of the basics.
:type sfile: str
:param sfile: Path to sfile
:type encoding: str
:param encoding: Encoding of the file.
:rtype: bool
"""
if not hasattr(sfile, "readline"):
try:
with open(sfile, 'r', encoding=encoding) as f:
tags = _get_line_tags(f=f, report=False)
except Exception:
return False
else:
try:
tags = _get_line_tags(f=sfile, report=False)
except Exception:
return False
if tags is not None:
try:
head_line = tags['1'][0][0]
except IndexError:
return False
try:
sfile_seconds = int(head_line[16:18])
except ValueError:
return False
if sfile_seconds == 60:
sfile_seconds = 0
try:
UTCDateTime(
int(head_line[1:5]), int(head_line[6:8]), int(head_line[8:10]),
int(head_line[11:13]), int(head_line[13:15]), sfile_seconds,
int(head_line[19:20]) * 100000)
return True
except Exception:
return False
else:
return False
def readheader(sfile, encoding='latin-1'):
"""
Read header information from a seisan nordic format S-file.
:type sfile: str
:param sfile: Path to the s-file
:type encoding: str
:param encoding: Encoding for file, used to decode from bytes to string
:returns: :class:`~obspy.core.event.event.Event`
"""
with open(sfile, 'r', encoding=encoding) as f:
tagged_lines = _get_line_tags(f)
if len(tagged_lines['1']) == 0:
raise NordicParsingError("No header lines found")
header = _readheader(head_lines=tagged_lines['1'])
return header
def _readheader(head_lines):
"""
Internal header reader.
:type head_lines: list
:param head_lines:
List of tuples of (strings, line-number) of the header lines.
:returns: :class:`~obspy.core.event.event.Event`
"""
# There are two possible types of origin line, one with all info, and a
# subsequent one for additional magnitudes.
head_lines.sort(key=lambda tup: tup[1])
# Construct a rough catalog, then merge events together to cope with
# multiple origins
_cat = Catalog()
for line in head_lines:
_cat.append(_read_origin(line=line[0]))
new_event = _cat.events.pop(0)
for event in _cat:
matched = False
origin_times = [origin.time for origin in new_event.origins]
if event.origins[0].time in origin_times:
origin_index = origin_times.index(event.origins[0].time)
agency = new_event.origins[origin_index].creation_info.agency_id
if event.creation_info.agency_id == agency:
event_desc = new_event.event_descriptions[origin_index].text
if event.event_descriptions[0].text == event_desc:
matched = True
new_event.magnitudes.extend(event.magnitudes)
if not matched:
new_event.origins.append(event.origins[0])
new_event.event_descriptions.append(event.event_descriptions[0])
# Set the useful things like preferred magnitude and preferred origin
new_event.preferred_origin_id = new_event.origins[0].resource_id
try:
# Select moment first, then local, then
mag_filter = ['MW', 'Mw', 'ML', 'Ml', 'MB', 'Mb',
'MS', 'Ms', 'MC', 'Mc']
_magnitudes = [(m.magnitude_type, m.resource_id)
for m in new_event.magnitudes]
preferred_magnitude = sorted(_magnitudes,
key=lambda x: mag_filter.index(x[0]))[0]
new_event.preferred_magnitude_id = preferred_magnitude[1]
except (ValueError, IndexError):
# If there is a magnitude not specified in filter
try:
new_event.preferred_magnitude_id = new_event.magnitudes[0].\
resource_id
except IndexError:
pass
return new_event
def _read_origin(line):
"""
Read one origin (type 1) line.
:param str line: Origin format (type 1) line
:return: `~obspy.core.event.Event`
"""
new_event = Event()
try:
sfile_seconds = line[16:20].strip()
if len(sfile_seconds) == 0:
sfile_seconds = 0.0
else:
sfile_seconds = float(sfile_seconds)
new_event.origins.append(Origin())
new_event.origins[0].time = UTCDateTime(
int(line[1:5]), int(line[6:8]), int(line[8:10]),
int(line[11:13]), int(line[13:15]), 0, 0) + sfile_seconds
except Exception:
raise NordicParsingError("Couldn't read a date from sfile")
# new_event.loc_mod_ind=line[20]
new_event.event_descriptions.append(EventDescription(text=line[21:23]))
for key, _slice in [('latitude', slice(23, 30)),
('longitude', slice(30, 38)),
('depth', slice(38, 43))]:
try:
new_event.origins[0].__dict__[key] = float(line[_slice])
except ValueError:
new_event.origins[0].__dict__[key] = None
if new_event.origins[0].depth:
new_event.origins[0].depth *= 1000.
if line[43].strip():
warnings.warn("Depth indicator {0} has not been mapped "
"to the event".format(line[43]))
if line[44].strip():
warnings.warn("Origin location indicator {0} has not been mapped "
"to the event".format(line[44]))
if line[10] == "F":
new_event.origins[0].time_fixed = True
new_event.creation_info = CreationInfo(agency_id=line[45:48].strip())
new_event.origins[0].creation_info = CreationInfo(
agency_id=line[45:48].strip())
used_station_count = line[49:51].strip()
if used_station_count != '':
new_event.origins[0].quality = OriginQuality(
used_station_count=int(used_station_count))
timeres = _float_conv(line[51:55])
if timeres is not None:
if new_event.origins[0].quality is not None:
new_event.origins[0].quality.standard_error = timeres
else:
new_event.origins[0].quality = OriginQuality(
standard_error=timeres)
# Read in magnitudes if they are there.
magnitudes = []
magnitudes.extend(_read_mags(line, new_event))
new_event.magnitudes = magnitudes
return new_event
def _read_mags(line, event):
"""
Read the magnitude info from a Nordic header line. Convenience function
"""
magnitudes = []
for index in [59, 67, 75]:
if not line[index].isspace():
magnitudes.append(Magnitude(
mag=_float_conv(line[index - 3:index]),
magnitude_type=_nortoevmag(line[index]),
creation_info=CreationInfo(
agency_id=line[index + 1:index + 4].strip()),
origin_id=event.origins[0].resource_id))
return magnitudes
def read_spectral_info(sfile, encoding='latin-1'):
"""
Read spectral info from an sfile.
:type sfile: str
:param sfile: Sfile to read from.
:type encoding: str
:param encoding: Encoding for file, used to decode from bytes to string
:returns:
list of dictionaries of spectral information, units as in seisan
manual, expect for logs which have been converted to floats.
"""
with open(sfile, 'r', encoding=encoding) as f:
tagged_lines = _get_line_tags(f=f)
spec_inf = _read_spectral_info(tagged_lines=tagged_lines)
return spec_inf
def _read_spectral_info(tagged_lines, event=None):
"""
Internal spectral reader.
:type tagged_lines: dict
:param tagged_lines: dictionary of tagged lines
:type event: :class:`~obspy.core.event.Event`
:param event: Event to associate spectral info with
:returns:
list of dictionaries of spectral information, units as in
seisan manual, expect for logs which have been converted to floats.
"""
if '3' not in tagged_lines.keys():
return {}
if event is None:
event = _readheader(head_lines=tagged_lines['1'])
origin_date = UTCDateTime(event.origins[0].time.date)
relevant_lines = []
for line in tagged_lines['3']:
if line[0][1:5] == 'SPEC':
relevant_lines.append(line)
relevant_lines = [line[0] for line in
sorted(relevant_lines, key=lambda tup: tup[1])]
spec_inf = {}
if not relevant_lines:
return spec_inf
for line in relevant_lines:
spec_str = line.strip()
if spec_str[5:12] in ['AVERAGE', 'STANDARD_DEVIATION']:
info = {}
station = spec_str[5:12]
channel = ''
info['moment'] = _float_conv(spec_str[16:22])
if info['moment'] is not None:
info['moment'] = 10 ** info['moment']
info['stress_drop'] = _float_conv(spec_str[24:30])
info['spectral_level'] = _float_conv(spec_str[32:38])
if info['spectral_level'] is not None:
info['spectral_level'] = 10 ** info['spectral_level']
info['corner_freq'] = _float_conv(spec_str[40:46])
info['source_radius'] = _float_conv(spec_str[47:54])
info['decay'] = _float_conv(spec_str[56:62])
info['window_length'] = _float_conv(spec_str[64:70])
info['moment_mag'] = _float_conv(spec_str[72:78])
spec_inf[(station, channel)] = info
continue
station = spec_str[4:9].strip()
channel = ''.join(spec_str[9:13].split())
try:
info = spec_inf[(station, channel)]
except KeyError:
info = {}
if spec_str[14] == 'T':
info['starttime'] = origin_date + \
(int(spec_str[15:17]) * 3600) +\
(int(spec_str[17:19]) * 60) + int(spec_str[19:21])
if info['starttime'] < event.origins[0].time:
# Wrong day, case of origin at end of day
info['starttime'] += 86400
info['kappa'] = _float_conv(spec_str[23:30])
info['distance'] = _float_conv(spec_str[32:38])
if spec_str[38:40] == 'VS':
info['velocity'] = _float_conv(spec_str[40:46])
info['velocity_type'] = 'S'
elif spec_str[38:40] == 'VP':
info['velocity'] = _float_conv(spec_str[40:46])
info['velocity_type'] = 'P'
else:
warnings.warn('Only VP and VS spectral information'
' implemented')
info['density'] = _float_conv(spec_str[48:54])
info['Q0'] = _float_conv(spec_str[56:62])
info['QA'] = _float_conv(spec_str[64:70])
elif spec_str[14] == 'M':
info['moment'] = _float_conv(spec_str[16:22])
if info['moment']:
info['moment'] = 10 ** info['moment']
info['stress_drop'] = _float_conv(spec_str[24:30])
info['spectral_level'] = _float_conv(spec_str[32:38])
if info['spectral_level']:
info['spectral_level'] = 10 ** info['spectral_level']
info['corner_freq'] = _float_conv(spec_str[40:46])
info['source_radius'] = _float_conv(spec_str[47:54])
info['decay'] = _float_conv(spec_str[56:62])
info['window_length'] = _float_conv(spec_str[64:70])
info['moment_mag'] = _float_conv(spec_str[72:78])
spec_inf[(station, channel)] = info
return spec_inf
def read_nordic(select_file, return_wavnames=False, encoding='latin-1'):
"""
Read a catalog of events from a Nordic formatted select file.
Generates a series of temporary files for each event in the select file.
:type select_file: str
:param select_file: Nordic formatted select.out file to open
:type return_wavnames: bool
:param return_wavnames:
If True, will return the names of the waveforms that the events
are associated with.
:type encoding: str
:param encoding: Encoding for file, used to decode from bytes to string
:return: catalog of events
:rtype: :class:`~obspy.core.event.event.Event`
"""
if not hasattr(select_file, "readline"):
try:
f = open(select_file, 'r', encoding=encoding)
except Exception:
try:
f = select_file.decode(encoding)
except Exception:
f = str(select_file)
else:
f = select_file
wav_names = []
event_str = []
catalog = Catalog()
for line in f:
if len(line.rstrip()) > 0:
event_str.append(line)
elif len(event_str) > 0:
catalog, wav_names = _extract_event(
event_str=event_str, catalog=catalog, wav_names=wav_names,
return_wavnames=return_wavnames)
event_str = []
f.close()
if len(event_str) > 0:
# May occur if the last line of the file is not blank as it should be
catalog, wav_names = _extract_event(
event_str=event_str, catalog=catalog, wav_names=wav_names,
return_wavnames=return_wavnames)
if return_wavnames:
return catalog, wav_names
for event in catalog:
event.scope_resource_ids()
return catalog
def _extract_event(event_str, catalog, wav_names, return_wavnames=False):
"""
Helper to extract event info from a list of line strings.
:param event_str: List of lines from sfile
:type event_str: list of str
:param catalog: Catalog to append the event to
:type catalog: `obspy.core.event.Catalog`
:param wav_names: List of waveform names
:type wav_names: list
:param return_wavnames: Whether to extract the waveform name or not.
:type return_wavnames: bool
:return: Adds event to catalog and returns. Works in place on catalog.
"""
tmp_sfile = io.StringIO()
for event_line in event_str:
tmp_sfile.write(event_line)
tagged_lines = _get_line_tags(f=tmp_sfile)
new_event = _readheader(head_lines=tagged_lines['1'])
new_event = _read_uncertainty(tagged_lines, new_event)
new_event = _read_highaccuracy(tagged_lines, new_event)
new_event = _read_focal_mechanisms(tagged_lines, new_event)
new_event = _read_moment_tensors(tagged_lines, new_event)
if return_wavnames:
wav_names.append(_readwavename(f=tmp_sfile))
new_event = _read_picks(tagged_lines=tagged_lines, new_event=new_event)
catalog += new_event
return catalog, wav_names
def _read_uncertainty(tagged_lines, event):
"""
Read hyp uncertainty line.
:param tagged_lines: Lines keyed by line type
:type tagged_lines: dict
:returns: updated event
:rtype: :class:`~obspy.core.event.event.Event`
"""
if 'E' not in tagged_lines.keys():
return event
# In principle there shouldn't be more than one error line, but I think
# there can be - need to associate the correct error
line = tagged_lines['E'][0][0]
# TODO: Convert this to ConfidenceEllipsoid
errors = {'x_err': None}
try:
errors = {'x_err': _float_conv(line[24:30]),
'y_err': _float_conv(line[32:38]),
'z_err': _float_conv(line[38:43]),
'xy_cov': _float_conv(line[43:55]),
'xz_cov': _float_conv(line[55:67]),
'yz_cov': _float_conv(line[67:79])}
except ValueError:
pass
orig = event.origins[0]
if errors['x_err'] is not None:
e = Ellipse.from_uncerts(errors['x_err'],
errors['y_err'],
errors['xy_cov'])
orig.origin_uncertainty = OriginUncertainty(
max_horizontal_uncertainty=e.a * 1000.,
min_horizontal_uncertainty=e.b * 1000.,
azimuth_max_horizontal_uncertainty=e.theta,
preferred_description="uncertainty ellipse")
orig.latitude_errors = QuantityError(
_km_to_deg_lat(errors['y_err']))
orig.longitude_errors = QuantityError(
_km_to_deg_lon(errors['x_err'], orig.latitude))
orig.depth_errors = QuantityError(errors['z_err'] * 1000.)
try:
orig.quality = OriginQuality(
azimuthal_gap=int(line[5:8]), standard_error=float(line[14:20]))
except ValueError:
pass
return event
def _read_highaccuracy(tagged_lines, event):
"""
Read high accuracy origin line.
:param tagged_lines: Lines keyed by line type
:type tagged_lines: dict
:returns: updated event
:rtype: :class:`~obspy.core.event.event.Event`
"""
if 'H' not in tagged_lines.keys():
return event
# In principle there shouldn't be more than one high precision line
line = tagged_lines['H'][0][0]
try:
dt = {'Y': _int_conv(line[1:5]),
'MO': _int_conv(line[6:8]),
'D': _int_conv(line[8:10]),
'H': _int_conv(line[11:13]),
'MI': _int_conv(line[13:15]),
'S': _float_conv(line[16:23])}
except ValueError:
pass
try:
ev_time = UTCDateTime(dt['Y'], dt['MO'], dt['D'],
dt['H'], dt['MI'], 0, 0) + dt['S']
if abs(event.origins[0].time - ev_time) < 0.1:
event.origins[0].time = ev_time
else:
print('High accuracy time differs from normal time by >0.1s')
except ValueError:
pass
try:
values = {'latitude': _float_conv(line[23:32]),
'longitude': _float_conv(line[33:43]),
'depth': _float_conv(line[44:52]),
'rms': _float_conv(line[53:59])}
except ValueError:
pass
if values['latitude'] is not None:
event.origins[0].latitude = values['latitude']
if values['longitude'] is not None:
event.origins[0].longitude = values['longitude']
if values['depth'] is not None:
event.origins[0].depth = values['depth'] * 1000.
if values['rms'] is not None:
if event.origins[0].quality is not None:
event.origins[0].quality.standard_error = values['rms']
else:
event.origins[0].quality = OriginQuality(
standard_error=values['rms'])
return event
def _read_focal_mechanisms(tagged_lines, event):
"""
Read focal mechanism info from s-file
:param tagged_lines: Lines keyed by line type
:type tagged_lines: dict
:returns: updated event
:rtype: :class:`~obspy.core.event.event.Event`
"""
if 'F' not in tagged_lines.keys():
return event
UserWarning("Found focal-mechanism info: reading amplitude-ratio fit,"
"number of bad polarities and number of bad amplitude ratios"
"is not implemented.")
for line, line_num in tagged_lines['F']:
nodal_p = NodalPlane(strike=float(line[0:10]), dip=float(line[10:20]),
rake=float(line[20:30]))
try:
# Apparently these don't have to be filled.
nodal_p.strike_errors = QuantityError(float(line[30:35]))
nodal_p.dip_errors = QuantityError(float(line[35:40]))
nodal_p.rake_errors = QuantityError(float(line[40:45]))
except ValueError:
pass
fm = FocalMechanism(nodal_planes=NodalPlanes(nodal_plane_1=nodal_p))
try:
fm.method_id = ResourceIdentifier(
"smi:nc.anss.org/focalMehcanism/" + line[70:77].strip())
fm.creation_info = CreationInfo(agency_id=line[66:69].strip)
fm.misfit = float(line[45:50])
fm.station_distribution_ratio = float(line[50:55])
except ValueError:
pass
event.focal_mechanisms.append(fm)
return event
def _read_moment_tensors(tagged_lines, event):
"""
Read moment tensors from s-file
:param tagged_lines: Lines keyed by line type
:type tagged_lines: dict
:returns: updated event
:rtype: :class:`~obspy.core.event.event.Event`
"""
if 'M' not in tagged_lines.keys():
return event
# Group moment tensor lines together
mt_lines = sorted(tagged_lines['M'], key=lambda tup: tup[1])
for mt_ind in range(len(mt_lines) // 2):
mt_line_1 = mt_lines[mt_ind * 2][0]
mt_line_2 = mt_lines[(mt_ind * 2) + 1][0]
if not str(mt_line_2[1:3]) == str('MT'):
raise NordicParsingError("Matching moment tensor lines not found.")
sfile_seconds = int(mt_line_1[16:18])
if sfile_seconds == 60:
sfile_seconds = 0
add_seconds = 60
else:
add_seconds = 0
ori_time = UTCDateTime(
int(mt_line_1[1:5]), int(mt_line_1[6:8]), int(mt_line_1[8:10]),
int(mt_line_1[11:13]), int(mt_line_1[13:15]), sfile_seconds,
int(mt_line_1[19:20]) * 100000) + add_seconds
event.origins.append(Origin(
time=ori_time, latitude=float(mt_line_1[23:30]),
longitude=float(mt_line_1[30:38]),
depth=float(mt_line_1[38:43]) * 1000,
creation_info=CreationInfo(agency_id=mt_line_1[45:48].strip())))
event.magnitudes.append(Magnitude(
mag=float(mt_line_1[55:59]),
magnitude_type=_nortoevmag(mt_line_1[59]),
creation_info=CreationInfo(agency_id=mt_line_1[60:63].strip()),
origin_id=event.origins[-1].resource_id))
event.focal_mechanisms.append(FocalMechanism(
moment_tensor=MomentTensor(
derived_origin_id=event.origins[-1].resource_id,
moment_magnitude_id=event.magnitudes[-1].resource_id,
scalar_moment=float(mt_line_2[52:62]), tensor=Tensor(
m_rr=float(mt_line_2[3:9]), m_tt=float(mt_line_2[10:16]),
m_pp=float(mt_line_2[17:23]), m_rt=float(mt_line_2[24:30]),
m_rp=float(mt_line_2[31:37]),
m_tp=float(mt_line_2[38:44])),
method_id=ResourceIdentifier(
"smi:nc.anss.org/momentTensor/" + mt_line_1[70:77].strip()
))))
return event
def _read_picks(tagged_lines, new_event):
"""
Internal pick reader. Use read_nordic instead.
:type tagged_lines: dict
:param tagged_lines: Lines keyed by line type
:type new_event: :class:`~obspy.core.event.event.Event`
:param new_event: event to associate picks with.
:returns: :class:`~obspy.core.event.event.Event`
"""
evtime = new_event.origins[0].time
pickline = []
# pick-lines can be tagged by either ' ' or '4'
tags = [' ', '4']
for tag in tags:
try:
pickline.extend(
[tup[0] for tup in sorted(
tagged_lines[tag], key=lambda tup: tup[1])])
except KeyError:
pass
header = sorted(tagged_lines['7'], key=lambda tup: tup[1])[0][0]
for line in pickline:
ain, snr = (None, None)
if line[18:28].strip() == '': # If line is empty miss it
continue
if len(line) < 80:
line = line.ljust(80) # Pick-lines without a tag may be short.
weight = line[14]
if weight not in ' 012349_': # Long phase name
weight = line[8]
phase = line[10:17].strip()
polarity = ''
elif weight == '_':
phase = line[10:17]
weight = 0
polarity = ''
else:
phase = line[10:14].strip()
polarity = line[16]
if weight == ' ':
weight = 0
polarity = POLARITY_MAPPING.get(polarity, "undecidable")
# It is valid nordic for the origin to be hour 23 and picks to be hour
# 00 or 24: this signifies a pick over a day boundary.
pick_hour = int(line[18:20])
pick_minute = int(line[20:22])
pick_seconds = float(line[22:29]) # 29 should be blank, but sometimes
# SEISAN appears to overflow here, see #2348
if pick_hour == 0 and evtime.hour == 23:
day_add = 86400
elif pick_hour >= 24: # Nordic supports up to 48 hours advanced.
day_add = 86400
pick_hour -= 24
else:
day_add = 0
time = UTCDateTime(
year=evtime.year, month=evtime.month, day=evtime.day,
hour=pick_hour, minute=pick_minute) + (pick_seconds + day_add)
if header[57:60] == 'AIN':
ain = _float_conv(line[57:60])
elif header[57:60] == 'SNR':
snr = _float_conv(line[57:60])
else:
warnings.warn('%s is not currently supported' % header[57:60])
# finalweight = _int_conv(line[68:70])
# Create a new obspy.event.Pick class for this pick
_waveform_id = WaveformStreamID(station_code=line[1:6].strip(),
channel_code=line[6:8].strip(),
network_code='NA')
pick = Pick(waveform_id=_waveform_id, phase_hint=phase,
polarity=polarity, time=time)
try:
pick.onset = ONSET_MAPPING[line[9]]
except KeyError:
pass
pick.evaluation_mode = EVALUATION_MAPPING.get(line[15], "manual")
# Note these two are not always filled - velocity conversion not yet
# implemented, needs to be converted from km/s to s/deg
# if not velocity == 999.0:
# new_event.picks[pick_index].horizontal_slowness = 1.0 / velocity
if _float_conv(line[46:51]) is not None:
pick.backazimuth = _float_conv(line[46:51])
# Create new obspy.event.Amplitude class which references above Pick
# only if there is an amplitude picked.
if _float_conv(line[33:40]) is not None:
_amplitude = Amplitude(generic_amplitude=_float_conv(line[33:40]),
period=_float_conv(line[41:45]),
pick_id=pick.resource_id,
waveform_id=pick.waveform_id)
if pick.phase_hint == 'IAML':
# Amplitude for local magnitude
_amplitude.type = 'AML'
# Set to be evaluating a point in the trace
_amplitude.category = 'point'
# Default AML unit in seisan is nm (Page 139 of seisan
# documentation, version 10.0)
_amplitude.generic_amplitude /= 1e9
_amplitude.unit = 'm'
_amplitude.magnitude_hint = 'ML'
else:
# Generic amplitude type
_amplitude.type = 'A'
if snr:
_amplitude.snr = snr
new_event.amplitudes.append(_amplitude)
elif _int_conv(line[29:33]) is not None:
# Create an amplitude instance for coda duration also
_amplitude = Amplitude(generic_amplitude=_int_conv(line[29:33]),
pick_id=pick.resource_id,
waveform_id=pick.waveform_id)
# Amplitude for coda magnitude
_amplitude.type = 'END'
# Set to be evaluating a point in the trace
_amplitude.category = 'duration'
_amplitude.unit = 's'
_amplitude.magnitude_hint = 'Mc'
if snr is not None:
_amplitude.snr = snr
new_event.amplitudes.append(_amplitude)
# Create new obspy.event.Arrival class referencing above Pick
if _float_conv(line[33:40]) is None:
arrival = Arrival(phase=pick.phase_hint, pick_id=pick.resource_id)
if weight is not None:
arrival.time_weight = weight
if _int_conv(line[60:63]) is not None:
arrival.backazimuth_residual = _int_conv(line[60:63])
if _float_conv(line[63:68]) is not None:
arrival.time_residual = _float_conv(line[63:68])
if _float_conv(line[70:75]) is not None:
arrival.distance = kilometers2degrees(_float_conv(line[70:75]))
if _int_conv(line[76:79]) is not None:
arrival.azimuth = _int_conv(line[76:79])
if ain is not None:
arrival.takeoff_angle = ain
new_event.origins[0].arrivals.append(arrival)
new_event.picks.append(pick)
return new_event
def readwavename(sfile, encoding='latin-1'):
"""
Extract the waveform filename from the s-file.
Returns a list of waveform names found in the s-file as multiples can
be present.
:type sfile: str
:param sfile: Path to the sfile
:type encoding: str
:param encoding: Encoding for file, used to decode from bytes to string
:returns: List of strings of wave paths
:rtype: list
"""
with open(sfile, 'r', encoding=encoding) as f:
wavenames = _readwavename(f=f)
return wavenames
def _readwavename(f):
"""
Internal wave-name reader.
:type f: file
:param f: File open in read-mode
:return: list of wave-file names
"""
wavename = []
for line in f:
if len(line) == 81 and line[79] == '6':
wavename.append(line[1:79].strip())
return wavename
def blanksfile(wavefile, evtype, userid, overwrite=False, evtime=None):
"""
Generate an empty s-file with a populated header for a given waveform.
:type wavefile: str
:param wavefile: Wave-file to associate with this S-file, the timing of \
the S-file will be taken from this file if evtime is not set.
:type evtype: str
:param evtype: Event type letter code, e.g. L, R, D
:type userid: str
:param userid: 4-character SEISAN USER ID
:type overwrite: bool
:param overwrite: Overwrite an existing S-file, default=False
:type evtime: :class:`~obspy.core.utcdatetime.UTCDateTime`
:param evtime: If given this will set the timing of the S-file
:returns: str, S-file name
"""
if evtime is None:
try:
st = read(wavefile)
evtime = st[0].stats.starttime
except Exception:
raise NordicParsingError('Wavefile: ' + wavefile +
' is invalid, try again with real data.')
# Check that user ID is the correct length
if len(userid) != 4:
raise NordicParsingError('User ID must be 4 characters long')
# Check that evtype is one of L,R,D
if evtype not in ['L', 'R', 'D']:
raise NordicParsingError('Event type must be either L, R or D')
# Generate s-file name in the format dd-hhmm-ss[L,R,D].Syyyymm
sfile = str(evtime.day).zfill(2) + '-' + str(evtime.hour).zfill(2) +\
str(evtime.minute).zfill(2) + '-' + str(evtime.second).zfill(2) +\
evtype + '.S' + str(evtime.year) + str(evtime.month).zfill(2)
# Check is sfile exists
if os.path.isfile(sfile) and not overwrite:
warnings.warn('Desired sfile: ' + sfile + ' exists, will not ' +
'overwrite')
for i in range(1, 10):
sfile = str(evtime.day).zfill(2) + '-' +\
str(evtime.hour).zfill(2) +\
str(evtime.minute).zfill(2) + '-' +\
str(evtime.second + i).zfill(2) + evtype + '.S' +\
str(evtime.year) + str(evtime.month).zfill(2)
if not os.path.isfile(sfile):
break
else:
msg = ('Tried generated files up to 20s in advance and found ' +
'all exist.')
raise NordicParsingError(msg)
with open(sfile, 'w') as f:
# Write line 1 of s-file
f.write(' ' + str(evtime.year) + ' ' + str(evtime.month).rjust(2) +
str(evtime.day).rjust(2) + ' ' +
str(evtime.hour).rjust(2) +
str(evtime.minute).rjust(2) + ' ' +
str(float(evtime.second)).rjust(4) + ' ' +
evtype + '1'.rjust(58) + '\n')
# Write line 2 of s-file
output_time = datetime.datetime.now()
f.write(' ACTION:ARG ' + str(output_time.year)[2:4] +
'-' + str(output_time.month).zfill(2) + '-' +
str(output_time.day).zfill(2) + ' ' +
str(output_time.hour).zfill(2) + ':' +
str(output_time.minute).zfill(2) + ' OP:' +
userid.ljust(4) + ' STATUS:' + 'ID:'.rjust(18) +
str(evtime.year) + str(evtime.month).zfill(2) +
str(evtime.day).zfill(2) + str(evtime.hour).zfill(2) +
str(evtime.minute).zfill(2) + str(evtime.second).zfill(2) +
'I'.rjust(6) + '\n')
# Write line 3 of s-file
write_wavfile = os.path.basename(wavefile)
f.write(' ' + write_wavfile + '6'.rjust(79 - len(write_wavfile)) +
'\n')
# Write final line of s-file
f.write(" STAT SP IPHASW D HRMM SECON CODA AMPLIT PERI AZIMU" +
" VELO AIN AR TRES W DIS CAZ7\n")
return sfile
def write_select(catalog, filename, userid='OBSP', evtype='L',
wavefiles=None, high_accuracy=True):
"""
Function to write a catalog to a select file in nordic format.
:type catalog: :class:`~obspy.core.event.event.Catalog`
:param catalog: A catalog of obspy events
:type filename: str
:param filename: Path to write to
:type userid: str
:param userid: Up to 4 character user ID
:type evtype: str
:param evtype:
Single character string to describe the event, either L, R or D.
:type wavefiles: list
:param wavefiles:
Waveforms to associate the events with, must be ordered in the same
way as the events in the catalog.
:type high_accuracy: bool
:param high_accuracy:
Whether to output pick seconds at 6.3f (high_accuracy) or
5.2f (standard)
"""
if not wavefiles:
wavefiles = ['DUMMY' for _i in range(len(catalog))]
with open(filename, 'w') as fout:
for event, wavfile in zip(catalog, wavefiles):
select = io.StringIO()
_write_nordic(event=event, filename=None, userid=userid,
evtype=evtype, wavefiles=wavfile,
string_io=select, high_accuracy=high_accuracy)
select.seek(0)
for line in select:
fout.write(line)
fout.write('\n')
def _write_nordic(event, filename, userid='OBSP', evtype='L', outdir='.',
wavefiles='DUMMY', explosion=False,
overwrite=True, string_io=None, high_accuracy=True):
"""
Write an :class:`~obspy.core.event.Event` to a nordic formatted s-file.
:type event: :class:`~obspy.core.event.event.Event`
:param event: A single obspy event
:type filename: str
:param filename:
Filename to write to, can be None, and filename will be generated from
the origin time in nordic format.
:type userid: str
:param userid: Up to 4 character user ID
:type evtype: str
:param evtype:
Single character string to describe the event, either L, R or D.
:type outdir: str
:param outdir: Path to directory to write to
:type wavefiles: list
:param wavefiles: Waveforms to associate the nordic file with
:type explosion: bool
:param explosion:
Note if the event is an explosion, will be marked by an E.
:type overwrite: bool
:param overwrite: force to overwrite old files, defaults to False
:type string_io: io.StringIO
:param string_io:
If given, will write to the StringIO object in memory rather than to
the filename.
:type high_accuracy: bool
:param high_accuracy:
Whether to output pick seconds at 6.3f (high_accuracy) or
5.2f (standard)
:returns: str: name of nordic file written
.. note::
Seisan can find waveforms either by their relative or absolute path, or
by looking for the file recursively in directories within the WAV
directory in your seisan install. Because all lines need to be less
than 79 characters long (fortran hangover) in the s-files, you will
need to determine whether the full-path is okay or not.
"""
# First we need to work out what to call the s-file and open it
# Check that user ID is the correct length
if len(userid) != 4:
raise NordicParsingError('%s User ID must be 4 characters long'
% userid)
# Check that outdir exists
if not os.path.isdir(outdir):
raise NordicParsingError('Out path does not exist, I will not '
'create this: ' + outdir)
# Check that evtype is one of L,R,D
if evtype not in ['L', 'R', 'D']:
raise NordicParsingError('Event type must be either L, R or D')
if explosion:
evtype += 'E'
# Check that there is one event
if isinstance(event, Catalog) and len(event) == 1:
event = event[0]
elif isinstance(event, Event):
event = event
else:
raise NordicParsingError('Needs a single event')
if not isinstance(wavefiles, list):
wavefiles = [str(wavefiles)]
# Determine name from origin time
try: