From 3521e83b26c138ed876757f7bf32031a190cbcea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Sun, 2 Feb 2020 21:52:44 +0100 Subject: [PATCH] Factorize calls to loadjson_ routine. --- matlab/cherrypick.m | 35 +++++++++++++++++++---------------- matlab/get_lhs_and_rhs.m | 36 +++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/matlab/cherrypick.m b/matlab/cherrypick.m index 5ca8ea925..c12a1987c 100644 --- a/matlab/cherrypick.m +++ b/matlab/cherrypick.m @@ -1,4 +1,4 @@ -function cherrypick(infile, outfold, eqtags, noresids) +function json = cherrypick(infile, outfold, eqtags, noresids, json) % Extract some equations in infile (mod file used for estimation) % and write them in outfile (mod file used for simulation). @@ -8,15 +8,16 @@ function cherrypick(infile, outfold, eqtags, noresids) % - outfold [string] Name of the folder where the generated files are saveda subset of the equations is to be printed. % - eqtags [cell] Equation tags of the selected equations. % - noresids [logical] Removes estimation residuals (not to be used in simulation) if true. +% - json [char] Content of a JSON file. % % OUTPUTS -% none. +% - json [char] Content of a JSON file. % % SPECIAL REQUIREMENTS % It is expected that the file infile.mod has already been run, and % that the associated JSON output is available. -% Copyright (C) 2019 Dynare Team +% Copyright © 2019-2020 Dynare Team % % This file is part of Dynare. % @@ -36,7 +37,7 @@ function cherrypick(infile, outfold, eqtags, noresids) global M_ % Set default value -if nargin<4 +if nargin<4 || isempty(noresids) noresids = true; end @@ -60,8 +61,10 @@ end rename = M_.equations_tags(strcmp('rename',M_.equations_tags(:,2)),[1,3]); isrename = ~isempty(rename); -% Load json file (original mod file) -orig = loadjson_(sprintf('%s/model/json/modfile-original.json', M_.dname)); +if nargin<5 + % Load json file (original mod file) + json = loadjson_(sprintf('%s/model/json/modfile-original.json', M_.dname)); +end % Create a new file. fid = fopen(sprintf('%s/model.inc', outfold), 'w'); @@ -76,7 +79,7 @@ for i=1:length(eqtags) % Get equation number. eqnum = get_equation_number_by_tag(eqtags{i}, M_); % Get the original equation. - [LHS, RHS] = get_lhs_and_rhs(eqtags{i}, M_, true); + [LHS, RHS] = get_lhs_and_rhs(eqtags{i}, M_, true, json); % Get the parameters, endogenous and exogenous variables in the current equation. [pnames, ~, xnames] = get_variables_and_parameters_in_equation(LHS, RHS, M_); lhs_expression = LHS; @@ -173,20 +176,20 @@ for i=1:length(eqtags) end end % Print tags - if iscell(orig.model) - tfields = fieldnames(orig.model{eqnum}.tags); - tags = sprintf('%s=''%s''', tfields{1}, orig.model{eqnum}.tags.(tfields{1})); + if iscell(json.model) + tfields = fieldnames(json.model{eqnum}.tags); + tags = sprintf('%s=''%s''', tfields{1}, json.model{eqnum}.tags.(tfields{1})); for j=2:length(tfields) - if ~isempty(orig.model{eqnum}.tags.(tfields{j})) - tags = sprintf('%s, %s=''%s''', tags, tfields{j}, orig.model{eqnum}.tags.(tfields{j})); + if ~isempty(json.model{eqnum}.tags.(tfields{j})) + tags = sprintf('%s, %s=''%s''', tags, tfields{j}, json.model{eqnum}.tags.(tfields{j})); end end else - tfields = fieldnames(orig.model.tags); - tags = sprintf('%s=''%s''', tfields{1}, orig.model.tags.(tfields{1})); + tfields = fieldnames(json.model.tags); + tags = sprintf('%s=''%s''', tfields{1}, json.model.tags.(tfields{1})); for j=2:length(tfields) - if ~isempty(orig.model.tags.(tfields{j})) - tags = sprintf('%s, %s=''%s''', tags, tfields{j}, orig.model.tags.(tfields{j})); + if ~isempty(json.model.tags.(tfields{j})) + tags = sprintf('%s, %s=''%s''', tags, tfields{j}, json.model.tags.(tfields{j})); end end end diff --git a/matlab/get_lhs_and_rhs.m b/matlab/get_lhs_and_rhs.m index 965157859..0c93f3aca 100644 --- a/matlab/get_lhs_and_rhs.m +++ b/matlab/get_lhs_and_rhs.m @@ -1,14 +1,18 @@ -function [lhs, rhs] = get_lhs_and_rhs(eqname, DynareModel, original) +function [lhs, rhs, json] = get_lhs_and_rhs(eqname, DynareModel, original, json) % Returns the left and right handsides of an equation. % % INPUTS -% - lhs [string] Left hand side of the equation. -% - rhs [string] Right hand side of the equation. -% - DynareModel [struct] Structure describing the current model (M_). +% - eqname [char] Name of the equation. +% - DynareModel [struct] Structure describing the current model (M_). +% - original [logical] fetch equation in modfile-original.json or modfile.json +% - json [char] content of the JSON file % % OUTPUTS -% - eqname [string] Name of the equation. +% - lhs [char] Left hand side of the equation. +% - rhs [char] Right hand side of the equation. + +% % % SPECIAL REQUIREMENTS % The user must have attached names to the equations using equation @@ -19,7 +23,7 @@ function [lhs, rhs] = get_lhs_and_rhs(eqname, DynareModel, original) % [name='Phillips curve'] % pi = beta*pi(1) + slope*y + lam; -% Copyright (C) 2018 Dynare Team +% Copyright © 2018-2020 Dynare Team % % This file is part of Dynare. % @@ -36,20 +40,26 @@ function [lhs, rhs] = get_lhs_and_rhs(eqname, DynareModel, original) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . -if nargin<3 +if nargin<3 || isempty(original) original = false; end -% Get the equation from the JSON output. -if original - jsonfil = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile-original.json']); -else - jsonfil = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile.json']); +% Load JSON file if nargin<4 +if nargin<4 + if original + json = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile-original.json']); + else + json = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile.json']); + end end -jsonmod = jsonfil.model; + +% Load model. +jsonmod = json.model; if isstruct(jsonmod) jsonmod = {jsonmod}; end + +% Load equation. jsoneqn = getEquationsByTags(jsonmod, 'name', eqname); % Get the lhs and rhs members of the selected equation.