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 required_netcdf_variables contains only root variables without / #2704

Open
gerritholl opened this issue Dec 21, 2023 · 3 comments

Comments

@gerritholl
Copy link
Collaborator

gerritholl commented Dec 21, 2023

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:

  • Satpy Version: main (v0.46.0-46-g989d9f0bd)

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
Copy link
Collaborator Author

This seems to have been introduced in #2237 by @ameraner and @pnuu.

@gerritholl gerritholl assigned adybbroe, pnuu and ameraner and unassigned adybbroe Dec 21, 2023
@djhoese
Copy link
Member

djhoese commented Dec 21, 2023

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
Copy link
Collaborator Author

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

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

No branches or pull requests

5 participants