From: <and...@us...> - 2009-01-15 16:05:55
|
Revision: 9311 http://plplot.svn.sourceforge.net/plplot/?rev=9311&view=rev Author: andrewross Date: 2009-01-15 16:05:35 +0000 (Thu, 15 Jan 2009) Log Message: ----------- Add plmap / plmeridians to python bindings. Implement example 19 in python and add the example to the standard python tests. Modified Paths: -------------- trunk/bindings/python/plplotcmodule.i trunk/bindings/swig-support/plplotcapi.i trunk/examples/python/xw19.py trunk/plplot_test/test_python.sh.in Modified: trunk/bindings/python/plplotcmodule.i =================================================================== --- trunk/bindings/python/plplotcmodule.i 2009-01-15 09:57:31 UTC (rev 9310) +++ trunk/bindings/python/plplotcmodule.i 2009-01-15 16:05:35 UTC (rev 9311) @@ -613,12 +613,14 @@ typedef PLINT (*defined_func)(PLFLT, PLFLT); typedef void (*fill_func)(PLINT, PLFLT*, PLFLT*); typedef void (*pltr_func)(PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer); +typedef void (*mapform_func)(PLINT, PLFLT *, PLFLT*); typedef PLFLT (*f2eval_func)(PLINT, PLINT, PLPointer); %{ typedef PLINT (*defined_func)(PLFLT, PLFLT); typedef void (*fill_func)(PLINT, PLFLT*, PLFLT*); typedef void (*pltr_func)(PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer); +typedef void (*mapform_func)(PLINT, PLFLT *, PLFLT*); typedef PLFLT (*f2eval_func)(PLINT, PLINT, PLPointer); %} @@ -642,6 +644,7 @@ enum callback_type { CB_0, CB_1, CB_2, CB_Python } pltr_type; PyObject* python_pltr = NULL; PyObject* python_f2eval = NULL; + PyObject* python_mapform = NULL; #if 0 #define MY_BLOCK_THREADS { \ @@ -753,6 +756,60 @@ return fresult; } + void do_mapform_callback(PLINT n, PLFLT *x, PLFLT *y) + { + PyObject *px, *py, *arglist, *result; + PyArrayObject *tmpx, *tmpy; + PLFLT *xx, *yy; + PLINT i; + + if(python_mapform) { /* if not something is terribly wrong */ + /* grab the Global Interpreter Lock to be sure threads don't mess us up */ + MY_BLOCK_THREADS + /* build the argument list */ +#ifdef PL_DOUBLE + px = PyArray_FromDimsAndData(1, &n, PyArray_DOUBLE,(char *)x); + py = PyArray_FromDimsAndData(1, &n, PyArray_DOUBLE,(char *)y); +#else + px = PyArray_FromDimsAndData(1, &n, PyArray_FLOAT,(char *)x); + py = PyArray_FromDimsAndData(1, &n, PyArray_FLOAT,(char *)y); +#endif + arglist = Py_BuildValue("(iOO)", n, px, py); + /* call the python function */ + result = PyEval_CallObject(python_mapform, arglist); + /* release the argument list */ + Py_DECREF(arglist); + Py_DECREF(px); + Py_DECREF(py); + /* check and unpack the result */ + if(result == NULL) { + fprintf(stderr, "call to python mapform function with 3 arguments failed\n"); + PyErr_SetString(PyExc_RuntimeError, "mapform callback must take 3 arguments."); + } /* else { + PyArg_ParseTuple(result,"OO",&px,&py); + PyArrayObject *tmpx = (PyArrayObject *)myArray_ContiguousFromObject(px, PyArray_PLFLT, 1, 1); + PyArrayObject *tmpy = (PyArrayObject *)myArray_ContiguousFromObject(py, PyArray_PLFLT, 1, 1); + if(tmpx == 0 || tmpy == 0 || tmpx->dimensions[0] != 1 ||tmpy->dimensions[0] != 1) { + fprintf(stderr, "pltr callback must return a 1-D vector or sequence\n"); + PyErr_SetString(PyExc_RuntimeError, "pltr callback must return a 1-sequence."); + } else { + xx = (PLFLT*)tmpx->data; + yy = (PLFLT*)tmpy->data; + for (i=0;i<n;i++) { + x[i] = xx[i]; + y[i] = yy[1]; + } + Py_XDECREF(tmpx); + Py_XDECREF(tmpy); + } + } */ + /* release the result */ + Py_XDECREF(result); + /* release the global interpreter lock */ + MY_UNBLOCK_THREADS + } + } + /* marshal the pltr function pointer argument */ pltr_func marshal_pltr(PyObject* input) { pltr_func result = do_pltr_callback; @@ -790,6 +847,19 @@ python_pltr = 0; } +/* marshal the mapform function pointer argument */ + mapform_func marshal_mapform(PyObject* input) { + mapform_func result = do_mapform_callback; + python_mapform = input; + Py_XINCREF(input); + return result; + } + + void cleanup_mapform(void) { + Py_XDECREF(python_mapform); + python_mapform = 0; + } + PLPointer marshal_PLPointer(PyObject* input,int isimg) { PLPointer result = NULL; switch(pltr_type) { @@ -854,6 +924,24 @@ $1 = NULL; } +%typemap(in) mapform_func mapform { + /* it must be a callable */ + if(!PyCallable_Check((PyObject*)$input)) { + PyErr_SetString(PyExc_ValueError, "mapform argument must be callable"); + return NULL; + } + $1 = marshal_mapform($input); +} +%typemap(freearg) mapform_func mapform { + cleanup_mapform(); +} + +/* you can omit the mapform func */ +%typemap(default) mapform_func mapform { + python_mapform = 0; + $1 = NULL; +} + /* convert an arbitrary Python object into the void* pointer they want */ %typemap(in) PLPointer PYOBJECT_DATA { if($input == Py_None) Modified: trunk/bindings/swig-support/plplotcapi.i =================================================================== --- trunk/bindings/swig-support/plplotcapi.i 2009-01-15 09:57:31 UTC (rev 9310) +++ trunk/bindings/swig-support/plplotcapi.i 2009-01-15 16:05:35 UTC (rev 9311) @@ -934,8 +934,6 @@ #endif -#ifdef SWIG_JAVA - /* plot continental outline in world coordinates */ void @@ -949,7 +947,6 @@ PLFLT dlong, PLFLT dlat, PLFLT minlong, PLFLT maxlong, PLFLT minlat, PLFLT maxlat ); -#endif #if 0 Modified: trunk/examples/python/xw19.py =================================================================== --- trunk/examples/python/xw19.py 2009-01-15 09:57:31 UTC (rev 9310) +++ trunk/examples/python/xw19.py 2009-01-15 16:05:35 UTC (rev 9311) @@ -1,2 +1,89 @@ +# $Id$ -print "This python equivalent of x19c not implemented yet" +# Copyright (C) Wesley Ebisuzaki +# Copyright (C) 2009 Andrew Ross +# +# Illustrates backdrop plotting of world, US maps. +# +# This file is part of PLplot. +# +# PLplot is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Library Public License as published +# by the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# PLplot is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public License +# along with PLplot; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# + +from plplot_py_demos import * + +# mapform19 +# +# Defines specific coordinate transformation for example 19. +# Not to be confused with mapform in src/plmap.c. +# x[], y[] are the coordinates to be plotted. +def mapform19(n, x, y): + for i in range(n): + radius = 90.0 - y[i] + xp = radius * cos(x[i] * pi / 180.0) + yp = radius * sin(x[i] * pi / 180.0) + x[i] = xp + y[i] = yp + return [x,y] + +# Null coordinate transform (equivalent to passing NULL to C +# version of plmap / plmeridians. +def nullmapform(n,x,y): + return [x,y] + +# main +# +# Does a series of 3-d plots for a given data set, with different +# viewing options in each plot. + +def main(): + + # Longitude (x) and latitude (y) + + miny = -70 + maxy = 80 + + # Cartesian plots + # Most of world + + minx = 190 + maxx = 190+360 + + plcol0(1) + plenv(minx, maxx, miny, maxy, 1, -1) + plmap(nullmapform,"usaglobe", minx, maxx, miny, maxy) + + # The Americas + + minx = 190 + maxx = 340 + + plcol0(1) + plenv(minx, maxx, miny, maxy, 1, -1) + plmap(nullmapform, "usaglobe", minx, maxx, miny, maxy) + + # Polar, Northern hemisphere + + minx = 0 + maxx = 360 + + plenv(-75., 75., -75., 75., 1, -1) + plmap(mapform19,"globe", minx, maxx, miny, maxy) + + pllsty(2) + plmeridians(mapform19,10.0, 10.0, 0.0, 360.0, -10.0, 80.0) + plend() + +main() Modified: trunk/plplot_test/test_python.sh.in =================================================================== --- trunk/plplot_test/test_python.sh.in 2009-01-15 09:57:31 UTC (rev 9310) +++ trunk/plplot_test/test_python.sh.in 2009-01-15 16:05:35 UTC (rev 9311) @@ -26,11 +26,10 @@ # $pythondir must be the build tree and not the source tree (if separate from # build tree) since must access plplot_python_start.py in build tree and # paths in that file are relative to build tree. -# Skip 19 because it is just a placeholder without a real implementation. # Skip 21 if using Numeric - it doesn't work # For 24 you need special fonts installed to get good result. lang="p" -for index in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 \ +for index in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 \ 20 22 23 24 25 26 27 28 29 30 31 @NUMPY_EXAMPLES@ ; do if [ "$verbose_test" ] ; then echo "x${index}" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |