GDL Python Module: General Instructions

Help
gnovak
2007-01-08
2013-06-03
  • gnovak
    gnovak
    2007-01-08

    I'm interested in using the GDL python module to call IDL code
    from Python.

    I'm having trouble figuring out how to make this happen.  The
    example from PYTHON.txt works:

    import GDL
    GDL.function("sin", (1,))

    It looks from the documentation that if I want to make the IDL call:
    retval = func(x,y,a=1,b=2)
    then I should make the Python call:
    retval = GDL.function("func", (x,y), dict(a=1, b=2) )
    correct?  Or should it be:
    retval = GDL.function("func", x, y, a=1, b=2)

    I'm also having trouble calling my own functions/procedures.
    When I have my own function in a pro file, like so:
    function func,x,y
      return,x*y
      end

    then GDL.function("func", (1,2)) fails with the backtrace listed
    below. 

    I can run procedures, so if procedure.pro contains:
    pro hello
      print,"Hello!"
      end

    then GDL.procedure("hello", (,), {}) prints "Hello!" as expected.
    However I can't seem to get data into or out of procedure calls.  For
    example, what am I to do if my pro file contains:
    pro procedure,x,y,z
      z = x*y
      end

    Here z is the output variable.  How do I specify it to GDL?  I
    imagine something like GDL.procedure((1,2,'retval'), {}), and
    then I'll have the result as GDL.retval or GDL.get('retval')

    Thanks!
    Greg

    ----------------------------------------------------------------------
    BACKTRACE
    #0  0x021828f4 in CheckSub (sub=0x0, argTuple=0x5b035a8, kwDict=0x0) at dpro.hpp:169
    #1  0x02184a20 in GDLSub (self=0x5b035a8, argTuple=0x5b035a8, kwDict=0x0, functionCall=false) at pythongdl.cpp:361
    #2  0x000911b8 in PyEval_EvalFrame (f=0x608b20) at Python/ceval.c:3558
    #3  0x00093480 in PyEval_EvalCodeEx (co=0x53cade0, globals=0x5b035a8, locals=0x2184a20, args=0x0, argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2736
    #4  0x00093610 in PyEval_EvalCode (co=0x5b035a8, globals=0x5b035a8, locals=0x0) at Python/ceval.c:484
    #5  0x0000d780 in PyRun_InteractiveOneFlags (fp=0x33b2c0, filename=0xd47d4 "<stdin>", flags=0xbffff14c) at Python/pythonrun.c:1265
    #6  0x0000d944 in PyRun_InteractiveLoopFlags (fp=0xa0001b9c, filename=0xd47d4 "<stdin>", flags=0xbffff14c) at Python/pythonrun.c:695
    #7  0x0000e5f8 in PyRun_AnyFileExFlags (fp=0xa0001b9c, filename=0xd47d4 "<stdin>", closeit=0, flags=0xbffff14c) at Python/pythonrun.c:658
    #8  0x000067e4 in Py_Main (argc=-1073745588, argv=0x20000000) at Modules/main.c:484
    #9  0x00001e48 in _start (argc=1, argv=0xbffff25c, envp=0xbffff264) at /SourceCache/Csu/Csu-58/crt.c:272
    #10 0x00001cf0 in start ()

     

  • Anonymous
    2011-03-03

    I'd really like to use Python GDL. How can i use stuff like

    "For example, what am I to do if my pro file contains:
    pro procedure,x,y,z
    z = x*y
    end

    Here z is the output variable. How do I specify it to GDL?" (from gnovak)

    in Python?

     

  • Anonymous
    2011-03-03

    I found a way to get what I want, i just don't really like it. Please let me know if there is a better way.
    Here is what I did: I changed the GDL_pro method in pythongdl.cpp to something like

      // Execute a GDL procedure
      PyObject *GDL_pro(PyObject *self, PyObject *argTuple, PyObject *kwDict)
      {
        PyObject *ret = GDLSub( self, argTuple, kwDict, false);
        Py_CLEAR(ret);
        if (kwDict == NULL) {
            return PyTuple_GetSlice(argTuple, 1, PyTuple_Size(argTuple));
        } else {
            return Py_BuildValue("(OO)", PyTuple_GetSlice(argTuple, 1, PyTuple_Size(argTuple)), kwDict);
        }
      }
    

    And now i have access to all changed parameters. with gnovaks example i can now do:

    x,y,z = GDL.pro(2,3,None)
    print z   # prints "6"

    Any comments?

     
  • Ronny Gerasch
    Ronny Gerasch
    2012-06-08

    To call your own procedures from python you need to run a GDL startup script first:
    GDL.script("/path/to/your/script/<filename>")
    your startupfile should look like this:
    !PATH=!PATH + ':/path/to/your/pro/files/'

    You can then call your procedures and pass parameters and keywords.

    for an example look here: http://code.google.com/p/blumegdl/