Package diffpy :: Package Structure :: Package Parsers :: Module P_bratoms
[hide private]
[frames] | no frames]

Source Code for Module diffpy.Structure.Parsers.P_bratoms

  1  ############################################################################## 
  2  # 
  3  # Structure         by DANSE Diffraction group 
  4  #                   Simon J. L. Billinge 
  5  #                   (c) 2007 trustees of the Michigan State University. 
  6  #                   All rights reserved. 
  7  # 
  8  # File coded by:    C. L. Farrow 
  9  # 
 10  # See AUTHORS.txt for a list of people who contributed. 
 11  # See LICENSE.txt for license information. 
 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   
27 -class P_bratoms(StructureParser):
28 """Parser for Bruce Ravel's Atoms structure format. 29 """ 30 31 plist = ["a", "b", "c", "alpha", "beta", "gamma"] 32
33 - def __init__(self):
34 StructureParser.__init__(self) 35 self.format = "bratoms" 36 return
37
38 - def parseLines(self, lines):
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 # Count the lines 55 ln = 0 56 try: 57 58 for line in lines: 59 ln += 1 60 61 # Strip comments from the line 62 for c in comlist: 63 idx = line.find(c) 64 if idx != -1: 65 line = line[:idx] 66 67 # Move on if there is not a line 68 if not line: continue 69 70 # Move on if there was only white space in the line 71 sline = line.split() 72 if not sline: continue 73 74 # Check if we have atoms following 75 if sline[0].startswith("atom"): 76 anext = True 77 continue 78 79 # Check for title 80 if sline[0].startswith("title"): 81 if title: title += "\n" 82 title += line[5:] 83 continue 84 85 # Get rid of pesky "=" and "," signs 86 while "=" in sline: sline.remove("=") 87 while "," in sline: sline.remove(",") 88 89 # space group 90 if sline and sline[0].startswith("space"): 91 meta["space"] = line[5:].strip() 92 continue 93 94 # output 95 if sline and sline[0].startswith("output"): 96 meta["output"] = line[6:].strip() 97 continue 98 99 # shift 100 if sline and sline[0].startswith("shift"): 101 meta["shift"] = line[5:].strip() 102 continue 103 104 # Check for other metadata 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 # Check for lattice information. 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 # Check for atom information 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 # Make sure we have atoms. 143 if len(atoms) == 0: 144 raise StructureFormatError("File contains no atoms") 145 146 # Make sure we have unit cell parameters 147 if pdict["a"] is None: 148 emsg = "Missing definition of cell parameter" 149 raise StructureFormatError(emsg) 150 151 # Fill in optional information if it was missing. 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 # End of parseLines 172
173 - def toLines(self, stru):
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 # Check the metadata for consistency 181 meta = getattr(stru, 'bratoms', {}) 182 183 # Make the lines 184 lines = [] 185 186 # title 187 titles = stru.title.split("\n") 188 for t in titles: 189 lines.append("title = %s" % t) 190 191 # a, b, c 192 lines.append("a = %f b = %f c = %f" % (lat.a, lat.b, lat.c)) 193 # alpha, beta, gamma 194 lines.append("alpha = %f beta = %f gamma = %f" % \ 195 (lat.alpha, lat.beta, lat.gamma)) 196 197 # space 198 lines.append("space = %s" % meta.get("space", "P 1")) 199 200 # edge 201 lines.append("edge = %s" % meta.get("edge", "K")) 202 203 # core 204 tag = meta.get("core") or stru[0].name or stru[0].element.title() 205 lines.append("core = %s" % tag) 206 207 # rmax 208 lines.append("rmax = %s" % meta.get("rmax", 6.0)) 209 210 # Other keys 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 # Add atoms 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 # End of toLines 236 237 # End of class P_pdffit 238 239 # Routines 240
241 -def getParser():
242 return P_bratoms()
243 244 # End of file 245