Commit c6aa7907 authored by Rafael Laboissiere's avatar Rafael Laboissiere

Imported Upstream version 1.0.3

parents
This diff is collapsed.
Name: ncarray
Version: 1.0.3
Date: 2014-05-04
Author: Alexander Barth <barth.alexander@gmail.com>
Maintainer: Alexander Barth <barth.alexander@gmail.com>
Title: ncarray
Description: Access a single or a collection of NetCDF files as a multi-dimensional array
Categories: IO
Depends: octave (>= 3.4.0), netcdf (>= 1.0.2), statistics (>= 1.0.6)
License: GPLv2+
Url: http://modb.oce.ulg.ac.be/mediawiki/index.php/ncArray
ncarray >> ncArray
ncArray
ncArray
ncCatArray
nccoord
nctimeunits
ncreadtime
cached_decompress
@ncArray/coord
@ncArray/ncArray
@ncArray/subsasgn
@ncArray/subsref
ncBaseArray
@BaseArray/BaseArray
@BaseArray/end
@BaseArray/full
@BaseArray/isnumeric
@BaseArray/max
@BaseArray/mean
@BaseArray/min
@BaseArray/moment
@BaseArray/numel
@BaseArray/prod
@BaseArray/reduce
@BaseArray/size
@BaseArray/std
@BaseArray/sum
@BaseArray/sumsq
@BaseArray/var
Test script
test_ncarray
\ No newline at end of file
Summary of important user-visible changes for ncarray 1.0.3:
------------------------------------------------------------
** use lower-case package name as upper-case latters in the package name can
cause troubles wiht pkg.
** new functions nctimeunits and ncreadtime for handling time variables.
Summary of important user-visible changes for ncArray 1.0.2:
------------------------------------------------------------
** new function: nansum, nanvar, nanstd: mean, variance, standard deviation
ignoring NaNs
** ncarray nows depends on package netcdf (instead of octcdf)
** Add missing dependency to statistics
** Avoid obsolete isequalwithequalnans functions
Summary of important user-visible changes for ncArray 1.0.1:
------------------------------------------------------------
** new function: nanmean: mean ignoring NaNs
** new function: load: load data based on a coordinate range or a single
value (only 1-dimensional coordinates are currently supported)
** CACHED_DECOMPRESS_DIR defaults now to a directory inside the system temporary
directory (/tmp on Linux)
** add support for xz compression
** optimization of assignement operator
** add units and standard_name in nccoord
Summary of important user-visible changes for ncArray 1.0.0:
------------------------------------------------------------
** Initial release
% Create a BaseArray.
% BA = BaseArray(SZ)
% Create a BaseArray of size SZ. BaseArray is an abstract class.
% Derived classes should implement the methods subsref and subsasgn.
% BaseArray implements several reduction methods such as sum, prod and mean.
% SZ should have least two elements.
%
function retval = BaseArray(sz)
self.sz = sz;
retval = class(self,'BaseArray');
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Return last index along a dimension.
function e = end(self,k,n)
e = size(self,k);
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Make full (dense) array.
function F = full(self);
n = length(self.sz);
idx.type = '()';
for i=1:n
idx.subs{i} = ':';
end
F = subsref(self,idx);
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Test if array is numeric.
% isn = isnumeric(self)
function isn = isnumeric(self)
isn = true;
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% len = length(A)
% Return the "length" of the object A. For matrix objects, the
% length is the number of rows or columns, whichever is greater.
function len = length(self)
len = max(size(self));
% Copyright (C) 2013 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the maximum.
% S = max (X, [], DIM)
% Compute the maximum along dimension DIM.
function s = max(self,B,varargin)
assert(isempty(B))
funred = @max;
funelem = @(x) x;
s = reduce(self,funred,funelem,varargin{:});
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the mean.
% s = mean (X, DIM)
% Compute the mean along dimension DIM.
function s = mean(self,varargin)
funred = @plus;
funelem = @(x) x;
[s,n] = reduce(self,funred,funelem,varargin{:});
if isempty(s)
s = 0;
else
s = s/n;
end
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the minimum.
% S = min (X, [], DIM)
% Compute the minimum along dimension DIM.
function s = min(self,B,varargin)
assert(isempty(B))
funred = @min;
funelem = @(x) x;
s = reduce(self,funred,funelem,varargin{:});
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the central moment.
% M = moment (X, ORDER, DIM)
% compute the central moment of the given order along dimension DIM.
function s = moment(self,order,varargin)
m = mean(self,varargin{:});
funred = @plus;
funelem = @(x) (x-m).^order;
[s,n] = reduce(self,funred,funelem,varargin{:});
if isempty(s)
s = NaN;
else
s = s/n;
end
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the mean (ignoring NaNs).
% s = mean (X, DIM)
% Compute the mean along dimension DIM.
function [s,total,count] = nanmean(self,varargin)
% s will be a cell element containing
% {the sum, the count of elements different from NaN}
% this is necessary to avoid 2 calls to reduce
[s,n] = reduce(self,...
@(x,y) funred(x,y),...
@(x) funelem(x),...
varargin{:});
if isempty(s)
s = 0;
else
total = s{1};
count = s{2};
s = NaN*zeros(size(total));
nonz = count ~= 0;
s(nonz) = total(nonz) ./ count(nonz);
end
end
function x = funelem(x)
% make sure x is not an ncArray
x = full(x);
m = isnan(x);
x(m) = 0;
x = {x,~m};
end
function s = funred(x,y)
s = {x{1} + y{1}, x{2} + y{2}} ;
end
% Copyright (C) 2013 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the standard deviation (ignoring NaNs).
% S = nanstd (X, OPT, DIM)
% Compute the standard deviation along dimension DIM. NaNs are treated as absent
% values.
% If OPT is equal to 1, then the standard deviation is bias-corrected.
function s = nanstd(self,varargin)
s = sqrt(nanvar(self,varargin{:}));
% Copyright (C) 2013 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the sum (ignoring NaNs).
% [s,count] = sum (X, DIM)
% Compute the sum along dimension DIM.
% The variable s is the sum and count the number of values summed.
function [total,count] = nansum(self,varargin)
% s will be a cell element containing
% {the sum, the count of elements different from NaN}
% this is necessary to avoid 2 calls to reduce
[s,n] = reduce(self,...
@(x,y) funred(x,y),...
@(x) funelem(x),...
varargin{:});
if isempty(s)
s = 0;
else
total = s{1};
count = s{2};
end
end
function x = funelem(x)
% make sure x is not an ncArray
x = full(x);
m = isnan(x);
x(m) = 0;
x = {x,~m};
end
function s = funred(x,y)
s = {x{1} + y{1}, x{2} + y{2}} ;
end
% Copyright (C) 2013 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the variance (ignoring NaNs).
% V = var (X, OPT, DIM)
% Compute the variance along dimension DIM.
% If OPT is equal to 1, then the variance is bias-corrected.
function s = nanvar(self,opt,varargin)
if nargin == 1
opt = 0;
elseif isempty(opt)
opt = 0;
end
nm = nanmean(self,varargin{:});
[s,n] = reduce(self,@funred, ...
@(x) funelem(x,nm),varargin{:});
if isempty(s)
s = 0;
else
total = s{1};
count = s{2};
if opt == 0
s = total./(count-1);
else
s = total./count;
end
end
end
function x = funelem(x,nm)
% make sure x is not an ncArray
x = full(x);
mask = isnan(x) || isnan(nm);
diff = zeros(size(x));
diff(mask) = x(mask) - nm(mask);
x = {diff.^2, ~mask};
end
function s = funred(x,y)
s = {x{1} + y{1}, x{2} + y{2}} ;
end
% Copyright (C) 2013 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Number of elements.
% n = numel(A)
function n = numel(self)
n = prod(self.sz);
% Compute the product.
% P = prod (X, DIM)
% Compute the product of all elements along dimension DIM.
function s = prod(self,varargin)
funred = @times;
funelem = @(x) x;
s = reduce(self,funred,funelem,varargin{:});
if isempty(s)
s = 1;
end
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Reduce array using callback fundtions.
% [S,N] = reduce(SELF,FUNRED,FUNELEM,DIM)
% Reduce array using the function FUNRED applied to all elements
% after the function FUNELEM was applied along dimension DIM.
function [s,n] = reduce(self,funred,funelem,dim)
sz = size(self);
if nargin == 3
dim = find(sz ~= 1,1);
if isempty(dim)
dim = 1;
end
end
idx.type = '()';
nd = length(sz);
idx.subs = cell(1,nd);
for i=1:nd
idx.subs{i} = ':';
end
n = size(self,dim);
if n == 0
s = [];
else
idx.subs{dim} = 1;
s = funelem(subsref(self,idx));
for i=2:n
idx.subs{dim} = i;
s = funred(s,funelem(subsref(self,idx)));
end
end
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Size of array.
% sz = size(self,dim)
function sz = size(self,dim)
sz = self.sz;
if length(sz) == 1
sz = [sz 1];
end
if nargin == 2
if dim > length(sz)
sz = 1;
else
sz = sz(dim);
end
end
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the standard deviation.
% S = std (X, OPT, DIM)
function s = std(self,varargin)
s = sqrt(var(self,varargin{:}));
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
% Compute the sum.
% S = sum (X, DIM)
% Compute the sum along dimension DIM.
function s = sum(self,varargin)
funred = @plus;
funelem = @(x) x;
s = reduce(self,funred,funelem,varargin{:});
if isempty(s)
s = 0;
end
% Copyright (C) 2012 Alexander Barth <barth.alexander@gmail.com>
%
% This program 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 2 of the License, or
% (at your option) any later version.
%
% This program 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 this program; If not, see <http://www.gnu.org/licenses/>.