#!/usr/bin/python import sys import os import re # Regular expressions - for removing comments import odict #ordered dictionary import copy import Gnuplot, Gnuplot.funcutils import string import time import math import cmath import random gnuplot = Gnuplot.Gnuplot() keys = ["wavelength","angle", "psi", "delta", "uncertainty_psi", "uncertainty_delta"] def getData(fileName): dataFile = open(fileName, "r") #Format is #wavelength angle psi delta uncertainty_psi uncertainty_delta title = dataFile.readline().strip() #Skip lines before data for i in range(1,4): dataFile.readline() data = [] line = dataFile.readline() while line != "": values = line.split(" ") if len(values) != len(keys): sys.stderr.write("Number of on line \""+line.strip() + "\" doesn't match number of keys ("+str(len(keys))+") " + str(keys) +"\n") sys.exit(1) #sys.stdout.write("Debug: Read line \""+line.strip()+"\"\n") point = {} for i in range(0, len(keys)): point.update({keys[i]:values[i]}) data.append(point) #data.append({"wavelength":values[0], "angle":values[1], "psi":values[2], "delta":values[3], "uncertainty_psi":values[4], "uncertainty_delta":values[4]}) line = dataFile.readline() return {"title":title, "data":data} def extractData(line, x, y, conditions): for condition in conditions: [name, value] = condition.split("=") if keys.count(name) <= 0: sys.stderr.write("Unknown key \""+name+"\" for condition " + str(condition) + "\n") sys.exit(1) values = value.split(",") match = False for v in values: #sys.stdout.write(line[name] + " .vs. " + str(v) + "\n") if float(line[name]) == float(v): match = True break if match == False: #sys.stdout.write("Condition \""+str(condition)+"\" not satisfied ("+str(line[name]) + " .vs. " + str(values[0])+ "\n") return None #sys.stdout.write("Condition satisfied\n") return [line[x], line[y]] if __name__ == "__main__": indexStart = 1 state = "--raw" for arg in sys.argv: if arg[0] == '-' and arg[1] == '-': indexStart += 1 state = arg #sys.stdout.write("State is " + state + "\n") if len(sys.argv) - indexStart < 3: sys.stderr.write("Usage: " +sys.argv[0] + " [options] datafile x y [conditions] outfile\n") sys.exit(1) datafile = sys.argv[indexStart] x = sys.argv[indexStart+1] y = sys.argv[indexStart+2] error = False for k in [x,y]: if keys.count(k) <= 0: sys.stderr.write("Unknown key \""+k+"\"\n") error = True if error: sys.exit(1) conditions = sys.argv[indexStart+3:len(sys.argv)-1] sys.stdout.write("Debug: Conditions are " + str(conditions) + "\n") for condition in conditions: sys.stdout.write(" Debug: Condition " + str(condition.split("=")) + "\n") outfile = sys.argv[len(sys.argv)-1] results = getData(datafile) toPlot = map(lambda e : extractData(e, x,y,conditions) , results["data"]) toPlot = filter(lambda e : e != None and len(e) == 2, toPlot) tmpFile = open(outfile+".tmp", "w") for point in toPlot: tmpFile.write(point[0] + " " + point[1] + "\n") tmpFile.close() if state == "--plot": command = "" command += "set term png size 960,480\n" command += "set output \"" + outfile + "\"\n" command += "set xlabel \"" + x + "\"\n" command += "set ylabel \"" + y + "\"\n" command += "set title \"" + results["title"] + "\"\n" command += "set key outside right\n" command += "plot \"" + outfile + ".tmp\" using 1:2 with points title \""+str(conditions)+"\"\n" command += "exit\n" tmpFile = open(outfile+".tmp.plt", "w") tmpFile.write(command) tmpFile.close() sys.stdout.write("Calling gnuplot now...\n") #sys.stdout.write("------------------------------------\n") os.system("gnuplot -p "+outfile+".tmp.plt") #2>/dev/null 1>/dev/null") #sys.stdout.write("------------------------------------\n") sys.stdout.write("gnuplot finished.\n") os.remove(outfile+".tmp") os.remove(outfile+".tmp.plt") sys.stdout.write("Done!\n") elif state == "--raw": os.system("mv " + outfile+".tmp " + outfile) sys.exit(0)