""" def run all the different actions that can be performed """
import stringactions as sa
from os import system
import optimisefuncs as opt
import math
from random import randint
import threading

TARGET_S11 = [-1,-1,-1,-2.5,-6,-15,-6,-2.5,-1,-1,-1]

def run():
    t=[]
    x=[1,41.6020,46.0289,32.1322,18.2673,-22.2088]
    y=[1,27.8382,49.3698,68.2568,76.9064,62.9388]
    write_input_file(x,y,'newtest')
    run_nec_command('newtest')
    rs = read_output_file('newtest')
    ts11, s11 = calc_s11(rs)
    obj, pe = calc_obj_value(ts11,s11)
    
    print('original ' + str(obj) + '\n', end="")
    perf_test(x,y)
    print("actions completed")
    
def write_input_file(y,z,fname):
    """ write to nec input file """
    file = open('c:/4nec2/out/' + fname + '.nec', 'w')
    file.write('CM Seeddesign \n')
    file.write('CM Zigzag Antenna \n')
    file.write('CE File generated by python \n')
    seg = 1

    #write the antenna
    for i in range(0,len(y)-1):
        file.write('GW %3i %3i %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f\n' % (1,seg,0,y[i],z[i],0,y[i+1],z[i+1],1))

    file.write('GE 0 \n')
    file.write('EK \n')
    file.write('EX %3i %3i %3i %3i %3i %3i %3i\n' % (0,1,1,1,1,0,0))
    file.write('GN -1 \n')
    
    file.write('FR %3i %3i %3i %3i %8.4f %8.4f\n' % (0,1,0,0,900,0))
    file.write('FR %3i %3i %3i %3i %8.4f %8.4f\n' % (0,11,0,0,850,10))

    file.write('LD %3i %3i %3i %3i %8.4f %8.4f\n' % (5,1,0,0,58000000,2))
    file.write('RP %3i %3i %3i %3i %8.4f %8.4f %8.4f %8.4f\n' % (0,1,1,1000,90,0,0,0))

    file.write('EN \n')
    file.close()

def run_nec_command(fname):
    """ run the 4nec2d software to get the output files """
    file = open(fname + '.tmp', 'w')
    file.write('c:/4nec2/out/' + fname + '.nec\n')
    file.write('c:/4nec2/out/' + fname + '.out\n')
    file.close()

    system('c:/4nec2/exe/Nec2dXS1k5.exe 0<'+fname+'.tmp')
    

def read_output_file(fname):
    """ read from the output file that was generated in the nec software """
    file = open('c:/4nec2/out/' + fname + '.out','r')
    rs=[] #required string
    f=False #have the required strings been found
    rp = 0 #string point
    
    for num, line in enumerate(file, 1):
        if 'FREQ.' in line:
            rp = num

        if num == rp + 4 and rp > 0:
            f = True

        if num > rp+4 and f and line=="\n":
            f= False
            
        if f:
            rs.append(line)
              
    file.close()

    return rs

def calc_s11(rs):
    """ calculate the s11 for the antenna """
    freq = []
    resist = []
    react = []

    for f in rs:
        fs = f.split(" ")
        fs = list(filter(None, fs))
        freq.append(float(fs[0]))
        resist.append(float(fs[5]))
        react.append(float(fs[6]))

    #print('freq',freq,'resist',resist,'react',react)

    refc = []
    s11 = []
    ts11 = 0
    for i in range(0,len(freq)):
        refc.append((resist[i] + 1j*react[i]- 50)/((resist[i]) + 1j*react[i] + 50));

        if refc[i]==0:
            s11.append(0)
        else:
            s11.append(20*math.log(abs(refc[i]),10))

        ts11 += s11[i]

    #print(s11)
    return (ts11, s11)

def calc_obj_value(ts11,s11):
    """ calculate the objective value """
    pe = [] # point errors
    te = 0 # total error

    for i in range(0, len(TARGET_S11)):
        pe.append((TARGET_S11[i]-s11[i])**2/(TARGET_S11[i])**2)
        te+=pe[i]

    ae = te/len(pe) # average error
    obj = 1/abs(ae) # objective value

    return (obj, pe)

def build_array(x,y):
    func_array =[]
    func_array.append(opt.shift_x_all_left)
    func_array.append(opt.shift_x_all_right)
    func_array.append(opt.shift_y_all_up)
    func_array.append(opt.shift_y_all_down)
    func_array.append(opt.shift_x_first_right)
    func_array.append(opt.shift_x_first_second_right)
    func_array.append(opt.shift_x_first_second_third_right)
    func_array.append(opt.shift_x_first_second_third_fourth_right)
    func_array.append(opt.shift_x_first_second_third_fourth_fifth_right)
    func_array.append(opt.shift_x_first_second_third_fourth_sixth_right)
    func_array.append(opt.shift_x_first_second_third_fifth_right)
    func_array.append(opt.shift_x_first_second_third_fifth_sixth_right)
    func_array.append(opt.shift_x_first_second_third_sixth_right)
    func_array.append(opt.shift_x_first_second_fourth_right)
    func_array.append(opt.shift_x_first_second_fourth_fifth_right)
    func_array.append(opt.shift_x_first_second_fourth_fifth_sixth_right)
    func_array.append(opt.shift_x_first_second_fourth_sixth_right)
    func_array.append(opt.shift_x_first_second_fifth_right)
    func_array.append(opt.shift_x_first_second_fifth_sixth_right)
    func_array.append(opt.shift_x_first_second_sixth_right)
    func_array.append(opt.shift_x_first_third_right)
    func_array.append(opt.shift_x_first_third_fourth_right)
    func_array.append(opt.shift_x_first_third_fourth_fifth_right)
    func_array.append(opt.shift_x_first_third_fourth_fifth_sixth_right)
    func_array.append(opt.shift_x_first_third_fourth_sixth_right)
    func_array.append(opt.shift_x_first_third_fifth_right)
    func_array.append(opt.shift_x_first_third_fifth_sixth_right)
    func_array.append(opt.shift_x_first_third_sixth_right)
    func_array.append(opt.shift_x_first_fourth_right)
    func_array.append(opt.shift_x_first_fourth_fifth_right)
    func_array.append(opt.shift_x_first_fourth_fifth_sixth_right)
    func_array.append(opt.shift_x_first_fourth_sixth_right)
    func_array.append(opt.shift_x_first_fifth_right)
    func_array.append(opt.shift_x_first_fifth_sixth_right)
    func_array.append(opt.shift_x_first_sixth_right)
    func_array.append(opt.shift_x_second_right)
    func_array.append(opt.shift_x_second_third_right)
    func_array.append(opt.shift_x_second_third_fourth_right)
    func_array.append(opt.shift_x_second_third_fourth_fifth_right)
    func_array.append(opt.shift_x_second_third_fourth_fifth_sixth_right)
    func_array.append(opt.shift_x_second_third_fourth_sixth_right)
    func_array.append(opt.shift_x_second_third_fifth_right)
    func_array.append(opt.shift_x_second_third_fifth_sixth_right)
    func_array.append(opt.shift_x_second_third_sixth_right)
    func_array.append(opt.shift_x_second_fourth_right)
    func_array.append(opt.shift_x_second_fourth_fifth_right)
    func_array.append(opt.shift_x_second_fourth_fifth_sixth_right)
    func_array.append(opt.shift_x_second_fourth_sixth_right)
    func_array.append(opt.shift_x_second_fifth_right)
    func_array.append(opt.shift_x_second_fifth_sixth_right)
    func_array.append(opt.shift_x_second_sixth_right)
    func_array.append(opt.shift_x_third_right)
    func_array.append(opt.shift_x_third_fourth_right)
    func_array.append(opt.shift_x_third_fourth_fifth_right)
    func_array.append(opt.shift_x_third_fourth_fifth_sixth_right)
    func_array.append(opt.shift_x_third_fourth_sixth_right)
    func_array.append(opt.shift_x_third_fifth_right)
    func_array.append(opt.shift_x_third_fifth_sixth_right)
    func_array.append(opt.shift_x_third_sixth_right)
    func_array.append(opt.shift_x_fourth_right)
    func_array.append(opt.shift_x_fourth_fifth_right)
    func_array.append(opt.shift_x_fourth_fifth_sixth_right)
    func_array.append(opt.shift_x_fourth_sixth_right)
    func_array.append(opt.shift_x_fifth_right)
    func_array.append(opt.shift_x_fifth_sixth_right)
    func_array.append(opt.shift_x_sixth_right)
    #################################################################################
    func_array.append(opt.shift_x_first_left)
    func_array.append(opt.shift_x_first_second_left)
    func_array.append(opt.shift_x_first_second_third_left)
    func_array.append(opt.shift_x_first_second_third_fourth_left)
    func_array.append(opt.shift_x_first_second_third_fourth_fifth_left)
    func_array.append(opt.shift_x_first_second_third_fourth_sixth_left)
    func_array.append(opt.shift_x_first_second_third_fifth_left)
    func_array.append(opt.shift_x_first_second_third_fifth_sixth_left)
    func_array.append(opt.shift_x_first_second_third_sixth_left)
    func_array.append(opt.shift_x_first_second_fourth_left)
    func_array.append(opt.shift_x_first_second_fourth_fifth_left)
    func_array.append(opt.shift_x_first_second_fourth_fifth_sixth_left)
    func_array.append(opt.shift_x_first_second_fourth_sixth_left)
    func_array.append(opt.shift_x_first_second_fifth_left)
    func_array.append(opt.shift_x_first_second_fifth_sixth_left)
    func_array.append(opt.shift_x_first_second_sixth_left)
    func_array.append(opt.shift_x_first_third_left)
    func_array.append(opt.shift_x_first_third_fourth_left)
    func_array.append(opt.shift_x_first_third_fourth_fifth_left)
    func_array.append(opt.shift_x_first_third_fourth_fifth_sixth_left)
    func_array.append(opt.shift_x_first_third_fourth_sixth_left)
    func_array.append(opt.shift_x_first_third_fifth_left)
    func_array.append(opt.shift_x_first_third_fifth_sixth_left)
    func_array.append(opt.shift_x_first_third_sixth_left)
    func_array.append(opt.shift_x_first_fourth_left)
    func_array.append(opt.shift_x_first_fourth_fifth_left)
    func_array.append(opt.shift_x_first_fourth_fifth_sixth_left)
    func_array.append(opt.shift_x_first_fourth_sixth_left)
    func_array.append(opt.shift_x_first_fifth_left)
    func_array.append(opt.shift_x_first_fifth_sixth_left)
    func_array.append(opt.shift_x_first_sixth_left)
    func_array.append(opt.shift_x_second_left)
    func_array.append(opt.shift_x_second_third_left)
    func_array.append(opt.shift_x_second_third_fourth_left)
    func_array.append(opt.shift_x_second_third_fourth_fifth_left)
    func_array.append(opt.shift_x_second_third_fourth_fifth_sixth_left)
    func_array.append(opt.shift_x_second_third_fourth_sixth_left)
    func_array.append(opt.shift_x_second_third_fifth_left)
    func_array.append(opt.shift_x_second_third_fifth_sixth_left)
    func_array.append(opt.shift_x_second_third_sixth_left)
    func_array.append(opt.shift_x_second_fourth_left)
    func_array.append(opt.shift_x_second_fourth_fifth_left)
    func_array.append(opt.shift_x_second_fourth_fifth_sixth_left)
    func_array.append(opt.shift_x_second_fourth_sixth_left)
    func_array.append(opt.shift_x_second_fifth_left)
    func_array.append(opt.shift_x_second_fifth_sixth_left)
    func_array.append(opt.shift_x_second_sixth_left)
    func_array.append(opt.shift_x_third_left)
    func_array.append(opt.shift_x_third_fourth_left)
    func_array.append(opt.shift_x_third_fourth_fifth_left)
    func_array.append(opt.shift_x_third_fourth_fifth_sixth_left)
    func_array.append(opt.shift_x_third_fourth_sixth_left)
    func_array.append(opt.shift_x_third_fifth_left)
    func_array.append(opt.shift_x_third_fifth_sixth_left)
    func_array.append(opt.shift_x_third_sixth_left)
    func_array.append(opt.shift_x_fourth_left)
    func_array.append(opt.shift_x_fourth_fifth_left)
    func_array.append(opt.shift_x_fourth_fifth_sixth_left)
    func_array.append(opt.shift_x_fourth_sixth_left)
    func_array.append(opt.shift_x_fifth_left)
    func_array.append(opt.shift_x_fifth_sixth_left)
    func_array.append(opt.shift_x_sixth_left)
    #################################################################################
    func_array.append(opt.shift_y_first_up)
    func_array.append(opt.shift_y_first_second_up)
    func_array.append(opt.shift_y_first_second_third_up)
    func_array.append(opt.shift_y_first_second_third_fourth_up)
    func_array.append(opt.shift_y_first_second_third_fourth_fifth_up)
    func_array.append(opt.shift_y_first_second_third_fourth_sixth_up)
    func_array.append(opt.shift_y_first_second_third_fifth_up)
    func_array.append(opt.shift_y_first_second_third_fifth_sixth_up)
    func_array.append(opt.shift_y_first_second_third_sixth_up)
    func_array.append(opt.shift_y_first_second_fourth_up)
    func_array.append(opt.shift_y_first_second_fourth_fifth_up)
    func_array.append(opt.shift_y_first_second_fourth_fifth_sixth_up)
    func_array.append(opt.shift_y_first_second_fourth_sixth_up)
    func_array.append(opt.shift_y_first_second_fifth_up)
    func_array.append(opt.shift_y_first_second_fifth_sixth_up)
    func_array.append(opt.shift_y_first_second_sixth_up)
    func_array.append(opt.shift_y_first_third_up)
    func_array.append(opt.shift_y_first_third_fourth_up)
    func_array.append(opt.shift_y_first_third_fourth_fifth_up)
    func_array.append(opt.shift_y_first_third_fourth_fifth_sixth_up)
    func_array.append(opt.shift_y_first_third_fourth_sixth_up)
    func_array.append(opt.shift_y_first_third_fifth_up)
    func_array.append(opt.shift_y_first_third_fifth_sixth_up)
    func_array.append(opt.shift_y_first_third_sixth_up)
    func_array.append(opt.shift_y_first_fourth_up)
    func_array.append(opt.shift_y_first_fourth_fifth_up)
    func_array.append(opt.shift_y_first_fourth_fifth_sixth_up)
    func_array.append(opt.shift_y_first_fourth_sixth_up)
    func_array.append(opt.shift_y_first_fifth_up)
    func_array.append(opt.shift_y_first_fifth_sixth_up)
    func_array.append(opt.shift_y_first_sixth_up)
    func_array.append(opt.shift_y_second_up)
    func_array.append(opt.shift_y_second_third_up)
    func_array.append(opt.shift_y_second_third_fourth_up)
    func_array.append(opt.shift_y_second_third_fourth_fifth_up)
    func_array.append(opt.shift_y_second_third_fourth_fifth_sixth_up)
    func_array.append(opt.shift_y_second_third_fourth_sixth_up)
    func_array.append(opt.shift_y_second_third_fifth_up)
    func_array.append(opt.shift_y_second_third_fifth_sixth_up)
    func_array.append(opt.shift_y_second_third_sixth_up)
    func_array.append(opt.shift_y_second_fourth_up)
    func_array.append(opt.shift_y_second_fourth_fifth_up)
    func_array.append(opt.shift_y_second_fourth_fifth_sixth_up)
    func_array.append(opt.shift_y_second_fourth_sixth_up)
    func_array.append(opt.shift_y_second_fifth_up)
    func_array.append(opt.shift_y_second_fifth_sixth_up)
    func_array.append(opt.shift_y_second_sixth_up)
    func_array.append(opt.shift_y_third_up)
    func_array.append(opt.shift_y_third_fourth_up)
    func_array.append(opt.shift_y_third_fourth_fifth_up)
    func_array.append(opt.shift_y_third_fourth_fifth_sixth_up)
    func_array.append(opt.shift_y_third_fourth_sixth_up)
    func_array.append(opt.shift_y_third_fifth_up)
    func_array.append(opt.shift_y_third_fifth_sixth_up)
    func_array.append(opt.shift_y_third_sixth_up)
    func_array.append(opt.shift_y_fourth_up)
    func_array.append(opt.shift_y_fourth_fifth_up)
    func_array.append(opt.shift_y_fourth_fifth_sixth_up)
    func_array.append(opt.shift_y_fourth_sixth_up)
    func_array.append(opt.shift_y_fifth_up)
    func_array.append(opt.shift_y_fifth_sixth_up)
    func_array.append(opt.shift_y_sixth_up)
    #################################################################################
    func_array.append(opt.shift_y_first_down)
    func_array.append(opt.shift_y_first_second_down)
    func_array.append(opt.shift_y_first_second_third_down)
    func_array.append(opt.shift_y_first_second_third_fourth_down)
    func_array.append(opt.shift_y_first_second_third_fourth_fifth_down)
    func_array.append(opt.shift_y_first_second_third_fourth_sixth_down)
    func_array.append(opt.shift_y_first_second_third_fifth_down)
    func_array.append(opt.shift_y_first_second_third_fifth_sixth_down)
    func_array.append(opt.shift_y_first_second_third_sixth_down)
    func_array.append(opt.shift_y_first_second_fourth_down)
    func_array.append(opt.shift_y_first_second_fourth_fifth_down)
    func_array.append(opt.shift_y_first_second_fourth_fifth_sixth_down)
    func_array.append(opt.shift_y_first_second_fourth_sixth_down)
    func_array.append(opt.shift_y_first_second_fifth_down)
    func_array.append(opt.shift_y_first_second_fifth_sixth_down)
    func_array.append(opt.shift_y_first_second_sixth_down)
    func_array.append(opt.shift_y_first_third_down)
    func_array.append(opt.shift_y_first_third_fourth_down)
    func_array.append(opt.shift_y_first_third_fourth_fifth_down)
    func_array.append(opt.shift_y_first_third_fourth_fifth_sixth_down)
    func_array.append(opt.shift_y_first_third_fourth_sixth_down)
    func_array.append(opt.shift_y_first_third_fifth_down)
    func_array.append(opt.shift_y_first_third_fifth_sixth_down)
    func_array.append(opt.shift_y_first_third_sixth_down)
    func_array.append(opt.shift_y_first_fourth_down)
    func_array.append(opt.shift_y_first_fourth_fifth_down)
    func_array.append(opt.shift_y_first_fourth_fifth_sixth_down)
    func_array.append(opt.shift_y_first_fourth_sixth_down)
    func_array.append(opt.shift_y_first_fifth_down)
    func_array.append(opt.shift_y_first_fifth_sixth_down)
    func_array.append(opt.shift_y_first_sixth_down)
    func_array.append(opt.shift_y_second_down)
    func_array.append(opt.shift_y_second_third_down)
    func_array.append(opt.shift_y_second_third_fourth_down)
    func_array.append(opt.shift_y_second_third_fourth_fifth_down)
    func_array.append(opt.shift_y_second_third_fourth_fifth_sixth_down)
    func_array.append(opt.shift_y_second_third_fourth_sixth_down)
    func_array.append(opt.shift_y_second_third_fifth_down)
    func_array.append(opt.shift_y_second_third_fifth_sixth_down)
    func_array.append(opt.shift_y_second_third_sixth_down)
    func_array.append(opt.shift_y_second_fourth_down)
    func_array.append(opt.shift_y_second_fourth_fifth_down)
    func_array.append(opt.shift_y_second_fourth_fifth_sixth_down)
    func_array.append(opt.shift_y_second_fourth_sixth_down)
    func_array.append(opt.shift_y_second_fifth_down)
    func_array.append(opt.shift_y_second_fifth_sixth_down)
    func_array.append(opt.shift_y_second_sixth_down)
    func_array.append(opt.shift_y_third_down)
    func_array.append(opt.shift_y_third_fourth_down)
    func_array.append(opt.shift_y_third_fourth_fifth_down)
    func_array.append(opt.shift_y_third_fourth_fifth_sixth_down)
    func_array.append(opt.shift_y_third_fourth_sixth_down)
    func_array.append(opt.shift_y_third_fifth_down)
    func_array.append(opt.shift_y_third_fifth_sixth_down)
    func_array.append(opt.shift_y_third_sixth_down)
    func_array.append(opt.shift_y_fourth_down)
    func_array.append(opt.shift_y_fourth_fifth_down)
    func_array.append(opt.shift_y_fourth_fifth_sixth_down)
    func_array.append(opt.shift_y_fourth_sixth_down)
    func_array.append(opt.shift_y_fifth_down)
    func_array.append(opt.shift_y_fifth_sixth_down)
    func_array.append(opt.shift_y_sixth_down)

    O,X,Y,A = perf_test(x,y, func_array)
    
    return(O,X,Y,A)
    
def perf_test(x,y, func_array):
    objs = []
    xs = []
    ys = []
    actions = []
    ox = list(x)
    oy = list(y)
        
    for func in func_array:
        x = list(ox)
        y = list(oy)
        OBJ,X,Y,A = func(x,y)
        objs.append(OBJ)
        xs.append(X)
        ys.append(Y)
        actions.append(A)
        
    return (objs,xs,ys,actions)
    


    
