casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
TablePlotQt4Agg.py
Go to the documentation of this file.
00001 import sys
00002 import pylab as pl
00003 #from Tkinter import *
00004 
00005 #defaults
00006 alg='hogbom'
00007 antennaid=-1
00008 archivefiles=False
00009 bandname=False
00010 blbased=False
00011 bptable=''
00012 caltable=False
00013 cell=[1,1]
00014 channels=[]
00015 complist=''
00016 comptype='P'
00017 constpb=0.4
00018 correlations=''
00019 datacolumn='CORRECTED_DATA'
00020 diskwrite=False
00021 fieldid=-1
00022 file=''
00023 fitmode='subtract'
00024 fitorder=0
00025 fitsfile=False
00026 fixpar=[]
00027 fluxdensity=-1
00028 fluxtable=False
00029 fontsize=15.
00030 freqdep=False
00031 freqtol=150000.
00032 gain=0.1
00033 gaincurve=True
00034 gainselect=''
00035 gaintable=''
00036 highres=False
00037 imagename=False
00038 imsize=[256,256]
00039 incremental=False
00040 iteration=''
00041 lowres=False
00042 mask=['']
00043 mfalg='mfclark'
00044 minpb=0.1
00045 mode='none'
00046 model=''
00047 msselect=''
00048 nchan=-1
00049 niter=0
00050 opacity=0.0001
00051 outputvis=False
00052 overplot=False
00053 plotsymbol=','
00054 refant=-1
00055 reference=[]
00056 region=[0.0]
00057 rmode='none'
00058 robust=0.0
00059 scaletype='PBCOR'
00060 sourcepar=[1,0,0]
00061 subplot=111
00062 solint=0.0
00063 spwid=-1
00064 standard='Perley-Taylor 99'
00065 start=0
00066 step=1
00067 stokes='I'
00068 threshold=0.0
00069 timebin='-1s'
00070 timerange=''
00071 title=''
00072 transfer=[]
00073 verbose = False
00074 vis = False
00075 weighting='natural'
00076 width=1
00077 windowsize=1.0
00078 xaxis=False
00079 xlabels=''
00080 yaxis='amp'
00081 ylabels=''
00082 
00083 class Cursors:  #namespace
00084     HAND, POINTER, SELECT_REGION, MOVE = range(4)
00085 cursors = Cursors()
00086 
00087 
00088 class PlotFlag:   
00089 
00090     def __init__(self,mm,TPLP):
00091         self.tb = mm.toolbar;
00092         self.TPLP = TPLP;
00093         self.canvas = self.tb.canvas;
00094 
00095         self.lastrect = None;
00096         self.flaglist = [];
00097         self.flagregionlist = [];
00098         self.axeslist = [];
00099 
00100         self.region = None;
00101         self.kpress = None;
00102         self.panel = 0;
00103         self.rows = 0;
00104         self.cols = 0;
00105         
00106         # a dict from axes index to a list of view limits
00107 #self._views = Stack()
00108 #        self._positions = Stack()  # stack of subplot positions
00109 #        self._xypress = None  # the  location and axis info at the time of the press
00110 #        self._idPress = None
00111 #        self._idRelease = None
00112 #        self._active = None
00113 #        self._lastCursor = None
00114 #        self._init_toolbar()
00115 #        self._idDrag=self.canvas.mpl_connect('motion_notify_event', self.mouse_move)
00116 #        self._button_pressed = None # determined by the button pressed at start
00117 #        self.mode = ''  # a mode string for the status bar
00118 #        self.set_history_buttons()
00119         
00120 
00121     def press_flag(self, event):
00122         'the press mouse button in flag mode callback'
00123         if event.button == 1:
00124             self.tb._button_pressed=1
00125         elif  event.button == 3:
00126             self.tb._button_pressed=3
00127         else:
00128             self.tb._button_pressed=None
00129             return
00130 
00131         x, y = event.x, event.y
00132         self.panel = event.inaxes._num
00133         self.rows = event.inaxes._rows
00134         self.cols = event.inaxes._cols
00135 
00136         # push the current view to define home if stack is empty
00137         if self.tb._views.empty(): self.tb.push_current()
00138 
00139         for i, a in enumerate(self.tb.canvas.figure.get_axes()):
00140             if event.inaxes==a and event.inaxes.get_navigate():
00141                 xmin, xmax = a.get_xlim()
00142                 ymin, ymax = a.get_ylim()
00143                 lim = xmin, xmax, ymin, ymax
00144                 self.tb._xypress = x, y, a, i, lim, a.transData.deepcopy()
00145                 self.region = event.xdata, event.ydata
00146 
00147                 break
00148         
00149         self.tb.canvas.mpl_disconnect(self.tb._idDrag)
00150         self.tb._idDrag=self.canvas.mpl_connect('motion_notify_event', self.flag_mouse_move)
00151         self.tb.press(event)
00152 
00153 
00154     def release_flag(self, event):
00155         'the release mouse button callback in flag mode'
00156         if self.tb._xypress is None: return
00157         x, y = event.x, event.y
00158 
00159 
00160         lastx, lasty, a, ind, lim, trans = self.tb._xypress
00161         lastxdata, lastydata = self.region
00162         
00163         # ignore singular clicks - 5 pixels is a threshold
00164         if abs(x-lastx)<5 or abs(y-lasty)<5:
00165             self.tb._xypress = None
00166             self.tb.release(event)
00167             self.tb.draw()
00168             return
00169 
00170         # mark rect
00171         lastx, lasty = a.transData.inverse_xy_tup( (lastx, lasty) )
00172         x, y = a.transData.inverse_xy_tup( (x, y) )
00173         Xmin,Xmax=a.get_xlim()
00174         Ymin,Ymax=a.get_ylim()
00175 
00176         if Xmin < Xmax:
00177             if x<lastx:  xmin, xmax = x, lastx
00178             else: xmin, xmax = lastx, x
00179             if xmin < Xmin: xmin=Xmin
00180             if xmax > Xmax: xmax=Xmax
00181         else:
00182             if x>lastx:  xmin, xmax = x, lastx
00183             else: xmin, xmax = lastx, x
00184             if xmin > Xmin: xmin=Xmin
00185             if xmax < Xmax: xmax=Xmax
00186 
00187         if Ymin < Ymax:
00188             if y<lasty:  ymin, ymax = y, lasty
00189             else: ymin, ymax = lasty, y
00190             if ymin < Ymin: ymin=Ymin
00191             if ymax > Ymax: ymax=Ymax
00192         else:
00193             if y>lasty:  ymin, ymax = y, lasty
00194             else: ymin, ymax = lasty, y
00195             if ymin > Ymin: ymin=Ymin
00196             if ymax < Ymax: ymax=Ymax
00197 
00198         px1,py1 = a.transData.xy_tup( (xmin, ymin) )
00199         px2,py2 = a.transData.xy_tup( (xmax, ymax) )
00200             
00201         self.draw_rect(px1, py1, px2, py2, xmin, ymin, xmax, ymax, a)
00202         print "Region to Flag on panel ", self.rows, self.cols, self.panel+1, ": ", xmin, ymin , "->" , xmax, ymax;
00203                 
00204         #if a.get_aspect() == 'equal': a.set_aspect('equal',True)
00205         #self.tb.draw()
00206         self.tb._xypress = None
00207         self.tb._button_pressed == None
00208 
00209         self.tb.push_current()
00210         self.tb.canvas.mpl_disconnect(self.tb._idDrag)
00211         self.tb._idDrag=self.canvas.mpl_connect('motion_notify_event', self.tb.mouse_move)
00212         self.tb.release(event)
00213         
00214         #self.TPLP.clickCoords([xmin,ymin,xmax,ymax])
00215 
00216 
00217     def flag_mouse_move(self, event):
00218         #print 'mouse_move', event.button
00219 
00220         if not event.inaxes or not self.tb._active:
00221             if self.tb._lastCursor != cursors.POINTER:
00222                 self.tb.set_cursor(cursors.POINTER)
00223                 self.tb._lastCursor = cursors.POINTER
00224         else:
00225             if self.tb._active=='ZOOM':
00226                 if self.tb._lastCursor != cursors.SELECT_REGION:
00227                     self.tb.set_cursor(cursors.SELECT_REGION)
00228                     self.tb._lastCursor = cursors.SELECT_REGION
00229                 if self.tb._xypress is not None:
00230                     x, y = event.x, event.y
00231                     lastx, lasty, a, ind, lim, trans= self.tb._xypress
00232                     self.tb.draw_rubberband(event, x, y, lastx, lasty)
00233             elif self.tb._active=='FLAG':
00234                 if self.tb._lastCursor != cursors.SELECT_REGION:
00235                     self.tb.set_cursor(cursors.SELECT_REGION)
00236                     self.tb._lastCursor = cursors.SELECT_REGION
00237                 if self.tb._xypress is not None:
00238                     x, y = event.x, event.y
00239                     lastx, lasty, a, ind, lim, trans= self.tb._xypress
00240                     self.tb.draw_rubberband(event, x, y, lastx, lasty)
00241                     #print lastx,lasty,"->", x,y
00242             elif (self.tb._active=='PAN' and
00243                   self.tb._lastCursor != cursors.MOVE):
00244                 self.tb.set_cursor(cursors.MOVE)
00245 
00246                 self.tb._lastCursor = cursors.MOVE
00247 
00248         if event.inaxes and event.inaxes.get_navigate():
00249 
00250             try: s = event.inaxes.format_coord(event.xdata, event.ydata)
00251             except ValueError: pass
00252             except OverflowError: pass            
00253             else:
00254                 if len(self.tb.mode):
00255                     self.tb.set_message('%s : %s' % (self.tb.mode, s))
00256                 else:
00257                     self.tb.set_message(s)
00258         else: self.tb.set_message(self.tb.mode)
00259 
00260 
00261     def press_key(self,event):
00262         #print event.key, event.xdata, event.ydata
00263         if event.key=='alt' and len(self.flaglist)>0:
00264            #self.tb.canvas._tkcanvas.delete(self.flaglist[len(self.flaglist)-1]);
00265            self.flaglist.pop();
00266            self.flagregionlist.pop();
00267            self.axeslist.pop();
00268            #print self.flagregionlist
00269 
00270 
00271     def flag(self, *args):
00272         'activate flag mode'
00273         if self.tb._active == 'FLAG':
00274             self.tb._active = None
00275         else:
00276             self.tb._active = 'FLAG'
00277             self.erase_rects();
00278 
00279         if self.tb._idPress is not None:
00280             self.tb._idPress=self.tb.canvas.mpl_disconnect(self.tb._idPress)
00281             self.tb.mode = ''
00282 
00283         if self.tb._idRelease is not None:
00284             self.tb._idRelease=self.tb.canvas.mpl_disconnect(self.tb._idRelease)
00285             self.tb.mode = ''
00286 
00287         if  self.tb._active:
00288             self.tb._idPress = self.tb.canvas.mpl_connect('button_press_event', self.press_flag)
00289             self.tb._idRelease = self.tb.canvas.mpl_connect('button_release_event', self.release_flag)
00290             self.tb.mode = 'Flag mode'
00291 
00292         self.tb.set_message(self.tb.mode)
00293 
00294 
00295 
00296 
00297     def draw_rect(self, x0, y0, x1, y1, x0data, y0data, x1data, y1data,a):
00298         self.flagregionlist.append([x0data,y0data,x1data,y1data,self.panel+1,self.rows,self.cols]);
00299         self.axeslist.append(a);
00300         height = self.tb.canvas.figure.bbox.height()
00301         y0 =  height-y0
00302         y1 =  height-y1
00303         #self.tb.canvas._tkcanvas.delete(self.tb.lastrect)
00304         #self.lastrect = self.tb.canvas.drawRectangle([x0, y0, x1, y1])
00305         self.lastrect = pl.plot([x0data, x1data, x1data, x0data, x0data],[y0data, y0data, y1data, y1data, y0data])
00306         self.flaglist.append(self.lastrect);
00307 
00308     def redraw_rects(self):
00309         #for q in self.flaglist:
00310           #self.tb.canvas._tkcanvas.delete(q);
00311         self.flaglist = [];
00312         
00313         #for q in self.flagregionlist:
00314         for z in range(0,len(self.flagregionlist)):
00315           q = self.flagregionlist[z];
00316           a = self.axeslist[z];
00317           x0=q[0]; y0=q[1]; x1=q[2]; y1=q[3];
00318           # map to new zoom limits (current fig co-ords)
00319           px0,py0 = a.transData.xy_tup( (x0, y0) )
00320           px1,py1 = a.transData.xy_tup( (x1, y1) )
00321 
00322           height = self.tb.canvas.figure.bbox.height()
00323           py0 =  height-py0
00324           py1 =  height-py1
00325           self.lastrect = pl.plot([x0data, x1data, x1data, x0data, x0data],[y0data, y0data, y1data, y1data, y0data])
00326           self.flaglist.append(self.lastrect);
00327 
00328           
00329     def erase_rects(self):
00330         #print "erase rects"
00331         #for q in self.flaglist:
00332           #self.tb.canvas._tkcanvas.delete(q);
00333         self.flaglist = [];
00334         self.flagregionlist = [];
00335         self.axeslist = [];
00336 
00337     def close(self):
00338         #self.bb.forget();
00339         #if self.tb._active == 'ZOOM':
00340         #    self.erase_rects();
00341         #if self.tb._active == 'PAN':
00342         #    self.erase_rects();
00343         
00344         #print "number of flag regions :",len(self.flagregionlist)
00345             
00346         if self.tb._active == 'FLAG':
00347             self.tb._active = None
00348         
00349         self.erase_rects();
00350 
00351         if self.tb._idPress is not None:
00352             self.tb._idPress=self.tb.canvas.mpl_disconnect(self.tb._idPress)
00353             self.tb.mode = ''
00354 
00355         if self.tb._idRelease is not None:
00356             self.tb._idRelease=self.tb.canvas.mpl_disconnect(self.tb._idRelease)
00357             self.tb.mode = ''
00358 
00359         if self.kpress is not None:
00360             self.kpress = self.tb.canvas.mpl_disconnect(self.kpress)
00361 
00362             
00363         self.tb.set_message(self.tb.mode)
00364 
00365 
00366 
00367     def start(self):
00368         ##  Read the current xylims and in plotdata (of a flagdata), force these xylims.
00369         ##  Do similar thing for getting the flag boxes to rescale when zoomed.
00370         #self.bb = self.tb._Button(text="FlagButton",file='hand.ppm',command=self.flag)
00371         if self.tb._active == 'ZOOM':
00372             self.tb.zoom();
00373         if self.tb._active == 'PAN':
00374             self.tb.pan();
00375         self.tb._active = 'FLAG'
00376         self.tb._idPress = self.tb.canvas.mpl_connect('button_press_event', self.press_flag)
00377         self.tb._idRelease = self.tb.canvas.mpl_connect('button_release_event', self.release_flag)
00378         self.tb.mode = 'Flag mode'
00379         self.kpress = self.tb.canvas.mpl_connect('key_press_event', self.press_key)
00380         self.tb.set_message(self.tb.mode)
00381 
00382 
00383 
00384 
00385 #mm = pl.get_current_fig_manager();
00386 #pf = PlotFlag(mm);
00387 #pf.start()
00388 
00389 
00390   ##  /opt/local/stow/python-2.4.2/share/matplotlib/
00391