Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

Here's a function I use for editing an IDF or IMF file in place that might be useful. It's inspired by the approach in jEPlus where you mark the variables you want to change in your IDF or IMF file by giving them a name in the form @@VariableName@@. It's handy for handling parametric runs where you are generating the values in Python code. Generally though it's easier to call jEPlus directly from Python if you just want a pre-set list of values or values from a known distribution.

import fileinput

def edit_template(substitutions, imf_path):
    """
    Replace all placeholder @@variables@@ in a template file with the
    correct values for the current run.

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {"BoilerEfficiency": 0.9} 
        will replace @@BoilerEfficiency@@ with 0.9 in the input file.
    imf_path : str
        The path to the IDF or IMF.

    Raises
    ------
    ValueError
        Raised if a value to replace was not found in the template file.

    """
    for key in subs:
        if key not in open(path, 'rb').read():
            raise ValueError("%s not in %s" % (key, path))
    for line in fileinput.FileInput(path, inplace=True):
        for s in substitutions:
            line = line.replace("@@%s@@" % s, str(substitutions[s]))  # enforce str
        print line,

Eppy is a really useful tool with a lot of thought gone into it as well so I wouldn't go reinventing the wheel. I'm sure any contributions you'd like to make would be welcomed. It's quite large, but the introductory tutorial is helpful (although if you already speak Python it can be a bit too detailed).

I haven't used the OpenStudio Python bindings so can't comment on that.

Here's a function I use for editing an IDF or IMF file in place that might be useful. It's inspired by the approach in jEPlus where you mark the variables you want to change in your IDF or IMF file by giving them a name in the form @@VariableName@@. It's handy for handling parametric runs where you are generating the values in Python code. Generally though it's easier to call jEPlus directly from Python (or not using Python at all...) if you just want a pre-set list of values or values from a known distribution.

import fileinput

def edit_template(substitutions, imf_path):
    """
    Replace all placeholder @@variables@@ in a template file with the
    correct values for the current run.

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {"BoilerEfficiency": 0.9} 
        will replace @@BoilerEfficiency@@ with 0.9 in the input file.
    imf_path : str
        The path to the IDF or IMF.

    Raises
    ------
    ValueError
        Raised if a value to replace was not found in the template file.

    """
    for key in subs:
        if key not in open(path, 'rb').read():
            raise ValueError("%s not in %s" % (key, path))
    for line in fileinput.FileInput(path, inplace=True):
        for s in substitutions:
            line = line.replace("@@%s@@" % s, str(substitutions[s]))  # enforce str
        print line,

Eppy is a really useful tool with a lot of thought gone into it as well so I wouldn't go reinventing the wheel. I'm sure any contributions you'd like to make would be welcomed. It's quite large, but the introductory tutorial is helpful (although if you already speak Python it can be a bit too detailed).

I haven't used the OpenStudio Python bindings so can't comment on that.

Here's a function I use for editing an IDF or IMF file in place that might be useful. It's inspired by the approach in jEPlus where you mark the variables you want to change in your IDF or IMF file by giving them a name in the form @@VariableName@@. It's handy for handling parametric runs where you are generating the values in Python code. Generally though it's easier to call jEPlus directly from Python (or not using Python at all...) if you just want a pre-set list of values or values from a known distribution.

import fileinput

def edit_template(substitutions, imf_path):
    """
    Replace all placeholder @@variables@@ in a template file with the
    correct values for the current run.

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {"BoilerEfficiency": 0.9} 
        will replace @@BoilerEfficiency@@ with 0.9 in the input file.
    imf_path : str
        The path to the IDF or IMF.

    Raises
    ------
    ValueError
        Raised if a value to replace was not found in the template file.

    """
    for key in subs:
    substitutions:
    if key not in open(path, open(imf_path, 'rb').read():
         raise ValueError("%s not in %s" % (key, path))
imf_path))
    for line in fileinput.FileInput(path, fileinput.FileInput(imf_path, inplace=True):
        for s in substitutions:
             line = line.replace("@@%s@@" % s, str(substitutions[s]))  # enforce str
        print line,

Eppy is a really useful tool with a lot of thought gone into it as well so I wouldn't go reinventing the wheel. I'm sure any contributions you'd like to make would be welcomed. It's quite large, but the introductory tutorial is helpful (although if you already speak Python it can be a bit too detailed).

I haven't used the OpenStudio Python bindings so can't comment on that.

Here's a function I use for editing an IDF or IMF file in place that might be useful. It's inspired by the approach in jEPlus where you mark the variables you want to change in your IDF or IMF file by giving them a name in the form @@VariableName@@. It's handy for handling parametric runs where you are generating the values in Python code. Generally though it's easier to call jEPlus directly from Python (or not using Python at all...) if you just want a pre-set list of values or values from a known distribution.

import fileinput

def edit_template(substitutions, imf_path):
    """
    Replace all placeholder @@variables@@ in a template file with the
    correct values for the current run.

    Parameters
    ----------
    substitutions: dict
        A dictionary mapping the variable to be substituted to the value
        to be substitute in its place, e.g. {"BoilerEfficiency": 0.9} 
        will replace @@BoilerEfficiency@@ with 0.9 in the input file.
    imf_path : str
        The path to the IDF or IMF.

    Raises
    ------
    ValueError
        Raised if a value to replace was not found in the template file.

    """
    for key in substitutions:
     if key not in open(imf_path, 'rb').read():
         raise ValueError("%s not in %s" % (key, imf_path))
     for line in fileinput.FileInput(imf_path, inplace=True):
         for s in substitutions:
              line = line.replace("@@%s@@" % s, str(substitutions[s]))  # enforce str
         print line,

Eppy is a really useful tool with a lot of thought gone into it as well so I wouldn't go reinventing the wheel. I'm sure any contributions you'd like to make would be welcomed. It's quite large, but the introductory tutorial is helpful (although if you already speak Python it can be a bit too detailed).

I haven't used the OpenStudio Python bindings so can't comment on that.