Skip to main content

Add your description here

Project description

ngiab-cal

A Python CLI tool to simplify hydrologic model calibration for NextGen In A Box (NGIAB) workflows.

Table of Contents

What is this?

ngiab-cal is a utility that works with the NGIAB folder structure. It automates the creation of a calibration directory with all necessary configuration files to run a modified version of ngen-cal.

The tool simplifies these key tasks:

  • Creating calibration configurations
  • Running the calibration process using Docker
  • Copying calibrated parameters back to your model configuration

Installation

Several installation options are available:

Using uvx (recommended)

# Run directly without installation
uvx ngiab-cal --help

# Or install as a tool
uv tool install ngiab-cal
ngiab-cal --help

Using pipx

pipx install ngiab-cal
ngiab-cal --help

Traditional pip installation

pip install ngiab-cal
python -m ngiab_cal --help

Requirements

  • Python 3.10+
  • Docker (for running calibrations)
  • Internet connection (for downloading USGS data)

Basic Usage

# Create calibration configuration
ngiab-cal /path/to/ngiab/data/folder -g USGS_GAGE_ID

# Create and run calibration (200 iterations)
ngiab-cal /path/to/ngiab/data/folder -g USGS_GAGE_ID --run -i 200

# Force recreation of calibration configuration
ngiab-cal /path/to/ngiab/data/folder -g USGS_GAGE_ID -f

Advanced Options

usage: ngiab-cal [-h] [-g GAGE] [-f] [--run] [-i ITERATIONS] [--debug] [-w WARMUP] [--calibration_ratio CALIBRATION_RATIO]
                  data_folder

Create a calibration config for ngen-cal

positional arguments:
  data_folder           Path to the folder you wish to calibrate

options:
  -h, --help            show this help message and exit
  -g GAGE, --gage GAGE  Gage ID to use for calibration
  -f, --force           Overwrite existing configuration
  --run                 Try to automatically run the calibration, this may be unstable
  -i ITERATIONS, --iterations ITERATIONS
                        Default:100 number of iterations to calibrate for

  --debug               enable debug logging
  -w WARMUP, --warmup WARMUP
                        Default:365
                        Number of days at the beginning of the simulation
                         to exclude from calibration objective metric calculation
  --calibration_ratio CALIBRATION_RATIO, --cr CALIBRATION_RATIO
                        Default:0.5
                        How to split time after warmup into calibration and validation.
                        1 == 100% calibration, 0 == 100% validation, 0.8 == 80% calibration 20% validation

Calibration Process

The tool applies a standard calibration workflow:

Default calibration settings on a 5 year period
|   year 1   |   year 2   |   year 3   |   year 4   |   year 5   |
|<- warmup ->|<-     calibration     ->|      no simulation      |
|<-             warmup               ->|<-      validation     ->|

Example: Calibrating CAMELS Basins

Here's a script to calibrate all CAMELS basins:

#!/bin/bash
# Download a list of CAMELS gage ids
wget https://raw.githubusercontent.com/peckhams/nextgen_basin_repo/5e1317256a9365ae3a24a250358314e1e9ffc339/CAMELS/Data/camels_name.txt ./camels_name.txt
output_folder=$(cat ~/.ngiab/preprocessor)
while read line
do
    gage=$(echo "$line" | cut -d ';' -f 1)
    echo $gage
    # subset the hydrofabric, calculate mean-average area forcings, generate model config files
    uvx --from ngiab_data_preprocess cli -i gage-"$gage" -sfr --start 2007-10-01 --end 2013-09-30
    # calibrate gage for 200 iterations
    uvx ngiab-cal "$output_folder"/gage-"$gage" -g "$gage" --run -i 200
done < <(tail -n +2 ./camels_name.txt)

Calibration Configuration File

ngiab-cal generates a configuration file at calibration/ngen_cal_conf.yaml that controls the calibration process. This file is created from a template with the following key sections:

General Configuration

general:
  strategy:
    type: estimation
    algorithm: dds         # Uses Dynamically Dimensioned Search algorithm
  name: calib              # Don't modify this
  log: true               # Enable logging
  workdir: /ngen/ngen/data/calibration  # Don't modify this working directory in the Docker container
  yaml_file: /ngen/ngen/data/calibration/ngen_cal_conf.yaml # Don't modify this either
  iterations: 100         # Number of calibration iterations (customizable with -i flag)
  restart: 0              # Start from beginning (0) or resume from iteration

Model Parameters

The file defines parameters for both CFE (Conceptual Functional Equivalent) and Noah-OWP-Modular (an extended, refactored version of the Noah-MP land surface model) hydrologic models:

CFE:
  - name: b               # CFE parameter name
    min: 2.0              # Minimum allowed value
    max: 15.0             # Maximum allowed value
    init: 4.05            # Initial value
  - name: satpsi
    min: 0.03
    max: 0.955
    init: 0.355
  # Additional parameters...

For each parameter, the configuration specifies:

  • name: Parameter identifier
  • min: Minimum allowed value during calibration
  • max: Maximum allowed value during calibration
  • init: Initial value

Evaluation Configuration

eval_params:
  objective: kge           # Kling-Gupta Efficiency as objective function
  evaluation_start: "..."  # Start time for calibration period
  evaluation_stop: "..."   # End time for calibration period
  valid_start_time: "..."  # Start time including warmup
  valid_end_time: "..."    # End time of simulation
  # Additional time parameters...
  basinID: 01646500        # USGS gage ID
  site_name: "USGS 01646500: "  # Label for plots

These time periods follow the calibration process diagram shown earlier, with separate periods for warmup, calibration, and validation.

How It Works

  1. Input Validation - Checks that all required files are present
  2. USGS Data Download - Retrieves observed streamflow for calibration
  3. Configuration Creation - Generates necessary files for ngen-cal
  4. Docker Execution - Runs the calibration in a containerized environment
  5. Parameter Extraction - Copies calibrated parameters back to your configuration

How is ngen-cal running?

The tool uses a custom branch of CIROH-UA/ngen-cal called ngiab_cal. This branch contains:

  • A modified version of ngen-cal
  • A Dockerfile that builds on top of the Next Gen In-A-Box container
  • Installation of ngen-cal inside the container

When you provide the --run argument, it downloads a pre-built Docker image (joshcu/ngiab-cal:demo) to run the calibration.

Limitations

ngiab-cal has several important limitations to be aware of:

  1. Limited Calibration Algorithms: Currently only configured to use the Dynamically Dimensioned Search (DDS) algorithm. Other algorithms available in the main ngen-cal branch are not supported.

  2. Limited Model Support: Only calibrates parameters for CFE (Conceptual Functional Equivalent) and NoahOWP-Modular hydrologic models. Other models in the NextGen framework are not currently supported.

  3. Single-Gage Calibration: Designed for calibrating single-basin models using one USGS streamgage. Multi-gage or multi-objective calibration is not supported.

  4. Custom ngen-cal Branch: Uses a modified version of ngen-cal from the ngiab_cal branch, which differs from the main branch. Features from newer releases of ngen-cal may not be available.

  5. Compatibility Concerns: The customized ngen-cal implementation may not be compatible with future changes to either ngen-cal or the NGIAB framework.

Development

Contributions welcome! Comprehensive development instructions coming soon

Visualisation

During calibration, plots will be created inside ./calibration/Output/Calibration_Run/ngen_*******_worker/Plot_Iteration/ After calibration, the tool will copy the validated parameters back into the root config folder which allows you to run ngiab with the guide script and use the teehr / tethys visualiser. More streamlined workflow coming soon.

License

"Software code created by U.S. Government employees is not subject to copyright in the United States (17 U.S.C. §105). The United States/Department of Commerce reserve all rights to seek and obtain copyright protection in countries other than the United States for Software authored in its entirety by the Department of Commerce. To this end, the Department of Commerce hereby grants to Recipient a royalty-free, nonexclusive license to use, copy, and create derivative works of the Software outside of the United States."

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

ngiab_cal-0.2.1.tar.gz (56.5 kB view details)

Uploaded Source

Built Distribution

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

ngiab_cal-0.2.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ngiab_cal-0.2.1.tar.gz
  • Upload date:
  • Size: 56.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for ngiab_cal-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b044f2c316258a514e5833ff6428e48310e1a82f802731b7a7d87be5b95053aa
MD5 08b4b8e2d11992fff7c1ee4c770de082
BLAKE2b-256 027c8a4f2b6e5e94eb8e80b9b7aa25e5ead042c7b7d3f9fc0639219949532890

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ngiab_cal-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for ngiab_cal-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2eb49a1035467ca9da229d81fcca062a5845acbdb140e278166214d672773ca
MD5 b30fba4611f0079ff11f5532bb95e299
BLAKE2b-256 59e9dcb9c11c9bfe936f9c8e7a2164a77bb48b8ff0c9af411bb21aceae4e5f38

See more details on using hashes here.

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