Skip to content

Commit

Permalink
Make method cacheable and cache a couple
Browse files Browse the repository at this point in the history
  • Loading branch information
alexamici committed Jan 1, 2025
1 parent 73325c9 commit c4e33a1
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions sarsen/sentinel1.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ def dc_estimate(self) -> xr.Dataset | None:
)
return ds.compute()

# make class hashable to allow caching methods calls

def __hash__(self):
id = (
self.product_urlpath,
self.measurement_group,
self.measurement_chunks,
) + tuple(self.kwargs.items())
return hash(id)

# SarProduct interaface

@functools.cached_property
Expand All @@ -163,17 +173,13 @@ def product_type(self) -> Any:
assert isinstance(prod_type, str)
return prod_type

def beta_nought(self, load=False) -> xr.DataArray:
if "_cached_beta_nought" not in vars(self):
measurement = self.measurement.data_vars["measurement"]
beta_nought = xarray_sentinel.calibrate_intensity(
measurement, self.calibration.betaNought
)
beta_nought = beta_nought.drop_vars(["pixel", "line"])
if load:
beta_nought = beta_nought.load()
self._cached_beta_nought = beta_nought
return self._cached_beta_nought
@functools.cache
def beta_nought(self) -> xr.DataArray:
measurement = self.measurement.data_vars["measurement"]
beta_nought = xarray_sentinel.calibrate_intensity(
measurement, self.calibration.betaNought
)
return beta_nought.drop_vars(["pixel", "line"])

def state_vectors(self) -> xr.DataArray:
return self.orbit.data_vars["position"]
Expand Down Expand Up @@ -211,6 +217,7 @@ def interp_sar(self, *args: Any, **kwargs: Any) -> xr.DataArray:
else:
return sarsen.SlantRangeSarProduct.interp_sar(self, *args, **kwargs)

@functools.cache
def product_info(self, **kwargs: Any) -> dict[str, Any]:
"""Get information about the Sentinel-1 product."""
measurement_groups = self.all_measurement_groups()
Expand Down

0 comments on commit c4e33a1

Please sign in to comment.