avoid repetition and do correct encode/decode

This commit is contained in:
Martin Diehl 2020-05-25 19:57:32 +02:00
parent 5d0a2aac8f
commit d9d089c359
1 changed files with 24 additions and 18 deletions

View File

@ -1,5 +1,6 @@
import multiprocessing import multiprocessing
import re import re
import inspect
import glob import glob
import os import os
import datetime import datetime
@ -502,7 +503,7 @@ class Result:
'meta': { 'meta': {
'Unit': x['meta']['Unit'], 'Unit': x['meta']['Unit'],
'Description': 'Absolute value of {} ({})'.format(x['label'],x['meta']['Description']), 'Description': 'Absolute value of {} ({})'.format(x['label'],x['meta']['Description']),
'Creator': 'damask.Result.add_abs v{}'.format(version) 'Creator': 'add_absolute'
} }
} }
def add_absolute(self,x): def add_absolute(self,x):
@ -530,7 +531,7 @@ class Result:
'meta': { 'meta': {
'Unit': kwargs['unit'], 'Unit': kwargs['unit'],
'Description': '{} (formula: {})'.format(kwargs['description'],kwargs['formula']), 'Description': '{} (formula: {})'.format(kwargs['description'],kwargs['formula']),
'Creator': 'damask.Result.add_calculation v{}'.format(version) 'Creator': 'add_calculation'
} }
} }
def add_calculation(self,label,formula,unit='n/a',description=None,vectorized=True): def add_calculation(self,label,formula,unit='n/a',description=None,vectorized=True):
@ -569,7 +570,7 @@ class Result:
'Description': 'Cauchy stress calculated from {} ({}) '.format(P['label'], 'Description': 'Cauchy stress calculated from {} ({}) '.format(P['label'],
P['meta']['Description'])+\ P['meta']['Description'])+\
'and {} ({})'.format(F['label'],F['meta']['Description']), 'and {} ({})'.format(F['label'],F['meta']['Description']),
'Creator': 'damask.Result.add_Cauchy v{}'.format(version) 'Creator': 'add_Cauchy'
} }
} }
def add_Cauchy(self,P='P',F='F'): def add_Cauchy(self,P='P',F='F'):
@ -595,7 +596,7 @@ class Result:
'meta': { 'meta': {
'Unit': T['meta']['Unit'], 'Unit': T['meta']['Unit'],
'Description': 'Determinant of tensor {} ({})'.format(T['label'],T['meta']['Description']), 'Description': 'Determinant of tensor {} ({})'.format(T['label'],T['meta']['Description']),
'Creator': 'damask.Result.add_determinant v{}'.format(version) 'Creator': 'add_determinant'
} }
} }
def add_determinant(self,T): def add_determinant(self,T):
@ -619,7 +620,7 @@ class Result:
'meta': { 'meta': {
'Unit': T['meta']['Unit'], 'Unit': T['meta']['Unit'],
'Description': 'Deviator of tensor {} ({})'.format(T['label'],T['meta']['Description']), 'Description': 'Deviator of tensor {} ({})'.format(T['label'],T['meta']['Description']),
'Creator': 'damask.Result.add_deviator v{}'.format(version) 'Creator': 'add_deviator'
} }
} }
def add_deviator(self,T): def add_deviator(self,T):
@ -643,7 +644,7 @@ class Result:
'meta' : { 'meta' : {
'Unit': T_sym['meta']['Unit'], 'Unit': T_sym['meta']['Unit'],
'Description': 'Eigenvalues of {} ({})'.format(T_sym['label'],T_sym['meta']['Description']), 'Description': 'Eigenvalues of {} ({})'.format(T_sym['label'],T_sym['meta']['Description']),
'Creator': 'damask.Result.add_eigenvalues v{}'.format(version) 'Creator': 'add_eigenvalues'
} }
} }
def add_eigenvalues(self,T_sym): def add_eigenvalues(self,T_sym):
@ -667,7 +668,7 @@ class Result:
'meta' : { 'meta' : {
'Unit': '1', 'Unit': '1',
'Description': 'Eigenvectors of {} ({})'.format(T_sym['label'],T_sym['meta']['Description']), 'Description': 'Eigenvectors of {} ({})'.format(T_sym['label'],T_sym['meta']['Description']),
'Creator': 'damask.Result.add_eigenvectors v{}'.format(version) 'Creator': 'add_eigenvectors'
} }
} }
def add_eigenvectors(self,T_sym): def add_eigenvectors(self,T_sym):
@ -703,7 +704,7 @@ class Result:
'Unit': 'RGB (8bit)', 'Unit': 'RGB (8bit)',
'Lattice': lattice, 'Lattice': lattice,
'Description': 'Inverse Pole Figure (IPF) colors along sample direction [{} {} {}]'.format(*m), 'Description': 'Inverse Pole Figure (IPF) colors along sample direction [{} {} {}]'.format(*m),
'Creator': 'damask.Result.add_IPFcolor v{}'.format(version) 'Creator': 'add_IPFcolor'
} }
} }
def add_IPFcolor(self,q,l): def add_IPFcolor(self,q,l):
@ -729,7 +730,7 @@ class Result:
'meta': { 'meta': {
'Unit': T_sym['meta']['Unit'], 'Unit': T_sym['meta']['Unit'],
'Description': 'Maximum shear component of {} ({})'.format(T_sym['label'],T_sym['meta']['Description']), 'Description': 'Maximum shear component of {} ({})'.format(T_sym['label'],T_sym['meta']['Description']),
'Creator': 'damask.Result.add_maximum_shear v{}'.format(version) 'Creator': 'add_maximum_shear'
} }
} }
def add_maximum_shear(self,T_sym): def add_maximum_shear(self,T_sym):
@ -756,7 +757,7 @@ class Result:
'meta': { 'meta': {
'Unit': T_sym['meta']['Unit'], 'Unit': T_sym['meta']['Unit'],
'Description': 'Mises equivalent {} of {} ({})'.format(t,T_sym['label'],T_sym['meta']['Description']), 'Description': 'Mises equivalent {} of {} ({})'.format(t,T_sym['label'],T_sym['meta']['Description']),
'Creator': 'damask.Result.add_Mises v{}'.format(version) 'Creator': 'add_Mises'
} }
} }
def add_Mises(self,T_sym): def add_Mises(self,T_sym):
@ -792,7 +793,7 @@ class Result:
'meta': { 'meta': {
'Unit': x['meta']['Unit'], 'Unit': x['meta']['Unit'],
'Description': '{}-norm of {} {} ({})'.format(o,t,x['label'],x['meta']['Description']), 'Description': '{}-norm of {} {} ({})'.format(o,t,x['label'],x['meta']['Description']),
'Creator': 'damask.Result.add_norm v{}'.format(version) 'Creator': 'add_norm'
} }
} }
def add_norm(self,x,ord=None): def add_norm(self,x,ord=None):
@ -820,7 +821,7 @@ class Result:
'Description': '2. Kirchhoff stress calculated from {} ({}) '.format(P['label'], 'Description': '2. Kirchhoff stress calculated from {} ({}) '.format(P['label'],
P['meta']['Description'])+\ P['meta']['Description'])+\
'and {} ({})'.format(F['label'],F['meta']['Description']), 'and {} ({})'.format(F['label'],F['meta']['Description']),
'Creator': 'damask.Result.add_PK2 v{}'.format(version) 'Creator': 'add_PK2'
} }
} }
def add_PK2(self,P='P',F='F'): def add_PK2(self,P='P',F='F'):
@ -856,7 +857,7 @@ class Result:
'Unit': '1', 'Unit': '1',
'Description': '{} coordinates of stereographic projection of pole (direction/plane) in crystal frame'\ 'Description': '{} coordinates of stereographic projection of pole (direction/plane) in crystal frame'\
.format('Polar' if polar else 'Cartesian'), .format('Polar' if polar else 'Cartesian'),
'Creator' : 'damask.Result.add_pole v{}'.format(version) 'Creator' : 'add_pole'
} }
} }
def add_pole(self,q,p,polar=False): def add_pole(self,q,p,polar=False):
@ -884,7 +885,7 @@ class Result:
'meta': { 'meta': {
'Unit': F['meta']['Unit'], 'Unit': F['meta']['Unit'],
'Description': 'Rotational part of {} ({})'.format(F['label'],F['meta']['Description']), 'Description': 'Rotational part of {} ({})'.format(F['label'],F['meta']['Description']),
'Creator': 'damask.Result.add_rotational_part v{}'.format(version) 'Creator': 'add_rotational_part'
} }
} }
def add_rotational_part(self,F): def add_rotational_part(self,F):
@ -908,7 +909,7 @@ class Result:
'meta': { 'meta': {
'Unit': T['meta']['Unit'], 'Unit': T['meta']['Unit'],
'Description': 'Spherical component of tensor {} ({})'.format(T['label'],T['meta']['Description']), 'Description': 'Spherical component of tensor {} ({})'.format(T['label'],T['meta']['Description']),
'Creator': 'damask.Result.add_spherical v{}'.format(version) 'Creator': 'add_spherical'
} }
} }
def add_spherical(self,T): def add_spherical(self,T):
@ -932,7 +933,7 @@ class Result:
'meta': { 'meta': {
'Unit': F['meta']['Unit'], 'Unit': F['meta']['Unit'],
'Description': 'Strain tensor of {} ({})'.format(F['label'],F['meta']['Description']), 'Description': 'Strain tensor of {} ({})'.format(F['label'],F['meta']['Description']),
'Creator': 'damask.Result.add_strain_tensor v{}'.format(version) 'Creator': 'add_strain_tensor'
} }
} }
def add_strain_tensor(self,F='F',t='V',m=0.0): def add_strain_tensor(self,F='F',t='V',m=0.0):
@ -964,7 +965,7 @@ class Result:
'Unit': F['meta']['Unit'], 'Unit': F['meta']['Unit'],
'Description': '{} stretch tensor of {} ({})'.format('Left' if t == 'V' else 'Right', 'Description': '{} stretch tensor of {} ({})'.format('Left' if t == 'V' else 'Right',
F['label'],F['meta']['Description']), F['label'],F['meta']['Description']),
'Creator': 'damask.Result.add_stretch_tensor v{}'.format(version) 'Creator': 'add_stretch_tensor'
} }
} }
def add_stretch_tensor(self,F='F',t='V'): def add_stretch_tensor(self,F='F',t='V'):
@ -1031,13 +1032,18 @@ class Result:
if self._allow_overwrite and result[0]+'/'+result[1]['label'] in f: if self._allow_overwrite and result[0]+'/'+result[1]['label'] in f:
dataset = f[result[0]+'/'+result[1]['label']] dataset = f[result[0]+'/'+result[1]['label']]
datset = result[1]['data'] datset = result[1]['data']
dataset.attrs['Overwritten'] = True dataset.attrs['Overwritten'] = 'Yes'.encode()
else: else:
dataset = f[result[0]].create_dataset(result[1]['label'],data=result[1]['data']) dataset = f[result[0]].create_dataset(result[1]['label'],data=result[1]['data'])
now = datetime.datetime.now().astimezone() now = datetime.datetime.now().astimezone()
dataset.attrs['Created'] = now.strftime('%Y-%m-%d %H:%M:%S%z').encode() dataset.attrs['Created'] = now.strftime('%Y-%m-%d %H:%M:%S%z').encode()
for l,v in result[1]['meta'].items(): for l,v in result[1]['meta'].items():
dataset.attrs[l]=v.encode() dataset.attrs[l]=v.encode()
creator = 'damask.Result.{} v{}'.format(dataset.attrs['Creator'].decode(),version)
dataset.attrs['Creator'] = creator.encode()
except OSError as err: except OSError as err:
print('Could not add dataset: {}.'.format(err)) print('Could not add dataset: {}.'.format(err))
lock.release() lock.release()