-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathhand_dinf.py
More file actions
612 lines (541 loc) · 21.3 KB
/
hand_dinf.py
File metadata and controls
612 lines (541 loc) · 21.3 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
"""D-infinity Height Above Nearest Drainage (HAND).
Uses D-inf angle decomposition for downstream tracing. At each cell,
the dominant neighbor (higher weight) is followed to find the nearest
stream cell. HAND = elevation - drain_elevation.
Algorithm
---------
CPU : Kahn's BFS topological sort with reverse propagation of drain_elev.
In-degrees use both D-inf neighbors; reverse pass follows dominant.
GPU : CuPy-via-CPU.
Dask: iterative tile sweep with BoundaryStore exit-label propagation.
"""
from __future__ import annotations
import math
import numpy as np
import xarray as xr
try:
import dask.array as da
except ImportError:
da = None
from xrspatial.hydro.flow_accumulation_dinf import _angle_to_neighbors
from xrspatial.hydro.flow_path_dinf import _angle_to_neighbors_py
from xrspatial.hydro._boundary_store import BoundaryStore
from xrspatial.hydro.watershed_dinf import (
_dominant_offset_py,
_preprocess_tiles,
_to_numpy_f64,
)
from xrspatial.utils import (
_validate_raster,
has_cuda_and_cupy,
is_cupy_array,
is_dask_cupy,
ngjit,
)
# =====================================================================
# CPU kernel
# =====================================================================
@ngjit
def _hand_dinf_cpu(flow_dir, flow_accum, elevation, H, W, threshold):
"""Compute HAND via Kahn's BFS with D-inf angle decomposition."""
in_degree = np.zeros((H, W), dtype=np.int32)
valid = np.zeros((H, W), dtype=np.int8)
is_stream = np.zeros((H, W), dtype=np.int8)
drain_elev = np.empty((H, W), dtype=np.float64)
hand_out = np.empty((H, W), dtype=np.float64)
for r in range(H):
for c in range(W):
v = flow_dir[r, c]
if v == v: # not NaN
valid[r, c] = 1
fa = flow_accum[r, c]
if fa == fa and fa >= threshold:
is_stream[r, c] = 1
drain_elev[r, c] = elevation[r, c]
else:
drain_elev[r, c] = np.nan
else:
drain_elev[r, c] = np.nan
hand_out[r, c] = np.nan
# In-degrees: both D-inf neighbors contribute
for r in range(H):
for c in range(W):
if valid[r, c] == 0:
continue
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, c])
if w1 > 0.0:
nr, nc = r + dy1, c + dx1
if 0 <= nr < H and 0 <= nc < W and valid[nr, nc] == 1:
in_degree[nr, nc] += 1
if w2 > 0.0:
nr, nc = r + dy2, c + dx2
if 0 <= nr < H and 0 <= nc < W and valid[nr, nc] == 1:
in_degree[nr, nc] += 1
# BFS topological order
order_r = np.empty(H * W, dtype=np.int64)
order_c = np.empty(H * W, dtype=np.int64)
head = np.int64(0)
tail = np.int64(0)
for r in range(H):
for c in range(W):
if valid[r, c] == 1 and in_degree[r, c] == 0:
order_r[tail] = r
order_c[tail] = c
tail += 1
while head < tail:
r = order_r[head]
c = order_c[head]
head += 1
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, c])
if w1 > 0.0:
nr, nc = r + dy1, c + dx1
if 0 <= nr < H and 0 <= nc < W and valid[nr, nc] == 1:
in_degree[nr, nc] -= 1
if in_degree[nr, nc] == 0:
order_r[tail] = nr
order_c[tail] = nc
tail += 1
if w2 > 0.0:
nr, nc = r + dy2, c + dx2
if 0 <= nr < H and 0 <= nc < W and valid[nr, nc] == 1:
in_degree[nr, nc] -= 1
if in_degree[nr, nc] == 0:
order_r[tail] = nr
order_c[tail] = nc
tail += 1
# Reverse pass: propagate drain_elev via dominant neighbor
for i in range(tail - 1, -1, -1):
r = order_r[i]
c = order_c[i]
if is_stream[r, c] == 1:
continue
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, c])
if w1 <= 0.0 and w2 <= 0.0:
drain_elev[r, c] = elevation[r, c]
continue
if w1 >= w2:
ddy, ddx = dy1, dx1
else:
ddy, ddx = dy2, dx2
nr, nc = r + ddy, c + ddx
if nr < 0 or nr >= H or nc < 0 or nc >= W:
drain_elev[r, c] = elevation[r, c]
continue
if valid[nr, nc] == 0:
drain_elev[r, c] = elevation[r, c]
continue
de = drain_elev[nr, nc]
if de == de:
drain_elev[r, c] = de
else:
drain_elev[r, c] = elevation[r, c]
for r in range(H):
for c in range(W):
if valid[r, c] == 1:
hand_out[r, c] = elevation[r, c] - drain_elev[r, c]
else:
hand_out[r, c] = np.nan
return hand_out
# =====================================================================
# CuPy backend
# =====================================================================
def _hand_dinf_cupy(fd_data, fa_data, elev_data, threshold):
import cupy as cp
fd_np = fd_data.get().astype(np.float64)
fa_np = fa_data.get().astype(np.float64)
el_np = elev_data.get().astype(np.float64)
H, W = fd_np.shape
out = _hand_dinf_cpu(fd_np, fa_np, el_np, H, W, threshold)
return cp.asarray(out)
# =====================================================================
# Dask tile kernel
# =====================================================================
@ngjit
def _hand_dinf_drain_elev_tile(flow_dir, flow_accum, elevation, h, w,
threshold,
exit_top, exit_bottom, exit_left, exit_right,
exit_tl, exit_tr, exit_bl, exit_br):
"""Compute drain_elev for a D-inf tile (for boundary propagation)."""
in_degree = np.zeros((h, w), dtype=np.int32)
valid = np.zeros((h, w), dtype=np.int8)
is_stream = np.zeros((h, w), dtype=np.int8)
drain_elev = np.empty((h, w), dtype=np.float64)
known = np.zeros((h, w), dtype=np.int8)
for r in range(h):
for c in range(w):
v = flow_dir[r, c]
if v == v:
valid[r, c] = 1
fa = flow_accum[r, c]
if fa == fa and fa >= threshold:
is_stream[r, c] = 1
drain_elev[r, c] = elevation[r, c]
known[r, c] = 1
else:
drain_elev[r, c] = np.nan
else:
drain_elev[r, c] = np.nan
# Apply exit labels at boundaries where dominant neighbor exits tile
for c in range(w):
if valid[0, c] == 1 and known[0, c] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[0, c])
if w1 <= 0.0 and w2 <= 0.0:
continue
if w1 >= w2:
ddy = dy1
else:
ddy = dy2
if 0 + ddy < 0:
el = exit_top[c]
if el == el:
drain_elev[0, c] = el
known[0, c] = 1
else:
drain_elev[0, c] = elevation[0, c]
known[0, c] = 1
for c in range(w):
if valid[h - 1, c] == 1 and known[h - 1, c] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[h - 1, c])
if w1 <= 0.0 and w2 <= 0.0:
continue
if w1 >= w2:
ddy = dy1
else:
ddy = dy2
if h - 1 + ddy >= h:
el = exit_bottom[c]
if el == el:
drain_elev[h - 1, c] = el
known[h - 1, c] = 1
else:
drain_elev[h - 1, c] = elevation[h - 1, c]
known[h - 1, c] = 1
for r in range(h):
if valid[r, 0] == 1 and known[r, 0] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, 0])
if w1 <= 0.0 and w2 <= 0.0:
continue
if w1 >= w2:
ddx = dx1
else:
ddx = dx2
if 0 + ddx < 0:
el = exit_left[r]
if el == el:
drain_elev[r, 0] = el
known[r, 0] = 1
else:
drain_elev[r, 0] = elevation[r, 0]
known[r, 0] = 1
for r in range(h):
if valid[r, w - 1] == 1 and known[r, w - 1] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, w - 1])
if w1 <= 0.0 and w2 <= 0.0:
continue
if w1 >= w2:
ddx = dx1
else:
ddx = dx2
if w - 1 + ddx >= w:
el = exit_right[r]
if el == el:
drain_elev[r, w - 1] = el
known[r, w - 1] = 1
else:
drain_elev[r, w - 1] = elevation[r, w - 1]
known[r, w - 1] = 1
# Corner overrides
if valid[0, 0] == 1 and known[0, 0] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[0, 0])
if not (w1 <= 0.0 and w2 <= 0.0):
if w1 >= w2:
ddy, ddx = dy1, dx1
else:
ddy, ddx = dy2, dx2
if 0 + ddy < 0 and 0 + ddx < 0:
if exit_tl == exit_tl:
drain_elev[0, 0] = exit_tl
known[0, 0] = 1
if valid[0, w - 1] == 1 and known[0, w - 1] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[0, w - 1])
if not (w1 <= 0.0 and w2 <= 0.0):
if w1 >= w2:
ddy, ddx = dy1, dx1
else:
ddy, ddx = dy2, dx2
if 0 + ddy < 0 and w - 1 + ddx >= w:
if exit_tr == exit_tr:
drain_elev[0, w - 1] = exit_tr
known[0, w - 1] = 1
if valid[h - 1, 0] == 1 and known[h - 1, 0] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[h - 1, 0])
if not (w1 <= 0.0 and w2 <= 0.0):
if w1 >= w2:
ddy, ddx = dy1, dx1
else:
ddy, ddx = dy2, dx2
if h - 1 + ddy >= h and 0 + ddx < 0:
if exit_bl == exit_bl:
drain_elev[h - 1, 0] = exit_bl
known[h - 1, 0] = 1
if valid[h - 1, w - 1] == 1 and known[h - 1, w - 1] == 0:
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[h - 1, w - 1])
if not (w1 <= 0.0 and w2 <= 0.0):
if w1 >= w2:
ddy, ddx = dy1, dx1
else:
ddy, ddx = dy2, dx2
if h - 1 + ddy >= h and w - 1 + ddx >= w:
if exit_br == exit_br:
drain_elev[h - 1, w - 1] = exit_br
known[h - 1, w - 1] = 1
# In-degrees (non-known cells only)
for r in range(h):
for c in range(w):
if valid[r, c] == 0 or known[r, c] == 1:
continue
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, c])
if w1 > 0.0:
nr, nc = r + dy1, c + dx1
if 0 <= nr < h and 0 <= nc < w:
if valid[nr, nc] == 1 and known[nr, nc] == 0:
in_degree[nr, nc] += 1
if w2 > 0.0:
nr, nc = r + dy2, c + dx2
if 0 <= nr < h and 0 <= nc < w:
if valid[nr, nc] == 1 and known[nr, nc] == 0:
in_degree[nr, nc] += 1
# BFS
order_r = np.empty(h * w, dtype=np.int64)
order_c = np.empty(h * w, dtype=np.int64)
head = np.int64(0)
tail = np.int64(0)
for r in range(h):
for c in range(w):
if valid[r, c] == 1 and known[r, c] == 0 and in_degree[r, c] == 0:
order_r[tail] = r
order_c[tail] = c
tail += 1
while head < tail:
r = order_r[head]
c = order_c[head]
head += 1
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, c])
if w1 > 0.0:
nr, nc = r + dy1, c + dx1
if 0 <= nr < h and 0 <= nc < w and valid[nr, nc] == 1 and known[nr, nc] == 0:
in_degree[nr, nc] -= 1
if in_degree[nr, nc] == 0:
order_r[tail] = nr
order_c[tail] = nc
tail += 1
if w2 > 0.0:
nr, nc = r + dy2, c + dx2
if 0 <= nr < h and 0 <= nc < w and valid[nr, nc] == 1 and known[nr, nc] == 0:
in_degree[nr, nc] -= 1
if in_degree[nr, nc] == 0:
order_r[tail] = nr
order_c[tail] = nc
tail += 1
# Reverse pass
for i in range(tail - 1, -1, -1):
r = order_r[i]
c = order_c[i]
dy1, dx1, w1, dy2, dx2, w2 = _angle_to_neighbors(flow_dir[r, c])
if w1 <= 0.0 and w2 <= 0.0:
drain_elev[r, c] = elevation[r, c]
continue
if w1 >= w2:
ddy, ddx = dy1, dx1
else:
ddy, ddx = dy2, dx2
nr, nc = r + ddy, c + ddx
if nr < 0 or nr >= h or nc < 0 or nc >= w:
drain_elev[r, c] = elevation[r, c]
continue
if valid[nr, nc] == 0:
drain_elev[r, c] = elevation[r, c]
continue
de = drain_elev[nr, nc]
if de == de:
drain_elev[r, c] = de
else:
drain_elev[r, c] = elevation[r, c]
return drain_elev
@ngjit
def _hand_dinf_tile_kernel(flow_dir, flow_accum, elevation, h, w, threshold,
exit_top, exit_bottom, exit_left, exit_right,
exit_tl, exit_tr, exit_bl, exit_br):
"""HAND tile kernel: returns HAND values (elevation - drain_elev)."""
drain_elev = _hand_dinf_drain_elev_tile(
flow_dir, flow_accum, elevation, h, w, threshold,
exit_top, exit_bottom, exit_left, exit_right,
exit_tl, exit_tr, exit_bl, exit_br)
out = np.empty((h, w), dtype=np.float64)
for r in range(h):
for c in range(w):
v = flow_dir[r, c]
if v == v:
out[r, c] = elevation[r, c] - drain_elev[r, c]
else:
out[r, c] = np.nan
return out
# =====================================================================
# Dask iterative tile sweep
# =====================================================================
def _compute_exit_labels(iy, ix, boundaries, flow_bdry,
chunks_y, chunks_x, n_tile_y, n_tile_x):
"""Same exit-label pattern as watershed_dinf but propagating drain_elev."""
from xrspatial.hydro.watershed_dinf import _compute_exit_labels as _ws_compute
return _ws_compute(iy, ix, boundaries, flow_bdry,
chunks_y, chunks_x, n_tile_y, n_tile_x)
def _process_tile_hand(iy, ix, flow_dir_da, flow_accum_da, elev_da,
boundaries, flow_bdry, threshold,
chunks_y, chunks_x, n_tile_y, n_tile_x):
fd_chunk = np.asarray(
flow_dir_da.blocks[iy, ix].compute(), dtype=np.float64)
fa_chunk = np.asarray(
flow_accum_da.blocks[iy, ix].compute(), dtype=np.float64)
el_chunk = np.asarray(
elev_da.blocks[iy, ix].compute(), dtype=np.float64)
h, w = fd_chunk.shape
exits = _compute_exit_labels(
iy, ix, boundaries, flow_bdry,
chunks_y, chunks_x, n_tile_y, n_tile_x)
drain_elev = _hand_dinf_drain_elev_tile(
fd_chunk, fa_chunk, el_chunk, h, w, threshold, *exits)
new_top = drain_elev[0, :].copy()
new_bottom = drain_elev[-1, :].copy()
new_left = drain_elev[:, 0].copy()
new_right = drain_elev[:, -1].copy()
changed = False
for side, new in (('top', new_top), ('bottom', new_bottom),
('left', new_left), ('right', new_right)):
old = boundaries.get(side, iy, ix).copy()
with np.errstate(invalid='ignore'):
mask = ~(np.isnan(old) & np.isnan(new))
if mask.any():
diff = old[mask] != new[mask]
if np.any(diff):
changed = True
break
boundaries.set('top', iy, ix, new_top)
boundaries.set('bottom', iy, ix, new_bottom)
boundaries.set('left', iy, ix, new_left)
boundaries.set('right', iy, ix, new_right)
return changed
def _hand_dinf_dask(flow_dir_da, flow_accum_da, elev_da, threshold):
chunks_y = flow_dir_da.chunks[0]
chunks_x = flow_dir_da.chunks[1]
n_tile_y = len(chunks_y)
n_tile_x = len(chunks_x)
flow_bdry = _preprocess_tiles(flow_dir_da, chunks_y, chunks_x)
flow_bdry = flow_bdry.snapshot()
boundaries = BoundaryStore(chunks_y, chunks_x, fill_value=np.nan)
max_iterations = max(n_tile_y, n_tile_x) * 2 + 10
for _iteration in range(max_iterations):
any_changed = False
for iy in range(n_tile_y):
for ix in range(n_tile_x):
c = _process_tile_hand(
iy, ix, flow_dir_da, flow_accum_da, elev_da,
boundaries, flow_bdry, threshold,
chunks_y, chunks_x, n_tile_y, n_tile_x)
if c:
any_changed = True
for iy in reversed(range(n_tile_y)):
for ix in reversed(range(n_tile_x)):
c = _process_tile_hand(
iy, ix, flow_dir_da, flow_accum_da, elev_da,
boundaries, flow_bdry, threshold,
chunks_y, chunks_x, n_tile_y, n_tile_x)
if c:
any_changed = True
if not any_changed:
break
boundaries = boundaries.snapshot()
def _tile_fn(fd_block, fa_block, el_block, block_info=None):
if block_info is None or 0 not in block_info:
return np.full(fd_block.shape, np.nan, dtype=np.float64)
iy, ix = block_info[0]['chunk-location']
h, w = fd_block.shape
exits = _compute_exit_labels(
iy, ix, boundaries, flow_bdry,
chunks_y, chunks_x, n_tile_y, n_tile_x)
return _hand_dinf_tile_kernel(
np.asarray(fd_block, dtype=np.float64),
np.asarray(fa_block, dtype=np.float64),
np.asarray(el_block, dtype=np.float64),
h, w, threshold, *exits)
return da.map_blocks(
_tile_fn,
flow_dir_da, flow_accum_da, elev_da,
dtype=np.float64,
meta=np.array((), dtype=np.float64),
)
def _hand_dinf_dask_cupy(flow_dir_da, flow_accum_da, elev_da, threshold):
import cupy as cp
fd_np = flow_dir_da.map_blocks(
lambda b: b.get(), dtype=flow_dir_da.dtype,
meta=np.array((), dtype=flow_dir_da.dtype))
fa_np = flow_accum_da.map_blocks(
lambda b: b.get(), dtype=flow_accum_da.dtype,
meta=np.array((), dtype=flow_accum_da.dtype))
el_np = elev_da.map_blocks(
lambda b: b.get(), dtype=elev_da.dtype,
meta=np.array((), dtype=elev_da.dtype))
result = _hand_dinf_dask(fd_np, fa_np, el_np, threshold)
return result.map_blocks(
cp.asarray, dtype=result.dtype,
meta=cp.array((), dtype=result.dtype))
# =====================================================================
# Public API
# =====================================================================
def hand_dinf(flow_dir_dinf: xr.DataArray,
flow_accum: xr.DataArray,
elevation: xr.DataArray,
threshold: float = 100,
name: str = 'hand_dinf') -> xr.DataArray:
"""Compute HAND using D-infinity flow direction.
Parameters
----------
flow_dir_dinf : xarray.DataArray
2D D-infinity flow direction grid.
flow_accum : xarray.DataArray
2D flow accumulation grid.
elevation : xarray.DataArray
2D elevation grid.
threshold : float, default 100
Minimum flow accumulation to define a stream cell.
name : str, default 'hand_dinf'
Name of output DataArray.
Returns
-------
xarray.DataArray
2D float64 HAND grid. Stream cells have HAND = 0.
"""
_validate_raster(flow_dir_dinf, func_name='hand_dinf', name='flow_dir_dinf')
_validate_raster(flow_accum, func_name='hand_dinf', name='flow_accum')
_validate_raster(elevation, func_name='hand_dinf', name='elevation')
fd_data = flow_dir_dinf.data
fa_data = flow_accum.data
el_data = elevation.data
if isinstance(fd_data, np.ndarray):
fd = fd_data.astype(np.float64)
fa = np.asarray(fa_data, dtype=np.float64)
el = np.asarray(el_data, dtype=np.float64)
H, W = fd.shape
out = _hand_dinf_cpu(fd, fa, el, H, W, float(threshold))
elif has_cuda_and_cupy() and is_cupy_array(fd_data):
out = _hand_dinf_cupy(fd_data, fa_data, el_data, float(threshold))
elif has_cuda_and_cupy() and is_dask_cupy(flow_dir_dinf):
out = _hand_dinf_dask_cupy(fd_data, fa_data, el_data, float(threshold))
elif da is not None and isinstance(fd_data, da.Array):
out = _hand_dinf_dask(fd_data, fa_data, el_data, float(threshold))
else:
raise TypeError(f"Unsupported array type: {type(fd_data)}")
return xr.DataArray(out,
name=name,
coords=flow_dir_dinf.coords,
dims=flow_dir_dinf.dims,
attrs=flow_dir_dinf.attrs)