/
core.py
347 lines (314 loc) · 13.6 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
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.builtins import * # NOQA
from future.utils import native_str
import datetime
import re
import warnings
from obspy import Catalog, UTCDateTime
from obspy.core.event import Origin, Magnitude
from obspy.core.inventory import Inventory
from obspy.core.util.misc import to_int_or_zero
try:
import shapefile
except ImportError as e:
HAS_PYSHP = False
PYSHP_VERSION = None
PYSHP_VERSION_AT_LEAST_1_2_11 = False
IMPORTERROR_MSG = str(e) + (
". ObsPy's write support for shapefiles requires the 'pyshp' module "
"to be installed in addition to the general ObsPy dependencies.")
else:
HAS_PYSHP = True
try:
PYSHP_VERSION = list(map(to_int_or_zero,
shapefile.__version__.split('.')))
except AttributeError:
PYSHP_VERSION = None
PYSHP_VERSION_AT_LEAST_1_2_11 = False
else:
PYSHP_VERSION_AT_LEAST_1_2_11 = PYSHP_VERSION >= [1, 2, 11]
PYSHP_VERSION_WARNING = (
'pyshp versions < 1.2.11 are buggy, e.g. in writing numerical values to '
'the dbf table, so e.g. timestamp float values might lack proper '
'precision. You should update to a newer pyshp version.')
def _write_shapefile(obj, filename, **kwargs):
"""
Write :class:`~obspy.core.inventory.inventory.Inventory` or
:class:`~obspy.core.event.Catalog` object to a ESRI shapefile.
:type obj: :class:`~obspy.core.event.Catalog` or
:class:`~obspy.core.inventory.Inventory`
:param obj: ObsPy object for shapefile output
:type filename: str
:param filename: Filename to write to. According to ESRI shapefile
definition, multiple files with the following suffixes will be written:
".shp", ".shx", ".dbj", ".prj". If filename does not end with ".shp",
it will be appended. Other files will be created with respective
suffixes accordingly.
"""
if not HAS_PYSHP:
raise ImportError(IMPORTERROR_MSG)
if not PYSHP_VERSION_AT_LEAST_1_2_11:
warnings.warn(PYSHP_VERSION_WARNING)
if not filename.endswith(".shp"):
filename += ".shp"
writer = shapefile.Writer(shapefile.POINT)
writer.autoBalance = 1
# create the layer
if isinstance(obj, Catalog):
_add_catalog_layer(writer, obj)
elif isinstance(obj, Inventory):
_add_inventory_layer(writer, obj)
else:
msg = ("Object for shapefile output must be "
"a Catalog or Inventory.")
raise TypeError(msg)
writer.save(filename)
_save_projection_file(filename.rsplit('.', 1)[0] + '.prj')
def _add_catalog_layer(writer, catalog):
"""
:type writer: :class:`shapefile.Writer`.
:param writer: pyshp Writer object
:type catalog: :class:`~obspy.core.event.Catalog`
:param catalog: Event data to add as a new layer.
"""
# [name, type, width, precision]
# field name is 10 chars max
# ESRI shapefile attributes are stored in dbf files, which can not
# store datetimes, only dates, see:
# http://www.gdal.org/drv_shapefile.html
# use POSIX timestamp for exact origin time, set time of first pick
# for events with no origin
field_definitions = [
["EventID", 'C', 100, None],
["OriginID", 'C', 100, None],
["MagID", 'C', 100, None],
["Date", 'D', None, None],
["OriginTime", 'N', 20, 6],
["FirstPick", 'N', 20, 6],
["Longitude", 'N', 16, 10],
["Latitude", 'N', 16, 10],
["Depth", 'N', 8, 3],
["MinHorUncM", 'N', 12, 3],
["MaxHorUncM", 'N', 12, 3],
["MaxHorAzi", 'N', 7, 3],
["OriUncDesc", 'C', 40, None],
["Magnitude", 'N', 8, 3],
]
_create_layer(writer, field_definitions)
for event in catalog:
# try to use preferred origin/magnitude, fall back to first or use
# empty one with `None` values in it
origin = (event.preferred_origin() or
event.origins and event.origins[0] or
Origin(force_resource_id=False))
magnitude = (event.preferred_magnitude() or
event.magnitudes and event.magnitudes[0] or
Magnitude(force_resource_id=False))
t_origin = origin.time
pick_times = [pick.time for pick in event.picks
if pick.time is not None]
t_pick = pick_times and min(pick_times) or None
date = t_origin or t_pick
feature = {}
# setting fields with `None` results in values of `0.000`
# need to really omit setting values if they are `None`
if event.resource_id is not None:
feature["EventID"] = str(event.resource_id)
if origin.resource_id is not None:
feature["OriginID"] = str(origin.resource_id)
if t_origin is not None:
# Use timestamp for exact timing
feature["OriginTime"] = t_origin.timestamp
if t_pick is not None:
# Use timestamp for exact timing
feature["FirstPick"] = t_pick.timestamp
if date is not None:
# ESRI shapefile attributes are stored in dbf files, which can
# not store datetimes, only dates. We still need to use the
# GDAL API with precision up to seconds (aiming at other output
# drivers of GDAL; `100` stands for GMT)
feature["Date"] = date.datetime
if origin.latitude is not None:
feature["Latitude"] = origin.latitude
if origin.longitude is not None:
feature["Longitude"] = origin.longitude
if origin.depth is not None:
feature["Depth"] = origin.depth / 1e3
if magnitude.mag is not None:
feature["Magnitude"] = magnitude.mag
if magnitude.resource_id is not None:
feature["MagID"] = str(magnitude.resource_id)
if origin.origin_uncertainty is not None:
ou = origin.origin_uncertainty
ou_description = ou.preferred_description
if ou_description == 'uncertainty ellipse':
feature["MinHorUncM"] = ou.min_horizontal_uncertainty
feature["MaxHorUncM"] = ou.max_horizontal_uncertainty
feature["MaxHorAzi"] = \
ou.azimuth_max_horizontal_uncertainty
feature["OriUncDesc"] = ou_description
elif ou_description == 'horizontal uncertainty':
feature["MinHorUncM"] = ou.horizontal_uncertainty
feature["MaxHorUncM"] = ou.horizontal_uncertainty
feature["MaxHorAzi"] = 0.0
feature["OriUncDesc"] = ou_description
else:
msg = ('Encountered an event with origin uncertainty '
'description of type "{}". This is not yet '
'implemented for output as shapefile. No origin '
'uncertainty will be added to shapefile for such '
'events.').format(ou_description)
warnings.warn(msg)
if origin.latitude is not None and origin.longitude is not None:
writer.point(origin.longitude, origin.latitude)
_add_record(writer, feature)
def _add_inventory_layer(writer, inventory):
"""
:type writer: :class:`shapefile.Writer`.
:param writer: pyshp Writer object
:type inventory: :class:`~obspy.core.inventory.Inventory`
:param inventory: Inventory data to add as a new layer.
"""
# [name, type, width, precision]
# field name is 10 chars max
# ESRI shapefile attributes are stored in dbf files, which can not
# store datetimes, only dates, see:
# http://www.gdal.org/drv_shapefile.html
# use POSIX timestamp for exact origin time, set time of first pick
# for events with no origin
field_definitions = [
["Network", 'C', 20, None],
["Station", 'C', 20, None],
["Longitude", 'N', 16, 10],
["Latitude", 'N', 16, 10],
["Elevation", 'N', 9, 3],
["StartDate", 'D', None, None],
["EndDate", 'D', None, None],
["Channels", 'C', 254, None],
]
_create_layer(writer, field_definitions)
for net in inventory:
for sta in net:
channel_list = ",".join(["%s.%s" % (cha.location_code, cha.code)
for cha in sta])
feature = {}
# setting fields with `None` results in values of `0.000`
# need to really omit setting values if they are `None`
if net.code is not None:
feature["Network"] = net.code
if sta.code is not None:
feature["Station"] = sta.code
if sta.latitude is not None:
feature["Latitude"] = sta.latitude
if sta.longitude is not None:
feature["Longitude"] = sta.longitude
if sta.elevation is not None:
feature["Elevation"] = sta.elevation
if sta.start_date is not None:
# ESRI shapefile attributes are stored in dbf files, which
# can not store datetimes, only dates. We still need to use
# the GDAL API with precision up to seconds (aiming at
# other output drivers of GDAL; `100` stands for GMT)
feature["StartDate"] = sta.start_date.datetime
if sta.end_date is not None:
# ESRI shapefile attributes are stored in dbf files, which
# can not store datetimes, only dates. We still need to use
# the GDAL API with precision up to seconds (aiming at
# other output drivers of GDAL; `100` stands for GMT)
feature["EndDate"] = sta.end_date.datetime
if channel_list:
feature["Channels"] = channel_list
if sta.latitude is not None and sta.longitude is not None:
writer.point(sta.longitude, sta.latitude)
_add_record(writer, feature)
wgs84_wkt = \
"""
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]]
"""
wgs84_wkt = re.sub(r'\s+', '', wgs84_wkt)
def _save_projection_file(filename):
with open(filename, 'wt') as fh:
fh.write(wgs84_wkt)
def _create_layer(writer, field_definitions):
# Add the fields we're interested in
for name, type_, width, precision in field_definitions:
# default field width is not set correctly for dates and booleans in
# shapefile <=1.2.10, see
# GeospatialPython/pyshp@ba61854aa7161fd7d4cff12b0fd08b6ec7581bb7 and
# GeospatialPython/pyshp#71 so work around this
if type_ == 'D':
width = 8
precision = 0
elif type_ == 'L':
width = 1
precision = 0
type_ = native_str(type_)
name = native_str(name)
kwargs = dict(fieldType=type_, size=width, decimal=precision)
# remove None's because shapefile.Writer.field() doesn't use None as
# placeholder but the default values directly
for key in list(kwargs.keys()):
if kwargs[key] is None:
kwargs.pop(key)
writer.field(name, **kwargs)
def _add_record(writer, feature):
values = []
for key, type_, width, precision in writer.fields:
value = feature.get(key)
# various hacks for old pyshp < 1.2.11
if not PYSHP_VERSION_AT_LEAST_1_2_11:
if type_ == 'C':
# mimick pyshp 1.2.12 behavior of putting 'None' in string
# fields for value of `None`
if value is None:
value = 'None'
else:
value = native_str(value)
# older pyshp is not correctly writing dates as thenowadays used
# '%Y%m%d' (8 chars), work around this
elif type_ == 'D':
if isinstance(value, (UTCDateTime, datetime.date)):
value = value.strftime('%Y%m%d')
# work around issues with older pyshp, backport 1.2.12 behavior
elif type_ == 'L':
# logical: 1 byte - initialized to 0x20 (space)
# otherwise T or F
if value in [True, 1]:
value = "T"
elif value in [False, 0]:
value = "F"
else:
value = ' '
# work around issues with older pyshp, backport 1.2.12 behavior
elif type_ in ('N', 'F'):
# numeric or float: number stored as a string, right justified,
# and padded with blanks to the width of the field.
if value in (None, ''):
value = ' ' * width # QGIS NULL
elif not precision:
# caps the size if exceeds the field size
value = format(value, "d")[:width].rjust(width)
else:
# caps the size if exceeds the field size
value = format(value, ".%sf" % precision)[:width].rjust(
width)
# work around older pyshp not converting `None`s properly (e.g. for
# float fields)
elif value is None:
value = ''
values.append(value)
writer.record(*values)
if __name__ == '__main__':
import doctest
doctest.testmod(exclude_empty=True)