From 0871111c118d2bd45094a175de25c9b4cbc0e573 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 19 Mar 2020 08:45:25 +0100 Subject: [PATCH] consistent and correct number of leading zeros --- processing/post/DADF5toDREAM3D.py | 57 ++++++++++++++++--------------- python/damask/result.py | 2 +- python/damask/util.py | 2 +- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/processing/post/DADF5toDREAM3D.py b/processing/post/DADF5toDREAM3D.py index af8e1067b..d90f3a19c 100755 --- a/processing/post/DADF5toDREAM3D.py +++ b/processing/post/DADF5toDREAM3D.py @@ -8,18 +8,18 @@ import numpy as np import damask -class AttributeManagerNullterm(h5py.AttributeManager): +class AttributeManagerNullterm(h5py.AttributeManager): """ Attribute management for DREAM.3D hdf5 files. - + String attribute values are stored as fixed-length string with NULLTERM - + References ---------- https://stackoverflow.com/questions/38267076 https://stackoverflow.com/questions/52750232 - """ + """ def create(self, name, data, shape=None, dtype=None): if isinstance(data,str): @@ -28,7 +28,7 @@ class AttributeManagerNullterm(h5py.AttributeManager): super().create(name=name,data=data+' ',dtype = h5py.Datatype(tid)) else: super().create(name=name,data=data,shape=shape,dtype=dtype) - + h5py._hl.attrs.AttributeManager = AttributeManagerNullterm # 'Monkey patch' @@ -59,61 +59,64 @@ options = parser.parse_args() for filename in options.filenames: f = damask.Result(filename) + N_digits = int(np.floor(np.log10(int(f.increments[-1][3:]))))+1 + f.pick('increments',options.inc) - for increment in damask.util.show_progress(f.iterate('increments'),len(f.selection['increments'])): + for inc in damask.util.show_progress(f.iterate('increments'),len(f.selection['increments'])): dirname = os.path.abspath(os.path.join(os.path.dirname(filename),options.dir)) try: os.mkdir(dirname) except FileExistsError: pass - o = h5py.File(dirname + '/' + os.path.splitext(filename)[0] + '_{}.dream3D'.format(increment),'w') + o = h5py.File(dirname + '/' + os.path.splitext(filename)[0] \ + + 'inc_{}.dream3D'.format(inc[3:].zfill(N_digits)),'w') o.attrs['DADF5toDREAM3D'] = '1.0' - o.attrs['FileVersion'] = '7.0' - + o.attrs['FileVersion'] = '7.0' + for g in ['DataContainerBundles','Pipeline']: # empty groups (needed) o.create_group(g) - data_container_label = 'DataContainers/ImageDataContainer' + data_container_label = 'DataContainers/ImageDataContainer' cell_data_label = data_container_label + '/CellData' # Phase information of DREAM.3D is constituent ID in DAMASK - o[cell_data_label + '/Phases'] = f.get_constituent_ID().reshape(tuple(f.grid)+(1,)) + o[cell_data_label + '/Phases'] = f.get_constituent_ID().reshape(tuple(f.grid)+(1,)) DAMASK_quaternion = f.read_dataset(f.get_dataset_location('orientation')) # Convert: DAMASK uses P = -1, DREAM.3D uses P = +1. Also change position of imagninary part DREAM_3D_quaternion = np.hstack((-DAMASK_quaternion['x'],-DAMASK_quaternion['y'],-DAMASK_quaternion['z'], DAMASK_quaternion['w'])).astype(np.float32) o[cell_data_label + '/Quats'] = DREAM_3D_quaternion.reshape(tuple(f.grid)+(4,)) - + # Attributes to CellData group o[cell_data_label].attrs['AttributeMatrixType'] = np.array([3],np.uint32) o[cell_data_label].attrs['TupleDimensions'] = f.grid.astype(np.uint64) - + # Common Attributes for groups in CellData for group in ['/Phases','/Quats']: o[cell_data_label + group].attrs['DataArrayVersion'] = np.array([2],np.int32) o[cell_data_label + group].attrs['Tuple Axis Dimensions'] = 'x={},y={},z={}'.format(*f.grid) - + o[cell_data_label + '/Phases'].attrs['ComponentDimensions'] = np.array([1],np.uint64) o[cell_data_label + '/Phases'].attrs['ObjectType'] = 'DataArray' o[cell_data_label + '/Phases'].attrs['TupleDimensions'] = f.grid.astype(np.uint64) - + o[cell_data_label + '/Quats'].attrs['ComponentDimensions'] = np.array([4],np.uint64) - o[cell_data_label + '/Quats'].attrs['ObjectType'] = 'DataArray' + o[cell_data_label + '/Quats'].attrs['ObjectType'] = 'DataArray' o[cell_data_label + '/Quats'].attrs['TupleDimensions'] = f.grid.astype(np.uint64) - + # Create EnsembleAttributeMatrix - ensemble_label = data_container_label + '/EnsembleAttributeMatrix' - + ensemble_label = data_container_label + '/EnsembleAttributeMatrix' + # Data CrystalStructures o[ensemble_label + '/CrystalStructures'] = np.uint32(np.array([999,\ Crystal_structures[f.get_crystal_structure()]])).reshape(2,1) o[ensemble_label + '/PhaseTypes'] = np.uint32(np.array([999,Phase_types['Primary']])).reshape(2,1) # ToDo - + # Attributes Ensemble Matrix o[ensemble_label].attrs['AttributeMatrixType'] = np.array([11],np.uint32) o[ensemble_label].attrs['TupleDimensions'] = np.array([2], np.uint64) - + # Attributes for data in Ensemble matrix for group in ['CrystalStructures','PhaseTypes']: # 'PhaseName' not required MD: But would be nice to take the phase name mapping o[ensemble_label+'/'+group].attrs['ComponentDimensions'] = np.array([1],np.uint64) @@ -121,15 +124,15 @@ for filename in options.filenames: o[ensemble_label+'/'+group].attrs['DataArrayVersion'] = np.array([2],np.int32) o[ensemble_label+'/'+group].attrs['ObjectType'] = 'DataArray' o[ensemble_label+'/'+group].attrs['TupleDimensions'] = np.array([2],np.uint64) - + geom_label = data_container_label + '/_SIMPL_GEOMETRY' - + o[geom_label + '/DIMENSIONS'] = np.int64(f.grid) o[geom_label + '/ORIGIN'] = np.float32(np.zeros(3)) o[geom_label + '/SPACING'] = np.float32(f.size) - + o[geom_label].attrs['GeometryName'] = 'ImageGeometry' o[geom_label].attrs['GeometryTypeName'] = 'ImageGeometry' - o[geom_label].attrs['GeometryType'] = np.array([0],np.uint32) - o[geom_label].attrs['SpatialDimensionality'] = np.array([3],np.uint32) - o[geom_label].attrs['UnitDimensionality'] = np.array([3],np.uint32) + o[geom_label].attrs['GeometryType'] = np.array([0],np.uint32) + o[geom_label].attrs['SpatialDimensionality'] = np.array([3],np.uint32) + o[geom_label].attrs['UnitDimensionality'] = np.array([3],np.uint32) diff --git a/python/damask/result.py b/python/damask/result.py index e50a689fb..c1d820685 100644 --- a/python/damask/result.py +++ b/python/damask/result.py @@ -1039,7 +1039,7 @@ class Result: elif mode.lower()=='point': v = VTK.from_polyData(self.cell_coordinates()) - N_digits = int(np.floor(np.log10(min(int(self.increments[-1][3:]),1))))+1 + N_digits = int(np.floor(np.log10(int(self.increments[-1][3:]))))+1 for i,inc in enumerate(util.show_progress(self.iterate('increments'),len(self.selection['increments']))): diff --git a/python/damask/util.py b/python/damask/util.py index 94d8cb95c..b73206067 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -258,7 +258,7 @@ def scale_to_coprime(v): return m//reduce(np.gcd,m) -class return_message(): +class return_message: """Object with formatted return message.""" def __init__(self,message):