# Path: pyDFTutils/ase_utils/kpoints.py
# def get_ir_kpts(atoms, mesh):
#     """
#     Gamma-centered IR kpoints. mesh : [nk1,nk2,nk3].
#     """
#     lattice = atoms.get_cell()
#     positions = atoms.get_scaled_positions()
#     numbers = atoms.get_atomic_numbers()
# 
#     cell = (lattice, positions, numbers)
#     mapping, grid = spglib.get_ir_reciprocal_mesh(
#         mesh, cell, is_shift=[0, 0, 0])
#     #print("%3d ->%3d %s" % (1, mapping[0], grid[0].astype(float) / mesh))
#     #print("Number of ir-kpoints: %d" % len(np.unique(mapping)))
#     #print(grid[np.unique(mapping)] / np.array(mesh, dtype=float))
#     return grid[np.unique(mapping)] / np.array(mesh, dtype=float)
# 
# def cubic_kpath(npoints=500,name=True):
#     """
#     return the kpoint path for cubic
#     Parameters:
#     ----------------
#     npoints: int
#         number of points.
# 
#     Returns:
#     -----------------
#     kpts:
#         kpoints
#     xs:
#         x cordinates for plot
#     xspecial:
#         x cordinates for special k points
#     """
#     special_path = special_paths['cubic']
#     points = special_points['cubic']
#     paths = parse_path_string(special_path)
#     special_kpts = [points[k] for k in paths[0]]
#     kpts, xs, xspecial = bandpath(
#         special_kpts, cell=np.eye(3), npoints=npoints)
#     if not name:
#         return kpts, xs, xspecial
#     else:
#         return kpts, xs, xspecial,special_path


import os
import shutil
import numpy as np
import subprocess
from glob import glob
from os.path import join, isfile, islink
from ase.atoms import Atoms
from ase.data import atomic_numbers
from ase.units import Bohr, Hartree, fs, Ha, eV
from ase.data import chemical_symbols
from ase.io.abinit import read_abinit
from ase.io.vasp import write_vasp
from ase.calculators.calculator import FileIOCalculator, Parameters, kpts2mp, \
    ReadError,all_changes,Calculator
from pyDFTutils.ase_utils.kpoints import get_ir_kpts, cubic_kpath
from string import Template
    t = template.substitute(kwargs)
    if 'kpath' in kwargs:
        kpath = kwargs['kpath']
        nk = len(kpath)
        t += "!====Phonon band kpoints===\n"
        t += "    nph1l %s\n" % nk
        t += " qph1l %s 1.0\n" % (' '.join([str(x) for x in kpath[0]]))
        for kpt in kpath[1:]:
            t += "       %s 1.0\n" % (' '.join([str(x) for x in kpt]))
    return t


def gen_ifc(prefix='abinit'):
    with open('%s_ifc.files' % prefix, 'w') as myfile:
        myfile.write(gen_ifc_files(prefix=prefix))

    with open('%s_ifc.in' % prefix, 'w') as myfile:
        myfile.write(
            gen_ifc_in(
                ifcflag=1,
                brav=1,
                nk=2,
                qpts=[2, 2, 2],
                nqshft=1,
                ifcana=1,
                ifcout=20,
                natifc=5,
                atifc="1 2 3 4 5",
                chneut=1,
                dipdip=1,

# Path: pyDFTutils/ase_utils/kpoints.py
# def get_ir_kpts(atoms, mesh):
#     """
#     Gamma-centered IR kpoints. mesh : [nk1,nk2,nk3].
#     """
#     lattice = atoms.get_cell()
#     positions = atoms.get_scaled_positions()
#     numbers = atoms.get_atomic_numbers()
# 
#     cell = (lattice, positions, numbers)
#     mapping, grid = spglib.get_ir_reciprocal_mesh(
#         mesh, cell, is_shift=[0, 0, 0])
#     #print("%3d ->%3d %s" % (1, mapping[0], grid[0].astype(float) / mesh))
#     #print("Number of ir-kpoints: %d" % len(np.unique(mapping)))
#     #print(grid[np.unique(mapping)] / np.array(mesh, dtype=float))
#     return grid[np.unique(mapping)] / np.array(mesh, dtype=float)
# 
# def cubic_kpath(npoints=500,name=True):
#     """
#     return the kpoint path for cubic
#     Parameters:
#     ----------------
#     npoints: int
#         number of points.
# 
#     Returns:
#     -----------------
#     kpts:
#         kpoints
#     xs:
#         x cordinates for plot
#     xspecial:
#         x cordinates for special k points
#     """
#     special_path = special_paths['cubic']
#     points = special_points['cubic']
#     paths = parse_path_string(special_path)
#     special_kpts = [points[k] for k in paths[0]]
#     kpts, xs, xspecial = bandpath(
#         special_kpts, cell=np.eye(3), npoints=npoints)
#     if not name:
#         return kpts, xs, xspecial
#     else:
#         return kpts, xs, xspecial,special_path


# Path: pyDFTutils/abinit/myabinit.py
import os
import shutil
import numpy as np
import subprocess
from glob import glob
from os.path import join, isfile, islink
from ase.atoms import Atoms
from ase.data import atomic_numbers
from ase.units import Bohr, Hartree, fs, Ha, eV
from ase.data import chemical_symbols
from ase.io.abinit import read_abinit
from ase.io.vasp import write_vasp
from ase.calculators.calculator import FileIOCalculator, Parameters, kpts2mp, \
    ReadError,all_changes,Calculator
from pyDFTutils.ase_utils.kpoints import get_ir_kpts, cubic_kpath
from string import Template

    t = template.substitute(kwargs)
    if 'kpath' in kwargs:
        kpath = kwargs['kpath']
        nk = len(kpath)
        t += "!====Phonon band kpoints===\n"
        t += "    nph1l %s\n" % nk
        t += " qph1l %s 1.0\n" % (' '.join([str(x) for x in kpath[0]]))
        for kpt in kpath[1:]:
            t += "       %s 1.0\n" % (' '.join([str(x) for x in kpt]))
    return t


def gen_ifc(prefix='abinit'):
    with open('%s_ifc.files' % prefix, 'w') as myfile:
        myfile.write(gen_ifc_files(prefix=prefix))

    with open('%s_ifc.in' % prefix, 'w') as myfile:
        myfile.write(
            gen_ifc_in(
                ifcflag=1,
                brav=1,
                nk=2,
                qpts=[2, 2, 2],
                nqshft=1,
                ifcana=1,
                ifcout=20,
                natifc=5,
                atifc="1 2 3 4 5",
                chneut=1,
                dipdip=1,

                kpath=cubic_kpath()[0]))