-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpolation.py
executable file
·323 lines (291 loc) · 10.1 KB
/
interpolation.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python3
#
# Creates various plots to check the interpolation methods.
#
import os
import sys
import matplotlib.cm
import numpy as np
import nevis
# Don't downsample
sys.argv = [x for x in sys.argv if x != '-debug']
# Load height data
nevis.howdy('Interpolation')
heights = nevis.gb()
ny, nx = heights.shape
r = nevis.spacing()
r2 = r // 2 # Height is taken at center of grid square
#
# Squares and objects to draw on the maps
#
# Squares as (square, lines, sea), where each square is (xlo, xhi, ylo, yhi),
# where each line is (x0, x1, y0, y1), and where sea is a bool indicating
# whether the sea mask should be shown
squares = []
# Labels as name : Coords
labels = {}
#
# Squares of about 15x15km look OK
#
# The grid is in 50m by 50m squares, and the height should be taken in the
# middle, so to draw horizontal, vertical, or diagonal lines that align with
# the grid, each number should end in 25 or 75.
#
#
# Ben Nevis 216666 771288
#
labels['Ben Nevis'] = nevis.ben()
square = [210000, 224000, 764000, 778000]
lines = []
# Vertical, from valley to peak
lines.append([216675, 216675, 769275, 771275])
# Vertical, from valley to peak
lines.append([216675, 216675, 773275, 771275])
# Horizontal, from valley up to peak
lines.append([214675, 216675, 771275, 771275])
# Diagonal, from valley up to peak
lines.append([216675 - 1400, 216675, 771275 - 1400, 771275])
squares.append((square, lines, False))
#
# The (firth of) clyde near Dumbarton Rock NS 4001 7391
#
labels['Dumbarton rock'] = nevis.Coords.from_square('NS 39931 74494')
# 239784 239931 672520 674494
square = [233000, 247000, 667000, 681000]
lines = []
# Near Rock, vertical, to hill on other side
lines.append([239925, 239925, 674475, 672525])
# Near Rock, horizontal
lines.append([239925, 239225, 674475, 674475])
squares.append((square, lines, True))
#
# Margate, near Dreamland and its Scenic Railway coaster 635107, 170534
#
labels['Scenic railway'] = nevis.Coords.from_square('TR 35107 70534')
square = [630000, 645000, 159000, 174000]
lines = []
# From margate vertically into the sea
lines.append([635125, 635125, 170225, 171825])
# Through a river and into the sea
lines.append([633125, 636125, 161825, 161825])
squares.append((square, lines, True))
#
# Sea issue NT68: Coast near North Berwick 359353, 682985
#
square = [357000, 371000, 676500, 690500]
lines = []
# Through square, horizontally
lines.append([360025, 369975, 684025, 684025])
# Through square, vertically
lines.append([365025, 365025, 680025, 689975])
squares.append((square, lines, True))
# Through below-sea level "land" in square below
lines.append([362825, 362825, 677925, 680525])
squares.append((square, lines, True))
#
# Sea issues NR35,24,34,44,33: Islay, south-east coast
#
labels['Laphroig'] = nevis.Coords(138639, 645208)
labels['Lagavulin'] = nevis.Coords(140190, 645660)
labels['Ardbeg'] = nevis.Coords(141478, 646419)
square = [119000, 151000, 629000, 661000]
lines = []
# Horizontally through squares
lines.append([130025, 132975, 652025, 652025])
lines.append([129025, 129025, 649975, 647025])
lines.append([120025, 149975, 645025, 645025])
lines.append([130025, 139975, 635025, 635025])
squares.append((square, lines, True))
#
# Sea issues: NR56,57: Jura south-east coast
#
labels['Jura distillery'] = nevis.Coords(152809, 666895)
square = [144000, 166000, 659000, 681000]
lines = []
# Horizontally through squares
lines.append([156025, 159975, 672025, 672025])
lines.append([153025, 159975, 666025, 666025])
lines.append([150025, 159975, 661525, 661525])
squares.append((square, lines, True))
#
# Sea issues: NR65,64,74,76,75: North-west Kintyre and south-west Knapdale
#
square = [159000, 181000, 639000, 671000]
lines = []
# Through NR76
lines.append([170025, 179975, 661525, 661525])
lines.append([171225, 171225, 669975, 660025])
lines.append([160025, 175025, 655025, 655025])
lines.append([160025, 169975, 645025, 645025])
lines.append([165025, 165025, 659975, 640025])
squares.append((square, lines, True))
#
# End of the world
#
square = [-8000, 8000, -8000, 8000]
lines = []
lines.append([-1025, 1025, 5525, 5525])
lines.append([5525, 5525, -1025, 1025])
lines.append([-1025, 1025, -1025, 1025])
squares.append((square, lines, False))
w, h = nx * r, ny * r
square = [w - 8000, w + 8000, -8000, 8000]
lines = []
lines.append([w - 1025, w + 1025, 5525, 5525])
lines.append([w - 5525, w - 5525, -1025, 1025])
lines.append([w - 1025, w + 1025, 1025, -1025])
squares.append((square, lines, False))
w, h = nx * r, ny * r
square = [w - 8000, w + 8000, h - 8000, h + 8000]
lines = []
lines.append([w - 1025, w + 1025, h - 5525, h - 5525])
lines.append([w - 5525, w - 5525, h - 1025, h + 1025])
lines.append([w - 1025, w + 1025, h - 1025, h + 1025])
squares.append((square, lines, False))
#
# Teignmouth
#
square = [290000, 300000, 65000, 80000]
lines = []
# Horizontally through squares
lines.append([293025, 295025, 72525, 72525])
squares.append((square, lines, True))
#
# Holme fen 520483, 289083
#
square = [510000, 570000, 280000, 320000]
lines = []
# Horizontally through squares
lines.append([520025, 531025, 289075, 289075])
lines.append([558825, 560825, 309825, 309825])
lines.append([559825, 559825, 308825, 310825])
squares.append((square, lines, True))
#
# Get interpolants
#
name = 'interpolation'
f = nevis.linear_interpolant()
g = nevis.spline(verbose=True)
h = lambda x, y: 1 if nevis.is_sea(nevis.Coords(x, y)) else 0
#
# Ensure results directory exists
#
root = 'results'
if not os.path.isdir(root):
os.makedirs(root)
#
# Figure: Full map
#
cmap = matplotlib.cm.get_cmap('tab10', 10)
fig, ax, data, ff = nevis.plot(
zoom=0.03,
headless=True)
for ii, sq in enumerate(squares):
square, line, show_sea_mask = sq
x0, x1, y0, y1 = square
x0, y0 = ff(x0, y0)
x1, y1 = ff(x1, y1)
a, b = [x0, x1, x1, x0, x0], [y0, y0, y1, y1, y0]
ax.plot(a, b, 'w', lw=3)
ax.plot(a, b, label=f'Square {ii + 1}')
ax.legend(loc='upper left')
path = os.path.join(root, f'{name}-check-0-map-full.png')
print()
print(f'Saving figure to {path}.')
fig.savefig(path)
#
# Figure 2: Zoomed maps
#
for ii, sq in enumerate(squares):
square, lines, show_sea_mask = sq
fig, ax, data, ff = nevis.plot(
boundaries=square,
labels=labels,
zoom=1,
small_grid=True,
headless=True)
for jj, line in enumerate(lines):
x0, x1, y0, y1 = line
a0, b0 = ff(x0, y0)
a1, b1 = ff(x1, y1)
c, d = 0.1 * (a1 - a0), 0.1 * (b1 - b0)
color = cmap(jj) if jj < 2 else cmap(1 + jj)
ax.plot([a0, a1], [b0, b1], label=f'line {jj + 1}', color=color)
ax.plot(
[a1, a1 - c + d, a1 - c - d, a1],
[b1, b1 - d - c, b1 - d + c, b1],
color=color)
if lines:
ax.legend(loc='lower right')
path = os.path.join(root, f'{name}-check-{ii + 1}-0-map.png')
print(f'Saving figure to {path}.')
fig.savefig(path)
# Figure 3: Line from known top to you
fs = (f, g, h) if show_sea_mask else (f, g)
for jj, line in enumerate(lines):
x0, x1, y0, y1 = line
p0, p1 = nevis.Coords(x0, y0), nevis.Coords(x1, y1)
fig, ax, q0, q1 = nevis.plot_line(
fs, p0, p1, figsize=(14, 10), evaluations=1000, headless=True)
ax.minorticks_on()
ax.grid(which='major')
ax.grid(which='minor', color='#eeeeee')
label = f'Line {jj + 1}'
# Compare with grid points
if x0 % r == x1 % r == y0 % r == y1 % r == r2:
# Vertical line
if x0 == x1:
i0 = q0.grid[1] // r
i1 = q1.grid[1] // r
j0 = x0 // r
ts = r * (np.arange(0, 1 + abs(i1 - i0)) - abs(y0 // r - i0))
s = 1 if i1 > i0 else -1
ss = np.arange(i0, i1 + s, s)
ok = np.nonzero((ss >= 0) & (ss < ny))
if np.any(ok):
ss = [heights[s, j0] for s in ss[ok]]
ax.plot(ts[ok], ss, '+', label='Data values')
if np.min(ss) <= 0 and np.max(ss) >= 0:
ax.axhline(0, color='k', lw=0.5, alpha=0.2)
ax.legend(loc='center left')
label += ', Vertical'
elif y0 == y1:
i0 = y0 // r
j0 = q0.grid[0] // r
j1 = q1.grid[0] // r
ts = r * (np.arange(0, 1 + abs(j1 - j0)) - abs(x0 // r - j0))
s = 1 if j1 > j0 else -1
ss = np.arange(j0, j1 + s, s)
ok = np.nonzero((ss >= 0) & (ss < nx))
if np.any(ok):
ss = np.array([heights[i0, s] for s in ss[ok]])
ax.plot(ts[ok], ss, '+', label='Data values')
if np.min(ss) <= 0 and np.max(ss) >= 0:
ax.axhline(0, color='k', lw=0.5, alpha=0.4)
ax.legend(loc='center left')
label += ', Horizontal'
elif (abs(y1 - y0) == abs(x1 - x0)):
pad = min(abs(q0.grid[0] - x0), abs(q0.grid[1] - y0)) // r
sx = 1 if x1 > x0 else -1
sy = 1 if y1 > y0 else -1
j0 = x0 // r - pad * sx
j1 = x1 // r + pad * sx
i0 = y0 // r - pad * sy
i1 = y1 // r + pad * sy
tjs = r * (np.arange(0, 1 + abs(j1 - j0)) - abs(x0 // r - j0))
tis = r * (np.arange(0, 1 + abs(i1 - i0)) - abs(y0 // r - i0))
sx = np.arange(j0, j1 + sx, sx)
sy = np.arange(i0, i1 + sy, sy)
ok = np.nonzero((sx >= 0) & (sy >= 0) & (sx < nx) & (sy < ny))
if np.any(ok):
ss = [heights[j, i] for j, i in zip(sy[ok], sx[ok])]
ts = np.sqrt(tjs**2 + tis**2) * np.sign(tjs)
ax.plot(ts[ok], ss, '+', label='Data values')
if np.min(ss) <= 0 and np.max(ss) >= 0:
ax.axhline(0, color='k', lw=0.5, alpha=0.2)
ax.legend(loc='center left')
label += ', Diagonal'
fig.text(0.99, 0.97, label, ha='right', va='center')
path = os.path.join(root, f'{name}-check-{ii + 1}-{jj + 1}-line.png')
print(f'Saving figure to {path}.')
fig.savefig(path)