Skip to content

Commit 6251096

Browse files
authored
cleaner coordinate aliasing (#116)
Take advantage of wpbonelli/xattree@54f07b0. This is a cleaner way to get lay/row/col/node indexing than a custom index, which is still pretty cutting-edge. We will probably still need a custom index to handle CRS etc but maybe best to wait for xarray to harden the api before developing one.
1 parent de9cd5d commit 6251096

File tree

7 files changed

+90
-103
lines changed

7 files changed

+90
-103
lines changed

docs/examples/quickstart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# check CHD
3030
assert chd.data["head"][0, 0].item() == 1.0
3131
assert chd.data.head.sel(per=0)[99].item() == 0.0
32+
assert chd.data.head.sel(per=0, node=99).item() == 0.0
3233
assert np.allclose(chd.data.head[:, 1:99], np.full(98, 1e30))
3334

3435
# check DIS

flopy4/mf6/gwf/dis.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
from xattree import array, dim, field, xattree
55

66
from flopy4.mf6.converters import convert_array
7-
from flopy4.mf6.indexes import grid_index
87
from flopy4.mf6.package import Package
98

109

11-
@xattree(index=grid_index, index_scope="gwf")
10+
@xattree
1211
class Dis(Package):
1312
length_units: str = field(
1413
default=None,
@@ -22,26 +21,23 @@ class Dis(Package):
2221
default=False, metadata={"block": "options"}
2322
)
2423
nlay: int = dim(
25-
# disable the otherwise automatic coordinate variable
26-
# because we're going to create another one for this
27-
# dimension with a different name via a custom index
28-
coord=False,
24+
coord="lay",
2925
scope="gwf",
3026
default=1,
3127
metadata={
3228
"block": "dimensions",
3329
},
3430
)
3531
ncol: int = dim(
36-
coord=False,
32+
coord="col",
3733
scope="gwf",
3834
default=2,
3935
metadata={
4036
"block": "dimensions",
4137
},
4238
)
4339
nrow: int = dim(
44-
coord=False,
40+
coord="row",
4541
scope="gwf",
4642
default=2,
4743
metadata={
@@ -79,7 +75,7 @@ class Dis(Package):
7975
converter=Converter(convert_array, takes_self=True, takes_field=True),
8076
)
8177
nnodes: int = dim(
82-
# coord=False,
78+
coord="node",
8379
scope="gwf",
8480
init=False,
8581
)

flopy4/mf6/indexes.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ def sel(self, labels):
4444
results.append(index.sel({k: labels[k]}))
4545
return merge_sel_results(results)
4646

47-
def to_pandas_index(self) -> pd.Index:
48-
# from https://github.com/corteva/rioxarray/pull/846/files#diff-917105823f61e63ef4afde8bed408a6c249e375690e56bc800406676f02551d8R418
49-
if len(self._indices) == 1:
50-
index = next(iter(self._indices.values()))
51-
if isinstance(index, PandasIndex):
52-
return index.to_pandas_index()
53-
54-
raise ValueError("Cannot convert MetaIndex to pandas.Index")
55-
5647

5748
def grid_index(dataset: xr.Dataset) -> MetaIndex:
5849
return MetaIndex(

flopy4/mf6/tdis.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
from xattree import ROOT, array, dim, field, xattree
88

99
from flopy4.mf6.converters import convert_array
10-
from flopy4.mf6.indexes import time_index
1110
from flopy4.mf6.package import Package
1211

1312

14-
@xattree(index=time_index, index_scope=ROOT)
13+
@xattree
1514
class Tdis(Package):
1615
@define
1716
class PeriodData:
@@ -20,7 +19,7 @@ class PeriodData:
2019
tsmult: float
2120

2221
nper: int = dim(
23-
coord=False,
22+
coord="per",
2423
default=1,
2524
scope=ROOT,
2625
metadata={"block": "dimensions"},

flopy4/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
def to_path(value: Any) -> Optional[Path]:
66
"""
7-
Convert a value to a Path if it has a value, otherwise return None.
7+
Try to convert a value to a Path if it's not None, otherwise return None.
88
"""
99
return Path(value) if value else None

pixi.lock

Lines changed: 74 additions & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

uv.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)