def xtc_coords(filename,idxs=None): """ Get the coordinates """ try: import xdrfile.libxdrfile2 as libxdr except ImportError: raise ImportError("libxdrfile2 library needed for xtc trajectories!") file = libxdr.xdrfile_open(filename, "r") natoms = libxdr.read_xtc_natoms(filename) nframes, offsets = libxdr.read_xtc_numframes(filename) box = np.zeros((3,3),dtype=np.float32) config = np.zeros((natoms,3),dtype=np.float32) if idxs is not None: # Seek to read frames specified by idxs. nframes = len(idxs) coords = np.zeros((nframes,3,natoms),dtype="float") for i in xrange(nframes): libxdr.xdr_seek(file,offsets[idxs[i]],0) status,step,timestmp,prec = libxdr.read_xtc(file, box, config) coords[i,:,:] = config.astype("float").T else: coords = np.zeros((nframes,3,natoms),dtype=np.float32) status = libxdr.exdrOK i = 0 while status == libxdr.exdrOK: status,step,timestmp,prec = libxdr.read_xtc(file, box, config) coords[i,:,:] = config.astype("float").T i += 1 libxdr.xdrfile_close(file) return coords
def _get_frame(self, index): libxdrfile2.xdr_seek(self.xdr_fp, long(self.offsets[index]), libxdrfile2.SEEK_SET) status, step, ftime = self._read(self.xdr_fp, self.box, self.x) # print status, step, ftime, prec self.x *= 10 # convert to angstrom self.box *= 10 # convert to angstrom self.time = ftime return self.box, self.x, self.time
def _get_frame( self, index ): libxdrfile2.xdr_seek( self.xdr_fp, long( self.offsets[ index ] ), libxdrfile2.SEEK_SET ) status, step, ftime = self._read( self.xdr_fp, self.box, self.x ) # print status, step, ftime, prec self.x *= 10 # convert to angstrom self.box *= 10 # convert to angstrom self.time = ftime return self.box, self.x, self.time