summaryrefslogtreecommitdiff
path: root/src/file.py
blob: 01841355e548aded320dc7f1812ce5a0b1e1d241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import re, sys, traceback, json
import phys

file_format_version = 6

def save_system(path, system):
	ser = dict()
	ser["input_intensity"] = system.input_intensity
	ser["version"] = file_format_version

	ser["elements"] = []
	for pol in system.elements:
		el = {
			"name": pol.name,
			"phase_retardation": pol.phase_retardation,
			"enable": pol.enable,
			"angle": pol.angle,
			"delta": pol.delta,
			"ref": pol.ref,
			"t1": pol.t1,
			"t2": pol.t2
		}

		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.input_intensity = float(ser["input_intensity"])

	for el in ser["elements"]:
		pol = phys.Polarizer()
		pol.phase_retardation = str(el["phase_retardation"])
		pol.name = str(el["name"])
		pol.enable = bool(el["enable"])
		pol.angle = float(el["angle"])
		pol.delta = float(el["delta"])
		pol.ref = el["ref"]
		pol.t1 = float(el["t1"])
		pol.t2 = float(el["t2"])
		system.elements.append(pol)

	return system