mampisarkar111 / netcdf4-python

Automatically exported from code.google.com/p/netcdf4-python
Other
0 stars 0 forks source link

variable.units crashes if 'units' attribute not defined #179

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
the units attribute of a variable should return None or an empty string when 
the 'units' attribute of a variable is not defined (which should not be assumed)

>>> var = ncfile.variables[var_name]

>>> print var
<type 'netCDF4.Variable'>
float32 BurnedFraction(time, lat, lon)
    long_name: monthly burned fraction
    _FillValue: -9999.0
unlimited dimensions: time
current shape = (12, 360, 720)

>>> print var.units
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "netCDF4.pyx", line 2616, in netCDF4.Variable.__getattr__ (netCDF4.c:32278)
  File "netCDF4.pyx", line 2437, in netCDF4.Variable.getncattr (netCDF4.c:30225)
  File "netCDF4.pyx", line 917, in netCDF4._get_att (netCDF4.c:14373)
AttributeError: NetCDF: Attribute not found

>>> 'units' in var.ncattrs()
False

Original issue reported on code.google.com by etourign...@gmail.com on 7 May 2013 at 6:47

GoogleCodeExporter commented 8 years ago
>>> print var.units
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "netCDF4.pyx", line 2616, in netCDF4.Variable.__getattr__ (netCDF4.c:32278)
  File "netCDF4.pyx", line 2437, in netCDF4.Variable.getncattr (netCDF4.c:30225)
  File "netCDF4.pyx", line 917, in netCDF4._get_att (netCDF4.c:14373)
AttributeError: NetCDF: Attribute not found

AttributeError seems like the right thing in this case. That's what will happen 
with any python object when you request an attribute that doesn't exist.  Why 
do you think returning None would be better?

Original comment by whitaker.jeffrey@gmail.com on 7 May 2013 at 8:34

GoogleCodeExporter commented 8 years ago
In this case it throws an error and scrpt execution stops

It would be much more user-friendly if it would return None an empty string ''.

Both would be much easier than doing this code:

if 'units' in var.ncattrs():
        var_units = var.getncattr('units')
    else:
        var_units=''

Original comment by etourign...@gmail.com on 7 May 2013 at 9:23

GoogleCodeExporter commented 8 years ago
unless I missed something about the 'units' attribute of netCDF4.Variable . I 
thought it was a special attribute... Is it defined anywhere, or treated the 
same as e.g. var.foobar ?

thanks

Original comment by etourign...@gmail.com on 7 May 2013 at 9:26

GoogleCodeExporter commented 8 years ago
The 'pythonic' way is to catch the exception, like this

try:
   units = var.units
except AttributeError:
   units = None # or whatever

Or you could just do

units = getattr(var,'units', None)

There is no such thing as a 'special' attribute in netcdf. There are metadata 
conventions, like CF, that require a units attribute to be present, but 
netcdf4-python does not try to enforce any particular metadata standard.

Original comment by whitaker.jeffrey@gmail.com on 7 May 2013 at 10:08

GoogleCodeExporter commented 8 years ago
ok it was my mistake. I had this old code which called var.units - and after 
running it on another file it gave this error.

Sorry for wasting your time on this! Thanks for this great tool.

Original comment by etourign...@gmail.com on 8 May 2013 at 11:29