save.m 5.39 KB
Newer Older
Stéphane Adjemian's avatar
Stéphane Adjemian committed
1
function save(o, basename, format) % --*-- Unitary tests --*--
2 3 4

% Saves a dseries object on disk.

5
% Copyright (C) 2013-2017 Dynare Team
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% 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 || isempty(format)
23
    format = 'mat';
24 25 26
end

if nargin<2 || isempty(basename)
27
    basename = 'dynare_series';
28 29 30 31
end

switch format
  case 'm'
Stéphane Adjemian's avatar
Stéphane Adjemian committed
32 33 34
    currentdirectorycontent = dir();
    if ismember([basename, '.m'],{currentdirectorycontent.name})
        copyfile([basename, '.m'],[basename, '.m.csv']);
35 36 37 38
    end
    fid = fopen([basename, '.m'],'w');
    fprintf(fid,'%% File created on %s.\n',datestr(now));
    fprintf(fid,'\n');
39
    fprintf(fid,'FREQ__ = %u;\n', frequency(o));
Stéphane Adjemian's avatar
Stéphane Adjemian committed
40
    fprintf(fid,'INIT__ = ''%s'';\n',date2string(firstdate(o)));
41 42
    fprintf(fid,'\n');
    fprintf(fid,'NAMES__ = {');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
43 44 45
    for i = 1:vobs(o)
        fprintf(fid,[ '''' o.name{i}  '''']);
        if i<vobs(o)
46 47 48 49 50
            fprintf(fid,'; ');
        end
    end
    fprintf(fid,'};\n');
    str = 'TEX__ = {';
Stéphane Adjemian's avatar
Stéphane Adjemian committed
51
    for i=1:vobs(o)-1
52 53 54
        str = [str, '''%s''; '];
    end
    str = [str, '''%s''};'];
Stéphane Adjemian's avatar
Stéphane Adjemian committed
55
    str = sprintf(str, o.tex{:});
56 57 58
    pattern = '(\w*)(\\\_)';
    str = regexprep(str, pattern, '$1\\\\_');
    fprintf(fid,str);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
59 60 61 62 63 64 65 66 67
    fprintf(fid,'\n');
    fprintf(fid,'OPS__ = {');
    for i = 1:vobs(o)
        if isempty(o.ops{i})
            fprintf(fid,[ '[]']);
        else
            fprintf(fid,[ '''' o.ops{i}  '''']);
        end
        if i<vobs(o)
68
            fprintf(fid,';');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
69 70 71
        end
    end
    fprintf(fid,'};\n\n');
72 73 74
    if ~isempty(fieldnames(o.tags))
        % User has defined tags on the variables.
        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 79 80
            for j=1:vobs(o)
                if ~isempty(o.tags.(tagnames{i}){j})
                    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 85 86 87 88 89 90 91 92
                    else
                        error('dseries::tags: Cannot save this type of tag!')
                    end
                end
            end
            fprintf(fid, '\n');
        end
        fprintf(fid,'\n\n');
    end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
93 94 95 96
    for v = 1:vobs(o)
        fprintf(fid,'%s = [\n', o.name{v});
        fprintf(fid,'%15.8g\n', o.data(1:end-1,v));
        fprintf(fid,'%15.8g];\n\n', o.data(end,v));
97 98 99
    end
    fclose(fid);
  case 'mat'
Stéphane Adjemian's avatar
Stéphane Adjemian committed
100 101 102 103 104
    FREQ__ = frequency(o);
    INIT__ = date2string(firstdate(o));
    NAMES__ = o.name;
    TEX__ = o.tex;
    OPS__ = o.ops;
105
    TAGS__ = o.tags;
106
    DATA__ = o.data;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
107
    currentdirectorycontent = dir();
Stéphane Adjemian's avatar
Stéphane Adjemian committed
108 109
    if ismember([basename, '.mat'], {currentdirectorycontent.name})
        copyfile([basename, '.mat'], [basename, '.old.mat']);
110
    end
111
    save([basename '.mat'], 'INIT__', 'FREQ__', 'NAMES__', 'TEX__', 'OPS__', 'TAGS__', 'DATA__');
112
  case 'csv'
Stéphane Adjemian's avatar
Stéphane Adjemian committed
113 114
    currentdirectorycontent = dir();
    if ismember([basename, '.csv'],{currentdirectorycontent.name})
115 116 117
        copyfile([basename, '.csv'],[basename, '.old.csv']);
    end
    fid = fopen([basename, '.csv'],'w');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
118
    fprintf(fid,',%s', o.name{:});
119
    fprintf(fid,'\n');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
120 121 122
    for t = 1:nobs(o)
        str = sprintf(', %15.8g',o.data(t,:));
        fprintf(fid, '%s%s\n',date2string(o.dates(t)), str);
123 124 125 126 127 128 129 130 131 132 133 134 135 136
    end
    fclose(fid);
end

%@test:1
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
137
%$    save(ts1,'ts1','csv');
138 139 140 141 142
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
143 144
%$ delete('ts1.csv');
%$
145 146 147 148 149 150 151 152 153 154 155 156 157
%$ T = all(t);
%@eof:1

%@test:2
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
158
%$    save(ts1,'ts1','m');
159 160 161 162 163
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
164 165
%$ delete('ts1.m');
%$
166 167 168 169 170 171 172 173 174 175 176 177 178
%$ T = all(t);
%@eof:2

%@test:3
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
179
%$    save(ts1,'ts1','mat');
180 181 182 183 184
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
185 186
%$ delete('ts1.mat');
%$
187 188 189 190 191 192 193 194 195 196
%$ T = all(t);
%@eof:3

%@test:4
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
197
%$ % Instantiate and save a time series object.
198 199
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
200
%$    ts1.save;
201 202 203 204 205
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
206
%$ delete('dynare_series.mat');
207
%$
208 209
%$ T = all(t);
%@eof:4