@@ -134,6 +134,81 @@ def parseLines(self, lines):
134134 raise e .with_traceback (exc_traceback )
135135 return self .stru
136136
137+ def parse_lines (self , lines ):
138+ """Parse list of lines in DISCUS format.
139+
140+ Parameters
141+ ----------
142+ lines : list of str
143+ List of lines from the input file.
144+
145+ Returns
146+ -------
147+ PDFFitStructure
148+ Parsed `PDFFitStructure` instance.
149+
150+ Raises
151+ ------
152+ StructureFormatError
153+ If the file is not in DISCUS format.
154+ """
155+ self .lines = lines
156+ ilines = self ._linesIterator ()
157+ self .stru = PDFFitStructure ()
158+ record_parsers = {
159+ "cell" : self ._parse_cell ,
160+ "format" : self ._parse_format ,
161+ "generator" : self ._parse_not_implemented ,
162+ "molecule" : self ._parse_not_implemented ,
163+ "ncell" : self ._parse_ncell ,
164+ "spcgr" : self ._parse_spcgr ,
165+ "symmetry" : self ._parse_not_implemented ,
166+ "title" : self ._parse_title ,
167+ "shape" : self ._parse_shape ,
168+ }
169+ try :
170+ # parse header
171+ for self .line in ilines :
172+ words = self .line .split ()
173+ if not words or words [0 ][0 ] == "#" :
174+ continue
175+ if words [0 ] == "atoms" :
176+ break
177+ rp = record_parsers .get (words [0 ], self ._parse_unknown_record )
178+ rp (words )
179+ # check if cell has been defined
180+ if not self .cell_read :
181+ emsg = "%d: unit cell not defined" % self .nl
182+ raise StructureFormatError (emsg )
183+ # parse atoms
184+ for self .line in ilines :
185+ words = self .line .replace ("," , " " ).split ()
186+ if not words or words [0 ][0 ] == "#" :
187+ continue
188+ self ._parse_atom (words )
189+ # self consistency check
190+ exp_natoms = reduce (lambda x , y : x * y , self .stru .pdffit ["ncell" ])
191+ # only check if ncell record exists
192+ if self .ncell_read and exp_natoms != len (self .stru ):
193+ emsg = "Expected %d atoms, read %d." % (
194+ exp_natoms ,
195+ len (self .stru ),
196+ )
197+ raise StructureFormatError (emsg )
198+ # take care of superlattice
199+ if self .stru .pdffit ["ncell" ][:3 ] != [1 , 1 , 1 ]:
200+ latpars = list (self .stru .lattice .abcABG ())
201+ superlatpars = [latpars [i ] * self .stru .pdffit ["ncell" ][i ] for i in range (3 )] + latpars [3 :]
202+ superlattice = Lattice (* superlatpars )
203+ self .stru .place_in_lattice (superlattice )
204+ self .stru .pdffit ["ncell" ] = [1 , 1 , 1 , exp_natoms ]
205+ except (ValueError , IndexError ):
206+ exc_type , exc_value , exc_traceback = sys .exc_info ()
207+ emsg = "%d: file is not in DISCUS format" % self .nl
208+ e = StructureFormatError (emsg )
209+ raise e .with_traceback (exc_traceback )
210+ return self .stru
211+
137212 def toLines (self , stru ):
138213 """Convert `Structure` stru to a list of lines in DISCUS format.
139214
0 commit comments