Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Behavior of set_fill_off() changed in v1.5.1 #972

Closed
mdklatt opened this issue Sep 18, 2019 · 2 comments
Closed

Behavior of set_fill_off() changed in v1.5.1 #972

mdklatt opened this issue Sep 18, 2019 · 2 comments

Comments

@mdklatt
Copy link

mdklatt commented Sep 18, 2019

netCDF4-1.5.1 (installed via pip 19.1.1)
Python 3.6.2 (virtualenv)
macOS Sierra (10.12.6)

Based on the documentation, my understanding ofDataset.set_fill_off() is that it leaves variables uninitialized upon creation. This is useful in cases were the variable is immediately filled with real data. With earlier versions of netCDF4, this works as expected:

""" Demonstrate fill value bug in netCDF4-1.5.1+

"""
from netCDF4 import Dataset
from numpy.ma import masked_all


# Write masked data to netCDF, which will be replaced by _FillValue.

dataset = Dataset("test.nc", "w")
dataset.set_fill_off()  # comment this out and everything works as expected
dim = dataset.createDimension("dim", 10)
var = dataset.createVariable("var1", "f8", (dim.name,))
var[:] = masked_all((10,), "f8")
dataset.close()


# Read variable from netCDF. It should be masked everywhere.

dataset = Dataset("test.nc", "r")
var = dataset.variables["var1"]
assert var[:].mask.all()

As of v1.5.1, the final assertion fails. Inspection of the netCDF file using ncdump shows that var1 is set to _FillValue as expected. However, when var1 is read from the file, it is not masked anywhere. The value is _FillValue (9.9692e+36) everywhere, but the mask of the resulting MaskedArray is False.

Somehow, calling set_fill_off() when the variable is created is preventing it from being masked correctly when the dataset is read. This seems contrary to the documentation, and is a new behavior in v1.5.1.

A workaround is to not call set_fill_off().

@jswhit
Copy link
Collaborator

jswhit commented Sep 19, 2019

I see the problem - a simple case of have and and instead of an or in an if statement. Fix in pull request #973

jswhit added a commit that referenced this issue Sep 20, 2019
jswhit added a commit that referenced this issue Sep 21, 2019
fix for issue #972 (masked array not returned when auto_fill off)
@jswhit
Copy link
Collaborator

jswhit commented Sep 21, 2019

Closed by #973. Thanks for the report.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants