"""
pymatgen を用いて結晶構造の高対称k点経路を計算し表示するスクリプト。

概要:
    入力された結晶構造のBravais格子に基づいて高対称k点経路を計算し、表示します。

詳細説明:
    このスクリプトは、指定されたCIFファイルから結晶構造を読み込み、
    その原始構造 (`primitive structure`) に基づいて高対称k点経路を生成します。
    経路タイプは `hinuma` (Hinuma et al. 2017) または `sc` (Setyawan-Curtarolo 2010)
    から選択でき、デフォルトは `hinuma` です。
    計算されたk点経路のセグメントと、各高対称点の分数座標を標準出力に表示します。
    コマンドライン引数を使用することで、入力ファイルと経路タイプを動的に指定できます。
    例: `python kpath_pymatgen.py my_structure.cif sc`

引数 (Parameters):
    :param sys.argv[1]: (str, optional) 入力CIFファイルへのパス。デフォルトは 'ZnO.cif' です。
    :param sys.argv[2]: (str, optional) 使用する高対称k点経路のタイプ。'hinuma' (デフォルト) または 'sc'。

戻り値 (Returns):
    :returns: None. 計算結果は標準出力に表示されます。エラーが発生した場合はメッセージを表示し終了します。

関連リンク:
    :doc:`kpath_pymatgen_usage`
"""

import sys
from pymatgen.core import Structure
from pymatgen.symmetry.bandstructure import HighSymmKpath

infile = 'ZnO.cif'
path_type = "hinuma"  # hinuma: Hinuma–Pizzi–Kumagai–Oba–Tanaka (2017)
                      # sc: Setyawan–Curtarolo (2010)

if __name__ == "__main__":
    if len(sys.argv) > 1:
        infile = sys.argv[1]
    if len(sys.argv) > 2:
        path_type = sys.argv[2]


def main():
    structure = Structure.from_file(infile)
    prim = structure.get_primitive_structure()

# 高対称点と経路を生成
    kpath = HighSymmKpath(prim, path_type = path_type)
    if kpath is None or kpath.kpath is None:
        print(f"\nError: Could not get k path for [{infile}] with the path_type={path_type}\n")
        exit()

# 経路の定義を見やすく表示
    print("\n=== k-path 経路 ===")
    for segment in kpath.kpath["path"]:
        print(" → ".join(segment))

# 高対称点座標を見やすく表示
    print("\n=== 高対称点座標 ===")
    for point, coord in kpath.kpath["kpoints"].items():
        print(f"{point:3s} : {coord}")


if __name__ == '__MAIN__':
    main()
        