Pages

mardi 26 juin 2012

use variables in the os.system in python: combine linux and python

In python we can use os.system to call linux command. For example,
cmd = 'echo Good'
os.system(cmd)

it will print "Good" on the terminal.

If we would like to use variables (already defined in python code) with a linux command, then we need use %variable.
for example, we would like to call /home/username/bin/xyz2flh/xyzflh to convert the positions from XYZ to latitude/longitude/height. This command asks for 4 inputs: the option of reference Ellipsoide (with Enter key), then three numbers for X position, Y position and Z position. The option of reference ellisoide is 2.

If in python code, we have defined the positions in sx, sy and sz, and then define a cmd to be used by os.system:

import os, sys
sx = -5246416.97098
sy= -3077275.36744 
sz = -1913808.08861


cmd = ('printf "%s\n%s %s %s" | /home/username/bin/xyz2flh/xyzflh' %(2, sx, sy, sz))

os.system(cmd)

The results of this python code would be like:

Ellipsoide de reference:
     WGS84 (ae = 6378137.00 ; 1/f = 298.257222) : tapez 1
  ITRF2005 (ae = 6378136.46 ; 1/f = 298.257650) : tapez 2
     autre..................................... : tapez 3
  entrez x y z en metres ou latitude(deg) longitude(deg) hauteur(m)
  latitude (deg):   -17.5767433707736  
  longitude (deg):    210.393669153517  
  hauteur (m):    96.8535648984835  


Attention: the format of the strings need be correct, separated with Enter key and spaces.



Aucun commentaire:

Enregistrer un commentaire