-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmaskedge.py
365 lines (331 loc) · 11.4 KB
/
maskedge.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import numpy as np
import netCDF4
import sys
#import Ngl
# This thing was originally written in C++ before there was a standard
# (1994 or 1995). Unfortunately, it needed to be recompiled for each
# domain so here I attempt a rewrite.
#
# There was even once a version that used the graphics from the HLU
# library that was a step on the road to NCL. Hmm - plots... include as
# many of the Ngl commands as you like.
# - Kate Hedstrom (2015)
# Read the rho-point land mask from a NetCDF grid file and produce
# the i,j pairs describing the land/sea interface. This was
# originally for the elliptic solver in SPEM, but is now for river
# sources in ROMS. It can also be used just to find out if you left
# any lakes in your domain when editing the land mask.
def readmask(ncfile):
nc = netCDF4.Dataset(ncfile, 'r')
mask2 = nc.variables['mask_rho'][:]
imask = mask2.astype(int)
nc.close()
# Stop the flood-fill at the edges
imask[0,:] = -1
imask[:,0] = -1
imask[-1,:] = -1
imask[:,-1] = -1
return imask
def flood_fill_water(imask, i, j, ii):
"""
Find all water touching point (i,j) and set to ii.
The recursive version works for teeny, tiny grids only, so we need
another way.
"""
Mp, Lp = imask.shape
Lm = Lp-2
Mm = Mp-2
jj = imask[j,i]
llist = []
llist.append((j,i))
while len(llist) > 0:
(j,i) = llist.pop()
imask[j,i] = ii
if ( imask[j,i-1] == jj and i > 1 ):
llist.append((j, i-1))
if ( imask[j-1,i] == jj and j > 1 ):
llist.append((j-1, i))
if ( imask[j,i+1] == jj and i < Lm ):
llist.append((j, i+1))
if ( imask[j+1,i] == jj and j < Mm ):
llist.append((j+1, i))
def flood_fill_land(imask, i, j, ii):
"""
Find all land touching point (i,j) and set to ii.
The recursive version works for teeny, tiny grids only, so we need
another way.
"""
Mp, Lp = imask.shape
Lm = Lp-2
Mm = Mp-2
jj = imask[j,i]
llist = []
llist.append((j,i))
while len(llist) > 0:
(j,i) = llist.pop()
imask[j,i] = ii
if ( imask[j,i-1] == jj and i > 1 ):
llist.append((j, i-1))
if ( imask[j-1,i] == jj and j > 1 ):
llist.append((j-1, i))
if ( imask[j,i+1] == jj and i < Lm ):
llist.append((j, i+1))
if ( imask[j+1,i] == jj and j < Mm ):
llist.append((j+1, i))
# now do the diagonals
if ( imask[j-1,i-1] == jj and i > 1 ):
llist.append((j-1, i-1))
if ( imask[j-1,i+1] == jj and j > 1 ):
llist.append((j-1, i+1))
if ( imask[j+1,i+1] == jj and i < Lm ):
llist.append((j+1, i+1))
if ( imask[j+1,i-1] == jj and j < Mm ):
llist.append((j+1, i-1))
def set_values(imask, k, val):
""" Set all k values to val"""
Mp, Lp = imask.shape
for j in range(1,Mp-1):
for i in range(1,Lp-1):
if (imask[j,i] == k): imask[j,i] = val
def warning(*objs):
print("STDERR: ", *objs, file=sys.stderr)
def color_water(imask):
""" Set each body of water to a different value, return number of
bodies of water."""
count = 2
Mp, Lp = imask.shape
for j in range(1,Mp-1):
for i in range(1,Lp-1):
if (imask[j,i] == 1):
flood_fill_water(imask, i, j, count)
warning("New body!", i, j)
count += 1
warning("There are", count-2, " bodies of water.")
return count-2
def peninsula(imask, plist, i, j, dir, iwat, iland):
"""
Process a found peninsula - append to a linked list of its points
and then set its mask points to water.
"""
Mp, Lp = imask.shape
plist.append((-3, -3))
p = (i,j)
# set up new linked list of Points
# assert(isEdge(p));
plist.append(p)
if (dir == 'east'):
seed = (i,j)
elif (dir == 'west'):
seed = (i-1,j-1)
elif (dir == 'south'):
seed = (i,j-1)
elif (dir == 'north'):
seed = (i-1,j)
# warning("Peninsula at", seed, dir)
# Trace the edge of the peninsula, keeping track of the psi
# points in a linked list. The seed point is kept for the
# flood_fill, setting the land to match the current water
# value.
while True:
if (dir == 'east'):
i += 1
p = (i,j)
if ((imask[j-1,i] == iland) and (imask[j-1,i-1] == iwat)):
dir = 'south'
elif ((imask[j,i] == iland) and (imask[j-1,i] == iwat)):
dir = 'east'
elif ((imask[j,i-1] == iland) and (imask[j,i] == iwat)):
dir = 'north'
elif (i==1 or j==1 or i==Lp-1 or j==Mp-1):
break
else:
warning("Problem in peninsula at ", i, j)
exit(1)
elif (dir == 'north'):
j += 1
p = (i,j)
if ((imask[j,i] == iland) and (imask[j-1,i] == iwat)):
dir = 'east'
elif ((imask[j,i-1] == iland) and (imask[j,i] == iwat)):
dir = 'north'
elif ((imask[j-1,i-1] == iland) and (imask[j,i-1] == iwat)):
dir = 'west'
elif (i==1 or j==1 or i==Lp-1 or j==Mp-1):
break
else:
warning("Problem in peninsula at ", i, j)
exit(1)
elif (dir == 'west'):
i -= 1
p = (i,j)
if ((imask[j,i-1] == iland) and (imask[j,i] == iwat)):
dir = 'north'
elif ((imask[j-1,i-1] == iland) and (imask[j,i-1] == iwat)):
dir = 'west'
elif ((imask[j-1,i] == iland) and (imask[j-1,i-1] == iwat)):
dir = 'south'
elif (i==1 or j==1 or i==Lp-1 or j==Mp-1):
break
else:
warning("Problem in peninsula at ", i, j)
exit(1)
elif (dir == 'south'):
j -= 1
p = (i,j)
if ((imask[j-1,i-1] == iland) and (imask[j,i-1] == iwat)):
dir = 'west'
elif ((imask[j-1,i] == iland) and (imask[j-1,i-1] == iwat)):
dir = 'south'
elif ((imask[j,i] == iland) and (imask[j-1,i] == iwat)):
dir = 'east'
elif (i==1 or j==1 or i==Lp-1 or j==Mp-1):
break
else:
warning("Problem in peninsula at ", i, j)
exit(1)
plist.append(p)
plist.append(p)
# time to flood_fill to show we are done with this peninsula
i = seed[0];
j = seed[1];
flood_fill_land(imask, i, j, iwat);
def island(imask, ilist, i, j, dir, iwat, iland):
"""
Process a found island - create a linked list of its points
and then set its mask points to water.
"""
ilist.append((-1, -1))
p = (i,j)
pstart = p
# add to list of i,j Points
# assert(!p.isEdge());
ilist.append(p)
# Now we change p so the exit condition doesn't happen yet...
p = (0,0)
if (dir == 'east'):
seed = (i,j)
elif (dir == 'west'):
seed = (i-1,j-1)
elif (dir == 'south'):
seed = (i,j-1)
elif (dir == 'north'):
seed = (i-1,j)
# warning("Island at", seed, dir)
# Trace the edge of the island, keeping track of the psi
# points in a linked list. Also keep track of the east and west
# edge segments so that we can later change the peninsula mask
# points to water.
while True:
if (p == pstart):
break
if (dir == 'east'):
i += 1
p = (i,j)
if ((imask[j-1,i] == iland) and (imask[j-1,i-1] == iwat)):
dir = 'south'
elif ((imask[j,i] == iland) and (imask[j-1,i] == iwat)):
dir = 'east'
elif ((imask[j,i-1] == iland) and (imask[j,i] == iwat)):
dir = 'north'
else:
warning("Problem in island at ", i, j)
exit(1)
elif (dir == 'north'):
j += 1
p = (i,j)
if ((imask[j,i] == iland) and (imask[j-1,i] == iwat)):
dir = 'east'
elif ((imask[j,i-1] == iland) and (imask[j,i] == iwat)):
dir = 'north'
elif ((imask[j-1,i-1] == iland) and (imask[j,i-1] == iwat)):
dir = 'west'
else:
warning("Problem in island at ", i, j)
exit(1)
elif (dir == 'west'):
i -= 1
p = (i,j)
if ((imask[j,i-1] == iland) and (imask[j,i] == iwat)):
dir = 'north'
elif ((imask[j-1,i-1] == iland) and (imask[j,i-1] == iwat)):
dir = 'west'
elif ((imask[j-1,i] == iland) and (imask[j-1,i-1] == iwat)):
dir = 'south'
else:
warning("Problem in island at ", i, j)
exit(1)
elif (dir == 'south'):
j -= 1
p = (i,j)
if ((imask[j-1,i-1] == iland) and (imask[j,i-1] == iwat)):
dir = 'west'
elif ((imask[j-1,i] == iland) and (imask[j-1,i-1] == iwat)):
dir = 'south'
elif ((imask[j,i] == iland) and (imask[j-1,i] == iwat)):
dir = 'east'
else:
warning("Problem in island at ", i, j)
exit(1)
ilist.append(p)
# time to flood_fill to show we are done with this island
i = seed[0];
j = seed[1];
flood_fill_land(imask, i, j, iwat);
def edges(imask, plist, iwat, iland):
"""
Search for peninsulas by going around the boundary.
"""
Mp, Lp = imask.shape
for j in range(1,Mp-2):
if ((imask[j,1] == iwat) and (imask[j+1,1] == iland)):
peninsula(imask, plist, 1, j+1, 'east', iwat, iland)
for i in range(1,Lp-2):
if ((imask[Mp-2,i] == iwat) and (imask[Mp-2,i+1] == iland)):
peninsula(imask, plist, i+1, Mp-1, 'south', iwat, iland)
for j in list(reversed(list(range(2,Mp-1)))):
if ((imask[j,Lp-2] == iwat) and (imask[j-1,Lp-2] == iland)):
peninsula(imask, plist, Lp-1, j, 'west', iwat, iland)
for i in list(reversed(list(range(2,Lp-1)))):
if ((imask[1,i] == iwat) and (imask[1,i-1] == iland)):
peninsula(imask, plist, i, 1, 'north', iwat, iland)
def interior(imask, ilist, iwat, iland):
"""
Search for islands by scanning the interior.
"""
Mp, Lp = imask.shape
for i in range(2,Lp-2):
for j in range(2,Mp-2):
if ((imask[j,i] == iwat) and (imask[j+1,i] == iland)):
island(imask, ilist, i, j+1, 'east', iwat, iland)
def main():
ncfile = sys.argv[1]
imask = readmask(ncfile)
lpoints = []
ipoints = []
# wks_type = "X11"
# wks = Ngl.open_wks(wks_type,"maskedge")
# res = Ngl.Resources()
# res.cnFillOn = True
# Ngl.contour(wks,imask,res)
count = color_water(imask)
# Ngl.contour(wks,imask,res)
iland = 0
for iwat in range(2, count+2):
edges(imask, lpoints, iwat, iland)
# Ngl.contour(wks,imask,res)
interior(imask, ipoints, iwat, iland)
# Ngl.contour(wks,imask,res)
set_values(imask, iland, iwat)
# Ngl.contour(wks,imask,res)
iland = iwat
# Islands first, then peninsulas
for point in ipoints:
i,j = point
print(i, j)
for point in lpoints:
i,j = point
print(i, j)
print(-10, -10)
# Ngl.end()
if __name__ == "__main__":
main()