Skip to content
Snippets Groups Projects
Commit 1f53cedb authored by Johannes Schumann's avatar Johannes Schumann
Browse files

Use f90nml package for jobcard

parent f3bdb933
No related branches found
No related tags found
1 merge request!1Merge python environment
...@@ -13,6 +13,10 @@ __email__ = "jschumann@km3net.de" ...@@ -13,6 +13,10 @@ __email__ = "jschumann@km3net.de"
__status__ = "Development" __status__ = "Development"
import f90nml import f90nml
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
INPUT_PATH = "/opt/buuinput2019/" INPUT_PATH = "/opt/buuinput2019/"
...@@ -48,8 +52,8 @@ class Jobcard(object): ...@@ -48,8 +52,8 @@ class Jobcard(object):
The input path pointing to the GiBUU lookup data which should be used The input path pointing to the GiBUU lookup data which should be used
""" """
def __init__(self, input_path=INPUT_PATH): def __init__(self, input_path=INPUT_PATH):
self.input_path = "'%s'" % input_path self.input_path = "%s" % input_path
self._groups = f90nml.Namelist() self._nml = f90nml.Namelist()
self.set_property("input", "path_to_input", self.input_path) self.set_property("input", "path_to_input", self.input_path)
def set_property(self, group, name, value): def set_property(self, group, name, value):
...@@ -64,36 +68,29 @@ class Jobcard(object): ...@@ -64,36 +68,29 @@ class Jobcard(object):
value: value:
The property value The property value
""" """
if group not in self._groups.keys(): if group not in self._nml.keys():
self._groups[group] = {} self._nml[group] = {}
self._groups[group][name] = value self._nml[group][name] = value
def remove_property(self, group, name): def remove_property(self, group, name):
del self._groups[group][name] del self._nml[group][name]
if len(self._groups[group]) == 0: if len(self._nml[group]) == 0:
del self._groups[group] del self._nml[group]
def __str__(self): def __str__(self):
retval = "" stream = StringIO()
for group, attrs in self._groups.items(): self._nml.write(stream)
retval += "&%s\n" % group return stream.getvalue()
for attr, value in attrs.items():
if type(value) is bool:
retval += "\t%s = .%s.\n" % (attr, str(value).lower())
else:
retval += "\t%s = %s\n" % (attr, value)
retval += "/\n\n"
return retval
def __getitem__(self, key): def __getitem__(self, key):
if isinstance(key, str): if isinstance(key, str):
if '/' in key: if '/' in key:
k = key.split('/') k = key.split('/')
return self._groups[k[0]][k[1]] return self._nml[k[0]][k[1]]
else: else:
return self._groups[key] return self._nml[key]
elif isinstance(key, tuple) and len(key) == 2: elif isinstance(key, tuple) and len(key) == 2:
return self._groups[key[0]][key[1]] return self._nml[key[0]][key[1]]
else: else:
raise IndexError("Invalid access to Jobcard") raise IndexError("Invalid access to Jobcard")
...@@ -107,7 +104,7 @@ def read_jobcard(fpath): ...@@ -107,7 +104,7 @@ def read_jobcard(fpath):
Filepath of the jobcard Filepath of the jobcard
""" """
jc = Jobcard() jc = Jobcard()
jc._groups = f90nml.read(fpath) jc._nml = f90nml.read(fpath)
return jc return jc
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment