forked from diffpy/diffpy.utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.py
168 lines (127 loc) · 4.59 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
import warnings
from copy import copy
import numpy as np
wavelength_warning_emsg = (
"INFO: 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, set "
"diffraction_object.wavelength = [number], "
"where diffraction_object is the variable name of you Diffraction Object, "
"and number is the wavelength in angstroms."
)
invalid_tth_emsg = "Two theta exceeds 180 degrees. Please check the input values for errors."
invalid_q_or_wavelength_emsg = (
"The supplied q-array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values."
)
inf_output_msg = "WARNING: The largest output is infinite and cannot 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_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
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:
warnings.warn(inf_output_msg)
return 2.0 * np.pi / copy(q)
def tth_to_d(ttharray, wavelength):
qarray = tth_to_q(ttharray, wavelength)
return 2.0 * np.pi / copy(qarray)
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_msg)
return 2.0 * np.pi / copy(d)
def d_to_tth(darray, wavelength):
qarray = d_to_q(darray)
return q_to_tth(qarray, wavelength)