# Welcome to xrayutilities’s documentation!¶

If you look for downloading the package go to Sourceforge (source distribution) or the Python package index (MS Windows binary). Installation instructions you find further down Installation.

Read more about xrayutilities below or in Journal of Applied Crystallography 2013, Volume 46, 1162-1170

# Introduction¶

xrayutilities is a collection of scripts used to analyze x-ray diffraction data. It consists of a python package and several routines coded in C. It especially useful for the reciprocal space conversion of diffraction data taken with linear and area detectors.

In the following two concepts of usage for the xrayutilities package will be described. First one should get a brief idea of how to analyze x-ray diffraction data with xrayutilities. After that the concept of how angular coordinates of Bragg reflections are calculated is presented.

## Concept of usage¶

xrayutilities provides a set of functions to read experimental data from various data file formats. All of them are gathered in the io-subpackage. After reading data with a function from the io-submodule the data might be corrected for monitor counts and/or absorption factor of a beam attenuator. A special set of functions is provided to perform this for point, linear and area detectors.

Since the amount of data taken with modern detectors often is too large to be able to work with them properly, a functions for reducing the data from linear and area detectors are provided. They use block-averaging to reduce the amount of data. Use those carefully not to loose the features you are interested in in your measurements.

After the pre-treatment of the data, the core part of the package is the transformation of the angular data to reciprocal space. This is done as described in more detail below using the experiment-module. The classes provided within the experiment module provide routines to help performing X-ray diffraction experiments. This includes methods to calculate the diffraction angles (described below) needed to align crystalline samples and to convert data between angular and reciprocal space. The conversion from angular to reciprocal space is implemented very general for various goniometer geometries. It is especially useful in combination with linear and area detectors as described in this article. In standard cases, Users will only need the initialized routines, which predefine a certain goniometer geometry like the popular four-cirlce and six-circle geometries.

After the conversion to reciprocal space, it is convenient to transform the data to a regular grid for visualization. For this purpose the gridder-module has been included into xrayutilities. For the visualization of the data in reciprocal space the usage of matplotlib is recommended.

A practical example showing the usage is given below.

## Angle calculation using the material classes¶

Calculation of angles needed to align Bragg reflections in various diffraction geometries is done using the Materials defined in the materials-package. This package provides a set of classes to describe crystal lattices and materials. Once such a material is properly defined one can calculate its properties, which includes the reciprocal lattice points, optical properties like the refractive index, the structure factor (including the atomic scattering factor) and the complex polarizability. These atomic properties are extracted from a database included in xrayutilities.

Using such a material and an experimental class from the experiment-module describing the experimental setup the needed diffraction angles can be calculated for certain coplanar diffraction (high, low incidence), grazing incidence diffraction and also special non-coplanar diffraction geometries.

## hello world¶

A first example with step by step explanation is shown in the following. It showcases the use of xrayutilities to calculate angles and read a scan recorded with a linear detector from spec-file and plots the result as reciprocal space map using matplotlib.

  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 """ Example script to show how to use xrayutilities to read and plot reciprocal space map scans from a spec file created at the ESRF/ID10B for details about the measurement see: D Kriegner et al. Nanotechnology 22 425704 (2011) http://dx.doi.org/10.1088/0957-4484/22/42/425704 """ import numpy import matplotlib.pyplot as plt import xrayutilities as xu import os # global setting for the experiment sample = "test" # sample name used also as file name for the data file energy = 8042.5 # x-ray energy in eV center_ch = 715.9 # center channel of the linear detector chpdeg = 345.28 # channels per degree of the linear detector roi=[100,1340] # region of interest of the detector nchannel = 1500 # number of channels of the detector # intensity normalizer function responsible for count time and absorber correction absfun = lambda d: d["detcorr"]/d["psd2"].astype(numpy.float) normalizer_detcorr = xu.IntensityNormalizer("MCA",mon="Monitor",time="Seconds",absfun=absfun) # substrate material used for Bragg peak calculation to correct for experimental offsets InP = xu.materials.InP # initialize experimental class to specify the reference directions of your crystal # 11-2: inplane reference # 111: surface normal hxrd = xu.HXRD(InP.Q(1,1,-2),InP.Q(1,1,1),en=energy) # configure linear detector # detector direction + parameters need to be given # mounted along z direction, which corresponds to twotheta hxrd.Ang2Q.init_linear('z-',center_ch,nchannel,chpdeg=chpdeg,roi=roi) # read spec file and save to HDF5-file # since reading is much faster from HDF5 once the data are transformed h5file = os.path.join("data",sample+".h5") try: s # try if spec file object already exist ("run -i" in ipython) except NameError: s = xu.io.SPECFile(sample+".spec",path="data") else: s.Update() s.Save2HDF5(h5file) ################################# # InP (333) reciprocal space map omalign = 43.0529 # experimental aligned values ttalign = 86.0733 [omnominal,dummy,dummy,ttnominal] = hxrd.Q2Ang(InP.Q(3,3,3)) # nominal values of the substrate peak # read the data from the HDF5 file #(scan number:36, names of motors in spec file: omega= sample rocking, gamma = twotheta) [om,tt],MAP = xu.io.geth5_scan(h5file,36,'omega','gamma') # normalize the intensity values (absorber and count time corrections) psdraw = normalizer_detcorr(MAP) # remove unusable detector channels/regions (no averaging of detector channels) psd = xu.blockAveragePSD(psdraw, 1, roi=roi) # convert angular coordinates to reciprocal space + correct for offsets [qx,qy,qz] = hxrd.Ang2Q.linear(om,tt,delta=[omalign-omnominal, ttalign-ttnominal]) # calculate data on a regular grid of 200x201 points gridder = xu.Gridder2D(200,201) gridder(qy,qz,psd) # maplog function limits the shown dynamic range to 8 orders of magnitude from the maxium INT = xu.maplog(gridder.data.T,8.,0) # plot the intensity as contour plot using matplotlib plt.figure() cf = plt.contourf(gridder.xaxis, gridder.yaxis,INT,100,extend='min') plt.xlabel(r'$Q_{[11\bar2]}$ ($\AA^{-1}$)') plt.ylabel(r'$Q_{[\bar1\bar1\bar1]}$ ($\AA^{-1}$)') cb = plt.colorbar(cf) cb.set_label(r"$\log($Int$)$ (cps)") 

More such examples can be found on the Examples page.

# xrayutilities Python package¶

xrayutilities is a Python package for assisting with x-ray diffraction experiments. Its the python package included in *xrayutilities*.

It helps with planning experiments as well as analyzing the data.

Authors: Dominik Kriegner and Eugen Wintersberger

for more details see the full API documentation of xrayutilities found here: Examples and API-documentation.

# Installation¶

## Express instructions¶

• install the dependencies (Windows: pythonxy; Linux/Unix: see below for dependencies).
• open a command line and navigate to the downloaded sources and execute:
> python setup.py install


which will install xrayutilities to the default directory. It should be possible to use it (import xrayutilities) from now on in python scripts.

Note

The python package of xrayutilities was formerly called ‘’xrutils’‘

## Detailed instructions¶

Installing xrayutilities is done using Python’s distutils

The package can be installed on Linux, Mac OS X and Microsoft Windows, however it is mostly tested on Linux/Unix platforms. Please inform one of the authors in case the installation fails!

## Required third party software¶

To keep the coding effort as small as possible xrayutilities depends on a large number of third party libraries and Python modules.

The needed dependencies are:
• GCC Gnu Compiler Collection or any compatible C compiler. On windows you most probably should use MinGW or CygWin. Others might work but are untested.
• HDF5 a versatile binary data format (library is implemented in C). Although the library is not called directly, it is needed by the pytables Python module (see below).
• Python the scripting language in which most of xrayutilities code is written in.
• git a version control system used to keep track on the xrayutilities development. (only needed for development)
Additionally, the following Python modules are needed in order to make xrayutilities work as intended:
• Numpy a Python module providing numerical array objects
• Scipy a Python module providing standard numerical routines, which is heavily using numpy arrays
• Python-Tables a powerful Python interface to HDF5.
• Matplotlib a Python module for high quality 1D and 2D plotting (optionally)
• IPython although not a dependency of xrayutilities the IPython shell is perfectly suited for the interactive use of the xrayutilities python package.

After installing all required packages you can continue with installing and building the C library.

## Building and installing the library and python package¶

xrayutilities uses the distutils packaging system to build and install all of its components. You can perform the installation by executing

>python setup.py install


or

>python setup.py install --prefix=INSTALLPATH


in the root directory of the source distribution.

The –prefix option sets the root directory for the installation. If it is omitted the libary is installed under /usr/lib/ on Unix systems or in the Python installation directory on Windows.

## Setup of the Python package¶

You need to make your Python installation aware of where to look for the module. This is usually only needed when installing in non-standard <install path> locations. For this case append the installation directory to your PYTHONPATH environment variable by

>export PYTHONPATH=\$PYTHONPATH:<local install path>/lib64/python2.7/site-packages


on a Unix/Linux terminal. Or, to make this configuration persistent append this line to your local .bashrc file in your home directory. On MS Windows you would like to create a environment variable in the system preferences under system in the advanced tab (Using pythonxy this is done automatically). Be sure to use the correct directory which might be similar to

<local install path>/Lib/site-packages


on Windows systems.

## Notes for installing on Windows¶

Since there is no packages manager on Windows the packages need to be installed manual (including all the dependecies) or a pre-packed solution needs to be used. We strongly suggest to use the pyhton(x,y) python distribution, which includes already all of the needed dependencies for installing xrayutilities.

The setup of the environment variables is also done by the python(x,y) installation. One can proceed with the installation of xrayutilities directly! The easiest way to do this on windows is to use the binaries distributed on the Python package index, otherwise one can follow the general installation instructions. Depending on your compiler on Microsoft Windows it might be necessary to perform the building of the Python extension separately and specify the compiler manually. This is done by

python setup.py build -c <compiler_name>
`

Using Python(x,y) you want to specify “mingw32” as compiler name.

In case you want to do it the hard way install all of the following (versions in brackets indicate the tested set of versions by the author (2.3.2012)):
• MinGW (0.4alpha)
• Python (2.7.2)
• numpy (1.6.1)
• scipy (0.10.1)
• numexpr (1.4.2) needed for pytables
• pytables (2.3.1)
• matplotlib (1.1.0)
• ipython (0.12)

It is suggested to add the MinGW binary directory, as well as the Python and Python-scripts directory to the Path environment variable as described above! Installation is done as described above.