sodri126 / netcdf4-python

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

lines in the end of variables of str type can't not be all blank. #155

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
sorry for my poor english.

please read the attachment.

-----------------------------------------

# -*- coding: cp936 -*-

from netCDF4 import Dataset

import numpy as np

h5name = 'hhh.nc'

root = Dataset( h5name, mode='w' )

root.set_fill_on()

root.createDimension('station',None)
root.createDimension('time',None)

times = root.createVariable('time','i',('time',))
stas = root.createVariable('station','i',('station',))

v1 = root.createVariable('r','i',('time','station') )
v2 = root.createVariable('t_mean','i',('time','station') )
v3 = root.createVariable('wp', str,('time','station') )

u = np.ma.masked_all( (20,5 ),'i')
u[...] = 222
v1[:, :] = u[:,:]

ntimes,nstas = v1.shape

print v1.shape
print v2.shape
print v3.shape

for i in range( ntimes ):
    print i, v1[i,:]

for i in range( ntimes ):
    print i, v2[i,:]

for itime in range( ntimes-3 ):
    for ista in range( nstas ):
        v3[itime,ista] = 'xxx'

#everything is ok until now.

## !!!!  uncomment the for statement following, will failed. it seems the end 
lines of variables with str type can't be ALL BLANK.
##for i in range( ntimes ):
##    print i, v3[i,:]

for itime in range( ntimes-1, ntimes ):
    for ista in range( nstas ):
        v3[itime,ista] = 'xxx'

for i in range( ntimes ):
    print i, v3[i,:]

root.close()

Original issue reported on code.google.com by shuwen...@gmail.com on 25 Dec 2012 at 8:03

Attachments:

GoogleCodeExporter commented 9 years ago
Confirmed.  The problem goes away if the time dimension is not unlimited, for 
example is you use

root.createDimension('time',20)

Don't know yet whether this is a bug in the C library or the python module.  
Variable length string arrays have not gotten much use. 

Original comment by josh.n.w...@gmail.com on 25 Dec 2012 at 3:06

GoogleCodeExporter commented 9 years ago
Seems like a library bug to me.  Don't see any way to work around it with the 
python interface.

Original comment by josh.n.w...@gmail.com on 26 Dec 2012 at 2:08

GoogleCodeExporter commented 9 years ago
there are two possible reasons:
1) when createVariable() with str type, a fill value can not be given.
2) in function _get in netcdf4.pyx, decode raise error because there's no fill 
value with str type. 

    data[i] = strdata[i].decode(default_encoding)

    --strdata[i] is not str type

Original comment by shuwen...@gmail.com on 26 Dec 2012 at 2:38

GoogleCodeExporter commented 9 years ago
decode is not raising an error, the segfault occurs in nc_get_vara when a value 
of the vlen string is requested that has not yet been written.  I don't think 
fill values are supported by the library for vlen types.  That means you can't 
read a value that has not yet been written.  This should not cause a segfault - 
so I consider this a bug in the library.

Original comment by josh.n.w...@gmail.com on 26 Dec 2012 at 4:01

GoogleCodeExporter commented 9 years ago

Original comment by whitaker.jeffrey@gmail.com on 7 Mar 2013 at 11:39