Skip to content

Commit

Permalink
Merge pull request #90 from DHI/bug/mikeio1d_together_with_mikepluspy
Browse files Browse the repository at this point in the history
Fix loading of MIKE IO 1D when MIKE+Py is present.
  • Loading branch information
gedaskir authored Feb 28, 2024
2 parents 08090c8 + 1195097 commit 025fafc
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 8 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

### Added


### Fixed


### Changed

## [0.6.1] - 2024-02-024

### Fixed

- Loading MIKE IO 1D together with MIKE+Py

## [0.6] - 2024-02-08

Expand Down Expand Up @@ -121,7 +124,8 @@
- Reading of res1d and xns11 files into pandas data frames


[unreleased]: https://github.com/DHI/mikeio1d/compare/v0.6...HEAD
[unreleased]: https://github.com/DHI/mikeio1d/compare/v0.6.1...HEAD
[0.6.1]: https://github.com/DHI/mikeio1d/releases/tag/v0.6.1
[0.6]: https://github.com/DHI/mikeio1d/releases/tag/v0.6
[0.5]: https://github.com/DHI/mikeio1d/releases/tag/v0.5
[0.4.1]: https://github.com/DHI/mikeio1d/releases/tag/v0.4.1
Expand Down
2 changes: 1 addition & 1 deletion mikeio1d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer.
# 'X.Y.dev0' is the canonical version of 'X.Y.dev'
#
__version__ = "0.6.0"
__version__ = "0.6.1"

if "64" not in platform.architecture()[0]:
raise Exception("This library has not been tested for a 32 bit system.")
Expand Down
26 changes: 24 additions & 2 deletions mikeio1d/mikepath.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import platform
import pythonnet

Expand All @@ -25,6 +26,8 @@ class MikePath:

mikeio1d_bin_path = os.path.join(os.path.dirname(__file__), "bin")

mikeio1d_bin_extra_path = os.path.join(os.path.dirname(__file__), "bin/DHI.Mike1D.MikeIO")

mike_install_path = os.environ.get("MIKE_INSTALL_PATH", None)

skip_bin_x64 = os.environ.get("MIKE_SKIP_BIN_X64", None)
Expand All @@ -49,7 +52,10 @@ def setup_mike_installation(syspath):
syspath: list of str
List of strings defining PATH variable.
"""
MikePath.update_mike_bin_to_mikepluspy_bin_if_used()

syspath.append(MikePath.mike_bin_path)
syspath.append(MikePath.mikeio1d_bin_extra_path)

if MikePath.is_linux:
MikePath.setup_mike_installation_linux()
Expand All @@ -58,9 +64,25 @@ def setup_mike_installation(syspath):
MikePath.setup_mike_installation_custom(syspath)

@staticmethod
def setup_mike_installation_custom(syspath):
syspath.append(MikePath.mikeio1d_bin_path)
def update_mike_bin_to_mikepluspy_bin_if_used():
"""
Updates the path for MIKE assemblies to the ones used by MIKE+Py package.
This requires that the MIKE+Py package was already loaded.
"""
mikepluspy = sys.modules.get("mikeplus")
if mikepluspy == None:
return

import mikeplus

mikeplus_bin_path = str(mikeplus.MikeImport.ActiveProduct().InstallRoot)
mikeplus_bin_path = os.path.join(mikeplus_bin_path, MikePath.bin_x64)

MikePath.mikeio1d_bin_path = mikeplus_bin_path
MikePath.mike_bin_path = mikeplus_bin_path

@staticmethod
def setup_mike_installation_custom(syspath):
# Some of the MIKE libraries will need to be resolved by DHI.Mike.Install,
# so set it up here.
import clr
Expand Down
3 changes: 2 additions & 1 deletion scripts/util_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UtilBuilder:
build_dir_name = os.path.join("bin", "Release", "netstandard2.0")

# Directory where libraries will be installed
bin_dir_name = os.path.join("mikeio1d", "bin")
bin_dir_name = os.path.join("mikeio1d", "bin", "DHI.Mike1D.MikeIO")

# Utility libraries to build and install
library_names = ["DHI.Mike1D.MikeIO"]
Expand Down Expand Up @@ -57,6 +57,7 @@ def copy_libraries_to_bin(self):
print(f" Copying file: {source_file_path_stripped}")
_, file_name = os.path.split(source_file)
destination_file = os.path.join(destination, file_name)
os.makedirs(destination, exist_ok=True)
shutil.copy2(source_file, destination_file)

def strip_source_file_path(self, file_path):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

setuptools.setup(
name="mikeio1d",
version="0.6.0",
version="0.6.1",
install_requires=[
"pythonnet>=3.0.0",
"numpy",
Expand Down
21 changes: 21 additions & 0 deletions tests/call_mikepluspy_mikeio1d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys


def test_mikeio1d_and_mikepluspy_coexistence(test_file_path):
# This test cannot run on a GitHub pipeline, because currently there is no way
# to have MIKE+ installation on a virtual machine there.
try:
import mikeplus
except:
return

from mikeplus.engines import Engine1D
from mikeio1d import Res1D

res1d = Res1D(test_file_path, result_reader_type="copier")

df = res1d.read()


if __name__ == "__main__":
test_mikeio1d_and_mikepluspy_coexistence(sys.argv[1])
12 changes: 12 additions & 0 deletions tests/test_various.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ def test_network_attributes_allow_chinese_characters(test_file_path):
assert res.nodes.美丽
assert res.nodes.学生
assert res.nodes.蛋糕


def test_mikeio1d_and_mikepluspy_coexistence(test_file_path):
# This test cannot run on a GitHub pipeline, because currently there is no way
# to have MIKE+ installation on a virtual machine there.

import subprocess

test_folder_path = os.path.dirname(os.path.abspath(__file__))
script_path = os.path.join(test_folder_path, "call_mikepluspy_mikeio1d.py")
exit_code = subprocess.call(["python", script_path, test_file_path])
assert exit_code == 0

0 comments on commit 025fafc

Please sign in to comment.