Finding Maximum Consecutive Dry Days in Daily Rainfall Data

Hi All,
I am trying to find maximum consecutive dry days in a long time series of daily rainfall. I am using a script to do for one year of data. Can somebody help to modify the code such that it counts the Max CDD for each year separately. Whenever the data is less than 0.1, it is considered as dry day.
max_dry = 0; % initialize the maximum number of consecuitive dry days
counter = 0;
for k=1:366
if a(k) == 0
counter = counter + 1;
else
if counter > max_dry
max_dry = counter;
max_dry_position = k - counter;
end
counter = 0;
end
end

댓글 수: 2

your code in incomplete! what is a ?
Sorry... a is my mxn matrix for daily rainfall. You can call it RR. in my case its 6210X1 matrix

댓글을 달려면 로그인하십시오.

 채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2022년 7월 5일
편집: Bjorn Gustavsson 2022년 7월 5일
Don't do it with loops, that is a lot of work. Use the vectorized functions to your advantage. Here's how to get at it with using diff and max:
drf = 3*randn(366,1); % mocking up some daily rainfall-data.
DryDayThresh = 0.1;
rdc = find(drf > DryDayThresh); % Find the rainy days
plot(diff(rdc)) % Plot the difference between rainy days, 1 means two consecutive rainy days.
[maxCDD] = max(diff(rdc))-1;
HTH

댓글 수: 4

yes, thanks alot for this. But how can I get the CDD for each year separately considering i have 17 years of dailiy rain data?
You simply put this into a loop and repeat the process for the daily rainfall-data for each separate year. To solve your problem for you will not help you develop your programming and problem solving skills, but here goes anyway:
drf = 3*randn(366,91); % mocking up some daily rainfall-data, for 91 years because why not
DryDayThresh = 0.1;
for iYear = size(drf,2):-1:1 % loop down to avoid reallocation of arrays I couldn't be bothered to preallocate
% In here we simply repeat the above process for each separate year.
rdc = find(drf(:,iYear) > DryDayThresh); % Find the rainy days
plot(diff(rdc)) % Plot the difference between rainy days, 1 means two consecutive rainy days.
[maxCDD(iYear)] = max(diff(rdc))-1;
end
Thanks alot.. that helped.
You're welcome, happy that it helped.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Saksham Gupta
Saksham Gupta 2022년 7월 5일
As per my understanding, you need assistance in writing code for your problem.
This code should be helpful as per the conditions shared by you.
max_dry = 0; % initialize the maximum number of consecuitive dry days
counter = 0;
% a is supposed to be having data of each day as vector, I am using length, as year can
% have either 365 or 366 days.
for k=1:length(a)
if a(k) < 0.1 % conditon as mentioned by you
counter = counter + 1;
else
counter = 0;
end
max_dry=max(counter,max_dry);
end

댓글 수: 1

yes that gives the total number of dry days out of my data. But I need the Max Consecutive Dry Days (CDD) for each individual year.

댓글을 달려면 로그인하십시오.

This code shows one way of finding all the runs of maximum length.
rng(12345)
drf = rand(366,1); % mocking up some daily rainfall-data.
DryDayThresh = 0.2;
drf = reshape(drf, 1, []); %need it to be a row vector
rdc = drf < DryDayThresh; % locate the dry days
starts = strfind([0 rdc], [0 1]);
stops = strfind([rdc 0], [1 0]);
durations = stops - starts + 1;
longest = max(durations)
longest = 3
long_idx = durations == longest;
start_of_longest = starts(long_idx)
start_of_longest = 1×2
234 345
end_of_longest = stops(long_idx)
end_of_longest = 1×2
236 347
plot(drf);
yline(DryDayThresh)
hold on
%stairs(rdc, 'k')
xline(start_of_longest, 'r')
xline(end_of_longest, 'r')

댓글 수: 1

rng(12345)
drf = rand(366,1); % mocking up some daily rainfall-data.
DryDayThresh = 0.2;
drf = reshape(drf, 1, []); %need it to be a row vector
rdc = drf < DryDayThresh; % locate the dry days
props = regionprops(rdc, 'BoundingBox', 'Area');
durations = [props.Area];
longest = max(durations);
long_idx = durations == longest;
selected_props = props(long_idx);
bounds_of_longest = round(vertcat(selected_props.BoundingBox) + [.5 .5 0 0]);
start_of_longest = bounds_of_longest(:,1)
start_of_longest = 2×1
234 345
end_of_longest = start_of_longest + bounds_of_longest(:,3) - 1
end_of_longest = 2×1
236 347
plot(drf);
yline(DryDayThresh)
hold on
xline(start_of_longest, 'r')
xline(end_of_longest, 'r')

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by