Skip to main content

Tools for importing GCAM output data

Project description

gcamreader logo

build docs lint DOI

gcamreader: a Python package for extracting content from GCAM databases

gcamreader provides functions for reading data from the output databases produced by GCAM.

Purpose

gcamreader was created to:

  • Use XML queries to extract data into a pandas DataFrame from a GCAM XML database.
  • Integrate GCAM with other Python packages.

Requirements

  • Python 3.11 or newer.
  • A Java runtime (JRE). gcamreader runs queries against local databases using a bundled copy of the GCAM ModelInterface, which requires Java. Remote queries use the BaseX REST API and also rely on a server running ModelInterface.

Installation

Set up a virtual environment

It is recommended to install gcamreader into an isolated virtual environment to avoid conflicts with other packages on your system.

Using the built-in venv module:

# Create a virtual environment in a directory named ".venv".
python -m venv .venv

# Activate it (macOS / Linux).
source .venv/bin/activate

# Activate it (Windows PowerShell).
.venv\Scripts\Activate.ps1

Alternatively, with conda:

conda create -n gcamreader python=3.11
conda activate gcamreader

Once the environment is activated, install gcamreader using one of the methods below. To leave the environment when you are finished, run deactivate (venv) or conda deactivate (conda).

Install the latest release from PyPI:

pip install gcamreader

Install the development version from source:

git clone https://github.com/JGCRI/gcamreader.git
cd gcamreader
pip install -e .

To work on the package, install the development and documentation extras:

pip install -e ".[dev,docs]"

Quickstart

Query a local database

import gcamreader

# Connect to a local GCAM database (the parent directory and database name).
conn = gcamreader.LocalDBConn("path/to/output", "database_basexdb")

# List the scenarios contained in the database.
scenarios = conn.listScenariosInDB()

# Parse a GCAM queries XML file and run the first query.
queries = gcamreader.parse_batch_query("Main_queries.xml")
df = conn.runQuery(queries[0])

Run many queries at once

import gcamreader

results = gcamreader.importdata(
    "path/to/output/database_basexdb",
    "Main_queries.xml",
)

# results is a dict keyed by query title, with DataFrame values.
for title, frame in results.items():
    print(title, None if frame is None else frame.shape)

Query a remote database

import gcamreader

conn = gcamreader.RemoteDBConn(
    dbfile="database_basexdb",
    username="user",
    password="secret",
    address="localhost",
    port=8984,
)
df = conn.runQuery(gcamreader.parse_batch_query("Main_queries.xml")[0])

Command line interface

gcamreader installs a gcamreader command for running the queries in a queries file and saving each result to a CSV file.

# Show the version.
gcamreader --version

# Query a local database and write CSV outputs to a directory.
gcamreader local \
    --database_path path/to/output/database_basexdb \
    --query_path Main_queries.xml \
    --output_path results/ \
    --force

# Query a remote BaseX server.
gcamreader remote \
    --username user \
    --database_name database_basexdb \
    --query_path Main_queries.xml \
    --output_path results/ \
    --hostname localhost \
    --port 8984

Use gcamreader --help, gcamreader local --help, or gcamreader remote --help for the full list of options.

Documentation

Full documentation, including the API reference, is available at https://jgcri.github.io/gcamreader/.

Contributing

Contributions are welcome. Please run the test suite and the linters before opening a pull request:

pytest
ruff check .
black --check .

Citation

If you use gcamreader in your work, please cite it using the DOI badge above.

License

gcamreader is released under the BSD 2-Clause License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gcamreader-1.5.0.tar.gz (13.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gcamreader-1.5.0-py3-none-any.whl (13.3 MB view details)

Uploaded Python 3

File details

Details for the file gcamreader-1.5.0.tar.gz.

File metadata

  • Download URL: gcamreader-1.5.0.tar.gz
  • Upload date:
  • Size: 13.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gcamreader-1.5.0.tar.gz
Algorithm Hash digest
SHA256 b91c0e1e98b50a847792092f4d5c72994dd91df93b1e93914ccb2181ccb1f517
MD5 a857174cd4a301306ea9abf5da409185
BLAKE2b-256 8f4830e3f4c97536e493a1423ea674e69872fd41da7bf87cd786130bfedffe3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcamreader-1.5.0.tar.gz:

Publisher: release.yml on JGCRI/gcamreader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gcamreader-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: gcamreader-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gcamreader-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf407a34950afa33526fe98e44b328682961e418934cdbec1f16822d41546773
MD5 3633896a42f14de5117dfb90c375fbc5
BLAKE2b-256 d4986fb7fff3e3ba4ab23a6117e4ad5e33a8664b841d48a8d2d8d3f7beab7722

See more details on using hashes here.

Provenance

The following attestation bundles were made for gcamreader-1.5.0-py3-none-any.whl:

Publisher: release.yml on JGCRI/gcamreader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page