1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import math
7import numpy as num
8import scipy.signal
10from pyrocko.orthodrome import positive_region
13class OutOfBounds(Exception):
14 pass
17class Tile(object):
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()
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
32 def x(self):
33 return self.xmin + num.arange(self.nx) * self.dx
35 def y(self):
36 return self.ymin + num.arange(self.ny) * self.dy
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')
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()
52 def yextend_with_repeat(self, ymin, ymax):
53 assert ymax >= self.ymax
54 assert ymin <= self.ymin
56 nlo = int(round((self.ymin - ymin) / self.dy))
57 nhi = int(round((ymax - self.ymax) / self.dy))
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, :]
65 self.ymin = ymin
66 self.data = data
67 self._set_maxes()
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()
78def multiple_of(x, dx, eps=1e-5):
79 return abs(int(round(x / dx))*dx - x) < dx * eps
82def combine(tiles, region=None):
83 if not tiles:
84 return None
86 dx = tiles[0].dx
87 dy = tiles[0].dy
88 dtype = tiles[0].data.dtype
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)
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)
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
113 nx = int(round((xmax - xmin) / dx)) + 1
114 ny = int(round((ymax - ymin) / dy)) + 1
116 data = num.zeros((ny, nx), dtype=dtype)
117 data[:, :] = 0
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]
134 if not num.any(num.isfinite(data)):
135 return None
137 return Tile(xmin, ymin, dx, dy, data)