Commit 15b37b03 authored by Kenneth D. Renard (Cont ARL/CISD) kdrenard's avatar Kenneth D. Renard (Cont ARL/CISD) kdrenard Committed by Kenneth Leiter

ENH: get numpy Array from XdmfArray (for Python)

This required addition of getElementSize() member function in
XdmfArray and XdmfArrayType
parent c0612087
......@@ -413,6 +413,13 @@ std::map<std::string, std::string> XdmfArray::getItemProperties() const
return arrayProperties;
}
int XdmfArray::getElementSize() const
{
boost::shared_ptr<const XdmfArrayType> aType = getArrayType();
if (aType == NULL) return(0);
return(aType->getElementSize());
}
std::string XdmfArray::getDimensionString() const
{
return(mDimensionString);
......
......@@ -307,6 +307,13 @@ public:
template<typename T>
void setValuesInternal(const boost::shared_ptr<std::vector<T> > array);
/**
* Get the size of an element in this array
*
* @return the size of an array element (0 if ArrayType is undefined)
*/
int getElementSize() const;
/**
* Get the original dimensions string from the stored array
*
......
......@@ -160,3 +160,8 @@ void XdmfArrayType::getProperties(std::map<std::string, std::string> & collected
precision << mPrecision;
collectedProperties["Precision"] = precision.str();
}
int XdmfArrayType::getElementSize() const
{
return (mPrecision);
}
......@@ -61,6 +61,8 @@ public:
*/
bool operator!=(const XdmfArrayType & arrayType) const;
int getElementSize() const;
protected:
/**
......
......@@ -127,6 +127,40 @@ swig -v -c++ -python -o XdmfCorePython.cpp XdmfCore.i
#ifndef SWIGJAVA
// Provide accessors from python lists to XdmfArrays
%extend XdmfArray {
PyObject * getBuffer ()
{
void *vp = $self->getValuesPointer();
Py_ssize_t sz = $self->size() * $self->getElementSize();
PyObject * c = PyBuffer_FromMemory(vp, sz);
return(c);
}
%pythoncode {
def getNumpyArray(self):
from numpy import frombuffer as ___frombuffer
buf = self.getBuffer()
aType = self.getArrayType()
if aType == XdmfArrayType.Int8() :
na = ___frombuffer(buf, 'int8')
elif aType == XdmfArrayType.Int16() :
na = ___frombuffer(buf, 'int16')
elif aType == XdmfArrayType.Int32() :
na = ___frombuffer(buf, 'int32')
elif aType == XdmfArrayType.Int64() :
na = ___frombuffer(buf, 'int64')
elif aType == XdmfArrayType.Float32() :
na = ___frombuffer(buf, 'float32')
elif aType == XdmfArrayType.Float64() :
na = ___frombuffer(buf, 'float64')
elif aType == XdmfArrayType.UInt8() :
na = ___frombuffer(buf, 'uint8')
elif aType == XdmfArrayType.UInt16() :
na = ___frombuffer(buf, 'uint16')
elif aType == XdmfArrayType.UInt32() :
na = ___frombuffer(buf, 'uint32')
return na;
};
void copyValueAsInt8(int index, char value) {
$self->insert(index, &value);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment