File: test_ncarray_nan.m

package info (click to toggle)
octave-ncarray 1.0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: makefile: 144
file content (80 lines) | stat: -rw-r--r-- 2,064 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% Copyright (C) 2013 Alexander Barth <barth.alexander@gmail.com>
% Copyright (C) 2019 John D <john.donoghue@ieee.org>
%
% 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 3 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 ncBaseArray, ncCatArray and ncArray.
function test_ncarray_nan()

% for octave prior to 3.8.0
if isempty(which('isequaln'))
  isequaln = @(x,y) isequalwithequalnans(x,y);
end

varname = 'SST';

tmpdir = tempname;
mkdir(tmpdir);

tmpfname = tempname(tmpdir);
dataref = randn(220,144,3);
%dataref(rand(size(dataref)) > 0.7) = NaN;
dataref(50:80,30:90,1:2) = NaN;

for i = 1:3  
  files{i} = fullfile(tmpdir,sprintf('file%d.nc',i));
  ncarray_example_file(files{i},dataref(:,:,i));
end

data = ncCatArray(3,files,varname);
reddata = nanmean(data,3);
if exist ("nanmean")
  reddataref = nanmean(dataref,3);
else
  reddataref = mean(dataref,3, 'omitnan');
endif
assertAlmostEqual(reddata, reddataref)

reddata = nansum(data,3);
reddataref = nansum(dataref,3);
assertAlmostEqual(reddata, reddataref)

reddata = nanvar(data,[],3);
if exist ("nanvar")
  reddataref = nanvar(dataref,[],3);
else
  reddataref = var(dataref,[],3, 'omitnan');
endif
diff = reddata - reddataref;
assert(max(diff(:)) < 1e-6)

reddata = nanstd(data,[],3);
if exist ("nanstd")
  reddataref = nanstd(dataref,[],3);
else
  reddataref = std(dataref,[],3, 'omitnan');
endif
diff = reddata - reddataref;
assert(max(diff(:)) < 1e-6)

% clean-up
for i = 1:3  
  delete(files{i});
end
rmdir(tmpdir);

endfunction

%!test
%! test_ncarray_nan ()