From f3c807f57782514efb0be5afcf5a21fa8f5e5282 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 29 Jul 2014 15:20:43 +0200
Subject: [PATCH] reporting: add writeCSV option for tables. closes #693

---
 doc/dynare.texi                               | 11 ++++++++++-
 matlab/reports/@report_table/report_table.m   |  2 ++
 matlab/reports/@report_table/writeTableFile.m | 15 ++++++++++++++-
 tests/reporting/CountryTablePage.m            |  3 ++-
 4 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index b1e8bb0a1..c66fa4b5a 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -11503,7 +11503,7 @@ reports. Default: @code{`black'}
 @end table
 @end defmethod
 
-@defmethod Report addTable data, showHlines, precision, range, seriesToUse, tableDirName, tableName, title, titleFormat, vlineAfter, vlineAfterEndOfPeriod, showVlines
+@defmethod Report addTable data, showHlines, precision, range, seriesToUse, tableDirName, tableName, title, titleFormat, vlineAfter, vlineAfterEndOfPeriod, showVlines, writeCSV
 Adds a @code{Table} to a @code{Section}.
 @optionshead
 @table @code
@@ -11523,10 +11523,12 @@ The date range of the data to be displayed. Default: @code{all}
 @item seriesToUse, @code{CELL_ARRAY_STRINGS}
 @xref{seriesToUse}.
 
+@anchor{tableDirName}
 @item tableDirName, @code{STRING}
 The name of the folder in which to store this table. Default:
 @code{tmpRepDir}
 
+@anchor{tableName}
 @item tableName, @code{STRING}
 The name to use when saving this table. Default: something of the
 form @code{table_pg1_sec2_row1_col3.tex}
@@ -11547,6 +11549,13 @@ every year, after the fourth quarter, etc.). Default: @code{false}
 
 @item showVlines, @code{BOOLEAN}
 Whether or not to show vertical lines separating the columns. Default: @code{false}
+
+@item writeCSV, @code{BOOLEAN}
+Whether or not to write a CSV file containing the data displayed in the
+table. The file will be saved in the directory specified by @ref{tableDirName}
+with the same base name as specified by @ref{tableName} with the ending
+@code{.csv}. Default: @code{false}
+
 @end table
 @end defmethod
 
diff --git a/matlab/reports/@report_table/report_table.m b/matlab/reports/@report_table/report_table.m
index 84b0d2f9d..ddc6c225c 100644
--- a/matlab/reports/@report_table/report_table.m
+++ b/matlab/reports/@report_table/report_table.m
@@ -49,6 +49,7 @@ o.data = '';
 o.seriesToUse = '';
 o.range = {};
 o.precision = 1;
+o.writeCSV = false;
 
 if nargin == 1
     assert(isa(varargin{1}, 'report_table'),['With one arg to Report_Table constructor, ' ...
@@ -115,6 +116,7 @@ assert(iscellstr(o.titleFormat), ...
        '@report_table.report_table: titleFormat must be a cell array of string(s)');
 assert(ischar(o.tableName), '@report_table.report_table: tableName must be a string');
 assert(ischar(o.tableDirName), '@report_table.report_table: tableDirName must be a string');
+assert(islogical(o.writeCSV), '@report_table.report_table: writeCSV must be either true or false');
 
 % using o.seriesToUse, create series objects and put them in o.series
 if ~isempty(o.data)
diff --git a/matlab/reports/@report_table/writeTableFile.m b/matlab/reports/@report_table/writeTableFile.m
index 311c12f72..7c0b7d1d9 100644
--- a/matlab/reports/@report_table/writeTableFile.m
+++ b/matlab/reports/@report_table/writeTableFile.m
@@ -171,13 +171,26 @@ fprintf(fid, '\\hline%%\n');
 fprintf(fid, '%%\n');
 
 % Write Report_Table Data
+if o.writeCSV
+    csvseries = dseries();
+end
 for i=1:ne
     o.series{i}.writeSeriesForTable(fid, o.range, o.precision, ncols);
+    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
 fprintf(fid, '\\bottomrule\n');
 fprintf(fid, '\\end{tabular}\\setlength{\\parindent}{0pt}\n \\par \\medskip\n\n');
 fprintf(fid, '%% End Report_Table Object\n');
diff --git a/tests/reporting/CountryTablePage.m b/tests/reporting/CountryTablePage.m
index 06ce66ea5..4f91d9fe3 100644
--- a/tests/reporting/CountryTablePage.m
+++ b/tests/reporting/CountryTablePage.m
@@ -43,7 +43,8 @@ notForOtherThree = {'BLT_', 'UNR_', 'UNR_BAR_', 'UNR_GAP_'};
 
 rep = rep.addTable('title', countryName, ...
                    'range', {trange, dates('2012a'):dates('2014a')}, ...
-                   'vlineAfter', {vline_after dates('2014q4')});
+                   'vlineAfter', {vline_after dates('2014q4')}, ...
+                   'writeCSV', true);
 
 
 
-- 
GitLab