Skip to content

Commit

Permalink
Modernise get_spacegroup: type annotation, better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajjackson committed May 28, 2024
1 parent 235f860 commit ffc2ea8
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions mctools/generic/get_spacegroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

import argparse
import os
from pathlib import Path
from typing import Optional

import ase.io
import spglib


def get_spacegroup(filename=False, format=False):

if filename:
pass
elif os.path.isfile('geometry.in'):
filename = 'geometry.in'
elif os.path.isfile('POSCAR'):
filename = 'POSCAR'
def get_default_file() -> Path:
for candidate in ("geometry.in", "POSCAR", "castep.cell"):
if (input := Path.cwd() / candidate).is_file():
return input
else:
raise Exception('No input file!')
raise ValueError("Input file not specified, no default found.")

if format:
atoms = ase.io.read(filename, format=format)
else:
atoms = ase.io.read(filename)

def get_spacegroup(filename: Optional[Path] = None,
format: Optional[str] = None):

if filename is None:
filename = get_default_file()

atoms = ase.io.read(str(filename), format=format)
cell = (atoms.cell.array, atoms.get_scaled_positions(), atoms.numbers)

print("| Threshold / Å | Space group |")
Expand All @@ -35,9 +36,9 @@ def get_spacegroup(filename=False, format=False):

def main():
parser = argparse.ArgumentParser()
parser.add_argument('filename', action='store', default=False,
parser.add_argument('filename', type=Path, default=None, nargs="?",
help="Input structure file")
parser.add_argument('-f', '--format', action='store', default=False,
parser.add_argument('-f', '--format', type=str, default=None,
help="File format for ASE importer")
args = parser.parse_args()
get_spacegroup(filename=args.filename, format=args.format)

0 comments on commit ffc2ea8

Please sign in to comment.