# BLUE KENUE boundary files (left and right) to CCHE2D boundary file *.mesh_mb --Clemens Dorfmann--
#---------------------------------------------------------------------#
import math
# Give here the filenames for Input and Output files:

Boundary_file_left = 'left_6p.i2s'
Boundary_file_right = 'right_6p.i2s'

Output_file = 'Boundary.mesh_mb'

#---------------------------------------------------------------------#

# read BlueKenue boundary files
left_file = open(Boundary_file_left, 'r')
right_file = open(Boundary_file_right, 'r')
Boundary_left = left_file.readlines()
Boundary_right = right_file.readlines()
left_file.close()
right_file.close()

# initialisation of some empty lists
liste_x_left = []
liste_y_left = []
liste_x_right = []
liste_y_right = []
liste_x_CC = []
liste_y_CC = []
left_noden = []
right_noden = []

# get number of nodes
hl = Boundary_left.index(':EndHeader\n')
hr = Boundary_right.index(':EndHeader\n')

left_noden.append(Boundary_left[hl+1].split())
right_noden.append(Boundary_right[hr+1].split())

nodenumb_left = int(left_noden[0][0])
nodenumb_right = int(right_noden[0][0])

if nodenumb_left == nodenumb_right:
    # delete BlueKenue header
    del Boundary_left[:hl+2]
    del Boundary_right[:hr+2]

    # read x y values into lists
    for lines in Boundary_left:
        Boundary_left = lines.split()
        liste_x_left.append(float(Boundary_left[0]))
        liste_y_left.append(float(Boundary_left[1]))

    for lines in Boundary_right:
        Boundary_right = lines.split()
        liste_x_right.append(float(Boundary_right[0]))
        liste_y_right.append(float(Boundary_right[1]))

    liste_x_CC = liste_x_left + liste_x_right
    liste_y_CC = liste_y_left + liste_y_right

    # fill columns 1 - 5
    col_1 = 2 * range(1,nodenumb_left+1)
    col_2 = 2 * [0] * nodenumb_left
    col_3 = 2 * [0] * nodenumb_left
    col_4 = 2 * [0] * nodenumb_left
    col_5 = 2 * [0] * nodenumb_left

    # write to CCHE2D boundary file
    CCHE2D_bound_file= open(Output_file, 'w')
    CCHE2D_bound_file.write(str(-22222) + '\t' + str(1) + '\t' +  str(0) + '\t' +  str(nodenumb_left) + '\n' \
                            + str(1) + '\t' +  str(1) + '\t' +  str(0) + '\n' \
                            + str(0) + '\t' +  str(nodenumb_left) + '\t' +  str(nodenumb_left) + '\n')

    for k in range(2*nodenumb_left):
        CCHE2D_bound_file.write(str(col_1[k]) + '\t' + str(col_2[k]) + '\t' + str(col_3[k])   \
                + '\t' + str(col_4[k]) + '\t' + str(col_5[k]) + '\t' \
                + str(liste_x_CC[k]) + '\t' + str(liste_y_CC[k]) + '\n')
    CCHE2D_bound_file.close()
else:
    print ('\nLeft and right boundary do not have the same number of nodes!')
    print ('Left boundary file has ' + str(nodenumb_left) + ' nodes.')
    print ('Right boundary file has ' + str(nodenumb_right) + ' nodes.')
    print ('Check the Input files!\n')






