-
Notifications
You must be signed in to change notification settings - Fork 0
/
bq_t4_look.py
executable file
·437 lines (350 loc) · 15.6 KB
/
bq_t4_look.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python
"""
bq_t4_look.py
Created by Mikolaj Szydlarski on 2014-04-29.
Copyright (c) 2014, ITA UiO - All rights reserved.
"""
import os
import sys
import datetime
import getopt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.ticker as ticker
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from PyQt4 import QtGui
from PyQt4 import Qt
from matplotlib import cm
from scipy.io.idl import readsav
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.image import NonUniformImage
plt.style.use('dark_background')
sys.path.append("./bq_t4_sys/")
sys.path.append("./bq_t4_sys/cstagger/")
import bifrost
first_file_name = "no_file"
first_slice = -1
first_depth = -1000.0
help_message = '''
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Python/Qt4 based quick look tool app for Bifrost cubes ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
You can setup some parameters from command line:
-i / --input - point to the snap file
-s / --slice - jump directly to cube[:,:,slice]
-z / --depth - finds a slice to the corresponding depth
val provided in real [mM] coordinates
-h / --help - print this help msg
............................................................
'''
def process_file_name(file_name):
a = os.path.splitext(file_name)[0]
a = a.split("/")[:-1]
a.append(" ")
where_str = "/".join(a)
b = os.path.splitext(file_name)[0]
b = b.split("/")[-1]
b = b.split("_")
base_name = "_".join(b[0:-1])
snap_n = b[-1]
return where_str.strip(), base_name.strip(), snap_n.strip()
def z2indx(z_vec, z):
return np.argmax( z_vec >= z )
def get_bifrost_param(file_name, offset):
where_str, base_name, snap_n = process_file_name(file_name)
return bifrost.read_idl_ascii(where_str + base_name + ('_%s' % str(int(snap_n) + offset)) + '.idl')
def get_bifrost_obj(file_name, offset, with_no_aux):
where_str, base_name, snap_n = process_file_name(file_name)
print '[Processing] : ' ,base_name, " snap No: ", int(snap_n) + offset, " in ", where_str
formater = "_%%0%ii" % len(str(int(snap_n) + offset))
return bifrost.OSC_data(int(snap_n) + offset, template = where_str + base_name + formater, meshfile = where_str + base_name + ".mesh", no_aux=with_no_aux), base_name, int(snap_n) + offset
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setAcceptDrops(True)
self.figure = plt.figure()
# --- Just for fun lets start with nice picture
img = mpimg.imread('./bq_t4_sys/thor.png')
ax = plt.Axes(self.figure,[0.,0.,1.,1.])
ax.set_axis_off()
self.figure.add_axes(ax)
plt.imshow(img,cmap=cm.get_cmap('gray'))
# ---------------------------------------------
self.canvas = FigureCanvas(self.figure)
# ---------------------------------------------
self.toolbar = NavigationToolbar(self.canvas, self)
# ---------------------------------------------
self.slider = QtGui.QSlider(orientation=Qt.Qt.Horizontal)
self.slider.setTickInterval(1)
self.slider.valueChanged.connect(self.plot)
# ----------------------------------------------
self.view_combo = QtGui.QComboBox(self)
self.view_combo.activated[str].connect(self.changeView)
self.view_combo.addItem('X-Y')
self.view_combo.addItem('X-Z')
self.view_combo.addItem('Y-Z')
self.view = 'X-Y'
self.slide_dimension = 2 # will slide by default though z
self.combo = QtGui.QComboBox(self)
self.combo.activated[str].connect(self.comboActivated)
# -- check box
self.hbox = QtGui.QHBoxLayout()
# self.hbox.addStretch(1)
self.back_button = QtGui.QPushButton('<',self)
self.back_button.clicked.connect(self.goBack)
self.next_button = QtGui.QPushButton('>',self)
self.next_button.clicked.connect(self.goNext)
self.check_abs = QtGui.QCheckBox('Absolute')
# self.check_abs.toggle()
self.check_abs.stateChanged.connect(self.plot)
self.check_bw = QtGui.QCheckBox('B&W')
# self.check_bw.toggle()
self.check_bw.stateChanged.connect(self.plot)
self.check_log = QtGui.QCheckBox('Log')
# self.check_log.toggle()
self.check_log.stateChanged.connect(self.plot)
self.check_si = QtGui.QCheckBox('CGS Units')
self.check_si.toggle()
self.check_si.stateChanged.connect(self.plot)
self.check_aux = QtGui.QCheckBox('No AUX')
self.check_aux.toggle()
self.check_aux.stateChanged.connect(self.reset_bifrost_obj)
self.hbox.addWidget(self.check_si)
self.hbox.addWidget(self.check_abs)
self.hbox.addWidget(self.check_log)
self.hbox.addWidget(self.check_bw)
self.hbox.addWidget(self.check_aux)
self.hbox.addWidget(self.back_button)
self.hbox.addWidget(self.next_button)
self.hbox.addWidget(self.combo)
self.hbox.addWidget(self.view_combo)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addLayout(self.hbox)
layout.addWidget(self.canvas)
layout.addWidget(self.slider)
self.setLayout(layout)
# actual data stuff
self.fpath = first_file_name
# self.data = np.einsum('ijk->kji', self.data)
self.tag = 'r'
if (first_file_name != "no_file"):
self.param = get_bifrost_param(self.fpath,0)
self.b, self.base_name, self.snap_n = get_bifrost_obj(self.fpath,0, self.check_aux.isChecked())
self.data = self.b.getvar(self.tag)
self.slide_dimension = 2
self.slider.setMinimum(0)
self.slider.setMaximum(self.data.shape[self.slide_dimension]-1)
if (first_slice != -1):
self.slider.setValue(first_slice)
if (first_depth != -1000.0):
self.slider.setValue(np.argmax( self.b.z >= first_depth ))
if self.slider.value() >= self.data.shape[self.slide_dimension]:
self.slider.setValue(self.data.shape[self.slide_dimension]-1)
elif self.slider.value() < 0:
self.slider.setValue(0)
self.setCombo()
self.plot()
else:
print "\t[!] Drag & Drop a snap file to plot its content"
def keyPressEvent(self, event):
key = event.key
if key == Qt.Qt.Key_Right:
self.slider.setValue(self.slider.value() + 1)
elif key == Qt.Qt.Key_Left:
self.slider.setValue(self.slider.value() - 1)
def goBack(self):
self.param = get_bifrost_param(self.fpath,-1)
self.b, self.base_name, self.snap_n = get_bifrost_obj(self.fpath,-1, self.check_aux.isChecked())
self.get_data()
self.plot()
where_str, base_name, snap_n = process_file_name(self.fpath)
snap_n = str(int(snap_n) - 1)
self.fpath = where_str + base_name + '_' + snap_n + '.snap'
pass
def goNext(self):
self.param = get_bifrost_param(self.fpath,1)
self.b, self.base_name, self.snap_n = get_bifrost_obj(self.fpath,1,self.check_aux.isChecked())
self.get_data()
self.plot()
where_str, base_name, snap_n = process_file_name(self.fpath)
snap_n = str(int(snap_n) + 1)
self.fpath = where_str + base_name + '_' + snap_n + '.snap'
pass
def setCombo(self):
self.combo.clear()
for tag in self.b.snapvars:
self.combo.addItem(tag)
if not self.check_aux.isChecked():
for tag in self.b.auxvars:
self.combo.addItem(tag)
def changeView(self, text):
self.view = text
self.get_data()
self.plot()
print 'View changed to: ', self.view
def comboActivated(self, text):
self.tag = text
self.data = self.b.getvar(self.tag)
self.plot()
def reset_bifrost_obj(self):
self.b, self.base_name, self.snap_n = get_bifrost_obj(self.fpath,0,self.check_aux.isChecked())
print self.b.snapvars
self.get_data()
def get_data(self):
self.data = self.b.getvar(self.tag)
if self.view == 'X-Y':
self.slide_dimension = 2
elif self.view == 'X-Z':
self.slide_dimension = 1
elif self.view == 'Y-Z':
self.slide_dimension = 0
self.slider.setMinimum(0)
self.slider.setMaximum(self.data.shape[self.slide_dimension]-1)
if self.slider.value() >= self.data.shape[self.slide_dimension]:
self.slider.setValue(self.data.shape[self.slide_dimension]-1)
elif self.slider.value() < 0:
self.slider.setValue(0)
self.setCombo()
def home(self):
self.toolbar.home()
def zoom(self):
self.toolbar.zoom()
def pan(self):
self.toolbar.pan()
def plot(self):
plt.clf()
ax = self.figure.add_subplot(111)
slice_str = '[?]'
# extension = []
if self.view == 'X-Y':
image = self.data[:,:,self.slider.value()]
slice_str = 'z = %f ' % self.b.z[self.slider.value()]
ax.set_ylabel('y-direction')
ax.set_xlabel('x-direction')
# extension = [0, self.param['mx'], 0, self.param['my']]
elif self.view == 'X-Z':
image = self.data[:,self.slider.value(),:]
slice_str = 'y = %f ' % self.b.y[self.slider.value()]
ax.set_ylabel('z-direction')
ax.set_xlabel('x-direction')
# extension = [0, self.param['mx'], 0, self.param['mz']]
elif self.view == 'Y-Z':
image = self.data[self.slider.value(),:,:]
slice_str = 'x = %f ' % self.b.x[self.slider.value()]
ax.set_ylabel('z-direction')
ax.set_xlabel('y-direction')
# extension = [0, self.param['my'], 0, self.param['mz']]
# image = np.fliplr(image)
# image = np.rot90(image,k=3)
label = "Value"
color = cm.get_cmap('jet')
ax.set_title("[%s] %s (Snap: %s) for %s \n[time: %s]" % (self.tag, self.base_name, self.snap_n, slice_str, str(datetime.timedelta(seconds=self.param['t']*self.param['u_t']))))
# ax.xaxis.set_major_locator(ticker.MultipleLocator(int(64)))
# ax.yaxis.set_major_locator(ticker.MultipleLocator(int(64)))
if self.check_si.isChecked():
if self.tag == 'r':
image = image * self.param['u_r']
unit_label = "[g/cm3]"
label = "Value %s" % unit_label
elif (self.tag == 'bx' or self.tag == 'by' or self.tag == 'bz'):
image = image * self.param['u_b']
unit_label = "[G]"
label = "Value %s" % unit_label
elif (self.tag == 'px' or self.tag == 'py' or self.tag == 'pz'):
image = image * self.param['u_p']
unit_label = "[Ba]"
label = "Value %s" % unit_label
elif self.tag == 'e':
image = image * self.param['u_e']
unit_label = "[erg]"
label = "Value %s" % unit_label
if self.check_abs.isChecked():
image = np.absolute(image)
label = "ABS( %s )" % label
if self.check_log.isChecked():
image = np.log10(image)
label = "Log10( %s )" % label
if self.check_bw.isChecked():
# color = cm.get_cmap('gist_yarg')
color = cm.get_cmap('Greys_r') # Mats favorite color palette
if self.view == 'X-Y':
ax.set_ylabel('y-direction [Mm]')
ax.set_xlabel('x-direction [Mm]')
im = NonUniformImage(ax, interpolation='bilinear', extent=(self.b.x.min(),self.b.x.max(),self.b.y.min(),self.b.y.max()), cmap=color)
im.set_data(self.b.x, self.b.y, np.fliplr(zip(*image[::-1])))
ax.images.append(im)
ax.set_xlim(self.b.x.min(),self.b.x.max())
ax.set_ylim(self.b.y.min(),self.b.y.max())
ax.xaxis.set_major_locator(ticker.MultipleLocator(int(4)))
ax.yaxis.set_major_locator(ticker.MultipleLocator(int(4)))
elif self.view == 'X-Z':
ax.set_ylabel('z-direction [Mm]')
ax.set_xlabel('x-direction [Mm]')
im = NonUniformImage(ax, interpolation='bilinear', extent=(self.b.x.min(),self.b.x.max(),self.b.z.min(),self.b.z.max()), cmap=color)
im.set_data(self.b.x, self.b.z[::-1], np.flipud(np.fliplr(zip(*image[::-1]))))
ax.images.append(im)
ax.set_xlim(self.b.x.min(),self.b.x.max())
ax.set_ylim(self.b.z.max(),self.b.z.min())
ax.xaxis.set_major_locator(ticker.MultipleLocator(int(4)))
ax.yaxis.set_major_locator(ticker.MultipleLocator(int(2)))
elif self.view == 'Y-Z':
ax.set_ylabel('z-direction [Mm]')
ax.set_xlabel('y-direction [Mm]')
im = NonUniformImage(ax, interpolation='bilinear', extent=(self.b.y.min(),self.b.y.max(),self.b.z.min(),self.b.z.max()), cmap=color)
im.set_data(self.b.y, self.b.z[::-1], np.flipud(np.fliplr(zip(*image[::-1]))))
ax.images.append(im)
ax.set_xlim(self.b.y.min(),self.b.y.max())
ax.set_ylim(self.b.z.max(),self.b.z.min())
ax.xaxis.set_major_locator(ticker.MultipleLocator(int(4)))
ax.yaxis.set_major_locator(ticker.MultipleLocator(int(2)))
# im = ax.imshow(image, interpolation='none', origin='lower', cmap=color, extent=extension)
# ax.text(0.025, 0.025, (r'$\langle B_{z} \rangle = %2.2e$'+'\n'+r'$\langle |B_{z}| \rangle = %2.2e$') % (np.average(img),np.average(np.absolute(img))), ha='left', va='bottom', transform=ax.transAxes)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax,label=label)
self.canvas.draw()
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
for url in event.mimeData().urls():
path = url.toLocalFile().toLocal8Bit().data()
if os.path.isfile(path):
self.fpath = path
self.param = get_bifrost_param(self.fpath,0)
self.b, self.base_name, self.snap_n = get_bifrost_obj(self.fpath,0, self.check_aux.isChecked())
self.get_data()
self.plot()
def main(argv=None):
global first_file_name
global first_slice
global first_depth
if argv is None:
argv = sys.argv
opts, args = getopt.getopt(argv[1:], "hi:s:z:", ["help","input=","slice=","depth="])
# option processing
for option, value in opts:
if option in ("-h", "--help"):
print help_message
return 0
if option in ("-i", "--input"):
first_file_name = value
print '[Input file] : ', first_file_name
if option in ("-s", "--slice"):
first_slice = int(value)
if option in ("-z", "--depth"):
first_depth = float(value)
print help_message
app = QtGui.QApplication(sys.argv)
main = Window()
main.setWindowTitle('Bifrost Q(t4)uick Look App')
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
sys.exit(main())