sunchaoatmo / netcdf4-python

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

writing single element value to #117

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Let us say I create a variable with a single element and write a python string 
to it.
Like so:
orthog_profile = Dataset('test.nc', 'w', 'NETCDF4_CLASSIC')
dum_dim = orthog_profile.createDimension('profile', 1)
cast_name = orthog_profile.createVariable('cast_name', str, 'profile')

data = np.array(['V09B01', 'V09B01', 'V09B01', 'V09B01', 'V09B01', 'V09B01', 
'V09B01', 'V09B01', 'V09B01', 'V09B01', 'V09B01', 'V09B01',
 'V09B01', 'V09B01', 'V09B01', 'V09B01', 'V09B01', 'V09B01'],
      dtype='|S6')

cast_name[:] = data[0]

I get the following error:

IndexError: single element VLEN slices must be specified by integers only

OK then if I create a variable with a dimension size of 2.

dum_dim_2 = orthog_profile.createDimension('profile_2', 2)
cast_name_2 = orthog_profile.createVariable('cast_name_2', str, 'profile_2')

cast_name_2[:] = data[0:2]

I get the same error

Basically I am having trouble writing multiple character strings.

I am using using version 0.9.9 with python version 2.6.6. on windows 7

Thank you in advance for your help.

Nathan

Original issue reported on code.google.com by space.8...@gmail.com on 10 Apr 2012 at 10:57

GoogleCodeExporter commented 8 years ago
It works if data is an object array (dtype ='O' instead of '|S6')

The error message is not very informative, I'll give you that.  I'll look into 
that.  

Original comment by whitaker.jeffrey@gmail.com on 11 Apr 2012 at 1:48

GoogleCodeExporter commented 8 years ago
Fixed the error message in svn: it now reads

'only numpy object arrays can be assigned to VLEN var slices'

Original comment by whitaker.jeffrey@gmail.com on 11 Apr 2012 at 12:08

GoogleCodeExporter commented 8 years ago
Ah -- Thank you so much for the help.

Nathan

Original comment by space.8...@gmail.com on 11 Apr 2012 at 2:49

GoogleCodeExporter commented 8 years ago
Actually, that doesn't seem to work either.

test = array([V09B01, V09B01, V09B01, V09B01, V09B01, V09B01, V09B01, V09B01,
       V09B01, V09B01, V09B01, V09B01, V09B01, V09B01, V09B01, V09B01,
       V09B01, V09B01], dtype=object)

cast_names[:] = test[0]

IndexError                                Traceback (most recent call last)
C:\netcdf_sandbox\<ipython-input-53-d7c2b6ded82a> in <module>()
----> 1 cast_names[:] = test[0]

C:\Python26\lib\site-packages\netCDF4.pyd in netCDF4.Variable.__setitem__ (netCD
F4.c:28519)()

C:\Python26\lib\site-packages\netCDF4.pyd in netCDF4.Variable._assign_vlen (netC
DF4.c:28038)()

IndexError: single element VLEN slices must be specified by integers only

Original comment by space.8...@gmail.com on 11 Apr 2012 at 10:27

GoogleCodeExporter commented 8 years ago
please update from svn and try again.

Original comment by whitaker.jeffrey@gmail.com on 11 Apr 2012 at 10:59

GoogleCodeExporter commented 8 years ago
You're right

cast_names[:] = test[0]

doesn't work, even with SVN.  The problem is that test[0] is not a numpy object 
array, but a python string. 

cast_names[0] = test[0] does work though, or even
cast_names[:] = test[0:1]

If you want to assign to a slice, you need a slice on the rhs.  If you assign 
with a string on the rhs, you need a single element on the lhs.

This is particular to VLEN arrays, for technical reasons.  Assigning to 
primitive data type variables is much more flexible.

Original comment by whitaker.jeffrey@gmail.com on 12 Apr 2012 at 1:01

GoogleCodeExporter commented 8 years ago
Right.  Got it. Thanks again for the help.

Original comment by space.8...@gmail.com on 12 Apr 2012 at 3:28

GoogleCodeExporter commented 8 years ago

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