forked from MolSSI-BSE/basis_set_exchange-xmlconvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_references.py
executable file
·74 lines (54 loc) · 1.79 KB
/
apply_references.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
import os
import sys
import glob
import time
import bse
ref_dir = 'refs'
def find_Z(js, Z):
ret = []
for c in js['citations']:
if int(Z) in c['Z']:
ret.append(c)
return ret
# files to search
file_list = glob.glob("*.json")
refdata = bse.read_references("refs/REFERENCES.json")
for f in file_list:
fbase = os.path.splitext(f)[0]
file_data = bse.read_json_basis(f)
# for each element
for el,data in file_data['basisSetElements'].items():
el = int(el)
if not 'elementReferences' in data:
continue
elref = data['elementReferences']
elsym = bse.lut.element_sym_from_Z(el)
if len(elref) > 1:
raise RuntimeError("Multiple references")
#elif len(elref == 0)
# continue
elref = elref[0]
reffile = os.path.join(ref_dir, elref + '.json')
file_ref_json = None
if os.path.isfile(reffile):
with open(reffile, 'r') as ftmp:
file_ref_json = json.loads(ftmp.read())
else:
print("Missing file {}".format(reffile))
continue
elref_data = find_Z(file_ref_json, el)
new_references = []
for d in elref_data:
if 'article' in d:
new_references.append(d['article'])
if not d['article'] in refdata:
print("File: ", f)
print("reffile: ", reffile)
raise RuntimeError("Reference {} not found in REFERENCES".format(d['article']))
else:
print("No article: {}".format(reffile))
#print(" Articles: ", new_references)
data['elementReferences'] = new_references
#os.rename(f, f+'.old')
bse.write_json_basis(f, file_data)