# 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/abinit_io.py
# def get_efermi(self):
#     return float(self._variables['e_fermie'].data)*Ha
# 
# class GSR(object):
#     def __init__(self,fname):
#         self._fname=fname
#         self._file = netcdf_file(self._fname)
#         self._variables=self._file.variables
# 
#     def get_efermi(self):
#         return float(self._variables['e_fermie'].data)*Ha
# 
#     def get_kpoints(self):
#         return self._variables['reduced_coordinates_of_kpoints'].data
# 
# def get_kpoints(self):
#     return self._variables['reduced_coordinates_of_kpoints'].data


import os
import shutil
import copy
import numpy as np
import subprocess
            import json
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
from pyDFTutils.abinit.abinit_io import get_efermi, GSR, get_kpoints
                if line.find('Magnetisation') != -1:  # last one
                    magmom = float(line.split('=')[-1].strip())
        return magmom

    def get_fermi_level(self):
        return self.read_fermi()

    def get_eigenvalues(self, kpt=0, spin=0):
        return self.get_kpts_info(kpt, spin, 'eigenvalues')

    def get_occupations(self, kpt=0, spin=0):
        return self.get_kpts_info(kpt, spin, 'occupations')

    def read_fermi(self):
        """Method that reads Fermi energy in Hartree from the output file
        and returns it in eV"""
        E_f = None
        filename = join(self.workdir, self.label + '.txt')
        text = open(filename).read().lower()
        assert 'error' not in text
        for line in iter(text.split('\n')):
            if line.rfind('fermi (or homo) energy (hartree) =') > -1:
                E_f = float(line.split('=')[1].strip().split()[0])
        return E_f * Hartree


    def get_efermi(self):
        gsr_fname=self.prefix+'o_GSR.nc'
        return get_efermi(gsr_fname)


# 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/abinit_io.py
# def get_efermi(self):
#     return float(self._variables['e_fermie'].data)*Ha
# 
# class GSR(object):
#     def __init__(self,fname):
#         self._fname=fname
#         self._file = netcdf_file(self._fname)
#         self._variables=self._file.variables
# 
#     def get_efermi(self):
#         return float(self._variables['e_fermie'].data)*Ha
# 
#     def get_kpoints(self):
#         return self._variables['reduced_coordinates_of_kpoints'].data
# 
# def get_kpoints(self):
#     return self._variables['reduced_coordinates_of_kpoints'].data


# Path: pyDFTutils/abinit/myabinit_vca.py
import os
import shutil
import copy
import numpy as np
import subprocess
            import json
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
from pyDFTutils.abinit.abinit_io import get_efermi, GSR, get_kpoints

                if line.find('Magnetisation') != -1:  # last one
                    magmom = float(line.split('=')[-1].strip())
        return magmom

    def get_fermi_level(self):
        return self.read_fermi()

    def get_eigenvalues(self, kpt=0, spin=0):
        return self.get_kpts_info(kpt, spin, 'eigenvalues')

    def get_occupations(self, kpt=0, spin=0):
        return self.get_kpts_info(kpt, spin, 'occupations')

    def read_fermi(self):
        """Method that reads Fermi energy in Hartree from the output file
        and returns it in eV"""
        E_f = None
        filename = join(self.workdir, self.label + '.txt')
        text = open(filename).read().lower()
        assert 'error' not in text
        for line in iter(text.split('\n')):
            if line.rfind('fermi (or homo) energy (hartree) =') > -1:
                E_f = float(line.split('=')[1].strip().split()[0])
        return E_f * Hartree


    def get_efermi(self):
        gsr_fname=self.prefix+'o_GSR.nc'
        return get_efermi(gsr_fname)


    def get_kpoints(self):