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

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