def time_diff(path_to_low_res, path_to_high_res, path_to_output): lowres = calliope.read_netcdf(path_to_low_res) highres = calliope.read_netcdf(path_to_high_res) energy_cap_lowres = ( lowres.get_formatted_array("energy_cap").sum("locs").sel(techs=[ tech.item() for tech in lowres.get_formatted_array("energy_cap").techs if "ac_transmission" not in str(tech) ]).to_series()) energy_cap_highres = ( highres.get_formatted_array("energy_cap").sum("locs").sel(techs=[ tech.item() for tech in highres.get_formatted_array("energy_cap").techs if "ac_transmission" not in str(tech) ]).to_series()) df = pd.DataFrame(data={ "high_res": energy_cap_highres, "low_res": energy_cap_lowres }).T df["number_timesteps"] = [ model.results.timesteps.shape[0] for model in (highres, lowres) ] df["cost"] = [ model.get_formatted_array("cost").sum().item() for model in (highres, lowres) ] df.T.to_csv(path_to_output, index=True, header=True)
def _run_setup_model(model_file, scenario, model_format, override_dict): """ Build model in CLI commands. Returns ``model``, a ready-to-run calliope.Model instance. """ # Try to determine model file type if not given explicitly if model_format is None: if model_file.split(".")[-1] in ["yaml", "yml"]: model_format = "yaml" elif model_file.split(".")[-1] in ["nc", "nc4", "netcdf"]: model_format = "netcdf" else: raise ValueError( "Cannot determine model file format based on file " 'extension for "{}". Set format explicitly with ' "--model_format.".format(model_file)) if model_format == "yaml": model = Model(model_file, scenario=scenario, override_dict=override_dict) elif model_format == "netcdf": if scenario is not None or override_dict is not None: raise ValueError( "When loading a pre-built model from NetCDF, the " "--scenario and --override_dict options are not available.") model = read_netcdf(model_file) else: raise ValueError("Invalid model format: {}".format(model_format)) return model
def _run_setup_model( model_file, scenario, model_format, override_dict): """ Build model in CLI commands. Returns ``model``, a ready-to-run calliope.Model instance. """ # Try to determine model file type if not given explicitly if model_format is None: if model_file.split('.')[-1] in ['yaml', 'yml']: model_format = 'yaml' elif model_file.split('.')[-1] in ['nc', 'nc4', 'netcdf']: model_format = 'netcdf' else: raise ValueError( 'Cannot determine model file format based on file ' 'extension for "{}". Set format explicitly with ' '--model_format.'.format(model_file) ) if model_format == 'yaml': model = Model( model_file, scenario=scenario, override_dict=override_dict ) elif model_format == 'netcdf': if scenario is not None or override_dict is not None: raise ValueError( 'When loading a pre-built model from NetCDF, the ' '--scenario and --override_dict options are not available.' ) model = read_netcdf(model_file) else: raise ValueError('Invalid model format: {}'.format(model_format)) return model
def test_solve_save_read_netcdf(self, model): with tempfile.TemporaryDirectory() as tempdir: out_path = os.path.join(tempdir, "model.nc") model.to_netcdf(out_path) assert os.path.isfile(out_path) model_from_disk = calliope.read_netcdf(out_path)
def excavate_installed_capacities(path_to_results, scaling_factor, path_to_capacities_raw, path_to_capacities_publish): """Excavate installed capacities from the model results.""" model = calliope.read_netcdf(path_to_results) capacities = _read_capacities(model) capacities.loc[:, "locs"] = capacities.loc[:, "locs"].str[:3] capacities.loc[:, "techs"] = capacities.loc[:, "techs"].map( lambda tech: tech if tech[-4] != "_" else tech[:-4]) capacities.dropna(subset=["energy_cap"], inplace=True) capacities = capacities.groupby(["locs", "techs"]).sum().reset_index() capacities = (capacities.pivot(index="locs", columns="techs", values="energy_cap").div( scaling_factor * 1e3)) # from MW to GW del capacities["demand_elec"] capacities.loc["EUR"] = capacities.sum() capacities.to_csv( path_to_capacities_raw, index=True, header=True, ) capacities.to_csv(path_to_capacities_publish, index=True, header=True, float_format="%.1f")
def excavate_all_results(paths_to_scenarios, path_to_units, scaling_factors, path_to_output): """Collect globally aggregated results.""" scenarios = { calliope.read_netcdf(path_to_scenario).results.attrs["scenario"]: calliope.read_netcdf(path_to_scenario) for path_to_scenario in paths_to_scenarios } units = (gpd.read_file(path_to_units).set_index("id").rename( index=lambda idx: idx.replace(".", "-")).rename_axis(index="locs")) variables = _set_up_variables(units) scaling_factors = _prepare_scaling_factors(scaling_factors) data = _excavate_data(scenarios, variables, scaling_factors).to_dataframe().reset_index() data[["Scenario", "Variable", "Unit", "Value"]].to_csv(path_to_output, header=True, index=False)
def test_solve_save_read_netcdf(self, model): with tempfile.TemporaryDirectory() as tempdir: out_path = os.path.join(tempdir, "model.nc") model.to_netcdf(out_path) assert os.path.isfile(out_path) model_from_disk = calliope.read_netcdf(out_path) for attr in ["results", "inputs", "run_config", "model_config"]: assert hasattr(model_from_disk, attr)
def test_netcdf_roundtrip_modelrun_to_yaml(self, model): with tempfile.TemporaryDirectory() as tempdir: out_path_nc = os.path.join(tempdir, 'model.nc') out_path_yaml = os.path.join(tempdir, 'modelrun.yaml') model.to_netcdf(out_path_nc) model_from_disk = calliope.read_netcdf(out_path_nc) model_from_disk.save_commented_model_yaml(out_path_yaml) assert os.path.isfile(out_path_yaml)
def test_solve_save_read_netcdf(self, model): model.run() with tempfile.TemporaryDirectory() as tempdir: out_path = os.path.join(tempdir, 'model.nc') model.to_netcdf(out_path) assert os.path.isfile(out_path) model_from_disk = calliope.read_netcdf(out_path)
def test_load_from_netcdf(self, model): with tempfile.TemporaryDirectory() as tempdir: out_path = os.path.join(tempdir, "model.nc") model.to_netcdf(out_path) model_from_disk = calliope.read_netcdf(out_path) assert hasattr(model_from_disk, "run_config") assert "run_config" in model_from_disk._model_data.attrs.keys() assert hasattr(model_from_disk, "model_config") assert "model_config" in model_from_disk._model_data.attrs.keys()
def main(path_to_model, scaling_factors, path_to_output): """Create table of important cost assumptions.""" model = calliope.read_netcdf(path_to_model) eur_per_kw = scaling_factors["power"] / scaling_factors[ "monetary"] * EUR_PER_KW ct_per_kw = scaling_factors["power"] / scaling_factors[ "monetary"] * CT_PER_KW energy_cap = (model.get_formatted_array("cost_energy_cap").squeeze( "costs").reindex(techs=list(TECHS.keys())).groupby("techs").mean( "locs").fillna(0).drop("costs")) * eur_per_kw energy_cap.loc["ac_transmission"] = transmission_investment_cost( model, eur_per_kw) annual_cost = (model.get_formatted_array("cost_om_annual").squeeze( "costs").reindex(techs=list(TECHS.keys())).groupby("techs").mean( "locs").fillna(0).drop("costs")) * eur_per_kw annual_cost.loc["ac_transmission"] = transmission_annual_cost( model, eur_per_kw) storage_cap = (model.get_formatted_array("cost_storage_cap").squeeze( "costs").reindex(techs=list(TECHS.keys())).groupby("techs").mean( "locs").fillna(0).drop("costs")) * eur_per_kw lifetime = (model.get_formatted_array("lifetime").reindex( techs=list(TECHS.keys())).groupby("techs").mean("locs").fillna(0)) lifetime.loc["ac_transmission"] = transmission_lifetime(model) variable_costs_prod = (model.get_formatted_array("cost_om_prod").squeeze( "costs").reindex(techs=list(TECHS.keys())).groupby("techs").mean( "locs").fillna(0).drop("costs")) * ct_per_kw variable_costs_con = (model.get_formatted_array("cost_om_con").squeeze( "costs").reindex(techs=list(TECHS.keys())).groupby("techs").mean( "locs").fillna(0).drop("costs")) * ct_per_kw variable_costs = variable_costs_prod + variable_costs_con all_costs = xr.Dataset({ "Overnight cost (€/kW)": energy_cap, "Overnight cost (€/kWh)": storage_cap, "Annual cost (€/kW/yr)": annual_cost, "Variable cost (€ct/kWh)": variable_costs, "Lifetime (yr)": lifetime, "Source": pd.Series(COST_SOURCES).to_xarray().rename(index="techs") }) all_costs.rename(techs="Technology").to_dataframe().rename( index=TECHS).to_csv(path_to_output, index=True, header=True, float_format="%.0f")
def excavate_all_results(paths_to_scenarios, scaling_factors, path_to_output): """Collect all results in standardised format""" scenarios = { Path(path_to_scenario).parent.stem: calliope.read_netcdf(path_to_scenario) for path_to_scenario in paths_to_scenarios } variables = _set_up_variables() scaling_factors = _prepare_scaling_factors(scaling_factors) data = _excavate_data(scenarios, variables, scaling_factors).reset_index() data["Model"] = MODEL_NAME data["Scenario"] = data["Scenario"].map(SCENARIO_NAME_MAP) data["Region"] = data["Region"].map(_rename_region) data[["Model", "Scenario", "Region", "Variable", "Value"]].to_csv(path_to_output, header=True, index=False)
def excavate_trade(path_to_results, scaling_factor, path_to_trade_amounts): """Excavate trade between countries from the model results.""" model = calliope.read_netcdf(path_to_results) imports = _read_imports(model).div(scaling_factor * 1e3) # from MWh to GWh exports = _read_exports(model).div(scaling_factor * 1e3) # from MWh to GWh pd.DataFrame(index=imports.index, data={ "import": imports, "export": exports.reindex(imports.index), "net": imports + exports.reindex(imports.index) }).to_csv(path_to_trade_amounts, index=True, header=True, float_format="%.1f")
def test_save_read_solve_save_netcdf(self, model): with tempfile.TemporaryDirectory() as tempdir: out_path = os.path.join(tempdir, "model.nc") model.to_netcdf(out_path) model_from_disk = calliope.read_netcdf(out_path) # Ensure _model_run doesn't exist to simulate a re-run # via the backend delattr(model_from_disk, "_model_run") model_from_disk.run(force_rerun=True) assert not hasattr(model_from_disk, "_model_run") with tempfile.TemporaryDirectory() as tempdir: out_path = os.path.join(tempdir, "model.nc") model_from_disk.to_netcdf(out_path) assert os.path.isfile(out_path)
def visualise_links(path_to_units, path_to_results, path_to_plot): model = calliope.read_netcdf(path_to_results) units = (gpd.read_file(path_to_units).set_index("id").rename( index=lambda idx: idx.replace(".", "-")).to_crs(EPSG_3035_PROJ4)) network_graph = read_network_graph(units, model) fig = plt.figure(figsize=(6.67, 6.67), constrained_layout=True) ax = fig.subplots(1, 1) plot_network(units, network_graph, ax) ax.set_xticks([]) ax.set_yticks([]) sns.despine(ax=ax, top=True, bottom=True, left=True, right=True) ax.set_xlim(MAP_MIN_X, MAP_MAX_X) ax.set_ylim(MAP_MIN_Y, MAP_MAX_Y) fig.savefig(path_to_plot, pil_kwargs={"compression": "tiff_lzw"})
def determine_y(path_to_results, scaling_factors, experiment_id, path_to_output): results = calliope.read_netcdf(path_to_results) pd.DataFrame(data={ "y-cost-eur": [system_cost(results, scaling_factors)], "y-pv-gw": [pv_capacity(results, scaling_factors)], "y-wind-gw": [wind_capacity(results, scaling_factors)], "y-hydro-gw": [hydro_capacity(results, scaling_factors)], "y-biofuel-gw": [biofuel_capacity(results, scaling_factors)], "y-storage-gw": [storage_capacity_power(results, scaling_factors)], "y-storage-gwh": [storage_capacity_energy(results, scaling_factors)], "y-transmission-gwkm": [transmission_capacity(results, scaling_factors)], }, index=[experiment_id]).to_csv(path_to_output, sep="\t", index=True, header=True)
def read_plot_data(path_to_result, scaling_factors, connected_regions): model = calliope.read_netcdf(path_to_result) gen = postprocess_combined_regions( model.get_formatted_array("carrier_prod").squeeze("carriers") / scaling_factors["power"], connected_regions.items()) cost = postprocess_combined_regions( model.get_formatted_array("cost").squeeze('costs') / scaling_factors["monetary"], connected_regions.items()) cap = postprocess_combined_regions( model.get_formatted_array("energy_cap") / scaling_factors["power"], connected_regions.items()) stor = postprocess_combined_regions( model.get_formatted_array("storage_cap") / scaling_factors["power"], connected_regions.items()) e_stor = postprocess_combined_regions( model.get_formatted_array("storage") / scaling_factors["power"], connected_regions.items()) dem = postprocess_combined_regions( model.get_formatted_array("carrier_con").squeeze("carriers").sel( techs=DEMAND) / scaling_factors["power"], connected_regions.items()) resource = postprocess_combined_regions( model.get_formatted_array("resource").sel(techs=VRES_TECHS), connected_regions.items()) pot = (resource * cap.sel(techs=VRES_TECHS)) flex_cost_share = cost.sel( techs=FLEX_TECHS).sum("techs") / cost.sum("techs") resolution_in_hours = (gen.timesteps[1].item() - gen.timesteps[0].item()) * NS_TO_H return [ PlotData(name="Wind and solar", ylabel="Relative potential", da=(pot.sel(techs=WIND_AND_PV).sum("techs")) / -dem, mask=flex_cost_share >= flex_cost_share.quantile(0.9).item()), PlotData(name="Bioenergy", ylabel="Relative generation", da=gen.sel(techs="biofuel") / resolution_in_hours / -dem, mask=flex_cost_share >= flex_cost_share.quantile(0.9).item()), PlotData(name="Hydrogen", ylabel="Storage level", da=e_stor.sel(techs=HYDROGEN) / stor.sel(techs=HYDROGEN), mask=flex_cost_share >= flex_cost_share.quantile(0.9).item()) ]
def excavate_installed_storage_capacities(path_to_results, scaling_factor, path_to_capacities_raw, path_to_capacities_publish): """Excavate installed storage capacities from the model results.""" model = calliope.read_netcdf(path_to_results) capacities = _read_capacities(model) capacities = capacities.groupby(["locs", "techs"]).sum().reset_index() capacities = (capacities.pivot( index="locs", columns="techs", values="storage_cap").div(scaling_factor * 1e3)) # from MWh to GWh capacities.loc["EUR"] = capacities.sum() capacities.to_csv( path_to_capacities_raw, index=True, header=True, ) capacities.to_csv(path_to_capacities_publish, index=True, header=True, float_format="%.1f")
def excavate_all_results(paths_to_scenarios, path_to_units, scaling_factors, aggregate_time, path_to_output): """Collect and postprocess results of all scenarios.""" models = [ calliope.read_netcdf(path_to_scenario) for path_to_scenario in paths_to_scenarios ] scenarios = {model.results.attrs["scenario"]: model for model in models} units = (pd.read_csv(path_to_units).set_index("id").rename( index=lambda idx: idx.replace(".", "-")).rename_axis( index="locs").to_xarray()) exporter = CalliopeExporter(scenarios, units, scaling_factors, aggregate_time) ds = xr.Dataset( {variable.name: exporter(variable) for variable in VARIABLES}) ds.coords["country_code"] = units.country_code ds.coords["location_name"] = units.name ds.coords["tech_group"] = list( scenarios.values())[0].get_formatted_array("inheritance") ds.coords["tech_group"].loc[:] = [ parent.split('.')[-1] for parent in ds["tech_group"].values ] ds.to_netcdf(path_to_output)
def visualise_model_results(path_to_results, scaling_factor, path_to_figure): """Plot the results.""" model = calliope.read_netcdf(path_to_results) generation = _create_generation_timeseries(model).div( scaling_factor * 1e3) # from MW to GW consumption = _create_consumption_timeseries(model).div( scaling_factor * 1e3) # from MW to GW generation = generation.reindex(columns=generation.columns.union( consumption.columns), fill_value=0) consumption = consumption.reindex(columns=generation.columns.union( consumption.columns), fill_value=0) sns.set_context('paper') fig = plt.figure(figsize=(8, 4)) ax = fig.add_subplot(111) ax.plot(generation + consumption) ax.legend(generation.columns) ax.set_xlabel("time") ax.set_ylabel("electricity generation [GW]") fig.savefig(path_to_figure, dpi=300)
def europe_model(self, request): return calliope.read_netcdf(request.param)
def germany_model(self, request): return calliope.read_netcdf(request.param)
def run(model_file, override_file, save_netcdf, save_csv, save_plots, save_logs, model_format, debug, quiet, pdb, profile, profile_filename): """ Execute the given model. Tries to guess from the file extension whether ``model_file`` is a YAML file or a pre-built model saved to NetCDF. This can also explicitly be set with the --model_format=yaml or --model_format=netcdf option. """ if debug: print(_get_version()) set_quietness_level(quiet) logging.captureWarnings(True) pywarning_logger = logging.getLogger('py.warnings') pywarning_logger.addHandler(console) start_time = datetime.datetime.now() with format_exceptions(debug, pdb, profile, profile_filename, start_time): if save_csv is None and save_netcdf is None: click.secho( '\n!!!\nWARNING: No options to save results have been ' 'specified.\nModel will run without saving results!\n!!!\n', fg='red', bold=True) tstart = start_time.strftime(_time_format) print('Calliope {} starting at {}\n'.format(__version__, tstart)) # Try to determine model file type if not given explicitly if model_format is None: if model_file.split('.')[-1] in ['yaml', 'yml']: model_format = 'yaml' elif model_file.split('.')[-1] in ['nc', 'nc4', 'netcdf']: model_format = 'netcdf' else: raise ValueError( 'Cannot determine model file format based on file ' 'extension for "{}". Set format explicitly with ' '--model_format.'.format(model_file)) if model_format == 'yaml': override_dict = {'run.save_logs': save_logs} model = Model(model_file, override_file=override_file, override_dict=override_dict) elif model_format == 'netcdf': if override_file is not None: raise ValueError( 'Overrides cannot be applied when loading a pre-built ' 'model from NetCDF. Please run without --override options.' ) model = read_netcdf(model_file) if save_logs is not None: model._model_data.attrs['run.save_logs'] = save_logs else: raise ValueError('Invalid model format: {}'.format(model_format)) print(model.info() + '\n') print('Starting model run...') model.run() termination = model._model_data.attrs.get('termination_condition', 'unknown') if save_csv: print('Saving CSV results to directory: {}'.format(save_csv)) model.to_csv(save_csv) if save_netcdf: print('Saving NetCDF results to file: {}'.format(save_netcdf)) model.to_netcdf(save_netcdf) if save_plots: if termination == 'optimal': print('Saving HTML file with plots to: {}'.format(save_plots)) model.plot.summary(to_file=save_plots) else: click.secho( 'Model termination condition non-optimal. Not saving plots', fg='red', bold=True) print_end_time(start_time)
def run(model_file, override_file, save_netcdf, save_csv, save_plots, save_logs, model_format, debug, pdb, profile, profile_filename): """ Execute the given model. Tries to guess from the file extension whether ``model_file`` is a YAML file or a pre-built model saved to NetCDF. This can also explicitly be set with the --model_format=yaml or --model_format=netcdf option. """ if debug: print(_get_version()) logging.captureWarnings(True) start_time = datetime.datetime.now() with format_exceptions(debug, pdb, profile, profile_filename, start_time): if save_csv is None and save_netcdf is None: print( '!!!\nWARNING: Neither save_csv nor save_netcdf have been ' 'specified. Model will run without saving results!\n!!!\n' ) tstart = start_time.strftime(_time_format) print('Calliope run starting at {}\n'.format(tstart)) # Try to determine model file type if not given explicitly if model_format is None: if model_file.split('.')[-1] in ['yaml', 'yml']: model_format = 'yaml' elif model_file.split('.')[-1] in ['nc', 'nc4', 'netcdf']: model_format = 'netcdf' else: raise ValueError( 'Cannot determine model file format based on file ' 'extension for "{}". Set format explicitly with ' '--model_format.'.format(model_file) ) if model_format == 'yaml': override_dict = { 'run.save_logs': save_logs } model = Model( model_file, override_file=override_file, override_dict=override_dict ) elif model_format == 'netcdf': if override_file is not None: raise ValueError( 'Overrides cannot be applied when loading a pre-built ' 'model from NetCDF. Please run without --override options.' ) model = read_netcdf(model_file) if save_logs is not None: model._model_data.attrs['run.save_logs'] = save_logs else: raise ValueError('Invalid model format: {}'.format(model_format)) # FIXME: get this from model._model_data rather than _model_run model_name = model._model_data.attrs.get('model.name', 'None') print('Model name: {}'.format(model_name)) msize = '{locs} locations, {techs} technologies, {times} timesteps'.format( locs=len(model._model_data.coords['locs'].values), techs=( len(model._model_data.coords['techs_non_transmission'].values) + len(model._model_data.coords['techs_transmission_names'].values) ), times=len(model._model_data.coords['timesteps'].values)) print('Model size: {}\n'.format(msize)) print('Starting model run...') model.run() if save_csv: print('Saving CSV results to directory: {}'.format(save_csv)) model.to_csv(save_csv) if save_netcdf: print('Saving NetCDF results to file: {}'.format(save_netcdf)) model.to_netcdf(save_netcdf) if save_plots: print('Saving HTML file with plots to: {}'.format(save_plots)) model.plot.summary(out_file=save_plots) print_end_time(start_time)
# Germany administrative regions model (GER-nuts1) with and without rooftop_pv de_nuts1_model, de_nuts1_gpd = run_scenario(['Germany'], 'poli_regions_nuts1', None, 1, 'de_nuts1-pv-calliope-3h') de_nuts1_nopv_model, de_nuts1_gpd = run_scenario(['Germany'], 'poli_regions_nuts1', None, 0, 'de_nuts1-nopv-calliope-3h') #--------------------------------------------------------------------------# # Germany max-p regions model (GER-max-p) with and without rooftop_pv de_maxp_model, de_maxp_gpd = run_scenario(['Germany'], 'max_p_regions', 3.8, 1, 'de_maxp-pv-calliope-3h') de_maxp_nopv_model, de_maxp_gpd = run_scenario(['Germany'], 'max_p_regions', 3.8, 0, 'de_maxp-nopv-calliope-3h') eu_nuts0_model = calliope.read_netcdf( 'calliope_model/results/de_eu-calliope-3h.nc') de_nuts1_model = calliope.read_netcdf( 'calliope_model/results/de_nuts1-pv-calliope-3h.nc') de_maxp_model = calliope.read_netcdf( 'calliope_model/results/de_maxp-pv-calliope-3h.nc') #-------------------------------------------------------------------------------------------- area_usage = pd.DataFrame(index=['GER NUTS0', 'GER NUTS1', 'GER MAX-P']) results_dic = { 'GER NUTS0': eu_nuts0_model, 'GER NUTS1': de_nuts1_model, 'GER MAX-P': de_maxp_model } for name, m in results_dic.items():
# model.backend.update_param('storage_cap_equals', {'%s::tes_dhw' %reg_dict[reg] : subset_users*regional_arch_matr[reg][0:subset_users].loc['tes_size'].mean()}) # model.backend.update_param('energy_cap_equals', {'%s::pv_rooftop' %reg_dict[reg] : subset_users*2}) # model.backend.update_param('energy_cap_equals', {'%s::grid' %reg_dict[reg] : subset_users*100}) # for t in x_h: # model.backend.update_param('resource', {('%s::demand_dhw' %reg_dict[reg],t) : regional_prof_matr[reg].iloc[:,0:subset_users].sum(axis=1).loc[t]}) # model._model_data.resource.loc[{'loc_techs_finite_resource':'%s::demand_dhw' %reg_dict[reg], 'timesteps':t}] = regional_prof_matr[reg].iloc[:,0:subset_users].mean(axis=1).loc[t] # model_vpp = model.backend.rerun() # model_vpp.to_netcdf('NetCDFs/results_0.nc') #%% # Processing results ### model_vpp = calliope.read_netcdf('NetCDFs/results_0.nc') p2h_vpp_dict = {} for reg in reg_dict.keys(): p2h_vpp_dict[reg_dict[reg]] = netcdf_to_data(model_vpp, reg_dict[reg]) # p2h_plot(p2h_vpp_dict['R11'],start,stop) #%% Importing realistic results # p2h_tot_dict = load_obj('regional_p2h_opt_dict_50k') # p2h_plot(p2h_tot_dict['Marche'],start,stop) #%% Comparison # regional_p2h_nrmse = {} # for reg in regions: # regional_p2h_nrmse[reg] = nrmse(p2h_vpp_dict[reg_dict[reg]],p2h_tot_dict[reg])
def _read_cost(path_to_model, scaling_factor): model = calliope.read_netcdf(path_to_model) return (model.get_formatted_array("total_levelised_cost").to_dataframe(). loc[("electricity", "monetary"), "total_levelised_cost"]) * scaling_factor
for v in p2h_individual_dict[us]._model_data.data_vars: if (isinstance( p2h_individual_dict[us]._model_data[v].values.flatten()[0], (np.bool_, bool))): p2h_individual_dict[us]._model_data[v] = p2h_individual_dict[ us]._model_data[v].astype(float) p2h_individual_dict[us].to_netcdf('NetCDFs/results_%d.nc' % us) #%% # Importing and processing results ### p2h_dict = {} for us in list(profiles_matrix.columns): try: p2h_dict[us] = netcdf_to_data( calliope.read_netcdf('NetCDFs/results_%d.nc' % us)) print('done %d' % us) except: break # save_obj(p2h_dict,'p2h_dict_realistic') #%% p2h_tot = pd.DataFrame(index=p2h_dict[0].index, columns=p2h_dict[0].columns).astype('float').fillna(0) for us in p2h_dict.keys(): for col in p2h_dict[us].columns: p2h_tot[col] += p2h_dict[us][col] p2h_plot(p2h_tot, start, stop) #(-p2h_tot['dhw_load']).to_csv('results/demand_dhw_tot.csv') #
''' ''' Model creation, run and saving to netCDF - Iteration 0 ''' model = calliope.Model( 'Model/model.yaml', scenario='2050_eff') #this version only includes the power sector model.run() model.to_netcdf('NetCDFs/results_0.nc') ''' Alternatively, previously run solutions can be read from netCDF files ''' # model = calliope.read_netcdf('NetCDFs/results_0.nc') # model.run(build_only=True, force_rerun=True) model_0 = calliope.read_netcdf('NetCDFs/results_0.nc') ''' Computation of nos_scores per location ''' cap_loc_score_0 = cap_loc_score_potential(model_0, techs=techs_new) ''' Extrapolation of relevant indicators ''' #Cost class costs_0 = model.get_formatted_array('cost').loc[{ 'costs': 'monetary' }].sum(['locs', 'techs']).to_pandas() cost_list.append(costs_0) ''' Creation and saving of a list of slacked neighbourhoods of the optimal cost '''
def run(model_file, scenario, save_netcdf, save_csv, save_plots, save_logs, model_format, override_dict, debug, quiet, pdb, profile, profile_filename): """ Execute the given model. Tries to guess from the file extension whether ``model_file`` is a YAML file or a pre-built model saved to NetCDF. This can also explicitly be set with the --model_format=yaml or --model_format=netcdf option. """ if debug: print(_get_version()) set_quietness_level(quiet) logging.captureWarnings(True) pywarning_logger = logging.getLogger('py.warnings') pywarning_logger.addHandler(console) start_time = datetime.datetime.now() with format_exceptions(debug, pdb, profile, profile_filename, start_time): if save_csv is None and save_netcdf is None: click.secho( '\n!!!\nWARNING: No options to save results have been ' 'specified.\nModel will run without saving results!\n!!!\n', fg='red', bold=True ) tstart = start_time.strftime(_time_format) print('Calliope {} starting at {}\n'.format(__version__, tstart)) # Try to determine model file type if not given explicitly if model_format is None: if model_file.split('.')[-1] in ['yaml', 'yml']: model_format = 'yaml' elif model_file.split('.')[-1] in ['nc', 'nc4', 'netcdf']: model_format = 'netcdf' else: raise ValueError( 'Cannot determine model file format based on file ' 'extension for "{}". Set format explicitly with ' '--model_format.'.format(model_file) ) if model_format == 'yaml': model = Model( model_file, scenario=scenario, override_dict=override_dict ) elif model_format == 'netcdf': if scenario is not None or override_dict is not None: raise ValueError( 'When loading a pre-built model from NetCDF, the ' '--scenario and --override_dict options are not available.' ) model = read_netcdf(model_file) else: raise ValueError('Invalid model format: {}'.format(model_format)) if save_logs: model._model_data.attrs['run.save_logs'] = save_logs print(model.info() + '\n') print('Starting model run...') model.run() termination = model._model_data.attrs.get( 'termination_condition', 'unknown') if save_csv: print('Saving CSV results to directory: {}'.format(save_csv)) model.to_csv(save_csv) if save_netcdf: print('Saving NetCDF results to file: {}'.format(save_netcdf)) model.to_netcdf(save_netcdf) if save_plots: if termination == 'optimal': print('Saving HTML file with plots to: {}'.format(save_plots)) model.plot.summary(to_file=save_plots) else: click.secho( 'Model termination condition non-optimal. Not saving plots', fg='red', bold=True ) print_end_time(start_time)