Giter Site home page Giter Site logo

znicholls / pymagicc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openscm/pymagicc

0.0 1.0 0.0 3.06 MB

Python wrapper for the simple climate model MAGICC

License: GNU Affero General Public License v3.0

Makefile 0.41% Jupyter Notebook 76.74% TeX 0.81% Python 22.05%

pymagicc's Introduction

Pymagicc

Build Status AppVeyor
Codecov Launch Binder
PyPI PyPI
status Zenodo

Pymagicc is a thin Python wrapper around the reduced complexity climate model MAGICC6. It wraps the CC-BY-NC-SA licensed MAGICC6 binary. Pymagicc itself is AGPL licensed.

MAGICC (Model for the Assessment of Greenhouse Gas Induced Climate Change) is widely used in the assessment of future emissions pathways in climate policy analyses, e.g. in the Fifth Assessment Report of the Intergovernmental Panel on Climate Change or to model the physical aspects of climate change in Integrated Assessment Models (IAMs).

Pymagicc makes the MAGICC model easily installable and usable from Python and allows for the easy modification of all MAGICC model parameters and emissions scenarios directly from Python. In climate research it can, for example, be used in the analysis of mitigation scenarios, in Integrated Assessment Models, complex climate model emulation, and uncertainty analyses, as well as in climate science education and communication.

See www.magicc.org for further information about the MAGICC model.

Basic Usage

import pymagicc
from pymagicc import scenarios
import matplotlib.pyplot as plt

for name, scen in scenarios.items():
    results, params = pymagicc.run(scen, return_config=True)
    temp = (results["SURFACE_TEMP"].GLOBAL.loc[1850:] -
            results["SURFACE_TEMP"].GLOBAL.loc[1850:1900].mean())
    temp.plot(label=name)
plt.legend()
plt.title("Global Mean Temperature Projection")
plt.ylabel(u"°C over pre-industrial (1850-1900 mean)")
# Run `plt.show()` to display the plot when running this example
# interactively or add `%matplotlib inline` on top when in a Jupyter Notebook.

For more example usage see this Jupyter Notebook. Thanks to the Binder project the Notebook can be run and modified without installing anything locally. A small interactive demo app using Jupyter Notebook's appmode extension is also available.

Installation

pip install pymagicc

On Linux and OS X the original compiled Windows binary available on http://www.magicc.org/ and included in Pymagicc can run using Wine.

On modern 64-bit systems one needs to use the 32-bit version of Wine

sudo dpkg --add-architecture i386
sudo apt-get install wine32

On 32-bit systems Debian/Ubuntu-based systems wine can be installed with

sudo apt-get install wine

On OS X wine is available in the Homebrew package manager:

brew install wine

It should also be available in other package managers, as well as directly from the Wine project.

Note that after the first install the first run of Pymagicc might be slow due to setting up of the wine configuration and be accompanied by pop-ups or debug output.

To run an example session using Jupyter Notebook and Python 3 you can run the following commands to create a virtual environment venv and install an editable version for local development:

git clone https://github.com/openclimatedata/pymagicc.git

cd pymagicc
make venv
./venv/bin/pip install -e .
./venv/bin/jupyter-notebook notebooks/Example.ipynb

Development

For local development run

make venv
./venv/bin/pip install --editable .

inside of a clone or download of the Pymagicc repository to install dependencies and an editable version of Pymagicc.

To run the tests run

./venv/bin/pytest tests --verbose

To skip tests which run MAGICC and take longer use

./venv/bin/pytest tests --skip-slow

To get a test coverage report, run

./venv/bin/pytest --cov

More Usage Examples

Use an included scenario

from pymagicc import rcp3pd

rcp3pd["WORLD"].head()

Read a MAGICC scenario file

from pymagicc import read_scen_file

scenario = read_scen_file("PATHWAY.SCEN")

Create a new scenario

Pymagicc uses Pandas DataFrames to represent scenarios. Dictionaries are used for scenarios with multiple regions.

import pandas as pd

scenario = pd.DataFrame({
    "FossilCO2": [8, 10, 9],
    "OtherCO2": [1.2, 1.1, 1.2],
    "CH4": [300, 250, 200]},
    index=[2010, 2020, 2030]
)

Run MAGICC for a scenario

output = pymagicc.run(scenario)

# Projected temperature adjusted to pre-industrial mean
temp = (output["SURFACE_TEMP"].GLOBAL -
        output["SURFACE_TEMP"].loc[1850:2100].GLOBAL.mean())

Using a different MAGICC version

The _magiccpath and _magiccbinary can be changed to point to different MAGICC development versions.

pymagicc._magiccpath = "/home/robert/openclimatedata/pymagicc/MAGICC_test/"
pymagicc._magiccbinary = "./magicc"

If an environment variable MAGICC_EXECUTABLE pointing to a MAGICC binary is set, _magiccpath and _magiccbinary will use the values set there, making it easy to point to different set of MAGICC files e.g. for testing.

Example usage in Bash:

MAGICC_EXECUTABLE=/tmp/magicc/magicc.exe python run_tests.py

Or in a script:

#!/bin/bash
export MAGICC_EXECUTABLE=/tmp/magicc/magicc.exe
python run_tests.py
python generate_plots.py

API

pymagicc

read_scen_file

read_scen_file(scen_file)

Reads a MAGICC .SCEN file and returns a a dictionary of DataFrames or, for World Only scenarios, a DataFrame.

run

run(scenario, output_dir=None, return_config=False, **kwargs)

Return output data and (optionally) used parameters from a MAGICC run.

Parameters
output_dir:
    Path for MAGICC data and binary, if None a temp file which will be
    deleted automatically.
return_config:
    Additionaly return the full list of parameters used. default False
kwargs:
    Parameters overwriting default parameters.
Returns
output: dict
    Dictionary with all data from MAGICC output files.
parameters: dict
    Parameters used in the MAGICC run. Only returned when
    ``return_config`` is set to True

write_scen_file

write_scen_file(scenario, path_or_buf=None, description1=None, description2=None, comment=None)

Write a Dictionary of DataFrames or DataFrame to a MAGICC .SCEN-file.

Parameters
scenario: DataFrame or Dict of DataFrames
    DataFrame (for scenarios with only the World region) or Dictionary with
    regions.
path_or_buf:
    Pathname or file-like object to write the scenario to.
description_1:
    Optional description line.
description_2:
    Optional second description line.
comment:
    Optional comment at end of scenario file.

Contributing

Please report issues or discuss feature requests on Pymagicc's issue tracker.

You can also contact the pymagicc authors via email [email protected].

License

The compiled MAGICC binary by Tom Wigley, Sarah Raper, and Malte Meinshausen included in this package is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

The pymagicc wrapper is free software under the GNU Affero General Public License v3, see LICENSE.

If you make any use of MAGICC, please cite:

M. Meinshausen, S. C. B. Raper and T. M. L. Wigley (2011). "Emulating coupled atmosphere-ocean and carbon cycle models with a simpler model, MAGICC6: Part I "Model Description and Calibration." Atmospheric Chemistry and Physics 11: 1417-1456. doi:10.5194/acp-11-1417-2011

See also the MAGICC website and Wiki for further information.

pymagicc's People

Contributors

rgieseke avatar lewisjared avatar swillner avatar znicholls avatar matthiasmengel avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.