forked from rubel75/BerryPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathphase_difference.py
67 lines (47 loc) · 1.87 KB
/
pathphase_difference.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import os
import sys
import numpy
pathphase_filename = lambda casename : os.path.join(casename, casename + '.pathphase')
def parse_pathphase(line):
parts = [x.strip() for x in line.split(' ') if x]
return int(parts[0]), float(parts[1])
def print_usage():
print >> sys.stderr, "Usage: {0} case1.pathphase case2.pathphase".format(sys.argv[0])
print >> sys.stderr, "Computes the phase difference between case2 and case1 taking into account 2*pi wrapping."
if __name__ == "__main__":
if (len(sys.argv) != 3):
print_usage()
sys.exit(1)
phase_delta = {}
# Open input .pathphase files
f_unperturbed = open(sys.argv[1], 'r')
f_perturbed = open(sys.argv[2], 'r')
# First line tells us how many paths there are
n1 = int(f_unperturbed.readline().strip())
n2 = int(f_perturbed.readline().strip())
if n1 != n2:
raise RuntimeError('Number of paths in pathphase files differ.')
# Second line gives us the direction vector -- we can ignore this..
f_unperturbed.readline()
f_perturbed.readline()
# Take the difference for each path
for i in range(n1):
line_unperturbed = f_unperturbed.readline()
line_perturbed = f_perturbed.readline()
k1_u, phase_u = parse_pathphase(line_unperturbed)
k1_p, phase_p = parse_pathphase(line_perturbed)
if (k1_u != k1_p):
raise RuntimeError('Order of paths is not the same between two pathphase files.')
# Put phases on [0, 2pi)
phase_u += 2*numpy.pi
phase_p += 2*numpy.pi
# Unwrap phases
phase_u, phase_p = numpy.unwrap([phase_u, phase_p])
# Get phase difference of unwrapped phases
phase_delta[k1_u] = phase_p - phase_u
print phase_delta
# TODO print results to file
# Close input files
f_unperturbed.close()
f_perturbed.close()