diff --git a/mlair/data_handler/data_handler_mixed_sampling.py b/mlair/data_handler/data_handler_mixed_sampling.py index 0bdd9b216073bd6d045233afb3fd945718117a98..84596ad081b922a92a91b3df0513a4e730b8eb53 100644 --- a/mlair/data_handler/data_handler_mixed_sampling.py +++ b/mlair/data_handler/data_handler_mixed_sampling.py @@ -316,10 +316,17 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi @classmethod def _split_chem_and_meteo_variables(cls, **kwargs): + """ + Select all used variables and split them into categories chem and other. + + Chemical variables are indicated by `cls.data_handler_climate_fir.chem_vars`. To indicate used variables, this + method uses 1) parameter `variables`, 2) keys from `statistics_per_var`, 3) keys from + `cls.data_handler_climate_fir.DEFAULT_VAR_ALL_DICT`. Option 3) is also applied if 1) or 2) are given but None. + """ if "variables" in kwargs: variables = kwargs.get("variables") elif "statistics_per_var" in kwargs: - variables = kwargs.get("statistics_per_var") + variables = kwargs.get("statistics_per_var").keys() else: variables = None if variables is None: @@ -348,14 +355,7 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi cls.prepare_build(sp_keys, chem_vars, cls.chem_indicator) sp_chem_unfiltered = cls.data_handler_unfiltered(station, **sp_keys) if len(meteo_vars) > 0: - if cls.data_handler_fir_pos is None: - if "extend_length_opts" in kwargs: - if isinstance(kwargs["extend_length_opts"], dict) and cls.meteo_indicator not in kwargs["extend_length_opts"].keys(): - cls.data_handler_fir_pos = 0 # use faster fir version without climate estimate - else: - cls.data_handler_fir_pos = 1 # use slower fir version with climate estimate - else: - cls.data_handler_fir_pos = 0 # use faster fir version without climate estimate + cls.set_data_handler_fir_pos(**kwargs) sp_keys = {k: copy.deepcopy(kwargs[k]) for k in cls.data_handler_fir[cls.data_handler_fir_pos].requirements() if k in kwargs} sp_keys = cls.build_update_transformation(sp_keys, dh_type="filtered_meteo") cls.prepare_build(sp_keys, meteo_vars, cls.meteo_indicator) @@ -369,8 +369,36 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi 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) + @classmethod + def set_data_handler_fir_pos(cls, **kwargs): + """ + Set position of fir data handler to use either faster FIR version or slower climate FIR. + + This method will set data handler indicator to 0 if either no parameter "extend_length_opts" is given or the + parameter is of type dict but has no entry for the meteo_indicator. In all other cases, indicator is set to 1. + """ + p_name = "extend_length_opts" + if cls.data_handler_fir_pos is None: + if p_name in kwargs: + if isinstance(kwargs[p_name], dict) and cls.meteo_indicator not in kwargs[p_name].keys(): + cls.data_handler_fir_pos = 0 # use faster fir version without climate estimate + else: + cls.data_handler_fir_pos = 1 # use slower fir version with climate estimate + else: + cls.data_handler_fir_pos = 0 # use faster fir version without climate estimate + @classmethod def prepare_build(cls, kwargs, var_list, var_type): + """ + Prepares for build of class. + + `variables` parameter is updated by `var_list`, which should only include variables of a specific type (e.g. + only chemical variables) indicated by `var_type`. Furthermore, this method cleans the `kwargs` dictionary as + follows: For all parameters provided as dict to separate between chem and meteo options (dict must have keys + from `cls.chem_indicator` and/or `cls.meteo_indicator`), this parameter is removed from kwargs and its value + related to `var_type` added again. In case there is no value for given `var_type`, the parameter is not added + at all (as this parameter is assumed to affect only other types of variables). + """ kwargs.update({"variables": var_list}) for k in list(kwargs.keys()): v = kwargs[k] @@ -382,17 +410,6 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi except KeyError: pass - @staticmethod - def adjust_window_opts(key: str, parameter_name: str, kwargs: dict): - try: - 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 - except KeyError: - pass - def _create_collection(self): collection = super()._create_collection() if self.id_class_other is not None: @@ -419,9 +436,10 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi # meteo transformation if len(meteo_vars) > 0: + cls.set_data_handler_fir_pos(**kwargs) kwargs_meteo = copy.deepcopy(kwargs) cls.prepare_build(kwargs_meteo, meteo_vars, cls.meteo_indicator) - dh_transformation = (cls.data_handler_fir[cls.data_handler_fir_pos or 0], cls.data_handler_unfiltered) + dh_transformation = (cls.data_handler_fir[cls.data_handler_fir_pos], cls.data_handler_unfiltered) transformation_meteo = super().transformation(set_stations, tmp_path=tmp_path, dh_transformation=dh_transformation, **kwargs_meteo)