# ajkhattak/nextgensandboxhub architecture

> Architecture diagram of the GitHub repository ajkhattak/nextgensandboxhub, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ajkhattak/nextgensandboxhub
- Repository: https://github.com/ajkhattak/nextgensandboxhub

## Overview

##### Project type and purpose
- This is an open-source workflow orchestration tool (NextGenSandboxHub) for setting up, configuring, and running hydrologic model simulations (NextGen, t-route) over sub-catchments.
- It ties together R scripts for hydrofabric processing, Python modules for configuration and execution, and external binaries/plugins (ngen, t-route, CIROH_DL_NextGen).

##### File structure
- Top-level orchestrator: `sandbox.py` (CLI entry point)
- Configuration directory: `configs/` (YAML templates for basefiles, sandbox, calibration, workflows)
- R processing modules: `src/R/` (data subsetting, parameter computation, plotting, main driver)
- Python orchestration modules: `src/python/` (baseline, configuration, driver, forcing, generate, helper, realization, runner, schema, validation)
- External dependencies/plugins: `extern/CIROH_DL_NextGen`, `extern/ngen-cal`, `extern/ngen_cal_plugins`
- Utility scripts: `utils/` (build sandbox, build models, S3 download, environment setup)
- Meta files: `.gitignore`, `README.md`, `LICENSE`, build scripts, submodule pointers

##### README insights
- Workflow steps (subset hydrofabric, download forcing, generate config/realization, run simulations)
- Relies on two environments: R (via Rscript or RStudio) and Python virtualenv (`.venv_forcing`, sandbox venv)
- External tool integrations: CIROH_DL_NextGen for atmospheric data, ngen + t-route for model execution
- Plugin pattern for calibration (`ngen_cal_plugins`)

##### Main components
a. CLI / Workflow Orchestrator
- `sandbox.py` (entry point handling flags: –subset, –forc, –conf, –run)
b. Configuration Management
- YAML files in `configs/` drive every step
c. R Processing Engine
- Scripts in `src/R/` invoked via Rscript or within RStudio
- Outputs: sub-catchment geopackages with computed parameters (TWI, GIUH, Nash cascade)
d. Python Processing Engine
- Modules in `src/python/` manage forcing download, configuration generation, realization assembly, validation, and runner logic
e. External Tools & Plugins
- CIROH_DL_NextGen for forcing data (invoked in its own venv)
- `ngen` core library and `t-route` binary for simulation execution
- Calibration plugins under `extern/ngen_cal_plugins` (strategy/plugin pattern)
f. Data Storage & Flow
- Input: hydrofabric geopackages (local or S3), forcing archives
- Intermediate: generated config JSON/YAML, realization files
- Output: simulation results, calibrated parameter sets

##### Relationships & interactions
- CLI → reads `configs/sandbox_config.yaml` → invokes R stage (subsetting) → writes geopackages → invokes Python forcing module (CIROH_DL) → fetches data → Python config generator → writes model config & realization → Python runner module → calls external `ngen` + `t-route` → reads/writes output directories
- Plugin loader in Python runner discovers and applies calibration plugins from `extern/ngen_cal_plugins`
- Utility scripts (`utils/`) support environment and dependency setup but aren’t part of the runtime pipeline

##### Architectural patterns & design principles
- Modular pipeline stages (subsetting, forcing, config, run) separated by flag-driven CLI
- Strategy/plugin pattern for calibration (discoverable plugins under a common interface)
- Clear separation of concerns:
- Data preparation (R)
- Data ingestion & config (Python)
- Simulation execution (external binaries/libraries)
- Configuration-first design: all steps parameterized by YAML

##### Technologies & frameworks to annotate
- Python 3.x, virtualenv
- R (>=3.x) + Rscript
- CIROH_DL_NextGen (Python package)
- NGEN (Python library)
- t-route (Fortran/C++ binary)
- YAML, JSON for configs
- Plugin discovery (Python entry points or dynamic import)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Layer
    Configs["Configuration Files\n(configs/)"]:::config
    click Configs "https://github.com/ajkhattak/nextgensandboxhub/tree/main/configs/"

    %% CLI Layer
    CLI["CLI Orchestrator\nsandbox.py"]:::cli
    click CLI "https://github.com/ajkhattak/nextgensandboxhub/blob/main/sandbox.py"

    %% R Processing Stage
    subgraph "R Processing Engine" 
        direction TB
        REngine["R Scripts\nsrc/R/"]:::r
    end
    click REngine "https://github.com/ajkhattak/nextgensandboxhub/tree/main/src/R/"

    %% Python Processing Stage
    subgraph "Python Processing Engine"
        direction TB
        PyEngine["Python Modules\nsrc/python/"]:::python
    end
    click PyEngine "https://github.com/ajkhattak/nextgensandboxhub/tree/main/src/python/"

    %% External Tools
    subgraph "External Tools & Plugins"
        direction TB
        CIROH["CIROH_DL_NextGen"]:::external
        click CIROH "https://github.com/ajkhattak/nextgensandboxhub/tree/main/extern/CIROH_DL_NextGen/"
        NGEN["NGEN Core"]:::external
        click NGEN "https://github.com/ajkhattak/nextgensandboxhub/tree/main/extern/ngen-cal/"
        Plugins["Calibration Plugins\nextern/ngen_cal_plugins/"]:::external
        click Plugins "https://github.com/ajkhattak/nextgensandboxhub/tree/main/extern/ngen_cal_plugins/"
        TRoute["t-route Binary"]:::external
    end

    %% Utility Scripts
    Utils["Utility Scripts\n(utils/)"]:::utils
    click Utils "https://github.com/ajkhattak/nextgensandboxhub/tree/main/utils/"

    %% Data Stores
    InputGeo["Hydrofabric Geopackages\n(local/S3)"]:::data
    ForcingData["Forcing Data\n(NetCDF archives)"]:::data
    ConfigOut["Generated Config\n(JSON/YAML)"]:::data
    Realization["Realization Files"]:::data
    OutputSim["Simulation Results"]:::data

    %% Pipeline Flow
    Configs -->|reads YAML| CLI
    CLI -->|–subset| REngine
    REngine -->|geopackages| InputGeo
    InputGeo -->|used by config| PyEngine
    CLI -->|–forc| PyEngine
    PyEngine -->|download forcing| CIROH
    CIROH -->|forcing archives| ForcingData
    ForcingData -->|input to config| PyEngine
    PyEngine -->|generate configs| ConfigOut
    PyEngine -->|assemble realization| Realization
    PyEngine -->|load plugins| Plugins
    PyEngine -->|run simulation| NGEN
    PyEngine -->|invoke binary| TRoute
    NGEN -->|model execution| OutputSim
    TRoute -->|routing results| OutputSim

    %% Utility support (not in main pipeline)
    Utils -.-> CLI

    %% Styles
    classDef config fill:#FFF2CC,stroke:#E6B800,stroke-width:1px;
    classDef cli fill:#D0E6FF,stroke:#3399FF,stroke-width:1px;
    classDef r fill:#E2F0D9,stroke:#6AA84F,stroke-width:1px;
    classDef python fill:#F4CCCC,stroke:#CC0000,stroke-width:1px;
    classDef external fill:#E0E0E0,stroke:#7F7F7F,stroke-width:1px;
    classDef data fill:#FFE6E6,stroke:#CC0000,stroke-width:1px;
    classDef utils fill:#EAD1DC,stroke:#B45F06,stroke-width:1px;
```
