Skip to content

Commit

Permalink
add fill_value='default' option
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit2 committed Oct 21, 2024
1 parent 1f67c94 commit aa9452a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4041,9 +4041,10 @@ behavior is similar to Fortran or Matlab, but different than numpy.
If fill_value is set to `False`, then the variable is not pre-filled.
The default netCDF fill values can be found in the dictionary `netCDF4.default_fillvals`.
If not set, the default fill value will be used but no `_FillValue` attribute will be created
(this is the default behavior of the netcdf-c library). `Variable.get_fill_value`
can be used to retrieve the fill value, even if the `_FillValue` attribute is
not set.
(this is the default behavior of the netcdf-c library). If you want to use the
default fill value, but have the `_FillValue` attribute set, use
`fill_value='default'` (note - this only works for primitive data types). ``Variable.get_fill_value`
can be used to retrieve the fill value, even if the `_FillValue` attribute is not set.
**`chunk_cache`**: If specified, sets the chunk cache size for this variable.
Persists as long as Dataset is open. Use `set_var_chunk_cache` to
Expand Down Expand Up @@ -4407,6 +4408,12 @@ behavior is similar to Fortran or Matlab, but different than numpy.
if ierr != NC_NOERR:
if grp.data_model != 'NETCDF4': grp._enddef()
_ensure_nc_success(ierr, extra_msg=error_info)
elif fill_value == 'default':
if self._isprimitive:
fillval = numpy.array(default_fillvals[self.dtype.str[1:]])
if not fillval.dtype.isnative: fillval.byteswap(True)
_set_att(self._grp, self._varid, '_FillValue',\
fillval, xtype=xtype)
else:
if self._isprimitive or self._isenum or \
(self._isvlen and self.dtype == str):
Expand Down
4 changes: 4 additions & 0 deletions test/test_get_fill_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def setUp(self):
if not dt.startswith('c'):
v = f.createVariable(dt+'_var',dt,dim)
v = f.createVariable('float_var',np.float64,dim,fill_value=fill_val)
# test fill_value='default' option (issue #1374)
v2 = f.createVariable('float_var2',np.float64,dim,fill_value='default')
f.close()

def tearDown(self):
Expand All @@ -33,6 +35,8 @@ def runTest(self):
# _FillValue attribute is set.
v = f['float_var']
assert_array_equal(fill_val, v.get_fill_value())
v = f['float_var2']
assert_array_equal(np.array(netCDF4.default_fillvals['f8']), v._FillValue)
f.close()

if __name__ == '__main__':
Expand Down

0 comments on commit aa9452a

Please sign in to comment.