From 29f7f1b029866379ce881f44765f72534d757fae Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Fri, 24 May 2019 15:12:11 +0200 Subject: [PATCH] added vtk_export to srf and kriging classes; pos and meshtype now stored --- gstools/field/srf.py | 26 +++++++++++++++++++++++++- gstools/krige/ordinary.py | 23 ++++++++++++++++++++++- gstools/krige/simple.py | 23 ++++++++++++++++++++++- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/gstools/field/srf.py b/gstools/field/srf.py index f0e2014b..94148c6e 100644 --- a/gstools/field/srf.py +++ b/gstools/field/srf.py @@ -24,7 +24,8 @@ reshape_axis_from_struct_to_unstruct, reshape_field_from_unstruct_to_struct, ) -from gstools.tools.geometric import pos2xyz +from gstools.tools.geometric import pos2xyz, xyz2pos +from gstools.tools.export import vtk_export as vtk_ex from gstools.field.upscaling import var_coarse_graining, var_no_scaling from gstools.field.condition import ordinary, simple @@ -101,6 +102,8 @@ def __init__( self._cond_val = None self._krige_type = None # initialize attributes + self.pos = None + self.mesh_type = None self.field = None self.raw_field = None self.krige_field = None @@ -149,6 +152,8 @@ def __call__( """ # internal conversation x, y, z = pos2xyz(pos) + self.pos = xyz2pos(x, y, z) + self.mesh_type = mesh_type # update the model/seed in the generator if any changes were made self.generator.update(self.model, seed) # format the positional arguments of the mesh @@ -203,6 +208,24 @@ def __call__( return self.field + def vtk_export(self, filename, fieldname="field"): + """Export the stored field to vtk. + + Parameters + ---------- + filename : :class:`str` + Filename of the file to be saved, including the path. Note that an + ending (.vtr or .vtu) will be added to the name. + fieldname : :class:`str`, optional + Name of the field in the VTK file. Default: "field" + """ + if not ( + self.pos is None or self.field is None or self.mesh_type is None + ): + vtk_ex(filename, self.pos, self.field, fieldname, self.mesh_type) + else: + print("gstools.SRF.vtk_export: No field stored in the srf class.") + def set_condition( self, cond_pos=None, cond_val=None, krige_type="ordinary" ): @@ -252,6 +275,7 @@ def cond_func(self, *args, **kwargs): """Conditioning method applied to the field.""" if self.condition: return CONDITION[self._krige_type](*args, **kwargs) + return None def structured(self, *args, **kwargs): """Generate an SRF on a structured mesh. diff --git a/gstools/krige/ordinary.py b/gstools/krige/ordinary.py index 08e9bcc5..5ca8b90e 100644 --- a/gstools/krige/ordinary.py +++ b/gstools/krige/ordinary.py @@ -26,7 +26,8 @@ reshape_axis_from_struct_to_unstruct, reshape_field_from_unstruct_to_struct, ) -from gstools.tools.geometric import pos2xyz +from gstools.tools.geometric import pos2xyz, xyz2pos +from gstools.tools.export import vtk_export as vtk_ex from gstools.krige.krigesum import krigesum __all__ = ["Ordinary"] @@ -84,6 +85,8 @@ def __call__(self, pos, mesh_type="unstructured"): # internal conversation x, y, z = pos2xyz(pos, dtype=np.double) c_x, c_y, c_z = pos2xyz(self.cond_pos, dtype=np.double) + self.pos = xyz2pos(x, y, z) + self.mesh_type = mesh_type # format the positional arguments of the mesh check_mesh(self.model.dim, x, y, z, mesh_type) mesh_type_changed = False @@ -143,6 +146,24 @@ def _get_vario_mat(self, pos1, pos2, add=False): return np.vstack((res, np.ones((1, res.shape[1])))) return res + def vtk_export(self, filename, fieldname="field"): + """Export the stored field to vtk. + + Parameters + ---------- + filename : :class:`str` + Filename of the file to be saved, including the path. Note that an + ending (.vtr or .vtu) will be added to the name. + fieldname : :class:`str`, optional + Name of the field in the VTK file. Default: "field" + """ + if not ( + self.pos is None or self.field is None or self.mesh_type is None + ): + vtk_ex(filename, self.pos, self.field, fieldname, self.mesh_type) + else: + print("gstools.SRF.vtk_export: No field stored in the srf class.") + def set_condition(self, cond_pos, cond_val): """Set the conditions for kriging. diff --git a/gstools/krige/simple.py b/gstools/krige/simple.py index e1f1f7ea..64f88491 100644 --- a/gstools/krige/simple.py +++ b/gstools/krige/simple.py @@ -26,7 +26,8 @@ reshape_axis_from_struct_to_unstruct, reshape_field_from_unstruct_to_struct, ) -from gstools.tools.geometric import pos2xyz +from gstools.tools.geometric import pos2xyz, xyz2pos +from gstools.tools.export import vtk_export as vtk_ex from gstools.krige.krigesum import krigesum __all__ = ["Simple"] @@ -87,6 +88,8 @@ def __call__(self, pos, mesh_type="unstructured"): # internal conversation x, y, z = pos2xyz(pos, dtype=np.double) c_x, c_y, c_z = pos2xyz(self.cond_pos, dtype=np.double) + self.pos = xyz2pos(x, y, z) + self.mesh_type = mesh_type # format the positional arguments of the mesh check_mesh(self.model.dim, x, y, z, mesh_type) mesh_type_changed = False @@ -135,6 +138,24 @@ def _get_cov_mat(self, pos1, pos2): ) ) + def vtk_export(self, filename, fieldname="field"): + """Export the stored field to vtk. + + Parameters + ---------- + filename : :class:`str` + Filename of the file to be saved, including the path. Note that an + ending (.vtr or .vtu) will be added to the name. + fieldname : :class:`str`, optional + Name of the field in the VTK file. Default: "field" + """ + if not ( + self.pos is None or self.field is None or self.mesh_type is None + ): + vtk_ex(filename, self.pos, self.field, fieldname, self.mesh_type) + else: + print("gstools.SRF.vtk_export: No field stored in the srf class.") + def set_condition(self, cond_pos, cond_val): """Set the conditions for kriging.