def test_incompatible_attributes(self): invalid_vars = [ Variable(['t'], pd.date_range('2000-01-01', periods=3), {'units': 'foobar'}), Variable(['t'], [0, 1, 2], {'add_offset': 0}, {'add_offset': 2}), Variable(['t'], [0, 1, 2], {'_FillValue': 0}, {'_FillValue': 2}), ] for var in invalid_vars: with self.assertRaises(ValueError): conventions.encode_cf_variable(var)
def set_variable(self, name, variable): variable = encode_cf_variable(variable) if self.format == 'NETCDF4': values, datatype = _nc4_values_and_dtype(variable) else: variable = encode_nc3_variable(variable) values = variable.values datatype = variable.dtype self.set_necessary_dimensions(variable) fill_value = variable.attrs.pop('_FillValue', None) encoding = variable.encoding nc4_var = self.ds.createVariable( varname=name, datatype=datatype, dimensions=variable.dimensions, zlib=encoding.get('zlib', False), complevel=encoding.get('complevel', 4), shuffle=encoding.get('shuffle', True), fletcher32=encoding.get('fletcher32', False), contiguous=encoding.get('contiguous', False), chunksizes=encoding.get('chunksizes'), endian=encoding.get('endian', 'native'), least_significant_digit=encoding.get('least_significant_digit'), fill_value=fill_value) nc4_var.set_auto_maskandscale(False) nc4_var[:] = values for k, v in iteritems(variable.attrs): # set attributes one-by-one since netCDF4<1.0.10 can't handle # OrderedDict as the input to setncatts nc4_var.setncattr(k, v)
def set_variable(self, name, variable): variable = encode_cf_variable(variable) self.set_necessary_dimensions(variable) fill_value = variable.attrs.pop('_FillValue', None) encoding = variable.encoding self.ds.createVariable( varname=name, datatype=variable.dtype, dimensions=variable.dimensions, zlib=encoding.get('zlib', False), complevel=encoding.get('complevel', 4), shuffle=encoding.get('shuffle', True), fletcher32=encoding.get('fletcher32', False), contiguous=encoding.get('contiguous', False), chunksizes=encoding.get('chunksizes'), endian=encoding.get('endian', 'native'), least_significant_digit=encoding.get('least_significant_digit'), fill_value=fill_value) nc4_var = self.ds.variables[name] nc4_var.set_auto_maskandscale(False) if variable.values.ndim == 0: nc4_var[:] = variable.values else: nc4_var[:] = variable.values[:] nc4_var.setncatts(variable.attrs)
def set_variable(self, name, variable): variable = encode_cf_variable(variable) data = coerce_nc3_dtype(variable.values) self.set_necessary_dimensions(variable) self.ds.createVariable(name, data.dtype, variable.dimensions) scipy_var = self.ds.variables[name] if data.ndim == 0: scipy_var.assignValue(data) else: scipy_var[:] = data[:] for k, v in variable.attrs.iteritems(): self._validate_attr_key(k) setattr(scipy_var, k, self._cast_attr_value(v))
def test_missing_fillvalue(self): v = Variable(['x'], np.array([np.nan, 1, 2, 3])) v.encoding = {'dtype': 'int16'} with self.assertWarns('floating point data as an integer'): conventions.encode_cf_variable(v)