ffmtools 0.1.0__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.
- ffmtools-0.1.0/LICENSE +21 -0
- ffmtools-0.1.0/PKG-INFO +106 -0
- ffmtools-0.1.0/README.md +87 -0
- ffmtools-0.1.0/pyproject.toml +43 -0
- ffmtools-0.1.0/setup.cfg +4 -0
- ffmtools-0.1.0/src/ffmclr/__init__.py +25 -0
- ffmtools-0.1.0/src/ffmclr/__main__.py +5 -0
- ffmtools-0.1.0/src/ffmclr/cli.py +37 -0
- ffmtools-0.1.0/src/ffmclr/core.py +191 -0
- ffmtools-0.1.0/src/ffmgen/__init__.py +32 -0
- ffmtools-0.1.0/src/ffmgen/__main__.py +5 -0
- ffmtools-0.1.0/src/ffmgen/cli.py +99 -0
- ffmtools-0.1.0/src/ffmgen/core.py +203 -0
- ffmtools-0.1.0/src/ffmgen/gui.py +315 -0
- ffmtools-0.1.0/src/ffmgen/templates/STAMFORD_basis.mdu +138 -0
- ffmtools-0.1.0/src/ffmgen/timecalc.py +107 -0
- ffmtools-0.1.0/src/ffmtools.egg-info/PKG-INFO +106 -0
- ffmtools-0.1.0/src/ffmtools.egg-info/SOURCES.txt +22 -0
- ffmtools-0.1.0/src/ffmtools.egg-info/dependency_links.txt +1 -0
- ffmtools-0.1.0/src/ffmtools.egg-info/entry_points.txt +3 -0
- ffmtools-0.1.0/src/ffmtools.egg-info/requires.txt +5 -0
- ffmtools-0.1.0/src/ffmtools.egg-info/top_level.txt +2 -0
- ffmtools-0.1.0/tests/test_ffmclr.py +90 -0
- ffmtools-0.1.0/tests/test_ffmgen.py +82 -0
ffmtools-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chih-Hung Hsu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ffmtools-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ffmtools
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Flood/flow modelling tools for 3Di subgrid: ffmgen (build .mdu files) and ffmclr (clean run outputs).
|
|
5
|
+
Author-email: aaronchh <aaronhsu219@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/AaronOET/ffmtools
|
|
8
|
+
Project-URL: Changelog, https://github.com/AaronOET/ffmtools/blob/main/CHANGELOG.md
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: pandas>=1.3
|
|
15
|
+
Requires-Dist: openpyxl>=3.0
|
|
16
|
+
Provides-Extra: test
|
|
17
|
+
Requires-Dist: pytest; extra == "test"
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# ffmtools
|
|
21
|
+
|
|
22
|
+
## ffmgen
|
|
23
|
+
|
|
24
|
+
Builds 3Di subgrid `.mdu` input files (plus a `_run_<name>.bat` launcher) from an
|
|
25
|
+
`mdu_prep.xlsx`-style workbook or an existing `.mdu`. Packaged from
|
|
26
|
+
`prototype/Singapore_003/mdugen_gui_v3_tk.py`.
|
|
27
|
+
|
|
28
|
+
### Install
|
|
29
|
+
|
|
30
|
+
```bat
|
|
31
|
+
cd D:\GITHUB_OET\ffmtools
|
|
32
|
+
pip install -e .
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### GUI
|
|
36
|
+
|
|
37
|
+
```bat
|
|
38
|
+
ffmgen :: loads .\mdu_prep.xlsx if present, else the built-in template
|
|
39
|
+
ffmgen gui STAMFORD_basis.mdu
|
|
40
|
+
python -m ffmgen
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Each tab is one parameter group (= one sheet of the workbook). The **On** checkbox
|
|
44
|
+
decides whether a line is active or written commented out with `#`.
|
|
45
|
+
`EndDate` / `EndTime` in the *time* tab are used on Preview/Generate to compute
|
|
46
|
+
`NTimesteps = (End - Start) / Dt` (rounded up if it does not divide evenly).
|
|
47
|
+
|
|
48
|
+
### Command line
|
|
49
|
+
|
|
50
|
+
```bat
|
|
51
|
+
ffmgen generate mdu_prep.xlsx -o runs -n STAMFORD_basis -t 4
|
|
52
|
+
ffmgen generate STAMFORD_basis.mdu --dry-run
|
|
53
|
+
ffmgen convert STAMFORD_basis.mdu mdu_prep.xlsx
|
|
54
|
+
ffmgen template STAMFORD_template.mdu
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Python
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import ffmgen
|
|
61
|
+
|
|
62
|
+
data = ffmgen.ensure_end_rows(ffmgen.read_any("mdu_prep.xlsx"))
|
|
63
|
+
ffmgen.update_ntimesteps(data)
|
|
64
|
+
ffmgen.write_mdu(data, out_dir="runs", name="STAMFORD_basis", threads=4)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Layout
|
|
68
|
+
|
|
69
|
+
| module | contents |
|
|
70
|
+
|---|---|
|
|
71
|
+
| `ffmgen.core` | parse/read/write `.mdu` and `.xlsx`, `to_mdu_text`, `write_mdu` |
|
|
72
|
+
| `ffmgen.timecalc` | `ensure_end_rows`, `update_ntimesteps` |
|
|
73
|
+
| `ffmgen.gui` | tkinter GUI (`App`, `run`) |
|
|
74
|
+
| `ffmgen.cli` | `ffmgen` console command |
|
|
75
|
+
| `ffmgen/templates/STAMFORD_basis.mdu` | built-in template |
|
|
76
|
+
|
|
77
|
+
## ffmclr
|
|
78
|
+
|
|
79
|
+
Removes the files a 3Di (subgrid) run leaves in a model folder. Packaged from
|
|
80
|
+
`prototype/Singapore_003/clean_3di_outputs.py`. Dry run by default.
|
|
81
|
+
|
|
82
|
+
```bat
|
|
83
|
+
ffmclr :: list what would be removed in the current folder
|
|
84
|
+
ffmclr D:\models\STAMFORD --yes :: delete
|
|
85
|
+
ffmclr --yes --keep-table :: keep the TableDataFile (faster restart)
|
|
86
|
+
ffmclr --move-to _old_run --yes :: move into <model>\_old_run instead of deleting
|
|
87
|
+
ffmclr -r :: also scan sub-folders
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Removed: `*_his.nc`, `*_map.nc`, `*_rst.nc`, `*.rst`, `*.dia`, `*_numlimdt.xyz`,
|
|
91
|
+
`*_timings.txt`, `.test`, and the `TableDataFile` named in the `.mdu`.
|
|
92
|
+
Never removed: anything referenced as input in an `.mdu`/`.ext`, and input
|
|
93
|
+
extensions (`.mdu .ini .ext .pli .pol .xyn .tim .tif .asc .rgb ...`).
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
import ffmclr
|
|
97
|
+
res = ffmclr.clean("D:/models/STAMFORD", dry_run=False, move_to="_old_run")
|
|
98
|
+
print(len(res.removed), res.failed)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Tests
|
|
102
|
+
|
|
103
|
+
```bat
|
|
104
|
+
pip install -e .[test]
|
|
105
|
+
pytest
|
|
106
|
+
```
|
ffmtools-0.1.0/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# ffmtools
|
|
2
|
+
|
|
3
|
+
## ffmgen
|
|
4
|
+
|
|
5
|
+
Builds 3Di subgrid `.mdu` input files (plus a `_run_<name>.bat` launcher) from an
|
|
6
|
+
`mdu_prep.xlsx`-style workbook or an existing `.mdu`. Packaged from
|
|
7
|
+
`prototype/Singapore_003/mdugen_gui_v3_tk.py`.
|
|
8
|
+
|
|
9
|
+
### Install
|
|
10
|
+
|
|
11
|
+
```bat
|
|
12
|
+
cd D:\GITHUB_OET\ffmtools
|
|
13
|
+
pip install -e .
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### GUI
|
|
17
|
+
|
|
18
|
+
```bat
|
|
19
|
+
ffmgen :: loads .\mdu_prep.xlsx if present, else the built-in template
|
|
20
|
+
ffmgen gui STAMFORD_basis.mdu
|
|
21
|
+
python -m ffmgen
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Each tab is one parameter group (= one sheet of the workbook). The **On** checkbox
|
|
25
|
+
decides whether a line is active or written commented out with `#`.
|
|
26
|
+
`EndDate` / `EndTime` in the *time* tab are used on Preview/Generate to compute
|
|
27
|
+
`NTimesteps = (End - Start) / Dt` (rounded up if it does not divide evenly).
|
|
28
|
+
|
|
29
|
+
### Command line
|
|
30
|
+
|
|
31
|
+
```bat
|
|
32
|
+
ffmgen generate mdu_prep.xlsx -o runs -n STAMFORD_basis -t 4
|
|
33
|
+
ffmgen generate STAMFORD_basis.mdu --dry-run
|
|
34
|
+
ffmgen convert STAMFORD_basis.mdu mdu_prep.xlsx
|
|
35
|
+
ffmgen template STAMFORD_template.mdu
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Python
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import ffmgen
|
|
42
|
+
|
|
43
|
+
data = ffmgen.ensure_end_rows(ffmgen.read_any("mdu_prep.xlsx"))
|
|
44
|
+
ffmgen.update_ntimesteps(data)
|
|
45
|
+
ffmgen.write_mdu(data, out_dir="runs", name="STAMFORD_basis", threads=4)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Layout
|
|
49
|
+
|
|
50
|
+
| module | contents |
|
|
51
|
+
|---|---|
|
|
52
|
+
| `ffmgen.core` | parse/read/write `.mdu` and `.xlsx`, `to_mdu_text`, `write_mdu` |
|
|
53
|
+
| `ffmgen.timecalc` | `ensure_end_rows`, `update_ntimesteps` |
|
|
54
|
+
| `ffmgen.gui` | tkinter GUI (`App`, `run`) |
|
|
55
|
+
| `ffmgen.cli` | `ffmgen` console command |
|
|
56
|
+
| `ffmgen/templates/STAMFORD_basis.mdu` | built-in template |
|
|
57
|
+
|
|
58
|
+
## ffmclr
|
|
59
|
+
|
|
60
|
+
Removes the files a 3Di (subgrid) run leaves in a model folder. Packaged from
|
|
61
|
+
`prototype/Singapore_003/clean_3di_outputs.py`. Dry run by default.
|
|
62
|
+
|
|
63
|
+
```bat
|
|
64
|
+
ffmclr :: list what would be removed in the current folder
|
|
65
|
+
ffmclr D:\models\STAMFORD --yes :: delete
|
|
66
|
+
ffmclr --yes --keep-table :: keep the TableDataFile (faster restart)
|
|
67
|
+
ffmclr --move-to _old_run --yes :: move into <model>\_old_run instead of deleting
|
|
68
|
+
ffmclr -r :: also scan sub-folders
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Removed: `*_his.nc`, `*_map.nc`, `*_rst.nc`, `*.rst`, `*.dia`, `*_numlimdt.xyz`,
|
|
72
|
+
`*_timings.txt`, `.test`, and the `TableDataFile` named in the `.mdu`.
|
|
73
|
+
Never removed: anything referenced as input in an `.mdu`/`.ext`, and input
|
|
74
|
+
extensions (`.mdu .ini .ext .pli .pol .xyn .tim .tif .asc .rgb ...`).
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
import ffmclr
|
|
78
|
+
res = ffmclr.clean("D:/models/STAMFORD", dry_run=False, move_to="_old_run")
|
|
79
|
+
print(len(res.removed), res.failed)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Tests
|
|
83
|
+
|
|
84
|
+
```bat
|
|
85
|
+
pip install -e .[test]
|
|
86
|
+
pytest
|
|
87
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ffmtools"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "aaronchh", email = "aaronhsu219@gmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "Flood/flow modelling tools for 3Di subgrid: ffmgen (build .mdu files) and ffmclr (clean run outputs)."
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = "MIT"
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
requires-python = ">=3.9"
|
|
16
|
+
dependencies = [
|
|
17
|
+
"pandas>=1.3",
|
|
18
|
+
"openpyxl>=3.0",
|
|
19
|
+
]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/AaronOET/ffmtools"
|
|
27
|
+
Changelog = "https://github.com/AaronOET/ffmtools/blob/main/CHANGELOG.md"
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
test = ["pytest"]
|
|
31
|
+
|
|
32
|
+
[project.scripts]
|
|
33
|
+
ffmgen = "ffmgen.cli:main"
|
|
34
|
+
ffmclr = "ffmclr.cli:main"
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["src"]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.package-data]
|
|
40
|
+
ffmgen = ["templates/*.mdu"]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
testpaths = ["tests"]
|
ffmtools-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""ffmclr - clean files generated by a 3Di (subgrid) model run.
|
|
2
|
+
|
|
3
|
+
Programmatic use::
|
|
4
|
+
|
|
5
|
+
import ffmclr
|
|
6
|
+
ffmclr.clean('path/to/model') # dry run: list only
|
|
7
|
+
ffmclr.clean('path/to/model', dry_run=False) # delete
|
|
8
|
+
ffmclr.clean('path/to/model', dry_run=False, move_to='_old_run')
|
|
9
|
+
|
|
10
|
+
Command line: ``ffmclr [MODEL_DIR] [--yes] [--keep-table] [-r] [--move-to DIR]``
|
|
11
|
+
(or ``python -m ffmclr``).
|
|
12
|
+
"""
|
|
13
|
+
from .core import (OUTPUT_PATTERNS, PROTECTED_EXT, CleanResult, is_model_dir,
|
|
14
|
+
read_mdu_references, find_generated, clean, human)
|
|
15
|
+
|
|
16
|
+
__version__ = '0.1.0'
|
|
17
|
+
|
|
18
|
+
__all__ = ['OUTPUT_PATTERNS', 'PROTECTED_EXT', 'CleanResult', 'is_model_dir',
|
|
19
|
+
'read_mdu_references', 'find_generated', 'clean', 'human', 'main', '__version__']
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def main(argv=None):
|
|
23
|
+
"""Console entry point (see ffmclr.cli)."""
|
|
24
|
+
from .cli import main as _main
|
|
25
|
+
return _main(argv)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Command line interface.
|
|
2
|
+
|
|
3
|
+
ffmclr dry run in the current folder: list what would be removed
|
|
4
|
+
ffmclr --yes actually delete
|
|
5
|
+
ffmclr --yes --keep-table delete but keep the table file (faster restart)
|
|
6
|
+
ffmclr PATH\\TO\\MODEL --yes clean another model folder
|
|
7
|
+
ffmclr --move-to _old_run --yes move instead of delete
|
|
8
|
+
"""
|
|
9
|
+
import argparse
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
from . import __version__
|
|
13
|
+
from .core import clean
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def main(argv=None):
|
|
17
|
+
ap = argparse.ArgumentParser(prog='ffmclr', description="Clean files generated by a 3Di model run.")
|
|
18
|
+
ap.add_argument('--version', action='version', version='%(prog)s ' + __version__)
|
|
19
|
+
ap.add_argument("model_dir", nargs="?", default='.',
|
|
20
|
+
help="model folder (default: current folder)")
|
|
21
|
+
ap.add_argument("--yes", "-y", action="store_true", help="actually delete (default is dry run)")
|
|
22
|
+
ap.add_argument("--keep-table", action="store_true", help="keep the TableDataFile")
|
|
23
|
+
ap.add_argument("--recursive", "-r", action="store_true", help="also scan sub-folders")
|
|
24
|
+
ap.add_argument("--move-to", metavar="DIR",
|
|
25
|
+
help="move files into DIR (relative to the model folder) instead of deleting")
|
|
26
|
+
a = ap.parse_args(argv)
|
|
27
|
+
try:
|
|
28
|
+
res = clean(a.model_dir, dry_run=not a.yes, keep_table=a.keep_table,
|
|
29
|
+
recursive=a.recursive, move_to=a.move_to)
|
|
30
|
+
except ValueError as e:
|
|
31
|
+
print(e, file=sys.stderr)
|
|
32
|
+
return 2
|
|
33
|
+
return 1 if res.failed else 0
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if __name__ == '__main__':
|
|
37
|
+
sys.exit(main())
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Find and remove files generated by a 3Di (subgrid) model run.
|
|
2
|
+
|
|
3
|
+
What counts as "generated":
|
|
4
|
+
* <name>_his.nc, <name>_map.nc history / map output
|
|
5
|
+
* *_rst.nc, *.rst restart files
|
|
6
|
+
* *.dia diagnostic log (e.g. subgridf90.dia)
|
|
7
|
+
* *_numlimdt.xyz, *_timings.txt solver diagnostics
|
|
8
|
+
* .test empty marker file written at start-up
|
|
9
|
+
* the TableDataFile named in the .mdu files (e.g. "l") - the pre-computed
|
|
10
|
+
subgrid table; 3Di rebuilds it on the next run (keep_table=True keeps it)
|
|
11
|
+
|
|
12
|
+
Safety:
|
|
13
|
+
* Any file referenced as an input in an .mdu or .ext file is never removed
|
|
14
|
+
(except the TableDataFile, see above).
|
|
15
|
+
* Input extensions (.mdu .ini .ext .pli .pol .xyn .tim .tif .asc .rgb ...)
|
|
16
|
+
are never removed, whatever their name.
|
|
17
|
+
* Only the model root folder is scanned unless recursive=True.
|
|
18
|
+
* The move_to folder (if inside the model folder) is never scanned, so a
|
|
19
|
+
second run does not pick up files moved there earlier.
|
|
20
|
+
"""
|
|
21
|
+
import fnmatch
|
|
22
|
+
import re
|
|
23
|
+
import shutil
|
|
24
|
+
from dataclasses import dataclass, field
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
__all__ = ['OUTPUT_PATTERNS', 'PROTECTED_EXT', 'CleanResult',
|
|
28
|
+
'is_model_dir', 'read_mdu_references', 'find_generated', 'clean', 'human']
|
|
29
|
+
|
|
30
|
+
OUTPUT_PATTERNS = [
|
|
31
|
+
"*_his.nc", "*_map.nc", "*_rst.nc", "*.rst",
|
|
32
|
+
"*.dia", "*_numlimdt.xyz", "*_timings.txt", ".test",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
PROTECTED_EXT = {
|
|
36
|
+
".mdu", ".ini", ".ext", ".pli", ".pol", ".xyn", ".tim", ".tif", ".tiff",
|
|
37
|
+
".asc", ".rgb", ".zrgb", ".ldb", ".xyz_input", ".dat", ".cfg", ".py",
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
KEY_VALUE = re.compile(r"^\s*([A-Za-z0-9_]+)\s*=\s*([^#]*)")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def is_model_dir(model_dir):
|
|
44
|
+
"""True if the folder contains at least one .mdu file."""
|
|
45
|
+
p = Path(model_dir)
|
|
46
|
+
return p.is_dir() and any(p.glob("*.mdu"))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def read_mdu_references(model_dir):
|
|
50
|
+
"""Return (set of input paths referenced, set of table-data paths).
|
|
51
|
+
|
|
52
|
+
Scans the .mdu files in ``model_dir`` and every .ext file below it.
|
|
53
|
+
"""
|
|
54
|
+
model_dir = Path(model_dir)
|
|
55
|
+
inputs, tables = set(), set()
|
|
56
|
+
for cfg in list(model_dir.glob("*.mdu")) + list(model_dir.rglob("*.ext")):
|
|
57
|
+
try:
|
|
58
|
+
text = cfg.read_text(errors="ignore")
|
|
59
|
+
except OSError:
|
|
60
|
+
continue
|
|
61
|
+
for line in text.splitlines():
|
|
62
|
+
m = KEY_VALUE.match(line)
|
|
63
|
+
if not m:
|
|
64
|
+
continue
|
|
65
|
+
key, value = m.group(1).lower(), m.group(2).strip()
|
|
66
|
+
if not value:
|
|
67
|
+
continue
|
|
68
|
+
# value may be "a b" for some keys; check every token as a file name
|
|
69
|
+
for token in value.split():
|
|
70
|
+
p = (cfg.parent / token).resolve() if cfg.suffix.lower() == ".ext" \
|
|
71
|
+
else (model_dir / token).resolve()
|
|
72
|
+
if key == "tabledatafile":
|
|
73
|
+
tables.add(p)
|
|
74
|
+
elif p.exists() and p.is_file():
|
|
75
|
+
inputs.add(p)
|
|
76
|
+
return inputs, tables
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _is_within(path, folder):
|
|
80
|
+
try:
|
|
81
|
+
path.relative_to(folder)
|
|
82
|
+
return True
|
|
83
|
+
except ValueError:
|
|
84
|
+
return False
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def find_generated(model_dir, recursive=False, keep_table=False, exclude=()):
|
|
88
|
+
"""List generated files as sorted ``[(Path, reason), ...]``.
|
|
89
|
+
|
|
90
|
+
``exclude``: folders whose contents are skipped (e.g. the move_to folder).
|
|
91
|
+
"""
|
|
92
|
+
model_dir = Path(model_dir).resolve()
|
|
93
|
+
excl = [Path(e).resolve() for e in exclude]
|
|
94
|
+
inputs, tables = read_mdu_references(model_dir)
|
|
95
|
+
files = model_dir.rglob("*") if recursive else model_dir.iterdir()
|
|
96
|
+
found = []
|
|
97
|
+
for f in files:
|
|
98
|
+
if not f.is_file():
|
|
99
|
+
continue
|
|
100
|
+
rp = f.resolve()
|
|
101
|
+
if any(_is_within(rp, e) for e in excl):
|
|
102
|
+
continue
|
|
103
|
+
if rp in tables:
|
|
104
|
+
if not keep_table:
|
|
105
|
+
found.append((f, "table data (rebuilt next run)"))
|
|
106
|
+
continue
|
|
107
|
+
if rp in inputs or f.suffix.lower() in PROTECTED_EXT:
|
|
108
|
+
continue
|
|
109
|
+
for pat in OUTPUT_PATTERNS:
|
|
110
|
+
if fnmatch.fnmatch(f.name.lower(), pat.lower()):
|
|
111
|
+
found.append((f, f"matches {pat}"))
|
|
112
|
+
break
|
|
113
|
+
return sorted(found)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def human(n):
|
|
117
|
+
"""Byte count -> '1.5 MB'."""
|
|
118
|
+
for unit in ("B", "KB", "MB", "GB"):
|
|
119
|
+
if n < 1024:
|
|
120
|
+
return f"{n:.1f} {unit}"
|
|
121
|
+
n /= 1024
|
|
122
|
+
return f"{n:.1f} TB"
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@dataclass
|
|
126
|
+
class CleanResult:
|
|
127
|
+
model_dir: Path
|
|
128
|
+
targets: list # [(Path, reason)] found
|
|
129
|
+
removed: list = field(default_factory=list) # Paths deleted / moved
|
|
130
|
+
failed: list = field(default_factory=list) # [(Path, error message)]
|
|
131
|
+
dest: Path = None # move_to folder, None = deleted
|
|
132
|
+
dry_run: bool = True
|
|
133
|
+
total_bytes: int = 0 # size of all targets
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _resolve_dest(model_dir, move_to):
|
|
137
|
+
if not move_to:
|
|
138
|
+
return None
|
|
139
|
+
dest = Path(move_to)
|
|
140
|
+
return (dest if dest.is_absolute() else model_dir / dest).resolve()
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def clean(model_dir='.', dry_run=True, keep_table=False, recursive=False,
|
|
144
|
+
move_to=None, verbose=True):
|
|
145
|
+
"""Find generated files and (unless ``dry_run``) delete them or move them to ``move_to``.
|
|
146
|
+
|
|
147
|
+
``move_to`` may be relative to ``model_dir``. Raises ValueError if
|
|
148
|
+
``model_dir`` is not a folder containing an .mdu file. Returns a CleanResult.
|
|
149
|
+
"""
|
|
150
|
+
log = print if verbose else (lambda *a, **k: None)
|
|
151
|
+
model_dir = Path(model_dir).resolve()
|
|
152
|
+
if not model_dir.is_dir():
|
|
153
|
+
raise ValueError(f"Not a folder: {model_dir}")
|
|
154
|
+
if not is_model_dir(model_dir):
|
|
155
|
+
raise ValueError(f"No .mdu file in {model_dir} - is this a 3Di model folder?")
|
|
156
|
+
|
|
157
|
+
dest = _resolve_dest(model_dir, move_to)
|
|
158
|
+
targets = find_generated(model_dir, recursive, keep_table, exclude=[dest] if dest else ())
|
|
159
|
+
res = CleanResult(model_dir=model_dir, targets=targets, dest=dest, dry_run=dry_run)
|
|
160
|
+
if not targets:
|
|
161
|
+
log("Nothing to clean.")
|
|
162
|
+
return res
|
|
163
|
+
|
|
164
|
+
sizes = {f: f.stat().st_size for f, _ in targets}
|
|
165
|
+
res.total_bytes = sum(sizes.values())
|
|
166
|
+
log(f"Model folder: {model_dir}")
|
|
167
|
+
for f, why in targets:
|
|
168
|
+
log(f" {f.relative_to(model_dir)!s:<30} {human(sizes[f]):>10} ({why})")
|
|
169
|
+
log(f"{len(targets)} file(s), {human(res.total_bytes)}")
|
|
170
|
+
|
|
171
|
+
if dry_run:
|
|
172
|
+
log("\nDry run - nothing removed. Re-run with --yes to clean.")
|
|
173
|
+
return res
|
|
174
|
+
|
|
175
|
+
if dest:
|
|
176
|
+
dest.mkdir(parents=True, exist_ok=True)
|
|
177
|
+
for f, _ in targets:
|
|
178
|
+
try:
|
|
179
|
+
if dest:
|
|
180
|
+
target = dest / f.relative_to(model_dir)
|
|
181
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
182
|
+
shutil.move(str(f), str(target))
|
|
183
|
+
else:
|
|
184
|
+
f.unlink()
|
|
185
|
+
res.removed.append(f)
|
|
186
|
+
except OSError as e:
|
|
187
|
+
res.failed.append((f, str(e)))
|
|
188
|
+
log(f" ! could not remove {f.name}: {e}")
|
|
189
|
+
action = f"moved to {dest}" if dest else "deleted"
|
|
190
|
+
log(f"{len(res.removed)} file(s) {action}." + (f" {len(res.failed)} failed." if res.failed else ""))
|
|
191
|
+
return res
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""ffmgen - build 3Di subgrid .mdu files from xlsx/mdu parameter sets.
|
|
2
|
+
|
|
3
|
+
Programmatic use::
|
|
4
|
+
|
|
5
|
+
import ffmgen
|
|
6
|
+
data = ffmgen.ensure_end_rows(ffmgen.read_any('mdu_prep.xlsx'))
|
|
7
|
+
ffmgen.update_ntimesteps(data)
|
|
8
|
+
ffmgen.write_mdu(data, out_dir='.', name='STAMFORD_basis')
|
|
9
|
+
|
|
10
|
+
Desktop GUI: ``ffmgen`` (or ``ffmgen gui``, ``python -m ffmgen``).
|
|
11
|
+
"""
|
|
12
|
+
from .core import (PAR_LIST, DEFAULT_NAME, EXE_DEFAULT, OMP_DEFAULT,
|
|
13
|
+
default_mdu_text, default_data,
|
|
14
|
+
parse_mdu, read_mdu, read_xlsx, write_xlsx, read_any,
|
|
15
|
+
check_groups, to_mdu_text, bat_text, name_from_path, write_mdu)
|
|
16
|
+
from .timecalc import ensure_end_rows, update_ntimesteps
|
|
17
|
+
|
|
18
|
+
__version__ = '0.1.0'
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
'PAR_LIST', 'DEFAULT_NAME', 'EXE_DEFAULT', 'OMP_DEFAULT',
|
|
22
|
+
'default_mdu_text', 'default_data',
|
|
23
|
+
'parse_mdu', 'read_mdu', 'read_xlsx', 'write_xlsx', 'read_any',
|
|
24
|
+
'check_groups', 'to_mdu_text', 'bat_text', 'name_from_path', 'write_mdu',
|
|
25
|
+
'ensure_end_rows', 'update_ntimesteps', 'main', '__version__',
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def main(argv=None):
|
|
30
|
+
"""Console entry point (see ffmgen.cli)."""
|
|
31
|
+
from .cli import main as _main
|
|
32
|
+
return _main(argv)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Command line interface.
|
|
2
|
+
|
|
3
|
+
ffmgen open the GUI (loads ./mdu_prep.xlsx if present)
|
|
4
|
+
ffmgen gui [SRC] open the GUI with SRC (.xlsx or .mdu)
|
|
5
|
+
ffmgen generate SRC [options] write <name>.mdu (+ _run_<name>.bat) without the GUI
|
|
6
|
+
ffmgen convert SRC DST .mdu <-> .xlsx
|
|
7
|
+
ffmgen template [DST] write the built-in template .mdu (stdout if no DST)
|
|
8
|
+
"""
|
|
9
|
+
import argparse
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
from . import __version__
|
|
13
|
+
from .core import (DEFAULT_NAME, EXE_DEFAULT, OMP_DEFAULT, check_groups, default_data,
|
|
14
|
+
default_mdu_text, name_from_path, read_any, to_mdu_text, write_mdu, write_xlsx)
|
|
15
|
+
from .timecalc import ensure_end_rows, update_ntimesteps
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _build_parser():
|
|
19
|
+
p = argparse.ArgumentParser(prog='ffmgen', description='Build 3Di subgrid .mdu files.')
|
|
20
|
+
p.add_argument('--version', action='version', version='%(prog)s ' + __version__)
|
|
21
|
+
sub = p.add_subparsers(dest='cmd')
|
|
22
|
+
|
|
23
|
+
g = sub.add_parser('gui', help='open the desktop GUI')
|
|
24
|
+
g.add_argument('src', nargs='?', help='.xlsx or .mdu to load at start (default ./mdu_prep.xlsx)')
|
|
25
|
+
g.add_argument('-o', '--out-dir', default='.', help='initial output folder')
|
|
26
|
+
|
|
27
|
+
gen = sub.add_parser('generate', help='write .mdu (+ .bat) from an .xlsx/.mdu')
|
|
28
|
+
gen.add_argument('src', nargs='?', help='.xlsx or .mdu (omit to use the built-in template)')
|
|
29
|
+
gen.add_argument('-o', '--out-dir', default='.', help='output folder (default .)')
|
|
30
|
+
gen.add_argument('-n', '--name', help='MDU name (default: derived from SRC)')
|
|
31
|
+
gen.add_argument('--exe', default=EXE_DEFAULT, help='executable in the .bat (default %(default)s)')
|
|
32
|
+
gen.add_argument('-t', '--threads', type=int, default=OMP_DEFAULT, help='OMP_NUM_THREADS (default %(default)s)')
|
|
33
|
+
gen.add_argument('--no-bat', action='store_true', help='do not write _run_<name>.bat')
|
|
34
|
+
gen.add_argument('--keep-ntimesteps', action='store_true',
|
|
35
|
+
help='do not recompute NTimesteps from EndDate/EndTime')
|
|
36
|
+
gen.add_argument('--dry-run', action='store_true', help='print the .mdu instead of writing files')
|
|
37
|
+
|
|
38
|
+
c = sub.add_parser('convert', help='convert between .mdu and .xlsx')
|
|
39
|
+
c.add_argument('src')
|
|
40
|
+
c.add_argument('dst', help='output .xlsx or .mdu')
|
|
41
|
+
|
|
42
|
+
t = sub.add_parser('template', help='write the built-in template .mdu')
|
|
43
|
+
t.add_argument('dst', nargs='?')
|
|
44
|
+
return p
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _generate(a):
|
|
48
|
+
if a.src:
|
|
49
|
+
data, name = read_any(a.src), name_from_path(a.src)
|
|
50
|
+
else:
|
|
51
|
+
data, name = default_data(), DEFAULT_NAME
|
|
52
|
+
data = ensure_end_rows(data)
|
|
53
|
+
check_groups(data.keys())
|
|
54
|
+
if not a.keep_ntimesteps:
|
|
55
|
+
update_ntimesteps(data)
|
|
56
|
+
if a.dry_run:
|
|
57
|
+
sys.stdout.write(to_mdu_text(data))
|
|
58
|
+
return 0
|
|
59
|
+
write_mdu(data, out_dir=a.out_dir, name=a.name or name,
|
|
60
|
+
write_bat=not a.no_bat, threads=a.threads, exe=a.exe)
|
|
61
|
+
return 0
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _convert(a):
|
|
65
|
+
data = read_any(a.src)
|
|
66
|
+
if a.dst.lower().endswith('.xlsx'):
|
|
67
|
+
write_xlsx(ensure_end_rows(data), a.dst)
|
|
68
|
+
elif a.dst.lower().endswith('.mdu'):
|
|
69
|
+
with open(a.dst, 'wt', encoding='utf-8', newline='') as f:
|
|
70
|
+
f.write(to_mdu_text(data))
|
|
71
|
+
else:
|
|
72
|
+
raise SystemExit('dst must end with .xlsx or .mdu')
|
|
73
|
+
print('Wrote', a.dst)
|
|
74
|
+
return 0
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def main(argv=None):
|
|
78
|
+
a = _build_parser().parse_args(argv)
|
|
79
|
+
if a.cmd in (None, 'gui'):
|
|
80
|
+
from .gui import run # tkinter imported only when needed
|
|
81
|
+
run(src=getattr(a, 'src', None), out_dir=getattr(a, 'out_dir', '.'))
|
|
82
|
+
return 0
|
|
83
|
+
if a.cmd == 'generate':
|
|
84
|
+
return _generate(a)
|
|
85
|
+
if a.cmd == 'convert':
|
|
86
|
+
return _convert(a)
|
|
87
|
+
if a.cmd == 'template':
|
|
88
|
+
if a.dst:
|
|
89
|
+
with open(a.dst, 'wt', encoding='utf-8', newline='') as f:
|
|
90
|
+
f.write(default_mdu_text())
|
|
91
|
+
print('Wrote', a.dst)
|
|
92
|
+
else:
|
|
93
|
+
sys.stdout.write(default_mdu_text())
|
|
94
|
+
return 0
|
|
95
|
+
return 1
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
if __name__ == '__main__':
|
|
99
|
+
sys.exit(main())
|