From 845f494cfe66d9906789651a0f8ac2cbf578e903 Mon Sep 17 00:00:00 2001 From: Christian Langevin Date: Wed, 24 Jul 2019 12:33:30 -0500 Subject: [PATCH 1/2] fix(autotest): move some output files to their own folder --- autotest/t032_test.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/autotest/t032_test.py b/autotest/t032_test.py index bbb0dde8e0..3f4c5fe73f 100644 --- a/autotest/t032_test.py +++ b/autotest/t032_test.py @@ -27,11 +27,15 @@ def test_polygon_from_ij(): nlay=2, delr=100, delc=100, top=3, botm=botm, model=m) - ncdf = NetCdf('toy.model.nc', m) + fname = os.path.join(mpth, 'toy.model.nc') + ncdf = NetCdf(fname, m) ncdf.write() - m.export('toy_model_two.nc') - dis.export('toy_model_dis.nc') + fname = os.path.join(mpth, 'toy_model_two.nc') + m.export(fname) + + fname = os.path.join(mpth, 'toy_model_dis.nc') + dis.export(fname) mg = m.modelgrid mg.set_coord_info(xoff=mg._xul_to_xll(600000.0, -45.0), From 83c761a8ad07811591a299420e92fd46d09eb1a9 Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Tue, 30 Jul 2019 10:20:11 +1200 Subject: [PATCH 2/2] fix(run_model): improve normal_msg and fix Python 2.7 memory issue --- flopy/mbase.py | 52 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/flopy/mbase.py b/flopy/mbase.py index 05ff34b295..757f23f67f 100644 --- a/flopy/mbase.py +++ b/flopy/mbase.py @@ -9,7 +9,6 @@ import abc import sys import os -import subprocess as sp import shutil import threading import warnings @@ -19,6 +18,7 @@ else: import Queue from datetime import datetime +from subprocess import Popen, PIPE, STDOUT import copy import numpy as np from flopy import utils, discretization @@ -1499,9 +1499,10 @@ def run_model(exe_name, namefile, model_ws='./', report : boolean, optional Save stdout lines to a list (buff) which is returned by the method . (default is False). - normal_msg : str + normal_msg : str or list Normal termination message used to determine if the - run terminated normally. (default is 'normal termination') + run terminated normally. More than one message can be provided using + a list. (Default is 'normal termination') use_async : boolean asynchronously read model stdout and report with timestamps. good for models that take long time to run. not good for models that run @@ -1519,12 +1520,11 @@ def run_model(exe_name, namefile, model_ws='./', success = False buff = [] - # convert normal_msg to lower case for comparison + # convert normal_msg to a list of lower case str for comparison if isinstance(normal_msg, str): - normal_msg = [normal_msg.lower()] - elif isinstance(normal_msg, list): - for idx, s in enumerate(normal_msg): - normal_msg[idx] = s.lower() + normal_msg = [normal_msg] + for idx, s in enumerate(normal_msg): + normal_msg[idx] = s.lower() # Check to make sure that program and namefile exist exe = which(exe_name) @@ -1568,24 +1568,31 @@ def q_output(output, q): for t in cargs: argv.append(t) + if sys.version_info[0:2] == (2, 7) and sys.platform != 'win32': + # Python 2.7 workaround for non-Windows + close_fds = True + else: + close_fds = False # default + # run the model with Popen - proc = sp.Popen(argv, - stdout=sp.PIPE, stderr=sp.STDOUT, cwd=model_ws) + proc = Popen(argv, stdout=PIPE, stderr=STDOUT, cwd=model_ws, + close_fds=close_fds) if not use_async: while True: - line = proc.stdout.readline() - c = line.decode('utf-8') - if c != '': + line = proc.stdout.readline().decode('utf-8') + if line == '' and proc.poll() is not None: + break + if line: for msg in normal_msg: - if msg in c.lower(): + if msg in line.lower(): success = True break - c = c.rstrip('\r\n') + line = line.rstrip('\r\n') if not silent: - print('{}'.format(c)) - if report == True: - buff.append(c) + print(line) + if report: + buff.append(line) else: break return success, buff @@ -1629,10 +1636,11 @@ def q_output(output, q): proc.stdout.close() for line in buff: - if normal_msg in line: - print("success") - success = True - break + for msg in normal_msg: + if msg in line.lower(): + print("success") + success = True + break if pause: input('Press Enter to continue...')