diff --git a/interfaces/cython/cantera/mixmaster/DataFrame.py b/interfaces/cython/cantera/mixmaster/DataFrame.py index c4aab8007..f67de94d7 100644 --- a/interfaces/cython/cantera/mixmaster/DataFrame.py +++ b/interfaces/cython/cantera/mixmaster/DataFrame.py @@ -125,11 +125,8 @@ class DataFrame(Frame): if self.plt: self.plt.destroy() - f = open(self.datafile.get(),'r') - lines = f.readlines() - vars = string.split(lines[0],',') - nlines = len(lines) - self.np = nlines - 1 + with open(self.datafile.get(),'r') as f: + vars = f.readline().split(',') nv = len(vars) vv = [] for n in range(nv): @@ -140,18 +137,11 @@ class DataFrame(Frame): break nv = len(vv) vars = vv - fdata = np.zeros((nv, self.np),'d') - for n in range(self.np): - v = string.split(lines[n+1],',') - for j in range(nv): - try: - fdata[j,n] = float(v[j]) - except: - fdata[j,n] = 0.0 - + fdata = np.genfromtxt(self.datafile.get(), dtype=float, delimiter=',', + skip_header=1).transpose() self.nsp = self.g.n_species self.y = np.zeros(self.nsp,'d') - self.data = np.zeros((self.nsp+6,self.np),'d') + self.data = np.zeros((self.nsp+6,fdata.shape[1]),'d') self.data[0,:] = fdata[0,:] self.label = ['-']*(self.nsp+6) self.label[0] = vars[0] @@ -184,10 +174,10 @@ class DataFrame(Frame): w.append((vars[n], self.newplot, 'check', self.loc, k + Y_LOC)) if self.data[P_LOC,0] == 0.0: - self.data[P_LOC,:] = np.ones(self.np,'d')*one_atm + self.data[P_LOC,:] = np.ones(fdata.shape[1],'d')*one_atm print('Warning: no pressure data. P set to 1 atm.') - self.sc.config(cnf={'from':0,'to':self.np-1}) + self.sc.config(cnf={'from':0,'to':fdata.shape[1]-1}) if self.loc.get() <= 0: self.loc.set(self.lastloc) self.updateplot() @@ -225,9 +215,9 @@ class DataFrame(Frame): if x['title'] == 'pressure': p = float(x.value()) fa = gdata.children('floatArray') - self.np = int(fa[0]['size']) + data_size = int(fa[0]['size']) - self.data = np.zeros((self.nsp+6,self.np),'d') + self.data = np.zeros((self.nsp+6,data_size),'d') w = [] for f in fa: t = f['title'] @@ -256,9 +246,9 @@ class DataFrame(Frame): self.label[V_LOC] = t w.append((t, self.newplot, 'check', self.loc, V_LOC)) - self.data[P_LOC,:] = np.ones(self.np,'d')*p + self.data[P_LOC,:] = np.ones(data_size,'d')*p self.label[P_LOC] = 'P (Pa)' - self.sc.config(cnf={'from':0,'to':self.np-1}) + self.sc.config(cnf={'from':0,'to':data_size-1}) if self.loc.get() <= 0: self.loc.set(self.lastloc) self.updateplot()