save.m 5.53 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.

Stéphane Adjemian's avatar
Stéphane Adjemian committed
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');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
39 40
    fprintf(fid,'FREQ__ = %s;\n',num2str(frequency(o)));
    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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        for i=1:length(tagnames)
            fprintf(fid, 'TAGS__.%s = cell(%s, 1);\n', tagnames{i}, num2str(vobs(o)));
            for j=1:vobs(o)
                if ~isempty(o.tags.(tagnames{i}){j})
                    if ischar(o.tags.(tagnames{i}){j})
                        fprintf(fid, 'TAGS__.%s(%s) = {''%s''};\n', tagnames{i}, num2str(j), o.tags.(tagnames{i}){j});
                    elseif isnumeric(o.tags.(tagnames{i}){j}) && iscalar(o.tags.(tagnames{i}){j})
                        fprintf(fid, 'TAGS__.%s(%s) = {%s};\n', tagnames{i}, num2str(j), o.tags.(tagnames{i}){j});
                    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
    str = [];
Stéphane Adjemian's avatar
Stéphane Adjemian committed
107 108
    for v = 1:vobs(o)
        str = sprintf('%s %s = o.data(:,%s);', str, o.name{v}, num2str(v));
109 110
    end
    eval(str);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
111
    currentdirectorycontent = dir();
Stéphane Adjemian's avatar
Stéphane Adjemian committed
112 113
    if ismember([basename, '.mat'], {currentdirectorycontent.name})
        copyfile([basename, '.mat'], [basename, '.old.mat']);
114
    end
115
    save([basename '.mat'], 'INIT__', 'FREQ__', 'NAMES__', 'TEX__', 'OPS__', 'TAGS__', o.name{:});
116
  case 'csv'
Stéphane Adjemian's avatar
Stéphane Adjemian committed
117 118
    currentdirectorycontent = dir();
    if ismember([basename, '.csv'],{currentdirectorycontent.name})
119 120 121
        copyfile([basename, '.csv'],[basename, '.old.csv']);
    end
    fid = fopen([basename, '.csv'],'w');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
122
    fprintf(fid,',%s', o.name{:});
123
    fprintf(fid,'\n');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
124 125 126
    for t = 1:nobs(o)
        str = sprintf(', %15.8g',o.data(t,:));
        fprintf(fid, '%s%s\n',date2string(o.dates(t)), str);
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    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,[]);
141
%$    save(ts1,'ts1','csv');
142 143 144 145 146
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
147 148
%$ delete('ts1.csv');
%$
149 150 151 152 153 154 155 156 157 158 159 160 161
%$ 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,[]);
162
%$    save(ts1,'ts1','m');
163 164 165 166 167
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
168 169
%$ delete('ts1.m');
%$
170 171 172 173 174 175 176 177 178 179 180 181 182
%$ 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,[]);
183
%$    save(ts1,'ts1','mat');
184 185 186 187 188
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
189 190
%$ delete('ts1.mat');
%$
191 192 193 194 195 196 197 198 199 200
%$ 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'};
%$
201
%$ % Instantiate and save a time series object.
202 203
%$ try
%$    ts1 = dseries(A,[],A_name,[]);
204
%$    ts1.save;
205 206 207 208 209
%$    t = 1;
%$ catch
%$    t = 0;
%$ end
%$
210
%$ delete('dynare_series.mat');
211
%$
212 213
%$ T = all(t);
%@eof:4