grat.model.sorters
¶
Ordering rules that decide which sites get water first.
The model allocates water strictly in list order, so the sort applied here determines priority: a site earlier in the list takes what it can before later sites are offered anything.
recharge_type_order
¶
Return the rank used to group sites by recharge type when sorting.
Ranks canals 1, basins 2, on-farm recharge 3, and anything else 4. Applied
last in sort_sites so that sites of the same type stay together.
Note that this ordering is not what decides which type is watered first —
grat.use_sites fixes that order independently, and it differs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
any site with a |
required |
Returns:
| Type | Description |
|---|---|
|
Rank from 1 to 4. |
Source code in src/grat/model/sorters.py
none_aware_attrgetter
¶
Build a sort key for an attribute that may be None on some sites.
Sorting on an attribute that is None for some sites raises a TypeError,
because None cannot be compared with a number. This wraps the attribute in
a tuple whose first element sorts the Nones as a group, so they always come
last regardless of sort direction, and the real values are compared only
against each other.
Adapted from https://stackoverflow.com/questions/61452346
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
name of the attribute to sort on. |
required | |
reverse
|
whether the caller will pass |
False
|
Returns:
| Type | Description |
|---|---|
|
A key function suitable for |
Source code in src/grat/model/sorters.py
sort_by_variable
¶
Sort sites by a single attribute.
Sites whose value is None are placed last in either direction. Use
sort_by_variables to sort on more than one attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_sites
|
sites to sort. |
required | |
variable
|
name of the attribute to sort on. |
required | |
reverse
|
sort descending rather than ascending. |
False
|
Returns:
| Type | Description |
|---|---|
|
A new sorted list; the input is left unchanged. |
Source code in src/grat/model/sorters.py
sort_by_variables
¶
Sort sites by several attributes at once.
Works by sorting repeatedly, least significant attribute first, relying on Python's sort being stable. List the attributes in the order you want them applied — most significant first::
sort_by_variables(sites, (("acres", True), ("index", True)))
See https://docs.python.org/3/howto/sorting.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_sites
|
sites to sort. Mutated: sorted in place. |
required | |
specs
|
|
required |
Returns:
| Type | Description |
|---|---|
|
The same list, sorted. |
Source code in src/grat/model/sorters.py
sort_sites
¶
Put sites into the order the model will process them.
Applies the chosen scenario's tie-breakers first, then sorts by recharge type last so it dominates: sites are grouped by type, with the scenario deciding the order within each group.
Each site's sort_index is set to its one-based position in the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sites
|
sites to order. Mutated: sorted in place, and each site's
|
required | |
sort_rule
|
which scenario to apply. |
None
|
Returns:
| Type | Description |
|---|---|
|
The sites in processing order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in src/grat/model/sorters.py
group_sites_by_recharge_type
¶
Split the sites into the four lists the daily loop works through.
A farm can appear in both the in-lieu and on-farm lists if it has both a
crop and an in_lieu_crop; the two recharge methods are tracked separately
on the site, so it can receive water as each.
In-lieu sites are re-sorted here rather than left in the order sort_sites
produced: they are ordered by groundwater change, lowest first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site_list
|
all sites, already in processing order. |
required | |
use_in_lieu
|
include in-lieu sites. When |
True
|
|
use_ofr
|
include on-farm sites. When |
True
|
|
rma_sort
|
order in-lieu sites by RMA rank first, then by groundwater
change within each rank. When |
False
|
Returns:
| Type | Description |
|---|---|
|
Four lists: basins, in-lieu sites, on-farm sites, canals. |
Source code in src/grat/model/sorters.py
mlrp_sorter
¶
Convert the best-ranked land in each agency to the MLRP crop.
Works through each agency's sites best-ranked first, switching each to the "MLRP" crop until that agency's target acreage is used up. Converted sites have their in-lieu crop cleared, so they take part in on-farm recharge only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sites
|
sites to consider. Mutated: sorted in place, and the |
required | |
gsa_target_percentage
|
share of each agency's total acreage to convert,
keyed by agency, e.g. |
None
|