diff --git a/src/subscript/fmuobs/fmuobs.py b/src/subscript/fmuobs/fmuobs.py index 7f4518277..5954d7e38 100644 --- a/src/subscript/fmuobs/fmuobs.py +++ b/src/subscript/fmuobs/fmuobs.py @@ -335,10 +335,10 @@ def fmuobs( starttime=starttime, ) else: - dframe = ertobs2df(input_str, cwd=includedir) + dframe = ertobs2df(input_str, cwd=includedir, starttime=starttime) if starttime: - dframe = compute_date_from_days(dframe) + dframe = compute_date_from_days(dframe, starttime=starttime) if not validate_internal_dframe(dframe): logger.error("Observation dataframe is invalid!") diff --git a/tests/test_fmuobs.py b/tests/test_fmuobs.py index ad26e75ef..4fb9d26a1 100644 --- a/tests/test_fmuobs.py +++ b/tests/test_fmuobs.py @@ -247,6 +247,55 @@ def test_integration(): assert subprocess.check_output(["fmuobs", "-h"]) +@pytest.mark.integration +def test_startdate_argument(tmp_path, mocker, monkeypatch): + """Test that the startdate argument is handled correctly""" + monkeypatch.chdir(tmp_path) + # Test that summary observations without a date are not valid without + # startdate argument + mocker.patch( + "sys.argv", + [ + "fmuobs", + "--yml", + "output.yml", + str( + TESTDATA_DIR / "summary_obs_without_date.obs", + ), + ], + ) + with pytest.raises( + ValueError, match=r"Can't have summary observation without a date" + ): + main() + # Test that summary observations without a date are valid with startdate argument + start_date = "2020-01-01" + mocker.patch( + "sys.argv", + [ + "fmuobs", + "--startdate", + start_date, + "--yml", + "output.yml", + str( + TESTDATA_DIR / "summary_obs_without_date.obs", + ), + ], + ) + main() + assert Path("output.yml").exists() + # Test that the dates in the output are correctly calculated + # from the startdate and the days in the input + with open(Path("output.yml"), encoding="utf8") as stream: + config = yaml.safe_load(stream) + for smry in config["smry"]: + for obs in smry["observations"]: + assert pd.to_datetime(obs["date"]) == pd.to_datetime( + start_date + ) + pd.to_timedelta(obs["days"], unit="D") + + @pytest.mark.integration @pytest.mark.parametrize("verbose", ["", "--verbose", "--debug"]) def test_commandline(tmp_path, verbose, mocker, caplog, monkeypatch): diff --git a/tests/testdata_fmuobs/summary_obs_without_date.obs b/tests/testdata_fmuobs/summary_obs_without_date.obs new file mode 100644 index 000000000..6e14a1dd1 --- /dev/null +++ b/tests/testdata_fmuobs/summary_obs_without_date.obs @@ -0,0 +1,28 @@ + -- These observations are modified from ert-doc.obs + -- and stripped down to demonstrate summary observations without dates + + SUMMARY_OBSERVATION SEP_TEST_2005 + { + VALUE = 100.0; + ERROR = 5; + DAYS = 135; + KEY = GOPR:BRENT; + }; + + SUMMARY_OBSERVATION SEP_TEST_2006 + { + VALUE = 100.0; + ERROR = 5; + DAYS = 500; + KEY = GOPR:BRENT; + }; + + SUMMARY_OBSERVATION SEP_TEST_2008 + { + VALUE = 213; + ERROR = 10; + DAYS = 911; + KEY = GOPR:NESS; + }; + + \ No newline at end of file