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
| import wx
import numpy as np
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
import os
import netCDF4
from netCDF4 import Dataset
def line_select_callback(eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.select_button = wx.Button(panel, label="Select file")
sizer.Add(self.select_button, 0, 0, 0)
self.select_button.Bind(wx.EVT_BUTTON, self.pick_file)
self.load_options = "netCDF4 files (nc)|*.nc| Text files (txt) |*.txt| All files |*.*"
panel.SetSizer(sizer)
def pick_file(self, event):
with wx.FileDialog(self, "Pick files", wildcard=self.load_options,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
chosen_file = fileDialog.GetPath()
if chosen_file.endswith('.txt'):
#Method pour utiliser autre frame
QuickLook(parent=self, text=chosen_file)
elif chosen_file.endswith('.nc'):
QuickLook_plot(self, text=chosen_file)
class QuickLook_plot(wx.Panel):
def __init__(self, parent,text=None):
wx.Panel.__init__(self, parent,style=wx.SUNKEN_BORDER , size = (610,650))
self.initiate_Matplotlib_Plot_Canvas()
def initiate_Matplotlib_Plot_Canvas(self):
self.figure = Figure(figsize =(8,6))
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1,flag=wx.ALL | wx.GROW)
self.SetSizerAndFit(self.sizer)
self.canvas.draw()
#plot figure
ppath='D:/stage2019/air.departure.sig995.2012.nc'
nc = Dataset('D:/stage2019/air.departure.sig995.2012.nc')
lons = nc.variables['lon'][:]
lats = nc.variables['lat'][:]
air_dep = nc.variables['air_dep'][:,:,:]
air_dep = air_dep[0,:,:]
self.axes.pcolormesh(air_dep,cmap=plt.get_cmap('binary'))
self.RS = RectangleSelector(self.axes,line_select_callback,
drawtype='box', useblit=False,
button=[1],minspanx=5, minspany=5,
spancoords='pixels',
interactive=True, rectprops = dict(facecolor='None',edgecolor='red',alpha=5,fill=False))
self.RS.to_draw.set_visible(True)
self.RS.extents = (600,1400,550,1600)
self.figure.canvas.draw()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'A test dialog')
frame.Show()
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop() |