Commit eee55d6e authored by Rafael Laboissiere's avatar Rafael Laboissiere

Update upstream source from tag 'upstream/1.0.4'

Update to upstream version '1.0.4'
with Debian dir 0e6f0902150e36dd1e926aef92e5fee40d77c7b6
parents ddd02460 ec22a8f1
/debian/files
/debian/octave-ncarray.debhelper.log
/debian/octave-ncarray.substvars
/debian/octave-ncarray/
/global-list
/local-list
Name: ncarray Name: ncarray
Version: 1.0.3 Version: 1.0.4
Date: 2014-05-04 Date: 2016-11-29
Author: Alexander Barth <barth.alexander@gmail.com> Author: Alexander Barth <barth.alexander@gmail.com>
Maintainer: Alexander Barth <barth.alexander@gmail.com> Maintainer: Alexander Barth <barth.alexander@gmail.com>
Title: ncarray Title: ncarray
......
Summary of important user-visible changes for ncarray 1.0.4:
------------------------------------------------------------
** Avoid obsolete strmatch function
** Use a tolerance in test script (bug #49391)
Summary of important user-visible changes for ncarray 1.0.3: Summary of important user-visible changes for ncarray 1.0.3:
------------------------------------------------------------ ------------------------------------------------------------
......
...@@ -32,7 +32,7 @@ end ...@@ -32,7 +32,7 @@ end
function x = funelem(x,nm) function x = funelem(x,nm)
% make sure x is not an ncArray % make sure x is not an ncArray
x = full(x); x = full(x);
mask = isnan(x) || isnan(nm); mask = isnan(x) | isnan(nm);
diff = zeros(size(x)); diff = zeros(size(x));
diff(mask) = x(mask) - nm(mask); diff(mask) = x(mask) - nm(mask);
x = {diff.^2, ~mask}; x = {diff.^2, ~mask};
......
...@@ -2,17 +2,20 @@ function B = subsref(self,idx) ...@@ -2,17 +2,20 @@ function B = subsref(self,idx)
% handle case with a single subscript % handle case with a single subscript
% for example CA(234) % for example CA(234)
if strcmp(idx.type,'()') && length(idx.subs) == 1 if strcmp(idx.type,'()') && length(idx.subs) == 1
% indices of elements in B % indices of elements in B
ind = idx.subs{1}; ind = idx.subs{1};
if strcmp(ind,':')
ind = [1:numel(self)]';
end
% output array % output array
B = zeros(size(ind)); B = zeros(size(ind));
B(:) = NaN; B(:) = NaN;
if self.overlap if self.overlap
% transform index to subscript % transform linear index ind to subscript subs
subs = cell(1,self.nd); subs = cell(1,self.nd);
[subs{:}] = ind2sub(size(self),ind); [subs{:}] = ind2sub(size(self),ind);
...@@ -26,22 +29,7 @@ if strcmp(idx.type,'()') && length(idx.subs) == 1 ...@@ -26,22 +29,7 @@ if strcmp(idx.type,'()') && length(idx.subs) == 1
for i=1:length(ind) for i=1:length(ind)
idxe.subs = mat2cell(subs(i,:),1,ones(1,self.nd)); idxe.subs = mat2cell(subs(i,:),1,ones(1,self.nd));
%B(i) = subsref_canonical(self,idxe); B(i) = subsref_canonical(self,idxe);
[idx_global,idx_local,sz] = idx_global_local_(self,idxe);
tmp = zeros(sz);
tmp(:) = NaN;
for j=1:self.na
% get subset from j-th array
subset = subsref(self.arrays{j},idx_local{j});
% set subset in global array B
tmp = subsasgn(tmp,idx_global{j},subset);
end
B(i) = tmp;
end end
else else
% assume all arrays does not overlap % assume all arrays does not overlap
......
...@@ -10,8 +10,13 @@ fprintf('Coordinates:\n') ...@@ -10,8 +10,13 @@ fprintf('Coordinates:\n')
for i = 1:length(c) for i = 1:length(c)
tmp = sprintf('%dx',size(c(i).val)); tmp = sprintf('%dx',size(c(i).val));
fprintf(' Name: "%s" standard name: "%s" size %s\n',... stdname = c(i).standard_name;
c(i).name,c(i).standard_name,tmp(1:end-1)); if isempty(stdname)
stdname = '<unset>';
end
fprintf(' Name: %15s; standard name: %25s; size %10s\n',...
c(i).name,stdname,tmp(1:end-1));
end end
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
% Load a subset of a variable based on range of coordiante variables. % Load a subset of a variable based on range of coordiante variables.
% The names of the coordinates (coord_name1, coord_name2,...) coorespond to the standard_name attribute. % The names of the coordinates (coord_name1, coord_name2,...) coorespond to the standard_name attribute.
% Only 1-dimensional coordinates are currently supported. % Only 1-dimensional coordinates are currently supported.
% Time units are converted to "datenum".
% %
% %
% Example % Example
...@@ -15,6 +16,18 @@ for i = 1:length(c) ...@@ -15,6 +16,18 @@ for i = 1:length(c)
c(i).v = full(c(i).val); c(i).v = full(c(i).val);
% per default take all data along a dimension % per default take all data along a dimension
c(i).index = ':'; c(i).index = ':';
% convert time units
if ~isempty(strfind(c(i).units,'since'))
[t0,f] = nctimeunits(c(i).units);
c(i).v = f*double(c(i).v) + t0;
end
% change vertical axis to positive up
if strcmp(c(i).positive,'down')
c(i).v = -double(c(i).v);
end
c(i).sub = c(i).v; c(i).sub = c(i).v;
end end
...@@ -38,7 +51,11 @@ for i = 1:2:length(varargin) ...@@ -38,7 +51,11 @@ for i = 1:2:length(varargin)
if numel(range) == 1 if numel(range) == 1
dist = abs(c(j).v - range); dist = abs(c(j).v - range);
[mindist,i] = min(dist); [mindist,i] = min(dist);
%i
%mindist %mindist
%c(j).v(i)
%datevec(c(j).v(i))
else else
i = find(range(1) < c(j).v & c(j).v < range(end)); i = find(range(1) < c(j).v & c(j).v < range(end));
i = min(i):max(i); i = min(i):max(i);
...@@ -49,6 +66,7 @@ for i = 1:2:length(varargin) ...@@ -49,6 +66,7 @@ for i = 1:2:length(varargin)
end end
idx = substruct('()',{c.index}); idx = substruct('()',{c.index});
%idx
data = subsref (self,idx); data = subsref (self,idx);
varargout = {data,c.sub}; varargout = {data,c.sub};
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
% Web: http://modb.oce.ulg.ac.be/mediawiki/index.php/ncArray % Web: http://modb.oce.ulg.ac.be/mediawiki/index.php/ncArray
% hidded constructor signature: % hidded constructor signature:
% data = ncArray(filename,varname) % data = ncArray(var,dims,coord);
% is used to create data with coordinate values by ncCatArray % is used to create data with coordinate values by ncCatArray
function retval = ncArray(varargin) function retval = ncArray(varargin)
......
...@@ -33,7 +33,6 @@ if length(idx) == 2 && strcmp(idx(2).type,'.') && strcmp(idx(2).subs,'coord') ...@@ -33,7 +33,6 @@ if length(idx) == 2 && strcmp(idx(2).type,'.') && strcmp(idx(2).subs,'coord')
j = sort(j); j = sort(j);
idx_c.type = '()'; idx_c.type = '()';
idx_c.subs = idx(1).subs(j); idx_c.subs = idx(1).subs(j);
varargout{i} = subsref(self.coord(i).val,idx_c); varargout{i} = subsref(self.coord(i).val,idx_c);
end end
else else
......
...@@ -37,14 +37,19 @@ else ...@@ -37,14 +37,19 @@ else
if length(tmp) == 1 if length(tmp) == 1
start(i) = tmp; start(i) = tmp;
else else
% check if indexes are at regular intervals
test = tmp(1):tmp(2)-tmp(1):tmp(end); test = tmp(1):tmp(2)-tmp(1):tmp(end);
if size(tmp,2) == 1
% tmp is a row vector, make test also a row vector
test = test';
end
if all(tmp == test) if all(tmp == test)
start(i) = tmp(1); start(i) = tmp(1);
stride(i) = tmp(2)-tmp(1); stride(i) = tmp(2)-tmp(1);
count(i) = (tmp(end)-tmp(1))/stride(i) +1; count(i) = (tmp(end)-tmp(1))/stride(i) +1;
else else
error('indeces'); error('indeces not at regular intervals');
end end
end end
end end
......
...@@ -52,7 +52,10 @@ if strcmp(idx.type,'()') ...@@ -52,7 +52,10 @@ if strcmp(idx.type,'()')
elseif strcmp(idx.type,'.') elseif strcmp(idx.type,'.')
% load attribute % load attribute
name = idx.subs; name = idx.subs;
index = strmatch(name,{self.vinfo.Attributes(:).Name}); % strmatch is obsolete
% index = strmatch(name,{self.vinfo.Attributes(:).Name});
index = find(strcmp(name,{self.vinfo.Attributes(:).Name}));
if isempty(index) if isempty(index)
error('variable %s has no attribute called %s',self.varname,name); error('variable %s has no attribute called %s',self.varname,name);
......
% Decompress a file using a cache. % Decompress a file using a cache.
% %
% [fname,success]=cached_decompress(filename) % [fname,success] = cached_decompress(filename)
% %
% Input: % Input:
% filename: name of the file which is possibly compressed % filename: name of the file which is possibly compressed
...@@ -14,15 +14,21 @@ ...@@ -14,15 +14,21 @@
% CACHED_DECOMPRESS_LOG_FID (default 1): file id for log message % CACHED_DECOMPRESS_LOG_FID (default 1): file id for log message
% CACHED_DECOMPRESS_MAX_SIZE (default 1e10): maximum size of cache in bytes. % CACHED_DECOMPRESS_MAX_SIZE (default 1e10): maximum size of cache in bytes.
% Alexander Barth, 2012-06-13 function fname = cached_decompress(url)
%
function [fname]=cached_decompress(url)
global CACHED_DECOMPRESS_DIR global CACHED_DECOMPRESS_DIR
global CACHED_DECOMPRESS_LOG_FID global CACHED_DECOMPRESS_LOG_FID
global CACHED_DECOMPRESS_MAX_SIZE global CACHED_DECOMPRESS_MAX_SIZE
if startswith(url,'http:') || ...
~(endswith(url,'.gz') || endswith(url,'.bz2') || endswith(url,'.xz'))
% opendap url or not compressed file
fname = url;
return
end
cache_dir = CACHED_DECOMPRESS_DIR; cache_dir = CACHED_DECOMPRESS_DIR;
if isempty(cache_dir) if isempty(cache_dir)
...@@ -32,13 +38,6 @@ if isempty(cache_dir) ...@@ -32,13 +38,6 @@ if isempty(cache_dir)
fprintf('creating directory %s for temporary files.\n',cache_dir); fprintf('creating directory %s for temporary files.\n',cache_dir);
end end
if beginswith(url,'http:') || ...
~(endswith(url,'.gz') || endswith(url,'.bz2') || endswith(url,'.xz'))
% opendap url or not compressed file
fname = url;
return
end
%if exist(cache_dir,'dir') ~= 7 %if exist(cache_dir,'dir') ~= 7
% error(['cache directory for compressed files does not exist. '... % error(['cache directory for compressed files does not exist. '...
% 'Please create the directory %s or change le value of the '... % 'Please create the directory %s or change le value of the '...
...@@ -47,10 +46,10 @@ end ...@@ -47,10 +46,10 @@ end
% where to print logs? default to screen % where to print logs? default to screen
fid=CACHED_DECOMPRESS_LOG_FID; fid = CACHED_DECOMPRESS_LOG_FID;
if (isempty(fid)) if (isempty(fid))
fid=1; fid = 1;
end end
% form filename for cache % form filename for cache
...@@ -58,22 +57,35 @@ end ...@@ -58,22 +57,35 @@ end
fname = url; fname = url;
fname = strrep(fname,'/','_SLASH_'); fname = strrep(fname,'/','_SLASH_');
fname = strrep(fname,'*','_STAR_'); fname = strrep(fname,'*','_STAR_');
fname = strrep(fname,'\','_BSLASH_');
fname = strrep(fname,':','_COLON_');
fname = fullfile(cache_dir,fname); fname = fullfile(cache_dir,fname);
% test if in cache % test if in cache
if exist(fname,'file') ~= 2 if exist(fname,'file') ~= 2
if endswith(url,'.gz') if endswith(url,'.gz')
syscmd('gunzip --stdout "%s" > "%s"',url,fname); cmd = 'gunzip --stdout -';
elseif endswith(url,'.bz2') elseif endswith(url,'.bz2')
syscmd('bunzip2 --stdout "%s" > "%s"',url,fname); cmd = 'bunzip2 --stdout -';
elseif endswith(url,'.xz')
cmd = 'unxz --stdout -';
else else
syscmd('unxz --stdout "%s" > "%s"',url,fname); cmd = 'cat';
end end
if startswith(url,'ftp://')
syscmd('curl --silent "%s" | %s > "%s"',url,cmd,fname);
else
syscmd('%s < "%s" > "%s"',cmd,url,fname);
end
else else
% fprintf(fid,'retrieve from cache %s\n',url); % fprintf(fid,'retrieve from cache %s\n',url);
end end
% check cache size % check cache size
d=dir(cache_dir); d=dir(cache_dir);
...@@ -107,23 +119,23 @@ if (cashe_size > max_cache_size) ...@@ -107,23 +119,23 @@ if (cashe_size > max_cache_size)
end end
end end
function t = beginswith(s,pre)
if length(pre) <= length(s) function t = startswith(s,ext)
t = strcmp(s(1:length(pre)),pre);
else if length(ext) <= length(s)
t = strcmp(s(1:length(ext)),ext);
else
t = 0; t = 0;
end end
end end
function t = endswith(s,ext) function t = endswith(s,ext)
if length(ext) <= length(s) if length(ext) <= length(s)
t = strcmp(s(end-length(ext)+1:end),ext); t = strcmp(s(end-length(ext)+1:end),ext);
else else
t = 0; t = 0;
end end
end end
...@@ -141,7 +153,7 @@ end ...@@ -141,7 +153,7 @@ end
end end
% Copyright (C) 2012-2013 Alexander Barth <barth.alexander@gmail.com> % Copyright (C) 2012-2013, 2015 Alexander Barth <barth.alexander@gmail.com>
% %
% This program is free software; you can redistribute it and/or modify % 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 % it under the terms of the GNU General Public License as published by
......
...@@ -80,6 +80,11 @@ elseif ischar(pattern) ...@@ -80,6 +80,11 @@ elseif ischar(pattern)
'http://www.mathworks.com/matlabcentral/fileexchange/19550']); 'http://www.mathworks.com/matlabcentral/fileexchange/19550']);
end end
end end
if isempty(filenames)
error('ncArray:nomatch','no file found matching %s',pattern);
end
elseif isa(pattern, 'function_handle') elseif isa(pattern, 'function_handle')
filenames = cell(1,length(range)); filenames = cell(1,length(range));
...@@ -120,12 +125,24 @@ if (dim > length(dims)) && ~isempty(coord) ...@@ -120,12 +125,24 @@ if (dim > length(dims)) && ~isempty(coord)
dims{dim} = catdimname; dims{dim} = catdimname;
coord(dim).dims = {catdimname}; coord(dim).dims = {catdimname};
coord(dim).val = range; coord(dim).val = range;
coord(dim).name = catdimname;
end end
for i=1:length(coord) for i=1:length(coord)
%test if value is already defined, if yes do nothing
if isfield(coord(i),'val')
if ~isempty(coord(i).val)
continue
end
end
% the number of the dimension might be different % the number of the dimension might be different
% find in coord(i).dims the index of the dimension called dims{dim} % find in coord(i).dims the index of the dimension called dims{dim}
% for example we concatenate over time, then two situations can arrise
% the coordinate variable lon can dependent on time (dimc is not empty)
% or it does not depdent on time (dimc is empty)
dimc = find(strcmp(coord(i).dims,dims{dim})); dimc = find(strcmp(coord(i).dims,dims{dim}));
if isempty(dimc) if isempty(dimc)
...@@ -133,12 +150,13 @@ for i=1:length(coord) ...@@ -133,12 +150,13 @@ for i=1:length(coord)
coord(i).val = ncBaseArray(filenames{1},coord(i).name,'vinfo',vinfo); coord(i).val = ncBaseArray(filenames{1},coord(i).name,'vinfo',vinfo);
else else
% coordinates do also depend on the dimension over which we concatenate % coordinates do also depend on the dimension over which we concatenate
%i,coord(i).name,dimc,dims{dim}
coord(i).val = arr(dimc,filenames,coord(i).name,finfos); coord(i).val = arr(dimc,filenames,coord(i).name,finfos);
end end
if dim > length(coord(i).dims) %if dim > length(coord(i).dims)
coord(i).dims{dim} = catdimname; % coord(i).dims{dim} = catdimname;
end %end
end end
data = ncArray(var,dims,coord); data = ncArray(var,dims,coord);
......
% Tutorial for using ncArray
% It is advised to run this script in an empty directory.
% It will delete and overwrite files named file1.nc, file2.nc and file3.nc.
% size of the example data (2x3)
n = 3;
m = 2;
% create 3 files (file1.nc, file2.nc,...) with a 2x3 variable called SST
data = zeros(n,m);
disp('create example files: file1.nc, file2.nc, file3.nc')
for i = 1:3
data(:) = i;
files{i} = sprintf('file%d.nc',i);
delete(files{i});
ncarray_example_file(files{i},data);
end
% Using ncArray
SST = ncArray('file1.nc','SST');
disp('load the entire file')
data = SST(:,:,:);
disp('get the attribute units')
units = SST.units;
disp('load a particular value');
data = SST(3,2,1);
% Using ncCatArray
disp('concatenate the files over the 3rd dimension (here time)')
SST = ncCatArray(3,{'file1.nc','file2.nc','file3.nc'},'SST');
% or just
% SST = ncCatArray(3,'file*.nc','SST');
disp('load all 3 files');
data = SST(:,:,:);
disp('load a particular value (1,2,1) of the 3rd file');
data = SST(1,2,3);
function ncarray_example_file(filename,data) % create an example NetCDF file with the name filename and given data
if ~isempty(which('nccreate')) && ~isempty(which('ncwriteatt')) && ... function ncarray_example_file(filename,data)
~isempty(which('ncwrite'))
% use matlab netcdf high level interface
%dtype = 'single';
dtype = 'double'; dtype = 'double';
sz = size(data);
% Variables % Variables
nccreate(filename,'lon','Format','classic','Datatype',dtype,... nccreate(filename,'lon','Format','classic','Datatype',dtype,...
'Dimensions',{'x',220, 'y',144}); 'Dimensions',{'x',sz(1), 'y',sz(2)});
ncwriteatt(filename,'lon','long_name','Longitude') ncwriteatt(filename,'lon','long_name','Longitude')
ncwriteatt(filename,'lon','units','degrees_east') ncwriteatt(filename,'lon','units','degrees_east')
nccreate(filename,'lat','Datatype',dtype,'Dimensions',{'x',220, 'y',144}); nccreate(filename,'lat','Datatype',dtype,'Dimensions',{'x',sz(1), 'y',sz(2)});
ncwriteatt(filename,'lat','long_name','Latitude') ncwriteatt(filename,'lat','long_name','Latitude')
ncwriteatt(filename,'lat','units','degrees_north') ncwriteatt(filename,'lat','units','degrees_north')
...@@ -22,7 +20,7 @@ if ~isempty(which('nccreate')) && ~isempty(which('ncwriteatt')) && ... ...@@ -22,7 +20,7 @@ if ~isempty(which('nccreate')) && ~isempty(which('ncwriteatt')) && ...
ncwriteatt(filename,'time','units','days since 1858-11-17 00:00:00 GMT') ncwriteatt(filename,'time','units','days since 1858-11-17 00:00:00 GMT')
nccreate(filename,'SST','Datatype',dtype,'Dimensions',... nccreate(filename,'SST','Datatype',dtype,'Dimensions',...
{'x',220, 'y',144, 'time',1}); {'x',sz(1), 'y',sz(2), 'time',1});
ncwriteatt(filename,'SST','missing_value',single(9999)) ncwriteatt(filename,'SST','missing_value',single(9999))
ncwriteatt(filename,'SST','_FillValue',single(9999)) ncwriteatt(filename,'SST','_FillValue',single(9999))
ncwriteatt(filename,'SST','units','degC') ncwriteatt(filename,'SST','units','degC')
...@@ -30,56 +28,19 @@ if ~isempty(which('nccreate')) && ~isempty(which('ncwriteatt')) && ... ...@@ -30,56 +28,19 @@ if ~isempty(which('nccreate')) && ~isempty(which('ncwriteatt')) && ...
ncwriteatt(filename,'SST','coordinates','lat lon') ncwriteatt(filename,'SST','coordinates','lat lon')
ncwrite(filename,'SST',data); ncwrite(filename,'SST',data);
else
% use octcdf interface
nc = netcdf(filename,'c');
% dimensions
nc('x') = size(data,1);
nc('y') = size(data,2);
nc('time') = size(data,3);
% variables
nc{'lon'} = ncdouble('y','x'); % 31680 elements
nc{'lon'}.long_name = ncchar('Longitude');
nc{'lon'}.units = ncchar('degrees_east');
nc{'lat'} = ncdouble('y','x'); % 31680 elements
nc{'lat'}.long_name = ncchar('Latitude');
nc{'lat'}.units = ncchar('degrees_north');
nc{'time'} = ncdouble('time'); % 1 elements
nc{'time'}.long_name = ncchar('Time');
nc{'time'}.units = ncchar('days since 1858-11-17 00:00:00 GMT');
nc{'SST'} = ncdouble('time','y','x'); % 31680 elements
nc{'SST'}.missing_value = ncdouble(9999);
nc{'SST'}.FillValue_ = ncdouble(9999);
nc{'SST'}.units = ncchar('degC');
nc{'SST'}.long_name = ncchar('Sea Surface Temperature');
nc{'SST'}.coordinates = ncchar('lat lon');
% global attributes
nc{'SST'}(:) = permute(data,[3 2 1]);
close(nc)
end
% Copyright (C) 2012,2013 Alexander Barth <barth.alexander@gmail.com> % Copyright (C) 2012,2013,2015 Alexander Barth <barth.alexander@gmail.com>
% %
% This program is free software; you can redistribute it and/or modify % 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 % it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or % the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version. % (at your option) any later version.
% %
% This program is distributed in the hope that it will be useful, % This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of % but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details. % GNU General Public License for more details.
% %
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with this program; If not, see <http://www.gnu.org/licenses/>. % along with this program; If not, see <http://www.gnu.org/licenses/>.
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
% coord is an empty structure if no coordinate information have been % coord is an empty structure if no coordinate information have been
% found. % found.
% TODO: use a predictable order for coord:
% lon, lat, depth, time, ensemble,...
% Author: Alexander Barth (barth.alexander@gmail.com) % Author: Alexander Barth (barth.alexander@gmail.com)
function [dims,coord] = nccoord(filename,varname) function [dims,coord] = nccoord(filename,varname)
...@@ -32,7 +35,8 @@ vinfo = finfo.Variables(index); ...@@ -32,7 +35,8 @@ vinfo = finfo.Variables(index);
dims = {vinfo.Dimensions(:).Name}; dims = {vinfo.Dimensions(:).Name};
% create empty coord array with the fields name and dims % create empty coord array with the fields name and dims
coord = struct('name',{},'dims',{},'standard_name',{},'units',{}); coord = struct('name',{},'dims',{},'standard_name',{},...
'units',{},'positive',{});