Skip to content

Commit

Permalink
simplify ind method (#189)
Browse files Browse the repository at this point in the history
* simplify ind method

* add unittest for index

* bump version to 1.0.4.dev
  • Loading branch information
DirkEilander authored Mar 14, 2024
1 parent 04819fd commit 16bc157
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion envs/hydromt-sfincs-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
- descartes
- ffmpeg
- geopandas>=0.8
- hydromt>=0.9.1
- hydromt>=0.9.1, <0.10
- jupyterlab
- matplotlib
- nbsphinx
Expand Down
2 changes: 1 addition & 1 deletion hydromt_sfincs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from os.path import dirname, join, abspath


__version__ = "1.0.3"
__version__ = "1.0.4.dev"

DATADIR = join(dirname(abspath(__file__)), "data")

Expand Down
4 changes: 1 addition & 3 deletions hydromt_sfincs/regulargrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ def empty_mask(self) -> xr.DataArray:
def ind(self, mask: np.ndarray) -> np.ndarray:
"""Return indices of active cells in mask."""
assert mask.shape == (self.nmax, self.mmax)
iok = np.where(np.transpose(mask) > 0)
iok = (iok[1], iok[0])
ind = np.ravel_multi_index(iok, (self.nmax, self.mmax), order="F")
ind = np.where(mask.ravel(order="F"))[0]
return ind

def write_ind(
Expand Down
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@
from os.path import abspath, dirname, join

import pytest
import numpy as np

from hydromt_sfincs.sfincs import SfincsModel
from hydromt_sfincs.regulargrid import RegularGrid

TESTDATADIR = join(dirname(abspath(__file__)), "data")
TESTMODELDIR = join(TESTDATADIR, "sfincs_test")


@pytest.fixture
def reggrid():
# create a simple regular grid
grid = RegularGrid(
x0=318650,
y0=5040000,
dx=150,
dy=150,
nmax=84, # height
mmax=36, # width
)
return grid


@pytest.fixture
def mask(reggrid):
# create a simple mask
mask = np.zeros((reggrid.nmax, reggrid.mmax), dtype="u1")
mask[2:, 3:-1] = 1
return mask


@pytest.fixture
def weirs():
feats = [
Expand Down
8 changes: 8 additions & 0 deletions tests/test_regulargrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import numpy as np


def test_ind(reggrid, mask):
ind = reggrid.ind(mask)
assert ind[0] == 254
assert ind[-1] == 2939
assert ind.size == np.sum(mask > 0)

0 comments on commit 16bc157

Please sign in to comment.