Encyclopedia API Tutorial¶
import requests
import matplotlib.pyplot as plt
# import json
# import os
# ENCYCLOPEDIA API URL
nomad_api_url ="https://encyclopedia-api.nomad-coe.eu/v1.0/materials"
# ENCYCLOPEDIA API Token (this one is valid for the NOMAD summer, no need to use a different one)
api_access_token ="eyJpYXQiOjE1MDQ3MTg2NTAsImFsZyI6IkhTMjU2IiwiZXhwIjoxNTA3MzEwNjUwfQ.eyJpZCI6InRlc3RfdG9rZW4ifQ.hiCIfi0bAW_O6CsMpQaTBzqUhpfu6VguVEudm9HV7Xs"
#### SEARCH EXAMPLES ###
# search for a material by element, Exclusive ON
# Note: this is the search used in order to get working the next steps for this tutorial. If you change
# the search criteria then the rest of this tutorial should be changed as well (calc nr, material nr, ...)
r = requests.post(nomad_api_url, auth=(api_access_token, ''), \
json = {"search_by":{"element": "Ag", "exclusive": "1"}})
# search for materials by element, Exclusive OFF
# r = requests.post(nomad_api_url, auth=(api_access_token, ''), \
# json = {"search_by":{"element": "Ag", "exclusive": "0"}})
# get the second page of previous results with 5 results per page
# r = requests.post(nomad_api_url, auth=(api_access_token, ''), \
# json = {"search_by":{"element": "Ag", "exclusive": "1", "page": 2, "per_page": 5}})
# search for materials using multiple elements
# r = requests.post(nomad_api_url, auth=(api_access_token, ''), \
# json = {"search_by":{"element": "Ti,O", "exclusive": "1"}})
# search for materials using formula
# r = requests.post(nomad_api_url, auth=(api_access_token, ''), \
# json = {"search_by":{"formula": "Ac2Ag3", "exclusive": "1"}})
# search can be done also by using specific properties in combination with elements/formula
# example: materials containing Ag and having DOS data
# r = requests.post(nomad_api_url, auth=(api_access_token, ''), \
# json = {"search_by":{"element": "Ag", "exclusive": "0"}, \
# "has_dos": "True"})
# check the status code of the search command
r.status_code
# check the results of our search
content = r.json()
content
# get the total number of results
content['total_results']
# check the number of results by counting the entries in "results" list
len(content['results'])
# let's show the formula, reduced formula and the space_group for each material found
for material in content['results']:
print("formula:", material['formula'], "reduced formula:", material['formula_reduced'], "space_group: ", material['space_group'])
# let's create a list with the available materials id
id_list =[]
for material in content['results']:
id_list.append(material['id'])
print(id_list)
# get more properties for a specific material (for example: material_id 714)
material_714 = requests.get(nomad_api_url + "/714", auth=(api_access_token, ''))
material_714.json()
# get all calculations for a specific material (material_id 714)
material_714_calcs = requests.get("https://encyclopedia-api.nomad-coe.eu/v1.0/materials/714/calculations", auth=(api_access_token, ''))
# show the available calculations for our material
calculations = material_714_calcs.json()
calculations
# check how many calculations we have available for this specific material
calculations['total_results']
# get the "id" and the "functional long name" property for the first calculation in our list
calculations['results'][0]['id']
calculations['results'][0]['functional_long_name']
# request a specific property for one calculation, here for example we ask for "density of states" data
r = requests.get("https://encyclopedia-api.nomad-coe.eu/v1.0/materials/714/calculations/249502?property=dos", auth=(api_access_token, ''))
# it is good from time to time to check the status returned by our HTTP request :)
r.status_code
# get only the DOS data from previous request result
dos = r.json()
# show the DOS data
dos
# let's see what kind of data we have there
dos['dos'].keys()
# get only the DOS energies
energies = dos['dos']['dos_energies']
# show the DOS energies
energies
# get the DOS values
values = dos['dos']['dos_values']
# show the DOS values
values
type(values)
# show plots inline in notebook
%matplotlib inline
# rescale the energy values by atomic unit of charge: 1.602176565e-19 (C)
new_energy = [item/1.602176565e-19 for item in energies]
# plotting things
plt.plot(values[0], energies, color="darkorange", linewidth=1.5)