def test_get_transect_uv(): mesh_path = os.path.join(my_data_folder, "pi-grid") mesh = load_mesh(mesh_path, abg=[50, 15, -90], usepickle=False, usejoblib=False) data_path = os.path.join(my_data_folder, "pi-results") u = get_data(data_path, "u", [1948, 1949], mesh, compute=False, depth=0) v = get_data(data_path, "v", [1948, 1949], mesh, compute=False, depth=0) u_int, v_int, lonreg2, latreg2 = get_vector_forplot(u, v, mesh) assert u_int.max() == pytest.approx(0.1808899530319614) assert u_int.min() == pytest.approx(-0.2817374262365221) assert u_int.mean() == pytest.approx(0.0033813037887997395) assert v_int.max() == pytest.approx(0.22638858037707627) assert v_int.min() == pytest.approx(-0.23170293870157044) assert v_int.mean() == pytest.approx(0.00045745072385952796) u = get_data(data_path, "u", [1948, 1949], mesh, compute=False, depth=1000) v = get_data(data_path, "v", [1948, 1949], mesh, compute=False, depth=1000) u_int, v_int, lonreg2, latreg2 = get_vector_forplot(u, v, mesh) assert u_int.max() == pytest.approx(0.11594848468259907) assert u_int.min() == pytest.approx(-0.06621269500377537) assert u_int.mean() == pytest.approx(0.0023697001377188153) assert v_int.max() == pytest.approx(0.07796926296747467) assert v_int.min() == pytest.approx(-0.08086355433299418) assert v_int.mean() == pytest.approx(-0.0004927562442740134)
def test_get_transect_uv(): mesh_path = os.path.join(my_data_folder, "pi-grid") mesh = load_mesh(mesh_path, abg=[50, 15, -90], usepickle=False, usejoblib=False) data_path = os.path.join(my_data_folder, "pi-results") u = get_data(data_path, "u", [1948, 1949], mesh) v = get_data(data_path, "v", [1948, 1949], mesh) u_nodes = tonodes3d(u, mesh) v_nodes = tonodes3d(v, mesh) lon_start = 120 lat_start = 75 lon_end = 120 lat_end = 80 lonlat = transect_get_lonlat(lon_start, lat_start, lon_end, lat_end, 30) dist, rot_u, rot_v = get_transect_uv(u_nodes, v_nodes, mesh, lonlat, myangle=0) assert dist.shape == (30.,) assert rot_u.shape == (30,47) assert rot_v.shape == (30,47) assert rot_u.max() == pytest.approx(0.012118130919170302) assert rot_u.min() == pytest.approx(-0.010595729181566588) assert rot_v.max() == pytest.approx(0.014261195850716241) assert rot_v.min() == pytest.approx(-0.0024032835331608696) dist, rot_u_90, rot_v_90 = get_transect_uv(u_nodes, v_nodes, mesh, lonlat, myangle=90) assert rot_u_90.max() == pytest.approx(0.014261195850716241) assert rot_u_90.min() == pytest.approx(-0.0024032835331608696) assert rot_v_90.max() == pytest.approx(0.012118130919170302) assert rot_v_90.min() == pytest.approx(-0.010595729181566588)
def test_get_plot_levels(): mesh_path = os.path.join(my_data_folder, "pi-grid") data_path = os.path.join(my_data_folder, "pi-results") mesh = load_mesh(mesh_path, abg=[50, 15, -90], usepickle=False, usejoblib=False) data = get_data(data_path, "temp", 1948, mesh, depth=0) data_levels = get_plot_levels([0, 1, 5], data, lev_to_data=False) assert data_levels[3] == 0.75 data_levels = get_plot_levels([0, 1, 5, 10], data, lev_to_data=False) assert data_levels[3] == 10 data_levels = get_plot_levels([-100, 100, 5], data, lev_to_data=True) assert data_levels[3] == pytest.approx(21.13924074) data_levels = get_plot_levels([-100, 100, 5], data, lev_to_data=False) assert data_levels[3] == 50
def test_plot(): mesh_path = os.path.join(my_data_folder, "pi-grid") data_path = os.path.join(my_data_folder, "pi-results") figure_path = os.path.join(my_data_folder, "baseline_images") mesh = load_mesh(mesh_path, abg=[50, 15, -90], usepickle=False, usejoblib=False) data = get_data(data_path, "temp", 1948, mesh, depth=0) # standard plot ax = plot(mesh, data, influence=800000) plt.savefig("./out.png") baseline_image = os.path.join(figure_path, "plot_temp_basic.png") compare_images("./out.png", baseline_image, tol=10) os.remove("./out.png") # inverce distance interpolation ax = plot(mesh, data, influence=800000, interp="idist") plt.savefig("./out.png") baseline_image = os.path.join(figure_path, "plot_temp_idist.png") compare_images("./out.png", baseline_image, tol=10) os.remove("./out.png") # pc projection ax = plot(mesh, data, influence=800000, mapproj="pc") plt.savefig("./out.png") baseline_image = os.path.join(figure_path, "plot_temp_pc.png") compare_images("./out.png", baseline_image, tol=10) os.remove("./out.png") # pc projection ax = plot(mesh, data, influence=800000, mapproj="pc") plt.savefig("./out.png") baseline_image = os.path.join(figure_path, "plot_temp_pc.png") compare_images("./out.png", baseline_image, tol=10) os.remove("./out.png") # np projection ax = plot(mesh, data, influence=800000, mapproj="np") plt.savefig("./out.png") baseline_image = os.path.join(figure_path, "plot_temp_np.png") compare_images("./out.png", baseline_image, tol=10) os.remove("./out.png")
def test_plot(): mesh_path = os.path.join(my_data_folder, 'pi-grid') data_path = os.path.join(my_data_folder, 'pi-results') figure_path = os.path.join(my_data_folder, 'baseline_images') mesh = load_mesh(mesh_path, abg=[50, 15, -90], usepickle=False, usejoblib=False) data = get_data(data_path, 'temp', 1948, mesh, depth=0) # standard plot ax = plot(mesh, data, influence=800000) plt.savefig('./out.png') baseline_image = os.path.join(figure_path, 'plot_temp_basic.png') compare_images('./out.png', baseline_image, tol=10) os.remove('./out.png') # inverce distance interpolation ax = plot(mesh, data, influence=800000, interp='idist') plt.savefig('./out.png') baseline_image = os.path.join(figure_path, 'plot_temp_idist.png') compare_images('./out.png', baseline_image, tol=10) os.remove('./out.png') # pc projection ax = plot(mesh, data, influence=800000, mapproj='pc') plt.savefig('./out.png') baseline_image = os.path.join(figure_path, 'plot_temp_pc.png') compare_images('./out.png', baseline_image, tol=10) os.remove('./out.png') # pc projection ax = plot(mesh, data, influence=800000, mapproj='pc') plt.savefig('./out.png') baseline_image = os.path.join(figure_path, 'plot_temp_pc.png') compare_images('./out.png', baseline_image, tol=10) os.remove('./out.png') # np projection ax = plot(mesh, data, influence=800000, mapproj='np') plt.savefig('./out.png') baseline_image = os.path.join(figure_path, 'plot_temp_np.png') compare_images('./out.png', baseline_image, tol=10) os.remove('./out.png')
def test_regriding(): mesh_path = os.path.join(my_data_folder, 'pi-grid') data_path = os.path.join(my_data_folder, 'pi-results') mesh = load_mesh(mesh_path, usepickle=False, usejoblib=False) lons = range(0, 360) lats = range(-90, 90) lons, lats = np.meshgrid(lons, lats) data = get_data(data_path, 'temp', 1948, mesh, depth=0) # default nn interpolation data_inter = fesom2regular(data, mesh, lons, lats) mmean = data_inter.mean() assert mmean == pytest.approx(6.309350409986763) assert isinstance(data_inter, np.ma.core.MaskedArray) # idist method data_inter = fesom2regular(data, mesh, lons, lats, how='idist') mmean = data_inter.mean() assert mmean == pytest.approx(6.308561066202526) assert isinstance(data_inter, np.ma.core.MaskedArray) # linear method from scipy data_inter = fesom2regular(data, mesh, lons, lats, how='linear') mmean = data_inter.mean() assert mmean == pytest.approx(13.582933890477655) assert isinstance(data_inter, np.ma.core.MaskedArray) # cubic method from scipy data_inter = fesom2regular(data, mesh, lons, lats, how='cubic') mmean = data_inter.mean() assert mmean == pytest.approx(13.39402615207708) assert isinstance(data_inter, np.ma.core.MaskedArray) # clean up os.remove(os.path.join(mesh_path, 'distances_3140_0_359_-90_-90_360_180_1')) os.remove(os.path.join(mesh_path, 'distances_3140_0_359_-90_-90_360_180_5')) os.remove(os.path.join(mesh_path, 'inds_3140_0_359_-90_-90_360_180_1')) os.remove(os.path.join(mesh_path, 'inds_3140_0_359_-90_-90_360_180_5')) os.remove(os.path.join(mesh_path, 'qhull_3140'))
def test_get_transect(): mesh_path = os.path.join(my_data_folder, "pi-grid") mesh = load_mesh(mesh_path, abg=[50, 15, -90], usepickle=False, usejoblib=False) data_path = os.path.join(my_data_folder, "pi-results") data = get_data(data_path, "temp", [1948, 1949], mesh) lon_start = -149 lat_start = 70.52 lon_end = -149 lat_end = 73 lonlat = transect_get_lonlat(lon_start, lat_start, lon_end, lat_end) dist, transect_data = get_transect(data, mesh, lonlat) assert dist.shape[0] == 30 assert dist.max() == pytest.approx(258.87327988009883) assert dist.min() == 0 assert transect_data.shape == (30, 47) assert transect_data.min() == pytest.approx(-1.446849) assert transect_data.max() == pytest.approx(0.38322645)
def test_xmoc_data(): mesh_path = os.path.join(my_data_folder, "pi-grid") data_path = os.path.join(my_data_folder, "pi-results") mesh = load_mesh(mesh_path, usepickle=False, usejoblib=False) # xarray as input data = get_data(data_path, "w", [1948], mesh, how="mean", compute=False) lats, moc = xmoc_data(mesh, data) assert moc.mean() == pytest.approx(-5.283107985611987) assert moc.max() == pytest.approx(32.49306582121843) assert moc.min() == pytest.approx(-79.29266240207812) # numpy as input data = get_data(data_path, "w", [1948], mesh, how="mean", compute=True) lats, moc = xmoc_data(mesh, data) assert moc.mean() == pytest.approx(-5.283107985611987) assert moc.max() == pytest.approx(32.49306582121843) assert moc.min() == pytest.approx(-79.29266240207812) # masked # xarray as input data = get_data(data_path, "w", [1948], mesh, how="mean", compute=False) lats, moc = xmoc_data(mesh, data, mask="Atlantic_MOC") assert moc.mean() == pytest.approx(-6.4900976496575975) assert moc.max() == pytest.approx(19.88617032403639) assert moc.min() == pytest.approx(-49.8639074605277) # numpy as input data = get_data(data_path, "w", [1948], mesh, how="mean", compute=True) lats, moc = xmoc_data(mesh, data, mask="Atlantic_MOC") assert moc.mean() == pytest.approx(-6.4900976496575975) assert moc.max() == pytest.approx(19.88617032403639) assert moc.min() == pytest.approx(-49.8639074605277) # different nlats # xarray as input data = get_data(data_path, "w", [1948], mesh, how="mean", compute=False) lats, moc = xmoc_data(mesh, data, nlats=30) assert moc.mean() == pytest.approx(-5.5225348525691) assert moc.max() == pytest.approx(32.37976644734857) assert moc.min() == pytest.approx(-80.2349660966104) # numpy as input data = get_data(data_path, "w", [1948], mesh, how="mean", compute=True) lats, moc = xmoc_data(mesh, data, nlats=30) assert moc.mean() == pytest.approx(-5.5225348525691) assert moc.max() == pytest.approx(32.37976644734857) assert moc.min() == pytest.approx(-80.2349660966104)
def test_ice_integrals(): mesh_path = os.path.join(my_data_folder, "pi-grid") data_path = os.path.join(my_data_folder, "pi-results") mesh = load_mesh(mesh_path, usepickle=False, usejoblib=False) # default get_data (with how='mean') should work. data = get_data(data_path, "a_ice", 1948, mesh, depth=0, compute=False) ext = ice_ext(data, mesh) assert ext.data[0] == pytest.approx(12710587600895.246) data = get_data(data_path, "a_ice", 1948, mesh, depth=0, compute=False) area = ice_area(data, mesh) assert area.data[0] == pytest.approx(9066097785122.738) data = get_data(data_path, "m_ice", 1948, mesh, depth=0, compute=False) vol = ice_vol(data, mesh) assert vol.data[0] == pytest.approx(13403821068217.506) # work with xarray as input data = get_data(data_path, "a_ice", 1948, mesh, depth=0, how="ori", compute=False) ext = ice_ext(data, mesh) area = ice_area(data, mesh) assert ext.data[0] == pytest.approx(12710587600895.246) assert area.data[0] == pytest.approx(9066097785122.738) data = get_data(data_path, "m_ice", 1948, mesh, depth=0, how="ori", compute=False) vol = ice_vol(data, mesh) assert vol.data[0] == pytest.approx(13403821068217.506) # work with numpy array as input data = get_data(data_path, "a_ice", 1948, mesh, depth=0, how="ori", compute=True) ext = ice_ext(data, mesh) # have to load data once again, since ice_ext actually modify numpy array. # I don't want to add .copy to the `ice_ext` function. data = get_data(data_path, "a_ice", 1948, mesh, depth=0, how="ori", compute=True) area = ice_area(data, mesh) assert ext.data[0] == pytest.approx(12710587600895.246) assert area.data[0] == pytest.approx(9066097785122.738) data = get_data(data_path, "m_ice", 1948, mesh, depth=0, how="ori", compute=True) vol = ice_vol(data, mesh) assert vol.data[0] == pytest.approx(13403821068217.506)
def test_volmean_data(): mesh_path = os.path.join(my_data_folder, "pi-grid") data_path = os.path.join(my_data_folder, "pi-results") mesh = load_mesh(mesh_path, usepickle=False, usejoblib=False) # xarray as input # time series data = get_data(data_path, "temp", [1948, 1949], mesh, how="ori", compute=False) result = volmean_data(data, mesh) assert np.nanmean(result) == pytest.approx(3.3736459180632776) # one point data = get_data(data_path, "temp", [1948], mesh, how="ori", compute=False) result = volmean_data(data, mesh) assert np.nanmean(result) == pytest.approx(3.4039440198518953) data = get_data(data_path, "temp", [1948, 1949], mesh, how="mean", compute=False) result = volmean_data(data, mesh) assert np.nanmean(result) == pytest.approx(3.3736459180632776) # numpy array as input # time series data = get_data(data_path, "temp", [1948, 1949], mesh, how="ori", compute=True) result = volmean_data(data, mesh) assert np.nanmean(result) == pytest.approx(3.3736459180632776) # one point data = get_data(data_path, "temp", [1948], mesh, how="ori", compute=True) result = volmean_data(data, mesh) assert np.nanmean(result) == pytest.approx(3.4039440198518953) data = get_data(data_path, "temp", [1948, 1949], mesh, how="mean", compute=True) result = volmean_data(data, mesh) assert np.nanmean(result) == pytest.approx(3.3736459180632776) # different depth ranges data = get_data(data_path, "temp", [1948], mesh, how="ori", compute=False) result = volmean_data(data, mesh, [0, 100]) assert np.nanmean(result) == pytest.approx(16.26645462001221) data = get_data(data_path, "temp", [1948], mesh, how="ori", compute=False) result = volmean_data(data, mesh, [0, 'bottom']) assert np.nanmean(result) == pytest.approx(3.4039440198518953) data = get_data(data_path, "temp", [1948], mesh, how="ori", compute=False) result = volmean_data(data, mesh, [500, 500]) assert np.nanmean(result) == pytest.approx(6.339069486142839)
def test_hovm_data(): mesh_path = os.path.join(my_data_folder, "pi-grid") data_path = os.path.join(my_data_folder, "pi-results") mesh = load_mesh(mesh_path, usepickle=False, usejoblib=False) # work on xarray # mean first, the hovm data = get_data(data_path, "temp", [1948, 1949], mesh, how="mean", compute=False) hovm = hovm_data(data, mesh) assert hovm.shape == (1, 47) assert np.nanmean(hovm) == pytest.approx(7.446110751429013) # hovm first, then mean data = get_data(data_path, "temp", [1948, 1949], mesh, how="ori", compute=False) hovm = hovm_data(data, mesh) assert hovm.shape == (2, 47) assert np.nanmean(hovm) == pytest.approx(7.446110751429013) # work on numpy array # mean first, the hovm data = get_data(data_path, "temp", [1948, 1949], mesh, how="mean", compute=True) hovm = hovm_data(data, mesh) assert hovm.shape == (1, 47) assert np.nanmean(hovm) == pytest.approx(7.446110751429013) # hovm first, then mean data = get_data(data_path, "temp", [1948, 1949], mesh, how="ori", compute=True) hovm = hovm_data(data, mesh) assert hovm.shape == (2, 47) assert np.nanmean(hovm) == pytest.approx(7.446110751429013) # test when only 1 time step of 3d field is in input data = get_data(data_path, "temp", [1948], mesh, how="mean", compute=False) hovm = hovm_data(data, mesh) assert hovm.shape == (1, 47) assert np.nanmean(hovm) == pytest.approx(7.440160989229884) data = get_data(data_path, "temp", [1948], mesh, how="mean", compute=True) hovm = hovm_data(data, mesh) assert hovm.shape == (1, 47) assert np.nanmean(hovm) == pytest.approx(7.440160989229884) # if we try to supplu 2d variable with pytest.raises(ValueError): data = get_data(data_path, "a_ice", [1948, 1949], mesh, how="mean", compute=True) hovm = hovm_data(data, mesh)
def test_get_data(): mesh_path = os.path.join(my_data_folder, 'pi-grid') data_path = os.path.join(my_data_folder, 'pi-results') mesh = load_mesh(mesh_path, usepickle=False, usejoblib=False) # variable on vertices temp = get_data(data_path, 'temp', 1948, mesh, depth=0) assert type(temp) == np.ndarray mmin = temp.min() assert mmin == pytest.approx(-1.8924446) mmax = temp.max() assert mmax == pytest.approx(28.816469) # variable on elements u = get_data(data_path, 'u', 1948, mesh, depth=0) assert type(u) == np.ndarray mmin = u.min() assert mmin == pytest.approx(-0.51486444) mmax = u.max() assert mmax == pytest.approx(0.27181712) # 2d variable on vertices ice = get_data(data_path, 'a_ice', 1948, mesh, depth=0) assert type(u) == np.ndarray mmean = ice.mean() assert mmean == pytest.approx(0.27451384) # get multiple years temp = get_data(data_path, 'temp', [1948, 1949], mesh, depth=0) mmean = temp.mean() assert mmean == pytest.approx(8.5541878) # get one record from multiple files temp = get_data(data_path, 'temp', [1948, 1949], mesh, records=slice(0, 1), depth=0) mmean = temp.mean() assert mmean == pytest.approx(8.4580183) # get different depth temp = get_data(data_path, 'temp', [1948, 1949], mesh, depth=200) mmean = temp.mean() assert mmean == pytest.approx(5.3856239) # get different depth and different aggregation temp = get_data(data_path, 'temp', [1948, 1949], mesh, depth=200, how='max') mmean = temp.mean() assert mmean == pytest.approx(5.6487503) # directly open ncfile (in data 1948, but we directly request 1949) temp = get_data(data_path, 'temp', [1948], mesh, depth=200, how='max', ncfile='{}/{}'.format(data_path, "temp.fesom.1949.nc")) mmean = temp.mean() assert mmean == pytest.approx(5.3057818) # return dask array temp = get_data(data_path, 'temp', [1948, 1949], mesh, depth=200, how='max', compute=False) assert isinstance(temp, xr.DataArray) # use range as argument temp = get_data(data_path, 'temp', range(1948, 1950), mesh, depth=200, how='max') mmean = temp.mean() assert mmean == pytest.approx(5.6487503)