Skip to content

Commit

Permalink
Fix default / serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed Jan 6, 2025
1 parent 0443ac0 commit de3a67d
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/pyaro/timeseries/Filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,7 @@ class ValleyFloorRelativeAltitudeFilter(StationFilter):

def __init__(
self,
topo: str
| pathlib.Path = "/lustre/storeB/project/aerocom/aerocom1/AEROCOM_OBSDATA/GTOPO30/merged",
topo: str | None = None,
*,
radius: float = 5000,
topo_var: str = "Band1",
Expand All @@ -1244,19 +1243,19 @@ def __init__(
"valleyfloor_relaltitude filter is missing required dependency 'xarray'. Please install to use this filter."
)

if isinstance(topo, str):
self._topo = pathlib.Path(topo)
elif isinstance(topo, pathlib.Path):
self._topo = topo
else:
raise TypeError(
f"Topo needs to be an instance of str or pathlib.Path. Got {type(topo)}."
)

if not self._topo.exists():
logger.warning(
f"Provided location for topography data ({self._topo}) does not exist. It should be either a .nc file, or a folder with several .nc files and a metadata.json file."
)
self._topo = None
if topo is not None:
try:
self._topo = pathlib.Path(topo)
except TypeError as e:
raise TypeError(
f"Topo needs to be an instance of str. Got {type(topo)}."
) from e

if not self._topo.exists():
logger.warning(
f"Provided location for topography data ({self._topo}) does not exist. It should be either a .nc file, or a folder with several .nc files and a metadata.json file."
)

self._topo_file = None
self._topo_var = topo_var
Expand All @@ -1266,7 +1265,8 @@ def __init__(

def init_kwargs(self):
return {
"topo": self._topo_file,
# Converting to string for serialization purposes.
"topo": str(self._topo_file),
"topo_var": self._topo_var,
"radius": self._radius,
"lower": self._lower,
Expand Down Expand Up @@ -1318,6 +1318,10 @@ def _update_topo_file_path(self, lat: float, lon: float) -> bool:
return self._topo_file != old_path

def filter_stations(self, stations: dict[str, Station]) -> dict[str, Station]:
if self._topo is None or (self._upper is None and self._lower is None):
# Default initialized filter should not do anything, so return unfiltered stations.
return stations

if "cf_units" not in sys.modules:
raise ModuleNotFoundError(
"valleyfloor_relaltitude filter is missing required dependency 'cf-units'. Please install to use this filter."
Expand Down

0 comments on commit de3a67d

Please sign in to comment.