14#include "MantidTypes/Core/DateAndTime.h"
15#include <boost/python/list.hpp>
18#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
19#define NO_IMPORT_ARRAY
20#include <numpy/arrayobject.h>
42template <
typename ElementType> PyObject *
clone1D(
const std::vector<ElementType> &cvector) {
43 Py_intptr_t dims[1] = {
static_cast<int>(cvector.size())};
44 return cloneND(cvector.data(), 1, dims);
55 Py_intptr_t dims[1] = {
static_cast<int>(cvector.size())};
57 PyArray_NewFromDescr(&PyArray_Type, descr, 1, dims,
nullptr,
nullptr, 0,
nullptr));
59 for (Py_intptr_t i = 0; i < dims[0]; ++i) {
60 void *itemPtr = PyArray_GETPTR1(nparray, i);
62 auto scalar = PyArray_Scalar(
reinterpret_cast<char *
>(&abstime), descr,
nullptr);
63 PyArray_SETITEM(nparray,
reinterpret_cast<char *
>(itemPtr), scalar);
66 return reinterpret_cast<PyObject *
>(nparray);
76 Py_intptr_t dims[1] = {
static_cast<int>(cvector.size())};
80 for (Py_intptr_t i = 0; i < dims[0]; ++i) {
81 void *itemPtr = PyArray_GETPTR1(nparray, i);
82 auto py_bool = PyBool_FromLong(
static_cast<long int>(cvector[i]));
83 PyArray_SETITEM(nparray,
reinterpret_cast<char *
>(itemPtr), py_bool);
86 return reinterpret_cast<PyObject *
>(nparray);
98template <
typename ElementType> PyObject *
cloneND(
const ElementType *carray,
const int ndims, Py_intptr_t *dims) {
102 size_t length(dims[0]);
104 for (
int i = 1; i < ndims; ++i) {
108 void *arrayData = PyArray_DATA(nparray);
109 const void *data =
static_cast<void *
>(
const_cast<ElementType *
>(carray));
111 std::memcpy(arrayData, data, PyArray_ITEMSIZE(nparray) * length);
113 return reinterpret_cast<PyObject *
>(nparray);
123template <> PyObject *
cloneND(
const std::string *carray,
const int ndims, Py_intptr_t *dims) {
124 boost::python::list pystrs;
125 const std::string *iter = carray;
126 for (
int i = 0; i < ndims; ++i) {
127 const Py_intptr_t length = dims[i];
128 for (Py_intptr_t j = 0; j < length; ++j) {
129 pystrs.append(iter->c_str());
133 PyObject *rawptr = pystrs.ptr();
141#define INSTANTIATE_CLONE1D(ElementType) \
142 template DLLExport PyObject *clone1D<ElementType>(const std::vector<ElementType> &cvector);
144#define INSTANTIATE_CLONEND(ElementType) \
145 template DLLExport PyObject *cloneND<ElementType>(const ElementType *, const int ndims, Py_intptr_t *dims);
147#define INSTANTIATE_CLONE(ElementType) \
148 INSTANTIATE_CLONE1D(ElementType) \
149 INSTANTIATE_CLONEND(ElementType)
#define INSTANTIATE_CLONE(ElementType)
#define INSTANTIATE_CLONEND(ElementType)
#define INSTANTIATE_CLONE1D(ElementType)
tagPyArrayObject PyArrayObject
_PyArray_Descr PyArray_Descr
#define MANTID_PYTHONINTERFACE_CORE_DLL
PyObject * cloneND(const ElementType *carray, const int ndims, Py_intptr_t *dims)
Returns a new numpy array with the a copy of the data from array.
PyObject * clone1D(const std::vector< ElementType > &cvector)
Returns a new numpy array with the a copy of the data from 1D vector with the exception of string ele...
MANTID_PYTHONINTERFACE_CORE_DLL PyArrayObject * func_PyArray_NewFromDescr(int datatype, int ndims, Py_intptr_t *dims)
equivalent to macro PyArray_NewFromDescr
MANTID_PYTHONINTERFACE_CORE_DLL PyArray_Descr * descr_ns()
MANTID_PYTHONINTERFACE_CORE_DLL npy_datetime to_npy_datetime(const Types::Core::DateAndTime &dateandtime)
Total nanoseconds since the unix epoch.
Defines a mapping between C++ type given by the template parameter and numpy type enum NPY_TYPES.