Issue with ncread when _FillValue, add_offset, and scale_factor are present
显示 更早的评论
I have run into an issue when trying to concatenate multiple netcdf files with the same variables into one file, combining these variables.
First, I create a new netcdf file using the header information from one of the files I wish to concatenate using ncinfo and ncwriteschema, changing the length of the variable along which I wish to concatenate to unlimited.
When I read a variable that does not have the attributes _FillValue, add_offset, and scale_factor using ncread, the variable reads into my workspace as I would expect, and writes as expected into the new file.
However, when I read a variable that does have those attributes, the variable reads into my workspace seemingly without applying those attributes, contrary to ncread's documentation. I checked this using netcdf.getVar, and the results are the same between the two methods, which seems to indicate that ncread is failing to apply the attributes.
Strangely, however, when I use ncwrite to store the variable data in the new netcdf file, and then read it back in with either ncread or netcdf.getVar, I get something different! The variable data now oscillates between the _FillValue and -1*_FillValue.
Hypothesis: To me, this seems to indicate that ncwrite is trying to apply the attributes, but since ncread or netcdf.getVar did not apply them inversely, it's now applying them a second time! This causes the values to be larger in magnitude than the _FillValue, and confuses the netCDF file.
An easy workaround would be to apply the attributes myself using ncreadatt and ncwriteatt, but I'm not sure my hypothesis is correct, so I'm hesitant to brute force the process in that way.
Working on attaching code and small enough netCDF files, will update when those are ready.
UPDATE: netCDF files are uploaded as "netcdf_files.zip", matlab code uploaded as "forumpost_ncread.m".
1 个评论
Daniel Holstein
2023-11-29
This is a few years old, but I am having a similar issue. When reading a .nc into matlab using netcdf.getVar the fill values are correct as defined in the file. If loading with ncread, all fill values are changed to NaN. I believe that netcdf.getVar is reading the matrices as they are stored, but ncread converts any value that equals a fill value to NaN... this can be an issue for manipulating these files, as the fill values can change...
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 NetCDF Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!