def filetype(filename): """Try to guess the type of the file.""" if os.path.isdir(filename): # Potentially a BundleTrajectory if BundleTrajectory.is_bundle(filename): return 'bundle' elif os.path.normpath(filename) == 'states': return 'eon' else: raise IOError('Directory: ' + filename) if filename.startswith('pg://'): return 'postgresql' fileobj = open(filename, 'rU') s3 = fileobj.read(3) if len(s3) == 0: raise IOError('Empty file: ' + filename) if s3.startswith('{"'): return 'json' if filename.endswith('.db'): return 'db' if filename.lower().endswith('.cmr'): return 'cmr' if is_tarfile(filename): return 'gpw' if s3 == 'CDF': from ase.io.pupynere import NetCDFFile nc = NetCDFFile(filename) if 'number_of_dynamic_atoms' in nc.dimensions: return 'dacapo' history = nc.history if history == 'GPAW restart file': return 'gpw-nc' if history == 'ASE trajectory': return 'nc' if history == 'Dacapo': return 'dacapo' if hasattr(nc, 'file_format') and nc.file_format.startswith('ETSF'): return 'etsf' raise IOError('Unknown netCDF file!') if is_zipfile(filename): return 'vnl' fileobj.seek(0) lines = fileobj.readlines(1000) if lines[0].startswith('PickleTrajectory'): return 'traj' if (lines[1].startswith('OUTER LOOP:') or filename.lower().endswith('.cube')): return 'cube' if ' ___ ___ ___ _ _ _ \n' in lines: return 'gpaw-text' if (' &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n' in lines[:90]): return 'dacapo-text' for line in lines: if line[0] != '#': word = line.strip() if word in ['ANIMSTEPS', 'CRYSTAL', 'SLAB', 'POLYMER', 'MOLECULE']: return 'xsf' filename_v = os.path.basename(filename) if 'POSCAR' in filename_v or 'CONTCAR' in filename_v: return 'vasp' if 'OUTCAR' in filename_v: return 'vasp_out' if filename.lower().endswith('.exi'): return 'exi' if filename.lower().endswith('.mol'): return 'mol' if filename.lower().endswith('.pdb'): return 'pdb' if filename.lower().endswith('.cif'): return 'cif' if filename.lower().endswith('.struct'): return 'struct' if filename.lower().endswith('.struct_out'): return 'struct_out' fileobj.seek(0) while True: line = fileobj.readline() if not line: break if 'Invoking FHI-aims ...' in line: return 'aims_out' if 'atom' in line: data = line.split() try: Atoms(symbols=[data[4]], positions=[[ float(data[1]), float(data[2]), float(data[3]) ]]) return 'aims' except: pass if filename.lower().endswith('.in'): fileobj.seek(0) while True: line = fileobj.readline() if not line: break if ('&system' in line) or ('&SYSTEM' in line): return 'esp_in' return 'aims' if filename.lower().endswith('.cfg'): return 'cfg' if os.path.split(filename)[1] == 'atoms.dat': return 'iwm' if filename.endswith('I_info'): return 'Cmdft' if lines[0].startswith('$coord') or os.path.basename(filename) == 'coord': return 'tmol' if (lines[0].startswith('$grad') or os.path.basename(filename) == 'gradient'): return 'tmol-gradient' if lines[0].startswith('Geometry'): return 'dftb' if filename.lower().endswith('.geom'): return 'castep_geom' if filename.lower().endswith('.castep'): return 'castep' if filename.lower().endswith('.cell'): return 'castep_cell' if s3 == '<?x': from ase.io.vtkxml import probe_vtkxml xmltype = probe_vtkxml(filename) if xmltype == 'ImageData': return 'vti' elif xmltype == 'StructuredGrid': return 'vts' elif xmltype == 'UnstructuredGrid': return 'vtu' elif xmltype is not None: raise IOError('Unknown VTK XML file!') if filename.lower().endswith('.sdf'): return 'sdf' if filename.lower().endswith('.gen'): return 'gen' if filename.lower().endswith('.con'): return 'eon' if 'ITEM: TIMESTEP\n' in lines: return 'lammps' if filename.lower().endswith('.gro'): return 'gromacs' if filename.lower().endswith('.log'): return 'gaussian_out' if filename.lower().endswith('.com'): return 'gaussian' if filename.lower().endswith('.g96'): return 'gromos' if filename.lower().endswith('.out'): return 'esp_out' if filename.endswith('.nw'): return 'nw' return 'xyz'
def filetype(filename): """Try to guess the type of the file.""" if os.path.isdir(filename): # Potentially a BundleTrajectory if BundleTrajectory.is_bundle(filename): return 'bundle' else: raise IOError('Directory: ' + filename) fileobj = open(filename) s3 = fileobj.read(3) if len(s3) == 0: raise IOError('Empty file: ' + filename) if is_tarfile(filename): return 'gpw' if s3 == 'CDF': from ase.io.pupynere import NetCDFFile nc = NetCDFFile(filename) if 'number_of_dynamic_atoms' in nc.dimensions: return 'dacapo' history = nc.history if history == 'GPAW restart file': return 'gpw-nc' if history == 'ASE trajectory': return 'nc' if history == 'Dacapo': return 'dacapo' if hasattr(nc, 'file_format') and nc.file_format.startswith('ETSF'): return 'etsf' raise IOError('Unknown netCDF file!') if is_zipfile(filename): return 'vnl' fileobj.seek(0) lines = fileobj.readlines(1000) if lines[0].startswith('PickleTrajectory'): return 'traj' if lines[1].startswith('OUTER LOOP:') or filename.lower().endswith( '.cube'): return 'cube' if ' ___ ___ ___ _ _ _ \n' in lines: return 'gpaw-text' if (' &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n' in lines[:90]): return 'dacapo-text' for line in lines: if line[0] != '#': word = line.strip() if word in ['ANIMSTEPS', 'CRYSTAL', 'SLAB', 'POLYMER', 'MOLECULE']: return 'xsf' filename_v = basename(filename) if 'POSCAR' in filename_v or 'CONTCAR' in filename_v: return 'vasp' if 'OUTCAR' in filename_v: return 'vasp_out' if filename.lower().endswith('.exi'): return 'exi' if filename.lower().endswith('.mol'): return 'mol' if filename.lower().endswith('.pdb'): return 'pdb' if filename.lower().endswith('.cif'): return 'cif' if filename.lower().endswith('.struct'): return 'struct' if filename.lower().endswith('.in'): return 'aims' if filename.lower().endswith('.out'): return 'aims_out' if filename.lower().endswith('.cfg'): return 'cfg' if os.path.split(filename)[1] == 'atoms.dat': return 'iwm' if filename.endswith('I_info'): return 'Cmdft' if lines[0].startswith('$coord'): return 'tmol' if lines[0].startswith('Geometry'): return 'dftb' if s3 == '<?x': from ase.io.vtkxml import probe_vtkxml xmltype = probe_vtkxml(filename) if xmltype == 'ImageData': return 'vti' elif xmltype == 'StructuredGrid': return 'vts' elif xmltype == 'UnstructuredGrid': return 'vtu' elif xmltype is not None: raise IOError('Unknown VTK XML file!') if filename.lower().endswith('.sdf'): return 'sdf' return 'xyz'
def filetype(filename): """Try to guess the type of the file.""" if os.path.isdir(filename): # Potentially a BundleTrajectory if BundleTrajectory.is_bundle(filename): return 'bundle' elif os.path.normpath(filename) == 'states': return 'eon' else: raise IOError('Directory: ' + filename) if filename.startswith('pg://'): return 'postgresql' fileobj = open(filename, 'rb') s3 = fileobj.read(3) if len(s3) == 0: raise IOError('Empty file: ' + filename) if s3.startswith(b'{"'): return 'json' if filename.endswith('.db'): return 'db' if filename.lower().endswith('.cmr'): return 'cmr' if is_tarfile(filename): return 'gpw' if s3 == 'CDF': from ase.io.pupynere import NetCDFFile nc = NetCDFFile(filename) if 'number_of_dynamic_atoms' in nc.dimensions: return 'dacapo' history = nc.history if history == 'GPAW restart file': return 'gpw-nc' if history == 'ASE trajectory': return 'nc' if history == 'Dacapo': return 'dacapo' if hasattr(nc, 'file_format') and nc.file_format.startswith('ETSF'): return 'etsf' raise IOError('Unknown netCDF file!') if is_zipfile(filename): return 'vnl' fileobj.seek(0) lines = fileobj.readlines(1000) if lines[0].startswith(b'AFFormatASE-Trajectory'): return 'traj' if lines[0].startswith(b'PickleTrajectory'): return 'trj' if (lines[1].startswith(b'OUTER LOOP:') or filename.lower().endswith('.cube')): return 'cube' if b' ___ ___ ___ _ _ _ \n' in lines: return 'gpaw-text' if (b' &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n' in lines[:90]): return 'dacapo-text' for line in lines: if line[0] != b'#': word = line.strip() if word in ['ANIMSTEPS', 'CRYSTAL', 'SLAB', 'POLYMER', 'MOLECULE']: return 'xsf' filename_v = os.path.basename(filename) if 'POSCAR' in filename_v or 'CONTCAR' in filename_v: return 'vasp' if 'OUTCAR' in filename_v: return 'vasp_out' if 'XDATCAR' in filename_v: return 'vasp_xdatcar' if filename.lower().endswith('.exi'): return 'exi' if filename.lower().endswith('.mol'): return 'mol' if filename.lower().endswith('.pdb'): return 'pdb' if filename.lower().endswith('.cif'): return 'cif' if filename.lower().endswith('.struct'): return 'struct' if filename.lower().endswith('.struct_out'): return 'struct_out' fileobj.seek(0) while True: line = fileobj.readline() if not line: break if b'Invoking FHI-aims ...' in line: return 'aims_out' if b'atom' in line: data = line.split() try: Atoms(symbols=[data[4]], positions=[[float(data[1]), float(data[2]), float(data[3])]]) return 'aims' except: pass if filename.lower().endswith('.in'): fileobj.seek(0) while True: line = fileobj.readline() if not line: break if ('&system' in line) or ('&SYSTEM' in line): return 'esp_in' return 'aims' if filename.lower().endswith('.cfg'): return 'cfg' if os.path.split(filename)[1] == 'atoms.dat': return 'iwm' if filename.endswith('I_info'): return 'Cmdft' if lines[0].startswith(b'$coord') or os.path.basename(filename) == 'coord': return 'tmol' if (lines[0].startswith(b'$grad') or os.path.basename(filename) == 'gradient'): return 'tmol-gradient' if lines[0].startswith(b'Geometry'): return 'dftb' if filename.lower().endswith('.geom'): return 'castep_geom' if filename.lower().endswith('.castep'): return 'castep' if filename.lower().endswith('.cell'): return 'castep_cell' if s3 == '<?x' and not filename.endswith('xsd'): from ase.io.vtkxml import probe_vtkxml xmltype = probe_vtkxml(filename) if xmltype == 'ImageData': return 'vti' elif xmltype == 'StructuredGrid': return 'vts' elif xmltype == 'UnstructuredGrid': return 'vtu' elif xmltype is not None: raise IOError('Unknown VTK XML file!') if filename.lower().endswith('.sdf'): return 'sdf' if filename.lower().endswith('.gen'): return 'gen' if filename.lower().endswith('.con'): return 'eon' if 'ITEM: TIMESTEP\n' in lines: return 'lammps' if filename.lower().endswith('.gro'): return 'gromacs' if filename.lower().endswith('.log'): return 'gaussian_out' if filename.lower().endswith('.com'): return 'gaussian' if filename.lower().endswith('.g96'): return 'gromos' if filename.lower().endswith('.out'): return 'esp_out' if filename.endswith('.nw'): return 'nw' if filename.endswith('xsd'): return 'xsd' return 'xyz'
def filetype(filename): """Try to guess the type of the file.""" fileobj = open(filename) s3 = fileobj.read(3) if len(s3) == 0: raise IOError('Empty file: ' + filename) if is_tarfile(filename): return 'gpw' if s3 == 'CDF': from ase.io.pupynere import NetCDFFile nc = NetCDFFile(filename) if 'number_of_dynamic_atoms' in nc.dimensions: return 'dacapo' history = nc.history if history == 'GPAW restart file': return 'gpw-nc' if history == 'ASE trajectory': return 'nc' if history == 'Dacapo': return 'dacapo' raise IOError('Unknown netCDF file!') if s3 == '<?x': from ase.io.vtkxml import probe_vtkxml xmltype = probe_vtkxml(filename) if xmltype == 'ImageData': return 'vti' elif xmltype == 'StructuredGrid': return 'vts' elif xmltype == 'UnstructuredGrid': return 'vtu' elif xmltype is not None: raise IOError('Unknown VTK XML file!') if is_zipfile(filename): return 'vnl' fileobj.seek(0) lines = fileobj.readlines(1000) if lines[0].startswith('PickleTrajectory'): return 'traj' if lines[1].startswith('OUTER LOOP:') or filename.lower().endswith('.cube'): return 'cube' if ' ___ ___ ___ _ _ _ \n' in lines: return 'gpaw-text' if (' &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n' in lines[:90]): return 'dacapo-text' for word in ['ANIMSTEPS', 'CRYSTAL', 'SLAB', 'POLYMER', 'MOLECULE']: if lines[0].startswith(word): return 'xsf' filename_v = basename(filename) if 'POSCAR' in filename_v or 'CONTCAR' in filename_v: return 'vasp' if filename.lower().endswith('.mol'): return 'mol' if filename.lower().endswith('.pdb'): return 'pdb' if filename.lower().endswith('.cif'): return 'cif' if os.path.split(filename)[1] == 'atoms.dat': return 'iwm' if filename.endswith('I_info'): return 'Cmdft' return 'xyz'