10000 Bugfix: Add missing case for simulations starting after the input data by zner0L · Pull Request #4 · PIK-LPJmL/pycoupler · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Bugfix: Add missing case for simulations starting after the input data #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pycoupler/coupler.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ def _copy_input(self, start_year, end_year):
start_year = self.config.start_coupling - 1
if end_year is None:
end_year = self.config.start_coupling - 1
if start_year > end_year:
raise ValueError("start_year cannot be later than end_year")

# iterate over each inputs to be send via sockets (get initial values)
for key in sock_inputs:
Expand All @@ -823,17 +825,26 @@ def _copy_input(self, start_year, end_year):

# determine start cut off and end cut off year
if meta_data.firstyear > end_year:
# data is after simulation period
cut_start_year = meta_data.firstyear
cut_end_year = meta_data.firstyear
elif meta_data.lastyear < start_year:
# data is before simulation period
cut_start_year = meta_data.lastyear
cut_end_year = meta_data.lastyear
elif meta_data.firstyear >= start_year:
# data begins in simulation period
cut_start_year = meta_data.firstyear
cut_end_year = min(meta_data.lastyear, end_year)
elif meta_data.lastyear < end_year:
# data ends in simulation period
cut_start_year = start_year
cut_end_year = meta_data.lastyear
elif meta_data.firstyear <= start_year and meta_data.lastyear >= start_year:
# data starts before simulation period,
# but simulation is within data period
cut_start_year = start_year
cut_end = cut_end_year = min(meta_data.lastyear, end_year)

cut_clm_start = [
f"{self._config.model_path}/bin/cutclm",
Expand Down
16 changes: 12 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import os
import pytest
from pycoupler.coupler import LPJmLCoupler


def get_test_path():
@pytest.fixture
def test_path():
"""Fixture for the test path."""
return os.path.dirname(os.path.abspath(__file__))


@pytest.fixture
def test_path():
"""Fixture for the test path."""
return get_test_path()
def lpjml_coupler(test_path):
config_coupled_fn = f"{test_path}/data/config_coupled_test.json"
# Using yield enables safe teardown of the fixture
# (see https://docs.pytest.org/en/stable/how-to/fixtures.html#safe-teardowns)
yield LPJmLCoupler(config_file=config_coupled_fn)
# Reset test line env variable
os.environ["TEST_LINE_COUNTER"] = "0"


def pytest_configure(config):
import sys

sys._called_from_test = True
os.environ["TEST_PATH"] = os.path.dirname(os.path.abspath(__file__))
os.environ["TEST_LINE_COUNTER"] = "0"


def pytest_unconfigure(config):
Expand Down
55 changes: 29 additions & 26 deletions tests/data/input/with_tillage.nc.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
{
"sim_name" : "coupled_test",
"source" : "LPJmL C Version 5.8.1",
"history" : "/p/projects/open/Jannes/copan_core/lpjml/LPJmL_internal/bin/lpjml /p/projects/open/Jannes/copan_core/lpjml/config_coupled_test.json",
"variable" : "grid",
"firstcell" : 27410,
"ncell" : 2,
"cellsize_lon" : 0.500000,
"cellsize_lat" : 0.500000,
"nstep" : 1,
"timestep" : 1,
"nbands" : 2,
"long_name" : "tillage",
"unit" : "",
"firstyear" : 2022,
"lastyear" : 2022,
"nyear" : 1,
"datatype" : "short",
"scalar" : 1,
"order" : "cellyear",
"bigendian" : false,
"format" : "cdf",
"ref_area" : {"filename" : "with_tillage.nc4.json", "format" : "meta"},
"filename" : "with_tillage.nc4",
"sim_name": "coupled_test",
"source": "LPJmL C Version 5.8.1",
"history": "/p/projects/open/Jannes/copan_core/lpjml/LPJmL_internal/bin/lpjml /p/projects/open/Jannes/copan_core/lpjml/config_coupled_test.json",
"variable": "grid",
"firstcell": 27410,
"ncell": 2,
"cellsize_lon": 0.500000,
"cellsize_lat": 0.500000,
"nstep": 1,
"timestep": 1,
"nbands": 2,
"long_name": "tillage",
"unit": "",
"firstyear": 2000,
"lastyear": 2022,
"nyear": 1,
"datatype": "short",
"scalar": 1,
"order": "cellyear",
"bigendian": false,
"format": "cdf",
"ref_area": {
"filename": "with_tillage.nc4.json",
"format": "meta"
},
"filename": "with_tillage.nc4",
"global_attrs": {
"institution":"PIK Potsdam",
"contact":"Jane Doe",
"institution": "PIK Potsdam",
"contact": "Jane Doe",
"comment": "check"
}
}
}
63 changes: 36 additions & 27 deletions tests/test_couple.py
6D4E
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
"""Test the LPJmLCoupler class."""

import os
import numpy as np
from unittest.mock import patch
from copy import deepcopy
from pycoupler.coupler import LPJmLCoupler
import pytest


from .conftest import get_test_path


@patch.dict(os.environ, {"TEST_PATH": get_test_path(), "TEST_LINE_COUNTER": "0"})
def test_lpjml_coupler(test_path):

config_coupled_fn = f"{test_path}/data/config_coupled_test.json"
lpjml_coupler = LPJmLCoupler(config_file=config_coupled_fn)

"""Test the LPJmLCoupler class."""
def test_lpjml_coupler(test_path, lpjml_coupler):
inputs = lpjml_coupler.read_input(copy=False)
outputs = lpjml_coupler.read_historic_output()

Expand Down Expand Up @@ -55,24 +43,45 @@ def test_lpjml_coupler(test_path):
assert lpjml_coupler.sim_years == []
assert lpjml_coupler.coupled_years == []
assert [year for year in lpjml_coupler.get_coupled_years()] == []

second_coupler = deepcopy(lpjml_coupler)
lpjml_coupler.code_to_name(to_iso_alpha_3=False)
assert lpjml_coupler.country[0].item() == "Germany"
second_coupler.code_to_name(to_iso_alpha_3=True)
assert second_coupler.country[0].item() == "DEU"

assert (
repr(lpjml_coupler)
== f"<pycoupler.LPJmLCoupler>\nSimulation: (version: 3, localhost:<none>)\n * sim_year 2050\n * ncell 2\n * ninput 1\nConfiguration:\n Settings: lpjml v5.8\n (general)\n * sim_name coupled_test\n * firstyear 2001\n * lastyear 2050\n * startgrid 27410\n * endgrid 27411\n * landuse yes\n (changed)\n * model_path LPJmL_internal\n * sim_path {test_path}/data/\n * outputyear 2022\n * output_metafile True\n * write_restart False\n * nspinup 0\n * float_grid True\n * restart_filename restart/restart_historic_run.lpj\n * outputyear 2022\n * radiation cloudiness\n * fix_co2 True\n * fix_co2_year 2018\n * fix_climate True\n * fix_climate_cycle 11\n * fix_climate_year 2013\n * river_routing False\n * tillage_type read\n * residue_treatment fixed_residue_remove\n * double_harvest False\n * intercrop True\n * sim_path {test_path}/data/\n Coupled model: copan:CORE\n * start_coupling 2023\n * input (coupled) ['with_tillage']\n * output (coupled) ['grid', 'pft_harvestc', 'cftfrac', 'soilc_agr_layer', 'hdate', 'country', 'region']\n " # noqa
)


@patch.dict(os.environ, {"TEST_PATH": get_test_path(), "TEST_LINE_COUNTER": "0"})
def test_copy_input(test_path):
def test_lpjml_coupler_codes_name(lpjml_coupler):
lpjml_coupler.code_to_name(to_iso_alpha_3=False)
assert lpjml_coupler.country[0].item() == "Germany"

config_coupled_fn = f"{test_path}/data/config_coupled_test.json"
lpjml_coupler = LPJmLCoupler(config_file=config_coupled_fn)

inputs = lpjml_coupler.read_input(copy=False)
assert lpjml_coupler._copy_input(start_year=2022, end_year=2022) == "tested"
def test_lpjml_coupler_codes_iso(lpjml_coupler):
lpjml_coupler.code_to_name(to_iso_alpha_3=True)
assert lpjml_coupler.country[0].item() == "DEU"


# Test all period combination cases (data period is 2000 to 2022)
@pytest.mark.parametrize(
"start_year,end_year",
[
(2005, 2015),
(1980, 1998),
(2024, 2025),
(1998, 2025),
(1998, 2020),
(2020, 2025),
(None, 2024),
(1998, None),
(None, None),
pytest.param(
2025,
1998,
marks=pytest.mark.xfail(
raises=pytest.raises(
ValueError, match="start_year cannot be later than end_year"
)
),
),
],
)
def test_lpjml_coupler_copy_input_(test_path, lpjml_coupler, start_year, end_year):
assert lpjml_coupler._copy_input(start_year, end_year) == "tested"
13 changes: 1 addition & 12 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Test the LPJmLData class."""

import os
import numpy as np
from unittest.mock import patch

from pycoupler.data import (
read_data, 8CC7
Expand All @@ -12,8 +10,6 @@
LPJmLInputType,
append_to_dict,
)
from pycoupler.coupler import LPJmLCoupler
from .conftest import get_test_path


def test_read_data(test_path):
Expand Down Expand Up @@ -44,14 +40,7 @@ def test_dataset(test_path):
assert list(data_dict.keys()) == ["with_tillage"]


@patch.dict(
os.environ, {"TEST_PATH": get_test_path(), "TEST_LINE_COUNTER": "0"}
) # noqa
def test_get_neighbourhood(test_path):

config_coupled_fn = f"{test_path}/data/config_coupled_test.json"
lpjml_coupler = LPJmLCoupler(config_file=config_coupled_fn)

def test_get_neighbourhood(lpjml_coupler):
neighbourhood = lpjml_coupler.grid.get_neighbourhood().values

test_neighbours = np.array(
Expand Down
0