config.py 7.86 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
106
    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
107
108
109
110
111
112
113
114
    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
115
    cache_path = Path.T(
116
        default='lassie_phases.cache',
Sebastian Heimann's avatar
Sebastian Heimann committed
117
118
        help='directory where lassie stores tabulated phases etc.')

Sebastian Heimann's avatar
Sebastian Heimann committed
119
    def __init__(self, *args, **kwargs):
Sebastian Heimann's avatar
Sebastian Heimann committed
120
        HasPaths.__init__(self, *args, **kwargs)
Sebastian Heimann's avatar
Sebastian Heimann committed
121
122
        self._receivers = None
        self._grid = None
123
        self._events = None
124
125
        self._config_name = 'untitled'

126
127
128
129
130
131
132
    def setup_image_function_contributions(self):
        '''
        Post-init setup of image function contributors.
        '''
        for ifc in self.image_function_contributions:
            ifc.setup(self)

133
134
135
136
137
138
139
140
141
    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
142

Sebastian Heimann's avatar
Sebastian Heimann committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    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
164
165
166
    def get_receivers(self):
        '''Aggregate receivers from different sources.'''

Sebastian Heimann's avatar
Sebastian Heimann committed
167
168
        fp = self.expand_path

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

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

Sebastian Heimann's avatar
Sebastian Heimann committed
190
191
        return self._receivers

192
193
194
195
196
    def get_events(self):
        if self.events_path is None:
            return None

        if self._events is None:
Sebastian Heimann's avatar
Sebastian Heimann committed
197
198
            self._events = model.load_events(self.expand_path(
                self.events_path))
199
200
201

        return self._events

Sebastian Heimann's avatar
Sebastian Heimann committed
202
203
204
    def get_grid(self):
        '''Get grid or make default grid.'''

205
206
        self.setup_image_function_contributions()

Sebastian Heimann's avatar
Sebastian Heimann committed
207
208
209
210
211
212
        if self._grid is None:

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

                fsmooth_max = max(
213
214
                    ifc.get_fsmooth() for ifc in
                    self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
215

216
217
                vmin = min(ifc.shifter.get_vmin() for ifc in
                           self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
218
219
220
221

                spacing = vmin / fsmooth_max / self.autogrid_density_factor

                lat0, lon0, north, east, depth = geo.bounding_box_square(
222
                    *geo.points_coords(receivers),
Sebastian Heimann's avatar
Sebastian Heimann committed
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
                    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
248
249
250
251
252
253
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 '.')
254
255
    config.set_config_name(op.splitext(op.basename(path))[0])

Sebastian Heimann's avatar
Sebastian Heimann committed
256
257
258
259
260
261
262
263
264
    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
265

266

Sebastian Heimann's avatar
Sebastian Heimann committed
267
268
269
270
__all__ = [
    'Config',
    'read_config',
]