Exemple #1
0
 def read_frame_from_file(self, file_name, frame_num):
     # note: this only needs to return the file pointers -- but should
     # only do so once that frame has been written!
     basename = os.path.basename(file_name)
     # basename should be in the format [0-9]+\.trr (as set by the
     # trajectory_filename method)
     # file_number = int(basename.split('.')[0])
     try:
         xyz, vel, box = self.read_frame_data(file_name, frame_num)
     except (IndexError, OSError, IOError) as e:
         # this means that no such frame exists yet, so we return None
         # IndexError in older version, OSError more recently (specific
         # MDTraj error)
         logger.debug("Expected exception caught: " + str(e))
         close_file_descriptors(basename)
         return None
     except RuntimeError as e:
         # TODO: matches "TRR read error"
         logger.debug("Received partial frame for %s %d", file_name,
                      frame_num + 1)
         return 'partial'
     else:
         logger.debug("Creating snapshot")
         snapshot = ExternalMDSnapshot(file_name=file_name,
                                       file_position=frame_num,
                                       engine=self)
         return snapshot
Exemple #2
0
    def write_frame_to_file(self, filename, snapshot, mode='w'):
        if os.path.isfile(filename):
            # stop if we already have this file; could also happen because
            # of a weird behavior in a mover. You must remove the files if
            # you don't want them.
            raise RuntimeError("File " + str(filename) + " exists. " +
                               "Preventing overwrite.")
        # type control before passing things to Cython code
        xyz = np.asarray([snapshot.xyz], dtype=np.float32)
        time = np.asarray([0.0], dtype=np.float32)
        step = np.asarray([0], dtype=np.int32)
        box = np.asarray([np.asarray(snapshot.box_vectors)], dtype=np.float32)
        lambd = np.asarray([0.0], dtype=np.float32)
        vel = np.asarray([np.asarray(snapshot.velocities)], dtype=np.float32)

        try:
            trr = TRRTrajectoryFile(filename, mode)
            trr._write(xyz, time, step, box, lambd, vel)
        finally:
            trr.close()
            close_file_descriptors(filename)