#!/usr/bin/env python
# This code will check the folder and some subfolder assigned by the user to see if the mp-*** listed in a text file are available or not
# If available, the selected property at selected concentration and temperature will be reported
# If not, N.A would be typed in front of the list
# By: Alireza Faghaninia


#!/usr/bin/env python

import argparse
import os
scripts_path = '~/dekode/'
os.system('cp ' + scripts_path + 'run_aMoBT.py .')
from run_aMoBT import find_reference, get_me_dielectrics
os.system("cp " + scripts_path + "calc_def_potential.py .")
from calc_def_potential import calc_def_potential
os.system('cp ' + scripts_path + 'find_DOS_peaks.py .')
from find_DOS_peaks import find_DOS_peaks

def find_properties(filename, n, T): #find the transport properties at a given n and T from aMoBT output
	count = 0
	mobility = 0
	conductivity = 0
	thermopower = 0
	at_correct_n = False
	if os.path.exists(filename):
		with open(filename) as aMoBT_output:
			for row in aMoBT_output:
				line = row.split()
				if at_correct_n:
					if float(line[0]) == T:
						mobility = float(line[1])
						conductivity = float(line[2])
						thermopower = float(line[3])
						at_correct_n = False
				if len(line) > 3:
					if 'Carrier' in line[0] and float(line[3]) == n:
						at_correct_n = True
	return mobility, conductivity, thermopower

def find_effective_mass(filename):
	m_e = 0.0000
	m_h = 0.0000
	if os.path.exists(filename):
		with open(filename) as aMoBT_output:
			for row in aMoBT_output:
				line = row.split()
				if len(line) >= 2:
					if line[0] == '***Proceed':
						if 'm*_e/m' in line[7]:
							m_e = float(line[9])
						if 'm*_h/m' in line[7]: 
							m_h = float(line[9])
	return m_e, m_h

def total_time(job_list):	# job_list is a list of string of jobs in the subfolders, i.e. geom, self, phonon, etc
        t_total = 0.0
        for job in job_list:
                if os.path.exists(job):
                        os.chdir(job)
                        if os.path.exists('OUTCAR'):
                                with open('OUTCAR', 'r') as outcar:
                                        for line in outcar:
                                                if 'Elapsed time (sec):' in line:
                                                        l = line.split()
                                                        t_total += float(l[3])
                        if 'deform' in job:
                                for j in ['-10', '-9', '-8', '-7', '-6', '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']: