From: <aba...@us...> - 2012-06-22 15:52:42
|
Revision: 10673 http://octave.svn.sourceforge.net/octave/?rev=10673&view=rev Author: abarth93 Date: 2012-06-22 15:52:36 +0000 (Fri, 22 Jun 2012) Log Message: ----------- Added Paths: ----------- trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/display.m trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncBaseArray.m trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncsub.m trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsasgn.m trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsref.m Added: trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/display.m =================================================================== --- trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/display.m (rev 0) +++ trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/display.m 2012-06-22 15:52:36 UTC (rev 10673) @@ -0,0 +1,4 @@ +function display(self) +disp([self.varname ' from ' self.filename]); +%self.vinfo +%self.dims Added: trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncBaseArray.m =================================================================== --- trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncBaseArray.m (rev 0) +++ trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncBaseArray.m 2012-06-22 15:52:36 UTC (rev 10673) @@ -0,0 +1,62 @@ +% V = ncBaseArray(filename,varname) +% V = ncBaseArray(filename,varname,'property',value,...) +% create a ncBaseArray that can be accessed as a normal matlab array. +% Ths object is a helper object. Users should normally call ncArray. +% +% For read access filename can be compressed if it has the extensions +% ".gz" or ".bz2". It use the function cache_decompress to cache to +% decompressed files. +% +% Data is loaded by ncread and saved by ncwrite. Values equal to _FillValue +% are thus replaced by NaN and the scaling (add_offset and +% scale_factor) is applied during loading and saving. +% +% Properties: +% 'tooBigToLoad': if tooBigToLoad is set to true, then only the minimum +% data will be loaded. However this can be quite slow. +% +% Example: +% +% Loading the variable (assuming V is 3 dimensional): +% +% x = V(1,1,1); % load element 1,1,1 +% x = V(:,:,:); % load the complete array +% x = V(); x = full(V) % loads also the complete array +% +% Saving data in the netcdf file: +% V(1,1,1) = x; % save x in element 1,1,1 +% V(:,:,:) = x; +% +% Attributes +% units = V.units; % get attribute called "units" +% V.units = 'degree C'; % set attributes; +% +% Note: use the '.()' notation if the attribute has a leading underscore +% (due to a limitation in the matlab parser): +% +% V.('_someStrangeAttribute') = 123; +% +% see also cache_decompress + +function retval = ncBaseArray(filename,varname,varargin) + +self.tooBigToLoad = false; +prop = varargin; + +for i=1:2:length(prop) + if strcmp(prop{i},'tooBigToLoad ') + self.tooBigToLoad = prop{i+1}; + end +end + +self.filename = filename; +self.varname = varname; + +self.vinfo = ncinfo(cached_decompress(filename),varname); +self.sz = self.vinfo.Size; + +self.dims = self.vinfo.Dimensions; +self.nd = length(self.dims); + +retval = class(self,'ncBaseArray',BaseArray(self.sz)); +end Added: trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncsub.m =================================================================== --- trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncsub.m (rev 0) +++ trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/ncsub.m 2012-06-22 15:52:36 UTC (rev 10673) @@ -0,0 +1,61 @@ +function [start, count, stride] = ncsub(self,idx) + +assert(strcmp(idx.type,'()')) + +% number of dimension (including singleton dimensions) +%n = length(size(self)); +n = self.nd; + +% number of subscripts +ns = length(idx.subs); + +if ns == 0 + % load all + start = ones(1,n); + count = self.sz; + stride = ones(1,n); +else + + start = ones(1,ns); + count = ones(1,ns); + stride = ones(1,ns); + + % sz is the size padded by 1 if more indices are given than n + sz = ones(1,ns); + sz(1:length(self.sz)) = self.sz; + + for i=1:ns + if isempty(idx.subs{i}) + count(i) = 0; + + elseif strcmp(idx.subs{i},':') + count(i) = sz(i); + + else + tmp = idx.subs{i}; + + if length(tmp) == 1 + start(i) = tmp; + else + test = tmp(1):tmp(2)-tmp(1):tmp(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'); + end + end + end + end + + assert(all(count(n+1:end) == 1 | count(n+1:end) == 0)) + assert(all(start(n+1:end) == 1)) + + if ~any(count == 0) + count = count(1:n); + start = start(1:n); + stride = stride(1:n); + end +end \ No newline at end of file Added: trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsasgn.m =================================================================== --- trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsasgn.m (rev 0) +++ trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsasgn.m 2012-06-22 15:52:36 UTC (rev 10673) @@ -0,0 +1,10 @@ +function self = subsasgn(self,idxs,x) +%idx + +idx = idxs(1); +assert(strcmp(idx.type,'()')) +[start, count, stride] = ncsub(self,idx); + +if all(count ~= 0) + ncwrite(self.filename,self.varname,x,start,stride); +end Added: trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsref.m =================================================================== --- trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsref.m (rev 0) +++ trunk/octave-forge/extra/ncArray/inst/@ncBaseArray/subsref.m 2012-06-22 15:52:36 UTC (rev 10673) @@ -0,0 +1,65 @@ +function x = subsref(self,idx) + +assert(length(idx) == 1) + +if strcmp(idx.type,'()') + % load data + + if strcmp(idx.type,'()') && length(idx.subs) == 1 ... + && length(idx.subs) < self.nd + % reference like A([2 3 1 5]) + + if self.tooBigToLoad + % number of elements in x + ind = idx.subs{1}; + + % transform index to subscript + subs = cell(1,self.nd); + [subs{:}] = ind2sub(size(self),ind); + + % make a nice array length(ind) by self.nd + subs = cell2mat(subs); + + % output array + x = zeros(size(ind)); + x(:) = NaN; + + + % get every element + % can be quite slow + idxe.type = '()'; + idxe.subs = cell(1,self.nd); + + for i=1:length(ind) + idxe.subs = mat2cell(subs(i,:),1,ones(1,self.nd)); + x(i) = subsref(self,idxe); + end + else + % load full array + tmp = full(self); + x = subsref(tmp,idx); + end + else + [start, count, stride] = ncsub(self,idx); + + if any(count == 0) + x = zeros(count); + else + x = ncread(cached_decompress(self.filename),self.varname,... + start,count,stride); + end + end +elseif strcmp(idx.type,'.') + % load attribute + name = idx.subs; + index = strmatch(name,{self.vinfo.Attributes(:).Name}); + + if isempty(index) + error('variable %s has no attribute called %s',self.varname,name); + else + x = self.vinfo.Attributes(index).Value; + end +else + error('not supported'); + +end \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |