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

import numpy as num 

from pyrocko.guts import Bool 

 

from .plugin import PluginConfig, Plugin 

 

 

class DerampConfig(PluginConfig): 

 

demean = Bool.T(optional=True, default=True) 

 

 

class Deramp(Plugin): 

 

def __init__(self, scene, config=None): 

self.scene = scene 

self.config = config or DerampConfig() 

 

self._log = scene._log.getChild('Deramp') 

 

def get_ramp_coefficients(self, displacement): 

'''Fit plane through the displacement data. 

 

:returns: Mean of the displacement and slopes in easting coefficients 

of the fitted plane. The array hold 

``[offset_e, offset_n, slope_e, slope_n]``. 

:rtype: :class:`numpy.ndarray` 

''' 

scene = self.scene 

msk = num.isfinite(displacement) 

displacement = displacement[msk] 

 

coords = scene.frame.coordinates[msk.flatten()] 

 

# Add ones for the offset 

coords = num.hstack(( 

num.ones_like(coords), 

coords)) 

 

coeffs, res, _, _ = num.linalg.lstsq( 

coords, displacement, rcond=None) 

 

return coeffs 

 

def set_demean(self, demean): 

assert isinstance(demean, bool) 

self.config.demean = demean 

self.update() 

 

def apply(self, displacement): 

'''Fit a plane onto the displacement data and substract it 

 

:param demean: Demean the displacement 

:type demean: bool 

:param inplace: Replace data of the scene (default: True) 

:type inplace: bool 

 

:return: ``None`` if ``inplace=True`` else a new Scene 

:rtype: ``None`` or :class:`~kite.Scene` 

''' 

self._log.debug('De-ramping scene') 

coeffs = self.get_ramp_coefficients(displacement) 

coords = self.scene.frame.coordinates 

 

ramp = coeffs[2:] * coords 

if self.config.demean: 

ramp += coeffs[:2] 

 

ramp = ramp.sum(axis=1).reshape(displacement.shape) 

 

displacement -= ramp 

return displacement