From: Rich E <reakinator@gm...>  20081221 18:12:48

Well I've made progress. In the numpy svn there is a good deal of documentation about how to use the typemaps in numpy.i (btw, none of which is related to the posted link I was previously/am confused about). It is quite simple to add an %apply directive in the interface file like: %apply (int DIM1, float* INPLACE_ARRAY1) {(int sizeWaveform, float* pWaveform)}; which will then search and replace any function that has the two variables in this format, effectively allowing the function to operate on a numpy array. But what if I have multiple array pointers in the same function? For example, here is a function in my library that converts spectra in rectangular coordinates to polar form: /*! \brief compute magnitude spectrum of a DFT * * \param sizeMag size of output Magnitude (half of input real FFT) * \param pFReal pointer to input FFT real array (real/imag floats) * \param pFMAg pointer to float array of magnitude spectrum */ void sms_spectrumMag( int sizeMag, float *pInRect, float *pOutMag) { int i, it2; float fReal, fImag; for (i=0; i<sizeMag; i++) { it2 = i << 1; fReal = pInRect[it2]; fImag = pInRect[it2+1]; pOutMag[i] = sqrtf(fReal * fReal + fImag * fImag); } } There are two arrays, one is half the size of the other. But, SWIG doesn't know this, according to the type map it will think *pInRect is of size sizeMag and will not know anything about *pOutMag. So what is the solution in this case? I assume that I will have to make my own typemap based on the numpy.i file, but I cannot tell how complicated or straightfoward this will be yet. I certainly don't want to start adding unnecessary size integers to the c functions. regards, Rich On Mon, Dec 8, 2008 at 3:09 PM, Rich E <reakinator@...> wrote: > On Mon, Dec 1, 2008 at 8:43 PM, Christopher Barker > <Chris.Barker@...> wrote: >> Rich E wrote: >>> >>> So I have a nice C library wrapped in python and I'd like to start >>> doing some plots of things in python... the problem is the data is all >>> stuck in C arrays. >> >>> But how would I go about returning a python list of the entire array, >>> or even better, an NumPy array? >> >> there is a numpy.i file distributed with numpy ( or with the docs, anyway). >> There was also a recent post on the numpy list with some extra hints and >> examples for using it. >> >> This is another nice article about it: >> >> http://matt.eifelle.com/2008/11/04/exposinganarrayinterfacewithswigforaccstructure/ > > After looking at this example, I have to admit that I am a bit > befuddled. I also couldn't get the code to work, but this is probably > because I am not understanding the example. So, I'll ask a couple > questions here and if there is a better place to do this, please let > me know. > > In the example, Matt is converting data in a c struct.. all I want to > do is, provided a pointer to a c array and the size of the array, > convert it to a numpy array. Hopefully then I will not have to write > a wrapper for every type of struct I have (and even worse, every > function that handles pointers to arrays). > > I would think such a function could be quite general, enough to even > be included with numpy. Matt's code is quite specific to the struct > he is handling and it is difficult to see what is necessary for my > very minimal needs. > >> >>> My thoughts are that I will have to convert between C arrays and >>> python types (list or NumPy) quite often, probably at a refresh rate >>> suitable for animation. >> >> the above allow a numpy array and your Ccode to share the same pointer, so >> it is very fast. >> >> Chris >> >> >>  >> Christopher Barker, Ph.D. >> Oceanographer >> >> Emergency Response Division >> NOAA/NOS/OR&R (206) 5266959 voice >> 7600 Sand Point Way NE (206) 5266329 fax >> Seattle, WA 98115 (206) 5266317 main reception >> >> Chris.Barker@... >> > 