-
Notifications
You must be signed in to change notification settings - Fork 0
/
guimaker_working.py
375 lines (283 loc) · 13.9 KB
/
guimaker_working.py
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import customtkinter
import tkinter as tk
from tkinter import ttk
from metaread import get_varnames, xreader, get_dimensions
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
#from matplotlib.figure import Figure
window_width = 1800
window_height = 2500
plt.style.use('dark_background')
# # for plots...
# def reset_axsize(w,h, ax=None):
# """ w, h: width, height in inches """
# if not ax: ax=plt.gca()
# l = ax.figure.subplotpars.left
# r = ax.figure.subplotpars.right
# t = ax.figure.subplotpars.top
# b = ax.figure.subplotpars.bottom
# figw = float(w)/(r-l)
# figh = float(h)/(t-b)
# ax.figure.set_size_inches(figw, figh)
# def set_mousewheel(widget, command):
# """Activate / deactivate mousewheel scrolling when
# cursor is over / not over the widget respectively."""
# widget.bind("<Enter>", lambda _: widget.bind_all('<MouseWheel>', command))
# widget.bind("<Leave>", lambda _: widget.unbind_all('<MouseWheel>'))
class Scrollable(tk.Frame):
"""
Make a frame scrollable with scrollbar on the right.
After adding or removing widgets to the scrollable frame,
call the update() method to refresh the scrollable area.
"""
def __init__(self, frame):
scrollbar = tk.Scrollbar(frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=True)
self.canvas = customtkinter.CTkCanvas(frame, width=window_width*.1, height=80, yscrollcommand=scrollbar.set)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=self.canvas.yview)
self.canvas.bind('<Configure>', self.__fill_canvas)
# # base class initialization
tk.Frame.__init__(self, frame)
# # assign this obj (the inner frame) to the windows item of the canvas
self.windows_item = self.canvas.create_window(0, 0, window=self, anchor=tk.NW)
self.canvas.bind_all('<MouseWheel>', lambda event: self.canvas.yview_scroll(int(-1*(event.delta/120)), "units"))
def __fill_canvas(self, event):
"Enlarge the windows item to the canvas width"
canvas_width = event.width
self.canvas.itemconfig(self.windows_item, width = canvas_width)
def update(self):
"Update the canvas and the scrollregion"
self.update_idletasks()
self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))
# print("update")
class ButtonMaker(tk.Frame):
def __init__(self,
*args,
xrds=None,
column_title=None,
buttons=None,
header_name=None,
canvasobject=None,
clickers=None,
move_buttons=None,
**kwargs):
super().__init__(*args, **kwargs)
self.xrds = xrds
self.canvasobject = canvasobject
self.original_size = canvasobject.fig.get_size_inches()
self.clickers = clickers
self.move_buttons = move_buttons
# now label stuff...
lbl=tk.Label(self, text=column_title, bg='red')
# lbl.grid(row=0, column=0, pady=1, fill=tk.BOTH)
lbl.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
for i, button in enumerate(buttons):
_cmd = lambda x=button: self.button_event(x)
self.button_x = ttk.Button(self, text=str(button), command=_cmd)
# self.button_x.grid(row=i+1, column=0, padx=1, pady=1, fill=tk.BOTH)
self.button_x.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
def button_event(self, x):
# get the value of the previous and next buttons
# clear the existing canvas...
#if len(self.canvasobject.fig.axes) > 1:
# self.canvasobject.fig.axes[1].remove()
#reset_axsize(self.original_size[1], self.original_size[0], self.canvasobject.ax)
# get the variable
plotting_variable = self.xrds.get(x) #.isel()#time=self.move_buttons.thetime.get())
for axx in [self.canvasobject.ax,
self.canvasobject.cbax,
self.canvasobject.histax]:
axx.clear()
# MOVE THIS SOMEWHERE ELSE SO THAT IT DOESNT HAPPEN EVERY CLICK
one_len_list = {}
for i,k in enumerate(plotting_variable.shape):
if k == 1:
one_len_list[plotting_variable.dims[i]] = 0
# just go ahead and select these -- makes dim size smaller
plotting_variable = plotting_variable.sel(**one_len_list)
# now check if there is a time like dimension
# time_coord = []
# possible_time_types = [np.dtype("<M8[ns]")]
# for c in plotting_variable.coords:
# f c.dtype in possible_time_types
# select the concat dim if it's in there. ..
if len(plotting_variable.shape) > 1:
if "CONCAT_DIM" in list(plotting_variable.dims):
plotting_variable = plotting_variable.isel(CONCAT_DIM=self.move_buttons.thetime.get())
if "Time" in list(plotting_variable.dims):
plotting_variable = plotting_variable.isel(Time=self.move_buttons.thetime.get())
if "time" in list(plotting_variable.dims):
plotting_variable = plotting_variable.isel(time=self.move_buttons.thetime.get())
if "XTIME" in list(plotting_variable.dims):
plotting_variable = plotting_variable.isel(XTIME=self.move_buttons.thetime.get())
if "date" in list(plotting_variable.dims):
plotting_variable = plotting_variable.isel(date=self.move_buttons.thetime.get())
# othersiwse, don't select anything ....
# get some info about the plotting variable ...
if len(plotting_variable.shape) > 5:
pass
if len(plotting_variable.shape) == 4:
pass
if len(plotting_variable.shape) == 3:
pass
if len(plotting_variable.shape) == 2:
a = plotting_variable.plot(ax=self.canvasobject.ax,
add_colorbar=False)
# add the colorbar
self.canvasobject.fig.colorbar(a, cax=self.canvasobject.cbax)
# make a histogram
plotting_variable.plot.hist(bins=100, ax=self.canvasobject.histax)
# plot the data...
self.canvasobject.canvas.draw()
if len(plotting_variable.shape) == 1:
print('h')
plotting_variable.plot(ax=self.canvasobject.ax)
# make a histogram
plotting_variable.plot.hist(bins=100, ax=self.canvasobject.histax)
self.canvasobject.canvas.draw()
# print the click value
# print()
class cbmaker(tk.Frame):
def __init__(self,
*args,
**kwargs):
super().__init__(*args, **kwargs)
self.spamVar = tk.StringVar()
self.spamCB = tk.Checkbutton(self, text='A', variable=self.spamVar, onvalue='yes', offvalue='no')
class MoveButtoner(tk.Frame):
def __init__(self, frame):
super().__init__()#K*args, **kwargs)
self.thetime = tk.IntVar()
self.thetime.set(0)
_cmd_nxt = lambda x=1 : self.move_timestep(x)
_cmd_prv = lambda x=-1 : self.move_timestep(x)
self.next = ttk.Button(frame, text='Next', command=_cmd_nxt)
self.prev = ttk.Button(frame, text='Prev', command=_cmd_prv)
self.next.pack(fill=tk.BOTH, expand=True, side='left')
self.prev.pack(fill=tk.BOTH, expand=True, side='left')
def move_timestep(self,x):
newtime = x + self.thetime.get()
self.thetime.set(newtime)
print(self.thetime.get())
class MplMaker(tk.Frame):
def __init__(self, frame):
# super().__init__(*args, **kwargs)
# self.fig = Figure(dpi=100)
# [left, bottom, width, height]
#self.ax = self.fig.add_subplot(1,5, (1,4))
#self.axcb = self.fig.add_subplot(1,5, 1)
self.fig = plt.figure(constrained_layout=True)
spec = self.fig.add_gridspec(ncols=2, nrows=2, width_ratios=(20,1), height_ratios=(4,1))
self.ax = self.fig.add_subplot(spec[0,0])
self.cbax = self.fig.add_subplot(spec[0,1])
self.histax = self.fig.add_subplot(spec[1,0])
self.canvas = FigureCanvasTkAgg(self.fig, frame)
def __call__(self, frame):
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=tk.BOTTOM,
fill=tk.BOTH,
expand=True)
self.toolbar = NavigationToolbar2Tk(self.canvas, frame)
self.toolbar.update()
self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
class App(customtkinter.CTk):
def __init__(self, thefile):
super().__init__()
ipadding = {'ipadx': 1, 'ipady': .1}
self.configure(bg='black')
# make the screen show up so it is centered on the screen
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
# set the position of the window to the center of the screen
# self.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
self.thefile=thefile
# read in the xarray stuff and make the buttons
datavars, coords = get_varnames(thefile)
xrds = xreader(thefile)
dimensions = get_dimensions(xrds)
mapper = MplMaker(self)
# Label across the entire top of the frame
if type(thefile) == list:
headerlabel = ".".join(thefile[0].split(".")[0:-1])+"*"
else:
headerlabel = thefile
label1 = tk.Label(self, text=headerlabel, bg="green", fg="white")
label1.pack(**{'ipadx': 5, 'ipady': 5}, fill=tk.X)
#
frame00 = tk.Frame(self, bg='orange', width=100, height=100)
frame00.pack(fill=tk.BOTH, expand=True, side=tk.TOP)
move_buttons = MoveButtoner(frame00)
# 4d variables
if len(dimensions['4d']) > 0:
frame4 = tk.Frame(self)
frame4.pack(**ipadding, expand=False, side=tk.LEFT)
scrollable_body4 = Scrollable(frame4)
self.button_frame_4 = ButtonMaker(scrollable_body4,
column_title='4dVars',
buttons=dimensions['4d'],
xrds=xrds,
canvasobject=mapper,
move_buttons=move_buttons)
self.button_frame_4.pack(fill=tk.Y)
scrollable_body4.update()
# 3d variables
if len(dimensions['3d']) > 0:
frame3 = tk.Frame(self)
frame3.pack(**ipadding, expand=False, side=tk.LEFT)
scrollable_body3 = Scrollable(frame3)
self.button_frame_3 = ButtonMaker(scrollable_body3,
column_title='3dVars',
buttons=dimensions['3d'],
xrds=xrds,
canvasobject=mapper,
move_buttons=move_buttons)
self.button_frame_3.pack()
scrollable_body3.update()
# 2d variables
if len(dimensions['2d']) > 0:
frame2 = tk.Frame(self)
frame2.pack(**ipadding, expand=False, side=tk.LEFT)
scrollable_body2 = Scrollable(frame2)
self.button_frame_2 = ButtonMaker(scrollable_body2,
column_title='2dVars',
buttons=dimensions['2d'],
xrds=xrds,
canvasobject=mapper,
move_buttons=move_buttons)
self.button_frame_2.pack()
scrollable_body2.update()
if len(dimensions['1d']) > 0:
frame1 = tk.Frame(self)
frame1.pack(**ipadding, expand=False, side=tk.LEFT)
scrollable_body1 = Scrollable(frame1)
self.button_frame_1 = ButtonMaker(scrollable_body1,
column_title='1dVars',
buttons=dimensions['1d'],
xrds=xrds,
canvasobject=mapper,
move_buttons=move_buttons)
self.button_frame_1.pack()
scrollable_body1.update()
# DIMENSIONS
# frame01 = tk.Frame(self)# width=.2*window_width, height=.9*window_height)
# frame01.pack(**ipadding, expand=False, fill=tk.Y, side=tk.LEFT)
# scrollable_body01 = Scrollable(frame01)
# self.button_frame_01 = ButtonMaker(scrollable_body01,
# column_title='Dimensions',
# buttons=['button', 'button', 'button'],
# xrds=xrds,
# canvasobject=mapper,
# move_buttons=move_buttons)
# self.button_frame_01.pack()
# scrollable_body01.update()
mapper(self)
### ADD MPL PLOT
# xrdescription = tk.Label(self, text=str(xrds))
# xrdescription.pack(**ipadding, expand=True, fill=tk.BOTH, side=tk.LEFT)
# make a yellow box next to the 2nd rown of buttons