-
Notifications
You must be signed in to change notification settings - Fork 0
/
BillCalc.m
476 lines (360 loc) · 31 KB
/
BillCalc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
function [ Bill,Status ] = BillCalc( Tariff,LoadData,Type)
% This function is for caluclating Annual bill based on any tariff
% updated on 24 November 2017
% Inputs:
% - TariffDetail: has the name of tariff as well as parameters needed for
% calculation
% - LoadData: is a table with one TimeStamp column and one Load column which
% can have multiple columns of different homes all in **kWh**
% - and one NetworkLoad column which has the network load (kWh) it's used
% for peak time, etc..
% The network load is only used for finding the times of the network peak
% so the absolute values are not that important.
% Type: reserved for further works
% Outputs:
% - Bill: is total annual bill for each home in $
% - Status: if everything was OK it's empty otherwise it has the error message
LoadData.Load(isnan(LoadData.Load))=0;
LoadData.Load(LoadData.Load<0)=0;
% for unitised bill:
BaseLoad=0.5*ones(size(LoadData.TimeStamp,1),1);
if nargin<3
Type='ordinary';
end
switch Tariff.Type
% The followings are for calculating the bill based on the tariff parameters.
case 'Block Quarterly'
% Daily component
Daily=Tariff.Parameters.Daily.Value*numel(unique(floor(datenum(LoadData.TimeStamp))));
% Bounds for quarters
AllBounds=Tariff.Parameters.Other.HighBound;
AllBounds=[0;AllBounds];
for i=2:size(AllBounds,1)-1
AllBoundsCost(i,1)=(AllBounds(i,1)-AllBounds(i-1,1))*Tariff.Parameters.Other.Value(i-1,1);
end
AllBoundsCostCS=cumsum(AllBoundsCost);
% base load for calculating unitised bill
BaseLoad=0.5*ones(size(LoadData.TimeStamp,1),1);
for Q=1:4
AllUsage(Q,:)=nansum(LoadData.Load(floor((LoadData.TimeStamp.Month-1)/3)==Q-1,:),1);
AllUsage_B(Q,1)=nansum(BaseLoad(floor((LoadData.TimeStamp.Month-1)/3)==Q-1,:),1);
for i=1:size(AllBounds,1)-1
UsageCost(Q,AllUsage(Q,:)>AllBounds(i)&AllUsage(Q,:)<=AllBounds(i+1))=Tariff.Parameters.Other.Value(i,1)*(AllUsage(Q,AllUsage(Q,:)>AllBounds(i)&AllUsage(Q,:)<=AllBounds(i+1))-AllBounds(i,1))+AllBoundsCostCS(i,1);
UsageCost_B(Q,AllUsage_B(Q,:)>AllBounds(i)&AllUsage_B(Q,:)<=AllBounds(i+1))=Tariff.Parameters.Other.Value(i,1)*(AllUsage_B(Q,AllUsage_B(Q,:)>AllBounds(i)&AllUsage_B(Q,:)<=AllBounds(i+1))-AllBounds(i,1))+AllBoundsCostCS(i,1);
end
end
%
if numel(find(sign(diff(AllBounds))<1))|| numel(find(AllBounds<0))
Status='Usage block bounds should be positive and incremental.';
Bill='';
elseif Tariff.Parameters.Daily.Value<0
Status='Daily charge should be positive.';
Bill='';
else
Status='';
Bill.Daily=repmat(Daily,1,size(LoadData.Load,2));
Bill.Usage=sum(UsageCost,1);
Bill.Usage_Q1=UsageCost(1,:);
Bill.Usage_Q2=UsageCost(2,:);
Bill.Usage_Q3=UsageCost(3,:);
Bill.Usage_Q4=UsageCost(4,:);
Bill.Total=Daily+sum(UsageCost,1);
Bill.Unitised=Bill.Total/(Daily+sum(UsageCost_B));
Bill.Components.Value=[Bill.Daily;Bill.Usage_Q1;Bill.Usage_Q2;Bill.Usage_Q3; Bill.Usage_Q4];
Bill.Components.Names={'Daily';'Usage_Q1';'Usage_Q2';'Usage_Q3';'Usage_Q4'};
end
case 'Block Annual'
Daily=Tariff.Parameters.Daily.Value*numel(unique(floor(datenum(LoadData.TimeStamp))));
AllBounds=Tariff.Parameters.Other.HighBound;
AllBounds=[0;AllBounds];
clear AllUsage UsageCost AllBoundsCost Bill
AllUsage(1,:)=nansum(LoadData.Load,1);
AllUsage_B(1,1)=nansum(BaseLoad,1);
for i=2:size(AllBounds,1)-1
AllBoundsCost(i,1)=(AllBounds(i,1)-AllBounds(i-1,1))*Tariff.Parameters.Other.Value(i-1,1);
end
AllBoundsCostCS=cumsum(AllBoundsCost);
for i=1:size(AllBounds,1)-1
UsageCost(1,AllUsage(1,:)>AllBounds(i)&AllUsage(1,:)<=AllBounds(i+1))=Tariff.Parameters.Other.Value(i,1)*(AllUsage(1,AllUsage(1,:)>AllBounds(i)&AllUsage(1,:)<=AllBounds(i+1))-AllBounds(i,1))+AllBoundsCostCS(i,1);
UsageCost_B(1,AllUsage_B(1,:)>AllBounds(i)&AllUsage_B(1,:)<=AllBounds(i+1))=Tariff.Parameters.Other.Value(i,1)*(AllUsage_B(1,AllUsage_B(1,:)>AllBounds(i)&AllUsage_B(1,:)<=AllBounds(i+1))-AllBounds(i,1))+AllBoundsCostCS(i,1);
end
if numel(find(sign(diff(AllBounds))<1))|| numel(find(AllBounds<0))
Status='Usage block bounds should be positive and incremental.';
Bill='';
elseif Tariff.Parameters.Daily.Value<0
Status='Daily charge should be positive.';
Bill='';
else
Status='';
Bill.Daily=repmat(Daily,1,size(LoadData.Load,2));
Bill.Usage=sum(UsageCost,1);
Bill.Total=Daily+sum(UsageCost,1);
Bill.Unitised=Bill.Total./(Daily+sum(UsageCost_B));
Bill.Components.Value=[Bill.Daily;Bill.Usage];
Bill.Components.Names={'Daily';'Usage'};
end
case 'Flat Rate'
Daily=Tariff.Parameters.Daily.Value*numel(unique(floor(datenum(LoadData.TimeStamp))));
if Tariff.Parameters.Other.Value<0
Status='Usage rate should be positive.';
Bill='';
else
Status='';
Bill.Daily=repmat(Daily,1,size(LoadData.Load,2));
Bill.Energy=Tariff.Parameters.Other.Value*nansum(LoadData.Load,1);
Bill.Total=Daily+Bill.Energy;
Bill_B=Daily+Tariff.Parameters.Other.Value*nansum(BaseLoad,1);
Bill.Components.Value=[Bill.Daily;Bill.Energy];
Bill.Unitised= Bill.Total./Bill_B;
Bill.Components.Names={'Daily';'Energy'};
end
case 'Flat Rate Seasonal'
Daily=Tariff.Parameters.Daily.Value*numel(unique(floor(datenum(LoadData.TimeStamp))));
if numel(find(sign(Tariff.Parameters.Other.Rate)<1))
Status='Usage rate should be positive.';
Bill='';
else
TimeIndex=zeros(size(LoadData.TimeStamp,1),1);
TimeIndexCheck=zeros(size(LoadData.TimeStamp,1),1);
for T=1:size(Tariff.Parameters.Other,1)
if Tariff.Parameters.Other.StartMonth(T,1)>Tariff.Parameters.Other.EndMonth(T,1)
TimeIndex((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1)),1)=T;
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1)),1)+1;
else
TimeIndex((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1)),1)=T;
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1)),1)+1;
end
end
if numel(find(sign([Tariff.Parameters.Daily.Value;[Tariff.Parameters.Other.Rate]])<1))
Status='Some tariff components rates are not correct!';
Bill='';
elseif numel(find(TimeIndexCheck<1))>0
Status='Tariff parameters are not sufficient for calculating bill!';
Bill='';
elseif numel(find(TimeIndexCheck>1))>0
Status='There is a problem in tariff parameters. Some time periods are included in more than one tariff components!';
Bill='';
else
for T=1:size(Tariff.Parameters.Other,1)
BillT(T,:)=Tariff.Parameters.Other.Rate(T,1)*nansum(LoadData.Load(TimeIndex==T,:));
BillT_B(T,1)=Tariff.Parameters.Other.Rate(T,1)*nansum(BaseLoad(TimeIndex==T,:));
end
Bill.Daily=repmat(Daily,1,size(LoadData.Load,2));
Bill.Energy=sum(BillT,1);
Bill.Total=Daily+sum(BillT,1);
Bill.Unitised=Bill.Total/(Daily+sum(BillT_B,1));
Status='';
Bill.Components.Value=[Bill.Daily;Bill.Energy];
Bill.Components.Names={'Daily';'Energy'};
end
end
case 'TOU'
[DayNumber,~] = weekday(LoadData.TimeStamp);
Daily=Tariff.Parameters.Daily.Value*numel(unique(floor(datenum(LoadData.TimeStamp))));
TimeIndex=zeros(size(LoadData.TimeStamp,1),1);
TimeIndexCheck=zeros(size(LoadData.TimeStamp,1),1);
for T=1:size(Tariff.Parameters.Other,1)
if Tariff.Parameters.Other.Weekday(T,1)==1
TimeIndex(DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=T;
TimeIndexCheck(DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=...
TimeIndexCheck(DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)+1;
end
if Tariff.Parameters.Other.Weekend(T,1)==1
TimeIndex((DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=T;
TimeIndexCheck((DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=...
TimeIndexCheck((DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)+1;
end
end
if numel(find(TimeIndexCheck<1))>0
Status='Tariff parameters are not sufficient for calculating bill!';
Bill='';
elseif numel(find(TimeIndexCheck>1))>0
Status='There is a problem in tariff parameters. Some time periods are included in more than one tariff components!';
Bill='';
else
for T=1:size(Tariff.Parameters.Other,1)
BillT(T,:)=Tariff.Parameters.Other.Rate(T,1)*nansum(LoadData.Load(TimeIndex==T,:));
BillT_B(T,1)=Tariff.Parameters.Other.Rate(T,1)*nansum(BaseLoad(TimeIndex==T,:));
end
Status='';
Bill.Daily=repmat(Daily,1,size(LoadData.Load,2));
Bill.TOU=sum(BillT,1);
Bill.Total=Daily+sum(BillT,1);
Bill.Unitised=Bill.Total/(Daily+sum(BillT_B,1));
Bill.Components.Value=[Bill.Daily;Bill.TOU];
Bill.Components.Names={'Daily';'TOU'};
end
case 'TOU Seasonal'
[DayNumber,~] = weekday(LoadData.TimeStamp);
Daily=Tariff.Parameters.Daily.Value*numel(unique(floor(datenum(LoadData.TimeStamp))));
TimeIndex=zeros(size(LoadData.TimeStamp,1),1);
TimeIndexCheck=zeros(size(LoadData.TimeStamp,1),1);
for T=1:size(Tariff.Parameters.Other,1)
if Tariff.Parameters.Other.Weekday(T,1)==1
if Tariff.Parameters.Other.StartMonth(T,1)>Tariff.Parameters.Other.EndMonth(T,1)
TimeIndex((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=T;
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)+1;
else
TimeIndex((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=T;
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)+1;
end
end
if Tariff.Parameters.Other.Weekend(T,1)==1
if Tariff.Parameters.Other.StartMonth(T,1)>Tariff.Parameters.Other.EndMonth(T,1)
TimeIndex((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=T;
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)|LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)+1;
else
TimeIndex((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=T;
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=Tariff.Parameters.Other.StartMonth(T,1)&LoadData.TimeStamp.Month<=Tariff.Parameters.Other.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*Tariff.Parameters.Other.StartHour(T,1)+Tariff.Parameters.Other.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*Tariff.Parameters.Other.EndHour(T,1)+Tariff.Parameters.Other.EndMin(T,1)),1)+1;
end
end
end
if numel(find(sign([Tariff.Parameters.Daily.Value;[Tariff.Parameters.Other.Rate]])<1))
Status='Some tariff components rates are not correct!';
Bill='';
elseif numel(find(TimeIndexCheck<1))>0
Status='Tariff parameters are not sufficient for calculating bill!';
Bill='';
elseif numel(find(TimeIndexCheck>1))>0
Status='There is a problem in tariff parameters. Some time periods are included in more than one tariff components!';
Bill='';
else
for T=1:size(Tariff.Parameters.Other,1)
BillT(T,:)=Tariff.Parameters.Other.Rate(T,1)*nansum(LoadData.Load(TimeIndex==T,:));
BillT_B(T,:)=Tariff.Parameters.Other.Rate(T,1)*nansum(BaseLoad(TimeIndex==T,:));
end
Bill.Daily=repmat(Daily,1,size(LoadData.Load,2));
Bill.TOU=sum(BillT,1);
Bill.Total=Daily+sum(BillT,1);
Bill.Unitised=Bill.Total/(Daily+sum(BillT_B,1));
Status='';
Bill.Components.Value=[Bill.Daily;Bill.TOU];
Bill.Components.Names={'Daily';'TOU'};
end
case 'Demand Charge'
Daily=Tariff.Parameters.Daily.Value*numel(unique(floor(datenum(LoadData.TimeStamp))));
[DayNumber,~] = weekday(LoadData.TimeStamp);
LoadData.Load=[LoadData.Load,BaseLoad];
TGs=unique(Tariff.Parameters.Other.TimeGroup);
TimeIndex=zeros(size(LoadData.TimeStamp));
TimeIndexCheck=zeros(size(LoadData.TimeStamp,1),1);
clear Res
for TT=1:size(TGs,1)
NewPar=Tariff.Parameters.Other(Tariff.Parameters.Other.TimeGroup==TGs(TT,1),:);
DayAverage=NewPar.DayAverage(1,1); % Only one day average per timegroup can be aceepted
NetworkPeak=NewPar.NetworkPeak(1,1);
for T=1:size(NewPar,1)
if NewPar.Weekday(T,1)==1
if NewPar.StartMonth(T,1)>NewPar.EndMonth(T,1)
TimeIndex((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)|LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=TT;
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)|LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)|LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)+1;
else
TimeIndex((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)&LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=TT;
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)&LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)&LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&DayNumber>1&DayNumber<7&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)+1;
end
end
if NewPar.Weekend(T,1)==1
if NewPar.StartMonth(T,1)>NewPar.EndMonth(T,1)
TimeIndex((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)|LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=TT;
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)|LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)|LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)+1;
else
TimeIndex((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)&LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=TT;
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)&LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)=...
TimeIndexCheck((LoadData.TimeStamp.Month>=NewPar.StartMonth(T,1)&LoadData.TimeStamp.Month<=NewPar.EndMonth(T,1))&(DayNumber==1|DayNumber==7)&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)>=(60*NewPar.StartHour(T,1)+NewPar.StartMin(T,1))&(60*LoadData.TimeStamp.Hour+LoadData.TimeStamp.Minute)<(60*NewPar.EndHour(T,1)+NewPar.EndMin(T,1)),1)+1;
end
end
end
DemandWindowTSNo= NewPar.DemandWindowTSNo(1,1);
NumberofPeaks=NewPar.NumberofPeaks(1,1);
TL2=LoadData;
TL2.Load(TimeIndex~=TT,:)=nan;
TL2.NetworkLoad(TimeIndex~=TT,:)=nan;
MnList=unique(LoadData.TimeStamp.Month(TimeIndex==TT));
for Mn2=1:size(MnList,1)
Mn=MnList(Mn2);
clear TempDay UPD NPD
TL3=TL2(TL2.TimeStamp.Month==Mn,:);
if DayAverage
if NetworkPeak
for Dn=1:size(unique(TL3.TimeStamp.Day),1)
[NPD(Dn,1),ind2]=nanmax(TL3.NetworkLoad(TL3.TimeStamp.Day==Dn,1));
TempDay(Dn,:)=nanmean(TL3.Load(TL3.TimeStamp.Day==Dn,:));
end
TempDay(isnan(TempDay))=0;
NPD(isnan(NPD))=0;
[ind3,ind4]=sort(NPD,'descend');
TempDay=TempDay(ind4,:);
else
for Dn=1:size(unique(TL3.TimeStamp.Day),1)
TempDay(Dn,:)=nanmean(TL3.Load(TL3.TimeStamp.Day==Dn,:));
end
TempDay(isnan(TempDay))=0;
TempDay=sort(TempDay,'descend');
end
else
if NetworkPeak
for Dn=1:size(unique(TL3.TimeStamp.Day),1)
[NPD(Dn,1),ind2]=nanmax(TL3.NetworkLoad(TL3.TimeStamp.Day==Dn,1));
TL3_2=LoadData.Load(LoadData.TimeStamp.Month==Mn&LoadData.TimeStamp.Day==Dn,:);
TempDay(Dn,:)=nanmean(TL3_2([ind2-DemandWindowTSNo+1:ind2],:),1);
end
TempDay(isnan(TempDay))=0;
NPD(isnan(NPD))=0;
[ind3,ind4]=sort(NPD,'descend');
TempDay=TempDay(ind4,:);
else
for Dn=1:size(unique(TL3.TimeStamp.Day),1)
[UPD(Dn,:),ind2]=nanmax(TL3.Load(TL3.TimeStamp.Day==Dn,:));
TL3_3=LoadData.Load(LoadData.TimeStamp.Month==Mn&LoadData.TimeStamp.Day==Dn,:);
TL3_4=TL3_3;
for iDTS=2:DemandWindowTSNo
TL3_4=TL3_4+[zeros(iDTS-1,size(TL3_3,2));TL3_3(1:end-iDTS+1,:)];
end
TL3_4=TL3_4/DemandWindowTSNo;
TempDay(Dn,:)=TL3_4(sub2ind(size(TL3_4), ind2 , 1:size(TL3_4,2)));
end
TempDay(isnan(TempDay))=0;
UPD(isnan(UPD))=0;
[ind3,ind4]=sort(UPD,'descend');
ind5=ind4+repmat([0:size(TempDay,1):size(TempDay,1)*size(TempDay,2)-1],size(TempDay,1),1);
TempDay=TempDay(ind5);
end
end
DemkW=2*nanmean(TempDay(1:NumberofPeaks,:),1); %kW
DemkW(DemkW<NewPar.MinDemandkW(1,1))=NewPar.MinDemandkW(1,1);
DemCost=NewPar.Rate(1,1)*DemkW;
if numel(strmatch('MinDemandCharge',NewPar.Properties.VariableNames))
DemCost(DemCost<NewPar.MinDemandCharge(1,1))=NewPar.MinDemandCharge(1,1);
end
Res{Mn,TT}=DemCost;
end
end
FinalDemCost=zeros(1,size(LoadData.Load,2));
for TT=1:size(TGs,1)
for Mn=1:12
try
FinalDemCost=FinalDemCost+ Res{Mn,TT};
end
end
end
Ener_AnyTime=Tariff.Parameters.Energy.Value(1,1)*nansum(LoadData.Load);
Status='';
Bill.Daily=repmat(Daily,1,size(LoadData.Load,2)-1);
Bill.Energy=Ener_AnyTime(1,1:end-1);
Bill.CapacityCharge=FinalDemCost(1,1:end-1);
Bill.Total=Bill.Daily+Bill.Energy+Bill.CapacityCharge;
Bill.Unitised=Bill.Total./(Daily+Ener_AnyTime(end)+FinalDemCost(end));
Bill.Components.Value=[Bill.Daily;Bill.Energy;Bill.CapacityCharge];
Bill.Components.Names={'Daily';'Energy';'CapacityCharge'};
end
end