Skip to content

BMI Python

Justin Singh-M. - NOAA edited this page Oct 12, 2023 · 4 revisions

BMI Models Written in Python

Enabling Python Integration

Python integration is controlled with the CMake build flag NGEN_WITH_PYTHON, however this currently defaults to "On"--you would need to turn this off if Python is not available in your environment. See the Dependencies documentation for specifics on Python requirements, but in summary you will need a working Python environment with NumPy installed. You can set up a Python environment anywhere with the usual environment variables. The appropriate Python environment should be active in the shell when ngen is run.

For Python BMI models specifically, you will also need to install the bmipy package, which provides a base class for Python BMI models.

BMI Python Model as Package Class

To use a Python BMI model, the model needs to be installed as a package in the Python environment and the package must have a class that extends bmipy, like so

from bmipy import Bmi
class bmi_model(Bmi):
    ...

TIP: If you are actively developing a Python BMI model, you may want to install your package with the -e flag.

As noted above, Python modules require the package and class name to be specified in the realization config via the python_class key, such as:

{
  "global": {
    "formulations": [
        { 
            "name": "bmi_python",
            "params": {
                "python_type": "mypackage.bmi_model",
                "model_type_name": "bmi_model",
                //...

BMI Python Example

An example implementation for an appropriate BMI model as a Python class is provided in the project, or you can examine the CSDMS-provided example Python model.