|
1 | | -from flopy4.mf6 import ( # noqa: F401 |
2 | | - gwf, |
3 | | - ims, |
4 | | - simulation, |
5 | | - tdis, |
6 | | -) |
7 | | -from flopy4.mf6.codec import dump |
| 1 | +from json import dump as dump_json |
| 2 | +from json import load as load_json |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from tomli import load as load_toml |
| 6 | +from tomli_w import dump as dump_toml |
| 7 | + |
| 8 | +from flopy4.mf6.codec import dump as dump_mf6 |
| 9 | +from flopy4.mf6.codec import load as load_mf6 |
8 | 10 | from flopy4.mf6.component import Component |
9 | | -from flopy4.mf6.converter import COMPONENT_CONVERTER |
| 11 | +from flopy4.mf6.converter import structure, unstructure |
10 | 12 | from flopy4.uio import DEFAULT_REGISTRY |
11 | 13 |
|
12 | | -# register io methods |
13 | | -# TODO: call format "mf6" or something? since it might include binary files |
14 | | -DEFAULT_REGISTRY.register_writer( |
15 | | - Component, "ascii", lambda c: dump(COMPONENT_CONVERTER.unstructure(c), c.path) |
16 | | -) |
| 14 | + |
| 15 | +def _load_mf6(path: Path) -> Component: |
| 16 | + with open(path, "r") as fp: |
| 17 | + return structure(load_mf6(fp), path) |
| 18 | + |
| 19 | + |
| 20 | +def _load_json(path: Path) -> Component: |
| 21 | + with open(path, "r") as fp: |
| 22 | + return structure(load_json(fp), path) |
| 23 | + |
| 24 | + |
| 25 | +def _load_toml(path: Path) -> Component: |
| 26 | + with open(path, "rb") as fp: |
| 27 | + return structure(load_toml(fp), path) |
| 28 | + |
| 29 | + |
| 30 | +def _write_mf6(component: Component) -> None: |
| 31 | + with open(component.path, "w") as fp: |
| 32 | + dump_mf6(unstructure(component), fp) |
| 33 | + |
| 34 | + |
| 35 | +def _write_json(component: Component) -> None: |
| 36 | + with open(component.path, "w") as fp: |
| 37 | + dump_json(unstructure(component), fp, indent=4) |
| 38 | + |
| 39 | + |
| 40 | +def _write_toml(component: Component) -> None: |
| 41 | + with open(component.path, "wb") as fp: |
| 42 | + dump_toml(unstructure(component), fp) |
| 43 | + |
| 44 | + |
| 45 | +DEFAULT_REGISTRY.register_loader(Component, "mf6", _load_mf6) |
| 46 | +DEFAULT_REGISTRY.register_loader(Component, "json", _load_json) |
| 47 | +DEFAULT_REGISTRY.register_loader(Component, "toml", _load_toml) |
| 48 | +DEFAULT_REGISTRY.register_writer(Component, "mf6", _write_mf6) |
| 49 | +DEFAULT_REGISTRY.register_writer(Component, "json", _write_json) |
| 50 | +DEFAULT_REGISTRY.register_writer(Component, "toml", _write_toml) |
0 commit comments