def read(cls, source, format=None, **kwargs): """ Class method to read Atoms object from file `source` according to `format` If `format` is None, filetype is inferred from filename. Returns a new Atoms instance; to read into an existing Atoms object, use the read_from() method. If `source` corresponds to a known format then it used to construct an appropriate iterator from the :attr:`AtomsReaders` dictionary. See :ref:`fileformats` for a list of supported file formats. If `source` corresponds to an unknown format then it is expected to be an iterator returning :class:`Atoms` objects. """ if isinstance(source, basestring) and '@' in os.path.basename(source): source, frame = source.split('@') if source.endswith('.db'): source = source + '@' + frame format = 'db' else: frame = parse_slice(frame) if 'frame' in kwargs: raise ValueError( "Conflicting frame references given: kwarg frame=%r and @-reference %r" % (kwargs['frame'], frame)) if not isinstance(frame, int): raise ValueError( "Frame @-reference %r does not resolve to single frame" % frame) kwargs['frame'] = frame from quippy.io import AtomsReaders filename, source, format = infer_format(source, format, AtomsReaders) opened = False if format in AtomsReaders: source = AtomsReaders[format](source, format=format, **kwargs) opened = True if isinstance(source, basestring): raise IOError("Don't know how to read from file '%s'" % source) if not hasattr(source, '__iter__'): raise IOError('Cannot read from %r - not an iterator' % source) at = iter(source).next() if not isinstance(at, cls): raise ValueError('Object %r read from %r is not Atoms instance' % (at, source)) if opened and hasattr(source, 'close'): source.close() if filename is not None: at.filename = filename return at
def __init__(self, source, format=None, start=None, stop=None, step=None, cache_mem_limit=-1, rename=None, **kwargs): def file_exists(f): return f == "stdin" or os.path.exists(f) or len(glob.glob(f)) > 0 self.source = source self.format = format self._start = start self._stop = stop self._step = step self.cache_mem_limit = cache_mem_limit logging.debug('AtomsReader memory limit %r' % self.cache_mem_limit) self._source_len = None self._cache_dict = {} self._cache_list = [] self._cache_mem_usage = [] self.opened = False self.reader = source self.rename = rename if isinstance(self.reader, basestring): if '@' in self.reader: self.reader, frames = self.reader.split('@') frames = parse_slice(frames) if start is not None or stop is not None or step is not None: raise ValueError( 'Conflicting frame references start=%r stop=%r step=%r and @-sytnax %r' % (start, stop, step, frames)) if isinstance(frames, int): if frames >= 0: frames = slice(frames, frames + 1, +1) else: frames = slice(frames, frames - 1, -1) self._start, self._stop, self._step = frames.start, frames.stop, frames.step self.filename = self.reader self.opened = True if self.reader in AtomsReaders: if format is None: format = self.reader elif format != 'string': self.reader = os.path.expanduser(self.reader) glob_list = sorted(glob.glob(self.reader)) if (len(glob_list) == 0): raise IOError("input file '%s' not found" % self.reader) if len(glob_list) > 1: self.reader = glob_list else: self.reader = glob_list[0] filename, self.reader, new_format = infer_format( self.reader, format, AtomsReaders) if format is None: format = new_format # special cases if source is a list or tuple of filenames or Atoms objects is_filename_sequence = False is_list_of_atoms = False if isinstance(self.reader, list) or isinstance(self.reader, tuple): is_filename_sequence = True is_list_of_atoms = True for item in self.reader: if '@' in item: item = item[:item.index('@')] if not isinstance(item, basestring) or not file_exists(item): is_filename_sequence = False if not isinstance(item, Atoms): is_list_of_atoms = False if is_filename_sequence: self.reader = AtomsSequenceReader(self.reader, format=format, **kwargs) elif is_list_of_atoms: # dummy reader which copies from an existing list or tuple of Atoms objects self.reader = [at.copy() for at in self.reader] else: if format is None: format = self.reader.__class__ if format in AtomsReaders: self.reader = AtomsReaders[format](self.reader, **kwargs) # check if reader is still a string or list of strings - indicates missing files or unknown format if isinstance(self.reader, basestring): raise IOError("Cannot read Atoms from file %s" % self.reader) elif isinstance(self.reader, list): is_list_of_strings = True for item in self.reader: if not isinstance(item, basestring): is_list_of_strings = False break if is_list_of_strings: raise IOError("Cannot read Atoms from files %s" % self.reader) if isinstance(self.reader, AtomsReader): self.reader = AtomsReaderCopier(self.reader) if not hasattr(self.reader, '__iter__'): # call Atoms constructor - this has beneficial side effect of making a copy self.reader = [Atoms(self.reader)]
'--tetra', action='store_true', help="Convert Si-O-Si bonds in tetrahedra to Si-Si bonds.") p.add_option('-p', '--plot', action='store_true') p.add_option( '-n', '--no-clear', action='store_true', help="Do not clear figure before plotting. Useful for comparisons") opt, infiles = p.parse_args() # check for proper format of --range if opt.range is not None: try: opt.range = parse_slice(opt.range) except: p.error( 'Cannot parse range "%s" - should be in format [start]:[stop][:step]' % opt.range) else: # Default is all frames opt.range = slice(0, None, None) if isinstance(opt.range, int): if opt.range >= 0: opt.range = slice(opt.range, opt.range + 1, +1) else: opt.range = slice(opt.range, opt.range - 1, -1) for infile in infiles: