File: writeTableFile.m

package info (click to toggle)
dynare 6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,648 kB
  • sloc: cpp: 79,109; ansic: 28,917; objc: 12,430; yacc: 4,528; pascal: 1,993; lex: 1,441; sh: 1,129; python: 634; makefile: 626; lisp: 163; xml: 18
file content (250 lines) | stat: -rw-r--r-- 8,043 bytes parent folder | download | duplicates (2)
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
function tableName = writeTableFile(o, pg, sec, row, col, rep_dir)
%function tableName = writeTableFile(o, pg, sec, row, col, rep_dir)
% Write a Report_Table object
%
% INPUTS
%   o         [report_table]  report_table object
%   pg        [integer]       this page number
%   sec       [integer]       this section number
%   row       [integer]       this row number
%   col       [integer]       this col number
%   rep_dir   [string]        directory containing report.tex
%
% OUTPUTS
%   tableName   [string]    name of table written
%
% SPECIAL REQUIREMENTS
%   none

% Copyright © 2013-2019 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 <https://www.gnu.org/licenses/>.

ne = length(o.series);
is_data_table = ~isempty(o.table_data);
if ne == 0 && ~is_data_table
    warning('@report_table.write: no series to plot, returning');
    return
end
if is_data_table
    ne = size(o.table_data{1}.data,2);
end
if exist([rep_dir '/' o.tableDirName], 'dir') ~= 7
    mkdir([rep_dir '/' o.tableDirName]);
end
if isempty(o.tableName)
    tableName = sprintf([o.tableDirName '/table_pg%d_sec%d_row%d_col%d.tex'], pg, sec, row, col);
else
    tableName = [o.tableDirName '/' o.tableName];
end

[fid, msg] = fopen([rep_dir '/' tableName], 'w');
if fid == -1
    error(['@report_table.writeTableFile: ' msg]);
end

fprintf(fid, '%% Report_Table Object written %s\n', datestr(now));
fprintf(fid, '\\begin{tabular}[t]{l}\n');
fprintf(fid, '\\setlength{\\parindent}{6pt}\n');
fprintf(fid, '\\setlength{\\tabcolsep}{4pt}\n');

%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;
if ~is_data_table
    fprintf(fid, '\\begin{tabular}{@{}l');
    if isempty(o.range)
        Dates = getMaxRange(o.series);
        o.range = {Dates};
    else
        Dates = o.range{1};
    end
    ndates = Dates.ndat;

    for i=1:ndates
        fprintf(fid, 'r');
        if o.showVlines
            fprintf(fid, '|');
        elseif o.vlineAfterEndOfPeriod && subperiod(Dates(i)) == Dates(i).freq
            fprintf(fid, '|');
        elseif ~isempty(o.vlineAfter)
            for j=1:length(o.vlineAfter)
                if Dates(i) == o.vlineAfter{j}
                    fprintf(fid, '|');
                end
            end
        end
    end
    years = unique(year(Dates));
    if length(o.range) > 1
        rhscols = strings(o.range{2});
        if o.range{2}.freq == 1
            rhscols = strrep(rhscols, 'Y', '');
        end
    else
        rhscols = {};
    end
    for i=1:length(rhscols)
        fprintf(fid, 'r');
        if o.showVlines
            fprintf(fid, '|');
        end
    end
    nrhc = length(rhscols);
    ncols = ndates+nlhc+nrhc;
    fprintf(fid, '@{}}%%\n');
    for i=1:length(o.title)
        if ~isempty(o.title{i})
            fprintf(fid, '\\multicolumn{%d}{c}{%s %s}\\\\\n', ...
                ncols, o.titleFormat{i}, o.title{i});
        end
    end
    fprintf(fid, '\\toprule%%\n');

    % Column Headers
    thdr = num2cell(years, size(years, 1));
    if Dates.freq == 1
        for i=1:size(thdr, 1)
            fprintf(fid, ' & %d', thdr{i, 1});
        end
        for i=1:length(rhscols)
            fprintf(fid, ' & %s', rhscols{i});
        end
    else
        thdr{1, 2} = subperiod(Dates)';
        if size(thdr, 1) > 1
            for i=2:size(thdr, 1)
                split = find(thdr{i-1, 2} == Dates.freq, 1, 'first');
                assert(~isempty(split), '@report_table.writeTableFile: Shouldn''t arrive here');
                thdr{i, 2} = thdr{i-1, 2}(split+1:end);
                thdr{i-1, 2} = thdr{i-1, 2}(1:split);
            end
        end
        for i=1:size(thdr, 1)
            fprintf(fid, ' & \\multicolumn{%d}{c}{%d}', size(thdr{i,2}, 2), thdr{i,1});
        end
        for i=1:length(rhscols)
            fprintf(fid, ' & %s', rhscols{i});
        end
        fprintf(fid, '\\\\\n');
        switch Dates.freq
            case 4
                sep = 'Q';
            case 12
                sep = 'M';
            case 52
                sep = 'W';
            otherwise
                error('@report_table.writeTableFile: Invalid frequency.');
        end
        for i=1:size(thdr, 1)
            period = thdr{i, 2};
            for j=1:size(period, 2)
                fprintf(fid, ' & \\multicolumn{1}{c');
                if o.showVlines
                    fprintf(fid, '|');
                elseif o.vlineAfterEndOfPeriod && j == size(period, 2)
                    fprintf(fid, '|');
                elseif ~isempty(o.vlineAfter)
                    for k=1:length(o.vlineAfter)
                        if year(o.vlineAfter{k}) == thdr{i} && ...
                                subperiod(o.vlineAfter{k}) == period(j)
                            fprintf(fid, '|');
                        end
                    end
                end
                fprintf(fid, '}{%s%d}', sep, period(j));
            end
        end
    end
else
    fprintf(fid, '\\begin{tabular}{');
    if o.showVlines
        fprintf(fid, '|');
    end
    if ~isempty(o.table_data{1}.column_names) && ne ~= length(o.table_data{1}.column_names)
        error(['@report_table.writeTableFile: when writing a data table and passing the ' ...
            '`column_names` option, it must have the same number of elements as the ' ...
            'number of columns in the data']);
    end
    for i = 1:ne
        if isempty(o.table_data{1}.column_names) ||isempty(o.table_data{1}.column_names{i})
            fprintf(fid, 'l');
        else
            fprintf(fid, 'r');
        end
        if o.showVlines
            fprintf(fid, '|');
        end
    end
    fprintf(fid,'}');
end

% Write Report_Table Data
if ~is_data_table
    fprintf(fid, '\\\\[-2pt]%%\n');
    fprintf(fid, '\\hline%%\n');
    fprintf(fid, '%%\n');
    if o.writeCSV
        csvseries = dseries();
    end
    for i=1:ne
        o.series{i}.writeSeriesForTable(fid, o.range, o.precision, ncols, o.highlightRows{mod(i,length(o.highlightRows))+1});
        if o.writeCSV
            if isempty(o.series{i}.tableSubSectionHeader)
                csvseries = [csvseries ...
                    o.series{i}.data(Dates).set_names([...
                    num2str(i) '_' ...
                    o.series{i}.data.name{:}])];
            end
        end
        if o.showHlines
            fprintf(fid, '\\hline\n');
        end
    end
    if o.writeCSV
        csvseries.save(strrep(o.tableName, '.tex', ''), 'csv');
    end
else
    if ~isempty(o.table_data{1}.column_names)
        fprintf(fid, '%%\n');
        fprintf(fid, '\\hline%%\n');
        fprintf(fid, '%%\n');
    end
    for i = 1:ne
        if ~isempty(o.table_data{1}.column_names) && ~isempty(o.table_data{1}.column_names{i})
            fprintf(fid, '%s', o.table_data{1}.column_names{i});
        end
        if i ~= ne
            fprintf(fid, ' & ');
        end
    end
    fprintf(fid, '\\\\[-2pt]%%\n');
    fprintf(fid, '\\hline%%\n');
    o.table_data{1}.writeDataForTable(fid, o.precision);
end
fprintf(fid, '\\bottomrule\n');
fprintf(fid, '\\end{tabular}\n');
fprintf(fid, '\\end{tabular}\n');
fprintf(fid, '\\setlength{\\parindent}{0pt}\n \\par \\medskip\n\n');
fprintf(fid, '%% End Report_Table Object\n');
if fclose(fid) == -1
    error('@report_table.writeTableFile: closing %s\n', o.filename);
end
end