From b768910728e8f7cca4ee5aae3ee06c01724cca1f Mon Sep 17 00:00:00 2001 From: Jorn Bruggeman Date: Sat, 27 Jul 2024 08:37:27 +0100 Subject: [PATCH] pyfabm: get diagnostic metadata from original variable, not coupled one (#83) --- src/c/fabm_c.F90 | 4 ++-- src/c/variable.F90 | 7 ++++--- src/fabm.F90 | 2 ++ src/pyfabm/__init__.py | 47 +++--------------------------------------- 4 files changed, 11 insertions(+), 49 deletions(-) diff --git a/src/c/fabm_c.F90 b/src/c/fabm_c.F90 index 49f47702..0642bb61 100644 --- a/src/c/fabm_c.F90 +++ b/src/c/fabm_c.F90 @@ -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) diff --git a/src/c/variable.F90 b/src/c/variable.F90 index 97285472..8fc3998d 100644 --- a/src/c/variable.F90 +++ b/src/c/variable.F90 @@ -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) @@ -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) diff --git a/src/fabm.F90 b/src/fabm.F90 index 27c681e0..02dd2244 100644 --- a/src/fabm.F90 +++ b/src/fabm.F90 @@ -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 @@ -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 diff --git a/src/pyfabm/__init__.py b/src/pyfabm/__init__.py index 36522218..1d3a6096 100644 --- a/src/pyfabm/__init__.py +++ b/src/pyfabm/__init__.py @@ -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 @@ -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)