Skip to content

Commit

Permalink
Merge pull request #585 from djhoese/bugfix-opt-proj-yaml
Browse files Browse the repository at this point in the history
Fix optimize_projection handling in YAML parsing
  • Loading branch information
djhoese committed Feb 16, 2024
2 parents 56da958 + 4dfcda6 commit 8549a9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pyresample/area_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def _create_area_def_from_dict(area_name, params):
center=params.pop("center"),
resolution=params.pop("resolution"),
radius=params.pop("radius"),
optimize_projection=params.pop("optimize_projection", False),
**kwargs,
)
if params:
Expand Down Expand Up @@ -416,7 +417,7 @@ def _get_proj4_args(proj4_args):


def create_area_def(area_id, projection, width=None, height=None, area_extent=None, shape=None, upper_left_extent=None,
center=None, resolution=None, radius=None, units=None, **kwargs):
center=None, resolution=None, radius=None, units=None, optimize_projection=False, **kwargs):
"""Create AreaDefinition from whatever information is known.
Parameters
Expand Down Expand Up @@ -500,7 +501,8 @@ def create_area_def(area_id, projection, width=None, height=None, area_extent=No
except (RuntimeError, CRSError):
# Assume that an invalid projection will be "fixed" by a dynamic area definition later
return _make_area(area_id, description, proj_id, projection, shape, area_extent,
resolution=resolution, **kwargs)
resolution=resolution, optimize_projection=optimize_projection,
**kwargs)

# If no units are provided, try to get units used in proj_dict. If still none are provided, use meters.
if units is None:
Expand Down Expand Up @@ -536,7 +538,8 @@ def create_area_def(area_id, projection, width=None, height=None, area_extent=No
resolution, upper_left_extent, units,
p, crs)
return _make_area(area_id, description, proj_id, projection, shape,
area_extent, resolution=resolution, **kwargs)
area_extent, resolution=resolution, optimize_projection=optimize_projection,
**kwargs)


def _make_area(
Expand All @@ -546,14 +549,13 @@ def _make_area(
projection: Union[dict, CRS],
shape: tuple[int, ...] | None,
area_extent: tuple[float, float, float, float] | None,
optimize_projection: bool = False,
resolution: tuple[float, float] | float | None = None,
**kwargs):
"""Handle the creation of an area definition for create_area_def."""
from pyresample.future.geometry import AreaDefinition
from pyresample.geometry import DynamicAreaDefinition

# Remove arguments that are only for DynamicAreaDefinition.
optimize_projection = kwargs.pop('optimize_projection', False)
resolution = kwargs.pop('resolution', None)
# If enough data is provided, create an AreaDefinition. If only shape or area_extent are found, make a
# DynamicAreaDefinition. If not enough information was provided, raise a ValueError.
if area_extent is not None and shape is not None:
Expand All @@ -562,6 +564,7 @@ def _make_area(
"description": description,
"proj_id": proj_id,
}
# FUTURE: Don't add kwargs to attrs, switch to explicit "attrs"
attrs.update(kwargs)
area_def = AreaDefinition(projection, shape, area_extent, attrs=attrs)
return area_def if pyresample.config.get("features.future_geometries", False) else area_def.to_legacy()
Expand Down
12 changes: 12 additions & 0 deletions pyresample/test/test_area_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ def test_dynamic_area_parser_passes_resolution(self):
test_area_file = os.path.join(TEST_FILES_PATH, 'areas.yaml')
test_area = parse_area_file(test_area_file, 'omerc_bb_1000')[0]
assert test_area.resolution == (1000, 1000)
assert test_area.optimize_projection

def test_dynamic_area_parser_opt_projection_nores(self):
"""Test that a dynamic area definition can be frozen when no resolution is passed via YAML."""
from pyresample import SwathDefinition, parse_area_file
test_area_file = os.path.join(TEST_FILES_PATH, 'areas.yaml')
test_area = parse_area_file(test_area_file, 'omerc_bb_nores')[0]
assert test_area.resolution is None
assert test_area.optimize_projection
lons, lats = np.meshgrid(np.arange(10, 20), np.arange(10, 20))
swath_def = SwathDefinition(lons, lats)
test_area.freeze(swath_def)

def test_multiple_file_content(self):
from pyresample import parse_area_file
Expand Down
7 changes: 7 additions & 0 deletions pyresample/test/test_files/areas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ omerc_bb_1000:
optimize_projection: True
resolution: 1000

omerc_bb_nores:
description: Oblique Mercator Bounding Box for Polar Overpasses
projection:
ellps: sphere
proj: omerc
optimize_projection: True

test_dynamic_resolution:
description: Dynamic with resolution specified in meters
projection:
Expand Down

0 comments on commit 8549a9e

Please sign in to comment.