dynare/matlab/reports/@report_table/write.m

154 lines
4.3 KiB
Matlab
Raw Normal View History

2013-02-19 15:48:47 +01:00
function o = write(o, fid)
%function o = write(o, fid)
% Write a Report_Table object
2013-02-15 16:52:35 +01:00
%
% INPUTS
% o [report_table] report_table object
2013-04-10 13:01:22 +02:00
% fid [integer] file id
2013-02-15 16:52:35 +01:00
%
% OUTPUTS
% o [report_table] report_table object
2013-02-15 16:52:35 +01:00
%
% SPECIAL REQUIREMENTS
% none
2013-02-12 14:23:41 +01:00
% Copyright (C) 2013 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
2013-03-08 18:59:26 +01:00
assert(fid ~= -1);
if ~o.seriesElements.numSeriesElements()
warning('@report_table.write: no series to plot, returning');
2013-03-29 18:59:14 +01:00
return;
end
2013-03-08 18:59:26 +01:00
%number of left-hand columns, 1 until we allow the user to group data,
% e.g.: GDP Europe
% GDP France
% GDP Germany
% this example would be two lh columns, with GDP Europe spanning both
nlhc = 1;
2013-02-15 16:52:35 +01:00
2013-03-29 18:59:14 +01:00
if isempty(o.range)
dates = o.seriesElements.getMaxRange();
else
dates = o.range;
end
ndates = dates.ndat;
fprintf(fid, '%% Report_Table Object\n');
2013-03-20 15:10:19 +01:00
fprintf(fid, '\\setlength{\\tabcolsep}{4pt}\n');
fprintf(fid, '\\begin{tabular}{@{}l');
2013-03-08 18:59:26 +01:00
for i=1:ndates
2013-05-14 12:10:39 +02:00
if o.showVlines
fprintf(fid, 'r|');
else
fprintf(fid, 'r');
if o.vlineAfterEndOfPeriod
if dates(i).time(2) == dates(i).freq
fprintf(fid, '|');
end
end
if ~isempty(o.vlineAfter)
if dates(i) == o.vlineAfter
if ~(o.vlineAfterEndOfPeriod && dates(i).time(2) == dates(i).freq)
fprintf(fid, '|');
end
end
end
end
2013-03-08 18:59:26 +01:00
end
datedata = dates.time;
years = unique(datedata(:, 1));
if o.annualAverages
yrsForAvgs = years;
else
yrsForAvgs = [];
end
for i=1:length(yrsForAvgs)
fprintf(fid, 'r');
if o.showVlines
fprintf(fid, '|');
end
end
2013-03-20 15:10:19 +01:00
fprintf(fid, '@{}}%%\n');
2013-03-08 18:59:26 +01:00
if ~isempty(o.title)
fprintf(fid, '\\multicolumn{%d}{c}{\\%s %s}\\\\\n', ...
ndates+nlhc, o.titleSize, o.title);
2013-03-08 18:59:26 +01:00
end
fprintf(fid, '\\toprule%%\n');
% Column Headers
2013-03-08 18:59:26 +01:00
thdr = num2cell(years, size(years, 1));
switch dates.freq
case 1
for i=1:size(thdr, 1)
fprintf(fid, ' & %d', thdr{i, 1});
end
case 4
thdr{1, 2} = datedata(:, 2)';
if size(thdr, 1) > 1
for i=2:size(thdr, 1)
split = find(thdr{i-1, 2} == 4, 1, 'first');
if isempty(split)
error('@report_table.write: Shouldn''t arrive here');
2013-03-08 18:59:26 +01:00
else
thdr{i, 2} = thdr{i-1, 2}(split+1:end);
thdr{i-1, 2} = thdr{i-1, 2}(1:split);
end
end
end
for i=1:size(thdr, 1)
fprintf(fid, ' & \\multicolumn{%d}{c}{%d}', size(thdr{i,2}, 2), thdr{i,1});
end
2013-03-19 12:50:25 +01:00
fprintf(fid, '\\\\[-10pt]%%\n');
2013-03-08 18:59:26 +01:00
for i=1:size(thdr, 1)
2013-03-19 12:50:25 +01:00
fprintf(fid, ' & \\multicolumn{%d}{c}{\\hrulefill}', size(thdr{i,2}, 2));
2013-03-08 18:59:26 +01:00
end
2013-03-19 12:50:25 +01:00
fprintf(fid, '\\\\%%\n');
2013-03-08 18:59:26 +01:00
for i=1:size(thdr, 1)
quarters = thdr{i, 2};
for j=1:size(quarters, 2)
2013-03-19 12:50:25 +01:00
fprintf(fid, ' & \\multicolumn{1}{c}{Q%d}', quarters(j));
2013-03-08 18:59:26 +01:00
end
end
case 12
error('@report_table.write: weekly dates not yet implemented');
2013-03-08 18:59:26 +01:00
otherwise
error('@report_table.write: invalid dseries frequency');
2013-03-19 12:50:25 +01:00
end
for i=1:length(yrsForAvgs)
fprintf(fid, ' & %d', years(i));
end
2013-09-26 16:08:13 +02:00
fprintf(fid, '\\\\[-2pt]%%\n');
fprintf(fid, '\\hline%%\n');
2013-03-08 18:59:26 +01:00
fprintf(fid, '%%\n');
% Write Report_Table Data
ne = o.seriesElements.numSeriesElements();
for i=1:ne
o.seriesElements(i).write(fid, dates, o.precision, yrsForAvgs);
2013-05-14 12:27:46 +02:00
if o.showHlines
2013-05-14 12:26:49 +02:00
fprintf(fid, '\\hline\n');
end
2013-03-08 18:59:26 +01:00
end
2013-03-20 15:10:19 +01:00
fprintf(fid, '\\bottomrule\n');
fprintf(fid, '\\end{tabular} \\par \\medskip\n\n');
fprintf(fid, '%% End Report_Table Object\n');
2013-03-08 18:59:26 +01:00
end