Skip to content
Snippets Groups Projects
Commit d74c7598 authored by jarunanp's avatar jarunanp
Browse files

add file converter and config file

parent 8210a4ad
No related branches found
No related tags found
No related merge requests found
filename_in: "/home/jarunanp/work/geg/19_04_Justin_Paraview/Production_openBC/Results/openBC_prod_10yr.csv"
filename_out: "/home/jarunanp/work/geg/19_04_Justin_Paraview/output/openBC_prod_10yr.vtk"
npoint_d1: 80
npoint_d2: 40
npoint_d3: 15
import os
import sys
import numpy as np
import yaml
def read_config_file(filename):
""" read input values """
print(" ... reading an input file", filename)
with open(filename, "r") as f:
config = yaml.load(f)
return config
def read_results(config):
""" read mesh and results """
print(" ... reading a result file", config["filename_in"])
# Open the file and read data
with open(config["filename_in"], "r") as f:
data=f.readlines()
header = data.pop(0)
header = header.replace('\"','')
header = header.replace(' ','')
header = header.strip().split(",")
header.pop(0)
header.pop(0)
header.pop(0)
nresults = len(header)
print(header, nresults)
results = {}
for i in range(nresults):
key = header[i]
results[key] = []
npoint_d1 = config["npoint_d1"]
npoint_d2 = config["npoint_d2"]
npoint_d3 = config["npoint_d3"]
npoints = npoint_d1*npoint_d2*npoint_d3
#
ncell_d1 = npoint_d1-1
ncell_d2 = npoint_d2-1
ncell_d3 = npoint_d3-1
ncells = ncell_d1*ncell_d2*ncell_d3
# Write output
with open(config["filename_out"], "w") as g:
g.write("# vtk DataFile Version 2.0\n")
g.write("Mesh data\n")
g.write("ASCII\n")
g.write("DATASET STRUCTURED_GRID\n")
g.write("DIMENSIONS {:d} {:d} {:d}\n".format(npoint_d1, npoint_d2, npoint_d3))
g.write("POINTS {:d} double\n".format(npoints))
# Loop over all data lines
for line in data:
line = line.replace('NAN','0.0')
values = line.strip().split(",")
x = values[0]
y = values[1]
z = values[2]
g.write("{} {} {}\n".format(x, y, z))
# store results to arrays
for i in range(nresults):
key = header[i]
results[key].append(values[i+3])
# Write data
g.write("POINT_DATA {:d}\n".format(npoints))
g.write("FIELD FieldData {:d}\n".format(nresults))
for i in range(nresults):
key = header[i]
results_data = results[key]
g.write("{} 1 {:d} float\n".format(key, npoints))
for value in results_data:
g.write("{} ".format(value))
g.write("\n")
if __name__ == "__main__":
# read config file
config = read_config_file("config.yaml")
# read result file
read_results(config)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment