|
1 | 1 | import sys |
| 2 | + |
2 | 3 | import numpy as np |
3 | 4 | from jinja2 import Environment, PackageLoader |
4 | | -from flopy4.mf6 import filters |
5 | | -from flopy4.mf6.spec import blocks_dict |
6 | 5 |
|
| 6 | +from flopy4.mf6 import filters |
| 7 | +from flopy4.mf6.spec import blocks_dict, fields_dict |
7 | 8 |
|
8 | 9 | env = Environment( |
9 | | -loader=PackageLoader("flopy4.mf6"), |
| 10 | + loader=PackageLoader("flopy4.mf6"), |
10 | 11 | trim_blocks=True, |
11 | 12 | lstrip_blocks=True, |
12 | 13 | ) |
13 | | -env.filters["dask_expand"] = filters.dask_expand |
14 | | -env.filters["nparray2string"] = filters.nparray2string |
| 14 | +env.filters["fieldkind"] = filters.fieldkind |
| 15 | +env.filters["fieldvalue"] = filters.fieldvalue |
| 16 | +env.filters["arraydelayed"] = filters.arraydelayed |
| 17 | +env.filters["array2string"] = filters.array2string |
15 | 18 |
|
16 | 19 |
|
17 | 20 | class Writer: |
18 | | - def _write_ascii(self, path) -> None: |
19 | | - # TODO: factor out an ascii writer separately |
20 | | - |
21 | | - block_spec = blocks_dict(type(self)) |
22 | | - blocks = {} |
23 | | - for block_name, block in block_spec.items(): |
24 | | - blocks[block_name] = {} |
25 | | - for field_name, field in block.items(): |
26 | | - if field_name == "data": |
27 | | - continue |
28 | | - blocks[block_name][field_name] = { |
29 | | - "spec": field, |
30 | | - "value": getattr(self, field_name), |
31 | | - } |
| 21 | + # TODO remove type: ignore statements below. |
| 22 | + # but idk how to properly type a mixin class. |
| 23 | + # this one assumes the presence of attributes: |
| 24 | + # - name |
| 25 | + # - path |
| 26 | + # - data |
32 | 27 |
|
| 28 | + def _write_ascii(self) -> None: |
| 29 | + cls = type(self) |
| 30 | + fields = fields_dict(cls) |
| 31 | + blocks = blocks_dict(cls) |
33 | 32 | template = env.get_template("blocks.jinja") |
34 | | - iterator = template.generate(blocks=blocks) |
35 | | - with np.printoptions( |
36 | | - precision=4, linewidth=sys.maxsize, threshold=sys.maxsize |
37 | | - ): |
38 | | - with open(path, "w") as f: |
| 33 | + iterator = template.generate(fields=fields, blocks=blocks, data=self.data) # type: ignore |
| 34 | + # are these printoptions always applicable? |
| 35 | + with np.printoptions(precision=4, linewidth=sys.maxsize, threshold=sys.maxsize): |
| 36 | + # TODO don't hardcode the filename, maybe a filename attribute? |
| 37 | + with open(self.path / self.name, "w") as f: # type: ignore |
39 | 38 | f.writelines(iterator) |
40 | 39 |
|
41 | 40 | def write(self) -> None: |
| 41 | + # TODO: factor out an ascii writer separately |
42 | 42 | self._write_ascii() |
43 | 43 | for child in self.children.values(): # type: ignore |
44 | 44 | child.write() |
0 commit comments