grat
¶
GRAT - a groundwater recharge allocation model.
The model lives in grat.model; grat.model.engine.start_analysis runs it across a
span of water years. grat.reports turns a run's results into report dataframes, and
grat.reports.writers writes them to csv.
To run the model and produce reports in one call::
from grat import read_report, run_grat
result = run_grat(sites, wafr, bottlenecks, crop_calendar, precip,
reports=["annual_summary_recharge", "acres_available_and_used"])
result["zip_path"] # the results archive
result["reports"] # the report names that were produced
read_report(result["output_folder"], "annual_summary_recharge")
grat.reports.catalog.ALL_REPORTS lists everything that can be asked for.
Before running, check your own input data for problems with grat.validation::
from grat.validation import validate_grat_inputs
report = validate_grat_inputs(sites, wafr, crop_calendar=crop_calendar)
report # prints a summary in a notebook cell
read_report
¶
Reads a report back from the folder it was written into.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_folder
|
the run's output folder. |
required | |
key
|
entry in |
required | |
year
|
water year, for the reports written into a per-year subfolder. |
None
|
|
dtype
|
optional column types, passed through to |
None
|
Returns: the report as a dataframe
Raises:
| Type | Description |
|---|---|
KeyError
|
|
FileNotFoundError
|
that report is not in |
Source code in src/grat/reports/writers.py
run_grat
¶
run_grat(
raw_sites,
raw_wafr,
raw_bottlenecks,
crop_calendar,
precip,
*,
reports=ALL_REPORTS,
output_parent=".",
zip_output=True,
zip_name=None,
**model_kwargs,
)
Runs the model across every water year and produces the requested reports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_sites
|
every canal, basin and farm, in processing order. |
required | |
raw_wafr
|
water available for recharge, indexed as |
required | |
raw_bottlenecks
|
the bottlenecks restricting delivery, or None. |
required | |
crop_calendar
|
accepted application rate, indexed as |
required | |
precip
|
precipitation, indexed as |
required | |
reports
|
report names to produce. Defaults to everything in the catalog; pass a smaller list and only the intermediates those reports need are built. |
ALL_REPORTS
|
|
output_parent
|
directory to create the timestamped results folder in. |
'.'
|
|
zip_output
|
zip the results folder when the run finishes. |
True
|
|
zip_name
|
name for the zip, defaulting to the results folder's name. |
None
|
|
**model_kwargs
|
passed through to |
{}
|
Returns:
| Type | Description |
|---|---|
|
A dict with four keys: |
|
|
|
|
|
|
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
a report name is not in the catalog. |
ValueError
|
on Windows, |
Source code in src/grat/run.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 | |
validate_grat_inputs
¶
validate_grat_inputs(
sites,
wafr,
*,
bottleneck_rows=None,
crop_calendar=None,
crop_calendar_in_lieu=None,
precip=None,
)
Runs every check that applies to the inputs provided, and returns the combined report.
Call this with whatever you've already loaded - only sites and wafr are
required, matching what build_sites and run_grat need at a minimum. Nothing
passed in is modified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sites
|
list of site attribute dictionaries, as returned by |
required | |
wafr
|
dictionary of |
required | |
bottleneck_rows
|
raw bottleneck rows (before |
None
|
|
crop_calendar
|
on-farm crop compatibility calendar, as returned by |
None
|
|
crop_calendar_in_lieu
|
in-lieu crop compatibility calendar, as returned by |
None
|
|
precip
|
dictionary of |
None
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
is True when no errors were found, and |
|
|
the issues found at each level. |