0

This is kind of specific to the algorithm I am using. But basically, I have an algorithm that runs using the following command:

w2cropconv -i /mnt/data/SHAVE_cubes/20120329/multi0/code_index.xml -I "Heightof0C" -o /mnt/data/SHAVE_cubes/20120329/multi0/.. -t "38.4 -97.85 21" -b "37.8 -97.15 1" -s "0.005 0.005 1" -R 

I run it with popen() using the following command:

p = subprocess.Popen( [ "w2cropconv", "-i", path, "-I", "HeightofOC", "-o", output_directory, "-t", NWloc, "-b", SEloc, "-s", "0.005 0.005 1", "-R", ] ) 

While the popen() command runs correctly, it does not actually output anything. In the first case, the algorithm outputs to the /multi0/ directory. The popep() function does not write to that directory, even though it really should. It is not a bug in my coding, I am convinced that it is something specific to popep() that I do not know.

I know that the input and output directories are the same because when I execute either, I get the following:

 key [i], val [/mnt/data/SHAVE_cubes/20120329/multi0/code_index.xml] key [I], val [HeightofOC] key [o], val [/mnt/data/SHAVE_cubes/20120329/Heightof0C] key [t], val [38.4 -97.85 21] key [b], val [37.8 -97.15 1] key [s], val [0.01 0.01 1] key [R], val [true] 

When they diverge, the correct process yields this output:

(../util/code_W2Unit.cc:66 getUtUnit) Initializing UDUNITS, Version 2 or greater... (../util/code_W2Unit.cc:82 getUtUnit) Set UDUNITS2_XML_PATH=/usr/local/WDSS2/WDSS2/w2/w2config/misc/udunits2.xml (code_LatLonConverter.cc:208 processInputField) Converting LatLonGrid: Heightof0C at 20120329-210000 location (lat=[58.366001 deg],lon=[-139.856 deg],h=[0 km]) and creating 1 output grids (code_DataRemapper.cc:138 getLookup) DataRemapper: creating lookup for LatLonGrid NWcorner: Loc: 58.4 -139.9 dim: [225 x 301] latres: [0.188] lonres: [0.275] to LatLonGrid NWcorner: Loc: 38.4 -97.8 dim: [120 x 140] latres: [0.005] lonres: [0.005] (code_DataRemapper.cc:140 getLookup) There are now 1 remapping lookup tables stored. (code_NetcdfDataEncoder.cc:122 applySettings) Using FINAL SPARSEGRID threshold of {-1} based on settings in w2config/misc/dataformat . 

Please excuse me if I do not answer right away, I am having a headache.

3
  • Are you absolutely sure path and output_directory are exactly correct? Maybe try subprocess.check_call(); Popen itself won't raise an exception if the command exits with a nonzero status code. Commented Nov 9, 2020 at 20:08
  • (Also, Popen will return an object representing the process you would have to wait to exit; check_call() does that for you.) Commented Nov 9, 2020 at 20:09
  • Please see my post, I've edited to show that the two are exactly the same. Can you explain your second comment a little more @AKX Commented Nov 9, 2020 at 20:13

1 Answer 1

2

Assuming the w2cropconv utility takes a while to do its crop-and-convert thing, you'll need to wait for it to complete (I mean, if that's what you want). Just doing p = subprocess.Popen(...) starts the process, then keeps going with your code.

You can either do

p = subprocess.Popen(...) retcode = p.wait() if retcode != 0: # process likely failed 

or the shorter (where ... is in your case exactly the same as for Popen)

subprocess.check_call(...) 

which will raise an exception for you after the process exits if it fails, and just wait otherwise.

Sign up to request clarification or add additional context in comments.

2 Comments

It isn't working this way either. It might be specific to the algorithm I'm using. The two commands have exactly the same output until basically the right one calls another script and returns different output which I will post in my original question. It might clue you in to what is going on.
I was able to solve the problem using os.system. Thanks for your help though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.