Calculation of events duration time at for a specific period
4 次查看(过去 30 天)
显示 更早的评论
Hi Everyone,
I've this code that able to create a xlsx file that holds the electricity tariffs over a year, depends on the hour and the season, it called Time of use,
I am interested to calculate the duration time (in hours) over one year, for every tariff, and save this data in the same table (or another table it's fine too).
for example : Tariff 0.2307 was active for 2000 Hours...etc.
Thanks in advance !
%% Define date&time arrays :
t1 = datetime(2021,1,1,0,0,0);
t2 = datetime(2021,12,31,23,0,0);
dt = (t1:hours(1):t2)'
%holidyas date array
Holidays = [datetime(2021,3,27,0,0,0) datetime(2021,3,28,0,0,0) datetime(2021,4,2,0,0,0) datetime(2021,4,3,0,0,0) datetime(2021,4,14,0,0,0) datetime(2021,4,15,0,0,0) datetime(2021,5,16,0,0,0) datetime(2021,5,17,0,0,0) datetime(2021,9,6,0,0,0) datetime(2021,9,7,0,0,0) datetime(2021,9,15,0,0,0) datetime(2021,9,16,0,0,0) datetime(2021,9,20,0,0,0) datetime(2021,9,27,0,0,0)]
Holidays = Holidays'
DateNtime=zeros(numel(dt),2); %preallocating
%% All Seasons Logic :
%% Hours between 00:00 - 16:00 "Off Peak" - Days of week. (All Seasons)
for i=1:numel(dt)
if hour(dt(i))>=0 & hour(dt(i))<=16 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
%% On Peak - Days of week only (All Seasons)
for i=1:numel(dt)
if hour(dt(i))>=17 & hour(dt(i))<=21 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.7170;
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2578;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=1.0789;
end
end
end
%% Season #1 Logic :
%% Hours between 00:00 - 16:00 "Off Peak" - Days of week. (Season#1)
for i=1:numel(dt)
if hour(dt(i))>=0 & hour(dt(i))<=16 & ismember(day(dt(i),'dayofweek'),[6 7 ])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
end
end
end
%% Hours between 22:00 - 23:00 "Off Peak" - All Week . (Season#1 only)
for i=1:numel(dt)
if hour(dt(i))>=22 & hour(dt(i))<=23 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5 6 7])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
end
end
end
%% Hour 23:00 "Off Peak" - Days of week. (Season#1)
for i=1:numel(dt)
if hour(dt(i))== 23 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.2307;
end
end
end
%% Weekends On Peak (Season #1)
for i=1:numel(dt)
if hour(dt(i))>=17 & hour(dt(i))<=21 & ismember(day(dt(i),'dayofweek'),[6 7])
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.7170;
end
end
end
%% Holidays On Peak (Seasons #1)
doy = day (Holidays(:,:),'dayofyear')
for i=1:numel(dt)
if hour(dt(i))>=17 & hour(dt(i))<=21 & ismember(day(dt(i),'dayofyear'),doy)
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.7170;
end
end
end
%% Holidays Off Peak 00:00 - 16:00 (Seasons #1)
doy = day (Holidays(:,:),'dayofyear')
for i=1:numel(dt)
if hour(dt(i))>=00 & hour(dt(i))<=16 & ismember(day(dt(i),'dayofyear'),doy)
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.7170;
end
end
end
%% Holidays Off Peak 22:00 - 23:00 (Seasons #1)
doy = day (Holidays(:,:),'dayofyear')
for i=1:numel(dt)
if hour(dt(i))>=22 & hour(dt(i))<=23 & ismember(day(dt(i),'dayofyear'),doy)
switch month(dt(i))
case {12,1,2} %% Season #1
DateNtime(i,2)=0.7170;
end
end
end
%% Hour 23:00 "Off Peak" - Days of week. (Season#2 & Season #3)
for i=1:numel(dt)
if hour(dt(i))== 23 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
%% Weekends. (Season #2 & Season #3)
for i=1:numel(dt)
if ismember(day(dt(i),'dayofweek'),[6 7])
switch month(dt(i))
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
%% Hour 22:00 "On Peak" - Days of week. (Season#2 & Season #3)
for i=1:numel(dt)
if hour(dt(i)) == 22 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2578;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=1.0789;
end
end
end
%% Holidays (Seasons #2 and #3)
doy = day (Holidays(:,:),'dayofyear')
for i=1:numel(dt)
if ismember(day(dt(i),'dayofyear'),doy)
switch month(dt(i))
case {3,4,5,10,11} %% Season #2
DateNtime(i,2)=0.2243;
case {6,7,8,9} %% Season #3
DateNtime(i,2)=0.2701;
end
end
end
%% Creat Table to export :
A = dt(:,1);
B = DateNtime(:,2);
newTable = table(A,B);
writetable(newTable,'TIME_OF_USE.xlsx');
0 个评论
回答(1 个)
Seth Furman
2022-12-8
It's not clear how you want to calculate these values.
Assuming you want to find the first and last timestamp for each tariff and take the difference, you could use groupsummary and then take the difference.
t = readtable("TIME_OF_USE.xlsx")
tMin = groupsummary(t,"B","min")
tMax = groupsummary(t,"B","max")
tDuration = removevars(tMin,"min_A");
tDuration.Duration = tMax.max_A - tMin.min_A
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!