4 import re # Regular expressions - for removing comments
5 import odict #ordered dictionary
8 import Gnuplot, Gnuplot.funcutils
15 gnuplot = Gnuplot.Gnuplot()
17 keys = ["wavelength","angle", "psi", "delta", "uncertainty_psi", "uncertainty_delta"]
19 def GetData(fileName):
20 dataFile = open(fileName, "r")
22 #wavelength angle psi delta uncertainty_psi uncertainty_delta
24 title = dataFile.readline().strip()
26 #Skip lines before data
32 line = dataFile.readline()
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")
38 #sys.stdout.write("Debug: Read line \""+line.strip()+"\"\n")
40 for i in range(0, len(keys)):
41 point.update({keys[i]:values[i]})
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()
46 return {"title":title, "data":data}
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")
54 values = value.split(",")
57 #sys.stdout.write(line[name] + " .vs. " + str(v) + "\n")
58 if float(line[name]) == float(v):
62 #sys.stdout.write("Condition \""+str(condition)+"\" not satisfied ("+str(line[name]) + " .vs. " + str(values[0])+ "\n")
64 #sys.stdout.write("Condition satisfied\n")
65 return [line[x], line[y]]
68 if __name__ == "__main__":
75 if arg[0] == '-' and arg[1] == '-':
78 #sys.stdout.write("State is " + state + "\n")
81 if len(sys.argv) - indexStart < 3:
82 sys.stderr.write("Usage: " +sys.argv[0] + " [options] datafile x y [conditions] outfile\n")
85 datafile = sys.argv[indexStart]
86 x = sys.argv[indexStart+1]
87 y = sys.argv[indexStart+2]
91 if keys.count(k) <= 0:
92 sys.stderr.write("Unknown key \""+k+"\"\n")
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]
104 results = getData(datafile)
107 toPlot = map(lambda e : extractData(e, x,y,conditions) , results["data"])
108 toPlot = filter(lambda e : e != None and len(e) == 2, toPlot)
110 tmpFile = open(outfile+".tmp", "w")
112 tmpFile.write(point[0] + " " + point[1] + "\n")
115 if state == "--plot":
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"
126 tmpFile = open(outfile+".tmp.plt", "w")
127 tmpFile.write(command)
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")
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)