1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """Parser for Bruce Ravel's Atoms structure format
16 """
17
18 __id__ = "$Id: P_bratoms.py 2825 2009-03-09 04:33:12Z juhas $"
19
20 import sys
21 import numpy
22
23 from diffpy.Structure import Lattice, Atom, StructureFormatError
24 from diffpy.Structure.bratomsstructure import BRAtomsStructure
25 from diffpy.Structure.Parsers import StructureParser
26
28 """Parser for Bruce Ravel's Atoms structure format.
29 """
30
31 plist = ["a", "b", "c", "alpha", "beta", "gamma"]
32
37
39 """Parse list of lines in atoms format.
40
41 Return Structure object or raise StructureFormatError.
42 """
43
44 comlist = ["#", "%", "!", "*"]
45 atoms = []
46 title = ""
47 anext = False
48 sg = None
49 structure = BRAtomsStructure()
50 meta = structure.bratoms
51 pdict = dict.fromkeys(self.plist)
52
53
54
55 ln = 0
56 try:
57
58 for line in lines:
59 ln += 1
60
61
62 for c in comlist:
63 idx = line.find(c)
64 if idx != -1:
65 line = line[:idx]
66
67
68 if not line: continue
69
70
71 sline = line.split()
72 if not sline: continue
73
74
75 if sline[0].startswith("atom"):
76 anext = True
77 continue
78
79
80 if sline[0].startswith("title"):
81 if title: title += "\n"
82 title += line[5:]
83 continue
84
85
86 while "=" in sline: sline.remove("=")
87 while "," in sline: sline.remove(",")
88
89
90 if sline and sline[0].startswith("space"):
91 meta["space"] = line[5:].strip()
92 continue
93
94
95 if sline and sline[0].startswith("output"):
96 meta["output"] = line[6:].strip()
97 continue
98
99
100 if sline and sline[0].startswith("shift"):
101 meta["shift"] = line[5:].strip()
102 continue
103
104
105 while sline and sline[0].strip() in meta:
106 key = sline.pop(0).strip()
107 if key == "central": key = "core"
108 meta[key] = sline.pop(0).strip()
109
110
111 while sline and sline[0].strip() in self.plist:
112 key = sline.pop(0).strip()
113 pdict[key] = float(sline.pop(0))
114
115
116 if sline and anext:
117
118 elraw = sline.pop(0).strip()
119 el = elraw[:1].upper() + elraw[1:].lower()
120 x = float(sline.pop(0))
121 y = float(sline.pop(0))
122 z = float(sline.pop(0))
123
124 tag = ""
125 if sline:
126 tag = sline.pop(0).strip()
127 occ = 1.0
128 if sline:
129 occ = float(sline.pop(0))
130
131 a = Atom( atype = el,
132 xyz = [x,y,z],
133 name = tag,
134 occupancy = occ)
135
136 atoms.append(a)
137
138 except (ValueError, IndexError), e:
139 emsg = "%d: file is not in Atoms format" % ln
140 raise StructureFormatError(emsg)
141
142
143 if len(atoms) == 0:
144 raise StructureFormatError("File contains no atoms")
145
146
147 if pdict["a"] is None:
148 emsg = "Missing definition of cell parameter"
149 raise StructureFormatError(emsg)
150
151
152 if pdict["alpha"] is None:
153 pdict["alpha"] = 90.0
154 if pdict["beta"] is None:
155 pdict["beta"] = pdict["alpha"]
156 if pdict["gamma"] is None:
157 pdict["gamma"] = pdict["alpha"]
158 if pdict["b"] is None:
159 pdict["b"] = pdict["a"]
160 if pdict["c"] is None:
161 pdict["c"] = pdict["a"]
162
163 if meta['core'] is None:
164 meta['core'] = atoms[0].element
165
166 lat = Lattice(**pdict)
167 structure.title = title
168 structure.lattice = lat
169 structure.extend(atoms)
170 return structure
171
172
174 """Convert Structure stru to a list of lines in PDFfit format.
175
176 Return list of strings.
177 """
178 lat = stru.lattice
179
180
181 meta = getattr(stru, 'bratoms', {})
182
183
184 lines = []
185
186
187 titles = stru.title.split("\n")
188 for t in titles:
189 lines.append("title = %s" % t)
190
191
192 lines.append("a = %f b = %f c = %f" % (lat.a, lat.b, lat.c))
193
194 lines.append("alpha = %f beta = %f gamma = %f" % \
195 (lat.alpha, lat.beta, lat.gamma))
196
197
198 lines.append("space = %s" % meta.get("space", "P 1"))
199
200
201 lines.append("edge = %s" % meta.get("edge", "K"))
202
203
204 tag = meta.get("core") or stru[0].name or stru[0].element.title()
205 lines.append("core = %s" % tag)
206
207
208 lines.append("rmax = %s" % meta.get("rmax", 6.0))
209
210
211 if meta.get('shift'):
212 lines.append("shift = %s" % meta["shift"])
213 if meta.get('output'):
214 lines.append("output = %s" % meta["output"])
215 if meta.get('nitrogen'):
216 lines.append("nitrogen = %s" % meta["nitrogen"])
217 if meta.get('argon'):
218 lines.append("argon = %s" % meta["argon"])
219 if meta.get('krypton'):
220 lines.append("krypton = %s" % meta["krypton"])
221
222
223 lines.append("atoms")
224 lines.append("%-8s%-10s%-10s%-10s%-10s%s" %
225 ("!", "x", "y", "z", "tag", "occ"))
226 for a in stru:
227 el = a.element.title()
228 name = a.name or el
229 x, y, z = a.xyz
230 occ = a.occupancy
231 lines.append("%-7s %-9f %-9f %-9f %-9s %f" % \
232 (el, x, y, z, name, occ))
233
234 return lines
235
236
237
238
239
240
243
244
245