1function ReshapeMatFiles(type, type2)
2% function ReshapeMatFiles(type, type2)
3% Reshapes and sorts (along the mcmc simulations) the mat files generated by DYNARE.
4% 4D-arrays are splitted along the first dimension.
5% 3D-arrays are splitted along the second dimension.
6%
7% INPUTS:
8%   type:            statistics type in the repertory:
9%                      dgse
10%                      irf_bvardsge
11%                      smooth
12%                      filter
13%                      error
14%                      innov
15%                      forcst
16%                      forcst1
17%   type2:           analysis type:
18%                      posterior
19%                      gsa
20%                      prior
21%
22% OUTPUTS:
23%    none
24%
25% SPECIAL REQUIREMENTS
26%    none
27
28% Copyright (C) 2003-2017 Dynare Team
29%
30% This file is part of Dynare.
31%
32% Dynare is free software: you can redistribute it and/or modify
33% it under the terms of the GNU General Public License as published by
34% the Free Software Foundation, either version 3 of the License, or
35% (at your option) any later version.
36%
37% Dynare is distributed in the hope that it will be useful,
38% but WITHOUT ANY WARRANTY; without even the implied warranty of
39% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40% GNU General Public License for more details.
41%
42% You should have received a copy of the GNU General Public License
43% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
44
45global M_ options_
46
47if nargin==1
48    MhDirectoryName = [ CheckPath('metropolis',M_.dname) filesep ];
49else
50    if strcmpi(type2,'posterior')
51        MhDirectoryName = [CheckPath('metropolis',M_.dname) filesep ];
52    elseif strcmpi(type2,'gsa')
53        if options_.opt_gsa.morris==1
54            MhDirectoryName = [CheckPath('gsa/screen',M_.dname) filesep ];
55        elseif options_.opt_gsa.morris==2
56            MhDirectoryName = [CheckPath('gsa/identif',M_.dname) filesep ];
57        elseif options_.opt_gsa.pprior
58            MhDirectoryName = [CheckPath(['gsa' filesep 'prior'],M_.dname) filesep ];
59        else
60            MhDirectoryName = [CheckPath(['gsa' filesep 'mc'],M_.dname) filesep ];
61        end
62    else
63        MhDirectoryName = [CheckPath('prior',M_.dname) filesep ];
64    end
65end
66switch type
67  case 'irf_dsge'
68    CAPtype  = 'IRF_DSGE';
69    TYPEsize = [ options_.irf , size(options_.varlist,1) , M_.exo_nbr ];
70    TYPEarray = 4;
71  case 'irf_bvardsge'
72    CAPtype  = 'IRF_BVARDSGE';
73    TYPEsize = [ options_.irf , length(options_.varobs) , M_.exo_nbr ];
74    TYPEarray = 4;
75  case 'smooth'
76    CAPtype  = 'SMOOTH';
77    TYPEsize = [ M_.endo_nbr , options_.nobs ];
78    TYPEarray = 3;
79  case 'filter'
80    CAPtype = 'FILTER';
81    TYPEsize = [ M_.endo_nbr , options_.nobs + 1 ];% TO BE CHECKED!
82    TYPEarray = 3;
83  case 'error'
84    CAPtype = 'ERROR';
85    TYPEsize = [ length(options_.varobs) , options_.nobs ];
86    TYPEarray = 3;
87  case 'innov'
88    CAPtype = 'INNOV';
89    TYPEsize = [ M_.exo_nbr , options_.nobs ];
90    TYPEarray = 3;
91  case 'forcst'
92    CAPtype = 'FORCST';
93    TYPEsize = [ M_.endo_nbr , options_.forecast ];
94    TYPEarray = 3;
95  case 'forcst1'
96    CAPtype = 'FORCST1';
97    TYPEsize = [ M_.endo_nbr , options_.forecast ];
98    TYPEarray = 3;
99  otherwise
100    disp('ReshapeMatFiles :: Unknown argument!')
101    return
102end
103
104TYPEfiles = dir([MhDirectoryName M_.fname '_' type '*.mat']);
105NumberOfTYPEfiles = length(TYPEfiles);
106B = options_.B;
107
108switch TYPEarray
109  case 4
110    if NumberOfTYPEfiles > 1
111        NumberOfPeriodsPerTYPEfiles = ceil(TYPEsize(1)/NumberOfTYPEfiles);
112        foffset = NumberOfTYPEfiles-floor(TYPEsize(1)/NumberOfPeriodsPerTYPEfiles);
113        reste = TYPEsize(1)-NumberOfPeriodsPerTYPEfiles*(NumberOfTYPEfiles-foffset);
114        idx = 0;
115        jdx = 0;
116        for f1=1:NumberOfTYPEfiles-foffset
117            eval(['STOCK_' CAPtype ' = zeros(NumberOfPeriodsPerTYPEfiles,TYPEsize(2),TYPEsize(3),B);'])
118            for f2 = 1:NumberOfTYPEfiles
119                load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
120                eval(['STOCK_' CAPtype '(:,:,1:+size(stock_' type ',3),idx+1:idx+size(stock_' type ',4))=stock_' ...
121                      type '(jdx+1:jdx+NumberOfPeriodsPerTYPEfiles,:,:,:);'])
122                eval(['idx = idx + size(stock_' type ',4);'])
123            end
124            %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',4);'])
125            save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(f1) '.mat'],['STOCK_' CAPtype]);
126            jdx = jdx + NumberOfPeriodsPerTYPEfiles;
127            idx = 0;
128        end
129        if reste
130            eval(['STOCK_' CAPtype ' = zeros(reste,TYPEsize(2),TYPEsize(3),B);'])
131            for f2 = 1:NumberOfTYPEfiles
132                load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
133                eval(['STOCK_' CAPtype '(:,:,:,idx+1:idx+size(stock_' type ',4))=stock_' type '(jdx+1:jdx+reste,:,:,:);'])
134                eval(['idx = idx + size(stock_' type ',4);'])
135            end
136            %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',4);'])
137            save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(NumberOfTYPEfiles-foffset+1) '.mat'],['STOCK_' CAPtype]);
138        end
139    else
140        load([MhDirectoryName M_.fname '_' type '1.mat']);
141        %eval(['STOCK_' CAPtype ' = sort(stock_' type ',4);'])
142        eval(['STOCK_' CAPtype ' = stock_' type ';'])
143        save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(1) '.mat'],['STOCK_' CAPtype ]);
144    end
145    % Original file format may be useful in some cases...
146    % for file = 1:NumberOfTYPEfiles
147    %  delete([MhDirectoryName M_.fname '_' type int2str(file) '.mat'])
148    % end
149  case 3
150    if NumberOfTYPEfiles>1
151        NumberOfPeriodsPerTYPEfiles = ceil( TYPEsize(2)/NumberOfTYPEfiles );
152        reste = TYPEsize(2)-NumberOfPeriodsPerTYPEfiles*(NumberOfTYPEfiles-1);
153        idx = 0;
154        jdx = 0;
155        for f1=1:NumberOfTYPEfiles-1
156            eval(['STOCK_' CAPtype ' = zeros(TYPEsize(1),NumberOfPeriodsPerTYPEfiles,B);'])
157            for f2 = 1:NumberOfTYPEfiles
158                load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
159                eval(['STOCK_' CAPtype '(:,:,idx+1:idx+size(stock_ ' type ',3))=stock_' type '(:,jdx+1:jdx+NumberOfPeriodsPerTYPEfiles,:);'])
160                eval(['idx = idx + size(stock_' type ',3);'])
161            end
162            %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',3);'])
163            save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(f1) '.mat'],['STOCK_' CAPtype]);
164            jdx = jdx + NumberOfPeriodsPerTYPEfiles;
165            idx = 0;
166        end
167        eval(['STOCK_' CAPtype ' = zeros(TYPEsize(1),reste,B);'])
168        for f2 = 1:NumberOfTYPEfiles
169            load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
170            eval(['STOCK_' CAPtype '(:,:,idx+1:idx+size(stock_' type ',3))=stock_' type '(:,jdx+1:jdx+reste,:);'])
171            eval(['idx = idx + size(stock_' type ',3);'])
172        end
173        %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',3);'])
174        save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(NumberOfTYPEfiles) '.mat'],['STOCK_' CAPtype]);
175    else
176        load([MhDirectoryName M_.fname '_' type '1.mat']);
177        %eval(['STOCK_' CAPtype ' = sort(stock_' type ',3);'])
178        eval(['STOCK_' CAPtype ' = stock_' type ';'])
179        save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(1) '.mat'],['STOCK_' CAPtype ]);
180    end
181    % Original file format may be useful in some cases...
182    % for file = 1:NumberOfTYPEfiles
183    %   delete([MhDirectoryName M_.fname '_' type  int2str(file) '.mat'])
184    % end
185end