possibility to export into new DADF5 file

This commit is contained in:
Martin Diehl 2022-11-06 23:19:14 +01:00
parent 0fa9631675
commit 015f1ec741
2 changed files with 60 additions and 0 deletions

View File

@ -1814,6 +1814,47 @@ class Result:
return None if (type(r) == dict and r == {}) else r
def export_DADF5(self,
fname,
output: Union[str, List[str]] = '*'):
"""
Export visible components into a new DADF5 file.
Parameters
----------
fname : str or pathlib.Path
Name of the DADF5 file to be created.
output : (list of) str
Names of the datasets to write.
Defaults to '*', in which case all datasets are write.
"""
if Path(fname).expanduser().absolute() == self.fname:
raise PermissionError(f'cannot overwrite {self.fname}')
with h5py.File(self.fname,'r') as f_in, h5py.File(fname,'w') as f_out:
for k,v in f_in.attrs.items():
f_out.attrs.create(k,v)
for g in ['setup','geometry','cell_to']:
f_in.copy(g,f_out)
for inc in util.show_progress(self.visible['increments']):
f_in.copy(inc,f_out,shallow=True)
for out in _match(output,f_in['/'.join([inc,'geometry'])].keys()):
f_in[inc]['geometry'].copy(out,f_out[inc]['geometry'])
for label in self.homogenizations:
f_in[inc]['homogenization'].copy(label,f_out[inc]['homogenization'],shallow=True)
for label in self.phases:
f_in[inc]['phase'].copy(label,f_out[inc]['phase'],shallow=True)
for ty in ['phase','homogenization']:
for label in self.visible[ty+'s']:
for field in _match(self.visible['fields'],f_in['/'.join([inc,ty,label])].keys()):
p = '/'.join([inc,ty,label,field])
for out in _match(output,f_in[p].keys()):
f_in[p].copy(out,f_out[p])
def place(self,
output: Union[str, List[str]] = '*',
flatten: bool = True,

View File

@ -6,6 +6,7 @@ import os
import sys
import hashlib
import fnmatch
import random
from datetime import datetime
import pytest
@ -562,3 +563,21 @@ class TestResult:
os.chdir(cwd)
r.export_setup('material.yaml',target_dir=t)
assert 'material.yaml' in os.listdir(absdir); (absdir/'material.yaml').unlink()
@pytest.mark.parametrize('fname',['4grains2x4x3_compressionY.hdf5',
'6grains6x7x8_single_phase_tensionY.hdf5'])
def test_export_DADF5(self,ref_path,tmp_path,fname):
r = Result(ref_path/fname)
r = r.view(phases = random.sample(r.phases,1))
r = r.view(increments = random.sample(r.increments,np.random.randint(2,len(r.increments))))
r.export_DADF5(tmp_path/fname)
r_exp = Result(tmp_path/fname)
assert str(r.get()) == str(r_exp.get())
assert str(r.place()) == str(r_exp.place())
@pytest.mark.parametrize('fname',['4grains2x4x3_compressionY.hdf5',
'6grains6x7x8_single_phase_tensionY.hdf5'])
def test_export_DADF5_name_clash(self,ref_path,tmp_path,fname):
r = Result(ref_path/fname)
with pytest.raises(PermissionError):
r.export_DADF5(r.fname)