# Path: molmod/units.py
# def parse_unit(expression):
# 
# Path: molmod/io/common.py
# class SlicedReader(object):
#     """Base class for readers that can read a slice of all the frames"""
# 
#     def __init__(self, f, sub=slice(None)):
#         """
#            Argument:
#             | ``f``  --  a filename or a file-like object
# 
#            Optional argument:
#             | ``sub``  --  a slice indicating which frames to read/skip
# 
#         """
#         if isinstance(f, basestring):
#             self._auto_close = True
#             self._f = open(f)
#         else:
#             self._auto_close = False
#             self._f = f
#         self._sub = sub
#         self._counter = 0
# 
#     def __del__(self):
#         """Clean up the open file"""
#         if self._auto_close:
#             self._f.close()
# 
#     def _read_frame(self):
#         """Read a single frame from the trajectory"""
#         raise NotImplementedError
# 
#     def _skip_frame(self):
#         """Skip a single frame from the trajectory"""
#         raise NotImplementedError
# 
#     def __iter__(self):
#         return self
# 
#     def __next__(self):
#         """Get the next frame from the file, taking into account the slice
# 
#            This method is part of the iterator protocol.
#         """
#         # skip frames as requested
#         while not slice_match(self._sub, self._counter):
#             self._skip_frame()
#             self._counter += 1
# 
#         result = self._read_frame()
#         self._counter += 1
#         return result
# 
# class FileFormatError(Exception):
#     """Is raised when unexpected data is encountered while reading a file"""
#     pass


from builtins import range
from molmod.units import picosecond, amu, angstrom, atm, deg
from molmod.io.common import SlicedReader, FileFormatError
import numpy as np
             | ``f``  --  a filename or a file-like object

           Optional arguments:
             | ``sub``  --  a slice indicating the frames to be skipped/selected
             | ``pos_unit``, ``vel_unit``, ``frc_unit``, ``time_unit``,
               ``mass_unit``  --  The conversion factors for the unit conversion
                from the units in the data file to atomic units. The defaults of
                these optional arguments correspond to the defaults of dlpoly.

           When the file starts with a line that satisfies the following
           conditions, it is assumed that this is a history restart file:

           * line consists of 6 words
           * first word equals 'timestep'
           * the following for words are integers
           * the last word is a float
        """
        SlicedReader.__init__(self, f, sub)
        self._counter = 1 # make our counter compatible with dlpoly
        self.pos_unit = pos_unit
        self.vel_unit = vel_unit
        self.frc_unit = frc_unit
        self.time_unit = time_unit
        self.mass_unit = mass_unit
        restart = self._detect_restart()
        if restart is None:
            try:
                self.header = next(self._f)[:-1]
                integers = tuple(int(word) for word in next(self._f).split())
                if len(integers) != 3:

# Path: molmod/units.py
# def parse_unit(expression):
# 
# Path: molmod/io/common.py
# class SlicedReader(object):
#     """Base class for readers that can read a slice of all the frames"""
# 
#     def __init__(self, f, sub=slice(None)):
#         """
#            Argument:
#             | ``f``  --  a filename or a file-like object
# 
#            Optional argument:
#             | ``sub``  --  a slice indicating which frames to read/skip
# 
#         """
#         if isinstance(f, basestring):
#             self._auto_close = True
#             self._f = open(f)
#         else:
#             self._auto_close = False
#             self._f = f
#         self._sub = sub
#         self._counter = 0
# 
#     def __del__(self):
#         """Clean up the open file"""
#         if self._auto_close:
#             self._f.close()
# 
#     def _read_frame(self):
#         """Read a single frame from the trajectory"""
#         raise NotImplementedError
# 
#     def _skip_frame(self):
#         """Skip a single frame from the trajectory"""
#         raise NotImplementedError
# 
#     def __iter__(self):
#         return self
# 
#     def __next__(self):
#         """Get the next frame from the file, taking into account the slice
# 
#            This method is part of the iterator protocol.
#         """
#         # skip frames as requested
#         while not slice_match(self._sub, self._counter):
#             self._skip_frame()
#             self._counter += 1
# 
#         result = self._read_frame()
#         self._counter += 1
#         return result
# 
# class FileFormatError(Exception):
#     """Is raised when unexpected data is encountered while reading a file"""
#     pass


# Path: molmod/io/dlpoly.py
from builtins import range
from molmod.units import picosecond, amu, angstrom, atm, deg
from molmod.io.common import SlicedReader, FileFormatError
import numpy as np

             | ``f``  --  a filename or a file-like object

           Optional arguments:
             | ``sub``  --  a slice indicating the frames to be skipped/selected
             | ``pos_unit``, ``vel_unit``, ``frc_unit``, ``time_unit``,
               ``mass_unit``  --  The conversion factors for the unit conversion
                from the units in the data file to atomic units. The defaults of
                these optional arguments correspond to the defaults of dlpoly.

           When the file starts with a line that satisfies the following
           conditions, it is assumed that this is a history restart file:

           * line consists of 6 words
           * first word equals 'timestep'
           * the following for words are integers
           * the last word is a float
        """
        SlicedReader.__init__(self, f, sub)
        self._counter = 1 # make our counter compatible with dlpoly
        self.pos_unit = pos_unit
        self.vel_unit = vel_unit
        self.frc_unit = frc_unit
        self.time_unit = time_unit
        self.mass_unit = mass_unit
        restart = self._detect_restart()
        if restart is None:
            try:
                self.header = next(self._f)[:-1]
                integers = tuple(int(word) for word in next(self._f).split())
                if len(integers) != 3:

                    raise FileFormatError("Second line must contain three integers.")