# BLUE KENUE channel mesh.t3s to SSIIM mesh koordina --Clemens Dorfmann--
# for Gabi, a present for the name day, 24.03.2012

#----------------------------------------------------------------------

# Give here the filenames for Input and Output mesh files:
Input_file = 'Mesh_j10.t3s'
Output_file = 'koordina'

j = 10              # give here the number of crosswise channel nodes j!

#----------------------------------------------------------------------

# Write SSIIM control file?:

control_file = True         # control_file  = True: write control file
                            #               = False: do not write control file

ZL = 9                      # Number of vertical layers (grid lines)
Roughness = 50.1            # Strickler roughness value [m^1/3/s]
Q = 120                     # Discharge [m^3/s]
WaterLevel = 685            # Water surface elevation [m]

#----------------------------------------------------------------------

# read BlueKenue t3s mesh file
BK_file = open(Input_file, 'r')
BK_text = BK_file.readlines()
BK_file.close()

# initialisation of some empty lists
liste_x_BK = []
liste_y_BK = []
liste_z_BK = []
liste_x_SS = []
liste_y_SS = []
liste_z_SS = []
liste_i = []
liste_j = []
line_noden = []

# get total number of nodes and number of streamwise nodes i
hE = BK_text.index(':EndHeader\n')
line_noden.append(BK_text[hE-4].split())
nodenumbers = int(line_noden[0][1])

i = nodenumbers / j

# delete header and connectivity table in BlueKenue t3s format
del BK_text[:hE+1]
del BK_text[nodenumbers:]

# read x y z values into lists
for lines in BK_text:
    BK_text = lines.split()
    liste_x_BK.append(float(BK_text[0]))
    liste_y_BK.append(float(BK_text[1]))
    liste_z_BK.append(float(BK_text[2]))

for k in range(i):
    liste_x_SS += reversed(liste_x_BK[k::i])
    liste_y_SS += reversed(liste_y_BK[k::i])
    liste_z_SS += reversed(liste_z_BK[k::i])

# i and j node numbering for SSIIM
for k in range(1, i + 1):
	liste_i += [k] * j

liste_j += range(1, j + 1) * i

# write to SSIIM koordina file
SSIIM_file= open(Output_file, 'w')

for k in range(i*j):
    SSIIM_file.write(str(liste_i[k]) + ' ' + str(liste_j[k]) + ' ' + str(liste_x_SS[k]) + \
            ' ' + str(liste_y_SS[k]) + ' ' + str(liste_z_SS[k]) + '\n')

SSIIM_file.close()


# write SSIIM control file
if control_file == True:
    liste_zl =[]
    for k in range(0, ZL):
        liste_zl += [round(k * 100.0/(ZL-1), 6)]
    liste_zl = str(liste_zl).replace(',', '')
    liste_zl = liste_zl.strip('[' ']')

    SSIIM_control_f= open('control', 'w')
    SSIIM_control_f.write('T                                title field' + '\n' \
                    + 'G 1 ' + str(i) + ' ' + str(j) + ' ' + str(ZL) + ' 1     grid and array sizes' + '\n' \
                    + 'G 3 ' + liste_zl + '      vertical grid distribution' + '\n' \
                    + 'W 1 ' + str(float(Roughness)) + ' ' + str(float(Q)) + ' ' + str(float(WaterLevel)) + '\n' \
                    + 'W 2 3 1 ' + str(int(i/2)) + ' ' + str(i) + '\n' \
                    + 'K 1 40000 60000' + '\n' \
                    + 'K 2 0 1' + '\n')
    SSIIM_control_f.close()







