IAMconsortium / pyam

Analysis & visualization of energy & climate scenarios
https://pyam-iamc.readthedocs.io/
Apache License 2.0
226 stars 118 forks source link

Aggregate variables with different units #460

Open pjuergens opened 3 years ago

pjuergens commented 3 years ago

If I have an IamDataFrame df with two variables, 'Primary Energy|Gas' with unit 'TWh/yr' and 'Emission Factor|Gas' with unit 'Mt/TWh'. If I want to aggregate both variables to 'Emissions|Gas' doing the following:

df.aggregate('Emissions|Gas', components=['Primary Energy|Gas', 'Emission Factor|Gas'], method=np.prod)

instead of getting the unit 'Mt/yr' I get two entries with units 'TWh/yr' and 'Mt/TWh'. I'd like to have either 1) automatic unit conversion or 2) manual unit conversion like df.aggregate('Emissions|Gas', components=['Primary Energy|Gas', 'Emission Factor|Gas'], method=np.prod, unit='Mt/TWh)

Currently as a workaround I'm doing something like temp_df = df.convert_unit('Mt/TWh', to='Mt/yr', factor=1) temp_df = temp_df.convert_unit('TWh/yr', to='Mt/yr', factor=1) temp_df.aggregate('Emissions|Gas', components=['Primary Energy|Gas', 'Emission Factor|Gas'], method=np.prod)

danielhuppmann commented 3 years ago

Thanks @pjuergens for raising this issue!

You are trying to do something that's going beyond the intent of the aggregation-function. But you actually found a nice workaround using standard pyam functions, so kudos!

Anyway, @gaurav-ganti started a similar issue (see #332) and in response @gidden started a PR a few months ago (see #333) that implemented basic operations (multiplication, division, addition, subtraction). Unfortunately, this development didn't quite make it into the master branch, but if you feel up to the task, you could look into this and bring it to completion! What do you think?