Skip to content

Commit

Permalink
feat(misc): modularize kwargs filter
Browse files Browse the repository at this point in the history
  • Loading branch information
j-haacker committed Mar 1, 2024
1 parent 9f3036c commit 62a27c4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
9 changes: 2 additions & 7 deletions cryoswath/l2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import geopandas as gpd
import inspect
from multiprocessing import Pool
import numpy as np
import os
Expand Down Expand Up @@ -200,12 +199,8 @@ def process_track(idx, reprocess, l2_paths, save_or_return, data_path, current_s
cs_full_file_names = kwargs.pop("cs_full_file_names")
else:
cs_full_file_names = load_cs_full_file_names(update="no")
# filter l1b_data kwargs
params = inspect.signature(l1b.l1b_data).parameters
l1b_kwargs = {k: v for k, v in kwargs.items() if k in params}
# filter to_l2 kwargs
params = inspect.signature(l1b.l1b_data.to_l2).parameters
to_l2_kwargs = {k: v for k, v in kwargs.items() if k in params and k != "swath_or_poca"}
l1b_kwargs = filter_kwargs(l1b.l1b_data, kwargs)
to_l2_kwargs = filter_kwargs(l1b.l1b_data.to_l2, kwargs, blacklist=["swath_or_poca"])
swath_poca_tuple = l1b.l1b_data.from_id(cs_time_to_id(idx), **l1b_kwargs)\
.to_l2(swath_or_poca="both", **to_l2_kwargs)
if save_or_return != "return":
Expand Down
6 changes: 4 additions & 2 deletions cryoswath/l3.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def build_dataset(region_of_interest: str|shapely.Polygon,
# I believe passing loading l2 data to the function prevents copying
# on .drop. an alternative would be to define l2_data nonlocal
# within the gridding function
l3_data = med_mad_cnt_grid(l2.from_id(cs_tracks.index), start_datetime=start_datetime, end_datetime=end_datetime,
aggregation_period=aggregation_period, timestep=timestep, spatial_res_meter=spatial_res_meter)
l3_data = med_mad_cnt_grid(l2.from_id(cs_tracks.index, **filter_kwargs(l2.from_id, kwargs)),
start_datetime=start_datetime, end_datetime=end_datetime,
aggregation_period=aggregation_period, timestep=timestep,
spatial_res_meter=spatial_res_meter)
l3_data.to_netcdf(build_path(region_of_interest, timestep, spatial_res_meter, aggregation_period))
return l3_data
__all__.append("build_dataset")
Expand Down
18 changes: 18 additions & 0 deletions cryoswath/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ftplib
import geopandas as gpd
import glob
import inspect
import numpy as np
import os
import pandas as pd
Expand Down Expand Up @@ -135,6 +136,23 @@ def convert_all_esri_to_feather(dir_path: str = None) -> None:
# return local_filename


# ! make recursive
def filter_kwargs(func: callable,
kwargs: dict, *,
blacklist: list[str] = None,
whitelist: list[str] = None,
) -> dict:
def ensure_list(tmp_list):
if tmp_list is None: return []
elif isinstance(tmp_list, str): return [tmp_list]
else: return tmp_list
blacklist = ensure_list(blacklist)
whitelist = ensure_list(whitelist)
params = inspect.signature(func).parameters
return {k: v for k, v in kwargs.items() if (k in params and k not in blacklist) or k in whitelist}
__all__.append("filter_kwargs")


def find_region_id(location: any, scope: str = "o2") -> str:
if isinstance(location, gpd.GeoDataFrame):
location = location.geometry
Expand Down

0 comments on commit 62a27c4

Please sign in to comment.