Skip to content

Commit

Permalink
EODC
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Aug 23, 2023
1 parent 150bdb5 commit 446683e
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 293 deletions.
364 changes: 140 additions & 224 deletions dev/Untitled.ipynb

Large diffs are not rendered by default.

Binary file not shown.
114 changes: 57 additions & 57 deletions premise/data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,13 +659,12 @@ def __get_iam_data(

elif filepath.suffix in [".xls", ".xlsx"]:
print(f"Reading {filepath} as excel file")
data = pd.read_excel(filepath, sheet_name=None)
data = pd.read_excel(filepath)

else:
raise ValueError(
f"Extension {filepath.suffix} is not supported. Please use .csv, .mif, .xls or .xlsx."
)

else:
# Uses an encrypted file
fernet_obj = Fernet(key)
Expand All @@ -688,7 +687,7 @@ def __get_iam_data(

# if a column name can be an integer
# we convert it to an integer
new_cols = {c: int(c) if c.isdigit() else c for c in dataframe.columns}
new_cols = {c: int(c) if str(c).isdigit() else c for c in dataframe.columns}
dataframe = dataframe.rename(columns=new_cols)

# remove any column that is a string
Expand Down Expand Up @@ -908,7 +907,7 @@ def __get_carbon_capture_rate(
# and that none of the CO2 emissions are captured

if not any(
x in data.variables.values.tolist() for x in dict_vars.get("cement - cco2")
x in data.variables.values.tolist() for x in dict_vars.get("cement - cco2", [])
):
cement_rate = xr.DataArray(
np.zeros((len(data.region), len(data.year))),
Expand All @@ -923,7 +922,7 @@ def __get_carbon_capture_rate(
cement_rate.coords["variables"] = "cement"

if not any(
x in data.variables.values.tolist() for x in dict_vars.get("steel - cco2")
x in data.variables.values.tolist() for x in dict_vars.get("steel - cco2", [])
):
steel_rate = xr.DataArray(
np.zeros((len(data.region), len(data.year))),
Expand All @@ -948,73 +947,74 @@ def __get_carbon_capture_rate(
# as it is sometimes neglected in the
# IAM files

if not any(
x in data.variables.values.tolist() for x in dict_vars.get("cement - cco2")
):
rate.loc[dict(region="World", variables="cement")] = 0
else:
try:
rate.loc[dict(region="World", variables="cement")] = (
if "World" in rate.region.values.tolist():
if not any(
x in data.variables.values.tolist() for x in dict_vars.get("cement - cco2", [])
):
rate.loc[dict(region="World", variables="cement")] = 0
else:
try:
rate.loc[dict(region="World", variables="cement")] = (
data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["cement - cco2"],
)
]
.sum(dim=["variables", "region"])
.values
/ data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["cement - co2"],
)
]
.sum(dim=["variables", "region"])
.values
)
except ZeroDivisionError:
rate.loc[dict(region="World", variables="cement")] = 0

try:
rate.loc[dict(region="World", variables="steel")] = data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["steel - cco2"],
)
].sum(dim=["variables", "region"]) / data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["steel - co2"],
)
].sum(
dim=["variables", "region"]
)
except ZeroDivisionError:
rate.loc[dict(region="World", variables="steel")] = 0

if not any(
x in data.variables.values.tolist() for x in dict_vars.get("steel - cco2", [])
):
rate.loc[dict(region="World", variables="steel")] = 0
else:
rate.loc[dict(region="World", variables="steel")] = (
data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["cement - cco2"],
variables=dict_vars["steel - cco2"],
)
]
.sum(dim=["variables", "region"])
.values
/ data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["cement - co2"],
variables=dict_vars["steel - co2"],
)
]
.sum(dim=["variables", "region"])
.values
)
except ZeroDivisionError:
rate.loc[dict(region="World", variables="cement")] = 0

try:
rate.loc[dict(region="World", variables="steel")] = data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["steel - cco2"],
)
].sum(dim=["variables", "region"]) / data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["steel - co2"],
)
].sum(
dim=["variables", "region"]
)
except ZeroDivisionError:
rate.loc[dict(region="World", variables="steel")] = 0

if not any(
x in data.variables.values.tolist() for x in dict_vars.get("steel - cco2")
):
rate.loc[dict(region="World", variables="steel")] = 0
else:
rate.loc[dict(region="World", variables="steel")] = (
data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["steel - cco2"],
)
]
.sum(dim=["variables", "region"])
.values
/ data.loc[
dict(
region=[r for r in self.regions if r != "World"],
variables=dict_vars["steel - co2"],
)
]
.sum(dim=["variables", "region"])
.values
)

# we ensure that the rate can only be between 0 and 1
rate.values = np.clip(rate, 0, 1)
Expand Down
Loading

0 comments on commit 446683e

Please sign in to comment.