Repository: pydata/xarray
Issue: Dataset.to_netcdf() fails to interpret 'encoding' option

Attempting to save a dataset with compression

``` python
ds.to_netcdf('demo.nc', mode='w', encoding={'data': {'zlib': True}})
```

raises an exception as follows::

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-ec637fd612fd> in <module>()
----> 1 ds.to_netcdf('demo.nc', mode='w', encoding={'data': {'zlib': True}})

/Users/takeshi/Developer/xray/xray/core/dataset.py in to_netcdf(self, path, mode, format, group, engine, encoding)
    782         from ..backends.api import to_netcdf
    783         return to_netcdf(self, path, mode, format=format, group=group,
--> 784                          engine=engine, encoding=encoding)
    785 
    786     dump = utils.function_alias(to_netcdf, 'dump')

/Users/takeshi/Developer/xray/xray/backends/api.py in to_netcdf(dataset, path, mode, format, group, engine, writer, encoding)
    352     store = store_cls(path, mode, format, group, writer)
    353     try:
--> 354         dataset.dump_to_store(store, sync=sync, encoding=encoding)
    355         if isinstance(path, BytesIO):
    356             return path.getvalue()

/Users/takeshi/Developer/xray/xray/core/dataset.py in dump_to_store(self, store, encoder, sync, encoding)
    728             variables, attrs = encoder(variables, attrs)
    729 
--> 730         store.store(variables, attrs, check_encoding)
    731         if sync:
    732             store.sync()

/Users/takeshi/Developer/xray/xray/backends/common.py in store(self, variables, attributes, check_encoding_set)
    226         cf_variables, cf_attrs = cf_encoder(variables, attributes)
    227         AbstractWritableDataStore.store(self, cf_variables, cf_attrs,
--> 228                                         check_encoding_set)

/Users/takeshi/Developer/xray/xray/backends/common.py in store(self, variables, attributes, check_encoding_set)
    201                                 if not (k in neccesary_dims and
    202                                         is_trivial_index(v)))
--> 203         self.set_variables(variables, check_encoding_set)
    204 
    205     def set_attributes(self, attributes):

/Users/takeshi/Developer/xray/xray/backends/common.py in set_variables(self, variables, check_encoding_set)
    211             name = _encode_variable_name(vn)
    212             check = vn in check_encoding_set
--> 213             target, source = self.prepare_variable(name, v, check)
    214             self.writer.add(source, target)
    215 

/Users/takeshi/Developer/xray/xray/backends/netCDF4_.py in prepare_variable(self, name, variable, check_encoding)
    259 
    260         encoding = _extract_nc4_encoding(variable,
--> 261                                          raise_on_invalid=check_encoding)
    262         nc4_var = self.ds.createVariable(
    263             varname=name,

/Users/takeshi/Developer/xray/xray/backends/netCDF4_.py in _extract_nc4_encoding(variable, raise_on_invalid, lsd_okay, backend)
    160         if invalid:
    161             raise ValueError('unexpected encoding parameters for %r backend: '
--> 162                              ' %r' % (backend, invalid))
    163     else:
    164         for k in list(encoding):

ValueError: unexpected encoding parameters for 'netCDF4' backend:  ['z', 'l', 'i', 'b']
```


Changed files:
xray/test/test_backends.py, xray/backends/netCDF4_.py

diff --git a/xray/backends/netCDF4_.py b/xray/backends/netCDF4_.py
index 4d5141e9d..61c446534 100644
--- a/xray/backends/netCDF4_.py
+++ b/xray/backends/netCDF4_.py
@@ -155,8 +155,7 @@ def _extract_nc4_encoding(variable, raise_on_invalid=False, lsd_okay=True,
             del encoding[k]
 
     if raise_on_invalid:
-        invalid = [k for enc in encoding for k in enc
-                   if k not in valid_encodings]
+        invalid = [k for k in encoding if k not in valid_encodings]
         if invalid:
             raise ValueError('unexpected encoding parameters for %r backend: '
                              ' %r' % (backend, invalid))
diff --git a/xray/test/test_backends.py b/xray/test/test_backends.py
index 033737409..781315005 100644
--- a/xray/test/test_backends.py
+++ b/xray/test/test_backends.py
@@ -512,6 +512,13 @@ class BaseNetCDF4Test(CFEncodedDataTest):
                                        if k in expected['time'].encoding)
                 self.assertDictEqual(actual_encoding, expected['time'].encoding)
 
+    def test_dump_encodings(self):
+        # regression test for #709
+        ds = Dataset({'x': ('y', np.arange(10.0))})
+        kwargs = dict(encoding={'x': {'zlib': True}})
+        with self.roundtrip(ds, save_kwargs=kwargs) as actual:
+            self.assertTrue(actual.x.encoding['zlib'])
+
     def test_dump_and_open_encodings(self):
         # Create a netCDF file with explicit time units
         # and make sure it makes it into the encodings