Passing data to system command

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Hieronymus

    #1

    Passing data to system command


    Hi,

    I have a bunch of x-y data contained in an array. I would like to
    plot the data using an
    external program (psxy in GMT). The plotting program takes x-y
    couples as standard
    input. How do I get the data into the system call? I used to do
    things in csh and awk,
    i.e., something like

    awk '{<some manipulations here>; print $1, $2}' filename | psxy <some
    options> >! output.ps

    The reason I'm trying to use python is because the manipulations are
    getting too cumbersome
    in awk. Now I have all the manipulations done in python, but I'm
    missing that last step.

    I've tried various things with os.system, popen, and subprocess, but
    so far without success.
    Does anyone know how to do this?

    chris


    ------------------------------------------------------------------------
    -------------------------------------------
    Christoph
    Hieronymus
    christoph.hiero nymus@geo.uu.se
    Associate
    Professor
    phone: (+46) 18-471 2383
    Uppsala
    University
    fax: (+46) 18-501 110
    Dept. of Earth Sciences (Geophysics)
    Villavägen 16
    SE-752 36 Uppsala, Sweden




  • hdante

    #2
    Re: Passing data to system command

    Should be like this:

    from subprocess import Popen, PIPE

    my_output = file('output1.p s', 'w')
    p1 = Popen(["psxy"], stdin = PIPE, stdout=my_outpu t)
    p1.stdin.write( my_format(array ))
    p1.communicate( )
    my_output.close ()

    I've never used that, though, please tell us if it worked.

    Chris Hieronymus wrote:[color=blue]
    > Hi,
    >
    > I have a bunch of x-y data contained in an array. I would like to
    > plot the data using an
    > external program (psxy in GMT). The plotting program takes x-y
    > couples as standard
    > input. How do I get the data into the system call? I used to do
    > things in csh and awk,
    > i.e., something like
    >
    > awk '{<some manipulations here>; print $1, $2}' filename | psxy <some
    > options> >! output.ps
    >
    > The reason I'm trying to use python is because the manipulations are
    > getting too cumbersome
    > in awk. Now I have all the manipulations done in python, but I'm
    > missing that last step.
    >
    > I've tried various things with os.system, popen, and subprocess, but
    > so far without success.
    > Does anyone know how to do this?
    >
    > chris
    >
    >
    > ------------------------------------------------------------------------
    > -------------------------------------------
    > Christoph
    > Hieronymus
    > christoph.hiero nymus@geo.uu.se
    > Associate
    > Professor
    > phone: (+46) 18-471 2383
    > Uppsala
    > University
    > fax: (+46) 18-501 110
    > Dept. of Earth Sciences (Geophysics)
    > Villavägen 16
    > SE-752 36 Uppsala, Sweden[/color]

    Comment

    • faulkner

      #3
      Re: Passing data to system command

      import os, subprocess

      xys = [[1,2],[3,4]]
      msg = '\n'.join([str(x) + ',' + str(y) for x, y in xys])
      os.popen('comma nd', 'w').write(msg)
      os.popen2('comm and')[0].write(msg)

      p = subprocess.Pope n('command', stdin=subproces s.PIPE)
      p.stdin.write(m sg)

      help(subprocess )
      help(os.popen)
      help(os.popen3)


      Chris Hieronymus wrote:[color=blue]
      > Hi,
      >
      > I have a bunch of x-y data contained in an array. I would like to
      > plot the data using an
      > external program (psxy in GMT). The plotting program takes x-y
      > couples as standard
      > input. How do I get the data into the system call? I used to do
      > things in csh and awk,
      > i.e., something like
      >
      > awk '{<some manipulations here>; print $1, $2}' filename | psxy <some
      > options> >! output.ps
      >
      > The reason I'm trying to use python is because the manipulations are
      > getting too cumbersome
      > in awk. Now I have all the manipulations done in python, but I'm
      > missing that last step.
      >
      > I've tried various things with os.system, popen, and subprocess, but
      > so far without success.
      > Does anyone know how to do this?
      >
      > chris
      >
      >
      > ------------------------------------------------------------------------
      > -------------------------------------------
      > Christoph
      > Hieronymus
      > christoph.hiero nymus@geo.uu.se
      > Associate
      > Professor
      > phone: (+46) 18-471 2383
      > Uppsala
      > University
      > fax: (+46) 18-501 110
      > Dept. of Earth Sciences (Geophysics)
      > Villavägen 16
      > SE-752 36 Uppsala, Sweden[/color]

      Comment

      • Cameron Laird

        #4
        Re: Passing data to system command

        In article <mailman.7179.1 150658017.27775 .python-list@python.org >,
        Chris Hieronymus <christoph.hier onymus@geo.uu.s e> wrote:

        Comment

        • Chris Hieronymus

          #5
          Re: Passing data to system command

          Hi,

          Holy mackerel, this really works; thanks a lot, guys. I played
          around a little bit with the
          suggestions by faulkner and hdante and pieced together the following
          script. I like this
          very much because I can write a bunch of data to the pipe, rather
          than making one big
          string containing perhaps several thousand lines of x-y pairs. I've
          tested the script for
          up to 100,000 data pairs and it works; passing a single string with
          that many lines to
          the psxy command generally leads to problems (?), I'm told...

          For any other newbie's out there that are trying to use python and
          GMT together:
          The script uses GMT's psxy command (with the required arguments),
          generates some
          x-y data (just a sine function), and writes each x-y pair as a string
          to the pipe. This should
          work equally for any other GMT-commands.

          I'm still trying to work out some of the details myself; I don't
          understand, yet, what exactly
          the command "communicat e" does; but it seems to be needed.

          chris


          =============== =============== =============== =============== =====

          #! /usr/bin/python

          from subprocess import Popen, PIPE
          from math import *
          from os import system

          psfile = 'output1.ps'
          cmd = 'psxy -R0/100/0/10 -JX10 -B10/1'


          my_output = file(psfile, 'w')
          p1 = Popen(cmd,stdin = PIPE,stdout=my_ output,shell=Tr ue)
          for i in range(10000):
          x = float(i)/100.0
          y = 4.*sin(x/10.)+5.0
          msg = str(x)+" "+str(y)+"\ n"
          p1.stdin.write( msg)

          p1.communicate( )
          my_output.close ()

          cmd = 'gv '+psfile
          print cmd
          p2 = Popen(cmd,shell =True)
          p2.communicate( )

          =============== =============== =============== =============== =====


          On Jun 18, 2006, at 11:27 PM, hdante wrote:
          [color=blue]
          > Should be like this:
          >
          > from subprocess import Popen, PIPE
          >
          > my_output = file('output1.p s', 'w')
          > p1 = Popen(["psxy"], stdin = PIPE, stdout=my_outpu t)
          > p1.stdin.write( my_format(array ))
          > p1.communicate( )
          > my_output.close ()
          >
          > I've never used that, though, please tell us if it worked.
          >
          > Chris Hieronymus wrote:[color=green]
          >> Hi,
          >>
          >> I have a bunch of x-y data contained in an array. I would like to
          >> plot the data using an
          >> external program (psxy in GMT). The plotting program takes x-y
          >> couples as standard
          >> input. How do I get the data into the system call? I used to do
          >> things in csh and awk,
          >> i.e., something like
          >>
          >> awk '{<some manipulations here>; print $1, $2}' filename | psxy <some
          >> options> >! output.ps
          >>
          >> The reason I'm trying to use python is because the manipulations are
          >> getting too cumbersome
          >> in awk. Now I have all the manipulations done in python, but I'm
          >> missing that last step.
          >>
          >> I've tried various things with os.system, popen, and subprocess, but
          >> so far without success.
          >> Does anyone know how to do this?
          >>
          >> chris
          >>
          >>
          >> ---------------------------------------------------------------------
          >> ---
          >> -------------------------------------------
          >> Christoph
          >> Hieronymus
          >> christoph.hiero nymus@geo.uu.se
          >> Associate
          >> Professor
          >> phone: (+46) 18-471 2383
          >> Uppsala
          >> University
          >> fax: (+46) 18-501 110
          >> Dept. of Earth Sciences (Geophysics)
          >> Villavägen 16
          >> SE-752 36 Uppsala, Sweden[/color]
          >
          > --
          > http://mail.python.org/mailman/listinfo/python-list[/color]

          ------------------------------------------------------------------------
          -------------------------------------------
          Christoph
          Hieronymus
          christoph.hiero nymus@geo.uu.se
          Associate
          Professor
          phone: (+46) 18-471 2383
          Uppsala
          University
          fax: (+46) 18-501 110
          Dept. of Earth Sciences (Geophysics)
          Villavägen 16
          SE-752 36 Uppsala, Sweden




          Comment

          • Cameron Laird

            #6
            Formatting practices (was: Passing data to system command)

            In article <mailman.7197.1 150720173.27775 .python-list@python.org >,
            Chris Hieronymus <christoph.hier onymus@geo.uu.s e> wrote:

            Comment

            Working...