summaryrefslogtreecommitdiff
path: root/src/file.py
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2019-04-28 17:29:18 +0200
committerPaweł Redman <pawel.redman@gmail.com>2019-04-28 19:07:20 +0200
commit52a8a95a8553f5b2a1224db474e6cf1d649533c8 (patch)
tree003e0c94839b6daebef19049c75ba0ed43082a7b /src/file.py
Initial commit
Diffstat (limited to 'src/file.py')
-rw-r--r--src/file.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/file.py b/src/file.py
new file mode 100644
index 0000000..4285207
--- /dev/null
+++ b/src/file.py
@@ -0,0 +1,44 @@
+import re, sys, traceback, json
+import phys
+
+file_format_version = 1
+
+def save_system(path, system):
+ ser = dict()
+ ser["version"] = file_format_version
+
+ ser["ignore"] = system.ignore
+ ser["elements"] = []
+ for pol in system.elements:
+ el = {
+ "type": pol.type,
+ "angle": pol.angle,
+ "delta": pol.delta,
+ "ref": pol.ref
+ }
+
+ ser["elements"].append(el)
+
+ with open(path, "w") as fd:
+ fd.write(json.dumps(ser))
+
+def open_system(path):
+ system = phys.System()
+
+ with open(path, "r") as fd:
+ ser = json.loads(fd.read())
+
+ if ser["version"] != file_format_version:
+ raise ValueError("Bad file version: expected %d, found %d" \
+ % (file_format_version, ser["version"]))
+
+ system.ignore = ser["ignore"]
+
+ for el in ser["elements"]:
+ pol = phys.Polarizer(str(el["type"]))
+ pol.angle = float(el["angle"])
+ pol.delta = float(el["delta"])
+ pol.ref = el["ref"]
+ system.elements.append(pol)
+
+ return system \ No newline at end of file