Unidata / netcdf4-python

netcdf4-python: python/numpy interface to the netCDF C library
http://unidata.github.io/netcdf4-python
MIT License
757 stars 264 forks source link

modify a string or replace a string attribute #110

Closed dopplershift closed 10 years ago

dopplershift commented 10 years ago

From shejo...@gmail.com on February 24, 2012 15:42:16

A variable: print x.field <type 'netCDF4.Variable'> float32 PV(u'level', u'lat', u'lon') longname: Potential Vorticity _FillValue: -999.0 units: K m2 kg-1 s**-1 code: 60 scale_factor: 1.0 add_offset: 0.0 path = /Data_3D unlimited dimensions = () current size = (60, 181, 360)

Modifying the longname attribute:

setattr(x.field,'longname','Ertels Potential Vorticity')

AttributeError Traceback (most recent call last) AttributeError: NetCDF: Operation not allowed in data mode

I also tried to treat the longname as array: In [37]: x.field.longname[:] = 'Ertels Potential Vorticity' TypeError: 'unicode' object does not support item assignment

Any tips on how this is done before writing the variable to the file?

Thanks, /M

Original issue: http://code.google.com/p/netcdf4-python/issues/detail?id=110

dopplershift commented 10 years ago

From whitaker.jeffrey@gmail.com on February 24, 2012 15:58:25

That looks like a bug that has been fixed recently in the netcdf C library. What version of the netcdf library are you using?

dopplershift commented 10 years ago

From shejo...@gmail.com on February 24, 2012 22:29:43

I'm using the following version of netcdf on a mac:

nc-config --all

This netCDF 4.1.3 has been built with the following features:

--cc -> gcc --cflags -> -I/sw/opt/netcdf7/include -I/sw/include --libs -> -L/sw/opt/netcdf7/lib -lnetcdf

--cxx -> g++ --has-c++ -> yes

--fc -> --fflags -> /sw/opt/netcdf7/include --flibs -> -L/sw/opt/netcdf7/lib -lnetcdff -lnetcdf --has-f77 -> no --has-f90 -> no

--has-dap -> yes --has-nc2 -> yes --has-nc4 -> yes --has-hdf5 -> yes --has-hdf4 -> no --has-pnetcdf-> no --has-szlib ->

--prefix -> /sw --includedir-> /sw/opt/netcdf7/include --version -> netCDF 4.1.3

and python 2.7 with the latest netCDF4 version: 4-0.9.9.

dopplershift commented 10 years ago

From josh.n.w...@gmail.com on February 25, 2012 05:59:35

Can't reproduce this. Here's my script

from netCDF4 import Dataset import numpy as np f = Dataset('pv.nc','w') lat = f.createDimension('lat',181) lon = f.createDimension('lon',360) lev = f.createDimension('lev',60) grp = f.createGroup('Data_3D') pv = grp.createVariable('PV',np.float32,('lev','lat','lon'),fill_value=-999) pv.units="K m2 kg-1 s**-1" f.close() f = Dataset('pv.nc','a') pv = f.groups['Data_3D'].variables['PV'] print pv pv.long_name = 'Ertels Potential Vorticity' f.close()

Running it gives

jeff-whitakers-imac-7:netcdf4-python jsw$ python testatt.py <type 'netCDF4.Variable'> float32 PV(u'lev', u'lat', u'lon') _FillValue: -999.0 units: K m2 kg-1 s**-1 path = /Data_3D unlimited dimensions = () current size = (60, 181, 360)

and ncdumping the resulting file gives

cdump -h pv.nc netcdf pv { dimensions: lat = 181 ; lon = 360 ; lev = 60 ;

group: Data_3D { variables: float PV(lev, lat, lon) ; PV:_FillValue = -999.f ; PV:units = "K m2 kg-1 s**-1" ; PV:long_name = "Ertels Potential Vorticity" ; } // group Data_3D }

Can you modify the above script to trigger the error?

dopplershift commented 10 years ago

From shejo...@gmail.com on February 25, 2012 10:40:33

Hi,

You script works as long as you create the longname attribute when you open the file and append it. If you create the attribute in the first creation of the file, close the file, reopen it, and then try and to modify or replace the attribute then the error is reproduced. Here's my modified script:


from netCDF4 import Dataset import numpy as np f = Dataset('pv.nc','w') lat = f.createDimension('lat',181) lon = f.createDimension('lon',360) lev = f.createDimension('lev',60) grp = f.createGroup('Data_3D') pv = grp.createVariable('PV',np.float32,('lev','lat','lon'),fill_value=-999) pv.units="K m2 kg-1 s**-1" pv.longname = 'Potential Vorticity' f.close() f = Dataset('pv.nc','a') pv = f.groups['Data_3D'].variables['PV'] print pv pv.longname = 'Ertels Potential Vorticity' f.close()


Result:

'import sitecustomize' failed; use -v for traceback <type 'netCDF4.Variable'> float32 PV(u'lev', u'lat', u'lon') _FillValue: -999.0 units: K m2 kg-1 s**-1 longname: Potential Vorticity path = /Data_3D unlimited dimensions = () current size = (60, 181, 360)

Traceback (most recent call last): File "/Users/marstonjohnston/Dropbox/sync_dir/pythoncodes/test2.py", line 24, in pv.longname = 'Ertels Potential Vorticity' File "netCDF4.pyx", line 2479, in netCDF4.Variable.setattr (netCDF4.c:25670) File "netCDF4.pyx", line 2314, in netCDF4.Variable.setncattr (netCDF4.c:24030) File "netCDF4.pyx", line 985, in netCDF4._set_att (netCDF4.c:11769) AttributeError: NetCDF: Operation not allowed in data mode

ncdump -h pv.nc after creation:

netcdf pv { dimensions: lat = 181 ; lon = 360 ; lev = 60 ;

group: Data_3D { variables: float PV(lev, lat, lon) ; PV:_FillValue = -999.f ; PV:units = "K m2 kg-1 s**-1" ; PV:longname = "Potential Vorticity" ; } // group Data_3D }

dopplershift commented 10 years ago

From whitaker.jeffrey@gmail.com on February 25, 2012 11:56:05

Confirmed. This is a library bug - I will report this to unidata and post the response here.

A workaround is to create a new attribute before trying to modify the existing one, then delete the new attribute.

pv.newatt = 'new attribute' pv.longname = 'Ertels Potential Vorticity' del pv.newatt

dopplershift commented 10 years ago

From whitaker.jeffrey@gmail.com on March 09, 2012 09:50:49

Unidata has opened an issue for this on their tracker: https://www.unidata.ucar.edu/jira/browse/NCF-156

dopplershift commented 10 years ago

From whitaker.jeffrey@gmail.com on March 09, 2012 12:34:55

It is now fixed in netcdf svn (should be in netcdf 4.2 final).

dopplershift commented 10 years ago

From shejo...@gmail.com on March 11, 2012 07:05:49

Thanks for all your help!

dopplershift commented 10 years ago

From whitaker.jeffrey@gmail.com on February 25, 2014 18:04:10

Status: Fixed