IAMconsortium / pyam

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

Adding peak temperature scenario metadata #237

Closed znicholls closed 5 years ago

znicholls commented 5 years ago

@gidden and @danielhuppmann is there a function that allows the user to easily add peak temperature for each scenario as a metadata column of an IamDataFrame? I'm thinking something like, df.add_peak_temperature_meta().

Maybe it's currently a two-line solution, something like the below?

mdf = df.data.groupby(["model", "scenario"]).max("Temperature")
df.set_meta(mdf)

Sorry if this is already in there and should be obvious, I couldn't work it out. If it's not in there, I'm happy for you to assign this one to me (I won't get to it straight away but will eventually).

danielhuppmann commented 5 years ago

Indeed, this use case already popped up in the SR15, in parictular notebook sr15_2.0_categories_indicators.ipynb at https://github.com/iiasa/ipcc_sr15_scenario_analysis.

Below, a few lines of code copy-pasted from that notebook - a bit more elaborate than your suggestion because it did a lot more things, like also adding a meta-column in which year peak temperature was reached, and doing that for multiple climate models.

median_warming = 'AR5 climate diagnostics|Temperature|Global Mean|MAGICC6|MED'

def peak_warming(x, return_year=False):
    peak = x[x == x.max()]
    if return_year:
        return peak.index[0]
    else:
        return float(max(peak))

median_temperature = sr1p5.filter(variable=median_warming).timeseries()
name = 'median warming at peak (MAGICC6)'
sr1p5.set_meta(median_temperature.apply(peak_warming, raw=False, axis=1), name)

I'm a bit hesitant about the very concise df.add_peak_temperature_meta(), because the exact variable string from which to derive that info will be application-specific - or which climate model to use.

But a more generic df.set_meta_from_data() might be useful, e.g.,

df.set_meta_from_data(name, method, column='value', **kwargs)

where

So your use case could then be implemented with:

df.set_meta_from_data(variable='Temperature', name='Peak temperature', method='max')
znicholls commented 5 years ago

Nice that looks very elegant!

gidden commented 5 years ago

Hi folks - just catching up on this now. It seems like @danielhuppmann has a proposal for a more generic metadata addition. Should we make that a separate issue with the proposal and close this one?

znicholls commented 5 years ago

Should we make that a separate issue with the proposal and close this one?

Works for me

gidden commented 5 years ago

Ok, great. I'll leave that to you guys then =)

On Tue, May 28, 2019 at 9:55 AM Zeb Nicholls notifications@github.com wrote:

Should we make that a separate issue with the proposal and close this one?

Works for me

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/IAMconsortium/pyam/issues/237?email_source=notifications&email_token=AAKUAEKQH22JEKLKC6IHIGDPXTQOZA5CNFSM4HPLBAZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWLJBFY#issuecomment-496406679, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKUAEMMNFARYGNDKKHYBFLPXTQOZANCNFSM4HPLBAZA .

danielhuppmann commented 5 years ago

closing in favour of #238