Skip to content

Commit

Permalink
pyfabm: use correct metadata for coupled diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
jornbr committed Jul 3, 2024
1 parent ffa5b2b commit da0ab7a
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions src/pyfabm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,12 @@ def __init__(
self.model.fabm.variable_get_metadata(
variable_pointer, ATTRIBUTE_LENGTH, strname, strunits, strlong_name
)
name = strname.value.decode("ascii")
units = strunits.value.decode("ascii")
long_name = strlong_name.value.decode("ascii")
if name is None:
name = strname.value.decode("ascii")
if units is None:
units = strunits.value.decode("ascii")
if long_name is None:
long_name = strlong_name.value.decode("ascii")
self.properties = VariableProperties(self.model, self.variable_pointer)

self.name = name
Expand Down Expand Up @@ -730,11 +733,17 @@ class DiagnosticVariable(Variable):
def __init__(
self,
model: "Model",
name: str,
units: str,
long_name: str,
path: str,
variable_pointer: ctypes.c_void_p,
index: int,
horizontal: bool,
):
Variable.__init__(self, model, variable_pointer=variable_pointer)
Variable.__init__(
self, model, name, units, long_name, path, variable_pointer=variable_pointer
)
self.data = None
self.horizontal = horizontal
self.index = index
Expand All @@ -749,13 +758,13 @@ def output(self) -> bool:
return self.model.fabm.variable_get_output(self.variable_pointer) != 0

def setSave(self, value: bool):
self.model.fabm.set_variable_save(
self.model.pmodel,
vartype = (
HORIZONTAL_DIAGNOSTIC_VARIABLE
if self.horizontal
else INTERIOR_DIAGNOSTIC_VARIABLE,
self.index,
1 if value else 0,
else INTERIOR_DIAGNOSTIC_VARIABLE
)
self.model.fabm.set_variable_save(
self.model.pmodel, vartype, self.index, 1 if value else 0
)

save = property(fset=setSave)
Expand Down Expand Up @@ -1283,18 +1292,56 @@ def updateConfiguration(self, settings=None):
StateVariable(self, ptr, self._bottom_state[i, ...])
)
for i in range(ndiag_interior.value):
self.fabm.get_variable_metadata(
self.pmodel,
INTERIOR_DIAGNOSTIC_VARIABLE,
i + 1,
ATTRIBUTE_LENGTH,
strname,
strunits,
strlong_name,
strpath,
)
ptr = self.fabm.get_variable(
self.pmodel, INTERIOR_DIAGNOSTIC_VARIABLE, i + 1
)
self.interior_diagnostic_variables._data.append(
DiagnosticVariable(self, ptr, i + 1, False)
DiagnosticVariable(
self,
strname.value.decode("ascii"),
strunits.value.decode("ascii"),
strlong_name.value.decode("ascii"),
strpath.value.decode("ascii"),
ptr,
i + 1,
False,
)
)
for i in range(ndiag_horizontal.value):
self.fabm.get_variable_metadata(
self.pmodel,
HORIZONTAL_DIAGNOSTIC_VARIABLE,
i + 1,
ATTRIBUTE_LENGTH,
strname,
strunits,
strlong_name,
strpath,
)
ptr = self.fabm.get_variable(
self.pmodel, HORIZONTAL_DIAGNOSTIC_VARIABLE, i + 1
)
self.horizontal_diagnostic_variables._data.append(
DiagnosticVariable(self, ptr, i + 1, True)
DiagnosticVariable(
self,
strname.value.decode("ascii"),
strunits.value.decode("ascii"),
strlong_name.value.decode("ascii"),
strpath.value.decode("ascii"),
ptr,
i + 1,
True,
)
)
for i in range(ndependencies_interior.value):
ptr = self.fabm.get_variable(self.pmodel, INTERIOR_DEPENDENCY, i + 1)
Expand Down

0 comments on commit da0ab7a

Please sign in to comment.