forked from adjtomo/pysep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_helpers.py
More file actions
121 lines (97 loc) · 3.28 KB
/
util_helpers.py
File metadata and controls
121 lines (97 loc) · 3.28 KB
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
"""
miscellaneous helper utilities
20160919 cralvizuri <cralvizuri@alaska.edu>
"""
from copy import deepcopy
from obspy.core import UTCDateTime
import glob
import obspy
def otime2eid(otime):
"""
Convert origin time to origin ID. The origin ID has format: YYYYMMDDHHMMSSsss
See also eid2otime.
Example
otime = "2009-04-07T20:12:55"
eid = otime2eid(otime)
print(otime, eid)
"""
yy = UTCDateTime(otime).year
mo = UTCDateTime(otime).month
dd = UTCDateTime(otime).day
hh = UTCDateTime(otime).hour
mm = UTCDateTime(otime).minute
ss = UTCDateTime(otime).second
ms = int(UTCDateTime(otime).microsecond / 1000.0) # mili?
eid = '%04d%02d%02d%02d%02d%02d%03d' % (yy, mo, dd, hh, mm, ss, ms)
return eid
def eid2otime(eid):
"""
Convert event ID to origin time.
See also otime2eid.
Example
eid = "20090407201255000"
otime = eid2otime(eid)
print(eid, otime)
"""
part1 = "%s" % eid[:-3]
part2 = "%s" % eid[-3:]
eid = part1 + '.' + part2
otime = UTCDateTime(eid)
return(otime)
def copy_trace(stream, component=None):
""" Copies given component from stream
"""
if len(stream) == 0:
raise Exception("Cannot extract trace from empty stream")
if not component:
return deepcopy(stream[0])
else:
trace = stream.select(component=component)[0]
return deepcopy(trace)
def remove_trace(stream, component=None):
""" Removes given component from stream
"""
try:
trace = stream.select(component=component)[0]
stream.remove(trace)
except:
return stream
def get_streams_from_dir(ddir):
'''
Get streams from dir (created by mass downloader)
mseed to stream
'''
from obspy.core import Trace, Stream
st = Stream()
tr = Trace()
for mseed_trace in glob.iglob(ddir + '/*.mseed'):
tr = obspy.read(mseed_trace)
st.append(tr[0])
return st
def get_inventory_from_xml(ddir):
'''
Return combined inventory multiple xml files
(created by mass downloader)
2021-05-17 NOTE getwaveform suite crashes with some xml files.
specially network VR (eg VRG02.xml)
some xml files might be missing data.
---8<---8<---8<---
File "/home/calvizur/UTILS/anaconda3/envs/seis38/lib/python3.8/site-packages/obspy/core/inventory/response.py", line 296, in normalization_frequency
self._normalization_frequency = Frequency(value)
File "/home/calvizur/UTILS/anaconda3/envs/seis38/lib/python3.8/site-packages/obspy/core/util/obspy_types.py", line 237, in __new__
if not cls._minimum <= float(value) <= cls._maximum:
TypeError: float() argument must be a string or a number, not 'NoneType'
---8<---8<---8<---
fix: use try/except. Better fix might be for obspy_types.py to recover from this without crashing.
'''
from obspy.core.inventory import Inventory
inventory = Inventory(networks=[],source='ObsPy 1.0.3')
#inventory = Inventory()
for xmlfile in glob.iglob(ddir + '/*.xml'):
try:
stninv = obspy.read_inventory(xmlfile)
inventory.networks.append(stninv[0])
except:
print('ERROR. Unable to append file', xmlfile)
#inventory.__add__(stninv)
return inventory