def clone( self, model: str = None, scenario: str = None, annotation: str = None, keep_solution: bool = True, shift_first_model_year: int = None, platform: Platform = None, ) -> "Scenario": """Clone the current scenario and return the clone. If the (`model`, `scenario`) given already exist on the :class:`.Platform`, the `version` for the cloned Scenario follows the last existing version. Otherwise, the `version` for the cloned Scenario is 1. .. note:: :meth:`clone` does not set or alter default versions. This means that a clone to new (`model`, `scenario`) names has no default version, and will not be returned by :meth:`Platform.scenario_list` unless `default=False` is given. Parameters ---------- model : str, optional New model name. If not given, use the existing model name. scenario : str, optional New scenario name. If not given, use the existing scenario name. annotation : str, optional Explanatory comment for the clone commit message to the database. keep_solution : bool, optional If :py:const:`True`, include all timeseries data and the solution (vars and equs) from the source scenario in the clone. If :py:const:`False`, only include timeseries data marked `meta=True` (see :meth:`.add_timeseries`). shift_first_model_year: int, optional If given, all timeseries data in the Scenario is omitted from the clone for years from `first_model_year` onwards. Timeseries data with the `meta` flag (see :meth:`.add_timeseries`) are cloned for all years. platform : :class:`Platform`, optional Platform to clone to (default: current platform) """ if shift_first_model_year is not None: if keep_solution: log.warning( "Override keep_solution=True for shift_first_model_year") keep_solution = False platform = platform or self.platform model = model or self.model scenario = scenario or self.scenario args = [platform, model, scenario, annotation, keep_solution] if check_year(shift_first_model_year, "first_model_year"): args.append(shift_first_model_year) return self._backend("clone", *args)
def test_check_year(): # If y is a string value, raise a Value Error. y1 = "a" s1 = "a" with pytest.raises(ValueError): assert utils.check_year(y1, s1) # If y = None. y2 = None s2 = None assert utils.check_year(y2, s2) is None # If y is integer. y3 = 4 s3 = 4 assert utils.check_year(y3, s3) is True
def remove_solution(self, first_model_year: int = None) -> None: """Remove the solution from the scenario. This function removes the solution (variables and equations) and timeseries data marked as `meta=False` from the scenario (see :meth:`.add_timeseries`). Parameters ---------- first_model_year: int, optional If given, timeseries data marked as `meta=False` is removed only for years from `first_model_year` onwards. Raises ------ ValueError If Scenario has no solution or if `first_model_year` is not `int`. """ if self.has_solution(): check_year(first_model_year, "first_model_year") self._backend("clear_solution", first_model_year) else: raise ValueError("This Scenario does not have a solution!")
def clone(self, model=None, scenario=None, annotation=None, keep_solution=True, shift_first_model_year=None, platform=None): """Clone the current scenario and return the clone. If the (`model`, `scenario`) given already exist on the :class:`Platform`, the `version` for the cloned Scenario follows the last existing version. Otherwise, the `version` for the cloned Scenario is 1. .. note:: :meth:`clone` does not set or alter default versions. This means that a clone to new (`model`, `scenario`) names has no default version, and will not be returned by :meth:`Platform.scenario_list` unless `default=False` is given. Parameters ---------- model : str, optional New model name. If not given, use the existing model name. scenario : str, optional New scenario name. If not given, use the existing scenario name. annotation : str, optional Explanatory comment for the clone commit message to the database. keep_solution : bool, default True If :py:const:`True`, include all timeseries data and the solution (vars and equs) from the source scenario in the clone. Otherwise, only timeseries data marked as `meta=True` (see :meth:`TimeSeries.add_timeseries`) or prior to `first_model_year` (see :meth:`TimeSeries.add_timeseries`) are cloned. shift_first_model_year: int, optional If given, the values of the solution are transfered to parameters `historical_*`, parameter `resource_volume` is updated, and the `first_model_year` is shifted. The solution is then discarded, see :meth:`TimeSeries.remove_solution`. platform : :class:`Platform`, optional Platform to clone to (default: current platform) """ err = 'Cloning across platforms is only possible {}' if platform is not None and not keep_solution: raise ValueError(err.format('with `keep_solution=True`!')) if platform is not None and shift_first_model_year is not None: raise ValueError(err.format('without shifting model horizon!')) if shift_first_model_year is not None: keep_solution = False msg = 'Shifting first model year to {} and removing solution' logger().info(msg.format(shift_first_model_year)) platform = platform or self.platform model = model or self.model scenario = scenario or self.scenario args = [platform._jobj, model, scenario, annotation, keep_solution] if check_year(shift_first_model_year, 'shift_first_model_year'): args.append(shift_first_model_year) return Scenario(platform, model, scenario, cache=self._cache, version=self._jobj.clone(*args))