I am trying to write a python program that will enter another program using a shell command, then issue commands to that program, then exit the program and continue running the rest of a python program. To be more precise, if I was simply running in a shell, I would type the following commands to get what I wanted:
-grass -text /water6/NO_BACKUP/Brandon/exposure/grassdata/WGS84_latlon/PERMANENT
-v.patch -e input=countytest,countytest2 output=countytesttotal
-exit
So to do this, I use the python code below:
#!/usr/bin/env python
import sys
import os
import subprocess
import time
os.system('clear')
trythis1 = 'grass -text /water6/NO_BACKUP/Brandon/exposure/grassdata/WGS84_latlon/PERMANENT'
trythis2 = 'v.patch -e input=countytest,countytest2 output=countytesttotal\n'
trythis3 = 'exit\n'
print "Updating zip code vector data using newest shape file"
proc = subprocess.Popen(trythis1, shell=True, stdin=subprocess.PIPE)
proc.stdin.write(trythis2)
proc.stdin.write(trythis3)
print('completed')
and for output I get this:
brandon@ca1flood:/water6/NO_BACKUP/Brandon/exposure/Scripts$ python ZipVectorUpdate.py
Updating zip code vector data using newest shape file
completed
brandon@ca1flood:/water6/NO_BACKUP/Brandon/exposure/Scripts$ Cleaning up temporary files.....
Starting GRASS ...
GRASS GUI should be text
__________ ___ __________ _______________
/ ____/ __ \/ | / ___/ ___/ / ____/ _/ ___/
/ / __/ /_/ / /| | \__ \\_ \ / / __ / / \__ \
/ /_/ / _, _/ ___ |___/ /__/ / / /_/ // / ___/ /
\____/_/ |_/_/ |_/____/____/ \____/___//____/
Welcome to GRASS 6.3.0 (2008)
GRASS homepage: http://grass.osgeo.org/
This version running thru: Bash Shell (/bin/bash)
Help is available with the command: g.manual -i
See the licence terms with: g.version -c
Start the GUI with: g.gui tcltk
When ready to quit enter: exit
Patching vector map <countytest@PERMANENT>...
Patching vector map <countytest2@PERMANENT>...
Building topology ...
866 primitives registered
Building areas: 100%
294 areas built
189 isles built
Attaching islands: 100%
Attaching centroids: 100%
Topology was built.
Number of nodes : 761
Number of primitives: 866
Number of points : 0
Number of lines : 0
Number of boundaries: 572
Number of centroids : 294
Number of areas : 294
Number of isles : 189
Intersections at borders will have to be snapped
Lines common between files will have to be edited
The header information also may have to be edited
v.patch complete. 2 vector maps patched
Closing monitors.....
Cleaning up temporary files.....
done
Goodbye from GRASS GIS
The odd thing is that, even if I dont put in a exit statement, the child process still closes out the grass program as if I did. Is there a way to make my primary program wait for the subprocess to complete so that things are done in a certain order? This program is part of a much larger set of programs that will need to run in a certain order, needing to wait until all the previous steps are completed before going on. Also, I had to add \n to get my statements to not run together, if I hadnt used them, my output file would have been named countytesttotalexit. There must be a better way to go about this. Any ideas?