Skip to content

Commit

Permalink
chore: reformat codebase with longer line length (#2362)
Browse files Browse the repository at this point in the history
The codebase currently has a line length limit of 79 (via #954) as suggested in PEP 8 (which is related to a 80 char limit of decades old IBM punch cards and terminals). However, longer line lengths are generally encouraged for modern Python development, which is a language that commonly has four-space indents needed to define logic structures, thus requiring more horizontal space than other programming languages.

Black has a default of 88, which is the same default for Ruff. Is this the ideal length for the flopy codebase? Not sure, but a quick scan of the suggested changes look much better.

Despite the size of this PR, it was quickly automated using these ruff commands (including quick fixes to ISC001):

ruff format
ruff check --select ISC001 --fix
ruff format

There are no functional changes in this PR, thus the code "runs the same". As such, .git-blame-ignore-revs can be amended to ignore these changes with (e.g.) GitHub's blame view (this would be a follow-up PR).
  • Loading branch information
mwtoews authored Nov 13, 2024
1 parent dc394cd commit 12a3bcd
Show file tree
Hide file tree
Showing 235 changed files with 1,828 additions and 5,201 deletions.
8 changes: 2 additions & 6 deletions .docs/Notebooks/dis_triangle_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@
sim = flopy.mf6.MFSimulation(
sim_name=name, version="mf6", exe_name="mf6", sim_ws=workspace
)
tdis = flopy.mf6.ModflowTdis(
sim, time_units="DAYS", perioddata=[[1.0, 1, 1.0]]
)
tdis = flopy.mf6.ModflowTdis(sim, time_units="DAYS", perioddata=[[1.0, 1, 1.0]])
gwf = flopy.mf6.ModflowGwf(sim, modelname=name, save_flows=True)
ims = flopy.mf6.ModflowIms(
sim,
Expand All @@ -227,9 +225,7 @@
vertices=vertices,
cell2d=cell2d,
)
npf = flopy.mf6.ModflowGwfnpf(
gwf, xt3doptions=[(True)], save_specific_discharge=None
)
npf = flopy.mf6.ModflowGwfnpf(gwf, xt3doptions=[(True)], save_specific_discharge=None)
ic = flopy.mf6.ModflowGwfic(gwf)


Expand Down
12 changes: 3 additions & 9 deletions .docs/Notebooks/dis_voronoi_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@
sim = flopy.mf6.MFSimulation(
sim_name=name, version="mf6", exe_name="mf6", sim_ws=sim_ws
)
tdis = flopy.mf6.ModflowTdis(
sim, time_units="DAYS", perioddata=[[1.0, 1, 1.0]]
)
tdis = flopy.mf6.ModflowTdis(sim, time_units="DAYS", perioddata=[[1.0, 1, 1.0]])
gwf = flopy.mf6.ModflowGwf(sim, modelname=name, save_flows=True)
ims = flopy.mf6.ModflowIms(
sim,
Expand All @@ -145,9 +143,7 @@
nlay = 1
top = 1.0
botm = [0.0]
disv = flopy.mf6.ModflowGwfdisv(
gwf, nlay=nlay, **disv_gridprops, top=top, botm=botm
)
disv = flopy.mf6.ModflowGwfdisv(gwf, nlay=nlay, **disv_gridprops, top=top, botm=botm)
npf = flopy.mf6.ModflowGwfnpf(
gwf,
xt3doptions=[(True)],
Expand Down Expand Up @@ -209,9 +205,7 @@
nlay = 1
top = 1.0
botm = [0.0]
disv = flopy.mf6.ModflowGwtdisv(
gwt, nlay=nlay, **disv_gridprops, top=top, botm=botm
)
disv = flopy.mf6.ModflowGwtdisv(gwt, nlay=nlay, **disv_gridprops, top=top, botm=botm)
ic = flopy.mf6.ModflowGwtic(gwt, strt=0.0)
sto = flopy.mf6.ModflowGwtmst(gwt, porosity=0.2)
adv = flopy.mf6.ModflowGwtadv(gwt, scheme="TVD")
Expand Down
12 changes: 3 additions & 9 deletions .docs/Notebooks/export_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
# Load our old friend...the Freyberg model

nam_file = "freyberg.nam"
model_ws = os.path.join(
"..", "..", "examples", "data", "freyberg_multilayer_transient"
)
model_ws = os.path.join("..", "..", "examples", "data", "freyberg_multilayer_transient")
ml = flopy.modflow.Modflow.load(nam_file, model_ws=model_ws, check=False)

# We can see the ``Modelgrid`` instance has generic entries, as does ``start_datetime``
Expand All @@ -44,9 +42,7 @@

# Setting the attributes of the ``ml.modelgrid`` is easy:

ml.modelgrid.set_coord_info(
xoff=123456.7, yoff=765432.1, angrot=15.0, crs=3070
)
ml.modelgrid.set_coord_info(xoff=123456.7, yoff=765432.1, angrot=15.0, crs=3070)
ml.dis.start_datetime = "7/4/1776"

ml.modeltime.start_datetime
Expand Down Expand Up @@ -125,9 +121,7 @@
export_dict = {"hds": hds, "cbc": cbc}

# export head and cell budget outputs to netcdf
fnc = flopy.export.utils.output_helper(
os.path.join(pth, "output.nc"), ml, export_dict
)
fnc = flopy.export.utils.output_helper(os.path.join(pth, "output.nc"), ml, export_dict)
# -

try:
Expand Down
16 changes: 4 additions & 12 deletions .docs/Notebooks/export_vtk_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
# load model for examples
nam_file = "freyberg.nam"
model_ws = Path(
os.path.join(
"..", "..", "examples", "data", "freyberg_multilayer_transient"
)
os.path.join("..", "..", "examples", "data", "freyberg_multilayer_transient")
)
ml = flopy.modflow.Modflow.load(nam_file, model_ws=model_ws, check=False)

Expand Down Expand Up @@ -107,9 +105,7 @@
# 3D Array export
# hk export, with points
model_hk_dir = output_dir / "HK"
ml.upw.hk.export(
model_hk_dir, smooth=True, fmt="vtk", name="HK", point_scalars=True
)
ml.upw.hk.export(model_hk_dir, smooth=True, fmt="vtk", name="HK", point_scalars=True)

# ### Package export to .vtu files
#
Expand Down Expand Up @@ -312,9 +308,7 @@

# +
# export heads as point scalars
vtkobj = vtk.Vtk(
ml, xml=True, pvd=True, point_scalars=True, vertical_exageration=10
)
vtkobj = vtk.Vtk(ml, xml=True, pvd=True, point_scalars=True, vertical_exageration=10)

# export heads for time step 1, stress periods 1, 50, 100, 1000
vtkobj.add_heads(hds, kstpkper=[(0, 0), (0, 49), (0, 99), (0, 999)])
Expand Down Expand Up @@ -559,9 +553,7 @@ def run_vertex_grid_example(ws):
# riv
riverline = [[(Lx - 1.0, Ly), (Lx - 1.0, 0.0)]]
rivcells = g.intersect(riverline, "line", 0)
rivspd = [
[(0, icpl), 320.0, 100000.0, 318] for icpl in rivcells["nodenumber"]
]
rivspd = [[(0, icpl), 320.0, 100000.0, 318] for icpl in rivcells["nodenumber"]]
riv = flopy.mf6.ModflowGwfriv(gwf, stress_period_data=rivspd)

# output control
Expand Down
6 changes: 1 addition & 5 deletions .docs/Notebooks/external_file_handling_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@
# list the files in model_ws that have 'hk' in the name
print(
"\n".join(
[
name
for name in os.listdir(ml.model_ws)
if "hk" in name or "impor" in name
]
[name for name in os.listdir(ml.model_ws) if "hk" in name or "impor" in name]
)
)

Expand Down
8 changes: 2 additions & 6 deletions .docs/Notebooks/get_transmissivities_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@
model_ws = temp_dir.name

m = flopy.modflow.Modflow("junk", version="mfnwt", model_ws=model_ws)
dis = flopy.modflow.ModflowDis(
m, nlay=nl, nrow=nr, ncol=nc, botm=botm, top=top
)
dis = flopy.modflow.ModflowDis(m, nlay=nl, nrow=nr, ncol=nc, botm=botm, top=top)
upw = flopy.modflow.ModflowUpw(m, hk=hk)
# -

Expand All @@ -88,9 +86,7 @@
# (cells that are partially within the open interval have reduced thickness, cells outside of the open interval have transmissivities of 0). If no `sctop` or `scbot` arguments are supplied, trasmissivites reflect the full saturated thickness in each column of cells (see plot below, which shows different open intervals relative to the model layering)

r, c = np.arange(nr), np.arange(nc)
T = flopy.utils.get_transmissivities(
heads, m, r=r, c=c, sctop=sctop, scbot=scbot
)
T = flopy.utils.get_transmissivities(heads, m, r=r, c=c, sctop=sctop, scbot=scbot)
np.round(T, 2)

m.dis.botm.array[:, r, c]
Expand Down
12 changes: 3 additions & 9 deletions .docs/Notebooks/gridgen_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@
)
print(msg)
else:
print(
f"gridgen executable was found at: {flopy_io.relpath_safe(gridgen_exe)}"
)
print(f"gridgen executable was found at: {flopy_io.relpath_safe(gridgen_exe)}")

# +
# temporary directory
Expand Down Expand Up @@ -264,9 +262,7 @@
gwf = flopy.mf6.ModflowGwf(sim, modelname=name, save_flows=True)
disv = flopy.mf6.ModflowGwfdisv(gwf, **disv_gridprops)
ic = flopy.mf6.ModflowGwfic(gwf)
npf = flopy.mf6.ModflowGwfnpf(
gwf, xt3doptions=True, save_specific_discharge=True
)
npf = flopy.mf6.ModflowGwfnpf(gwf, xt3doptions=True, save_specific_discharge=True)
chd = flopy.mf6.ModflowGwfchd(gwf, stress_period_data=chdspd)
budget_file = f"{name}.bud"
head_file = f"{name}.hds"
Expand Down Expand Up @@ -367,9 +363,7 @@
gwf = flopy.mf6.ModflowGwf(sim, modelname=name, save_flows=True)
disu = flopy.mf6.ModflowGwfdisu(gwf, **disu_gridprops)
ic = flopy.mf6.ModflowGwfic(gwf)
npf = flopy.mf6.ModflowGwfnpf(
gwf, xt3doptions=True, save_specific_discharge=True
)
npf = flopy.mf6.ModflowGwfnpf(gwf, xt3doptions=True, save_specific_discharge=True)
chd = flopy.mf6.ModflowGwfchd(gwf, stress_period_data=chdspd)
budget_file = f"{name}.bud"
head_file = f"{name}.hds"
Expand Down
36 changes: 9 additions & 27 deletions .docs/Notebooks/groundwater2023_watershed_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def densify_geometry(line, step, keep_internal_nodes=True):
lines_strings = []
if keep_internal_nodes:
for idx in range(1, len(line)):
lines_strings.append(
shapely.geometry.LineString(line[idx - 1 : idx + 1])
)
lines_strings.append(shapely.geometry.LineString(line[idx - 1 : idx + 1]))
else:
lines_strings = [shapely.geometry.LineString(line)]

Expand Down Expand Up @@ -262,9 +260,7 @@ def set_idomain(grid, boundary):
multiplier = 1.175
transition = 20000.0
ncells = 7
smoothr = [
transition * (multiplier - 1.0) / (multiplier ** float(ncells) - 1.0)
]
smoothr = [transition * (multiplier - 1.0) / (multiplier ** float(ncells) - 1.0)]
for i in range(ncells - 1):
smoothr.append(smoothr[i] * multiplier)
smooth = smoothr.copy()
Expand Down Expand Up @@ -326,9 +322,7 @@ def set_idomain(grid, boundary):
alpha=0.2,
cmap="Reds_r",
)
cg = pmv.contour_array(
top_sg_vrc, levels=levels, linewidths=0.3, colors="0.75"
)
cg = pmv.contour_array(top_sg_vrc, levels=levels, linewidths=0.3, colors="0.75")
pmv.plot_inactive()

ax.plot(bp[:, 0], bp[:, 1], "k-")
Expand Down Expand Up @@ -503,9 +497,7 @@ def set_idomain(grid, boundary):
delc=dx,
)
g = Gridgen(gwf.modelgrid, model_ws=temp_path)
adpoly = [
[[(1000, 1000), (3000, 1000), (3000, 2000), (1000, 2000), (1000, 1000)]]
]
adpoly = [[[(1000, 1000), (3000, 1000), (3000, 2000), (1000, 2000), (1000, 1000)]]]
adpoly = boundary_polygon + [boundary_polygon[0]]
adpoly = [[adpoly]]
g.add_refinement_features([lgr_poly], "polygon", 2, range(1))
Expand Down Expand Up @@ -570,9 +562,7 @@ def set_idomain(grid, boundary):
nodes = np.array(nodes)

# +
tri = Triangle(
maximum_area=5000 * 5000, angle=30, nodes=nodes, model_ws=temp_path
)
tri = Triangle(maximum_area=5000 * 5000, angle=30, nodes=nodes, model_ws=temp_path)
poly = bp
tri.add_polygon(poly)
tri.build(verbose=False)
Expand Down Expand Up @@ -753,9 +743,7 @@ def set_idomain(grid, boundary):
gg = grids[idx]
tt = topo_grids[idx]
for g, t in zip(gg[1:], tt[1:]):
pmvc = flopy.plot.PlotMapView(
modelgrid=g, ax=ax, extent=extent
)
pmvc = flopy.plot.PlotMapView(modelgrid=g, ax=ax, extent=extent)
pmvc.plot_array(top_ngc, vmin=vmin, vmax=vmax)
pmvc.plot_grid(**grid_dict)
cgc = pmvc.contour_array(top_ngc, **contour_dict)
Expand Down Expand Up @@ -873,9 +861,7 @@ def set_idomain(grid, boundary):
length=9,
pad=2,
)
cbar.ax.set_title(
"Elevation (m)", pad=2.5, loc="center", fontdict=font_dict
)
cbar.ax.set_title("Elevation (m)", pad=2.5, loc="center", fontdict=font_dict)
# -

# ### Plot the river intersection for the six grids
Expand Down Expand Up @@ -939,12 +925,8 @@ def set_idomain(grid, boundary):
gg = grids[idx]
tt = intersections[idx]
for g, t in zip(gg[1:], tt[1:]):
pmvc = flopy.plot.PlotMapView(
modelgrid=g, ax=ax, extent=extent
)
pmvc.plot_array(
t, masked_values=(0,), cmap=intersection_cmap
)
pmvc = flopy.plot.PlotMapView(modelgrid=g, ax=ax, extent=extent)
pmvc.plot_array(t, masked_values=(0,), cmap=intersection_cmap)
pmvc.plot_grid(**grid_dict)

# plot lgr polyline
Expand Down
4 changes: 1 addition & 3 deletions .docs/Notebooks/groundwater_paper_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@

# The discretization of the model is specified with the discretization file (DIS) of MODFLOW. The aquifer is divided into 201 cells of length 10 m and width 1 m. The first input of the discretization package is the name of the model object. All other input arguments are self explanatory.

fpm.ModflowDis(
model, nlay=1, nrow=1, ncol=201, delr=10, delc=1, top=50, botm=0
)
fpm.ModflowDis(model, nlay=1, nrow=1, ncol=201, delr=10, delc=1, top=50, botm=0)

# Active cells and the like are defined with the Basic package (BAS), which is required for every MODFLOW model. It contains the {\tt ibound} array, which is used to specify which cells are active (value is positive), inactive (value is 0), or fixed head (value is negative). The {\tt numpy} package (aliased as {\tt np}) can be used to quickly initialize the {\tt ibound} array with values of 1, and then set the {\tt ibound} value for the first and last columns to -1. The {\tt numpy} package (and Python, in general) uses zero-based indexing and supports negative indexing so that row 1 and column 1, and row 1 and column 201, can be referenced as [0, 0], and [0, -1], respectively. Although this simulation is for steady flow, starting heads still need to be specified. They are used as the head for fixed-head cells (where {\tt ibound} is negative), and as a starting point to compute the saturated thickness for cases of unconfined flow.

Expand Down
24 changes: 6 additions & 18 deletions .docs/Notebooks/groundwater_paper_uspb_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,15 @@
# +
hedObj = flopy.utils.HeadFile(os.path.join(ws, "DG.hds"), precision="double")
h = hedObj.get_data(kstpkper=(0, 0))
cbcObj = flopy.utils.CellBudgetFile(
os.path.join(ws, "DG.cbc"), precision="double"
)
cbcObj = flopy.utils.CellBudgetFile(os.path.join(ws, "DG.cbc"), precision="double")

frf = cbcObj.get_data(kstpkper=(0, 0), text="FLOW RIGHT FACE")[0]
fff = cbcObj.get_data(kstpkper=(0, 0), text="FLOW FRONT FACE")[0]
qx, qy, qz = flopy.utils.postprocessing.get_specific_discharge(
(frf, fff, None), ml
)
qx, qy, qz = flopy.utils.postprocessing.get_specific_discharge((frf, fff, None), ml)

# +
cnt = np.arange(1200, 1700, 100)
f, (ax1, ax2) = plt.subplots(
1, 2, figsize=(6.75, 4.47), constrained_layout=True
)
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(6.75, 4.47), constrained_layout=True)
ax1.set_xlim(0, xmax)
ax1.set_ylim(0, ymax)
ax2.set_xlim(0, xmax)
Expand Down Expand Up @@ -177,9 +171,7 @@
cb = plt.colorbar(h2, cax=ax3)
cb.ax.set_ylabel("Simulated head, m")

ax1.plot(
[-10000, 0], [-10000, 0], color="purple", lw=0.75, label="STR reaches"
)
ax1.plot([-10000, 0], [-10000, 0], color="purple", lw=0.75, label="STR reaches")
ax1.plot(
[-10000],
[-10000],
Expand All @@ -193,9 +185,7 @@
leg = ax1.legend(loc="upper left", numpoints=1, prop={"size": 6})
leg.draw_frame(False)

ax1.text(
0.0, 1.01, "Model layer 4", ha="left", va="bottom", transform=ax1.transAxes
)
ax1.text(0.0, 1.01, "Model layer 4", ha="left", va="bottom", transform=ax1.transAxes)
ax2.text(
0.98,
0.02,
Expand All @@ -204,9 +194,7 @@
va="bottom",
transform=ax2.transAxes,
)
ax2.text(
0.0, 1.01, "Model layer 5", ha="left", va="bottom", transform=ax2.transAxes
)
ax2.text(0.0, 1.01, "Model layer 5", ha="left", va="bottom", transform=ax2.transAxes)

plt.savefig(os.path.join(ws, "uspb_heads.png"), dpi=300)
# -
Expand Down
12 changes: 3 additions & 9 deletions .docs/Notebooks/mf6_complex_model_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@
)

# initial conditions
ic = flopy.mf6.ModflowGwfic(
gwf, pname="ic", strt=50.0, filename=f"{model_name}.ic"
)
ic = flopy.mf6.ModflowGwfic(gwf, pname="ic", strt=50.0, filename=f"{model_name}.ic")

# node property flow
npf = flopy.mf6.ModflowGwfnpf(
Expand Down Expand Up @@ -143,9 +141,7 @@
for layer in range(0, 3):
sy[layer]["data"] = 0.2

ss = flopy.mf6.ModflowGwfsto.ss.empty(
gwf, layered=True, default_value=0.000001
)
ss = flopy.mf6.ModflowGwfsto.ss.empty(gwf, layered=True, default_value=0.000001)

sto = flopy.mf6.ModflowGwfsto(
gwf,
Expand Down Expand Up @@ -296,9 +292,7 @@

obs_recarray = {
"head_obs.csv": [("h1_13_8", "HEAD", (2, 12, 7))],
"intercell_flow_obs1.csv": [
("ICF1_1.0", "FLOW-JA-FACE", (0, 4, 5), (0, 5, 5))
],
"intercell_flow_obs1.csv": [("ICF1_1.0", "FLOW-JA-FACE", (0, 4, 5), (0, 5, 5))],
"head-hydrographs.csv": [
("h3-13-9", "HEAD", (2, 12, 8)),
("h3-12-8", "HEAD", (2, 11, 7)),
Expand Down
Loading

0 comments on commit 12a3bcd

Please sign in to comment.