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.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), table.get_array(options.stress).reshape(-1,3,3)).reshape(-1,9),
scriptID) scriptID)
table.to_ASCII() table.to_ASCII(name)

View File

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