[gdal-dev] Python bindings on RedHat cluster (was: AttributeError: 'MaskedArray' object has no attribute 'typecode' -- when writing GeoTiff)

Scott Sinclair scott.sinclair.za at gmail.com
Tue Jul 21 02:14:05 EDT 2009


Hi Greg,

It's always a good idea to communicate via the list, there are more
eyes and skills there. The conversations are also publicly archived,
making it easier for others having similar problems to search for
help.

In this case, the mistake is mine, I see that this list behaves
differently to others I am used to and I forgot to "Reply to all".

On my machine _gdal_array is a shared library module. It sounds like
there's a problem with the way GDAL (python bindings at least) is set
up on your cluster, because Python can't find this module.

I don't have any experience here, but if you go to a Python prompt and do

>>> import osgeo
>>> print(osgeo)

You should be able to work out where the GDAL bindings are installed
and then set up the paths for your cluster appropriately.

Cheers,
Scott

2009/7/20 Greg Fiske <gfiske at whrc.org>:
> Hi Scott - thanks very much for your response.  It is probably obvious that
> I'm new to working with numpy and gdal.
>
> I am working with python 2.6 running gdal version 1.6 and numpy version
> 1.3.0 on a Linux RedHat cluster.  When I run your code I get an error
> labeled:  "ImportError: No module named _gdal_array"
>
> Have you seen this before?  Any thoughts?
>
> Thanks and best regards,
>
> Greg
>
> -----Original Message-----
> From: Scott Sinclair [mailto:scott.sinclair.za at gmail.com]
> Sent: Monday, July 20, 2009 3:58 AM
> To: Greg Fiske
> Subject: Re: [gdal-dev] AttributeError: 'MaskedArray' object has no
> attribute 'typecode' -- when writing GeoTiff
>
>> 2009/7/17 Greg Fiske <gfiske at whrc.org>:
>> In python, I'm writing GeoTiff files from numpy arrays.  I keep getting
> the
>> error:
>> AttributeError: 'MaskedArray' object has no attribute 'typecode'
>
> Hi Greg,
>
> What versions of the GDAL bindings and NumPy are you using?
>
> Perhaps your GDAL bindings don't have masked array support, you might
> try passing a view of the MaskedArray to WriteRaster() e.g.
> WriteRaster(dst_filename, d.view(type=np.ndarray)).
>
> The following standalone version of your example works for me with
> GDAL 1.6.1 and a recent SVN version of NumPy:
>
> -------------------------------------------------------
> import numpy as np
> import numpy.ma as ma
>
> try:
>    from osgeo import gdal
>    from osgeo import osr
> except ImportError:
>    import gdal
>    import osr
>
> def WriteRaster(dst_filename, raster):
>    format = "GTiff"
>    driver = gdal.GetDriverByName(format)
>
>    ysize, xsize = raster.shape
>
>    dst_ds = driver.Create(dst_filename, xsize, ysize, 1, gdal.GDT_Float32)
>
>    srs = osr.SpatialReference()
>    srs.ImportFromEPSG(4326) #WGS84 lat long.
>    dst_ds.SetProjection(srs.ExportToWkt())
>
>    dst_ds.GetRasterBand(1).WriteArray(raster)
>    dst_ds = None
>
> fname = 'test.tif'
>
> stack_size = 3
> rows = 5
> cols = 5
>
> array_list = []
>
> for i in range(stack_size):
>    a = np.arange(25, dtype=np.float32)
>    a = a.reshape((rows, cols))
>    a[i, 2] = -9999
>
>    array_list.append(a)
>
> b = np.vstack([array_list])
> c = np.ma.masked_array(b, -9999)
> d = c.mean(axis=0)
>
> # default fill value is different from -9999
> ma.set_fill_value(d, -9999)
>
> WriteRaster(fname, d)
> -------------------------------------------------------
>
> I'm fairly sure that this doesn't do what you want though. The line 'c
> = np.ma.masked_array(b, -9999)' masks all the values in the array c
> and should probably be changed to 'c = np.ma.masked_values(b, -9999)'.
> See
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.masked_values.h
> tml
>
> To avoid other surprises, it's probably also worth checking that the
> data type in your call to driver.Create() matches the data type of the
> array you're trying to write.
>
> Cheers,
> Scott
>
>


More information about the gdal-dev mailing list