Skip to content
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

allow _FillValue to be set using fill_value createVariable kwarg for vlen string vars #732

Merged
merged 11 commits into from
Oct 25, 2017
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
`'standard'` or `'gregorian'`. If `None` (the default), check that the calendar
attribute is present on each variable and values are unique across files raising
a `ValueError` otherwise.
* Allow _FillValue to be set for vlen string variables (issue #730).

version 1.3.0 (tag v1.3.0rel)
==============================
Expand Down
18 changes: 10 additions & 8 deletions netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3469,14 +3469,16 @@ behavior is similar to Fortran or Matlab, but different than numpy.
if grp.data_model != 'NETCDF4': grp._enddef()
_ensure_nc_success(ierr)
else:
# cast fill_value to type of variable.
# also make sure it is written in native byte order
# (the same as the data)
if self._isprimitive or self._isenum:
fillval = numpy.array(fill_value, self.dtype)
if not fillval.dtype.isnative: fillval.byteswap(True)
_set_att(self._grp, self._varid, '_FillValue',\
fillval, xtype=xtype)
if self._isprimitive or self._isenum or \
(self._isvlen and self.dtype == str):
if self._isvlen and self.dtype == str:
_set_att(self._grp, self._varid, '_FillValue',\
_tostr(fill_value), xtype=xtype, force_ncstring=True)
else:
fillval = numpy.array(fill_value, self.dtype)
if not fillval.dtype.isnative: fillval.byteswap(True)
_set_att(self._grp, self._varid, '_FillValue',\
fillval, xtype=xtype)
else:
raise AttributeError("cannot set _FillValue attribute for VLEN or compound variable")
if least_significant_digit is not None:
Expand Down
10 changes: 10 additions & 0 deletions test/tst_masked5.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ def setUp(self):
f = Dataset(self.testfile, 'w')
d = f.createDimension('x',6)
v = f.createVariable('v', "i2", 'x')
# issue 730: set fill_value for vlen str vars
v2 = f.createVariable('v2',str,'x',fill_value=u'<missing>')

v.missing_value = self.missing_values
v[:] = self.v
v2[0]='first'

f.close()

Expand All @@ -41,13 +44,20 @@ def test_scaled(self):

f = Dataset(self.testfile)
v = f.variables["v"]
v2 = f.variables["v2"]
self.assertTrue(isinstance(v[:], ma.core.MaskedArray))
assert_array_equal(v[:], self.v_ma)
assert_array_equal(v[2],self.v[2]) # issue #624.
v.set_auto_mask(False)
self.assertTrue(isinstance(v[:], np.ndarray))
assert_array_equal(v[:], self.v)

# issue 730
assert (v2[0]=='first')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be u'first' for it to pass on both python 2/3. The rule of thumb is to always use bytes/unicode literals (b'first' and u'first') unless you want native strings.

print v2[1]
assert (v2[1]==u'<missing>')


f.close()


Expand Down