diff --git a/src/fmu/sumo/explorer/objects/_search_context.py b/src/fmu/sumo/explorer/objects/_search_context.py index c46326f4..5afe0afd 100644 --- a/src/fmu/sumo/explorer/objects/_search_context.py +++ b/src/fmu/sumo/explorer/objects/_search_context.py @@ -328,8 +328,8 @@ def _to_sumo(self, obj, blob=None): "cpgrid_property": objects.CPGridProperty }.get(cls) if constructor is None: - warnings.warn(f"No constructor for class {cls}") - constructor = objects.Child + warnings.warn(f"No constructor for class {cls}") + constructor = objects.Child return constructor(self._sumo, obj, blob) def __len__(self): @@ -559,7 +559,7 @@ def _maybe_prefetch(self, index): uuid = self._hits[index] if self._cache.has(uuid): return - uuids = self._hits[index : min(index + 100, len(self._hits))] + uuids = self._hits[index:min(index + 100, len(self._hits))] uuids = [uuid for uuid in uuids if not self._cache.has(uuid)] hits = self.__search_all( {"ids": {"values": uuids}}, @@ -577,7 +577,7 @@ async def _maybe_prefetch_async(self, index): uuid = self._hits[index] if self._cache.has(uuid): return - uuids = self._hits[index : min(index + 100, len(self._hits))] + uuids = self._hits[index:min(index + 100, len(self._hits))] uuids = [uuid for uuid in uuids if not self._cache.has(uuid)] hits = await self.__search_all_async( {"ids": {"values": uuids}}, @@ -811,17 +811,38 @@ def all(self): @property def cases(self): """Cases from current selection.""" - return objects.Cases(self) + uuids = self._get_field_values("fmu.case.uuid.keyword") + return objects.Cases(self, uuids) + + @property + async def cases_async(self): + """Cases from current selection.""" + uuids = await self._get_field_values_async("fmu.case.uuid.keyword") + return objects.Cases(self, uuids) @property def iterations(self): """Iterations from current selection.""" - return objects.Iterations(self) + uuids = self._get_field_values("fmu.iteration.uuid.keyword") + return objects.Iterations(self, uuids) + + @property + async def iterations_async(self): + """Iterations from current selection.""" + uuids = await self._get_field_values_async("fmu.iteration.uuid.keyword") + return objects.Iterations(self, uuids) @property def realizations(self): """Realizations from current selection.""" - return objects.Realizations(self) + uuids = self._get_field_values("fmu.realization.uuid.keyword") + return objects.Realizations(self, uuids) + + @property + async def realizations_async(self): + """Realizations from current selection.""" + uuids = await self._get_field_values_async("fmu.realization.uuid.keyword") + return objects.Realizations(self, uuids) @property def template_paths(sc): diff --git a/src/fmu/sumo/explorer/objects/cases.py b/src/fmu/sumo/explorer/objects/cases.py index e607aec5..a7d20c9e 100644 --- a/src/fmu/sumo/explorer/objects/cases.py +++ b/src/fmu/sumo/explorer/objects/cases.py @@ -3,42 +3,11 @@ from fmu.sumo.explorer.objects._search_context import SearchContext class Cases(SearchContext): - def __init__(self, sc): - super().__init__(sc._sumo, sc._must, sc._must_not) + def __init__(self, sc, uuids): + super().__init__(sc._sumo, must=[{"terms": {"fmu.case.uuid.keyword": uuids}}]) + self._hits = uuids return - def __len__(self): - if self._length is None: - if self._hits is None: - self._hits = self._search_all(select=False) - pass - self._length = len(self._hits) - pass - return self._length - - async def length_async(self): - if self._length is None: - if self._hits is None: - self._hits = self._search_all(select=False) - pass - self._length = len(self._hits) - pass - return self._length - - def _search_all(self, select=False): - uuids = self._get_field_values("fmu.case.uuid.keyword") - if select is False: - return uuids - # ELSE - return SearchContext(must=[{"ids": {"values": uuids}}])._search_all(select=select) - - async def _search_all_async(self, select=False): - uuids = await self._get_field_values_async("fmu.case.uuid.keyword") - if select is False: - return uuids - # ELSE - return await SearchContext(must=[{"ids": {"values": uuids}}])._search_all_async(select=select) - def _maybe_prefetch(self, index): return diff --git a/src/fmu/sumo/explorer/objects/cpgrid.py b/src/fmu/sumo/explorer/objects/cpgrid.py index f0c0363a..fceb291f 100644 --- a/src/fmu/sumo/explorer/objects/cpgrid.py +++ b/src/fmu/sumo/explorer/objects/cpgrid.py @@ -27,7 +27,7 @@ def to_cpgrid(self): raise RuntimeError("Unable to import xtgeo; probably not installed.") try: return grid_from_file(self.blob) - except TypeError as err: + except TypeError as type_err: raise TypeError(f"Unknown format: {self.format}") from type_err async def to_cpgrid_async(self): @@ -42,5 +42,5 @@ async def to_cpgrid_async(self): try: return grid_from_file(await self.blob_async) - except TypeError as err: + except TypeError as type_err: raise TypeError(f"Unknown format: {self.format}") from type_err diff --git a/src/fmu/sumo/explorer/objects/cpgrid_property.py b/src/fmu/sumo/explorer/objects/cpgrid_property.py index e8103799..b146aa94 100644 --- a/src/fmu/sumo/explorer/objects/cpgrid_property.py +++ b/src/fmu/sumo/explorer/objects/cpgrid_property.py @@ -27,7 +27,7 @@ def to_cpgrid_property(self): raise RuntimeError("Unable to import xtgeo; probably not installed.") try: return gridproperty_from_file(self.blob) - except TypeError as err: + except TypeError as type_err: raise TypeError(f"Unknown format: {self.format}") from type_err async def to_cpgrid_property_async(self): @@ -42,5 +42,5 @@ async def to_cpgrid_property_async(self): try: return gridproperty_from_file(await self.blob_async) - except TypeError as err: + except TypeError as type_err: raise TypeError(f"Unknown format: {self.format}") from type_err diff --git a/src/fmu/sumo/explorer/objects/iterations.py b/src/fmu/sumo/explorer/objects/iterations.py index fecb0d9f..dd942d94 100644 --- a/src/fmu/sumo/explorer/objects/iterations.py +++ b/src/fmu/sumo/explorer/objects/iterations.py @@ -4,34 +4,11 @@ from fmu.sumo.explorer.objects._search_context import SearchContext class Iterations(SearchContext): - def __init__(self, _search_context): - super().__init__(_search_context._sumo, _search_context._must, _search_context._must_not) + def __init__(self, sc, uuids): + super().__init__(sc._sumo, must=[{"terms": {"fmu.iteration.uuid.keyword": uuids}}]) + self._hits = uuids return - def __len__(self): - if self._length is None: - if self._hits is None: - self._hits = self._search_all(select=False) - pass - self._length = len(self._hits) - pass - return self._length - - async def length_async(self): - if self._length is None: - if self._hits is None: - self._hits = self._search_all(select=False) - pass - self._length = len(self._hits) - pass - return self._length - - def _search_all(self, select=False): - return self._get_field_values("fmu.iteration.uuid.keyword") - - async def _search_all_async(self, select=False): - return await self._get_field_values_async("fmu.iteration.uuid.keyword") - def _maybe_prefetch(self, index): return diff --git a/src/fmu/sumo/explorer/objects/realizations.py b/src/fmu/sumo/explorer/objects/realizations.py index b328cddb..eea2ad81 100644 --- a/src/fmu/sumo/explorer/objects/realizations.py +++ b/src/fmu/sumo/explorer/objects/realizations.py @@ -4,33 +4,11 @@ from fmu.sumo.explorer.objects._search_context import SearchContext class Realizations(SearchContext): - def __init__(self, sc): - super().__init__(sc._sumo, sc._must, sc._must_not) + def __init__(self, sc, uuids): + super().__init__(sc._sumo, must=[{"terms": {"fmu.realization.uuid.keyword": uuids}}]) + self._hits = uuids return - def __len__(self): - if self._length is None: - if self._hits is None: - self._hits = self._search_all(select=False) - pass - self._length = len(self._hits) - pass - return self._length - - async def length_async(self): - if self._length is None: - if self._hits is None: - self._hits = self._search_all(select=False) - pass - self._length = len(self._hits) - pass - return self._length - - def _search_all(self, select=False): - return self._get_field_values("fmu.realization.uuid.keyword") - - async def _search_all_async(self, select=False): - return await self._get_field_values_async("fmu.realization.uuid.keyword") def _maybe_prefetch(self, index): return diff --git a/tests/test_explorer.py b/tests/test_explorer.py index 9b9fda67..cd921df8 100644 --- a/tests/test_explorer.py +++ b/tests/test_explorer.py @@ -327,4 +327,23 @@ def test_seismic_case_by_uuid(explorer: Explorer, seismic_case_uuid: str): assert "Trace" in channel_list assert "SEGYTraceHeader" in channel_list +def test_grids_and_properties(explorer: Explorer): + cases_with_grids = explorer.grids.cases.filter(status="keep").cases + cases_with_gridprops = explorer.grid_properties.cases.filter(status="keep").cases + cgs=set([case.uuid for case in cases_with_grids]) + cgps=set([case.uuid for case in cases_with_gridprops]) + assert cgs==cgps + case=cases_with_grids[0] + grids=case.grids + gridprops=case.grid_properties + xtgrid=grids[0].to_cpgrid() + gridspec=grids[0].metadata["data"]["spec"] + assert xtgrid.nlay == gridspec["nlay"] + assert xtgrid.nrow == gridspec["nrow"] + assert xtgrid.ncol == gridspec["ncol"] + xtgridprop=gridprops[0].to_cpgrid_property() + gridpropspec = gridprops[0].metadata["data"]["spec"] + assert xtgridprop.nlay == gridpropspec["nlay"] + assert xtgridprop.nrow == gridpropspec["nrow"] + assert xtgridprop.ncol == gridpropspec["ncol"]