[docs]classTXYZReader(base.ReaderBase):"""Reads from a TXYZ file"""format=['TXYZ','ARC']# these are assumed!units={'time':'ps','length':'Angstrom'}_Timestep=base.Timestepdef__init__(self,filename,**kwargs):super(TXYZReader,self).__init__(filename,**kwargs)# the filename has been parsed to be either be foo.xyz or foo.xyz.bz2 by# coordinates::core.py so the last file extension will tell us if it is# bzipped or notroot,ext=os.path.splitext(self.filename)self.xyzfile=util.anyopen(self.filename)self._cache=dict()# Check if file has box information savedwithutil.openany(self.filename)asinp:inp.readline()line=inp.readline()# If second line has float at second position, we have box infotry:float(line.split()[1])exceptValueError:self.periodic=Falseelse:self.periodic=Trueself.ts=self._Timestep(self.n_atoms,**self._ts_kwargs)self._read_next_timestep()@property@cached('n_atoms')defn_atoms(self):"""number of atoms in a frame"""withutil.anyopen(self.filename)asf:n=f.readline().split()[0]# need to check type of nreturnint(n)@property@cached('n_frames')defn_frames(self):try:returnself._read_xyz_n_frames()exceptIOError:return0def_read_xyz_n_frames(self):# the number of lines in the XYZ file will be 1 greater than the# number of atomslinesPerFrame=self.n_atoms+1ifself.periodic:linesPerFrame+=1counter=0offsets=[]withutil.anyopen(self.filename)asf:line=Truewhileline:ifnotcounter%linesPerFrame:offsets.append(f.tell())line=f.readline()counter+=1# need to check this is an integer!n_frames=int(counter/linesPerFrame)self._offsets=offsetsreturnn_framesdef_read_frame(self,frame):self.xyzfile.seek(self._offsets[frame])self.ts.frame=frame-1# gets +1'd in nextreturnself._read_next_timestep()def_read_next_timestep(self,ts=None):# check that the timestep object existsiftsisNone:ts=self.tsf=self.xyzfiletry:# we assume that there is only one header line per framef.readline()ifself.periodic:ts.dimensions=f.readline().split()# convert all entries at the end once for optimal speedtmp_buf=[]foriinrange(self.n_atoms):tmp_buf.append(f.readline().split()[2:5])ts.positions=tmp_bufts.frame+=1returntsexcept(ValueError,IndexError)aserr:raiseEOFError(err)def_reopen(self):self.close()self.open_trajectory()defopen_trajectory(self):ifself.xyzfileisnotNone:raiseIOError(errno.EALREADY,'TXYZ file already opened',self.filename)self.xyzfile=util.anyopen(self.filename)# reset tsts=self.tsts.frame=-1returnself.xyzfile

[docs]defclose(self):"""Close arc trajectory file if it was open."""ifself.xyzfileisNone:returnself.xyzfile.close()self.xyzfile=None