Skip to content

Commit ed4f2ac

Browse files
committed
Python: fix prof3d.py to work with changing number of cells in time
1 parent d448447 commit ed4f2ac

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

Utilities/Python/scripts/prof3d.py

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
Use option --save_animation to save the animation (no slider) to a movie file
1212
1313
Example:
14-
$ python prof3d.py --with-slider
14+
$ python prof3d.py --with_slider
1515
"""
1616

1717
import sys
18+
import csv
1819
import pandas as pd
1920
import matplotlib.pyplot as plt
2021
import numpy as np
@@ -24,8 +25,8 @@
2425
import argparse
2526

2627
parser = argparse.ArgumentParser()
27-
parser.add_argument('--with-slider', action='store_true', help='Control animation with a time slider')
28-
parser.add_argument('--save-animation', action='store_true', help='Save animation')
28+
parser.add_argument('--with_slider', action='store_true', help='Control animation with a time slider')
29+
parser.add_argument('--save_animation', action='store_true', help='Save animation')
2930

3031
args = parser.parse_args()
3132

@@ -39,41 +40,60 @@
3940
# Close all previously opened figures
4041
plt.close('all')
4142

42-
tmpa = 20.
43-
scalar_min = 20.
44-
scalar_max = 360.
43+
scalar_min = 0.
44+
scalar_max = 120.
4545

46-
filenames = ['./my_prof_1.csv',
47-
'./my_prof_2.csv',
48-
'./my_prof_3.csv]
46+
filenames = ['../Current_Results/pine_21O2_40_1C_cat_prof_4.csv']
4947

5048
# create lists to store information about each profile
5149
IOR = []
5250
X = []
5351
Y = []
5452
Z = []
55-
df = []
53+
df = {}
5654

57-
for i in range(len(filenames)):
55+
for i, filename in enumerate(filenames):
56+
57+
data = []
58+
max_cols = 0
5859

5960
# read header information
6061

61-
with open(filenames[i]) as f:
62+
with open(filename,'r') as f:
6263
# Skip the first 1 lines
6364
for j in range(1):
6465
next(f)
6566
first_line = f.readline().strip('\n')
6667

67-
header=first_line.split(",")[1:5]
68-
IOR.append(int(header[0])) #; print(IOR)
69-
X.append(float(header[1])) #; print(X)
70-
Y.append(float(header[2])) #; print(Y)
71-
Z.append(float(header[3])) #; print(Z)
72-
73-
# sys.exit()
74-
75-
df.append(pd.read_csv(filenames[i],skiprows=3,header=None))
76-
df[i].fillna(tmpa, inplace=True)
68+
header=first_line.split(",")[1:5]
69+
IOR.append(int(header[0])) ; print(IOR)
70+
X.append(float(header[1])) ; print(X)
71+
Y.append(float(header[2])) ; print(Y)
72+
Z.append(float(header[3])) ; print(Z)
73+
next(f)
74+
75+
# Read lines one at a time
76+
while True:
77+
line = f.readline()
78+
if not line: # End of file
79+
break
80+
row = line.strip().split(',') # Adjust delimiter if needed
81+
82+
# Convert each element to float, handling errors gracefully
83+
try:
84+
row = [float(value) if value else None for value in row]
85+
except ValueError:
86+
# If a value cannot be converted, keep it as None
87+
row = [float(value) if value.replace('.', '', 1).isdigit() else None for value in row]
88+
89+
data.append(row)
90+
max_cols = max(max_cols, len(row)) # Track the maximum number of columns
91+
92+
# Normalize rows to have the same number of columns
93+
data = [row + [None] * (max_cols - len(row)) for row in data]
94+
95+
# Convert to a Pandas DataFrame
96+
df[i] = pd.DataFrame(data)
7797

7898
# sys.exit()
7999

0 commit comments

Comments
 (0)