Skip to main content

A modern Python client library for accessing Geological Survey of Sweden groundwater data APIs with type safety and pandas integration

Project description

SGU Client

A modern Python client library for accessing Geological Survey of Sweden (SGU) groundwater data APIs with type safety and pandas integration.

Python Version PyPI version License: MIT codecov Code style: ruff

This package is not affiliated with or endorsed by SGU.

Features

  • Type-safe: Full type hints with Pydantic validation
  • Pandas integration: Convert data to DataFrames or Series with optional pandas dependency
  • Friendly shortcuts: Convenience functions to access stations by names, modeled levels by coordinates, and much more.

Get going with your analysis in just a few lines of code:

from sgu_client import SGUClient

with SGUClient() as client:
    measurements = client.levels.observed.get_measurements_by_name(platsbeteckning="95_2")
    df = measurements.to_dataframe()

# ... rest of your awesome workflow

Installation

Using uv

# basic installation
uv add sgu-client

# with pandas support for dataframe conversion
uv add "sgu-client[recommended]"

... or using pip

# basic installation
pip install sgu-client

# with pandas support for dataframe conversion
pip install "sgu-client[recommended]"

Usage

Initializing a client

from sgu_client import SGUClient

client = SGUClient()

# with custom configuration
from sgu_client import SGUConfig
config = SGUConfig(timeout=60, debug=True, max_retries=5)
client = SGUClient(config=config)

# as a context manager
with SGUClient() as client:
    stations = client.levels.observed.get_stations(limit=10)

Observed groundwater levels

Access real-time and historical groundwater monitoring data from SGU's network of observation stations.

from sgu_client import SGUClient
from datetime import UTC, datetime


with SGUClient() as client:
    # basic API endpoint usage
    stations = client.levels.observed.get_stations(limit=50)
    
    # with OGC API filters, like bbox of southern Sweden
    stations = client.levels.observed.get_stations(
        bbox=[12.0, 55.0, 16.0, 58.0],
        limit=100
    )
    
    # convenience function to get station by name 
    station = client.levels.observed.get_station_by_name(
        platsbeteckning="95_2"  # or obsplatsnamn="95_2"
    )
    # or multiple stations by names
    stations = client.levels.observed.get_stations_by_names(
        platsbeteckning=["95_2", "101_1"]  # or obsplatsnamn=["Lagga_2", ...]
    )

    # convenience function to get measurements by station name
    measurements = client.levels.observed.get_measurements_by_name(
        platsbeteckning="95_2",  # or obsplatsnamn="95_2"
        limit=100
    )
    # or multiple stations by names
    measurements = client.levels.observed.get_measurements_by_names(
        platsbeteckning=["95_2", "101_1"],  # or obsplatsnamn=["Lagga_2", ...]
        limit=100
    )

    # filter by datetime for faster responses
    tmin = datetime(2020, 1, 1, tzinfo=UTC)
    tmax = datetime(2021, 1, 1, tzinfo=UTC)
    measurements = client.levels.observed.get_measurements_by_names(
        platsbeteckning=["95_2"], tmin=tmin, tmax=tmax, limit=10
    )
    
    # responses that create lists of features can be converted to pandas DataFrames
    measurements = client.levels.observed.get_measurements_by_names(
        platsbeteckning=["95_2", "101_1"],  # or obsplatsnamn=["Lagga_2", ...]
        limit=100
    )
    df = measurements.to_dataframe()  
    # or series
    series = measurements.to_series()  # defaults to 'grundvattenniva_m_o_h' (head) column with datetime index

Modeled groundwater levels

Access modeled groundwater levels from SGU-HYPE.

from sgu_client import SGUClient

with SGUClient() as client:
    # basic API endpoint usage
    areas = client.levels.modeled.get_areas(limit=10)
    
    # with OGC API filters, like bbox of southern Sweden
    areas = client.levels.modeled.get_areas(
        bbox=[12.0, 55.0, 16.0, 58.0],
        limit=20
    )
    
    # get a specific area by ID
    area = client.levels.modeled.get_area("omraden.30125")
    print(f"Area ID: {area.properties.omrade_id}")
    print(f"Geometry: {area.geometry.type}")
    
    # convenience function to get levels for a specific area
    levels = client.levels.modeled.get_levels_by_area(30125, limit=10)
    # or multiple areas by IDs
    levels = client.levels.modeled.get_levels_by_areas(
        area_ids=[30125, 30126],
        limit=50
    )

    # convenience function to get levels by coordinates
    levels = client.levels.modeled.get_levels_by_coords(
        lat=57.7089,
        lon=11.9746,
        limit=10
    )
    
    # responses that create lists of features can be converted to pandas DataFrames
    levels = client.levels.modeled.get_levels_by_areas(
        area_ids=[30125, 30126],
        limit=50
    )
    df = levels.to_dataframe()
    # or series
    series = levels.to_series()  # defaults to 'fyllnadsgrad_sma' (relative level, small resources) column with datetime index

Working with Typed Data

All responses are fully typed with Pydantic models:

from sgu_client import SGUClient

client = SGUClient()

# Get stations with full type safety
stations = client.levels.observed.get_stations(limit=5)

for station in stations.features:
    # All properties are typed and documented
    print(f"Station: {station.properties.obsplatsnamn}")
    print(f"Municipality: {station.properties.kommun}")
    print(f"Coordinates: {station.geometry.coordinates}")

# Get measurements with automatic datetime parsing
measurements = client.levels.observed.get_measurements(limit=5)
for measurement in measurements.features:
    props = measurement.properties
    print(f"Date: {props.observation_date}")  # Parsed datetime object
    print(f"Level: {props.grundvattenniva_m_o_h} m above sea level")

API Reference

SGUClient

The main client class providing access to all SGU APIs.

  • levels.observed - Observed groundwater level measurements
  • levels.modeled - Modeled groundwater levels from SGU-HYPE

Configuration

from sgu_client import SGUConfig

config = SGUConfig(
    timeout=30,        # Request timeout in seconds
    max_retries=3,     # Maximum retry attempts
    debug=False        # Enable debug logging
)

Development

We recommend using uv for development:

# Clone the repository
git clone https://github.com/officialankan/sgu-client.git
cd sgu-client

# Install dependencies and sync environment
uv sync --all-extras

# Run tests
uv run pytest

# Format and lint code
uv run ruff format
uv run ruff check --fix

Release Process

To release a new version:

  1. Create PR with your changes + version bump in pyproject.toml
  2. PR will be tested and published to TestPyPI for verification
  3. Once merged, the new version is automatically published to PyPI

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Roadmap

  • Initial release with observed and modeled groundwater levels
  • Add example notebooks and tutorials
  • Add support for groundwater quality API
  • Add support for geological data API

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

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

sgu_client-0.2.1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

sgu_client-0.2.1-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file sgu_client-0.2.1.tar.gz.

File metadata

  • Download URL: sgu_client-0.2.1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sgu_client-0.2.1.tar.gz
Algorithm Hash digest
SHA256 77d199155cff7431f13e7db08b6d385f9639497de4bf7f858ea5d9f36750756e
MD5 4b13a0353b5bd9cff8a010aaec2b6e59
BLAKE2b-256 8953999be46d02f800eebafaf0c6ff0cc329d15cbc0d24bd3a788004d055241a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgu_client-0.2.1.tar.gz:

Publisher: ci-cd.yml on officialankan/sgu-client

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

File details

Details for the file sgu_client-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: sgu_client-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sgu_client-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9da3f5538c23d198c703affc6a19a339d7cf648000d63523336c86e54d993cc3
MD5 76bd3a00a4506f8917e558f5f7a713f7
BLAKE2b-256 b89b1e8e17404ce54400d597d09d5afc96585912aa021f79d544a134aaf92aff

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgu_client-0.2.1-py3-none-any.whl:

Publisher: ci-cd.yml on officialankan/sgu-client

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