-
Notifications
You must be signed in to change notification settings - Fork 2
/
h5_utilities_new.py
428 lines (332 loc) · 14.2 KB
/
h5_utilities_new.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
#
# *******************************************************************
# *******************************************************************
# new python 3.X routines for OSIRIS data analysis
#
# *******************************************************************
# *******************************************************************
import sys
sys.path.append('/Users/franktsung/Documents/codes/python-tsung/')
sys.path.append('/Volumes/Lacie-5TB/codes/pyVisOS/')
# *******************************************************************
# *******************************************************************
# import h5_utilities
# *******************************************************************
# *******************************************************************
%matplotlib notebook
#%matplotlib widget
import numpy as np
import sys
# *******************************************************************
# *******************************************************************
# *******************************************************************
# *******************************************************************
import osh5io
import osh5def
import osh5vis
import osh5utils
import osh5visipy
import matplotlib.pyplot as plt
import hfhi
# *******************************************************************
# *******************************************************************
# *******************************************************************
# *******************************************************************
import numpy
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
# *******************************************************************
# *******************************************************************
def phase_space_average_new(path,prefix,fileno,interval,nfiles):
file_begin=fileno
file_end=fileno+((nfiles)*interval)
filename=path+prefix+repr(file_begin).zfill(6)+'.h5'
# print(filename)
hdf5_data=osh5io.read_h5(filename)
# print(hdf5_data)
xmin=hdf5_data.axes[0].min
xmax=hdf5_data.axes[0].max
ymin=hdf5_data.axes[1].min
ymax=hdf5_data.axes[1].max
# dx=(xmax-xmin)/float(nx[0]-1)
# dy=(ymax-ymin)/float(nx[1]-1)
# print(dx)
# print(dy)
# xmax=xmax-dx
# ymax=ymax-dy
# xmin=xmin+10.0*dx
# ymin=ymin-10.0*dy
# print(repr(xmin)+' '+repr(xmax)+' '+repr(ymin)+' '+repr(ymax))
nx=hdf5_data.shape
xaxis=numpy.linspace(xmin,xmax,num=nx[0])
yaxis=numpy.linspace(ymin,ymax,num=nx[1])
total_files=1
# print(repr(file_begin)+' '+repr(file_end)+' '+repr(nfiles))
for i_file in range(file_begin+interval,file_end,interval):
# print(repr(i_file))
filename=path+prefix+repr(i_file).zfill(6)+'.h5'
# print(filename)
try:
hdf5_data_temp=osh5io.read_h5(filename)
total_files=total_files+1
xmin_temp=hdf5_data_temp.axes[0].min
xmax_temp=hdf5_data_temp.axes[0].max
ymin_temp=hdf5_data_temp.axes[1].min
ymax_temp=hdf5_data_temp.axes[1].max
nx_temp=hdf5_data_temp.shape
if((xmin==xmin_temp) and (xmax==xmax_temp) and (ymin==ymin_temp) and (ymax==ymax_temp)):
# print('passed')
hdf5_data=numpy.abs(hdf5_data)+numpy.abs(hdf5_data_temp)
else:
# print('no pass')
xaxis_temp=numpy.linspace(xmin_temp,xmax_temp,nx_temp[0])
yaxis_temp=numpy.linspace(ymin_temp,ymax_temp,nx_temp[1])
interp_func=interpolate.interp2d(xaxis_temp,yaxis_temp,hdf5_data_temp,kind='cubic')
temp_data=numpy.zeros((nx[0],nx[1]))
for ix in range(0,nx[0]):
for iy in range(0,nx[1]):
temp_data[iy,ix]=interp_func(xaxis[ix],yaxis[iy])
hdf5_data=numpy.abs(hdf5_data)+numpy.abs(temp_data)
except:
print('error')
continue
# print(repr(total_files))
hdf5_data=hdf5_data/float(total_files)
return hdf5_data
# *******************************************************************
# *******************************************************************
def srs_quick_look(path,fileno):
path_e1=path_main+'/MS/FLD/e1/'
file_prefix_e1='e1-'
path_e2=path_main+'/MS/FLD/e2/'
file_prefix_e2='e2-'
path_e3=path_main+'/MS/FLD/e3/'
file_prefix_e3='e3-'
path_b1=path_main+'/MS/FLD/b1/'
file_prefix_b1='b1-'
path_b2=path_main+'/MS/FLD/b2/'
file_prefix_b2='b2-'
path_b3=path_main+'/MS/FLD/b3/'
file_prefix_b3='b3-'
path_p1x1_e = path_main+'/MS/PHA/p1x1/species_1/'
file_prefix_p1x1_e = 'p1x1-species_1-'
# fileno = 22500
interval = 30
nfiles=10
path_p1p2_e = path_main+'/MS/PHA/p1p2/species_1/'
file_prefix_p1p2_e = 'p1p2-species_1-'
from scipy import signal
filename_e1=path_e1+file_prefix_e1+repr(fileno).zfill(6)+'.h5'
filename_e2=path_e2+file_prefix_e2+repr(fileno).zfill(6)+'.h5'
filename_e3=path_e3+file_prefix_e3+repr(fileno).zfill(6)+'.h5'
filename_b1=path_b1+file_prefix_b1+repr(fileno).zfill(6)+'.h5'
filename_b2=path_b2+file_prefix_b2+repr(fileno).zfill(6)+'.h5'
filename_b3=path_b3+file_prefix_b3+repr(fileno).zfill(6)+'.h5'
filename_p1x1_e = path_p1x1_e+file_prefix_p1x1_e+repr(fileno).zfill(6)+'.h5'
filename_p1p2_e = path_p1p2_e+file_prefix_p1p2_e+repr(fileno).zfill(6)+'.h5'
# print(filename)
e1 = osh5io.read_h5(filename_e1)
e2 = osh5io.read_h5(filename_e2)
e3 = osh5io.read_h5(filename_e3)
b1 = osh5io.read_h5(filename_b1)
b2 = osh5io.read_h5(filename_b2)
b3 = osh5io.read_h5(filename_b3)
# print(e1.axes[0].attrs)
# Method 1: Just read files
# p1x1_e = osh5io.read_h5(filename_p1x1_e)
# p1p2_e = osh5io.read_h5(filename_p1p2_e)
# Method 2: Average over N steps
interval=30
nfiles = 20
p1x1_e = phase_space_average_new(path_p1x1_e,file_prefix_p1x1_e,fileno,interval,nfiles)
p1p2_e = phase_space_average_new(path_p1p2_e,file_prefix_p1p2_e,fileno,interval,nfiles)
SMALL_SIZE = 12
MEDIUM_SIZE = 14
BIGGER_SIZE = 18
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
s1 = e2*b3 - e3 * b2
s1.data_attrs['NAME']='S1'
s1.data_attrs['LONG_NAME']='S_1'
n_smooth=200
smoothing=np.ones((1,n_smooth))/n_smooth
n_avg = 200
smooth_array=np.ones((1,n_avg))/n_avg
temp=signal.convolve2d(s1,smooth_array,mode='same',boundary='wrap')
s1[:,:]=temp[:,:]
s1 = np.average(s1,axis=0)
plt.figure(figsize=(10,7.0))
plt.subplot(222)
temp=signal.convolve2d(e1,smoothing,mode='same',boundary='wrap')
osh5vis.osplot(np.average(e1,axis=0))
plt.ylim(-0.02,0.02)
# plt.subplot(222)
# osh5vis.osplot(e2,cmap='seismic',vmin=-0.02,vmax=0.02)
# plt.subplot(223)
# osh5vis.osplot(s1,cmap='PuBu',vmin=0,vmax=0.000025)
plt.subplot(221)
p1x1_e = numpy.abs(p1x1_e)
data_max= numpy.amax(p1x1_e)
p1x1_e=p1x1_e/data_max
osh5vis.oscontourf(p1x1_e,levels=[10**-5,3.0*10**-3,5*10**-3,1*10**-2,10**-1,3*10**-1,5*10**1],norm=LogNorm(),cmap='terrain',vmin=1e-3,vmax=100)
osh5vis.oscontour(p1x1_e,levels=[10**-5,3.0*10**-3,5*10**-3,1*10**-2,10**-1,3*10**-1,5*10**1],norm=LogNorm(),colors='black',linestyles='dashed',vmin=1e-5,vmax=500,colorbar=False)
plt.subplot(223)
# osh5vis.osplot(np.log(np.abs(p1p2_e)+1e-5),cmap='hsv')
p1p2_e = numpy.abs(p1p2_e)
data_max= numpy.amax(p1p2_e)
p1p2_e=p1p2_e/data_max
osh5vis.oscontourf(p1p2_e,levels=[10**-6,3.0*10**-3,5*10**-3,1*10**-2,10**-1,3*10**-1,5*10**1],norm=LogNorm(),cmap='terrain',vmin=1e-3,vmax=100)
osh5vis.oscontour(p1p2_e,levels=[10**-6,3.0*10**-3,5*10**-3,1*10**-2,10**-1,3*10**-1,5*10**1],norm=LogNorm(),colors='black',linestyles='dashed',vmin=1e-5,vmax=500,colorbar=False)
# plt.xlim(-0.8,0.8)
plt.xlim(-0.35,0.35)
plt.subplot(224)
osh5vis.osplot(s1, title='Axial <Poynting Flux>')
plt.ylim(0,0.00009)
plt.tight_layout()
plt.show()
# *******************************************************************
# *******************************************************************
# *******************************************************************
# *******************************************************************
def upic_bvp_2(rundir,*args,**kwpassthrough):
#2345
import os
def something_2(rundir,file_no):
v_phase = 1/.3
v_group = 3/v_phase
my_path=os.getcwd()
#print(my_path)
working_dir=my_path+'/'+rundir
#print(working_dir)
dir_0=rundir+'/Vx_x/'
dir_1=rundir+'/Ex/'
quan_0_prefix='vx_x_'
quan_1_prefix='Ex-0_'
fig = plt.figure(figsize=(10,8) )
phase_space_filename=dir_0+quan_0_prefix+repr(file_no).zfill(6)+'.h5'
e1_filename=dir_1+quan_1_prefix+repr(file_no).zfill(6)+'.h5'
phase_space_data = osh5io.read_h5(phase_space_filename)
e1_data = np.average(osh5io.read_h5(e1_filename),axis=0)
fig.suptitle('Landau Boundary Problem, time = '+repr(phase_space_data.run_attrs['TIME'][0])+' $\omega_p^{-1}$ \n \n')
p1_x1_plot = plt.subplot(221)
osh5vis.osplot(np.log(np.abs(phase_space_data)+1e-10),title='Phase Space',vmax=10,vmin=-2,cmap='Blues')
# =====================================================================
# =====================================================================
# the following lines draw vertical axis indicating the group velocity
# p1_x1_plot.axvline(x=512-v_group*phase_space_data.run_attrs['TIME'][0])
# p1_x1_plot.axvline(x=512+v_group*phase_space_data.run_attrs['TIME'][0])
# =====================================================================
# =====================================================================
e1_plot = plt.subplot(222)
osh5vis.osplot(e1_data,title='Electric Field')
# plt.tight_layout()
fig.subplots_adjust(top=0.82)
fv_plot = plt.subplot(223)
# print(hdf5_data)
xmin=phase_space_data.axes[0].min
xmax=phase_space_data.axes[0].max
ymin=phase_space_data.axes[1].min
ymax=phase_space_data.axes[1].max
# dx=(xmax-xmin)/float(nx[0]-1)
# dy=(ymax-ymin)/float(nx[1]-1)
# print(dx)
# print(dy)
# xmax=xmax-dx
# ymax=ymax-dy
# xmin=xmin+10.0*dx
# ymin=ymin-10.0*dy
# print(repr(xmin)+' '+repr(xmax)+' '+repr(ymin)+' '+repr(ymax))
nx=phase_space_data.shape
xaxis=np.linspace(xmin,xmax,num=nx[0])
yaxis=np.linspace(ymin,ymax,num=nx[1])
plt.plot(xaxis,np.average(phase_space_data,axis=1))
plt.xlabel('$v/v_{th}$')
plt.ylabel('f(v) [a.u]')
fv_plot.set_ylim([0, 1500])
fig.subplots_adjust(top=0.82)
fv_slices = plt.subplot(224)
# print(hdf5_data)
xmin=phase_space_data.axes[0].min
xmax=phase_space_data.axes[0].max
ymin=phase_space_data.axes[1].min
ymax=phase_space_data.axes[1].max
# dx=(xmax-xmin)/float(nx[0]-1)
# dy=(ymax-ymin)/float(nx[1]-1)
# print(dx)
# print(dy)
# xmax=xmax-dx
# ymax=ymax-dy
# xmin=xmin+10.0*dx
# ymin=ymin-10.0*dy
# print(repr(xmin)+' '+repr(xmax)+' '+repr(ymin)+' '+repr(ymax))
nx=phase_space_data.shape
xaxis=np.linspace(xmin,xmax,num=nx[0])
yaxis=np.linspace(ymin,ymax,num=nx[1])
plt.plot(xaxis,np.average(phase_space_data[:,492:532],axis=1))
plt.plot(xaxis,np.average(phase_space_data[:,352:372],axis=1),'r')
plt.plot(xaxis,np.average(phase_space_data[:,652:672],axis=1),'g')
plt.xlabel('$v/v_{th}$')
plt.ylabel('f(v) [a.u]')
fv_slices.set_ylim([0, 1500])
plt.tight_layout()
plt.show()
# plt.show()
#2345
# my_path=os.getcwd()
# working_dir=my_path+'/'+rundir
phase_space_dir=rundir+'/Vx_x/'
files=sorted(os.listdir(phase_space_dir))
print(files[1])
start=files[1].find('vx_x_')+5
end=files[1].find('.')
print(files[1][start:end])
file_interval=int(files[1][start:end])
file_max=(len(files)-1)*file_interval
print(file_max)
interact(something_2,rundir=rundir,file_no=widgets.IntSlider(min=0,max=file_max,step=file_interval,value=0), continuous_update=False)
#something(rundir=rundir,file_no=20)
# *******************************************************************
# *******************************************************************
def rwigner(data):
nx=data.shape[0]
nxh = ((nx+1) // 2)
temp=np.zeros((nx,nx))
wdata=np.zeros((nx,nxh))
# temp1d=np.zeros(nx)
dataplus=np.zeros(nx)
dataminus=np.zeros(nx)
temp[:,0]=data[:]
for j in range(1,nx):
dataplus=np.roll(data,j)
dataminus=np.roll(data,-j)
temp[:,j] = dataplus[:] * dataminus[:]
for i in range(nx):
temp1d=np.fft.fft(temp[:,i])
wdata[i,:]=np.abs(temp1d[0:nxh])
return wdata
# *******************************************************************
# *******************************************************************
def os_rwigner(real_array,xaxis):
nx=real_array.shape
nxh=(nx[0]+1)/2
dx=(xaxis.max-xaxis.min)/nx
#
# the wigner transform is not FFT because it is roughly the FFT of the correlation
# so the k-range is a little different than those of the FFT,
# hence the 0.5
#
kmax=0.5*(np.pi)/dx
dk=(0.5*np.pi)/xaxis.max
w_data= rwigner(real_array)
kaxis=osh5def.DataAxis(0,kmax,nxh,attrs={'NAME':'k','LONG_NAME':'wave number', 'UNITS': '\omega_0/c' })
data_attrs={'UNITS': '[a.u.]','NAME': '$W_{\phi}$', 'LONG_NAME': 'Wigner Transform' }
run_attrs = {'XMAX' : np.array( [xaxis.max, kmax] ) , 'XMIN' : np.array( [ xaxis.min, 0 ] ) }
os5data_wigner=osh5def.H5Data(w_data,timestamp='x', data_attrs=data_attrs, axes=[xaxis,kaxis])
return os5data_wigner
# *******************************************************************
# *******************************************************************