# Path: pyDFTutils/ase_utils/symbol.py
# def symbol_number(symbols):
#     """
#     symbols can be also atoms. Thus the chemical symbols will be used.
#     Fe Fe Fe O -> {Fe1:0 Fe2:1 Fe3:2 O1:3}
#     """
#     try:
#         symbs=symbols.copy().get_chemical_symbols()
#     except Exception:
#         symbs=symbols
#     symdict={}
#     result=OrderedDict()
#     for i,sym in enumerate(symbs):
#         if sym in symdict:
#             symdict[sym]=symdict[sym]+1
#         else:
#             symdict[sym]=1
#         result[sym+str(symdict[sym])]=i
#     return result
# 
# def symnum_to_sym(symbol_number):
#     """
#     symnum-> sym. eg: Fe1-> Fe
#     """
#     try:
#         a=re.search('[A-Za-z]+',symbol_number).group()
#         return a
#     except AttributeError:
#         raise AttributeError('%s is not a good symbol_number'%symbol_number)


import numpy as np
import os.path
import re
from ase.io import read
from ase.io.vasp import read_vasp
from pyDFTutils.ase_utils.symbol import symbol_number, symnum_to_sym
from ase.atoms import Atoms
def read_unsorted_poscar(filename='CONTCAR'):
    s, p, c = read_poscar_and_unsort(filename=filename)
    atoms = Atoms(symbols=s, positions=p, cell=c)
    return atoms


def get_electrons(filename='POTCAR'):
    """
    get dict {symbol: valence} from POTCAR
    """
    nelect = dict()
    lines = open(filename).readlines()
    for n, line in enumerate(lines):
        if line.find('TITEL') != -1:
            symbol = line.split('=')[1].split()[1].split('_')[0].strip()
            valence = float(
                lines[n + 4].split(';')[1].split('=')[1].split()[0].strip())
            nelect[symbol] = valence
    return nelect


def get_symdict(filename='POSCAR', atoms=None):
    """
    get a symbol_number: index dict.
    """
    if filename is None and atoms is not None:
        syms = atoms.get_chemical_symbols()
    elif filename is not None and atoms is None:
        syms = read(filename).get_chemical_symbols()


# Path: pyDFTutils/ase_utils/symbol.py
# def symbol_number(symbols):
#     """
#     symbols can be also atoms. Thus the chemical symbols will be used.
#     Fe Fe Fe O -> {Fe1:0 Fe2:1 Fe3:2 O1:3}
#     """
#     try:
#         symbs=symbols.copy().get_chemical_symbols()
#     except Exception:
#         symbs=symbols
#     symdict={}
#     result=OrderedDict()
#     for i,sym in enumerate(symbs):
#         if sym in symdict:
#             symdict[sym]=symdict[sym]+1
#         else:
#             symdict[sym]=1
#         result[sym+str(symdict[sym])]=i
#     return result
# 
# def symnum_to_sym(symbol_number):
#     """
#     symnum-> sym. eg: Fe1-> Fe
#     """
#     try:
#         a=re.search('[A-Za-z]+',symbol_number).group()
#         return a
#     except AttributeError:
#         raise AttributeError('%s is not a good symbol_number'%symbol_number)


# Path: pyDFTutils/vasp/vasp_utils.py
import numpy as np
import os.path
import re
from ase.io import read
from ase.io.vasp import read_vasp
from pyDFTutils.ase_utils.symbol import symbol_number, symnum_to_sym
from ase.atoms import Atoms

def read_unsorted_poscar(filename='CONTCAR'):
    s, p, c = read_poscar_and_unsort(filename=filename)
    atoms = Atoms(symbols=s, positions=p, cell=c)
    return atoms


def get_electrons(filename='POTCAR'):
    """
    get dict {symbol: valence} from POTCAR
    """
    nelect = dict()
    lines = open(filename).readlines()
    for n, line in enumerate(lines):
        if line.find('TITEL') != -1:
            symbol = line.split('=')[1].split()[1].split('_')[0].strip()
            valence = float(
                lines[n + 4].split(';')[1].split('=')[1].split()[0].strip())
            nelect[symbol] = valence
    return nelect


def get_symdict(filename='POSCAR', atoms=None):
    """
    get a symbol_number: index dict.
    """
    if filename is None and atoms is not None:
        syms = atoms.get_chemical_symbols()
    elif filename is not None and atoms is None:
        syms = read(filename).get_chemical_symbols()


    symdict = symbol_number(syms)