html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/issues/1781#issuecomment-354370636,https://api.github.com/repos/pydata/xarray/issues/1781,354370636,MDEyOklzc3VlQ29tbWVudDM1NDM3MDYzNg==,3698640,2017-12-28T22:55:28Z,2017-12-28T22:55:28Z,CONTRIBUTOR,"I've got a reproducible example of this (sorry for the length): ```python In [1]: import xarray as xr ...: import numpy as np ...: import pandas as pd ...: import netCDF4 ``` ```python In [2]: xr.show_versions() ``` INSTALLED VERSIONS ------------------ commit: None python: 3.5.4.final.0 python-bits: 64 OS: Darwin OS-release: 17.3.0 machine: x86_64 processor: byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 xarray: 0.10.0 pandas: 0.21.0 numpy: 1.13.3 scipy: 1.0.0 netCDF4: 1.3.1 h5netcdf: None Nio: None bottleneck: 1.2.1 cyordereddict: None dask: 0.16.0 matplotlib: 2.1.0 cartopy: None seaborn: 0.8.1 setuptools: 36.5.0.post20170921 pip: 9.0.1 conda: None pytest: 3.3.1 IPython: 6.2.1 sphinx: None ```python In [3]: nds = netCDF4.Dataset('test.nc', 'w', format='NETCDF4') ``` ```python In [4]: nds.createDimension('X', 3) ...: nds.createVariable('X', str, ('X', ), fill_value='invalid') ...: nds.createVariable('xname', str, ('X', ), fill_value='invalid') ...: nds.createVariable('var1', np.float32, ('X', ), fill_value=np.nan) ...: ...: nds.variables['X'][:] = np.array(['first', 'second', 'third']) ...: nds.variables['xname'][:] = np.array(['the_first', 'invalid', 'the_third']) ...: nds.variables['var1'][:] = np.random.random((3, )) ``` ```python In [5]: nds.variables['X'] ``` vlen X(X) _FillValue: invalid vlen data type: unlimited dimensions: current shape = (3,) ```python In [6]: nds.variables['X'][:] ``` array(['first', 'second', 'third'], dtype=object) ```python In [7]: nds.variables['xname'] ``` vlen xname(X) _FillValue: invalid vlen data type: unlimited dimensions: current shape = (3,) ```python In [8]: nds.variables['xname'][:] ``` array(['the_first', 'invalid', 'the_third'], dtype=object) ```python In [9]: nds.variables['var1'] ``` float32 var1(X) _FillValue: nan unlimited dimensions: current shape = (3,) filling on ```python In [10]: nds.variables['var1'][:] ``` array([ 0.58315825, 0.82443351, 0.39345944], dtype=float32) ```python In [11]: nds.close() ``` ```python In [12]: ds = xr.open_dataset('test.nc') ``` --------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) in () ----> 1 ds = xr.open_dataset('test.nc') ~/miniconda2/envs/spatial3/lib/python3.5/site-packages/xarray/backends/api.py in open_dataset(filename_or_obj, group, decode_cf, mask_and_scale, decode_times, autoclose, concat_characters, decode_coords, engine, chunks, lock, cache, drop_variables) 303 lock = _default_lock(filename_or_obj, engine) 304 with close_on_error(store): --> 305 return maybe_decode_store(store, lock) 306 else: 307 if engine is not None and engine != 'scipy': ~/miniconda2/envs/spatial3/lib/python3.5/site-packages/xarray/backends/api.py in maybe_decode_store(store, lock) 223 store, mask_and_scale=mask_and_scale, decode_times=decode_times, 224 concat_characters=concat_characters, decode_coords=decode_coords, --> 225 drop_variables=drop_variables) 226 227 _protect_dataset_variables_inplace(ds, cache) ~/miniconda2/envs/spatial3/lib/python3.5/site-packages/xarray/conventions.py in decode_cf(obj, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables) 1153 vars, attrs, coord_names = decode_cf_variables( 1154 vars, attrs, concat_characters, mask_and_scale, decode_times, -> 1155 decode_coords, drop_variables=drop_variables) 1156 ds = Dataset(vars, attrs=attrs) 1157 ds = ds.set_coords(coord_names.union(extra_coords).intersection(vars)) ~/miniconda2/envs/spatial3/lib/python3.5/site-packages/xarray/conventions.py in decode_cf_variables(variables, attributes, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables) 1086 k, v, concat_characters=concat_characters, 1087 mask_and_scale=mask_and_scale, decode_times=decode_times, -> 1088 stack_char_dim=stack_char_dim) 1089 if decode_coords: 1090 var_attrs = new_vars[k].attrs ~/miniconda2/envs/spatial3/lib/python3.5/site-packages/xarray/conventions.py in decode_cf_variable(name, var, concat_characters, mask_and_scale, decode_times, decode_endianness, stack_char_dim) 998 if (has_fill or scale_factor is not None or add_offset is not None): 999 if has_fill and np.array(fill_value).dtype.kind in ['U', 'S', 'O']: -> 1000 if string_encoding is not None: 1001 raise NotImplementedError( 1002 'variable %r has a _FillValue specified, but ' UnboundLocalError: local variable 'string_encoding' referenced before assignment This seems to be produced by the fact that `string_encoding` is not defined for unicode-encoded variable-length string types. Could the problem be the lack of `'O'` and `'U'` in conventions.py line [952](https://github.com/pydata/xarray/blob/6eac8574f85623b18b59ba1ac9cf95e09c87980b/xarray/conventions.py#L952)? I'll try my hand at a PR.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,282061228