Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2014 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9155161 to your computer and use it in GitHub Desktop.
Save anonymous/9155161 to your computer and use it in GitHub Desktop.
function t = combine(fileList, n)
% this combines all the files in fileList and saves some output files
addpath('jsonlab/');
% read in the file
x = [];
for ii = 1:numel(fileList)
fid = fopen(fileList{ii}, 'r');
raw = fscanf(fid, '%g,%g', [2 inf])';
x = [x; raw];
fclose(fid);
end
% get the dates in matlab's format (subtract 5 h to get local EST)
for i = 1:length(x)
d{i} = datestr(datenum([1970 1 1 -5 0 x(i,1)]));
end
% remove everything bigger than 7000 watts (don't like this, but I have outliers)
data = x(:,2);
data(data>7e3) = 6e3;
% transpose
time_data = d';
% JSON requires milliseconds
millisecond_time = x(:,1).*1000;
savejson('',[millisecond_time, data], fullfile('output',[n '.json']));
% now save CSV
print_csv(n, data, time_data)
% create a matlab timeseries
t = timeseries(data, time_data, 'Name', n);
t.TimeInfo.Units = 'days'; % this is the default from the datestr input
t.TimeInfo.Format = 'dd, HH:MM';
save(n, 't');
end
function combine_files
P1files = {'10_1.txt', '10_2.txt'};
P2files = {'11_1.txt', '11_2.txt'};
p1 = combine(P1files, 'Meter_1');
p2 = combine(P2files, 'Meter_2');
save('power_usage', 'p1', 'p2');
system('rm *.txt');
end
function [sync_data] = combined_plot
load power_usage;
close all;
% now combine the time series
[ts1,ts2] = synchronize(p1,p2, 'union');
combined_usage = ts1+ts2;
% combined_usage.TimeInfo.Units = 'hours';
% last_time = combined_usage.Time(end);
% t = combined_usage.Time;
% time_step = 10/(24*60); % days
% c = resample(combined_usage, t(1):time_step:t(end));
c = combined_usage;
c.Name = 'combined power usage';
c.DataInfo.Interpolation = tsdata.interpolation('zoh');
figure;
plot(c);
print_csv('combined', c.Data, getabstime(c));
% now we want a chart for each day
dv = datevec(getabstime(c));
days = unique(dv(:,1:3),'rows');
[~, subs] = ismember(dv(:,1:3), days, 'rows');
% now we want a time series for each day
daily_data = {}; % cell array of each days' data
subplots = figure;
together = figure;
first_day = getsamples(c, subs==2);
first_day = setabstime(first_day, datestr(getabstime(first_day)));
start_date = datevec(first_day.TimeInfo.StartDate);
start_day_date = start_date(1:3);
first_day.Name = 'reference';
colors = lines(length(days));
% tsc = tscollection(first_day);
for iDay = 2:(length(days)-1)
% create subarray for these days
k = getsamples(c, subs==iDay);
k_sync_time = datevec(getabstime(k));
k_sync_time(:,1:3) = ones(length(k_sync_time),1)*start_day_date;
k_to_sync = setabstime(k, datestr(k_sync_time));
[k_sync, ~] = synchronize(k_to_sync, first_day, 'union');
% k2 = k;
% k2.Time = kp.Time;
daily_data{iDay-1} = k;
k_sync.Name = ['Day ' num2str(iDay-1)];
sync_data{iDay-1} = k_sync;
% tsc = addts(tsc,k_sync);
% daily_data_common{iDay} = k;
figure(subplots);
subplot(5,2,iDay-1);
plot(k);
k.data = smooth(k.Time, k.Data, 0.1,'rloess');
plot(k, 'ro-');
st_time = getabstime(k);
title(datestr(st_time(1),'dddd, dd-mm'));
% combined plot
figure(together);
hold on;
k_sync.data = smooth(k_sync.Time, k_sync.Data, 0.1,'rloess');
plot(k_sync,'Color', colors(iDay -1, :));
end
daily_sum = [datenum(days) accumarray(subs, c.Data)];
figure;
bar(daily_sum(2:(end-1),1), daily_sum(2:(end-1),2));
datetick('x');
end
function watt_hr = compute_watt_hr(p)
st = p.Time(1);
ed = p.Time(end);
total_power = sum([0;diff(p.Time)]*24.*p.Data);
total_time = (ed - st)*24; % hours
watt_hr = total_power/total_time;
disp(' *** ');
disp(['Average watt/hr for this period: ' num2str(watt_hr)]);
disp(' *** ');
end
function [P1, P2] = plot_power
t1 = load('Meter_1');
t2 = load('Meter_2');
p1 = t1.t;
p2 = t2.t;
close all;
figure;
subplot(2,1,1);
w1 = get_ticks_and_plot(p1);
subplot(2,1,2);
w2 = get_ticks_and_plot(p2);
disp(' *** ');
disp(['Average watt/hr together: ' num2str(w1 + w2)]);
disp(' *** ');
end
function watt_hr = get_ticks_and_plot(p)
p.DataInfo.Interpolation = tsdata.interpolation('zoh');
plot(p);
watt_hr = compute_watt_hr(p);
text(0.2,4500,num2str(watt_hr));
% xt = (st : (2/24) : ed);
% set(gca, 'XTick', xt );
% fun = @(n) sprintf('%02d:00',n);
% xtl = arrayfun(fun,xt,'uniformoutput',false);
% set(gca,'xticklabel',xtl);
grid on;
end
function print_csv(n, data, t)
[nrows,~]= size(t);
filename = [n '.csv'];
fid = fopen(fullfile('output',filename), 'w');
fprintf(fid, '%s,%s\n', 'date,price');
for row=1:nrows
fprintf(fid, '%s,%d\n', t{row}, data(row));
end
fclose(fid);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment