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

UnboundLocalError when opening netCDF file #1781

Closed
stefanomattia opened this issue Dec 14, 2017 · 1 comment · Fixed by #1803
Closed

UnboundLocalError when opening netCDF file #1781

stefanomattia opened this issue Dec 14, 2017 · 1 comment · Fixed by #1803
Labels
Milestone

Comments

@stefanomattia
Copy link

Code Sample, a copy-pastable example if possible

import xarray as xr
import netCDF4 as nc
product = '/Users/stefano/src/s5p/products/NO2/'\
          'S5P_OFFL_L2__NO2____20171107T195219_20171107T213349_00361_01_001107_20171108T122727.nc'

# opening the product with netCDF4 works fine
no2 = nc.Dataset(product)
no2.groups
# correctly shows groups' content
OrderedDict([('PRODUCT', <class 'netCDF4._netCDF4.Group'>
              group /PRODUCT:
                  dimensions(sizes): scanline(5640), ground_pixel(450), corner(4), time(1), polynomial_exponents(6), layer(34), vertices(2)
                  variables(dimensions): int32 scanline(scanline), int32 ground_pixel(ground_pixel), int32 time(time), int32 corner(corner), int32 polynomial_exponents(polynomial_exponents), int32 layer(layer), int32 vertices(vertices), float32 latitude(time,scanline,ground_pixel), float32 longitude(time,scanline,ground_pixel), int32 delta_time(time,scanline), <class 'str'> time_utc(time,scanline), uint8 qa_value(time,scanline,ground_pixel), float32 nitrogendioxide_tropospheric_column(time,scanline,ground_pixel), float32 nitrogendioxide_tropospheric_column_precision(time,scanline,ground_pixel), float32 averaging_kernel(time,scanline,ground_pixel,layer), float32 air_mass_factor_troposphere(time,scanline,ground_pixel), float32 air_mass_factor_total(time,scanline,ground_pixel), int32 tm5_tropopause_layer_index(time,scanline,ground_pixel), float32 tm5_constant_a(layer,vertices), float32 tm5_constant_b(layer,vertices)
                  groups: SUPPORT_DATA),
             ('METADATA', <class 'netCDF4._netCDF4.Group'>
              group /METADATA:
                  dimensions(sizes): 
                  variables(dimensions): 
                  groups: QA_STATISTICS, ALGORITHM_SETTINGS, GRANULE_DESCRIPTION, ISO_METADATA, EOP_METADATA, ESA_METADATA)])
# opening the product with xarray raises an UnboundLocalError exception
no2 = xr.open_dataset(product, group='/PRODUCT')
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-26-2052b95f2d95> in <module>()
----> 1 no2 = xr.open_dataset(product, group='/PRODUCT')

/Users/stefano/anaconda/lib/python3.6/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':

/Users/stefano/anaconda/lib/python3.6/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)

/Users/stefano/anaconda/lib/python3.6/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))

/Users/stefano/anaconda/lib/python3.6/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

/Users/stefano/anaconda/lib/python3.6/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
# Opening another group with xarray works fine
no2 = xr.open_dataset(product, group='/METADATA/QA_STATISTICS')
no2
<xarray.Dataset>
Dimensions:                                                (nitrogendioxide_stratospheric_column_histogram_axis: 100, nitrogendioxide_stratospheric_column_pdf_axis: 400, nitrogendioxide_total_column_histogram_axis: 100, nitrogendioxide_total_column_pdf_axis: 400, nitrogendioxide_tropospheric_column_histogram_axis: 100, nitrogendioxide_tropospheric_column_pdf_axis: 400, vertices: 2)
[...]

Problem description

An UnboundLocalError exception is raised while trying to open a netCDF file on a specific group ('/PRODUCT'). Opening the file within another group works fine.
The same file can be correctly opened with the netCDF4 library.

Expected Output

File correctly loaded.

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Darwin OS-release: 17.2.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: None.None

xarray: 0.10.0
pandas: 0.20.1
numpy: 1.12.1
scipy: 0.19.0
netCDF4: 1.2.4
h5netcdf: 0.5.0
Nio: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.15.3
matplotlib: 2.1.0
cartopy: 0.15.1
seaborn: 0.8.1
setuptools: 27.2.0
pip: 9.0.1
conda: 4.3.29
pytest: 3.0.7
IPython: 5.3.0
sphinx: 1.5.6

@shoyer shoyer added the bug label Dec 19, 2017
@delgadom
Copy link
Contributor

I've got a reproducible example of this (sorry for the length):

In [1]: import xarray as xr
   ...: import numpy as np
   ...: import pandas as pd

   ...: import netCDF4
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
In [3]: nds = netCDF4.Dataset('test.nc', 'w', format='NETCDF4')
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, ))
In [5]: nds.variables['X']
<class 'netCDF4._netCDF4.Variable'>
vlen X(X)
    _FillValue: invalid
vlen data type: <class 'str'>
unlimited dimensions: 
current shape = (3,)
In [6]: nds.variables['X'][:]
array(['first', 'second', 'third'], dtype=object)
In [7]: nds.variables['xname']
<class 'netCDF4._netCDF4.Variable'>
vlen xname(X)
    _FillValue: invalid
vlen data type: <class 'str'>
unlimited dimensions: 
current shape = (3,)
In [8]: nds.variables['xname'][:]
array(['the_first', 'invalid', 'the_third'], dtype=object)
In [9]: nds.variables['var1']
<class 'netCDF4._netCDF4.Variable'>
float32 var1(X)
    _FillValue: nan
unlimited dimensions: 
current shape = (3,)
filling on
In [10]: nds.variables['var1'][:]
array([ 0.58315825,  0.82443351,  0.39345944], dtype=float32)
In [11]: nds.close()
In [12]: ds = xr.open_dataset('test.nc')
---------------------------------------------------------------------------

UnboundLocalError                         Traceback (most recent call last)

<ipython-input-14-1a1dee73db10> in <module>()
----> 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? I'll try my hand at a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants