Skip to content

Variables with no "units" attribute cause a crash by AttributeError (cfunits 3.3.7) #119

@FObersteiner

Description

@FObersteiner

except TypeError:

this line just caused on my system:

Traceback (most recent call last):
  File "/home/user/Code/Python/nc-checkers/cf-checker/.venv/bin/cfchecks", line 10, in <module>
    sys.exit(main())
             ~~~~^^
  File "/home/user/Code/Python/nc-checkers/cf-checker/.venv/lib/python3.13/site-packages/cfchecker/cfchecks.py", line 3402, in main
    inst.checker(file)
    ~~~~~~~~~~~~^^^^^^
  File "/home/user/Code/Python/nc-checkers/cf-checker/.venv/lib/python3.13/site-packages/cfchecker/cfchecks.py", line 531, in checker
    return self._checker()
           ~~~~~~~~~~~~~^^
  File "/home/user/Code/Python/nc-checkers/cf-checker/.venv/lib/python3.13/site-packages/cfchecker/cfchecks.py", line 823, in _checker
    self.chkTimeVariableAttributes(var)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/home/user/Code/Python/nc-checkers/cf-checker/.venv/lib/python3.13/site-packages/cfchecker/cfchecks.py", line 2931, in chkTimeVariableAttributes
    varUnits = Units(var.units)
                     ^^^^^^^^^
  File "src/netCDF4/_netCDF4.pyx", line 5030, in netCDF4._netCDF4.Variable.__getattr__
  File "src/netCDF4/_netCDF4.pyx", line 4752, in netCDF4._netCDF4.Variable.getncattr
  File "src/netCDF4/_netCDF4.pyx", line 1667, in netCDF4._netCDF4._get_att
  File "src/netCDF4/_netCDF4.pyx", line 2164, in netCDF4._netCDF4._ensure_nc_success
AttributeError: NetCDF: Attribute not found

...which basically means that the Units class returns an AttributeError, not a TypeError. This might be a version thing (I'm using cfunits 3.3.7 as the requirements.txt does not specify a specific version). I worked around by using except (AttributeError, TypeError): instead of just the TypeError in the line quoted at the start of this post.

Edit: here's a file to reproduce the issue: https://thredds.atmohub.kit.edu/thredds/fileServer/iagos-caribic/IAGOS-CARIBIC_WSM_files_collection_20240717/CARIBIC-1/WSM_19980109_012_CMB_MUC_V01.nc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions