gui.py~ 9.14 KB
Newer Older
Jérémy Baudry's avatar
Jérémy Baudry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
#!/usr/bin/python
from gi.repository import Gtk
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
 


class Handler:
    def __init__(self):
        self.gladefile = "/home/jeremy/Bureau/GUI/wim.glade"
        self.builder = Gtk.Builder()
        self.builder.add_from_file(self.gladefile)
    	self.builder.connect_signals(self)
    	self.window = self.builder.get_object("window1")
    	self.window.show()
	self.hscale1 = self.builder.get_object("hscale1")         
	self.hscale2 = self.builder.get_object("hscale2")
	self.thick = self.builder.get_object("checkbutton1")
	self.conc = self.builder.get_object("checkbutton2")
	self.meanfloe = self.builder.get_object("checkbutton3")
	self.FSTD = self.builder.get_object("checkbutton5")

	

	
  
    def disable(self,widget,event):
	return True

    def on_destroy(self,closewindow):
        closewindow.hide()

    def on_window1_destroy(self,window):
        Gtk.main_quit()

#===================choose_file_button===================================
    def on_filechooserbutton1_file_set(self,choosefile):
	global data
	data = choosefile.get_filename()
	self.readnc(data)
	ad1 = Gtk.Adjustment(25,0,99,1,1, 0)
        self.hscale1.set_adjustment(adjustment=ad1)
	ad2 = Gtk.Adjustment(25,0,99,1,1, 0)
        self.hscale2.set_adjustment(adjustment=ad2)	
	global xvalue
	xvalue=x_axis[0]
	global timevalue
	timevalue=time[0]

	self.windowthick = Gtk.Window()
	self.windowthick.set_default_size(400,400)
	self.windowthick.connect("delete-event",self.disable)
	self.fig1=plt.figure(1)
	canvas1 = FigureCanvas(self.fig1)
	canvas1.set_size_request(100,100)
	self.axthick = self.fig1.add_subplot(111)
        self.axthick.plot(x_axis,thickness)
	self.vline1=self.axthick.axvline(x_axis[xvalue],color='k',ls='dashed')
	self.hline1=self.axthick.axhline(thickness[xvalue],color='k',ls='dashed')
	self.pointer=self.axthick.plot(x_axis[xvalue],thickness[xvalue],'ro',markersize=10.0)[0]
	self.axthick.set_ylabel('Mean ice thickness')
	self.windowthick.set_title('Mean ice thickness')
	self.vbox = Gtk.VBox()
	self.windowthick.add(self.vbox)
	self.vbox.pack_start(canvas1,True,True, 0)
	toolbar = NavigationToolbar(canvas1,self.windowthick)
	self.vbox.pack_start(toolbar, False, False,0)

	self.windowconc = Gtk.Window()
	self.windowconc.set_default_size(400,400)
	self.windowconc.set_title('Ice concentration')
	self.windowconc.connect("delete-event",self.disable)
	self.fig3=plt.figure(3)
	canvas3 = FigureCanvas(self.fig3)
	canvas3.set_size_request(100,100)
	self.axconc = self.fig3.add_subplot(111)
	self.axconc.plot(x_axis,ice_concentration)
	self.vline2=self.axconc.axvline(x_axis[xvalue],color='k',ls='dashed')
	self.hline2=self.axconc.axhline(ice_concentration[xvalue],color='k',ls='dashed')
	self.pointer2=self.axconc.plot(x_axis[xvalue],ice_concentration[xvalue],'ro',markersize=10.0)[0]
	self.axconc.set_ylabel('Ice concentration')
	self.vboxconc = Gtk.VBox()
	self.windowconc.add(self.vboxconc)
	self.vboxconc.pack_start(canvas3,True,True, 0)
	toolbar3 = NavigationToolbar(canvas3,self.windowconc)
	self.vboxconc.pack_start(toolbar3, False, False,0)

	self.windowmeanfloe = Gtk.Window()
	self.windowmeanfloe.set_default_size(400,400)
	self.windowmeanfloe.set_title('Mean Floe size')
	self.windowmeanfloe.connect("delete-event",self.disable)
	self.fig4=plt.figure(4)
	canvas4 = FigureCanvas(self.fig4)
	canvas4.set_size_request(100,100)
	self.axmeanfloe = self.fig4.add_subplot(111)
	self.axmeanfloe.plot(x_axis,D_ave)
	self.vline3=self.axmeanfloe.axvline(x_axis[xvalue],color='k',ls='dashed')
	self.hline3=self.axmeanfloe.axhline(D_ave[xvalue],color='k',ls='dashed')
	self.pointer3=self.axmeanfloe.plot(x_axis[xvalue],D_ave[xvalue],'ro',markersize=10.0)[0]
	self.axconc.set_ylabel('Mean floe size')
	self.vboxmeanfloe = Gtk.VBox()
	self.windowmeanfloe.add(self.vboxmeanfloe)
	self.vboxmeanfloe.pack_start(canvas4,True,True, 0)
	toolbar4 = NavigationToolbar(canvas4,self.windowmeanfloe)
	self.vboxmeanfloe.pack_start(toolbar4, False, False,0)

	self.windowfstd = Gtk.Window()
	self.windowfstd.set_default_size(400,400)
	self.windowfstd.set_title('FSTD')
	self.windowfstd.connect("delete-event",self.disable)
	self.fig2=plt.figure(2)
	canvas2 = FigureCanvas(self.fig2)
	canvas2.set_size_request(100,100)
	self.axfstd= self.fig2.add_subplot(111)
	FSD2=FSD[:,:,xvalue,timevalue]
	ITD2=ITD[:,xvalue]
	global FSTD
	FSTD=np.zeros((50,60))
	for i in xrange(0,49):
	 FSTD[i,:]=FSD2[i,:]*ITD2[i]
	self.fstdplot=self.axfstd.imshow(FSTD, interpolation='nearest', cmap=cm.coolwarm)
	self.cbar = self.fig2.colorbar(self.fstdplot)
	self.cbar.set_clim(0,0.1)
	self.vboxfstd = Gtk.VBox()
	self.windowfstd.add(self.vboxfstd)
	self.vboxfstd.pack_start(canvas2,True,True, 0)
	toolbar2 = NavigationToolbar(canvas2,self.windowfstd)
	self.vboxfstd.pack_start(toolbar2, False, False,0)


	if self.thick.get_active():
	 self.plot_thick(x_axis,thickness,xvalue)
	if self.conc.get_active():
	 self.plot_conc(x_axis,ice_concentration,xvalue)
	if self.meanfloe.get_active():
	 self.plot_meanfloe(x_axis,D_ave,xvalue)
	if self.FSTD.get_active():
         self.plot_FSTD(xvalue,timevalue,Fsize,hcat,FSD,ITD)


#======================scale_buttons======================================	
    def on_hscale1_value_changed(self,timescale):	
	timevalue=timescale.get_value()
	if self.FSTD.get_active():
         self.plot_FSTD(xvalue,timevalue,Fsize,hcat,FSD,ITD)
	
    def on_hscale2_value_changed(self,xscale):
        xvalue=xscale.get_value()
	if self.thick.get_active():
         self.plot_thick(x_axis,thickness,xvalue)
	if self.FSTD.get_active():
         self.plot_FSTD(xvalue,timevalue,Fsize,hcat,FSD,ITD)
	if self.conc.get_active():
	 self.plot_conc(x_axis,ice_concentration,xvalue)
	if self.meanfloe.get_active():
	 self.plot_meanfloe(x_axis,D_ave,xvalue)

#====================toggle_button=========================================
    def on_checkbutton1_toggled(self,but1):
	if but1.get_active():
	 self.plot_thick(x_axis,thickness,xvalue)
	else:
	 self.on_destroy(self.windowthick)

    def on_checkbutton2_toggled(self,butconc):
	if butconc.get_active():
	 self.plot_conc(x_axis,ice_concentration,xvalue)
	else:
	 self.on_destroy(self.windowconc)

    def on_checkbutton3_toggled(self,butmeanfloe):
	if butmeanfloe.get_active():
	 self.plot_meanfloe(x_axis,D_ave,xvalue)
	else:
	 self.on_destroy(self.windowmeanfloe)

    def on_checkbutton5_toggled(self,butFSTD):
	if butFSTD.get_active():
	 self.plot_FSTD(xvalue,timevalue,Fsize,hcat,FSD,ITD)
	else:
	 self.on_destroy(self.windowfstd)

#==========================================================================
    def plot_thick(self,x_axis,thickness,xvalue):
	 self.pointer.set_ydata(thickness[xvalue])
	 self.vline1.set_xdata(x_axis[xvalue])
	 self.hline1.set_ydata(thickness[xvalue])
	 self.pointer.set_xdata(x_axis[xvalue])
	 self.windowthick.show_all()
	 self.windowthick.present()
	 self.window.present()

#==========================================================================
    def plot_conc(self,x_axis,ice_concentration,xvalue):
	 self.pointer2.set_ydata(ice_concentration[xvalue])
	 self.vline2.set_xdata(x_axis[xvalue])
	 self.hline2.set_ydata(ice_concentration[xvalue])
	 self.pointer2.set_xdata(x_axis[xvalue])
	 self.windowconc.show_all()
	 self.windowconc.present()
	 self.window.present()

#==========================================================================
    def plot_meanfloe(self,x_axis,D_ave,xvalue):
	 self.pointer3.set_ydata(D_ave[xvalue])
	 self.vline3.set_xdata(x_axis[xvalue])
	 self.hline3.set_ydata(D_ave[xvalue])
	 self.pointer3.set_xdata(x_axis[xvalue])
	 self.windowmeanfloe.show_all()
	 self.windowmeanfloe.present()
	 self.window.present()

#=========================================================================    
    def plot_FSTD(self,xvalue,timevalue,Fsize,hcat,FSD,ITD):
	FSD2=FSD[:,:,xvalue,timevalue]
	ITD2=ITD[:,xvalue]
	global FSTD
	FSTD=np.zeros((50,60))
	for i in xrange(0,49):
	 FSTD[i,:]=FSD2[i,:]*ITD2[i]
	self.fstdplot.set_data(FSTD)
	plt.draw()
        self.windowfstd.show_all()
	self.windowfstd.present()
	self.window.present()
#==========================================================================

    def readnc(self,data):
	global omega
        global x_axis
	global time
	global hcat
	global Fsize
	global spectrum
	global D_ave
	global ice_concentration
	global FSD
	global Hs
	global ITD
        global thickness
        fh = Dataset(data)
	omega = fh.variables['omega'][:]
        x_axis = fh.variables['x_axis'][:]
	time = fh.variables['time'][:]
	hcat= fh.variables['thickness categories'][:]
	Fsize= fh.variables['floe size'][:]
	spectrum = fh.variables['Spectrum'][:,:,:]
	D_ave = fh.variables['Dave'][:]
	ice_concentration = fh.variables['Ice concentration'][:]
        thickness = fh.variables['Ice thickness'][:]
	FSD = fh.variables['Floe size distribution'][:,:,:,:]
	Hs = fh.variables['significant height'][:,:]
	ITD = fh.variables['Ice Thickness Distribution'][:,:]
        fh.close()


if __name__ == "__main__":
  main = Handler()
Gtk.main()