From 4f02e58010791b395bfd918b24d6502bcc862bbe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Tue, 17 Jun 2014 16:03:30 +0200
Subject: [PATCH] Added the possibility to pass a dseries object to the data
 command (the user can manipulate the data in the mod file, using dseries
 class methods, and use them for estimation without writing the data on disk).

---
 matlab/global_initialization.m         |  1 +
 matlab/utilities/dataset/makedataset.m | 58 +++++++++++++++++---------
 preprocessor/ComputingTasks.cc         | 12 +++++-
 preprocessor/DynareBison.yy            |  4 +-
 preprocessor/DynareFlex.ll             |  1 +
 5 files changed, 53 insertions(+), 23 deletions(-)

diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m
index 364edc3023..1122b99bb9 100644
--- a/matlab/global_initialization.m
+++ b/matlab/global_initialization.m
@@ -349,6 +349,7 @@ estimation_info.structural_innovation_corr_options_index = {};
 estimation_info.structural_innovation_corr.range_index = {};
 options_.initial_period = dates(1,1);
 options_.dataset.file = [];
+options_.dataset.series = [];
 options_.dataset.firstobs = dates();
 options_.dataset.lastobs = dates();
 options_.dataset.nobs = NaN;
diff --git a/matlab/utilities/dataset/makedataset.m b/matlab/utilities/dataset/makedataset.m
index 52064d441b..08b91229c0 100644
--- a/matlab/utilities/dataset/makedataset.m
+++ b/matlab/utilities/dataset/makedataset.m
@@ -23,18 +23,29 @@ function [DynareDataset, DatasetInfo] = makedataset(DynareOptions)
 %
 % See also dynare_estimation_init
 
-if isempty(DynareOptions.datafile) && isempty(DynareOptions.dataset.file)
+if isempty(DynareOptions.datafile) && isempty(DynareOptions.dataset.file) && isempty(DynareOptions.dataset.series)
     if gsa_flag
         DynareDataset = dseries();
         return
     else
-        error('datafile option is missing')
+        error('makedataset: datafile option is missing!')
     end
 end
 
 if isempty(DynareOptions.datafile) && ~isempty(DynareOptions.dataset.file)
     datafile = DynareOptions.dataset.file;
     newdatainterface = 1;
+elseif isempty(DynareOptions.datafile) && ~isempty(DynareOptions.dataset.series)
+    try
+        dseriesobjectforuserdataset = evalin('base', DynareOptions.dataset.series);
+    catch
+        error(sprintf('makedataset: %s is unknown!', DynareOptions.dataset.series))
+    end
+    if ~isdseries(dseriesobjectforuserdataset)
+        error(sprintf('makedataset: %s has to be a dseries object!', DynareOptions.dataset.series))
+    end
+    datafile = [];
+    newdatainterface = 1;
 elseif ~isempty(DynareOptions.datafile) && isempty(DynareOptions.dataset.file)
     datafile = DynareOptions.datafile;
     newdatainterface = 0;
@@ -45,29 +56,36 @@ else
 end
 
 % Check extension.
-allowed_extensions = {'m','mat','csv','xls','xlsx'};
-datafile_extension = get_file_extension(datafile);
-if isempty(datafile_extension)
-    available_extensions = {}; j = 1;
-    for i=1:length(allowed_extensions)
-        if exist([datafile '.' allowed_extensions{i}])
-            available_extensions(j) = {allowed_extensions{i}};
-            j = j+1;
+if ~isempty(datafile)
+    allowed_extensions = {'m','mat','csv','xls','xlsx'};
+    datafile_extension = get_file_extension(datafile);
+    if isempty(datafile_extension)
+        available_extensions = {}; j = 1;
+        for i=1:length(allowed_extensions)
+            if exist([datafile '.' allowed_extensions{i}])
+                available_extensions(j) = {allowed_extensions{i}};
+                j = j+1;
+            end
         end
+        if isempty(available_extensions)
+            error(['I can''t find a datafile (with allowed extension)!'])
+        end
+        if length(available_extensions)>1
+            error(sprintf(['You did not specify an extension for the datafile, but more than one candidate ' ...
+                           'are available in the designed folder!\nPlease, add an extension to the datafile ' ...
+                           '(m, mat, csv, xls or xlsx are legal extensions).']));
+        end
+        datafile = [datafile '.' available_extensions{1}];
     end
-    if isempty(available_extensions)
-        error(['I can''t find a datafile (with allowed extension)!'])
-    end
-    if length(available_extensions)>1
-        error(sprintf(['You did not specify an extension for the datafile, but more than one candidate ' ...
-                       'are available in the designed folder!\nPlease, add an extension to the datafile ' ...
-                       '(m, mat, csv, xls or xlsx are legal extensions).']));
-    end
-    datafile = [datafile '.' available_extensions{1}];
 end
 
 % Load the data in a dseries object.
-DynareDataset = dseries(datafile);
+if ~isempty(datafile)
+    DynareDataset = dseries(datafile);
+else
+    DynareDataset = dseriesobjectforuserdataset;
+    clear('dseriesobjectforuserdataset');
+end
 
 % Select a subset of the variables.
 DynareDataset = DynareDataset{DynareOptions.varobs{:}};
diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc
index 25049b0828..c23908a0d5 100644
--- a/preprocessor/ComputingTasks.cc
+++ b/preprocessor/ComputingTasks.cc
@@ -1876,9 +1876,17 @@ EstimationDataStatement::checkPass(ModFileStructure &mod_file_struct, WarningCon
         exit(EXIT_FAILURE);
       }
 
-  if (options_list.string_options.find("file") == options_list.string_options.end())
+  if ((options_list.string_options.find("file") == options_list.string_options.end()) &&
+      (options_list.string_options.find("series") == options_list.string_options.end()))
     {
-      cerr << "ERROR: The file option must be passed to the data statement." << endl;
+      cerr << "ERROR: The file or series option must be passed to the data statement." << endl;
+      exit(EXIT_FAILURE);
+    }
+
+  if ((options_list.string_options.find("file") != options_list.string_options.end()) &&
+      (options_list.string_options.find("series") != options_list.string_options.end()))
+    {
+      cerr << "ERROR: The file and series options cannot be used simultaneously in the data statement." << endl;
       exit(EXIT_FAILURE);
     }
 }
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index 5f3f336bad..0c754a4bc0 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -90,7 +90,7 @@ class ParsingDriver;
 %token BVAR_REPLIC BYTECODE ALL_VALUES_REQUIRED
 %token CALIB_SMOOTHER CHANGE_TYPE CHECK CONDITIONAL_FORECAST CONDITIONAL_FORECAST_PATHS CONF_SIG CONSTANT CONTROLLED_VAREXO CORR COVAR CUTOFF CYCLE_REDUCTION LOGARITHMIC_REDUCTION
 %token CONSIDER_ALL_ENDOGENOUS CONSIDER_ONLY_OBSERVED
-%token DATAFILE FILE DOUBLING DR_CYCLE_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_MAXITER DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION DIFFERENTIATE_FORWARD_VARS
+%token DATAFILE FILE SERIES DOUBLING DR_CYCLE_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_TOL DR_LOGARITHMIC_REDUCTION_MAXITER DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION DIFFERENTIATE_FORWARD_VARS
 %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT EXTENDED_PATH ENDOGENOUS_PRIOR
 %token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME
 %token <string_val> FLOAT_NUMBER DATES
@@ -1366,6 +1366,7 @@ data_options_list : data_options_list COMMA data_options
                   ;
 
 data_options : o_file
+             | o_series
              | o_data_first_obs
              | o_data_last_obs
              | o_data_nobs
@@ -2493,6 +2494,7 @@ o_simul_seed : SIMUL_SEED EQUAL INT_NUMBER { driver.error("'simul_seed' option i
 o_qz_criterium : QZ_CRITERIUM EQUAL non_negative_number { driver.option_num("qz_criterium", $3); };
 o_qz_zero_threshold : QZ_ZERO_THRESHOLD EQUAL non_negative_number { driver.option_num("qz_zero_threshold", $3); };
 o_file : FILE EQUAL filename { driver.option_str("file", $3); };
+o_series : SERIES EQUAL symbol { driver.option_str("series", $3); };
 o_datafile : DATAFILE EQUAL filename { driver.option_str("datafile", $3); };
 o_nobs : NOBS EQUAL vec_int
          { driver.option_vec_int("nobs", $3); }
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index 6ab6faab0c..a552f19973 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -604,6 +604,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2
 <DYNARE_STATEMENT>simul_replic {return token::SIMUL_REPLIC;}
 <DYNARE_STATEMENT>xls_sheet {return token::XLS_SHEET;}
 <DYNARE_STATEMENT>xls_range {return token::XLS_RANGE;}
+<DYNARE_STATEMENT>series {return token::SERIES;}
 <DYNARE_STATEMENT>mh_recover {return token::MH_RECOVER;}
 <DYNARE_STATEMENT>planner_discount {return token::PLANNER_DISCOUNT;}
 <DYNARE_STATEMENT>calibration {return token::CALIBRATION;}
-- 
GitLab