pyhector

Build Status Codecov
PyPI Version PyPI Python Versions
Docs Launch Binder
JOSS Zenodo

pyhector is a Python interface for the simple global climate carbon-cycle model Hector.

pyhector makes the simple climate model Hector easily installable and usable from Python and can for example be used in the analysis of mitigation scenarios, in integrated assessment models, complex climate model emulation, and uncertainty analyses.

Hector is written in C++ and developed at the Pacific Northwest National Laboratory. The model description is published in

Hartin, C. A., Patel, P., Schwarber, A., Link, R. P., and Bond-Lamberty, B. P.: A simple object-oriented and open-source model for scientific and policy analyses of the global climate system – Hector v1.0, Geosci. Model Dev., 8, 939-955, doi:10.5194/gmd-8-939-2015, 2015.

See the Hector repository and wiki for further information.

The Python interface pyhector is developed by Sven Willner and Robert Gieseke at the Potsdam Institute for Climate Impact Research.

It is currently based on a slightly modified Hector version and a generic wrapper for Hector’s API. See the included git submodules for the specific commits that are being used.

Installation

Prerequisites

Hector requires Boost, so to install and use pyhector you need to have the filesystem and system modules of Boost version 1.52 or later installed (see also the Hector build instructions).

On Ubuntu/Debian these can be installed by invoking

sudo apt-get install libboost-filesystem-dev libboost-system-dev

On macOS Boost is available through the Homebrew package manager, it might be advisable to use a Homebrew installed Python for installing pyhector:

brew install boost

Windows is (as Hector) in principle supported but not yet tested for pyhector. Pull request with installation notes for Windows are welcome.

Install using pip

You can simply install pyhector from PyPI by invoking

pip install pyhector

Usage

This repository also contains a Jupyter Notebook you can run live and experiment with, courtesy of the Binder project (Status). The notebook can be viewed as a static version using nbviewer.

Basic example

import pyhector

output = pyhector.run(pyhector.rcp26)

Advanced example

import pyhector
from pyhector import rcp26, rcp45, rcp60, rcp85

import matplotlib.pyplot as plt

for rcp in [rcp26, rcp45, rcp60, rcp85]:
    output = pyhector.run(rcp, {"core": {"endDate": 2100}})
    temp = output["temperature.Tgav"]
    # Adjust to 1850 - 1900 reference period
    temp = temp.loc[1850:] - temp.loc[1850:1900].mean()
    temp.plot(label=rcp.name.split("_")[0])
plt.title("Global mean temperature")
plt.ylabel("°C over pre-industrial (1850-1900 mean)")
plt.legend(loc="best")
plt.show()
Temperature Plot of RCP scenarios

Development

For local development you can clone the repository, update the dependencies and install in a virtual environment with pip.

git clone https://github.com/openclimatedata/pyhector.git --recursive
cd pyhector
python3 -m venv venv
./venv/bin/pip install --editable .

To update pyhector and all submodules you can run

git pull --recurse-submodules
git submodule update --init --recursive
./venv/bin/pip install --editable .

Tests can be run locally with

python setup.py test

Changelog

0.7.0

  • overhauled docs to include tables for configuration dicts
  • fixed start_date bug when not setting observables

0.6.0

  • explicitly state C++11 in setup.py
  • enable spinup output to be readable

0.5.2

  • config dictionary can also take a Pandas series instead of list of tuples for time series
  • add function to export scenarios as CSV files (in Hector format)
  • add API docs using Sphinx and Readthedocs

0.4.0

  • return parameters only when requested in run function
  • allow different configuration objects to be used

0.3.0

  • default config object uses Python numbers or booleans instead of strings, units can be included as tuples like (35.0, 'pptv') and time series as lists of tuples like 'N2ON_emissions': [(1765, 11), (2000, 8), (2300, 8)]

0.2.4

  • first PyPI beta release