Skip to content

Commit 60b4b38

Browse files
authored
Merge pull request #578 from mraspaud/add-attrs-to-swath-def
Add attrs to future swath definition
2 parents 8181023 + 5b05353 commit 60b4b38

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

pyresample/future/geometry/swath.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,64 @@
1919

2020
from __future__ import annotations
2121

22-
from pyresample.geometry import CoordinateDefinition, SwathDefinition # noqa
22+
import numpy as np
23+
24+
from pyresample.geometry import _get_highest_level_class # noqa
25+
from pyresample.geometry import CoordinateDefinition, DimensionError # noqa
26+
from pyresample.geometry import SwathDefinition as LegacySwathDefinition
27+
28+
29+
class SwathDefinition(LegacySwathDefinition):
30+
"""Swath defined by lons and lats.
31+
32+
Parameters
33+
----------
34+
lons : numpy array
35+
lats : numpy array
36+
crs: pyproj.CRS,
37+
The CRS to use. longlat on WGS84 by default.
38+
attrs: dict,
39+
A dictionary made to store metadata.
40+
41+
Attributes
42+
----------
43+
shape : tuple
44+
Swath shape
45+
size : int
46+
Number of elements in swath
47+
ndims : int
48+
Swath dimensions
49+
lons : object
50+
Swath lons
51+
lats : object
52+
Swath lats
53+
cartesian_coords : object
54+
Swath cartesian coordinates
55+
"""
56+
57+
def __init__(self, lons, lats, crs=None, attrs=None):
58+
super().__init__(lons, lats, crs=crs)
59+
self.attrs = attrs or {}
60+
61+
def __getitem__(self, key):
62+
"""Slice a 2D geographic definition."""
63+
y_slice, x_slice = key
64+
return self.__class__(
65+
lons=self.lons[y_slice, x_slice],
66+
lats=self.lats[y_slice, x_slice],
67+
attrs=self.attrs
68+
)
69+
70+
def concatenate(self, other):
71+
"""Concatenate coordinate definitions."""
72+
if self.ndim != other.ndim:
73+
raise DimensionError(('Unable to concatenate %sD and %sD '
74+
'geometries') % (self.ndim, other.ndim))
75+
if self.crs != other.crs:
76+
raise ValueError("Incompatible CRSs.")
77+
klass = _get_highest_level_class(self, other)
78+
lons = np.concatenate((self.lons, other.lons))
79+
lats = np.concatenate((self.lats, other.lats))
80+
attrs = self.attrs.copy()
81+
attrs.update(other.attrs)
82+
return klass(lons, lats, attrs=attrs)

pyresample/test/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def create_test_swath(swath_class):
7676
7777
"""
7878
def _create_test_swath(lons, lats, **kwargs):
79+
if swath_class is SwathDefinition:
80+
kwargs.pop("nproc", None)
7981
return swath_class(lons, lats, **kwargs)
8082
return _create_test_swath
8183

pyresample/test/test_geometry/test_swath.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,43 @@ def assert_np_dict_allclose(dict1, dict2):
462462
np.testing.assert_allclose(val, dict2[key])
463463
except TypeError:
464464
assert val == dict2[key]
465+
466+
467+
def test_future_swath_has_attrs():
468+
"""Test that future SwathDefinition has attrs."""
469+
from pyresample.future.geometry import SwathDefinition
470+
lons, lats = _gen_swath_lons_lats()
471+
attrs = dict(meta="data")
472+
swath = SwathDefinition(lons, lats, attrs=attrs)
473+
assert swath.attrs == attrs
474+
475+
476+
def test_future_swath_slice_has_attrs():
477+
"""Test that future sliced SwathDefinition has attrs."""
478+
from pyresample.future.geometry import SwathDefinition
479+
lons, lats = _gen_swath_lons_lats()
480+
attrs = dict(meta="data")
481+
swath = SwathDefinition(lons, lats, attrs=attrs)[0:1, 0:1]
482+
assert swath.attrs == attrs
483+
484+
485+
def test_future_swath_concat_has_attrs():
486+
"""Test that future concatenated SwathDefinition has attrs."""
487+
from pyresample.future.geometry import SwathDefinition
488+
lons, lats = _gen_swath_lons_lats()
489+
attrs1 = dict(meta1="data")
490+
swath1 = SwathDefinition(lons, lats, attrs=attrs1)
491+
attrs2 = dict(meta2="data")
492+
swath2 = SwathDefinition(lons, lats, attrs=attrs2)
493+
swath = swath1.concatenate(swath2)
494+
assert swath.attrs == dict(meta1="data", meta2="data")
495+
496+
497+
def test_future_swath_concat_fails_on_different_crs():
498+
"""Test that future concatenated SwathDefinition must have the same crs."""
499+
from pyresample.future.geometry import SwathDefinition
500+
lons, lats = _gen_swath_lons_lats()
501+
swath1 = SwathDefinition(lons, lats, crs="mycrs")
502+
swath2 = SwathDefinition(lons, lats, crs="myothercrs")
503+
with pytest.raises(ValueError, match="Incompatible CRSs."):
504+
_ = swath1.concatenate(swath2)

0 commit comments

Comments
 (0)