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,35 +6,38 @@ 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)
header,keyword = f.readline().split() except TypeError:
if keyword == 'header': f = fname
header = int(header)
header,keyword = f.readline().split()
if keyword == 'header':
header = int(header)
else:
raise Exception
self.comments = [f.readline()[:-1] for i in range(header-1)]
labels_raw = f.readline().split()
self.data = pd.read_csv(f,delim_whitespace=True,header=None)
labels_repeated = [l.split('_',1)[1] if '_' in l else l for l in labels_raw]
self.data.rename(columns=dict(zip([l for l in self.data.columns],labels_repeated)),inplace=True)
self.shape = {}
for l in labels_raw:
tensor_column = re.search(':.*?_',l)
if tensor_column:
my_shape = tensor_column.group()[1:-1].split('x')
self.shape[l.split('_',1)[1]] = tuple([int(d) for d in my_shape])
else: else:
raise Exception vector_column = re.match('.*?_',l)
self.comments = [f.readline()[:-1] for i in range(header-1)] if vector_column:
labels_raw = f.readline().split() self.shape[l.split('_',1)[1]] = (int(l.split('_',1)[0]),)
self.data = pd.read_csv(f,delim_whitespace=True,header=None)
labels_repeated = [l.split('_',1)[1] if '_' in l else l for l in labels_raw]
self.data.rename(columns=dict(zip([l for l in self.data.columns],labels_repeated)),inplace=True)
self.shape = {}
for l in labels_raw:
tensor_column = re.search(':.*?_',l)
if tensor_column:
my_shape = tensor_column.group()[1:-1].split('x')
self.shape[l.split('_',1)[1]] = tuple([int(d) for d in my_shape])
else: else:
vector_column = re.match('.*?_',l) self.shape[l]=(1,)
if vector_column:
self.shape[l.split('_',1)[1]] = (int(l.split('_',1)[0]),)
else:
self.shape[l]=(1,)
self.labels = list(dict.fromkeys(labels_repeated)) self.labels = list(dict.fromkeys(labels_repeated))
def get_array(self,label): def get_array(self,label):
@ -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:
for line in header: f.write(line+'\n') f = open(fname,'w')
self.data.to_csv(f,sep=' ',index=False,header=False) except TypeError:
f = fname
for line in header: f.write(line+'\n')
self.data.to_csv(f,sep=' ',index=False,header=False)