Skip to content

Commit

Permalink
Add contextmanager for hold on applying constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
SorooshMani-NOAA committed Jan 16, 2024
1 parent 1c21daa commit eaf4928
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion ocsmesh/hfun/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import tempfile
from time import time
from typing import Union, List, Callable, Optional, Iterable, Tuple
from contextlib import ExitStack
from contextlib import ExitStack, contextmanager
import warnings
try:
# pylint: disable=C0412
Expand Down Expand Up @@ -234,6 +234,8 @@ def __init__(self,
self._hmax = float(hmax) if hmax is not None else hmax
self._verbosity = int(verbosity)
self._constraints = []
self._hold_const = False
self._hold_const_hit = 0


def __del__(self):
Expand Down Expand Up @@ -499,6 +501,18 @@ def msh_t(
return output_mesh


@contextmanager
def hold_applying_added_constratins(self):
try:
self._hold_const = True
yield
finally:
self._hold_const = False
if self._hold_const_hit > 0:
self.apply_added_constraints()
self._hold_const_hit = 0


def apply_added_constraints(self) -> None:
"""Apply all the added constraints
Expand All @@ -514,6 +528,9 @@ def apply_added_constraints(self) -> None:
None
"""

if self._hold_const:
self._hold_const_hit += 1
return
self.apply_constraints(self._constraints)


Expand Down
2 changes: 1 addition & 1 deletion ocsmesh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,7 @@ def create_rectangle_mesh(
def raster_from_numpy(
filename,
data,
mgrid,
mgrid, # Needs to have ij indexing!
crs=CRS.from_epsg(4326)
) -> None:
x = mgrid[0][:, 0]
Expand Down

0 comments on commit eaf4928

Please sign in to comment.