1function save(o, basename, format) % --*-- Unitary tests --*--
2
3% Saves a dseries object on disk.
4
5% Copyright (C) 2013-2017 Dynare Team
6%
7% This file is part of Dynare.
8%
9% Dynare is free software: you can redistribute it and/or modify
10% it under the terms of the GNU General Public License as published by
11% the Free Software Foundation, either version 3 of the License, or
12% (at your option) any later version.
13%
14% Dynare is distributed in the hope that it will be useful,
15% but WITHOUT ANY WARRANTY; without even the implied warranty of
16% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17% GNU General Public License for more details.
18%
19% You should have received a copy of the GNU General Public License
20% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
21
22if nargin<3 || isempty(format)
23    format = 'mat';
24end
25
26if nargin<2 || isempty(basename)
27    basename = 'dynare_series';
28end
29
30switch format
31  case 'm'
32    currentdirectorycontent = dir();
33    if ismember([basename, '.m'],{currentdirectorycontent.name})
34        copyfile([basename, '.m'],[basename, '.m.csv']);
35    end
36    fid = fopen([basename, '.m'],'w');
37    fprintf(fid,'%% File created on %s.\n',datestr(now));
38    fprintf(fid,'\n');
39    fprintf(fid,'FREQ__ = %u;\n', frequency(o));
40    fprintf(fid,'INIT__ = ''%s'';\n',date2string(firstdate(o)));
41    fprintf(fid,'\n');
42    fprintf(fid,'NAMES__ = {');
43    for i = 1:vobs(o)
44        fprintf(fid,[ '''' o.name{i}  '''']);
45        if i<vobs(o)
46            fprintf(fid,'; ');
47        end
48    end
49    fprintf(fid,'};\n');
50    str = 'TEX__ = {';
51    for i=1:vobs(o)-1
52        str = [str, '''%s''; '];
53    end
54    str = [str, '''%s''};'];
55    str = sprintf(str, o.tex{:});
56    pattern = '(\w*)(\\\_)';
57    str = regexprep(str, pattern, '$1\\\\_');
58    fprintf(fid,str);
59    fprintf(fid,'\n');
60    fprintf(fid,'OPS__ = {');
61    for i = 1:vobs(o)
62        if isempty(o.ops{i})
63            fprintf(fid,[ '[]']);
64        else
65            fprintf(fid,[ '''' o.ops{i}  '''']);
66        end
67        if i<vobs(o)
68            fprintf(fid,';');
69        end
70    end
71    fprintf(fid,'};\n\n');
72    if ~isempty(fieldnames(o.tags))
73        % User has defined tags on the variables.
74        tagnames = fieldnames(o.tags);
75        fprintf(fid, 'TAGS__ = struct();\n');
76        for i=1:length(tagnames)
77            fprintf(fid, 'TAGS__.%s = cell(%u, 1);\n', tagnames{i}, vobs(o));
78            for j=1:vobs(o)
79                if ~isempty(o.tags.(tagnames{i}){j})
80                    if ischar(o.tags.(tagnames{i}){j})
81                        fprintf(fid, 'TAGS__.%s(%u) = {''%s''};\n', tagnames{i}, j, o.tags.(tagnames{i}){j});
82                    elseif isnumeric(o.tags.(tagnames{i}){j}) && iscalar(o.tags.(tagnames{i}){j})
83                        fprintf(fid, 'TAGS__.%s(%u) = {%s};\n', tagnames{i}, j, o.tags.(tagnames{i}){j});
84                    else
85                        error('dseries::tags: Cannot save this type of tag!')
86                    end
87                end
88            end
89            fprintf(fid, '\n');
90        end
91        fprintf(fid,'\n\n');
92    end
93    for v = 1:vobs(o)
94        fprintf(fid,'%s = [\n', o.name{v});
95        fprintf(fid,'%15.8g\n', o.data(1:end-1,v));
96        fprintf(fid,'%15.8g];\n\n', o.data(end,v));
97    end
98    fclose(fid);
99  case 'mat'
100    FREQ__ = frequency(o);
101    INIT__ = date2string(firstdate(o));
102    NAMES__ = o.name;
103    TEX__ = o.tex;
104    OPS__ = o.ops;
105    TAGS__ = o.tags;
106    DATA__ = o.data;
107    currentdirectorycontent = dir();
108    if ismember([basename, '.mat'], {currentdirectorycontent.name})
109        copyfile([basename, '.mat'], [basename, '.old.mat']);
110    end
111    save([basename '.mat'], 'INIT__', 'FREQ__', 'NAMES__', 'TEX__', 'OPS__', 'TAGS__', 'DATA__');
112  case 'csv'
113    currentdirectorycontent = dir();
114    if ismember([basename, '.csv'],{currentdirectorycontent.name})
115        copyfile([basename, '.csv'],[basename, '.old.csv']);
116    end
117    fid = fopen([basename, '.csv'],'w');
118    fprintf(fid,',%s', o.name{:});
119    fprintf(fid,'\n');
120    for t = 1:nobs(o)
121        str = sprintf(', %15.8g',o.data(t,:));
122        fprintf(fid, '%s%s\n',date2string(o.dates(t)), str);
123    end
124    fclose(fid);
125end
126
127%@test:1
128%$ % Define a data set.
129%$ A = [transpose(1:10),2*transpose(1:10)];
130%$
131%$ % Define names
132%$ A_name = {'A1';'A2'};
133%$
134%$ % Instantiate a time series object.
135%$ try
136%$    ts1 = dseries(A,[],A_name,[]);
137%$    save(ts1,'ts1','csv');
138%$    t = 1;
139%$ catch
140%$    t = 0;
141%$ end
142%$
143%$ delete('ts1.csv');
144%$
145%$ T = all(t);
146%@eof:1
147
148%@test:2
149%$ % Define a data set.
150%$ A = [transpose(1:10),2*transpose(1:10)];
151%$
152%$ % Define names
153%$ A_name = {'A1';'A2'};
154%$
155%$ % Instantiate a time series object.
156%$ try
157%$    ts1 = dseries(A,[],A_name,[]);
158%$    save(ts1,'ts1','m');
159%$    t = 1;
160%$ catch
161%$    t = 0;
162%$ end
163%$
164%$ delete('ts1.m');
165%$
166%$ T = all(t);
167%@eof:2
168
169%@test:3
170%$ % Define a data set.
171%$ A = [transpose(1:10),2*transpose(1:10)];
172%$
173%$ % Define names
174%$ A_name = {'A1';'A2'};
175%$
176%$ % Instantiate a time series object.
177%$ try
178%$    ts1 = dseries(A,[],A_name,[]);
179%$    save(ts1,'ts1','mat');
180%$    t = 1;
181%$ catch
182%$    t = 0;
183%$ end
184%$
185%$ delete('ts1.mat');
186%$
187%$ T = all(t);
188%@eof:3
189
190%@test:4
191%$ % Define a data set.
192%$ A = [transpose(1:10),2*transpose(1:10)];
193%$
194%$ % Define names
195%$ A_name = {'A1';'A2'};
196%$
197%$ % Instantiate and save a time series object.
198%$ try
199%$    ts1 = dseries(A,[],A_name,[]);
200%$    ts1.save;
201%$    t = 1;
202%$ catch
203%$    t = 0;
204%$ end
205%$
206%$ delete('dynare_series.mat');
207%$
208%$ T = all(t);
209%@eof:4
210