def test_swath_wrap(self): lons1 = np.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats1 = np.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) lons1 += 180. if (sys.version_info < (2, 6) or (sys.version_info >= (3, 0) and sys.version_info < (3, 4))): swath_def = geometry.BaseDefinition(lons1, lats1) else: with warnings.catch_warnings(record=True) as w1: swath_def = geometry.BaseDefinition(lons1, lats1) self.assertFalse( len(w1) != 1, 'Failed to trigger a warning on longitude wrapping') self.assertFalse(( '-180:+180' not in str(w1[0].message) ), 'Failed to trigger correct warning about longitude wrapping' ) lons2, lats2 = swath_def.get_lonlats() self.assertTrue( id(lons1) != id(lons2), msg='Caching of swath coordinates failed with longitude wrapping') self.assertTrue(lons2.min() > -180 and lons2.max() < 180, 'Wrapping of longitudes failed for SwathDefinition')
def test_base_lon_wrapping(self): lons1 = np.arange(-135., +135, 50.) lats = np.ones_like(lons1) * 70. with warnings.catch_warnings(record=True) as w1: base_def1 = geometry.BaseDefinition(lons1, lats) self.assertFalse( len(w1) != 0, 'Got warning <%s>, but was not expecting one' % w1) lons2 = np.where(lons1 < 0, lons1 + 360, lons1) with warnings.catch_warnings(record=True) as w2: base_def2 = geometry.BaseDefinition(lons2, lats) self.assertFalse( len(w2) != 1, 'Failed to trigger a warning on longitude wrapping') self.assertFalse( ('-180:+180' not in str(w2[0].message)), 'Failed to trigger correct warning about longitude wrapping') self.assertFalse(base_def1 != base_def2, 'longitude wrapping to [-180:+180] did not work') with warnings.catch_warnings(record=True) as w3: base_def3 = geometry.BaseDefinition(None, None) self.assertFalse( len(w3) != 0, 'Got warning <%s>, but was not expecting one' % w3) self.assert_raises(ValueError, base_def3.get_lonlats)
def test_base_type(self): lons1 = np.arange(-135., +135, 50.) lats = np.ones_like(lons1) * 70. # Test dtype is preserved without longitude wrapping basedef = geometry.BaseDefinition(lons1, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons1.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons1.dtype, lons.dtype, )) lons1_ints = lons1.astype('int') basedef = geometry.BaseDefinition(lons1_ints, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons1_ints.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons1_ints.dtype, lons.dtype, )) # Test dtype is preserved with automatic longitude wrapping lons2 = np.where(lons1 < 0, lons1 + 360, lons1) with warnings.catch_warnings(record=True) as w: basedef = geometry.BaseDefinition(lons2, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons2.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons2.dtype, lons.dtype, )) lons2_ints = lons2.astype('int') with warnings.catch_warnings(record=True) as w: basedef = geometry.BaseDefinition(lons2_ints, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons2_ints.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons2_ints.dtype, lons.dtype, ))