Skip to content
Snippets Groups Projects
Commit d28ad0a0 authored by leufen1's avatar leufen1
Browse files

new try with convolve

parent 0682b903
No related branches found
No related tags found
5 merge requests!319add all changes of dev into release v1.4.0 branch,!318Resolve "release v1.4.0",!317enabled window_lead_time=1,!295Resolve "data handler FIR filter",!259Draft: Resolve "WRF-Datahandler should inherit from SingleStationDatahandler"
Pipeline #67295 passed
This commit is part of merge request !318. Comments created here will be created in the context of that merge request.
...@@ -3,6 +3,7 @@ import warnings ...@@ -3,6 +3,7 @@ import warnings
from typing import Union, Callable from typing import Union, Callable
import logging import logging
import os import os
import time
import datetime import datetime
import numpy as np import numpy as np
...@@ -266,12 +267,19 @@ class ClimateFIRFilter: ...@@ -266,12 +267,19 @@ class ClimateFIRFilter:
# apply vectorized fir filter along the tmp dimension # apply vectorized fir filter along the tmp dimension
kwargs = {"fs": fs, "cutoff_high": cutoff_high, "order": order, kwargs = {"fs": fs, "cutoff_high": cutoff_high, "order": order,
"causal": False, "padlen": int(min(padlen_factor, 1) * length), "h": h} "causal": False, "padlen": int(min(padlen_factor, 1) * length), "h": h}
with TimeTracking(name="numpy_vec"): # with TimeTracking(name="numpy_vec"):
filt = fir_filter_numpy_vectorized(filter_input_data, var_dim, new_dim, kwargs) # filt = fir_filter_numpy_vectorized(filter_input_data, var_dim, new_dim, kwargs)
with TimeTracking(name="xr_apply_ufunc"): # with TimeTracking(name="xr_apply_ufunc"):
filt = xr.apply_ufunc(fir_filter_vectorized, filter_input_data, time_axis, # filt = xr.apply_ufunc(fir_filter_vectorized, filter_input_data, time_axis,
input_core_dims=[[new_dim], []], output_core_dims=[[new_dim]], vectorize=True, # input_core_dims=[[new_dim], []], output_core_dims=[[new_dim]], vectorize=True,
kwargs=kwargs) # kwargs=kwargs)
with TimeTracking(name="convolve"):
slicer = slice(int(-(length - 1) / 2), int((length - 1) / 2))
filt = xr.apply_ufunc(fir_filter_convolve_vectorized, filter_input_data.sel(window=slicer),
input_core_dims=[["window"]],
output_core_dims=[["window"]],
vectorize=True,
kwargs={"h": h})
# plot # plot
if self.plot_path is not None: if self.plot_path is not None:
...@@ -404,6 +412,10 @@ def fir_filter_numpy_vectorized(filter_input_data, var_dim, new_dim, kwargs): ...@@ -404,6 +412,10 @@ def fir_filter_numpy_vectorized(filter_input_data, var_dim, new_dim, kwargs):
return filt_np return filt_np
def fir_filter_convolve_vectorized(data, h):
return signal.convolve(data, h, mode='same', method="direct") / sum(h)
def fir_filter_vectorized(data, time_stamp=None, fs=1, order=5, cutoff_low=None, cutoff_high=None, window="hamming", def fir_filter_vectorized(data, time_stamp=None, fs=1, order=5, cutoff_low=None, cutoff_high=None, window="hamming",
h=None, h=None,
causal=True, causal=True,
...@@ -435,10 +447,10 @@ def fir_filter_vectorized(data, time_stamp=None, fs=1, order=5, cutoff_low=None, ...@@ -435,10 +447,10 @@ def fir_filter_vectorized(data, time_stamp=None, fs=1, order=5, cutoff_low=None,
y = signal.lfilter(h, 1., data) y = signal.lfilter(h, 1., data)
else: else:
padlen = padlen if padlen is not None else 3 * len(h) padlen = padlen if padlen is not None else 3 * len(h)
# print(sum(sel))
# if sum(sel) <= padlen: # if sum(sel) <= padlen:
# y = np.empty_like(data[sel]) # y = np.empty_like(data[sel])
# else: # else:
# with TimeTracking():
# y = signal.filtfilt(h, 1., data[sel], padlen=padlen) # y = signal.filtfilt(h, 1., data[sel], padlen=padlen)
y = signal.filtfilt(h, 1., data, padlen=padlen) y = signal.filtfilt(h, 1., data, padlen=padlen)
# res[sel] = y # res[sel] = y
...@@ -446,20 +458,10 @@ def fir_filter_vectorized(data, time_stamp=None, fs=1, order=5, cutoff_low=None, ...@@ -446,20 +458,10 @@ def fir_filter_vectorized(data, time_stamp=None, fs=1, order=5, cutoff_low=None,
return y return y
def fir_filter_vectorized_short(data, time_stamp=None, fs=1, order=5, cutoff_low=None, cutoff_high=None, def fir_filter_vectorized_short(data, h=None, padlen=None):
window="hamming",
h=None,
causal=True,
padlen=None):
"""Expects numpy array.""" """Expects numpy array."""
sel = ~np.isnan(data) y = signal.filtfilt(h, 1., data, padlen=padlen)
res = np.empty_like(data) return y
if sum(sel) <= padlen:
y = np.empty_like(data[sel])
else:
y = signal.filtfilt(h, 1., data[sel], padlen=padlen)
res[sel] = y
return res
class KolmogorovZurbenkoBaseClass: class KolmogorovZurbenkoBaseClass:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment