Skip to content

Commit

Permalink
Merge pull request #513 from djhoese/remove-warnings2
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese authored May 7, 2023
2 parents 327cec1 + ed0efa2 commit 4b75366
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
3 changes: 2 additions & 1 deletion pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,8 @@ def freeze(self, lonslats=None, resolution=None, shape=None, proj_info=None,
Shape parameters are ignored if the instance is created
with the `optimize_projection` flag set to True.
"""
proj_dict = self._get_proj_dict()
with ignore_pyproj_proj_warnings():
proj_dict = self._get_proj_dict()
projection = self._projection
if proj_info is not None:
# this is now our complete projection information
Expand Down
8 changes: 4 additions & 4 deletions pyresample/gradient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ def _get_projection_coordinates(self, datachunks):
self.dst_x, self.dst_y = transform(
self.dst_x, self.dst_y,
src_prj=dst_crs, dst_prj=src_crs)
self.prj = pyproj.Proj(**self.source_geo_def.proj_dict)
self.prj = pyproj.Proj(self.source_geo_def.crs)
else:
self.src_x, self.src_y = transform(
self.src_x, self.src_y,
src_prj=src_crs, dst_prj=dst_crs)
self.prj = pyproj.Proj(**self.target_geo_def.proj_dict)
self.prj = pyproj.Proj(self.target_geo_def.crs)

def _get_src_poly(self, src_y_start, src_y_end, src_x_start, src_x_end):
"""Get bounding polygon for source chunk."""
Expand Down Expand Up @@ -373,9 +373,9 @@ def _check_input_coordinates(dst_x, dst_y,
raise ValueError("Target arrays should all have the same shape")


def get_border_lonlats(geo_def):
def get_border_lonlats(geo_def: AreaDefinition):
"""Get the border x- and y-coordinates."""
if geo_def.proj_dict['proj'] == 'geos':
if geo_def.is_geostationary:
lon_b, lat_b = get_geostationary_bounding_box_in_lonlats(geo_def, 3600)
else:
lons, lats = geo_def.get_boundary_lonlats()
Expand Down
5 changes: 2 additions & 3 deletions pyresample/test/test_dask_ewa.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ def _get_test_swath_def(input_shape, chunk_size, geo_dims):

def _get_test_target_area(output_shape, output_proj=None):
from pyresample.geometry import AreaDefinition
from pyresample.utils import proj4_str_to_dict
if output_proj is None:
output_proj = ('+proj=lcc +datum=WGS84 +ellps=WGS84 '
'+lon_0=-95. +lat_0=25 +lat_1=25 +units=m +no_defs')
target = AreaDefinition(
'test_target',
'test_target',
'test_target',
proj4_str_to_dict(output_proj),
output_proj,
output_shape[1], # width
output_shape[0], # height
(-100000., -150000., 100000., 150000.),
Expand Down Expand Up @@ -146,7 +145,7 @@ def _coord_and_crs_checks(new_data, target_area, has_bands=False):
assert 'bands' in new_data.coords
assert 'crs' in new_data.coords
assert isinstance(new_data.coords['crs'].item(), CRS)
assert 'lcc' in new_data.coords['crs'].item().to_proj4()
assert "Lambert" in new_data.coords['crs'].item().coordinate_operation.method_name
assert new_data.coords['y'].attrs['units'] == 'meter'
assert new_data.coords['x'].attrs['units'] == 'meter'
assert target_area.crs == new_data.coords['crs'].item()
Expand Down
4 changes: 2 additions & 2 deletions pyresample/test/test_geometry/test_swath.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def test_compute_optimal_bb(self, create_test_swath):
proj_dict.update({'x_0': 0, 'y_0': 0, 'units': 'm',
'k': 1, 'gamma': 0,
'no_defs': None, 'type': 'crs'})
assert_np_dict_allclose(res.proj_dict, proj_dict)
assert res.crs == CRS.from_dict(proj_dict)
assert res.shape == (6, 3)

area = create_test_swath(nplons, nplats)
Expand All @@ -464,7 +464,7 @@ def test_compute_optimal_bb(self, create_test_swath):
proj_dict.update({'x_0': 0, 'y_0': 0, 'units': 'm',
'k': 1, 'gamma': 0,
'no_defs': None, 'type': 'crs'})
assert_np_dict_allclose(res.proj_dict, proj_dict)
assert res.crs == CRS.from_dict(proj_dict)
assert res.shape == (6, 3)

def test_compute_optimal_bb_with_resolution(self, create_test_swath):
Expand Down
53 changes: 32 additions & 21 deletions pyresample/test/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""Tests for the gradien search resampling."""

import unittest
import warnings
from unittest import mock

import dask.array as da
Expand All @@ -34,10 +35,10 @@
from pyresample.gradient import ResampleBlocksGradientSearchResampler


class TestOGradientResampler(unittest.TestCase):
class TestOGradientResampler:
"""Test case for the gradient resampling."""

def setUp(self):
def setup_method(self):
"""Set up the test case."""
from pyresample.gradient import StackingGradientSearchResampler
self.src_area = AreaDefinition('dst', 'dst area', None,
Expand All @@ -53,9 +54,10 @@ def setUp(self):
(-2717181.7304994687, -5571048.14031214,
1378818.2695005313, -1475048.1403121399))

self.resampler = StackingGradientSearchResampler(self.src_area, self.dst_area)
self.swath_resampler = StackingGradientSearchResampler(self.src_swath,
self.dst_area)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message=".*which is still EXPERIMENTAL.*", category=UserWarning)
self.resampler = StackingGradientSearchResampler(self.src_area, self.dst_area)
self.swath_resampler = StackingGradientSearchResampler(self.src_swath, self.dst_area)

def test_get_projection_coordinates_area_to_area(self):
"""Check that the coordinates are initialized, for area -> area."""
Expand Down Expand Up @@ -245,8 +247,9 @@ def test_resample_swath_to_area_2d(self):
"""Resample swath to area, 2d."""
data = xr.DataArray(da.ones(self.src_swath.shape, dtype=np.float64),
dims=['y', 'x'])
res = self.swath_resampler.compute(
data, method='bil').compute(scheduler='single-threaded')
with np.errstate(invalid="ignore"): # 'inf' space pixels cause runtime warnings
res = self.swath_resampler.compute(
data, method='bil').compute(scheduler='single-threaded')
assert res.shape == self.dst_area.shape
assert not np.all(np.isnan(res))

Expand All @@ -256,8 +259,9 @@ def test_resample_swath_to_area_3d(self):
dtype=np.float64) *
np.array([1, 2, 3])[:, np.newaxis, np.newaxis],
dims=['bands', 'y', 'x'])
res = self.swath_resampler.compute(
data, method='bil').compute(scheduler='single-threaded')
with np.errstate(invalid="ignore"): # 'inf' space pixels cause runtime warnings
res = self.swath_resampler.compute(
data, method='bil').compute(scheduler='single-threaded')
assert res.shape == (3, ) + self.dst_area.shape
for i in range(res.shape[0]):
arr = np.ravel(res[i, :, :])
Expand Down Expand Up @@ -599,24 +603,31 @@ def test_check_overlap():
assert check_overlap(poly1, poly2) is False


@mock.patch('pyresample.gradient.get_geostationary_bounding_box_in_lonlats')
def test_get_border_lonlats(get_geostationary_bounding_box):
"""Test that correct methods are called in get_border_lonlats()."""
def test_get_border_lonlats_geos():
"""Test that correct methods are called in get_border_lonlats() with geos inputs."""
from pyresample.gradient import get_border_lonlats
geo_def = mock.MagicMock(proj_dict={'proj': 'geos'})
get_geostationary_bounding_box.return_value = 1, 2
res = get_border_lonlats(geo_def)
geo_def = AreaDefinition("", "", "",
"+proj=geos +h=1234567", 2, 2, [1, 2, 3, 4])
with mock.patch("pyresample.gradient.get_geostationary_bounding_box_in_lonlats") as get_geostationary_bounding_box:
get_geostationary_bounding_box.return_value = 1, 2
res = get_border_lonlats(geo_def)
assert res == (1, 2)
get_geostationary_bounding_box.assert_called_with(geo_def, 3600)
geo_def.get_boundary_lonlats.assert_not_called()

lon_sides = mock.MagicMock(side1=np.array([1]), side2=np.array([2]),

def test_get_border_lonlats():
"""Test that correct methods are called in get_border_lonlats()."""
from pyresample.boundary import SimpleBoundary
from pyresample.gradient import get_border_lonlats
lon_sides = SimpleBoundary(side1=np.array([1]), side2=np.array([2]),
side3=np.array([3]), side4=np.array([4]))
lat_sides = mock.MagicMock(side1=np.array([1]), side2=np.array([2]),
lat_sides = SimpleBoundary(side1=np.array([1]), side2=np.array([2]),
side3=np.array([3]), side4=np.array([4]))
geo_def = mock.MagicMock()
geo_def.get_boundary_lonlats.return_value = lon_sides, lat_sides
lon_b, lat_b = get_border_lonlats(geo_def)
geo_def = AreaDefinition("", "", "",
"+proj=lcc +lat_1=25 +lat_2=25", 2, 2, [1, 2, 3, 4])
with mock.patch.object(geo_def, "get_boundary_lonlats") as get_boundary_lonlats:
get_boundary_lonlats.return_value = lon_sides, lat_sides
lon_b, lat_b = get_border_lonlats(geo_def)
assert np.all(lon_b == np.array([1, 2, 3, 4]))
assert np.all(lat_b == np.array([1, 2, 3, 4]))

Expand Down

0 comments on commit 4b75366

Please sign in to comment.