#! /usr/bin/env python3
"""------------------------------*- python -*----------------------------------
  ==  == ====== ====   ====    |
                    \\     ||  | HZDR Multiphase Case Collection for OpenFOAM
  ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.811
  ||  ||  //    ||  // || \\   | License: Creative Commons Attribution 4.0
  ==  == ====== ====   ==  ==  |
-------------------------------------------------------------------------------
License
    This file is part of the HZDR Multiphase Case Collection for OpenFOAM.

    Copyright (C) 2022 Helmholtz-Zentrum Dresden-Rossendorf

    The HZDR Multiphase Case Collection for OpenFOAM is licensed under the
    Creative Commons Attribution 4.0 International License. To view a copy of
    this license, visit http://creativecommons.org/licenses/by/4.0/ or send a
    letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

Description
    Calculate crosssectional-averaged axial plots

----------------------------------------------------------------------------"""

import os
import csv
import string
import operator

timestep = '4'

z = [-0.21, -0.2, -0.07, -0.06, -0.01, 0, -0.005, 0.005, 0.01, 0.02, 0.04, 0.05,
     0.09, 0.1, 0.19, 0.2, 0.39, 0.4]

source = '../postProcessing'

alpha_data = []
z_alpha = []
for x in string.ascii_uppercase:
    for y in range(1,3):
        for root, dirs, filenames in os.walk(source):
            for directory in dirs:
                if directory==str(x)+str(y)+".average":
                    with open(source+"/"+directory+"/0/volFieldValue.dat",
                              encoding="utf-8") as f:
                        f.readline()
                        for line in f:
                            if line.startswith(timestep):
                                data = line.split()
                                alpha_data.append(data[1])

z_alpha = list(zip(z, alpha_data))
print(z_alpha)
z_alpha_sort = sorted(z_alpha, key = operator.itemgetter(0))
print(z_alpha_sort)

result_dir = "../postProcessing/python"
if not os.path.exists(result_dir):
    os.mkdir(result_dir)
result_dir += "/" + timestep
if not os.path.exists(result_dir):
    os.mkdir(result_dir)

file_out = f"{result_dir}/alpha_axial_csAvg.val"

with open(file_out, 'w', encoding="utf-8") as f:
    csv_writer = csv.writer(f, delimiter=' ')
    csv_writer.writerows(z_alpha_sort)
f.close()


uAir_data = []
z_uAir = []

for x in string.ascii_uppercase:
    for y in range(1,3):
        for root, dirs, filenames in os.walk(source):
            for directory in dirs:
                if directory==str(x)+str(y)+".average":
                    with open(source+"/"+directory+"/0/volFieldValue.dat",
                              encoding="utf-8") as f:
                        f.readline()
                        for line in f:
                            if line.startswith(timestep):
                                line = line.replace(")", "").replace("(", "")
                                data = line.split()
                                uAir_data.append(data[2:])

z_uAir = [(k, l[0], l[1], l[2]) for k, l in zip(z, uAir_data)]
print(z_uAir)
z_uAir_sort = sorted(z_uAir, key = operator.itemgetter(0))
print(z_uAir_sort)

file_out = f"{result_dir}/uAir_axial_csAvg.val"

with open(file_out, 'w', encoding="utf-8") as f:
    csv_writer = csv.writer(f, delimiter=' ')
    csv_writer.writerows(z_uAir_sort)
f.close()
