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

---
 doc/dynare.texi                        | 10 +++++++++-
 matlab/reports/@graph/graph.m          |  4 +++-
 matlab/reports/@graph/writeGraphFile.m | 20 ++++++++++++++++++++
 tests/reporting/CountryGraphPage.m     |  3 ++-
 4 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index 7c9e838ee..b1e8bb0a1 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -11322,7 +11322,7 @@ command. Default: @code{`!'}
 @end defmethod
 
 @anchor{addGraph}
-@defmethod Report addGraph axisShape, data, graphDirName, graphName, graphSize, height, showGrid, showLegend, showLegendBox, legendLocation, legendOrientation, legendFontSize, miscTikzAxisOptions, miscTikzPictureOptions, seriesToUse, shade, shadeColor, shadeOpacity, tickFontSize, title, titleFontSize, titleFormat, width, xlabel, ylabel, xAxisTight, xrange, xTicks, xTickLabels, xTickLabelAnchor, xTickLabelRotation, yAxisTight, yTickLabelFixed, yTickLabelPrecision, yTickLabelScaled, yTickLabelZeroFill, yrange, showZeroline, zeroLineColor
+@defmethod Report addGraph axisShape, data, graphDirName, graphName, graphSize, height, showGrid, showLegend, showLegendBox, legendLocation, legendOrientation, legendFontSize, miscTikzAxisOptions, miscTikzPictureOptions, seriesToUse, shade, shadeColor, shadeOpacity, tickFontSize, title, titleFontSize, titleFormat, width, writeCSV, xlabel, ylabel, xAxisTight, xrange, xTicks, xTickLabels, xTickLabelAnchor, xTickLabelRotation, yAxisTight, yTickLabelFixed, yTickLabelPrecision, yTickLabelScaled, yTickLabelZeroFill, yrange, showZeroline, zeroLineColor
 Adds a @code{Graph} to a @code{Section}.
 @optionshead
 @table @code
@@ -11337,10 +11337,12 @@ to the left, right, bottom, and top of the graphed line(s). @code{`L'} means
 that there is an axis to the left and bottom of the graphed line(s). Default:
 @code{`box'}
 
+@anchor{graphDirName}
 @item graphDirName, @code{STRING}
 The name of the folder in which to store this figure. Default:
 @code{tmpRepDir}
 
+@anchor{graphName}
 @item graphName, @code{STRING}
 The name to use when saving this figure. Default: something of the
 form @code{graph_pg1_sec2_row1_col3.tex}
@@ -11426,6 +11428,12 @@ the title and subtitles. Default: @code{TikZ default}
 @item width, @code{DOUBLE}
 The width of the graph, in inches. Default: @code{6.0}
 
+@item writeCSV, @code{BOOLEAN}
+Whether or not to write a CSV file with only the plotted data. The file will be
+saved in the directory specified by @ref{graphDirName} with the same base name
+as specified by @ref{graphName} with the ending @code{.csv}. Default:
+@code{false}
+
 @item xlabel, @code{STRING}
 The x-axis label. Default: @code{none}
 
diff --git a/matlab/reports/@graph/graph.m b/matlab/reports/@graph/graph.m
index 7aa052514..4d73da534 100644
--- a/matlab/reports/@graph/graph.m
+++ b/matlab/reports/@graph/graph.m
@@ -85,6 +85,8 @@ o.height = 4.5;
 o.miscTikzPictureOptions = '';
 o.miscTikzAxisOptions = '';
 
+o.writeCSV = false;
+
 if nargin == 1
     assert(isa(varargin{1}, 'graph'),['@graph.graph: with one arg you ' ...
                         'must pass a graph object']);
@@ -139,7 +141,7 @@ assert(isint(o.yTickLabelPrecision), '@graph.graph: o.yTickLabelPrecision must b
 assert(islogical(o.yTickLabelFixed), '@graph.graph: yTickLabelFixed must be either true or false');
 assert(islogical(o.yTickLabelZeroFill), '@graph.graph: yTickLabelZeroFill must be either true or false');
 assert(islogical(o.yTickLabelScaled), '@graph.graph: yTickLabelScaled must be either true or false');
-
+assert(islogical(o.writeCSV), '@graph.graph: writeCSV must be either true or false');
 assert(ischar(o.shadeColor), '@graph.graph: shadeColor must be a string');
 assert(ischar(o.zeroLineColor), '@graph.graph: zeroLineColor must be a string');
 assert(any(strcmp(o.axisShape, {'box', 'L'})), ['@graph.graph: axisShape ' ...
diff --git a/matlab/reports/@graph/writeGraphFile.m b/matlab/reports/@graph/writeGraphFile.m
index bd820c9e9..85b09d41d 100644
--- a/matlab/reports/@graph/writeGraphFile.m
+++ b/matlab/reports/@graph/writeGraphFile.m
@@ -231,8 +231,25 @@ if o.showZeroline
         o.zeroLineColor, dd.ndat);
 end
 
+if o.writeCSV
+    csvseries = dseries();
+end
 for i=1:ne
     o.series{i}.writeSeriesForGraph(fid, dd);
+    if o.writeCSV
+        csvseries = [csvseries ...
+            o.series{i}.data(dd).set_names([...
+            o.series{i}.data.name{:} '_' ...
+            o.series{i}.graphLegendName '_' ...
+            o.series{i}.graphLineColor '_' ...
+            o.series{i}.graphLineStyle '_' ...
+            num2str(o.series{i}.graphLineWidth) '_' ...
+            o.series{i}.graphMarker '_' ...
+            o.series{i}.graphMarkerEdgeColor '_' ...
+            o.series{i}.graphMarkerFaceColor '_' ...
+            num2str(o.series{i}.graphMarkerSize)]) ...
+            ];
+    end
     if o.showLegend
         le = o.series{i}.getNameForLegend();
         if ~isempty(le)
@@ -240,6 +257,9 @@ for i=1:ne
         end
     end
 end
+if o.writeCSV
+    csvseries.save(strrep(o.graphName, '.tex', ''), 'csv');
+end
 fprintf(fid, '\\end{axis}\n\\end{tikzpicture}%%');
 if fclose(fid) == -1
     error('@graph.writeGraphFile: closing %s\n', o.filename);
diff --git a/tests/reporting/CountryGraphPage.m b/tests/reporting/CountryGraphPage.m
index 6323649b2..d67bb28a1 100644
--- a/tests/reporting/CountryGraphPage.m
+++ b/tests/reporting/CountryGraphPage.m
@@ -25,7 +25,8 @@ rep = rep.addGraph('title', 'Interest Rate', ...
                    'showZeroline', true, ...
                    'xTicks', [1,5,10,15,find(srange(1)==prange),length(prange)], ...
                    'xTickLabels',{startpoint{:},'2008Q1','2009Q2','2010Q3',shaded{:}, endpoint{:}},...
-                   'xTickLabelRotation', 0);
+                   'xTickLabelRotation', 0, ...
+                   'writeCSV', true);
 rep = rep.addSeries('data', db_q{['RS_' countryAbbr]}, ...
                     'graphLineColor', 'blue', ...
                     'graphLineStyle', 'dashed', ...
-- 
GitLab