Metadata-Version: 2.4
Name: masharikiweather
Version: 0.2.9
Summary: A geostatistical extraction and alignment engine for African weather and satellite data.
Author: Teofilo Ligawa, Samuel Mbugua, James Muiru, Hilda Muthoni, Ciira wa Maina
Maintainer: DSAIL, Dedan Kimathi University of Technology
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: numpy>=1.24.0
Requires-Dist: pandas>=2.0.0
Requires-Dist: xarray>=2023.0.0
Requires-Dist: dask>=2023.10.0
Requires-Dist: distributed>=2023.10.0
Requires-Dist: fsspec==2025.3.0
Requires-Dist: huggingface_hub>=0.19.0
Requires-Dist: tqdm>=4.65.0
Requires-Dist: zarr>=3.0.0
Requires-Dist: netCDF4>=1.6.4
Requires-Dist: h5netcdf>=1.2.0
Requires-Dist: pyarrow>=14.0.0
Requires-Dist: fastparquet>=2023.10.0
Requires-Dist: h5py>=3.12.1
Requires-Dist: scipy>=1.11.0
Requires-Dist: filter-stations>=0.7.1
Requires-Dist: pytest>=8.0.0
Requires-Dist: scikit-learn>=1.4.2
Requires-Dist: dagster>=1.6.0
Requires-Dist: obstore>=0.10.0
Requires-Dist: gcsfs>=2023.9.0
Requires-Dist: cdsapi>=0.7.0
Requires-Dist: earthaccess>=0.9.0
Requires-Dist: rioxarray>=0.15.0
Requires-Dist: python-dotenv>=1.0.0
Requires-Dist: requests>=2.28.0
Provides-Extra: ml
Requires-Dist: torch>=2.0.0; extra == "ml"
Requires-Dist: torch-geometric>=2.5.0; extra == "ml"
Requires-Dist: torchvision; extra == "ml"
Provides-Extra: docs
Requires-Dist: sphinx>=8.0.0; extra == "docs"
Requires-Dist: sphinx-rtd-theme>=2.0.0; extra == "docs"
Requires-Dist: myst-parser>=3.0.0; extra == "docs"
Provides-Extra: arco
Requires-Dist: obstore>=0.10.0; extra == "arco"
Requires-Dist: gcsfs>=2023.9.0; extra == "arco"
Provides-Extra: extract
Requires-Dist: cdsapi>=0.7.0; extra == "extract"
Requires-Dist: earthaccess>=0.9.0; extra == "extract"
Requires-Dist: rioxarray>=0.15.0; extra == "extract"
Requires-Dist: requests>=2.28.0; extra == "extract"
Requires-Dist: python-dotenv>=1.0.0; extra == "extract"
Provides-Extra: all
Requires-Dist: masharikiweather[arco,docs,extract,ml]; extra == "all"

# MasharikiWeather

## Overview

**MasharikiWeather** is an open initiative to create an **ML-ready, framework-agnostic weather dataset for Africa**.  
It draws inspiration from the [**PeakWeather**](https://huggingface.co/datasets/MeteoSwiss/PeakWeather) project — an integrated, harmonized, and machine-learning–ready climate dataset.

The goal is to study and adapt PeakWeather's design philosophy to **African data realities**, such as sparse ground station observations, multimodal data sources, and irregular spatiotemporal grids.

Ultimately, MasharikiWeather provides a **multi-variable benchmark dataset** supporting **physics-based**, **AI-based**, and **hybrid forecasting** pipelines across frameworks including **PyTorch**, **TensorFlow**, **JAX**, and **NumPy**.

---

## Authors

Developed at the **Centre for Data Science and Artificial Intelligence (DSAIL)**, Dedan Kimathi University of Technology:
- Teofilo Ligawa
- Samuel Mbugua
- James Muiru
- Hilda Muthoni
- Ciira wa Maina

---

## Documentation

Full API references, data models, architecture, and user guides are available in [docs/](docs/).

To build and view documentation locally:
```bash
pip install -e .[docs]
sphinx-build -b html docs docs/_build/html
python -m http.server 8080 --directory docs/_build/html
```

---

## Usage

Create and activate a Python virtual environment:

```bash
python -m venv .venv
source .venv/bin/activate
```

Install the package:

```bash
pip install masharikiweather
```

### Authentication (Bring Your Own Key)

MasharikiWeather adheres to a Bring-Your-Own-Key (BYOK) architecture. External API keys can be passed directly via arguments, environment variables (`TAHMO_API_KEY`, `CDSAPI_KEY`, `EARTHDATA_USERNAME`), standard configuration files (`~/.cdsapirc`, `~/.netrc`), or local `.env` files.

For account creation and credential setup, consult the official documentation:
- Copernicus Climate Data Store: [Copernicus CDS User Guide](https://cds.climate.copernicus.eu/user-guide)
- NASA Earthdata / IMERG: [NASA earthaccess Documentation](https://earthaccess.readthedocs.io/en/stable/)

If reading from online Hugging Face stores, you can authenticate via the CLI (`huggingface-cli login`) or pass a token to the dataset constructor.

### Data Extraction

Before initializing `MasharikiWeatherDataset`, pull station observations and gridded fields to local storage (`./mashariki-data`). You do not need to know station codes in advance; extractors discover stations automatically within a bounding box (`EAST_AFRICA_BBOX`, `KENYA_BBOX`, or `AFRICA_BBOX`).

```python
from masharikiweather import pull_weather_data
from masharikiweather.extract.apis.base import EAST_AFRICA_BBOX
from masharikiweather.extract.apis.tahmo import TAHMOExtractor

# Option A: Unified multi-source pull (stations + gridded reanalysis)
pull_weather_data(
    start_date="2023-01-01",
    end_date="2023-06-30",
    bbox=EAST_AFRICA_BBOX,
    station_source=["tahmo", "ghcn"],
    gridded_source=["era5"],
    output_dir="./mashariki-data",
)

# Option B: Direct TAHMO extraction with concurrent workers
# Writes semi-wide yearly Parquet ({year}.parquet) by default and optional ordinal-slot Zarr
tahmo = TAHMOExtractor(output_dir="./mashariki-data/obs/tahmo")
tahmo.extract(
    start_date="2023-01-01",
    end_date="2023-06-30",
    bbox=EAST_AFRICA_BBOX,
    save_parquet=True,   # writes ./mashariki-data/obs/tahmo/2023.parquet
    save_zarr=False,     # set True to also write an ordinal-slot Zarr store
    generate_metadata=True,
)
```

Extraction populates `./mashariki-data/obs/{source}/{year}.parquet`, `./mashariki-data/gridded/{source}/`, and `./mashariki-data/obs/{source}/metadata/{installation,parameters}.parquet`, which `MasharikiWeatherDataset` loads directly.

### Quickstart

```python
from masharikiweather import MasharikiWeatherDataset, pull_weather_data
import matplotlib.pyplot as plt

# 1. Initialize dataset with local storage and physical temporal frequency
dataset = MasharikiWeatherDataset(
    root="./mashariki-data",
    source_obs=["tahmo", "ghcn"],
    years=[2023, 2024],
    freq="h",
    aggregation_methods={"precipitation": "sum", "temperature": "mean"},
)

# 2. Retrieve separated observation strands with consistent DatetimeIndex and MultiIndex
df_tahmo, mask_tahmo = dataset.get_tahmo_observations(
    stations=["TA00001", "TA00016"],
    parameters=["temperature", "precipitation"],
    first_date="2023-01-01",
    last_date="2023-01-07",
    return_mask=True,
)

df_ghcn = dataset.get_ghcn_observations(
    parameters=["temperature"],
    first_date="2023-01-01",
    last_date="2023-01-07",
)

# 3. Synchronize strands for cross-analysis
aligned = dataset.align_observations({"tahmo": df_tahmo, "ghcn": df_ghcn})

# 4. Extract Gridded Satellite/Reanalysis context at station locations
gridded = dataset.get_gridded_for_stations(
    groups=["era5"],
    stations=["TA00001", "TA00016"],
    variables=["total_precipitation"],
    method_overrides={"total_precipitation": "linear"},
)
print(gridded["era5"].head())
```

## Objectives

1. **Establish a standardized meteorological pipeline**
   - Explore dataset schema, preprocessing design, and data fusion principles.
2. **Develop an Africa-centered multi-source fusion framework**
   - Harmonize station, reanalysis, satellite, and static prior datasets in a unified structure.
3. **Build a benchmark-ready, multi-variable dataset**
   - Include precipitation, temperature, humidity, solar radiation, wind, and other atmospheric variables.
4. **Enable framework-agnostic ML integration**
   - Support export and loading across ML frameworks using formats like **Zarr**, **NetCDF**, and **HDF5**.
5. **Advance African climate AI infrastructure**
   - Provide standardized, transparent, and reproducible weather datasets tailored to African needs.

---

## Core Concept

Africa's meteorological landscape is characterized by:
- Sparse ground observations (TAHMO, GHCNd, CDS In-Situ).  
- Diverse gridded data products (ERA5, CHIRPS, TAMSAT, IMERG).  
- Static surface properties that influence local weather (elevation, slope, aspect, land cover).  
- Spatial and temporal inconsistencies across sources.  

MasharikiWeather addresses these challenges through:
- **Spatiotemporal Graph Learning** connecting stations, satellite, and reanalysis data.  
- **Integration of static priors** to capture topographic and land-surface context.  
- **Unified variable alignment** for consistent modeling inputs.  
- **Multi-scale representation**, enabling both local and continental evaluation.  
- **ML-ready exports**, providing compatibility across major deep learning frameworks.

---

## Data Sources

| Source | Type | Coverage | Variables | Role |
|--------|------|-----------|------------|------|
| **TAHMO** | In-situ (stations) | Sub-Saharan Africa | Precipitation, Temperature, Solar Radiation, Relative Humidity | TAHMO ground observations |
| **CDS In-Situ** | In-situ (stations) | Global / Africa | Surface temperature, pressure, wind, humidity | WMO ground observations |
| **GHCN (NOAA)** | In-situ (stations) | Global / Africa | Daily and hourly precipitation, max/min temperature | Climatological reference |
| **ERA5** | Reanalysis | Global | Full atmospheric suite (surface & pressure levels) | Baseline physics |
| **IMERG** | Satellite | Global | Precipitation (half-hourly, V07 Final & V08 Late) | High-resolution rainfall |
| **CAMS** | Reanalysis | Global | Aerosols, PM2.5, PM10, reactive gases | Atmospheric composition |
| **MERRA-2** | Reanalysis | Global | Meteorology, aerosol optical depth | NASA atmospheric baseline |
| **Open-Meteo** | Reanalysis / NWP | Global | Multimodel weather parameters | Low-latency queries |
| **NASA POWER** | Satellite / Reanalysis | Global | Solar irradiance, meteorology | Surface solar and agroclimatology |
| **PVGIS** | Satellite / Solar | Europe, Africa | Solar irradiance, PV performance | Photovoltaic modeling |
| **SARAH-3** | Satellite | Europe, Africa | Surface solar irradiance, DNI | EUMETSAT solar reference |
| **LandSAF** | Satellite | Africa, Europe | Surface temperature, albedo, DSSF | Land surface processes |
| **TROPOMI** | Satellite | Global | NO2, CO, CH4, O3, SO2 | Sentinel-5P trace gas monitoring |
| **Static Priors (EE)** | Earth Engine Layers | Africa | Elevation, Slope, Aspect, Land Cover, Distance to Water | Geophysical context |

---

## Project Structure & Architecture

The core package logic is modularized inside `masharikiweather/`:

### Core Data & Preprocessing (`masharikiweather/`)
- **`dataset.py`**: User-facing dataset interface. Implements `MasharikiWeatherDataset`, separated observation accessors (`get_tahmo_observations`, `get_cds_insitu_observations`, `get_ghcn_observations`), cross-strand alignment (`align_observations`), metadata catalogs (`installation.parquet`, `parameters.parquet`), and high-level extraction entrypoints (`pull_station_data`, `pull_gridded_data`, `pull_weather_data`).
- **`extract/apis/`**: Multi-source API extraction layer (TAHMO, GHCN, CDS In-Situ, ERA5, IMERG, CAMS, MERRA-2, Open-Meteo, NASA POWER, PVGIS, SARAH-3, LandSAF, TROPOMI), Dagster assets, custom IO managers (`ParquetStationIOManager`, `ZarrGriddedIOManager`), and Bring-Your-Own-Key resolution (`CredentialsManager`).
- **`rechunker.py`**: Dask and Zarr chunking optimizations.

---

## Roadmap & Design Alignment

| Design Objective | MasharikiWeather Adaptation |
|------------------|-----------------------------|
| Global ML-ready weather dataset | African-focused ML-ready dataset |
| Harmonized across ERA5, GFS, and observations | Fusion of TAHMO, GHCN, CDS In-Situ, ERA5, IMERG, static priors |
| Precipitation-focused benchmarking | Multi-variable (precip, temp, humidity, radiation, topography) |
| Cloud-scale Zarr exports | Cloud and local exports via Zarr, NetCDF, and Parquet |
| Open and reproducible ML access | Reproducible African weather research |

---

## Phased Roadmap

### Phase 1 — Exploration & Schema Study
- Study open meteorological schemas, documentation, and data loaders.
- Analyze variable harmonization and metadata organization.

### Phase 2 — MasharikiWeather Schema Design
- Define temporal resolution and coordinate references (lat/lon/time).
- Standardize variable names and CF-compliant metadata.

### Phase 3 — TAHMO + ERA5 Integration
- Align station-based and gridded data through interpolation.
- Preserve collocated sensor channels without destructive averaging.
- Store as unified datasets with metadata catalogs.

### Phase 4 — Multi-source Expansion
- Integrate TAHMO, CDS In-Situ, GHCN, ERA5, IMERG, CAMS, MERRA-2, Open-Meteo, NASA POWER, PVGIS, SARAH-3, LandSAF, and TROPOMI.
- Evaluate inter-product correlations and consistency.

### Phase 5 — Integrate Static Priors
- Merge static features (elevation, slope, aspect, land cover, distance to water).
- Cyclically encode aspect angles to prevent angular boundary discontinuities.

### Phase 6 — ML-Ready Export
- Export standardized datasets to Zarr, NetCDF, and Parquet.
- Provide data loaders for PyTorch Geometric, TensorFlow, and JAX.

### Phase 7 — Benchmark & Evaluation
- Implement baseline models using standardized spatiotemporal workflows.
- Compare performance across variables and African regions.

---

## Guiding Principles

- **Reproducibility** — Version-controlled, scriptable data processing.  
- **Transparency** — Clear documentation for transformation steps.  
- **Scalability** — Built for batch workflows (Dask, Zarr, Parquet).  
- **Inclusivity** — Designed around African data realities.  
- **Framework-agnosticism** — ML-ready for PyTorch, TensorFlow, JAX, and NumPy.

---

## Contributing
We welcome contributions from the DSAIL team and the broader scientific community. Please review our [Contribution Guidelines](CONTRIBUTING.md) before submitting pull requests.

## Credits & Affiliation
Developed at the **Centre for Data Science and Artificial Intelligence (DSAIL)**, Dedan Kimathi University of Technology, inspired by **[PeakWeather](https://arxiv.org/abs/2506.13652)** and **[WeatherBench2](https://sites.research.google/gr/weatherbench/)**.

MasharikiWeather is a contribution toward open, harmonized climate AI infrastructure for Africa.
