def test_round_trip(any_ds: xr.Dataset, tmp_path): path = tmp_path / "if" pm2io.write_interchange_format(path, any_ds.pr.to_interchange_format()) with path.with_suffix(".yaml").open() as fd: print(fd.read()) actual = pm2io.from_interchange_format(pm2io.read_interchange_format(path)) utils.assert_ds_aligned_equal(any_ds, actual)
def test_read_published_data(self): actual = pm2io.from_interchange_format( pm2io.read_interchange_format( DATA_PATH / "Guetschow-et-al-2021-PRIMAP-crf96_2021-v1" ) ) expected = primap2.open_dataset( DATA_PATH / "Guetschow-et-al-2021-PRIMAP-crf96_2021-v1.nc" ) assert_ds_aligned_equal(actual, expected, equal_nan=True)
def test_missing_file(minimal_ds, tmp_path): path = tmp_path / "if" pm2io.write_interchange_format(path, minimal_ds.pr.to_interchange_format()) with path.with_suffix(".yaml").open() as fd: content = fd.readlines() with path.with_suffix(".yaml").open("w") as fd: for line in content: if "data_file" in line: continue fd.write(line) # first test automatic discovery actual = pm2io.from_interchange_format(pm2io.read_interchange_format(path)) utils.assert_ds_aligned_equal(minimal_ds, actual) # now test without csv file path.with_suffix(".csv").unlink() with pytest.raises(FileNotFoundError, match="Data file not found at"): pm2io.read_interchange_format(path)
def test_inharmonic_units(minimal_ds, tmp_path): path = tmp_path / "if" pm2io.write_interchange_format(path, minimal_ds.pr.to_interchange_format()) df = pd.read_csv(path.with_suffix(".csv")) df.loc[3, "unit"] = "m" df.to_csv(path.with_suffix(".csv"), index=False, quoting=csv.QUOTE_NONNUMERIC) with pytest.raises(ValueError, match="More than one unit"): pm2io.from_interchange_format(pm2io.read_interchange_format(path))
def test_roundtrip(self, tmp_path): file_input = DATA_PATH / "test_read_wide_csv_file_output.csv" file_temp = tmp_path / "test_interchange_format" data = pd.read_csv(file_input, index_col=0, dtype=object) attrs = { "attrs": { "area": "area (ISO3)", "cat": "category (IPCC2006)", "scen": "scenario (general)", "sec_cats": ["Class (class)", "Type (type)"], }, "time_format": "%Y", "dimensions": {"CO2": ["area (ISO3)"]}, } pm2io.write_interchange_format(file_temp, data, attrs) read_data = pm2io.read_interchange_format(file_temp) read_attrs = read_data.attrs assert read_attrs == attrs pd.testing.assert_frame_equal(data, read_data)