forked from diffpy/diffpy.utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.py
219 lines (168 loc) · 5.95 KB
/
transforms.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
import warnings
from copy import copy
import numpy as np
wavelength_warning_emsg = (
"No wavelength has been specified. You can continue to use the DiffractionObject, but "
"some of its powerful features will not be available. "
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), "
"you may set do.wavelength = 1.54 with the unit in angstroms."
)
invalid_tth_emsg = "Two theta exceeds 180 degrees. Please check the input values for errors."
invalid_q_or_d_or_wavelength_emsg = (
"The supplied input array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values."
)
inf_output_wmsg = (
"INFO: The largest output value in the array is infinite. This is allowed, but it will not be plotted."
)
def _validate_inputs(q, wavelength):
if wavelength is None:
warnings.warn(wavelength_warning_emsg, UserWarning)
return np.empty(0)
pre_factor = wavelength / (4 * np.pi)
if np.any(np.abs(q * pre_factor) > 1.0):
raise ValueError(invalid_q_or_d_or_wavelength_emsg)
def q_to_tth(q, wavelength):
r"""
Helper function to convert q to two-theta.
If wavelength is missing, returns x-values that are integer indexes
By definition the relationship is:
.. math::
\sin\left(\frac{2\theta}{2}\right) = \frac{\lambda q}{4 \pi}
thus
.. math::
2\theta_n = 2 \arcsin\left(\frac{\lambda q}{4 \pi}\right)
Parameters
----------
q : 1D array
The array of :math:`q` values numpy.array([qs]).
The units of q must be reciprocal of the units of wavelength.
wavelength : float
Wavelength of the incoming x-rays/neutrons/electrons
Returns
-------
tth : 1D array
The array of :math:`2\theta` values in degrees numpy.array([tths]).
"""
_validate_inputs(q, wavelength)
q.astype(float)
tth = copy(q) # initialize output array of same shape
if wavelength is not None:
tth = np.rad2deg(2.0 * np.arcsin(q * wavelength / (4 * np.pi)))
else: # return intensities vs. an x-array that is just the index
for i, _ in enumerate(q):
tth[i] = i
return tth
def tth_to_q(tth, wavelength):
r"""
Helper function to convert two-theta to q on independent variable axis.
If wavelength is missing, returns independent variable axis as integer indexes.
By definition the relationship is:
.. math::
\sin\left(\frac{2\theta}{2}\right) = \frac{\lambda q}{4 \pi}
thus
.. math::
q = \frac{4 \pi \sin\left(\frac{2\theta}{2}\right)}{\lambda}
Parameters
----------
tth : 1D array
The array of :math:`2\theta` values np.array([tths]).
The units of tth are expected in degrees.
wavelength : float
Wavelength of the incoming x-rays/neutrons/electrons
Returns
-------
q : 1D array
The array of :math:`q` values np.array([qs]).
The units for the q-values are the inverse of the units of the provided wavelength.
"""
tth.astype(float)
if np.any(np.deg2rad(tth) > np.pi):
raise ValueError(invalid_tth_emsg)
q = copy(tth)
if wavelength is not None:
pre_factor = (4.0 * np.pi) / wavelength
q = pre_factor * np.sin(np.deg2rad(tth / 2))
else: # return intensities vs. an x-array that is just the index
warnings.warn(wavelength_warning_emsg, UserWarning)
for i, _ in enumerate(q):
q[i] = i
return q
def q_to_d(q):
r"""
Helper function to convert q to d on independent variable axis, using :math:`d = \frac{2 \pi}{q}`.
Parameters
----------
q : 1D array
The array of :math:`q` values np.array([qs]).
The units of q must be reciprocal of the units of wavelength.
Returns
-------
d : 1D array
The array of :math:`d` values np.array([ds]).
"""
if 0 in q:
print(inf_output_wmsg)
return 2.0 * np.pi / copy(q)
def tth_to_d(tth, wavelength):
r"""
Helper function to convert two-theta to d on independent variable axis.
The formula is .. math:: d = \frac{\lambda}{2 \sin\left(\frac{2\theta}{2}\right)}.
Here we convert tth to q first, then to d.
Parameters
----------
tth : 1D array
The array of :math:`2\theta` values np.array([tths]).
The units of tth are expected in degrees.
wavelength : float
Wavelength of the incoming x-rays/neutrons/electrons
Returns
-------
d : 1D array
The array of :math:`d` values np.array([ds]).
"""
q = tth_to_q(tth, wavelength)
d = copy(tth)
if wavelength is None:
warnings.warn(wavelength_warning_emsg, UserWarning)
for i, _ in enumerate(tth):
d[i] = i
return d
if 0 in q:
warnings.warn(inf_output_wmsg)
return 2.0 * np.pi / copy(q)
def d_to_q(d):
r"""
Helper function to convert q to d using :math:`d = \frac{2 \pi}{q}`.
Parameters
----------
d : 1D array
The array of :math:`d` values np.array([ds]).
Returns
-------
q : 1D array
The array of :math:`q` values np.array([qs]).
The units of q must be reciprocal of the units of wavelength.
"""
if 0 in d:
warnings.warn(inf_output_wmsg)
return 2.0 * np.pi / copy(d)
def d_to_tth(d, wavelength):
r"""
Helper function to convert d to two-theta on independent variable axis.
The formula is .. math:: 2\theta = 2 \arcsin\left(\frac{\lambda}{2d}\right).
Here we convert d to q first, then to tth.
Parameters
----------
d : 1D array
The array of :math:`d` values np.array([ds]).
wavelength : float
Wavelength of the incoming x-rays/neutrons/electrons
Returns
-------
tth : 1D array
The array of :math:`2\theta` values np.array([tths]).
The units of tth are expected in degrees.
"""
q = d_to_q(d)
return q_to_tth(q, wavelength)