#This initializes the REST adaptor. You may need to put your own API key in as an arg.
a = MPRester()

#Entries are the basic unit for thermodynamic and other analyses in pymatgen.
#This gets all entries belonging to the Ca-C-O system.
entries = a.get_entries_in_chemsys(['Ca', 'C', 'O'])

#With entries, you can do many sophisticated analyses, like creating phase diagrams.
pd = PhaseDiagram(entries)

"""
Explanation: Generating the phase diagram
To generate a phase diagram, we obtain entries from the Materials Project and call the PhaseDiagram class in pymatgen.
End of explanation
"""


#Let's show all phases, including unstable ones
plotter = PDPlotter(pd, show_unstable=True)
plotter.show()

"""
Explanation: Plotting the phase diagram
To plot a phase diagram, we send our phase diagram object into the PDPlotter class.
End of explanation
"""


import collections

data = collections.defaultdict(list)
for e in entries:
    decomp, ehull = pd.get_decomp_and_e_above_hull(e)
    data["Materials ID"].append(e.entry_id)
    data["Composition"].append(e.composition.reduced_formula)
    data["Ehull"].append(ehull)    
    data["Decomposition"].append(" + ".join(["%.2f %s" % (v, k.composition.formula) for k, v in decomp.items()]))

from pandas import DataFrame
df = DataFrame(data, columns=["Materials ID", "Composition", "Ehull", "Decomposition"])

print(df)

"""
Explanation: Calculating energy above hull and other phase equilibria properties
To perform more sophisticated analyses, use the PDAnalyzer object.
End of explanation
"""


