Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include first and last recession flows in util_RecessionSegments.m #3

Open
RY4GIT opened this issue Oct 31, 2024 · 0 comments
Open

Comments

@RY4GIT
Copy link

RY4GIT commented Oct 31, 2024

% find timesteps with decreasing flow
decreasing_flow = Q(2:end)<(Q(1:end-1)+eps);
% start on a non-decreasing point
start_point = find(decreasing_flow==0,1);
decreasing_flow = decreasing_flow(start_point:end);
% find start and end of decreasing sections
flow_change = find(decreasing_flow(1:end-1) ~= decreasing_flow(2:end));
% reshape into x by 2 array (columns = start, end of decrease)
flow_change = flow_change(1:(2*floor(size(flow_change,1)./2)));
flow_change = reshape(flow_change,2,[]).';

Issue Description:

The current code excludes the first and last recession segments of the flow record, when the recession segments are partial (if the timeseries starts during recession, or the timeseries ends during recession).

This is generally fine (and may be more appropriate) for long time-series data; however, it can lead to an issue for short time frames, where you don't wanna miss any recession segments.

Proposed Solution:

I added a Python code snippet that accounts for the first and last recession flows (background: I've been translating this to Python).

I apologize that it is not in Matlab. I've documented this here to provide information for others who may encounter the same issue. I'm not looking for an immediate fix right now—rather thank you for your great work! The whole McMillan Lab has been keep using your TOSSH codes.

# Identify timesteps with decreasing flow (same as original)
_decreasing_flow = Q[1:].to_numpy() < (
    Q[:-1].to_numpy() + eps
) 

# The index for the first decreasing flow
# [ADDED: If the flow record starts from decreasing values, set the first starting point as the first "non-decreasing" index, so that it gets detected by np.diff]
if _decreasing_flow[0]: 
    start_point = 0
else:
    start_point = np.where(_decreasing_flow == 0)[0][0]

# Get the decreasing flow section  (same as original)
decreasing_flow = _decreasing_flow[start_point:]

# [ADDED: If the flow record starts from decreasing values, change the first value of decreasing_flow to False, so that it gets detected by np.diff]
if _decreasing_flow[0]:
    decreasing_flow[0] = False

# Find start and end of decreasing sections (same as original)
_flow_change = np.where(np.diff(decreasing_flow) != 0)[0] + start_point

# [ADDED: If the flow is still decreasing at the end of the period, append that to flow change]
if decreasing_flow[-1]:
    _flow_change = np.append(_flow_change, len(Q))

# Detect the flow change  (same as original)
flow_change = _flow_change[: 2 * (len(_flow_change) // 2)].reshape(-1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant