API Reference¶
hms2cng exposes all public functions for programmatic use. Import from the package directly or from individual modules.
# Package-level imports
from hms2cng import (
export_basin_geometry, export_hms_results,
export_all_basin_geometry, export_all_results,
get_project_manifest, export_project_manifest, export_full_project,
DuckSession,
)
# Module-level imports
from hms2cng.project import get_project_manifest, export_project_manifest, export_full_project
from hms2cng.geometry import export_basin_geometry, get_basin_layer_gdf, export_all_basin_geometry
from hms2cng.results import export_hms_results, export_all_results
from hms2cng.duckdb_session import DuckSession, query_parquet, spatial_join
from hms2cng.pmtiles import generate_pmtiles_from_input
from hms2cng.postgis_sync import sync_to_postgres
hms2cng.project¶
hms2cng.project.get_project_manifest(hms_file)
¶
Return a dict with project-level metadata (suitable for a 1-row DataFrame).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hms_file
|
Path
|
Path to the .hms project file or project directory. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys: project_name, project_file, hms_version, crs_epsg, |
dict
|
is_gridded, num_basin_models, num_met_models, num_control_specs, |
dict
|
num_runs, basin_models, met_models, run_names, export_timestamp. |
dict
|
The list fields (basin_models, met_models, run_names) are JSON strings. |
Source code in hms2cng/project.py
hms2cng.project.export_project_manifest(hms_file, output_dir)
¶
Write manifest.parquet, run_registry.parquet, and basin_inventory.parquet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hms_file
|
Path
|
Path to the .hms project file or project directory. |
required |
output_dir
|
Path
|
Directory to write the three parquet files. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Path, Path, Path]
|
Tuple of (manifest_path, run_registry_path, basin_inventory_path). |
Source code in hms2cng/project.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
hms2cng.project.export_full_project(hms_file, output_dir, *, layers=None, variables=None, out_crs='EPSG:4326', skip_errors=True, sort=True)
¶
Export an entire HMS project to a single consolidated GeoParquet.
Produces
output_dir/
{project_slug}.parquet -- all geometry + results with layer discriminator
manifest.json -- JSON catalog (schema v2.0)
The layer column discriminates between geometry layers (subbasins,
reaches, junctions, ...) and result variables (outflow, stage, ...).
Query examples::
SELECT * FROM 'project.parquet' WHERE layer = 'subbasins'
SELECT * FROM 'project.parquet' WHERE layer = 'outflow' AND run_name = 'Run 1'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hms_file
|
Path
|
Path to the .hms project file or project directory. |
required |
output_dir
|
Path
|
Root output directory for the archive. |
required |
layers
|
Optional[list]
|
Geometry layers to export (default: all available). |
None
|
variables
|
Optional[list]
|
Result variables to export (default: Outflow, Inflow, Stage, Depth). |
None
|
out_crs
|
str
|
Output CRS (default EPSG:4326). |
'EPSG:4326'
|
skip_errors
|
bool
|
If True, skip layers/runs with errors and continue. |
True
|
sort
|
bool
|
If True, apply Hilbert spatial sorting within each layer. |
True
|
Returns:
| Type | Description |
|---|---|
dict
|
Summary dict with keys: parquet_file, manifest_json, |
dict
|
geometry_rows, results_rows, errors. |
Source code in hms2cng/project.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
hms2cng.project.slugify(name)
¶
Convert a run/basin name to a filesystem-safe slug.
Replaces any non-word character with an underscore and lowercases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name to slugify (e.g. "Run 1 - Calibration"). |
required |
Returns:
| Type | Description |
|---|---|
str
|
A lowercase slug, e.g. "run_1___calibration". |
Source code in hms2cng/project.py
hms2cng.geometry¶
hms2cng.geometry.export_basin_geometry(basin_input, output, layer=None, *, crs_epsg=None, out_crs='EPSG:4326')
¶
Export a basin geometry layer to GeoParquet.
Source code in hms2cng/geometry.py
hms2cng.geometry.get_basin_layer_gdf(basin_input, layer='subbasins', *, crs_epsg=None, out_crs='EPSG:4326')
¶
Return a GeoDataFrame for a basin geometry layer.
Notes
- If crs_epsg is not provided and cannot be inferred, output CRS will be unset and no transformation to out_crs will occur.
- For "watershed", requires a .map file to construct polygons.
Source code in hms2cng/geometry.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
hms2cng.geometry.export_all_basin_geometry(hms_file, output_dir, *, layers=None, out_crs='EPSG:4326', skip_errors=True)
¶
Export geometry for ALL basin models in an HMS project.
Output: output_dir/{basin_slug}/{layer}.parquet Each file gains 'project_name' and 'basin_model' columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hms_file
|
Path
|
Path to the .hms project file or project directory. |
required |
output_dir
|
Path
|
Root output directory (geometry files go in subdirs per basin). |
required |
layers
|
Optional[list]
|
Geometry layers to attempt. Defaults to ["subbasins", "junctions", "reaches", "watershed", "subbasin_polygons", "longest_flowpaths"]. |
None
|
out_crs
|
Optional[str]
|
Output CRS (default EPSG:4326). |
'EPSG:4326'
|
skip_errors
|
bool
|
If True, skip layers that fail; otherwise raise. |
True
|
Returns:
| Type | Description |
|---|---|
list
|
List of Path objects for created parquet files. |
Source code in hms2cng/geometry.py
hms2cng.results¶
hms2cng.results.export_hms_results(results_input, output, *, element_type='subbasin', variable='Flow Out', crs_epsg=None, out_crs='EPSG:4326')
¶
Export HMS results summary statistics to GeoParquet.
- If given a folder, we look for RUN_*.results XML and parse it.
- Geometry is inferred from the parent HMS project (find .hms and .basin).
This currently exports summary statistics (peak/min/avg). Full time-series export can be added once DSS reading dependencies are standardized.
Source code in hms2cng/results.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
hms2cng.results.export_all_results(hms_file, output_dir, *, variables=None, out_crs='EPSG:4326', skip_errors=True)
¶
Export results for ALL runs in an HMS project.
Output: output_dir/{run_slug}/{variable_slug}.parquet Each file is enriched with: project_name, run_name, basin_model, met_model, control_spec, start_date, end_date, time_interval_minutes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hms_file
|
Path
|
Path to the .hms project file or project directory. |
required |
output_dir
|
Path
|
Root output directory (results go in per-run subdirs). |
required |
variables
|
Optional[list]
|
Result variables to export. Defaults to ["Outflow"]. |
None
|
out_crs
|
Optional[str]
|
Output CRS (default EPSG:4326). |
'EPSG:4326'
|
skip_errors
|
bool
|
If True, skip runs/variables that fail; otherwise raise. |
True
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (created_paths: list[Path], errors: list[str]). |
Source code in hms2cng/results.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
hms2cng.duckdb_session¶
hms2cng.duckdb_session.DuckSession
¶
DuckDB session with spatial extension pre-loaded
Source code in hms2cng/duckdb_session.py
register_parquet(path, name='_')
¶
hms2cng.duckdb_session.query_parquet(input_file, sql)
¶
Quick helper to query a single GeoParquet file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
Path
|
Path to GeoParquet file |
required |
sql
|
str
|
SQL query (use '_' as table name) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas DataFrame with query results |
Example
df = query_parquet("subbasins.parquet", "SELECT * FROM _ WHERE max_value > 1000")
Source code in hms2cng/duckdb_session.py
hms2cng.duckdb_session.spatial_join(left_file, right_file, predicate='ST_Intersects', output_file=None)
¶
Perform spatial join between two GeoParquet files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_file
|
Path
|
Left GeoParquet file |
required |
right_file
|
Path
|
Right GeoParquet file |
required |
predicate
|
str
|
Spatial predicate (ST_Intersects, ST_Contains, ST_Within, etc.) |
'ST_Intersects'
|
output_file
|
Optional[Path]
|
Optional output file for results |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with joined results |
Source code in hms2cng/duckdb_session.py
hms2cng.pmtiles¶
hms2cng.pmtiles.generate_pmtiles_from_input(input_file, output, layer_name='layer', min_zoom=None, max_zoom=None)
¶
Generate PMTiles (or MBTiles) from GeoParquet (vector).
Source code in hms2cng/pmtiles.py
hms2cng.postgis_sync¶
hms2cng.postgis_sync.sync_to_postgres(input_file, postgres_uri, table_name, schema='public', if_exists='replace')
¶
Sync GeoParquet file to PostGIS table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
Path
|
Path to GeoParquet file |
required |
postgres_uri
|
str
|
PostgreSQL connection URI (postgresql://user:pass@host:port/db) |
required |
table_name
|
str
|
Target table name |
required |
schema
|
str
|
Target schema |
'public'
|
if_exists
|
str
|
'replace', 'append', or 'fail' |
'replace'
|