similar logic as in geom class

- filename is not part of the object
- transparent handling of files, strings, and path-like objects for file
IO
This commit is contained in:
Martin Diehl 2019-11-26 14:26:25 +01:00
parent 81abc43920
commit 845cfc34ec
2 changed files with 39 additions and 33 deletions

View File

@ -39,4 +39,4 @@ for name in filenames:
table.add_array('Cauchy',damask.mechanics.Cauchy(table.get_array(options.defgrad).reshape(-1,3,3),
table.get_array(options.stress).reshape(-1,3,3)).reshape(-1,9),
scriptID)
table.to_ASCII()
table.to_ASCII(name)

View File

@ -6,9 +6,12 @@ import numpy as np
class Table():
"""Read and write to ASCII tables"""
def __init__(self,name):
self.name = name
with open(self.name) as f:
def __init__(self,fname):
try:
f = open(fname)
except TypeError:
f = fname
header,keyword = f.readline().split()
if keyword == 'header':
header = int(header)
@ -56,7 +59,7 @@ class Table():
self.data = pd.concat([self.data,new_data],axis=1)
def to_ASCII(self,name=None):
def to_ASCII(self,fname):
labels = []
for l in self.labels:
if(self.shape[l] == (1,)):
@ -72,6 +75,9 @@ class Table():
+ self.comments\
+ [' '.join(labels)]
with open(name if name is not None else self.name,'w') as f:
try:
f = open(fname,'w')
except TypeError:
f = fname
for line in header: f.write(line+'\n')
self.data.to_csv(f,sep=' ',index=False,header=False)