diff --git a/Snakefile b/Snakefile index fb04256b..b8bc284a 100644 --- a/Snakefile +++ b/Snakefile @@ -166,6 +166,8 @@ if config['enable'].get('retrieve_natura_raster', True): script: 'scripts/retrieve_natura_raster.py' +ruleorder: build_hydro_profile > build_renewable_profiles + rule build_renewable_profiles: input: base_network="networks/base.nc", diff --git a/config.default.yaml b/config.default.yaml index 0ba49caf..9f096fff 100755 --- a/config.default.yaml +++ b/config.default.yaml @@ -169,6 +169,7 @@ transformers: load: scaling_factor: 1.0 + fallback_year: 2013 costs: year: 2030 diff --git a/config.tutorial.yaml b/config.tutorial.yaml index 52b5b866..e74a2967 100755 --- a/config.tutorial.yaml +++ b/config.tutorial.yaml @@ -147,6 +147,7 @@ transformers: load: scaling_factor: 1.0 + fallback_year: 2013 costs: year: 2030 diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 8ac455f7..98893010 100755 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -212,11 +212,28 @@ def attach_load(n): substation_lv_i = n.buses.index[n.buses['substation_lv']] regions = (gpd.read_file(snakemake.input.regions).set_index('name') .reindex(substation_lv_i)) - # TODO: is there a fallback if year not in timeseries_opsd? - opsd_load = (timeseries_opsd(slice(*n.snapshots[[0,-1]].year.astype(str)), + + available_years = range(2011,2016) + requested_years = n.snapshots.year[[0,-1]] + use_fallback = any(year not in available_years for year in requested_years) + + if use_fallback: + fallback_year = str(snakemake.config["load"]["fallback_year"]) + load_years = [fallback_year, fallback_year] + logger.warning(f"Requested years {list(requested_years.unique().values)} " + f"for load time series not in available years {list(available_years)}. " + f"Falling back to year {fallback_year}.") + else: + load_years = requested_years.astype(str) + + opsd_load = (timeseries_opsd(slice(*load_years), snakemake.input.opsd_load) * snakemake.config.get('load', {}).get('scaling_factor', 1.0)) + if use_fallback: + assert len(requested_years.unique()) == 1, "Fallback for load time series requires single year!" + opsd_load.index = opsd_load.index.map(lambda t: t.replace(year=requested_years[0])) + # Convert to naive UTC (has to be explicit since pandas 0.24) opsd_load.index = opsd_load.index.tz_localize(None) diff --git a/scripts/build_hydro_profile.py b/scripts/build_hydro_profile.py index 339fccaf..46911363 100644 --- a/scripts/build_hydro_profile.py +++ b/scripts/build_hydro_profile.py @@ -75,15 +75,23 @@ if __name__ == "__main__": snakemake = mock_snakemake('build_hydro_profile') configure_logging(snakemake) + year = snakemake.wildcards.year config = snakemake.config['renewable']['hydro'] cutout_dir = os.path.dirname(snakemake.input.cutout) - cutout = atlite.Cutout(config['cutout'], cutout_dir=cutout_dir) + cutout_config = config['cutout'] + if year: cutout_config = cutout_config.format(year=year) + + cutout = atlite.Cutout(cutout_config, cutout_dir=cutout_dir) countries = snakemake.config['countries'] country_shapes = gpd.read_file(snakemake.input.country_shapes).set_index('name')['geometry'].reindex(countries) country_shapes.index.name = 'countries' eia_stats = vhydro.get_eia_annual_hydro_generation(snakemake.input.eia_hydro_generation).reindex(columns=countries) + + if year not in eia_stats.index: + eia_stats.loc[year] = eia_stats.mean() + inflow = cutout.runoff(shapes=country_shapes, smooth=True, lower_threshold_quantile=True, diff --git a/scripts/build_renewable_profiles.py b/scripts/build_renewable_profiles.py index a02d397b..7b838725 100644 --- a/scripts/build_renewable_profiles.py +++ b/scripts/build_renewable_profiles.py @@ -282,14 +282,16 @@ if __name__ == '__main__': pgb.streams.wrap_stderr() - config = snakemake.config['renewable'][snakemake.wildcards.technology] - year = snakemake.wildcards.year + config = snakemake.config['renewable'][snakemake.wildcards.technology] + cutout_name = config['cutout'] + if year: cutout_name = cutout_name.format(year=year) + snapshots = dict(start=year, end=str(int(year)+1), closed="left") if year else snakememake.config['snapshots'] time = pd.date_range(freq='m', **snapshots) params = dict(years=slice(*time.year[[0, -1]]), months=slice(*time.month[[0, -1]])) - cutout = atlite.Cutout(config['cutout'], + cutout = atlite.Cutout(cutout_name, cutout_dir=os.path.dirname(snakemake.input.cutout), **params) diff --git a/test/config.test1.yaml b/test/config.test1.yaml index a1fc59ba..2774be8a 100755 --- a/test/config.test1.yaml +++ b/test/config.test1.yaml @@ -147,6 +147,7 @@ transformers: load: scaling_factor: 1.0 + fallback_year: 2013 costs: year: 2030