newbie question: using Python to coordinate C++ program and use gnuplot

Terry Reedy tjreedy at home.com
Thu Aug 16 12:03:21 EDT 2001


CC'ed
"Eduardo Zea" <gezea at princeton.edu> wrote in message
news:3B771A9C.3BB8317 at princeton.edu...

> 1) I have a C++ program that reads a file with parameters (say
> parameters.txt) and runs a simulation, then produces an output file
(say
> output.txt). I want to write a Python program that loops over the
> parameter values and on each loop does this:
>
> 1. Updates parameters.txt
> 2. Runs the C++ program with the updated file
> 3. Renames the output of the C++ program according to the simulation
> number, e.g. output1.txt
> 2) A python program that opens gnuplot in a new terminal, and sends
to
> it a comand like "plot output.txt". And re-issues this command each
time
> it updates output.txt, so that gnuplot displays the data as it is
being
> produced by the program.

I believe something like the following may work.

import os
n=0
plot=os.popen('gnuplot', 'w') #there is optional third param bufsize
for param1 in param1list:
    for param2 in param2 list:
       ...
           open("parameters.txt",
"w").write(format%(param1,param2,...)).close()
           os.system("C++ program parameters.txt")
           plot.write("plot output.txt") # before rename
           os.rename('output.txt', 'output%d.txt'%n)
           n += 1

Or, if params are not crossed so neatly,

for p1,p2,...pk in ((v11,v12,...v1k), ...(vn1,vn2,...vnk)): # with ...
expanded to actual list

> Eduardo Zea
> Department of Ecology and Evolutionary Biology
> Princeton University

Terry J. Reedy
ps. since my long-ago PhD was in ecology, I would not mind an email
note on what your simulation does.






More information about the Python-list mailing list