Commit 50bd6754 authored by Jorn Bruggeman's avatar Jorn Bruggeman
Browse files

python driver: tweaked gui test case; enable access to model long names

parent a872e711
......@@ -28,6 +28,7 @@ fabm.get_variable_counts.argtypes = [ctypes.POINTER(ctypes.c_int),ctypes.POINTER
fabm.get_variable_metadata.argtypes = [ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
fabm.get_parameter_metadata.argtypes = [ctypes.c_int,ctypes.c_int,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(ctypes.c_int),ctypes.POINTER(ctypes.c_int)]
fabm.get_dependency_metadata.argtypes = [ctypes.c_int,ctypes.c_int,ctypes.c_char_p,ctypes.c_char_p]
fabm.get_model_metadata.argtypes = [ctypes.c_char_p,ctypes.c_int,ctypes.c_char_p]
fabm.get_real_parameter.argtypes = [ctypes.c_int,ctypes.c_int]
fabm.get_real_parameter.restype = ctypes.c_double
fabm.get_integer_parameter.argtypes = [ctypes.c_int,ctypes.c_int]
......@@ -60,20 +61,31 @@ unicodesuperscript = {'1':u'\u00B9','2':u'\u00B2','3':u'\u00B3',
'4':u'\u2074','5':u'\u2075','6':u'\u2076',
'7':u'\u2077','8':u'\u2078','9':u'\u2079',
'0':u'\u2070','-':u'\u207B'}
unicodesubscript = {'1':u'\u2081','2':u'\u2082','3':u'\u2083',
'4':u'\u2084','5':u'\u2085','6':u'\u2086',
'7':u'\u2087','8':u'\u2088','9':u'\u2089',
'0':u'\u2080'}
supnumber = re.compile('(?<=\w)(-?\d+)(?=[ \*+\-/]|$)')
supenumber = re.compile('(?<=\d)e-(\d+)(?=[ \*+\-/]|$)')
oldsupminus = re.compile('/(\w+)\*\*(\d+)(?=[ \*+\-/]|$)')
oldsup = re.compile('/(\w+)\*\*(\d+)(?=[ \*+\-/]|$)')
supenumber = re.compile('(?<=\d)e(-?\d+)(?=[ \*+\-/]|$)')
oldsupminus = re.compile('/(\w+)(?:\*\*)|\^(\d+)(?=[ \*+\-/]|$)')
oldsup = re.compile('(?<=\w)(?:\*\*)|\^(-?\d+)(?=[ \*+\-/]|$)')
oldsub = re.compile('(?<=\w)_(-?\d+)(?=[ \*+\-/]|$)')
def createPrettyUnit(unit):
def repl(m):
def replace_superscript(m):
return u''.join([unicodesuperscript[n] for n in m.group(1)])
def replace_subscript(m):
return u''.join([unicodesubscript[n] for n in m.group(1)])
def reple(m):
return u'\u00D710\u207B%s' % u''.join([unicodesuperscript[n] for n in m.group(1)])
def replold(m):
return u'\u00D710%s' % u''.join([unicodesuperscript[n] for n in m.group(1)])
def reploldminus(m):
return u' %s\u207B%s' % (m.group(1),u''.join([unicodesuperscript[n] for n in m.group(2)]))
#def replold(m):
# return u'%s%s' % (m.group(1),u''.join([unicodesuperscript[n] for n in m.group(2)]))
unit = oldsup.sub(replace_superscript,unit)
unit = oldsub.sub(replace_subscript,unit)
unit = supenumber.sub(reple,unit)
unit = supnumber.sub(repl,unit)
unit = oldsupminus.sub(replold,unit)
unit = supnumber.sub(replace_superscript,unit)
#unit = oldsupminus.sub(reploldminus,unit)
return unit
def printTree(root,stringmapper,indent=''):
......@@ -197,6 +209,11 @@ class Model(object):
fabm.initialize(path)
self.updateConfiguration()
def getModelLongName(self,name):
strname = ctypes.create_string_buffer(ATTRIBUTE_LENGTH)
fabm.get_model_metadata(name,ATTRIBUTE_LENGTH,strname)
return strname.value
def saveSettings(self):
environment = dict([(dependency.name,dependency.value) for dependency in self.dependencies])
state = dict([(variable.name,variable.value) for variable in self.state_variables])
......
......@@ -53,6 +53,17 @@ class ItemModel(QtCore.QAbstractItemModel):
root.addTree(self.model.parameters,'parameters')
root.addTree(self.model.state_variables,'initialization')
root.addChild(env)
class Submodel:
def __init__(self,long_name):
self.units = None
self.units_unicode = None
self.value = None
self.long_name = long_name
def processNode(n,prefix=()):
if isinstance(n.object,basestring) and prefix and prefix[-1] not in ('parameters','initialization','environment'):
n.object = Submodel(self.model.getModelLongName('/'.join(prefix)))
for child in n.children: processNode(child,prefix+(child.name,))
processNode(root)
if self.root is not None:
def processChange(newnode,oldnode,parent):
oldnode.object = newnode.object
......@@ -119,26 +130,24 @@ class ItemModel(QtCore.QAbstractItemModel):
def data(self,index,role):
if not index.isValid(): return
entry = index.internalPointer()
data = entry.object
if role==QtCore.Qt.DisplayRole:
entry = index.internalPointer()
if index.column()==0: return entry.name
data = entry.object
if index.column()==0:
if isinstance(data,basestring): return entry.name
return data.long_name
if not isinstance(data,basestring):
if index.column()==1:
return data.getValue()
elif index.column()==2:
return data.value
elif index.column()==2 and data.units:
return data.units_unicode
elif index.column()==3:
return data.long_name
return entry.name
elif role==QtCore.Qt.ToolTipRole and index.parent().isValid():
data = index.internalPointer().object
if not isinstance(data,basestring): return data.long_name
if not isinstance(data,basestring): return '%s (%s)' % (data.long_name,entry.name)
elif role==QtCore.Qt.EditRole:
data = index.internalPointer().object
if not isinstance(data,basestring): return data.getValue()
elif role==QtCore.Qt.FontRole and index.column()==1:
if index.column()==0: return entry.name
data = index.internalPointer().object
if isinstance(data,pyfabm.Parameter) and data.value!=data.default:
font = QtGui.QFont()
font.setBold(True)
......@@ -162,4 +171,4 @@ class ItemModel(QtCore.QAbstractItemModel):
def headerData(self,section,orientation,role):
if orientation==QtCore.Qt.Horizontal and role==QtCore.Qt.DisplayRole and section>=0 and section<4:
return ('name','value','units','description')[section]
return ('name','value','units','symbol')[section]
......@@ -244,6 +244,21 @@
call copy_to_c_string(environment_units(index),units)
end subroutine
subroutine get_model_metadata(name,length,long_name) bind(c)
!DIR$ ATTRIBUTES DLLEXPORT :: get_model_metadata
character(kind=c_char),intent(in) :: name(*)
integer(c_int), intent(in), value :: length
character(kind=c_char),intent(out) :: long_name(length)
character(len=attribute_length),pointer :: pname
class (type_base_model), pointer :: found_model
call c_f_pointer(c_loc(name), pname)
found_model => model%root%find_model(pname(:index(pname,C_NULL_CHAR)-1))
if (.not.associated(found_model)) call driver%fatal_error('get_model_metadata','model "'//pname(:index(pname,C_NULL_CHAR)-1)//'" not found.')
call copy_to_c_string(found_model%long_name,long_name)
end subroutine
subroutine link_dependency_data(index,value) bind(c)
!DIR$ ATTRIBUTES DLLEXPORT :: link_dependency_data
integer(c_int),intent(in),value :: index
......
#!/usr/bin/env python
import sys
import sys,optparse
parser = optparse.OptionParser()
options, args = parser.parse_args()
yamlfile = '../fabm-npzd-carbonate.yaml'
if len(sys.argv)>1: yamlfile = sys.argv[1]
if args: yamlfile = args[0]
try:
import pyfabm
......
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