#! /usr/bin/env python import ppgplot, Numeric, math import sys, string, fileinput # import Scientific #from Scientific.Functions.LeastSquares import leastSquaresFit # Abbreviate the fit command #fit=Scientific.Functions.LeastSquares.leastSquaresFit # define linear function for use in fits def linear(parameters, x): a=parameters[0] b=parameters[1] return a*x+b # Function to work out avg, sx and sigmax from a list of numbers def stat(numbers): sum=0 # Calculate avg, sx and sigmax if len(numbers)>1: for i in numbers: sum=sum+i # Store total sum for later total=sum avg=sum/len(numbers) sum=0 for i in numbers: sum=sum+(i-avg)**2 sx=math.sqrt(sum/(len(numbers)-1)) sigmax=math.sqrt(sum/len(numbers)) else: avg=numbers[0] sx=0 sigmax=0 sum=numbers[0] # Calculate mean numbers.sort() if (len(numbers) % 2)==1: mean=numbers[(len(numbers)-1)/2] else: mean=(numbers[(len(numbers)/2)-1]+numbers[(len(numbers)/2)])/2 # Return results return avg, sx, sigmax, total, mean # print help def print_help(): print """ \n Plotit.py by Enno Middelberg, October 2003 \n Your choice:\n h - print this message ---- Flagging ---- a - delete above b - delete below l - delete to left r - delete to right q - delete rectangle k - keep rectangle (delete outside) o - delete nearest point ---- Analyze data ---- p - print nearest point t - statistics of points in rectangle i - plot histogram of x data j - plot histogram of y data f - fit linear function to data ---- Change plot ---- s - re-scale z - zoom in <> - change plot symbol x - exit""" # return coordinates of rectangle drawn by user # arguments: anchor of rectangle (first corner's coords) def get_rectangle(x1, y1): action=ppgplot.pgband(2, 0, x1, y1) x2=action[0] y2=action[1] if x2xmax: xmax=i # find plot boundaries - ymin and ymax ymin=yarray[0] ymax=yarray[0] for i in yarray: if iymax: ymax=i # Store data for later use dataxmin=xmin dataxmax=xmax dataymin=ymin dataymax=ymax # add and subtract 5% as extra margin xmin=xmin-0.05*(xmax-xmin) xmax=xmax+0.05*(xmax-xmin) # add and subtract 5% as extra margin ymin=ymin-0.05*(ymax-ymin) ymax=ymax+0.05*(ymax-ymin) print "\n New plot dimensions: xmin=%.1f, xmax=%.1f, ymin=%.1f, ymax=%.1f" % (xmin, xmax, ymin, ymax) # initialize plot window ppgplot.pgenv(xmin, xmax, ymin, ymax, 0, 1) ppgplot.pglab(xlabel, ylabel, title) # plot data ppgplot.pgpt(xarray, yarray, pttype) # plot hollow 1 sigma rectangle with cross in it ppgplot.pgsfs(2) ppgplot.pgrect(rect_xmin, rect_xmax, rect_ymin, rect_ymax) # start flagging action=ppgplot.pgband(7, 0, 0, 0) # report nearest data point if action[2]=="p" or action[2]=="P": x1=action[0] y1=action[1] min=math.sqrt((xmax-xmin)**2+(ymax-ymin)**2) for i in range(len(xarray)): dist=math.sqrt((xarray[i]-x1)**2+(yarray[i]-y1)**2) if dist=ylim: xcopy.append(xarray[i]) ycopy.append(yarray[i]) xarray=Numeric.array(xcopy) yarray=Numeric.array(ycopy) # delete right of vertical line if action[2]=="r": xlim=action[0] print "\n Flag right of x=%f" % xlim xcopy=[] ycopy=[] for i in range(len(xarray)): if xarray[i]<=xlim: xcopy.append(xarray[i]) ycopy.append(yarray[i]) xarray=Numeric.array(xcopy) yarray=Numeric.array(ycopy) # delete left of vertical line if action[2]=="l": xlim=action[0] print "\n Flag left of x=%f" % xlim xcopy=[] ycopy=[] for i in range(len(xarray)): if xarray[i]>=xlim: xcopy.append(xarray[i]) ycopy.append(yarray[i]) xarray=Numeric.array(xcopy) yarray=Numeric.array(ycopy) # delete inside or outside of rectangle if action[2]=="q" or action[2]=="k": key=action[2] (x1, x2, y1, y2)=get_rectangle(action[0], action[1]) print "\n Limits: x=%f and x=%f" % (x1, x2) print " y=%f and y=%f" % (y1, y2) xcopy=[] ycopy=[] if key=="q": for i in range(len(xarray)): if not (x1<=xarray[i]<=x2 and y1<=yarray[i]<=y2): xcopy.append(xarray[i]) ycopy.append(yarray[i]) if key=="k": for i in range(len(xarray)): if x1<=xarray[i]<=x2 and y1<=yarray[i]<=y2: xcopy.append(xarray[i]) ycopy.append(yarray[i]) xarray=Numeric.array(xcopy) yarray=Numeric.array(ycopy) # clear "action" or last click it will be evaluated in next loop action=(0,0,"") # zoom in if action[2]=="z": (xmin, xmax, ymin, ymax)=get_rectangle(action[0], action[1]) print "\n New plot dimensions: xmin=%.1f, xmax=%.1f, ymin=%.1f, ymax=%.1f" % (xmin, xmax, ymin, ymax) # clear "action" or last click it will be evaluated in next loop action=(0,0,"") # decrease plot symbol number if action[2]=="<": pttype=pttype-1 # increase plot symbol number if action[2]==">": pttype=pttype+1 # delete nearest point (click left=="A") if action[2]=="o" or action[2]=="A": x1=action[0] y1=action[1] min=math.sqrt((xmax-xmin)**2+(ymax-ymin)**2) for i in range(len(xarray)): dist=math.sqrt((xarray[i]-x1)**2+(yarray[i]-y1)**2) if dist