stplot 0.1.0__py3-none-any.whl

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 (49) hide show
  1. stplot/__init__.py +21 -0
  2. stplot/config/__init__.py +33 -0
  3. stplot/config/loader.py +106 -0
  4. stplot/config/models.py +328 -0
  5. stplot/config/presets/__init__.py +1 -0
  6. stplot/config/presets/default.yaml +208 -0
  7. stplot/config/presets/horizons_one_plan.yaml +59 -0
  8. stplot/io/__init__.py +6 -0
  9. stplot/io/spatial.py +72 -0
  10. stplot/io/tabular.py +154 -0
  11. stplot/plots/__init__.py +122 -0
  12. stplot/plots/barbell.py +400 -0
  13. stplot/plots/batch.py +135 -0
  14. stplot/plots/domains.py +1143 -0
  15. stplot/plots/executive.py +474 -0
  16. stplot/plots/export.py +74 -0
  17. stplot/plots/infographics.py +1118 -0
  18. stplot/plots/lollipop.py +285 -0
  19. stplot/plots/maps.py +738 -0
  20. stplot/plots/matrix.py +190 -0
  21. stplot/plots/one_plan.py +642 -0
  22. stplot/plots/paired.py +437 -0
  23. stplot/plots/scorecards.py +1940 -0
  24. stplot/plots/state.py +373 -0
  25. stplot/plots/state_trends.py +1413 -0
  26. stplot/plots/tables.py +107 -0
  27. stplot/plots/timeseries.py +607 -0
  28. stplot/plots/trends.py +305 -0
  29. stplot/reporting/__init__.py +24 -0
  30. stplot/reporting/captions.py +177 -0
  31. stplot/reporting/html_companion.py +339 -0
  32. stplot/reporting/templates.py +485 -0
  33. stplot/reporting/word.py +369 -0
  34. stplot/theme/__init__.py +25 -0
  35. stplot/theme/folium.py +62 -0
  36. stplot/theme/mpl.py +181 -0
  37. stplot/theme/plotly.py +130 -0
  38. stplot/transform/__init__.py +60 -0
  39. stplot/transform/categories.py +51 -0
  40. stplot/transform/columns.py +103 -0
  41. stplot/transform/filters.py +385 -0
  42. stplot/transform/merge.py +173 -0
  43. stplot/transform/pairing.py +154 -0
  44. stplot/transform/qa.py +297 -0
  45. stplot/transform/tables.py +573 -0
  46. stplot/transform/zones.py +354 -0
  47. stplot-0.1.0.dist-info/METADATA +235 -0
  48. stplot-0.1.0.dist-info/RECORD +49 -0
  49. stplot-0.1.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,485 @@
1
+ """Production Word (.docx) report templates for NPS-FM and Horizons One Plan reporting."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ from stplot.config import Config, ensure_config
9
+ from stplot.reporting.word import _import_docx, _set_cell_shading
10
+
11
+
12
+ def _hex_to_rgb(hex_color: str, rgb_color: Any) -> Any:
13
+ cleaned = hex_color.lstrip("#")
14
+ if len(cleaned) != 6:
15
+ return rgb_color(0, 119, 163)
16
+ return rgb_color(int(cleaned[0:2], 16), int(cleaned[2:4], 16), int(cleaned[4:6], 16))
17
+
18
+
19
+ def _setup_a4_document(
20
+ cfg: Config,
21
+ header_text: str,
22
+ footer_text: str,
23
+ ) -> tuple[Any, Any, Any, Any, Any]:
24
+ """Create an A4 python-docx Document with margins, headers, footers, and base font."""
25
+ Document, xml_helpers, Mm, rgb_color = _import_docx()
26
+ from docx.enum.text import WD_ALIGN_PARAGRAPH
27
+ from docx.shared import Pt
28
+
29
+ doc = Document()
30
+ for section in doc.sections:
31
+ section.page_width = Mm(210)
32
+ section.page_height = Mm(297)
33
+ section.top_margin = Mm(20)
34
+ section.bottom_margin = Mm(20)
35
+ section.left_margin = Mm(20)
36
+ section.right_margin = Mm(20)
37
+ section.different_first_page_header_footer = True
38
+
39
+ hdr_p = section.header.paragraphs[0]
40
+ hdr_p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
41
+ h_run = hdr_p.add_run(header_text)
42
+ h_run.font.size = Pt(8.5)
43
+ h_run.font.color.rgb = rgb_color(100, 112, 125)
44
+
45
+ ftr_p = section.footer.paragraphs[0]
46
+ ftr_p.alignment = WD_ALIGN_PARAGRAPH.CENTER
47
+ f_run = ftr_p.add_run(footer_text)
48
+ f_run.font.size = Pt(8.5)
49
+ f_run.font.color.rgb = rgb_color(120, 130, 140)
50
+
51
+ accent_rgb = _hex_to_rgb(cfg.theme.accent_color, rgb_color)
52
+ text_rgb = _hex_to_rgb(cfg.theme.text_color, rgb_color)
53
+
54
+ normal = doc.styles["Normal"]
55
+ normal.font.name = "Calibri"
56
+ normal.font.size = Pt(10.5)
57
+ normal.font.color.rgb = text_rgb
58
+
59
+ return doc, xml_helpers, accent_rgb, text_rgb, rgb_color
60
+
61
+
62
+ def _add_cover_page(
63
+ doc: Any,
64
+ *,
65
+ banner_hex: str,
66
+ badge_text: str,
67
+ xml_helpers: tuple[Any, Any],
68
+ accent_rgb: Any,
69
+ rgb_color: Any,
70
+ ) -> None:
71
+ """Build a crisp, modern A4 cover page with top color banner and metadata table."""
72
+ from docx.shared import Pt
73
+
74
+ banner_tbl = doc.add_table(rows=1, cols=1)
75
+ banner_cell = banner_tbl.rows[0].cells[0]
76
+ _set_cell_shading(banner_cell, banner_hex, xml_helpers)
77
+
78
+ bp1 = banner_cell.paragraphs[0]
79
+ bp1.paragraph_format.space_before = Pt(14)
80
+ bp1.paragraph_format.space_after = Pt(4)
81
+ r_badge = bp1.add_run(badge_text.upper())
82
+ r_badge.bold = True
83
+ r_badge.font.size = Pt(9.5)
84
+ r_badge.font.color.rgb = rgb_color(210, 238, 248)
85
+
86
+ bp2 = banner_cell.add_paragraph()
87
+ bp2.paragraph_format.space_before = Pt(4)
88
+ bp2.paragraph_format.space_after = Pt(6)
89
+ r_title = bp2.add_run("{{ report_title }}")
90
+ r_title.bold = True
91
+ r_title.font.size = Pt(24)
92
+ r_title.font.color.rgb = rgb_color(255, 255, 255)
93
+
94
+ bp3 = banner_cell.add_paragraph()
95
+ bp3.paragraph_format.space_before = Pt(0)
96
+ bp3.paragraph_format.space_after = Pt(14)
97
+ r_sub = bp3.add_run("{{ subtitle }}")
98
+ r_sub.font.size = Pt(12.5)
99
+ r_sub.font.color.rgb = rgb_color(230, 242, 248)
100
+
101
+ sp = doc.add_paragraph()
102
+ sp.paragraph_format.space_before = Pt(18)
103
+
104
+ meta_tbl = doc.add_table(rows=4, cols=2)
105
+ meta_rows = [
106
+ ("Region / Jurisdiction", "{{ region_name }}"),
107
+ ("Reporting Period", "{{ reporting_period }}"),
108
+ ("Domains Covered", "{{ domains_covered }}"),
109
+ ("Prepared By", "{{ author }}"),
110
+ ]
111
+ for r_idx, (k, v) in enumerate(meta_rows):
112
+ c0, c1 = meta_tbl.rows[r_idx].cells
113
+ _set_cell_shading(c0, "#f1f5f9", xml_helpers)
114
+ _set_cell_shading(c1, "#ffffff", xml_helpers)
115
+ c0.text = k
116
+ c1.text = v
117
+ for run in c0.paragraphs[0].runs:
118
+ run.bold = True
119
+ run.font.size = Pt(10)
120
+ run.font.color.rgb = accent_rgb
121
+ for run in c1.paragraphs[0].runs:
122
+ run.font.size = Pt(10)
123
+
124
+ p_cov = doc.add_paragraph()
125
+ p_cov.paragraph_format.space_before = Pt(18)
126
+ p_cov.add_run("{% if cover_fig %}{{ cover_fig }}{% endif %}")
127
+
128
+ doc.add_page_break()
129
+
130
+
131
+ def _add_h1(doc: Any, text: str, accent_rgb: Any) -> None:
132
+ h = doc.add_heading(text, level=1)
133
+ for run in h.runs:
134
+ run.font.color.rgb = accent_rgb
135
+
136
+
137
+ def _add_h2(doc: Any, text: str, rgb_color: Any) -> None:
138
+ h = doc.add_heading(text, level=2)
139
+ for run in h.runs:
140
+ run.font.color.rgb = rgb_color(40, 58, 78)
141
+
142
+
143
+ def _add_h3(doc: Any, text: str, rgb_color: Any) -> None:
144
+ h = doc.add_heading(text, level=3)
145
+ for run in h.runs:
146
+ run.font.color.rgb = rgb_color(55, 75, 95)
147
+
148
+
149
+ def _add_fig(doc: Any, fig_expr: str, cap_expr: str, rgb_color: Any) -> None:
150
+ from docx.enum.text import WD_ALIGN_PARAGRAPH
151
+ from docx.shared import Pt
152
+
153
+ p_img = doc.add_paragraph()
154
+ p_img.alignment = WD_ALIGN_PARAGRAPH.CENTER
155
+ p_img.paragraph_format.space_before = Pt(6)
156
+ p_img.paragraph_format.space_after = Pt(2)
157
+ p_img.add_run(fig_expr)
158
+
159
+ p_cap = doc.add_paragraph()
160
+ p_cap.alignment = WD_ALIGN_PARAGRAPH.CENTER
161
+ p_cap.paragraph_format.space_after = Pt(10)
162
+ r_cap = p_cap.add_run(cap_expr)
163
+ r_cap.italic = True
164
+ r_cap.font.size = Pt(9.0)
165
+ r_cap.font.color.rgb = rgb_color(95, 105, 118)
166
+
167
+
168
+ def _add_opt_fig(doc: Any, var_name: str, cap_var: str, rgb_color: Any) -> None:
169
+ """Insert a figure + caption block only when ``var_name`` is truthy in the render context."""
170
+ doc.add_paragraph(f"{{%p if {var_name} %}}")
171
+ _add_fig(doc, f"{{{{ {var_name} }}}}", f"{{{{ {cap_var} }}}}", rgb_color)
172
+ doc.add_paragraph("{%p endif %}")
173
+
174
+
175
+ _FMU_DOMAINS: list[tuple[str, str]] = [
176
+ ("rivers_soe", "Rivers — State of Environment (SoE / RepSite)"),
177
+ ("rivers_impact", "Rivers — Impact Sites"),
178
+ ("lakes", "Lakes"),
179
+ ("estuaries", "Estuaries"),
180
+ ("coastal", "Coastal & Recreational Bathing"),
181
+ ("discharges", "Consented Point-Source Discharges"),
182
+ ]
183
+
184
+
185
+ def _add_fmu_domain_subsections(doc: Any, rgb_color: Any) -> None:
186
+ """Add conditional per-domain subsections inside an FMU loop (omitted if not present)."""
187
+ for key, label in _FMU_DOMAINS:
188
+ doc.add_paragraph(f"{{%p if fmu.{key} %}}")
189
+ _add_h3(doc, label, rgb_color)
190
+ doc.add_paragraph(f"{{%p if fmu.{key}.commentary %}}")
191
+ doc.add_paragraph(f"{{{{ fmu.{key}.commentary }}}}")
192
+ doc.add_paragraph("{%p endif %}")
193
+ _add_opt_fig(doc, f"fmu.{key}.main_fig", f"fmu.{key}.caption", rgb_color)
194
+ doc.add_paragraph("{%p endif %}")
195
+
196
+
197
+ def create_npsfm_report_template(
198
+ output_path: str | Path,
199
+ *,
200
+ config: str | Path | Config | None = None,
201
+ ) -> Path:
202
+ """Create a multi-domain NPS-FM Regional State & Trends Word (``.docx``) report template.
203
+
204
+ Structured by domain at both Regional and FMU levels:
205
+ - **Cover Page**: Banner, region, reporting period, domains covered, author, hero graphic.
206
+ - **Executive Summary**: Narrative, Regional KPI Cards, Multi-Domain Snapshot Strip,
207
+ and Key Findings bullets (``{%p for item in key_findings %}``).
208
+ - **Section 1 — Regional Overview by Domain**:
209
+ - 1.1 Rivers — State of Environment (SoE / RepSite)
210
+ - 1.2 Rivers — Impact Sites
211
+ - 1.3 Lakes
212
+ - 1.4 Estuaries
213
+ - 1.5 Coastal & Recreational Bathing
214
+ - 1.6 Consented Point-Source Discharges
215
+ - **Section 2 — FMU Chapters** (``{%p for fmu in fmus %}``): Automatic page-broken chapter
216
+ per FMU with overview figure, conditional per-domain subsections (``fmu.rivers_soe``,
217
+ ``fmu.rivers_impact``, ``fmu.lakes``, ``fmu.estuaries``, ``fmu.coastal``,
218
+ ``fmu.discharges``), and FMU metrics summary table.
219
+ - **Appendix**: Methodology, Preliminary Results & appended styled summary tables.
220
+
221
+ Returns:
222
+ Resolved :class:`pathlib.Path` to the written ``.docx`` template.
223
+ """
224
+ from docx.shared import Pt
225
+
226
+ cfg = ensure_config(config or "npsfm_lawa")
227
+ out_path = Path(output_path)
228
+ out_path.parent.mkdir(parents=True, exist_ok=True)
229
+
230
+ doc, xml_helpers, accent_rgb, _, rgb_color = _setup_a4_document(
231
+ cfg,
232
+ header_text="{{ report_title }} | {{ reporting_period }}",
233
+ footer_text="NPS-FM State & Trends Report — {{ region_name }} | {{ author }}",
234
+ )
235
+
236
+ _add_cover_page(
237
+ doc,
238
+ banner_hex="#005f83",
239
+ badge_text="National Policy Statement for Freshwater Management (NPS-FM)",
240
+ xml_helpers=xml_helpers,
241
+ accent_rgb=accent_rgb,
242
+ rgb_color=rgb_color,
243
+ )
244
+
245
+ # Executive Summary
246
+ _add_h1(doc, "Executive Summary", accent_rgb)
247
+ doc.add_paragraph("{{ executive_summary }}")
248
+ _add_opt_fig(doc, "kpi_fig", "kpi_caption", rgb_color)
249
+ _add_opt_fig(doc, "domain_strip_fig", "domain_strip_caption", rgb_color)
250
+
251
+ _add_h2(doc, "Key Regional Findings", rgb_color)
252
+ doc.add_paragraph("{%p for item in key_findings %}")
253
+ p_b = doc.add_paragraph(style="List Bullet")
254
+ p_b.add_run("{{ item }}")
255
+ doc.add_paragraph("{%p endfor %}")
256
+ doc.add_page_break()
257
+
258
+ # Section 1: Regional Overview split cleanly by Domain
259
+ _add_h1(doc, "1. Regional Overview — By Environmental Domain", accent_rgb)
260
+ doc.add_paragraph("{{ regional_overview_intro }}")
261
+
262
+ _add_h2(doc, "1.1 Rivers — State of Environment (SoE / RepSite)", rgb_color)
263
+ doc.add_paragraph("{{ rivers_soe_commentary or rivers_commentary }}")
264
+ _add_opt_fig(doc, "regional_map_fig", "regional_map_caption", rgb_color)
265
+ _add_opt_fig(doc, "state_trend_butterfly_fig", "state_trend_butterfly_caption", rgb_color)
266
+ _add_opt_fig(doc, "regional_heatmap_fig", "regional_heatmap_caption", rgb_color)
267
+ _add_opt_fig(doc, "risk_matrix_fig", "risk_matrix_caption", rgb_color)
268
+
269
+ doc.add_paragraph("{%p if rivers_impact_commentary or rivers_impact_fig %}")
270
+ _add_h2(doc, "1.2 Rivers — Impact Sites", rgb_color)
271
+ doc.add_paragraph("{{ rivers_impact_commentary }}")
272
+ _add_opt_fig(doc, "rivers_impact_fig", "rivers_impact_caption", rgb_color)
273
+ doc.add_paragraph("{%p endif %}")
274
+
275
+ _add_h2(doc, "1.3 Lakes — Trophic Status & Attribute Grades", rgb_color)
276
+ doc.add_paragraph("{{ lakes_commentary }}")
277
+ _add_opt_fig(doc, "lakes_fig", "lakes_caption", rgb_color)
278
+
279
+ _add_h2(doc, "1.4 Estuaries", rgb_color)
280
+ doc.add_paragraph("{{ estuaries_commentary or estuaries_coastal_commentary }}")
281
+ _add_opt_fig(doc, "estuary_coastal_fig", "estuary_coastal_caption", rgb_color)
282
+
283
+ _add_h2(doc, "1.5 Coastal & Recreational Bathing", rgb_color)
284
+ doc.add_paragraph("{{ coastal_commentary }}")
285
+ _add_opt_fig(doc, "swim_calendar_fig", "swim_calendar_caption", rgb_color)
286
+
287
+ _add_h2(doc, "1.6 Consented Point-Source Discharges", rgb_color)
288
+ doc.add_paragraph("{{ discharges_commentary }}")
289
+ _add_opt_fig(doc, "discharges_fig", "discharges_caption", rgb_color)
290
+
291
+ # Section 2: FMU-by-FMU Chapters with Conditional Domain Subsections
292
+ doc.add_page_break()
293
+ _add_h1(doc, "2. Freshwater Management Unit (FMU) Results", accent_rgb)
294
+ doc.add_paragraph("{{ fmu_section_intro }}")
295
+
296
+ doc.add_paragraph("{%p for fmu in fmus %}")
297
+ _add_h2(doc, "2.{{ loop.index }} {{ fmu.name }} FMU", rgb_color)
298
+ doc.add_paragraph("{{ fmu.summary }}")
299
+ _add_opt_fig(doc, "fmu.overview_fig", "fmu.overview_caption", rgb_color)
300
+
301
+ # Conditional per-domain subsections inside this FMU
302
+ _add_fmu_domain_subsections(doc, rgb_color)
303
+
304
+ doc.add_paragraph("{%p if fmu.domain_notes %}")
305
+ p_dom = doc.add_paragraph()
306
+ r_dom = p_dom.add_run("FMU Domain Summary: ")
307
+ r_dom.bold = True
308
+ p_dom.add_run("{{ fmu.domain_notes }}")
309
+ doc.add_paragraph("{%p endif %}")
310
+
311
+ # Per-FMU summary metrics table
312
+ tbl = doc.add_table(rows=4, cols=4)
313
+ headers = ["Domain / Metric", "Sites (n)", "Current State", "10-Yr Trend"]
314
+ for idx, htxt in enumerate(headers):
315
+ cell = tbl.rows[0].cells[idx]
316
+ cell.text = htxt
317
+ _set_cell_shading(cell, "#005f83", xml_helpers)
318
+ for run in cell.paragraphs[0].runs:
319
+ run.bold = True
320
+ run.font.size = Pt(9.5)
321
+ run.font.color.rgb = rgb_color(255, 255, 255)
322
+ tbl.rows[1].cells[0].text = "{%tr for row in fmu.metrics %}"
323
+ tbl.rows[2].cells[0].text = "{{ row.metric }}"
324
+ tbl.rows[2].cells[1].text = "{{ row.n_sites }}"
325
+ tbl.rows[2].cells[2].text = "{{ row.state }}"
326
+ tbl.rows[2].cells[3].text = "{{ row.trend }}"
327
+ tbl.rows[3].cells[0].text = "{%tr endfor %}"
328
+
329
+ doc.add_paragraph("{%p if not loop.last %}")
330
+ doc.add_page_break()
331
+ doc.add_paragraph("{%p endif %}")
332
+ doc.add_paragraph("{%p endfor %}")
333
+
334
+ # Appendix
335
+ doc.add_page_break()
336
+ _add_h1(doc, "Appendix — Methods, Preliminary Results & Summary Tables", accent_rgb)
337
+ doc.add_paragraph("{{ appendix_methods }}")
338
+
339
+ doc.save(str(out_path))
340
+ return out_path
341
+
342
+
343
+ def create_one_plan_report_template(
344
+ output_path: str | Path,
345
+ *,
346
+ config: str | Path | Config | None = None,
347
+ ) -> Path:
348
+ """Create a Horizons One Plan Compliance Word (``.docx``) report template.
349
+
350
+ Structured by domain at both Regional and FMU/WMS levels:
351
+ - **Cover Page**: Horizons navy/teal cover banner, reporting period, domains, author.
352
+ - **Executive Summary**: Narrative, Regional Compliance KPI Cards, Multi-Domain
353
+ Compliance Strip, Pass/Fail Donut Grid, and Key Findings bullets.
354
+ - **Section 1 — Regional One Plan Compliance by Domain**:
355
+ - 1.1 Rivers — Representative (SoE) Sites
356
+ - 1.2 Rivers — Impact Sites
357
+ - 1.3 Lakes
358
+ - 1.4 Estuaries
359
+ - 1.5 Coastal & Recreational Bathing
360
+ - 1.6 Consented Point-Source Discharges
361
+ - **Section 2 — FMU & Water Management Subzone (WMS) Chapters** (``{%p for fmu in fmus %}``):
362
+ Per-FMU overview, conditional per-domain subsections (``fmu.rivers_soe``,
363
+ ``fmu.rivers_impact``, ``fmu.lakes``, ``fmu.estuaries``, ``fmu.coastal``,
364
+ ``fmu.discharges``), and WMS subzone compliance table.
365
+ - **Appendix**: Data Sufficiency (``nOK``: ``OK`` vs ``Insufficient data`` /
366
+ ``Insufficient FLOW data``) & appended summary tables.
367
+
368
+ Returns:
369
+ Resolved :class:`pathlib.Path` to the written ``.docx`` template.
370
+ """
371
+ from docx.shared import Pt
372
+
373
+ cfg = ensure_config(config or "horizons_one_plan")
374
+ out_path = Path(output_path)
375
+ out_path.parent.mkdir(parents=True, exist_ok=True)
376
+
377
+ doc, xml_helpers, accent_rgb, _, rgb_color = _setup_a4_document(
378
+ cfg,
379
+ header_text="{{ report_title }} | {{ reporting_period }}",
380
+ footer_text="One Plan Compliance Report — {{ region_name }} | {{ author }}",
381
+ )
382
+
383
+ _add_cover_page(
384
+ doc,
385
+ banner_hex="#1b365d",
386
+ badge_text="Horizons Regional Council — One Plan (Schedule E) Compliance",
387
+ xml_helpers=xml_helpers,
388
+ accent_rgb=accent_rgb,
389
+ rgb_color=rgb_color,
390
+ )
391
+
392
+ # Executive Summary
393
+ _add_h1(doc, "Executive Summary", accent_rgb)
394
+ doc.add_paragraph("{{ executive_summary }}")
395
+ _add_opt_fig(doc, "kpi_fig", "kpi_caption", rgb_color)
396
+ _add_opt_fig(doc, "domain_strip_fig", "domain_strip_caption", rgb_color)
397
+ _add_opt_fig(doc, "donuts_fig", "donuts_caption", rgb_color)
398
+
399
+ _add_h2(doc, "Key Compliance Findings", rgb_color)
400
+ doc.add_paragraph("{%p for item in key_findings %}")
401
+ p_b = doc.add_paragraph(style="List Bullet")
402
+ p_b.add_run("{{ item }}")
403
+ doc.add_paragraph("{%p endfor %}")
404
+ doc.add_page_break()
405
+
406
+ # Section 1: Regional Compliance split cleanly by Domain
407
+ _add_h1(doc, "1. Regional One Plan Compliance — By Environmental Domain", accent_rgb)
408
+ doc.add_paragraph("{{ regional_overview_intro }}")
409
+
410
+ _add_h2(doc, "1.1 Rivers — Representative (SoE) Sites", rgb_color)
411
+ doc.add_paragraph("{{ rivers_soe_commentary or rivers_commentary }}")
412
+ _add_opt_fig(doc, "pass_fail_bar_fig", "pass_fail_bar_caption", rgb_color)
413
+ _add_opt_fig(doc, "bullet_scorecard_fig", "bullet_scorecard_caption", rgb_color)
414
+ _add_opt_fig(doc, "regional_map_fig", "regional_map_caption", rgb_color)
415
+
416
+ doc.add_paragraph("{%p if rivers_impact_commentary or rivers_impact_fig %}")
417
+ _add_h2(doc, "1.2 Rivers — Impact Sites", rgb_color)
418
+ doc.add_paragraph("{{ rivers_impact_commentary }}")
419
+ _add_opt_fig(doc, "rivers_impact_fig", "rivers_impact_caption", rgb_color)
420
+ doc.add_paragraph("{%p endif %}")
421
+
422
+ doc.add_paragraph("{%p if lakes_commentary or lakes_fig %}")
423
+ _add_h2(doc, "1.3 Lakes", rgb_color)
424
+ doc.add_paragraph("{{ lakes_commentary }}")
425
+ _add_opt_fig(doc, "lakes_fig", "lakes_caption", rgb_color)
426
+ doc.add_paragraph("{%p endif %}")
427
+
428
+ _add_h2(doc, "1.4 Estuaries & Coastal / Recreational Bathing", rgb_color)
429
+ doc.add_paragraph("{{ lakes_coastal_commentary or coastal_commentary }}")
430
+ _add_opt_fig(doc, "lakes_coastal_fig", "lakes_coastal_caption", rgb_color)
431
+
432
+ _add_h2(doc, "1.5 Consented Point-Source Discharges", rgb_color)
433
+ doc.add_paragraph("{{ discharges_commentary }}")
434
+ _add_opt_fig(doc, "discharge_card_fig", "discharge_card_caption", rgb_color)
435
+
436
+ # Section 2: FMU & Water Management Subzone (WMS) Chapters with Conditional Domains
437
+ doc.add_page_break()
438
+ _add_h1(doc, "2. FMU & Water Management Subzone (WMS) Results", accent_rgb)
439
+ doc.add_paragraph("{{ fmu_section_intro }}")
440
+
441
+ doc.add_paragraph("{%p for fmu in fmus %}")
442
+ _add_h2(doc, "2.{{ loop.index }} {{ fmu.name }} FMU", rgb_color)
443
+ doc.add_paragraph("{{ fmu.summary }}")
444
+ _add_opt_fig(doc, "fmu.overview_fig", "fmu.overview_caption", rgb_color)
445
+
446
+ # Conditional per-domain subsections inside this FMU
447
+ _add_fmu_domain_subsections(doc, rgb_color)
448
+
449
+ doc.add_paragraph("{%p if fmu.domain_notes %}")
450
+ p_dom = doc.add_paragraph()
451
+ r_dom = p_dom.add_run("FMU Domain & Subzone Summary: ")
452
+ r_dom.bold = True
453
+ p_dom.add_run("{{ fmu.domain_notes }}")
454
+ doc.add_paragraph("{%p endif %}")
455
+
456
+ # Per-FMU WMS Subzone compliance table
457
+ tbl = doc.add_table(rows=4, cols=4)
458
+ headers = ["Water Management Subzone", "Site Group", "% Passing", "Status"]
459
+ for idx, htxt in enumerate(headers):
460
+ cell = tbl.rows[0].cells[idx]
461
+ cell.text = htxt
462
+ _set_cell_shading(cell, "#1b365d", xml_helpers)
463
+ for run in cell.paragraphs[0].runs:
464
+ run.bold = True
465
+ run.font.size = Pt(9.5)
466
+ run.font.color.rgb = rgb_color(255, 255, 255)
467
+ tbl.rows[1].cells[0].text = "{%tr for row in fmu.subzones %}"
468
+ tbl.rows[2].cells[0].text = "{{ row.subzone }}"
469
+ tbl.rows[2].cells[1].text = "{{ row.site_group }}"
470
+ tbl.rows[2].cells[2].text = "{{ row.pct_pass }}"
471
+ tbl.rows[2].cells[3].text = "{{ row.status }}"
472
+ tbl.rows[3].cells[0].text = "{%tr endfor %}"
473
+
474
+ doc.add_paragraph("{%p if not loop.last %}")
475
+ doc.add_page_break()
476
+ doc.add_paragraph("{%p endif %}")
477
+ doc.add_paragraph("{%p endfor %}")
478
+
479
+ # Appendix
480
+ doc.add_page_break()
481
+ _add_h1(doc, "Appendix — Data Sufficiency (nOK) & Regional Tables", accent_rgb)
482
+ doc.add_paragraph("{{ data_sufficiency_notes }}")
483
+
484
+ doc.save(str(out_path))
485
+ return out_path