-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decoding netCDF is giving incorrect values for a large file #5597
Comments
A small question to clarify:
Is that the output of the |
That example is actually a different file than the original. I unpacked the original file externally using In all the examples, the data is the same time and location, so they should be the same values outside of whatever is lost from compressing to int16 and decompressing, and the output arrays are from selecting a single day (24 hours) at a single location from the dataset returned by So actually there are three files I've tested with, all of which should have the same data (assuming the issue isn't with how the files are built, which could be the case): |
@ohsqueezy Does this issue also show up, if just plain netCDF4 is used to open the files? |
Thanks. Does passing different values to |
Thanks for your help! I checked using the netCDF4 module, and the data is returned correctly $ d = netCDF4.Dataset("BIG_FILE_packed.nc")
$ d["ssrd"][d["time"][:] < d["time"][24], d["latitude"][:] == 44.8, d["longitude"][:] == 287.1]
> masked_array(
data=[[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 0. ]],
[[ 25651.61906215]],
[[ 354743.1221522 ]],
[[1091757.933255 ]],
[[2170377.23235622]],
[[3482363.69999847]],
[[4704882.32554591]],
[[5689654.23783437]],
[[6297785.304381 ]],
[[6534906.36839455]],
[[6543665.4578304 ]],
[[6543665.4578304 ]],
[[6543665.4578304 ]]],
mask=False,
fill_value=1e+20) I tried with $ xarray.open_dataset("BIG_FILE_packed.nc", engine="scipy").ssrd.sel(latitude=44.8, longitude=287.1, method="nearest").values[:23]
> array([2.000000e+00, 2.000000e+00, 2.000000e+00, 2.000000e+00,
2.000000e+00, 2.000000e+00, 2.000000e+00, 2.000000e+00,
2.000000e+00, 2.000000e+00, 2.000000e+00, 2.000000e+00,
2.565200e+04, 3.547440e+05, 1.091760e+06, 2.170378e+06,
3.482364e+06, 4.704884e+06, 5.689655e+06, 6.297786e+06,
6.534908e+06, 6.543667e+06, 6.543667e+06], dtype=float32) I should mention that in another large packed dataset from this API, I have gotten the same error but with a very small decimal value in place of the zero instead of 2. |
@ohsqueezy You might also try |
h5netcdf seems to be a separate issue for me as it gives me the error
I looked into it once though, and I think I might be able to fix that. I'll also see if I can build a small netCDF that has reproducible behavior! |
can you post the value of |
sure, no prob $ xarray.open_dataset("BIG_FILE_packed.nc").ssrd.encoding
{'source': 'BIG_FILE_packed.nc',
'original_shape': (743, 1801, 3600),
'dtype': dtype('int16'),
'missing_value': -32767,
'_FillValue': -32767,
'scale_factor': 625.6492454183389,
'add_offset': 20500023.17537729} |
This may just be the expected floating point error from using float32:
If you use full float64, then the data does decode to 0.0:
So the question then is why this ends up being decoded using float32 instead of float64, and if that logic should be adjusted or made customizable: xarray/xarray/coding/variables.py Line 225 in eea7673
|
That explains it to me! Not sure if it's still useful but I exported the subset as a netCDF file. In [59]: packed_vals = xarray.open_dataset("packed_solar_data_subset.nc", mask_and_scale=False).ssrd.values
In [60]: packed_vals[0] * numpy.float32(e["scale_factor"]) + numpy.float32(e["add_offset"])
Out[60]: 2.0
In [61]: packed_vals[0] * numpy.float64(e["scale_factor"]) + numpy.float64(e["add_offset"])
Out[61]: 0.0 Hm actually I think converting the packed vals to 64 bit and then decoding does what I'm looking for In [62]: xarray.decode_cf(xarray.open_dataset("packed_solar_data_subset.nc", mask_and_scale=False).astype(numpy.float64)).ssrd.values
Out[62]:
array([ 0. , 0. , 0. ,
0. , 0. , 0. ,
0. , 0. , 0. ,
0. , 0. , 0. ,
25651.61906215, 354743.1221522 , 1091757.933255 ,
2170377.23235622, 3482363.69999847, 4704882.32554591,
5689654.23783437, 6297785.304381 , 6534906.36839455,
6543665.4578304 , 6543665.4578304 ]) |
Thanks for sharing the subset netCDF file, that is very helpful for debugging indeed! The weird thing is that the dtype picking logic seems to have a special case that, per the code comment, suggesting we want to be using float64 here: xarray/xarray/coding/variables.py Lines 231 to 238 in eea7673
But in fact, the dtype picking logic doesn't do that, because the dtype is already converted into float32, first. The culprit seems to be this line in CFMaskCoder, which promotes the dtype to float32 to fit a fill-value of NaN: xarray/xarray/coding/variables.py Line 202 in eea7673
To fix this, I think logic in |
This is aimed at in #7654 |
New attempt here: #8713 |
@ohsqueezy After #7654 going out of focus I've resurrected the changes in #8713. Would you mind taking a look there, maybe test and let us know your thoughts on that? |
What happened:
0 value is decoded as 2
What you expected to happen:
Data encoded to -32766 should translate to 0
Minimal Complete Verifiable Example:
The first example is the base file I've been using, which is a 9GB packed netCDF. The first 12 values in this lookup should be 0 but are getting decoded as 2.
This second example shows that if the file is decoded without automatic
mask_and_scale
, the value is decoded as 0 when applying the scale factor and add offset to an example value in the interpreter.When the netCDF is unpacked using the
nco
command line tool, the correct values are unpacked.Something else that may be relevant is that another file with this same packed data but as much smaller subset (1.7KB) of the big file is unpacked correctly.
For this to be a real verifiable example, I can transfer the 9GB file to someone or give instructions on how to download it from the climate API I'm getting it from! I'm not sure if this is an issue with xarray or the API or something I'm doing wrong. I've mostly been using an older version of xarray, but I also tested on the most recent version available on PIP:
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.9.6 (default, Jul 8 2021, 20:44:16)
[GCC 5.4.0 20160609]
python-bits: 64
OS: Linux
OS-release: 4.4.0-200-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.0
libnetcdf: 4.7.4
xarray: 0.18.2
pandas: 1.3.0
numpy: 1.21.0
scipy: None
netCDF4: 1.5.7
pydap: None
h5netcdf: 0.11.0
h5py: 3.3.0
Nio: None
zarr: None
cftime: 1.5.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: 0.9.9.0
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 51.3.3
pip: 20.3.3
conda: None
pytest: None
IPython: 7.25.0
sphinx: None
The text was updated successfully, but these errors were encountered: