Merge branch 'master' of git.ucc.asn.au:/matches/honours
[matches/honours.git] / research / ellipsometry / process.py~
1 #!/usr/bin/python
2 import sys
3 import os
4 import re # Regular expressions - for removing comments
5 import odict #ordered dictionary
6 import copy
7
8 import Gnuplot, Gnuplot.funcutils
9 import string
10 import time
11 import math
12 import cmath
13 import random
14
15 gnuplot = Gnuplot.Gnuplot()
16
17 keys = ["wavelength","angle", "psi", "delta", "uncertainty_psi", "uncertainty_delta"]
18
19 def getData(fileName):
20         dataFile = open(fileName, "r")
21         #Format is 
22         #wavelength     angle   psi     delta   uncertainty_psi uncertainty_delta
23
24         title = dataFile.readline().strip()
25         
26         #Skip lines before data
27         for i in range(1,4):
28                 dataFile.readline()
29
30         data = []
31         
32         line = dataFile.readline()
33         while line != "":
34                 values = line.split("   ")
35                 if len(values) != len(keys):
36                         sys.stderr.write("Number of on line \""+line.strip() + "\" doesn't match number of keys ("+str(len(keys))+") " + str(keys) +"\n")
37                         sys.exit(1)
38                 #sys.stdout.write("Debug: Read line \""+line.strip()+"\"\n")
39                 point = {}
40                 for i in range(0, len(keys)):
41                         point.update({keys[i]:values[i]})
42                 data.append(point)
43                 #data.append({"wavelength":values[0], "angle":values[1], "psi":values[2], "delta":values[3], "uncertainty_psi":values[4], "uncertainty_delta":values[4]})
44                 line = dataFile.readline()
45
46         return {"title":title, "data":data}
47
48 def extractData(line, x, y, conditions):
49         for condition in conditions:
50                 [name, value] = condition.split("=")
51                 if keys.count(name) <= 0:
52                         sys.stderr.write("Unknown key \""+name+"\" for condition " + str(condition) + "\n")
53                         sys.exit(1)
54                 values = value.split(",")
55                 match = False
56                 for v in values:
57                         #sys.stdout.write(line[name] + " .vs. " + str(v) + "\n")
58                         if float(line[name]) == float(v):
59                                 match = True
60                                 break
61                 if match == False:
62                         #sys.stdout.write("Condition \""+str(condition)+"\" not satisfied ("+str(line[name]) + " .vs. " + str(values[0])+ "\n")
63                         return None
64                 #sys.stdout.write("Condition satisfied\n")
65         return [line[x], line[y]]
66
67
68 if __name__ == "__main__":
69
70         
71
72         indexStart = 1
73         state = "--raw"
74         for arg in sys.argv:
75                 if arg[0] == '-' and arg[1] == '-':
76                         indexStart += 1
77                         state = arg
78         #sys.stdout.write("State is " + state + "\n")
79                         
80
81         if len(sys.argv) - indexStart < 3: 
82                 sys.stderr.write("Usage: " +sys.argv[0] + " [options] datafile x y [conditions] outfile\n")
83                 sys.exit(1)
84
85         datafile = sys.argv[indexStart]
86         x = sys.argv[indexStart+1]
87         y = sys.argv[indexStart+2]
88
89         error = False
90         for k in [x,y]:
91                 if keys.count(k) <= 0:
92                         sys.stderr.write("Unknown key \""+k+"\"\n")
93                         error = True
94         if error:
95                 sys.exit(1)
96                 
97                 
98         conditions = sys.argv[indexStart+3:len(sys.argv)-1]
99         sys.stdout.write("Debug: Conditions are " + str(conditions) + "\n")
100         for condition in conditions:
101                 sys.stdout.write("      Debug: Condition " + str(condition.split("=")) + "\n")  
102         outfile = sys.argv[len(sys.argv)-1]
103
104         results = getData(datafile)
105
106         
107         toPlot = map(lambda e : extractData(e, x,y,conditions) , results["data"])
108         toPlot = filter(lambda e : e != None and len(e) == 2, toPlot)
109
110         tmpFile = open(outfile+".tmp", "w")
111         for point in toPlot:
112                 tmpFile.write(point[0] + "      " + point[1] + "\n")
113         tmpFile.close()
114
115         if state == "--plot":
116                 command = ""
117                 command += "set term png size 960,480\n"
118                 command += "set output \"" + outfile + "\"\n"
119                 command += "set xlabel \"" + x + "\"\n"
120                 command += "set ylabel \"" + y + "\"\n"
121                 command += "set title \"" + results["title"] + "\"\n"
122                 command += "set key outside right\n"
123                 command += "plot \"" + outfile + ".tmp\" using 1:2 with points title \""+str(conditions)+"\"\n"
124                 command += "exit\n"
125
126                 tmpFile = open(outfile+".tmp.plt", "w")
127                 tmpFile.write(command)
128                 tmpFile.close()
129
130                 sys.stdout.write("Calling gnuplot now...\n")
131                 #sys.stdout.write("------------------------------------\n")
132                 os.system("gnuplot -p "+outfile+".tmp.plt") #2>/dev/null 1>/dev/null")
133                 #sys.stdout.write("------------------------------------\n")
134                 sys.stdout.write("gnuplot finished.\n")
135
136                 os.remove(outfile+".tmp")
137                 os.remove(outfile+".tmp.plt")
138                 sys.stdout.write("Done!\n")
139         elif state == "--raw":
140                 os.system("mv " + outfile+".tmp " + outfile)
141         sys.exit(0)
142         
143
144         
145         
146

UCC git Repository :: git.ucc.asn.au