Skip to content

Commit aa9452a

Browse files
author
Jeff Whitaker
committed
add fill_value='default' option
1 parent 1f67c94 commit aa9452a

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

‎src/netCDF4/_netCDF4.pyx‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,9 +4041,10 @@ behavior is similar to Fortran or Matlab, but different than numpy.
40414041
If fill_value is set to `False`, then the variable is not pre-filled.
40424042
The default netCDF fill values can be found in the dictionary `netCDF4.default_fillvals`.
40434043
If not set, the default fill value will be used but no `_FillValue` attribute will be created
4044-
(this is the default behavior of the netcdf-c library). `Variable.get_fill_value`
4045-
can be used to retrieve the fill value, even if the `_FillValue` attribute is
4046-
not set.
4044+
(this is the default behavior of the netcdf-c library). If you want to use the
4045+
default fill value, but have the `_FillValue` attribute set, use
4046+
`fill_value='default'` (note - this only works for primitive data types). ``Variable.get_fill_value`
4047+
can be used to retrieve the fill value, even if the `_FillValue` attribute is not set.
40474048
40484049
**`chunk_cache`**: If specified, sets the chunk cache size for this variable.
40494050
Persists as long as Dataset is open. Use `set_var_chunk_cache` to
@@ -4407,6 +4408,12 @@ behavior is similar to Fortran or Matlab, but different than numpy.
44074408
if ierr != NC_NOERR:
44084409
if grp.data_model != 'NETCDF4': grp._enddef()
44094410
_ensure_nc_success(ierr, extra_msg=error_info)
4411+
elif fill_value == 'default':
4412+
if self._isprimitive:
4413+
fillval = numpy.array(default_fillvals[self.dtype.str[1:]])
4414+
if not fillval.dtype.isnative: fillval.byteswap(True)
4415+
_set_att(self._grp, self._varid, '_FillValue',\
4416+
fillval, xtype=xtype)
44104417
else:
44114418
if self._isprimitive or self._isenum or \
44124419
(self._isvlen and self.dtype == str):

‎test/test_get_fill_value.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def setUp(self):
1616
if not dt.startswith('c'):
1717
v = f.createVariable(dt+'_var',dt,dim)
1818
v = f.createVariable('float_var',np.float64,dim,fill_value=fill_val)
19+
# test fill_value='default' option (issue #1374)
20+
v2 = f.createVariable('float_var2',np.float64,dim,fill_value='default')
1921
f.close()
2022

2123
def tearDown(self):
@@ -33,6 +35,8 @@ def runTest(self):
3335
# _FillValue attribute is set.
3436
v = f['float_var']
3537
assert_array_equal(fill_val, v.get_fill_value())
38+
v = f['float_var2']
39+
assert_array_equal(np.array(netCDF4.default_fillvals['f8']), v._FillValue)
3640
f.close()
3741

3842
if __name__ == '__main__':

0 commit comments

Comments
 (0)