First time here? Check out the Help page!
1 | initial version |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
2 | No.2 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version I'm filing away where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.inp' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy various non-SIM output files generated
os.remove(renamedinp)
3 | No.3 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version I'm filing away where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.inp' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy various non-SIM output files generated
os.remove(renamedinp)
weatherbatch():
4 | No.4 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version I'm filing away where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.inp' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy various non-SIM output files generated
os.remove(renamedinp)
weatherbatch():
weatherbatch()
5 | No.5 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version I'm filing away where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.inp' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy various non-SIM output files generated
copy
os.remove(renamedinp)
weatherbatch()
6 | No.6 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version I'm filing away where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.inp' '.BIN' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
weatherbatch()
7 | No.7 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version I'm filing away where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.BIN' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
weatherbatch()
EDIT (10/11/16): ... I was prompted by a request to make this compatible when run directly from notepad++ to share the current, more polished state of this script. I specifically tested this and found it working with the notepad++ plugin "PyNPP." It's much further developed now, and incorporates a habit I've picked up to set things up with a "testing files" subdirectory... hopefully the intended structure is self-evident enough!
import glob
import subprocess
import shutil
import os
import re
#this function 'weatherbatch()' is designed to be executed in a working directory to include any number of weatherfiles (*.bin) and input files (*.inp). It will simulate each unique combination possible between those files via doe2 commandline. SIM output files should remain alongside the targeted files when the loop is finished with self-descriptive names to indicate which input and weatherfile corresponds.
# Review these environment variables for running doe2 on command line
doe2_location = 'C:\doe22'
doe2_version = 'EXE48r' #<-- As would be entered for command line execution
doe2bat_location = doe2_location+'\DOE22.BAT'
doe2_weather_directory = doe2_location+'\weather'
def weatherbatch():
for w in glob.glob('*.BIN'):
# get a copy of the weather file into the doe2 weather directory
shutil.copy(w, doe2_weather_directory)
for i in glob.glob('*.inp'):
# strip '.BIN' from w for commandline execution
# (done via regex to make this case-insensitive)
weather = re.sub('\.bin', '', w, flags=re.IGNORECASE)
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
# (done via regex to make this case-insensitive)
inputname = re.sub('\.inp', '', i, flags=re.IGNORECASE)
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe2bat_location, doe2_version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
# for testing purposes:
if __name__ == '__main__':
import sys
here = sys.path[0]
# change the working directory to a subdirectory "testing files"
# where I've collected some .inp and .bin files
os.chdir('./testing files')
weatherbatch()
8 | No.8 Revision |
I used Amir's current answer to make a working script for doe22, v48r:v48r:
import glob
import subprocess
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version I'm filing away where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.names.
import glob
import subprocess
import shutil
import os
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.BIN' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
weatherbatch()
EDIT (10/11/16): ... I was prompted by a request to make this compatible when run directly from notepad++ to share the current, more polished state of this script. I specifically tested this and found it working with the notepad++ plugin "PyNPP." It's much further developed now, and incorporates a habit I've picked up to set things up with a "testing files" subdirectory... hopefully the intended structure is self-evident enough!enough!
import glob
import subprocess
import shutil
import os
import re
import glob
import subprocess
import shutil
import os
import re
#this function 'weatherbatch()' is designed to be executed in a working directory to include any number of weatherfiles (*.bin) and input files (*.inp). It will simulate each unique combination possible between those files via doe2 commandline. SIM output files should remain alongside the targeted files when the loop is finished with self-descriptive names to indicate which input and weatherfile corresponds.
# Review these environment variables for running doe2 on command line
doe2_location = 'C:\doe22'
doe2_version = 'EXE48r' #<-- As would be entered for command line execution
doe2bat_location = doe2_location+'\DOE22.BAT'
doe2_weather_directory = doe2_location+'\weather'
def weatherbatch():
for w in glob.glob('*.BIN'):
# get a copy of the weather file into the doe2 weather directory
shutil.copy(w, doe2_weather_directory)
for i in glob.glob('*.inp'):
# strip '.BIN' from w for commandline execution
# (done via regex to make this case-insensitive)
weather = re.sub('\.bin', '', w, flags=re.IGNORECASE)
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
# (done via regex to make this case-insensitive)
inputname = re.sub('\.inp', '', i, flags=re.IGNORECASE)
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe2bat_location, doe2_version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
# for testing purposes:
if __name__ == '__main__':
import sys
here = sys.path[0]
# change the working directory to a subdirectory "testing files"
# where I've collected some .inp and .bin files
os.chdir('./testing files')
weatherbatch()
9 | No.9 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocessv48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import osnames.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.BIN' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
weatherbatch()
EDIT (10/11/16): ... I was prompted by a request to make this compatible when run directly from notepad++ to share the current, more polished state of this script. I specifically tested this and found it working with the notepad++ plugin "PyNPP." It's much further developed now, and incorporates a habit I've picked up to set things up with a "testing files" subdirectory... hopefully the intended structure is self-evident enough!
import glob
import subprocess
import shutil
import os
import re
import glob
import subprocess
import shutil
import os
import re
#this function 'weatherbatch()' is designed to be executed in a working directory to include any number of weatherfiles (*.bin) and input files (*.inp). It will simulate each unique combination possible between those files via doe2 commandline. SIM output files should remain alongside the targeted files when the loop is finished with self-descriptive names to indicate which input and weatherfile corresponds.
# Review these environment variables for running doe2 on command line
doe2_location = 'C:\doe22'
doe2_version = 'EXE48r' #<-- As would be entered for command line execution
doe2bat_location = doe2_location+'\DOE22.BAT'
doe2_weather_directory = doe2_location+'\weather'
def weatherbatch():
for w in glob.glob('*.BIN'):
# get a copy of the weather file into the doe2 weather directory
shutil.copy(w, doe2_weather_directory)
for i in glob.glob('*.inp'):
# strip '.BIN' from w for commandline execution
# (done via regex to make this case-insensitive)
weather = re.sub('\.bin', '', w, flags=re.IGNORECASE)
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
# (done via regex to make this case-insensitive)
inputname = re.sub('\.inp', '', i, flags=re.IGNORECASE)
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe2bat_location, doe2_version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
# for testing purposes:
if __name__ == '__main__':
# change the working directory to a subdirectory "testing files"
# where I've collected some .inp and .bin files
os.chdir('./testing files')
weatherbatch()
Observe:
1. This is set up to work with a
"testing files" subdirectory,
hopefully the intended structure is
self-evident!
2. string.replace()
actions on the working filenames have been replaced by regular
expression re.sub()
equivalents to be case-insensitive
10 | No.10 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: ... and here is a more developed version where each simulation's outputs aren't written over the previous run. For each simulation, the input file is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.
import glob
import subprocess
import shutil
import os
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
# strip '.BIN' from w for commandline execution
weather = w.replace('.BIN','')
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
inputname = i.replace('.inp','')
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
weatherbatch()
EDIT (10/11/16): ... I was prompted by a request to make this compatible when run directly from notepad++ to share the current, more polished state of this script. I specifically tested this and found it working with the notepad++ plugin "PyNPP."
import glob
import subprocess
import shutil
import os
import re
#this function 'weatherbatch()' is designed to be executed in a working directory to include any number of weatherfiles (*.bin) and input files (*.inp). It will simulate each unique combination possible between those files via doe2 commandline. SIM output files should remain alongside the targeted files when the loop is finished with self-descriptive names to indicate which input and weatherfile corresponds.
# Review these environment variables for running doe2 on command line
doe2_location = 'C:\doe22'
doe2_version = 'EXE48r' #<-- As would be entered for command line execution
doe2bat_location = doe2_location+'\DOE22.BAT'
doe2_weather_directory = doe2_location+'\weather'
def weatherbatch():
for w in glob.glob('*.BIN'):
# get a copy of the weather file into the doe2 weather directory
shutil.copy(w, doe2_weather_directory)
for i in glob.glob('*.inp'):
# strip '.BIN' from w for commandline execution
# (done via regex to make this case-insensitive)
weather = re.sub('\.bin', '', w, flags=re.IGNORECASE)
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
# (done via regex to make this case-insensitive)
inputname = re.sub('\.inp', '', i, flags=re.IGNORECASE)
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe2bat_location, doe2_version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
# for testing purposes:
if __name__ == '__main__':
# change the working directory to a subdirectory "testing files"
# where I've collected some .inp and .bin files
os.chdir('./testing files')
weatherbatch()
Observe:
1. Observe:
string.replace()
actions on the working filenames have been replaced by re.sub()
equivalents to be case-insensitiveshell = True
to the end of the subprocess.call
: subprocess.call([doe2bat_location, doe2_version, input, weather], shell =True)
11 | No.11 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT: EDIT (10/12/2016): ... and here following is a more developed developed, current version where each simulation's outputs aren't written over the of the same script. I am retaining the relatively simplified example above in this answer to facilitate comparison and clarity of concept for others traversing the bridges between python, doe2, and e+. Previous iterations available in past edits to this answer for anyone interested.
I was prompted in the answer commentary to try and make this script compatible when executed directly from notepad++.
While I've pushed the module further in form/function, I confess I'm not certain what made previous run. For each simulation, the input file versions incompatible with notepad++ execution, and I only recently picked up the realization that was possible (I've been used to executing all python efforts from powershell). This is renamed to append the weather file name, so that the resulting outputs have identifiable and unique names.tested and working using Python 2.7 and the notepad++ plugin called "PyNPP" using the default shortcut Alt+Shift+F5.
I was also prompted to get my act together with respect to docstrings. Hopefully this is a little easier on our French friends' sensibilities now =)!
"""Automate batch simulation for any number of doe2 input & weather files."""
import os
import glob
import subprocess
import shutil
import os
# Check re
# Review these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
doe2_location = 'C:\doe22'
doe2_version = 'EXE48r' #<-- As would be entered for command line execution
doe2bat_location = doe2_location+'\DOE22.BAT'
doe2_weather_directory = doe2_location+'\weather'
def weatherbatch():
for i weatherbatch(target_path):
"""This function will simulate every unique combination possible between a
group of co-located weather files (*.bin) and input files (*.inp) via doe2
commandline.
The argument 'target_path' is the directory containing the weather (*.bin)
and input (*.inp) files.
When finished, BDL files and SIM output files with self-descriptive names
to indicate the associated weather / input combination should remain
alongside the targeted files.
Be mindful limits to doe2 command line execution apply to this script as
well. Excessive characters and the presence of any spaces in glob.glob('*.inp'):
the input or
weather file names will result in errors.
"""
# store current working directory
start_dir = os.getcwd()
# move to the target directory containing the weather/input files
os.chdir(target_path)
for w in glob.glob('*.BIN'):
# ensure a copy of the weatherfile exists in the doe2 weather directory
shutil.copy(w, doe2_weather_directory)
# strip '.BIN' from w for commandline execution
execution
# (done via regex to make this case-insensitive)
weather = w.replace('.BIN','')
re.sub('\.bin', '', w, flags=re.IGNORECASE)
for i in glob.glob('*.inp'):
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
# (done via regex to make this case-insensitive)
inputname = i.replace('.inp','')
re.sub('\.inp', '', i, flags=re.IGNORECASE)
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
subprocess.call([doe22batlocation, doe22version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
weatherbatch()
EDIT (10/11/16): ... I was prompted by a request to make # execute commandline doe2 for this compatible when run directly from notepad++ to share the current, more polished state of this script. I specifically tested this and found it working with the notepad++ plugin "PyNPP."
import glob
import subprocess
import shutil
import os
import re
#this function 'weatherbatch()' is designed to be executed in a working directory to include any number of weatherfiles (*.bin) and input files (*.inp). It will simulate each unique combination possible between those files via doe2 commandline. SIM output files should remain alongside the targeted files when the loop is finished with self-descriptive names to indicate which input and weatherfile corresponds.
# Review these environment variables for running doe2 on command line
doe2_location = 'C:\doe22'
doe2_version = 'EXE48r' #<-- As would be entered for command line execution
doe2bat_location = doe2_location+'\DOE22.BAT'
doe2_weather_directory = doe2_location+'\weather'
def weatherbatch():
for w in glob.glob('*.BIN'):
# get a copy of the weather file into the doe2 weather directory
shutil.copy(w, doe2_weather_directory)
for i in glob.glob('*.inp'):
# strip '.BIN' from w for commandline execution
# (done via regex to make this case-insensitive)
weather = re.sub('\.bin', '', w, flags=re.IGNORECASE)
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
# (done via regex to make this case-insensitive)
inputname = re.sub('\.inp', '', i, flags=re.IGNORECASE)
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
combination
subprocess.call([doe2bat_location, doe2_version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
# return to the original working directory
os.chdir(start_dir)
# for testing purposes:
if __name__ == '__main__':
# change the working directory to """A subdirectory '/testing files' is kept alongside this module,
containing a subdirectory "testing files"
# where I've collected some plurality of .inp and .bin files
os.chdir('./testing files')
weatherbatch()
files.
"""
#establish the target directory
testing_path = os.getcwd()+'/testing files'
#execute weatherbatch on the target directory
weatherbatch(testing_path)
Observe:Closing Observations:
+'/testing files'
on line 71 to simply plop this into the same directory containing your *.INP and *.BIN files.string.replace()
actions on the working re.sub()
equivalents to be shell = True
to the end of the subprocess.call
subprocess.call([doe2bat_location, doe2_version, input, weather], shell =True)
12 | No.12 Revision |
I used Amir's current answer to make a working script for doe22, v48r:
import glob
import subprocess
# Check these environment variables for running doe2 on command line
doe22batlocation = 'C:\doe22\DOE22.BAT'
doe22version = 'EXE48r'
def weatherbatch():
for i in glob.glob('*.inp'):
for w in glob.glob('*.BIN'):
input = i.replace('.inp','')
weather = w.replace('.BIN','')
subprocess.call([doe22batlocation, doe22version, input, weather])
weatherbatch()
EDIT (10/12/2016): ... and following is a more developed, current version of the same script. I am retaining the relatively simplified simple example above in this answer to facilitate comparison and clarity of concept for others traversing the bridges between python, doe2, and e+. Previous iterations available in past edits to this answer for anyone interested.
I was prompted in the answer commentary to try and make this script compatible when executed directly from notepad++. While I've pushed the module further in form/function,
I confess I'm not certain what made previous versions incompatible with notepad++ execution, and I only recently picked up the realization that was possible (I've been used to executing all python efforts to-date from powershell). This The following is tested and working using Python 2.7 and the notepad++ plugin called "PyNPP" using the default with the shortcut Alt+Shift+F5.
I was also prompted to get my act together with respect to docstrings. Hopefully docstring styling. As a self-learner I appreciate the encouragement, and hopefully this is a little easier on our French friends' sensibilities now =)!
"""Automate batch simulation for any number of doe2 input & weather files."""
import os
import glob
import subprocess
import shutil
import re
# Review these environment variables for running doe2 on command line
doe2_location = 'C:\doe22'
doe2_version = 'EXE48r' #<-- As would be entered for command line execution
doe2bat_location = doe2_location+'\DOE22.BAT'
doe2_weather_directory = doe2_location+'\weather'
def weatherbatch(target_path):
"""This function will simulate every unique combination possible between a
group of co-located weather files (*.bin) and input files (*.inp) via doe2
commandline.
The argument 'target_path' is the directory containing the weather (*.bin)
and input (*.inp) files.
When finished, BDL files and SIM output files with self-descriptive names
to indicate the associated weather / input combination should remain
alongside the targeted files.
Be mindful limits to doe2 command line execution apply to this script as
well. Excessive characters and the presence of any spaces in the input or
weather file names will result in errors.
"""
# store current working directory
start_dir = os.getcwd()
# move to the target directory containing the weather/input files
os.chdir(target_path)
for w in glob.glob('*.BIN'):
# ensure a copy of the weatherfile exists in the doe2 weather directory
shutil.copy(w, doe2_weather_directory)
# strip '.BIN' from w for commandline execution
# (done via regex to make this case-insensitive)
weather = re.sub('\.bin', '', w, flags=re.IGNORECASE)
for i in glob.glob('*.inp'):
# make a copy of 'Project.inp' called 'Project_in_weatherfile.inp'
# (done via regex to make this case-insensitive)
inputname = re.sub('\.inp', '', i, flags=re.IGNORECASE)
renamedinp = inputname + '_in_' + weather + '.inp'
shutil.copy(i, renamedinp)
# strip '.inp' from renamedinp for commandline execution
input = renamedinp.replace('.inp','')
# execute commandline doe2 for this weather + input combination
subprocess.call([doe2bat_location, doe2_version, input, weather])
# remove the renamedinp copy
os.remove(renamedinp)
# return to the original working directory
os.chdir(start_dir)
# for testing purposes:
if __name__ == '__main__':
"""A subdirectory '/testing files' is kept alongside this module,
containing a plurality of .inp and .bin files.
"""
#establish the target directory
testing_path = os.getcwd()+'/testing files'
#execute weatherbatch on the target directory
weatherbatch(testing_path)
Closing Observations:
+'/testing files'
on line 71 to simply string.replace()
actions on the working file names are replaced by regular expression re.sub()
equivalents to be case-insensitive (this is a little more robust).shell = True
to the end of the subprocess.call
like this: subprocess.call([doe2bat_location, doe2_version, input, weather], shell =True)