-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoamToCSV.py
More file actions
68 lines (46 loc) · 1.37 KB
/
foamToCSV.py
File metadata and controls
68 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
"""
This function offers an simple way to convert the results file from FOAM format to CSV.
It will erase the strings which start with a word.
How to use it: python foamToCSV.py time/field outputfile.
i.e. python foamToCSV 0/p p
Warning: It works only on reconstructed file
Warning: it will erase also the first numerical line
Warning: it will work only on scalar field. For vector, see foamToCSVVector.py
Tested with Python 2.7.16
"""
import os
import sys
version=1
subversion=0
if sys.argv[1]=='-help':
print("Version "+str(version)+"."+str(subversion))
print("-version for version number")
print("-help to run the help")
print("Run as python foamToCSV.py time/field outputfile")
print("WARNING: It doesn't work with vector fields")
if sys.argv[1]=='-version':
print("Version "+str(version)+"."+str(subversion))
def read_file(file_name):
with open(file_name) as f:
content = f.readlines()
f.close()
return content
def foamToCSV(file_name, filename_out):
file_contents = read_file(file_name)
temp = open(filename_out, 'w')
with open(file_name, 'r') as f:
flag=False
for line in file_contents:
if line[0].isdigit() and flag:
temp.write(line)
if line[0].isdigit():
flag=True
else:
flag=False
temp.close()
f.close()
return
file_name = sys.argv[1]
filename_out = sys.argv[2]
foamToCSV(file_name, filename_out)