forked from spaulins-usgs/flopy_netcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_rstfiles.py
137 lines (116 loc) · 5.29 KB
/
create_rstfiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
from pathlib import Path
project_root_path = Path(__file__).parent.parent
def get_section(f):
lines = Path(f).open().readlines()
line = next(iter([l for l in lines if "# section:" in l]), None)
section = "misc" if not line else line.rpartition(":")[2].strip()
return section
def create_toc_section(f, title, stems, upper_case=False):
if upper_case:
title = title.upper()
line = f"{title}\n" + len(title) * "-" + "\n\n"
line += ".. toctree::\n :maxdepth: 2\n\n"
for stem in stems:
line += f" Notebooks/{stem}\n"
line += "\n\n"
f.write(line)
def create_gallery_section(f, name, title, stems):
line = f"{title}\n" + len(title) * "-" + "\n\n"
line += f".. nbgallery::\n :name: {name}\n\n"
for stem in stems:
line += f" Notebooks/{stem}\n"
line += "\n\n"
f.write(line)
def create_tutorials_rst():
rst_path = project_root_path / ".docs" / "tutorials.rst"
nbs_path = project_root_path / ".docs" / "Notebooks"
filenames = sorted(
[
path.name
for path in nbs_path.rglob("*.py")
if "tutorial" in path.name
]
)
print(f"Creating {rst_path}")
with open(rst_path, "w") as rst_file:
rst_file.write("Tutorials\n=========\n\n")
rst_file.write(
"The following tutorials demonstrate basic FloPy features and usage with MODFLOW 2005, MODFLOW 6, and related programs.\n\n"
)
sections = {
"flopy": {"title": "FloPy", "files": []},
"mf6": {"title": "MODFLOW 6", "files": []},
"mf2005": {"title": "MODFLOW-2005", "files": []},
"lgr": {"title": "MODFLOW-LGR", "files": []},
"nwt": {"title": "MODFLOW-NWT", "files": []},
"mt3dms": {"title": "MT3DMS", "files": []},
"pest": {"title": "PEST", "files": []},
"misc": {"title": "Miscellaneous", "files": []},
}
for file_name in filenames:
section_name = get_section(nbs_path / file_name)
sections[section_name]["files"].append(file_name)
for section_name, section in sections.items():
file_names = section["files"]
if any(file_names):
create_toc_section(
f=rst_file,
title=section["title"],
stems=[fn.rpartition(".")[0] for fn in file_names],
)
def create_examples_rst():
rst_path = project_root_path / ".docs" / "examples.rst"
nbs_path = project_root_path / ".docs" / "Notebooks"
filenames = sorted(
[
path.name
for path in nbs_path.rglob("*.py")
if "example" in path.name
]
)
print(f"Creating {rst_path}")
with open(rst_path, "w") as rst_file:
rst_file.write("Examples gallery\n================\n\n")
rst_file.write(
"The following examples illustrate the functionality of Flopy. After the `tutorials <https://flopy.readthedocs.io/en/latest/tutorials.html>`_, the examples are the best resource for learning the underlying capabilities of FloPy.\n\n"
)
sections = {
"dis": {"title": "Preprocessing and Discretization", "files": []},
"viz": {"title": "Postprocessing and Visualization", "files": []},
"export": {"title": "Exporting data", "files": []},
"flopy": {"title": "Other FloPy features", "files": []},
"mf6": {"title": "MODFLOW 6 examples", "files": []},
"mfusg": {"title": "MODFLOW USG examples", "files": []},
"mf2005": {
"title": "MODFLOW-2005/MODFLOW-NWT examples",
"files": [],
},
"modpath": {"title": "MODPATH examples", "files": []},
"mt3d": {"title": "MT3D and SEAWAT examples", "files": []},
"2016gw-paper": {
"title": "Examples from Bakker and others (2016)",
"description": "Bakker, Mark, Post, Vincent, Langevin, C. D., Hughes, J. D., White, J. T., Starn, J. J. and Fienen, M. N., 2016, Scripting MODFLOW Model Development Using Python and FloPy: Groundwater, v. 54, p. 733–739, https://doi.org/10.1111/gwat.12413.",
"files": [],
},
"2023gw-paper": {
"title": "Examples from Hughes and others (2023)",
"description": "Hughes, Joseph D., Langevin, Christian D., Paulinski, Scott R., Larsen, Joshua D., and Brakenhoff, David, 2023, FloPy Workflows for Creating Structured and Unstructured MODFLOW Models: Groundwater, https://doi.org/10.1111/gwat.13327.",
"files": [],
},
"misc": {"title": "Miscellaneous examples", "files": []},
}
for file_name in filenames:
section_name = get_section(nbs_path / file_name)
sections[section_name]["files"].append(file_name)
for section_name, section in sections.items():
file_names = section["files"]
if any(file_names):
create_gallery_section(
f=rst_file,
name=section_name,
title=section["title"],
stems=[fn.rpartition(".")[0] for fn in file_names],
)
if __name__ == "__main__":
create_tutorials_rst()
create_examples_rst()