Mercurial > forge
annotate main/netcdf/inst/private/test_netcdf_high_level_interface.m @ 12711:a4de85b2e6a0 octave-forge
fix bug 47014 and make test code work in matlab
author | abarth93 |
---|---|
date | Fri, 19 Feb 2016 13:45:14 +0000 |
parents | 8476336c120c |
children |
rev | line source |
---|---|
11992 | 1 %% Copyright (C) 2013 Alexander Barth |
2 %% | |
3 %% This program is free software; you can redistribute it and/or modify | |
4 %% it under the terms of the GNU General Public License as published by | |
5 %% the Free Software Foundation; either version 2 of the License, or | |
6 %% (at your option) any later version. | |
7 %% | |
8 %% This program is distributed in the hope that it will be useful, | |
9 %% but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 %% GNU General Public License for more details. | |
12 %% | |
13 %% You should have received a copy of the GNU General Public License | |
14 %% along with this program; If not, see <http://www.gnu.org/licenses/>. | |
12711 | 15 import_netcdf |
11988 | 16 |
17 fname = [tempname '-octave-netcdf.nc']; | |
18 | |
19 | |
20 nccreate(fname,'temp','Dimensions',{'lon',10,'lat',20}); | |
21 nccreate(fname,'salt','Dimensions',{'lon',10,'lat',20}); | |
22 nccreate(fname,'u','Dimensions',{'lon','lat'}); | |
23 u = randn(10,20); | |
24 ncwrite(fname,'u',u); | |
25 | |
12290
5b3d9192547f
use isequaln instead of isequalwithequalnans if available
abarth93
parents:
11992
diff
changeset
|
26 % for octave prior to 3.8.0 |
5b3d9192547f
use isequaln instead of isequalwithequalnans if available
abarth93
parents:
11992
diff
changeset
|
27 if isempty(which('isequaln')) |
5b3d9192547f
use isequaln instead of isequalwithequalnans if available
abarth93
parents:
11992
diff
changeset
|
28 isequaln = @(x,y) isequalwithequalnans(x,y); |
5b3d9192547f
use isequaln instead of isequalwithequalnans if available
abarth93
parents:
11992
diff
changeset
|
29 end |
5b3d9192547f
use isequaln instead of isequalwithequalnans if available
abarth93
parents:
11992
diff
changeset
|
30 |
11988 | 31 u2 = ncread(fname,'u'); |
12290
5b3d9192547f
use isequaln instead of isequalwithequalnans if available
abarth93
parents:
11992
diff
changeset
|
32 assert(isequaln(u,u2)); |
11988 | 33 |
34 u2 = ncread(fname,'u',[10 5],[inf inf],[1 1]); | |
12290
5b3d9192547f
use isequaln instead of isequalwithequalnans if available
abarth93
parents:
11992
diff
changeset
|
35 assert(isequaln(u(10:end,5:end),u2)); |
11988 | 36 |
37 ncwriteatt(fname,'temp','units','degree Celsius'); | |
38 assert(strcmp(ncreadatt(fname,'temp','units'),'degree Celsius')); | |
39 | |
40 ncwriteatt(fname,'temp','range',[0 10]); | |
41 assert(isequal(ncreadatt(fname,'temp','range'),[0 10])); | |
42 | |
43 ncwriteatt(fname,'temp','float_range',single([0 10])); | |
44 assert(isequal(ncreadatt(fname,'temp','float_range'),[0 10])); | |
45 | |
46 ncwriteatt(fname,'temp','int_range',int32([0 10])); | |
47 assert(isequal(ncreadatt(fname,'temp','int_range'),[0 10])); | |
48 | |
49 info = ncinfo(fname); | |
50 assert(length(info.Variables) == 3) | |
51 assert(strcmp(info.Variables(1).Name,'temp')); | |
52 assert(isequal(info.Variables(1).Size,[10 20])); | |
53 delete(fname); | |
54 | |
55 | |
56 nccreate(fname,'temp','Dimensions',{'lon',10,'lat',20},'Format','64bit'); | |
57 delete(fname); | |
58 | |
59 nccreate(fname,'temp','Dimensions',{'lon',10,'lat',20},'Format','classic'); | |
60 info = ncinfo(fname); | |
61 assert(strcmp(info.Format,'classic')); | |
62 | |
63 delete(fname); | |
64 | |
65 % netcdf4 | |
66 | |
67 nccreate(fname,'temp','Dimensions',{'lon',10,'lat',20},'Format','netcdf4'); | |
68 ncwriteatt(fname,'temp','uint_range',uint32([0 10])); | |
69 assert(isequal(ncreadatt(fname,'temp','uint_range'),[0 10])); | |
70 | |
71 info = ncinfo(fname); | |
72 assert(strcmp(info.Format,'netcdf4')); | |
73 delete(fname) | |
74 | |
75 % scalar variable | |
76 nccreate(fname,'temp','Format','netcdf4','Datatype','double'); | |
77 ncwrite(fname,'temp',123); | |
78 assert(ncread(fname,'temp') == 123) | |
79 delete(fname) | |
80 | |
81 | |
82 % test unlimited dimension with nccreate | |
83 fname = [tempname '-octave-netcdf.nc']; | |
84 nccreate(fname,'temp','Dimensions',{'lon',10,'lat',inf}); | |
85 %system(['ncdump -h ' fname]) | |
86 | |
87 info = ncinfo(fname); | |
88 assert(~info.Dimensions(1).Unlimited) | |
89 assert(info.Dimensions(2).Unlimited) | |
90 | |
91 delete(fname) | |
92 | |
12561
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
93 |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
94 % test double with _FillValue |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
95 |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
96 fname = [tempname '-octave-netcdf.nc']; |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
97 fv = 99999.; |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
98 nccreate(fname,'flag','Dimensions',{'lon',10,'lat',10},'Datatype','double',... |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
99 'FillValue',fv); |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
100 |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
101 %system(['ncdump -h ' fname]) |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
102 data = zeros(10,10); |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
103 data(1,2) = fv; |
12711 | 104 ncid = netcdf.open(fname,'NC_WRITE'); |
105 varid = netcdf.inqVarID(ncid, 'flag'); | |
106 netcdf.putVar(ncid,varid,data); | |
107 netcdf.close(ncid) | |
108 | |
12561
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
109 data2 = ncread(fname,'flag'); |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
110 data(data == fv) = NaN; |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
111 assert(isequaln(data,data2)) |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
112 delete(fname) |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
113 |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
114 |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
115 % test char with _FillValue |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
116 |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
117 fname = [tempname '-octave-netcdf.nc']; |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
118 fv = '*'; |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
119 nccreate(fname,'flag','Dimensions',{'lon',10,'lat',10},'Datatype','char',... |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
120 'FillValue',fv); |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
121 data = repmat('.',[10 10]); |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
122 data(1,2) = fv; |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
123 |
12711 | 124 ncid = netcdf.open(fname,'NC_WRITE'); |
125 varid = netcdf.inqVarID(ncid, 'flag'); | |
126 netcdf.putVar(ncid,varid,data); | |
127 netcdf.close(ncid) | |
128 | |
12561
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
129 data2 = ncread(fname,'flag'); |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
130 assert(isequal(data,data2)) |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
131 delete(fname) |
8476336c120c
ncread: do not replace _FillValue by NaNs for characters
abarth93
parents:
12290
diff
changeset
|
132 |
12711 | 133 |
134 | |
135 % test case for bug 47014 | |
136 | |
137 bug_47014 |