def test_squashed_file(self, tmp_path_factory, bout_xyt_example_files): path = bout_xyt_example_files(tmp_path_factory, nxpe=4, nype=3, nt=1, squashed=True, write_to_disk=True) with pytest.warns(UserWarning): actual = open_boutdataset(datapath=path, keep_xboundaries=False) expected = create_bout_ds(lengths=(6, 8, 12, 7)) expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(t_array="t") xrt.assert_equal( actual.drop_vars(["x", "y", "z"]).load(), expected.drop_vars( METADATA_VARS + _BOUT_PER_PROC_VARIABLES + _BOUT_TIME_DEPENDENT_META_VARS, errors="ignore", ), ) # check creation without writing to disk gives identical result fake_ds_list = bout_xyt_example_files(None, nxpe=4, nype=3, nt=1, squashed=True) with pytest.warns(UserWarning): fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False) xrt.assert_identical(actual, fake)
def test_salpha(self, tmp_path_factory, bout_xyt_example_files): path = bout_xyt_example_files( tmp_path_factory, nxpe=3, nype=3, nt=1, syn_data_type="stepped", grid="grid", write_to_disk=True, ) actual = open_boutdataset( datapath=path, geometry="s-alpha", gridfilepath=path.parent.joinpath("grid.nc"), ) # check dataset can be saved save_dir = tmp_path_factory.mktemp("data") actual.bout.save(save_dir.joinpath("boutdata.nc")) # check creation without writing to disk gives identical result fake_ds_list, fake_grid_ds = bout_xyt_example_files( None, nxpe=3, nype=3, nt=1, syn_data_type="stepped", grid="grid") fake = open_boutdataset(datapath=fake_ds_list, geometry="s-alpha", gridfilepath=fake_grid_ds) xrt.assert_identical(actual, fake)
def test_combine_along_y(self, tmp_path_factory, bout_xyt_example_files): path = bout_xyt_example_files( tmp_path_factory, nxpe=1, nype=3, nt=1, syn_data_type="stepped", write_to_disk=True, ) with pytest.warns(UserWarning): actual = open_boutdataset(datapath=path, keep_xboundaries=False) bout_ds = create_bout_ds expected = concat( [bout_ds(0), bout_ds(1), bout_ds(2)], dim="y", data_vars="minimal") expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(t_array="t") xrt.assert_equal( actual.drop_vars(["x", "y", "z"]).load(), expected.drop_vars(METADATA_VARS + _BOUT_PER_PROC_VARIABLES, errors="ignore"), ) # check creation without writing to disk gives identical result fake_ds_list = bout_xyt_example_files(None, nxpe=1, nype=3, nt=1, syn_data_type="stepped") with pytest.warns(UserWarning): fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False) xrt.assert_identical(actual, fake)
def test_combine_along_xy(self, tmpdir_factory, bout_xyt_example_files): path = bout_xyt_example_files(tmpdir_factory, nxpe=4, nype=3, nt=1, syn_data_type='stepped') actual = open_boutdataset(datapath=path, keep_xboundaries=False) bout_ds = create_bout_ds line1 = concat( [bout_ds(0), bout_ds(1), bout_ds(2), bout_ds(3)], dim='x', data_vars='minimal') line2 = concat( [bout_ds(4), bout_ds(5), bout_ds(6), bout_ds(7)], dim='x', data_vars='minimal') line3 = concat( [bout_ds(8), bout_ds(9), bout_ds(10), bout_ds(11)], dim='x', data_vars='minimal') expected = concat([line1, line2, line3], dim='y', data_vars='minimal') xrt.assert_equal( actual.load(), expected.drop(METADATA_VARS + _BOUT_PER_PROC_VARIABLES, errors='ignore'))
def test_single_file(self, tmpdir_factory, bout_xyt_example_files): path = bout_xyt_example_files(tmpdir_factory, nxpe=1, nype=1, nt=1) actual = open_boutdataset(datapath=path, keep_xboundaries=False) expected = create_bout_ds() xrt.assert_equal(actual.load(), expected.drop(METADATA_VARS + _BOUT_PER_PROC_VARIABLES, errors='ignore'))
def test_open_grid(self, create_example_grid_file): example_grid = create_example_grid_file with pytest.warns(UserWarning): result = open_boutdataset(datapath=example_grid) result = result.drop_vars(["x", "y"]) assert_equal(result, open_dataset(example_grid)) result.close()
def test_squashed_doublenull_file( self, tmp_path_factory, bout_xyt_example_files, keep_xboundaries, keep_yboundaries, ): path = bout_xyt_example_files( tmp_path_factory, nxpe=4, nype=6, nt=1, lengths=(6, 4, 4, 7), guards={ "x": 2, "y": 2 }, squashed=True, write_to_disk=True, topology="upper-disconnected-double-null", ) with pytest.warns(UserWarning): ds = open_boutdataset( datapath=path, keep_xboundaries=keep_xboundaries, keep_yboundaries=keep_yboundaries, ) # bout_xyt_example_files when creating a 'squashed' file just makes it with # y-size nype*lengths[2]+2*myg, which is 6*4+4=28, so with upper and lower # boundaries removed, y-size should be 28-4*2=20. assert ds.sizes["t"] == 6 assert ds.sizes["x"] == 20 if keep_xboundaries else 16 assert ds.sizes["y"] == 32 if keep_yboundaries else 24 assert ds.sizes["z"] == 7
def test_open_grid_chunks_not_in_grid(self, create_example_grid_file): example_grid = create_example_grid_file with pytest.warns(UserWarning): result = open_boutdataset(datapath=example_grid, chunks={"anonexistantdimension": 5}) result = result.drop_vars(["x", "y"]) assert_equal(result, open_dataset(example_grid)) result.close()
def test_salpha(self, tmpdir_factory, bout_xyt_example_files): path = bout_xyt_example_files(tmpdir_factory, nxpe=3, nype=3, nt=1, syn_data_type='stepped', grid='grid') actual = open_boutdataset(datapath=path, geometry='s-alpha', gridfilepath=Path(path).parent.joinpath('grid.nc')) # check dataset can be saved save_dir = tmpdir_factory.mktemp('data') actual.bout.save(str(save_dir.join('boutdata.nc')))
def test_drop_vars(self, tmpdir_factory, bout_xyt_example_files): path = bout_xyt_example_files(tmpdir_factory, nxpe=4, nype=1, nt=1, syn_data_type='stepped') ds = open_boutdataset(datapath=path, keep_xboundaries=False, drop_variables=['T']) assert 'T' not in ds.keys() assert 'n' in ds.keys()
def test_open_grid_extra_dims(self, create_example_grid_file, tmpdir_factory): example_grid = open_dataset(create_example_grid_file) new_var = DataArray(name='new', data=[[1, 2], [8, 9]], dims=['x', 'w']) dodgy_grid_directory = tmpdir_factory.mktemp("dodgy_grid") dodgy_grid_path = dodgy_grid_directory.join('dodgy_grid.nc') merge([example_grid, new_var]).to_netcdf(dodgy_grid_path, engine='netcdf4') with pytest.warns(UserWarning, match="drop all variables containing " "the dimensions 'w'"): result = open_boutdataset(datapath=dodgy_grid_path) assert_equal(result, example_grid) result.close()
def test_open_grid_apply_geometry(self, create_example_grid_file): @register_geometry(name="Schwarzschild") def add_schwarzschild_coords(ds, coordinates=None): ds['event_horizon'] = 4.0 return ds example_grid = create_example_grid_file result = result = open_boutdataset(datapath=example_grid, geometry="Schwarzschild") assert_equal(result['event_horizon'], DataArray(4.0)) # clean up del REGISTERED_GEOMETRIES["Schwarzschild"] result.close()
def test_drop_vars(self, tmp_path_factory, bout_xyt_example_files): datapath = bout_xyt_example_files( tmp_path_factory, nxpe=4, nype=1, nt=1, syn_data_type="stepped", write_to_disk=True, ) with pytest.warns(UserWarning): ds = open_boutdataset(datapath=datapath, keep_xboundaries=False, drop_variables=["T"]) assert "T" not in ds.keys() assert "n" in ds.keys()
def get_outputs(self, run_info): """Read the BOUT++ outputs into an xarray dataframe""" data_files = self._get_output_path(run_info, self.target_filename) return open_boutdataset(data_files, info=False)
def test_open_grid_chunks(self, create_example_grid_file): example_grid = create_example_grid_file result = open_boutdataset(datapath=example_grid, chunks={'x': 4, 'y': 5}) assert_equal(result, open_dataset(example_grid)) result.close()
def test_open_grid_chunks_not_in_grid(self, create_example_grid_file): example_grid = create_example_grid_file result = open_boutdataset(datapath=example_grid, chunks={'anonexistantdimension': 5}) assert_equal(result, open_dataset(example_grid)) result.close()
def test_combine_along_xy(self, tmp_path_factory, bout_xyt_example_files, bout_v5, metric_3D, lengths): path = bout_xyt_example_files( tmp_path_factory, nxpe=4, nype=3, nt=1, lengths=lengths, syn_data_type="stepped", write_to_disk=True, bout_v5=bout_v5, metric_3D=metric_3D, ) with pytest.warns(UserWarning): actual = open_boutdataset(datapath=path, keep_xboundaries=False) def bout_ds(syn_data_type): return create_bout_ds(syn_data_type, bout_v5=bout_v5, metric_3D=metric_3D, lengths=lengths) line1 = concat( [bout_ds(0), bout_ds(1), bout_ds(2), bout_ds(3)], dim="x", data_vars="minimal", ) line2 = concat( [bout_ds(4), bout_ds(5), bout_ds(6), bout_ds(7)], dim="x", data_vars="minimal", ) line3 = concat( [bout_ds(8), bout_ds(9), bout_ds(10), bout_ds(11)], dim="x", data_vars="minimal", ) expected = concat([line1, line2, line3], dim="y", data_vars="minimal") expected = expected.set_coords(["t_array", "dx", "dy", "dz"]).rename(t_array="t") vars_to_drop = METADATA_VARS + _BOUT_PER_PROC_VARIABLES xrt.assert_equal( actual.drop_vars(["x", "y", "z"]).load(), expected.drop_vars(vars_to_drop, errors="ignore"), ) # check creation without writing to disk gives identical result fake_ds_list = bout_xyt_example_files( None, nxpe=4, nype=3, nt=1, lengths=lengths, syn_data_type="stepped", bout_v5=bout_v5, metric_3D=metric_3D, ) with pytest.warns(UserWarning): fake = open_boutdataset(datapath=fake_ds_list, keep_xboundaries=False) xrt.assert_identical(actual, fake)
def test_open_grid(self, create_example_grid_file): example_grid = create_example_grid_file result = open_boutdataset(datapath=example_grid) assert_equal(result, open_dataset(example_grid)) result.close()