Commit 38cbaedf authored by Jorn Bruggeman's avatar Jorn Bruggeman
Browse files

pyfabm: protect against unsaved diagnostics

parent e14aea0e
......@@ -284,15 +284,16 @@ class StateVariable(Variable):
class DiagnosticVariable(Variable):
def __init__(self,variable_pointer,index,horizontal):
Variable.__init__(self,variable_pointer=variable_pointer)
pdata = ctypes.POINTER(ctypes.c_double)()
if horizontal:
fabm.get_horizontal_diagnostic_data(index+1,ctypes.byref(pdata))
else:
fabm.get_interior_diagnostic_data(index+1,ctypes.byref(pdata))
self.data = pdata.contents
def getValue(self):
return self.data.value
if not asattr(self, 'data'):
pdata = ctypes.POINTER(ctypes.c_double)()
if horizontal:
fabm.get_horizontal_diagnostic_data(index+1,ctypes.byref(pdata))
else:
fabm.get_interior_diagnostic_data(index+1,ctypes.byref(pdata))
self.data = None if not pdata else pdata.contents
return None if self.data is None else self.data.value
@property
def output(self):
......
......@@ -2,7 +2,7 @@
module fabm_python
use iso_c_binding, only: c_double, c_int, c_char, C_NULL_CHAR, c_f_pointer, c_loc, c_ptr
use iso_c_binding, only: c_double, c_int, c_char, C_NULL_CHAR, c_f_pointer, c_loc, c_ptr, c_null_ptr
!DIR$ ATTRIBUTES DLLEXPORT :: STATE_VARIABLE,DIAGNOSTIC_VARIABLE,CONSERVED_QUANTITY
......@@ -487,14 +487,22 @@ contains
!DIR$ ATTRIBUTES DLLEXPORT :: get_interior_diagnostic_data
integer(c_int), intent(in), value :: index
type(c_ptr), intent(out) :: ptr
ptr = c_loc(model%get_interior_diagnostic_data(index))
real(rk), pointer :: pvalue
ptr = c_null_ptr
pvalue => model%get_interior_diagnostic_data(index)
if (associated(pvalue)) ptr = c_loc(pvalue)
end subroutine get_interior_diagnostic_data
subroutine get_horizontal_diagnostic_data(index, ptr) bind(c)
!DIR$ ATTRIBUTES DLLEXPORT :: get_horizontal_diagnostic_data
integer(c_int), intent(in), value :: index
type(c_ptr), intent(out) :: ptr
ptr = c_loc(model%get_horizontal_diagnostic_data(index))
real(rk), pointer :: pvalue
ptr = c_null_ptr
pvalue => model%get_horizontal_diagnostic_data(index)
if (associated(pvalue)) ptr = c_loc(pvalue)
end subroutine get_horizontal_diagnostic_data
subroutine finalize()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment