About the X-ray diffraction


Any good-hearted person
when i download the .json data which apps should i ues to open it? I just want to obtain it to compare my data


Hi @burning,

The data isn’t in a format that a traditional XRD program can read. If you have some knowledge of pymatgen, you can use that to plot the XRD pattern. Otherwise, I’d recommend using the website to view it.


thanks shyamd
what website should I search?


Hi @burning,

It depends on what you mean by search. Both MP and ICSD have XRD patterns if you’re searching for specific compositions or specific structures. There are no websites where you can search by the pattern. Most XRD diffractometers come with programs to do this. So, I would look at your instrument and see what software comes with it.


Hi @burning,

The XRDCalculator class in our pymatgen library has flexible methods for calculating XRD data and working with it. Below is an example that obtains a structure by material ID.

Given our XRD JSON file, you simply need to parse this in a format useful to you. The JSON format allows us to store metadata in a clear way. If, for example, you wish to convert the data to CSV suitable for plotting in e.g. Excel, I have provided a short Python program below as an example.


Using XRDCalculator

from pymatgen import MPRester
from pymatgen.analysis.diffraction.xrd import XRDCalculator

mpr = MPRester()

structure = mpr.get_structure_by_material_id("mp-81")

# Defaults to Cu K_alpha radiation, no symmetry refinement, no Debye-Waller factors
xrd_calculator = XRDCalculator()

# Defaults to return scaled (peak value 100) intensities with two_theta_range (0, 90)
pattern = xrd_calculator.get_pattern(structure)

# xrd_calculator.get_plot(structure) will generate an annotated XRD plot given the data.

import csv

with open('pattern.csv', 'w') as f:
    fieldnames = ['two_theta', 'intensity']
    writer = csv.writer(f)
    for row in zip(pattern.x, pattern.y):

Outputting XRD JSON to CSV

# Already downloaded XRD JSON file? Use the following to load into `data`
# and skip to the `print("Writing...")` line below.
# import json
# with open('mp-81_xrd_Cu.json') as f:
#     data = json.load(f)

import requests

material_id = "mp-81"
rad_source = "Cu"

rv = requests.get(
    data = rv.json()
    print("Error. Perhaps unknown material id or radiation source.")

print("Writing pattern at wavelength {}".format(str(data['wavelength'])))
with open('pattern.csv', 'w') as f:
    fieldnames = ['amplitude', 'hkl', 'two_theta', 'd_spacing']
    assert fieldnames == data['meta']
    writer = csv.writer(f)
    for amplitude, hkl, two_theta, d_spacing in data['pattern']:
        hkl = "(" + " ".join(str(i) for i in hkl) + ")"
        writer.writerow((amplitude, hkl, two_theta, d_spacing))