import xml.etree.ElementTree as ET
import io ,sys
import os
import subprocess
import lxml
import numpy as np


def functionTest(path,IndextobeFound,Search_Parameter_array,Value_array):
	#################################################################################
	if (len(Search_Parameter_array))==2:
         tree = ET.parse(path + r'\parameters.xml')
         root = tree.getroot()
         Found = 0
         List_of_Index = []
         for elem in root.iter('LightPathElement'):
             List_of_Index =elem.attrib['Index']
             if(List_of_Index==IndextobeFound):
                    for List_of_Index in elem.iter('Parameter'):
                         Found = 0
                         for subelem in List_of_Index:
                            if (subelem.text == Search_Parameter_array[0]): 
                                Found = 1
                            if (Found == 1):		
                                if (subelem.tag == 'Value'):
                                    subelem.text = str(Value_array[0])


         tree.write(path + r'\parameters.xml',encoding="UTF-8",xml_declaration=True)	
        
         tree = ET.parse(path + r'\parameters.xml')
         root = tree.getroot()
         Found = 0
         List_of_Index = []
         for elem in root.iter('LightPathElement'):
             List_of_Index =elem.attrib['Index']
             if(List_of_Index==IndextobeFound):
                    for List_of_Index in elem.iter('Parameter'):
                         Found = 0
                         for subelem in List_of_Index:
                            if (subelem.text == Search_Parameter_array[1]): 
                                Found = 1
                            if (Found == 1):		
                                if (subelem.tag == 'Value'):
                                    subelem.text = str(Value_array[1])
         tree.write(path + r'\parameters.xml',encoding="UTF-8",xml_declaration=True)	                                    
        
	else:  
        
         tree = ET.parse(path + r'\parameters.xml')
         root = tree.getroot()
         Found = 0
         List_of_Index = []
         for elem in root.iter('LightPathElement'):
             List_of_Index =elem.attrib['Index']
             if(List_of_Index==IndextobeFound):
                    for List_of_Index in elem.iter('Parameter'):
                         Found = 0
                         for subelem in List_of_Index:
                            if (subelem.text == Search_Parameter_array[0]): 
                                Found = 1
                            if (Found == 1):		
                                if (subelem.tag == 'Value'):
                                    subelem.text = str(Value_array[0])


         tree.write(path + r'\parameters.xml',encoding="UTF-8",xml_declaration=True)	
    
	#################################################################################
	#Running Batch File in Output_Folder1
	subprocess.call([path + r'\sample_batch.bat'])
	# read result from XML
	tree = ET.parse(path + r'\results.xml')
	root = tree.getroot()
    
	for List_of_Index1 in root.iter('Value'):
		    print (List_of_Index1.text) 			  
		    return float(List_of_Index1.text)
