config.py 3.95 KB
Newer Older
Sebastian Heimann's avatar
Sebastian Heimann committed
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
import logging

from pyrocko.guts import Object, String, Float, Timestamp, List, Bool, load
from pyrocko import model

from lassie import receiver, ifc, grid, geo

guts_prefix = 'lassie'

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


class Config(Object):

    stations_path = String.T(
        optional=True,
        help='stations file in Pyrocko format')

    receivers = List.T(
        receiver.Receiver.T(),
        help='receiver coordinates if not read from file')

    data_paths = List.T(
        String.T(),
        help='list of directories paths to search for data')

    blacklist = List.T(
        String.T(),
        help='codes in the form NET.STA.LOC of receivers to be excluded')

    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')

    detections_path = String.T(
        optional=True,
41
42
43
44
        help='output file name for detections')
    
    stackmax_path = String.T(
        optional=True,
45
        help='output path name for stacked traces')
Sebastian Heimann's avatar
Sebastian Heimann committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

    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')

61
    image_function_contributions = List.T(
Sebastian Heimann's avatar
Sebastian Heimann committed
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
        ifc.IFC.T(),
        help='image function contributions')

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

    detector_threshold = Float.T(
        default=200.,
        help='threshold on detector function')

    def __init__(self, *args, **kwargs):
        Object.__init__(self, *args, **kwargs)
        self._receivers = None
        self._grid = None

    def get_receivers(self):
        '''Aggregate receivers from different sources.'''

        if self._receivers is None:
            self._receivers = list(self.receivers)
            if self.stations_path:
                for station in model.load_stations(self.stations_path):
                    self._receivers.append(
                        receiver.Receiver(
                            codes=station.nsl(),
                            lat=station.lat,
                            lon=station.lon))

        return self._receivers

    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(
102
103
                    ifc.get_fsmooth() for ifc in
                    self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
104

105
106
                vmin = min(ifc.shifter.get_vmin() for ifc in
                           self.image_function_contributions)
Sebastian Heimann's avatar
Sebastian Heimann committed
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

                spacing = vmin / fsmooth_max / self.autogrid_density_factor

                lat0, lon0, north, east, depth = geo.bounding_box_square(
                    *receiver.receivers_coords(receivers),
                    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


def read_config(file_path):
    conf = load(filename=file_path)
    return conf

__all__ = [
    'Config',
    'read_config',
]