0

I have a program written in C that manipulates FITS images. The images are stored as an array of floats and there is some associated metadata (image dimensions but also other FITS metadata). What I'm interested in doing is adding a capability to use python scripts (and all the goodness that things like astropy etc. can provide) so that users can extend the capability of the program. In order to do that I need to spawn a python process with the path to a script, but I also need to make the image and metadata available to the spawned python script so it can manipulate it, and that's the bit that I can't get to grips with at all.

To provide a slightly concrete example, supposing my image has dimensions int rx, ry, nchans and the pixel data is stored in float* img = malloc(rx * ry * nchans * sizeof(float)) and I want to spawn a very simple python script that is provided with that data, divides the value of each element of img by 2 and then exits and returns to the calling program, how can I share the area of memory allocated to img with the python script and then how could I define some python variables within the script that would access those ints and float* and allow me to manipulate them until the python script exits and control passes back to my program? Ideally I'm looking for a reasonably OS-agnostic approach as my program targets Linux, MacOS and Windows, but if I have to handle different OSes differently then so be it.

Adrian K-B.
  • 99
  • 1
  • 8
  • There are multiple different ways to achieve this. The key to the problem is establashing a place where multiple processes can both read and write. A simple form of this is a text document. However, you may be interested in researching Inter-process communication. – FRINTSO Jan 27 '23 at 13:12
  • These might be helpful: https://stackoverflow.com/q/1056051/10622916, https://www.geeksforgeeks.org/calling-python-from-c-set-1/, http://web.mit.edu/people/amliu/vrut/python/ext/callingPython.html, http://insti.physics.sunysb.edu/itp/computing/doc/python/python-ext/section2_2_7.html (search results for "call python from c") – Bodo Jan 27 '23 at 13:35

0 Answers0