Coverage for C:\src\imod-python\imod\mf6\write_context.py: 100%
48 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 14:15 +0200
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 14:15 +0200
1from __future__ import annotations
3from copy import deepcopy
4from dataclasses import dataclass
5from os.path import relpath
6from pathlib import Path
7from typing import Optional, Union
10@dataclass
11class WriteContext:
12 """
13 This class is used in the process of writing modflow inputfiles.
14 It is a container for options that are used when writing.
16 Parameters
17 ----------
18 simulation_directory: Path
19 The directory where the .nam file for the modflow simulation will be written
20 use_binary: bool
21 If True, bulk data will be written in a binary format readable by modflow. Regular package input files
22 will still be rendered as text.
23 use_absolute_paths: bool
24 If True, paths in the modlfow inputfiles will be rendered as absoule paths on your system.
25 This makes the modflow input files less portable to other systems but facilitates reading them by Flopy
26 write_directory: Optional[Path] = None
27 The directory where the next outputfile will be written. Users do not need to set this parameter. If not provided
28 it will be set to the simulation_directrory.
29 """
31 def __init__(
32 self,
33 simulation_directory: Path = Path("."),
34 use_binary: bool = False,
35 use_absolute_paths: bool = False,
36 write_directory: Optional[Union[str, Path]] = None,
37 ):
38 self.__simulation_directory = Path(simulation_directory)
39 self.__use_binary = use_binary
40 self.__use_absolute_paths = use_absolute_paths
41 self.__write_directory = (
42 Path(write_directory)
43 if write_directory is not None
44 else self.__simulation_directory
45 )
46 self.__is_partitioned = False
48 def get_formatted_write_directory(self) -> Path:
49 """
50 This method returns a path that is absolute or relative in agreement with the use_absolute_paths setting.
51 This is usefull when the path will be written to a modflow input file. If it is not absolute, it will
52 be relative to the simulation directory, which makes it usable by MF6.
53 """
54 if self.use_absolute_paths:
55 return self.__write_directory
56 return Path(relpath(self.write_directory, self.__simulation_directory))
58 def copy_with_new_write_directory(self, new_write_directory: Path) -> WriteContext:
59 new_context = deepcopy(self)
60 new_context.__write_directory = Path(new_write_directory)
61 return new_context
63 @property
64 def simulation_directory(self) -> Path:
65 return self.__simulation_directory
67 @property
68 def use_binary(self) -> bool:
69 return self.__use_binary
71 @use_binary.setter
72 def use_binary(self, value) -> None:
73 self.__use_binary = value
75 @property
76 def use_absolute_paths(self) -> bool:
77 return self.__use_absolute_paths
79 @property
80 def write_directory(self) -> Path:
81 return self.__write_directory
83 @property
84 def root_directory(self) -> Path:
85 """
86 returns the simulation directory, or nothing, depending on use_absolute_paths; use this to compose paths
87 that are in agreement with the use_absolute_paths setting.
88 """
89 if self.use_absolute_paths:
90 return self.__simulation_directory
91 else:
92 return Path("")
94 @property
95 def is_partitioned(self) -> bool:
96 return self.__is_partitioned
98 @is_partitioned.setter
99 def is_partitioned(self, value: bool) -> None:
100 self.__is_partitioned = value