Factorize calls to loadjson_ routine.

time-shift
Stéphane Adjemian (Charybdis) 2020-02-02 21:52:44 +01:00
parent 1e0da3a6a7
commit 3521e83b26
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
2 changed files with 42 additions and 29 deletions

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
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.