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

import numpy as num 

import logging 

 

from pyrocko import trace 

from pyrocko.guts import (Object, Dict, String, Float, Bool, Int) 

 

logger = logging.getLogger('grond.synthetic_tests') 

 

guts_prefix = 'grond' 

 

 

class SyntheticWaveformNotAvailable(Exception): 

pass 

 

 

class SyntheticTest(Object): 

inject_solution = Bool.T(default=False) 

respect_data_availability = Bool.T(default=False) 

real_noise_scale = Float.T(default=0.0) 

white_noise_scale = Float.T(default=0.0) 

relative_white_noise_scale = Float.T(default=0.0) 

random_response_scale = Float.T(default=0.0) 

real_noise_toffset = Float.T(default=-3600.) 

random_seed = Int.T(optional=True) 

x = Dict.T(String.T(), Float.T()) 

 

def __init__(self, **kwargs): 

Object.__init__(self, **kwargs) 

self._problem = None 

self._synthetics = None 

 

def set_problem(self, problem): 

self._problem = problem 

self._synthetics = None 

 

def get_problem(self): 

if self._problem is None: 

raise SyntheticWaveformNotAvailable( 

'SyntheticTest.set_problem() has not been called yet') 

 

return self._problem 

 

def get_x(self): 

problem = self.get_problem() 

if self.x: 

x = problem.preconstrain( 

problem.get_parameter_array(self.x)) 

 

else: 

x = problem.preconstrain( 

problem.pack( 

problem.base_source)) 

 

return x 

 

def get_synthetics(self): 

problem = self.get_problem() 

if self._synthetics is None: 

x = self.get_x() 

results = problem.forward(x) 

synthetics = {} 

for iresult, result in enumerate(results): 

tr = result.trace.pyrocko_trace() 

tfade = tr.tmax - tr.tmin 

tr_orig = tr.copy() 

tr.extend(tr.tmin - tfade, tr.tmax + tfade) 

rstate = num.random.RandomState( 

(self.random_seed or 0) + iresult) 

 

if self.random_response_scale != 0: 

tf = RandomResponse(scale=self.random_response_scale) 

tf.set_random_state(rstate) 

tr = tr.transfer( 

tfade=tfade, 

transfer_function=tf) 

 

if self.white_noise_scale != 0.0: 

u = rstate.normal( 

scale=self.white_noise_scale, 

size=tr.data_len()) 

 

tr.ydata += u 

 

if self.relative_white_noise_scale != 0.0: 

u = rstate.normal( 

scale=self.relative_white_noise_scale * num.std( 

tr_orig.ydata), 

size=tr.data_len()) 

 

tr.ydata += u 

 

synthetics[result.trace.codes] = tr 

 

self._synthetics = synthetics 

 

return self._synthetics 

 

def get_waveform(self, nslc, tmin, tmax, tfade=0., freqlimits=None): 

synthetics = self.get_synthetics() 

if nslc not in synthetics: 

s = 'no synthetics for %s available' % '.'.join(nslc) 

logger.warn(s) 

from grond.dataset import NotFound 

raise NotFound(s) 

 

tr = synthetics[nslc] 

tr.extend(tmin - tfade * 2.0, tmax + tfade * 2.0) 

 

tr = tr.transfer( 

tfade=tfade, 

freqlimits=freqlimits) 

 

tr.chop(tmin, tmax) 

return tr 

 

 

class RandomResponse(trace.FrequencyResponse): 

 

scale = Float.T(default=0.0) 

 

def set_random_state(self, rstate): 

self._rstate = rstate 

 

def evaluate(self, freqs): 

n = freqs.size 

return 1.0 + ( 

self._rstate.normal(scale=self.scale, size=n) + 

0.0J * self._rstate.normal(scale=self.scale, size=n)) 

 

 

__all__ = ''' 

SyntheticTest 

SyntheticWaveformNotAvailable 

'''.split()