From 845cfc34ecc09fdb4e78c2a9c6378157b9cf0fe9 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 26 Nov 2019 14:26:25 +0100 Subject: [PATCH] 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 --- processing/post/addCauchy.py | 2 +- python/damask/table.py | 70 +++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/processing/post/addCauchy.py b/processing/post/addCauchy.py index 9037567f8..3a0e14da7 100755 --- a/processing/post/addCauchy.py +++ b/processing/post/addCauchy.py @@ -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) diff --git a/python/damask/table.py b/python/damask/table.py index 81901c252..e77ae2b9c 100644 --- a/python/damask/table.py +++ b/python/damask/table.py @@ -5,36 +5,39 @@ 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: - header,keyword = f.readline().split() - if keyword == 'header': - header = int(header) + + def __init__(self,fname): + try: + f = open(fname) + except TypeError: + f = fname + + 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: - 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]) + vector_column = re.match('.*?_',l) + if vector_column: + self.shape[l.split('_',1)[1]] = (int(l.split('_',1)[0]),) else: - vector_column = re.match('.*?_',l) - 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.shape[l]=(1,) + + self.labels = list(dict.fromkeys(labels_repeated)) def get_array(self,label): @@ -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: - for line in header: f.write(line+'\n') - self.data.to_csv(f,sep=' ',index=False,header=False) + 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)