grat.model.engine
¶
Top-level water allocation across canals, basins and farms.
GRAT allocates each day's water available for recharge (WAFR) to the sites that
can take it, working down a priority order. start_analysis runs a span of
years, process_year runs one, and use_sites runs a single day.
Within a day the order is fixed: canals, then in-lieu fields, then basins, then on-farm recharge. That whole sequence runs twice — once drawing on each district's own (primary) sources, and again on other districts' (secondary) sources for whatever is left.
timer_func
¶
Log how long the decorated function took to run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
the function to time. |
required |
Returns:
| Type | Description |
|---|---|
|
The wrapped function, which logs its runtime at INFO level once it |
|
|
returns and then passes the result through unchanged. |
Source code in src/grat/model/engine.py
fill_basins
¶
fill_basins(
basin_list: list,
day: int,
wafr: dict,
bottleneck_list: list,
secondary_wafr: bool = False,
transfer_limits: dict | None = None,
)
Apply water to every basin in turn.
Basins are filled in list order, so earlier basins get first call on the
available water. See Basin.use_basin for how each one is filled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basin_list
|
list
|
basins to fill, in processing order. |
required |
day
|
int
|
zero-indexed day of the water year. |
required |
wafr
|
dict
|
volume still available (acre-feet) per day, keyed by WAFR source name. Mutated: water applied is subtracted from it. |
required |
bottleneck_list
|
list
|
every bottleneck in the system. Mutated. |
required |
secondary_wafr
|
bool
|
draw from secondary (out-of-district) sources instead of primary (in-district) ones. |
False
|
transfer_limits
|
dict | None
|
remaining yearly transfer budget (acre-feet) per
district. Mutated. |
None
|
Source code in src/grat/model/engine.py
basins_update_daily_values
¶
Drain a day's percolation from every basin into groundwater recharge.
Call once per day after the basins have been filled. See
Basin.update_daily_values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basin_list
|
list
|
basins to drain. |
required |
day
|
int
|
zero-indexed day of the water year. |
required |
Source code in src/grat/model/engine.py
proportionally_fill
¶
proportionally_fill(
day: int,
canal_list: list,
wafr: dict,
selected_wafr: str,
secondary_wafr: bool = False,
)
Share one WAFR source's water across the canals it can reach.
Unlike basins and farms, which are filled in priority order until the water runs out, canals are filled together: each one receives a share of the source's volume in proportion to how much headroom it has. If the source has enough water for everyone, every canal fills.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
day
|
int
|
zero-indexed day of the water year. |
required |
canal_list
|
list
|
canals to consider. Those the source cannot reach are skipped. |
required |
wafr
|
dict
|
volume still available (acre-feet) per day, keyed by WAFR source name. Mutated: water applied is subtracted from it. |
required |
selected_wafr
|
str
|
name of the WAFR source being shared out. |
required |
secondary_wafr
|
bool
|
match canals on their secondary (out-of-district) sources instead of their primary (in-district) ones. |
False
|
Source code in src/grat/model/engine.py
fill_canals
¶
Fill the canals from every WAFR source that has water.
Sources are worked through largest first, so the most plentiful source is
shared out before the scarcer ones. Each is distributed by
proportionally_fill.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
canal_list
|
list
|
canals to fill. |
required |
day
|
int
|
zero-indexed day of the water year. |
required |
wafr
|
dict
|
volume still available (acre-feet) per day, keyed by WAFR source name. Mutated: water applied is subtracted from it. |
required |
secondary_wafr
|
bool
|
match canals on their secondary (out-of-district) sources instead of their primary (in-district) ones. |
False
|
Source code in src/grat/model/engine.py
canals_update_daily_values
¶
Drain a day's percolation from every canal into groundwater recharge.
Call once per day after the canals have been filled. See
Basin.update_daily_values, which canals inherit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
canal_list
|
list
|
canals to drain. |
required |
day
|
int
|
zero-indexed day of the water year. |
required |
Source code in src/grat/model/engine.py
use_farms
¶
use_farms(
site_list: list,
day: int,
wafr: dict,
bottleneck_list: list,
crop_calendar: dict,
in_lieu: bool = False,
secondary_wafr: bool = False,
transfer_limits: dict | None = None,
)
Apply water to every field in turn.
Fields are served in list order, so earlier fields get first call on the
available water. See Farm.use_site for how each one is filled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site_list
|
list
|
fields to water, in processing order. Callers pass either the
on-farm sites or the in-lieu sites, matched to |
required |
day
|
int
|
zero-indexed day of the water year. |
required |
wafr
|
dict
|
volume still available (acre-feet) per day, keyed by WAFR source name. Mutated: water applied is subtracted from it. |
required |
bottleneck_list
|
list
|
every bottleneck in the system. Mutated. |
required |
crop_calendar
|
dict
|
accepted application rate (feet per day) by crop. Must match
|
required |
in_lieu
|
bool
|
apply as in-lieu recharge rather than on-farm. |
False
|
secondary_wafr
|
bool
|
draw from secondary (out-of-district) sources instead of primary (in-district) ones. |
False
|
transfer_limits
|
dict | None
|
remaining yearly transfer budget (acre-feet) per district.
Mutated. |
None
|
Source code in src/grat/model/engine.py
sort_wafr_source_by_volume
¶
Rank the WAFR sources that have a non-zero volume on a day, largest first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wafr
|
dict
|
volume available (acre-feet) per day, keyed by WAFR source name. |
required |
day
|
int
|
zero-indexed day of the water year. |
required |
Returns:
| Type | Description |
|---|---|
|
Source names in descending order of volume. Sources sitting at exactly |
|
|
zero that day are omitted; the test is for non-zero, so a negative |
|
|
volume is kept and sorts last. |
Source code in src/grat/model/engine.py
check_wafr_availability
¶
Report whether any WAFR source has water left on a day.
Used to skip the remaining allocation work once the day's water is spent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wafr
|
volume available (acre-feet) per day, keyed by WAFR source name. |
required | |
day
|
zero-indexed day of the water year. |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in src/grat/model/engine.py
use_sites
¶
use_sites(
day,
wafr,
canal_list,
basin_list,
in_lieu_sites,
ofr_sites,
bottlenecks,
crop_calendar,
crop_calendar_in_lieu,
transfer_limits,
)
Allocate a single day's water across every site that can take it.
Runs the priority order twice: first against each district's own primary sources, then against secondary sources from other districts for whatever water is left. Within each pass the order is canals, in-lieu fields, basins, on-farm recharge. The three site types after canals are skipped entirely if the day's water is already spent.
Ends by draining the day's percolation from the canals and basins into groundwater recharge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
day
|
zero-indexed day of the water year. |
required | |
wafr
|
volume still available (acre-feet) per day, keyed by WAFR source name. Mutated: water applied is subtracted from it. |
required | |
canal_list
|
canals, in processing order. |
required | |
basin_list
|
basins, in processing order. |
required | |
in_lieu_sites
|
in-lieu fields, in processing order. |
required | |
ofr_sites
|
on-farm recharge fields, in processing order. |
required | |
bottlenecks
|
every bottleneck in the system. Mutated. |
required | |
crop_calendar
|
accepted application rate (feet per day) for on-farm
recharge, indexed as |
required | |
crop_calendar_in_lieu
|
accepted application rate (feet per day) for
in-lieu recharge, indexed as |
required | |
transfer_limits
|
remaining yearly transfer budget (acre-feet) per district. Mutated. |
required |
Source code in src/grat/model/engine.py
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 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 | |
process_year
¶
process_year(
site_list,
bottleneck_list,
wafr,
crop_calendar,
precip,
crop_calendar_in_lieu=None,
use_in_lieu=True,
use_ofr=True,
rma_sort=True,
transfer_limits=None,
)
Run GRAT for a single water year.
Applies precipitation to every site except canals, groups the sites by recharge type, then walks the year one day at a time allocating water. The length of the year is taken from the WAFR series, so a 366-entry series runs a leap year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site_list
|
every canal, basin and farm, already in processing order. Mutated: the sites accumulate the year's results. |
required | |
bottleneck_list
|
every bottleneck in the system. Mutated. |
required | |
wafr
|
volume available (acre-feet) per day, keyed by WAFR source name. Mutated: drawn down to zero as the year is allocated. |
required | |
crop_calendar
|
accepted application rate (feet per day) for on-farm
recharge, indexed as |
required | |
precip
|
daily precipitation depth (decimal-feet) for the water year,
keyed by precipitation grid identifier. |
required | |
crop_calendar_in_lieu
|
accepted application rate (feet per day) for
in-lieu recharge, indexed as |
None
|
|
use_in_lieu
|
include in-lieu recharge. |
True
|
|
use_ofr
|
include on-farm recharge. |
True
|
|
rma_sort
|
order in-lieu sites by RMA rank first. |
True
|
|
transfer_limits
|
yearly transfer budget (acre-feet) per district, from
|
None
|
Returns:
| Type | Description |
|---|---|
|
The year's results, holding the modified |
|
|
and |
Source code in src/grat/model/engine.py
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 | |
start_analysis
¶
start_analysis(
raw_sites,
raw_wafr,
raw_bottlenecks,
crop_calendar,
precip,
years=None,
crop_calendar_in_lieu=None,
on_year=None,
use_in_lieu=True,
use_ofr=True,
rma_sort=True,
transfer_goals=None,
)
Run GRAT across a span of water years.
Each year is run against a deep copy of the inputs, so the sites, WAFR and bottlenecks start every year in their original state and the years stay independent of one another. The copies are discarded once the year is done.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_sites
|
every canal, basin and farm, in processing order. Copied per year, so this list is not modified. |
required | |
raw_wafr
|
volume available (acre-feet) per day, keyed by WAFR source
name, for each year — indexed as |
required | |
raw_bottlenecks
|
every bottleneck in the system, or |
required | |
crop_calendar
|
accepted application rate (feet per day) for on-farm
recharge, indexed as |
required | |
precip
|
daily precipitation depth (decimal-feet) for each year, indexed
as |
required | |
years
|
water years to run. Defaults to 2001-2099. |
None
|
|
crop_calendar_in_lieu
|
accepted application rate (feet per day) for
in-lieu recharge, indexed as |
None
|
|
use_in_lieu
|
include in-lieu recharge. |
True
|
|
use_ofr
|
include on-farm recharge. |
True
|
|
rma_sort
|
order in-lieu sites by RMA rank first. |
True
|
|
transfer_goals
|
transfer goal per district or sustainability zone, used
to derive each year's transfer budgets. See
|
None
|
|
on_year
|
called once per water year, just before that year's results are
discarded, as |
None
|
Note
Returns nothing. Each year's results are handed to on_year if one was
given and then discarded, so a long run holds one year at a time. Call
process_year directly to get a single year's results back.
Source code in src/grat/model/engine.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | |