pytroll / satpy

Python package for earth-observing satellite data processing
http://satpy.readthedocs.org/en/latest/
GNU General Public License v3.0
1.08k stars 298 forks source link

UnboundLocalError when required_netcdf_variables contains only root variables without / #2704

Open gerritholl opened 11 months ago

gerritholl commented 11 months ago

Describe the bug

When a reader derives from NetCDF4FileHandler and uses required_netcdf_variables using only root variables, thus no groups and no required variables containing a /, instantiation will fail with UnboundLocalError.

To Reproduce

# Your code here
from satpy.readers.netcdf_utils import NetCDF4FileHandler
import numpy as np
import xarray as xr

ft_info = {"file_reader": NetCDF4FileHandler, "file_patterns": ["{stem}.nc"], "required_netcdf_variables": ["panarea", "strómboli"]}

nm = "/tmp/test.nc"
ds = xr.Dataset()
ds["panarea"] = xr.DataArray(np.array([[0, 1, 2]]), dims=["y", "x"])
ds["strómboli"] = xr.DataArray(np.array([[1, 1, 2]]), dims=["y", "x"])
ds.to_netcdf(nm)

h = NetCDF4FileHandler("/tmp/test.nc", {"stem": "test"}, ft_info)

Expected behavior

I expect that I can create the file handler, or that it perhaps errors later due to not having implemented some mandatory pseudo-abstract methods.

Actual results

Traceback (most recent call last):
  File "/data/gholl/checkouts/protocode/mwe/satpy-req-nc-vars-bug.py", line 16, in <module>
    h = NetCDF4FileHandler("/tmp/test.nc", {"stem": "test"}, ft_info)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/gholl/checkouts/satpy/satpy/readers/netcdf_utils.py", line 114, in __init__
    self._collect_listed_variables(file_handle, listed_variables)
  File "/data/gholl/checkouts/satpy/satpy/readers/netcdf_utils.py", line 182, in _collect_listed_variables
    if p != "attr":
       ^
UnboundLocalError: cannot access local variable 'p' where it is not associated with a value

Environment Info:

Additional context

Although I haven't tested it, it would seem to not collect any root variables following an attribute.

I ran into this when writing unit tests for #2686.

gerritholl commented 11 months ago

This seems to have been introduced in https://github.com/pytroll/satpy/pull/2237 by @ameraner and @pnuu.

djhoese commented 11 months ago

Shouldn't root variables start with a /? Oh I guess they don't have to do they? Either way, can this be fixed by setting an initial value for p?

gerritholl commented 11 months ago

/ is a prohibited character in variable names. And _collect_listed_variables can't handle it either, as it will try to look for ''.