python fetch_MoTi_entries.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python fetch_MoTi_entries.py相关的知识,希望对你有一定的参考价值。

import json
from urllib.request import urlopen
from urllib.request import Request

BASE_URL = 'http://aflowlib.duke.edu/AFLOWDATA/LIB2_RAW/Mo_pvTi_sv'

# Fetch a list of all Mo Ti aflowlib entries by prototype
req = Request(BASE_URL + '?aflowlib_entries')
res = urlopen(req).read().decode('UTF-8')

aflowlib_entries = res.replace('\n', '').split(',')[:10]

print('Fetching data:')
# For each entry get its json
entries_data = []
count = 1
for entry in aflowlib_entries:
    obj = {}
    obj['prototype'] = entry

    # auid
    req = Request(BASE_URL + '/' + entry + '?auid')
    res = urlopen(req).read().decode('UTF-8').replace('\n', '')
    obj['auid'] = res
    
    # get species
    req = Request(BASE_URL + '/' + entry + '?species')
    res = urlopen(req).read().decode('UTF-8').replace('\n', '')
    obj['species'] = res.split(',')

    # get composition
    req = Request(BASE_URL + '/' + entry + '?composition')
    res = urlopen(req).read().decode('UTF-8').replace('\n', '')
    obj['composition'] = [int(d) for d in res.split(',')]

    # get stoichiometry
    req = Request(BASE_URL + '/' + entry + '?stoichiometry')
    res = urlopen(req).read().decode('UTF-8').replace('\n', '')
    obj['stoichiometry'] = [float(d) for d in res.split(',')]


    # formation entropy per atom
    req = Request(BASE_URL + '/' + entry + '?enthalpy_formation_atom')
    res = urlopen(req).read().decode('UTF-8').replace('\n', '')
    obj['enthalpy_formation_atom'] = float(res)

    # sg relax
    req = Request(BASE_URL + '/' + entry + '?spacegroup_relax')
    res = urlopen(req).read().decode('UTF-8').replace('\n', '')
    obj['spacegroup_relax'] = int(res)

    # POSCAR
    req = Request(BASE_URL + '/' + entry + '/CONTCAR.relax')
    res = urlopen(req).read().decode('UTF-8')
    poscar_lines = res.split('\n')
    poscar_lines.insert(5, ' '.join(obj['species']))
    poscar = '\n'.join(poscar_lines)
    obj['poscar'] = poscar

    print('%s of %s               %s' % (
        count,
        len(aflowlib_entries),
        BASE_URL + '/' + entry
    ))
    count += 1
    entries_data.append(obj)


print('Writing to json')
with open('entries_data.json', 'w') as outfile:
    json.dump(
        entries_data,
        outfile,
        sort_keys=True,
        indent=2,
        ensure_ascii=False
    )
print('done!')

以上是关于python fetch_MoTi_entries.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python