From 928ef7bf42424e515854387cf741831eb47a3f2a Mon Sep 17 00:00:00 2001
From: leufen1 <l.leufen@fz-juelich.de>
Date: Fri, 3 Dec 2021 15:05:22 +0100
Subject: [PATCH] DataHandlerMixedSamplingWithClimateAndFirFilter can now use
 different options for window_history_size and window_history_offset (for chem
 and meteo)

---
 .../data_handler_mixed_sampling.py            | 21 +++++++++++++++++++
 .../data_handler/data_handler_with_filter.py  |  2 +-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/mlair/data_handler/data_handler_mixed_sampling.py b/mlair/data_handler/data_handler_mixed_sampling.py
index f84a23b5..eb3f78dc 100644
--- a/mlair/data_handler/data_handler_mixed_sampling.py
+++ b/mlair/data_handler/data_handler_mixed_sampling.py
@@ -346,30 +346,47 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi
         filter_add_unfiltered = kwargs.get("filter_add_unfiltered", False)
         sp_chem, sp_chem_unfiltered = None, None
         sp_meteo, sp_meteo_unfiltered = None, None
+
         if len(chem_vars) > 0:
             sp_keys = {k: copy.deepcopy(kwargs[k]) for k in cls.data_handler_climate_fir.requirements() if k in kwargs}
             sp_keys = cls.build_update_kwargs(sp_keys, dh_type="filtered_chem")
             sp_keys.update({"variables": chem_vars})
+            cls.adjust_window_opts("chem", "window_history_size", sp_keys)
+            cls.adjust_window_opts("chem", "window_history_offset", sp_keys)
             sp_chem = cls.data_handler_climate_fir(station, **sp_keys)
             if filter_add_unfiltered is True:
                 sp_keys = {k: copy.deepcopy(kwargs[k]) for k in cls.data_handler_unfiltered.requirements() if k in kwargs}
                 sp_keys = cls.build_update_kwargs(sp_keys, dh_type="unfiltered_chem")
                 sp_keys.update({"variables": chem_vars})
+                cls.adjust_window_opts("chem", "window_history_size", sp_keys)
+                cls.adjust_window_opts("chem", "window_history_offset", sp_keys)
                 sp_chem_unfiltered = cls.data_handler_unfiltered(station, **sp_keys)
         if len(meteo_vars) > 0:
             sp_keys = {k: copy.deepcopy(kwargs[k]) for k in cls.data_handler_fir.requirements() if k in kwargs}
             sp_keys = cls.build_update_kwargs(sp_keys, dh_type="filtered_meteo")
             sp_keys.update({"variables": meteo_vars})
+            cls.adjust_window_opts("meteo", "window_history_size", sp_keys)
+            cls.adjust_window_opts("meteo", "window_history_offset", sp_keys)
             sp_meteo = cls.data_handler_fir(station, **sp_keys)
             if filter_add_unfiltered is True:
                 sp_keys = {k: copy.deepcopy(kwargs[k]) for k in cls.data_handler_unfiltered.requirements() if k in kwargs}
                 sp_keys = cls.build_update_kwargs(sp_keys, dh_type="unfiltered_meteo")
                 sp_keys.update({"variables": meteo_vars})
+                cls.adjust_window_opts("meteo", "window_history_size", sp_keys)
+                cls.adjust_window_opts("meteo", "window_history_offset", sp_keys)
                 sp_meteo_unfiltered = cls.data_handler_unfiltered(station, **sp_keys)
 
         dp_args = {k: copy.deepcopy(kwargs[k]) for k in cls.own_args("id_class") if k in kwargs}
         return cls(sp_chem, sp_meteo, sp_chem_unfiltered, sp_meteo_unfiltered, chem_vars, meteo_vars, **dp_args)
 
+    @staticmethod
+    def adjust_window_opts(key: str, parameter_name: str, kwargs: dict):
+        if parameter_name in kwargs:
+            window_opt = kwargs.pop(parameter_name)
+            if isinstance(window_opt, dict):
+                window_opt = window_opt[key]
+            kwargs[parameter_name] = window_opt
+
     def _create_collection(self):
         collection = super()._create_collection()
         if self.id_class_other is not None:
@@ -389,6 +406,8 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi
         # chem transformation
         kwargs_chem = copy.deepcopy(kwargs)
         kwargs_chem["variables"] = chem_vars
+        cls.adjust_window_opts("chem", "window_history_size", kwargs_chem)
+        cls.adjust_window_opts("chem", "window_history_offset", kwargs_chem)
         dh_transformation = (cls.data_handler_climate_fir, cls.data_handler_unfiltered)
         transformation_chem = super().transformation(set_stations, tmp_path=tmp_path,
                                                      dh_transformation=dh_transformation, **kwargs_chem)
@@ -396,6 +415,8 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi
         # meteo transformation
         kwargs_meteo = copy.deepcopy(kwargs)
         kwargs_meteo["variables"] = meteo_vars
+        cls.adjust_window_opts("meteo", "window_history_size", kwargs_meteo)
+        cls.adjust_window_opts("meteo", "window_history_offset", kwargs_meteo)
         dh_transformation = (cls.data_handler_fir, cls.data_handler_unfiltered)
         transformation_meteo = super().transformation(set_stations, tmp_path=tmp_path,
                                                       dh_transformation=dh_transformation, **kwargs_meteo)
diff --git a/mlair/data_handler/data_handler_with_filter.py b/mlair/data_handler/data_handler_with_filter.py
index a522f53b..9b7b27f8 100644
--- a/mlair/data_handler/data_handler_with_filter.py
+++ b/mlair/data_handler/data_handler_with_filter.py
@@ -189,7 +189,7 @@ class DataHandlerFirFilterSingleStation(DataHandlerFilterSingleStation):
     def apply_filter(self):
         """Apply FIR filter only on inputs."""
         fir = FIRFilter(self.input_data.astype("float32"), self.fs, self.filter_order, self.filter_cutoff_freq,
-                        self.filter_window_type, self.target_dim, self.time_dim)
+                        self.filter_window_type, self.target_dim, self.time_dim, station_name=self.station)
         self.fir_coeff = fir.filter_coefficients
         filter_data = fir.filtered_data
         self.input_data = xr.concat(filter_data, pd.Index(self.create_filter_index(), name=self.filter_dim))
-- 
GitLab