% LAST CHANGES NOVEMBER 22, 2005 BY VICTOR
% oplot4a.m
% HAS TO BE RUN AFTER DYNARE
function oplot4(varargin)

oo_=evalin('base','oo_');
M_=evalin('base','M_');
var_list_=evalin('base','var_list_');
options_=evalin('base','options_');
if isfield(oo_,'irfs')
    ff=fieldnames(oo_.irfs);
    for jj=1:length(ff)
        eval([ff{jj},'=oo_.irfs.',ff{jj},''';']);
    end
end

if length(varargin)==0
verbose=input(' VERBOSE MODE ? ([y]/n) ==>> ','s');
else
    verbose=[];
end
if isfield(oo_,'PosteriorIRF')
            if isfield(oo_.PosteriorIRF,'dsge')
                doHPD=input('Only one question... Do you want the Posterio IRF? (1/[0]) ');
                if doHPD==1;
                    colHPD=input('Give color of HPD ==>> ','s');
                    if isempty(colHPD);colHPD='b';
                    end
                end
            end
end
clear vars
if isempty(verbose)
    verbose=1;
elseif strcmpi(verbose,'y')
    verbose=1;
elseif strcmpi(verbose,'n')
    verbose=0;
else
    error('CAN ONLY GIVE y/n or EMPTY')
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% DO YOUR CHANGES HERE:
if exist('dynare_version')>0

    gg='4';

else
    gg=which('dynare');
end

if ~isempty(regexp(gg,'4'));% first is 0 on octave
    %	if isfield(oo_,'irfs');
    %		for jj=1:size(fieldnames(oo_.irfs),1)
    %		eval([fieldnames(oo_.irfs){jj},'=oo_.irfs{',num2str(jj),'};']);
    %		end
    %		end

    Sigma_e_=M_.Sigma_e;
    lgy_=M_.endo_names;
    lgx_orig_ord_=1:size(Sigma_e_,1);
    lgx_=M_.exo_names;
    exo_nbr=M_.exo_nbr;
    endo_nbr=M_.endo_nbr;
end
if exist('tmplst','var');

    var_list_=tmplst;
end

if exist('oo_','var');
    if isfield(oo_,'steady_state')
        ys_=oo_.steady_state;
    end
end
if exist('M_','var')
    if isfield(M_,'fname')
        fname_=M_.fname;
    end
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

if isempty(var_list_);
    vars=strvcat(lgy_);
else
    vars=strvcat(var_list_);
end;
%      vars=strvcat(lgy_);
tmplst=vars;
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %


%var_list_=sortrows(var_list_)
newtitle=regexprep(str2cell(tmplst),'\_','\\_');

% if exist('relabel_oil','file')
% relabel_oil;
% else
% end
%%%% assign steady states
for jj=1:size(lgy_,1)
    if jj>size(ys_,1);
        ys_(end+1)=1;
    end
    eval([deblank(lgy_(jj,:)),'=ys_(',num2str(jj),');']);
end

% paramfile='param_nawm';
rc=[];
if length(varargin)<2
disp('##########################################################')
rc=input('give number of rows and columns [r,c] (default [5,3]) == >> ');
disp('##########################################################')
else
    rc=varargin{2};
end
if isempty(rc)
norows=5; % number of rows
nocols= 3; % number of columns
else
    norows=rc(1);
    nocols=rc(2);
end

freq=4; % tick step on time axis
ssflag=1; % do relative IRFs
propw=1.5;% distance between lines in parameter window
if ~exist('horizon','var');
    horizon=options_.irf;
end
% Parameter stuff to appear in parameter window
% params_m=['Model ',regexprep(fname_,'_','\\_')];

%partext=strvcat(params_m,params_oil,params_rig,params_rig_s,params_irr,params_irr_s,params_adj,params_elast,params_utl);
partext='';
% AND DON'T GO BELOW
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %


% AUXILIARY TO OPLOT4A

SS(lgx_orig_ord_,lgx_orig_ord_)=Sigma_e_+1e-14*eye(size(Sigma_e_,1));
slab(lgx_orig_ord_,:) = lgx_;

n = size(var_list_,1);
if n == 0; n = endo_nbr; end; % lgy_;

% get active shocks out of var-cov matrix
ashks=[1:exo_nbr]';
ashks=ashks((diag(SS)>1e-13));

jj=[];
if verbose
    
if length(varargin)<3
    disp('Active shocks in your mod:')
    disp([num2str(ashks),(repmat(' = ',size(ashks,1),1)),slab(ashks,:)]);
    jj=input('Enter a shock index for oplot (ex [2,4]): ');
else
    jj=varargin{3};
end

    oplot4core;
else
    for jj=1:size(ashks,1)

        oplot4core;
    end %loop shocks
end %if verbose