write.m 4.26 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
function o = write(o, fid)
%function o = write(o, fid)
3
% Write a Report_Table object
Houtan Bastani's avatar
Houtan Bastani committed
4
5
%
% INPUTS
6
%   o           [report_table]    report_table object
Houtan Bastani's avatar
Houtan Bastani committed
7
%   fid         [integer]  file id
Houtan Bastani's avatar
Houtan Bastani committed
8
9
%
% OUTPUTS
10
%   o           [report_table]    report_table object
Houtan Bastani's avatar
Houtan Bastani committed
11
12
13
%
% SPECIAL REQUIREMENTS
%   none
Houtan Bastani's avatar
Houtan Bastani committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

% 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/>.

Houtan Bastani's avatar
Houtan Bastani committed
32
assert(fid ~= -1);
33
if ~o.seriesElements.numSeriesElements()
34
    warning('@report_table.write: no series to plot, returning');
35
    return;
36
37
end

Houtan Bastani's avatar
Houtan Bastani committed
38
39
40
41
42
43
%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;
Houtan Bastani's avatar
Houtan Bastani committed
44

45
46
47
48
49
50
51
if isempty(o.range)
    dates = o.seriesElements.getMaxRange();
else
    dates = o.range;
end
ndates = dates.ndat;

52
fprintf(fid, '%% Report_Table Object\n');
Houtan Bastani's avatar
Houtan Bastani committed
53
54
fprintf(fid, '\\setlength{\\tabcolsep}{4pt}\n');
fprintf(fid, '\\begin{tabular}{@{}l');
Houtan Bastani's avatar
Houtan Bastani committed
55
56

for i=1:ndates
57
    if o.showVlines
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        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
72
73
        end
    end
Houtan Bastani's avatar
Houtan Bastani committed
74
end
75
76
77
78
79
80
81
82
83
84
85
86
87
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
Houtan Bastani's avatar
Houtan Bastani committed
88
fprintf(fid, '@{}}%%\n');
Houtan Bastani's avatar
Houtan Bastani committed
89
if ~isempty(o.title)
90
    fprintf(fid, '\\multicolumn{%d}{c}{\\%s %s}\\\\\n', ...
91
            ndates+nlhc, o.titleSize, o.title);
Houtan Bastani's avatar
Houtan Bastani committed
92
93
94
end
fprintf(fid, '\\toprule%%\n');

95
% Column Headers
Houtan Bastani's avatar
Houtan Bastani committed
96
97
98
99
100
101
102
103
104
105
106
107
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)
108
                    error('@report_table.write: Shouldn''t arrive here');
Houtan Bastani's avatar
Houtan Bastani committed
109
110
111
112
113
114
115
116
117
                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
118
        fprintf(fid, '\\\\[-10pt]%%\n');
Houtan Bastani's avatar
Houtan Bastani committed
119
        for i=1:size(thdr, 1)
120
            fprintf(fid, ' & \\multicolumn{%d}{c}{\\hrulefill}', size(thdr{i,2}, 2));
Houtan Bastani's avatar
Houtan Bastani committed
121
        end
122
        fprintf(fid, '\\\\%%\n');
Houtan Bastani's avatar
Houtan Bastani committed
123
124
125
        for i=1:size(thdr, 1)
            quarters = thdr{i, 2};
            for j=1:size(quarters, 2)
126
                fprintf(fid, ' & \\multicolumn{1}{c}{Q%d}', quarters(j));
Houtan Bastani's avatar
Houtan Bastani committed
127
128
129
            end
        end
    case 12
130
        error('@report_table.write: weekly dates not yet implemented');
Houtan Bastani's avatar
Houtan Bastani committed
131
    otherwise
132
        error('@report_table.write: invalid dynSeries frequency');
133
end
134
135
136
for i=1:length(yrsForAvgs)
    fprintf(fid, ' & %d', years(i));
end
137
138
fprintf(fid, '\\\\[-2pt]%%\n');
fprintf(fid, '\\hline%%\n');
Houtan Bastani's avatar
Houtan Bastani committed
139
140
fprintf(fid, '%%\n');

141
% Write Report_Table Data
142
ne = o.seriesElements.numSeriesElements();
143
for i=1:ne
144
    o.seriesElements(i).write(fid, dates, o.precision, yrsForAvgs);
145
    if o.showHlines
146
147
        fprintf(fid, '\\hline\n');
    end
Houtan Bastani's avatar
Houtan Bastani committed
148
149
end

Houtan Bastani's avatar
Houtan Bastani committed
150
151
fprintf(fid, '\\bottomrule\n');
fprintf(fid, '\\end{tabular} \\par \\medskip\n\n');
152
fprintf(fid, '%% End Report_Table Object\n');
Houtan Bastani's avatar
Houtan Bastani committed
153
end