Coverage for /usr/local/lib/python3.11/dist-packages/grond/problems/volume_point/problem.py: 57%

47 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-26 16:25 +0000

1import logging 

2 

3from pyrocko import gf, util 

4from pyrocko.guts import String, Float, Dict 

5 

6from grond.meta import expand_template, Parameter, \ 

7 has_get_plot_classes 

8 

9from ..base import Problem, ProblemConfig 

10 

11guts_prefix = 'grond' 

12logger = logging.getLogger('grond.problems.volume_point') 

13km = 1e3 

14as_km = dict(scale_factor=km, scale_unit='km') 

15as_km3 = dict(scale_factor=km**3, scale_unit='km^3') 

16 

17 

18class VolumePointProblemConfig(ProblemConfig): 

19 ranges = Dict.T(String.T(), gf.Range.T()) 

20 distance_min = Float.T(default=0.0) 

21 

22 def get_problem(self, event, target_groups, targets): 

23 self.check_deprecations() 

24 

25 if event.depth is None: 

26 event.depth = 0. 

27 

28 base_source = gf.ExplosionSource.from_pyrocko_event(event) 

29 base_source.stf = gf.HalfSinusoidSTF(duration=event.duration or 0.0) 

30 

31 subs = dict( 

32 event_name=event.name, 

33 event_time=util.time_to_str(event.time)) 

34 

35 problem = VolumePointProblem( 

36 name=expand_template(self.name_template, subs), 

37 base_source=base_source, 

38 target_groups=target_groups, 

39 targets=targets, 

40 ranges=self.ranges, 

41 distance_min=self.distance_min, 

42 norm_exponent=self.norm_exponent) 

43 

44 return problem 

45 

46 

47@has_get_plot_classes 

48class VolumePointProblem(Problem): 

49 

50 problem_parameters = [ 

51 Parameter('north_shift', 'm', label='Northing', **as_km), 

52 Parameter('east_shift', 'm', label='Easting', **as_km), 

53 Parameter('depth', 'm', label='Depth', **as_km), 

54 Parameter('volume_change', 'm^3', label='Volume Change', **as_km3) 

55 ] 

56 

57 problem_waveform_parameters = [ 

58 Parameter('time', 's', label='Time'), 

59 Parameter('duration', 's', label='Duration') 

60 ] 

61 

62 distance_min = Float.T(default=0.0) 

63 

64 def pack(self, source): 

65 arr = self.get_parameter_array(source) 

66 for ip, p in enumerate(self.parameters): 

67 if p.name == 'time': 

68 arr[ip] -= self.base_source.time 

69 return arr 

70 

71 def get_source(self, x): 

72 d = self.get_parameter_dict(x) 

73 

74 p = {k: float(self.ranges[k].make_relative(self.base_source[k], d[k])) 

75 for k in self.base_source.keys() if k in d} 

76 

77 stf = None 

78 if self.has_waveforms: 

79 stf = gf.HalfSinusoidSTF(duration=float(d.duration)) 

80 

81 source = self.base_source.clone(stf=stf, **p) 

82 return source 

83 

84 @classmethod 

85 def get_plot_classes(cls): 

86 from . import plot 

87 plots = super(VolumePointProblem, cls).get_plot_classes() 

88 plots.extend([plot.VolumePointLocationPlot]) 

89 return plots