Skip to content

Commit

Permalink
change API for LSM and flow direction in component definition
Browse files Browse the repository at this point in the history
Turn them into methods rather than properties, and add "requires" to them to be more explicit and to avoid confusion with SpaceDomain properties (land_sea_mask and flow_direction).
  • Loading branch information
Thibault Hallouin committed Jun 30, 2021
1 parent b46162b commit f1d86ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
34 changes: 20 additions & 14 deletions cm4twc/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class MetaComponent(abc.ABCMeta):
_states_info = None
_outputs_info = None
_solver_history = None
_land_sea_mask = None
_flow_direction = None
_requires_land_sea_mask = None
_requires_flow_direction = None

@property
def category(cls):
Expand Down Expand Up @@ -84,13 +84,11 @@ def outputs_metadata(cls):
def solver_history(cls):
return cls._solver_history

@property
def flow_direction(cls):
return cls._flow_direction
def requires_flow_direction(cls):
return cls._requires_flow_direction

@property
def land_sea_mask(cls):
return cls._land_sea_mask
def requires_land_sea_mask(cls):
return cls._requires_land_sea_mask

def __str__(cls):
info = [
Expand All @@ -109,8 +107,16 @@ def __str__(cls):
+ [" category: {}".format(getattr(cls, '_category'))]
+ info
+ [" solver history: {}".format(getattr(cls, '_solver_history'))]
+ [" land sea mask: {}".format(getattr(cls, '_land_sea_mask'))]
+ [" flow direction: {}".format(getattr(cls, '_flow_direction'))]
+ [
" requires land sea mask: {}".format(
getattr(cls, '_requires_land_sea_mask')()
)
]
+ [
" requires flow direction: {}".format(
getattr(cls, '_requires_flow_direction')()
)
]
+ [")"]
)

Expand All @@ -128,8 +134,8 @@ class Component(metaclass=MetaComponent):
_states_info = {}
_outputs_info = {}
_solver_history = 1
_land_sea_mask = False
_flow_direction = False
_requires_land_sea_mask = False
_requires_flow_direction = False

def __init__(self, saving_directory, timedomain, spacedomain,
dataset=None, parameters=None, constants=None, records=None,
Expand Down Expand Up @@ -556,7 +562,7 @@ def _check_spacedomain(self, spacedomain):
"for spacedomain".format(Grid.__name__)
)

if self._land_sea_mask:
if self._requires_land_sea_mask:
if spacedomain.land_sea_mask is None:
raise RuntimeError(
"'land_sea_mask' must be set in {} of {} "
Expand All @@ -565,7 +571,7 @@ def _check_spacedomain(self, spacedomain):
self.__class__.__name__)
)

if self._flow_direction:
if self._requires_flow_direction:
if spacedomain.flow_direction is None:
raise RuntimeError(
"'flow_direction' must be set in {} of {} "
Expand Down
4 changes: 2 additions & 2 deletions tests/components/surfacelayer/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class Dummy(SurfaceLayerComponent):
}
}
_solver_history = 1
_land_sea_mask = True
_flow_direction = True
_requires_land_sea_mask = True
_requires_flow_direction = True

def initialise(self,
# component states
Expand Down

0 comments on commit f1d86ea

Please sign in to comment.