Commit ec22a8f1 by Rafael Laboissiere

### New upstream version 1.0.4

parent c6aa7907
 Name: ncarray Version: 1.0.3 Date: 2014-05-04 Version: 1.0.4 Date: 2016-11-29 Author: Alexander Barth Maintainer: Alexander Barth 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: ------------------------------------------------------------ ... ...
 ... ... @@ -32,7 +32,7 @@ end function x = funelem(x,nm) % make sure x is not an ncArray x = full(x); mask = isnan(x) || isnan(nm); mask = isnan(x) | isnan(nm); diff = zeros(size(x)); diff(mask) = x(mask) - nm(mask); x = {diff.^2, ~mask}; ... ...
 ... ... @@ -2,17 +2,20 @@ function B = subsref(self,idx) % handle case with a single subscript % for example CA(234) if strcmp(idx.type,'()') && length(idx.subs) == 1 % indices of elements in B ind = idx.subs{1}; if strcmp(ind,':') ind = [1:numel(self)]'; end % output array B = zeros(size(ind)); B(:) = NaN; if self.overlap % transform index to subscript % transform linear index ind to subscript subs subs = cell(1,self.nd); [subs{:}] = ind2sub(size(self),ind); ... ... @@ -26,22 +29,7 @@ if strcmp(idx.type,'()') && length(idx.subs) == 1 for i=1:length(ind) idxe.subs = mat2cell(subs(i,:),1,ones(1,self.nd)); %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; B(i) = subsref_canonical(self,idxe); end else % assume all arrays does not overlap ... ...
 ... ... @@ -10,8 +10,13 @@ fprintf('Coordinates:\n') for i = 1:length(c) tmp = sprintf('%dx',size(c(i).val)); fprintf(' Name: "%s" standard name: "%s" size %s\n',... c(i).name,c(i).standard_name,tmp(1:end-1)); stdname = c(i).standard_name; if isempty(stdname) stdname = ''; end fprintf(' Name: %15s; standard name: %25s; size %10s\n',... c(i).name,stdname,tmp(1:end-1)); end ... ...
 ... ... @@ -2,6 +2,7 @@ % 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. % Only 1-dimensional coordinates are currently supported. % Time units are converted to "datenum". % % % Example ... ... @@ -15,6 +16,18 @@ for i = 1:length(c) c(i).v = full(c(i).val); % per default take all data along a dimension 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; end ... ... @@ -38,7 +51,11 @@ for i = 1:2:length(varargin) if numel(range) == 1 dist = abs(c(j).v - range); [mindist,i] = min(dist); %i %mindist %c(j).v(i) %datevec(c(j).v(i)) else i = find(range(1) < c(j).v & c(j).v < range(end)); i = min(i):max(i); ... ... @@ -49,6 +66,7 @@ for i = 1:2:length(varargin) end idx = substruct('()',{c.index}); %idx data = subsref (self,idx); varargout = {data,c.sub}; ... ...
 ... ... @@ -38,7 +38,7 @@ % Web: http://modb.oce.ulg.ac.be/mediawiki/index.php/ncArray % hidded constructor signature: % data = ncArray(filename,varname) % data = ncArray(var,dims,coord); % is used to create data with coordinate values by ncCatArray function retval = ncArray(varargin) ... ...
 ... ... @@ -33,7 +33,6 @@ if length(idx) == 2 && strcmp(idx(2).type,'.') && strcmp(idx(2).subs,'coord') j = sort(j); idx_c.type = '()'; idx_c.subs = idx(1).subs(j); varargout{i} = subsref(self.coord(i).val,idx_c); end else ... ...
 ... ... @@ -37,14 +37,19 @@ else if length(tmp) == 1 start(i) = tmp; else % check if indexes are at regular intervals 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) start(i) = tmp(1); stride(i) = tmp(2)-tmp(1); count(i) = (tmp(end)-tmp(1))/stride(i) +1; else error('indeces'); error('indeces not at regular intervals'); end end end ... ...
 ... ... @@ -52,7 +52,10 @@ if strcmp(idx.type,'()') elseif strcmp(idx.type,'.') % load attribute 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) error('variable %s has no attribute called %s',self.varname,name); ... ...
 % Decompress a file using a cache. % % [fname,success]=cached_decompress(filename) % [fname,success] = cached_decompress(filename) % % Input: % filename: name of the file which is possibly compressed ... ... @@ -14,15 +14,21 @@ % CACHED_DECOMPRESS_LOG_FID (default 1): file id for log message % 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_LOG_FID 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; if isempty(cache_dir) ... ... @@ -32,13 +38,6 @@ if isempty(cache_dir) fprintf('creating directory %s for temporary files.\n',cache_dir); 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 % error(['cache directory for compressed files does not exist. '... % 'Please create the directory %s or change le value of the '... ... ... @@ -47,10 +46,10 @@ end % where to print logs? default to screen fid=CACHED_DECOMPRESS_LOG_FID; fid = CACHED_DECOMPRESS_LOG_FID; if (isempty(fid)) fid=1; fid = 1; end % form filename for cache ... ... @@ -58,22 +57,35 @@ end fname = url; fname = strrep(fname,'/','_SLASH_'); fname = strrep(fname,'*','_STAR_'); fname = strrep(fname,'\','_BSLASH_'); fname = strrep(fname,':','_COLON_'); fname = fullfile(cache_dir,fname); % test if in cache if exist(fname,'file') ~= 2 if endswith(url,'.gz') syscmd('gunzip --stdout "%s" > "%s"',url,fname); cmd = 'gunzip --stdout -'; elseif endswith(url,'.bz2') syscmd('bunzip2 --stdout "%s" > "%s"',url,fname); cmd = 'bunzip2 --stdout -'; elseif endswith(url,'.xz') cmd = 'unxz --stdout -'; else syscmd('unxz --stdout "%s" > "%s"',url,fname); cmd = 'cat'; end if startswith(url,'ftp://') syscmd('curl --silent "%s" | %s > "%s"',url,cmd,fname); else syscmd('%s < "%s" > "%s"',cmd,url,fname); end else % fprintf(fid,'retrieve from cache %s\n',url); end % check cache size d=dir(cache_dir); ... ... @@ -107,23 +119,23 @@ if (cashe_size > max_cache_size) end end function t = beginswith(s,pre) if length(pre) <= length(s) t = strcmp(s(1:length(pre)),pre); else function t = startswith(s,ext) if length(ext) <= length(s) t = strcmp(s(1:length(ext)),ext); else t = 0; end end end function t = endswith(s,ext) if length(ext) <= length(s) if length(ext) <= length(s) t = strcmp(s(end-length(ext)+1:end),ext); else else t = 0; end end end ... ... @@ -141,7 +153,7 @@ end end % Copyright (C) 2012-2013 Alexander Barth % Copyright (C) 2012-2013, 2015 Alexander Barth % % 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 ... ...
 ... ... @@ -80,6 +80,11 @@ elseif ischar(pattern) 'http://www.mathworks.com/matlabcentral/fileexchange/19550']); end end if isempty(filenames) error('ncArray:nomatch','no file found matching %s',pattern); end elseif isa(pattern, 'function_handle') filenames = cell(1,length(range)); ... ... @@ -120,12 +125,24 @@ if (dim > length(dims)) && ~isempty(coord) dims{dim} = catdimname; coord(dim).dims = {catdimname}; coord(dim).val = range; coord(dim).name = catdimname; end 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 % 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})); if isempty(dimc) ... ... @@ -133,12 +150,13 @@ for i=1:length(coord) coord(i).val = ncBaseArray(filenames{1},coord(i).name,'vinfo',vinfo); else % 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); end if dim > length(coord(i).dims) coord(i).dims{dim} = catdimname; end %if dim > length(coord(i).dims) % coord(i).dims{dim} = catdimname; %end end 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);