Hi,
I am posting here to seek for help on type conversion between Python (Numeric Python) and C.
Attachment A is a math function written in C, which is called by a Python program. I had studied SWIG and Python/C API a bit. I was able to pass numeric array (Numeric Python) into the C function and access/change these arrays.
Problem I am having is that I couldn't quite get the array converted back to some PyObject or something that Python Interpreter can understand. So, alternatively I passed an empty numeric array as well as other arguments, and had return value(result) stored in that chunk of place. It sort of worked. However, if such function was called few times, it would result in segmentation fault.
This is a lousy way to get around the return type matter, and probably is causing seg fault. Besides a correct type conversion, a proper SWIG interface probably could've saved all the fuss, maybe? ( attachment B is the SWIG interface I am using for this function)
I am trying to study more on C API section of NumPy document, and to gain more knowledge off SWIG(which I only understand very basic way of it). Any feedback will be great. Thanks for your time, friends. Truly appreicate your precious time.
Ronnie
P.S. Those PyObject pointers are created with Numeric.Array( data, Numeric,Float64 ).
Attachment A -
/*
-
*******************************************************************************
-
*
-
* Function Name :
-
* cressman()
-
*
-
* Usage :
-
* PyObject* cressman
-
* (int nx,int ny,PyObject* result,PyObject* us,PyObject* rlats, PyObject*
-
* rlons, int nst,float disinf,float slon,float elon,float slat,float elat)
-
*
-
* Description :
-
* Objective analysis by Cressman method
-
*
-
* Aumument :
-
* nx(I) : x-dimension of grid data
-
* ny(I) : y-dimension of grid data
-
* result(I): an empty result set
-
* us(I) : input station data
-
* rlons(I) : x-coordinate of station
-
* rlats(I) : y-coordinate of station
-
* nst(I) : number of station data
-
* disinf(I): influence radius distance(km)
-
* slon(I) : x-coordinate of start grid longitude
-
* elon(I) : x-coordinate of end grid longitude
-
* slat(I) : y-coordinate of start grid latitude
-
* elat(I) : y-coordinate of end grid latitude
-
*
-
* Data Files : None
-
*
-
* Called Function :
-
* ardist()
-
*
-
* Return Value :
-
* data : grid data value
-
*
-
* Linking Lib : None
-
*
-
***************************************************************************
-
*/
-
PyObject* cressman
-
(int nx,int ny,PyObject* result,PyObject* us,PyObject* rlats, PyObject* rlons, int nst,float disinf,float slon,float elon,float slat,float elat)
-
{
-
if( nst < 1)
-
return;
-
-
// initialization
-
float xdelta,ydelta,disinf2,wsum,usum,xx,yy;
-
float **data;
-
float data_ave, sum;
-
int i,j,k,count;
-
-
// PyObject(python-C API) related
-
PyArrayObject *p_us, *p_rlats, *p_rlons, *p_result;
-
double *array_us, *array_rlats, *array_rlons, *array_result;
-
-
p_us= (PyArrayObject *) us;
-
p_rlats= (PyArrayObject *) rlats;
-
p_rlons= (PyArrayObject *) rlons;
-
p_result= (PyArrayObject *) result;
-
-
if ( nst != p_us->dimensions[0])
-
return;
-
-
array_us = (double*) p_us->data;
-
array_rlats = (double*) p_rlats->data;
-
array_rlons = (double*) p_rlons->data;
-
array_result = (double*) p_result->data;
-
-
// find out data avg
-
sum=0;
-
count=0;
-
for (i=0;i<nst;i++)
-
{
-
if (array_us[i]>spv)
-
{
-
sum+=array_us[i];
-
count++;
-
}
-
}
-
if (count!=0)
-
data_ave= sum / count;
-
-
xdelta = (elon - slon)/(nx -1);
-
ydelta = (elat- slat)/(ny -1);
-
disinf2 = pow(disinf,2);
-
-
for( i = 0; i < ny; i++)
-
{
-
for( j = 0; j < nx; j++)
-
{
-
wsum = 0.;
-
usum = 0.;
-
xx = slon + j*xdelta;
-
yy = slat + i*ydelta;
-
-
for( k = 0; k < nst; k++)
-
{
-
if( array_us[k] <= spv )
-
continue;
-
-
//ardist just a function that calculates distance between 2 points
-
float adist = ardist(array_rlons[k],array_rlats[k],xx,yy);
-
if( adist > disinf )
-
continue;
-
float adist2 = pow(adist,2);
-
float w = (disinf2 - adist2)/(disinf2+adist2);
-
-
wsum = wsum + w;
-
usum = usum + (w*array_us[k]);
-
}
-
if( wsum != 0 )
-
//data[i][j] = usum/wsum;
-
array_result[i*nx+j] = usum/wsum;
-
else
-
//data[i][j] = data_ave;
-
array_result[i*nx+j] = data_ave;
-
-
}
-
}
-
return result;
-
}
-
Attachment B - SWIG interface -
/* objmod.i */
-
%module objmod
-
%{
-
/* Put header files here or function declarations like below */
-
PyObject* cressman(int nx,int ny,PyObject* result,PyObject* us,
-
PyObject* rlats,PyObject* rlons,int nst,float disinf,
-
float slon,float elon,float slat,float elat);
-
float ardist(float rlon,float rlat,float xx,float yy);
-
%}
-
-
extern PyObject* cressman(int nx,int ny,PyObject* result,PyObject* us,
-
PyObject* rlats,PyObject* rlons,int nst,float disinf,
-
float slon,float elon,float slat,float elat);
-
extern float ardist(float rlon,float rlat,float xx,float yy);
-
0 2106 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: mike420 |
last post by:
THE GOOD:
1. pickle
2. simplicity and uniformity
3. big library (bigger would be even better)
THE BAD:
|
by: youngdubliner |
last post by:
I'm having a problem ........
I've stripped all my code to help isolate the problem.
Its seems to be with importing numarray when python is embedded in C.
I have a simple C program it Opens Python imports a script and
then Closes Python
like so .......
|
by: martino |
last post by:
Hi, I am trying to install NumPy (v23.8) onder Macosx (panther
version). Python (v2.3) is bundled into panther and I installed the
IDE on top of that. After having untarred the source distribution in
the desktop directory and typed
>python setup.py install
as recommended in the attached documentation, I get:
running install
|
by: Gustav Hållberg |
last post by:
I tried finding a discussion around adding the possibility to have
optional underscores inside numbers in Python. This is a popular option
available in several "competing" scripting langauges, that I would love
to see in Python.
Examples:
1_234_567
0xdead_beef
3.141_592
|
by: PL |
last post by:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for
example, add 1 to each element) and pass it back to Python.
With these building blocks I will be able to figure out all the rest of
what I need to do for my project. I am very familiar with Python, but
less so with C++ and Boost or SWIG.
Does anyone have an example with all steps that I can follow? More
specifically I am looking for the C++ code, ".i" file for...
| |
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c */
#include <time.h>
|
by: robinsiebler |
last post by:
I've never had any call to use floating point numbers and now that I
want to, I can't!
*** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. ***
0.29999999999999999
0.29999999999999999
|
by: Ethan Furman |
last post by:
len wrote:
I've never had the (mis?)fortune to work with COBOL -- what are the
files like? Fixed format, or something like a dBase III style? I
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |