|
| 1 | +from distutils.dir_util import copy_tree |
| 2 | +from pathlib import Path |
| 3 | +import docker |
| 4 | +import docker.errors |
| 5 | +import pytest |
| 6 | +import requests |
| 7 | +from PyStemmusScope import config_io |
| 8 | +from PyStemmusScope import forcing_io |
| 9 | +from PyStemmusScope import soil_io |
| 10 | +from PyStemmusScope.bmi.implementation import StemmusScopeBmi |
| 11 | +from PyStemmusScope.bmi.docker_utils import pull_image |
| 12 | +from . import data_folder |
| 13 | +import platform |
| 14 | + |
| 15 | + |
| 16 | +SCOPE_INPUTDATA_v2_1 = "https://github.com/Christiaanvandertol/SCOPE/raw/2.1/input/" |
| 17 | +SCOPE_INPUTDATA_v1_7 = ( |
| 18 | + "https://github.com/Christiaanvandertol/SCOPE/raw/1.73/data/input/" |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def docker_available(): |
| 23 | + try: |
| 24 | + docker.APIClient() |
| 25 | + |
| 26 | + # Github Actions windows runners couldn't pull the image: |
| 27 | + if platform.system() == "Windows": |
| 28 | + pull_image("ghcr.io/ecoextreml/stemmus_scope:1.5.0") |
| 29 | + |
| 30 | + return True |
| 31 | + except docker.errors.DockerException as err: |
| 32 | + if "Error while fetching server API version" in str(err): |
| 33 | + return False |
| 34 | + if "404 Client Error" in str(err): # Can't find image |
| 35 | + return False |
| 36 | + else: |
| 37 | + raise err # Unknown error. |
| 38 | + |
| 39 | + |
| 40 | +cfg_file = data_folder / "config_file_docker.txt" |
| 41 | +vegetation_property_dir = ( |
| 42 | + data_folder / "directories" / "model_parameters" / "vegetation_property" |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +def write_config_file(cfg: dict, file: Path) -> None: |
| 47 | + with file.open("w") as f: |
| 48 | + for key, val in cfg.items(): |
| 49 | + f.write(f"{key}={val}\n") |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope="session") |
| 53 | +def prep_input_data(): |
| 54 | + optipar_path = ( |
| 55 | + vegetation_property_dir / "fluspect_parameters" / "Optipar2017_ProspectD.mat" |
| 56 | + ) |
| 57 | + if not optipar_path.exists(): |
| 58 | + r = requests.get( # Older version due to v2 compatibility issues |
| 59 | + SCOPE_INPUTDATA_v1_7 + "fluspect_parameters/Optipar2017_ProspectD.mat" |
| 60 | + ) |
| 61 | + assert r.status_code == 200 |
| 62 | + optipar_path.open("wb").write(r.content) |
| 63 | + |
| 64 | + flex_s3_path = vegetation_property_dir / "radiationdata" / "FLEX-S3_std.atm" |
| 65 | + if not flex_s3_path.exists(): |
| 66 | + r = requests.get(SCOPE_INPUTDATA_v2_1 + "radiationdata/FLEX-S3_std.atm") |
| 67 | + assert r.status_code == 200 |
| 68 | + flex_s3_path.open("wb").write(r.content) |
| 69 | + |
| 70 | + soil_path = vegetation_property_dir / "soil_spectrum" / "soilnew.txt" |
| 71 | + if not soil_path.exists(): |
| 72 | + r = requests.get(SCOPE_INPUTDATA_v2_1 + "soil_spectra/soilnew.txt") |
| 73 | + assert r.status_code == 200 |
| 74 | + soil_path.open("wb").write(r.content) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture(scope="session") |
| 78 | +def prepare_data_config(tmpdir_factory, prep_input_data) -> Path: |
| 79 | + tempdir = Path(tmpdir_factory.mktemp("tempdir")) |
| 80 | + output_dir = tempdir / "output_dir" |
| 81 | + input_dir = tempdir / "input_dir" |
| 82 | + output_dir.mkdir() |
| 83 | + input_dir.mkdir() |
| 84 | + config = config_io.read_config(cfg_file) |
| 85 | + config["OutputPath"] = str(output_dir) + "/" |
| 86 | + config["InputPath"] = str(input_dir) + "/" |
| 87 | + |
| 88 | + forcing_io.prepare_forcing(config) |
| 89 | + soil_io.prepare_soil_data(config) |
| 90 | + soil_io.prepare_soil_init(config) |
| 91 | + |
| 92 | + copy_tree(src=str(vegetation_property_dir), dst=str(input_dir)) |
| 93 | + |
| 94 | + config_dir = tempdir / "config.txt" |
| 95 | + write_config_file(config, config_dir) |
| 96 | + |
| 97 | + return config_dir |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.skipif(not docker_available(), reason="Docker not available") |
| 101 | +def test_initialize(prepare_data_config): |
| 102 | + model = StemmusScopeBmi() |
| 103 | + model.initialize(str(prepare_data_config)) |
| 104 | + model.update() |
| 105 | + model.finalize() |
0 commit comments