Example #1
0
    def open_trajectory(self):
        """Open xdr trajectory file.

        :Returns: pointer to XDRFILE (and sets self.xdrfile)
        :Raises:  :exc:`IOError` with code EALREADY if file was already opened or
                  ENOENT if the file cannot be found
        """
        if not self.xdrfile is None:
            raise IOError(errno.EALREADY, 'XDR file already opened',
                          self.filename)
        if not os.path.exists(self.filename):
            # must check; otherwise might segmentation fault
            raise IOError(errno.ENOENT, 'XDR file not found', self.filename)
        self.xdrfile = libxdrfile2.xdrfile_open(self.filename, 'r')
        # reset ts
        ts = self.ts
        ts.status = libxdrfile2.exdrOK
        ts.frame = 0
        ts.step = 0
        ts.time = 0
        # additional data for XTC
        ts.prec = 0
        # additional data for TRR
        ts.lmbda = 0
        return self.xdrfile
Example #2
0
    def open_trajectory(self):
        """Open xdr trajectory file.

        :Returns: pointer to XDRFILE (and sets self.xdrfile)
        :Raises:  :exc:`IOError` with code EALREADY if file was already opened or
                  ENOENT if the file cannot be found
        """
        if not self.xdrfile is None:
            raise IOError(errno.EALREADY, 'XDR file already opened', self.filename)
        if not os.path.exists(self.filename):
            # must check; otherwise might segmentation fault
            raise IOError(errno.ENOENT, 'XDR file not found', self.filename)
        self.xdrfile = libxdrfile2.xdrfile_open(self.filename, 'r')
        # reset ts
        ts = self.ts
        ts.status = libxdrfile2.exdrOK
        ts.frame = 0
        ts.step = 0
        ts.time = 0
        # additional data for XTC
        ts.prec = 0
        # additional data for TRR
        ts.lmbda = 0
        return self.xdrfile
Example #3
0
    def __init__(self, filename, numatoms, start=0, step=1, delta=None, precision=1000.0, remarks=None,
                 convert_units=None):
        """ Create a new TrjWriter

        :Arguments:
          *filename*
             name of output file
          *numatoms*
             number of atoms in trajectory file

        :Keywords:
          *start*
             starting timestep; only used when *delta* is set.
          *step*
             skip between subsequent timesteps; only used when *delta* is set.
          *delta*
             timestep to use. If set will override any time information contained in the
             passed :class:`Timestep` objects; otherwise that will be used. If in the
             latter case :attr:`~Timestep.time` is unavailable the TrjWriter will default
             to setting the trajectory time at 1 MDAnalysis unit (typically 1ps) per step.
          *precision*
              accuracy for lossy XTC format as a power of 10 (ignored
              for TRR) [1000.0]
          *convert_units*
             ``True``: units are converted to the MDAnalysis base format; ``None`` selects
             the value of :data:`MDAnalysis.core.flags` ['convert_lengths'].
             (see :ref:`flags-label`)

        .. versionchanged:: 0.8.0
           The TRR writer is now able to write TRRs without coordinates/velocities/forces,
           depending on the properties available in the :class:`Timestep` objects passed to
           :meth:`~TRRWriter.write`.
        """
        assert self.format in ('XTC', 'TRR')

        if numatoms == 0:
            raise ValueError("TrjWriter: no atoms in output trajectory")
        self.filename = filename
        # Convert filename to ascii because of SWIG bug.
        # See: http://sourceforge.net/p/swig/feature-requests/75
        # Only needed for Python < 3
        if sys.version_info[0] < 3:
            if isinstance(filename, unicode):
                self.filename = filename.encode("UTF-8")

        if convert_units is None:
            convert_units = MDAnalysis.core.flags['convert_lengths']
        self.convert_units = convert_units  # convert length and time to base units on the fly?
        self.numatoms = numatoms

        self.frames_written = 0
        self.start = start
        self.step = step
        self.delta = delta
        self.remarks = remarks
        self.precision = precision  # only for XTC
        self.xdrfile = libxdrfile2.xdrfile_open(self.filename, 'w')

        self.ts = None
        # To flag empty properties to be skipped when writing a TRR it suffices to pass an empty 2D array with shape(
        # natoms,0)
        if self.format == 'TRR':
            self._emptyarr = numpy.array([], dtype=numpy.float32).reshape(self.numatoms, 0)
Example #4
0
    def __init__(self,
                 filename,
                 numatoms,
                 start=0,
                 step=1,
                 delta=None,
                 precision=1000.0,
                 remarks=None,
                 convert_units=None):
        """ Create a new TrjWriter

        :Arguments:
          *filename*
             name of output file
          *numatoms*
             number of atoms in trajectory file

        :Keywords:
          *start*
             starting timestep; only used when *delta* is set.
          *step*
             skip between subsequent timesteps; only used when *delta* is set.
          *delta*
             timestep to use. If set will override any time information contained in the
             passed :class:`Timestep` objects; otherwise that will be used. If in the
             latter case :attr:`~Timestep.time` is unavailable the TrjWriter will default
             to setting the trajectory time at 1 MDAnalysis unit (typically 1ps) per step.
          *precision*
              accuracy for lossy XTC format as a power of 10 (ignored
              for TRR) [1000.0]
          *convert_units*
             ``True``: units are converted to the MDAnalysis base format; ``None`` selects
             the value of :data:`MDAnalysis.core.flags` ['convert_lengths'].
             (see :ref:`flags-label`)

        .. versionchanged:: 0.8.0
           The TRR writer is now able to write TRRs without coordinates/velocities/forces,
           depending on the properties available in the :class:`Timestep` objects passed to
           :meth:`~TRRWriter.write`.
        """
        assert self.format in ('XTC', 'TRR')

        if numatoms == 0:
            raise ValueError("TrjWriter: no atoms in output trajectory")
        self.filename = filename
        # Convert filename to ascii because of SWIG bug.
        # See: http://sourceforge.net/p/swig/feature-requests/75
        # Only needed for Python < 3
        if sys.version_info[0] < 3:
            if isinstance(filename, unicode):
                self.filename = filename.encode("UTF-8")

        if convert_units is None:
            convert_units = MDAnalysis.core.flags['convert_lengths']
        self.convert_units = convert_units  # convert length and time to base units on the fly?
        self.numatoms = numatoms

        self.frames_written = 0
        self.start = start
        self.step = step
        self.delta = delta
        self.remarks = remarks
        self.precision = precision  # only for XTC
        self.xdrfile = libxdrfile2.xdrfile_open(self.filename, 'w')

        self.ts = None
        # To flag empty properties to be skipped when writing a TRR it suffices to pass an empty 2D array with shape(
        # natoms,0)
        if self.format == 'TRR':
            self._emptyarr = numpy.array([], dtype=numpy.float32).reshape(
                self.numatoms, 0)