Skip to content

Commit 18791bb

Browse files
authored
wrap field decorators, increase ruff line length (#131)
wrap the xattree field decorators to provide a block parameter
1 parent 79c55cf commit 18791bb

File tree

17 files changed

+259
-288
lines changed

17 files changed

+259
-288
lines changed

flopy4/mf6/decorators.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from attrs import NOTHING
2+
from xattree import array as xattree_array
3+
from xattree import coord as xattree_coord
4+
from xattree import dim as xattree_dim
5+
from xattree import field as xattree_field
6+
7+
8+
def field(
9+
default=NOTHING,
10+
validator=None,
11+
converter=None,
12+
repr=True,
13+
eq=True,
14+
init=True,
15+
metadata=None,
16+
block: str | None = None,
17+
):
18+
"""Create a field."""
19+
if block:
20+
metadata = metadata or {}
21+
metadata["block"] = block
22+
return xattree_field(
23+
default=default,
24+
validator=validator,
25+
converter=converter,
26+
repr=repr,
27+
eq=eq,
28+
init=init,
29+
metadata=metadata,
30+
)
31+
32+
33+
def dim(
34+
scope=None,
35+
coord: bool | str = True,
36+
default=NOTHING,
37+
repr=True,
38+
eq=True,
39+
init=True,
40+
metadata=None,
41+
block: str | None = None,
42+
):
43+
"""Create a dimension field."""
44+
if block:
45+
metadata = metadata or {}
46+
metadata["block"] = block
47+
return xattree_dim(
48+
scope=scope,
49+
coord=coord,
50+
default=default,
51+
repr=repr,
52+
eq=eq,
53+
init=init,
54+
metadata=metadata,
55+
)
56+
57+
58+
def coord(
59+
scope=None,
60+
default=NOTHING,
61+
repr=True,
62+
eq=True,
63+
metadata=None,
64+
block: str | None = None,
65+
):
66+
"""Create a coordinate field."""
67+
if block:
68+
metadata = metadata or {}
69+
metadata["block"] = block
70+
return xattree_coord(
71+
scope=scope,
72+
default=default,
73+
repr=repr,
74+
eq=eq,
75+
metadata=metadata,
76+
)
77+
78+
79+
def array(
80+
cls=None,
81+
dims=None,
82+
default=NOTHING,
83+
validator=None,
84+
converter=None,
85+
repr=True,
86+
eq=None,
87+
metadata=None,
88+
block: str | None = None,
89+
):
90+
"""Create an array field."""
91+
if block:
92+
metadata = metadata or {}
93+
metadata["block"] = block
94+
return xattree_array(
95+
cls=cls,
96+
dims=dims,
97+
default=default,
98+
validator=validator,
99+
converter=converter,
100+
repr=repr,
101+
eq=eq,
102+
metadata=metadata,
103+
)

flopy4/mf6/gwf/__init__.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import xarray as xr
77
from attrs import define
88
from flopy.discretization.grid import Grid
9-
from xattree import field, xattree
9+
from xattree import xattree
1010

11+
from flopy4.mf6.decorators import field
1112
from flopy4.mf6.gwf.chd import Chd
1213
from flopy4.mf6.gwf.dis import Dis
1314
from flopy4.mf6.gwf.ic import Ic
@@ -64,22 +65,14 @@ class NewtonOptions:
6465
newton: bool = field()
6566
under_relaxation: bool = field()
6667

67-
list: Optional[str] = field(default=None, metadata={"block": "options"})
68-
print_input: bool = field(default=False, metadata={"block": "options"})
69-
print_flows: bool = field(default=False, metadata={"block": "options"})
70-
save_flows: bool = field(default=False, metadata={"block": "options"})
71-
newtonoptions: Optional[NewtonOptions] = field(
72-
default=None, metadata={"block": "options"}
73-
)
74-
nc_mesh2d_filerecord: Optional[Path] = field(
75-
default=None, metadata={"block": "options"}
76-
)
77-
nc_structured_filerecord: Optional[Path] = field(
78-
default=None, metadata={"block": "options"}
79-
)
80-
nc_filerecord: Optional[Path] = field(
81-
default=None, metadata={"block": "options"}
82-
)
68+
list: Optional[str] = field(block="options", default=None)
69+
print_input: bool = field(block="options", default=False)
70+
print_flows: bool = field(block="options", default=False)
71+
save_flows: bool = field(block="options", default=False)
72+
newtonoptions: Optional[NewtonOptions] = field(block="options", default=None)
73+
nc_mesh2d_filerecord: Optional[Path] = field(block="options", default=None)
74+
nc_structured_filerecord: Optional[Path] = field(block="options", default=None)
75+
nc_filerecord: Optional[Path] = field(block="options", default=None)
8376

8477
@property
8578
def grid(self) -> Grid:

flopy4/mf6/gwf/chd.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import numpy as np
55
from attrs import Converter, define
66
from numpy.typing import NDArray
7-
from xattree import array, field, xattree
7+
from xattree import xattree
88

99
from flopy4.mf6.converters import convert_array
10+
from flopy4.mf6.decorators import array, field
1011
from flopy4.mf6.package import Package
1112

1213

@@ -20,57 +21,47 @@ class Steps:
2021
steps: list[int] = field()
2122
frequency: int = field()
2223

23-
auxiliary: Optional[list[str]] = array(
24-
default=None, metadata={"block": "options"}
25-
)
26-
auxmultname: Optional[str] = field(
27-
default=None, metadata={"block": "options"}
28-
)
29-
boundnames: bool = field(default=False, metadata={"block": "options"})
30-
print_input: bool = field(default=False, metadata={"block": "options"})
31-
print_flows: bool = field(default=False, metadata={"block": "options"})
32-
save_flows: bool = field(default=False, metadata={"block": "options"})
33-
ts_filerecord: Optional[Path] = field(
34-
default=None, metadata={"block": "options"}
35-
)
36-
obs_filerecord: Optional[Path] = field(
37-
default=None, metadata={"block": "options"}
38-
)
24+
auxiliary: Optional[list[str]] = array(block="options", default=None)
25+
auxmultname: Optional[str] = field(block="options", default=None)
26+
boundnames: bool = field(block="options", default=False)
27+
print_input: bool = field(block="options", default=False)
28+
print_flows: bool = field(block="options", default=False)
29+
save_flows: bool = field(block="options", default=False)
30+
ts_filerecord: Optional[Path] = field(block="options", default=None)
31+
obs_filerecord: Optional[Path] = field(block="options", default=None)
3932
dev_no_newton: bool = field(default=False, metadata={"block": "options"})
40-
maxbound: Optional[int] = field(
41-
default=None, metadata={"block": "dimensions"}
42-
)
33+
maxbound: Optional[int] = field(block="dimensions", default=None)
4334
head: Optional[NDArray[np.floating]] = array(
35+
block="period",
4436
dims=(
4537
"nper",
4638
"nnodes",
4739
),
4840
default=None,
49-
metadata={"block": "period"},
5041
converter=Converter(convert_array, takes_self=True, takes_field=True),
5142
)
5243
aux: Optional[NDArray[np.floating]] = array(
44+
block="period",
5345
dims=(
5446
"nper",
5547
"nnodes",
5648
),
5749
default=None,
58-
metadata={"block": "period"},
5950
converter=Converter(convert_array, takes_self=True, takes_field=True),
6051
)
6152
boundname: Optional[NDArray[np.str_]] = array(
53+
block="period",
6254
dims=(
6355
"nper",
6456
"nnodes",
6557
),
6658
default=None,
67-
metadata={"block": "period"},
6859
converter=Converter(convert_array, takes_self=True, takes_field=True),
6960
)
7061
steps: Optional[NDArray[np.object_]] = array(
7162
Steps,
63+
block="period",
7264
dims=("nper", "nnodes"),
7365
default=None,
74-
metadata={"block": "period"},
7566
converter=Converter(convert_array, takes_self=True, takes_field=True),
7667
)

flopy4/mf6/gwf/dis.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from attrs import Converter
33
from flopy.discretization.structuredgrid import StructuredGrid
44
from numpy.typing import NDArray
5-
from xattree import array, dim, field, xattree
5+
from xattree import xattree
66

77
from flopy4.mf6.converters import convert_array
8+
from flopy4.mf6.decorators import array, dim, field
89
from flopy4.mf6.package import Package
910

1011

@@ -14,65 +15,57 @@ class Dis(Package):
1415
default=None,
1516
metadata={"block": "options"},
1617
)
17-
nogrb: bool = field(default=False, metadata={"block": "options"})
18-
xorigin: float = field(default=None, metadata={"block": "options"})
19-
yorigin: float = field(default=None, metadata={"block": "options"})
20-
angrot: float = field(default=None, metadata={"block": "options"})
21-
export_array_netcdf: bool = field(
22-
default=False, metadata={"block": "options"}
23-
)
18+
nogrb: bool = field(block="options", default=False)
19+
xorigin: float = field(block="options", default=None)
20+
yorigin: float = field(block="options", default=None)
21+
angrot: float = field(block="options", default=None)
22+
export_array_netcdf: bool = field(block="options", default=False)
2423
nlay: int = dim(
24+
block="dimensions",
2525
coord="lay",
2626
scope="gwf",
2727
default=1,
28-
metadata={
29-
"block": "dimensions",
30-
},
3128
)
3229
ncol: int = dim(
30+
block="dimensions",
3331
coord="col",
3432
scope="gwf",
3533
default=2,
36-
metadata={
37-
"block": "dimensions",
38-
},
3934
)
4035
nrow: int = dim(
36+
block="dimensions",
4137
coord="row",
4238
scope="gwf",
4339
default=2,
44-
metadata={
45-
"block": "dimensions",
46-
},
4740
)
4841
delr: NDArray[np.floating] = array(
42+
block="griddata",
4943
default=1.0,
5044
dims=("ncol",),
51-
metadata={"block": "griddata"},
5245
converter=Converter(convert_array, takes_self=True, takes_field=True),
5346
)
5447
delc: NDArray[np.floating] = array(
48+
block="griddata",
5549
default=1.0,
5650
dims=("nrow",),
57-
metadata={"block": "griddata"},
5851
converter=Converter(convert_array, takes_self=True, takes_field=True),
5952
)
6053
top: NDArray[np.floating] = array(
54+
block="griddata",
6155
default=1.0,
6256
dims=("ncol", "nrow"),
63-
metadata={"block": "griddata"},
6457
converter=Converter(convert_array, takes_self=True, takes_field=True),
6558
)
6659
botm: NDArray[np.floating] = array(
60+
block="griddata",
6761
default=0.0,
6862
dims=("ncol", "nrow", "nlay"),
69-
metadata={"block": "griddata"},
7063
converter=Converter(convert_array, takes_self=True, takes_field=True),
7164
)
7265
idomain: NDArray[np.integer] = array(
66+
block="griddata",
7367
default=1,
7468
dims=("ncol", "nrow", "nlay"),
75-
metadata={"block": "griddata"},
7669
converter=Converter(convert_array, takes_self=True, takes_field=True),
7770
)
7871
nnodes: int = dim(

flopy4/mf6/gwf/ic.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import numpy as np
22
from attrs import Converter
33
from numpy.typing import NDArray
4-
from xattree import array, field, xattree
4+
from xattree import xattree
55

66
from flopy4.mf6.converters import convert_array
7+
from flopy4.mf6.decorators import array, field
78
from flopy4.mf6.package import Package
89

910

1011
@xattree
1112
class Ic(Package):
1213
strt: NDArray[np.floating] = array(
14+
block="packagedata",
1315
dims=("nnodes",),
1416
default=1.0,
15-
metadata={"block": "packagedata"},
1617
converter=Converter(convert_array, takes_self=True, takes_field=True),
1718
)
18-
export_array_ascii: bool = field(
19-
default=False, metadata={"block": "options"}
20-
)
21-
export_array_netcdf: bool = field(
22-
default=False,
23-
metadata={"block": "options"},
24-
)
19+
export_array_ascii: bool = field(block="options", default=False)
20+
export_array_netcdf: bool = field(block="options", default=False)

0 commit comments

Comments
 (0)