mewilliams / netcdf4-python

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

modify a string or replace a string attribute #110

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
A variable:
print x.field
<type 'netCDF4.Variable'>
float32 PV(u'level', u'lat', u'lon')
    longname: Potential Vorticity
    _FillValue: -999.0
    units: K m**2 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 reported on code.google.com by shejo...@gmail.com on 24 Feb 2012 at 10:42

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

Original comment by whitaker.jeffrey@gmail.com on 24 Feb 2012 at 11:58

GoogleCodeExporter commented 9 years ago
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.

Original comment by shejo...@gmail.com on 25 Feb 2012 at 6:29

GoogleCodeExporter commented 9 years ago
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 m**2 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 m**2 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 m**2 kg**-1 s**-1" ;
        PV:long_name = "Ertels Potential Vorticity" ;
  } // group Data_3D
}

Can you modify the above script to trigger the error?

Original comment by josh.n.w...@gmail.com on 25 Feb 2012 at 1:59

GoogleCodeExporter commented 9 years ago
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 m**2 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 m**2 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 <module>
    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 m**2 kg**-1 s**-1" ;
        PV:longname = "Potential Vorticity" ;
  } // group Data_3D
}

Original comment by shejo...@gmail.com on 25 Feb 2012 at 6:40

GoogleCodeExporter commented 9 years ago
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

Original comment by whitaker.jeffrey@gmail.com on 25 Feb 2012 at 7:56

GoogleCodeExporter commented 9 years ago
Unidata has opened an issue for this on their tracker:

https://www.unidata.ucar.edu/jira/browse/NCF-156

Original comment by whitaker.jeffrey@gmail.com on 9 Mar 2012 at 5:50

GoogleCodeExporter commented 9 years ago
It is now fixed in netcdf svn (should be in netcdf 4.2 final).

Original comment by whitaker.jeffrey@gmail.com on 9 Mar 2012 at 8:34

GoogleCodeExporter commented 9 years ago
Thanks for all your help! 

Original comment by shejo...@gmail.com on 11 Mar 2012 at 2:05

GoogleCodeExporter commented 9 years ago

Original comment by whitaker.jeffrey@gmail.com on 26 Feb 2014 at 2:04