changeset 12561:8476336c120c octave-forge

ncread: do not replace _FillValue by NaNs for characters
author abarth93
date Fri, 20 Feb 2015 16:30:08 +0000
parents 9f933cf29066
children ab73f1e8aa6a
files main/netcdf/inst/ncread.m main/netcdf/inst/private/test_netcdf_high_level_interface.m
diffstat 2 files changed, 42 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/main/netcdf/inst/ncread.m	Fri Feb 20 15:24:59 2015 +0000
+++ b/main/netcdf/inst/ncread.m	Fri Feb 20 16:30:08 2015 +0000
@@ -28,8 +28,9 @@
 ## loaded.
 ##
 ## If the variable has the _FillValue attribute, then the corresponding values
-## are replaced by NaN. NetCDF attributes scale_factor (default 1) and 
-## add_offset (default 0) are use the transform the variable during the loading:
+## are replaced by NaN (except for characters). NetCDF attributes scale_factor 
+## (default 1) and add_offset (default 0) are use the transform the variable 
+## during the loading:
 ##
 ## x = scale_factor * x_in_file + add_offset
 ##
@@ -97,7 +98,8 @@
   end
 end
 
-if !isempty(fv)
+if !isempty(fv) && xtype != netcdf_getConstant('char') && ...
+   xtype != netcdf_getConstant('string')
   x(x == fv) = NaN;
 end
 
--- a/main/netcdf/inst/private/test_netcdf_high_level_interface.m	Fri Feb 20 15:24:59 2015 +0000
+++ b/main/netcdf/inst/private/test_netcdf_high_level_interface.m	Fri Feb 20 16:30:08 2015 +0000
@@ -90,3 +90,40 @@
 
 delete(fname)
 
+
+% test double with _FillValue
+
+fname = [tempname '-octave-netcdf.nc'];
+fv = 99999.;
+nccreate(fname,'flag','Dimensions',{'lon',10,'lat',10},'Datatype','double',...
+         'FillValue',fv);
+
+%system(['ncdump -h ' fname])
+data = zeros(10,10);
+data(1,2) = fv;
+ncid = netcdf_open(fname,'NC_WRITE');
+varid = netcdf_inqVarID(ncid, 'flag');
+netcdf_putVar(ncid,varid,data);
+data2 = ncread(fname,'flag');
+data(data == fv) = NaN;
+assert(isequaln(data,data2))
+delete(fname)
+
+
+% test char with _FillValue
+
+fname = [tempname '-octave-netcdf.nc'];
+fv = '*';
+nccreate(fname,'flag','Dimensions',{'lon',10,'lat',10},'Datatype','char',...
+         'FillValue',fv);
+data = repmat('.',[10 10]);
+data(1,2) = fv;
+
+ncid = netcdf_open(fname,'NC_WRITE');
+varid = netcdf_inqVarID(ncid, 'flag');
+netcdf_putVar(ncid,varid,data);
+data2 = ncread(fname,'flag');
+
+assert(isequal(data,data2))
+delete(fname)
+