Skip to content

Commit

Permalink
black and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
OnnoEbbens committed Jun 26, 2023
1 parent e1da378 commit 405e0ec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 43 deletions.
67 changes: 25 additions & 42 deletions nlmod/dims/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def xy_to_icell2d(xy, ds):
number of the icell2d value of a cell containing the xy point.
"""

icell2d = (np.abs(ds.x.data - xy[0]) +
np.abs(ds.y.data - xy[1])).argmin().item()
icell2d = (np.abs(ds.x.data - xy[0]) + np.abs(ds.y.data - xy[1])).argmin().item()

return icell2d

Expand Down Expand Up @@ -355,8 +354,7 @@ def refine(
fname, geom_type, level = refinement_feature
if not model_coordinates and ds_has_rotation:
raise (
Exception(
"Converting files to model coordinates not supported")
Exception("Converting files to model coordinates not supported")
)
g.add_refinement_features(fname, geom_type, level, layers=[0])
elif len(refinement_feature) == 2:
Expand All @@ -378,10 +376,8 @@ def refine(
)
mask = geom_types == geom_type
# features = [gdf[mask].unary_union]
features = list(
gdf[mask].geometry.explode(index_parts=True))
g.add_refinement_features(
features, geom_type, level, layers=[0])
features = list(gdf[mask].geometry.explode(index_parts=True))
g.add_refinement_features(features, geom_type, level, layers=[0])
g.build()
gridprops = g.get_gridprops_disv()
gridprops["area"] = g.get_area()
Expand Down Expand Up @@ -423,15 +419,13 @@ def ds_to_gridprops(ds_in, gridprops, method="nearest", nodata=-1):
y = xr.DataArray(xyi[:, 1], dims=("icell2d",))
if method in ["nearest", "linear"]:
# resample the entire dataset in one line
ds_out = ds_in.interp(x=x, y=y, method=method,
kwargs={"fill_value": None})
ds_out = ds_in.interp(x=x, y=y, method=method, kwargs={"fill_value": None})
else:
ds_out = xr.Dataset(coords={"layer": ds_in.layer.data, "x": x, "y": y})

# add other variables
for data_var in ds_in.data_vars:
data_arr = structured_da_to_ds(
ds_in[data_var], ds_out, method=method)
data_arr = structured_da_to_ds(ds_in[data_var], ds_out, method=method)
ds_out[data_var] = data_arr

if "area" in gridprops:
Expand Down Expand Up @@ -529,8 +523,7 @@ def update_ds_from_layer_ds(ds, layer_ds, method="nearest", **kwargs):
else:
x = ds.x
y = ds.y
layer_ds = layer_ds.interp(
x=x, y=y, method=method, kwargs={"fill_value": None})
layer_ds = layer_ds.interp(x=x, y=y, method=method, kwargs={"fill_value": None})
for var in layer_ds.data_vars:
ds[var] = layer_ds[var]
else:
Expand Down Expand Up @@ -593,8 +586,7 @@ def col_to_list(col_in, ds, cellids):
# 2d vertex grid
col_lst = col_in.data[cellids[0]]
else:
raise ValueError(
f"could not create a column list for col_in={col_in}")
raise ValueError(f"could not create a column list for col_in={col_in}")
else:
col_lst = [col_in] * len(cellids[0])

Expand Down Expand Up @@ -685,8 +677,7 @@ def lrc_to_reclist(
for i_aux in aux:
if isinstance(i_aux, str):
if "layer" in ds[i_aux].dims and len(cellids) != 3:
cols.append(col_to_list(
i_aux, ds, (np.array(layers),) + cellids))
cols.append(col_to_list(i_aux, ds, (np.array(layers),) + cellids))
else:
cols.append(col_to_list(i_aux, ds, cellids))
else:
Expand Down Expand Up @@ -781,8 +772,7 @@ def lcid_to_reclist(
for i_aux in aux:
if isinstance(i_aux, str):
if "layer" in ds[i_aux].dims and len(cellids) != 2:
cols.append(col_to_list(
i_aux, ds, (np.array(layers),) + cellids))
cols.append(col_to_list(i_aux, ds, (np.array(layers),) + cellids))
else:
cols.append(col_to_list(i_aux, ds, cellids))
else:
Expand Down Expand Up @@ -994,8 +984,7 @@ def gdf_to_data_array_struc(

# interpolate data
if interp_method is not None:
arr = interpolate_gdf_to_array(
gdf, gwf, field=field, method=interp_method)
arr = interpolate_gdf_to_array(gdf, gwf, field=field, method=interp_method)
da.values = arr

return da
Expand All @@ -1008,8 +997,7 @@ def gdf_to_data_array_struc(
raise ValueError(
"multiple geometries in one cell please define aggregation method"
)
gdf_agg = aggregate_vector_per_cell(
gdf_cellid, {field: agg_method}, gwf)
gdf_agg = aggregate_vector_per_cell(gdf_cellid, {field: agg_method}, gwf)
else:
# aggregation not neccesary
gdf_agg = gdf_cellid[[field]]
Expand Down Expand Up @@ -1070,13 +1058,13 @@ def gdf_to_da(
raise ValueError(
"multiple geometries in one cell please define aggregation method"
)
if agg_method in ['nearest']:
if agg_method in ["nearest"]:
modelgrid = modelgrid_from_ds(ds)
gdf_agg = aggregate_vector_per_cell(gdf_cellid, {column: agg_method},
modelgrid)
else:
gdf_agg = aggregate_vector_per_cell(
gdf_cellid, {column: agg_method})
gdf_cellid, {column: agg_method}, modelgrid
)
else:
gdf_agg = aggregate_vector_per_cell(gdf_cellid, {column: agg_method})
else:
# aggregation not neccesary
gdf_agg = gdf_cellid[[column]]
Expand Down Expand Up @@ -1116,8 +1104,7 @@ def interpolate_gdf_to_array(gdf, gwf, field="values", method="nearest"):
# check geometry
geom_types = gdf.geometry.type.unique()
if geom_types[0] != "Point":
raise NotImplementedError(
"can only use interpolation with point geometries")
raise NotImplementedError("can only use interpolation with point geometries")

# check field
if field not in gdf.columns:
Expand Down Expand Up @@ -1153,23 +1140,20 @@ def _agg_max_length(gdf, col):

def _agg_length_weighted(gdf, col):
nanmask = gdf[col].isna()
aw = (gdf.length * gdf[col]).sum(skipna=True) / \
gdf.loc[~nanmask].length.sum()
aw = (gdf.length * gdf[col]).sum(skipna=True) / gdf.loc[~nanmask].length.sum()
return aw


def _agg_nearest(gdf, col, modelgrid):
if modelgrid.grid_type == 'structured':
if modelgrid.grid_type == "structured":
cid = gdf["cellid"].values[0]
cellcenter = Point(
modelgrid.xcellcenters[0, cid[1]],
modelgrid.ycellcenters[cid[0], 0])
modelgrid.xcellcenters[0, cid[1]], modelgrid.ycellcenters[cid[0], 0]
)
val = gdf.iloc[gdf.distance(cellcenter).argmin()].loc[col]
elif modelgrid.grid_type == 'vertex':
elif modelgrid.grid_type == "vertex":
cid = gdf["cellid"].values[0]
cellcenter = Point(
modelgrid.xcellcenters[cid],
modelgrid.ycellcenters[cid])
cellcenter = Point(modelgrid.xcellcenters[cid], modelgrid.ycellcenters[cid])
val = gdf.iloc[gdf.distance(cellcenter).argmin()].loc[col]

return val
Expand Down Expand Up @@ -1597,8 +1581,7 @@ def mask_model_edge(ds, idomain):
"""
# add constant head cells at model boundaries
if "angrot" in ds.attrs and ds.attrs["angrot"] != 0.0:
raise NotImplementedError(
"model edge not yet calculated for rotated grids")
raise NotImplementedError("model edge not yet calculated for rotated grids")

# get mask with grid edges
xmin = ds["x"] == ds["x"].min()
Expand Down
2 changes: 1 addition & 1 deletion nlmod/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from importlib import metadata
from platform import python_version

__version__ = "0.6.1b"
__version__ = "0.6.1"


def show_versions() -> None:
Expand Down

0 comments on commit 405e0ec

Please sign in to comment.