Coverage for /builds/debichem-team/python-ase/ase/io/cjson.py: 100.00%
29 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-03-06 04:00 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-03-06 04:00 +0000
1"""Module to read atoms in chemical json file format.
3https://wiki.openchemistry.org/Chemical_JSON
4"""
5import json
7import numpy as np
9from ase import Atoms
10from ase.cell import Cell
13# contract and lower case string
14def contract(dictionary):
15 dcopy = {key.replace(' ', '').lower(): dictionary[key] for key in
16 dictionary}
17 return dcopy
20def read_cjson(fileobj):
21 """Read a Chemical Json file as written by avogadro2 (>=1.93.0)
23 See https://wiki.openchemistry.org/Chemical_JSON
24 """
25 data = contract(json.load(fileobj))
26 atoms = Atoms()
27 datoms = data['atoms']
29 atoms = Atoms(datoms['elements']['number'])
31 if 'unitcell' in data:
32 cell = data['unitcell']
33 a = cell['a']
34 b = cell['b']
35 c = cell['c']
36 alpha = cell['alpha']
37 beta = cell['beta']
38 gamma = cell['gamma']
39 atoms.cell = Cell.fromcellpar([a, b, c, alpha, beta, gamma])
40 atoms.pbc = True
42 coords = contract(datoms['coords'])
43 if '3d' in coords:
44 positions = np.array(coords['3d']).reshape(len(atoms), 3)
45 atoms.set_positions(positions)
46 else:
47 positions = np.array(coords['3dfractional']).reshape(len(atoms), 3)
48 atoms.set_scaled_positions(positions)
50 yield atoms