From b89b849981356c36da12483ba32f55ccc590c438 Mon Sep 17 00:00:00 2001 From: Jora Singh Randhawa Date: Fri, 6 Jul 2018 12:41:30 +0200 Subject: [PATCH] New function detect_drift, not finished #303 --- wwdata/Class_HydroData.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/wwdata/Class_HydroData.py b/wwdata/Class_HydroData.py index 566952d6e..586e0938c 100644 --- a/wwdata/Class_HydroData.py +++ b/wwdata/Class_HydroData.py @@ -1544,6 +1544,48 @@ def get_correlation(self,data_1,data_2,arange,zero_intercept=False, return slope,intercept,r_sq + def detect_drift(self, arange, max_slope, period=None): + # data input or using self.data? + """ + This function calculates the slope of the data in a certain given + period by for example fitting a line through it and compare it with + the maximum expected slope. + + Parameters + ---------- + arange : 2-element array of ints + the range in which to apply the function + max_slope : int + the maximum slope a signal is expected to have over a certain period + period : + the period over which a certain slope is allowed + + Returns + ---------- + information about the drift + """ + from scipy import signal + + if period is None or period is arange: + detrended_values = signal.detrend(self.data[arange[0]:arange[1]]) + line_segment = self.data[arange[0]:arange[1]] - detrended_values[:] + slope = line_segment[-1] - line_segment[0] / (arange[1]-arange[0]) + + if slope > max_slope[0]: + print('The actual slope is larger than the specified max slope') + + plt.plot(signal.detrend(self.data[arange[0]:arange[1]]), 'r', + self.data[arange[0]:arange[1]], 'g', + self.data[arange[0]:arange[1]] - signal.detrend(self.data + [arange[0]:arange[1]]), 'y') + + + else: + pass + + return None + + #============================================================================== # DAILY PROFILE CALCULATION #==============================================================================