changeset 12680:1a9c41feaf2b octave-forge

add derived Array class
author abarth93
date Mon, 24 Aug 2015 08:49:21 +0000
parents a74726ba86d1
children 0c936bf960f6
files extra/ncArray/inst/@DerivedArray/DerivedArray.m extra/ncArray/inst/@DerivedArray/subsref.m extra/ncArray/inst/@ncArray/ncArray.m extra/ncArray/inst/test_ncarray.m
diffstat 4 files changed, 72 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/ncArray/inst/@DerivedArray/DerivedArray.m	Mon Aug 24 08:49:21 2015 +0000
@@ -0,0 +1,38 @@
+% Create a derived array
+%
+% V = DerivedArray(function,{params})
+%
+% Example:
+% Temp_in_C = DerivedArray(@(t) t-273.15,{Temp_in_K})
+%
+% see also ncArray
+% Web: http://modb.oce.ulg.ac.be/mediawiki/index.php/ncArray
+
+
+function retval = DerivedArray(fun,params)
+
+self.fun = fun;
+self.params = params;
+
+retval = class(self,'DerivedArray',BaseArray(size(params{1})));
+
+
+
+
+
+
+% Copyright (C) 2015 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/>.
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/ncArray/inst/@DerivedArray/subsref.m	Mon Aug 24 08:49:21 2015 +0000
@@ -0,0 +1,9 @@
+function varargout = subsref(self,idx)
+
+  p = {};
+  for i = 1:length(self.params)
+    %i,self.params{i},idx
+    p{i} = subsref(self.params{i},idx);
+  end
+
+  varargout{1} = self.fun(p{:});
\ No newline at end of file
--- a/extra/ncArray/inst/@ncArray/ncArray.m	Thu Aug 20 09:03:54 2015 +0000
+++ b/extra/ncArray/inst/@ncArray/ncArray.m	Mon Aug 24 08:49:21 2015 +0000
@@ -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)
--- a/extra/ncArray/inst/test_ncarray.m	Thu Aug 20 09:03:54 2015 +0000
+++ b/extra/ncArray/inst/test_ncarray.m	Mon Aug 24 08:49:21 2015 +0000
@@ -12,15 +12,36 @@
 mkdir(tmpdir);
 
 tmpfname = tempname(tmpdir);
+refdata = {};
+
 for i = 1:3  
   files{i} = fullfile(tmpdir,sprintf('file%d.nc',i));
-  ncarray_example_file(files{i},randn(220,144));
+  refdata{i} = randn(220,144);                                      
+  ncarray_example_file(files{i},refdata{i});
 end
 
 
 filename = files{1};
 
 
+%%% test DerivedArray
+
+% reading convertion with 1 argument
+
+SST = ncArray(files{1},varname);
+Temp_in_K = DerivedArray(@(t) t + 273.15,{SST});
+ref = refdata{1} + 273.15;
+assert(isequaln(Temp_in_K(:,:,:),ref))
+
+% reading convertion with 2 arguments
+
+SST1 = ncArray(files{1},varname);
+SST2 = ncArray(files{2},varname);
+temp_trend = DerivedArray(@(t1,t2) t2 - t1,{SST1,SST2});
+ref = refdata{2} - refdata{1};
+assert(isequaln(temp_trend(:,:,:),ref))
+
+
 % test ncread/ncwrite
 
 copyfile(files{1},tmpfname);
@@ -359,6 +380,8 @@
 
 assert(strcmp(CA2.('units'),'degC'));
 
+
+
 test_ncarray_nan
 
 % clean-up