From 323579afa97d6d467ba8cb6bb681db8a6a1b5a35 Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Sun, 28 Apr 2024 23:36:35 +0200 Subject: [PATCH 1/2] Move the co2_factors into a csv file --- docs/energy_co2_conversion_factors.csv | 15 +++ prepare_package.py | 9 +- src/multi_vector_simulator/utils/constants.py | 95 +++++-------------- 3 files changed, 42 insertions(+), 77 deletions(-) create mode 100644 docs/energy_co2_conversion_factors.csv diff --git a/docs/energy_co2_conversion_factors.csv b/docs/energy_co2_conversion_factors.csv new file mode 100644 index 000000000..dcf5a2fd2 --- /dev/null +++ b/docs/energy_co2_conversion_factors.csv @@ -0,0 +1,15 @@ +energy_carrier_name,unit,value,energy_carrier_unit,information source,CO2 per energy_carrier_unit +Biodiesel,kWh_eleq/l,0.06290669,l,,0 +Crude_oil,kWh_eleq/kg,11.63042204,kg,,0 +Diesel,kWh_eleq/l,9.48030688,l,"https://epact.energy.gov/fuel-conversion-factors, conversion gallon->4.546092 l",0 +Electricity,kWh_eleq/kWh_el,1,kWh_el,,0 +Ethane,kWh_eleq/l,5.149767951,l,,0 +Ethanol,kWh_eleq/l,0.04242544,l,,0 +Gas,kWh_eleq/m3,0.00933273,l,"https://epact.energy.gov/fuel-conversion-factors, conversion gallon->4.546092 l",0 +Gasoline,kWh_eleq/l,8.735753974,l,,0 +H2,kWh_eleq/kgH2,33.47281985,kgH2,"https://epact.energy.gov/fuel-conversion-factors",0 +Heat,KWh_eleq/kWh_therm,1.0002163,kWh_therm,,0 +Kerosene,kWh_eleq/l,8.908073954,l,,0 +LNG,kWh_eleq/kg,12.69270292,kg,,0 +LPG,kWh_eleq/l,6.472821609,l,,0 +Natural_gas,kWh_eleq/m3,0.00933273,l,"https://epact.energy.gov/fuel-conversion-factors, conversion gallon->4.546092 l",0 diff --git a/prepare_package.py b/prepare_package.py index ff1b47e0c..e57b6594c 100644 --- a/prepare_package.py +++ b/prepare_package.py @@ -22,10 +22,11 @@ os.path.join(".", pgk_data_src), os.path.join(pkg_data_folder, pkg_data_dest) ) # Move the MVS parameters from the docs into the package_data folder -shutil.copyfile( - os.path.join(".", "docs", "MVS_parameters_list.csv"), - os.path.join(pkg_data_folder, "MVS_parameters_list.csv"), -) +for doc_file in ("MVS_parameters_list.csv", "energy_co2_conversion_factors.csv"): + shutil.copyfile( + os.path.join(".", "docs", doc_file), + os.path.join(pkg_data_folder, doc_file), + ) # Rebuild the package os.system("python setup.py sdist bdist_wheel") diff --git a/src/multi_vector_simulator/utils/constants.py b/src/multi_vector_simulator/utils/constants.py index a44b9bf5d..9aec9cb6c 100644 --- a/src/multi_vector_simulator/utils/constants.py +++ b/src/multi_vector_simulator/utils/constants.py @@ -4,6 +4,7 @@ """ import os +import pandas as pd from copy import deepcopy from multi_vector_simulator.utils.constants_json_strings import * @@ -309,79 +310,27 @@ }, } -ENERGY_CARRIER_UNIT = "energy_carrier_unit" -DEFAULT_WEIGHTS_ENERGY_CARRIERS = { - "LNG": { - UNIT: "kWh_eleq/kg", - VALUE: 12.69270292, - ENERGY_CARRIER_UNIT: "kg", - }, - "Crude_oil": { - UNIT: "kWh_eleq/kg", - VALUE: 11.63042204, - ENERGY_CARRIER_UNIT: "kg", - }, - "Diesel": { - UNIT: "kWh_eleq/l", - VALUE: 9.48030688, - ENERGY_CARRIER_UNIT: "l", - }, # https://epact.energy.gov/fuel-conversion-factors, conversion gallon->4.546092 l - "Kerosene": { - UNIT: "kWh_eleq/l", - VALUE: 8.908073954, - ENERGY_CARRIER_UNIT: "l", - }, - "Gasoline": { - UNIT: "kWh_eleq/l", - VALUE: 8.735753974, - ENERGY_CARRIER_UNIT: "l", - }, - "LPG": { - UNIT: "kWh_eleq/l", - VALUE: 6.472821609, - ENERGY_CARRIER_UNIT: "l", - }, - "Ethane": { - UNIT: "kWh_eleq/l", - VALUE: 5.149767951, - ENERGY_CARRIER_UNIT: "l", - }, - "H2": { - UNIT: "kWh_eleq/kgH2", - VALUE: 33.47281985, - ENERGY_CARRIER_UNIT: "kgH2", - }, # https://epact.energy.gov/fuel-conversion-factors - "Electricity": { - UNIT: "kWh_eleq/kWh_el", - VALUE: 1, - ENERGY_CARRIER_UNIT: "kWh_el", - }, - "Biodiesel": { - UNIT: "kWh_eleq/l", - VALUE: 0.06290669, - ENERGY_CARRIER_UNIT: "l", - }, - "Ethanol": { - UNIT: "kWh_eleq/l", - VALUE: 0.04242544, - ENERGY_CARRIER_UNIT: "l", - }, - "Natural_gas": { - UNIT: "kWh_eleq/m3", - VALUE: 0.00933273, - ENERGY_CARRIER_UNIT: "l", - }, # https://epact.energy.gov/fuel-conversion-factors, conversion gallon->4.546092 l - "Gas": { - UNIT: "kWh_eleq/m3", - VALUE: 0.00933273, - ENERGY_CARRIER_UNIT: "l", - }, # https://epact.energy.gov/fuel-conversion-factors, conversion gallon->4.546092 l - "Heat": { - UNIT: "KWh_eleq/kWh_therm", - VALUE: 1.0002163, - ENERGY_CARRIER_UNIT: "kWh_therm", - }, -} +WEIGHTS_ENERGY_CARRIER = "weights_energy_carrier" + +try: + energy_carriers_file = "energy_co2_conversion_factors.csv" + FILE_PATH = os.path.join( + os.path.dirname( + os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + ), + "docs", + ) + if os.path.exists(FILE_PATH): + FILE_PATH = PACKAGE_DATA_PATH + + df = pd.read_csv( + os.path.join(FILE_PATH, "energy_co2_conversion_factors.csv"), index_col=0 + ) + + DEFAULT_WEIGHTS_ENERGY_CARRIERS = df.to_dict(orient="index") +except FileNotFoundError: + DEFAULT_WEIGHTS_ENERGY_CARRIERS = None + # dict keys in results_json file TIMESERIES = "timeseries" From 1bafd07f8d0600fefd2a0d7d56c92401ad601b6c Mon Sep 17 00:00:00 2001 From: "pierre-francois.duc" Date: Wed, 2 Nov 2022 11:25:43 +0100 Subject: [PATCH 2/2] Include userdefined energy carrier if they are in the json file --- .../C0_data_processing.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/multi_vector_simulator/C0_data_processing.py b/src/multi_vector_simulator/C0_data_processing.py index f66ae826d..56cbd87ce 100644 --- a/src/multi_vector_simulator/C0_data_processing.py +++ b/src/multi_vector_simulator/C0_data_processing.py @@ -25,6 +25,7 @@ import os import sys import pprint as pp +import jsonschema import pandas as pd import warnings from multi_vector_simulator.version import version_num @@ -37,6 +38,8 @@ FILENAME, HEADER, JSON_PROCESSED, + WEIGHTS_ENERGY_CARRIER, + DEFAULT_WEIGHTS_ENERGY_CARRIERS, ) from multi_vector_simulator.utils.exceptions import MaximumCapValueInvalid @@ -74,6 +77,8 @@ def all(dict_values): # C1.check_input_values(dict_values) # todo Check, whether files (demand, generation) are existing + process_user_energy_carrier_weights(dict_values) + # Adds costs to each asset and sub-asset, adds time series to assets process_all_assets(dict_values) @@ -1850,6 +1855,50 @@ def treat_multiple_flows(dict_asset, dict_values, parameter): dict_asset[parameter].update({"values_info": values_info}) +def process_user_energy_carrier_weights(dict_values): + """ + Parameters + ---------- + dict_asset: + dictionary of the asset + """ + if WEIGHTS_ENERGY_CARRIER in dict_values: + + SCHEMA = { + "type": "object", + "properties": { + "objects": { + "type": "object", + "additionalProperties": { + "type": "object", + "properties": { + UNIT: {"type": "string"}, + VALUE: {"type": "number"}, + "energy_carrier_unit": {"type": "string"}, + "information source": {"type": "string"}, + "CO2 per energy_carrier_unit": {"type": "number"}, + }, + "required": [ + UNIT, + VALUE, + ], + }, + } + }, + } + + try: + jsonschema.validate(dict_values[WEIGHTS_ENERGY_CARRIER], SCHEMA) + valid_json = True + except jsonschema.exceptions.ValidationError: + valid_json = False + # TODO log validation error + + if valid_json is True: + DEFAULT_WEIGHTS_ENERGY_CARRIERS.update(dict_values[WEIGHTS_ENERGY_CARRIER]) + logging.info("Added user custom energy carrier weights") + + # reads timeseries specifically when the need comes from a multiple or output busses situation # returns the timeseries. Does not update any dictionary def get_timeseries_multiple_flows(settings, dict_asset, file_name, header):