sodri126 / netcdf4-python

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

could not broadcast where mask from shape (1,1) into shape () #184

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
# python 2.7.4 on win32
# netCDF4 1.0.4

#please look at the code:
#-----------------------------

from netCDF4 import Dataset
import numpy as np

FILE_NAME = 'tst_mask.nc'

n1 = 100
n2 = 20

nc = Dataset(FILE_NAME,'w' )
nc.createDimension('n1',n1 )
nc.createDimension('n2',n2)

v = nc.createVariable('dd', int, ('n1','n2'), zlib=True)

data = np.ma.arange(100)
data.shape=(5,20)
data = np.ma.masked_less( data, 20 )
data = np.ma.masked_equal( data, 70 )

#------ test np.array ---
data2 = np.ma.masked_all_like( data )
for i in range( 5 ):
    for j in range( n2 ):
        data2[ i, j ] = data[i, j]
print 'numpy array ok'

#------ test nc variable 1 ----
v[:5,:] = data[:,:]
print 'nc varaible 1 ok'

#----- test nc variable 2 ---
for i in range( 5 ):
    for j in range( n2 ):
        v[ i, j ] = data[ i, j ]
        ## run accross an error here:

        ## -----------------------------------------
        ##>
        ##    v[ i, j ] = data[ i, j ]
        ##  File "netCDF4.pyx", line 2915, in netCDF4.Variable.__setitem__ (netCDF4.c:36449)
        ##  File "C:\Program Files\Python27\lib\site-packages\numpy-1.7.1-py2.7-win32.egg\numpy\ma\core.py", line 3398, in filled
        ##    np.copyto(result, fill_value, where=m)
        ##ValueError: could not broadcast where mask from shape (1,1) into shape ()
        ## -----------------------------------------

print 'nc variable 2 ok'

nc.close()

Original issue reported on code.google.com by shuwen...@gmail.com on 24 May 2013 at 4:16

GoogleCodeExporter commented 9 years ago
replacing that loop with

v[0:5,0:n2]=data[0:5,0:n2]

works.

data[i,j] has type

<class 'numpy.ma.core.MaskedConstant'>

and I don't think netcdf4-python knows how to deal with that.

Original comment by whitaker.jeffrey@gmail.com on 4 Jun 2013 at 6:34

GoogleCodeExporter commented 9 years ago
when v is vlen type:

v[0:5,0:n2]=data[0:5,0:n2]

doesn't work well always.

sometime it works well.
but it raises exception sometime. 

Original comment by shuwen...@gmail.com on 6 Jun 2013 at 3:18

GoogleCodeExporter commented 9 years ago

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