MATLAB NetCDF Library Error: 'NC_EBADTYPE' during endDef()

5 visualizaciones (últimos 30 días)
HSIN-Yi CHEN
HSIN-Yi CHEN el 29 de En. de 2024
Respondida: Brahmadev el 5 de Feb. de 2024
Hello MATLAB community,
I hope this message finds you well. I am currently facing an issue while working with the MATLAB NetCDF library and would greatly appreciate your assistance.
Problem Description:I am encountering an error when using the netcdf.endDef(ncid) function in MATLAB. The error message is as follows:
Error using matlab.internal.imagesci.netcdflib
The NetCDF library encountered an error during execution of
'endDef' function - 'Not a valid data type or _FillValue type
mismatch (NC_EBADTYPE)'.
Error in netcdf.endDef (line 33)
matlab.internal.imagesci.netcdflib('endDef', ncid);
This issue arises when I attempt to add specific variable data into the Var structure that I am writing to a NetCDF file. Which I refer to the vardata here. The problem seems to be related to the Var.FillValue attribute. The endDef function works fine if I do not include the Var.FillValue.
I suspect there might be something wrong with how I am defining the Var.FillValue attribute. I've tried various approaches, but the error persists. Could someone please provide guidance on the correct way to define the FillValue attribute within the structure?
Any help or insights into this issue would be highly appreciated. Thank you in advance for your assistance.

Respuestas (1)

Brahmadev
Brahmadev el 5 de Feb. de 2024
The error indicates that either the 'FillValue' attribute has a value of an unexpected datatype or it is not being assigned as expected. The 'netcdf.putAtt' and 'netcdf.defVarFill' functions can be used to define the '_FillValue' attribute for 'netCDF' variables. Note that, the datatype of the '_FillValue' attribute value should be the same as the datatype of the variables. And, 'netcdf.putAtt' cannot be used to set the '_FillValue' attribute of NetCDF4 files, 'netcdf.defVarFill' should be used for the same.
See example below:
% Step 1: Open or create a NetCDF file
ncid = netcdf.create('myFile2.nc', 'CLASSIC_MODEL');
% Step 2: Define dimensions
dimid_x = netcdf.defDim(ncid, 'x', 100); % Dimension of size 100 in the x-direction
dimid_y = netcdf.defDim(ncid, 'y', 100); % Dimension of size 100 in the y-direction
dimids = [dimid_x, dimid_y]; % Array of dimension IDs
% Step 3: Define a variable of type NC_FLOAT
varid = netcdf.defVar(ncid, 'temperature', 'NC_FLOAT', dimids);
% Define the _FillValue attribute for the variable
fillValue = single(-9999);
netcdf.putAtt(ncid, varid, '_FillValue', fillValue);
% Step 4: Exit the define mode
netcdf.endDef(ncid);
% Step 5: Write data to the variable
temperature_data = rand(100, 100, 'single'); % 100x100 array of single-precision floats
% Write the data to the 'temperature' variable
netcdf.putVar(ncid, varid, temperature_data);
% Step 6: Close the NetCDF file
netcdf.close(ncid);
You can also refer to the folowing documentation links for more information:
  1. 'defVarFill': https://www.mathworks.com/help/matlab/ref/netcdf.defvarfill.html
  2. 'putAtt': https://www.mathworks.com/help/matlab/ref/netcdf.putatt.html
Hope this helps in resolving your query.

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by