[Numpy-discussion] Convert Numeric array to numpy array

Philip Riggs priggs at cnr.colostate.edu
Mon May 21 12:13:09 EDT 2007


I am not finding an answer to this question in the latest numpy  
documentation. I have a package that still uses Numeric (GDAL with  
python bindings). Is this valid code that will work as expected to  
convert the Numeric array to a numpy array (very simplified from my  
script)?

import numpy
from Numeric import *
import gdal
import gdalconst
import gdalnumeric

tempmap = array ([[1,0,1,0],[1,1,0,1]])
map = numpy.array(tempmap)
mask = piecewise (map, map > 0, (1,0))

scalarmap = numpy.array([[0.5,0.2,0.3,0,4],[0.7,0.5,0.6,0.3]])
surfacemap = scalarmap * mask

Thanks
Philip

PS:

I considered converting the old bindings to use numpy arrays, but  
receive an error. I believe the problem lies in the datatype  
conversion shown below. Is there an easy way to fix this?


# GDALDataType
GDT_Unknown = 0
GDT_Byte = 1
GDT_UInt16 = 2
GDT_Int16 = 3
GDT_UInt32 = 4
GDT_Int32 = 5
GDT_Float32 = 6
GDT_Float64 = 7
GDT_CInt16 = 8
GDT_CInt32 = 9
GDT_CFloat32 = 10
GDT_CFloat64 = 11
GDT_TypeCount = 12

def GDALTypeCodeToNumericTypeCode( gdal_code ):
     if gdal_code == GDT_Byte:
         return UnsignedInt8
     elif gdal_code == GDT_UInt16:
         return UnsignedInt16
     elif gdal_code == GDT_Int16:
         return Int16
     elif gdal_code == GDT_UInt32:
         return UnsignedInt32
     elif gdal_code == GDT_Int32:
         return Int32
     elif gdal_code == GDT_Float32:
         return Float32
     elif gdal_code == GDT_Float64:
         return Float64
     elif gdal_code == GDT_CInt16:
         return Complex32
     elif gdal_code == GDT_CInt32:
         return Complex32
     elif gdal_code == GDT_CFloat32:
         return Complex32
     elif gdal_code == GDT_CFloat64:
         return Complex64
     else:
         return None

def NumericTypeCodeToGDALTypeCode( numeric_code ):
     if numeric_code == UnsignedInt8:
         return GDT_Byte
     elif numeric_code == Int16:
         return GDT_Int16
     elif numeric_code == UnsignedInt16:
         return GDT_UInt16
     elif numeric_code == Int32:
         return GDT_Int32
     elif numeric_code == UnsignedInt32:
         return GDT_UInt32
     elif numeric_code == Int:
         return GDT_Int32
     elif numeric_code == UnsignedInteger:
         return GDT_UInt32
     elif numeric_code == Float32:
         return GDT_Float32
     elif numeric_code == Float64:
         return GDT_Float64
     elif numeric_code == Complex32:
         return GDT_CFloat32
     elif numeric_code == Complex64:
         return GDT_CFloat64
     else:
         return None
  
   



More information about the NumPy-Discussion mailing list