def test_data_and_x_y_2d(self): '''Test data, x, and y 2d; no keyword name for x and y''' c_data, c_lons, c_lats = add_cyclic(self.data2d, self.lon2d, self.lat2d) assert_array_equal(c_data, self.c_data2d) assert_array_equal(c_lons, self.c_lon2d) assert_array_equal(c_lats, self.c_lat2d)
def test_precision_has_cyclic(self): '''Test precision keyword detecting cyclic point''' r_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1) r_lons = np.concatenate((self.lons, np.array([360. + 1e-3]))) c_data, c_lons = add_cyclic(r_data, x=r_lons, precision=1e-2) assert_array_equal(c_data, r_data) assert_array_equal(c_lons, r_lons)
def test_masked_data(self): '''Test masked data''' new_data = ma.masked_less(self.data2d, 3) c_data = add_cyclic(new_data) r_data = ma.concatenate((self.data2d, self.data2d[:, :1]), axis=1) assert_array_equal(c_data, r_data) assert ma.is_masked(c_data)
def test_has_cyclic_2d_full(self): '''Test detection of cyclic point 2d including y''' c_data, c_lons, c_lats = add_cyclic(self.c_data2d, x=self.c_lon2d, y=self.c_lat2d) assert_array_equal(c_data, self.c_data2d) assert_array_equal(c_lons, self.c_lon2d) assert_array_equal(c_lats, self.c_lat2d)
def test_data_and_x_1d_y_2d(self): '''Test data and x 1d and y 2d''' c_data, c_lons, c_lats = add_cyclic(self.data2d, x=self.lons, y=self.lat2d) assert_array_equal(c_data, self.c_data2d) assert_array_equal(c_lons, self.c_lons) assert_array_equal(c_lats, self.c_lat2d)
def test_data_and_x_y_with_axis_nd(self): '''Test axis keyword data 4d, x 3d and y 2d''' c_data, c_lons, c_lats = add_cyclic(self.data4d, x=self.lon3d, y=self.lat2d, axis=1) assert_array_equal(c_data, self.c_data4d) assert_array_equal(c_lons, self.c_lon3d) assert_array_equal(c_lats, self.c_lat2d)
def test_precision_has_cyclic_no(self): '''Test precision keyword detecting no cyclic point''' new_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1) new_lons = np.concatenate((self.lons, np.array([360. + 1e-3]))) c_data, c_lons = add_cyclic(new_data, x=new_lons, precision=2e-4) r_data = np.concatenate((new_data, new_data[:, :1]), axis=1) r_lons = np.concatenate((new_lons, np.array([360.]))) assert_array_equal(c_data, r_data) assert_array_equal(c_lons, r_lons)
def test_masked_data_and_x_y_2d(self): '''Test masked data and x''' new_data = ma.masked_less(self.data2d, 3) new_lon = ma.masked_less(self.lon2d, 2) c_data, c_lons, c_lats = add_cyclic(new_data, x=new_lon, y=self.lat2d) r_data = ma.concatenate((self.data2d, self.data2d[:, :1]), axis=1) r_lons = np.concatenate( (self.lon2d, np.full((self.lon2d.shape[0], 1), 360.)), axis=1) assert_array_equal(c_data, r_data) assert_array_equal(c_lons, r_lons) assert_array_equal(c_lats, self.c_lat2d) assert ma.is_masked(c_data) assert ma.is_masked(c_lons) assert not ma.is_masked(c_lats)
def test_cyclic_has_cyclic(self): '''Test detection of cyclic point with cyclic keyword''' new_lons = np.deg2rad(self.lon2d) new_lats = np.deg2rad(self.lat2d) r_data = np.concatenate((self.data2d, self.data2d[:, :1]), axis=1) r_lons = np.concatenate( (new_lons, np.full((new_lons.shape[0], 1), np.deg2rad(360.))), axis=1) r_lats = np.concatenate((new_lats, new_lats[:, -1:]), axis=1) c_data, c_lons, c_lats = add_cyclic(r_data, x=r_lons, y=r_lats, cyclic=np.deg2rad(360.)) assert_array_equal(c_data, self.c_data2d) assert_array_equal(c_lons, r_lons) assert_array_equal(c_lats, r_lats)
def test_cyclic(self): '''Test cyclic keyword with axis data 4d, x 3d and y 2d''' new_lons = np.deg2rad(self.lon3d) new_lats = np.deg2rad(self.lat2d) c_data, c_lons, c_lats = add_cyclic(self.data4d, x=new_lons, y=new_lats, axis=1, cyclic=np.deg2rad(360.)) r_lons = np.concatenate( (new_lons, np.full( (new_lons.shape[0], 1, new_lons.shape[2]), np.deg2rad(360.))), axis=1) r_lats = np.concatenate((new_lats, new_lats[:, -1:]), axis=1) assert_array_equal(c_data, self.c_data4d) assert_array_equal(c_lons, r_lons) assert_array_equal(c_lats, r_lats)
def test_invalid_x_size_3d(self): '''Catch wrong x size 3d''' with pytest.raises(ValueError): c_data, c_lons = add_cyclic(self.data4d, x=self.lon3d[:, :-1, :], axis=1)
def test_invalid_x_size_2d(self): '''Catch wrong x size 2d''' with pytest.raises(ValueError): c_data, c_lons = add_cyclic(self.data2d, x=self.lon2d[:, :-1])
def test_invalid_y_dimensionality(self): '''Catch wrong y dimensions''' with pytest.raises(ValueError): c_data, c_lons, c_lats = add_cyclic(self.data2d, x=self.lon2d, y=self.lat3d)
def test_invalid_x_dimensionality(self): '''Catch wrong x dimensions''' with pytest.raises(ValueError): c_data, c_lons = add_cyclic(self.data2d, x=self.lon3d)
def test_data_only_ignore_y(self): '''Test y given but no x''' c_data = add_cyclic(self.data2d, y=self.lat2d) assert_array_equal(c_data, self.c_data2d)
def test_invalid_y_size(self): '''Catch wrong y size 2d''' with pytest.raises(ValueError): c_data, c_lons, c_lats = add_cyclic(self.data2d, x=self.lon2d, y=self.lat2d[:, 1:])
def test_data_only(self): '''Test only data no x given''' c_data = add_cyclic(self.data2d) assert_array_equal(c_data, self.c_data2d)
def test_data_and_x_with_axis_3d(self): '''Test axis keyword data 4d, x 3d''' c_data, c_lons = add_cyclic(self.data4d, x=self.lon3d, axis=1) assert_array_equal(c_data, self.c_data4d) assert_array_equal(c_lons, self.c_lon3d)
def test_data_only_with_axis(self): '''Test axis keyword data only''' c_data = add_cyclic(self.data4d, axis=1) assert_array_equal(c_data, self.c_data4d)
def test_data_and_x_1d(self): '''Test data 2d and x 1d''' c_data, c_lons = add_cyclic(self.data2d, x=self.lons) assert_array_equal(c_data, self.c_data2d) assert_array_equal(c_lons, self.c_lons)
def test_has_cyclic_2d(self): '''Test detection of cyclic point 2d''' c_data, c_lons = add_cyclic(self.c_data2d, x=self.c_lon2d) assert_array_equal(c_data, self.c_data2d) assert_array_equal(c_lons, self.c_lon2d)
def test_data_and_x_2d(self): '''Test data and x 2d; no keyword name for x''' c_data, c_lons = add_cyclic(self.data2d, self.lon2d) assert_array_equal(c_data, self.c_data2d) assert_array_equal(c_lons, self.c_lon2d)
def test_invalid_axis(self): '''Catch wrong axis keyword''' with pytest.raises(ValueError): add_cyclic(self.data2d, axis=-3)
def main(): # data with longitude centers from 0 to 360 nlon = 24 nlat = 12 # 7.5, 22.5, ..., 337.5, 352.5 dlon = 360//nlon lon = np.linspace(dlon/2., 360.-dlon/2., nlon) # -82.5, -67.5, ..., 67.5, 82.5 dlat = 180//nlat lat = np.linspace(-90.+dlat/2., 90.-dlat/2., nlat) # 0, 1, ..., 10, 11, 11, 10, ..., 1, 0 data = np.concatenate((np.arange(nlon // 2), np.arange(nlon // 2)[::-1])) data = np.tile(data, nlat).reshape((nlat, nlon)) fig = plt.figure() # plot with central longitude 180 ax1 = fig.add_subplot(2, 2, 1, projection=ccrs.Robinson(central_longitude=180)) ax1.set_title("1d longitudes, central longitude=180", fontsize='small') ax1.set_global() ax1.contourf(lon, lat, data, transform=ccrs.PlateCarree(), cmap='RdBu') ax1.coastlines() # plot with central longitude 0 ax2 = fig.add_subplot(2, 2, 2, projection=ccrs.Robinson(central_longitude=0)) ax2.set_title("1d longitudes, central longitude=0", fontsize='small') ax2.set_global() ax2.contourf(lon, lat, data, transform=ccrs.PlateCarree(), cmap='RdBu') ax2.coastlines() # add cyclic points to data and longitudes # latitudes are unchanged in 1-dimension cdata, clon, clat = cutil.add_cyclic(data, lon, lat) ax3 = fig.add_subplot(2, 2, 3, projection=ccrs.Robinson(central_longitude=180)) ax3.set_title("Cyclic 1d longitudes, central longitude=180", fontsize='small') ax3.set_global() ax3.contourf(clon, clat, cdata, transform=ccrs.PlateCarree(), cmap='RdBu') ax3.coastlines() # add_cyclic also works with 2-dimensional data # Cyclic points are added to data, longitudes, and latitudes to # ensure the dimensions of the returned arrays are all the same shape. lon2d, lat2d = np.meshgrid(lon, lat) cdata, clon2d, clat2d = cutil.add_cyclic(data, lon2d, lat2d) ax4 = fig.add_subplot(2, 2, 4, projection=ccrs.Robinson(central_longitude=0)) ax4.set_title("Cyclic 2d longitudes, central longitude=0", fontsize='small') ax4.set_global() ax4.contourf(clon2d, clat2d, cdata, transform=ccrs.PlateCarree(), cmap='RdBu') ax4.coastlines() plt.show()