From 60bda54f73fa228fae06353c78b8d8050197f956 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Fri, 10 Jan 2025 01:33:41 +0100 Subject: [PATCH] raise exception if projection info is missing --- mllam_data_prep/ops/projection.py | 8 ++++---- tests/test_from_config.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/mllam_data_prep/ops/projection.py b/mllam_data_prep/ops/projection.py index 1c60182..3bcf160 100644 --- a/mllam_data_prep/ops/projection.py +++ b/mllam_data_prep/ops/projection.py @@ -191,14 +191,14 @@ def validate_projection_consistency( >>> validate_projection_consistency([proj2, proj4]) Traceback (most recent call last): ... - ValueError: Multiple projections found in the dataset.Currently only one projection is supported. + NotImplementedError: Multiple projections found in the dataset. Currently only one projection is supported. """ proj_obs = [pyproj.CRS.from_cf(proj) for proj in projections] # Check that all projections are the same if len(set(proj_obs)) > 1: - raise ValueError( - "Multiple projections found in the dataset." + raise NotImplementedError( + "Multiple projections found in the dataset. " "Currently only one projection is supported." ) @@ -256,7 +256,7 @@ def get_projection_crs(ds: xr.Dataset) -> Dict[str, Any]: vars_wo_proj = set(ds.data_vars) - set(vars_w_proj.keys()) - proj_vars if len(proj_vars) > 1: - raise ValueError( + raise NotImplementedError( f"Multiple referenced projections found in the dataset {ds.encoding.get('source',None)}: {proj_vars}. " "Currently only one projection is supported." ) diff --git a/tests/test_from_config.py b/tests/test_from_config.py index 583b4bc..e20a59f 100644 --- a/tests/test_from_config.py +++ b/tests/test_from_config.py @@ -346,6 +346,28 @@ def test_projection_from_config(projection: Dict): ), "CRS mismatch" +@pytest.mark.parametrize( + "projection", + [ + {"proj2": {"dims": "[x y]", "crs_wkt": "EPSG:4326"}}, + ], +) +def test_requirement_of_single_projection(projection: Dict): + """ + Test that assertion is raised when projections + are missing from input datasets. + """ + # Adding projection config to the example config + config = yaml.safe_load(testconfig.VALID_EXAMPLE_CONFIG_YAML) + config["inputs"]["danra_surface"]["projections"] = projection + config_yaml = yaml.dump(config) + + config = mdp.Config.from_yaml(config_yaml) + + with pytest.raises(NotImplementedError): + mdp.create_dataset(config=config) + + def test_danra_example(): fp_config = Path(__file__).parent.parent / "example.danra.yaml" with tempfile.TemporaryDirectory(suffix=".zarr") as tmpdir: