Skip to content

Commit 69408a7

Browse files
committed
options.py Added an options file which was in the archived boututils but not in the current version.
pfile.py Added an older file that we used to use for loading the pfile from the kinetic efit files.
1 parent 0d66e49 commit 69408a7

2 files changed

Lines changed: 506 additions & 0 deletions

File tree

src/boututils/options.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
"""Module to allow BOUT.inp files to be read into python and
2+
manipulated with ease.
3+
4+
5+
Nick Walkden, June 2015
6+
nick.walkden@ccfe.ac.uk
7+
8+
"""
9+
10+
import os
11+
from copy import copy
12+
13+
14+
class BOUTOptions(object):
15+
"""Class to store and interact with options from BOUT++
16+
17+
Parameters
18+
----------
19+
inp_path : str, optional
20+
Path to BOUT++ options file
21+
22+
Examples
23+
--------
24+
25+
Instantiate with
26+
27+
>>> myOpts = BOUTOptions()
28+
>>> myOpts.read_inp('path/to/input/file')
29+
30+
or
31+
32+
>>> myOpts = BOUTOptions('path/to/input/file')
33+
34+
To get a list of sections use
35+
36+
>>> section_list = myOpts.list_sections
37+
>>> # Also print to screen:
38+
>>> section_list = myOpts.list_sections(verbose=True)
39+
40+
Each section of the input is stored as a dictionary attribute so
41+
that, if you want all the settings in the section [ddx]:
42+
43+
>> ddx_opt_dict = myOpts.ddx
44+
45+
and access individual settings by
46+
47+
>>> ddx_setting = myOpts.ddx['first']
48+
49+
Any settings in BOUT.inp without a section are stored in
50+
51+
>>> root_dict = myOpts.root
52+
53+
TODO
54+
----
55+
- Merge this and BoutOptionsFile or replace with better class
56+
57+
"""
58+
59+
def __init__(self, inp_path=None):
60+
self._sections = ["root"]
61+
62+
for section in self._sections:
63+
super(BOUTOptions, self).__setattr__(section, {})
64+
65+
if inp_path is not None:
66+
self.read_inp(inp_path)
67+
68+
def read_inp(self, inp_path=""):
69+
"""Read a BOUT++ input file
70+
71+
Parameters
72+
----------
73+
inp_path : str, optional
74+
Path to the input file (default: current directory)
75+
76+
"""
77+
78+
filename = os.path.join(inp_path, "BOUT.inp")
79+
try:
80+
inpfile = open(filename, "r")
81+
except OSError:
82+
raise TypeError(f"ERROR: Could not read file {filename}")
83+
84+
current_section = "root"
85+
inplines = inpfile.read().splitlines()
86+
# Close the file after use
87+
inpfile.close()
88+
for line in inplines:
89+
# remove white space
90+
line = line.replace(" ", "")
91+
92+
if len(line) > 0 and line[0] != "#":
93+
# Only read lines that are not comments or blank
94+
if "[" in line:
95+
# Section header
96+
section = line.split("[")[1].split("]")[0]
97+
current_section = copy(section)
98+
if current_section not in self._sections:
99+
self.add_section(current_section)
100+
101+
elif "=" in line:
102+
# option setting
103+
attribute = line.split("=")[0]
104+
value = line.split("=")[1].split("#")[0]
105+
value = value.replace("\n", "")
106+
value = value.replace("\t", "")
107+
value = value.replace("\r", "")
108+
value = value.replace('"', "")
109+
self.__dict__[copy(current_section)][copy(attribute)] = copy(value)
110+
else:
111+
pass
112+
113+
def add_section(self, section):
114+
"""Add a section to the options
115+
116+
Parameters
117+
----------
118+
section : str
119+
The name of a new section
120+
121+
TODO
122+
----
123+
- Guard against wrong type
124+
"""
125+
self._sections.append(section)
126+
super(BOUTOptions, self).__setattr__(section, {})
127+
128+
def remove_section(self, section):
129+
"""Remove a section from the options
130+
131+
Parameters
132+
----------
133+
section : str
134+
The name of a section to remove
135+
136+
TODO
137+
----
138+
- Fix undefined variable
139+
"""
140+
if section in self._sections:
141+
self._sections.pop(self._sections.index(section))
142+
super(BOUTOptions, self).__delattr__(section)
143+
else:
144+
print(f"WARNING: Section {section} not found.\n")
145+
146+
def list_sections(self, verbose=False):
147+
"""Return all the sections in the options
148+
149+
Parameters
150+
----------
151+
verbose : bool, optional
152+
If True, print sections to screen
153+
154+
TODO
155+
----
156+
- Better pretty-print
157+
"""
158+
if verbose:
159+
print("Sections Contained: \n")
160+
for section in self._sections:
161+
print("\t{section}\n")
162+
163+
return self._sections

0 commit comments

Comments
 (0)