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) % Extract some equations in infile (mod file used for estimation)
% and write them in outfile (mod file used for simulation). % 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. % - 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. % - eqtags [cell] Equation tags of the selected equations.
% - noresids [logical] Removes estimation residuals (not to be used in simulation) if true. % - noresids [logical] Removes estimation residuals (not to be used in simulation) if true.
% - json [char] Content of a JSON file.
% %
% OUTPUTS % OUTPUTS
% none. % - json [char] Content of a JSON file.
% %
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% It is expected that the file infile.mod has already been run, and % It is expected that the file infile.mod has already been run, and
% that the associated JSON output is available. % that the associated JSON output is available.
% Copyright (C) 2019 Dynare Team % Copyright © 2019-2020 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -36,7 +37,7 @@ function cherrypick(infile, outfold, eqtags, noresids)
global M_ global M_
% Set default value % Set default value
if nargin<4 if nargin<4 || isempty(noresids)
noresids = true; noresids = true;
end end
@ -60,8 +61,10 @@ end
rename = M_.equations_tags(strcmp('rename',M_.equations_tags(:,2)),[1,3]); rename = M_.equations_tags(strcmp('rename',M_.equations_tags(:,2)),[1,3]);
isrename = ~isempty(rename); isrename = ~isempty(rename);
% Load json file (original mod file) if nargin<5
orig = loadjson_(sprintf('%s/model/json/modfile-original.json', M_.dname)); % Load json file (original mod file)
json = loadjson_(sprintf('%s/model/json/modfile-original.json', M_.dname));
end
% Create a new file. % Create a new file.
fid = fopen(sprintf('%s/model.inc', outfold), 'w'); fid = fopen(sprintf('%s/model.inc', outfold), 'w');
@ -76,7 +79,7 @@ for i=1:length(eqtags)
% Get equation number. % Get equation number.
eqnum = get_equation_number_by_tag(eqtags{i}, M_); eqnum = get_equation_number_by_tag(eqtags{i}, M_);
% Get the original equation. % 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. % Get the parameters, endogenous and exogenous variables in the current equation.
[pnames, ~, xnames] = get_variables_and_parameters_in_equation(LHS, RHS, M_); [pnames, ~, xnames] = get_variables_and_parameters_in_equation(LHS, RHS, M_);
lhs_expression = LHS; lhs_expression = LHS;
@ -173,20 +176,20 @@ for i=1:length(eqtags)
end end
end end
% Print tags % Print tags
if iscell(orig.model) if iscell(json.model)
tfields = fieldnames(orig.model{eqnum}.tags); tfields = fieldnames(json.model{eqnum}.tags);
tags = sprintf('%s=''%s''', tfields{1}, orig.model{eqnum}.tags.(tfields{1})); tags = sprintf('%s=''%s''', tfields{1}, json.model{eqnum}.tags.(tfields{1}));
for j=2:length(tfields) for j=2:length(tfields)
if ~isempty(orig.model{eqnum}.tags.(tfields{j})) if ~isempty(json.model{eqnum}.tags.(tfields{j}))
tags = sprintf('%s, %s=''%s''', tags, tfields{j}, orig.model{eqnum}.tags.(tfields{j})); tags = sprintf('%s, %s=''%s''', tags, tfields{j}, json.model{eqnum}.tags.(tfields{j}));
end end
end end
else else
tfields = fieldnames(orig.model.tags); tfields = fieldnames(json.model.tags);
tags = sprintf('%s=''%s''', tfields{1}, orig.model.tags.(tfields{1})); tags = sprintf('%s=''%s''', tfields{1}, json.model.tags.(tfields{1}));
for j=2:length(tfields) for j=2:length(tfields)
if ~isempty(orig.model.tags.(tfields{j})) if ~isempty(json.model.tags.(tfields{j}))
tags = sprintf('%s, %s=''%s''', tags, tfields{j}, orig.model.tags.(tfields{j})); tags = sprintf('%s, %s=''%s''', tags, tfields{j}, json.model.tags.(tfields{j}));
end end
end 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. % Returns the left and right handsides of an equation.
% %
% INPUTS % INPUTS
% - lhs [string] Left hand side of the equation. % - eqname [char] Name of the equation.
% - rhs [string] Right hand side of the equation. % - DynareModel [struct] Structure describing the current model (M_).
% - 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 % 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 % SPECIAL REQUIREMENTS
% The user must have attached names to the equations using equation % 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'] % [name='Phillips curve']
% pi = beta*pi(1) + slope*y + lam; % pi = beta*pi(1) + slope*y + lam;
% Copyright (C) 2018 Dynare Team % Copyright © 2018-2020 Dynare Team
% %
% This file is part of Dynare. % 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 % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if nargin<3 if nargin<3 || isempty(original)
original = false; original = false;
end end
% Get the equation from the JSON output. % Load JSON file if nargin<4
if original if nargin<4
jsonfil = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile-original.json']); if original
else json = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile-original.json']);
jsonfil = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile.json']); else
json = loadjson_([DynareModel.fname filesep() 'model' filesep() 'json' filesep() 'modfile.json']);
end
end end
jsonmod = jsonfil.model;
% Load model.
jsonmod = json.model;
if isstruct(jsonmod) if isstruct(jsonmod)
jsonmod = {jsonmod}; jsonmod = {jsonmod};
end end
% Load equation.
jsoneqn = getEquationsByTags(jsonmod, 'name', eqname); jsoneqn = getEquationsByTags(jsonmod, 'name', eqname);
% Get the lhs and rhs members of the selected equation. % Get the lhs and rhs members of the selected equation.