from pymatgen.io.lobster.outputs import Doscar
from pymatgen.core.structure import IStructure
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def get_pdos_array(pdos):
    pdos_list = []
    for orbital in pdos:
        for spin in pdos[orbital]:
            pdos_list.append(pdos[orbital][spin])
    return np.array(pdos_list)


DOSCAR_path = '/home/sure/Document/vasptoolkit/examples/DOSCAR.lobster'
POSCAR_path = '/home/sure/Document/vasptoolkit/examples/POSCAR'
doscar = Doscar(DOSCAR_path,POSCAR_path)

struc = IStructure.from_file(POSCAR_path)

sites_dict = {}
for index,site in enumerate(struc.sites):
    symbol = site.species.elements[0].symbol
    sites_dict[index] = symbol

pdos_dict = {}
for index,atom in enumerate(doscar.pdos):
    symbol = sites_dict[index]
    # pd.set_option('display.max_rows', None)
    # print(pd.DataFrame(np.transpose(get_pdos_array(atom))))
    # input()
    if symbol in pdos_dict:
        pdos_dict[symbol] += get_pdos_array(atom)
    else:
        pdos_dict[symbol] = get_pdos_array(atom)

energy_array = doscar.energies


dir_name = '/home/sure/Document/vasptoolkit/examples/'
for symbol in pdos_dict:
    pdos = np.transpose(pdos_dict[symbol])
    df = pd.DataFrame(pdos,index=energy_array)
    file_name = symbol + '_pdos.csv'
    df.to_csv(dir_name+file_name)
from pymatgen.io.lobster.outputs import Doscar
from pymatgen.core.structure import IStructure
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def get_pdos_array(pdos):
    pdos_list = []
    for orbital in pdos:
        for spin in pdos[orbital]:
            pdos_list.append(pdos[orbital][spin])
    return np.array(pdos_list)


DOSCAR_path = '/home/sure/Document/vasptoolkit/examples/DOSCAR.lobster'
POSCAR_path = '/home/sure/Document/vasptoolkit/examples/POSCAR'
doscar = Doscar(DOSCAR_path,POSCAR_path)

struc = IStructure.from_file(POSCAR_path)

sites_dict = {}
for index,site in enumerate(struc.sites):
    symbol = site.species.elements[0].symbol
    sites_dict[index] = symbol

pdos_dict = {}
for index,atom in enumerate(doscar.pdos):
    symbol = sites_dict[index]
    # pd.set_option('display.max_rows', None)
    # print(pd.DataFrame(np.transpose(get_pdos_array(atom))))
    # input()
    if symbol in pdos_dict:
        pdos_dict[symbol] += get_pdos_array(atom)
    else:
        pdos_dict[symbol] = get_pdos_array(atom)

energy_array = doscar.energies


dir_name = '/home/sure/Document/vasptoolkit/examples/'
for symbol in pdos_dict:
    pdos = np.transpose(pdos_dict[symbol])
    df = pd.DataFrame(pdos,index=energy_array)
    file_name = symbol + '_pdos.csv'
    df.to_csv(dir_name+file_name)
# pd.set_option('display.max_rows', None) # print(pd.DataFrame(np.transpose(get_pdos_array(atom)))) # input()