Skip to content

Commit ef5ca6a

Browse files
authored
refactor(FlopyBinaryData): make properties readonly, deprecate set_float() (#2421)
Minor cleanup. Use properties instead of attributes. Deprecate set_float() and replace usages with precision setter.
1 parent 1f9c0e3 commit ef5ca6a

File tree

4 files changed

+87
-41
lines changed

4 files changed

+87
-41
lines changed

flopy/mf6/utils/binarygrid_util.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, filename, precision="double", verbose=False):
5959
super().__init__()
6060

6161
# set attributes
62-
self.set_float(precision=precision)
62+
self.precision = precision
6363
self.verbose = verbose
6464
self._initial_len = 50
6565
self._recorddict = {}
@@ -83,7 +83,7 @@ def __init__(self, filename, precision="double", verbose=False):
8383
t = line.split()
8484
self._version = t[1]
8585

86-
# version
86+
# ntxt
8787
line = self.read_text(self._initial_len).strip()
8888
t = line.split()
8989
self._ntxt = int(t[1])
@@ -154,20 +154,20 @@ def __init__(self, filename, precision="double", verbose=False):
154154
self.file.close()
155155

156156
# initialize the model grid to None
157-
self.__modelgrid = None
157+
self._modelgrid = None
158158

159159
# set ia and ja
160-
self.__set_iaja()
160+
self._set_iaja()
161161

162162
# internal functions
163-
def __set_iaja(self):
163+
def _set_iaja(self):
164164
"""
165165
Set ia and ja from _datadict.
166166
"""
167167
self._ia = self._datadict["IA"] - 1
168168
self._ja = self._datadict["JA"] - 1
169169

170-
def __set_modelgrid(self):
170+
def _set_modelgrid(self):
171171
"""
172172
Define structured, vertex, or unstructured grid based on MODFLOW 6
173173
discretization type.
@@ -246,11 +246,11 @@ def __set_modelgrid(self):
246246
except:
247247
print(f"could not set model grid for {self.file.name}")
248248

249-
self.__modelgrid = modelgrid
249+
self._modelgrid = modelgrid
250250

251251
return
252252

253-
def __build_vertices_cell2d(self):
253+
def _build_vertices_cell2d(self):
254254
"""
255255
Build the mf6 vertices and cell2d array to generate a VertexGrid
256256
@@ -268,7 +268,7 @@ def __build_vertices_cell2d(self):
268268
]
269269
return vertices, cell2d
270270

271-
def __get_iverts(self):
271+
def _get_iverts(self):
272272
"""
273273
Get a list of the vertices that define each model cell.
274274
@@ -292,7 +292,7 @@ def __get_iverts(self):
292292
print(f"returning iverts from {self.file.name}")
293293
return iverts
294294

295-
def __get_verts(self):
295+
def _get_verts(self):
296296
"""
297297
Get a list of the x, y pair for each vertex from the data in the
298298
binary grid file.
@@ -317,7 +317,7 @@ def __get_verts(self):
317317
print(f"returning verts from {self.file.name}")
318318
return verts
319319

320-
def __get_cellcenters(self):
320+
def _get_cellcenters(self):
321321
"""
322322
Get the cell centers centroids for a MODFLOW 6 GWF model that uses
323323
the DISV or DISU discretization.
@@ -689,7 +689,7 @@ def iverts(self):
689689
-------
690690
iverts : list of lists of ints
691691
"""
692-
return self.__get_iverts()
692+
return self._get_iverts()
693693

694694
@property
695695
def verts(self):
@@ -700,7 +700,7 @@ def verts(self):
700700
-------
701701
verts : ndarray of floats
702702
"""
703-
return self.__get_verts()
703+
return self._get_verts()
704704

705705
@property
706706
def cellcenters(self):
@@ -711,7 +711,7 @@ def cellcenters(self):
711711
-------
712712
cellcenters : ndarray of floats
713713
"""
714-
return self.__get_cellcenters()
714+
return self._get_cellcenters()
715715

716716
@property
717717
def modelgrid(self):
@@ -722,9 +722,9 @@ def modelgrid(self):
722722
-------
723723
modelgrid : StructuredGrid, VertexGrid, UnstructuredGrid
724724
"""
725-
if self.__modelgrid is None:
726-
self.__set_modelgrid()
727-
return self.__modelgrid
725+
if self._modelgrid is None:
726+
self._set_modelgrid()
727+
return self._modelgrid
728728

729729
@property
730730
def cell2d(self):
@@ -736,7 +736,7 @@ def cell2d(self):
736736
cell2d : list of lists
737737
"""
738738
if self._grid_type in ("DISV", "DISV2D", "DISV1D"):
739-
vertices, cell2d = self.__build_vertices_cell2d()
739+
vertices, cell2d = self._build_vertices_cell2d()
740740
else:
741741
vertices, cell2d = None, None
742742
return vertices, cell2d

flopy/utils/observationfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def __init__(self, filename, verbose=False, isBinary="auto"):
302302
precision = "single"
303303
if "double" in cline[5:11].lower():
304304
precision = "double"
305-
self.set_float(precision)
305+
self.precision = precision
306306
lenobsname = int(cline[11:])
307307

308308
# get number of observations
@@ -370,7 +370,7 @@ def __init__(self, filename, verbose=False, hydlbl_len=20):
370370
if self.nobs < 0:
371371
self.nobs = abs(self.nobs)
372372
precision = "double"
373-
self.set_float(precision)
373+
self.precision = precision
374374

375375
# continue reading the file
376376
self.itmuni = self.read_integer()
@@ -438,7 +438,7 @@ def __init__(self, filename, precision="double", verbose=False):
438438
439439
"""
440440
super().__init__()
441-
self.set_float(precision=precision)
441+
self.precision = precision
442442
# initialize class information
443443
self.verbose = verbose
444444
# open binary head file

flopy/utils/swroutputfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, filename, swrtype="stage", precision="double", verbose=False)
4747
4848
"""
4949
super().__init__()
50-
self.set_float(precision=precision)
50+
self.precision = precision
5151
self.header_dtype = np.dtype(
5252
[
5353
("totim", self.floattype),

flopy/utils/utils_def.py

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,85 @@
33
"""
44

55
from datetime import timedelta
6+
from typing import Literal, get_args
7+
from warnings import warn
68

79
import numpy as np
810

11+
Precision = Literal["single", "double"]
12+
913

1014
class FlopyBinaryData:
1115
"""
12-
The FlopyBinaryData class is a class to that defines the data types for
13-
integer, floating point, and character data in MODFLOW binary
14-
files. The FlopyBinaryData class is the super class from which the
15-
specific derived classes are formed. This class should not be
16-
instantiated directly.
17-
16+
Defines integer, floating point, and character data types for
17+
MODFLOW binary files.
1818
"""
1919

2020
def __init__(self):
21-
self.integer = np.int32
22-
self.integerbyte = self.integer(1).nbytes
21+
self.precision = "double"
22+
23+
@property
24+
def integer(self) -> type:
25+
return np.int32
26+
27+
@property
28+
def integerbyte(self) -> int:
29+
return self.integer(1).nbytes
30+
31+
@property
32+
def character(self) -> type:
33+
return np.uint8
34+
35+
@property
36+
def textbyte(self) -> int:
37+
return 1
38+
39+
@property
40+
def precision(self) -> Precision:
41+
return self._precision
42+
43+
@precision.setter
44+
def precision(self, value: Precision):
45+
if value not in get_args(Precision):
46+
raise ValueError(
47+
f"Invalid floating precision '{value}', expected 'single' or 'double'"
48+
)
49+
50+
value = value.lower()
51+
self._precision = value
52+
if value == "double":
53+
self._real = np.float64
54+
self._floattype = "f8"
55+
else:
56+
self._real = np.float32
57+
self._floattype = "f4"
58+
self._realbyte = self.real(1).nbytes
2359

24-
self.character = np.uint8
25-
self.textbyte = 1
60+
@property
61+
def real(self) -> type:
62+
return self._real
2663

27-
return
64+
@property
65+
def realbyte(self) -> int:
66+
return self._realbyte
67+
68+
@property
69+
def floattype(self) -> str:
70+
return self._floattype
2871

2972
def set_float(self, precision):
73+
"""
74+
Set floating point precision.
75+
76+
.. deprecated:: 3.5
77+
This method will be removed in Flopy 3.10+.
78+
Use ``precision`` property setter instead.
79+
"""
80+
warn(
81+
"set_float() is deprecated, use precision property setter instead",
82+
PendingDeprecationWarning,
83+
)
3084
self.precision = precision
31-
if precision.lower() == "double":
32-
self.real = np.float64
33-
self.floattype = "f8"
34-
else:
35-
self.real = np.float32
36-
self.floattype = "f4"
37-
self.realbyte = self.real(1).nbytes
38-
return
3985

4086
def read_text(self, nchar=20):
4187
bytesvalue = self._read_values(self.character, nchar).tobytes()

0 commit comments

Comments
 (0)