From 7bcb4a5c2b21d8bba66ab2ad55559983a6e1ac9a Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Mon, 27 May 2024 11:28:28 +0200 Subject: [PATCH 1/2] Add optional mesh arguments to fields Add a key guess for .cff and .h5 files Signed-off-by: paul.profizi --- src/ansys/dpf/core/data_sources.py | 13 +++++++++++++ tests/test_datasources.py | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/ansys/dpf/core/data_sources.py b/src/ansys/dpf/core/data_sources.py index 7b8eeec0de8..912ef751582 100644 --- a/src/ansys/dpf/core/data_sources.py +++ b/src/ansys/dpf/core/data_sources.py @@ -120,6 +120,9 @@ def set_result_file_path(self, filepath, key=""): # Handle no key given and no file extension if key == "" and os.path.splitext(filepath)[1] == "": key = self.guess_result_key(str(filepath)) + # Look for another extension for .h5 and .cff files + if key == "" and os.path.splitext(filepath)[1] in [".h5", ".cff"]: + key = self.guess_second_key(str(filepath)) if key == "": self._api.data_sources_set_result_file_path_utf8(self, str(filepath)) else: @@ -136,6 +139,16 @@ def guess_result_key(filepath: str) -> str: return result_key return "" + @staticmethod + def guess_second_key(filepath: str) -> str: + """For files with an h5 or cff extension, look for another extension.""" + without_ext = os.path.splitext(filepath)[0] + new_split = os.path.splitext(without_ext) + new_key = "" + if len(new_split) > 1: + new_key = new_split[1][1:] + return new_key + def set_domain_result_file_path( self, path: Union[str, os.PathLike], domain_id: int, key: Union[str, None] = None ): diff --git a/tests/test_datasources.py b/tests/test_datasources.py index 383601a5776..726735a7532 100644 --- a/tests/test_datasources.py +++ b/tests/test_datasources.py @@ -57,6 +57,28 @@ def test_setresultpath_data_sources_no_extension(d3plot_beam, binout_glstat, ser assert data_sources.result_key == "binout" +def test_set_resultpath_data_sources_h5(server_type): + from ansys.dpf.core import examples + cas_h5_file = examples.download_fluent_axial_comp(server=server_type)["cas"][0] + print(cas_h5_file) + data_sources = dpf.core.DataSources(server=server_type) + data_sources.set_result_file_path(cas_h5_file) + assert data_sources.result_key == "cas" + data_sources = dpf.core.DataSources(result_path=cas_h5_file, server=server_type) + assert data_sources.result_key == "cas" + + +def test_set_resultpath_data_sources_cff(server_type): + from ansys.dpf.core import examples + cas_h5_file = examples.download_cfx_heating_coil(server=server_type)["cas"] + print(cas_h5_file) + data_sources = dpf.core.DataSources(server=server_type) + data_sources.set_result_file_path(cas_h5_file) + assert data_sources.result_key == "cas" + data_sources = dpf.core.DataSources(result_path=cas_h5_file, server=server_type) + assert data_sources.result_key == "cas" + + def test_addupstream_data_sources(allkindofcomplexity, server_type): data_sources = dpf.core.DataSources(server=server_type) data_sources2 = dpf.core.DataSources(server=server_type) From 2c3b8eb3cb8ea8160702108dbcd243427004388c Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Mon, 27 May 2024 14:31:09 +0200 Subject: [PATCH 2/2] Restrict to officially supported .cas.h5, .dat.h5, .cas.cff, and .dat.cff Signed-off-by: paul.profizi --- src/ansys/dpf/core/data_sources.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ansys/dpf/core/data_sources.py b/src/ansys/dpf/core/data_sources.py index 912ef751582..5468c7a3dee 100644 --- a/src/ansys/dpf/core/data_sources.py +++ b/src/ansys/dpf/core/data_sources.py @@ -142,11 +142,14 @@ def guess_result_key(filepath: str) -> str: @staticmethod def guess_second_key(filepath: str) -> str: """For files with an h5 or cff extension, look for another extension.""" + accepted = ["cas", "dat"] without_ext = os.path.splitext(filepath)[0] new_split = os.path.splitext(without_ext) new_key = "" if len(new_split) > 1: - new_key = new_split[1][1:] + key = new_split[1][1:] + if key in accepted: + new_key = key return new_key def set_domain_result_file_path(