# Path: pyDFTutils/ase_utils/symbol.py
# def get_symdict(filename='POSCAR',atoms=None):
#     """
#     get a symbol_number: index dict.
#     """
#     if 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)
#     return symdict
# 
# 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)


from ase.utils.geometry import rotate
from ase.io import read
from ase.geometry.cell import cellpar_to_cell, cell_to_cellpar
from pyDFTutils.ase_utils.symbol import get_symdict,symbol_number,symnum_to_sym
from pyDFTutils.ase_utils.ase_utils import scaled_pos_to_pos,force_near_0,pos_to_scaled_pos
from collections import OrderedDict,Iterable
from ase.calculators.neighborlist import NeighborList
    from scipy.sparse import csr_matrix
    from scipy.sparse.csgraph import dijkstra
import numpy as np
    import argparse
        if var_distance and len(self.vertexes)<6:
            #print("max_distance too small,tring to increase by 0.06")
            self.get_octahedra(self.atoms,center_atom,atom_symbol,max_distance+0.06)
        elif var_distance and len(self.vertexes)>6:
            #print("max_distance too small,tring to decrease by 0.05")
            self.get_octahedra(self.atoms,center_atom,atom_symbol,max_distance-0.05)
        else:
            pass
            #print("%d vertexes found,max distance is %s"%(len(self.vertexes),max_distance))

        self.number_of_vertexes=len(self.vertexes)
        self.max_distance=max_distance

        self.pair()
        self.get_bond_angle()
        self.get_bond_lengths()
        self.get_rotations()
        self.get_avg_rotations()

        return self.vertexes

    def read_octahedra(self,filename,center_sym_number,vertex_symbol,max_distance,var_distance=False):
        """
        read octahedra infomation from file filename.
        filename: filename
        center_sym_number: eg. 'Fe1'
        vertex_symbol: eg. 'O'
        max_distance : maximum distance between center and vertex
        var_distance: if max_distance too small/ large, find a better one. Use if you are sure there is really a octahedra.
        """

# Path: pyDFTutils/ase_utils/symbol.py
# def get_symdict(filename='POSCAR',atoms=None):
#     """
#     get a symbol_number: index dict.
#     """
#     if 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)
#     return symdict
# 
# 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/perovskite/octahedra.py
from ase.utils.geometry import rotate
from ase.io import read
from ase.geometry.cell import cellpar_to_cell, cell_to_cellpar
from pyDFTutils.ase_utils.symbol import get_symdict,symbol_number,symnum_to_sym
from pyDFTutils.ase_utils.ase_utils import scaled_pos_to_pos,force_near_0,pos_to_scaled_pos
from collections import OrderedDict,Iterable
from ase.calculators.neighborlist import NeighborList
    from scipy.sparse import csr_matrix
    from scipy.sparse.csgraph import dijkstra
import numpy as np
    import argparse

        if var_distance and len(self.vertexes)<6:
            #print("max_distance too small,tring to increase by 0.06")
            self.get_octahedra(self.atoms,center_atom,atom_symbol,max_distance+0.06)
        elif var_distance and len(self.vertexes)>6:
            #print("max_distance too small,tring to decrease by 0.05")
            self.get_octahedra(self.atoms,center_atom,atom_symbol,max_distance-0.05)
        else:
            pass
            #print("%d vertexes found,max distance is %s"%(len(self.vertexes),max_distance))

        self.number_of_vertexes=len(self.vertexes)
        self.max_distance=max_distance

        self.pair()
        self.get_bond_angle()
        self.get_bond_lengths()
        self.get_rotations()
        self.get_avg_rotations()

        return self.vertexes

    def read_octahedra(self,filename,center_sym_number,vertex_symbol,max_distance,var_distance=False):
        """
        read octahedra infomation from file filename.
        filename: filename
        center_sym_number: eg. 'Fe1'
        vertex_symbol: eg. 'O'
        max_distance : maximum distance between center and vertex
        var_distance: if max_distance too small/ large, find a better one. Use if you are sure there is really a octahedra.
        """

        sym_dict=get_symdict(filename=filename)