Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the GCPDefinition #476

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def __init__(self, lons, lats, nprocs=1, crs=None):
if not isinstance(lons, (np.ndarray, DataArray)):
lons = np.asanyarray(lons)
lats = np.asanyarray(lats)
super(SwathDefinition, self).__init__(lons, lats, nprocs)
super().__init__(lons, lats, nprocs)
if lons.shape != lats.shape:
raise ValueError('lon and lat arrays must have same shape')
elif lons.ndim > 2:
Expand Down Expand Up @@ -3001,3 +3001,31 @@ def enclose_areas(*areas, area_id="joint-area"):

def _numpy_values_to_native(values):
return [n.item() if isinstance(n, np.number) else n for n in values]


class GCPDefinition(SwathDefinition):
"""Swath definition with added support for GCPs.

GCPs should contain longitudes and latitudes.
"""

def __init__(self, lons, lats, gcps):
"""Instantiate the GCPDefinition.

Arguments:
lons: longitudes
lats: latitudes
gcps: Array of ground control points
"""
super().__init__(lons, lats)
self.gcps = gcps

def get_coarse_bbox_lonlats(self):
"""Get a coarse bounding box from the gcps.

Assumes gcps are a 2d array.
"""
return np.hstack((self.gcps[0, :-1],
self.gcps[:-1, -1],
self.gcps[-1, -1:0:-1],
self.gcps[-1:0:-1, 0]))
30 changes: 30 additions & 0 deletions pyresample/test/test_gcp_def.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Test the GCPDefinition class."""
import numpy as np
import pytest

from pyresample.geometry import GCPDefinition

gcp_dtype = np.dtype([("longitude", float),
("latitude", float),
("altitude", float),
("x", float),
("y", float)])


def test_coarse_bounding_box(gcp_definition):
"""Test the coarse bounding box method."""
bblons = np.hstack((np.arange(10), np.arange(19, 99, 10), np.arange(99, 90, -1), np.arange(90, 0, -10)))
np.testing.assert_array_equal(bblons, gcp_definition.get_coarse_bbox_lonlats()["longitude"])


@pytest.fixture
def gcp_definition():
"""Create a GCPDefinition instance."""
lons = None
lats = None
gcps = np.zeros(100, dtype=gcp_dtype)
gcps["longitude"] = np.arange(100)
gcps["latitude"] = np.arange(100, 200)
gcps = gcps.reshape((10, 10))
gdef = GCPDefinition(lons, lats, gcps)
return gdef