def test_run_to_nc_extra_instead_of_dimension_run_id(scm_run): with tempfile.TemporaryDirectory() as tempdir: out_fname = join(tempdir, "out.nc") scm_run["run_id"] = [1, 2, 1] run_to_nc(scm_run, out_fname, dimensions=("scenario", ), extras=("run_id", )) loaded = ScmRun.from_nc(out_fname) assert_scmdf_almost_equal(scm_run, loaded, check_ts_names=False)
def load(self, key): """ Parameters ---------- key: str Returns ------- :class:`scmdata.ScmRun` """ return ScmRun.from_nc(key)
def test_run_to_nc_loop_tricky_variable_name(scm_run, start_variable): # tests that the mapping between variable and units works even with # tricky variable names that get renamed in various was before serialising to # disk assert "Primary Energy|Coal" in scm_run.get_unique_meta("variable") scm_run["variable"] = scm_run["variable"].apply( lambda x: x.replace("Primary Energy|Coal", start_variable)) scm_run["unit"] = scm_run["variable"].apply( lambda x: "EJ/yr" if x != start_variable else "MJ / yr") with tempfile.TemporaryDirectory() as tempdir: out_fname = join(tempdir, "out.nc") scm_run.to_nc(out_fname, dimensions=("scenario", )) loaded = ScmRun.from_nc(out_fname) assert_scmdf_almost_equal(scm_run, loaded, check_ts_names=False)
def test_run_to_nc_dimensions_cover_all_metadata(): start = ScmRun( np.arange(6).reshape(3, 2), index=[2010, 2020, 2030], columns={ "variable": "Surface Temperature", "unit": "K", "model": ["model_a", "model_b"], "scenario": "scen_a", "region": "World", }, ) with tempfile.TemporaryDirectory() as tempdir: out_fname = join(tempdir, "out.nc") start.to_nc(out_fname, dimensions=("region", "model", "scenario")) loaded = ScmRun.from_nc(out_fname) assert_scmdf_almost_equal(start, loaded, check_ts_names=False)
def save(self, sr): """ Save a ScmRun to the database The dataset should not contain any duplicate metadata for the database levels Parameters ---------- sr : :class:`scmdata.ScmRun` Data to save Raises ------ ValueError If duplicate metadata are present for the requested database levels KeyError If metadata for the requested database levels are not found Returns ------- str Key where the data is saved """ key = self.get_key(sr) ensure_dir_exists(key) if os.path.exists(key): existing_run = ScmRun.from_nc(key) sr = run_append([existing_run, sr]) # Check for required extra dimensions dimensions = self.kwargs.get("dimensions", None) if not dimensions: nunique_meta_vals = sr.meta.nunique() dimensions = nunique_meta_vals[ nunique_meta_vals > 1].index.tolist() sr.to_nc(key, dimensions=dimensions) return key