easier to read, more flexible

This commit is contained in:
Martin Diehl 2020-06-03 10:43:07 +02:00
parent 4f495a7a05
commit c5d0c7e52e
3 changed files with 12 additions and 14 deletions

View File

@ -1,9 +1,9 @@
"""Tools for pre and post processing of DAMASK simulations."""
import os as _os
from pathlib import Path as _Path
import re as _re
name = 'damask'
with open(_os.path.join(_os.path.dirname(__file__),'VERSION')) as _f:
with open(_Path(__file__).parent/_Path('VERSION')) as _f:
version = _re.sub(r'^v','',_f.readline().strip())
# make classes directly accessible as damask.Class

View File

@ -6,6 +6,7 @@ import os
import datetime
import xml.etree.ElementTree as ET
import xml.dom.minidom
from pathlib import Path
from functools import partial
import h5py
@ -88,7 +89,7 @@ class Result:
'con_physics': self.con_physics, 'mat_physics': self.mat_physics
}
self.fname = os.path.abspath(fname)
self.fname = Path(fname).absolute()
self._allow_overwrite = False
@ -1163,7 +1164,7 @@ class Result:
'Dimensions': '{} {} {} {}'.format(*self.grid,np.prod(shape))}
data_items[-1].text='{}:{}'.format(os.path.split(self.fname)[1],name)
with open(os.path.splitext(self.fname)[0]+'.xdmf','w') as f:
with open(self.fname.with_suffix('.xdmf').name,'w') as f:
f.write(xml.dom.minidom.parseString(ET.tostring(xdmf).decode()).toprettyxml())
@ -1239,7 +1240,4 @@ class Result:
u = self.read_dataset(self.get_dataset_location('u_n' if mode.lower() == 'cell' else 'u_p'))
v.add(u,'u')
file_out = '{}_inc{}'.format(os.path.splitext(os.path.basename(self.fname))[0],
inc[3:].zfill(N_digits))
v.write(file_out)
v.write('{}_inc{}'.format(self.fname.stem,inc[3:].zfill(N_digits)))

View File

@ -1,4 +1,4 @@
import os
from pathlib import Path
import pandas as pd
import numpy as np
@ -126,8 +126,8 @@ class VTK:
vtkUnstructuredGrid, and vtkPolyData.
"""
ext = os.path.splitext(fname)[1]
if ext == '.vtk':
ext = Path(fname).suffix
if ext == '.vtk' or dataset_type:
reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(fname)
reader.Update()
@ -176,10 +176,10 @@ class VTK:
writer = vtk.vtkXMLPolyDataWriter()
default_ext = writer.GetDefaultFileExtension()
name, ext = os.path.splitext(fname)
ext = Path(fname).suffix
if ext and ext != '.'+default_ext:
raise ValueError('Given extension {} is not .{}'.format(ext,default_ext))
writer.SetFileName('{}.{}'.format(name,default_ext))
raise ValueError('Given extension {} does not match default .{}'.format(ext,default_ext))
writer.SetFileName(str(Path(fname).with_suffix('.'+default_ext)))
writer.SetCompressorTypeToZLib()
writer.SetDataModeToBinary()
writer.SetInputData(self.geom)