config.py 7.97 KB
Newer Older
Sebastian Heimann's avatar
Sebastian Heimann committed
1
import logging
Sebastian Heimann's avatar
Sebastian Heimann committed
2
import os.path as op
Sebastian Heimann's avatar
Sebastian Heimann committed
3

Marius Kriegerowski's avatar
Marius Kriegerowski committed
4
from pyrocko.guts import Object, String, Float, Timestamp, List, Bool
Sebastian Heimann's avatar
Sebastian Heimann committed
5
from pyrocko import model, guts
6
from pyrocko.fdsn import station as fs
Sebastian Heimann's avatar
Sebastian Heimann committed
7
from pyrocko.gf import TPDef
Sebastian Heimann's avatar
Sebastian Heimann committed
8
9

from lassie import receiver, ifc, grid, geo
10
11
from lassie.common import Earthmodel, HasPaths, Path, LassieError, \
    expand_template
Sebastian Heimann's avatar
Sebastian Heimann committed
12
13
14
15
16
17

guts_prefix = 'lassie'

logger = logging.getLogger('lassie.config')


Sebastian Heimann's avatar
Sebastian Heimann committed
18
class Config(HasPaths):
Sebastian Heimann's avatar
Sebastian Heimann committed
19

Sebastian Heimann's avatar
Sebastian Heimann committed
20
    stations_path = Path.T(
Sebastian Heimann's avatar
Sebastian Heimann committed
21
22
23
        optional=True,
        help='stations file in Pyrocko format')

Sebastian Heimann's avatar
Sebastian Heimann committed
24
    stations_stationxml_path = Path.T(
25
26
27
        optional=True,
        help='stations file in StationXML format')

Sebastian Heimann's avatar
Sebastian Heimann committed
28
29
30
31
32
    receivers = List.T(
        receiver.Receiver.T(),
        help='receiver coordinates if not read from file')

    data_paths = List.T(
Sebastian Heimann's avatar
Sebastian Heimann committed
33
        Path.T(),
Sebastian Heimann's avatar
Sebastian Heimann committed
34
35
        help='list of directories paths to search for data')

Sebastian Heimann's avatar
Sebastian Heimann committed
36
    events_path = Path.T(
37
38
39
40
41
42
43
        optional=True,
        help='limit processing to time windows around given events')

    event_time_window_factor = Float.T(
        default=2.,
        help='controls length of time windows for event-wise processing')

Sebastian Heimann's avatar
Sebastian Heimann committed
44
45
46
47
    blacklist = List.T(
        String.T(),
        help='codes in the form NET.STA.LOC of receivers to be excluded')

48
49
50
51
52
53
54
55
    whitelist = List.T(
        String.T(),
        help='codes in the form NET.STA.LOC of receivers to be included')

    distance_max = Float.T(
        optional=True,
        help='receiver maximum distance from grid')

Sebastian Heimann's avatar
Sebastian Heimann committed
56
57
58
59
60
61
62
63
    tmin = Timestamp.T(
        optional=True,
        help='beginning of time interval to be processed')

    tmax = Timestamp.T(
        optional=True,
        help='end of time interval to be processed')

Sebastian Heimann's avatar
Sebastian Heimann committed
64
    run_path = Path.T(
Sebastian Heimann's avatar
Sebastian Heimann committed
65
        optional=True,
Sebastian Heimann's avatar
Sebastian Heimann committed
66
        help='output is saved to this directory')
67

Sebastian Heimann's avatar
Sebastian Heimann committed
68
69
70
    save_figures = Bool.T(
        default=False,
        help='flag to activate saving of detection figures')
Sebastian Heimann's avatar
Sebastian Heimann committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    grid = grid.Grid.T(
        optional=True,
        help='definition of search grid, if not given, a default grid is '
             'chosen')

    autogrid_radius_factor = Float.T(
        default=1.5,
        help='size factor to use when automatically choosing a grid size')

    autogrid_density_factor = Float.T(
        default=10.0,
        help='grid density factor used when automatically choosing a grid '
             'spacing')

86
    image_function_contributions = List.T(
Sebastian Heimann's avatar
Sebastian Heimann committed
87
88
89
90
91
92
93
        ifc.IFC.T(),
        help='image function contributions')

    sharpness_normalization = Bool.T(
        default=True,
        help='whether to divide image function frames by their mean value')

94
95
96
97
    ifc_count_normalization = Bool.T(
        default=False,
        help='whether to divide image function by number of contributors')

Sebastian Heimann's avatar
Sebastian Heimann committed
98
99
100
101
    detector_threshold = Float.T(
        default=200.,
        help='threshold on detector function')

102
103
104
105
    detector_tpeaksearch = Float.T(
        optional=True,
        help='search time span for peak detection')

106
107
108
109
110
    fill_incomplete_with_zeros = Bool.T(
        default=False,
        help='fill incomplete trace time windows with zeros '
             '(and let edge effects ruin your day)')

Sebastian Heimann's avatar
Sebastian Heimann committed
111
112
113
114
115
116
117
118
    earthmodels = List.T(
        Earthmodel.T(),
        help='list of earthmodels usable in shifters')

    tabulated_phases = List.T(
        TPDef.T(),
        help='list of tabulated phase definitions usable shifters')

Sebastian Heimann's avatar
Sebastian Heimann committed
119
    cache_path = Path.T(
120
        default='lassie_phases.cache',
Sebastian Heimann's avatar
Sebastian Heimann committed
121
122
        help='directory where lassie stores tabulated phases etc.')

Sebastian Heimann's avatar
Sebastian Heimann committed
123
    def __init__(self, *args, **kwargs):
Sebastian Heimann's avatar
Sebastian Heimann committed
124
        HasPaths.__init__(self, *args, **kwargs)
Sebastian Heimann's avatar
Sebastian Heimann committed
125
126
        self._receivers = None
        self._grid = None
127
        self._events = None
128
129
        self._config_name = 'untitled'

130
131
132
133
134
135
136
    def setup_image_function_contributions(self):
        '''
        Post-init setup of image function contributors.
        '''
        for ifc in self.image_function_contributions:
            ifc.setup(self)

137
138
139
140
141
142
143
144
145
    def set_config_name(self, config_name):
        self._config_name = config_name

    def expand_path(self, path):
        def extra(path):
            return expand_template(path, dict(
                config_name=self._config_name))

        return HasPaths.expand_path(self, path, extra=extra)
Sebastian Heimann's avatar
Sebastian Heimann committed
146

Sebastian Heimann's avatar
Sebastian Heimann committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    def get_events_path(self):
        run_path = self.expand_path(self.run_path)
        return op.join(run_path, 'events.list')

    def get_ifm_dir_path(self):
        run_path = self.expand_path(self.run_path)
        return op.join(run_path, 'ifm')

    def get_ifm_path_template(self):
        return op.join(
            self.get_ifm_dir_path(),
            '%(station)s_%(tmin_ms)s.mseed')

    def get_detections_path(self):
        run_path = self.expand_path(self.run_path)
        return op.join(run_path, 'detections.list')

    def get_figures_path_template(self):
        run_path = self.expand_path(self.run_path)
        return op.join(run_path, 'figures', 'detection_%(id)06i.%(format)s')

Sebastian Heimann's avatar
Sebastian Heimann committed
168
169
170
    def get_receivers(self):
        '''Aggregate receivers from different sources.'''

Sebastian Heimann's avatar
Sebastian Heimann committed
171
172
        fp = self.expand_path

Sebastian Heimann's avatar
Sebastian Heimann committed
173
174
175
        if self._receivers is None:
            self._receivers = list(self.receivers)
            if self.stations_path:
Sebastian Heimann's avatar
Sebastian Heimann committed
176
                for station in model.load_stations(fp(self.stations_path)):
Sebastian Heimann's avatar
Sebastian Heimann committed
177
178
179
180
                    self._receivers.append(
                        receiver.Receiver(
                            codes=station.nsl(),
                            lat=station.lat,
181
182
                            lon=station.lon,
                            z=station.depth))
Sebastian Heimann's avatar
Sebastian Heimann committed
183

184
            if self.stations_stationxml_path:
Sebastian Heimann's avatar
Sebastian Heimann committed
185
                sx = fs.load_xml(filename=fp(self.stations_stationxml_path))
186
187
188
189
190
                for station in sx.get_pyrocko_stations():
                    self._receivers.append(
                        receiver.Receiver(
                            codes=station.nsl(),
                            lat=station.lat,
191
192
                            lon=station.lon,
                            z=station.depth))
193

Sebastian Heimann's avatar
Sebastian Heimann committed
194
195
        return self._receivers

196
197
198
199
200
    def get_events(self):
        if self.events_path is None:
            return None

        if self._events is None:
Sebastian Heimann's avatar
Sebastian Heimann committed
201
202
            self._events = model.load_events(self.expand_path(
                self.events_path))
203
204
205

        return self._events

Sebastian Heimann's avatar
Sebastian Heimann committed
206
207
208
    def get_grid(self):
        '''Get grid or make default grid.'''

209
210
        self.setup_image_function_contributions()

Sebastian Heimann's avatar
Sebastian Heimann committed
211
212
213
214
215
216
        if self._grid is None:

            if not self.grid:
                receivers = self.get_receivers()

                fsmooth_max = max(
217
218
                    ifc.get_fsmooth() for ifc in
                    self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
219

220
221
                vmin = min(ifc.shifter.get_vmin() for ifc in
                           self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
222
223
224
225

                spacing = vmin / fsmooth_max / self.autogrid_density_factor

                lat0, lon0, north, east, depth = geo.bounding_box_square(
226
                    *geo.points_coords(receivers),
Sebastian Heimann's avatar
Sebastian Heimann committed
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
                    scale=self.autogrid_radius_factor)

                self._grid = grid.Carthesian3DGrid(
                    lat=lat0,
                    lon=lon0,
                    xmin=north[0],
                    xmax=north[1],
                    dx=spacing,
                    ymin=east[0],
                    ymax=east[1],
                    dy=spacing,
                    zmin=depth[0],
                    zmax=depth[1],
                    dz=spacing)

                logger.info('automatic grid:\n%s' % self._grid)

            else:
                self._grid = self.grid

            self._grid.update()

        return self._grid


Sebastian Heimann's avatar
Sebastian Heimann committed
252
253
254
255
256
257
def read_config(path):
    config = guts.load(filename=path)
    if not isinstance(config, Config):
        raise LassieError('invalid Lassie configuration in file "%s"' % path)

    config.set_basepath(op.dirname(path) or '.')
258
259
    config.set_config_name(op.splitext(op.basename(path))[0])

Sebastian Heimann's avatar
Sebastian Heimann committed
260
261
262
263
264
265
266
267
268
    return config


def write_config(config, path):
    basepath = config.get_basepath()
    dirname = op.dirname(path) or '.'
    config.change_basepath(dirname)
    guts.dump(config, filename=path)
    config.change_basepath(basepath)
Sebastian Heimann's avatar
Sebastian Heimann committed
269

270

Sebastian Heimann's avatar
Sebastian Heimann committed
271
272
273
274
__all__ = [
    'Config',
    'read_config',
]