# Extract boundary file *.mesh_mb from existing CCHE2D mesh file --Clemens Dorfmann--
#---------------------------------------------------------------------#
# Give here the filenames for Input and Output files:

Input_file = 'Fisching_aus_BK_400p_498x20.geo'

Output_file = 'Boundary_498x20_.mesh_mb'

factor = 1
#---------------------------------------------------------------------#

# read CCHE2D geo mesh file
CCHE2D_file = open(Input_file, 'r')
CC_text = CCHE2D_file.readlines()
CCHE2D_file.close()


# initialisation of some empty lists
line_ij = []
liste_x_CC = []
liste_y_CC = []
liste_x_left_bound = []
liste_x_right_bound = []
liste_y_left_bound = []
liste_y_right_bound = []

# get i (streamwise node number) and j (crosswise node number) from first line
line_ij.append(CC_text[0].split())
i = int(line_ij[0][1])
j = int(line_ij[0][0])

CC_text.pop(0)


# read x y values into lists
for lines in CC_text:
    CC_text = lines.split()
    liste_x_CC.append(float(CC_text[0]))
    liste_y_CC.append(float(CC_text[1]))

for k in range(1):
    liste_x_left_bound += (liste_x_CC[::factor*j])
    liste_y_left_bound += (liste_y_CC[::factor*j])
    liste_x_right_bound += (liste_x_CC[j-1::factor*j])
    liste_y_right_bound += (liste_y_CC[j-1::factor*j])

liste_x_CC_bound = liste_x_left_bound + liste_x_right_bound
liste_y_CC_bound = liste_y_left_bound + liste_y_right_bound

node_numb = len(liste_x_left_bound)
print (node_numb)

# fill columns 1 - 5
col_1 = 2 * range(1,(node_numb + 1))
col_2 = 2 * [0] * node_numb
col_3 = 2 * [0] * node_numb
col_4 = 2 * [0] * node_numb
col_5 = 2 * [0] * node_numb
print (col_1)
# 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(node_numb) + '\n' \
                        + str(1) + '\t' +  str(1) + '\t' +  str(0) + '\n' \
                        + str(0) + '\t' +  str(node_numb) + '\t' +  str(node_numb) + '\n')

for k in range(2 * node_numb):
    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_bound[k]) + '\t' + str(liste_y_CC_bound[k]) + '\n')
CCHE2D_bound_file.close()











