Closed GoogleCodeExporter closed 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
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
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
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
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
Original issue reported on code.google.com by
etourign...@gmail.com
on 7 May 2013 at 6:47