'HDF error (NC_EHDFERR)' during execution of 'close' function using matlab.internal.imagesci.netcdflib
Show older comments
One of our users has some code which unexpectedly fails with error messages and creates a corrupted NetCDF file in MATLAB versions R2020b and newer.
This is the code:
outfile='test.nc';
if exist(outfile,'file')
delete(outfile);
end
nrow=128; ncol=128;
nccreate(outfile,'shape','dimensions',{'lon',nrow,'lat',ncol,'time',inf},'chunksize',[nrow,ncol,1],'datatype','int16','fillvalue',0,'format','netcdf4','deflatelevel',9);
nccreate(outfile,'axis', 'dimensions',{'lon',nrow,'lat',ncol,'time',inf},'chunksize',[nrow,ncol,1],'datatype','double','fillvalue',0,'format','netcdf4','deflatelevel',9);
nccreate(outfile,'lfloc','dimensions',{'lon',nrow,'lat',ncol,'time',inf},'chunksize',[nrow,ncol,1],'datatype','int16','fillvalue',0,'format','netcdf4','deflatelevel',9);
nccreate(outfile,'islnd','dimensions',{'lon',nrow,'lat',ncol},'chunksize',[nrow,ncol],'datatype','int16','fillvalue',0,'format','netcdf4','deflatelevel',9);
nccreate(outfile,'lon','dimensions',{'lon',nrow,'lat',ncol});
nccreate(outfile,'lat','dimensions',{'lon',nrow,'lat',ncol});
islnd=zeros(nrow,ncol);
islnd(:,:)=1;
nccreate(outfile,'time','dimensions',{'time',inf});
ncwrite(outfile,'islnd',int16(islnd));
These are the error messages:
Warning: The following error was caught while executing 'onCleanup' class destructor:
Error using matlab.internal.imagesci.netcdflib
The NetCDF library encountered an error during execution of 'close' function - 'HDF error
(NC_EHDFERR)'.
Error in netcdf.close (line 16)
matlab.internal.imagesci.netcdflib('close', ncid);
Error in internal.matlab.imagesci.nc/close (line 145)
netcdf.close(rootid);
Error in ncwrite>@()ncObj.close() (line 84)
cleanUp = onCleanup(@()ncObj.close());
Error in onCleanup/delete (line 80)
obj.task();
Error in ncwrite (line 61)
if nargin > 0
Error in test (line 15)
ncwrite(outfile,'islnd',int16(islnd));
> In ncwrite (line 61)
In test (line 15)
Error using matlab.internal.imagesci.netcdflib
The NetCDF library encountered an error during execution of 'inqVar' function - 'HDF
error (NC_EHDFERR)'.
Error in netcdf.inqVar (line 26)
[varname,xtype,dimids,natts] = matlab.internal.imagesci.netcdflib('inqVar', ncid, varid);
Error in internal.matlab.imagesci.nc/varInfo (line 1372)
netcdf.inqVar(gid,varid);
Error in internal.matlab.imagesci.nc/write (line 754)
varinfo = this.varInfo(gid, varid);
Error in ncwrite (line 87)
ncObj.write(varName, varData, start, stride);
Error in test (line 15)
ncwrite(outfile,'islnd',int16(islnd))
However, the same code succeeds and creates a normal NetCDF file in MATLAB versions R2020a and earlier. Any suggestions as to why the error starts in MATLAB R2020b and how to resolve it?
Accepted Answer
More Answers (1)
Charles Lee
on 9 Jan 2024
Hi Hans,
It might be a few months late, but it seems we encountered the same issue. When I was using R2020a, I could write to a netCDF file using the following function:
% ET attribute
nccreate(outfile,'ET','datatype','double','Dimensions',{'lat' row 'lon' col},'DeflateLevel',5);
ncwriteatt(outfile,'ET','unit','mm d-1');
ncwriteatt(outfile,'ET','long_name','Total Evapotranspiration');
% lon attribute
nccreate(outfile,'lon','datatype','double','Dimensions',{'lon'},'DeflateLevel',5);
ncwriteatt(outfile,'lon','units','degrees_east');
ncwriteatt(outfile,'lon','long_name','longitude');
% lat attribute
nccreate(outfile,'lat','datatype','double','Dimensions',{'lat'},'DeflateLevel',5);
ncwriteatt(outfile,'lat','units','degrees_north');
ncwriteatt(outfile,'lat','long_name','latitude');
% write-in
ncwrite(outfile,'ET',ET);
ncwrite(outfile,'lat',lat);
ncwrite(outfile,'lon',lon);
However, upon switching to the R2023b version, I encountered an error similar to yours. I found that the problem was resolved when I made changes to the following format:
% lon attribute
nccreate(outfile,'lon','datatype','double','Dimensions',{'lon',col},'DeflateLevel',5);
ncwriteatt(outfile,'lon','units','degrees_east');
ncwriteatt(outfile,'lon','long_name','longitude');
ncwrite(outfile,'lon',lon);
% lat attribute
nccreate(outfile,'lat','datatype','double','Dimensions',{'lat',row},'DeflateLevel',5);
ncwriteatt(outfile,'lat','units','degrees_north');
ncwriteatt(outfile,'lat','long_name','latitude');
ncwrite(outfile,'lat',lat);
% ET attribute
nccreate(outfile,'ET','datatype','double','Dimensions',{'lat',row,'lon',col},'DeflateLevel',5);
ncwriteatt(outfile,'ET','unit','mm d-1');
ncwriteatt(outfile,'ET','long_name','Total Evapotranspiration');
ncwrite(outfile,'ET',ET);
To summarize, the key change was this: I first defined the 'lat' and 'lon' variables along with their dimensions because these two variables are related to the dimensions of the 'ET' variable.
Hope this still proves helpful to you!
Categories
Find more on NetCDF Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!