1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6import math 

7import numpy as num 

8import scipy.signal 

9 

10from pyrocko.orthodrome import positive_region 

11 

12 

13class OutOfBounds(Exception): 

14 pass 

15 

16 

17class Tile(object): 

18 

19 def __init__(self, xmin, ymin, dx, dy, data): 

20 self.xmin = float(xmin) 

21 self.ymin = float(ymin) 

22 self.dx = float(dx) 

23 self.dy = float(dy) 

24 self.data = data 

25 self._set_maxes() 

26 

27 def _set_maxes(self): 

28 self.ny, self.nx = self.data.shape 

29 self.xmax = self.xmin + (self.nx-1) * self.dx 

30 self.ymax = self.ymin + (self.ny-1) * self.dy 

31 

32 def x(self): 

33 return self.xmin + num.arange(self.nx) * self.dx 

34 

35 def y(self): 

36 return self.ymin + num.arange(self.ny) * self.dy 

37 

38 def decimate(self, ndeci): 

39 assert ndeci % 2 == 0 

40 kernel = num.ones((ndeci+1, ndeci+1)) 

41 kernel /= num.sum(kernel) 

42 data = scipy.signal.convolve2d( 

43 self.data.astype(float), kernel, mode='valid') 

44 

45 self.data = data[::ndeci, ::ndeci].astype(self.data.dtype) 

46 self.xmin += ndeci/2 

47 self.ymin += ndeci/2 

48 self.dx *= ndeci 

49 self.dy *= ndeci 

50 self._set_maxes() 

51 

52 def yextend_with_repeat(self, ymin, ymax): 

53 assert ymax >= self.ymax 

54 assert ymin <= self.ymin 

55 

56 nlo = int(round((self.ymin - ymin) / self.dy)) 

57 nhi = int(round((ymax - self.ymax) / self.dy)) 

58 

59 nx, ny = self.nx, self.ny 

60 data = num.zeros((ny+nlo+nhi, nx), dtype=self.data.dtype) 

61 data[:nlo, :] = self.data[nlo, :] 

62 data[nlo:nlo+ny, :] = self.data 

63 data[nlo+ny:, :] = self.data[-1, :] 

64 

65 self.ymin = ymin 

66 self.data = data 

67 self._set_maxes() 

68 

69 def get(self, x, y): 

70 ix = int(round((x - self.xmin) / self.dx)) 

71 iy = int(round((y - self.ymin) / self.dy)) 

72 if 0 <= ix < self.nx and 0 <= iy < self.ny: 

73 return self.data[iy, ix] 

74 else: 

75 raise OutOfBounds() 

76 

77 

78def multiple_of(x, dx, eps=1e-5): 

79 return abs(int(round(x / dx))*dx - x) < dx * eps 

80 

81 

82def combine(tiles, region=None): 

83 if not tiles: 

84 return None 

85 

86 dx = tiles[0].dx 

87 dy = tiles[0].dy 

88 dtype = tiles[0].data.dtype 

89 

90 assert all(t.dx == dx for t in tiles) 

91 assert all(t.dy == dy for t in tiles) 

92 assert all(t.data.dtype == dtype for t in tiles) 

93 assert all(multiple_of(t.xmin, dx) for t in tiles) 

94 assert all(multiple_of(t.ymin, dy) for t in tiles) 

95 

96 if region is None: 

97 xmin = min(t.xmin for t in tiles) 

98 xmax = max(t.xmax for t in tiles) 

99 ymin = min(t.ymin for t in tiles) 

100 ymax = max(t.ymax for t in tiles) 

101 else: 

102 xmin, xmax, ymin, ymax = positive_region(region) 

103 

104 if not multiple_of(xmin, dx): 

105 xmin = math.floor(xmin / dx) * dx 

106 if not multiple_of(xmax, dx): 

107 xmax = math.ceil(xmax / dx) * dx 

108 if not multiple_of(ymin, dy): 

109 ymin = math.floor(ymin / dy) * dy 

110 if not multiple_of(ymax, dy): 

111 ymax = math.ceil(ymax / dy) * dy 

112 

113 nx = int(round((xmax - xmin) / dx)) + 1 

114 ny = int(round((ymax - ymin) / dy)) + 1 

115 

116 data = num.zeros((ny, nx), dtype=dtype) 

117 data[:, :] = 0 

118 

119 for t in tiles: 

120 for txmin in (t.xmin, t.xmin + 360.): 

121 ix = int(round((txmin - xmin) / dx)) 

122 iy = int(round((t.ymin - ymin) / dy)) 

123 ixlo = max(ix, 0) 

124 ixhi = min(ix+t.nx, nx) 

125 iylo = max(iy, 0) 

126 iyhi = min(iy+t.ny, ny) 

127 jxlo = ixlo-ix 

128 jxhi = jxlo + max(0, ixhi - ixlo) 

129 jylo = iylo-iy 

130 jyhi = jylo + max(0, iyhi - iylo) 

131 if iyhi > iylo and ixhi > ixlo: 

132 data[iylo:iyhi, ixlo:ixhi] = t.data[jylo:jyhi, jxlo:jxhi] 

133 

134 if not num.any(num.isfinite(data)): 

135 return None 

136 

137 return Tile(xmin, ymin, dx, dy, data)