|
6 | 6 | import numpy as np
|
7 | 7 | from scipy.optimize import dual_annealing
|
8 | 8 | from scipy.signal import convolve
|
| 9 | +from xraydb import material_mu |
9 | 10 |
|
10 | 11 | from diffpy.utils.parsers.loaddata import loadData
|
11 | 12 |
|
@@ -194,6 +195,57 @@ def get_package_info(package_names, metadata=None):
|
194 | 195 | return metadata
|
195 | 196 |
|
196 | 197 |
|
| 198 | +def get_density_from_cloud(sample_composition, mp_token=""): |
| 199 | + """Function to get material density from the MP or COD database. |
| 200 | +
|
| 201 | + It is not implemented yet. |
| 202 | + """ |
| 203 | + raise NotImplementedError( |
| 204 | + "So sorry, density computation from composition is not implemented right now. " |
| 205 | + "We hope to have this implemented in the next release. " |
| 206 | + "Please rerun specifying a sample mass density." |
| 207 | + ) |
| 208 | + |
| 209 | + |
| 210 | +def compute_mu_using_xraydb(sample_composition, energy, sample_mass_density=None, packing_fraction=None): |
| 211 | + """Compute the attenuation coefficient (mu) using the XrayDB database. |
| 212 | +
|
| 213 | + Computes mu based on the sample composition and energy. |
| 214 | + User should provide a sample mass density or a packing fraction. |
| 215 | + If neither density nor packing fraction is specified, or if both are specified, a ValueError will be raised. |
| 216 | + Reference: https://xraypy.github.io/XrayDB/python.html#xraydb.material_mu. |
| 217 | +
|
| 218 | + Parameters |
| 219 | + ---------- |
| 220 | + sample_composition : str |
| 221 | + The chemical formula of the material. |
| 222 | + energy : float |
| 223 | + The energy of the incident x-rays in keV. |
| 224 | + sample_mass_density : float, optional, Default is None |
| 225 | + The mass density of the packed powder/sample in g/cm*3. |
| 226 | + packing_fraction : float, optional, Default is None |
| 227 | + The fraction of sample in the capillary (between 0 and 1). |
| 228 | + Specify either sample_mass_density or packing_fraction but not both. |
| 229 | +
|
| 230 | + Returns |
| 231 | + ------- |
| 232 | + mu : float |
| 233 | + The attenuation coefficient mu in mm^{-1}. |
| 234 | + """ |
| 235 | + if (sample_mass_density is None and packing_fraction is None) or ( |
| 236 | + sample_mass_density is not None and packing_fraction is not None |
| 237 | + ): |
| 238 | + raise ValueError( |
| 239 | + "You must specify either sample_mass_density or packing_fraction, but not both. " |
| 240 | + "Please rerun specifying only one." |
| 241 | + ) |
| 242 | + if packing_fraction is not None: |
| 243 | + sample_mass_density = get_density_from_cloud(sample_composition) * packing_fraction |
| 244 | + energy_eV = energy * 1000 |
| 245 | + mu = material_mu(sample_composition, energy_eV, density=sample_mass_density, kind="total") / 10 |
| 246 | + return mu |
| 247 | + |
| 248 | + |
197 | 249 | def _top_hat(z, half_slit_width):
|
198 | 250 | """Create a top-hat function, return 1.0 for values within the specified
|
199 | 251 | slit width and 0 otherwise."""
|
|
0 commit comments