diff --git a/mlair/data_handler/data_handler_wrf_chem.py b/mlair/data_handler/data_handler_wrf_chem.py index 158a7fb2bb8f5d3674b9b312f47a389dc0c5e90f..af4660c13bf6cc98ef82b92baf184f8585b606f4 100644 --- a/mlair/data_handler/data_handler_wrf_chem.py +++ b/mlair/data_handler/data_handler_wrf_chem.py @@ -748,29 +748,21 @@ class DataHandlerSingleGridColumn(DataHandlerSingleStation): raise NotImplementedError( f"Method `apply_toarstats` is not implemented for var_logical_z_coord_selector != 0: " f"Is {self.var_logical_z_coord_selector}") - data = self.apply_toarstats(data, meta, self.input_output_sampling4toarstats[1]) + data = self.prepare_and_apply_toarstats(data, meta, self.input_output_sampling4toarstats[1]) data = self._slice_prep(data, start=start, end=end) return data, meta @TimeTrackingWrapper - def apply_toarstats(self, data, meta, target_sampling="daily"): - tz_where = tzwhere.tzwhere() - local_time_zone = tz_where.tzNameAt(latitude=meta[self.loader.physical_y_coord_name], - longitude=meta[self.loader.physical_x_coord_name]) - # local_time_zone = self.tzwhere.tzNameAt(latitude=meta[self.loader.physical_y_coord_name], - # longitude=meta[self.loader.physical_x_coord_name]) - hdata = data.squeeze().to_pandas() - hdata.index = self.set_time_zone(hdata.index) - hdata.index = hdata.index.tz_convert(local_time_zone) + def prepare_and_apply_toarstats(self, data, meta, target_sampling="daily"): + local_time_zone = self.get_local_time_zone_from_lat_lon(meta=meta) + hdata = self.set_external_time_zone_and_convert_to_local_time_zone(data, local_time_zone) hsampling_data = [] for i, var in enumerate(hdata.columns): hdf = toarstats(target_sampling, self.statistics_per_var[var], hdata[var], (meta[self.loader.physical_y_coord_name], meta[self.loader.physical_x_coord_name])) - # if i == 0: - hsampling_data.append(xr.DataArray(hdf, coords=[hdf.index, [var]], dims=[self.loader.physical_t_coord_name, self.target_dim])) - # else: - # df[var] = hdf + hsampling_data.append(xr.DataArray(hdf, coords=[hdf.index, [var]], + dims=[self.loader.physical_t_coord_name, self.target_dim])) sampling_data = xr.concat(hsampling_data, dim=self.target_dim) sampling_data = sampling_data.broadcast_like(data, exclude=self.loader.physical_t_coord_name).dropna( self.loader.physical_t_coord_name) @@ -781,6 +773,48 @@ class DataHandlerSingleGridColumn(DataHandlerSingleStation): return self._force_dask_computation(sampling_data) + def set_external_time_zone_and_convert_to_local_time_zone(self, data, local_time_zone): + """ + + :param data: + :type data: + :param local_time_zone: + :type local_time_zone: + :return: + :rtype: + """ + hdata = data.squeeze().to_pandas() + hdata.index = self.set_time_zone(hdata.index) + hdata.index = hdata.index.tz_convert(local_time_zone) + logging.debug(f"Set local time zone for {self.station} to: {local_time_zone}") + return hdata + + def get_local_time_zone_from_lat_lon(self, lat=None, lon=None, meta=None): + """ + Retuns name of time zone for given lat lon coordinates. + + Method also accepts a meta data pd.DataFrame where lat and lon are extracted with the loader's physical x and y + coord names + + :param lat: + :type lat: + :param lon: + :type lon: + :param meta: + :type meta: pd.DataFrame + :return: + :rtype: str + """ + if (lat is None and lon is None) and (meta is not None): + lat = meta[self.loader.physical_y_coord_name] + lon = meta[self.loader.physical_x_coord_name] + + + tz_where = tzwhere.tzwhere() + local_time_zone = tz_where.tzNameAt(latitude=lat, longitude=lon) + logging.debug(f"Detect local time zone '{local_time_zone}' based on lat={lat}, lon={lon}") + return local_time_zone + def set_time_zone(self, time_index): """ Sets time zone information on a given index @@ -793,7 +827,7 @@ class DataHandlerSingleGridColumn(DataHandlerSingleStation): dti = pd.to_datetime(time_index) dti = dti.tz_localize(self.time_zone) - logging.info(f"Set external time zone for {self.station} to: {self.time_zone}") + logging.debug(f"Set external time zone for {self.station} to: {self.time_zone}") return dti @staticmethod