grat 0.3.2__tar.gz → 0.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. {grat-0.3.2 → grat-0.3.3}/.github/workflows/ci.yml +3 -0
  2. {grat-0.3.2 → grat-0.3.3}/.github/workflows/publish.yml +3 -0
  3. {grat-0.3.2 → grat-0.3.3}/PKG-INFO +1 -1
  4. {grat-0.3.2 → grat-0.3.3}/docs/reports.md +2 -0
  5. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/bottlenecks.csv +1 -1
  6. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/oakvalley.dbf +0 -0
  7. {grat-0.3.2 → grat-0.3.3}/src/grat/model/engine.py +13 -3
  8. {grat-0.3.2 → grat-0.3.3}/src/grat/reports/builders.py +1 -1
  9. {grat-0.3.2 → grat-0.3.3}/src/grat/reports/selector.py +2 -7
  10. {grat-0.3.2 → grat-0.3.3}/src/grat/reports/writers.py +37 -0
  11. {grat-0.3.2 → grat-0.3.3}/src/grat/run.py +14 -1
  12. {grat-0.3.2 → grat-0.3.3}/tests/test_loaders.py +8 -8
  13. {grat-0.3.2 → grat-0.3.3}/tests/test_model.py +14 -0
  14. {grat-0.3.2 → grat-0.3.3}/tests/test_reports.py +50 -1
  15. {grat-0.3.2 → grat-0.3.3}/.github/workflows/docs.yml +0 -0
  16. {grat-0.3.2 → grat-0.3.3}/.gitignore +0 -0
  17. {grat-0.3.2 → grat-0.3.3}/.pre-commit-config.yaml +0 -0
  18. {grat-0.3.2 → grat-0.3.3}/.python-version +0 -0
  19. {grat-0.3.2 → grat-0.3.3}/LICENSE +0 -0
  20. {grat-0.3.2 → grat-0.3.3}/README.md +0 -0
  21. {grat-0.3.2 → grat-0.3.3}/docs/index.md +0 -0
  22. {grat-0.3.2 → grat-0.3.3}/docs/model.md +0 -0
  23. {grat-0.3.2 → grat-0.3.3}/examples/oak_valley_walkthrough.ipynb +0 -0
  24. {grat-0.3.2 → grat-0.3.3}/mkdocs.yml +0 -0
  25. {grat-0.3.2 → grat-0.3.3}/pyproject.toml +0 -0
  26. {grat-0.3.2 → grat-0.3.3}/src/grat/__init__.py +0 -0
  27. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/CCC_inlieu.csv +0 -0
  28. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/CCC_inlieu_multiplier.csv +0 -0
  29. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/CCC_ofr.csv +0 -0
  30. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/Precip_optimistic.csv +0 -0
  31. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/Precip_pessimistic.csv +0 -0
  32. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/README.md +0 -0
  33. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/WAFR_optimistic.csv +0 -0
  34. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/WAFR_pessimistic.csv +0 -0
  35. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/canals.csv +0 -0
  36. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/oakvalley.cpg +0 -0
  37. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/oakvalley.prj +0 -0
  38. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/oakvalley.shp +0 -0
  39. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/OakValley/oakvalley.shx +0 -0
  40. {grat-0.3.2 → grat-0.3.3}/src/grat/datasets/__init__.py +0 -0
  41. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/__init__.py +0 -0
  42. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/get_bottlenecks.py +0 -0
  43. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/get_canals.py +0 -0
  44. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/get_ccc.py +0 -0
  45. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/get_precip.py +0 -0
  46. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/get_sites.py +0 -0
  47. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/get_wafr.py +0 -0
  48. {grat-0.3.2 → grat-0.3.3}/src/grat/loaders/helpers.py +0 -0
  49. {grat-0.3.2 → grat-0.3.3}/src/grat/model/__init__.py +0 -0
  50. {grat-0.3.2 → grat-0.3.3}/src/grat/model/bottlenecks.py +0 -0
  51. {grat-0.3.2 → grat-0.3.3}/src/grat/model/constants.py +0 -0
  52. {grat-0.3.2 → grat-0.3.3}/src/grat/model/sites.py +0 -0
  53. {grat-0.3.2 → grat-0.3.3}/src/grat/model/sorters.py +0 -0
  54. {grat-0.3.2 → grat-0.3.3}/src/grat/model/transfers.py +0 -0
  55. {grat-0.3.2 → grat-0.3.3}/src/grat/reports/__init__.py +0 -0
  56. {grat-0.3.2 → grat-0.3.3}/src/grat/reports/catalog.py +0 -0
  57. {grat-0.3.2 → grat-0.3.3}/src/grat/reports/converters.py +0 -0
  58. {grat-0.3.2 → grat-0.3.3}/src/grat/validation.py +0 -0
  59. {grat-0.3.2 → grat-0.3.3}/tests/__init__.py +0 -0
  60. {grat-0.3.2 → grat-0.3.3}/tests/conftest.py +0 -0
  61. {grat-0.3.2 → grat-0.3.3}/tests/test_smoke.py +0 -0
  62. {grat-0.3.2 → grat-0.3.3}/tests/test_validation.py +0 -0
  63. {grat-0.3.2 → grat-0.3.3}/uv.lock +0 -0
@@ -6,6 +6,9 @@ on:
6
6
  pull_request:
7
7
  branches: [main]
8
8
 
9
+ permissions:
10
+ contents: read
11
+
9
12
  jobs:
10
13
  lint:
11
14
  runs-on: ubuntu-latest
@@ -4,6 +4,9 @@ on:
4
4
  release:
5
5
  types: [published]
6
6
 
7
+ permissions:
8
+ contents: read
9
+
7
10
  jobs:
8
11
  build:
9
12
  runs-on: ubuntu-latest
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: grat
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: Groundwater Recharge Assessment Tool
5
5
  Project-URL: Homepage, https://github.com/blueraster/earth-genome-grat
6
6
  Project-URL: Repository, https://github.com/blueraster/earth-genome-grat
@@ -28,6 +28,8 @@ skipped (with a log line) rather than failing the run when it's missing:
28
28
  - No bottlenecks → `NEEDS_BOTTLENECKS` reports are skipped.
29
29
  - No transfer list → `NEEDS_TRANSFERS` reports are skipped.
30
30
 
31
+ `run_grat` logs each skip once, before the run starts.
32
+
31
33
  ## How a run produces reports (`selector.py`)
32
34
 
33
35
  Two entry points, called once each per run:
@@ -1,5 +1,5 @@
1
1
  bid,name,capacity_cfs,capacity_irr_cfs,wafrsource
2
- 0,Turlock Main Canal,180,97,Folsom Lake
2
+ 0,Main Canal,180,97,Folsom Lake
3
3
  1,Canal 1,24,14,"Folsom Lake,Yolo Creek"
4
4
  2,Canal 2,35,20,Folsom Lake
5
5
  3,Canal 3,24,13,Folsom Lake
@@ -20,7 +20,6 @@ from grat.model.transfers import calc_transfer_limits
20
20
 
21
21
  logger = logging.getLogger(__name__)
22
22
 
23
- # TODO - these are hard-code to remove need for config.py but need to be moved OSGRAT
24
23
  transfer_supply_sources = {}
25
24
 
26
25
 
@@ -479,7 +478,8 @@ def start_analysis(
479
478
  year, so this list is not modified.
480
479
  raw_wafr: volume available (acre-feet) per day, keyed by WAFR source
481
480
  name, for each year — indexed as `[year][source][day]`.
482
- raw_bottlenecks: every bottleneck in the system. Copied per year.
481
+ raw_bottlenecks: every bottleneck in the system, or `None` if there are
482
+ none. Copied per year.
483
483
  crop_calendar: accepted application rate (feet per day) for on-farm
484
484
  recharge, indexed as `[crop][soil_type][day]`.
485
485
  precip: daily precipitation depth (decimal-feet) for each year, indexed
@@ -511,6 +511,15 @@ def start_analysis(
511
511
  if years is None:
512
512
  years = DEFAULT_YEARS
513
513
 
514
+ unsorted = sum(1 for site in raw_sites if site.sort_index is None)
515
+ if unsorted:
516
+ logger.warning(
517
+ "%d of %d sites have no sortIndex; the model will run them in the order of the input data. "
518
+ "Call sorters.sort_sites first to set a processing order.",
519
+ unsorted,
520
+ len(raw_sites),
521
+ )
522
+
514
523
  for year in years:
515
524
  logger.info("Running GRAT for %s", year)
516
525
 
@@ -532,9 +541,10 @@ def start_analysis(
532
541
  transfer_goals,
533
542
  )
534
543
 
544
+ # the model needs a list; on_year still gets None so reports know bottlenecks were missing
535
545
  process_year(
536
546
  sites,
537
- bottlenecks,
547
+ bottlenecks if bottlenecks is not None else [],
538
548
  wafr,
539
549
  crop_calendar,
540
550
  year_precip,
@@ -677,7 +677,7 @@ def bottlenecks_capacity_limitations_report(year, bottleneck_list):
677
677
  (
678
678
  year,
679
679
  f"BottleneckID {b.bottleneck_id}",
680
- list(b.available_throughput).count(0),
680
+ sum(1 for t in b.available_throughput if t <= 0),
681
681
  )
682
682
  )
683
683
 
@@ -79,16 +79,11 @@ def report_picker(
79
79
  logger.info("Generating reports")
80
80
  wanted = catalog.expand(names)
81
81
 
82
+ # run_grat logs these skips once per run
82
83
  if bottlenecks is None:
83
- skipped = wanted & catalog.NEEDS_BOTTLENECKS
84
- if skipped:
85
- logger.info("No bottlenecks provided, skipping %s", ", ".join(sorted(skipped)))
86
84
  wanted -= catalog.NEEDS_BOTTLENECKS
87
85
 
88
86
  if transfer_list is None:
89
- skipped = wanted & catalog.NEEDS_TRANSFERS
90
- if skipped:
91
- logger.warning("No transfer list provided, skipping %s", ", ".join(sorted(skipped)))
92
87
  wanted -= catalog.NEEDS_TRANSFERS
93
88
 
94
89
  def publish(df, key, folder=output_folder):
@@ -226,7 +221,7 @@ SITE_INFO_DTYPES = {
226
221
  "bottleneckIDs": "string",
227
222
  "soilType": "string",
228
223
  "rsi": float,
229
- "sortIndex": int,
224
+ "sortIndex": "Int64",
230
225
  "crop": "string",
231
226
  "inLieuCrop": "string",
232
227
  "subbasin": "string",
@@ -8,6 +8,8 @@ from typing import NamedTuple
8
8
 
9
9
  import pandas as pd
10
10
 
11
+ from grat.reports.catalog import YEAR_SUBFOLDER
12
+
11
13
  logger = logging.getLogger(__name__)
12
14
 
13
15
 
@@ -61,6 +63,41 @@ REPORT_FILES: dict[str, ReportFile] = {
61
63
  }
62
64
 
63
65
 
66
+ # Windows' default MAX_PATH is 260, which includes the terminating null
67
+ WINDOWS_MAX_PATH = 259
68
+
69
+
70
+ def check_output_path_length(output_parent, max_length=WINDOWS_MAX_PATH):
71
+ """Fails early if the longest report path under `output_parent` would be too long.
72
+
73
+ Without this, the run gets as far as writing the first per-year report and then
74
+ raises a `FileNotFoundError` that doesn't say why.
75
+
76
+ Args:
77
+ output_parent: directory the timestamped results folder will be created in.
78
+ max_length: longest path allowed. Defaults to Windows' limit.
79
+
80
+ Raises:
81
+ ValueError: the longest report path would be over `max_length`.
82
+ """
83
+ # same length as a real results folder and year subfolder
84
+ results_folder = os.path.join(os.path.abspath(output_parent), "results_00000000000000")
85
+ year_folder = os.path.join(results_folder, "0000")
86
+ longest = max(
87
+ (
88
+ os.path.join(year_folder if key in YEAR_SUBFOLDER else results_folder, f.filename)
89
+ for key, f in REPORT_FILES.items()
90
+ ),
91
+ key=len,
92
+ )
93
+
94
+ if len(longest) > max_length:
95
+ raise ValueError(
96
+ f"Report paths would be up to {len(longest)} characters, over the {max_length}-character limit "
97
+ f"(e.g. {longest}). Use an output_parent at least {len(longest) - max_length} characters shorter."
98
+ )
99
+
100
+
64
101
  def construct_output_folder_name(path_to_folder: str):
65
102
  """Creates a new unique folder using the current datetime to store the exported csv results.
66
103
 
@@ -23,11 +23,12 @@ held anything in memory::
23
23
  """
24
24
 
25
25
  import logging
26
+ import os
26
27
 
27
28
  from grat.model.engine import start_analysis
28
29
  from grat.reports import catalog
29
30
  from grat.reports.selector import post_process, report_picker
30
- from grat.reports.writers import construct_output_folder_name, zip_output_folder
31
+ from grat.reports.writers import check_output_path_length, construct_output_folder_name, zip_output_folder
31
32
 
32
33
  logger = logging.getLogger(__name__)
33
34
 
@@ -90,8 +91,20 @@ def run_grat(
90
91
 
91
92
  Raises:
92
93
  KeyError: a report name is not in the catalog.
94
+ ValueError: on Windows, `output_parent` is too long for the report paths.
93
95
  """
94
96
  names = catalog.expand(reports)
97
+
98
+ # report_picker skips these every year; say so once here
99
+ skipped = names & catalog.NEEDS_BOTTLENECKS if raw_bottlenecks is None else set()
100
+ if skipped:
101
+ logger.info("No bottlenecks provided, skipping %s", ", ".join(sorted(skipped)))
102
+ skipped = names & catalog.NEEDS_TRANSFERS if not model_kwargs.get("transfer_goals") else set()
103
+ if skipped:
104
+ logger.warning("No transfer list provided, skipping %s", ", ".join(sorted(skipped)))
105
+
106
+ if os.name == "nt":
107
+ check_output_path_length(output_parent)
95
108
  output_folder = construct_output_folder_name(output_parent)
96
109
  logger.info("Writing reports to %s", output_folder)
97
110
 
@@ -248,7 +248,7 @@ class TestGetBottlenecks:
248
248
  @pytest.fixture(autouse=True)
249
249
  def setup(self, oak_valley):
250
250
  self.bottlenecks = get_bottlenecks.load_bottlenecks(oak_valley / "bottlenecks.csv")
251
- self.turlock = next(b for b in self.bottlenecks if b.name == "Turlock Main Canal")
251
+ self.main_canal = next(b for b in self.bottlenecks if b.name == "Main Canal")
252
252
 
253
253
  def test_get_bn(self):
254
254
  assert len(self.bottlenecks) == 19
@@ -257,12 +257,12 @@ class TestGetBottlenecks:
257
257
  assert [b.bottleneck_id for b in self.bottlenecks] == list(range(19))
258
258
 
259
259
  def test_capacity(self):
260
- assert self.turlock.capacity == pytest.approx(356.94) # 180 cfs x 1.983 acre-feet per day
261
- assert self.turlock.capacity_irrigation == pytest.approx(192.351) # 97 cfs x 1.983 acre-feet per day
260
+ assert self.main_canal.capacity == pytest.approx(356.94) # 180 cfs x 1.983 acre-feet per day
261
+ assert self.main_canal.capacity_irrigation == pytest.approx(192.351) # 97 cfs x 1.983 acre-feet per day
262
262
 
263
263
  def test_wafr_sources(self):
264
264
  canal_1 = next(b for b in self.bottlenecks if b.name == "Canal 1")
265
- assert self.turlock.wafr_sources == ["Folsom Lake"]
265
+ assert self.main_canal.wafr_sources == ["Folsom Lake"]
266
266
  assert canal_1.wafr_sources == ["Folsom Lake", "Yolo Creek"]
267
267
 
268
268
  def test_wafr_sources_match_wafr(self, oak_valley):
@@ -272,10 +272,10 @@ class TestGetBottlenecks:
272
272
  assert {s for b in self.bottlenecks for s in b.wafr_sources} <= wafr_sources
273
273
 
274
274
  def test_available_throughput_array(self):
275
- assert self.turlock.available_throughput[0] == pytest.approx(192.351)
276
- assert self.turlock.available_throughput[30] == pytest.approx(192.351)
277
- assert self.turlock.available_throughput[31] == pytest.approx(356.94)
278
- assert self.turlock.available_throughput[182] == pytest.approx(192.351)
275
+ assert self.main_canal.available_throughput[0] == pytest.approx(192.351)
276
+ assert self.main_canal.available_throughput[30] == pytest.approx(192.351)
277
+ assert self.main_canal.available_throughput[31] == pytest.approx(356.94)
278
+ assert self.main_canal.available_throughput[182] == pytest.approx(192.351)
279
279
 
280
280
 
281
281
  class TestLoadSites:
@@ -1452,3 +1452,17 @@ class TestCalcTransferLimits:
1452
1452
  def test_district_transfer_goals_defaults_to_empty_when_omitted(self):
1453
1453
  result = calc_transfer_limits(self.wafr, self.transfer_sources)
1454
1454
  assert result == {"transfer_wafr_sources": self.transfer_sources}
1455
+
1456
+
1457
+ class TestUnsortedSitesWarning:
1458
+ def test_warns_when_sort_index_missing(self, caplog):
1459
+ raw_sites = [sites.Basin(grat_id="a"), sites.Basin(grat_id="b")]
1460
+ with caplog.at_level(logging.WARNING):
1461
+ engine.start_analysis(raw_sites, {}, None, {}, None, years=[])
1462
+ assert "2 of 2 sites have no sortIndex" in caplog.text
1463
+
1464
+ def test_no_warning_once_sorted(self, caplog):
1465
+ raw_sites = sorters.sort_sites([sites.Basin(grat_id="a"), sites.Basin(grat_id="b")])
1466
+ with caplog.at_level(logging.WARNING):
1467
+ engine.start_analysis(raw_sites, {}, None, {}, None, years=[])
1468
+ assert "sortIndex" not in caplog.text
@@ -1,3 +1,4 @@
1
+ import logging
1
2
  import os
2
3
  from types import SimpleNamespace
3
4
 
@@ -6,6 +7,7 @@ import pandas as pd
6
7
  import pytest
7
8
 
8
9
  from grat.reports import builders, catalog, converters, selector, writers
10
+ from grat.run import run_grat
9
11
 
10
12
  wd = os.path.abspath(os.path.dirname(__file__))
11
13
 
@@ -140,7 +142,7 @@ def test_daily_wafr_report_drops_day_366():
140
142
 
141
143
  def test_bottlenecks_capacity_limitations_report():
142
144
  bottleneck_list = [
143
- SimpleNamespace(bottleneck_id=10, available_throughput=[0, 0, 5] + [1] * 363),
145
+ SimpleNamespace(bottleneck_id=10, available_throughput=[0, -0.5, 5] + [1] * 363),
144
146
  SimpleNamespace(bottleneck_id=2, available_throughput=[1] * 366),
145
147
  SimpleNamespace(bottleneck_id=1, available_throughput=[0] * 366),
146
148
  ]
@@ -245,3 +247,50 @@ def test_write_csv_append(tmp_path):
245
247
  writers.write_csv(df, str(tmp_path), "replaced.csv")
246
248
  writers.write_csv(df, str(tmp_path), "replaced.csv")
247
249
  assert len(pd.read_csv(tmp_path / "replaced.csv")) == 1
250
+
251
+
252
+ def test_run_grat_logs_missing_transfers_once(tmp_path, caplog):
253
+ with caplog.at_level(logging.WARNING):
254
+ run_grat(
255
+ [],
256
+ {},
257
+ None,
258
+ {},
259
+ None,
260
+ years=[],
261
+ reports=["daily_water_transfer_by_sustain_zone_report"],
262
+ output_parent=str(tmp_path),
263
+ zip_output=False,
264
+ )
265
+
266
+ assert caplog.text.count("No transfer list provided") == 1
267
+
268
+
269
+ def test_check_output_path_length(tmp_path):
270
+ writers.check_output_path_length(str(tmp_path))
271
+
272
+ with pytest.raises(ValueError, match="characters shorter"):
273
+ writers.check_output_path_length(str(tmp_path / ("x" * 250)))
274
+
275
+
276
+ def test_post_process_monthly_reports_with_null_sort_index(tmp_path):
277
+ # EG-179: nothing in a run calls sort_sites, so every sortIndex is written blank,
278
+ # and reading it back as int broke reports 06 and 07
279
+ sites = [
280
+ SimpleNamespace(grat_id="a", recharge_type="Basin", sort_index=None),
281
+ SimpleNamespace(grat_id="b", recharge_type="OFR", sort_index=None),
282
+ ]
283
+ writers.write_report(builders.get_site_info(sites), str(tmp_path), "site_info")
284
+
285
+ months = {str(m): [1.0, 2.0] for m in range(1, 13)}
286
+ monthly = pd.DataFrame(
287
+ {"Year": [2001, 2001], "gratID": ["a", "b"], "recharge_volume_type": "OFR/Basin/Canal", **months}
288
+ )
289
+ writers.write_report(monthly, str(tmp_path), "monthly_recharge_by_site_temp")
290
+
291
+ names = ["monthly_recharge_by_site_report", "monthly_recharge_by_site_report_detailed"]
292
+ results = selector.post_process(names, str(tmp_path))
293
+
294
+ for name in names:
295
+ assert sorted(results[name]["gratID"]) == ["a", "b"]
296
+ assert os.path.exists(tmp_path / writers.REPORT_FILES[name].filename)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes