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

Fix CRS object being recreated when adding CRS coordinate #915

Merged
merged 2 commits into from
Sep 27, 2019
Merged
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
7 changes: 5 additions & 2 deletions satpy/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,11 @@ def add_crs_xy_coords(data_arr, area):
# default lat/lon projection
latlon_proj = "+proj=latlong +datum=WGS84 +ellps=WGS84"
# otherwise get it from the area definition
proj_str = getattr(area, 'proj_str', latlon_proj)
crs = CRS.from_string(proj_str)
if hasattr(area, 'crs'):
crs = area.crs
else:
proj_str = getattr(area, 'proj_str', latlon_proj)
crs = CRS.from_string(proj_str)
data_arr = data_arr.assign_coords(crs=crs)

# Add x/y coordinates if possible
Expand Down
26 changes: 24 additions & 2 deletions satpy/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ def test_2d_ewa(self, get_lonlats, ll2cr, fornav):
self.assertIn('lcc', new_data.coords['crs'].item().to_proj4())
self.assertEqual(new_data.coords['y'].attrs['units'], 'meter')
self.assertEqual(new_data.coords['x'].attrs['units'], 'meter')
if hasattr(target_area, 'crs'):
self.assertIs(target_area.crs, new_data.coords['crs'].item())

@mock.patch('satpy.resample.fornav')
@mock.patch('satpy.resample.ll2cr')
Expand Down Expand Up @@ -352,6 +354,8 @@ def test_3d_ewa(self, get_lonlats, ll2cr, fornav):
self.assertEqual(new_data.coords['x'].attrs['units'], 'meter')
np.testing.assert_equal(new_data.coords['bands'].values,
['R', 'G', 'B'])
if hasattr(target_area, 'crs'):
self.assertIs(target_area.crs, new_data.coords['crs'].item())


class TestNativeResampler(unittest.TestCase):
Expand Down Expand Up @@ -400,6 +404,8 @@ def test_expand_dims(self):
self.assertIn('lcc', new_data.coords['crs'].item().to_proj4())
self.assertEqual(new_data.coords['y'].attrs['units'], 'meter')
self.assertEqual(new_data.coords['x'].attrs['units'], 'meter')
if hasattr(target_area, 'crs'):
self.assertIs(target_area.crs, new_data.coords['crs'].item())

def test_expand_dims_3d(self):
"""Test expanding native resampling with 3D data."""
Expand All @@ -424,6 +430,8 @@ def test_expand_dims_3d(self):
self.assertIn('lcc', new_data.coords['crs'].item().to_proj4())
self.assertEqual(new_data.coords['y'].attrs['units'], 'meter')
self.assertEqual(new_data.coords['x'].attrs['units'], 'meter')
if hasattr(target_area, 'crs'):
self.assertIs(target_area.crs, new_data.coords['crs'].item())

def test_expand_without_dims(self):
"""Test expanding native resampling with no dimensions specified."""
Expand All @@ -440,6 +448,8 @@ def test_expand_without_dims(self):
self.assertIn('crs', new_data.coords)
self.assertIsInstance(new_data.coords['crs'].item(), CRS)
self.assertIn('lcc', new_data.coords['crs'].item().to_proj4())
if hasattr(target_area, 'crs'):
self.assertIs(target_area.crs, new_data.coords['crs'].item())

def test_expand_without_dims_4D(self):
"""Test expanding native resampling with 4D data with no dimensions specified."""
Expand Down Expand Up @@ -499,6 +509,8 @@ def test_bil_resampling(self, resampler, create_filename, zarr_open,
self.assertIn('lcc', new_data.coords['crs'].item().to_proj4())
self.assertEqual(new_data.coords['y'].attrs['units'], 'meter')
self.assertEqual(new_data.coords['x'].attrs['units'], 'meter')
if hasattr(target_area, 'crs'):
self.assertIs(target_area.crs, new_data.coords['crs'].item())

# Test that the resampling info is tried to read from the disk
resampler = BilinearResampler(source_swath, target_area)
Expand Down Expand Up @@ -625,6 +637,8 @@ def test_area_def_coordinates(self):
new_data_arr.coords['x'].attrs['units'], 'meter')
self.assertIn('crs', new_data_arr.coords)
self.assertIsInstance(new_data_arr.coords['crs'].item(), CRS)
if hasattr(area_def, 'crs'):
self.assertIs(area_def.crs, new_data_arr.coords['crs'].item())

# already has coords
data_arr = xr.DataArray(
Expand All @@ -643,6 +657,8 @@ def test_area_def_coordinates(self):
if CRS is not None:
self.assertIn('crs', new_data_arr.coords)
self.assertIsInstance(new_data_arr.coords['crs'].item(), CRS)
if hasattr(area_def, 'crs'):
self.assertIs(area_def.crs, new_data_arr.coords['crs'].item())

# lat/lon area
area_def = AreaDefinition(
Expand All @@ -667,6 +683,8 @@ def test_area_def_coordinates(self):
new_data_arr.coords['x'].attrs['units'], 'degrees_east')
self.assertIn('crs', new_data_arr.coords)
self.assertIsInstance(new_data_arr.coords['crs'].item(), CRS)
if hasattr(area_def, 'crs'):
self.assertIs(area_def.crs, new_data_arr.coords['crs'].item())

def test_swath_def_coordinates(self):
"""Test coordinates being added with an SwathDefinition."""
Expand Down Expand Up @@ -711,6 +729,7 @@ class TestBucketAvg(unittest.TestCase):
"""Test the bucket resampler."""

def setUp(self):
"""Create fake area definitions and resampler to be tested."""
from satpy.resample import BucketAvg
get_lonlats = mock.MagicMock()
get_lonlats.return_value = (1, 2)
Expand All @@ -719,14 +738,14 @@ def setUp(self):
self.bucket = BucketAvg(self.source_geo_def, self.target_geo_def)

def test_init(self):
"""Test bucket resampler initialization"""
"""Test bucket resampler initialization."""
self.assertIsNone(self.bucket.resampler)
self.assertTrue(self.bucket.source_geo_def == self.source_geo_def)
self.assertTrue(self.bucket.target_geo_def == self.target_geo_def)

@mock.patch('pyresample.bucket.BucketResampler')
def test_precompute(self, bucket):
"""Test bucket resampler precomputation"""
"""Test bucket resampler precomputation."""
bucket.return_value = True
self.bucket.precompute()
self.assertTrue(self.bucket.resampler)
Expand Down Expand Up @@ -810,6 +829,7 @@ class TestBucketSum(unittest.TestCase):
"""Test the sum bucket resampler."""

def setUp(self):
"""Create fake area definitions and resampler to be tested."""
from satpy.resample import BucketSum
get_lonlats = mock.MagicMock()
get_lonlats.return_value = (1, 2)
Expand Down Expand Up @@ -850,6 +870,7 @@ class TestBucketCount(unittest.TestCase):
"""Test the count bucket resampler."""

def setUp(self):
"""Create fake area definitions and resampler to be tested."""
from satpy.resample import BucketCount
get_lonlats = mock.MagicMock()
get_lonlats.return_value = (1, 2)
Expand Down Expand Up @@ -886,6 +907,7 @@ class TestBucketFraction(unittest.TestCase):
"""Test the fraction bucket resampler."""

def setUp(self):
"""Create fake area definitions and resampler to be tested."""
from satpy.resample import BucketFraction
get_lonlats = mock.MagicMock()
get_lonlats.return_value = (1, 2)
Expand Down