def test_good_dtypes(): # FIXME: Using `np.asfarray` will prevent from using complex dtypes. # NOTE: There is probably a more "numpy" efficient way to test this. q, p, x, y = data() dtypes = [int, float, np.integer, np.float16, np.float32, np.float64, np.float128, np.floating] for dtype in dtypes: zslice(np.empty_like(q, dtype=dtype), p, p0=0)
def test_good_dtypes(data): # FIXME: Using `np.asfarray` will prevent from using complex dtypes. # NOTE: There is probably a more "numpy" efficient way to test this. dtypes = [ int, float, np.integer, np.float16, np.float32, np.float64, np.float128, np.floating, ] for dtype in dtypes: zslice(np.empty_like(data["q"], dtype=dtype), data["p"], p0=0)
def test_bad_dtypes(data): # FIXME: Boolean array are converted to float! Only str fails correctly. with pytest.raises(ValueError): zslice(np.empty_like(data["q"], dtype=np.str_), data["p"], p0=0)
def test_p0_wrong_shape(data): with pytest.raises(ValueError): zslice(data["q"], data["p"], p0=np.zeros((2, 2)))
def test_mismatch_shapes(data): with pytest.raises(ValueError): zslice(data["q"], data["p"][0], p0=0)
def test_mismatch_shapes(): q, p, x, y = data() with pytest.raises(ValueError): zslice(q, p[0], p0=0)
def test_corret_results_2D(data, expected_results): K, I, J = data["q"].shape s50 = zslice(data["q"].reshape(K, -1), data["p"].reshape(K, -1), p0=-50) np.testing.assert_almost_equal(s50, expected_results.ravel())
def test_gt_3D_input(data): with pytest.raises(ValueError): zslice(data["q"][np.newaxis, ...], data["p"][np.newaxis, ...], p0=0)
def test_2D_input(data): K, I, J = data["q"].shape s50 = zslice(data["q"].reshape(K, -1), data["p"].reshape(K, -1), p0=-50) assert s50.shape == (I * J, )
def test_gt_3D_input(): q, p, x, y = data() with pytest.raises(ValueError): zslice(q[np.newaxis, ...], p[np.newaxis, ...], p0=0)
def test_1D_input(): q, p, x, y = data() with pytest.raises(ValueError): zslice(q.ravel(), p.ravel(), p0=0)
def test_2D_input(): q, p, x, y = data() K, I, J = q.shape s50 = zslice(q.reshape(K, -1), p.reshape(K, -1), p0=-50) assert s50.shape == (I*J,)
def test_3D_input(): q, p, x, y = data() K, I, J = q.shape s50 = zslice(q, p, p0=-50) assert s50.shape == (I, J)
def test_bad_dtypes(): # FIXME: Boolean array are converted to float! Only str fails correctly. q, p, x, y = data() with pytest.raises(ValueError): zslice(np.empty_like(q, dtype=np.str_), p, p0=0)
def test_p0_wrong_shape(): q, p, x, y = data() with pytest.raises(ValueError): zslice(q, p, p0=np.zeros((2, 2)))
def test_corret_results_3D(): q, p, x, y = data() s50 = zslice(q, p, p0=-50) f50 = expected_results() np.testing.assert_almost_equal(s50, f50)
def test_3D_input(data): K, I, J = data["q"].shape s50 = zslice(data["q"], data["p"], p0=-50) assert s50.shape == (I, J)
def test_corret_results_2D(): q, p, x, y = data() K, I, J = q.shape s50 = zslice(q.reshape(K, -1), p.reshape(K, -1), p0=-50) f50 = expected_results() np.testing.assert_almost_equal(s50, f50.ravel())
def test_1D_input(data): with pytest.raises(ValueError): zslice(data["q"].ravel(), data["p"].ravel(), p0=0)
def test_p0_outside_bounds(): with pytest.raises(ValueError): q, p, x, y = data() K, I, J = q.shape zslice(q, p, p0=50)
def test_corret_results_3D(data, expected_results): s50 = zslice(data["q"], data["p"], p0=-50) np.testing.assert_almost_equal(s50, expected_results)
# In[7]: # calculate the 3D z values using formula terms by specifying this derived vertical coordinate # with a terrible name z3d = var.coord('sea_surface_height_above_reference_ellipsoid').points # In[8]: # read the 3D chuck of data var3d = var.data # In[9]: # specify depth for fixed z slice z0 = -25 isoslice = zslice(var3d, z3d, z0) # In[10]: # For some reason I cannot tricontourf with NaNs. isoslice = ma.masked_invalid(isoslice) vmin, vmax = isoslice.min(), isoslice.max() isoslice = isoslice.filled(fill_value=-999) # In[11]: def make_map(projection=ccrs.PlateCarree()): fig, ax = plt.subplots(figsize=(9, 13), subplot_kw=dict(projection=projection)) gl = ax.gridlines(draw_labels=True)
def test_p0_outside_bounds(data): with pytest.raises(ValueError): K, I, J = data["q"].shape zslice(data["q"], data["p"], p0=50)
gl.xlabels_top = gl.ylabels_right = False gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER ax.coastlines('50m') return fig, ax cube = iris.load_cube(url, "sea_water_potential_temperature") cube = cube[-1, ...] # last time step lon = cube.coord(axis='X').points lat = cube.coord(axis='Y').points p = cube.coord("sea_surface_height_above_reference_ellipsoid").points p0 = -250 isoslice = zslice(cube.data, p, p0) fig, ax = make_map() ax.set_extent( [lon.min(), lon.max(), lat.min(), lat.max()] ) cs = ax.pcolormesh( lon, lat, ma.masked_invalid(isoslice), ) kw = {"shrink": 0.65, "orientation": "horizontal", "extend": "both"} cbar = fig.colorbar(cs, **kw)