This repository was archived by the owner on Jul 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_distributions.py
114 lines (86 loc) · 3.7 KB
/
read_distributions.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
import numpy
from srwlib import *
from silx.gui.plot import Plot2D, ImageView
###################################################################
# copied from SRW's uti_plot_com and slightly modified (no _enum)
def file_load(_fname, _read_labels=1):
nLinesHead = 11
hlp = []
with open(_fname,'r') as f:
for i in range(nLinesHead):
hlp.append(f.readline())
ne, nx, ny = [int(hlp[i].replace('#','').split()[0]) for i in [3,6,9]]
ns = 1
testStr = hlp[nLinesHead - 1]
if testStr[0] == '#':
ns = int(testStr.replace('#','').split()[0])
e0,e1,x0,x1,y0,y1 = [float(hlp[i].replace('#','').split()[0]) for i in [1,2,4,5,7,8]]
data = numpy.squeeze(numpy.loadtxt(_fname, dtype=numpy.float64)) #get data from file (C-aligned flat)
allrange = e0, e1, ne, x0, x1, nx, y0, y1, ny
arLabels = ['Photon Energy', 'Horizontal Position', 'Vertical Position', 'Intensity']
arUnits = ['eV', 'm', 'm', 'ph/s/.1%bw/mm^2']
if _read_labels:
arTokens = hlp[0].split(' [')
arLabels[3] = arTokens[0].replace('#','')
arUnits[3] = '';
if len(arTokens) > 1:
arUnits[3] = arTokens[1].split('] ')[0]
for i in range(3):
arTokens = hlp[i*3 + 1].split()
nTokens = len(arTokens)
nTokensLabel = nTokens - 3
nTokensLabel_mi_1 = nTokensLabel - 1
strLabel = ''
for j in range(nTokensLabel):
strLabel += arTokens[j + 2]
if j < nTokensLabel_mi_1: strLabel += ' '
arLabels[i] = strLabel
arUnits[i] = arTokens[nTokens - 1].replace('[','').replace(']','')
return data, None, allrange, arLabels, arUnits
def loadNumpyFormat(filename):
data, dump, allrange, arLabels, arUnits = file_load(filename)
dim_x = allrange[5]
dim_y = allrange[8]
np_array = data.reshape((dim_y, dim_x))
np_array = np_array.transpose()
x_coordinates = numpy.linspace(allrange[3], allrange[4], dim_x)
y_coordinates = numpy.linspace(allrange[6], allrange[7], dim_y)
return x_coordinates, y_coordinates, np_array
def plot_2D(x, y, data, xlabel="X [m]", ylabel="Y [m]", title=None):
xmin, xmax = x.min(), x.max()
ymin, ymax = y.min(), y.max()
origin = (xmin, ymin)
scale = (abs((xmax-xmin)/len(x)), abs((ymax-ymin)/len(y)))
# PyMCA inverts axis!!!! histogram must be calculated reversed
data_to_plot = []
for y_index in range(0, len(y)):
x_values = []
for x_index in range(0, len(x)):
x_values.append(data[x_index, y_index])
x_values.reverse()
data_to_plot.append(x_values)
plot_canvas = ImageView()
plot_canvas.setGraphTitle(title)
plot_canvas.toolBar()
plot_canvas.addImage(numpy.array(data_to_plot),
xlabel=xlabel,
ylabel=ylabel,
legend="data",
origin=origin,
scale=scale,
replace=True)
return plot_canvas
x, z, intensity_source_dimension = loadNumpyFormat("intensity_source_dimension.dat")
x_first, z_first, intensity_angular_distribution = loadNumpyFormat("intensity_angular_distribution.dat")
plot_canvas_1 = plot_2D(x, z, intensity_source_dimension, xlabel="X [m]", ylabel="Z [m]", title="X,Z")
plot_canvas_2 = plot_2D(x_first, z_first, intensity_angular_distribution, xlabel="X' [rad]", ylabel="Z' [rad]", title="AlphaX, AlphaZ")
plot_canvas_1.show()
plot_canvas_2.show()
output = []
output.append(x)
output.append(z)
output.append(intensity_source_dimension)
output.append(x_first)
output.append(z_first)
output.append(intensity_angular_distribution)
out_object = output