diff --git a/matlab/@dynSeries/private/check_file_extension.m b/matlab/@dynSeries/private/check_file_extension.m new file mode 100644 index 0000000000000000000000000000000000000000..63d627f0b0f3428f57695ee9999d4dad48d3b8f8 --- /dev/null +++ b/matlab/@dynSeries/private/check_file_extension.m @@ -0,0 +1,26 @@ +function b = check_file_extension(file,type) + +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +remain = file; +while ~isempty(remain) + [ext, remain] = strtok(remain,'.'); +end +b = strcmp(ext,type); \ No newline at end of file diff --git a/matlab/@dynSeries/private/get_cells_id.m b/matlab/@dynSeries/private/get_cells_id.m new file mode 100644 index 0000000000000000000000000000000000000000..74a6f006de582acb03792ea496bb50a07927847f --- /dev/null +++ b/matlab/@dynSeries/private/get_cells_id.m @@ -0,0 +1,24 @@ +function [B,C] = get_cells_id(str,sep) + +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +sep_locations = transpose(strfind(str,sep)); +B = [1; sep_locations+1]; +C = [sep_locations-1; length(str)]; \ No newline at end of file diff --git a/matlab/@dynSeries/private/isint.m b/matlab/@dynSeries/private/isint.m new file mode 100644 index 0000000000000000000000000000000000000000..0338f2393441acfa79d58a1364a945da4abd95f8 --- /dev/null +++ b/matlab/@dynSeries/private/isint.m @@ -0,0 +1,21 @@ +function b = isint(a) +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +b = (floor(a)==a); \ No newline at end of file diff --git a/matlab/@dynSeries/private/ismonthly.m b/matlab/@dynSeries/private/ismonthly.m new file mode 100644 index 0000000000000000000000000000000000000000..28c9e0cfe3ba756d819e9f187b3f03df25ed98e5 --- /dev/null +++ b/matlab/@dynSeries/private/ismonthly.m @@ -0,0 +1,37 @@ +function b = ismonthly(date) + +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +[year, remain] = strtok(date,'M'); +if ~isint(str2num(year)) + b = 0; + return +end +[month, remain] = strtok(remain,'M'); +if ~isempty(remain) + b = 0; + return +end +month = str2num(month); +if ~isint(month) || month<1 || month>12 + b = 0; + return +end +b = 1; \ No newline at end of file diff --git a/matlab/@dynSeries/private/isquaterly.m b/matlab/@dynSeries/private/isquaterly.m new file mode 100644 index 0000000000000000000000000000000000000000..9310435da5f93bac7bfbf8941eada6b55989cc91 --- /dev/null +++ b/matlab/@dynSeries/private/isquaterly.m @@ -0,0 +1,37 @@ +function b = isquaterly(date) + +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +[year, remain] = strtok(date,'Q'); +if ~isint(str2num(year)) + b = 0; + return +end +[quarter, remain] = strtok(remain,'Q'); +if ~isempty(remain) + b = 0; + return +end +quarter = str2num(quarter); +if ~isint(quarter) || quarter<1 || quarter>4 + b = 0; + return +end +b = 1; \ No newline at end of file diff --git a/matlab/@dynSeries/private/isweekly.m b/matlab/@dynSeries/private/isweekly.m new file mode 100644 index 0000000000000000000000000000000000000000..266bd495844b7e59efaa22777de69dc8cd6ff0a5 --- /dev/null +++ b/matlab/@dynSeries/private/isweekly.m @@ -0,0 +1,37 @@ +function b = isweekly(date) + +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +[year, remain] = strtok(date,'W'); +if ~isint(str2num(year)) + b = 0; + return +end +[week, remain] = strtok(remain,'W'); +if ~isempty(remain) + b = 0; + return +end +week = str2num(week); +if ~isint(week) || week<1 || week>52 + b = 0; + return +end +b = 1; \ No newline at end of file diff --git a/matlab/@dynSeries/private/isyearly.m b/matlab/@dynSeries/private/isyearly.m new file mode 100644 index 0000000000000000000000000000000000000000..defe5901ccd253e4a39d5ad50dec2ed067ae81de --- /dev/null +++ b/matlab/@dynSeries/private/isyearly.m @@ -0,0 +1,41 @@ +function b = isyearly(date) + +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +year = str2num(date); +if ~isempty(year) + if isint(year) + b = 1; + else + b = 0; + end + return +else + [year, remain] = strtok(date,'Y'); + if ~isequal(remain,'Y') + b = 0; + return + end + if ~isint(str2num(year)) + b = 0; + return + end + b = 2; +end \ No newline at end of file diff --git a/matlab/@dynSeries/private/readcsv.m b/matlab/@dynSeries/private/readcsv.m new file mode 100644 index 0000000000000000000000000000000000000000..de5b7b480898719b15525d5cbfa880235883f9f8 --- /dev/null +++ b/matlab/@dynSeries/private/readcsv.m @@ -0,0 +1,135 @@ +function [list_of_variables, data, time] = readcsv(file, withtime, withnames, noemptycell) + +% Copyright (C) 2012 Dynare Team +% +% 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/>. + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +if ~withtime && ~withname && noemptycell + % Use matlab builtin routine! + data = csvread(file); +end + +if ~( isequal(withtime,0) || isequal(withtime,1) ) + error('readcsv:: Second input argument has to be equal to 1 or 0!') +end + +if ~( isequal(withnames,0) || isequal(withnames,1) ) + error('readcsv:: Third input argument has to be equal to 1 or 0!') +end + +% Output initialization +time = []; data = []; list_of_variables = []; + +% Check if file exists. +if check_file_extension(file,'csv') + try + fid = fopen(file,'r'); + catch + error(['readcsv: I can''t find file ' file '!']) + end +else + error('readcsv: Wrong file extension!') +end + +% bfile contains a vector of ascii codes. +bfile = fread(fid); + +% Close (csv) file. +fclose(fid); + +% Set newline code (ok for *nix, check for mac and windows) +if isunix + newline_code = 10; +else + error('readcsv:: Not implemented for your OS!') +end + +% Get the positions of the end-of-line code; +end_of_line_locations = find(bfile==newline_code); +tmp = find(bfile==newline_code); + +% Get the number of lines in the file. +ndx = length(tmp); + +% Create a cell of indices for each line. +b = [1; end_of_line_locations+1]; +c = [end_of_line_locations-1; length(bfile)+1]; +b = b(1:end-1); +c = c(1:end-1); + +linea = 1; + +if withnames + % Get the first line of the csv file (names of the variables). + linee = char(transpose(bfile(b(linea):c(linea)))); + % Get the content of the first line and determine the number of variables and their names. + [B,C] = get_cells_id(linee,','); + if withtime + B = B(2:end); + C = C(2:end); + end + list_of_variables = cell(length(B),1); + number_of_variables = length(list_of_variables); + for i=1:number_of_variables + list_of_variables(i) = {linee(B(i):C(i))}; + end + linea = linea+1; +end + +% Get following line (number 1 or 2 depending on withnames flag) +linee = char(transpose(bfile(b(linea):c(linea)))); +comma_locations = transpose(strfind(linee,',')); +B = 1; +C = comma_locations(1)-1; + +if withtime + tmp = linee(B:C); + % Check the dates formatting + if ~(isyearly(tmp) || isquaterly(tmp) || ismonthly(tmp) || isweekly(tmp)) + error('readcsv:: Formatting error. I can''t read the dates!') + end + if isyearly(tmp)==2 + % Remove the Y (gpm/iris date format) if necessary + tmp = { tmp(1:end-1) }; + end + initial_date = dynDate(tmp); + first = 2; +else + initial_date = dynDate(1); + first = 1; +end + +if ~withnames + number_of_variables = length(tmp)-withtime; +end + +% Initialization of matrix data. +data = zeros(ndx,number_of_variables); + +% Populate data. +for linea = 1+withnames:ndx + linee = char(transpose(bfile(b(linea):c(linea)))); + [B,C] = get_cells_id(linee,','); + for i=first:length(B) + if isequal(B(i),C(i)) + data(linea,i-withtime) = NaN; + else + data(linea,i-withtime) = str2double(linee(B(i):C(i))); + end + end +end \ No newline at end of file