config.py 7.6 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
120
121
122
    def __init__(self, *args, **kwargs):
        Object.__init__(self, *args, **kwargs)
        self._receivers = None
        self._grid = None
123
        self._events = None
124
125
126
127
128
129
130
131
132
133
134
        self._config_name = 'untitled'

    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
135

Sebastian Heimann's avatar
Sebastian Heimann committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    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
157
158
159
    def get_receivers(self):
        '''Aggregate receivers from different sources.'''

Sebastian Heimann's avatar
Sebastian Heimann committed
160
161
        fp = self.expand_path

Sebastian Heimann's avatar
Sebastian Heimann committed
162
163
164
        if self._receivers is None:
            self._receivers = list(self.receivers)
            if self.stations_path:
Sebastian Heimann's avatar
Sebastian Heimann committed
165
                for station in model.load_stations(fp(self.stations_path)):
Sebastian Heimann's avatar
Sebastian Heimann committed
166
167
168
169
                    self._receivers.append(
                        receiver.Receiver(
                            codes=station.nsl(),
                            lat=station.lat,
170
171
                            lon=station.lon,
                            z=station.depth))
Sebastian Heimann's avatar
Sebastian Heimann committed
172

173
            if self.stations_stationxml_path:
Sebastian Heimann's avatar
Sebastian Heimann committed
174
                sx = fs.load_xml(filename=fp(self.stations_stationxml_path))
175
176
177
178
179
                for station in sx.get_pyrocko_stations():
                    self._receivers.append(
                        receiver.Receiver(
                            codes=station.nsl(),
                            lat=station.lat,
180
181
                            lon=station.lon,
                            z=station.depth))
182

Sebastian Heimann's avatar
Sebastian Heimann committed
183
184
        return self._receivers

185
186
187
188
189
    def get_events(self):
        if self.events_path is None:
            return None

        if self._events is None:
Sebastian Heimann's avatar
Sebastian Heimann committed
190
191
            self._events = model.load_events(self.expand_path(
                self.events_path))
192
193
194

        return self._events

Sebastian Heimann's avatar
Sebastian Heimann committed
195
196
197
198
199
200
201
202
203
    def get_grid(self):
        '''Get grid or make default grid.'''

        if self._grid is None:

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

                fsmooth_max = max(
204
205
                    ifc.get_fsmooth() for ifc in
                    self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
206

207
208
                vmin = min(ifc.shifter.get_vmin() for ifc in
                           self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
209
210
211
212

                spacing = vmin / fsmooth_max / self.autogrid_density_factor

                lat0, lon0, north, east, depth = geo.bounding_box_square(
213
                    *geo.points_coords(receivers),
Sebastian Heimann's avatar
Sebastian Heimann committed
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
                    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
239
240
241
242
243
244
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 '.')
245
246
    config.set_config_name(op.splitext(op.basename(path))[0])

Sebastian Heimann's avatar
Sebastian Heimann committed
247
248
249
250
251
252
253
254
255
    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
256

257

Sebastian Heimann's avatar
Sebastian Heimann committed
258
259
260
261
__all__ = [
    'Config',
    'read_config',
]