"""------------------------------*- 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
    create a CSV file

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

import numpy as np

## scaling keeping aspect ratio
# set target size in x direction and y_max
inputfile  = 'C2_shape_OpenFOAM_t3.5_extracted_from_paper.dat'
outputfile = 'C2_shape_OpenFOAM_t3.5_xyz.csv'

target_xmin   = 0.2
target_xmax   = 0.8
target_ymax   = 1.8
zconst        = 0.51 # to write 3D points

# read coordinates
data = np.loadtxt(inputfile)
x = data[:,0]
y = data[:,1]

# scale
xcentre = np.mean(x)
ycentre = np.mean(y)

xloc = x - xcentre
yloc = y - ycentre

scale  = ( target_xmax - target_xmin )/( np.max(xloc)-np.min(xloc) )

xnew = xcentre + scale*xloc
ynew = ycentre + scale*yloc

# shift
xnew = xnew - np.max(xnew) + target_xmax
ynew = ynew - np.max(ynew) + target_ymax

# add z coordinate
znew = np.ones(np.size(xnew)) * zconst

# save to file
np.savetxt(outputfile,np.c_[xnew,ynew,znew],delimiter=',',header='x,y,z')
print(outputfile + " written.")
