Skip to content

Commit

Permalink
pyfabm: get diagnostic metadata from original variable, not coupled o…
Browse files Browse the repository at this point in the history
…ne (#83)
  • Loading branch information
jornbr authored Jul 27, 2024
1 parent ce46f0f commit b768910
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/c/fabm_c.F90
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ function get_variable(pmodel, category, index) bind(c) result(pvariable)
case (BOTTOM_STATE_VARIABLE)
variable => model%p%bottom_state_variables(index)%target
case (INTERIOR_DIAGNOSTIC_VARIABLE)
variable => model%p%interior_diagnostic_variables(index)%target
variable => model%p%interior_diagnostic_variables(index)%original
case (HORIZONTAL_DIAGNOSTIC_VARIABLE)
variable => model%p%horizontal_diagnostic_variables(index)%target
variable => model%p%horizontal_diagnostic_variables(index)%original
case (CONSERVED_QUANTITY)
variable => model%p%conserved_quantities(index)%target
case (INTERIOR_DEPENDENCY, HORIZONTAL_DEPENDENCY, SCALAR_DEPENDENCY)
Expand Down
7 changes: 4 additions & 3 deletions src/c/variable.F90
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ function variable_get_background_value(pvariable) bind(c) result(value)
type (type_internal_variable), pointer :: variable

call c_f_pointer(pvariable, variable)
value = 0.0_rk
if (size(variable%background_values%pointers) > 0) value = variable%background_values%pointers(1)%p
value = variable%background_values%value
end function variable_get_background_value

function variable_get_missing_value(pvariable) bind(c) result(value)
Expand All @@ -76,10 +75,12 @@ function variable_get_output(pvariable) bind(c) result(value)
type (c_ptr), value, intent(in) :: pvariable
integer(kind=c_int) :: value

integer :: output
type (type_internal_variable), pointer :: variable

call c_f_pointer(pvariable, variable)
value = logical2int(variable%output /= output_none)
output = iand(variable%output, not(output_always_available))
value = logical2int(output /= output_none)
end function variable_get_output

function variable_is_required(pvariable) bind(c) result(value)
Expand Down
2 changes: 2 additions & 0 deletions src/fabm.F90
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module fabm
integer :: externalid = 0

type (type_internal_variable), pointer :: target => null()
type (type_internal_variable), pointer :: original => null()
end type

!> Metadata for an interior state variable
Expand Down Expand Up @@ -2809,6 +2810,7 @@ subroutine copy_variable_metadata(internal_variable, external_variable)
external_variable%missing_value = internal_variable%missing_value
external_variable%output = iand(internal_variable%output, not(output_always_available))
external_variable%target => internal_variable
external_variable%original => internal_variable

! Prepend long names of ancestor models to long name of variable.
owner => internal_variable%owner
Expand Down
47 changes: 3 additions & 44 deletions src/pyfabm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,11 @@ class DiagnosticVariable(Variable):
def __init__(
self,
model: "Model",
name: str,
units: str,
long_name: str,
variable_pointer: ctypes.c_void_p,
index: int,
horizontal: bool,
):
Variable.__init__(
self, model, name, units, long_name, variable_pointer=variable_pointer
)
Variable.__init__(self, model, variable_pointer=variable_pointer)
self.data = None
self.horizontal = horizontal
self.index = index
Expand Down Expand Up @@ -1279,54 +1274,18 @@ 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,
strpath.value.decode("ascii"),
strunits.value.decode("ascii"),
strlong_name.value.decode("ascii"),
ptr,
i + 1,
False,
)
DiagnosticVariable(self, 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,
strpath.value.decode("ascii"),
strunits.value.decode("ascii"),
strlong_name.value.decode("ascii"),
ptr,
i + 1,
True,
)
DiagnosticVariable(self, 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 b768910

Please sign in to comment.