Skip to content

Commit

Permalink
Fix area definition YAML not warning on typos
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Jan 3, 2024
1 parent eaf8367 commit 26d8dc9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pyresample/area_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,25 @@ def _create_area_def_from_dict(area_name, params):
'upper_right_xy', 'units'])
params['resolution'] = _capture_subarguments(params, 'resolution', ['resolution', 'dx', 'dy', 'units'])
params['radius'] = _capture_subarguments(params, 'radius', ['radius', 'dx', 'dy', 'units'])
area_def = create_area_def(**params)
area_id = params.pop("area_id", area_name)
kwargs = {}
if "description" in params:
kwargs["description"] = params.pop("description")
if "proj_id" in params:
kwargs["proj_id"] = params.pop("proj_id")

Check warning on line 224 in pyresample/area_config.py

View check run for this annotation

Codecov / codecov/patch

pyresample/area_config.py#L224

Added line #L224 was not covered by tests
area_def = create_area_def(
area_id,
params.pop("projection"),
area_extent=params.pop("area_extent"),
shape=params.pop("shape"),
upper_left_extent=params.pop("upper_left_extent"),
center=params.pop("center"),
resolution=params.pop("resolution"),
radius=params.pop("radius"),
**kwargs,
)
if params:
warnings.warn(f"Unused/unexpected area definition parameter(s) for {area_id}: {params=}", stacklevel=4)
return area_def


Expand All @@ -240,11 +258,12 @@ def _capture_subarguments(params: dict, arg_name: str, sub_arg_list: list[str])
for sub_arg in sub_arg_list:
sub_arg_value = argument.get(sub_arg)
# Don't append units to the argument.
if sub_arg_value is not None:
if sub_arg in ('lower_left_xy', 'upper_right_xy') and isinstance(sub_arg_value, list):
list_of_values.extend(sub_arg_value)
else:
list_of_values.append(sub_arg_value)
if sub_arg_value is None:
continue
if sub_arg in ('lower_left_xy', 'upper_right_xy') and isinstance(sub_arg_value, list):
list_of_values.extend(sub_arg_value)
else:
list_of_values.append(sub_arg_value)
# If units are provided, convert to xarray.
if units is not None:
return DataArray(list_of_values, attrs={'units': units})
Expand Down
22 changes: 22 additions & 0 deletions pyresample/test/test_area_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import unittest

import numpy as np
import pytest

from pyresample.test.utils import TEST_FILES_PATH

Expand Down Expand Up @@ -278,3 +279,24 @@ def test_area_def_rst_list():
assert call_args[0][1]['include_static_files']
# check that static files are not included for the second area
assert not call_args[1][1]['include_static_files']


def test_unused_params_warn():
"""Test unused parameters produce a warning."""
from pyresample.area_config import load_area_from_string

yaml_str = """ease_sh2:
description: Antarctic EASE grid
projection:
a: 6371228.0
units: m
lon_0: 0
proj: laea
lat_0: -90
revolution: 1000
area_extent:
lower_left_xy: [-5326849.0625, -5326849.0625]
upper_right_xy: [5326849.0625, 5326849.0625]
units: m"""
with pytest.warns(UserWarning, match=r"Unused/unexpected area definition parameter.*revolution"):
load_area_from_string(yaml_str)

0 comments on commit 26d8dc9

Please sign in to comment.